| 1 | /* praat_actions.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2018,2020-2024 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 2 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 "../kar/longchar.h" |
| 22 | #include "machine.h" |
| 23 | #include "GuiP.h" |
| 24 | |
| 25 | #define BUTTON_LEFT -240 |
| 26 | #define BUTTON_RIGHT -5 |
| 27 | |
| 28 | static OrderedOf <structPraat_Command> theActions; |
| 29 | void praat_actions_exit_optimizeByLeaking () { theActions. _ownItems = false; } |
| 30 | static GuiMenu praat_writeMenu; |
| 31 | static GuiMenuItem praat_writeMenuSeparator; |
| 32 | static GuiForm praat_form; |
| 33 | static bool actionsInvisible = false; |
| 34 | |
| 35 | static void fixSelectionSpecification (ClassInfo *class1, integer *n1, ClassInfo *class2, integer *n2, ClassInfo *class3, integer *n3) { |
| 36 | /* |
| 37 | Function: |
| 38 | sort the specification pairs *class(i), *n(i) according to class name, with null classes at the end. |
| 39 | Postconditions: |
| 40 | if (*class2) !! *class1; |
| 41 | if (*class3) !! *class2; |
| 42 | (*class1) -> className <= (*class2) -> className <= (*class3) -> className; |
| 43 | Usage: |
| 44 | Called by praat_addAction () and praat_removeAction (). |
| 45 | */ |
| 46 | |
| 47 | /* |
| 48 | Fix unusual input bubblewise. |
| 49 | */ |
| 50 | if (! *class1 && *class2) { |
| 51 | *class1 = *class2; |
| 52 | *n1 = *n2; |
| 53 | *class2 = nullptr; |
| 54 | *n2 = 0; |
| 55 | } |
| 56 | if (! *class2 && *class3) { |
| 57 | *class2 = *class3; |
| 58 | *n2 = *n3; |
| 59 | *class3 = nullptr; |
| 60 | *n3 = 0; |
| 61 | if (! *class1 && *class2) { |
| 62 | *class1 = *class2; |
| 63 | *n1 = *n2; |
| 64 | *class2 = nullptr; |
| 65 | *n2 = 0; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | Now: if *class3, then *class2, and if *class2, then *class1. |
| 71 | Bubble-sort the input by class name. |
| 72 | */ |
| 73 | if (*class2 && str32cmp ((*class1) -> className, (*class2) -> className) > 0) { |
| 74 | ClassInfo helpClass1 = *class1; *class1 = *class2; *class2 = helpClass1; |
| 75 | integer helpN1 = *n1; *n1 = *n2; *n2 = helpN1; |
| 76 | } |
| 77 | if (*class3 && str32cmp ((*class2) -> className, (*class3) -> className) > 0) { |
| 78 | ClassInfo helpClass2 = *class2; *class2 = *class3; *class3 = helpClass2; |
| 79 | integer helpN2 = *n2; *n2 = *n3; *n3 = helpN2; |
| 80 | if (str32cmp ((*class1) -> className, (*class2) -> className) > 0) { |
| 81 | ClassInfo helpClass1 = *class1; *class1 = *class2; *class2 = helpClass1; |
| 82 | integer helpN1 = *n1; *n1 = *n2; *n2 = helpN1; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static integer lookUpMatchingAction (ClassInfo class1, ClassInfo class2, ClassInfo class3, ClassInfo class4, conststring32 title) { |
| 88 | /* |
| 89 | An action command is fully specified by its environment (the selected classes) and its title. |
| 90 | Precondition: |
| 91 | class1, class2, and class3 must be in sorted order. |
| 92 | */ |
| 93 | for (integer i = 1; i <= theActions.size; i ++) { |
| 94 | Praat_Command action = theActions.at [i]; |
| 95 | if (class1 == action -> class1 && class2 == action -> class2 && |
| 96 | class3 == action -> class3 && class4 == action -> class4 && |
| 97 | title && action -> title && str32equ (action -> title.get(), title) |
| 98 | ) |
| 99 | return i; |
| 100 | } |
| 101 | return 0; // not found |
| 102 | } |
| 103 | |
| 104 | void praat_addAction1_ (ClassInfo class1, integer n1, |
| 105 | conststring32 title, conststring32 after, uint32 flags, UiCallback callback, conststring32 nameOfCallback) |
| 106 | { praat_addAction4_ (class1, n1, nullptr, 0, nullptr, 0, nullptr, 0, title, after, flags, callback, nameOfCallback); } |
| 107 | |
| 108 | void praat_addAction2_ (ClassInfo class1, integer n1, ClassInfo class2, integer n2, |
| 109 | conststring32 title, conststring32 after, uint32 flags, UiCallback callback, conststring32 nameOfCallback) |
| 110 | { praat_addAction4_ (class1, n1, class2, n2, nullptr, 0, nullptr, 0, title, after, flags, callback, nameOfCallback); } |
| 111 | |
| 112 | void praat_addAction3_ (ClassInfo class1, integer n1, ClassInfo class2, integer n2, ClassInfo class3, integer n3, |
| 113 | conststring32 title, conststring32 after, uint32 flags, UiCallback callback, conststring32 nameOfCallback) |
| 114 | { praat_addAction4_ (class1, n1, class2, n2, class3, n3, nullptr, 0, title, after, flags, callback, nameOfCallback); } |
| 115 | |
| 116 | static void praat_addAction4__ (ClassInfo class1, integer n1, ClassInfo class2, integer n2, ClassInfo class3, integer n3, ClassInfo class4, integer n4, |
| 117 | conststring32 title, conststring32 after, uint32 flags, UiCallback callback, conststring32 nameOfCallback) |
| 118 | { |
| 119 | try { |
| 120 | uint32 depth = flags, key = 0; |
| 121 | bool unhidable = false, hidden = false, attractive = false; |
| 122 | uint32 guiFlags = 0; |
| 123 | if (flags > 7) { |
| 124 | depth = ((flags & GuiMenu_DEPTH_7) >> 16); |
| 125 | unhidable = (flags & GuiMenu_UNHIDABLE) != 0; |
| 126 | hidden = (flags & GuiMenu_HIDDEN) != 0 && ! unhidable; |
| 127 | key = flags & 0x000000FF; |
| 128 | guiFlags = key ? flags & (0x000000FF | GuiMenu_SHIFT | GuiMenu_BUTTON_STATE_MASK) : flags & GuiMenu_BUTTON_STATE_MASK; |
| 129 | attractive = (guiFlags & GuiMenu_ATTRACTIVE) != 0; |
| 130 | } |
| 131 | fixSelectionSpecification (& class1, & n1, & class2, & n2, & class3, & n3); |
| 132 | |
| 133 | if (callback && ! title) |
| 134 | Melder_throw (U"An action command with callback has no title. Classes: ", |
| 135 | class1 ? class1 -> className : U"", U" ", |
| 136 | class2 ? class2 -> className : U"", U" ", |
| 137 | class3 ? class3 -> className : U"", U" ", |
| 138 | class4 ? class4 -> className : U"", U"." |
| 139 | ); |
| 140 | |
| 141 | if (! class1) |
| 142 | Melder_throw (U"The action command \"", title, U"\" has no first class."); |
| 143 | |
| 144 | /* |
| 145 | Determine the position of the new command. |
| 146 | */ |
| 147 | integer position; |
| 148 | if (after) { // search for existing command with same selection |
| 149 | const integer found = lookUpMatchingAction (class1, class2, class3, class4, after); |
| 150 | if (found == 0) |
| 151 | Melder_throw (U"The action command \"", title, U"\" cannot be put after \"", after, U"\",\n" |
| 152 | U"because the latter command does not exist."); |
| 153 | position = found + 1; // after 'after' |
| 154 | } else { |
| 155 | position = theActions.size + 1; // at end |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | Make new command. |
| 160 | */ |
| 161 | autoPraat_Command action = Thing_new (Praat_Command); |
| 162 | action -> class1 = class1; |
| 163 | action -> n1 = n1; |
| 164 | action -> class2 = class2; |
| 165 | action -> n2 = n2; |
| 166 | action -> class3 = class3; |
| 167 | action -> n3 = n3; |
| 168 | action -> class4 = class4; |
| 169 | action -> n4 = n4; |
| 170 | action -> title = Melder_dup_f (title); |
| 171 | action -> depth = depth; |
| 172 | action -> callback = callback; // null for a separator |
| 173 | action -> nameOfCallback = nameOfCallback; |
| 174 | action -> button = nullptr; |
| 175 | action -> script = autostring32(); |
| 176 | action -> hidden = hidden; |
| 177 | action -> unhidable = unhidable; |
| 178 | action -> attractive = attractive; |
| 179 | |
| 180 | /* |
| 181 | Insert new command. |
| 182 | */ |
| 183 | theActions. addItemAtPosition_move (action.move(), position); |
| 184 | } catch (MelderError) { |
| 185 | Melder_flushError (); |
| 186 | } |
| 187 | } |
| 188 | void praat_addAction4_ (ClassInfo class1, integer n1, ClassInfo class2, integer n2, ClassInfo class3, integer n3, ClassInfo class4, integer n4, |
| 189 | conststring32 title, conststring32 after, uint32 flags, UiCallback callback, conststring32 nameOfCallback) |
| 190 | { |
| 191 | const char32 *pSeparator = str32str (title, U" || "); |
| 192 | if (! pSeparator) |
| 193 | return praat_addAction4__ (class1, n1, class2, n2, class3, n3, class4, n4, |
| 194 | title, after, flags, callback, nameOfCallback); |
| 195 | if (flags < 8) |
| 196 | flags *= GuiMenu_DEPTH_1; // turn 1..7 into GuiMenu_DEPTH_1..GuiMenu_DEPTH_7, because the flags are ORed below |
| 197 | integer positionOfSeparator = pSeparator - title; |
| 198 | static MelderString string; |
| 199 | MelderString_copy (& string, title); |
| 200 | char32 *pTitle = & string.string [0]; |
| 201 | do { |
| 202 | pTitle [positionOfSeparator] = U'\0'; |
| 203 | praat_addAction4__ (class1, n1, class2, n2, class3, n3, class4, n4, |
| 204 | pTitle, after, flags, callback, nameOfCallback); |
| 205 | pTitle += positionOfSeparator + 4; // step past " || " |
| 206 | pSeparator = str32str (pTitle, U" || "); |
| 207 | if (pSeparator) |
| 208 | positionOfSeparator = pSeparator - pTitle; |
| 209 | flags |= GuiMenu_HIDDEN; |
| 210 | } while (pSeparator); |
| 211 | praat_addAction4__ (class1, n1, class2, n2, class3, n3, class4, n4, |
| 212 | pTitle, after, flags | GuiMenu_HIDDEN, callback, nameOfCallback); |
| 213 | } |
| 214 | |
| 215 | static void deleteDynamicMenu () { |
| 216 | if (praatP.phase != praat_HANDLING_EVENTS) |
| 217 | return; |
| 218 | if (actionsInvisible) |
| 219 | return; |
| 220 | static integer numberOfDeletions; |
| 221 | trace (U"deletion #", ++ numberOfDeletions); |
| 222 | #if motif |
| 223 | /* |
| 224 | Undangle the menu items. They can be destroyed by the removal of their pull-down menu. |
| 225 | */ |
| 226 | for (integer i = 1; i <= theActions.size; i ++) { |
| 227 | Praat_Command action = theActions.at [i]; |
| 228 | Melder_assert (action); |
| 229 | Melder_assert (Thing_isa (action, classPraat_Command)); |
| 230 | if (action -> button) { |
| 231 | Melder_assert (action -> button -> classInfo != classGuiMenu); |
| 232 | if (action -> button -> classInfo == classGuiMenuItem) |
| 233 | action -> button = nullptr; // prevent destruction; the menu will take care of that |
| 234 | } |
| 235 | } |
| 236 | #endif |
| 237 | for (integer i = 1; i <= theActions.size; i ++) { |
| 238 | Praat_Command action = theActions.at [i]; |
| 239 | Melder_assert (action); |
| 240 | Melder_assert (Thing_isa (action, classPraat_Command)); |
| 241 | if (action -> button) { |
| 242 | trace (U"trying to destroy action ", i, U" of ", theActions.size, U", button ", Melder_pointer (action -> button), U": ", action -> title.get()); |
| 243 | #if gtk || cocoa |
| 244 | if (action -> button -> d_parent == praat_form) { |
| 245 | trace (U"destroy a label or a push button or a cascade button"); |
| 246 | GuiObject_destroy (action -> button -> d_widget); |
| 247 | } else if (praat_writeMenu && action -> button -> d_parent == praat_writeMenu) { |
| 248 | trace (U"destroying Save menu item"); |
| 249 | GuiObject_destroy (action -> button -> d_widget); |
| 250 | } |
| 251 | #elif motif |
| 252 | //trace ((int) * (char *) action -> button); // try to access the memory |
| 253 | Melder_assert (action -> button -> classInfo); |
| 254 | //trace (2); |
| 255 | if (action -> button -> classInfo == classGuiButton) |
| 256 | Melder_assert (action -> button -> d_widget); |
| 257 | //trace (3); |
| 258 | if (action -> button -> classInfo == classGuiButton && action -> button -> d_widget -> subMenuId) { // a cascade button (not a direct child of the form)? |
| 259 | #define TRY_BARLESS 0 |
| 260 | #if TRY_BARLESS |
| 261 | GuiObject_destroy (action -> button -> d_widget); |
| 262 | GuiObject_destroy (action -> button -> d_widget -> subMenuId); |
| 263 | #else |
| 264 | /* |
| 265 | We check that the parent is a RowColumn with exactly two children: a cascade button and a pulldown menu |
| 266 | */ |
| 267 | GuiObject xmMenuBarRowColumn = action -> button -> d_widget -> parent; |
| 268 | Melder_assert (xmMenuBarRowColumn); |
| 269 | trace (U"destroy the xm menu bar (widget class ", xmMenuBarRowColumn -> widgetClass, U"); this also destroys the xm button and the xm menu"); |
| 270 | Melder_assert (xmMenuBarRowColumn -> widgetClass == xmRowColumnWidgetClass); |
| 271 | GuiObject xmCascadeButton = xmMenuBarRowColumn -> firstChild; |
| 272 | Melder_assert (xmCascadeButton); |
| 273 | Melder_assert (xmCascadeButton -> widgetClass == xmCascadeButtonWidgetClass); |
| 274 | Melder_assert (xmCascadeButton == action -> button -> d_widget); |
| 275 | GuiObject xmPulldownMenu = xmCascadeButton -> nextSibling; |
| 276 | Melder_assert (xmPulldownMenu); |
| 277 | Melder_assert (xmPulldownMenu -> widgetClass == xmPulldownMenuWidgetClass); |
| 278 | Melder_assert (! xmPulldownMenu -> nextSibling); |
| 279 | Melder_assert (xmCascadeButton -> subMenuId == xmPulldownMenu); |
| 280 | /* |
| 281 | Let's do some controlled destruction. |
| 282 | */ |
| 283 | //GuiObject_destroy (xmCascadeButton); Melder_assert (xmMenuBarRowColumn -> firstChild == xmPulldownMenu); |
| 284 | //GuiObject_destroy (xmPulldownMenu); Melder_assert (xmMenuBarRowColumn -> firstChild == nullptr); |
| 285 | GuiObject_destroy (xmMenuBarRowColumn); |
| 286 | //GuiObject_destroy (action -> button -> d_widget); // that would be only the cascade button |
| 287 | #endif |
| 288 | } else if (action -> button -> d_parent == praat_form) { |
| 289 | GuiObject xmLabelOrButton = action -> button -> d_widget; |
| 290 | Melder_assert (xmLabelOrButton); |
| 291 | Melder_assert (xmLabelOrButton -> widgetClass == xmLabelWidgetClass || xmLabelOrButton -> widgetClass == xmPushButtonWidgetClass); |
| 292 | trace (U"destroy a label or a push button"); |
| 293 | GuiObject_destroy (xmLabelOrButton); |
| 294 | } |
| 295 | #endif |
| 296 | action -> button = nullptr; // undangle |
| 297 | } |
| 298 | } |
| 299 | if (praat_writeMenu) { |
| 300 | #if gtk || cocoa |
| 301 | if (praat_writeMenuSeparator) { |
| 302 | trace (U"destroy the Save menu separator"); |
| 303 | GuiObject_destroy (praat_writeMenuSeparator -> d_widget); |
| 304 | } |
| 305 | //praat_writeMenu -> f_empty (); |
| 306 | #elif motif |
| 307 | GuiObject_destroy (praat_writeMenu -> d_xmMenuTitle); |
| 308 | GuiObject_destroy (praat_writeMenu -> d_widget); |
| 309 | praat_writeMenu = GuiMenu_createInWindow (praatP.menuBar, U"Save", 0); |
| 310 | #endif |
| 311 | praat_writeMenuSeparator = nullptr; // undangle |
| 312 | } |
| 313 | actionsInvisible = true; |
| 314 | } |
| 315 | |
| 316 | static void updateDynamicMenu () { |
| 317 | if (praatP.phase != praat_HANDLING_EVENTS) |
| 318 | return; |
| 319 | praat_sortActions (); |
| 320 | deleteDynamicMenu (); |
| 321 | praat_show (); |
| 322 | } |
| 323 | |
| 324 | void praat_addActionScript (conststring32 className1, integer n1, conststring32 className2, integer n2, conststring32 className3, integer n3, |
| 325 | conststring32 title, conststring32 after, integer depth, conststring32 script) |
| 326 | { |
| 327 | try { |
| 328 | ClassInfo class1 = nullptr, class2 = nullptr, class3 = nullptr; |
| 329 | Melder_assert (className1 && className2 && className3 && title && after && script); |
| 330 | if (className1 [0] != U'\0') |
| 331 | class1 = Thing_classFromClassName (className1, nullptr); |
| 332 | if (className2 [0] != U'\0') |
| 333 | class2 = Thing_classFromClassName (className2, nullptr); |
| 334 | if (className3 [0] != U'\0') |
| 335 | class3 = Thing_classFromClassName (className3, nullptr); |
| 336 | fixSelectionSpecification (& class1, & n1, & class2, & n2, & class3, & n3); |
| 337 | |
| 338 | if (script [0] != U'\0' && title [0] == U'\0') |
| 339 | Melder_throw (U"Command with callback has no title. Classes: ", className1, U" ", className2, U" ", className3, U"."); |
| 340 | |
| 341 | if (className1 [0] == U'\0') |
| 342 | Melder_throw (U"Command \"", title, U"\" has no first class."); |
| 343 | |
| 344 | /* |
| 345 | If the button already exists, remove it. |
| 346 | */ |
| 347 | {// scope |
| 348 | integer found = lookUpMatchingAction (class1, class2, class3, nullptr, title); |
| 349 | if (found) |
| 350 | theActions. removeItem (found); |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | Determine the position of the new command. |
| 355 | */ |
| 356 | integer position; |
| 357 | if (after [0] != U'\0') { // search for existing command with same selection |
| 358 | integer found = lookUpMatchingAction (class1, class2, class3, nullptr, after); |
| 359 | if (found) |
| 360 | position = found + 1; // after 'after' |
| 361 | else |
| 362 | position = theActions.size + 1; // at end |
| 363 | } else { |
| 364 | position = theActions.size + 1; // at end |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | Create new command. |
| 369 | */ |
| 370 | autoPraat_Command action = Thing_new (Praat_Command); |
| 371 | action -> class1 = class1; |
| 372 | action -> n1 = n1; |
| 373 | action -> class2 = class2; |
| 374 | action -> n2 = n2; |
| 375 | action -> class3 = class3; |
| 376 | action -> n3 = n3; |
| 377 | action -> title = title [0] != U'\0' ? Melder_dup_f (title) : autostring32(); // allow old-fashioned untitled separators |
| 378 | action -> depth = depth; |
| 379 | action -> callback = ( script [0] != U'\0' ? DO_RunTheScriptFromAnyAddedMenuCommand : nullptr ); // null for a separator |
| 380 | action -> button = nullptr; |
| 381 | if (script [0] == U'\0') { |
| 382 | action -> script = autostring32(); |
| 383 | } else { |
| 384 | structMelderFile file { }; |
| 385 | Melder_relativePathToFile (script, & file); |
| 386 | action -> script = Melder_dup_f (MelderFile_peekPath (& file)); |
| 387 | } |
| 388 | action -> after = ( after [0] != U'\0' ? Melder_dup_f (after) : autostring32() ); |
| 389 | action -> phase = praatP.phase; |
| 390 | if (praatP.phase >= praat_READING_BUTTONS) { |
| 391 | static integer uniqueID = 0; |
| 392 | action -> uniqueID = ++ uniqueID; |
| 393 | } |
| 394 | action -> added = true; |
| 395 | |
| 396 | /* |
| 397 | Insert new command. |
| 398 | */ |
| 399 | theActions. addItemAtPosition_move (action.move(), position); |
| 400 | updateDynamicMenu (); |
| 401 | } catch (MelderError) { |
| 402 | Melder_throw (U"Praat: script action not added."); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | void praat_removeAction (ClassInfo class1, ClassInfo class2, ClassInfo class3, conststring32 title) { |
| 407 | try { |
| 408 | integer n1, n2, n3; |
| 409 | fixSelectionSpecification (& class1, & n1, & class2, & n2, & class3, & n3); |
| 410 | integer found = lookUpMatchingAction (class1, class2, class3, nullptr, title); |
| 411 | if (! found) { |
| 412 | Melder_throw (U"Action command \"", class1 -> className, |
| 413 | class2 ? U" & ": U"", class2 -> className, |
| 414 | class3 ? U" & ": U"", class3 -> className, |
| 415 | U": ", title, U"\" not found." |
| 416 | ); |
| 417 | } |
| 418 | theActions. removeItem (found); |
| 419 | } catch (MelderError) { |
| 420 | Melder_throw (U"Praat: action not removed."); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void praat_removeAction_classNames (conststring32 className1, conststring32 className2, |
| 425 | conststring32 className3, conststring32 title) |
| 426 | { |
| 427 | try { |
| 428 | ClassInfo class1 = nullptr, class2 = nullptr, class3 = nullptr; |
| 429 | Melder_assert (className1 && className2 && className3 && title); |
| 430 | if (className1 [0] != U'\0') |
| 431 | class1 = Thing_classFromClassName (className1, nullptr); |
| 432 | if (className2 [0] != U'\0') |
| 433 | class2 = Thing_classFromClassName (className2, nullptr); |
| 434 | if (className3 [0] != U'\0') |
| 435 | class3 = Thing_classFromClassName (className3, nullptr); |
| 436 | praat_removeAction (class1, class2, class3, title); |
| 437 | updateDynamicMenu (); |
| 438 | } catch (MelderError) { |
| 439 | Melder_throw (U"Praat: action not removed."); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | void praat_hideAction (ClassInfo class1, ClassInfo class2, ClassInfo class3, conststring32 title) { |
| 444 | try { |
| 445 | integer n1, n2, n3; |
| 446 | fixSelectionSpecification (& class1, & n1, & class2, & n2, & class3, & n3); |
| 447 | integer found = lookUpMatchingAction (class1, class2, class3, nullptr, title); |
| 448 | if (! found) { |
| 449 | Melder_throw (U"Praat: action command \"", class1 ? class1 -> className : nullptr, |
| 450 | class2 ? U" & ": nullptr, class2 ? class2 -> className : nullptr, |
| 451 | class3 ? U" & ": nullptr, class3 ? class3 -> className : nullptr, |
| 452 | U": ", title, U"\" not found." |
| 453 | ); |
| 454 | } |
| 455 | Praat_Command action = theActions.at [found]; |
| 456 | if (! action -> hidden) { |
| 457 | action -> hidden = true; |
| 458 | if (praatP.phase >= praat_READING_BUTTONS) |
| 459 | action -> toggled = ! action -> toggled; |
| 460 | updateDynamicMenu (); |
| 461 | } |
| 462 | } catch (MelderError) { |
| 463 | Melder_throw (U"Praat: action not hidden."); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | void praat_hideAction_classNames (conststring32 className1, conststring32 className2, |
| 468 | conststring32 className3, conststring32 title) |
| 469 | { |
| 470 | try { |
| 471 | ClassInfo class1 = nullptr, class2 = nullptr, class3 = nullptr; |
| 472 | Melder_assert (className1 && className2 && className3 && title); |
| 473 | if (className1 [0] != U'\0') |
| 474 | class1 = Thing_classFromClassName (className1, nullptr); |
| 475 | if (className2 [0] != U'\0') |
| 476 | class2 = Thing_classFromClassName (className2, nullptr); |
| 477 | if (className3 [0] != U'\0') |
| 478 | class3 = Thing_classFromClassName (className3, nullptr); |
| 479 | praat_hideAction (class1, class2, class3, title); |
| 480 | } catch (MelderError) { |
| 481 | Melder_throw (U"Praat: action not hidden."); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | void praat_showAction (ClassInfo class1, ClassInfo class2, ClassInfo class3, conststring32 title) { |
| 486 | try { |
| 487 | integer n1, n2, n3; |
| 488 | fixSelectionSpecification (& class1, & n1, & class2, & n2, & class3, & n3); |
| 489 | integer found = lookUpMatchingAction (class1, class2, class3, nullptr, title); |
| 490 | if (! found) { |
| 491 | Melder_throw (U"Action command \"", class1 ? class1 -> className : nullptr, |
| 492 | class2 ? U" & ": nullptr, class2 ? class2 -> className : nullptr, |
| 493 | class3 ? U" & ": nullptr, class3 ? class3 -> className : nullptr, |
| 494 | U": ", title, U"\" not found." |
| 495 | ); |
| 496 | } |
| 497 | Praat_Command action = theActions.at [found]; |
| 498 | if (action -> hidden) { |
| 499 | action -> hidden = false; |
| 500 | if (praatP.phase >= praat_READING_BUTTONS) |
| 501 | action -> toggled = ! action -> toggled; |
| 502 | updateDynamicMenu (); |
| 503 | } |
| 504 | } catch (MelderError) { |
| 505 | Melder_throw (U"Praat: action not shown."); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | void praat_showAction_classNames (conststring32 className1, conststring32 className2, |
| 510 | conststring32 className3, conststring32 title) |
| 511 | { |
| 512 | try { |
| 513 | ClassInfo class1 = nullptr, class2 = nullptr, class3 = nullptr; |
| 514 | Melder_assert (className1 && className2 && className3 && title); |
| 515 | if (className1 [0] != U'\0') |
| 516 | class1 = Thing_classFromClassName (className1, nullptr); |
| 517 | if (className2 [0] != U'\0') |
| 518 | class2 = Thing_classFromClassName (className2, nullptr); |
| 519 | if (className3 [0] != U'\0') |
| 520 | class3 = Thing_classFromClassName (className3, nullptr); |
| 521 | praat_showAction (class1, class2, class3, title); |
| 522 | } catch (MelderError) { |
| 523 | Melder_throw (U"Praat: action not shown."); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | void praat_sortActions () { |
| 528 | for (integer i = 1; i <= theActions.size; i ++) { |
| 529 | Praat_Command action = theActions.at [i]; |
| 530 | action -> sortingTail = i; |
| 531 | } |
| 532 | std::sort (theActions.begin(), theActions.end(), |
| 533 | [] (Praat_Command me, Praat_Command thee) { |
| 534 | int compare = str32cmp (my class1 -> className, thy class1 -> className); |
| 535 | if (compare != 0) |
| 536 | return my class1 == classDaata ? true : thy class1 == classDaata ? false : ( compare < 0 ); |
| 537 | if (my class2) { |
| 538 | if (! thy class2) |
| 539 | return false; |
| 540 | compare = str32cmp (my class2 -> className, thy class2 -> className); |
| 541 | if (compare != 0) |
| 542 | return compare < 0; |
| 543 | } else if (thy class2) |
| 544 | return true; |
| 545 | if (my class3) { |
| 546 | if (! thy class3) |
| 547 | return false; |
| 548 | compare = str32cmp (my class3 -> className, thy class3 -> className); |
| 549 | if (compare != 0) |
| 550 | return compare < 0; |
| 551 | } else if (thy class3) |
| 552 | return true; |
| 553 | return my sortingTail < thy sortingTail; |
| 554 | } |
| 555 | ); |
| 556 | } |
| 557 | |
| 558 | static conststring32 numberString (integer number) { |
| 559 | return number == 1 ? U"one" : number == 2 ? U"two" : number == 3 ? U"three" : U"any number of"; |
| 560 | } |
| 561 | static conststring32 classString (ClassInfo klas) { |
| 562 | return klas == classDaata ? U"" : klas -> className; |
| 563 | } |
| 564 | static conststring32 objectString (integer number) { |
| 565 | return number == 1 ? U"object" : U"objects"; |
| 566 | } |
| 567 | static bool allowExecutionHook (void *closure) { |
| 568 | UiCallback callback = (UiCallback) closure; |
| 569 | Melder_assert (sizeof (callback) == sizeof (void *)); |
| 570 | integer numberOfMatchingCallbacks = 0, firstMatchingCallback = 0; |
| 571 | for (integer i = 1; i <= theActions.size; i ++) { |
| 572 | Praat_Command me = theActions.at [i]; |
| 573 | if (my callback == callback) { |
| 574 | integer sel1, sel2 = 0, sel3 = 0, sel4 = 0; |
| 575 | if (! my class1) |
| 576 | Melder_throw (U"No class1???"); |
| 577 | numberOfMatchingCallbacks += 1; |
| 578 | if (firstMatchingCallback == 0) |
| 579 | firstMatchingCallback = i; |
| 580 | sel1 = ( my class1 == classDaata ? theCurrentPraatObjects -> totalSelection : praat_numberOfSelected (my class1) ); |
| 581 | if (sel1 == 0) |
| 582 | continue; |
| 583 | if (my class2 && (sel2 = praat_numberOfSelected (my class2)) == 0) |
| 584 | continue; |
| 585 | if (my class3 && (sel3 = praat_numberOfSelected (my class3)) == 0) |
| 586 | continue; |
| 587 | if (my class4 && (sel4 = praat_numberOfSelected (my class4)) == 0) |
| 588 | continue; |
| 589 | if (sel1 + sel2 + sel3 + sel4 != theCurrentPraatObjects -> totalSelection) |
| 590 | continue; |
| 591 | if ((my n1 && sel1 != my n1) || (my n2 && sel2 != my n2) || (my n3 && sel3 != my n3) || (my n4 && sel4 != my n4)) |
| 592 | continue; |
| 593 | return true; // found a matching action |
| 594 | } |
| 595 | } |
| 596 | if (numberOfMatchingCallbacks == 1) { |
| 597 | Praat_Command me = theActions.at [firstMatchingCallback]; |
| 598 | Melder_appendError (U"Selection changed! It should be:"); |
| 599 | if (my class1) |
| 600 | Melder_appendError (U" ", numberString (my n1), U" ", classString (my class1), U" ", objectString (my n1)); |
| 601 | if (my class2) |
| 602 | Melder_appendError (U" ", numberString (my n2), U" ", classString (my class2), U" ", objectString (my n2)); |
| 603 | if (my class3) |
| 604 | Melder_appendError (U" ", numberString (my n3), U" ", classString (my class3), U" ", objectString (my n3)); |
| 605 | if (my class4) |
| 606 | Melder_appendError (U" ", numberString (my n4), U" ", classString (my class4), U" ", objectString (my n4)); |
| 607 | throw MelderError (); |
| 608 | } else { |
| 609 | Melder_throw (U"Selection changed!"); |
| 610 | } |
| 611 | return false; |
| 612 | } |
| 613 | |
| 614 | static void do_menu (Praat_Command me, const bool isModified) { |
| 615 | if (my callback == DO_RunTheScriptFromAnyAddedMenuCommand) { |
| 616 | UiHistory_write (U"\nrunScript: "); |
| 617 | try { |
| 618 | DO_RunTheScriptFromAnyAddedMenuCommand (nullptr, 0, nullptr, my script.get(), nullptr, nullptr, false, nullptr, nullptr); |
| 619 | } catch (MelderError) { |
| 620 | Melder_flushError (U"Command \"", my title.get(), U"\" not executed."); |
| 621 | } |
| 622 | praat_updateSelection (); return; |
| 623 | } else { |
| 624 | if (my title && ! str32str (my title.get(), U"...")) { |
| 625 | UiHistory_write (U"\n"); |
| 626 | UiHistory_write (my title.get()); |
| 627 | } |
| 628 | Ui_setAllowExecutionHook (allowExecutionHook, (void *) my callback); // BUG: one shouldn't assign a function pointer to a void pointer |
| 629 | try { |
| 630 | my callback (nullptr, 0, nullptr, nullptr, nullptr, my title.get(), isModified, nullptr, nullptr); |
| 631 | } catch (MelderError) { |
| 632 | Melder_flushError (U"Command \"", my title.get(), U"\" not executed."); |
| 633 | } |
| 634 | Ui_setAllowExecutionHook (nullptr, nullptr); |
| 635 | praat_updateSelection (); return; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | static void cb_menu (Praat_Command me, GuiMenuItemEvent event) { |
| 640 | const bool isModified = event -> shiftKeyPressed || event -> commandKeyPressed || event -> optionKeyPressed; |
| 641 | do_menu (me, isModified); |
| 642 | } |
| 643 | |
| 644 | static void gui_button_cb_menu (Praat_Command me, GuiButtonEvent event) { |
| 645 | const bool isModified = event -> shiftKeyPressed || event -> commandKeyPressed || event -> optionKeyPressed; |
| 646 | do_menu (me, isModified); |
| 647 | } |
| 648 | |
| 649 | void praat_actions_show () { |
| 650 | #if defined (macintosh) |
| 651 | const int BUTTON_VSPACING = 8; |
| 652 | #else |
| 653 | const int BUTTON_VSPACING = 5; |
| 654 | #endif |
| 655 | /* |
| 656 | The selection has changed; |
| 657 | kill the dynamic menu and the write menu. |
| 658 | */ |
| 659 | if (! theCurrentPraatApplication -> batch) { |
| 660 | deleteDynamicMenu (); |
| 661 | if (! Melder_backgrounding) { |
| 662 | GuiThing_setSensitive (praat_writeMenu, false); |
| 663 | if (praat_writeMenuSeparator) |
| 664 | GuiThing_hide (praat_writeMenuSeparator); |
| 665 | } |
| 666 | |
| 667 | /* |
| 668 | Determine the visibility and sensitivity of all the actions. |
| 669 | */ |
| 670 | if (theCurrentPraatObjects -> totalSelection != 0 && ! Melder_backgrounding) |
| 671 | GuiThing_setSensitive (praat_writeMenu, true); |
| 672 | } |
| 673 | for (integer i = 1; i <= theActions.size; i ++) { |
| 674 | Praat_Command action = theActions.at [i]; |
| 675 | integer sel1 = 0, sel2 = 0, sel3 = 0, sel4 = 0; |
| 676 | const integer n1 = action -> n1, n2 = action -> n2, n3 = action -> n3, n4 = action -> n4; |
| 677 | |
| 678 | /* |
| 679 | Clean up from previous selection. |
| 680 | */ |
| 681 | action -> visible = false; |
| 682 | action -> executable = false; |
| 683 | |
| 684 | /* |
| 685 | Match the actually selected classes with the selection required for this visibility. |
| 686 | */ |
| 687 | if (! action -> class1) |
| 688 | continue; // at least one class selected |
| 689 | sel1 = ( action -> class1 == classDaata ? theCurrentPraatObjects -> totalSelection : praat_numberOfSelected (action -> class1) ); |
| 690 | if (sel1 == 0) |
| 691 | continue; |
| 692 | if (action -> class2 && (sel2 = praat_numberOfSelected (action -> class2)) == 0) |
| 693 | continue; |
| 694 | if (action -> class3 && (sel3 = praat_numberOfSelected (action -> class3)) == 0) |
| 695 | continue; |
| 696 | if (action -> class4 && (sel4 = praat_numberOfSelected (action -> class4)) == 0) |
| 697 | continue; |
| 698 | if (sel1 + sel2 + sel3 + sel4 != theCurrentPraatObjects -> totalSelection) |
| 699 | continue; // other classes selected? Do not show |
| 700 | action -> visible = ! action -> hidden; |
| 701 | |
| 702 | /* |
| 703 | Match the actually selected objects with the selection required for this action. |
| 704 | */ |
| 705 | if (! action -> callback) |
| 706 | continue; // separators are not executable |
| 707 | if ((n1 && sel1 != n1) || (n2 && sel2 != n2) || (n3 && sel3 != n3) || (n4 && sel4 != n4)) |
| 708 | continue; |
| 709 | action -> executable = true; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | Create a new column of buttons in the dynamic menu. |
| 714 | */ |
| 715 | if (! theCurrentPraatApplication -> batch && ! Melder_backgrounding) { |
| 716 | actionsInvisible = false; |
| 717 | GuiMenu currentSubmenu1 = nullptr, currentSubmenu2 = nullptr; |
| 718 | bool writeMenuGoingToSeparate = false; |
| 719 | int y = Machine_getMenuBarBottom () + 10; |
| 720 | for (integer i = 1; i <= theActions.size; i ++) { // add buttons or make existing buttons sensitive (executable) |
| 721 | Praat_Command me = theActions.at [i]; |
| 722 | if (my depth == 0) { |
| 723 | currentSubmenu1 = nullptr; |
| 724 | currentSubmenu2 = nullptr; // prevent attachment of later deep actions to earlier submenus after removal of label |
| 725 | } |
| 726 | if (my depth == 1) |
| 727 | currentSubmenu2 = nullptr; // prevent attachment of later deep actions to earlier submenus after removal of label |
| 728 | if (! my visible) |
| 729 | continue; |
| 730 | if (my callback) { |
| 731 | /* |
| 732 | Apparently a true command: create a button in the dynamic menu. |
| 733 | If it is a subcommand (depth > 0), put it in the current submenu, |
| 734 | but only if this exists (umbrella against stray submenu specifications). |
| 735 | */ |
| 736 | GuiMenu parentMenu = ( my depth > 1 && currentSubmenu2 ? currentSubmenu2 : my depth > 0 && currentSubmenu1 ? currentSubmenu1 : nullptr ); |
| 737 | |
| 738 | if (str32nequ (my title.get(), U"Save ", 5) || str32nequ (my title.get(), U"Write ", 6) || str32nequ (my title.get(), U"Append to ", 10)) { |
| 739 | parentMenu = praat_writeMenu; |
| 740 | if (! praat_writeMenuSeparator) { |
| 741 | if (writeMenuGoingToSeparate) |
| 742 | praat_writeMenuSeparator = GuiMenu_addSeparator (parentMenu); |
| 743 | else if (str32equ (my title.get(), U"Save as binary file...")) |
| 744 | writeMenuGoingToSeparate = true; |
| 745 | } |
| 746 | } |
| 747 | if (parentMenu) { |
| 748 | my button = GuiMenu_addItem (parentMenu, my added ? Melder_cat (U"\u207A", my title.get()) : my title.get(), |
| 749 | ( my executable ? 0 : GuiMenu_INSENSITIVE ), |
| 750 | cb_menu, me |
| 751 | ); |
| 752 | } else { |
| 753 | my button = GuiButton_createShown (praat_form, |
| 754 | BUTTON_LEFT, BUTTON_RIGHT, y, y + Gui_PUSHBUTTON_HEIGHT, |
| 755 | my added ? Melder_cat (U"\u207A", my title.get()) : my title.get(), gui_button_cb_menu, |
| 756 | me, ( my executable ? 0 : GuiButton_INSENSITIVE ) | ( my attractive ? GuiButton_ATTRACTIVE : 0 ) |
| 757 | ); |
| 758 | y += Gui_PUSHBUTTON_HEIGHT + BUTTON_VSPACING; |
| 759 | } |
| 760 | } else if (i == theActions.size || theActions.at [i + 1] -> depth == 0) { |
| 761 | /* |
| 762 | Apparently a labelled separator. |
| 763 | */ |
| 764 | my button = GuiLabel_createShown (praat_form, BUTTON_LEFT, BUTTON_RIGHT, y, y + Gui_LABEL_HEIGHT, my added ? Melder_cat (U"\u207A", my title.get()) : my title.get(), 0); |
| 765 | y += Gui_LABEL_HEIGHT + BUTTON_VSPACING; |
| 766 | } else if (! my title || my title [0] == U'-') { |
| 767 | /* |
| 768 | Apparently a separator in a submenu. |
| 769 | */ |
| 770 | if (currentSubmenu2 || currentSubmenu1) { // these separators are not shown in a flattened menu |
| 771 | my button = GuiMenu_addSeparator (currentSubmenu2 ? currentSubmenu2 : currentSubmenu1); |
| 772 | GuiThing_show (my button); |
| 773 | } |
| 774 | } else { |
| 775 | /* |
| 776 | Apparently a submenu. |
| 777 | */ |
| 778 | if (my depth == 0 || ! currentSubmenu1) { |
| 779 | currentSubmenu1 = GuiMenu_createInForm (praat_form, |
| 780 | BUTTON_LEFT, BUTTON_RIGHT, y, y + Gui_PUSHBUTTON_HEIGHT, |
| 781 | my added ? Melder_cat (U"\u207A", my title.get()) : my title.get(), 0 |
| 782 | ); |
| 783 | y += Gui_PUSHBUTTON_HEIGHT + BUTTON_VSPACING; |
| 784 | my button = currentSubmenu1 -> d_cascadeButton.get(); |
| 785 | } else { |
| 786 | currentSubmenu2 = GuiMenu_createInMenu (currentSubmenu1, my added ? Melder_cat (U"\u207A", my title.get()) : my title.get(), 0); |
| 787 | my button = currentSubmenu2 -> d_menuItem.get(); |
| 788 | } |
| 789 | GuiThing_show (my button); |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | void praat_actions_createWriteMenu (GuiWindow window) { |
| 796 | if (theCurrentPraatApplication -> batch) |
| 797 | return; |
| 798 | praat_writeMenu = GuiMenu_createInWindow (window, U"Save", GuiMenu_INSENSITIVE); |
| 799 | #if gtk |
| 800 | GuiMenu_addSeparator (praat_writeMenu); |
| 801 | #endif |
| 802 | } |
| 803 | |
| 804 | void praat_actions_createDynamicMenu (GuiWindow window) { |
| 805 | if (theCurrentPraatApplication -> batch) |
| 806 | return; |
| 807 | praat_form = window; |
| 808 | } |
| 809 | |
| 810 | void praat_saveAddedActions (MelderString *buffer) { |
| 811 | integer maxID = 0; |
| 812 | for (integer iaction = 1; iaction <= theActions.size; iaction ++) { |
| 813 | Praat_Command action = theActions.at [iaction]; |
| 814 | if (action -> uniqueID > maxID) |
| 815 | maxID = action -> uniqueID; |
| 816 | } |
| 817 | for (integer ident = 1; ident <= maxID; ident ++) |
| 818 | for (integer iaction = 1; iaction <= theActions.size; iaction ++) { |
| 819 | Praat_Command me = theActions.at [iaction]; |
| 820 | if (my uniqueID == ident && ! my hidden && my title) { |
| 821 | MelderString_append (buffer, U"Add action command...", |
| 822 | U" ", my class1 -> className, U" ", my n1, |
| 823 | U" ", ( my class2 ? my class2 -> className : U"\"\"" ), U" ", my n2, |
| 824 | U" ", ( my class3 ? my class3 -> className : U"\"\"" ), U" ", my n3, |
| 825 | U" \"", my title.get(), U"\" \"", ( my after ? my after.get() : U"" ), U"\" ", my depth); |
| 826 | MelderString_append (buffer, U" ", my script ? my script.get() : U"", U"\n"); |
| 827 | break; |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | void praat_saveToggledActions (MelderString *buffer) { |
| 833 | for (integer iaction = 1; iaction <= theActions.size; iaction ++) { |
| 834 | Praat_Command me = theActions.at [iaction]; |
| 835 | if (my toggled && my title && ! my uniqueID && ! my script) { |
| 836 | MelderString_append (buffer, ( my hidden ? U"Hide" : U"Show" ), U" action command...", |
| 837 | U" ", my class1 -> className, |
| 838 | U" ", ( my class2 ? my class2 -> className : U"\"\"" ), |
| 839 | U" ", ( my class3 ? my class3 -> className : U"\"\"" ), |
| 840 | U" ", my title.get(), U"\n" |
| 841 | ); |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | int praat_doAction (conststring32 title, conststring32 arguments, Interpreter interpreter) { |
| 847 | Praat_Command actionFound = nullptr; |
| 848 | for (integer i = 1; i <= theActions.size; i ++) { |
| 849 | Praat_Command action = theActions.at [i]; |
| 850 | if (action -> executable && str32equ (action -> title.get(), title)) { |
| 851 | actionFound = action; |
| 852 | break; |
| 853 | } |
| 854 | } |
| 855 | if (! actionFound) |
| 856 | return 0; |
| 857 | if (actionFound -> callback == DO_RunTheScriptFromAnyAddedMenuCommand) { |
| 858 | const conststring32 scriptPath = actionFound -> script.get(); |
| 859 | const conststring32 preferencesFolderPath = MelderFolder_peekPath (Melder_preferencesFolder()); |
| 860 | const bool scriptIsInPlugin = Melder_startsWith (scriptPath, preferencesFolderPath); |
| 861 | Melder_throw ( |
| 862 | U"From a script you cannot directly call a menu command that calls another script. Use instead: \nrunScript: ", |
| 863 | scriptIsInPlugin ? U"preferencesDirectory$ + " : U"", |
| 864 | U"\"", |
| 865 | scriptIsInPlugin ? scriptPath + Melder_length (preferencesFolderPath) : scriptPath, |
| 866 | U"\"", |
| 867 | arguments && arguments [0] ? U", " : U"", |
| 868 | arguments && arguments [0] ? arguments : U"", |
| 869 | U"\n" |
| 870 | ); |
| 871 | } |
| 872 | actionFound -> callback (nullptr, 0, nullptr, arguments, interpreter, title, false, nullptr, nullptr); |
| 873 | return 1; |
| 874 | } |
| 875 | |
| 876 | int praat_doAction (conststring32 title, integer narg, Stackel args, Interpreter interpreter) { |
| 877 | Praat_Command actionFound = nullptr; |
| 878 | for (integer i = 1; i <= theActions.size; i ++) { |
| 879 | Praat_Command action = theActions.at [i]; |
| 880 | if (action -> executable && str32equ (action -> title.get(), title)) { |
| 881 | actionFound = action; |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | if (! actionFound) |
| 886 | return 0; |
| 887 | if (actionFound -> callback == DO_RunTheScriptFromAnyAddedMenuCommand) { |
| 888 | const conststring32 scriptPath = actionFound -> script.get(); |
| 889 | const conststring32 preferencesFolderPath = MelderFolder_peekPath (Melder_preferencesFolder()); |
| 890 | const bool scriptIsInPlugin = Melder_startsWith (scriptPath, preferencesFolderPath); |
| 891 | Melder_throw ( |
| 892 | U"From a script you cannot directly call a menu command that calls another script. Use instead: \nrunScript: ", |
| 893 | scriptIsInPlugin ? U"preferencesDirectory$ + " : U"", |
| 894 | U"\"", |
| 895 | scriptIsInPlugin ? scriptPath + Melder_length (preferencesFolderPath) : scriptPath, |
| 896 | U"\"", |
| 897 | narg > 0 ? U", ..." : U"", |
| 898 | U"\n" |
| 899 | ); |
| 900 | } |
| 901 | actionFound -> callback (nullptr, narg, args, nullptr, interpreter, title, false, nullptr, nullptr); |
| 902 | return 1; |
| 903 | } |
| 904 | |
| 905 | integer praat_getNumberOfActions () { return theActions.size; } |
| 906 | |
| 907 | Praat_Command praat_getAction (integer i) |
| 908 | { return i < 0 || i > theActions.size ? nullptr : theActions.at [i]; } |
| 909 | |
| 910 | void praat_background () { |
| 911 | if (Melder_batch) |
| 912 | return; |
| 913 | if (Melder_backgrounding) |
| 914 | return; |
| 915 | deleteDynamicMenu (); |
| 916 | praat_list_background (); |
| 917 | Melder_backgrounding = true; |
| 918 | } |
| 919 | |
| 920 | void praat_foreground () { |
| 921 | if (Melder_batch) |
| 922 | return; |
| 923 | if (! Melder_backgrounding) |
| 924 | return; |
| 925 | Melder_backgrounding = false; |
| 926 | praat_list_foreground (); |
| 927 | praat_show (); |
| 928 | } |
| 929 | |
| 930 | static bool actionIsToBeIncluded (Praat_Command command, bool deprecated, bool includeSaveAPI, |
| 931 | bool includeQueryAPI, bool includeModifyAPI, bool includeToAPI, |
| 932 | bool includePlayAPI, bool includeDrawAPI, bool includeHelpAPI, bool includeWindowAPI) |
| 933 | { |
| 934 | const bool obsolete = ( deprecated && (command -> deprecationYear < Melder_appYear() - 10 || command -> deprecationYear < 2017) ); |
| 935 | const bool hiddenByDefault = ( command -> hidden != command -> toggled ); |
| 936 | const bool explicitlyHidden = hiddenByDefault && ! deprecated; |
| 937 | const bool hidden = explicitlyHidden || ! command -> callback || command -> noApi || obsolete || |
| 938 | (! includeWindowAPI && Melder_nequ (command -> nameOfCallback, U"WINDOW_", 7)) || |
| 939 | (! includeHelpAPI && Melder_nequ (command -> nameOfCallback, U"HELP_", 5)) || |
| 940 | (! includeDrawAPI && Melder_nequ (command -> nameOfCallback, U"GRAPHICS_", 9)) || |
| 941 | (! includePlayAPI && Melder_nequ (command -> nameOfCallback, U"PLAY_", 5)) || |
| 942 | (! includeToAPI && Melder_nequ (command -> nameOfCallback, U"CONVERT_", 8)) || |
| 943 | (! includeModifyAPI && Melder_nequ (command -> nameOfCallback, U"MODIFY_", 7)) || |
| 944 | (! includeQueryAPI && Melder_nequ (command -> nameOfCallback, U"QUERY_", 6)) || |
| 945 | (! includeSaveAPI && Melder_nequ (command -> nameOfCallback, U"SAVE_", 5)); |
| 946 | return (command -> forceApi || ! hidden) && command -> callback != DO_RunTheScriptFromAnyAddedMenuCommand; |
| 947 | } |
| 948 | |
| 949 | static bool actionHasFileNameArgument (Praat_Command command) { |
| 950 | const bool hasFileNameArgument = |
| 951 | Melder_nequ (command -> nameOfCallback, U"READ_ONE__", 10) || |
| 952 | Melder_nequ (command -> nameOfCallback, U"SAVE_", 5) |
| 953 | ; |
| 954 | return hasFileNameArgument; |
| 955 | } |
| 956 | |
| 957 | static conststring32 getReturnType (Praat_Command command) { |
| 958 | const conststring32 returnType = |
| 959 | Melder_nequ (command -> nameOfCallback, U"NEW1_", 5) ? U"PraatObject" : |
| 960 | Melder_nequ (command -> nameOfCallback, U"READ_ONE__", 10) ? U"PraatObject" : |
| 961 | Melder_nequ (command -> nameOfCallback, U"REAL_", 5) ? U"double" : |
| 962 | Melder_nequ (command -> nameOfCallback, U"INTEGER_", 8) ? U"int64_t" : |
| 963 | Melder_nequ (command -> nameOfCallback, U"STRING_", 7) ? U"char *" : |
| 964 | Melder_nequ (command -> nameOfCallback, U"REPORT_", 7) ? U"char *" : |
| 965 | Melder_nequ (command -> nameOfCallback, U"LIST_", 5) ? U"char *" : |
| 966 | Melder_nequ (command -> nameOfCallback, U"INFO_", 5) ? U"char *" : |
| 967 | Melder_nequ (command -> nameOfCallback, U"HINT_", 5) ? U"char *" : |
| 968 | U"void"; |
| 969 | return returnType; |
| 970 | } |
| 971 | |
| 972 | void praat_actions_writeC (bool isInHeaderFile, bool includeSaveAPI, |
| 973 | bool includeQueryAPI, bool includeModifyAPI, bool includeToAPI, |
| 974 | bool includePlayAPI, bool includeDrawAPI, bool includeHelpAPI, bool includeWindowAPI) |
| 975 | { |
| 976 | integer i = 1; |
| 977 | try { |
| 978 | integer numberOfApiActions = 0; |
| 979 | for (; i <= theActions.size; i ++) { |
| 980 | //Melder_casual (i, U": ", theActions.at [i] -> class1 -> className, U": ", theActions.at [i] -> title.get()); |
| 981 | Praat_Command command = theActions.at [i]; |
| 982 | const bool deprecated = ( command -> deprecationYear > 0 ); |
| 983 | if (! actionIsToBeIncluded (command, deprecated, includeSaveAPI, includeQueryAPI, includeModifyAPI, |
| 984 | includeToAPI, includePlayAPI, includeDrawAPI, includeHelpAPI, includeWindowAPI)) continue; |
| 985 | MelderInfo_writeLine (U"\n/* Action command \"", command -> title.get(), U"\"", |
| 986 | deprecated ? U", deprecated " : U"", deprecated ? Melder_integer (command -> deprecationYear) : U"", |
| 987 | U" */"); |
| 988 | conststring32 returnType = getReturnType (command); |
| 989 | MelderInfo_writeLine (returnType, U" Praat", str32chr (command -> nameOfCallback, U'_'), U" ("); |
| 990 | const bool isDirect = ! str32str (command -> title.get(), U"..."); |
| 991 | if (isDirect) { |
| 992 | } else { |
| 993 | command -> callback (nullptr, -1, nullptr, nullptr, nullptr, nullptr, false, nullptr, nullptr); |
| 994 | } |
| 995 | if (actionHasFileNameArgument (command)) |
| 996 | MelderInfo_writeLine (U"\tconst char *fileName"); |
| 997 | MelderInfo_write (U")"); |
| 998 | if (isInHeaderFile) { |
| 999 | MelderInfo_writeLine (U";"); |
| 1000 | } else { |
| 1001 | MelderInfo_writeLine (U" {"); |
| 1002 | MelderInfo_writeLine (U"}"); |
| 1003 | } |
| 1004 | numberOfApiActions += 1; |
| 1005 | } |
| 1006 | } catch (MelderError) { |
| 1007 | Melder_throw (U"Action not written to C library: ", i, |
| 1008 | U": ", theActions.at [i] -> class1 ? theActions.at [i] -> class1 -> className : U"?? class ??", |
| 1009 | U": ", theActions.at [i] -> title.get() |
| 1010 | ); |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | /* End of file praat_actions.cpp */ |