praat_actions.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /* praat_actions.cpp
  2. *
  3. * Copyright (C) 1992-2018 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. #include "praatP.h"
  19. #include "praat_script.h"
  20. #include "praat_version.h"
  21. #include "../kar/longchar.h"
  22. #include "machine.h"
  23. #include "GuiP.h"
  24. #define BUTTON_LEFT -240
  25. #define BUTTON_RIGHT -5
  26. static OrderedOf <structPraat_Command> theActions;
  27. void praat_actions_exit_optimizeByLeaking () { theActions. _ownItems = false; }
  28. static GuiMenu praat_writeMenu;
  29. static GuiMenuItem praat_writeMenuSeparator;
  30. static GuiForm praat_form;
  31. static bool actionsInvisible = false;
  32. static void fixSelectionSpecification (ClassInfo *class1, integer *n1, ClassInfo *class2, integer *n2, ClassInfo *class3, integer *n3) {
  33. /*
  34. * Function:
  35. * sort the specification pairs *class(i), *n(i) according to class name, with null classes at the end.
  36. * Postconditions:
  37. * if (*class2) !! *class1;
  38. * if (*class3) !! *class2;
  39. * (*class1) -> className <= (*class2) -> className <= (*class3) -> className;
  40. * Usage:
  41. * Called by praat_addAction () and praat_removeAction ().
  42. */
  43. /* Fix unusual input bubblewise. */
  44. if (! *class1 && *class2) { *class1 = *class2; *n1 = *n2; *class2 = nullptr; *n2 = 0; }
  45. if (! *class2 && *class3) { *class2 = *class3; *n2 = *n3; *class3 = nullptr; *n3 = 0;
  46. if (! *class1 && *class2) { *class1 = *class2; *n1 = *n2; *class2 = nullptr; *n2 = 0; } }
  47. /* Now: if *class3, then *class2, and if *class2, then *class1.
  48. * Bubble-sort the input by class name.
  49. */
  50. if (*class2 && str32cmp ((*class1) -> className, (*class2) -> className) > 0) {
  51. ClassInfo helpClass1 = *class1; *class1 = *class2; *class2 = helpClass1;
  52. integer helpN1 = *n1; *n1 = *n2; *n2 = helpN1;
  53. }
  54. if (*class3 && str32cmp ((*class2) -> className, (*class3) -> className) > 0) {
  55. ClassInfo helpClass2 = *class2; *class2 = *class3; *class3 = helpClass2;
  56. integer helpN2 = *n2; *n2 = *n3; *n3 = helpN2;
  57. if (str32cmp ((*class1) -> className, (*class2) -> className) > 0) {
  58. ClassInfo helpClass1 = *class1; *class1 = *class2; *class2 = helpClass1;
  59. integer helpN1 = *n1; *n1 = *n2; *n2 = helpN1;
  60. }
  61. }
  62. }
  63. static integer lookUpMatchingAction (ClassInfo class1, ClassInfo class2, ClassInfo class3, ClassInfo class4, conststring32 title) {
  64. /*
  65. * An action command is fully specified by its environment (the selected classes) and its title.
  66. * Precondition:
  67. * class1, class2, and class3 must be in sorted order.
  68. */
  69. for (integer i = 1; i <= theActions.size; i ++) {
  70. Praat_Command action = theActions.at [i];
  71. if (class1 == action -> class1 && class2 == action -> class2 &&
  72. class3 == action -> class3 && class4 == action -> class4 &&
  73. title && action -> title && str32equ (action -> title.get(), title)) return i;
  74. }
  75. return 0; // not found
  76. }
  77. void praat_addAction1_ (ClassInfo class1, integer n1,
  78. conststring32 title, conststring32 after, uint32 flags, UiCallback callback, conststring32 nameOfCallback)
  79. { praat_addAction4_ (class1, n1, nullptr, 0, nullptr, 0, nullptr, 0, title, after, flags, callback, nameOfCallback); }
  80. void praat_addAction2_ (ClassInfo class1, integer n1, ClassInfo class2, integer n2,
  81. conststring32 title, conststring32 after, uint32 flags, UiCallback callback, conststring32 nameOfCallback)
  82. { praat_addAction4_ (class1, n1, class2, n2, nullptr, 0, nullptr, 0, title, after, flags, callback, nameOfCallback); }
  83. void praat_addAction3_ (ClassInfo class1, integer n1, ClassInfo class2, integer n2, ClassInfo class3, integer n3,
  84. conststring32 title, conststring32 after, uint32 flags, UiCallback callback, conststring32 nameOfCallback)
  85. { praat_addAction4_ (class1, n1, class2, n2, class3, n3, nullptr, 0, title, after, flags, callback, nameOfCallback); }
  86. void praat_addAction4_ (ClassInfo class1, integer n1, ClassInfo class2, integer n2, ClassInfo class3, integer n3, ClassInfo class4, integer n4,
  87. conststring32 title, conststring32 after, uint32 flags, UiCallback callback, conststring32 nameOfCallback)
  88. {
  89. try {
  90. int depth = flags, key = 0;
  91. bool unhidable = false, hidden = false, attractive = false;
  92. uint32 guiFlags = 0;
  93. if (flags > 7) {
  94. depth = ((flags & praat_DEPTH_7) >> 16);
  95. unhidable = (flags & praat_UNHIDABLE) != 0;
  96. hidden = (flags & praat_HIDDEN) != 0 && ! unhidable;
  97. key = flags & 0x000000FF;
  98. guiFlags = key ? flags & (0x000000FF | GuiMenu_SHIFT | GuiMenu_BUTTON_STATE_MASK) : flags & GuiMenu_BUTTON_STATE_MASK;
  99. attractive = (guiFlags & praat_ATTRACTIVE) != 0;
  100. }
  101. fixSelectionSpecification (& class1, & n1, & class2, & n2, & class3, & n3);
  102. if (callback && ! title)
  103. Melder_throw (U"An action command with callback has no title. Classes: ",
  104. class1 ? class1 -> className : U"", U" ",
  105. class2 ? class2 -> className : U"", U" ",
  106. class3 ? class3 -> className : U"", U" ",
  107. class4 ? class4 -> className : U"", U".");
  108. if (! class1)
  109. Melder_throw (U"The action command \"", title, U"\" has no first class.");
  110. /*
  111. * Determine the position of the new command.
  112. */
  113. integer position;
  114. if (after && after [0] != U'*') { // search for existing command with same selection
  115. integer found = lookUpMatchingAction (class1, class2, class3, class4, after);
  116. if (found == 0)
  117. Melder_throw (U"The action command \"", title, U"\" cannot be put after \"", after, U"\",\n"
  118. U"because the latter command does not exist.");
  119. position = found + 1; // after 'after'
  120. } else {
  121. position = theActions.size + 1; // at end
  122. }
  123. /*
  124. * Make new command.
  125. */
  126. autoPraat_Command action = Thing_new (Praat_Command);
  127. action -> class1 = class1;
  128. action -> n1 = n1;
  129. action -> class2 = class2;
  130. action -> n2 = n2;
  131. action -> class3 = class3;
  132. action -> n3 = n3;
  133. action -> class4 = class4;
  134. action -> n4 = n4;
  135. action -> title = Melder_dup_f (title);
  136. action -> depth = depth;
  137. action -> callback = callback; // null for a separator
  138. action -> nameOfCallback = nameOfCallback;
  139. action -> button = nullptr;
  140. action -> script = autostring32();
  141. action -> hidden = hidden;
  142. action -> unhidable = unhidable;
  143. action -> attractive = attractive;
  144. /*
  145. * Insert new command.
  146. */
  147. theActions. addItemAtPosition_move (action.move(), position);
  148. } catch (MelderError) {
  149. Melder_flushError ();
  150. }
  151. }
  152. static void deleteDynamicMenu () {
  153. if (praatP.phase != praat_HANDLING_EVENTS) return;
  154. if (actionsInvisible) return;
  155. static integer numberOfDeletions;
  156. trace (U"deletion #", ++ numberOfDeletions);
  157. for (integer i = 1; i <= theActions.size; i ++) {
  158. Praat_Command action = theActions.at [i];
  159. if (action -> button) {
  160. trace (U"trying to destroy action ", i, U" of ", theActions.size, U": ", action -> title.get());
  161. #if gtk || cocoa
  162. if (action -> button -> d_parent == praat_form) {
  163. trace (U"destroy a label or a push button or a cascade button");
  164. GuiObject_destroy (action -> button -> d_widget);
  165. } else if (praat_writeMenu && action -> button -> d_parent == praat_writeMenu) {
  166. trace (U"destroying Save menu item");
  167. GuiObject_destroy (action -> button -> d_widget);
  168. }
  169. #elif motif
  170. if (action -> button -> classInfo == classGuiButton && action -> button -> d_widget -> subMenuId) { // a cascade button (not a direct child of the form)?
  171. trace (U"destroy the xm menu bar; this also destroys the xm button and the xm menu");
  172. GuiObject_destroy (action -> button -> d_widget -> parent); // the Motif parent, i.e. not d_parent -> d_widget !
  173. } else if (action -> button -> d_parent == praat_form) {
  174. trace (U"destroy a label or a push button");
  175. GuiObject_destroy (action -> button -> d_widget);
  176. }
  177. #endif
  178. action -> button = nullptr; // undangle
  179. }
  180. }
  181. if (praat_writeMenu) {
  182. #if gtk || cocoa
  183. if (praat_writeMenuSeparator) {
  184. trace (U"destroy the Save menu separator");
  185. GuiObject_destroy (praat_writeMenuSeparator -> d_widget);
  186. }
  187. //praat_writeMenu -> f_empty ();
  188. #elif motif
  189. GuiObject_destroy (praat_writeMenu -> d_xmMenuTitle);
  190. GuiObject_destroy (praat_writeMenu -> d_widget);
  191. praat_writeMenu = GuiMenu_createInWindow (praatP.menuBar, U"Save", 0);
  192. #endif
  193. praat_writeMenuSeparator = nullptr; // undangle
  194. }
  195. actionsInvisible = true;
  196. }
  197. static void updateDynamicMenu () {
  198. if (praatP.phase != praat_HANDLING_EVENTS) return;
  199. praat_sortActions ();
  200. deleteDynamicMenu ();
  201. praat_show ();
  202. }
  203. void praat_addActionScript (conststring32 className1, integer n1, conststring32 className2, integer n2, conststring32 className3, integer n3,
  204. conststring32 title, conststring32 after, integer depth, conststring32 script)
  205. {
  206. try {
  207. ClassInfo class1 = nullptr, class2 = nullptr, class3 = nullptr;
  208. Melder_assert (className1 && className2 && className3 && title && after && script);
  209. if (className1 [0] != U'\0')
  210. class1 = Thing_classFromClassName (className1, nullptr);
  211. if (className2 [0] != U'\0')
  212. class2 = Thing_classFromClassName (className2, nullptr);
  213. if (className3 [0] != U'\0')
  214. class3 = Thing_classFromClassName (className3, nullptr);
  215. fixSelectionSpecification (& class1, & n1, & class2, & n2, & class3, & n3);
  216. if (script [0] != U'\0' && title [0] == U'\0')
  217. Melder_throw (U"Command with callback has no title. Classes: ", className1, U" ", className2, U" ", className3, U".");
  218. if (className1 [0] == U'\0')
  219. Melder_throw (U"Command \"", title, U"\" has no first class.");
  220. /*
  221. * If the button already exists, remove it.
  222. */
  223. {// scope
  224. integer found = lookUpMatchingAction (class1, class2, class3, nullptr, title);
  225. if (found)
  226. theActions. removeItem (found);
  227. }
  228. /*
  229. * Determine the position of the new command.
  230. */
  231. integer position;
  232. if (after [0] != U'\0') { // search for existing command with same selection
  233. integer found = lookUpMatchingAction (class1, class2, class3, nullptr, after);
  234. if (found) {
  235. position = found + 1; // after 'after'
  236. } else {
  237. position = theActions.size + 1; // at end
  238. }
  239. } else {
  240. position = theActions.size + 1; // at end
  241. }
  242. /*
  243. * Create new command.
  244. */
  245. autoPraat_Command action = Thing_new (Praat_Command);
  246. action -> class1 = class1;
  247. action -> n1 = n1;
  248. action -> class2 = class2;
  249. action -> n2 = n2;
  250. action -> class3 = class3;
  251. action -> n3 = n3;
  252. action -> title = title [0] != U'\0' ? Melder_dup_f (title) : autostring32(); // allow old-fashioned untitled separators
  253. action -> depth = depth;
  254. action -> callback = script [0] != U'\0' ? DO_RunTheScriptFromAnyAddedMenuCommand : nullptr; // null for a separator
  255. action -> button = nullptr;
  256. if (script [0] == U'\0') {
  257. action -> script = autostring32();
  258. } else {
  259. structMelderFile file { };
  260. Melder_relativePathToFile (script, & file);
  261. action -> script = Melder_dup_f (Melder_fileToPath (& file));
  262. }
  263. action -> after = after [0] != U'\0' ? Melder_dup_f (after) : autostring32();
  264. action -> phase = praatP.phase;
  265. if (praatP.phase >= praat_READING_BUTTONS) {
  266. static integer uniqueID = 0;
  267. action -> uniqueID = ++ uniqueID;
  268. }
  269. /*
  270. * Insert new command.
  271. */
  272. theActions. addItemAtPosition_move (action.move(), position);
  273. updateDynamicMenu ();
  274. } catch (MelderError) {
  275. Melder_throw (U"Praat: script action not added.");
  276. }
  277. }
  278. void praat_removeAction (ClassInfo class1, ClassInfo class2, ClassInfo class3, conststring32 title) {
  279. try {
  280. integer n1, n2, n3;
  281. fixSelectionSpecification (& class1, & n1, & class2, & n2, & class3, & n3);
  282. integer found = lookUpMatchingAction (class1, class2, class3, nullptr, title);
  283. if (! found) {
  284. Melder_throw (U"Action command \"", class1 -> className,
  285. class2 ? U" & ": U"", class2 -> className,
  286. class3 ? U" & ": U"", class3 -> className,
  287. U": ", title, U"\" not found.");
  288. }
  289. theActions. removeItem (found);
  290. } catch (MelderError) {
  291. Melder_throw (U"Praat: action not removed.");
  292. }
  293. }
  294. void praat_removeAction_classNames (conststring32 className1, conststring32 className2,
  295. conststring32 className3, conststring32 title)
  296. {
  297. try {
  298. ClassInfo class1 = nullptr, class2 = nullptr, class3 = nullptr;
  299. Melder_assert (className1 && className2 && className3 && title);
  300. if (className1 [0] != U'\0')
  301. class1 = Thing_classFromClassName (className1, nullptr);
  302. if (className2 [0] != U'\0')
  303. class2 = Thing_classFromClassName (className2, nullptr);
  304. if (className3 [0] != U'\0')
  305. class3 = Thing_classFromClassName (className3, nullptr);
  306. praat_removeAction (class1, class2, class3, title);
  307. updateDynamicMenu ();
  308. } catch (MelderError) {
  309. Melder_throw (U"Praat: action not removed.");
  310. }
  311. }
  312. void praat_hideAction (ClassInfo class1, ClassInfo class2, ClassInfo class3, conststring32 title) {
  313. try {
  314. integer n1, n2, n3;
  315. fixSelectionSpecification (& class1, & n1, & class2, & n2, & class3, & n3);
  316. integer found = lookUpMatchingAction (class1, class2, class3, nullptr, title);
  317. if (! found) {
  318. Melder_throw (U"Praat: action command \"", class1 ? class1 -> className : nullptr,
  319. class2 ? U" & ": nullptr, class2 ? class2 -> className : nullptr,
  320. class3 ? U" & ": nullptr, class3 ? class3 -> className : nullptr,
  321. U": ", title, U"\" not found.");
  322. }
  323. Praat_Command action = theActions.at [found];
  324. if (! action -> hidden) {
  325. action -> hidden = true;
  326. if (praatP.phase >= praat_READING_BUTTONS) action -> toggled = ! action -> toggled;
  327. updateDynamicMenu ();
  328. }
  329. } catch (MelderError) {
  330. Melder_throw (U"Praat: action not hidden.");
  331. }
  332. }
  333. void praat_hideAction_classNames (conststring32 className1, conststring32 className2,
  334. conststring32 className3, conststring32 title)
  335. {
  336. try {
  337. ClassInfo class1 = nullptr, class2 = nullptr, class3 = nullptr;
  338. Melder_assert (className1 && className2 && className3 && title);
  339. if (className1 [0] != U'\0')
  340. class1 = Thing_classFromClassName (className1, nullptr);
  341. if (className2 [0] != U'\0')
  342. class2 = Thing_classFromClassName (className2, nullptr);
  343. if (className3 [0] != U'\0')
  344. class3 = Thing_classFromClassName (className3, nullptr);
  345. praat_hideAction (class1, class2, class3, title);
  346. } catch (MelderError) {
  347. Melder_throw (U"Praat: action not hidden.");
  348. }
  349. }
  350. void praat_showAction (ClassInfo class1, ClassInfo class2, ClassInfo class3, conststring32 title) {
  351. try {
  352. integer n1, n2, n3;
  353. fixSelectionSpecification (& class1, & n1, & class2, & n2, & class3, & n3);
  354. integer found = lookUpMatchingAction (class1, class2, class3, nullptr, title);
  355. if (! found) {
  356. Melder_throw (U"Action command \"", class1 ? class1 -> className : nullptr,
  357. class2 ? U" & ": nullptr, class2 ? class2 -> className : nullptr,
  358. class3 ? U" & ": nullptr, class3 ? class3 -> className : nullptr,
  359. U": ", title, U"\" not found.");
  360. }
  361. Praat_Command action = theActions.at [found];
  362. if (action -> hidden) {
  363. action -> hidden = false;
  364. if (praatP.phase >= praat_READING_BUTTONS) action -> toggled = ! action -> toggled;
  365. updateDynamicMenu ();
  366. }
  367. } catch (MelderError) {
  368. Melder_throw (U"Praat: action not shown.");
  369. }
  370. }
  371. void praat_showAction_classNames (conststring32 className1, conststring32 className2,
  372. conststring32 className3, conststring32 title)
  373. {
  374. try {
  375. ClassInfo class1 = nullptr, class2 = nullptr, class3 = nullptr;
  376. Melder_assert (className1 && className2 && className3 && title);
  377. if (className1 [0] != U'\0')
  378. class1 = Thing_classFromClassName (className1, nullptr);
  379. if (className2 [0] != U'\0')
  380. class2 = Thing_classFromClassName (className2, nullptr);
  381. if (className3 [0] != U'\0')
  382. class3 = Thing_classFromClassName (className3, nullptr);
  383. praat_showAction (class1, class2, class3, title);
  384. } catch (MelderError) {
  385. Melder_throw (U"Praat: action not shown.");
  386. }
  387. }
  388. static int compareActions (const void *void_me, const void *void_thee) {
  389. Praat_Command me = * (Praat_Command *) void_me, thee = * (Praat_Command *) void_thee;
  390. int compare;
  391. compare = str32cmp (my class1 -> className, thy class1 -> className);
  392. if (compare) return my class1 == classDaata ? -1 : thy class1 == classDaata ? 1 : compare;
  393. if (my class2) {
  394. if (! thy class2) return 1;
  395. compare = str32cmp (my class2 -> className, thy class2 -> className);
  396. if (compare) return compare;
  397. } else if (thy class2) return -1;
  398. if (my class3) {
  399. if (! thy class3) return 1;
  400. compare = str32cmp (my class3 -> className, thy class3 -> className);
  401. if (compare) return compare;
  402. } else if (thy class3) return -1;
  403. if (my sortingTail < thy sortingTail) return -1;
  404. return 1;
  405. }
  406. void praat_sortActions () {
  407. for (integer i = 1; i <= theActions.size; i ++) {
  408. Praat_Command action = theActions.at [i];
  409. action -> sortingTail = i;
  410. }
  411. qsort (& theActions.at [1], theActions.size, sizeof (Praat_Command), compareActions);
  412. }
  413. static conststring32 numberString (int number) {
  414. return number == 1 ? U"one" : number == 2 ? U"two" : number == 3 ? U"three" : U"any number of";
  415. }
  416. static conststring32 classString (ClassInfo klas) {
  417. return klas == classDaata ? U"" : klas -> className;
  418. }
  419. static conststring32 objectString (int number) {
  420. return number == 1 ? U"object" : U"objects";
  421. }
  422. static bool allowExecutionHook (void *closure) {
  423. UiCallback callback = (UiCallback) closure;
  424. Melder_assert (sizeof (callback) == sizeof (void *));
  425. integer numberOfMatchingCallbacks = 0, firstMatchingCallback = 0;
  426. for (integer i = 1; i <= theActions.size; i ++) {
  427. Praat_Command me = theActions.at [i];
  428. if (my callback == callback) {
  429. integer sel1, sel2 = 0, sel3 = 0, sel4 = 0;
  430. if (! my class1) Melder_throw (U"No class1???");
  431. numberOfMatchingCallbacks += 1;
  432. if (! firstMatchingCallback) firstMatchingCallback = i;
  433. sel1 = my class1 == classDaata ? theCurrentPraatObjects -> totalSelection : praat_numberOfSelected (my class1);
  434. if (sel1 == 0) continue;
  435. if (my class2 && (sel2 = praat_numberOfSelected (my class2)) == 0) continue;
  436. if (my class3 && (sel3 = praat_numberOfSelected (my class3)) == 0) continue;
  437. if (my class4 && (sel4 = praat_numberOfSelected (my class4)) == 0) continue;
  438. if (sel1 + sel2 + sel3 + sel4 != theCurrentPraatObjects -> totalSelection) continue;
  439. if ((my n1 && sel1 != my n1) || (my n2 && sel2 != my n2) || (my n3 && sel3 != my n3) || (my n4 && sel4 != my n4)) continue;
  440. return true; // found a matching action
  441. }
  442. }
  443. if (numberOfMatchingCallbacks == 1) {
  444. Praat_Command me = theActions.at [firstMatchingCallback];
  445. Melder_appendError (U"Selection changed! It should be:");
  446. if (my class1) Melder_appendError (U" ", numberString (my n1), U" ", classString (my class1), U" ", objectString (my n1));
  447. if (my class2) Melder_appendError (U" ", numberString (my n2), U" ", classString (my class2), U" ", objectString (my n2));
  448. if (my class3) Melder_appendError (U" ", numberString (my n3), U" ", classString (my class3), U" ", objectString (my n3));
  449. if (my class4) Melder_appendError (U" ", numberString (my n4), U" ", classString (my class4), U" ", objectString (my n4));
  450. throw MelderError ();
  451. } else {
  452. Melder_throw (U"Selection changed!");
  453. }
  454. return false;
  455. }
  456. static void do_menu (Praat_Command me, bool modified) {
  457. if (my callback == DO_RunTheScriptFromAnyAddedMenuCommand) {
  458. UiHistory_write (U"\nrunScript: ");
  459. try {
  460. DO_RunTheScriptFromAnyAddedMenuCommand (nullptr, 0, nullptr, my script.get(), nullptr, nullptr, false, nullptr);
  461. } catch (MelderError) {
  462. Melder_flushError (U"Command \"", my title.get(), U"\" not executed.");
  463. }
  464. praat_updateSelection (); return;
  465. } else {
  466. if (my title && ! str32str (my title.get(), U"...")) {
  467. UiHistory_write (U"\n");
  468. UiHistory_write (my title.get());
  469. }
  470. Ui_setAllowExecutionHook (allowExecutionHook, (void *) my callback); // BUG: one shouldn't assign a function pointer to a void pointer
  471. try {
  472. my callback (nullptr, 0, nullptr, nullptr, nullptr, my title.get(), modified, nullptr);
  473. } catch (MelderError) {
  474. Melder_flushError (U"Command \"", my title.get(), U"\" not executed.");
  475. }
  476. Ui_setAllowExecutionHook (nullptr, nullptr);
  477. praat_updateSelection (); return;
  478. }
  479. }
  480. static void cb_menu (Praat_Command me, GuiMenuItemEvent event) {
  481. bool modified = event -> shiftKeyPressed || event -> commandKeyPressed || event -> optionKeyPressed || event -> extraControlKeyPressed;
  482. do_menu (me, modified);
  483. }
  484. static void gui_button_cb_menu (Praat_Command me, GuiButtonEvent event) {
  485. do_menu (me, event -> shiftKeyPressed | event -> commandKeyPressed | event -> optionKeyPressed | event -> extraControlKeyPressed);
  486. }
  487. void praat_actions_show () {
  488. #if defined (macintosh)
  489. const int BUTTON_VSPACING = 8;
  490. #else
  491. const int BUTTON_VSPACING = 5;
  492. #endif
  493. /*
  494. * The selection has changed;
  495. * kill the dynamic menu and the write menu.
  496. */
  497. if (! theCurrentPraatApplication -> batch) {
  498. deleteDynamicMenu ();
  499. if (! Melder_backgrounding) {
  500. GuiThing_setSensitive (praat_writeMenu, false);
  501. if (praat_writeMenuSeparator) GuiThing_hide (praat_writeMenuSeparator);
  502. }
  503. /* Determine the visibility and sensitivity of all the actions.
  504. */
  505. if (theCurrentPraatObjects -> totalSelection != 0 && ! Melder_backgrounding)
  506. GuiThing_setSensitive (praat_writeMenu, true);
  507. }
  508. for (integer i = 1; i <= theActions.size; i ++) {
  509. Praat_Command action = theActions.at [i];
  510. integer sel1 = 0, sel2 = 0, sel3 = 0, sel4 = 0;
  511. integer n1 = action -> n1, n2 = action -> n2, n3 = action -> n3, n4 = action -> n4;
  512. /* Clean up from previous selection. */
  513. action -> visible = false;
  514. action -> executable = false;
  515. /* Match the actually selected classes with the selection required for this visibility. */
  516. if (! action -> class1) continue; // at least one class selected
  517. sel1 = action -> class1 == classDaata ? theCurrentPraatObjects -> totalSelection : praat_numberOfSelected (action -> class1);
  518. if (sel1 == 0) continue;
  519. if (action -> class2 && (sel2 = praat_numberOfSelected (action -> class2)) == 0) continue;
  520. if (action -> class3 && (sel3 = praat_numberOfSelected (action -> class3)) == 0) continue;
  521. if (action -> class4 && (sel4 = praat_numberOfSelected (action -> class4)) == 0) continue;
  522. if (sel1 + sel2 + sel3 + sel4 != theCurrentPraatObjects -> totalSelection) continue; // other classes selected? Do not show
  523. action -> visible = ! action -> hidden;
  524. /* Match the actually selected objects with the selection required for this action. */
  525. if (! action -> callback) continue; // separators are not executable
  526. if ((n1 && sel1 != n1) || (n2 && sel2 != n2) || (n3 && sel3 != n3) || (n4 && sel4 != n4)) continue;
  527. action -> executable = true;
  528. }
  529. /* Create a new column of buttons in the dynamic menu. */
  530. if (! theCurrentPraatApplication -> batch && ! Melder_backgrounding) {
  531. actionsInvisible = false;
  532. GuiMenu currentSubmenu1 = nullptr, currentSubmenu2 = nullptr;
  533. bool writeMenuGoingToSeparate = false;
  534. int y = Machine_getMenuBarHeight () + 10;
  535. for (integer i = 1; i <= theActions.size; i ++) { // add buttons or make existing buttons sensitive (executable)
  536. Praat_Command me = theActions.at [i];
  537. if (my depth == 0) currentSubmenu1 = nullptr, currentSubmenu2 = nullptr; // prevent attachment of later deep actions to earlier submenus after removal of label
  538. if (my depth == 1) currentSubmenu2 = nullptr; // prevent attachment of later deep actions to earlier submenus after removal of label
  539. if (! my visible) continue;
  540. if (my callback) {
  541. /* Apparently a true command: create a button in the dynamic menu.
  542. * If it is a subcommand (depth > 0), put it in the current submenu,
  543. * but only if this exists (umbrella against stray submenu specifications).
  544. */
  545. GuiMenu parentMenu = my depth > 1 && currentSubmenu2 ? currentSubmenu2 : my depth > 0 && currentSubmenu1 ? currentSubmenu1 : nullptr;
  546. if (str32nequ (my title.get(), U"Save ", 5) || str32nequ (my title.get(), U"Write ", 6) || str32nequ (my title.get(), U"Append to ", 10)) {
  547. parentMenu = praat_writeMenu;
  548. if (! praat_writeMenuSeparator) {
  549. if (writeMenuGoingToSeparate)
  550. praat_writeMenuSeparator = GuiMenu_addSeparator (parentMenu);
  551. else if (str32equ (my title.get(), U"Save as binary file..."))
  552. writeMenuGoingToSeparate = true;
  553. }
  554. }
  555. if (parentMenu) {
  556. my button = GuiMenu_addItem (parentMenu, my title.get(),
  557. ( my executable ? 0 : GuiMenu_INSENSITIVE ),
  558. cb_menu, me);
  559. } else {
  560. my button = GuiButton_createShown (praat_form,
  561. BUTTON_LEFT, BUTTON_RIGHT, y, y + Gui_PUSHBUTTON_HEIGHT,
  562. my title.get(), gui_button_cb_menu,
  563. me,
  564. ( my executable ? 0 : GuiButton_INSENSITIVE ) | ( my attractive ? GuiButton_ATTRACTIVE : 0 ));
  565. y += Gui_PUSHBUTTON_HEIGHT + BUTTON_VSPACING;
  566. #if gtk
  567. /* Dit soort onzin zou eigenlijk in GuiButton moeten */
  568. gtk_button_set_alignment (GTK_BUTTON (my button -> d_widget), 0.0f, 0.5f);
  569. #endif
  570. }
  571. } else if (i == theActions.size || theActions.at [i + 1] -> depth == 0) {
  572. /*
  573. * Apparently a labelled separator.
  574. */
  575. my button = GuiLabel_createShown (praat_form, BUTTON_LEFT, BUTTON_RIGHT, y, y + Gui_LABEL_HEIGHT, my title.get(), 0);
  576. y += Gui_LABEL_HEIGHT + BUTTON_VSPACING;
  577. } else if (! my title || my title [0] == U'-') {
  578. /*
  579. * Apparently a separator in a submenu.
  580. */
  581. if (currentSubmenu2 || currentSubmenu1) { // these separators are not shown in a flattened menu
  582. my button = GuiMenu_addSeparator (currentSubmenu2 ? currentSubmenu2 : currentSubmenu1);
  583. GuiThing_show (my button);
  584. }
  585. } else {
  586. /*
  587. * Apparently a submenu.
  588. */
  589. if (my depth == 0 || ! currentSubmenu1) {
  590. currentSubmenu1 = GuiMenu_createInForm (praat_form,
  591. BUTTON_LEFT, BUTTON_RIGHT, y, y + Gui_PUSHBUTTON_HEIGHT,
  592. my title.get(), 0);
  593. y += Gui_PUSHBUTTON_HEIGHT + BUTTON_VSPACING;
  594. my button = currentSubmenu1 -> d_cascadeButton.get();
  595. } else {
  596. currentSubmenu2 = GuiMenu_createInMenu (currentSubmenu1, my title.get(), 0);
  597. my button = currentSubmenu2 -> d_menuItem.get();
  598. }
  599. GuiThing_show (my button);
  600. }
  601. }
  602. }
  603. }
  604. void praat_actions_createWriteMenu (GuiWindow window) {
  605. if (theCurrentPraatApplication -> batch) return;
  606. praat_writeMenu = GuiMenu_createInWindow (window, U"Save", GuiMenu_INSENSITIVE);
  607. #if gtk
  608. GuiMenu_addSeparator (praat_writeMenu);
  609. #endif
  610. }
  611. void praat_actions_init () {
  612. }
  613. void praat_actions_createDynamicMenu (GuiWindow window) {
  614. if (theCurrentPraatApplication -> batch) return;
  615. praat_form = window;
  616. }
  617. void praat_saveAddedActions (MelderString *buffer) {
  618. integer maxID = 0;
  619. for (integer iaction = 1; iaction <= theActions.size; iaction ++) {
  620. Praat_Command action = theActions.at [iaction];
  621. if (action -> uniqueID > maxID)
  622. maxID = action -> uniqueID;
  623. }
  624. for (integer ident = 1; ident <= maxID; ident ++)
  625. for (integer iaction = 1; iaction <= theActions.size; iaction ++) {
  626. Praat_Command me = theActions.at [iaction];
  627. if (my uniqueID == ident && ! my hidden && my title) {
  628. MelderString_append (buffer, U"Add action command...",
  629. U" ", my class1 -> className, U" ", my n1,
  630. U" ", ( my class2 ? my class2 -> className : U"\"\"" ), U" ", my n2,
  631. U" ", ( my class3 ? my class3 -> className : U"\"\"" ), U" ", my n3,
  632. U" \"", my title.get(), U"\" \"", ( my after ? my after.get() : U"" ), U"\" ", my depth);
  633. MelderString_append (buffer, U" ", my script ? my script.get() : U"", U"\n");
  634. break;
  635. }
  636. }
  637. }
  638. void praat_saveToggledActions (MelderString *buffer) {
  639. for (integer iaction = 1; iaction <= theActions.size; iaction ++) {
  640. Praat_Command me = theActions.at [iaction];
  641. if (my toggled && my title && ! my uniqueID && ! my script) {
  642. MelderString_append (buffer, ( my hidden ? U"Hide" : U"Show" ), U" action command...",
  643. U" ", my class1 -> className,
  644. U" ", ( my class2 ? my class2 -> className : U"\"\"" ),
  645. U" ", ( my class3 ? my class3 -> className : U"\"\"" ),
  646. U" ", my title.get(), U"\n");
  647. }
  648. }
  649. }
  650. int praat_doAction (conststring32 command, conststring32 arguments, Interpreter interpreter) {
  651. integer i = 1;
  652. while (i <= theActions.size && (! theActions.at [i] -> executable || str32cmp (theActions.at [i] -> title.get(), command))) i ++;
  653. if (i > theActions.size) return 0; // not found
  654. theActions.at [i] -> callback (nullptr, 0, nullptr, arguments, interpreter, command, false, nullptr);
  655. return 1;
  656. }
  657. int praat_doAction (conststring32 command, integer narg, Stackel args, Interpreter interpreter) {
  658. integer i = 1;
  659. while (i <= theActions.size && (! theActions.at [i] -> executable || str32cmp (theActions.at [i] -> title.get(), command))) i ++;
  660. if (i > theActions.size) return 0; // not found
  661. theActions.at [i] -> callback (nullptr, narg, args, nullptr, interpreter, command, false, nullptr);
  662. return 1;
  663. }
  664. integer praat_getNumberOfActions () { return theActions.size; }
  665. Praat_Command praat_getAction (integer i)
  666. { return i < 0 || i > theActions.size ? nullptr : theActions.at [i]; }
  667. void praat_background () {
  668. if (Melder_batch) return;
  669. if (Melder_backgrounding) return;
  670. deleteDynamicMenu ();
  671. praat_list_background ();
  672. Melder_backgrounding = true;
  673. if (! praatP.dontUsePictureWindow) praat_picture_background ();
  674. }
  675. void praat_foreground () {
  676. if (Melder_batch) return;
  677. if (! Melder_backgrounding) return;
  678. Melder_backgrounding = false;
  679. praat_list_foreground ();
  680. praat_show ();
  681. if (! praatP.dontUsePictureWindow) praat_picture_foreground ();
  682. }
  683. static bool actionIsToBeIncluded (Praat_Command command, bool deprecated, bool includeSaveAPI,
  684. bool includeQueryAPI, bool includeModifyAPI, bool includeToAPI,
  685. bool includePlayAPI, bool includeDrawAPI, bool includeHelpAPI, bool includeWindowAPI)
  686. {
  687. bool obsolete = ( deprecated && (command -> deprecationYear < PRAAT_YEAR - 10 || command -> deprecationYear < 2017) );
  688. bool hiddenByDefault = ( command -> hidden != command -> toggled );
  689. bool explicitlyHidden = hiddenByDefault && ! deprecated;
  690. bool hidden = explicitlyHidden || ! command -> callback || command -> noApi || obsolete ||
  691. (! includeWindowAPI && Melder_nequ (command -> nameOfCallback, U"WINDOW_", 7)) ||
  692. (! includeHelpAPI && Melder_nequ (command -> nameOfCallback, U"HELP_", 5)) ||
  693. (! includeDrawAPI && Melder_nequ (command -> nameOfCallback, U"GRAPHICS_", 9)) ||
  694. (! includePlayAPI && Melder_nequ (command -> nameOfCallback, U"PLAY_", 5)) ||
  695. (! includeToAPI && Melder_nequ (command -> nameOfCallback, U"CONVERT_", 8)) ||
  696. (! includeModifyAPI && Melder_nequ (command -> nameOfCallback, U"MODIFY_", 7)) ||
  697. (! includeQueryAPI && Melder_nequ (command -> nameOfCallback, U"QUERY_", 6)) ||
  698. (! includeSaveAPI && Melder_nequ (command -> nameOfCallback, U"SAVE_", 5));
  699. return command -> forceApi || ! hidden;
  700. }
  701. static bool actionHasFileNameArgument (Praat_Command command) {
  702. bool hasFileNameArgument =
  703. Melder_nequ (command -> nameOfCallback, U"READ1_", 6) ||
  704. Melder_nequ (command -> nameOfCallback, U"SAVE_", 5)
  705. ;
  706. return hasFileNameArgument;
  707. }
  708. static conststring32 getReturnType (Praat_Command command) {
  709. const conststring32 returnType =
  710. Melder_nequ (command -> nameOfCallback, U"NEW1_", 5) ? U"PraatObject" :
  711. Melder_nequ (command -> nameOfCallback, U"READ1_", 6) ? U"PraatObject" :
  712. Melder_nequ (command -> nameOfCallback, U"REAL_", 5) ? U"double" :
  713. Melder_nequ (command -> nameOfCallback, U"INTEGER_", 8) ? U"int64_t" :
  714. Melder_nequ (command -> nameOfCallback, U"STRING_", 7) ? U"char *" :
  715. Melder_nequ (command -> nameOfCallback, U"REPORT_", 7) ? U"char *" :
  716. Melder_nequ (command -> nameOfCallback, U"LIST_", 5) ? U"char *" :
  717. Melder_nequ (command -> nameOfCallback, U"INFO_", 5) ? U"char *" :
  718. Melder_nequ (command -> nameOfCallback, U"HINT_", 5) ? U"char *" :
  719. U"void";
  720. return returnType;
  721. }
  722. void praat_actions_writeC (bool isInHeaderFile, bool includeSaveAPI,
  723. bool includeQueryAPI, bool includeModifyAPI, bool includeToAPI,
  724. bool includePlayAPI, bool includeDrawAPI, bool includeHelpAPI, bool includeWindowAPI)
  725. {
  726. integer numberOfApiActions = 0;
  727. for (integer i = 1; i <= theActions.size; i ++) {
  728. Praat_Command command = theActions.at [i];
  729. bool deprecated = ( command -> deprecationYear > 0 );
  730. if (! actionIsToBeIncluded (command, deprecated, includeSaveAPI, includeQueryAPI, includeModifyAPI,
  731. includeToAPI, includePlayAPI, includeDrawAPI, includeHelpAPI, includeWindowAPI)) continue;
  732. MelderInfo_writeLine (U"\n/* Action command \"", command -> title.get(), U"\"",
  733. deprecated ? U", deprecated " : U"", deprecated ? Melder_integer (command -> deprecationYear) : U"",
  734. U" */");
  735. conststring32 returnType = getReturnType (command);
  736. MelderInfo_writeLine (returnType, U" Praat", str32chr (command -> nameOfCallback, U'_'), U" (");
  737. bool isDirect = ! str32str (command -> title.get(), U"...");
  738. if (isDirect) {
  739. } else {
  740. command -> callback (nullptr, -1, nullptr, nullptr, nullptr, nullptr, false, nullptr);
  741. }
  742. if (actionHasFileNameArgument (command)) {
  743. MelderInfo_writeLine (U"\tconst char *fileName");
  744. }
  745. MelderInfo_write (U")");
  746. if (isInHeaderFile) {
  747. MelderInfo_writeLine (U";");
  748. } else {
  749. MelderInfo_writeLine (U" {");
  750. MelderInfo_writeLine (U"}");
  751. }
  752. numberOfApiActions += 1;
  753. }
  754. }
  755. /* End of file praat_actions.cpp */