GuiRadioButton.cpp 11 KB

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