Debian Codegraph @

sys/Gui_messages.cpp

raw
1 /* Gui_messages.cpp
2 *
3 * Copyright (C) 1992-2018,2020-2026 Paul Boersma,
4 * 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 <time.h>
21
22 #include <assert.h>
23 #ifdef _WIN32
24 #include <windows.h>
25 #endif
26
27 #include "melder.h"
28 #include "Graphics.h"
29 #include "Gui.h"
30 #include "Interpreter.h"
31 #include "Script.h"
32 #include "Notebook.h"
33 #include "GuiTrust.h"
34
35 //#include <QuartzCore/CoreAnimation.h>
36
37 /********** Exported variable. **********/
38
39 static GuiWindow Melder_topShell;
40
41 /********** PROGRESS **********/
42
43 static bool theProgressCancelled = false;
44
45 static bool waitWhileProgress (double progress, conststring32 message, GuiDialog dia,
46 GuiProgressBar scale, GuiLabel label1, GuiLabel label2, GuiButton cancelButton)
47 {
48 #if gtk
49 (void) cancelButton; // the Interrupt button has its own callback
50 // Wait for all pending events to be processed. If anybody knows how to inspect GTK's
51 // event queue for specific events, dump the code here, please.
52 // Until then, the button click attaches a g_object data key named "pressed" to the cancelButton
53 // which this function reads out in order to tell whether interruption has occurred
54 while (gtk_events_pending ()) {
55 trace (U"event pending");
56 gtk_main_iteration ();
57 }
58 #elif motif
59 XEvent event;
60 while (PeekMessage (& event, 0, 0, 0, PM_REMOVE)) {
61 if (event. message == WM_KEYDOWN) {
62 /*
63 * Ignore all key-down messages, except Escape.
64 */
65 if (LOWORD (event. wParam) == VK_ESCAPE) {
66 XtUnmanageChild (dia -> d_widget);
67 return false; // don't continue
68 }
69 } else if (event. message == WM_LBUTTONDOWN) {
70 /*
71 * Ignore all mouse-down messages, except click in Interrupt button.
72 */
73 GuiObject me = (GuiObject) GetWindowLongPtr (event. hwnd, GWLP_USERDATA);
74 if (me == cancelButton -> d_widget) {
75 XtUnmanageChild (dia -> d_widget);
76 return false; // don't continue
77 }
78 } else if (event. message != WM_SYSKEYDOWN) {
79 /*
80 * Process paint messages etc.
81 */
82 DispatchMessage (& event);
83 }
84 }
85 #elif cocoa
86 (void) cancelButton; // the Interrupt button has its own callback
87 #endif
88 if (progress >= 1.0) {
89 GuiThing_hide (dia);
90 } else {
91 if (progress <= 0.0)
92 progress = 0.0;
93 GuiThing_show (dia); // TODO: prevent raising to the front
94 const char32 *newline = str32chr (message, U'\n');
95 if (newline) {
96 static MelderString buffer;
97 MelderString_copy (& buffer, message);
98 buffer.string [newline - message] = U'\0';
99 GuiLabel_setText (label1, buffer.string);
100 buffer.string [newline - message] = U'\n';
101 GuiLabel_setText (label2, buffer.string + (newline - message) + 1);
102 } else {
103 GuiLabel_setText (label1, message);
104 GuiLabel_setText (label2, U"");
105 }
106 #if gtk
107 trace (U"update the progress bar");
108 GuiProgressBar_setValue (scale, progress);
109 while (gtk_events_pending ()) {
110 trace (U"event pending");
111 gtk_main_iteration ();
112 }
113 trace (U"check whether the cancel button has the \"pressed\" key set");
114 if (g_object_steal_data (G_OBJECT (cancelButton -> d_widget), "pressed")) {
115 trace (U"the cancel button has been pressed");
116 return false; // don't continue
117 }
118 #elif motif
119 GuiProgressBar_setValue (scale, progress);
120 GdiFlush ();
121 #elif cocoa
122 GuiProgressBar_setValue (scale, progress);
123 //[scale -> d_cocoaProgressBar displayIfNeeded];
124 //[CATransaction flush];
125 GuiShell_drain (scale -> d_shell, true, true);
126 if (theProgressCancelled) {
127 theProgressCancelled = false;
128 return false;
129 }
130 #endif
131 }
132 trace (U"continue");
133 return true;
134 }
135
136 static GuiButton theProgressCancelButton = nullptr;
137
138 #if gtk || cocoa
139 static void progress_dia_close (Thing /* boss */) {
140 theProgressCancelled = true;
141 #if gtk
142 g_object_set_data (G_OBJECT (theProgressCancelButton -> d_widget), "pressed", (gpointer) 1);
143 #endif
144 }
145 static void progress_cancel_btn_press (Thing /* boss */, GuiButtonEvent /* event */) {
146 theProgressCancelled = true;
147 #if gtk
148 g_object_set_data (G_OBJECT (theProgressCancelButton -> d_widget), "pressed", (gpointer) 1);
149 #endif
150 }
151 #endif
152
153 static void _Melder_dia_init (GuiDialog *dia, GuiProgressBar *scale, GuiLabel *label1, GuiLabel *label2, GuiButton *cancelButton, bool hasMonitor) {
154 trace (U"creating the dialog");
155 *dia = GuiDialog_create (Melder_topShell, 200, 100, 500, hasMonitor ? 530 : 200, U"Work in progress",
156 #if gtk || cocoa
157 progress_dia_close, nullptr,
158 #else
159 nullptr, nullptr,
160 #endif
161 GuiDialog_Modality::MODELESS
162 );
163
164 trace (U"creating the labels");
165 *label1 = GuiLabel_createShown (*dia, 3, 503, 0, Gui_LABEL_HEIGHT, U"label1", 0);
166 *label2 = GuiLabel_createShown (*dia, 3, 503, 30, 30 + Gui_LABEL_HEIGHT, U"label2", 0);
167
168 trace (U"creating the scale");
169 *scale = GuiProgressBar_createShown (*dia, 3, -3, 70, 110, 0);
170
171 trace (U"creating the cancel button");
172 *cancelButton = GuiButton_createShown (*dia, 0, 500, 170, 170 + Gui_PUSHBUTTON_HEIGHT,
173 U"Interrupt",
174 #if gtk
175 progress_cancel_btn_press, nullptr,
176 #elif cocoa
177 progress_cancel_btn_press, nullptr,
178 #else
179 nullptr, nullptr,
180 #endif
181 0
182 );
183 trace (U"end");
184 }
185
186 static void gui_progress (double progress, conststring32 message) {
187 static double lastTime;
188 static GuiDialog dia = nullptr;
189 static GuiProgressBar scale = nullptr;
190 static GuiLabel label1 = nullptr, label2 = nullptr;
191 const double now = Melder_clock ();
192 if (progress <= 0.0 || progress >= 1.0 ||
193 now - lastTime > 0.25) // this time step must be much longer than the null-event waiting time
194 {
195 if (! dia)
196 _Melder_dia_init (& dia, & scale, & label1, & label2, & theProgressCancelButton, false);
197 if (! waitWhileProgress (progress, message, dia, scale, label1, label2, theProgressCancelButton))
198 Melder_throw (U"Interrupted!");
199 lastTime = now;
200 }
201 }
202
203 static autoGraphics graphics;
204
205 static void gui_drawingarea_cb_expose (Thing /* boss */, GuiDrawingArea_ExposeEvent /* event */) {
206 if (! graphics)
207 return;
208 Graphics_play (graphics.get(), graphics.get());
209 }
210
211 static void * gui_monitor (double progress, conststring32 message) {
212 static double lastTime;
213 static GuiDialog dia = nullptr;
214 static GuiProgressBar scale = nullptr;
215 static GuiDrawingArea drawingArea = nullptr;
216 static GuiButton cancelButton = nullptr;
217 static GuiLabel label1 = nullptr, label2 = nullptr;
218 const double now = Melder_clock ();
219 if (progress <= 0.0 || progress >= 1.0 ||
220 now - lastTime > 0.25) // this time step must be much longer than the null-event waiting time
221 {
222 if (! dia) {
223 _Melder_dia_init (& dia, & scale, & label1, & label2, & cancelButton, true);
224 drawingArea = GuiDrawingArea_createShown (dia, 0, 500, 230, 530,
225 gui_drawingarea_cb_expose, nullptr, nullptr, nullptr, nullptr, nullptr, 0);
226 GuiThing_show (dia);
227 graphics = Graphics_create_xmdrawingarea (drawingArea);
228 }
229 if (progress <= 0.0 && graphics) {
230 Graphics_clearRecording (graphics.get());
231 Graphics_startRecording (graphics.get());
232 Graphics_clearWs (graphics.get());
233 }
234 if (! waitWhileProgress (progress, message, dia, scale, label1, label2, cancelButton))
235 Melder_throw (U"Interrupted!");
236 lastTime = now;
237 if (progress == 0.0)
238 return graphics.get();
239 }
240 return nullptr;
241 }
242
243 #if cocoa
244 static void mac_message (NSAlertStyle macAlertType, conststring32 message32) {
245 static char16 message16 [4000];
246 const integer messageLength = Melder_length (message32);
247 uinteger j = 0;
248 for (int i = 0; i < messageLength && j <= 4000 - 3; i ++) {
249 char32 kar = message32 [i];
250 if (kar <= 0x00'FFFF) {
251 message16 [j ++] = (char16) kar;
252 } else if (kar <= 0x10'FFFF) {
253 kar -= 0x01'0000;
254 message16 [j ++] = (char16) (0x00'D800 | (kar >> 10));
255 message16 [j ++] = (char16) (0x00'DC00 | (kar & 0x00'03FF));
256 }
257 }
258 message16 [j] = u'\0'; // append null byte because we are going to search this string
259
260 /*
261 Split up the message between header (will appear in bold) and rest.
262 The split is done at the first line break, except if the first line ends in a colon,
263 in which case the split is done at the second line break.
264 */
265 const char16 *lineBreak = & message16 [0];
266 for (; *lineBreak != u'\0'; lineBreak ++)
267 if (*lineBreak == u'\n')
268 break;
269 if (*lineBreak == u'\n' && lineBreak - message16 > 0 && lineBreak [-1] == u':')
270 for (lineBreak ++; *lineBreak != u'\0'; lineBreak ++)
271 if (*lineBreak == u'\n')
272 break;
273 uinteger lengthOfFirstSentence = (uinteger) (lineBreak - message16);
274 /*
275 Create an alert dialog with an icon that is appropriate for the level.
276 */
277 NSAlert *alert = [[NSAlert alloc] init];
278 [alert setAlertStyle: macAlertType];
279 if (macAlertType == NSCriticalAlertStyle)
280 [alert addButtonWithTitle: @"Crash"];
281 /*
282 Add the header in bold.
283 */
284 NSString *header = [[NSString alloc] initWithCharacters: (const unichar *) & message16 [0] length: lengthOfFirstSentence]; // note: init can change the object pointer!
285 if (header) { // make this very safe, because we can be at error time or at fatal time
286 [alert setMessageText: header];
287 [header release];
288 }
289 /*
290 Add the rest of the message in small type.
291 */
292 if (lengthOfFirstSentence < j) {
293 NSString *rest = [[NSString alloc] initWithCharacters: (const unichar *) & lineBreak [1] length: j - 1 - lengthOfFirstSentence];
294 if (rest) { // make this very safe, because we can be at error time or at crash time
295 [alert setInformativeText: rest];
296 [rest release];
297 }
298 }
299 /*
300 Display the alert dialog and synchronously wait for the user to click OK.
301 */
302 [alert runModal];
303 [alert release];
304 }
305 #endif
306
307 #define theMessageFund_SIZE 100'000
308 static char * theMessageFund = nullptr;
309
310 static void gui_crash (conststring32 message) {
311 free (theMessageFund);
312 Melder_casual (U"PRAAT CRASH MESSAGE:\n", message, U"\n(END OF PRAAT CRASH MESSAGE)");
313 #if gtk
314 GuiObject dialog = gtk_message_dialog_new (GTK_WINDOW (Melder_topShell -> d_gtkWindow), GTK_DIALOG_DESTROY_WITH_PARENT,
315 GTK_MESSAGE_ERROR, GTK_BUTTONS_NONE, "%s", Melder_peek32to8 (message));
316 gtk_dialog_add_buttons (GTK_DIALOG (dialog), "Crash", GTK_RESPONSE_OK, nullptr);
317 gtk_dialog_run (GTK_DIALOG (dialog));
318 gtk_widget_destroy (GTK_WIDGET (dialog));
319 #elif motif
320 MessageBox (nullptr, Melder_peek32toW (message), L"Crashing bug", MB_OK | MB_TOPMOST | MB_ICONSTOP);
321 #elif cocoa
322 mac_message (NSAlertStyleCritical, message);
323 SysError (11);
324 #endif
325 }
326
327 static void gui_error (conststring32 message) {
328 const bool memoryIsLow = str32str (message, U"Out of memory");
329 if (memoryIsLow)
330 free (theMessageFund);
331 /*
332 It is not impossible that the program crashes during `gtk_dialog_run` or `MessageBox` or `runModal`,
333 especially if these are called at expose time.
334 Write the message to stdout just in case.
335 */
336 Melder_casual (U"PRAAT ERROR MESSAGE:\n", message, U"(END OF PRAAT ERROR MESSAGE)");
337 #if gtk
338 trace (U"create dialog");
339 GuiObject dialog = gtk_message_dialog_new (GTK_WINDOW (Melder_topShell -> d_gtkWindow), GTK_DIALOG_DESTROY_WITH_PARENT,
340 GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "%s", Melder_peek32to8 (message));
341 trace (U"run dialog");
342 gtk_dialog_run (GTK_DIALOG (dialog));
343 trace (U"destroy dialog");
344 gtk_widget_destroy (GTK_WIDGET (dialog));
345 #elif motif
346 MessageBox (nullptr, Melder_peek32toW (message), L"Message", MB_OK | MB_TOPMOST | MB_ICONWARNING); // or (HWND) XtWindow ((GuiObject) Melder_topShell)
347 #elif cocoa
348 mac_message (NSAlertStyleWarning, message);
349 #endif
350 if (memoryIsLow) {
351 theMessageFund = (char *) malloc (theMessageFund_SIZE);
352 if (! theMessageFund) {
353 #if gtk
354 GuiObject dialog = gtk_message_dialog_new (GTK_WINDOW (Melder_topShell -> d_gtkWindow), GTK_DIALOG_DESTROY_WITH_PARENT,
355 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Praat is very low on memory.\nSave your work and quit Praat.\nIf you don't do that, Praat may crash.");
356 gtk_dialog_run (GTK_DIALOG (dialog));
357 gtk_widget_destroy (GTK_WIDGET (dialog));
358 #elif motif
359 MessageBox (nullptr, L"Praat is very low on memory.\nSave your work and quit Praat.\nIf you don't do that, Praat may crash.", L"Message", MB_OK);
360 #elif cocoa
361 mac_message (NSAlertStyleCritical, U"Praat is very low on memory.\nSave your work and quit Praat.\nIf you don't do that, Praat may crash.");
362 #endif
363 }
364 }
365 }
366
367 static void gui_warning (conststring32 message) {
368 Melder_casual (U"PRAAT WARNING MESSAGE:\n", message, U"\n(END OF PRAAT WARNING MESSAGE)");
369 #if gtk
370 GuiObject dialog = gtk_message_dialog_new (GTK_WINDOW (Melder_topShell -> d_gtkWindow), GTK_DIALOG_DESTROY_WITH_PARENT,
371 GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "%s", Melder_peek32to8 (message));
372 gtk_dialog_run (GTK_DIALOG (dialog));
373 gtk_widget_destroy (GTK_WIDGET (dialog));
374 #elif motif
375 MessageBox (nullptr, Melder_peek32toW (message), L"Warning", MB_OK | MB_TOPMOST | MB_ICONINFORMATION);
376 #elif cocoa
377 mac_message (NSAlertStyleInformational, message);
378 #endif
379 }
380
381 static void gui_trust (void *void_interpreter, conststring32 action) {
382 Interpreter interpreter = (Interpreter) void_interpreter;
383 if (interpreter) {
384 Script script = interpreter -> scriptReference;
385 Notebook notebook = interpreter -> notebookReference;
386 if (script && script -> trusted || notebook && notebook -> trusted)
387 return; // the request should be granted
388 if (Melder_appVersion() < 7000)
389 return; // no trust checking before Praat 7.0
390 conststring32 paragraphs [1+5] = { };
391 if (script) {
392 paragraphs [1] = U"The script";
393 paragraphs [2] = Melder_cat (U"“", script -> string.get(), U"”");
394 } else if (notebook) {
395 paragraphs [1] = U"The notebook";
396 paragraphs [2] = Melder_cat (U"“", notebook -> string.get(), U"”");
397 } else
398 paragraphs [1] = U"Your untitled script or notebook";
399 paragraphs [3] = U"requests permission to:";
400 paragraphs [4] = action;
401 conststring32 option1 = U"CANCEL\n(because I don’t want the requested action to happen)";
402 conststring32 option2 =
403 script ?
404 U"Yes, I allow this script to perform the action that it requests\n(and ask me again next time)"
405 : notebook ?
406 U"Yes, I allow this notebook to perform the action that it requests\n(and ask me again next time)"
407 :
408 U"Yes, I allow this script or notebook to perform the action that it requests\n(and ask me again next time)";
409 conststring32 option3 =
410 script ?
411 U"Yes, and I even allow this script to CONTROL MY COMPUTER from now on (i.e. to perform any action,\n"
412 "including saving, deleting, calling system commands, and internetting), because I FULLY TRUST\n"
413 "the skills and intentions of the authors of the script AND of all the scripts called by it."
414 : notebook ?
415 U"Yes, and I even allow this notebook to CONTROL MY COMPUTER from now on (i.e. to perform any action,\n"
416 "including saving, deleting, calling system commands, and internetting), because I FULLY TRUST\n"
417 "the skills and intentions of the authors of the notebook AND of all the scripts and notebooks called by it."
418 :
419 U"(The option to allow this script or notebook to control your computer will be available\n"
420 "after you save the script or notebook. Until then, clicking here will allow only this one action.)";
421 const integer buttonClicked = GuiTrust_get (nullptr, nullptr,
422 paragraphs [1], paragraphs [2], paragraphs [3], paragraphs [4], paragraphs [5],
423 option1, option2, option3, nullptr, nullptr, interpreter
424 );
425 if (buttonClicked == 2)
426 return; // allow this one action
427 Melder_assert (buttonClicked == 3);
428 if (script)
429 script -> trusted = true;
430 else if (notebook)
431 notebook -> trusted = true;
432 }
433 }
434
435 void Gui_injectMessageProcs (GuiWindow parent) {
436 theMessageFund = (char *) malloc (theMessageFund_SIZE);
437 assert (theMessageFund);
438 Melder_topShell = parent;
439 Melder_setCrashProc (gui_crash);
440 Melder_setErrorProc (gui_error);
441 Melder_setWarningProc (gui_warning);
442 Melder_setProgressProc (gui_progress);
443 Melder_setMonitorProc (gui_monitor);
444 Melder_setTrustProc (gui_trust);
445 }
446
447 /* End of file Gui_messages.cpp */