GuiCheckButton.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* GuiCheckButton.cpp
  2. *
  3. * Copyright (C) 1993-2012,2013,2014,2015,2016,2017 Paul Boersma,
  4. * 2007-2008 Stefan de Konink, 2010 Franz Brausse, 2013 Tom Naughton
  5. *
  6. * This code 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 (at
  9. * your option) any later version.
  10. *
  11. * This code is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the 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 work. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "GuiP.h"
  20. Thing_implement (GuiCheckButton, GuiControl, 0);
  21. #if motif
  22. #define iam_checkbutton \
  23. Melder_assert (widget -> widgetClass == xmToggleButtonWidgetClass); \
  24. GuiCheckButton me = (GuiCheckButton) widget -> userData
  25. #else
  26. #define iam_checkbutton \
  27. GuiCheckButton me = (GuiCheckButton) _GuiObject_getUserData (widget)
  28. #endif
  29. #if gtk
  30. static void _GuiGtkCheckButton_destroyCallback (GuiObject widget, gpointer void_me) {
  31. (void) widget;
  32. iam (GuiCheckButton);
  33. forget (me);
  34. }
  35. static void _GuiGtkCheckButton_valueChangedCallback (GuiObject widget, gpointer void_me) {
  36. iam (GuiCheckButton);
  37. if (my d_valueChangedCallback && ! my d_blockValueChangedCallbacks) {
  38. struct structGuiCheckButtonEvent event { me };
  39. my d_valueChangedCallback (my d_valueChangedBoss, & event);
  40. }
  41. }
  42. #elif motif
  43. void _GuiWinCheckButton_destroy (GuiObject widget) {
  44. iam_checkbutton;
  45. _GuiNativeControl_destroy (widget);
  46. forget (me); // NOTE: my widget is not destroyed here
  47. }
  48. void _GuiWinCheckButton_handleClick (GuiObject widget) {
  49. iam_checkbutton;
  50. if (my d_valueChangedCallback) {
  51. struct structGuiCheckButtonEvent event { me };
  52. my d_valueChangedCallback (my d_valueChangedBoss, & event);
  53. }
  54. }
  55. #elif cocoa
  56. @implementation GuiCocoaCheckButton {
  57. GuiCheckButton d_userData;
  58. }
  59. - (void) dealloc { // override
  60. GuiCheckButton me = d_userData;
  61. forget (me);
  62. trace (U"deleting a check button");
  63. [super dealloc];
  64. }
  65. - (GuiThing) getUserData {
  66. return d_userData;
  67. }
  68. - (void) setUserData: (GuiThing) userData {
  69. Melder_assert (userData == nullptr || Thing_isa (userData, classGuiCheckButton));
  70. d_userData = static_cast <GuiCheckButton> (userData);
  71. }
  72. - (void) _guiCocoaButton_activateCallback: (id) widget {
  73. Melder_assert (self == widget); // sender (widget) and receiver (self) happen to be the same object
  74. GuiCheckButton me = d_userData;
  75. if (my d_valueChangedCallback) {
  76. Melder_assert (! my d_blockValueChangedCallbacks);
  77. struct structGuiCheckButtonEvent event { me };
  78. my d_valueChangedCallback (my d_valueChangedBoss, & event);
  79. }
  80. }
  81. @end
  82. #endif
  83. GuiCheckButton GuiCheckButton_create (GuiForm parent, int left, int right, int top, int bottom,
  84. conststring32 buttonText, GuiCheckButton_ValueChangedCallback valueChangedCallback, Thing valueChangedBoss, uint32 flags)
  85. {
  86. autoGuiCheckButton me = Thing_new (GuiCheckButton);
  87. my d_shell = parent -> d_shell;
  88. my d_parent = parent;
  89. my d_valueChangedCallback = valueChangedCallback;
  90. my d_valueChangedBoss = valueChangedBoss;
  91. #if gtk
  92. my d_widget = gtk_check_button_new_with_label (Melder_peek32to8 (buttonText));
  93. _GuiObject_setUserData (my d_widget, me.get());
  94. my v_positionInForm (my d_widget, left, right, top, bottom, parent);
  95. gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (my d_widget), (flags & GuiCheckButton_SET) != 0);
  96. if (flags & GuiCheckButton_INSENSITIVE) {
  97. GuiThing_setSensitive (me.get(), false);
  98. }
  99. g_signal_connect (G_OBJECT (my d_widget), "destroy", G_CALLBACK (_GuiGtkCheckButton_destroyCallback), me.get());
  100. g_signal_connect (GTK_TOGGLE_BUTTON (my d_widget), "toggled", G_CALLBACK (_GuiGtkCheckButton_valueChangedCallback), me.get());
  101. #elif motif
  102. my d_widget = _Gui_initializeWidget (xmToggleButtonWidgetClass, parent -> d_widget, buttonText);
  103. _GuiObject_setUserData (my d_widget, me.get());
  104. my d_widget -> isRadioButton = false;
  105. my d_widget -> window = CreateWindow (L"button", Melder_peek32toW (_GuiWin_expandAmpersands (buttonText)),
  106. WS_CHILD | BS_AUTOCHECKBOX | WS_CLIPSIBLINGS,
  107. my d_widget -> x, my d_widget -> y, my d_widget -> width, my d_widget -> height,
  108. my d_widget -> parent -> window, (HMENU) 1, theGui.instance, nullptr);
  109. SetWindowLongPtr (my d_widget -> window, GWLP_USERDATA, (LONG_PTR) my d_widget);
  110. SetWindowFont (my d_widget -> window, GetStockFont (ANSI_VAR_FONT), false);
  111. my v_positionInForm (my d_widget, left, right, top, bottom, parent);
  112. if (flags & GuiCheckButton_SET) {
  113. Button_SetCheck (my d_widget -> window, BST_CHECKED);
  114. }
  115. if (flags & GuiCheckButton_INSENSITIVE) {
  116. GuiThing_setSensitive (me.get(), false);
  117. }
  118. #elif cocoa
  119. GuiCocoaCheckButton *checkButton = [[GuiCocoaCheckButton alloc] init];
  120. my d_widget = (GuiObject) checkButton;
  121. my v_positionInForm (my d_widget, left, right, top, bottom, parent);
  122. [checkButton setUserData: me.get()];
  123. [checkButton setButtonType: NSSwitchButton];
  124. [checkButton setTitle: (NSString *) Melder_peek32toCfstring (buttonText)];
  125. [checkButton setTarget: checkButton];
  126. [checkButton setAction: @selector (_guiCocoaButton_activateCallback:)];
  127. if (flags & GuiCheckButton_SET) {
  128. [checkButton setState: NSOnState];
  129. }
  130. #endif
  131. return me.releaseToAmbiguousOwner();
  132. }
  133. GuiCheckButton GuiCheckButton_createShown (GuiForm parent, int left, int right, int top, int bottom,
  134. conststring32 buttonText, GuiCheckButton_ValueChangedCallback valueChangedCallback, Thing valueChangedBoss, uint32 flags)
  135. {
  136. GuiCheckButton me = GuiCheckButton_create (parent, left, right, top, bottom, buttonText, valueChangedCallback, valueChangedBoss, flags);
  137. GuiThing_show (me);
  138. return me;
  139. }
  140. bool GuiCheckButton_getValue (GuiCheckButton me) {
  141. bool value = false;
  142. #if gtk
  143. value = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (my d_widget)); // gtk_check_button inherits from gtk_toggle_button
  144. #elif motif
  145. value = (Button_GetState (my d_widget -> window) & 0x0003) == BST_CHECKED;
  146. #elif cocoa
  147. GuiCocoaCheckButton *checkButton = (GuiCocoaCheckButton *) my d_widget;
  148. value = [checkButton state] == NSOnState;
  149. #endif
  150. return value;
  151. }
  152. void GuiCheckButton_setValue (GuiCheckButton me, bool value) {
  153. GuiControlBlockValueChangedCallbacks block (me);
  154. #if gtk
  155. gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (my d_widget), value);
  156. #elif motif
  157. Button_SetCheck (my d_widget -> window, value ? BST_CHECKED : BST_UNCHECKED);
  158. #elif cocoa
  159. GuiCocoaCheckButton *checkButton = (GuiCocoaCheckButton *) my d_widget;
  160. [checkButton setState: value ? NSOnState: NSOffState];
  161. #endif
  162. }
  163. /* End of file GuiCheckButton.cpp */