Debian Codegraph @

sys/GuiDialog.cpp

raw symbols SCIP
1 /* GuiDialog.cpp
2 *
3 * Copyright (C) 1993-2018,2020,2021,2024,2025 Paul Boersma, 2013 Tom Naughton
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 3 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 #include "GuiP.h"
20
21 Thing_implement (GuiDialog, GuiShell, 0);
22
23 #if gtk
24 static void _GuiGtkDialog_destroyCallback (GuiObject widget, gpointer void_me) {
25 //TRACE
26 trace (U"destroying dialog widget ", Melder_pointer (widget));
27 (void) widget;
28 iam (GuiDialog);
29 trace (U"destroying dialog ", Melder_pointer (me));
30 forget (me); // BUG: if not forget, then potential memory leak
31 }
32 static gboolean _GuiGtkDialog_goAwayCallback (GuiObject widget, GdkEvent *event, gpointer void_me) {
33 (void) widget;
34 (void) event;
35 iam (GuiDialog);
36 if (my d_goAwayCallback)
37 my d_goAwayCallback (my d_goAwayBoss);
38 return true; // signal handled (don't destroy dialog)
39 }
40 #elif motif
41 static void _GuiMotifDialog_destroyCallback (GuiObject widget, XtPointer void_me, XtPointer call) {
42 //TRACE
43 trace (U"destroying dialog widget ", Melder_pointer (widget));
44 (void) widget; (void) call;
45 iam (GuiDialog);
46 trace (U"destroying dialog ", Melder_pointer (me));
47 forget (me); // BUG: if not forget, then potential memory leak
48 }
49 static void _GuiMotifDialog_goAwayCallback (GuiObject widget, XtPointer void_me, XtPointer call) {
50 (void) widget; (void) call;
51 iam (GuiDialog);
52 if (my d_goAwayCallback)
53 my d_goAwayCallback (my d_goAwayBoss);
54 }
55 #endif
56
57 static void gui_blocking_dialog_cb_close (GuiDialog me) {
58 my clickedButtonId = 0;
59 #if cocoa
60 [NSApp stopModal];
61 #endif
62 }
63 GuiDialog GuiDialog_create (GuiWindow parent, int x, int y, int width, int height,
64 conststring32 title, GuiShell_GoAwayCallback goAwayCallback, Thing goAwayBoss, GuiDialog_Modality modality)
65 {
66 autoGuiDialog me = Thing_new (GuiDialog);
67 my d_parent = parent;
68 if (modality == GuiDialog_Modality::BLOCKING) {
69 Melder_assert (! goAwayCallback);
70 Melder_assert (! goAwayBoss);
71 my d_goAwayCallback = gui_blocking_dialog_cb_close;
72 my d_goAwayBoss = me.get();
73 } else {
74 my d_goAwayCallback = goAwayCallback;
75 my d_goAwayBoss = goAwayBoss;
76 }
77 #if gtk
78 my d_gtkWindow = (GtkWindow *) gtk_dialog_new ();
79 #if 0
80 static const GdkRGBA backgroundColour { 0.92, 0.92, 0.92, 1.0 };
81 gtk_widget_override_background_color (GTK_WIDGET (my d_gtkWindow), GTK_STATE_FLAG_NORMAL, & backgroundColour);
82 #endif
83 if (parent &&0) {
84 Melder_assert (parent -> d_widget);
85 GuiObject toplevel = gtk_widget_get_ancestor (GTK_WIDGET (parent -> d_widget), GTK_TYPE_WINDOW);
86 if (toplevel) {
87 gtk_window_set_transient_for (GTK_WINDOW (my d_gtkWindow), GTK_WINDOW (toplevel));
88 gtk_window_set_destroy_with_parent (GTK_WINDOW (my d_gtkWindow), true);
89 }
90 }
91 g_signal_connect (G_OBJECT (my d_gtkWindow), "delete-event",
92 my d_goAwayCallback ? G_CALLBACK (_GuiGtkDialog_goAwayCallback) : G_CALLBACK (gtk_widget_hide_on_delete), me.get());
93 gtk_window_set_default_size (GTK_WINDOW (my d_gtkWindow), width, height);
94 //gtk_window_set_modal (GTK_WINDOW (my d_gtkWindow), modality >= GuiDialog_Modality::MODAL);
95 gtk_window_set_resizable (GTK_WINDOW (my d_gtkWindow), false);
96 //GuiObject vbox = GTK_DIALOG (my d_gtkWindow) -> vbox;
97 GuiObject vbox = gtk_dialog_get_content_area (GTK_DIALOG (my d_gtkWindow));
98 my d_widget = gtk_fixed_new ();
99 _GuiObject_setUserData (my d_widget, me.get());
100 gtk_widget_set_size_request (GTK_WIDGET (my d_widget), width, height);
101 gtk_container_add (GTK_CONTAINER (vbox /*my d_gtkWindow*/), GTK_WIDGET (my d_widget));
102 gtk_widget_show (GTK_WIDGET (my d_widget));
103 g_signal_connect (G_OBJECT (my d_widget), "destroy", G_CALLBACK (_GuiGtkDialog_destroyCallback), me.get());
104 #if defined (chrome)
105 my chrome_surrogateShellTitleLabelWidget = gtk_label_new (Melder_peek32to8 (title));
106 gtk_label_set_use_markup (GTK_LABEL (my chrome_surrogateShellTitleLabelWidget), true);
107 gtk_widget_set_size_request (GTK_WIDGET (my chrome_surrogateShellTitleLabelWidget), width, 31 /*Machine_getTextHeight()*/);
108 gtk_misc_set_alignment (GTK_MISC (my chrome_surrogateShellTitleLabelWidget), 0.5, 0.0);
109 //gtk_widget_set_xalign (GTK_WIDGET (my chrome_surrogateShellTitleLabelWidget), 0.5);
110 gtk_fixed_put (GTK_FIXED (my d_widget), GTK_WIDGET (my chrome_surrogateShellTitleLabelWidget), 0 /*8*/, 0);
111 gtk_widget_show (GTK_WIDGET (my chrome_surrogateShellTitleLabelWidget));
112 #endif
113 GuiShell_setTitle (me.get(), title);
114 #elif motif
115 my d_xmShell = XmCreateDialogShell (/*parent ? parent -> d_widget :*/ nullptr, "dialogShell", nullptr, 0);
116 XtVaSetValues (my d_xmShell, XmNdeleteResponse, my d_goAwayCallback ? XmDO_NOTHING : XmUNMAP, XmNx, x, XmNy, y, nullptr);
117 if (my d_goAwayCallback)
118 XmAddWMProtocolCallback (my d_xmShell, 'delw', _GuiMotifDialog_goAwayCallback, (char *) me.get());
119 GuiShell_setTitle (me.get(), title);
120 my d_widget = XmCreateForm (my d_xmShell, "dialog", nullptr, 0);
121 XtVaSetValues (my d_widget, XmNwidth, (Dimension) width, XmNheight, (Dimension) height, nullptr);
122 _GuiObject_setUserData (my d_widget, me.get());
123 XtAddCallback (my d_widget, XmNdestroyCallback, _GuiMotifDialog_destroyCallback, me.get());
124 XtVaSetValues (my d_widget, XmNdialogStyle,
125 modality >= GuiDialog_Modality::MODAL ? XmDIALOG_FULL_APPLICATION_MODAL : XmDIALOG_MODELESS,
126 XmNautoUnmanage, False, nullptr
127 );
128 #elif cocoa
129 (void) parent;
130 NSRect rect = { { (CGFloat) x, (CGFloat) y }, { (CGFloat) width, (CGFloat) height } };
131 my d_cocoaShell = [[GuiCocoaShell alloc]
132 initWithContentRect: rect
133 styleMask: NSTitledWindowMask | NSClosableWindowMask
134 backing: NSBackingStoreBuffered
135 defer: false
136 ];
137 [my d_cocoaShell setMinSize: NSMakeSize (500.0, 500.0)]; // BUG: should not be needed
138 [my d_cocoaShell setTitle: (NSString *) Melder_peek32toCfstring (title)];
139 //[my d_cocoaShell makeKeyAndOrderFront: nil];
140 //[my d_cocoaShell layoutIfNeeded];
141 [my d_cocoaShell setDistinctiveBackGround];
142 my d_widget = (GuiObject) [my d_cocoaShell contentView];
143 [my d_cocoaShell setUserData: me.get()];
144 [my d_cocoaShell setReleasedWhenClosed: NO];
145 #endif
146 my d_shell = me.get();
147 return me.releaseToAmbiguousOwner();
148 }
149
150 void GuiDialog_setDefaultCallback (GuiDialog me, GuiDialog_DefaultCallback callback, Thing boss) {
151 my d_defaultCallback = callback;
152 my d_defaultBoss = boss;
153 }
154
155 static void gui_blocking_dialog_cb_default (GuiDialog me) {
156 my clickedButtonId = my defaultButtonId;
157 #if cocoa
158 [NSApp stopModal];
159 #endif
160 }
161 #if motif
162 std::vector <HWND> theOtherWindows;
163 BOOL CALLBACK enumWindowsProc (HWND window, LPARAM lParam) {
164 HWND modalWindow = (HWND) lParam;
165 if (window != modalWindow)
166 theOtherWindows. push_back (window);
167 return true;
168 }
169 #endif
170 integer GuiDialog_run (GuiDialog me) {
171 //TRACE
172 GuiDialog_setDefaultCallback (me, gui_blocking_dialog_cb_default, me);
173 my clickedButtonId = -1;
174 /*
175 `my clickedButtonId` will be modified away from -1 in one of three ways:
176 - clicking the dialog's Close button, via `gui_blocking_dialog_cb_close`, which will set it to 0
177 - clicking a normal button (with text), via `gui_blocking_dialog_cb_ok`,
178 which will set it to the sequential id of the clicked button (1/2/3...)
179 - typing Enter, via `gui_blocking_dialog_cb_default`,
180 which will set it to the sequential id of the default button (1/2/3...), or to 0 if there is no default button
181 */
182 #if gtk
183 gtk_dialog_run (GTK_DIALOG (my d_gtkWindow));
184 #elif motif
185 theOtherWindows. clear ();
186 EnumThreadWindows (GetCurrentThreadId (), enumWindowsProc, (LPARAM) my d_xmShell -> window);
187 for (HWND window: theOtherWindows)
188 EnableWindow (window, false);
189 UpdateWindow (my d_xmShell -> window); // the only way to actually show the contents of the dialog (or my d_widget -> window)
190 if (my defaultButton)
191 SetFocus (my defaultButton -> d_widget -> window); // otherwise, no key-down messges will be received by this window
192 do {
193 MSG event;
194 GetMessage (& event, nullptr, 0, 0);
195 if (event. message == WM_COMMAND)
196 trace (event. message);
197 if (event. hwnd) {
198 GuiObject object = (GuiObject) GetWindowLongPtr (event. hwnd, GWLP_USERDATA);
199 /*
200 In case the window goes out of and into focus, the default button will no longer be in focus,
201 but (fortunately) the window will continue to respond to keys.
202 `IsDialogMessage` will no longer handle the Enter key, though, so we should capture the Enter key here.
203 (The window will react even after the user clicks in another window, i.e. when we expect to be out of focus!)
204 This is a HACK.
205 */
206 if (event. message == WM_KEYDOWN && LOWORD (event. wParam) == VK_RETURN && my defaultButton) {
207 my clickedButtonId = my defaultButtonId;
208 break;
209 }
210
211 if (IsDialogMessage (my d_xmShell -> window, & event)) { // not my d_widget -> window, because that would prevent closing
212 trace (U"dialog message ", event. message);
213 } else if (event. message == WM_PAINT) {
214 trace (U"paint ", event. message);
215 //TranslateMessage (& event);
216 //DispatchMessage (& event);
217 }
218 }
219 } while (my clickedButtonId == -1);
220 for (HWND window: theOtherWindows)
221 EnableWindow (window, true);
222 #elif cocoa
223 [[NSApplication sharedApplication] runModalForWindow: my d_cocoaShell];
224 #endif
225 return my clickedButtonId;
226 }
227
228 /* End of file GuiDialog.cpp */