| 1 |
/* TextEditor.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1997-2026 Paul Boersma, 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 "ScriptEditor.h" |
| 20 |
#include "machine.h" |
| 21 |
#include "../kar/longchar.h" |
| 22 |
#include "EditorM.h" |
| 23 |
#include "../kar/UnicodeData.h" |
| 24 |
|
| 25 |
Thing_implement (TextEditor, Editor, 0); |
| 26 |
|
| 27 |
#include "Prefs_define.h" |
| 28 |
#include "TextEditor_prefs.h" |
| 29 |
#include "Prefs_install.h" |
| 30 |
#include "TextEditor_prefs.h" |
| 31 |
#include "Prefs_copyToInstance.h" |
| 32 |
#include "TextEditor_prefs.h" |
| 33 |
|
| 34 |
static CollectionOf <structTextEditor> theReferencesToAllOpenTextEditors; |
| 35 |
|
| 36 |
/***** TextEditor methods *****/ |
| 37 |
|
| 38 |
void structTextEditor :: v9_destroy () noexcept { |
| 39 |
our openDialog. reset(); // don't delay till delete |
| 40 |
our saveDialog. reset(); // don't delay till delete |
| 41 |
theReferencesToAllOpenTextEditors. undangleItem (this); |
| 42 |
TextEditor_Parent :: v9_destroy (); |
| 43 |
} |
| 44 |
|
| 45 |
void structTextEditor :: v_nameChanged () { |
| 46 |
if (our v_fileBased ()) { |
| 47 |
/* |
| 48 |
We totally ignore the name that our boss wants to give us. |
| 49 |
Instead, we compose the window title from three ingredients: |
| 50 |
|
| 51 |
(1) whether we are already associated with a file or not; |
| 52 |
(2) if so, the full file path; |
| 53 |
(3) whether our text has been modified (i.e. whether we are "dirty"). |
| 54 |
|
| 55 |
(last checked 2023-02-25) |
| 56 |
*/ |
| 57 |
const bool dirtinessAlreadyShown = GuiWindow_setDirty (our windowForm, our dirty); |
| 58 |
static MelderString windowTitle; |
| 59 |
if (MelderFile_isNull (& our file)) { |
| 60 |
MelderString_copy (& windowTitle, U"(untitled"); |
| 61 |
if (our dirty && ! dirtinessAlreadyShown) |
| 62 |
MelderString_append (& windowTitle, U", modified"); |
| 63 |
MelderString_append (& windowTitle, U")"); |
| 64 |
} else { |
| 65 |
MelderString_copy (& windowTitle, U"File ", MelderFile_messageName (& our file)); |
| 66 |
if (our dirty && ! dirtinessAlreadyShown) |
| 67 |
MelderString_append (& windowTitle, U" (modified)"); |
| 68 |
} |
| 69 |
GuiShell_setTitle (our windowForm, windowTitle.string); |
| 70 |
} else { |
| 71 |
/* |
| 72 |
We will set our window title to the name that our boss wants. |
| 73 |
This occurs e.g. in the Info window. |
| 74 |
(last checked 2023-02-25) |
| 75 |
*/ |
| 76 |
TextEditor_Parent :: v_nameChanged (); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
#pragma mark - File menu |
| 81 |
|
| 82 |
static void openDocument (TextEditor me, MelderFile file) { |
| 83 |
for (integer ieditor = 1; ieditor <= theReferencesToAllOpenTextEditors.size; ieditor ++) { |
| 84 |
TextEditor editor = theReferencesToAllOpenTextEditors.at [ieditor]; |
| 85 |
if (editor != me && MelderFile_equal (file, & editor -> file)) { |
| 86 |
Editor_raise (editor); |
| 87 |
/* |
| 88 |
Destruction alarm! |
| 89 |
When we combine the destruction of an object with the presentation of a message, |
| 90 |
we shall always follow the "build message -- destroy -- show message" paradigm. |
| 91 |
Actually, in this case this is not only safe, but also crucial, |
| 92 |
because at the time of writing (2019-04-28) the owner of `file` is owned by `me`, |
| 93 |
so that destroying `me` would dangle `file`. |
| 94 |
*/ |
| 95 |
Melder_appendError (U"Text file ", file, U" is already open."); |
| 96 |
forget (me); |
| 97 |
Melder_flushError (); |
| 98 |
return; |
| 99 |
} |
| 100 |
} |
| 101 |
autostring32 text = MelderFile_readText (file); |
| 102 |
GuiText_setString (my textWidget, text.get()); |
| 103 |
/* |
| 104 |
GuiText_setString has invoked the changeCallback, |
| 105 |
which has set `my dirty` to `true`. Fix this. |
| 106 |
*/ |
| 107 |
my dirty = false; |
| 108 |
MelderFile_copy (file, & my file); // not until the file has been safely read |
| 109 |
Thing_setName (me, nullptr); |
| 110 |
} |
| 111 |
|
| 112 |
static void newDocument (TextEditor me) { |
| 113 |
GuiText_setString (my textWidget, U""); // implicitly sets my dirty to `true` |
| 114 |
my dirty = false; |
| 115 |
MelderFile_setToNull (& my file); |
| 116 |
if (my v_fileBased ()) |
| 117 |
Thing_setName (me, nullptr); |
| 118 |
} |
| 119 |
|
| 120 |
static void saveDocument (TextEditor me, MelderFile file) { |
| 121 |
autostring32 text = GuiText_getString (my textWidget); |
| 122 |
MelderFile_writeText_e (file, text.get(), Melder_getOutputEncoding ()); |
| 123 |
my dirty = false; |
| 124 |
MelderFile_copy (file, & my file); // not until the file has been safely written |
| 125 |
if (my v_fileBased ()) |
| 126 |
Thing_setName (me, nullptr); |
| 127 |
} |
| 128 |
|
| 129 |
static void closeDocument (TextEditor me) { |
| 130 |
forget (me); |
| 131 |
} |
| 132 |
|
| 133 |
static void cb_open_ok (UiForm sendingForm, integer /* narg */, Stackel /* args */, conststring32 /* sendingString */, |
| 134 |
Interpreter /* interpreter */, conststring32 /* invokingButtonTitle */, bool /* modified */, void *void_me, Editor /* optionalEditor */) |
| 135 |
{ |
| 136 |
iam (TextEditor); |
| 137 |
MelderFile file = UiFile_getFile (sendingForm); |
| 138 |
openDocument (me, file); |
| 139 |
} |
| 140 |
|
| 141 |
static void cb_showOpen (EditorCommand cmd) { |
| 142 |
TextEditor me = (TextEditor) cmd -> d_editor; |
| 143 |
if (! my openDialog) |
| 144 |
my openDialog = UiInfile_create (my windowForm, nullptr, U"Open", cb_open_ok, me, nullptr, nullptr, false); |
| 145 |
UiInfile_do (my openDialog.get()); |
| 146 |
} |
| 147 |
|
| 148 |
static void cb_saveAs_ok (UiForm sendingForm, integer /* narg */, Stackel /* args */, conststring32 /* sendingString */, |
| 149 |
Interpreter /* interpreter */, conststring32 /* invokingButtonTitle */, bool /* modified */, void *void_me, Editor /* optionalEditor */) |
| 150 |
{ |
| 151 |
iam (TextEditor); |
| 152 |
MelderFile file = UiFile_getFile (sendingForm); |
| 153 |
saveDocument (me, file); |
| 154 |
} |
| 155 |
|
| 156 |
static void menu_cb_saveAs (TextEditor me, EDITOR_ARGS) { |
| 157 |
if (! my saveDialog) |
| 158 |
my saveDialog = UiOutfile_create (my windowForm, nullptr, U"Save", cb_saveAs_ok, me, nullptr, nullptr); |
| 159 |
char32 defaultName [300]; |
| 160 |
Melder_sprint (defaultName,300, |
| 161 |
my v_fileBased () ? |
| 162 |
MelderFile_isNull (& my file) ? |
| 163 |
Melder_cat (U"untitled", my v_extension ()) |
| 164 |
: |
| 165 |
MelderFile_name (& my file) |
| 166 |
: |
| 167 |
U"info.txt" |
| 168 |
); |
| 169 |
UiOutfile_do (my saveDialog.get(), defaultName); |
| 170 |
} |
| 171 |
|
| 172 |
static void gui_button_cb_saveAndOpen (EditorCommand cmd, GuiButtonEvent /* event */) { |
| 173 |
TextEditor me = (TextEditor) cmd -> d_editor; |
| 174 |
GuiThing_hide (my dirtyOpenDialog); |
| 175 |
if (MelderFile_isNull (& my file)) { |
| 176 |
menu_cb_saveAs (me, cmd, nullptr, 0, nullptr, nullptr, nullptr); |
| 177 |
} else { |
| 178 |
try { |
| 179 |
saveDocument (me, & my file); |
| 180 |
} catch (MelderError) { |
| 181 |
Melder_flushError (); |
| 182 |
return; |
| 183 |
} |
| 184 |
cb_showOpen (cmd); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
static void gui_button_cb_cancelOpen (EditorCommand cmd, GuiButtonEvent /* event */) { |
| 189 |
TextEditor me = (TextEditor) cmd -> d_editor; |
| 190 |
GuiThing_hide (my dirtyOpenDialog); |
| 191 |
} |
| 192 |
|
| 193 |
static void gui_button_cb_discardAndOpen (EditorCommand cmd, GuiButtonEvent /* event */) { |
| 194 |
TextEditor me = (TextEditor) cmd -> d_editor; |
| 195 |
GuiThing_hide (my dirtyOpenDialog); |
| 196 |
cb_showOpen (cmd); |
| 197 |
} |
| 198 |
|
| 199 |
static void menu_cb_open (TextEditor me, EDITOR_ARGS) { |
| 200 |
if (my dirty) { |
| 201 |
if (! my dirtyOpenDialog) { |
| 202 |
int buttonWidth = 120, buttonSpacing = 20; |
| 203 |
my dirtyOpenDialog = GuiDialog_create (my windowForm, |
| 204 |
150, 70, |
| 205 |
Gui_LEFT_DIALOG_SPACING + 3 * buttonWidth + 2 * buttonSpacing + Gui_RIGHT_DIALOG_SPACING, |
| 206 |
Gui_TOP_DIALOG_SPACING + Gui_TEXTFIELD_HEIGHT + Gui_VERTICAL_DIALOG_SPACING_SAME + 2 * Gui_BOTTOM_DIALOG_SPACING + Gui_PUSHBUTTON_HEIGHT, |
| 207 |
U"Text changed", nullptr, nullptr, GuiDialog_Modality::MODAL |
| 208 |
); |
| 209 |
GuiLabel_createShown (my dirtyOpenDialog, |
| 210 |
Gui_LEFT_DIALOG_SPACING, - Gui_RIGHT_DIALOG_SPACING, |
| 211 |
Gui_TOP_DIALOG_SPACING, Gui_TOP_DIALOG_SPACING + Gui_LABEL_HEIGHT, |
| 212 |
U"The text has changed! Save changes?", 0 |
| 213 |
); |
| 214 |
int x = Gui_LEFT_DIALOG_SPACING, y = - Gui_BOTTOM_DIALOG_SPACING; |
| 215 |
GuiButton_createShown (my dirtyOpenDialog, |
| 216 |
x, x + buttonWidth, y - Gui_PUSHBUTTON_HEIGHT, y, |
| 217 |
U"Discard & Open", gui_button_cb_discardAndOpen, cmd, 0 |
| 218 |
); |
| 219 |
x += buttonWidth + buttonSpacing; |
| 220 |
GuiButton_createShown (my dirtyOpenDialog, |
| 221 |
x, x + buttonWidth, y - Gui_PUSHBUTTON_HEIGHT, y, |
| 222 |
U"Cancel", gui_button_cb_cancelOpen, cmd, 0 |
| 223 |
); |
| 224 |
x += buttonWidth + buttonSpacing; |
| 225 |
GuiButton_createShown (my dirtyOpenDialog, |
| 226 |
x, x + buttonWidth, y - Gui_PUSHBUTTON_HEIGHT, y, |
| 227 |
U"Save & Open", gui_button_cb_saveAndOpen, cmd, 0 |
| 228 |
); |
| 229 |
} |
| 230 |
GuiThing_show (my dirtyOpenDialog); |
| 231 |
} else { |
| 232 |
cb_showOpen (cmd); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
static void gui_button_cb_saveAndNew (EditorCommand cmd, GuiButtonEvent /* event */) { |
| 237 |
TextEditor me = (TextEditor) cmd -> d_editor; |
| 238 |
GuiThing_hide (my dirtyNewDialog); |
| 239 |
if (MelderFile_isNull (& my file)) { |
| 240 |
menu_cb_saveAs (me, cmd, nullptr, 0, nullptr, nullptr, nullptr); |
| 241 |
} else { |
| 242 |
try { |
| 243 |
saveDocument (me, & my file); |
| 244 |
} catch (MelderError) { |
| 245 |
Melder_flushError (); |
| 246 |
return; |
| 247 |
} |
| 248 |
newDocument (me); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
static void gui_button_cb_cancelNew (EditorCommand cmd, GuiButtonEvent /* event */) { |
| 253 |
TextEditor me = (TextEditor) cmd -> d_editor; |
| 254 |
GuiThing_hide (my dirtyNewDialog); |
| 255 |
} |
| 256 |
|
| 257 |
static void gui_button_cb_discardAndNew (EditorCommand cmd, GuiButtonEvent /* event */) { |
| 258 |
TextEditor me = (TextEditor) cmd -> d_editor; |
| 259 |
GuiThing_hide (my dirtyNewDialog); |
| 260 |
newDocument (me); |
| 261 |
} |
| 262 |
|
| 263 |
static void menu_cb_new (TextEditor me, EDITOR_ARGS) { |
| 264 |
if (my v_fileBased () && my dirty) { |
| 265 |
if (! my dirtyNewDialog) { |
| 266 |
int buttonWidth = 120, buttonSpacing = 20; |
| 267 |
my dirtyNewDialog = GuiDialog_create (my windowForm, |
| 268 |
150, 70, Gui_LEFT_DIALOG_SPACING + 3 * buttonWidth + 2 * buttonSpacing + Gui_RIGHT_DIALOG_SPACING, |
| 269 |
Gui_TOP_DIALOG_SPACING + Gui_TEXTFIELD_HEIGHT + Gui_VERTICAL_DIALOG_SPACING_SAME + 2 * Gui_BOTTOM_DIALOG_SPACING + Gui_PUSHBUTTON_HEIGHT, |
| 270 |
U"Text changed", nullptr, nullptr, GuiDialog_Modality::MODAL |
| 271 |
); |
| 272 |
GuiLabel_createShown (my dirtyNewDialog, |
| 273 |
Gui_LEFT_DIALOG_SPACING, - Gui_RIGHT_DIALOG_SPACING, |
| 274 |
Gui_TOP_DIALOG_SPACING, Gui_TOP_DIALOG_SPACING + Gui_LABEL_HEIGHT, |
| 275 |
U"The text has changed! Save changes?", 0 |
| 276 |
); |
| 277 |
int x = Gui_LEFT_DIALOG_SPACING, y = - Gui_BOTTOM_DIALOG_SPACING; |
| 278 |
GuiButton_createShown (my dirtyNewDialog, |
| 279 |
x, x + buttonWidth, y - Gui_PUSHBUTTON_HEIGHT, y, |
| 280 |
U"Discard & New", gui_button_cb_discardAndNew, cmd, 0 |
| 281 |
); |
| 282 |
x += buttonWidth + buttonSpacing; |
| 283 |
GuiButton_createShown (my dirtyNewDialog, |
| 284 |
x, x + buttonWidth, y - Gui_PUSHBUTTON_HEIGHT, y, |
| 285 |
U"Cancel", gui_button_cb_cancelNew, cmd, 0 |
| 286 |
); |
| 287 |
x += buttonWidth + buttonSpacing; |
| 288 |
GuiButton_createShown (my dirtyNewDialog, |
| 289 |
x, x + buttonWidth, y - Gui_PUSHBUTTON_HEIGHT, y, |
| 290 |
U"Save & New", gui_button_cb_saveAndNew, cmd, 0 |
| 291 |
); |
| 292 |
} |
| 293 |
GuiThing_show (my dirtyNewDialog); |
| 294 |
} else { |
| 295 |
newDocument (me); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
static void gui_button_cb_cancelReopen (EditorCommand cmd, GuiButtonEvent /* event */) { |
| 300 |
TextEditor me = (TextEditor) cmd -> d_editor; |
| 301 |
GuiThing_hide (my dirtyReopenDialog); |
| 302 |
} |
| 303 |
|
| 304 |
static void gui_button_cb_discardAndReopen (EditorCommand cmd, GuiButtonEvent /* event */) { |
| 305 |
TextEditor me = (TextEditor) cmd -> d_editor; |
| 306 |
GuiThing_hide (my dirtyReopenDialog); |
| 307 |
openDocument (me, & my file); |
| 308 |
} |
| 309 |
|
| 310 |
static void menu_cb_reopen (TextEditor me, EDITOR_ARGS) { |
| 311 |
Melder_assert (my v_fileBased()); |
| 312 |
if (MelderFile_isNull (& my file)) |
| 313 |
Melder_throw (U"Cannot reopen from disk, because the text has never been saved yet."); |
| 314 |
if (my dirty) { |
| 315 |
if (! my dirtyReopenDialog) { |
| 316 |
int buttonWidth = 250, buttonSpacing = 20; |
| 317 |
my dirtyReopenDialog = GuiDialog_create (my windowForm, |
| 318 |
150, 70, Gui_LEFT_DIALOG_SPACING + 2 * buttonWidth + 1 * buttonSpacing + Gui_RIGHT_DIALOG_SPACING, |
| 319 |
Gui_TOP_DIALOG_SPACING + Gui_TEXTFIELD_HEIGHT + Gui_VERTICAL_DIALOG_SPACING_SAME + 2 * Gui_BOTTOM_DIALOG_SPACING + Gui_PUSHBUTTON_HEIGHT, |
| 320 |
U"Text changed", nullptr, nullptr, GuiDialog_Modality::MODAL |
| 321 |
); |
| 322 |
GuiLabel_createShown (my dirtyReopenDialog, |
| 323 |
Gui_LEFT_DIALOG_SPACING, - Gui_RIGHT_DIALOG_SPACING, |
| 324 |
Gui_TOP_DIALOG_SPACING, Gui_TOP_DIALOG_SPACING + Gui_LABEL_HEIGHT, |
| 325 |
U"The text in the editor contains changes! Reopen nevertheless?", 0 |
| 326 |
); |
| 327 |
int x = Gui_LEFT_DIALOG_SPACING, y = - Gui_BOTTOM_DIALOG_SPACING; |
| 328 |
GuiButton_createShown (my dirtyReopenDialog, |
| 329 |
x, x + buttonWidth, y - Gui_PUSHBUTTON_HEIGHT, y, |
| 330 |
U"Keep visible version", gui_button_cb_cancelReopen, cmd, GuiButton_CANCEL |
| 331 |
); |
| 332 |
x += buttonWidth + buttonSpacing; |
| 333 |
GuiButton_createShown (my dirtyReopenDialog, |
| 334 |
x, x + buttonWidth, y - Gui_PUSHBUTTON_HEIGHT, y, |
| 335 |
U"Replace with version from disk", gui_button_cb_discardAndReopen, cmd, GuiButton_DEFAULT |
| 336 |
); |
| 337 |
} |
| 338 |
GuiThing_show (my dirtyReopenDialog); |
| 339 |
} else { |
| 340 |
try { |
| 341 |
openDocument (me, & my file); |
| 342 |
} catch (MelderError) { |
| 343 |
Melder_flushError (); |
| 344 |
return; |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
static void menu_cb_clear (TextEditor me, EDITOR_ARGS) { |
| 350 |
my v_clear (); |
| 351 |
} |
| 352 |
|
| 353 |
static void menu_cb_save (TextEditor me, EDITOR_ARGS) { |
| 354 |
if (MelderFile_isNull (& my file)) { |
| 355 |
menu_cb_saveAs (me, cmd, nullptr, 0, nullptr, nullptr, nullptr); |
| 356 |
} else { |
| 357 |
try { |
| 358 |
saveDocument (me, & my file); |
| 359 |
} catch (MelderError) { |
| 360 |
Melder_flushError (); |
| 361 |
return; |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
static void gui_button_cb_saveAndClose (TextEditor me, GuiButtonEvent /* event */) { |
| 367 |
GuiThing_hide (my dirtyCloseDialog); |
| 368 |
if (MelderFile_isNull (& my file)) { |
| 369 |
menu_cb_saveAs (me, Editor_getMenuCommand (me, U"File", U"Save as..."), nullptr, 0, nullptr, nullptr, nullptr); |
| 370 |
} else { |
| 371 |
try { |
| 372 |
saveDocument (me, & my file); |
| 373 |
} catch (MelderError) { |
| 374 |
Melder_flushError (); |
| 375 |
return; |
| 376 |
} |
| 377 |
closeDocument (me); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
static void gui_button_cb_cancelClose (TextEditor me, GuiButtonEvent /* event */) { |
| 382 |
GuiThing_hide (my dirtyCloseDialog); |
| 383 |
} |
| 384 |
|
| 385 |
static void gui_button_cb_discardAndClose (TextEditor me, GuiButtonEvent /* event */) { |
| 386 |
GuiThing_hide (my dirtyCloseDialog); |
| 387 |
closeDocument (me); |
| 388 |
} |
| 389 |
|
| 390 |
void structTextEditor :: v_goAway () { |
| 391 |
if (our v_fileBased () && our dirty) { |
| 392 |
if (! our dirtyCloseDialog) { |
| 393 |
int buttonWidth = 120, buttonSpacing = 20; |
| 394 |
our dirtyCloseDialog = GuiDialog_create (our windowForm, |
| 395 |
150, 70, Gui_LEFT_DIALOG_SPACING + 3 * buttonWidth + 2 * buttonSpacing + Gui_RIGHT_DIALOG_SPACING, |
| 396 |
Gui_TOP_DIALOG_SPACING + Gui_TEXTFIELD_HEIGHT + Gui_VERTICAL_DIALOG_SPACING_SAME + 2 * Gui_BOTTOM_DIALOG_SPACING + Gui_PUSHBUTTON_HEIGHT, |
| 397 |
U"Text changed", nullptr, nullptr, GuiDialog_Modality::MODAL |
| 398 |
); |
| 399 |
GuiLabel_createShown (our dirtyCloseDialog, |
| 400 |
Gui_LEFT_DIALOG_SPACING, - Gui_RIGHT_DIALOG_SPACING, |
| 401 |
Gui_TOP_DIALOG_SPACING, Gui_TOP_DIALOG_SPACING + Gui_LABEL_HEIGHT, |
| 402 |
U"The text has changed! Save changes?", 0 |
| 403 |
); |
| 404 |
int x = Gui_LEFT_DIALOG_SPACING, y = - Gui_BOTTOM_DIALOG_SPACING; |
| 405 |
GuiButton_createShown (our dirtyCloseDialog, |
| 406 |
x, x + buttonWidth, y - Gui_PUSHBUTTON_HEIGHT, y, |
| 407 |
U"Discard & Close", gui_button_cb_discardAndClose, this, 0 |
| 408 |
); |
| 409 |
x += buttonWidth + buttonSpacing; |
| 410 |
GuiButton_createShown (our dirtyCloseDialog, |
| 411 |
x, x + buttonWidth, y - Gui_PUSHBUTTON_HEIGHT, y, |
| 412 |
U"Cancel", gui_button_cb_cancelClose, this, 0 |
| 413 |
); |
| 414 |
x += buttonWidth + buttonSpacing; |
| 415 |
GuiButton_createShown (our dirtyCloseDialog, |
| 416 |
x, x + buttonWidth, y - Gui_PUSHBUTTON_HEIGHT, y, |
| 417 |
U"Save & Close", gui_button_cb_saveAndClose, this, 0 |
| 418 |
); |
| 419 |
} |
| 420 |
if (our dirtyNewDialog) |
| 421 |
GuiThing_hide (our dirtyNewDialog); |
| 422 |
if (our dirtyOpenDialog) |
| 423 |
GuiThing_hide (our dirtyOpenDialog); |
| 424 |
if (our dirtyReopenDialog) |
| 425 |
GuiThing_hide (our dirtyReopenDialog); |
| 426 |
GuiThing_show (our dirtyCloseDialog); |
| 427 |
} else { |
| 428 |
closeDocument (this); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
#pragma mark - Edit menu |
| 433 |
|
| 434 |
static void menu_cb_undo (TextEditor me, EDITOR_ARGS) { |
| 435 |
GuiText_undo (my textWidget); |
| 436 |
} |
| 437 |
|
| 438 |
static void menu_cb_redo (TextEditor me, EDITOR_ARGS) { |
| 439 |
GuiText_redo (my textWidget); |
| 440 |
} |
| 441 |
|
| 442 |
static void menu_cb_cut (TextEditor me, EDITOR_ARGS) { |
| 443 |
GuiText_cut (my textWidget); // use ((XmAnyCallbackStruct *) call) -> event -> xbutton. time |
| 444 |
} |
| 445 |
|
| 446 |
static void menu_cb_copy (TextEditor me, EDITOR_ARGS) { |
| 447 |
GuiText_copy (my textWidget); |
| 448 |
} |
| 449 |
|
| 450 |
static void menu_cb_paste (TextEditor me, EDITOR_ARGS) { |
| 451 |
GuiText_paste (my textWidget); |
| 452 |
} |
| 453 |
|
| 454 |
static void menu_cb_erase (TextEditor me, EDITOR_ARGS) { |
| 455 |
GuiText_remove (my textWidget); |
| 456 |
} |
| 457 |
|
| 458 |
static bool getSelectedLines (TextEditor me, integer *firstLine, integer *lastLine) { |
| 459 |
integer left, right; |
| 460 |
autostring32 text = GuiText_getStringAndSelectionPosition (my textWidget, & left, & right); |
| 461 |
const integer textLength = Melder_length (text.get()); |
| 462 |
Melder_assert (left >= 0); |
| 463 |
Melder_assert (left <= right); |
| 464 |
if (right > textLength) |
| 465 |
Melder_crash (U"The end of the selection is at position ", right, |
| 466 |
U", which is beyond the end of the text, which is at position ", textLength, U"."); |
| 467 |
integer i = 0; |
| 468 |
*firstLine = 1; |
| 469 |
/* |
| 470 |
Cycle through the text in order to see how many linefeeds we pass. |
| 471 |
*/ |
| 472 |
for (; i < left; i ++) |
| 473 |
if (text [i] == U'\n') |
| 474 |
(*firstLine) ++; |
| 475 |
if (left == right) |
| 476 |
return false; |
| 477 |
*lastLine = *firstLine; |
| 478 |
for (; i < right - 1; i ++) // a newline at the end of a line is ignored (it belongs to the previous line) |
| 479 |
if (text [i] == U'\n') |
| 480 |
(*lastLine) ++; |
| 481 |
return true; |
| 482 |
} |
| 483 |
|
| 484 |
static integer getPositionToInsertTabBeforeSelection (conststring32 text, const integer startingPosition) { |
| 485 |
if (startingPosition == 0) |
| 486 |
return 0; // we will insert a tab at the start of the text |
| 487 |
for (integer position = startingPosition - 1; position >= 0; position --) |
| 488 |
if (text [position] == U'\n') |
| 489 |
return position + 1; // we will insert a tab after the last newline |
| 490 |
return 0; // we were on line 1 and will therefore insert a tab at the start of the text |
| 491 |
} |
| 492 |
static void menu_cb_shiftRight (TextEditor me, EDITOR_ARGS) { |
| 493 |
/* |
| 494 |
Get the old text from the GuiText. |
| 495 |
*/ |
| 496 |
integer leftPosition, rightPosition; // based between characters |
| 497 |
autostring32 oldText = GuiText_getStringAndSelectionPosition (my textWidget, & leftPosition, & rightPosition); |
| 498 |
|
| 499 |
/* |
| 500 |
Convert the old text to the new text. |
| 501 |
*/ |
| 502 |
autoMelderString newText; |
| 503 |
const integer positionToInsertTabBeforeSelection = getPositionToInsertTabBeforeSelection (oldText.get(), leftPosition); |
| 504 |
MelderString_ncopy (& newText, oldText.get(), positionToInsertTabBeforeSelection); |
| 505 |
MelderString_appendCharacter (& newText, U'\t'); |
| 506 |
MelderString_nappend (& newText, & oldText [positionToInsertTabBeforeSelection], |
| 507 |
leftPosition - positionToInsertTabBeforeSelection); |
| 508 |
for (integer position = leftPosition; position < rightPosition; position ++) { |
| 509 |
MelderString_appendCharacter (& newText, oldText [position]); |
| 510 |
if (position < rightPosition - 1 && oldText [position] == U'\n') // a newline at the end of the selection is ignored (it belongs to the previous line) |
| 511 |
MelderString_appendCharacter (& newText, U'\t'); |
| 512 |
} |
| 513 |
const integer newEndOfSelection = newText.length; |
| 514 |
MelderString_append (& newText, & oldText [rightPosition]); |
| 515 |
|
| 516 |
/* |
| 517 |
Put the new text into the GuiText. |
| 518 |
*/ |
| 519 |
GuiText_setString (my textWidget, newText.string); |
| 520 |
GuiText_setSelection (my textWidget, leftPosition + 1, newEndOfSelection); |
| 521 |
GuiText_scrollToSelection (my textWidget); |
| 522 |
#ifdef _WIN32 |
| 523 |
GuiThing_show (my windowForm); |
| 524 |
#endif |
| 525 |
} |
| 526 |
|
| 527 |
static integer getPositionToDeleteTabBeforeSelection (conststring32 text, const integer startingPosition) { |
| 528 |
if (startingPosition == 0) { |
| 529 |
if (text [startingPosition] == U'\t') |
| 530 |
return 0; // we can delete a tab from the start of the text |
| 531 |
else |
| 532 |
return -1; // we cannot delete a tab before the start of the text |
| 533 |
} |
| 534 |
for (integer position = startingPosition - 1; position >= 0; position --) |
| 535 |
if (text [position] == U'\n') { |
| 536 |
if (text [position + 1] == U'\t') |
| 537 |
return position + 1; // we will delete a tab after the last newline |
| 538 |
else |
| 539 |
return -1; // we will not look past the last newline |
| 540 |
} |
| 541 |
return text [0] == U'\t' ? 0 : -1; // we were on line 1 and may therefore delete a tab at the start of the text |
| 542 |
} |
| 543 |
static void menu_cb_shiftLeft (TextEditor me, EDITOR_ARGS) { |
| 544 |
/* |
| 545 |
Get the old text from the GuiText. |
| 546 |
*/ |
| 547 |
integer leftPosition, rightPosition; // based between characters |
| 548 |
autostring32 oldText = GuiText_getStringAndSelectionPosition (my textWidget, & leftPosition, & rightPosition); |
| 549 |
|
| 550 |
/* |
| 551 |
Convert the old text to the new text. |
| 552 |
First the part before the selection. |
| 553 |
*/ |
| 554 |
autoMelderString newText; |
| 555 |
const integer positionToDeleteTabBeforeSelection = getPositionToDeleteTabBeforeSelection (oldText.get(), leftPosition); |
| 556 |
const bool haveToDeleteTabBeforeSelection = ( positionToDeleteTabBeforeSelection >= 0 ); |
| 557 |
if (positionToDeleteTabBeforeSelection == leftPosition) { |
| 558 |
MelderString_ncopy (& newText, oldText.get(), positionToDeleteTabBeforeSelection); |
| 559 |
// skip the tab |
| 560 |
constexpr integer numberOfDeletedTabsInThisPart = 0; |
| 561 |
MelderString_nappend (& newText, & oldText [positionToDeleteTabBeforeSelection + numberOfDeletedTabsInThisPart], |
| 562 |
leftPosition - (positionToDeleteTabBeforeSelection + numberOfDeletedTabsInThisPart)); |
| 563 |
} else if (haveToDeleteTabBeforeSelection) { |
| 564 |
MelderString_ncopy (& newText, oldText.get(), positionToDeleteTabBeforeSelection); |
| 565 |
// skip the tab |
| 566 |
constexpr integer numberOfDeletedTabsInThisPart = 1; |
| 567 |
MelderString_nappend (& newText, & oldText [positionToDeleteTabBeforeSelection + numberOfDeletedTabsInThisPart], |
| 568 |
leftPosition - (positionToDeleteTabBeforeSelection + numberOfDeletedTabsInThisPart)); |
| 569 |
} else { |
| 570 |
MelderString_ncopy (& newText, oldText.get(), leftPosition); |
| 571 |
} |
| 572 |
/* |
| 573 |
Then the part inside the selection. |
| 574 |
*/ |
| 575 |
if (positionToDeleteTabBeforeSelection == leftPosition) { |
| 576 |
constexpr integer numberOfDeletedTabsInThisPart = 1; |
| 577 |
for (integer position = leftPosition + numberOfDeletedTabsInThisPart; position < rightPosition; position ++) |
| 578 |
if (oldText [position] != U'\t' || position > leftPosition && oldText [position - 1] != U'\n') |
| 579 |
MelderString_appendCharacter (& newText, oldText [position]); |
| 580 |
} else { |
| 581 |
constexpr integer numberOfDeletedTabsInThisPart = 0; |
| 582 |
for (integer position = leftPosition + numberOfDeletedTabsInThisPart; position < rightPosition; position ++) |
| 583 |
if (oldText [position] != U'\t' || position > leftPosition && oldText [position - 1] != U'\n') |
| 584 |
MelderString_appendCharacter (& newText, oldText [position]); |
| 585 |
} |
| 586 |
/* |
| 587 |
And finally the part after the selection. |
| 588 |
*/ |
| 589 |
const integer newEndOfSelection = newText.length; |
| 590 |
if (positionToDeleteTabBeforeSelection == rightPosition) |
| 591 |
MelderString_append (& newText, & oldText [rightPosition + 1]); |
| 592 |
else |
| 593 |
MelderString_append (& newText, & oldText [rightPosition]); |
| 594 |
|
| 595 |
/* |
| 596 |
Put the new text into the GuiText. |
| 597 |
*/ |
| 598 |
GuiText_setString (my textWidget, newText.string); |
| 599 |
GuiText_setSelection (my textWidget, leftPosition - |
| 600 |
( haveToDeleteTabBeforeSelection && positionToDeleteTabBeforeSelection != leftPosition ? 1 : 0 ), newEndOfSelection); |
| 601 |
GuiText_scrollToSelection (my textWidget); |
| 602 |
#ifdef _WIN32 |
| 603 |
GuiThing_show (my windowForm); |
| 604 |
#endif |
| 605 |
} |
| 606 |
|
| 607 |
#pragma mark - Search menu |
| 608 |
|
| 609 |
static autostring32 theFindString, theReplaceString; |
| 610 |
static void do_find (TextEditor me) { |
| 611 |
if (! theFindString) // e.g. when the user does "Find again" before having done any "Find" |
| 612 |
return; |
| 613 |
integer left, right; |
| 614 |
autostring32 text = GuiText_getStringAndSelectionPosition (my textWidget, & left, & right); |
| 615 |
char32 *location = str32str (& text [right], theFindString.get()); |
| 616 |
if (location) { |
| 617 |
const integer index = location - text.get(); |
| 618 |
GuiText_setSelection (my textWidget, index, index + Melder_length (theFindString.get())); |
| 619 |
GuiText_scrollToSelection (my textWidget); |
| 620 |
#ifdef _WIN32 |
| 621 |
GuiThing_show (my windowForm); |
| 622 |
#endif |
| 623 |
} else { |
| 624 |
/* |
| 625 |
Try from the start of the document. |
| 626 |
*/ |
| 627 |
location = str32str (text.get(), theFindString.get()); |
| 628 |
if (location) { |
| 629 |
const integer index = location - text.get(); |
| 630 |
GuiText_setSelection (my textWidget, index, index + Melder_length (theFindString.get())); |
| 631 |
GuiText_scrollToSelection (my textWidget); |
| 632 |
#ifdef _WIN32 |
| 633 |
GuiThing_show (my windowForm); |
| 634 |
#endif |
| 635 |
} else { |
| 636 |
Melder_beep (); |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
static void do_replace (TextEditor me) { |
| 642 |
if (! theReplaceString) // e.g. when the user does "Replace again" before having done any "Replace" |
| 643 |
return; |
| 644 |
autostring32 selection = GuiText_getSelection (my textWidget); |
| 645 |
if (! Melder_equ (selection.get(), theFindString.get())) { |
| 646 |
do_find (me); |
| 647 |
return; |
| 648 |
} |
| 649 |
integer left, right; |
| 650 |
autostring32 text = GuiText_getStringAndSelectionPosition (my textWidget, & left, & right); |
| 651 |
GuiText_replace (my textWidget, left, right, theReplaceString.get()); |
| 652 |
GuiText_setSelection (my textWidget, left, left + Melder_length (theReplaceString.get())); |
| 653 |
GuiText_scrollToSelection (my textWidget); |
| 654 |
#ifdef _WIN32 |
| 655 |
GuiThing_show (my windowForm); |
| 656 |
#endif |
| 657 |
} |
| 658 |
|
| 659 |
static void menu_cb_find (TextEditor me, EDITOR_ARGS) { |
| 660 |
EDITOR_FORM (U"Find", nullptr) |
| 661 |
TEXTFIELD (findString, U"Find", U"", 5) |
| 662 |
EDITOR_OK |
| 663 |
if (theFindString) SET_STRING (findString, theFindString.get()); |
| 664 |
EDITOR_DO |
| 665 |
theFindString = Melder_dup (findString); |
| 666 |
#ifdef macintosh |
| 667 |
/* |
| 668 |
Perhaps don't use the system-wide Find pasteboard, |
| 669 |
by which other applications can see what you are searching for in Praat's text windows. |
| 670 |
Remember ever showing your app in Xcode to somebody, |
| 671 |
revealing to your onlooker the name of the person you last looked up in your email? |
| 672 |
*/ |
| 673 |
// NSPasteboard * theFindPasteBoard = [NSPasteboard pasteboardWithName: NSPasteboardNameFind create: NO]; |
| 674 |
// [theFindPasteBoard ...] |
| 675 |
#endif |
| 676 |
do_find (me); |
| 677 |
EDITOR_END |
| 678 |
} |
| 679 |
|
| 680 |
static void menu_cb_findAgain (TextEditor me, EDITOR_ARGS) { |
| 681 |
do_find (me); |
| 682 |
} |
| 683 |
|
| 684 |
static void menu_cb_useSelectionForFind (TextEditor me, EDITOR_ARGS) { |
| 685 |
theFindString = GuiText_getSelection (my textWidget); |
| 686 |
} |
| 687 |
|
| 688 |
static void menu_cb_replace (TextEditor me, EDITOR_ARGS) { |
| 689 |
EDITOR_FORM (U"Find", nullptr) |
| 690 |
COMMENT (U"This is a \"slow\" find-and-replace method;") |
| 691 |
COMMENT (U"if the selected text is identical to the Find string,") |
| 692 |
COMMENT (U"the selected text will be replaced by the Replace string;") |
| 693 |
COMMENT (U"otherwise, the next occurrence of the Find string will be selected.") |
| 694 |
COMMENT (U"So you typically need two clicks on Apply to get a text replaced.") |
| 695 |
TEXTFIELD (findString, U"Find", U"", 5) |
| 696 |
TEXTFIELD (replaceString, U"Replace with", U"", 5) |
| 697 |
EDITOR_OK |
| 698 |
if (theFindString) |
| 699 |
SET_STRING (findString, theFindString.get()); |
| 700 |
if (theReplaceString) |
| 701 |
SET_STRING (replaceString, theReplaceString.get()); |
| 702 |
EDITOR_DO |
| 703 |
theFindString = Melder_dup (findString); |
| 704 |
theReplaceString = Melder_dup (replaceString); |
| 705 |
do_replace (me); |
| 706 |
EDITOR_END |
| 707 |
} |
| 708 |
|
| 709 |
static void menu_cb_replaceAgain (TextEditor me, EDITOR_ARGS) { |
| 710 |
do_replace (me); |
| 711 |
} |
| 712 |
|
| 713 |
static void menu_cb_whereAmI (TextEditor me, EDITOR_ARGS) { |
| 714 |
integer numberOfLinesLeft, numberOfLinesRight; |
| 715 |
if (! getSelectedLines (me, & numberOfLinesLeft, & numberOfLinesRight)) { |
| 716 |
Melder_information (U"The cursor is on line ", numberOfLinesLeft, U"."); |
| 717 |
} else if (numberOfLinesLeft == numberOfLinesRight) { |
| 718 |
Melder_information (U"The selection is on line ", numberOfLinesLeft, U"."); |
| 719 |
} else { |
| 720 |
Melder_information (U"The selection runs from line ", numberOfLinesLeft, U" to line ", numberOfLinesRight, U"."); |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
static void menu_cb_goToLine (TextEditor me, EDITOR_ARGS) { |
| 725 |
EDITOR_FORM (U"Go to line", nullptr) |
| 726 |
NATURAL (lineToGo, U"Line", U"1") |
| 727 |
EDITOR_OK |
| 728 |
integer firstLine, lastLine; |
| 729 |
getSelectedLines (me, & firstLine, & lastLine); |
| 730 |
SET_INTEGER (lineToGo, firstLine) |
| 731 |
EDITOR_DO |
| 732 |
autostring32 text = GuiText_getString (my textWidget); |
| 733 |
integer currentLine = 1; |
| 734 |
integer left = 0, right = 0; |
| 735 |
if (lineToGo == 1) { |
| 736 |
for (; text [right] != U'\n' && text [right] != U'\0'; right ++) { } |
| 737 |
} else { |
| 738 |
for (; text [left] != U'\0'; left ++) { |
| 739 |
if (text [left] == U'\n') { |
| 740 |
currentLine ++; |
| 741 |
if (currentLine == lineToGo) { |
| 742 |
left ++; |
| 743 |
for (right = left; text [right] != U'\n' && text [right] != U'\0'; right ++) { } |
| 744 |
break; |
| 745 |
} |
| 746 |
} |
| 747 |
} |
| 748 |
} |
| 749 |
if (left == Melder_length (text.get())) |
| 750 |
right = left; |
| 751 |
else if (text [right] == U'\n') |
| 752 |
right ++; |
| 753 |
GuiText_setSelection (my textWidget, left, right); |
| 754 |
GuiText_scrollToSelection (my textWidget); |
| 755 |
EDITOR_END |
| 756 |
} |
| 757 |
|
| 758 |
#pragma mark - Convert menu |
| 759 |
|
| 760 |
static void menu_cb_convertToCString (TextEditor me, EDITOR_ARGS) { |
| 761 |
autostring32 text = GuiText_getString (my textWidget); |
| 762 |
char32 buffer [2] = U" "; |
| 763 |
const conststring32 hex [16] = { U"0", U"1", U"2", U"3", U"4", U"5", U"6", U"7", U"8", U"9", U"A", U"B", U"C", U"D", U"E", U"F" }; |
| 764 |
MelderInfo_open (); |
| 765 |
MelderInfo_write (U"\""); |
| 766 |
for (char32 *p = & text [0]; *p != U'\0'; p ++) { |
| 767 |
char32 kar = *p; |
| 768 |
if (kar == U'\n') { |
| 769 |
MelderInfo_write (U"\\n\"\n\""); |
| 770 |
} else if (kar == U'\t') { |
| 771 |
MelderInfo_write (U" "); |
| 772 |
} else if (kar == U'\"') { |
| 773 |
MelderInfo_write (U"\\\""); |
| 774 |
} else if (kar == U'\\') { |
| 775 |
MelderInfo_write (U"\\\\"); |
| 776 |
} else if (kar > 127) { |
| 777 |
if (kar <= 0x00FFFF) |
| 778 |
MelderInfo_write (U"\\u", hex [kar >> 12], hex [(kar >> 8) & 0x00'000F], hex [(kar >> 4) & 0x00'000F], hex [kar & 0x00'000F]); |
| 779 |
else |
| 780 |
MelderInfo_write (U"\\U", |
| 781 |
hex [kar >> 28], hex [(kar >> 24) & 0x00'000F], |
| 782 |
hex [(kar >> 20) & 0x00'000F], hex [(kar >> 16) & 0x00'000F], |
| 783 |
hex [(kar >> 12) & 0x00'000F], hex [(kar >> 8) & 0x00'000F], |
| 784 |
hex [(kar >> 4) & 0x00'000F], hex [kar & 0x00'000F] |
| 785 |
); |
| 786 |
} else { |
| 787 |
buffer [0] = *p; |
| 788 |
MelderInfo_write (& buffer [0]); |
| 789 |
} |
| 790 |
} |
| 791 |
MelderInfo_write (U"\""); |
| 792 |
MelderInfo_close (); |
| 793 |
} |
| 794 |
|
| 795 |
#pragma mark - Font menu |
| 796 |
|
| 797 |
static void updateSizeMenu (TextEditor me) { |
| 798 |
if (my fontSizeButton_10) |
| 799 |
GuiMenuItem_check (my fontSizeButton_10, my instancePref_fontSize() == 10.0); |
| 800 |
if (my fontSizeButton_12) |
| 801 |
GuiMenuItem_check (my fontSizeButton_12, my instancePref_fontSize() == 12.0); |
| 802 |
if (my fontSizeButton_14) |
| 803 |
GuiMenuItem_check (my fontSizeButton_14, my instancePref_fontSize() == 14.0); |
| 804 |
if (my fontSizeButton_18) |
| 805 |
GuiMenuItem_check (my fontSizeButton_18, my instancePref_fontSize() == 18.0); |
| 806 |
if (my fontSizeButton_24) |
| 807 |
GuiMenuItem_check (my fontSizeButton_24, my instancePref_fontSize() == 24.0); |
| 808 |
} |
| 809 |
static void setFontSize (TextEditor me, double fontSize) { |
| 810 |
GuiText_setFontSize (my textWidget, fontSize); |
| 811 |
my setInstancePref_fontSize (fontSize); |
| 812 |
updateSizeMenu (me); |
| 813 |
} |
| 814 |
|
| 815 |
static void menu_cb_10 (TextEditor me, EDITOR_ARGS) { setFontSize (me, 10.0); } |
| 816 |
static void menu_cb_12 (TextEditor me, EDITOR_ARGS) { setFontSize (me, 12.0); } |
| 817 |
static void menu_cb_14 (TextEditor me, EDITOR_ARGS) { setFontSize (me, 14.0); } |
| 818 |
static void menu_cb_18 (TextEditor me, EDITOR_ARGS) { setFontSize (me, 18.0); } |
| 819 |
static void menu_cb_24 (TextEditor me, EDITOR_ARGS) { setFontSize (me, 24.0); } |
| 820 |
static void menu_cb_fontSize (TextEditor me, EDITOR_ARGS) { |
| 821 |
EDITOR_FORM (U"Text window: Font size", nullptr) |
| 822 |
POSITIVE (fontSize, U"Font size (points)", U"12") |
| 823 |
EDITOR_OK |
| 824 |
SET_REAL (fontSize, my instancePref_fontSize()); |
| 825 |
EDITOR_DO |
| 826 |
setFontSize (me, fontSize); |
| 827 |
EDITOR_END |
| 828 |
} |
| 829 |
|
| 830 |
static void gui_text_cb_changed (TextEditor me, GuiTextEvent /* event */) { |
| 831 |
if (! my dirty) { |
| 832 |
my dirty = true; |
| 833 |
my v_nameChanged (); |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
void structTextEditor :: v_createChildren () { |
| 838 |
textWidget = GuiText_createShown (our windowForm, 0, 0, Machine_getMenuBarBottom (), 0, GuiText_SCROLLED); |
| 839 |
GuiText_setChangedCallback (textWidget, gui_text_cb_changed, this); |
| 840 |
} |
| 841 |
|
| 842 |
void structTextEditor :: v_createMenus () { |
| 843 |
TextEditor_Parent :: v_createMenus (); |
| 844 |
|
| 845 |
if (v_fileBased ()) { |
| 846 |
Editor_addCommand (this, U"File", U"New", 'N', menu_cb_new); |
| 847 |
Editor_addCommand (this, U"File", U"Open...", 'O', menu_cb_open); |
| 848 |
Editor_addCommand (this, U"File", U"Reopen from disk", GuiMenu_COMMAND_EXTRA | 'O', menu_cb_reopen); |
| 849 |
} else { |
| 850 |
Editor_addCommand (this, U"File", U"Clear", 'N', menu_cb_clear); |
| 851 |
} |
| 852 |
Editor_addCommand (this, U"File", U"-- save --", 0, nullptr); |
| 853 |
if (v_fileBased ()) { |
| 854 |
Editor_addCommand (this, U"File", U"Save", 'S', menu_cb_save); |
| 855 |
Editor_addCommand (this, U"File", U"Save as...", 0, menu_cb_saveAs); |
| 856 |
} else { |
| 857 |
Editor_addCommand (this, U"File", U"Save as...", 'S', menu_cb_saveAs); |
| 858 |
} |
| 859 |
Editor_addCommand (this, U"File", U"-- close --", 0, nullptr); |
| 860 |
GuiText_setUndoItem (textWidget, Editor_addCommand (this, U"Edit", U"Undo", 'Z', menu_cb_undo)); |
| 861 |
GuiText_setRedoItem (textWidget, Editor_addCommand (this, U"Edit", U"Redo", 'Y', menu_cb_redo)); |
| 862 |
Editor_addCommand (this, U"Edit", U"-- cut copy paste --", 0, nullptr); |
| 863 |
Editor_addCommand (this, U"Edit", U"Cut", 'X', menu_cb_cut); |
| 864 |
Editor_addCommand (this, U"Edit", U"Copy", 'C', menu_cb_copy); |
| 865 |
Editor_addCommand (this, U"Edit", U"Paste", 'V', menu_cb_paste); |
| 866 |
Editor_addCommand (this, U"Edit", U"Erase", 0, menu_cb_erase); |
| 867 |
Editor_addCommand (this, U"Edit", U"-- layout --", 0, nullptr); |
| 868 |
Editor_addCommand (this, U"Edit", U"Shift right", ']', menu_cb_shiftRight); |
| 869 |
Editor_addCommand (this, U"Edit", U"Shift left", '[', menu_cb_shiftLeft); |
| 870 |
|
| 871 |
Editor_addMenu (this, U"Search", 0); |
| 872 |
Editor_addCommand (this, U"Search", U"Find...", 'F', menu_cb_find); |
| 873 |
Editor_addCommand (this, U"Search", U"Find again", 'G', menu_cb_findAgain); |
| 874 |
Editor_addCommand (this, U"Search", U"Replace...", GuiMenu_COMMAND_EXTRA | 'F', menu_cb_replace); |
| 875 |
Editor_addCommand (this, U"Search", U"Replace again", GuiMenu_COMMAND_EXTRA | 'G', menu_cb_replaceAgain); |
| 876 |
Editor_addCommand (this, U"Search", U"Use selection for find", 'E', menu_cb_useSelectionForFind); |
| 877 |
Editor_addCommand (this, U"Search", U"-- line --", 0, nullptr); |
| 878 |
Editor_addCommand (this, U"Search", U"Where am I?", 0, menu_cb_whereAmI); |
| 879 |
Editor_addCommand (this, U"Search", U"Go to line...", 'L', menu_cb_goToLine); |
| 880 |
|
| 881 |
Editor_addMenu (this, U"Convert", 0); |
| 882 |
Editor_addCommand (this, U"Convert", U"Convert to C string", 0, menu_cb_convertToCString); |
| 883 |
|
| 884 |
Editor_addMenu (this, U"Font", 0); |
| 885 |
Editor_addCommand (this, U"Font", U"Font size...", 0, menu_cb_fontSize); |
| 886 |
fontSizeButton_10 = Editor_addCommand (this, U"Font", U"10", GuiMenu_CHECKBUTTON, menu_cb_10); |
| 887 |
fontSizeButton_12 = Editor_addCommand (this, U"Font", U"12", GuiMenu_CHECKBUTTON, menu_cb_12); |
| 888 |
fontSizeButton_14 = Editor_addCommand (this, U"Font", U"14", GuiMenu_CHECKBUTTON, menu_cb_14); |
| 889 |
fontSizeButton_18 = Editor_addCommand (this, U"Font", U"18", GuiMenu_CHECKBUTTON, menu_cb_18); |
| 890 |
fontSizeButton_24 = Editor_addCommand (this, U"Font", U"24", GuiMenu_CHECKBUTTON, menu_cb_24); |
| 891 |
} |
| 892 |
|
| 893 |
void TextEditor_init (TextEditor me, conststring32 initialText) { |
| 894 |
/* |
| 895 |
The width of a text window should be at least one tab plus 84 single-width characters, |
| 896 |
if the font size is 12. |
| 897 |
*/ |
| 898 |
constexpr double characterWidth = |
| 899 |
#if defined (macintosh) |
| 900 |
0.625 * 12; // TODO: make a bit bigger |
| 901 |
#elif defined (_WIN32) |
| 902 |
0.689 * 12; |
| 903 |
#else |
| 904 |
0.584 * 12; // TODO: make much bigger (e.g. as size 14 is now, as in what Terminal calls size 12) |
| 905 |
#endif |
| 906 |
constexpr double tabWidth = |
| 907 |
#if defined (_WIN32) |
| 908 |
8.0 * characterWidth; |
| 909 |
#elif defined (macintosh) |
| 910 |
4.0 * characterWidth; |
| 911 |
#else |
| 912 |
4.0 * characterWidth; |
| 913 |
#endif |
| 914 |
constexpr double numberOfFittingCharacters = 84.0; |
| 915 |
constexpr double overlapWithNextCharacter = |
| 916 |
#ifdef macintosh |
| 917 |
0.5 * characterWidth - 1; |
| 918 |
#else |
| 919 |
0.5 * characterWidth; |
| 920 |
#endif |
| 921 |
constexpr int width = int (tabWidth + numberOfFittingCharacters * characterWidth + overlapWithNextCharacter); |
| 922 |
Editor_init (me, 0, 0, width, 400, U"", nullptr); |
| 923 |
setFontSize (me, my instancePref_fontSize()); |
| 924 |
if (initialText) { |
| 925 |
GuiText_setString (my textWidget, initialText); |
| 926 |
my dirty = false; // was set to true in valueChanged callback |
| 927 |
if (my v_fileBased ()) |
| 928 |
Thing_setName (me, nullptr); |
| 929 |
} |
| 930 |
theReferencesToAllOpenTextEditors. addItem_ref (me); |
| 931 |
} |
| 932 |
|
| 933 |
autoTextEditor TextEditor_create (conststring32 initialText) { |
| 934 |
try { |
| 935 |
autoTextEditor me = Thing_new (TextEditor); |
| 936 |
TextEditor_init (me.get(), initialText); |
| 937 |
return me; |
| 938 |
} catch (MelderError) { |
| 939 |
Melder_throw (U"Text window not created."); |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
#pragma mark - Export |
| 944 |
|
| 945 |
void TextEditor_showOpen (TextEditor me) { |
| 946 |
cb_showOpen (Editor_getMenuCommand (me, U"File", U"Open...")); |
| 947 |
} |
| 948 |
|
| 949 |
/* End of file TextEditor.cpp */ |