Debian Codegraph @

sys/GuiRadioButton.cpp

raw symbols SCIP
1 /* GuiRadioButton.cpp
2 *
3 * Copyright (C) 1993-2018,2024 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20 * pb 2007/12/26 extracted from Motif
21 * sdk 2007/12/27 gtk
22 * pb 2010/05/15 prevented procreation of valueChanged events in GuiRadioButton_setValue
23 * pb 2010/06/14 HandleControlClick
24 * pb 2010/11/28 removed Motif
25 * pb 2011/04/06 C++
26 */
27
28 #include "GuiP.h"
29
30 Thing_implement (GuiRadioButton, GuiControl, 0);
31
32 #if motif
33 #define iam_radiobutton \
34 Melder_assert (widget -> widgetClass == xmToggleButtonWidgetClass); \
35 GuiRadioButton me = (GuiRadioButton) widget -> userData
36 #else
37 #define iam_radiobutton \
38 GuiRadioButton me = (GuiRadioButton) _GuiObject_getUserData (widget)
39 #endif
40
41 static int _GuiRadioButton_getPosition (GuiRadioButton me) {
42 int position = 1;
43 while (my d_previous) {
44 position ++;
45 me = my d_previous;
46 }
47 return position;
48 }
49
50 #if gtk
51 static void _GuiGtkRadioButton_destroyCallback (GuiObject widget, gpointer void_me) {
52 (void) widget;
53 iam (GuiRadioButton);
54 forget (me);
55 }
56 static void _GuiGtkRadioButton_handleToggle (GuiObject widget, gpointer void_me) {
57 iam (GuiRadioButton);
58 trace (U"enter");
59 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) {
60 trace (U"on");
61 if (my d_valueChangedCallback && ! my d_blockValueChangedCallbacks) {
62 struct structGuiRadioButtonEvent event { me };
63 event. position = _GuiRadioButton_getPosition (me);
64 my d_valueChangedCallback (my d_valueChangedBoss, & event);
65 }
66 }
67 }
68 #elif motif
69 void _GuiWinRadioButton_destroy (GuiObject widget) {
70 iam_radiobutton;
71 _GuiNativeControl_destroy (widget);
72 forget (me); // NOTE: my widget is not destroyed here
73 }
74 void _GuiWinRadioButton_handleClick (GuiObject widget) {
75 iam_radiobutton;
76 Button_SetCheck (widget -> window, BST_CHECKED);
77 /*
78 * Deselect the sister buttons.
79 */
80 for (GuiRadioButton sibling = my d_previous; sibling != nullptr; sibling = sibling -> d_previous) {
81 Button_SetCheck (sibling -> d_widget -> window, BST_UNCHECKED);
82 }
83 for (GuiRadioButton sibling = my d_next; sibling != nullptr; sibling = sibling -> d_next) {
84 Button_SetCheck (sibling -> d_widget -> window, BST_UNCHECKED);
85 }
86 if (my d_valueChangedCallback) {
87 struct structGuiRadioButtonEvent event { me };
88 event. position = _GuiRadioButton_getPosition (me);
89 my d_valueChangedCallback (my d_valueChangedBoss, & event);
90 }
91 }
92 #elif cocoa
93 @implementation GuiCocoaRadioButton {
94 GuiRadioButton d_userData;
95 }
96 - (void) dealloc { // override
97 GuiRadioButton me = d_userData;
98 forget (me);
99 trace (U"deleting a radio button");
100 [super dealloc];
101 }
102 - (GuiThing) getUserData {
103 return d_userData;
104 }
105 - (void) setUserData: (GuiThing) userData {
106 Melder_assert (userData == nullptr || Thing_isa (userData, classGuiRadioButton));
107 d_userData = static_cast <GuiRadioButton> (userData);
108 }
109 - (void) _guiCocoaRadioButton_activateCallback: (id) widget {
110 trace (U"enter");
111 Melder_assert (self == widget); // sender (widget) and receiver (self) happen to be the same object
112 GuiRadioButton me = d_userData;
113 /*
114 * Deselect the sister buttons.
115 */
116 for (GuiRadioButton sibling = my d_previous; sibling != nullptr; sibling = sibling -> d_previous) {
117 [sibling -> d_cocoaRadioButton setState: NSOffState];
118 }
119 for (GuiRadioButton sibling = my d_next; sibling != nullptr; sibling = sibling -> d_next) {
120 [sibling -> d_cocoaRadioButton setState: NSOffState];
121 }
122 if (my d_valueChangedCallback) {
123 Melder_assert (! my d_blockValueChangedCallbacks);
124 struct structGuiRadioButtonEvent event { me };
125 event. position = _GuiRadioButton_getPosition (me);
126 my d_valueChangedCallback (my d_valueChangedBoss, & event);
127 }
128 }
129 @end
130 #endif
131
132 static GuiRadioButton latestRadioButton = nullptr;
133
134 void GuiRadioGroup_begin () {
135 latestRadioButton = nullptr;
136 }
137
138 void GuiRadioGroup_end () {
139 latestRadioButton = nullptr;
140 }
141
142 GuiRadioButton GuiRadioButton_create (GuiForm parent, int left, int right, int top, int bottom,
143 conststring32 buttonText, GuiRadioButtonCallback valueChangedCallback, Thing valueChangedBoss, uint32 flags)
144 {
145 trace (U"begin: text %", buttonText);
146 autoGuiRadioButton me = Thing_new (GuiRadioButton);
147 my d_shell = parent -> d_shell;
148 my d_parent = parent;
149 my d_valueChangedCallback = valueChangedCallback;
150 my d_valueChangedBoss = valueChangedBoss;
151 my d_previous = latestRadioButton;
152 my d_next = nullptr;
153 #if gtk
154 my d_widget = gtk_radio_button_new_with_label_from_widget (latestRadioButton ? GTK_RADIO_BUTTON (latestRadioButton -> d_widget) : nullptr, Melder_peek32to8 (buttonText));
155 _GuiObject_setUserData (my d_widget, me.get());
156 my v_positionInForm (my d_widget, left, right, top, bottom, parent);
157 if (flags & GuiRadioButton_SET) {
158 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (my d_widget), true);
159 } else {
160 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (my d_widget), false);
161 }
162 g_signal_connect (G_OBJECT (my d_widget), "destroy", G_CALLBACK (_GuiGtkRadioButton_destroyCallback), me.get());
163 g_signal_connect (GTK_TOGGLE_BUTTON (my d_widget), "toggled", G_CALLBACK (_GuiGtkRadioButton_handleToggle), me.get());
164 #elif motif
165 my d_widget = _Gui_initializeWidget (xmToggleButtonWidgetClass, parent -> d_widget, buttonText);
166 _GuiObject_setUserData (my d_widget, me.get());
167 my d_widget -> isRadioButton = true;
168 my d_widget -> window = CreateWindow (L"button", Melder_peek32toW (_GuiWin_expandAmpersands (buttonText)),
169 WS_CHILD
170 | ( my d_widget -> parent -> radioBehavior ? BS_AUTORADIOBUTTON : BS_RADIOBUTTON )
171 | WS_CLIPSIBLINGS,
172 my d_widget -> x, my d_widget -> y, my d_widget -> width, my d_widget -> height,
173 my d_widget -> parent -> window, (HMENU) 1, theGui.instance, nullptr);
174 SetWindowLongPtr (my d_widget -> window, GWLP_USERDATA, (LONG_PTR) my d_widget);
175 SetWindowFont (my d_widget -> window, theWinGuiNormalLabelFont (), false);
176 my v_positionInForm (my d_widget, left, right, top, bottom, parent);
177 if (flags & GuiRadioButton_SET) {
178 Button_SetCheck (my d_widget -> window, BST_CHECKED);
179 }
180 #elif cocoa
181 my d_cocoaRadioButton = [[GuiCocoaRadioButton alloc] init];
182 my d_widget = my d_cocoaRadioButton;
183 my v_positionInForm (my d_widget, left, right, top, bottom, parent);
184 [my d_cocoaRadioButton setUserData: me.get()];
185 [my d_cocoaRadioButton setButtonType: NSRadioButton];
186 if (true) {
187 /*
188 HACK:
189 The button's state will be under our control,
190 instead of automatically changing under the user's clicks on the neighbours.
191 Make the button therefore behave as a switch button but look as a radio button.
192 */
193 NSImage *image = [my d_cocoaRadioButton image], *alternateImage = [my d_cocoaRadioButton alternateImage];
194 [my d_cocoaRadioButton setButtonType: NSSwitchButton];
195 [my d_cocoaRadioButton setImage: image];
196 [my d_cocoaRadioButton setAlternateImage: alternateImage];
197 }
198 [my d_cocoaRadioButton setTitle: (NSString *) Melder_peek32toCfstring (buttonText)];
199 if (flags & GuiCheckButton_SET) {
200 [my d_cocoaRadioButton setState: NSOnState];
201 }
202 [my d_cocoaRadioButton setTarget: my d_cocoaRadioButton];
203 [my d_cocoaRadioButton setAction: @selector (_guiCocoaRadioButton_activateCallback:)];
204 #elif cocoa
205 /*
206 An alternate way to implement the single-button hack.
207 */
208 NSRect matrixRect = NSMakeRect (20.0, 20.0, 125.0, 125.0);
209 my d_cocoaRadioButton = [[GuiCocoaRadioButton alloc] initWithFrame:matrixRect];
210 [my d_cocoaRadioButton setUserData: me.get()];
211 [my d_cocoaRadioButton setButtonType: NSRadioButton];
212 [my d_cocoaRadioButton setTitle: (NSString *) Melder_peek32toCfstring (buttonText)];
213 NSMatrix *radioMatrix = [[NSMatrix alloc] initWithFrame: matrixRect mode: NSRadioModeMatrix
214 prototype: (NSCell *) [my d_cocoaRadioButton cell] numberOfRows: 1 numberOfColumns: 1];
215 my d_widget = (GuiObject) radioMatrix; //my d_cocoaRadioButton;
216 my v_positionInForm (my d_widget, left, right, top, bottom, parent);
217 [radioMatrix addSubview: my d_cocoaRadioButton];
218 [my d_cocoaRadioButton setUserData: me.get()];
219 [my d_cocoaRadioButton setButtonType: NSRadioButton];
220 [my d_cocoaRadioButton setTitle: (NSString *) Melder_peek32toCfstring (buttonText)];
221 if (flags & GuiCheckButton_SET) {
222 [my d_cocoaRadioButton setState: NSOnState];
223 }
224 [my d_cocoaRadioButton setTarget: my d_cocoaRadioButton];
225 [my d_cocoaRadioButton setAction: @selector (_guiCocoaRadioButton_activateCallback:)];
226 #endif
227 if (flags & GuiRadioButton_INSENSITIVE) {
228 GuiThing_setSensitive (me.get(), false);
229 }
230 if (my d_previous) {
231 Melder_assert (my d_previous);
232 Melder_assert (my d_previous -> classInfo == classGuiRadioButton);
233 my d_previous -> d_next = me.get();
234 }
235 latestRadioButton = me.get();
236 trace (U"end");
237 return me.releaseToAmbiguousOwner();
238 }
239
240 GuiRadioButton GuiRadioButton_createShown (GuiForm parent, int left, int right, int top, int bottom,
241 conststring32 buttonText, GuiRadioButtonCallback valueChangedCallback, Thing valueChangedBoss, uint32 flags)
242 {
243 GuiRadioButton me = GuiRadioButton_create (parent, left, right, top, bottom, buttonText, valueChangedCallback, valueChangedBoss, flags);
244 GuiThing_show (me);
245 return me;
246 }
247
248 bool GuiRadioButton_getValue (GuiRadioButton me) {
249 bool value = false;
250 #if gtk
251 value = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (my d_widget)); // gtk_check_button inherits from gtk_toggle_button
252 #elif motif
253 value = (Button_GetState (my d_widget -> window) & 0x0003) == BST_CHECKED;
254 #elif cocoa
255 value = [my d_cocoaRadioButton state] == NSOnState;
256 #endif
257 return value;
258 }
259
260 void GuiRadioButton_set (GuiRadioButton me) {
261 trace (U"enter");
262 GuiControlBlockValueChangedCallbacks block (me); // the value should be set without calling the valueChanged callback (crucial on GTK)
263 #if gtk
264 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (my d_widget), true);
265 #elif motif
266 Button_SetCheck (my d_widget -> window, BST_CHECKED);
267 /*
268 * Deselect the sister buttons.
269 */
270 for (GuiRadioButton sibling = my d_previous; sibling != nullptr; sibling = sibling -> d_previous) {
271 Button_SetCheck (sibling -> d_widget -> window, BST_UNCHECKED);
272 }
273 for (GuiRadioButton sibling = my d_next; sibling != nullptr; sibling = sibling -> d_next) {
274 Button_SetCheck (sibling -> d_widget -> window, BST_UNCHECKED);
275 }
276 #elif cocoa
277 [my d_cocoaRadioButton setState: NSOnState];
278 /*
279 * Deselect the sister buttons.
280 */
281 for (GuiRadioButton sibling = my d_previous; sibling != nullptr; sibling = sibling -> d_previous) {
282 [sibling -> d_cocoaRadioButton setState: NSOffState];
283 }
284 for (GuiRadioButton sibling = my d_next; sibling != nullptr; sibling = sibling -> d_next) {
285 [sibling -> d_cocoaRadioButton setState: NSOffState];
286 }
287 #endif
288 trace (U"exit");
289 }
290
291 /* End of file GuiRadioButton.cpp */