mp3splt-gtk
radio_helper.c
Go to the documentation of this file.
1 /**********************************************************
2  *
3  * mp3splt-gtk -- utility based on mp3splt,
4  * for mp3/ogg splitting without decoding
5  *
6  * Copyright: (C) 2005-2013 Alexandru Munteanu
7  * Contact: m@ioalex.net
8  *
9  * http://mp3splt.sourceforge.net/
10  *
11  *********************************************************/
12 
13 /**********************************************************
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28  * USA.
29  *
30  *********************************************************/
31 
32 /*!********************************************************
33  * \file
34  *
35  * this file contains the code for the radio button helpers.
36  ********************************************************/
37 
38 #include "radio_helper.h"
39 
40 static GtkRadioButton *rh_get_radio_from_value(GtkWidget *radio_button, gint value);
41 
42 GtkWidget *rh_append_radio_to_vbox(GtkWidget *radio_button, const gchar *text,
43  gint value, void (*callback)(GtkToggleButton *, gpointer), gpointer callback_data,
44  GtkWidget *vbox)
45 {
46  GtkWidget *new_radio_button =
47  gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_button), text);
48  gtk_box_pack_start(GTK_BOX(vbox), new_radio_button, FALSE, FALSE, 0);
49 
50  if (callback)
51  {
52  g_signal_connect(GTK_TOGGLE_BUTTON(new_radio_button), "toggled", G_CALLBACK(callback),
53  callback_data);
54  }
55 
56  g_object_set_data(G_OBJECT(new_radio_button), "value", GINT_TO_POINTER(value));
57 
58  return new_radio_button;
59 }
60 
61 gint rh_get_active_value(GtkWidget *radio_button)
62 {
63  gint active_value = -1;
64  GSList *radio_button_list = gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio_button));
65 
66  gint i = 0;
67  for(i = 0; i < g_slist_length(radio_button_list);i++)
68  {
69  GtkRadioButton *current_radio = (GtkRadioButton *) g_slist_nth_data(radio_button_list, i);
70  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(current_radio)))
71  {
72  active_value = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(current_radio), "value"));
73  break;
74  }
75  }
76 
77  return active_value;
78 }
79 
80 void rh_set_radio_value(GtkWidget *radio_button, gint key_value, gboolean value)
81 {
82  GtkRadioButton *radio = rh_get_radio_from_value(radio_button, key_value);
83  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio), value);
84 }
85 
86 static GtkRadioButton *rh_get_radio_from_value(GtkWidget *radio_button, gint value)
87 {
88  GSList *radio_button_list = gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio_button));
89 
90  gint i = 0;
91  gint list_length = g_slist_length(radio_button_list);
92  for(i = 0; i < list_length;i++)
93  {
94  GtkRadioButton *current_radio = (GtkRadioButton *) g_slist_nth_data(radio_button_list, i);
95  gint current_value = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(current_radio), "value"));
96  if (current_value == value)
97  {
98  return current_radio;
99  break;
100  }
101  }
102 
103  return NULL;
104 }
105