]> git.street.me.uk Git - andy/viking.git/blob - src/vikradiogroup.c
Add internal track right click menu variable.
[andy/viking.git] / src / vikradiogroup.c
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>
23
24 #include "vikradiogroup.h"
25
26 static GObjectClass *parent_class;
27
28 static void radio_group_finalize ( GObject *gob );
29 static void radio_group_class_init ( VikRadioGroupClass *klass );
30
31 struct _VikRadioGroup {
32   GtkVBox parent;
33   GSList *radios;
34   guint options_count;
35 };
36
37 GType vik_radio_group_get_type (void)
38 {
39   static GType vrg_type = 0;
40
41   if (!vrg_type)
42   {
43     static const GTypeInfo vrg_info = 
44     {
45       sizeof (VikRadioGroupClass),
46       NULL, /* base_init */
47       NULL, /* base_finalize */
48       (GClassInitFunc) radio_group_class_init, /* class init */
49       NULL, /* class_finalize */
50       NULL, /* class_data */
51       sizeof (VikRadioGroup),
52       0,
53       NULL /* instance init */
54     };
55     vrg_type = g_type_register_static ( GTK_TYPE_VBOX, "VikRadioGroup", &vrg_info, 0 );
56   }
57
58   return vrg_type;
59 }
60
61 static void radio_group_class_init ( VikRadioGroupClass *klass )
62 {
63   /* Destructor */
64   GObjectClass *object_class;
65
66   object_class = G_OBJECT_CLASS (klass);
67
68   object_class->finalize = radio_group_finalize;
69
70   parent_class = g_type_class_peek_parent (klass);
71 }
72
73 GtkWidget *vik_radio_group_new ( GList *options )
74 {
75   VikRadioGroup *vrg;
76   GtkWidget *t;
77   gchar *label;
78   GList *option = options;
79
80   if ( ! options )
81     return NULL;
82
83   vrg = VIK_RADIO_GROUP ( g_object_new ( VIK_RADIO_GROUP_TYPE, NULL ) );
84
85   label = g_list_nth_data(options, 0);
86   t = gtk_radio_button_new_with_label ( NULL, label );
87   gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(t), TRUE );
88   gtk_box_pack_start ( GTK_BOX(vrg), t, FALSE, FALSE, 0 );
89
90   vrg->radios = g_slist_append ( NULL, t );
91   vrg->options_count = 1;
92
93   while ( ( option = g_list_next(option) ) != NULL )
94   {
95     label = option->data;
96     t = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(vrg->radios->data), label );
97     vrg->radios = g_slist_append( vrg->radios, t );
98     gtk_box_pack_start ( GTK_BOX(vrg), GTK_WIDGET(t), FALSE, FALSE, 0 );
99     vrg->options_count++;
100   }
101
102   return GTK_WIDGET(vrg);
103 }
104
105 GtkWidget *vik_radio_group_new_static ( const gchar **options )
106 {
107   VikRadioGroup *vrg;
108   GtkWidget *t;
109
110   if ( ! *options )
111     return NULL;
112
113   vrg = VIK_RADIO_GROUP ( g_object_new ( VIK_RADIO_GROUP_TYPE, NULL ) );
114
115   t = gtk_radio_button_new_with_label ( NULL, options[0] );
116   gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(t), TRUE );
117   gtk_box_pack_start ( GTK_BOX(vrg), t, FALSE, FALSE, 0 );
118
119   vrg->radios = g_slist_append ( NULL, t );
120   vrg->options_count = 1;
121
122   for ( options++ ; *options ; options++ )
123   {
124     t = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(vrg->radios->data), *options );
125     vrg->radios = g_slist_append( vrg->radios, t );
126     gtk_box_pack_start ( GTK_BOX(vrg), GTK_WIDGET(t), FALSE, FALSE, 0 );
127     vrg->options_count++;
128   }
129
130   return GTK_WIDGET(vrg);
131 }
132
133
134 void vik_radio_group_set_selected ( VikRadioGroup *vrg, guint8 i )
135 {
136   if ( i < vrg->options_count )
137     gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(g_slist_nth_data(vrg->radios,i)), TRUE );
138 }
139
140 guint8 vik_radio_group_get_selected ( VikRadioGroup *vrg )
141 {
142   guint8 i = 0;
143   GSList *iter = vrg->radios;
144   while ( iter )
145   {
146     if ( gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(iter->data) ) )
147       return i;
148     iter = iter->next;
149     i++;
150   }
151   return 0;
152 }
153
154 static void radio_group_finalize ( GObject *gob )
155 {
156   VikRadioGroup *vrg = VIK_RADIO_GROUP ( gob );
157   if ( vrg->radios )
158     g_slist_free ( vrg->radios );
159   G_OBJECT_CLASS(parent_class)->finalize(gob);
160 }
161