1 /*
2  * ===========================
3  * VDK Visual Development Kit
4  * Version 0.4
5  * October 1998
6  * ===========================
7  *
8  * Copyright (C) 1998, Mario Motta
9  * Developed by Mario Motta <mmotta@guest.net>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  * 02111-1307, USA.
25  *
26 */
27 
28 #include "vdk/radiobtngroup.h"
29 #include "vdk/forms.h"
30 #include <cstring>
31 static int defaultFlag = 0; // stop emit if raised
32 
ToggleEvent(GtkWidget * wid,gpointer gp)33 void VDKRadioButtonGroup::ToggleEvent(GtkWidget *wid, gpointer gp)
34 {
35   g_return_if_fail(wid != NULL);
36   g_return_if_fail(gp != NULL);
37   VDKRadioButton* buttonToggled  =
38     reinterpret_cast<VDKRadioButton*> (gp);
39 
40   VDKRadioButtonGroup* group = buttonToggled->Group();
41   int t = 0;
42   int toggled = -1;
43    if(defaultFlag)
44     {
45       defaultFlag--;
46       return;
47     }
48   RadioButtonListIterator li(group->Buttons);
49   for(;li;li++,t++)
50     {
51       if(li.current() == buttonToggled)
52 	{
53 	toggled = t;
54 	break;
55 	}
56     }
57   if(toggled < 0)
58     return;
59   bool active = GTK_TOGGLE_BUTTON(wid)->active ? true : false;
60   buttonToggled->Checked(active);
61   if(active)
62     {
63       group->Selected(toggled);
64       group->SignalEmit(toggled_signal);
65       group->SignalEmit("toggled");
66     }
67 }
68 /*
69  */
VDKRadioButton(VDKRadioButtonGroup * group,char * label)70 VDKRadioButton::VDKRadioButton( VDKRadioButtonGroup* group,
71 			       char* label):
72   VDKCheckButton(group->Owner(),label),rbgroup(group)
73 {
74   gtk_signal_disconnect(GTK_OBJECT(widget),connectId);
75   gtk_widget_destroy(widget);
76   /*
77   if(! rbgroup->GsGroup())
78     {
79       widget = gtk_radio_button_new_with_label(NULL,label);
80       rbgroup->GsGroup(gtk_radio_button_group(GTK_RADIO_BUTTON(widget)));
81     }
82   else
83     {
84       // GtkWidget* wid  = rbgroup->Buttons[rbgroup->Buttons.size()-1]->Widget();
85       widget = gtk_radio_button_new_with_label(rbgroup->GsGroup(),label);
86       rbgroup->GsGroup(gtk_radio_button_group(GTK_RADIO_BUTTON(widget)));
87     }
88   */
89   widget =
90     gtk_radio_button_new_with_label(
91 				    rbgroup->GsGroup() ?
92 				    rbgroup->GsGroup() : NULL,
93 				    label);
94   rbgroup->GsGroup(gtk_radio_button_group(GTK_RADIO_BUTTON(widget)));
95 
96   gtk_signal_connect(GTK_OBJECT(widget),"toggled",
97 		     GTK_SIGNAL_FUNC(VDKRadioButtonGroup::ToggleEvent),
98 		     (gpointer) this);
99   rbgroup->GBox()->Add(this);
100   rbgroup->Buttons.add(this);
101 }
102 
103 
104 /*
105  */
VDKRadioButton(VDKForm * owner,char * label)106 VDKRadioButton::VDKRadioButton(VDKForm* owner,char* label):
107   VDKCheckButton(owner,label),rbgroup(NULL)
108 {
109 }
110 
111 /*
112  */
113 void
Add(VDKObject * obj,int,int,int,int)114 VDKRadioButtonGroup::Add(VDKObject* obj, int , int , int , int )
115 {
116   char* label;
117   VDKRadioButton* radiobutton = dynamic_cast<VDKRadioButton*>(obj);
118   g_return_if_fail(radiobutton != NULL);
119   label = new char[std::strlen(((char*)radiobutton->Caption))+1];
120   std::strcpy(label,(char*)radiobutton->Caption);
121   gtk_signal_disconnect(GTK_OBJECT(radiobutton->widget),
122 			radiobutton->connectId);
123   gtk_widget_destroy(radiobutton->widget);
124   radiobutton->widget =
125     gtk_radio_button_new_with_label( gs_group ? gs_group : NULL,label);
126   gs_group = gtk_radio_button_group(GTK_RADIO_BUTTON(radiobutton->widget));
127   gtk_signal_connect(GTK_OBJECT(radiobutton->widget),"toggled",
128 		     GTK_SIGNAL_FUNC(VDKRadioButtonGroup::ToggleEvent),
129 		     (gpointer) radiobutton);
130   box->Add(radiobutton);
131   Buttons.add(radiobutton);
132   radiobutton->Group(this);
133   delete label;
134 }
135 /*
136  */
VDKRadioButtonGroup(VDKForm * owner,int mode)137 VDKRadioButtonGroup::VDKRadioButtonGroup(VDKForm* owner,
138 					 int mode):
139   VDKObjectContainer(owner),
140   Selected(
141 	   "Selected",
142 	   this,
143 	   0,
144 	   &VDKRadioButtonGroup::SetSelected
145 	   ),
146   ButtonList("ButtonList",this,NULL)
147 {
148 box = new VDKEventBox(owner,mode);
149 widget = box->Widget();
150 owner->Objects().add(box);
151 gs_group = NULL;
152 ButtonList(&Buttons);
153 }
154 /*
155  */
156 void
RemoveButton(VDKRadioButton * button)157 VDKRadioButtonGroup::RemoveButton(VDKRadioButton* button)
158 {
159 if(Buttons.remove(button))
160   {
161     button->Visible = false;
162     if(Buttons.size() <= 0)
163       gs_group = NULL;
164     if (GTK_WIDGET_VISIBLE (widget))
165       gtk_widget_queue_resize (GTK_WIDGET (widget));
166   }
167 }
168 /*
169  */
SetDefault(int bn)170 void VDKRadioButtonGroup::SetDefault(int bn)
171 {
172 if(bn >= 0 && bn < Buttons.size())
173   {
174     defaultFlag+=2; // deny "toggled" signal processing
175     GtkWidget* wid = Buttons[bn]->Widget();
176     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(wid),TRUE);
177     Selected(bn);
178   }
179 }
180 
SetSelected(int index)181 void VDKRadioButtonGroup::SetSelected(int index)
182 {
183   if(index >= 0 && index < Buttons.size())
184     Buttons[index]->Checked = true;
185 }
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196