| 1 |
#ifndef _Editor_h_ |
| 2 |
#define _Editor_h_ |
| 3 |
/* Editor.h |
| 4 |
* |
| 5 |
* Copyright (C) 1992-2020,2022,2023,2025 Paul Boersma |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* See the GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "DataGui.h" |
| 22 |
#include "Collection.h" |
| 23 |
#include "Gui.h" |
| 24 |
#include "Ui.h" |
| 25 |
#include "Graphics.h" |
| 26 |
|
| 27 |
Thing_declare (Editor); |
| 28 |
Thing_declare (ScriptEditor); |
| 29 |
|
| 30 |
Thing_define (EditorMenu, Thing) { |
| 31 |
Editor d_editor; |
| 32 |
autostring32 menuTitle; |
| 33 |
GuiMenu menuWidget; |
| 34 |
OrderedOf<structEditorCommand> commands; |
| 35 |
}; |
| 36 |
|
| 37 |
typedef MelderCallback <void, structDataGui, EditorCommand, UiForm, integer /*narg*/, Stackel /*args*/, conststring32, Interpreter> DataGuiCommandCallback; |
| 38 |
typedef MelderCallback <void, structEditor, EditorCommand, UiForm, integer /*narg*/, Stackel /*args*/, conststring32, Interpreter> EditorCommandCallback; |
| 39 |
|
| 40 |
Thing_define (EditorCommand, Thing) { |
| 41 |
Editor d_editor; |
| 42 |
DataGui sender; |
| 43 |
EditorMenu menu; |
| 44 |
autostring32 itemTitle; |
| 45 |
GuiMenuItem itemWidget; |
| 46 |
DataGuiCommandCallback commandCallback; |
| 47 |
autostring32 script; |
| 48 |
autoUiForm d_uiform; |
| 49 |
}; |
| 50 |
|
| 51 |
typedef MelderCallback <void, structEditor> Editor_DataChangedCallback; |
| 52 |
typedef MelderCallback <void, structEditor> Editor_DestructionCallback; |
| 53 |
|
| 54 |
typedef MelderCallback <void, structEditor, autoDaata /* publication */> Editor_PublicationCallback; |
| 55 |
|
| 56 |
Thing_define (Editor, DataGui) { |
| 57 |
GuiWindow windowForm; |
| 58 |
GuiMenuItem undoButton, redoButton, clearUndoHistoryButton, searchButton; |
| 59 |
OrderedOf<structEditorMenu> menus; |
| 60 |
|
| 61 |
struct Undo { |
| 62 |
static constexpr integer MAX_DEPTH = 10; |
| 63 |
autoDaata data [1+MAX_DEPTH]; // the data that can be displayed and edited |
| 64 |
autostring32 description [1+MAX_DEPTH]; |
| 65 |
integer position = 0; |
| 66 |
} undo; |
| 67 |
virtual autoDaata v_dataToSaveForUndo () { |
| 68 |
return our data() ? Data_copy (our data()) : autoDaata(); |
| 69 |
} |
| 70 |
virtual void v_restoreDataFromUndo (Daata savedData) { |
| 71 |
Thing_swap (our data(), savedData); |
| 72 |
} |
| 73 |
|
| 74 |
Editor_DataChangedCallback d_dataChangedCallback; |
| 75 |
Editor_DestructionCallback d_destructionCallback; |
| 76 |
Editor_PublicationCallback d_publicationCallback; |
| 77 |
autostring32 callbackSocket; |
| 78 |
|
| 79 |
void v9_destroy () noexcept |
| 80 |
override; |
| 81 |
void v1_info () |
| 82 |
override; |
| 83 |
void v_nameChanged () |
| 84 |
override; // sets the window and icon titles to reflect the new name |
| 85 |
|
| 86 |
virtual void v_goAway () { forget_nozero (this); } |
| 87 |
virtual bool v_canFullScreen () { return false; } |
| 88 |
virtual bool v_scriptable () { return true; } |
| 89 |
|
| 90 |
virtual bool v_hasMenuBar () { return true; } |
| 91 |
virtual bool v_canReportSettings () { return true; } |
| 92 |
virtual bool v_hasEditMenu () { return true; } |
| 93 |
virtual bool v_hasPlayMenu () { return false; } |
| 94 |
virtual bool v_hasHelpMenu () { return true; } |
| 95 |
EditorMenu fileMenu, editMenu, queryMenu, playMenu, helpMenu; |
| 96 |
void v_createMenus () |
| 97 |
override; |
| 98 |
void v_createMenuItems_edit (EditorMenu menu) |
| 99 |
override; |
| 100 |
|
| 101 |
virtual void v_createChildren () { } |
| 102 |
|
| 103 |
virtual void v1_dataChanged (Editor /* sender */) { } |
| 104 |
|
| 105 |
#include "Editor_prefs.h" |
| 106 |
}; |
| 107 |
|
| 108 |
GuiMenuItem DataGuiMenu_addCommand (EditorMenu me, conststring32 itemTitle /* cattable */, uint32 flags, |
| 109 |
DataGuiCommandCallback commandCallback, DataGui sender); |
| 110 |
GuiMenuItem EditorMenu_addCommand (EditorMenu me, conststring32 itemTitle /* cattable */, uint32 flags, |
| 111 |
EditorCommandCallback commandCallback); |
| 112 |
GuiMenuItem EditorCommand_getItemWidget (EditorCommand me); |
| 113 |
|
| 114 |
EditorMenu Editor_addMenu (Editor me, conststring32 menuTitle, uint32 flags); |
| 115 |
GuiObject EditorMenu_getMenuWidget (EditorMenu me); |
| 116 |
|
| 117 |
GuiMenuItem Editor_addCommand (Editor me, conststring32 menuTitle, conststring32 itemTitle, uint32 flags, EditorCommandCallback commandCallback); |
| 118 |
GuiMenuItem Editor_addCommandScript (Editor me, conststring32 menuTitle, conststring32 itemTitle, uint32 flags, |
| 119 |
conststring32 script); |
| 120 |
void Editor_setMenuSensitive (Editor me, conststring32 menu, bool sensitive); |
| 121 |
|
| 122 |
inline void Editor_raise (Editor me) |
| 123 |
/* |
| 124 |
Message: "move your window to the front", i.e. |
| 125 |
if you are invisible, then make your window visible at the front; |
| 126 |
if you are iconized, then deiconize yourself at the front; |
| 127 |
if you are already visible, just move your window to the front." |
| 128 |
*/ |
| 129 |
{ |
| 130 |
GuiThing_show (my windowForm); |
| 131 |
} |
| 132 |
inline void Editor_dataChanged (Editor me, Editor sender) |
| 133 |
/* |
| 134 |
Message: "your data has changed by an action from inside *or* outside yourself, |
| 135 |
so you may e.g. like to redraw yourself." |
| 136 |
*/ |
| 137 |
{ |
| 138 |
my v1_dataChanged (sender); |
| 139 |
} |
| 140 |
inline void Editor_setDataChangedCallback (Editor me, Editor_DataChangedCallback dataChangedCallback) |
| 141 |
/* |
| 142 |
* Message from boss: "notify me by calling this dataChangedCallback every time your data is changed from *inside* yourself." |
| 143 |
* |
| 144 |
* In Praat, the dataChangedCallback is useful if there is more than one editor |
| 145 |
* with the same data; in this case, the owner of all those editors will |
| 146 |
* (in the dataChangedCallback it installed) notify the change to the other editors |
| 147 |
* by sending them a dataChanged () message. |
| 148 |
*/ |
| 149 |
{ |
| 150 |
my d_dataChangedCallback = dataChangedCallback; |
| 151 |
} |
| 152 |
inline void Editor_broadcastDataChanged (Editor me) |
| 153 |
/* |
| 154 |
* Message to boss: "my data has changed by an action from inside myself." |
| 155 |
* |
| 156 |
* The editor has to call this after every menu command, click or key press that causes a change in the data. |
| 157 |
*/ |
| 158 |
{ |
| 159 |
if (my d_dataChangedCallback) |
| 160 |
my d_dataChangedCallback (me); |
| 161 |
} |
| 162 |
inline void Editor_setDestructionCallback (Editor me, Editor_DestructionCallback destructionCallback) |
| 163 |
/* |
| 164 |
* Message from observer: "notify me by calling this destructionCallback every time you destroy yourself." |
| 165 |
* |
| 166 |
* In Praat, "destroying yourself" typically happens when the user closes the editor window |
| 167 |
* or when an object that is being viewed in an editor window is "Remove"d. |
| 168 |
* Typically, the observer will (in the destructionCallback it installed) remove all dangling references to this editor. |
| 169 |
*/ |
| 170 |
{ |
| 171 |
my d_destructionCallback = destructionCallback; |
| 172 |
} |
| 173 |
inline void Editor_broadcastDestruction (Editor me) |
| 174 |
/* |
| 175 |
* Message to boss: "I am destroying all my members and will free myself shortly." |
| 176 |
* |
| 177 |
* The editor calls this once, namely in Editor::v9_destroy(). |
| 178 |
*/ |
| 179 |
{ |
| 180 |
if (my d_destructionCallback) |
| 181 |
my d_destructionCallback (me); |
| 182 |
} |
| 183 |
inline void Editor_setPublicationCallback (Editor me, Editor_PublicationCallback publicationCallback) |
| 184 |
/* |
| 185 |
* Message from boss: "notify me by calling this publicationCallback every time you have a piece of data to publish." |
| 186 |
* |
| 187 |
* In Praat, editors typically "publish" a piece of data when the user chooses |
| 188 |
* things like "Extract selected sound", "Extract visible pitch curve", or "View spectral slice". |
| 189 |
* Typically, the boss will (in the publicationCallback it installed) install the published data in Praat's object list, |
| 190 |
* but the editor doesn't have to know that. |
| 191 |
*/ |
| 192 |
{ |
| 193 |
my d_publicationCallback = publicationCallback; |
| 194 |
} |
| 195 |
inline void Editor_broadcastPublication (Editor me, autoDaata publication) |
| 196 |
/* |
| 197 |
* Message to boss: "I have a piece of data for you to publish." |
| 198 |
* |
| 199 |
* The editor has to call this every time the user wants to "publish" ("Extract") some data from the editor. |
| 200 |
* |
| 201 |
* Constraint: "publication" has to be new data, either "extracted" or "copied" from data in the editor. |
| 202 |
* The boss becomes the owner of this published data, |
| 203 |
* so the call to broadcastPublication() has to transfer ownership of "publication". |
| 204 |
*/ |
| 205 |
{ |
| 206 |
if (my d_publicationCallback) |
| 207 |
my d_publicationCallback (me, publication.move()); |
| 208 |
} |
| 209 |
|
| 210 |
/***** For inheritors. *****/ |
| 211 |
|
| 212 |
void Editor_init (Editor me, int x, int y , int width, int height, |
| 213 |
conststring32 title, Daata data); |
| 214 |
/* |
| 215 |
This creates my shell and my windowForm, |
| 216 |
calls the v_createMenus and v_createChildren methods, |
| 217 |
and manages my shell and my windowForm. |
| 218 |
'width' and 'height' determine the dimensions of the editor: |
| 219 |
if 'width' < 0, the width of the screen is added to it; |
| 220 |
if 'height' < 0, the height of the screeen is added to it; |
| 221 |
if 'width' is 0, the width is based on the children; |
| 222 |
if 'height' is 0, the height is base on the children. |
| 223 |
'x' and 'y' determine the position of the editor: |
| 224 |
if 'x' > 0, 'x' is the distance to the left edge of the screen; |
| 225 |
if 'x' < 0, |'x'| is the distance to the right edge of the screen; |
| 226 |
if 'x' is 0, the editor is horizontally centred on the screen; |
| 227 |
if 'y' > 0, 'y' is the distance to the top of the screen; |
| 228 |
if 'y' < 0, |'y'| is the distance to the bottom of the screen; |
| 229 |
if 'y' is 0, the editor is vertically centred on the screen; |
| 230 |
This routine does not transfer ownership of '*pData' to the Editor, |
| 231 |
and the Editor will not destroy 'data' when the Editor itself is destroyed. |
| 232 |
*/ |
| 233 |
|
| 234 |
void Editor_save (Editor me, conststring32 cattableText); // for Undo |
| 235 |
|
| 236 |
autoUiForm UiForm_createE (EditorCommand cmd, conststring32 title, conststring32 invokingButtonTitle, conststring32 helpTitle); |
| 237 |
void UiForm_parseStringE (EditorCommand cmd, integer narg, Stackel args, conststring32 arguments, Interpreter interpreter); |
| 238 |
autoUiForm UiOutfile_createE (EditorCommand cmd, conststring32 title, conststring32 invokingButtonTitle, conststring32 helpTitle); |
| 239 |
autoUiForm UiInfile_createE (EditorCommand cmd, conststring32 title, conststring32 invokingButtonTitle, conststring32 helpTitle); |
| 240 |
|
| 241 |
EditorCommand Editor_getMenuCommand (Editor me, conststring32 menuTitle, conststring32 itemTitle); |
| 242 |
void Editor_doMenuCommand (Editor me, conststring32 command, integer narg, Stackel args, conststring32 arguments, Interpreter interpreter); |
| 243 |
|
| 244 |
#endif |
| 245 |
/* End of file Editor.h */ |