Debian Codegraph @

sys/DemoEditor.cpp

raw
1 /* DemoEditor.cpp
2 *
3 * Copyright (C) 2009-2026 Paul Boersma
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 "DemoEditor.h"
20 #include "machine.h"
21 #include "praatP.h"
22 #include "../kar/UnicodeData.h"
23
24 Thing_implement (DemoEditor, Editor, 0);
25
26 static DemoEditor theReferenceToTheOnlyDemoEditor;
27
28 void Demo_interpreterGoesAway (Interpreter interpreter) {
29 if (theReferenceToTheOnlyDemoEditor && interpreter == theReferenceToTheOnlyDemoEditor -> interpreterReference)
30 theReferenceToTheOnlyDemoEditor -> interpreterReference = nullptr; // undangle
31 }
32
33 bool Demo_hasGraphics (Graphics graphics) {
34 return theReferenceToTheOnlyDemoEditor && theReferenceToTheOnlyDemoEditor -> graphics.get() == graphics;
35 }
36
37 /***** DemoEditor methods *****/
38
39 void structDemoEditor :: v9_destroy () noexcept {
40 Melder_free (our praatPicture);
41 theReferenceToTheOnlyDemoEditor = nullptr;
42 DemoEditor_Parent :: v9_destroy ();
43 }
44
45 void structDemoEditor :: v1_info () {
46 DemoEditor_Parent :: v1_info ();
47 MelderInfo_writeLine (U"Colour: ", MelderColour_name (((PraatPicture) praatPicture) -> colour));
48 MelderInfo_writeLine (U"Font: ", kGraphics_font_getText (((PraatPicture) praatPicture) -> font));
49 MelderInfo_writeLine (U"Font size: ", ((PraatPicture) praatPicture) -> fontSize);
50 }
51
52 void structDemoEditor :: v_goAway () {
53 if (our waitingForInput) {
54 if (our interpreterReference) {
55 const integer lineNumber = our interpreterReference -> lineNumber;
56 Interpreter_stop (our interpreterReference);
57 #if 0
58 Melder_flushError (
59 U"You interrupted the script at line ", lineNumber, U".\n"
60 U"The Demo window will now close."
61 );
62 #endif
63 }
64 forget (theReferenceToTheOnlyDemoEditor);
65 } else
66 DemoEditor_Parent :: v_goAway ();
67 }
68
69 void structDemoEditor :: v_createMenus () {
70 DemoEditor_Parent :: v_createMenus ();
71 }
72
73 static void gui_drawingarea_cb_expose (DemoEditor me, GuiDrawingArea_ExposeEvent /* event */) {
74 if (! my graphics)
75 return; // could be the case in the very beginning
76 static integer count=0;
77 //Melder_casual(U"gui_drawingarea_cb_expose ", ++count);
78 //Graphics_clearWs (my foregroundGraphics.get());
79 Graphics_play (my graphics.get(), my graphics.get());
80 }
81
82 static void gui_drawingarea_cb_mouse (DemoEditor me, GuiDrawingArea_MouseEvent event) {
83 if (! my graphics)
84 return; // could be the case in the very beginning
85 if (! event -> isClick())
86 return;
87 my clicked = true;
88 my keyPressed = false;
89 my x = event -> x;
90 my y = event -> y;
91 my key = UNICODE_BULLET;
92 my shiftKeyPressed = event -> shiftKeyPressed;
93 my commandKeyPressed = event -> commandKeyPressed;
94 my optionKeyPressed = event -> optionKeyPressed;
95 if (my waitingForInput) {
96 my waitingForInput = false;
97 Melder_require (my interpreterReference,
98 U"The script that was waiting for input (e.g. your mouse click) no longer exists.");
99 try {
100 autoPraatBackground background;
101 Melder_assert (my interpreterReference -> owningInterpreterStack);
102 my interpreterReference -> owningInterpreterStack -> resumeFromTop ();
103 } catch (MelderError) {
104 Melder_throw (U"This happened after you clicked in the Demo window.");
105 }
106 }
107 }
108
109 static void gui_drawingarea_cb_key (DemoEditor me, GuiDrawingArea_KeyEvent event) {
110 if (! my graphics)
111 return; // could be the case in the very beginning
112 my clicked = false;
113 my keyPressed = true;
114 my x = 0;
115 my y = 0;
116 my key = event -> key;
117 trace (U"\"", my key, U"\"");
118 my shiftKeyPressed = event -> shiftKeyPressed;
119 my commandKeyPressed = event -> commandKeyPressed;
120 my optionKeyPressed = event -> optionKeyPressed;
121 if (my waitingForInput) {
122 my waitingForInput = false;
123 Melder_require (my interpreterReference,
124 U"The script that was waiting for input (e.g. your key press) no longer exists.");
125 try {
126 autoPraatBackground background;
127 Melder_assert (my interpreterReference -> owningInterpreterStack);
128 my interpreterReference -> owningInterpreterStack -> resumeFromTop ();
129 } catch (MelderError) {
130 Melder_throw (U"This happened after you pressed a key in the Demo window.");
131 }
132 }
133 }
134
135 static void gui_drawingarea_cb_resize (DemoEditor me, GuiDrawingArea_ResizeEvent event) {
136 if (! my graphics)
137 return; // could be the case in the very beginning
138 trace (event -> width, U" ", event -> height);
139 Graphics_setWsViewport (my graphics.get(), 0.0, event -> width, 0.0, event -> height);
140 Graphics_setWsWindow (my graphics.get(), 0.0, 100.0, 0.0, 100.0);
141 //Graphics_setViewport (my graphics.get(), 0.0, 100.0, 0.0, 100.0);
142 Graphics_updateWs (my graphics.get());
143 }
144
145 void structDemoEditor :: v_createChildren () {
146 our drawingArea = GuiDrawingArea_createShown (our windowForm, 0, 0, 0, 0,
147 gui_drawingarea_cb_expose, gui_drawingarea_cb_mouse,
148 gui_drawingarea_cb_key, gui_drawingarea_cb_resize, nullptr, this, 0
149 );
150 }
151
152 void DemoEditor_init (DemoEditor me) {
153 Editor_init (me, 0, 0, 1344, 756, U"", nullptr); // 70 percent of the standard 1920x1080 screen
154
155 my graphics = Graphics_create_xmdrawingarea (my drawingArea);
156 Graphics_setWsWindow (my graphics.get(), 0.0, 100.0, 0.0, 100.0);
157 Graphics_setWsViewport (my graphics.get(),
158 0.0, GuiControl_getWidth (my drawingArea),
159 0.0, GuiControl_getHeight (my drawingArea)
160 );
161 Graphics_startRecording (my graphics.get());
162 Graphics_setViewport (my graphics.get(), 0.0, 100.0, 0.0, 100.0);
163 Graphics_setColour (my graphics.get(), Melder_WHITE);
164 Graphics_setWindow (my graphics.get(), 0.0, 1.0, 0.0, 1.0);
165 Graphics_fillRectangle (my graphics.get(), 0.0, 1.0, 0.0, 1.0);
166 Graphics_setColour (my graphics.get(), Melder_BLACK);
167 Graphics_updateWs (my graphics.get());
168 }
169
170 autoDemoEditor DemoEditor_create () {
171 try {
172 autoDemoEditor me = Thing_new (DemoEditor);
173 DemoEditor_init (me.get());
174 return me;
175 } catch (MelderError) {
176 Melder_throw (U"Demo window not created.");
177 }
178 }
179
180 void Demo_open () {
181 if (Melder_batch) {
182 /*
183 Batch scripts have to be able to run demos.
184 */
185 //Melder_batch = false;
186 }
187 if (! theReferenceToTheOnlyDemoEditor) {
188 autoDemoEditor editor = DemoEditor_create ();
189 Melder_assert (editor);
190 //GuiObject_show (editor -> windowForm);
191 editor -> praatPicture = Melder_calloc_f (structPraatPicture, 1);
192 theCurrentPraatPicture = (PraatPicture) editor -> praatPicture;
193 theCurrentPraatPicture -> graphics = editor -> graphics.get();
194 theCurrentPraatPicture -> font = kGraphics_font::HELVETICA;
195 theCurrentPraatPicture -> fontSize = 10;
196 theCurrentPraatPicture -> lineType = Graphics_DRAWN;
197 theCurrentPraatPicture -> colour = Melder_BLACK;
198 theCurrentPraatPicture -> lineWidth = 1.0;
199 theCurrentPraatPicture -> arrowSize = 1.0;
200 theCurrentPraatPicture -> speckleSize = 1.0;
201 theCurrentPraatPicture -> x1NDC = 0.0;
202 theCurrentPraatPicture -> x2NDC = 100.0;
203 theCurrentPraatPicture -> y1NDC = 0.0;
204 theCurrentPraatPicture -> y2NDC = 100.0;
205 theReferenceToTheOnlyDemoEditor = editor.get();
206 editor.releaseToUser();
207 }
208 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0)
209 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
210 U"Please click or type into the Demo window or close it.");
211 theCurrentPraatPicture = (PraatPicture) theReferenceToTheOnlyDemoEditor -> praatPicture;
212 }
213
214 void Demo_close () {
215 theCurrentPraatPicture = & theForegroundPraatPicture;
216 Graphics_updateWs (theReferenceToTheOnlyDemoEditor -> graphics.get());
217 }
218
219 int Demo_windowTitle (conststring32 title) {
220 autoDemoOpen demo;
221 Thing_setName (theReferenceToTheOnlyDemoEditor, title);
222 return 1;
223 }
224
225 int Demo_show () {
226 if (! theReferenceToTheOnlyDemoEditor)
227 return 0;
228 autoDemoOpen demo;
229 GuiThing_show (theReferenceToTheOnlyDemoEditor -> windowForm);
230 Graphics_updateWs (theReferenceToTheOnlyDemoEditor -> graphics.get());
231 #if defined (UNIX)
232 Melder_sleep (0.02); // because GuiShell_drain is not guaranteed to drain if called within 16 ms from previous
233 #endif
234 GuiShell_drain (theReferenceToTheOnlyDemoEditor -> windowForm, true, false);
235 return 1;
236 }
237
238 #if cocoa
239 @interface DemoWindowTimer: NSObject
240 - (void) timerCallback: (NSTimer *) timer;
241 @end
242 @implementation DemoWindowTimer
243 - (void) timerCallback: (NSTimer *) timer {
244 (void) timer;
245 printf ("eureka\n");
246 }
247 @end
248 DemoWindowTimer *theDemoWindowTimer;
249 #endif // cocoa
250
251 void Demo_timer (double duration) {
252 #if cocoa
253 if (! theDemoWindowTimer)
254 theDemoWindowTimer = [[DemoWindowTimer alloc] init];
255 [NSTimer
256 scheduledTimerWithTimeInterval: duration
257 target: theDemoWindowTimer
258 selector: @selector (timerCallback)
259 userInfo: nil
260 repeats: false
261 ];
262 #endif // cocoa
263 }
264
265 void Demo_waitForInput (Interpreter interpreter) {
266 //TRACE
267 Melder_assert (interpreter);
268 Melder_assert (interpreter -> owningInterpreterStack);
269
270 trace (U"is in second Pass? ", interpreter -> isInSecondPass);
271 if (interpreter -> isInSecondPass) {
272 interpreter -> isInSecondPass = false; // TODO: needed?
273 } else {
274 /*
275 This function pauses the interpreter.
276 Key or mouse input will resume the interpreter.
277 */
278 if (! theReferenceToTheOnlyDemoEditor)
279 Melder_throw (U"Cannot do demoWaitForInput() if the Demo window isn’t visible.");
280 trace (U"is the Demo window already waiting for input? ", theReferenceToTheOnlyDemoEditor -> waitingForInput);
281 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) {
282 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
283 U"Please click or type into the Demo window or close it.");
284 }
285 //GuiThing_show (theReferenceToTheOnlyDemoEditor -> windowForm);
286 theReferenceToTheOnlyDemoEditor -> clicked = false;
287 theReferenceToTheOnlyDemoEditor -> keyPressed = false;
288 theReferenceToTheOnlyDemoEditor -> waitingForInput = true;
289 interpreter -> owningInterpreterStack -> haltAll ();
290 trace (U"setting the Demo window's interpreter to ", Melder_pointer (interpreter));
291 theReferenceToTheOnlyDemoEditor -> interpreterReference = interpreter;
292 }
293 }
294
295 void Demo_peekInput (Interpreter interpreter) {
296 if (! theReferenceToTheOnlyDemoEditor)
297 return;
298 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) {
299 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
300 U"Please click or type into the Demo window or close it.");
301 }
302 //GuiObject_show (theReferenceToTheOnlyDemoEditor -> windowForm);
303 theReferenceToTheOnlyDemoEditor -> clicked = false;
304 theReferenceToTheOnlyDemoEditor -> keyPressed = false;
305 theReferenceToTheOnlyDemoEditor -> x = 0;
306 theReferenceToTheOnlyDemoEditor -> y = 0;
307 theReferenceToTheOnlyDemoEditor -> key = U'\0';
308 theReferenceToTheOnlyDemoEditor -> shiftKeyPressed = false;
309 theReferenceToTheOnlyDemoEditor -> commandKeyPressed = false;
310 theReferenceToTheOnlyDemoEditor -> optionKeyPressed = false;
311 theReferenceToTheOnlyDemoEditor -> waitingForInput = true;
312 {// scope
313 autoMelderSaveCurrentFolder saveFolder;
314 try {
315 #if gtk
316 while (gtk_events_pending ()) {
317 gtk_main_iteration ();
318 }
319 #elif cocoa
320 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
321 [theReferenceToTheOnlyDemoEditor -> windowForm -> d_cocoaShell flushWindow]; // TODO: needed?
322 Graphics_updateWs (theReferenceToTheOnlyDemoEditor -> graphics.get()); // make sure that even texts will be drawn
323 while (NSEvent *nsEvent = [NSApp
324 nextEventMatchingMask: NSAnyEventMask
325 untilDate: [NSDate distantPast] // don't wait
326 inMode: NSDefaultRunLoopMode
327 dequeue: YES])
328 {
329 [NSApp sendEvent: nsEvent];
330 }
331 [NSApp updateWindows]; // called automatically? TODO: remove
332 [pool release];
333 #elif defined (_WIN32)
334 XEvent event;
335 while (PeekMessage (& event, 0, 0, 0, PM_REMOVE)) {
336 XtDispatchEvent (& event);
337 }
338 #endif
339 } catch (MelderError) {
340 Melder_flushError (U"An error made it to the outer level in the Demo window; should not occur! Please write to [email protected]");
341 }
342 }
343 theReferenceToTheOnlyDemoEditor -> waitingForInput = false;
344 if (theReferenceToTheOnlyDemoEditor -> userWantsToClose) {
345 Interpreter_stop (interpreter);
346 forget (theReferenceToTheOnlyDemoEditor);
347 Melder_throw (U"You interrupted the script.");
348 }
349 }
350
351 bool Demo_clicked () {
352 if (! theReferenceToTheOnlyDemoEditor)
353 return false;
354 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) {
355 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
356 U"Please click or type into the Demo window or close it.");
357 }
358 return theReferenceToTheOnlyDemoEditor -> clicked;
359 }
360
361 double Demo_x () {
362 if (! theReferenceToTheOnlyDemoEditor)
363 return undefined;
364 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) {
365 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
366 U"Please click or type into the Demo window or close it.");
367 }
368 trace (U"NDC before: ", theReferenceToTheOnlyDemoEditor -> graphics -> d_x1NDC, U" ", theReferenceToTheOnlyDemoEditor -> graphics -> d_x2NDC);
369 Graphics_setInner (theReferenceToTheOnlyDemoEditor -> graphics.get());
370 trace (U"NDC after: ", theReferenceToTheOnlyDemoEditor -> graphics -> d_x1NDC, U" ", theReferenceToTheOnlyDemoEditor -> graphics -> d_x2NDC);
371 double xWC, yWC;
372 trace (U"DC: x ", theReferenceToTheOnlyDemoEditor -> x, U", y ", theReferenceToTheOnlyDemoEditor -> y);
373 Graphics_DCtoWC (theReferenceToTheOnlyDemoEditor -> graphics.get(), theReferenceToTheOnlyDemoEditor -> x, theReferenceToTheOnlyDemoEditor -> y, & xWC, & yWC);
374 trace (U"WC: x ", xWC, U", y ", yWC);
375 Graphics_unsetInner (theReferenceToTheOnlyDemoEditor -> graphics.get());
376 return xWC;
377 }
378
379 double Demo_y () {
380 if (! theReferenceToTheOnlyDemoEditor)
381 return undefined;
382 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) {
383 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
384 U"Please click or type into the Demo window or close it.");
385 }
386 Graphics_setInner (theReferenceToTheOnlyDemoEditor -> graphics.get());
387 double xWC, yWC;
388 Graphics_DCtoWC (theReferenceToTheOnlyDemoEditor -> graphics.get(), theReferenceToTheOnlyDemoEditor -> x, theReferenceToTheOnlyDemoEditor -> y, & xWC, & yWC);
389 Graphics_unsetInner (theReferenceToTheOnlyDemoEditor -> graphics.get());
390 return yWC;
391 }
392
393 bool Demo_keyPressed () {
394 if (! theReferenceToTheOnlyDemoEditor)
395 return false;
396 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) {
397 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
398 U"Please click or type into the Demo window or close it.");
399 }
400 return theReferenceToTheOnlyDemoEditor -> keyPressed;
401 }
402
403 char32 Demo_key () {
404 if (! theReferenceToTheOnlyDemoEditor)
405 return U'\0';
406 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) {
407 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
408 U"Please click or type into the Demo window or close it.");
409 }
410 return theReferenceToTheOnlyDemoEditor -> key;
411 }
412
413 bool Demo_shiftKeyPressed () {
414 if (! theReferenceToTheOnlyDemoEditor)
415 return false;
416 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) {
417 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
418 U"Please click or type into the Demo window or close it.");
419 }
420 return theReferenceToTheOnlyDemoEditor -> shiftKeyPressed;
421 }
422
423 bool Demo_commandKeyPressed () {
424 if (! theReferenceToTheOnlyDemoEditor)
425 return false;
426 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) {
427 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
428 U"Please click or type into the Demo window or close it.");
429 }
430 return theReferenceToTheOnlyDemoEditor -> commandKeyPressed;
431 }
432
433 bool Demo_optionKeyPressed () {
434 if (! theReferenceToTheOnlyDemoEditor)
435 return false;
436 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) {
437 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
438 U"Please click or type into the Demo window or close it.");
439 }
440 return theReferenceToTheOnlyDemoEditor -> optionKeyPressed;
441 }
442
443 bool Demo_input (conststring32 keys) {
444 if (! theReferenceToTheOnlyDemoEditor)
445 return false;
446 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) {
447 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
448 U"Please click or type into the Demo window or close it.");
449 }
450 return str32chr (keys, theReferenceToTheOnlyDemoEditor -> key) != nullptr;
451 }
452
453 bool Demo_clickedIn (double left, double right, double bottom, double top) {
454 if (! theReferenceToTheOnlyDemoEditor || ! theReferenceToTheOnlyDemoEditor -> clicked)
455 return false;
456 if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) {
457 Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
458 U"Please click or type into the Demo window or close it.");
459 }
460 if (! theReferenceToTheOnlyDemoEditor -> clicked)
461 return false;
462 const double xWC = Demo_x (), yWC = Demo_y ();
463 return xWC >= left && xWC < right && yWC >= bottom && yWC < top;
464 }
465
466 void Demo_saveToPdfFile (MelderFile file) {
467 if (! theReferenceToTheOnlyDemoEditor)
468 return;
469 if (! theReferenceToTheOnlyDemoEditor -> graphics)
470 return; // could be the case in the very beginning
471 const double resolution = theReferenceToTheOnlyDemoEditor -> graphics -> resolution;
472 autoGraphics pdfGraphics = Graphics_create_pdffile (file, resolution,
473 undefined, GuiControl_getWidth (theReferenceToTheOnlyDemoEditor -> drawingArea) / resolution,
474 undefined, GuiControl_getHeight (theReferenceToTheOnlyDemoEditor -> drawingArea) / resolution
475 );
476 pdfGraphics -> d_x2DCmax = 1e9;
477 pdfGraphics -> d_y2DCmax = 1e9;
478 Graphics_setWsWindow (pdfGraphics.get(), 0.0, 100.0, 0.0, 100.0);
479 Graphics_setWsViewport (pdfGraphics.get(),
480 0.0, GuiControl_getWidth (theReferenceToTheOnlyDemoEditor -> drawingArea),
481 0.0, GuiControl_getHeight (theReferenceToTheOnlyDemoEditor -> drawingArea)
482 );
483 Graphics_play (theReferenceToTheOnlyDemoEditor -> graphics.get(), pdfGraphics.get());
484 }
485
486 /* End of file DemoEditor.cpp */