Debian Codegraph @

sys/UiPause.cpp

raw
1 /* UiPause.cpp
2 *
3 * Copyright (C) 2009-2020,2022-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 "UiPause.h"
20 #include "praatP.h"
21
22 static autoUiForm thePauseForm;
23 static int thePauseForm_clicked = 0;
24 static int theCancelContinueButton = 0;
25 static Interpreter thePauseForm_interpreterReference; // TODO: this should be a weak_ptr to the InterpreterStack
26 static structMelderFolder thePauseForm_savedFolder; // TODO: probably remove
27 static Editor thePauseForm_savedEditorReference;
28
29 void UiPause_interpreterGoesAway (Interpreter interpreter) {
30 if (interpreter == thePauseForm_interpreterReference)
31 thePauseForm_interpreterReference = nullptr; // undangle
32 }
33
34 static void thePauseFormOkCallback (UiForm /* sendingForm */, integer /* narg */, Stackel /* args */,
35 conststring32 /* sendingString */, Interpreter /* interpreter */,
36 conststring32 /* invokingButtonTitle */, bool /* modified */, void *closure, Editor optionalEditor)
37 {
38 if (! thePauseForm_interpreterReference) { // interpreter was destroyed?
39 GuiThing_hide (thePauseForm -> d_dialogForm); // BUG: memory leak
40 thePauseForm_savedEditorReference = nullptr;
41 thePauseForm. releaseToUser();
42 return;
43 }
44 if (! thePauseForm) // BUG: perhaps there was a mistake in the script
45 return;
46 Melder_assert (thePauseForm_interpreterReference);
47 Melder_assert (Thing_isa (thePauseForm_interpreterReference, classInterpreter));
48 if (thePauseForm_interpreterReference -> optionalDynamicEnvironmentEditor() != thePauseForm_savedEditorReference) {
49 Melder_assert (thePauseForm_savedEditorReference);
50 Melder_assert (! thePauseForm_interpreterReference -> optionalDynamicEnvironmentEditor());
51 Melder_assert (thePauseForm_interpreterReference -> optionalDynamicEditorEnvironmentClassName());
52 // testing the assumption that the environment can be lost but never added during pause
53 Melder_assert (thePauseForm);
54 if (thePauseForm -> d_dialogForm) {
55 GuiThing_hide (thePauseForm -> d_dialogForm); // BUG: memory leak
56 //thePauseForm_interpreterReference -> isInSecondPass = false; // TODO: corrected? and what about isHalted?
57 }
58 thePauseForm. releaseToUser(); // undangle (will be autodestroyed when unmanaged)
59 Melder_assert (! thePauseForm);
60 const integer lineNumber = thePauseForm_interpreterReference -> lineNumber;
61 Interpreter_stop (thePauseForm_interpreterReference);
62 Melder_flushError (U"Cannot continue after pause, because the ", thePauseForm_interpreterReference -> optionalDynamicEditorEnvironmentClassName(), U" has been closed.",
63 U"\nScript interrupted at line ", lineNumber, U"."); // a top-level error message
64 return;
65 }
66 /*
67 Get the data from the pause form.
68 */
69 Melder_assert (thePauseForm);
70 thePauseForm_clicked = UiForm_getClickedContinueButton (thePauseForm.get());
71 if (thePauseForm_clicked != theCancelContinueButton)
72 UiForm_Interpreter_addVariables (thePauseForm.get(), (Interpreter) closure); // 'closure', not 'interpreter' or 'theInterpreter'!
73 Melder_assert (thePauseForm_clicked >= 1 && thePauseForm_clicked <= 10);
74 autostring32 clickedText = Melder_dup (thePauseForm -> continueTexts [thePauseForm_clicked].get()); // very safe
75 if (thePauseForm -> d_dialogForm) {
76 GuiThing_hide (thePauseForm -> d_dialogForm); // BUG: memory leak
77 //thePauseForm_interpreterReference -> isInSecondPass = false; // TODO: corrected? and what about isHalted?
78 }
79 thePauseForm. releaseToUser(); // undangle (will be autodestroyed when unmanaged)
80 Melder_assert (! thePauseForm);
81 /*
82 Resume the interpreter.
83 */
84 //Melder_setCurrentFolder (& thePauseForm_savedFolder); // TODO: remove
85 Melder_assert (thePauseForm_interpreterReference);
86 try {
87 autoPraatBackground background;
88 Melder_assert (thePauseForm_interpreterReference -> owningInterpreterStack);
89 thePauseForm_interpreterReference -> owningInterpreterStack -> resumeFromTop ();
90 } catch (MelderError) {
91 /*
92 These errors will often not be reached, because Interpreter_resume contains some Melder_flushError().
93 */
94 if (thePauseForm_clicked == theCancelContinueButton)
95 Melder_throw (U"This happened after you cancelled the pause form.");
96 else
97 Melder_throw (U"This happened after you clicked “", clickedText.get(), U"” in the pause form.");
98 }
99 }
100 static void thePauseFormCancelCallback (UiForm /* dia */, void * /* closure */) {
101 if (! thePauseForm_interpreterReference) { // interpreter was destroyed?
102 GuiThing_hide (thePauseForm -> d_dialogForm); // BUG: memory leak
103 thePauseForm_savedEditorReference = nullptr;
104 thePauseForm. releaseToUser();
105 return;
106 }
107 if (! thePauseForm) // BUG: perhaps there was a mistake in the script
108 return;
109 Melder_assert (thePauseForm_interpreterReference);
110 Melder_assert (Thing_isa (thePauseForm_interpreterReference, classInterpreter));
111 /*
112 We arrive here if the user clicked the close box of the dialog window, or if they clicked Stop.
113 */
114 Melder_assert (thePauseForm);
115 if (thePauseForm -> d_dialogForm) {
116 GuiThing_hide (thePauseForm -> d_dialogForm); // BUG: memory leak
117 //thePauseForm_interpreterReference -> isInSecondPass = false; // TODO: corrected? and what about isHalted?
118 }
119 thePauseForm. releaseToUser(); // undangle (will be autodestroyed when unmanaged)
120 Melder_assert (! thePauseForm);
121 if (theCancelContinueButton != 0) {
122 /*
123 The pause window apparently contains a "Cancel"-like button, so it doesn’t contain a Stop button.
124 Hence, if we arrive here, the user must have clicked the close box of the dialog window.
125 We divert this click to the "Cancel"-like button,
126 and it will be the script’s responsibility to handle it.
127 */
128 thePauseForm_clicked = theCancelContinueButton; // there was
129 if (thePauseForm_interpreterReference -> optionalDynamicEnvironmentEditor() != thePauseForm_savedEditorReference) {
130 Melder_assert (thePauseForm_savedEditorReference);
131 Melder_assert (! thePauseForm_interpreterReference -> optionalDynamicEnvironmentEditor());
132 Melder_assert (thePauseForm_interpreterReference -> optionalDynamicEditorEnvironmentClassName());
133 // testing the assumption that the environment can be lost but never added during pause
134 const integer lineNumber = thePauseForm_interpreterReference -> lineNumber;
135 Interpreter_stop (thePauseForm_interpreterReference);
136 Melder_flushError (U"Cannot continue after pause, because the ", thePauseForm_interpreterReference -> optionalDynamicEditorEnvironmentClassName(), U" has been closed.",
137 U"\nScript interrupted at line ", lineNumber, U"."); // a top-level error message
138 }
139 /*
140 Resume the interpreter.
141 */
142 //Melder_setCurrentFolder (& thePauseForm_savedFolder); // TODO: remove
143 try {
144 autoPraatBackground background;
145 Melder_assert (thePauseForm_interpreterReference -> owningInterpreterStack);
146 thePauseForm_interpreterReference -> owningInterpreterStack -> resumeFromTop ();
147 } catch (MelderError) {
148 Melder_flushError (U"This happened after you stopped the pause form.");
149 }
150 } else {
151 /*
152 The pause window apparently contains no "Cancel"-like button, so it must contain a Stop button.
153 Perform the normal action for the Stop button, which is to post a message about interruption.
154 A click in the close box of the dialog window performs the same action as the Stop button.
155 */
156 const integer lineNumber = thePauseForm_interpreterReference -> lineNumber;
157 Interpreter_stop (thePauseForm_interpreterReference);
158 Melder_flushError (U"You interrupted the script at line ", lineNumber, U"."); // a top-level error message
159 }
160 }
161 void UiPause_begin (GuiWindow topShell, Editor optionalPauseWindowOwningEditor, conststring32 title, Interpreter interpreter) {
162 Melder_assert (interpreter);
163 if (interpreter -> isInSecondPass)
164 return; // this can happen in `pauseScript()` and in `pause`.
165 if (thePauseForm)
166 Melder_throw (Melder_upperCaseAppName(), U" cannot have more than one pause form at a time.");
167 thePauseForm = UiForm_create (topShell, optionalPauseWindowOwningEditor, Melder_cat (U"Pause: ", title),
168 thePauseFormOkCallback, interpreter, // pass interpreter as closure!
169 nullptr, nullptr
170 );
171 }
172 void UiPause_real (conststring32 label, conststring32 defaultValue) {
173 if (! thePauseForm)
174 Melder_throw (U"The function “real” should be between a “beginPause” and an “endPause”.");
175 UiForm_addReal (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
176 }
177 void UiPause_positive (conststring32 label, conststring32 defaultValue) {
178 if (! thePauseForm)
179 Melder_throw (U"The function “positive” should be between a “beginPause” and an “endPause”.");
180 UiForm_addPositive (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
181 }
182 void UiPause_integer (conststring32 label, conststring32 defaultValue) {
183 if (! thePauseForm)
184 Melder_throw (U"The function “integer” should be between a “beginPause” and an “endPause”.");
185 UiForm_addInteger (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
186 }
187 void UiPause_natural0 (conststring32 label, conststring32 defaultValue) {
188 if (! thePauseForm)
189 Melder_throw (U"The function “natural0” should be between a “beginPause” and an “endPause”.");
190 UiForm_addNatural0 (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
191 }
192 void UiPause_natural1 (conststring32 label, conststring32 defaultValue) {
193 if (! thePauseForm)
194 Melder_throw (U"The function “natural1” should be between a “beginPause” and an “endPause”.");
195 UiForm_addNatural1 (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
196 }
197 void UiPause_natural (conststring32 label, conststring32 defaultValue) {
198 if (! thePauseForm)
199 Melder_throw (U"The function “natural” should be between a “beginPause” and an “endPause”.");
200 UiForm_addNatural1 (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
201 }
202 void UiPause_word (conststring32 label, conststring32 defaultValue) {
203 if (! thePauseForm)
204 Melder_throw (U"The function “word” should be between a “beginPause” and an “endPause”.");
205 UiForm_addWord (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
206 }
207 void UiPause_sentence (conststring32 label, conststring32 defaultValue) {
208 if (! thePauseForm)
209 Melder_throw (U"The function “sentence” should be between a “beginPause” and an “endPause”.");
210 UiForm_addSentence (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
211 }
212 void UiPause_text (conststring32 label, conststring32 defaultValue, integer numberOfLines) {
213 if (! thePauseForm)
214 Melder_throw (U"The function “text” should be between a “beginPause” and an “endPause”.");
215 UiForm_addText (thePauseForm.get(), nullptr, nullptr, label, defaultValue, numberOfLines);
216 }
217 void UiPause_boolean (conststring32 label, bool defaultValue) {
218 if (! thePauseForm)
219 Melder_throw (U"The function “boolean” should be between a “beginPause” and an “endPause”.");
220 UiForm_addBoolean (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
221 }
222 void UiPause_infile (conststring32 label, conststring32 defaultValue, integer numberOfLines) {
223 if (! thePauseForm)
224 Melder_throw (U"The function “infile” should be between a “beginPause” and an “endPause”.");
225 UiForm_addInfile (thePauseForm.get(), nullptr, nullptr, label, defaultValue, numberOfLines);
226 }
227 void UiPause_outfile (conststring32 label, conststring32 defaultValue, integer numberOfLines) {
228 if (! thePauseForm)
229 Melder_throw (U"The function “outfile” should be between a “beginPause” and an “endPause”.");
230 UiForm_addOutfile (thePauseForm.get(), nullptr, nullptr, label, defaultValue, numberOfLines);
231 }
232 void UiPause_folder (conststring32 label, conststring32 defaultValue, integer numberOfLines) {
233 if (! thePauseForm)
234 Melder_throw (U"The function “folder” should be between a “beginPause” and an “endPause”.");
235 UiForm_addFolder (thePauseForm.get(), nullptr, nullptr, label, defaultValue, numberOfLines);
236 }
237 void UiPause_realvector (conststring32 label, kUi_realVectorFormat defaultFormat, conststring32 defaultValue, integer numberOfLines) {
238 if (! thePauseForm)
239 Melder_throw (U"The function “realvector” should be between a “beginPause” and an “endPause”.");
240 UiForm_addRealVector (thePauseForm.get(), nullptr, nullptr, label, defaultFormat, defaultValue, numberOfLines);
241 }
242 void UiPause_positivevector (conststring32 label, kUi_realVectorFormat defaultFormat, conststring32 defaultValue, integer numberOfLines) {
243 if (! thePauseForm)
244 Melder_throw (U"The function “positivevector” should be between a “beginPause” and an “endPause”.");
245 UiForm_addRealVector (thePauseForm.get(), nullptr, nullptr, label, defaultFormat, defaultValue, numberOfLines);
246 }
247 void UiPause_integervector (conststring32 label, kUi_integerVectorFormat defaultFormat, conststring32 defaultValue, integer numberOfLines) {
248 if (! thePauseForm)
249 Melder_throw (U"The function “integervector” should be between a “beginPause” and an “endPause”.");
250 UiForm_addIntegerVector (thePauseForm.get(), nullptr, nullptr, label, defaultFormat, defaultValue, numberOfLines);
251 }
252 void UiPause_naturalvector (conststring32 label, kUi_integerVectorFormat defaultFormat, conststring32 defaultValue, integer numberOfLines) {
253 if (! thePauseForm)
254 Melder_throw (U"The function “naturalvector” should be between a “beginPause” and an “endPause”.");
255 UiForm_addIntegerVector (thePauseForm.get(), nullptr, nullptr, label, defaultFormat, defaultValue, numberOfLines);
256 }
257 void UiPause_choice (conststring32 label, int defaultValue) {
258 if (! thePauseForm)
259 Melder_throw (U"The function “choice” should be between a “beginPause” and an “endPause”.");
260 UiForm_addChoice (thePauseForm.get(), nullptr, nullptr, nullptr, label, defaultValue, 1);
261 }
262 void UiPause_optionmenu (conststring32 label, int defaultValue) {
263 if (! thePauseForm)
264 Melder_throw (U"The function “optionmenu” should be between a “beginPause” and an “endPause”.");
265 UiForm_addOptionMenu (thePauseForm.get(), nullptr, nullptr, nullptr, label, defaultValue, 1);
266 }
267 void UiPause_option (conststring32 optionText) {
268 if (! thePauseForm)
269 Melder_throw (U"The function “option” should be between a “beginPause” and an “endPause”.");
270 UiOption option = UiForm_addOption (thePauseForm.get(), optionText);
271 if (! option) {
272 thePauseForm. reset();
273 Melder_throw (U"Found the function “option” without a preceding “choice” or “optionmenu”.");
274 }
275 }
276 void UiPause_heading (conststring32 label) {
277 if (! thePauseForm)
278 Melder_throw (U"The function “comment” should be between a “beginPause” and an “endPause”.");
279 UiForm_addHeading (thePauseForm.get(), nullptr, label);
280 }
281 void UiPause_comment (conststring32 label) {
282 //if (thePauseForm_secondPass)
283 // return; // this can happen in `pauseScript()` and in `pause`. TODO: is this needed?
284 if (! thePauseForm)
285 Melder_throw (U"The function “comment” should be between a “beginPause” and an “endPause”.");
286 UiForm_addComment (thePauseForm.get(), nullptr, label);
287 }
288 void UiPause_caption (conststring32 label) {
289 if (! thePauseForm)
290 Melder_throw (U"The function “caption” should be between a “beginPause” and an “endPause”.");
291 UiForm_addCaption (thePauseForm.get(), nullptr, label);
292 }
293
294 int UiPause_end (int numberOfContinueButtons, int defaultContinueButton, int cancelContinueButton,
295 conststring32 continueText1, conststring32 continueText2, conststring32 continueText3,
296 conststring32 continueText4, conststring32 continueText5, conststring32 continueText6,
297 conststring32 continueText7, conststring32 continueText8, conststring32 continueText9,
298 conststring32 continueText10, Interpreter interpreter)
299 {
300 //TRACE
301 Melder_assert (interpreter);
302 trace (U"is in second Pass? ", interpreter -> isInSecondPass);
303 if (interpreter -> isInSecondPass) {
304 Melder_assert (interpreter == thePauseForm_interpreterReference);
305 Melder_assert (! thePauseForm);
306 interpreter -> isInSecondPass = false; // TODO: needed?
307 return thePauseForm_clicked;
308 } else {
309 if (! thePauseForm)
310 Melder_throw (U"Found the function “endPause” without a preceding “beginPause”.");
311 thePauseForm_interpreterReference = interpreter;
312 thePauseForm_savedEditorReference = interpreter -> optionalDynamicEnvironmentEditor();
313 UiForm_setPauseForm (thePauseForm.get(), numberOfContinueButtons, defaultContinueButton, cancelContinueButton,
314 continueText1, continueText2, continueText3, continueText4, continueText5,
315 continueText6, continueText7, continueText8, continueText9, continueText10,
316 thePauseFormCancelCallback
317 );
318 theCancelContinueButton = cancelContinueButton;
319 UiForm_finish (thePauseForm.get());
320 //if (theCurrentPraatApplication -> batch) goto end;
321 thePauseForm_clicked = 0;
322 Melder_assert (interpreter -> owningInterpreterStack);
323 interpreter -> owningInterpreterStack -> haltAll ();
324 /*
325 Put the pause form on the screen.
326 */
327 UiForm_destroyWhenUnmanaged (thePauseForm.get());
328 UiForm_do (thePauseForm.get(), false);
329 return 0;
330 }
331 }
332
333 void UiPause_pauseScript (GuiWindow topShell, Editor optionalPauseWindowOwningEditor, Interpreter interpreter, conststring32 text) {
334 Melder_assert (interpreter);
335 if (interpreter -> isInSecondPass) {
336 Melder_assert (interpreter == thePauseForm_interpreterReference);
337 Melder_assert (! thePauseForm);
338 interpreter -> isInSecondPass = false;
339 } else {
340 if (thePauseForm)
341 Melder_throw (Melder_upperCaseAppName(), U" cannot have more than one pause form at a time.");
342 thePauseForm = UiForm_create (topShell, optionalPauseWindowOwningEditor, U"Pause: stop or continue",
343 thePauseFormOkCallback, interpreter, // pass interpreter as closure!
344 nullptr, nullptr
345 );
346 UiForm_addComment (thePauseForm.get(), nullptr, text);
347 thePauseForm_interpreterReference = interpreter;
348 thePauseForm_savedEditorReference = interpreter -> optionalDynamicEnvironmentEditor();
349 UiForm_setPauseForm (thePauseForm.get(), 1, 1, 0,
350 U"Continue", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
351 thePauseFormCancelCallback
352 );
353 theCancelContinueButton = 0;
354 UiForm_finish (thePauseForm.get());
355 //if (theCurrentPraatApplication -> batch) goto end;
356 thePauseForm_clicked = 0;
357 Melder_assert (interpreter -> owningInterpreterStack);
358 interpreter -> owningInterpreterStack -> haltAll ();
359 /*
360 Put the pause form on the screen.
361 */
362 UiForm_destroyWhenUnmanaged (thePauseForm.get());
363 UiForm_do (thePauseForm.get(), false);
364 }
365 }
366
367 void UiPause_cleanUp () {
368 #if defined (macintosh) || 1
369 thePauseForm. reset(); // TODO: this works only if pause windows aren't children of editor windows
370 #else
371 thePauseForm. releaseToUser(); // BUG: potential memory leak (but it can be a child of an editor window)
372 #endif
373 thePauseForm_clicked = 0;
374 theCancelContinueButton = 0;
375 thePauseForm_interpreterReference = nullptr;
376 //thePauseForm_secondPass = false; // TODO: probably remove
377 MelderFolder_setToNull (& thePauseForm_savedFolder); // TODO: probably remove
378 thePauseForm_savedEditorReference = nullptr;
379 }
380
381 /* End of file UiPause.cpp */