Debian Codegraph @

sys/Editor.cpp

raw
1 /* Editor.cpp
2 *
3 * Copyright (C) 1992-2024,2026 Paul Boersma, 2008 Stefan de Konink, 2010 Franz Brausse
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 <time.h>
20 #include "ScriptEditor.h"
21 #include "machine.h"
22 #include "EditorM.h"
23 #include "praat_script.h"
24
25 Thing_implement (Editor, DataGui, 0);
26
27 #include "Prefs_define.h"
28 #include "Editor_prefs.h"
29 #include "Prefs_install.h"
30 #include "Editor_prefs.h"
31 #include "Prefs_copyToInstance.h"
32 #include "Editor_prefs.h"
33
34 /********** class EditorCommand **********/
35
36 Thing_implement (EditorCommand, Thing, 0);
37
38 /********** class EditorMenu **********/
39
40 Thing_implement (EditorMenu, Thing, 0);
41
42 /********** functions **********/
43
44 static void commonCallback (EditorCommand me, GuiMenuItemEvent /* event */) {
45 if (my d_editor && my d_editor -> v_scriptable () && ! str32str (my itemTitle.get(), U"...")) {
46 UiHistory_write (U"\n");
47 UiHistory_write_colonize (my itemTitle.get());
48 }
49 try {
50 my commandCallback (my sender, me, nullptr, 0, nullptr, nullptr, nullptr);
51 } catch (MelderError) {
52 if (! Melder_hasError (U"Script exited."))
53 Melder_appendError (U"Menu command “", my itemTitle.get(), U"” not completed.");
54 Melder_flushError ();
55 }
56 }
57
58 static GuiMenuItem DataGuiMenu_addCommand_ (EditorMenu me, conststring32 itemTitle /* cattable */, uint32 flags,
59 DataGuiCommandCallback commandCallback, DataGui optionalSender)
60 {
61 autoEditorCommand thee = Thing_new (EditorCommand);
62 thy d_editor = my d_editor;
63 thy sender = ( optionalSender ? optionalSender : thy d_editor );
64 thy menu = me;
65 const bool titleIsHeader = Melder_endsWith (itemTitle, U":");
66 if (titleIsHeader) {
67 flags |= GuiMenu_UNDERLINED;
68 if (itemTitle [0] == U'-' && itemTitle [1] == U' ') {
69 GuiMenu_addSeparator (my menuWidget);
70 itemTitle += 2;
71 }
72 }
73 const int depth = (flags & GuiMenu_DEPTH_3) >> 16; // the maximum depth in editor windows is 3
74 thy itemTitle = Melder_dup (itemTitle);
75 if (depth > 0) {
76 /*
77 bikeshed choices for indented menu items
78 */
79 [[maybe_unused]] constexpr conststring32 space = U" "; // minimalist
80 [[maybe_unused]] constexpr conststring32 fourDots = U"\u205E "; // not evenly dispersed
81 [[maybe_unused]] constexpr conststring32 twoDots = U"\u205A "; // dispersion OKish
82 [[maybe_unused]] constexpr conststring32 oneDot = U"· "; // dispersion good, not too prominent, not really thin
83 [[maybe_unused]] constexpr conststring32 hyphenationPoint = U"\u2027 "; // xx
84 [[maybe_unused]] constexpr conststring32 pipe = U"| "; // not long enough, prominent
85 [[maybe_unused]] constexpr conststring32 boxDrawingLightVertical = U"\u2502 "; // not long enough, prominent
86 [[maybe_unused]] constexpr conststring32 boxDrawingLightQuadrupleDashVertical = U"\u250A "; // not long enough, but nicely thin
87 itemTitle = Melder_cat (space, itemTitle);
88 }
89 if (! commandCallback)
90 flags |= GuiMenu_INSENSITIVE;
91 thy itemWidget =
92 titleIsHeader ? GuiMenu_addItem (my menuWidget, itemTitle, flags, nullptr, nullptr) :
93 ! commandCallback ? GuiMenu_addSeparator (my menuWidget) :
94 flags & GuiMenu_HIDDEN ? nullptr :
95 GuiMenu_addItem (my menuWidget, itemTitle, flags, commonCallback, thee.get())
96 ; // DANGLE BUG: me can be killed by Collection_addItem(), but EditorCommand::destroy doesn't remove the item
97 thy commandCallback = commandCallback;
98 GuiMenuItem result = thy itemWidget;
99 my commands. addItem_move (thee.move());
100 return result;
101 }
102 GuiMenuItem DataGuiMenu_addCommand (EditorMenu me, conststring32 itemTitle /* cattable */, uint32 flags,
103 DataGuiCommandCallback commandCallback, DataGui optionalSender)
104 {
105 if (flags < 3) // the maximum depth in editor windows is 3
106 flags *= GuiMenu_DEPTH_1; // turn 1..3 into GuiMenu_DEPTH_1..GuiMenu_DEPTH_3, because the flags are ORed below
107 const char32 *pSeparator = str32str (itemTitle, U" || ");
108 if (! pSeparator)
109 return DataGuiMenu_addCommand_ (me, itemTitle, flags, commandCallback, optionalSender);
110 integer positionOfSeparator = pSeparator - itemTitle;
111 static MelderString string;
112 MelderString_copy (& string, itemTitle);
113 char32 *pTitle = & string.string [0];
114 GuiMenuItem result = nullptr;
115 do {
116 pTitle [positionOfSeparator] = U'\0';
117 GuiMenuItem menuItem = DataGuiMenu_addCommand_ (me, pTitle, flags, commandCallback, optionalSender);
118 if (menuItem)
119 result = menuItem; // only the first
120 pTitle += positionOfSeparator + 4; // step past " || "
121 pSeparator = str32str (pTitle, U" || ");
122 if (pSeparator)
123 positionOfSeparator = pSeparator - pTitle;
124 flags |= GuiMenu_HIDDEN;
125 } while (pSeparator);
126 (void) DataGuiMenu_addCommand_ (me, pTitle, flags, commandCallback, optionalSender);
127 return result;
128 }
129 GuiMenuItem EditorMenu_addCommand (EditorMenu me, conststring32 itemTitle /* cattable */, uint32 flags,
130 EditorCommandCallback commandCallback)
131 {
132 return DataGuiMenu_addCommand (me, itemTitle, flags, commandCallback.get(), nullptr);
133 }
134
135 /*GuiObject EditorCommand_getItemWidget (EditorCommand me) { return my itemWidget; }*/
136
137 EditorMenu Editor_addMenu (Editor me, conststring32 menuTitle, uint32 flags) {
138 autoEditorMenu thee = Thing_new (EditorMenu);
139 thy d_editor = me;
140 thy menuTitle = Melder_dup (menuTitle);
141 thy menuWidget = GuiMenu_createInWindow (my windowForm, menuTitle, flags);
142 return my menus. addItem_move (thee.move());
143 }
144
145 /*GuiObject EditorMenu_getMenuWidget (EditorMenu me) { return my menuWidget; }*/
146
147 GuiMenuItem Editor_addCommand (Editor me, conststring32 menuTitle, conststring32 itemTitle, uint32 flags, EditorCommandCallback commandCallback)
148 {
149 try {
150 integer numberOfMenus = my menus.size;
151 for (integer imenu = 1; imenu <= numberOfMenus; imenu ++) {
152 EditorMenu menu = my menus.at [imenu];
153 if (str32equ (menuTitle, menu -> menuTitle.get()))
154 return EditorMenu_addCommand (menu, itemTitle, flags, commandCallback);
155 }
156 Melder_throw (U"Menu “", menuTitle, U"” does not exist.");
157 } catch (MelderError) {
158 Melder_throw (U"Command “", itemTitle, U"” not inserted in menu “", menuTitle, U"”.");
159 }
160 }
161
162 static void Editor_scriptCallback (Editor me, EditorCommand cmd, UiForm /* sendingForm */,
163 integer /* narg */, Stackel /* args */, conststring32 /* sendingString */, Interpreter /* interpreter */)
164 {
165 praat_executeScriptFromEditorCommand (me, cmd, cmd -> script.get());
166 }
167
168 static EditorMenu findMenu (Editor me, conststring32 menuTitle) {
169 const integer numberOfMenus = my menus.size;
170 for (integer imenu = 1; imenu <= numberOfMenus; imenu ++) {
171 EditorMenu menu = my menus.at [imenu];
172 if (str32equ (menu -> menuTitle.get(), menuTitle))
173 return menu;
174 }
175 return nullptr;
176 }
177 static GuiMenuItem EditorMenu_addCommandScript (EditorMenu me, conststring32 itemTitle, uint32 flags, conststring32 script) {
178 autoEditorCommand cmd = Thing_new (EditorCommand);
179 cmd -> d_editor = my d_editor;
180 cmd -> sender = my d_editor;
181 cmd -> menu = me;
182 const bool titleIsHeader = Melder_endsWith (itemTitle, U":");
183 if (titleIsHeader) {
184 if (itemTitle [0] == U'-' && itemTitle [1] == U' ') {
185 flags |= GuiMenu_UNDERLINED;
186 GuiMenu_addSeparator (my menuWidget);
187 itemTitle += 2;
188 }
189 cmd -> commandCallback = nullptr;
190 flags |= GuiMenu_INSENSITIVE;
191 } else
192 cmd -> commandCallback = Editor_scriptCallback;
193 if (itemTitle && itemTitle [0] != U'-')
194 itemTitle = Melder_cat (U"\u207A", itemTitle);
195 const int depth = (flags & GuiMenu_DEPTH_3) >> 16; // the maximum depth in editor windows is 3
196 if (depth > 0) {
197 /*
198 bikeshed choices for indented menu items
199 */
200 [[maybe_unused]] constexpr conststring32 space = U" "; // minimalist
201 [[maybe_unused]] constexpr conststring32 fourDots = U"\u205E "; // not evenly dispersed
202 [[maybe_unused]] constexpr conststring32 twoDots = U"\u205A "; // dispersion OKish
203 [[maybe_unused]] constexpr conststring32 oneDot = U"· "; // dispersion good, not too prominent, not really thin
204 [[maybe_unused]] constexpr conststring32 hyphenationPoint = U"\u2027 "; // xx
205 [[maybe_unused]] constexpr conststring32 pipe = U"| "; // not long enough, prominent
206 [[maybe_unused]] constexpr conststring32 boxDrawingLightVertical = U"\u2502 "; // not long enough, prominent
207 [[maybe_unused]] constexpr conststring32 boxDrawingLightQuadrupleDashVertical = U"\u250A "; // not long enough, but nicely thin
208 itemTitle = Melder_cat (space, itemTitle);
209 }
210 cmd -> itemTitle = Melder_dup (itemTitle);
211 cmd -> itemWidget = ( script == nullptr || ! itemTitle || itemTitle [0] == U'-' ? GuiMenu_addSeparator (my menuWidget) :
212 titleIsHeader ?
213 GuiMenu_addItem (my menuWidget, itemTitle, flags, nullptr, nullptr) :
214 GuiMenu_addItem (my menuWidget, itemTitle, flags, commonCallback, cmd.get()) ); // DANGLE BUG
215 if (script [0] == U'\0') {
216 cmd -> script = Melder_dup (U"");
217 } else {
218 structMelderFile file { };
219 Melder_relativePathToFile (script, & file);
220 cmd -> script = Melder_dup (MelderFile_peekPath (& file));
221 }
222 GuiMenuItem result = cmd -> itemWidget;
223 my commands. addItem_move (cmd.move());
224 return result;
225 }
226 GuiMenuItem Editor_addCommandScript (Editor me, conststring32 menuTitle, conststring32 itemTitle, uint32 flags,
227 conststring32 script)
228 {
229 EditorMenu menu = findMenu (me, menuTitle);
230 if (menu)
231 return EditorMenu_addCommandScript (menu, itemTitle, flags, script);
232 /*
233 Try again, in case the menu title is obsolete.
234 */
235 conststring32 alternativeMenuTitle =
236 str32equ (menuTitle, U"Spectrum") ? U"Spectrogram" :
237 str32equ (menuTitle, U"View") || str32equ (menuTitle, U"Select") ? U"Time" : // or "Play", but we cannot know
238 str32equ (menuTitle, U"Formant") ? U"Formants" :
239 str32equ (menuTitle, U"Query") ? U"Edit" : // not appropriate, but better than nothing
240 nullptr;
241 if (alternativeMenuTitle) {
242 EditorMenu alternativeMenu = findMenu (me, alternativeMenuTitle);
243 if (alternativeMenu) {
244 GuiMenuItem menuItem = EditorMenu_addCommandScript (alternativeMenu, itemTitle, flags, script);
245 static bool warningGiven = false;
246 if (! warningGiven) {
247 warningGiven = true;
248 Melder_warning (U"The menu “", menuTitle, U"” no longer exists. The command “", itemTitle,
249 U"” has been installed in the menu “", alternativeMenuTitle, U"” instead. "
250 U"You could consider updating the script that installed “", script, U"”, "
251 U"which is either the buttons file or a plug-in."
252 );
253 }
254 return menuItem;
255 } // else issue the original warning
256 }
257 Melder_warning (
258 U"Menu “", menuTitle, U"” does not exist.\n"
259 U"Command “", itemTitle, U"” not inserted in menu “", menuTitle, U"”.\n"
260 U"To fix this, go to Praat->Preferences->Buttons->Editors, and remove the script from this menu.\n"
261 U"You may then want to install the script in a different menu."
262 );
263 return nullptr;
264 }
265
266 void Editor_setMenuSensitive (Editor me, conststring32 menuTitle, bool sensitive) {
267 integer numberOfMenus = my menus.size;
268 for (integer imenu = 1; imenu <= numberOfMenus; imenu ++) {
269 EditorMenu menu = my menus.at [imenu];
270 if (str32equ (menuTitle, menu -> menuTitle.get())) {
271 GuiThing_setSensitive (menu -> menuWidget, sensitive);
272 return;
273 }
274 }
275 }
276
277 EditorCommand Editor_getMenuCommand (Editor me, conststring32 menuTitle, conststring32 itemTitle) {
278 integer numberOfMenus = my menus.size;
279 for (int imenu = 1; imenu <= numberOfMenus; imenu ++) {
280 EditorMenu menu = my menus.at [imenu];
281 if (str32equ (menuTitle, menu -> menuTitle.get())) {
282 integer numberOfCommands = menu -> commands.size, icommand;
283 for (icommand = 1; icommand <= numberOfCommands; icommand ++) {
284 EditorCommand command = menu -> commands.at [icommand];
285 if (str32equ (itemTitle, command -> itemTitle.get()))
286 return command;
287 }
288 }
289 }
290 Melder_throw (U"Command “", itemTitle, U"” not found in menu “", menuTitle, U"”.");
291 }
292
293 void Editor_doMenuCommand (Editor me, conststring32 commandTitle, integer narg, Stackel args, conststring32 arguments, Interpreter interpreter) {
294 Melder_assert (me);
295 integer numberOfMenus = my menus.size;
296 for (int imenu = 1; imenu <= numberOfMenus; imenu ++) {
297 EditorMenu menu = my menus.at [imenu];
298 integer numberOfCommands = menu -> commands.size;
299 for (integer icommand = 1; icommand <= numberOfCommands; icommand ++) {
300 EditorCommand command = menu -> commands.at [icommand];
301 if (str32equ (commandTitle, command -> itemTitle.get())) {
302 command -> commandCallback (command -> sender, command, nullptr, narg, args, arguments, interpreter);
303 return;
304 }
305 }
306 }
307 Melder_assert (my classInfo);
308 Melder_throw (U"Command not available in ", my classInfo -> className, U".");
309 }
310
311 /********** class Editor **********/
312
313 void structEditor :: v9_destroy () noexcept {
314 MelderAudio_stopPlaying (MelderAudio_IMPLICIT);
315 /*
316 The following command must be performed before the shell is destroyed.
317 Otherwise, we would be forgetting dangling command dialogs here.
318 */
319 our menus.removeAllItems();
320 for (integer i = theReferencesToAllOpenScriptEditors.size; i >= 1; i --) {
321 ScriptEditor scriptEditor = theReferencesToAllOpenScriptEditors.at [i];
322 if (scriptEditor -> optionalReferenceToOwningEditor != this)
323 continue;
324 // if (scriptEditor -> dirty || scriptEditor -> interpreter && scriptEditor -> interpreter -> running) {
325 // scriptEditor -> optionalReferenceToOwningEditor = nullptr; // undangle
326 // if (scriptEditor -> interpreter)
327 // scriptEditor -> interpreter -> undangleEditorEnvironments();
328 // Thing_setName (scriptEditor, nullptr);
329 // Editor_setMenuSensitive (scriptEditor, U"Run", false);
330 // } else
331 forget (scriptEditor);
332 }
333 Interpreters_undangleEnvironment (this);
334
335 Editor_broadcastDestruction (this);
336 if (our windowForm) {
337 #if gtk
338 if (our windowForm -> d_gtkWindow) {
339 Melder_assert (GTK_IS_WIDGET (our windowForm -> d_gtkWindow));
340 gtk_widget_destroy (GTK_WIDGET (our windowForm -> d_gtkWindow));
341 }
342 #elif motif
343 if (our windowForm -> d_xmShell) {
344 XtDestroyWidget (our windowForm -> d_xmShell);
345 }
346 #elif cocoa
347 if (our windowForm -> drawingArea) {
348 GuiCocoaDrawingArea *cocoaDrawingArea = (GuiCocoaDrawingArea *) our windowForm -> drawingArea -> d_widget;
349 if (cocoaDrawingArea)
350 [cocoaDrawingArea setUserData: nullptr];
351 }
352 if (our windowForm -> d_cocoaShell) {
353 NSWindow *cocoaWindow = our windowForm -> d_cocoaShell;
354 //our windowForm -> d_cocoaShell = nullptr;
355 [cocoaWindow close]; // this should *release* the window as well, because `releasedWhenClosed` is on by default for NSWindows
356 }
357 #endif
358 }
359 Editor_Parent :: v9_destroy ();
360 }
361
362 void structEditor :: v1_info () {
363 // skipping parent classes
364 MelderInfo_writeLine (U"Editor type: ", Thing_className (this));
365 MelderInfo_writeLine (U"Editor name: ", our name ? our name.get() : U"<no name>");
366 time_t today = time (nullptr);
367 MelderInfo_writeLine (U"Date: ", Melder_peek8to32_u (ctime (& today))); // includes a newline
368 if (our data()) {
369 MelderInfo_writeLine (U"Data type: ", our data() -> classInfo -> className);
370 MelderInfo_writeLine (U"Data name: ", our data() -> name.get());
371 }
372 }
373
374 void structEditor :: v_nameChanged () {
375 if (our name) {
376 GuiShell_setTitle (our windowForm, our name.get());
377 for (integer i = 1; i <= theReferencesToAllOpenScriptEditors.size; i ++) {
378 ScriptEditor scriptEditor = theReferencesToAllOpenScriptEditors.at [i];
379 if (scriptEditor -> optionalReferenceToOwningEditor == this)
380 Thing_setName (theReferencesToAllOpenScriptEditors.at [i], nullptr);
381 }
382 }
383 }
384
385 static void menu_cb_sendBackToCallingProgram (Editor me, EDITOR_ARGS) {
386 if (my data()) {
387 structMelderFile file { };
388 MelderFolder_getFile (Melder_preferencesFolder(), U"praat_backToCaller.Data", & file);
389 Data_writeToTextFile (my data(), & file);
390 /*
391 We used to do:
392 */
393 //sendsocket (Melder_peek32to8 (my callbackSocket.get()), Melder_peek32to8 (my data() -> name.get()));
394 /*
395 From Praat 7 on, this is no longer possible.
396 Either we have to find a different way to notify the calling program,
397 or we don't notify them, so that they will have to poll (which was needed on the Mac anyway).
398 (this comment 2024-11-16)
399 */
400 }
401 my v_goAway ();
402 }
403
404 static void menu_cb_close (Editor me, EDITOR_ARGS) {
405 my v_goAway ();
406 if (optionalInterpreter && (optionalInterpreter -> optionalOwningEnvironmentEditor() == me || optionalInterpreter -> optionalDynamicEnvironmentEditor() == me))
407 optionalInterpreter -> undangleEditorEnvironments ();
408 }
409
410 static void menu_cb_quit (Editor /* me */, EDITOR_ARGS) {
411 Gui_runQuitApplicationCallback ();
412 }
413
414 static void menu_cb_undo (Editor me, EDITOR_ARGS) {
415 if (! my data())
416 return; // apparently we aren't *meant* to have data
417 if (my undo.position == 0)
418 return; // nothing to restore (just in case the Undo button is inappropriately active)
419 Daata savedData = my undo.data [my undo.position].get();
420 conststring32 savedDescription = my undo.description [my undo.position].get();
421 my undo.position -= 1;
422 if (! savedData)
423 return;
424 my v_restoreDataFromUndo (savedData);
425 /*
426 The new text on the Undo button will be the description of the next-lower saved commmand.
427 */
428 char32 buttonText [100];
429 if (my undo.position == 0) {
430 GuiThing_setSensitive (my undoButton, false);
431 GuiMenuItem_setText (my undoButton, U"Can't undo");
432 } else {
433 conststring32 nextLowerSavedDescription = my undo.description [my undo.position].get();
434 Melder_sprint (buttonText,100, U"Undo ", nextLowerSavedDescription);
435 GuiMenuItem_setText (my undoButton, buttonText);
436 }
437 /*
438 The new text on the Redo button will be the description of the commmand just undone.
439 */
440 GuiThing_setSensitive (my redoButton, true);
441 Melder_sprint (buttonText,100, U"Redo ", savedDescription);
442 GuiMenuItem_setText (my redoButton, buttonText);
443
444 Editor_broadcastDataChanged (me);
445 }
446
447 static void menu_cb_redo (Editor me, EDITOR_ARGS) {
448 if (! my data())
449 return; // apparently we aren't *meant* to have data
450 if (my undo.position == my undo.MAX_DEPTH)
451 return; // nothing to restore (just in case the Redo button is inappropriately active)
452 my undo.position += 1;
453 Daata savedData = my undo.data [my undo.position].get();
454 conststring32 savedDescription = my undo.description [my undo.position].get();
455 if (! savedData)
456 return;
457 my v_restoreDataFromUndo (savedData);
458 /*
459 The new text on the Redo button will be the description of the next-higher saved commmand.
460 */
461 char32 buttonText [100];
462 if (my undo.position == my undo.MAX_DEPTH || ! my undo.data [my undo.position + 1]) {
463 GuiThing_setSensitive (my redoButton, false);
464 GuiMenuItem_setText (my redoButton, U"Can't redo");
465 } else {
466 conststring32 nextHigherSavedDescription = my undo.description [my undo.position + 1].get();
467 Melder_sprint (buttonText,100, U"Redo ", nextHigherSavedDescription);
468 GuiMenuItem_setText (my redoButton, buttonText);
469 }
470 /*
471 The new text on the Undo button will be the description of the commmand just redone.
472 */
473 GuiThing_setSensitive (my undoButton, true);
474 Melder_sprint (buttonText,100, U"Undo ", savedDescription);
475 GuiMenuItem_setText (my undoButton, buttonText);
476
477 Editor_broadcastDataChanged (me);
478 }
479
480 static void menu_cb_clearUndoHistory (Editor me, EDITOR_ARGS) {
481 for (integer i = 1; i <= my undo.MAX_DEPTH; i ++) {
482 my undo.data [i]. reset();
483 my undo.description [i]. reset();
484 }
485 my undo.position = 0;
486 GuiThing_setSensitive (my undoButton, false);
487 GuiThing_setSensitive (my redoButton, false);
488 GuiThing_setSensitive (my clearUndoHistoryButton, false);
489 GuiMenuItem_setText (my undoButton, U"Can't undo");
490 GuiMenuItem_setText (my redoButton, U"Can't redo");
491 }
492
493 static void menu_cb_searchManual (Editor /* me */, EDITOR_ARGS) {
494 Melder_search ();
495 }
496
497 static void menu_cb_newScript (Editor me, EDITOR_ARGS) {
498 autoScriptEditor scriptEditor = ScriptEditor_createFromText (me, nullptr);
499 scriptEditor.releaseToUser();
500 }
501
502 static void menu_cb_openScript (Editor me, EDITOR_ARGS) {
503 autoScriptEditor scriptEditor = ScriptEditor_createFromText (me, nullptr);
504 TextEditor_showOpen (scriptEditor.get());
505 scriptEditor.releaseToUser();
506 }
507
508 void structEditor :: v_createMenuItems_edit (EditorMenu menu) {
509 if (our data()) {
510 our undoButton = EditorMenu_addCommand (menu, U"Can't undo", GuiMenu_INSENSITIVE + 'Z', menu_cb_undo);
511 our redoButton = EditorMenu_addCommand (menu, U"Can't redo", GuiMenu_INSENSITIVE + 'Y', menu_cb_redo);
512 our clearUndoHistoryButton = EditorMenu_addCommand (menu, U"Clear undo history", GuiMenu_INSENSITIVE, menu_cb_clearUndoHistory);
513 }
514 }
515
516 static void INFO_EDITOR__settingsReport (Editor me, EDITOR_ARGS) {
517 INFO_EDITOR
518 Thing_info (me);
519 INFO_EDITOR_END
520 }
521
522 static void INFO_DATA__info (Editor me, EDITOR_ARGS) {
523 INFO_DATA
524 if (my data())
525 Thing_info (my data());
526 INFO_DATA_END
527 }
528
529 void structEditor :: v_createMenus () {
530 v_createMenuItems_prefs (our fileMenu);
531 v_createMenuItems_save (our fileMenu);
532 if (our v_hasEditMenu ()) {
533 our editMenu = Editor_addMenu (this, U"Edit", 0);
534 v_createMenuItems_edit (our editMenu);
535 }
536 }
537
538 static void gui_window_cb_goAway (Editor me) {
539 Melder_assert (me);
540 Melder_assert (Thing_isa (me, classEditor));
541 my v_goAway ();
542 }
543
544 void praat_addCommandsToEditor (Editor me);
545 void Editor_init (Editor me, int x, int y, int width, int height, conststring32 title, Daata data) {
546 DataGui_init (me, data, true, me); // I am my own boss! BUG: check editability
547 /*
548 Zero widths are taken from the preferences.
549 */
550 if (width == 0)
551 width = my classPref_shellWidth();
552 if (height == 0)
553 height = my classPref_shellHeight();
554
555 double xmin, ymin, widthmax, heightmax;
556 Gui_getWindowPositioningBounds (& xmin, & ymin, & widthmax, & heightmax);
557 /*
558 Negative widths are relative to the whole screen.
559 */
560 if (width < 0)
561 width += (int) widthmax;
562 if (height < 0)
563 height += (int) heightmax;
564 /*
565 Don't start with a maximized window, because then the user doesn't know what a click on the maximize button means.
566 */
567 Melder_clipRight (& width, (int) widthmax - 100);
568 Melder_clipRight (& height, (int) heightmax - 100);
569 /*
570 Make sure that the window has at least a sane size,
571 just in case the user made the previous window very small (Praat's FunctionEditor saves the last size),
572 or the user edited the preferences file (which might save a window size).
573 */
574 Melder_clipLeft (200, & width);
575 Melder_clipLeft (150, & height);
576 /*
577 Now that the size is right, establish the position.
578 */
579 int left, right, top, bottom;
580 if (x > 0) {
581 /*
582 Positive x: relative to the left edge of the screen.
583 */
584 left = (int) xmin + x;
585 right = left + width;
586 } else if (x < 0) {
587 /*
588 Negative x: relative to the right edge of the screen.
589 */
590 right = (int) xmin + (int) widthmax + x;
591 left = right - width;
592 } else {
593 /*
594 Zero x: randomize between the left and right edge of the screen.
595 */
596 left = (int) NUMrandomInteger ((int) xmin + 4, (int) xmin + (int) widthmax - width - 4);
597 right = left + width;
598 }
599 if (y > 0) {
600 /*
601 Positive y: relative to the top of the screen.
602 */
603 top = (int) ymin + y;
604 bottom = top + height;
605 } else if (y < 0) {
606 /*
607 Negative y: relative to the bottom of the screen.
608 */
609 bottom = (int) ymin + (int) heightmax + y;
610 top = bottom - height;
611 } else {
612 /*
613 Zero y: randomize between the top and bottom of the screen.
614 */
615 top = (int) NUMrandomInteger ((int) ymin + 4, (int) ymin + (int) heightmax - height - 4);
616 //Melder_casual (ymin, U" ", heightmax, U" ", height, U" ", top);
617 bottom = top + height;
618 }
619 #if defined (macintoshXXX) || gtk
620 top += Machine_getTitleBarHeight ();
621 bottom += Machine_getTitleBarHeight ();
622 #endif
623 my windowForm = GuiWindow_create (left, top, width, height, 450, 350, title,
624 gui_window_cb_goAway, me, my v_canFullScreen () ? GuiWindow_FULLSCREEN : 0);
625 Thing_setName (me, title);
626
627 /*
628 Create menus.
629 */
630
631 if (my v_hasMenuBar ())
632 GuiWindow_addMenuBar (my windowForm);
633
634 my v_createChildren ();
635
636 if (my v_hasMenuBar ()) {
637 my fileMenu = Editor_addMenu (me, U"File", 0);
638 if (my v_canReportSettings ()) {
639 EditorMenu_addCommand (my fileMenu, U"Editor info || Settings report", 0, INFO_EDITOR__settingsReport);
640 if (my data())
641 EditorMenu_addCommand (my fileMenu, Melder_cat (Thing_className (my data()), U" info"), 0, INFO_DATA__info);
642 }
643 my v_createMenus ();
644 EditorMenu helpMenu = Editor_addMenu (me, U"Help", 0);
645 my v_createMenuItems_help (helpMenu);
646 EditorMenu_addCommand (helpMenu, U"-- search --", 0, nullptr);
647 my searchButton = EditorMenu_addCommand (helpMenu, U"Search manual...", 'M', menu_cb_searchManual);
648 if (my v_scriptable ()) {
649 EditorMenu_addCommand (my fileMenu, U"-- scripting --", 0, 0);
650 EditorMenu_addCommand (my fileMenu, U"New editor script", 0, menu_cb_newScript);
651 EditorMenu_addCommand (my fileMenu, U"Open editor script...", 0, menu_cb_openScript);
652 }
653 /*
654 Add the scripted commands.
655 */
656 praat_addCommandsToEditor (me);
657 EditorMenu_addCommand (my fileMenu, U"-- closing --", 0, 0);
658 if (my callbackSocket)
659 EditorMenu_addCommand (my fileMenu, U"Send back to calling program", 0, menu_cb_sendBackToCallingProgram);
660 EditorMenu_addCommand (my fileMenu, U"Close", 'W', menu_cb_close);
661 #ifndef macintosh
662 EditorMenu_addCommand (my fileMenu, Melder_cat (U"Quit ", Melder_upperCaseAppName (), U" || Quit"), 'Q', menu_cb_quit);
663 #endif
664 }
665 GuiThing_show (my windowForm);
666 }
667
668 void Editor_save (Editor me, conststring32 cattableDescription) {
669 /*
670 Construct the text for the Undo button before `cattableDescription` goes invalid.
671 */
672 char32 undoText [100];
673 Melder_sprint (undoText,100, U"Undo ", cattableDescription);
674 /*
675 Save the data and description into the undo buffer.
676 */
677 autostring32 descriptionToSaveForUndo = Melder_dup (cattableDescription); // uncat before calling any virtual function
678 autoDaata dataToSaveForUndo = my v_dataToSaveForUndo();
679 if (! dataToSaveForUndo)
680 return;
681 if (my undo.position == my undo.MAX_DEPTH)
682 for (integer i = 1; i < my undo.MAX_DEPTH; i ++) {
683 my undo.data [i] = my undo.data [i + 1].move();
684 my undo.description [i] = my undo.description [i + 1].move();
685 }
686 else
687 my undo.position += 1;
688 my undo.data [my undo.position] = dataToSaveForUndo.move();
689 my undo.description [my undo.position] = descriptionToSaveForUndo.move();
690 for (integer i = my undo.position + 1; i < my undo.MAX_DEPTH; i ++) {
691 my undo.data [i]. reset();
692 my undo.description [i]. reset();
693 }
694 /*
695 Update some Undo buttons.
696 */
697 if (! my undoButton)
698 return;
699 GuiThing_setSensitive (my undoButton, true);
700 GuiMenuItem_setText (my undoButton, undoText);
701 GuiThing_setSensitive (my clearUndoHistoryButton, true);
702 }
703
704 /* End of file Editor.cpp */