]>
Commit | Line | Data |
---|---|---|
50a14534 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | ||
22 | #include <gtk/gtk.h> | |
5d1e579d | 23 | #include <glib/gi18n.h> |
50a14534 EB |
24 | |
25 | #include "vikradiogroup.h" | |
26 | ||
27 | static GObjectClass *parent_class; | |
28 | ||
29 | static void radio_group_finalize ( GObject *gob ); | |
30 | static void radio_group_class_init ( VikRadioGroupClass *klass ); | |
31 | ||
32 | struct _VikRadioGroup { | |
33 | GtkVBox parent; | |
34 | GSList *radios; | |
35 | guint options_count; | |
36 | }; | |
37 | ||
38 | GType vik_radio_group_get_type (void) | |
39 | { | |
40 | static GType vrg_type = 0; | |
41 | ||
42 | if (!vrg_type) | |
43 | { | |
44 | static const GTypeInfo vrg_info = | |
45 | { | |
46 | sizeof (VikRadioGroupClass), | |
47 | NULL, /* base_init */ | |
48 | NULL, /* base_finalize */ | |
49 | (GClassInitFunc) radio_group_class_init, /* class init */ | |
50 | NULL, /* class_finalize */ | |
51 | NULL, /* class_data */ | |
52 | sizeof (VikRadioGroup), | |
53 | 0, | |
54 | NULL /* instance init */ | |
55 | }; | |
56 | vrg_type = g_type_register_static ( GTK_TYPE_VBOX, "VikRadioGroup", &vrg_info, 0 ); | |
57 | } | |
58 | ||
59 | return vrg_type; | |
60 | } | |
61 | ||
62 | static void radio_group_class_init ( VikRadioGroupClass *klass ) | |
63 | { | |
64 | /* Destructor */ | |
65 | GObjectClass *object_class; | |
66 | ||
67 | object_class = G_OBJECT_CLASS (klass); | |
68 | ||
69 | object_class->finalize = radio_group_finalize; | |
70 | ||
71 | parent_class = g_type_class_peek_parent (klass); | |
72 | } | |
73 | ||
cdcaf41c | 74 | GtkWidget *vik_radio_group_new ( GList *options ) |
50a14534 EB |
75 | { |
76 | VikRadioGroup *vrg; | |
77 | GtkWidget *t; | |
cdcaf41c QT |
78 | gchar *label; |
79 | GList *option = options; | |
50a14534 | 80 | |
cdcaf41c | 81 | if ( ! options ) |
50a14534 EB |
82 | return NULL; |
83 | ||
84 | vrg = VIK_RADIO_GROUP ( g_object_new ( VIK_RADIO_GROUP_TYPE, NULL ) ); | |
85 | ||
cdcaf41c | 86 | label = g_list_nth_data(options, 0); |
9a5ef106 | 87 | t = gtk_radio_button_new_with_label ( NULL, _(label) ); |
50a14534 EB |
88 | gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(t), TRUE ); |
89 | gtk_box_pack_start ( GTK_BOX(vrg), t, FALSE, FALSE, 0 ); | |
90 | ||
91 | vrg->radios = g_slist_append ( NULL, t ); | |
92 | vrg->options_count = 1; | |
93 | ||
cdcaf41c | 94 | while ( ( option = g_list_next(option) ) != NULL ) |
50a14534 | 95 | { |
cdcaf41c | 96 | label = option->data; |
5d1e579d | 97 | t = gtk_radio_button_new_with_label_from_widget ( |
9a5ef106 | 98 | GTK_RADIO_BUTTON(vrg->radios->data), _(label)); |
cdcaf41c | 99 | vrg->radios = g_slist_append( vrg->radios, t ); |
50a14534 EB |
100 | gtk_box_pack_start ( GTK_BOX(vrg), GTK_WIDGET(t), FALSE, FALSE, 0 ); |
101 | vrg->options_count++; | |
102 | } | |
103 | ||
104 | return GTK_WIDGET(vrg); | |
105 | } | |
106 | ||
8c721f83 EB |
107 | GtkWidget *vik_radio_group_new_static ( const gchar **options ) |
108 | { | |
109 | VikRadioGroup *vrg; | |
110 | GtkWidget *t; | |
111 | ||
112 | if ( ! *options ) | |
113 | return NULL; | |
114 | ||
115 | vrg = VIK_RADIO_GROUP ( g_object_new ( VIK_RADIO_GROUP_TYPE, NULL ) ); | |
116 | ||
9a5ef106 | 117 | t = gtk_radio_button_new_with_label ( NULL, _(options[0]) ); |
8c721f83 EB |
118 | gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(t), TRUE ); |
119 | gtk_box_pack_start ( GTK_BOX(vrg), t, FALSE, FALSE, 0 ); | |
120 | ||
121 | vrg->radios = g_slist_append ( NULL, t ); | |
122 | vrg->options_count = 1; | |
123 | ||
124 | for ( options++ ; *options ; options++ ) | |
125 | { | |
9a5ef106 | 126 | t = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(vrg->radios->data), _(*options) ); |
7960e897 | 127 | vrg->radios = g_slist_append( vrg->radios, t ); |
8c721f83 EB |
128 | gtk_box_pack_start ( GTK_BOX(vrg), GTK_WIDGET(t), FALSE, FALSE, 0 ); |
129 | vrg->options_count++; | |
130 | } | |
131 | ||
132 | return GTK_WIDGET(vrg); | |
133 | } | |
134 | ||
135 | ||
50a14534 EB |
136 | void vik_radio_group_set_selected ( VikRadioGroup *vrg, guint8 i ) |
137 | { | |
138 | if ( i < vrg->options_count ) | |
139 | gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(g_slist_nth_data(vrg->radios,i)), TRUE ); | |
140 | } | |
141 | ||
142 | guint8 vik_radio_group_get_selected ( VikRadioGroup *vrg ) | |
143 | { | |
144 | guint8 i = 0; | |
145 | GSList *iter = vrg->radios; | |
146 | while ( iter ) | |
147 | { | |
148 | if ( gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(iter->data) ) ) | |
149 | return i; | |
150 | iter = iter->next; | |
151 | i++; | |
152 | } | |
153 | return 0; | |
154 | } | |
155 | ||
156 | static void radio_group_finalize ( GObject *gob ) | |
157 | { | |
158 | VikRadioGroup *vrg = VIK_RADIO_GROUP ( gob ); | |
159 | if ( vrg->radios ) | |
160 | g_slist_free ( vrg->radios ); | |
161 | G_OBJECT_CLASS(parent_class)->finalize(gob); | |
162 | } | |
163 |