| 1 |
/* GuiButton.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1993-2008,2010-2020,2024,2026 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 3 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 (GuiButton, GuiControl, 0); |
| 23 |
|
| 24 |
#if gtk |
| 25 |
#define iam_button GuiButton me = (GuiButton) userData |
| 26 |
#elif motif |
| 27 |
#define iam_button GuiButton me = (GuiButton) widget -> userData |
| 28 |
#elif cocoa |
| 29 |
#define iam_button GuiButton me = (GuiButton) self -> d_userData |
| 30 |
#endif |
| 31 |
|
| 32 |
#if gtk |
| 33 |
static void _GuiGtkButton_destroyCallback (GuiObject /* widget */, gpointer userData) { |
| 34 |
GuiButton me = (GuiButton) userData; |
| 35 |
trace (U"destroying GuiButton ", Melder_pointer (me)); |
| 36 |
forget (me); |
| 37 |
} |
| 38 |
static void _GuiGtkButton_activateCallback (GuiObject widget, gpointer userData) { |
| 39 |
GuiButton me = (GuiButton) userData; |
| 40 |
structGuiButtonEvent event { me, false, false, false }; |
| 41 |
if (my d_activateCallback) { |
| 42 |
try { |
| 43 |
my d_activateCallback (my d_activateBoss, & event); |
| 44 |
} catch (MelderError) { |
| 45 |
Melder_flushError (U"Your click on button \"", Melder_peek8to32_u (gtk_widget_get_name (GTK_WIDGET (widget))), U"\" was not completely handled."); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
#elif motif |
| 50 |
void _GuiWinButton_destroy (GuiObject widget) { |
| 51 |
iam_button; |
| 52 |
if (widget == widget -> shell -> defaultButton) |
| 53 |
widget -> shell -> defaultButton = nullptr; // remove dangling reference |
| 54 |
if (widget == widget -> shell -> cancelButton) |
| 55 |
widget -> shell -> cancelButton = nullptr; // remove dangling reference |
| 56 |
_GuiNativeControl_destroy (widget); |
| 57 |
forget (me); // NOTE: my widget is not destroyed here |
| 58 |
} |
| 59 |
void _GuiWinButton_handleClick (GuiObject widget) { |
| 60 |
iam_button; |
| 61 |
if (my d_activateCallback) { |
| 62 |
structGuiButtonEvent event { me, false, false, false }; |
| 63 |
try { |
| 64 |
my d_activateCallback (my d_activateBoss, & event); |
| 65 |
} catch (MelderError) { |
| 66 |
Melder_flushError (U"Your click on button \"", widget -> name.get(), U"\" was not completely handled."); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
bool _GuiWinButton_tryToHandleShortcutKey (GuiObject widget) { |
| 71 |
iam_button; |
| 72 |
if (my d_activateCallback) { |
| 73 |
structGuiButtonEvent event { me, false, false, false }; |
| 74 |
try { |
| 75 |
my d_activateCallback (my d_activateBoss, & event); |
| 76 |
} catch (MelderError) { |
| 77 |
Melder_flushError (U"Your key click on button \"", widget -> name.get(), U"\" was not completely handled."); |
| 78 |
} |
| 79 |
return true; |
| 80 |
} |
| 81 |
return false; |
| 82 |
} |
| 83 |
#elif cocoa |
| 84 |
@implementation GuiCocoaButton { |
| 85 |
GuiButton d_userData; |
| 86 |
} |
| 87 |
- (void) dealloc { // override |
| 88 |
GuiButton me = d_userData; |
| 89 |
forget (me); |
| 90 |
trace (U"deleting a button"); |
| 91 |
[super dealloc]; |
| 92 |
} |
| 93 |
- (GuiThing) getUserData { |
| 94 |
return d_userData; |
| 95 |
} |
| 96 |
- (void) setUserData: (GuiThing) userData { |
| 97 |
Melder_assert (userData == nullptr || Thing_isa (userData, classGuiButton)); |
| 98 |
d_userData = static_cast <GuiButton> (userData); |
| 99 |
} |
| 100 |
- (void) _guiCocoaButton_activateCallback: (id) widget { |
| 101 |
Melder_assert (self == widget); // sender (widget) and receiver (self) happen to be the same object |
| 102 |
GuiButton me = d_userData; |
| 103 |
if (my d_activateCallback) { |
| 104 |
structGuiButtonEvent event { me, false, false, false }; |
| 105 |
try { |
| 106 |
my d_activateCallback (my d_activateBoss, & event); |
| 107 |
} catch (MelderError) { |
| 108 |
Melder_flushError (U"Your click on button \"", my name.get(), U"\" was not completely handled."); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
@end |
| 113 |
#endif |
| 114 |
|
| 115 |
static void gui_blocking_dialog_cb_ok (GuiDialog me, GuiButtonEvent event) { |
| 116 |
my clickedButtonId = event -> button -> d_sequentalIdInDialog; |
| 117 |
#if gtk |
| 118 |
gtk_dialog_response (GTK_DIALOG (my d_gtkWindow), 0); // or `my clickedButtonId`, but return value of gtk_dialog_run is ignored |
| 119 |
#elif cocoa |
| 120 |
[NSApp stopModal]; |
| 121 |
#endif |
| 122 |
} |
| 123 |
GuiButton GuiButton_create (GuiForm parent, int left, int right, int top, int bottom, |
| 124 |
conststring32 buttonText, GuiButton_ActivateCallback activateCallback, Thing activateBoss, uint32 flags) |
| 125 |
{ |
| 126 |
autoGuiButton me = Thing_new (GuiButton); |
| 127 |
my d_shell = parent -> d_shell; |
| 128 |
my d_parent = parent; |
| 129 |
my d_activateCallback = activateCallback; |
| 130 |
my d_activateBoss = activateBoss; |
| 131 |
if (! my d_activateCallback) { |
| 132 |
/* |
| 133 |
This must be a button in a blocking dialog. |
| 134 |
*/ |
| 135 |
Melder_assert (Thing_isa (my d_shell, classGuiDialog)); |
| 136 |
GuiDialog dialog = (GuiDialog) my d_shell; |
| 137 |
my d_activateCallback = gui_blocking_dialog_cb_ok; |
| 138 |
my d_activateBoss = dialog; |
| 139 |
my d_sequentalIdInDialog = ++ dialog -> latestCreatedButtonId; |
| 140 |
if (flags & GuiButton_DEFAULT) { |
| 141 |
dialog -> defaultButtonId = my d_sequentalIdInDialog; |
| 142 |
dialog -> defaultButton = me.get(); |
| 143 |
} |
| 144 |
} |
| 145 |
#if gtk |
| 146 |
my d_widget = gtk_button_new_with_label (Melder_peek32to8 (buttonText)); |
| 147 |
gtk_button_set_relief (GTK_BUTTON (my d_widget), GTK_RELIEF_NORMAL); |
| 148 |
_GuiObject_setUserData (my d_widget, me.get()); |
| 149 |
my v_positionInForm (my d_widget, left, right, top, bottom, parent); |
| 150 |
if (flags & GuiButton_DEFAULT || flags & GuiButton_ATTRACTIVE) { |
| 151 |
gtk_widget_set_can_default (GTK_WIDGET (my d_widget), true); |
| 152 |
GtkWidget *shell = gtk_widget_get_toplevel (GTK_WIDGET (my d_widget)); |
| 153 |
Melder_assert (shell); |
| 154 |
gtk_window_set_default (GTK_WINDOW (shell), GTK_WIDGET (my d_widget)); |
| 155 |
} else if (1) { |
| 156 |
gtk_button_set_focus_on_click (GTK_BUTTON (my d_widget), false); |
| 157 |
gtk_widget_set_can_default (GTK_WIDGET (my d_widget), false); |
| 158 |
} |
| 159 |
g_signal_connect (G_OBJECT (my d_widget), "destroy", G_CALLBACK (_GuiGtkButton_destroyCallback), me.get()); |
| 160 |
g_signal_connect (GTK_BUTTON (my d_widget), "clicked", G_CALLBACK (_GuiGtkButton_activateCallback), me.get()); |
| 161 |
GtkWidget *internalLabel = gtk_bin_get_child (GTK_BIN (my d_widget)); |
| 162 |
if (GTK_IS_LABEL (internalLabel)) |
| 163 |
gtk_label_set_justify (GTK_LABEL (internalLabel), GTK_JUSTIFY_CENTER); |
| 164 |
// if (flags & GuiButton_CANCEL) { |
| 165 |
// parent -> shell -> cancelButton = parent -> cancelButton = my widget; |
| 166 |
// } |
| 167 |
#elif motif |
| 168 |
my d_widget = _Gui_initializeWidget (xmPushButtonWidgetClass, parent -> d_widget, buttonText); |
| 169 |
_GuiObject_setUserData (my d_widget, me.get()); |
| 170 |
my d_widget -> window = CreateWindow (L"button", Melder_peek32toW (_GuiWin_expandAmpersands (my d_widget -> name.get())), |
| 171 |
WS_CHILD |
| 172 |
| ( flags & (GuiButton_DEFAULT | GuiButton_ATTRACTIVE) ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON ) |
| 173 |
| ( flags & GuiButton_MULTILINE ? BS_MULTILINE : 0 ) |
| 174 |
| WS_CLIPSIBLINGS, |
| 175 |
my d_widget -> x, my d_widget -> y, my d_widget -> width, my d_widget -> height, |
| 176 |
my d_widget -> parent -> window, (HMENU) 1, theGui.instance, nullptr |
| 177 |
); |
| 178 |
SetWindowLongPtr (my d_widget -> window, GWLP_USERDATA, (LONG_PTR) my d_widget); |
| 179 |
SetWindowFont (my d_widget -> window, flags & GuiButton_DEFAULT ? theWinGuiBoldLabelFont () : theWinGuiNormalLabelFont (), false); |
| 180 |
my v_positionInForm (my d_widget, left, right, top, bottom, parent); |
| 181 |
if (flags & GuiButton_DEFAULT || flags & GuiButton_ATTRACTIVE) |
| 182 |
parent -> d_widget -> shell -> defaultButton = parent -> d_widget -> defaultButton = my d_widget; |
| 183 |
if (flags & GuiButton_CANCEL) |
| 184 |
parent -> d_widget -> shell -> cancelButton = parent -> d_widget -> cancelButton = my d_widget; |
| 185 |
#elif cocoa |
| 186 |
GuiCocoaButton *button = [[GuiCocoaButton alloc] init]; |
| 187 |
my name = Melder_dup_f (buttonText); |
| 188 |
my d_widget = (GuiObject) button; |
| 189 |
my v_positionInForm (my d_widget, left, right, top, bottom, parent); |
| 190 |
[button setUserData: me.get()]; |
| 191 |
[button setButtonType: NSMomentaryPushInButton]; |
| 192 |
if (bottom - top < 30) |
| 193 |
[button setBezelStyle: NSBezelStylePush]; // fixed height, ... |
| 194 |
else |
| 195 |
[button setBezelStyle: NSBezelStyleFlexiblePush]; // ... custom height |
| 196 |
[button setImagePosition: NSNoImage]; |
| 197 |
[button setBordered: YES]; |
| 198 |
static NSFont *theButtonFont; |
| 199 |
if (! theButtonFont) |
| 200 |
theButtonFont = [NSFont systemFontOfSize: 13.0]; |
| 201 |
[button setFont: theButtonFont]; |
| 202 |
[button setTitle: (NSString *) Melder_peek32toCfstring (buttonText)]; |
| 203 |
[button setTarget: (id) my d_widget]; |
| 204 |
[button setAction: @selector (_guiCocoaButton_activateCallback:)]; |
| 205 |
//[button setAutoresizingMask: NSViewNotSizable]; |
| 206 |
if (flags & GuiButton_DEFAULT) { |
| 207 |
[button setKeyEquivalent: @"\r"]; |
| 208 |
[[button window] setDefaultButtonCell: [button cell]]; // does nothing (and doesn't help flexible push buttons) |
| 209 |
} |
| 210 |
if (flags & GuiButton_CANCEL) |
| 211 |
[button setKeyEquivalent: [NSString stringWithFormat: @"%c", 27]]; // Escape key |
| 212 |
if (flags & GuiButton_ATTRACTIVE) { |
| 213 |
//[button setKeyEquivalent: @"\r"]; // slow! |
| 214 |
[button highlight: YES]; // lasts only till it's clicked! |
| 215 |
//[button setBezelStyle: NSThickerSquareBezelStyle]; |
| 216 |
//[button setFont: [NSFont boldSystemFontOfSize: 14.0]]; |
| 217 |
} |
| 218 |
#endif |
| 219 |
if (flags & GuiButton_INSENSITIVE) |
| 220 |
GuiThing_setSensitive (me.get(), false); |
| 221 |
return me.releaseToAmbiguousOwner(); |
| 222 |
} |
| 223 |
|
| 224 |
GuiButton GuiButton_createShown (GuiForm parent, int left, int right, int top, int bottom, |
| 225 |
conststring32 buttonText, GuiButton_ActivateCallback activateCallback, Thing activateBoss, uint32 flags) |
| 226 |
{ |
| 227 |
GuiButton me = GuiButton_create (parent, left, right, top, bottom, buttonText, activateCallback, activateBoss, flags); |
| 228 |
GuiThing_show (me); |
| 229 |
return me; |
| 230 |
} |
| 231 |
|
| 232 |
void GuiButton_setText (GuiButton me, conststring32 text /* cattable */) { |
| 233 |
#if gtk |
| 234 |
gtk_button_set_label (GTK_BUTTON (my d_widget), Melder_peek32to8 (text)); |
| 235 |
#elif motif |
| 236 |
my d_widget -> name = Melder_dup_f (text); |
| 237 |
_GuiNativeControl_setTitle (my d_widget); |
| 238 |
#elif cocoa |
| 239 |
[(NSButton *) my d_widget setTitle: (NSString *) Melder_peek32toCfstring (text)]; |
| 240 |
#endif |
| 241 |
} |
| 242 |
|
| 243 |
/* End of file GuiButton.cpp */ |