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