| 1 |
/* praat_menuCommands.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2018,2020-2025 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 "praatP.h" |
| 20 |
#include "praat_script.h" |
| 21 |
#include "GuiP.h" |
| 22 |
|
| 23 |
static OrderedOf <structPraat_Command> theCommands; |
| 24 |
void praat_menuCommands_exit_optimizeByLeaking () { theCommands. _ownItems = false; } |
| 25 |
|
| 26 |
void praat_sortMenuCommands () { |
| 27 |
for (integer i = 1; i <= theCommands.size; i ++) { |
| 28 |
Praat_Command command = theCommands.at [i]; |
| 29 |
command -> sortingTail = i; |
| 30 |
} |
| 31 |
std::sort (theCommands.begin(), theCommands.end(), |
| 32 |
[] (Praat_Command me, Praat_Command thee) { |
| 33 |
if (my window) { |
| 34 |
if (! thy window) |
| 35 |
return false; |
| 36 |
int compare = str32cmp (my window.get(), thy window.get()); |
| 37 |
if (compare != 0) |
| 38 |
return compare < 0; |
| 39 |
} else if (thy window) |
| 40 |
return true; |
| 41 |
if (my menu) { |
| 42 |
if (! thy menu) |
| 43 |
return false; |
| 44 |
int compare = str32cmp (my menu.get(), thy menu.get()); |
| 45 |
if (compare != 0) |
| 46 |
return compare < 0; |
| 47 |
} else if (thy menu) |
| 48 |
return true; |
| 49 |
return my sortingTail < thy sortingTail; |
| 50 |
} |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
static integer lookUpMatchingMenuCommand_0 (conststring32 window, conststring32 menu, conststring32 title) { |
| 55 |
/* |
| 56 |
A menu command is fully specified by its environment (window + menu) and its title. |
| 57 |
*/ |
| 58 |
for (integer i = 1; i <= theCommands.size; i ++) { |
| 59 |
Praat_Command command = theCommands.at [i]; |
| 60 |
conststring32 tryWindow = command -> window.get(); |
| 61 |
conststring32 tryMenu = command -> menu.get(); |
| 62 |
conststring32 tryTitle = command -> title.get(); |
| 63 |
if ( |
| 64 |
(window == tryWindow || (window && tryWindow && str32equ (window, tryWindow))) && |
| 65 |
(menu == tryMenu || (menu && tryMenu && str32equ (menu, tryMenu))) && |
| 66 |
(title == tryTitle || (title && tryTitle && str32equ (title, tryTitle))) |
| 67 |
) |
| 68 |
return i; |
| 69 |
} |
| 70 |
return 0; // not found |
| 71 |
} |
| 72 |
|
| 73 |
static void do_menu (Praat_Command me, bool isModified) { |
| 74 |
if (my callback == DO_RunTheScriptFromAnyAddedMenuCommand) { |
| 75 |
UiHistory_write (U"\nrunScript: "); |
| 76 |
try { |
| 77 |
DO_RunTheScriptFromAnyAddedMenuCommand (nullptr, 0, nullptr, my script.get(), nullptr, nullptr, false, nullptr, nullptr); |
| 78 |
} catch (MelderError) { |
| 79 |
Melder_flushError (U"Command \"", my title.get(), U"\" not executed."); |
| 80 |
} |
| 81 |
praat_updateSelection (); |
| 82 |
} else { |
| 83 |
if (my title && ! str32str (my title.get(), U"...")) { |
| 84 |
UiHistory_write (U"\n"); |
| 85 |
UiHistory_write (my title.get()); |
| 86 |
} |
| 87 |
try { |
| 88 |
my callback (nullptr, 0, nullptr, nullptr, nullptr, my title.get(), isModified, nullptr, nullptr); |
| 89 |
} catch (MelderError) { |
| 90 |
Melder_flushError (U"Command \"", my title.get(), U"\" not executed."); |
| 91 |
} |
| 92 |
praat_updateSelection (); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
static void gui_button_cb_menu (Praat_Command me, GuiButtonEvent event) { |
| 97 |
const bool isModified = event -> shiftKeyPressed || event -> commandKeyPressed || event -> optionKeyPressed; |
| 98 |
do_menu (me, isModified); |
| 99 |
} |
| 100 |
|
| 101 |
static void gui_cb_menu (Praat_Command me, GuiMenuItemEvent event) { |
| 102 |
const bool isModified = event -> shiftKeyPressed || event -> commandKeyPressed || event -> optionKeyPressed; |
| 103 |
do_menu (me, isModified); |
| 104 |
} |
| 105 |
|
| 106 |
static GuiMenu windowMenuToWidget (conststring32 window, conststring32 menu) { |
| 107 |
return |
| 108 |
str32equ (window, U"Picture") ? praat_picture_resolveMenu (menu) : |
| 109 |
str32equ (window, U"Objects") ? praat_objects_resolveMenu (menu) : nullptr; |
| 110 |
} |
| 111 |
|
| 112 |
static GuiMenuItem praat_addMenuCommand__ (conststring32 window, conststring32 menu, conststring32 title /* cattable */, |
| 113 |
conststring32 after, uint32 flags, UiCallback callback, conststring32 nameOfCallback) |
| 114 |
{ |
| 115 |
uint32 depth = flags, key = 0; |
| 116 |
bool unhidable = false, hidden = false, noApi = false, forceApi = false; |
| 117 |
int deprecationYear = 0; |
| 118 |
uint32 guiFlags = 0; |
| 119 |
if (flags > 7) { |
| 120 |
depth = (flags & GuiMenu_DEPTH_7) >> 16; |
| 121 |
unhidable = (flags & GuiMenu_UNHIDABLE) != 0; |
| 122 |
hidden = (flags & GuiMenu_HIDDEN) != 0 && ! unhidable; |
| 123 |
key = flags & 0x000000FF; |
| 124 |
noApi = (flags & GuiMenu_NO_API) != 0; |
| 125 |
forceApi = (flags & GuiMenu_FORCE_API) != 0; |
| 126 |
deprecationYear = (flags & GuiMenu_DEPRECATED) == GuiMenu_DEPRECATED ? 2000 + (flags >> 24) : 0; |
| 127 |
guiFlags = key ? flags & (0x000000FF | GuiMenu_SHIFT | GuiMenu_OPTION | GuiMenu_BUTTON_STATE_MASK) : flags & GuiMenu_BUTTON_STATE_MASK; |
| 128 |
} |
| 129 |
if (callback && ! title) { |
| 130 |
Melder_flushError (U"praat_addMenuCommand: command with callback has no title. Window \"", window, U"\", menu \"", menu, U"\"."); |
| 131 |
return nullptr; |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
Determine the position of the new command. |
| 136 |
*/ |
| 137 |
integer position; |
| 138 |
if (after) { // search for existing command with same selection |
| 139 |
const integer found = lookUpMatchingMenuCommand_0 (window, menu, after); |
| 140 |
if (found == 0) { |
| 141 |
Melder_flushError (U"praat_addMenuCommand: the command \"", title, U"\" cannot be put after \"", after, U"\",\n" |
| 142 |
U"in the menu \"", menu, U"\" in the window \"", window, U"\"\n" |
| 143 |
U"because the latter command does not exist."); |
| 144 |
return nullptr; |
| 145 |
} |
| 146 |
position = found + 1; // after 'after' |
| 147 |
} else { |
| 148 |
position = theCommands.size + 1; // at end |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
Make new command. |
| 153 |
*/ |
| 154 |
autoPraat_Command command = Thing_new (Praat_Command); |
| 155 |
command -> window = Melder_dup_f (window); |
| 156 |
command -> menu = Melder_dup_f (menu); |
| 157 |
command -> title = Melder_dup_f (title); |
| 158 |
trace (U"insert new command \"", title, U"\""); |
| 159 |
command -> depth = depth; |
| 160 |
command -> callback = callback; // null for a separator or cascade button |
| 161 |
command -> nameOfCallback = nameOfCallback; |
| 162 |
command -> executable = !! callback; |
| 163 |
command -> script = autostring32(); |
| 164 |
command -> hidden = hidden; |
| 165 |
command -> unhidable = unhidable; |
| 166 |
command -> deprecationYear = deprecationYear; |
| 167 |
command -> noApi = noApi; |
| 168 |
command -> forceApi = forceApi; |
| 169 |
|
| 170 |
if (! theCurrentPraatApplication -> batch) { |
| 171 |
GuiMenu parentMenu = nullptr; |
| 172 |
|
| 173 |
/* |
| 174 |
WHERE TO PUT IT? |
| 175 |
Determine parent menu widget. |
| 176 |
This is not going to fail: |
| 177 |
if 'depth' is inappropriate, the alleged subitem will be put in the top menu. |
| 178 |
*/ |
| 179 |
if (depth == 0) { |
| 180 |
/* |
| 181 |
Put the new command in the window's top menu. |
| 182 |
*/ |
| 183 |
parentMenu = windowMenuToWidget (window, menu); |
| 184 |
} else { |
| 185 |
/* |
| 186 |
Put the new command in a submenu. |
| 187 |
The supermenu to put the new command in is the first menu that we find when going up. |
| 188 |
*/ |
| 189 |
for (integer parentPosition = position - 1; parentPosition > 0; parentPosition --) { |
| 190 |
Praat_Command parentCommand = theCommands.at [parentPosition]; |
| 191 |
if (parentCommand -> depth == depth - 1 && str32equ (parentCommand -> menu.get(), command -> menu.get())) { |
| 192 |
/* |
| 193 |
We found the supermenu. |
| 194 |
*/ |
| 195 |
if (! parentCommand -> callback && parentCommand -> title && parentCommand -> title [0] != U'-') { |
| 196 |
if (! parentCommand -> button) |
| 197 |
Melder_crash (U"No button for ", window, U"/", menu, U"/", title, U"."); |
| 198 |
Thing_cast (GuiMenuItem, parentButton_as_GuiMenuItem, parentCommand -> button); |
| 199 |
parentMenu = parentButton_as_GuiMenuItem -> d_menu; |
| 200 |
} |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
if (! parentMenu) |
| 205 |
parentMenu = windowMenuToWidget (window, menu); // fallback: put the command in the window's top menu |
| 206 |
} |
| 207 |
if (! parentMenu) { |
| 208 |
trace (U"WARNING: no parent menu for ", window, U"/", menu, U"/", title, U"."); |
| 209 |
return nullptr; |
| 210 |
} |
| 211 |
|
| 212 |
/* |
| 213 |
WHAT TO PUT THERE? |
| 214 |
*/ |
| 215 |
if (! title || title [0] == U'-') { |
| 216 |
trace (U"insert the command as a separator"); |
| 217 |
command -> button = GuiMenu_addSeparator (parentMenu); |
| 218 |
Melder_assert (command -> button); |
| 219 |
} else if (! callback) { |
| 220 |
trace (U"insert the command as a submenu"); |
| 221 |
command -> button = GuiMenu_createInMenu (parentMenu, title, 0) -> d_menuItem.get(); |
| 222 |
Melder_assert (command -> button); |
| 223 |
} else { |
| 224 |
trace (U"insert the command as a normal menu item"); |
| 225 |
command -> button = GuiMenu_addItem (parentMenu, title, guiFlags, gui_cb_menu, command.get()); |
| 226 |
Melder_assert (command -> button); |
| 227 |
} |
| 228 |
if (hidden) |
| 229 |
GuiThing_hide (command -> button); |
| 230 |
} |
| 231 |
Thing_cast (GuiMenuItem, button_as_GuiMenuItem, command -> button); |
| 232 |
theCommands. addItemAtPosition_move (command.move(), position); |
| 233 |
return button_as_GuiMenuItem; |
| 234 |
} |
| 235 |
GuiMenuItem praat_addMenuCommand_ (conststring32 window, conststring32 menu, conststring32 title /* cattable */, |
| 236 |
conststring32 after, uint32 flags, UiCallback callback, conststring32 nameOfCallback) |
| 237 |
{ |
| 238 |
const char32 *pSeparator = str32str (title, U" || "); |
| 239 |
if (! pSeparator) |
| 240 |
return praat_addMenuCommand__ (window, menu, title, after, flags, callback, nameOfCallback); |
| 241 |
if (flags < 8) |
| 242 |
flags *= GuiMenu_DEPTH_1; // turn 1..7 into GuiMenu_DEPTH_1..GuiMenu_DEPTH_7, because the flags are ORed below |
| 243 |
integer positionOfSeparator = pSeparator - title; |
| 244 |
static MelderString string; |
| 245 |
MelderString_copy (& string, title); |
| 246 |
char32 *pTitle = & string.string [0]; |
| 247 |
GuiMenuItem result = nullptr; |
| 248 |
do { |
| 249 |
pTitle [positionOfSeparator] = U'\0'; |
| 250 |
GuiMenuItem menuItem = praat_addMenuCommand__ (window, menu, pTitle, after, flags, callback, nameOfCallback); |
| 251 |
if (menuItem) |
| 252 |
result = menuItem; // only the first |
| 253 |
pTitle += positionOfSeparator + 4; // step past " || " |
| 254 |
pSeparator = str32str (pTitle, U" || "); |
| 255 |
if (pSeparator) |
| 256 |
positionOfSeparator = pSeparator - pTitle; |
| 257 |
flags |= GuiMenu_HIDDEN; |
| 258 |
} while (pSeparator); |
| 259 |
(void) praat_addMenuCommand__ (window, menu, pTitle, after, flags | GuiMenu_HIDDEN, callback, nameOfCallback); |
| 260 |
return result; |
| 261 |
} |
| 262 |
|
| 263 |
void praat_addMenuCommandScript (conststring32 window, conststring32 menu, conststring32 title, |
| 264 |
conststring32 after, integer depth, conststring32 script) |
| 265 |
{ |
| 266 |
try { |
| 267 |
Melder_assert (window && menu && title && after && script); |
| 268 |
if (script [0] != U'\0' && title [0] == U'\0') |
| 269 |
Melder_throw (U"Command with script has no title. Window \"", window, U"\", menu \"", menu, U"\"."); |
| 270 |
|
| 271 |
/* |
| 272 |
Determine the position of the new command. |
| 273 |
*/ |
| 274 |
integer position; |
| 275 |
if (Melder_length (after)) { // search for existing command with same selection |
| 276 |
const integer found = lookUpMatchingMenuCommand_0 (window, menu, after); |
| 277 |
if (found == 0) { |
| 278 |
/*Melder_throw (U"The menu command \"", title, U"\" cannot be put after \"", after, U"\",\n" |
| 279 |
U"in the menu \"", menu, "\" in the window \"", window, U"\"\n" |
| 280 |
U"because the latter command does not exist.");*/ |
| 281 |
position = theCommands.size + 1; // default: at end |
| 282 |
} else |
| 283 |
position = found + 1; // after 'after' |
| 284 |
} else { |
| 285 |
position = theCommands.size + 1; // at end |
| 286 |
} |
| 287 |
|
| 288 |
/* |
| 289 |
Make new command. |
| 290 |
*/ |
| 291 |
autoPraat_Command command = Thing_new (Praat_Command); |
| 292 |
command -> window = Melder_dup_f (window); |
| 293 |
command -> menu = Melder_dup_f (menu); |
| 294 |
command -> title = ( title [0] != U'\0' ? Melder_dup_f (title) : autostring32() ); // allow old-fashioned untitled separators |
| 295 |
command -> depth = depth; |
| 296 |
command -> callback = ( script [0] != U'\0' ? DO_RunTheScriptFromAnyAddedMenuCommand : nullptr ); // null for a separator or cascade button |
| 297 |
command -> executable = ( script [0] != U'\0' ); |
| 298 |
command -> noApi = true; |
| 299 |
if (script [0] == U'\0') { |
| 300 |
command -> script = Melder_dup_f (U""); // empty string, which will be needed to signal origin |
| 301 |
} else { |
| 302 |
structMelderFile file { }; |
| 303 |
Melder_relativePathToFile (script, & file); |
| 304 |
command -> script = Melder_dup_f (MelderFile_peekPath (& file)); |
| 305 |
} |
| 306 |
command -> after = ( after [0] != U'\0' ? Melder_dup_f (after) : autostring32() ); |
| 307 |
if (praatP.phase >= praat_READING_BUTTONS) { |
| 308 |
static integer uniqueID = 0; |
| 309 |
command -> uniqueID = ++ uniqueID; |
| 310 |
} |
| 311 |
|
| 312 |
if (! theCurrentPraatApplication -> batch) { |
| 313 |
GuiMenu parentMenu = nullptr; |
| 314 |
|
| 315 |
/* |
| 316 |
WHERE TO PUT IT? |
| 317 |
Determine parent menu widget. |
| 318 |
This is not going to fail: |
| 319 |
if 'depth' is inappropriate, the alleged subitem will be put in the top menu. |
| 320 |
*/ |
| 321 |
if (depth == 0) { |
| 322 |
parentMenu = windowMenuToWidget (window, menu); // not a subitem: in the top menu |
| 323 |
} else { |
| 324 |
for (integer parentPosition = position - 1; parentPosition > 0; parentPosition --) { |
| 325 |
Praat_Command parentCommand = theCommands.at [parentPosition]; |
| 326 |
if (parentCommand -> depth == depth - 1) { |
| 327 |
if (! parentCommand -> callback && parentCommand -> title && parentCommand -> title [0] != U'-') { |
| 328 |
if (! parentCommand -> button) |
| 329 |
Melder_crash (U"No button for ", window, U"/", menu, U"/", title, U"."); |
| 330 |
Melder_assert (parentCommand -> button -> classInfo == classGuiMenuItem); |
| 331 |
parentMenu = (static_cast <GuiMenuItem> (parentCommand -> button)) -> d_menu; |
| 332 |
} |
| 333 |
break; |
| 334 |
} |
| 335 |
} |
| 336 |
if (! parentMenu) |
| 337 |
parentMenu = windowMenuToWidget (window, menu); // fallback: a subitem without a menu title |
| 338 |
} |
| 339 |
if (parentMenu) { |
| 340 |
/* |
| 341 |
WHAT TO PUT THERE? |
| 342 |
*/ |
| 343 |
if (title [0] == U'\0' || title [0] == U'-') { |
| 344 |
command -> button = GuiMenu_addSeparator (parentMenu); |
| 345 |
} else if (script [0] == U'\0') { |
| 346 |
command -> button = GuiMenu_createInMenu (parentMenu, Melder_cat (U"\u207A", title), 0) -> d_menuItem.get(); |
| 347 |
} else { |
| 348 |
command -> button = GuiMenu_addItem (parentMenu, Melder_cat (U"\u207A", title), 0, gui_cb_menu, command.get()); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
theCommands. addItemAtPosition_move (command.move(), position); |
| 353 |
|
| 354 |
if (praatP.phase >= praat_HANDLING_EVENTS) |
| 355 |
praat_sortMenuCommands (); |
| 356 |
} catch (MelderError) { |
| 357 |
Melder_throw (U"Script menu command not added."); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
void praat_hideMenuCommand (conststring32 window, conststring32 menu, conststring32 title) { |
| 362 |
if (theCurrentPraatApplication -> batch || ! window || ! menu || ! title) |
| 363 |
return; |
| 364 |
const integer found = lookUpMatchingMenuCommand_0 (window, menu, title); |
| 365 |
if (found == 0) |
| 366 |
return; |
| 367 |
Praat_Command command = theCommands.at [found]; |
| 368 |
if (! command -> hidden && ! command -> unhidable) { |
| 369 |
command -> hidden = true; |
| 370 |
if (praatP.phase >= praat_READING_BUTTONS) |
| 371 |
command -> toggled = ! command -> toggled; |
| 372 |
if (command -> button) |
| 373 |
GuiThing_hide (command -> button); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
void praat_showMenuCommand (conststring32 window, conststring32 menu, conststring32 title) { |
| 378 |
if (theCurrentPraatApplication -> batch || ! window || ! menu || ! title) |
| 379 |
return; |
| 380 |
const integer found = lookUpMatchingMenuCommand_0 (window, menu, title); |
| 381 |
if (found == 0) |
| 382 |
return; |
| 383 |
Praat_Command command = theCommands.at [found]; |
| 384 |
if (command -> hidden) { |
| 385 |
command -> hidden = false; |
| 386 |
if (praatP.phase >= praat_READING_BUTTONS) |
| 387 |
command -> toggled = ! command -> toggled; |
| 388 |
if (command -> button) |
| 389 |
GuiThing_show (command -> button); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
void praat_saveAddedMenuCommands (MelderString *buffer) { |
| 394 |
/* |
| 395 |
The procedure as it is now, runs in M*N time, |
| 396 |
where M is the number of added commands and N is the total number of commands. |
| 397 |
Sorting first instead runs in N log N time and will therefore be faster if M >> log N ≈ 10. |
| 398 |
*/ |
| 399 |
integer maxID = 0; |
| 400 |
for (integer i = 1; i <= theCommands.size; i ++) { |
| 401 |
Praat_Command command = theCommands.at [i]; |
| 402 |
if (command -> uniqueID > maxID) |
| 403 |
maxID = command -> uniqueID; |
| 404 |
} |
| 405 |
for (integer id = 1; id <= maxID; id ++) // sorted |
| 406 |
for (integer i = 1; i <= theCommands.size; i ++) { |
| 407 |
Praat_Command me = theCommands.at [i]; |
| 408 |
if (my uniqueID == id && ! my hidden && my window && my menu && my title) { |
| 409 |
MelderString_append (buffer, U"Add menu command... \"", my window.get(), U"\" \"", my menu.get(), U"\" \"", my title.get(), U"\" \"", |
| 410 |
( my after ? my after.get() : U"" ), U"\" ", my depth, U" ", ( my script ? my script.get() : U"" ), U"\n"); |
| 411 |
break; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
void praat_saveToggledMenuCommands (MelderString *buffer) { |
| 417 |
for (integer i = 1; i <= theCommands.size; i ++) { |
| 418 |
Praat_Command me = theCommands.at [i]; |
| 419 |
if (my toggled && my window && my menu && my title && ! my uniqueID && ! my script) |
| 420 |
MelderString_append (buffer, my hidden ? U"Hide" : U"Show", U" menu command... \"", |
| 421 |
my window.get(), U"\" \"", my menu.get(), U"\" ", my title.get(), U"\n"); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
/***** FIXED BUTTONS *****/ |
| 426 |
|
| 427 |
void praat_addFixedButtonCommand_ (GuiForm parent, conststring32 title, UiCallback callback, conststring32 nameOfCallback, int x, int y) { |
| 428 |
autoPraat_Command me = Thing_new (Praat_Command); |
| 429 |
my window = Melder_dup_f (U"Objects"); |
| 430 |
my title = Melder_dup_f (title); |
| 431 |
my callback = callback; |
| 432 |
my nameOfCallback = nameOfCallback; |
| 433 |
my unhidable = true; |
| 434 |
my noApi = ( str32equ (title, U"Inspect") ); |
| 435 |
if (theCurrentPraatApplication -> batch) { |
| 436 |
my button = nullptr; |
| 437 |
} else { |
| 438 |
GuiThing button = my button = GuiButton_create (parent, x, x + 82, -y - Gui_PUSHBUTTON_HEIGHT, -y, |
| 439 |
title, gui_button_cb_menu, me.get(), 0); |
| 440 |
GuiThing_setSensitive (button, false); |
| 441 |
GuiThing_show (button); |
| 442 |
} |
| 443 |
my executable = false; |
| 444 |
theCommands. addItemAtPosition_move (me.move(), 0); |
| 445 |
} |
| 446 |
|
| 447 |
void praat_sensitivizeFixedButtonCommand (conststring32 title, bool sensitive) { |
| 448 |
Praat_Command commandFound = nullptr; |
| 449 |
for (integer i = 1; i <= theCommands.size; i ++) { |
| 450 |
Praat_Command command = theCommands.at [i]; |
| 451 |
if (str32equ (command -> title.get(), title)) { |
| 452 |
commandFound = command; |
| 453 |
break; |
| 454 |
} |
| 455 |
} |
| 456 |
if (! commandFound) |
| 457 |
Melder_crash (U"Unkown fixed button <<", title, U">>"); |
| 458 |
commandFound -> executable = sensitive; |
| 459 |
if (! theCurrentPraatApplication -> batch && ! Melder_backgrounding) |
| 460 |
GuiThing_setSensitive (commandFound -> button, sensitive); |
| 461 |
} |
| 462 |
|
| 463 |
int praat_doMenuCommand (conststring32 title, conststring32 arguments, Interpreter interpreter) { |
| 464 |
Praat_Command commandFound = nullptr; |
| 465 |
for (integer i = 1; i <= theCommands.size; i ++) { |
| 466 |
Praat_Command command = theCommands.at [i]; |
| 467 |
if (command -> executable && str32equ (command -> title.get(), title) && |
| 468 |
(str32equ (command -> window.get(), U"Objects") || str32equ (command -> window.get(), U"Picture"))) |
| 469 |
{ |
| 470 |
commandFound = command; |
| 471 |
break; |
| 472 |
} |
| 473 |
} |
| 474 |
if (! commandFound) |
| 475 |
return 0; |
| 476 |
if (commandFound -> callback == DO_RunTheScriptFromAnyAddedMenuCommand) { |
| 477 |
const conststring32 scriptPath = commandFound -> script.get(); |
| 478 |
const conststring32 preferencesFolderPath = MelderFolder_peekPath (Melder_preferencesFolder()); |
| 479 |
const bool scriptIsInPlugin = Melder_startsWith (scriptPath, preferencesFolderPath); |
| 480 |
Melder_throw ( |
| 481 |
U"From a script you cannot directly call a menu command that calls another script. Use instead: \nrunScript: ", |
| 482 |
scriptIsInPlugin ? U"preferencesDirectory$ + " : U"", |
| 483 |
U"\"", |
| 484 |
scriptIsInPlugin ? scriptPath + Melder_length (preferencesFolderPath) : scriptPath, |
| 485 |
U"\"", |
| 486 |
arguments && arguments [0] ? U", " : U"", |
| 487 |
arguments && arguments [0] ? arguments : U"", |
| 488 |
U"\n" |
| 489 |
); |
| 490 |
} |
| 491 |
commandFound -> callback (nullptr, 0, nullptr, arguments, interpreter, title, false, nullptr, nullptr); |
| 492 |
return 1; |
| 493 |
} |
| 494 |
|
| 495 |
int praat_doMenuCommand (conststring32 title, integer narg, Stackel args, Interpreter interpreter) { |
| 496 |
Praat_Command commandFound = nullptr; |
| 497 |
for (integer i = 1; i <= theCommands.size; i ++) { |
| 498 |
Praat_Command command = theCommands.at [i]; |
| 499 |
if (command -> executable && str32equ (command -> title.get(), title) && |
| 500 |
(str32equ (command -> window.get(), U"Objects") || str32equ (command -> window.get(), U"Picture"))) |
| 501 |
{ |
| 502 |
commandFound = command; |
| 503 |
break; |
| 504 |
} |
| 505 |
} |
| 506 |
if (! commandFound) |
| 507 |
return 0; |
| 508 |
if (commandFound -> callback == DO_RunTheScriptFromAnyAddedMenuCommand) { |
| 509 |
const conststring32 scriptPath = commandFound -> script.get(); |
| 510 |
const conststring32 preferencesFolderPath = MelderFolder_peekPath (Melder_preferencesFolder()); |
| 511 |
const bool scriptIsInPlugin = Melder_startsWith (scriptPath, preferencesFolderPath); |
| 512 |
Melder_throw ( |
| 513 |
U"From a script you cannot directly call a menu command that calls another script. Use instead: \nrunScript: ", |
| 514 |
scriptIsInPlugin ? U"preferencesDirectory$ + " : U"", |
| 515 |
U"\"", |
| 516 |
scriptIsInPlugin ? scriptPath + Melder_length (preferencesFolderPath) : scriptPath, |
| 517 |
U"\"", |
| 518 |
narg > 0 ? U", ..." : U"", |
| 519 |
U"\n" |
| 520 |
); |
| 521 |
} |
| 522 |
commandFound -> callback (nullptr, narg, args, nullptr, interpreter, title, false, nullptr, nullptr); |
| 523 |
return 1; |
| 524 |
} |
| 525 |
|
| 526 |
integer praat_getNumberOfMenuCommands () { return theCommands.size; } |
| 527 |
|
| 528 |
Praat_Command praat_getMenuCommand (integer i) |
| 529 |
{ return i < 1 || i > theCommands.size ? nullptr : theCommands.at [i]; } |
| 530 |
|
| 531 |
void praat_addCommandsToEditor (Editor me) { |
| 532 |
conststring32 windowClassName = my classInfo -> className; |
| 533 |
for (integer i = 1; i <= theCommands.size; i ++) { |
| 534 |
Praat_Command command = theCommands.at [i]; |
| 535 |
if (str32equ (command -> window.get(), windowClassName)) |
| 536 |
Editor_addCommandScript (me, command -> menu.get(), command -> title.get(), |
| 537 |
((uint32) command -> depth) << 16, command -> script.get()); |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
static bool commandIsToBeIncluded (Praat_Command command, bool deprecated, bool includeCreateAPI, bool includeReadAPI, |
| 542 |
bool includeRecordAPI, bool includePlayAPI, bool includeDrawAPI, bool includeHelpAPI, bool includeWindowAPI) |
| 543 |
{ |
| 544 |
const bool obsolete = ( deprecated && (command -> deprecationYear < Melder_appYear() - 10 || command -> deprecationYear < 2023) ); |
| 545 |
const bool hiddenByDefault = ( command -> hidden != command -> toggled ); |
| 546 |
const bool explicitlyHidden = hiddenByDefault && ! deprecated; |
| 547 |
const bool hidden = explicitlyHidden || ! command -> callback || command -> noApi || obsolete || |
| 548 |
(! includeWindowAPI && Melder_nequ (command -> nameOfCallback, U"WINDOW_", 7)) || |
| 549 |
(! includeHelpAPI && Melder_nequ (command -> nameOfCallback, U"HELP_", 5)) || |
| 550 |
(! includeDrawAPI && Melder_nequ (command -> nameOfCallback, U"GRAPHICS_", 9)) || |
| 551 |
(! includePlayAPI && Melder_nequ (command -> nameOfCallback, U"PLAY_", 5)) || |
| 552 |
(! includeRecordAPI && Melder_nequ (command -> nameOfCallback, U"RECORD_", 7)) || |
| 553 |
(! includeReadAPI && Melder_nequ (command -> nameOfCallback, U"READ_MULTIPLE__", 5)) || |
| 554 |
(! includeReadAPI && Melder_nequ (command -> nameOfCallback, U"READ_ONE__", 10)) || |
| 555 |
(! includeCreateAPI && Melder_nequ (command -> nameOfCallback, U"NEW1_", 5)); |
| 556 |
return (command -> forceApi || ! hidden) && command -> callback != DO_RunTheScriptFromAnyAddedMenuCommand; |
| 557 |
} |
| 558 |
|
| 559 |
static bool commandHasFileNameArgument (Praat_Command command) { |
| 560 |
const bool hasFileNameArgument = |
| 561 |
Melder_nequ (command -> nameOfCallback, U"READ_ONE__", 10) || |
| 562 |
Melder_nequ (command -> nameOfCallback, U"SAVE_", 5) |
| 563 |
; |
| 564 |
return hasFileNameArgument; |
| 565 |
} |
| 566 |
|
| 567 |
static conststring32 getReturnType (Praat_Command command) { |
| 568 |
const conststring32 returnType = |
| 569 |
Melder_nequ (command -> nameOfCallback, U"NEW1_", 5) ? U"PraatObject" : |
| 570 |
Melder_nequ (command -> nameOfCallback, U"READ_ONE__", 10) ? U"PraatObject" : |
| 571 |
Melder_nequ (command -> nameOfCallback, U"REAL_", 5) ? U"double" : |
| 572 |
Melder_nequ (command -> nameOfCallback, U"INTEGER_", 8) ? U"int64_t" : |
| 573 |
Melder_nequ (command -> nameOfCallback, U"STRING_", 7) ? U"char *" : |
| 574 |
Melder_nequ (command -> nameOfCallback, U"REPORT_", 7) ? U"char *" : |
| 575 |
Melder_nequ (command -> nameOfCallback, U"LIST_", 5) ? U"char *" : |
| 576 |
Melder_nequ (command -> nameOfCallback, U"INFO_", 5) ? U"char *" : |
| 577 |
Melder_nequ (command -> nameOfCallback, U"HINT_", 5) ? U"char *" : |
| 578 |
U"void"; |
| 579 |
return returnType; |
| 580 |
} |
| 581 |
|
| 582 |
void praat_menuCommands_writeC (bool isInHeaderFile, bool includeCreateAPI, bool includeReadAPI, |
| 583 |
bool includeRecordAPI, bool includePlayAPI, bool includeDrawAPI, bool includeHelpAPI, bool includeWindowAPI) |
| 584 |
{ |
| 585 |
try { |
| 586 |
integer numberOfApiMenuCommands = 0; |
| 587 |
for (integer i = 1; i <= theCommands.size; i ++) { |
| 588 |
Praat_Command command = theCommands.at [i]; |
| 589 |
const bool deprecated = ( command -> deprecationYear > 0 ); |
| 590 |
if (! commandIsToBeIncluded (command, deprecated, includeCreateAPI, includeReadAPI, |
| 591 |
includeRecordAPI, includePlayAPI, includeDrawAPI, includeHelpAPI, includeWindowAPI)) continue; |
| 592 |
MelderInfo_writeLine (U"\n/* Menu command \"", command -> title.get(), U"\"", |
| 593 |
deprecated ? U", deprecated " : U"", deprecated ? Melder_integer (command -> deprecationYear) : U"", |
| 594 |
U" */"); |
| 595 |
conststring32 returnType = getReturnType (command); |
| 596 |
MelderInfo_writeLine (returnType, U" Praat", str32chr (command -> nameOfCallback, U'_'), U" ("); |
| 597 |
const bool isDirect = ! str32str (command -> title.get(), U"..."); |
| 598 |
if (isDirect) { |
| 599 |
} else { |
| 600 |
command -> callback (nullptr, -1, nullptr, nullptr, nullptr, nullptr, false, nullptr, nullptr); |
| 601 |
} |
| 602 |
if (commandHasFileNameArgument (command)) |
| 603 |
MelderInfo_writeLine (U"\tconst char *fileName"); |
| 604 |
MelderInfo_write (U")"); |
| 605 |
if (isInHeaderFile) { |
| 606 |
MelderInfo_writeLine (U";"); |
| 607 |
} else { |
| 608 |
MelderInfo_writeLine (U" {"); |
| 609 |
MelderInfo_writeLine (U"}"); |
| 610 |
} |
| 611 |
numberOfApiMenuCommands += 1; |
| 612 |
} |
| 613 |
} catch (MelderError) { |
| 614 |
Melder_throw (U"Menu commands not written to C library."); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
/* End of file praat_menuCommands.cpp */ |