DemoEditor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* DemoEditor.cpp
  2. *
  3. * Copyright (C) 2009-2011,2013,2015,2016,2017 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 "DemoEditor.h"
  19. #include "machine.h"
  20. #include "praatP.h"
  21. #include "../kar/UnicodeData.h"
  22. Thing_implement (DemoEditor, Editor, 0);
  23. static DemoEditor theReferenceToTheOnlyDemoEditor;
  24. /***** DemoEditor methods *****/
  25. void structDemoEditor :: v_destroy () noexcept {
  26. Melder_free (praatPicture);
  27. theReferenceToTheOnlyDemoEditor = nullptr;
  28. DemoEditor_Parent :: v_destroy ();
  29. }
  30. void structDemoEditor :: v_info () {
  31. DemoEditor_Parent :: v_info ();
  32. MelderInfo_writeLine (U"Colour: ", Graphics_Colour_name (((PraatPicture) praatPicture) -> colour));
  33. MelderInfo_writeLine (U"Font: ", kGraphics_font_getText ((kGraphics_font) ((PraatPicture) praatPicture) -> font));
  34. MelderInfo_writeLine (U"Font size: ", ((PraatPicture) praatPicture) -> fontSize);
  35. }
  36. void structDemoEditor :: v_goAway () {
  37. if (waitingForInput) {
  38. userWantsToClose = true;
  39. } else {
  40. DemoEditor_Parent :: v_goAway ();
  41. }
  42. }
  43. void structDemoEditor :: v_createMenus () {
  44. DemoEditor_Parent :: v_createMenus ();
  45. }
  46. static void gui_drawingarea_cb_expose (DemoEditor me, GuiDrawingArea_ExposeEvent /* event */) {
  47. if (! my graphics) return; // could be the case in the very beginning
  48. /*
  49. * Erase the background. Don't record this erasure!
  50. */
  51. Graphics_stopRecording (my graphics.get()); // the only place in Praat (the Picture window has a separate Graphics for erasing)?
  52. Graphics_setColour (my graphics.get(), Graphics_WHITE);
  53. Graphics_setWindow (my graphics.get(), 0.0, 1.0, 0.0, 1.0);
  54. Graphics_fillRectangle (my graphics.get(), 0.0, 1.0, 0.0, 1.0);
  55. Graphics_setColour (my graphics.get(), Graphics_BLACK);
  56. Graphics_startRecording (my graphics.get());
  57. Graphics_play (my graphics.get(), my graphics.get());
  58. }
  59. static void gui_drawingarea_cb_click (DemoEditor me, GuiDrawingArea_ClickEvent event) {
  60. if (! my graphics) return; // could be the case in the very beginning
  61. my clicked = true;
  62. my keyPressed = false;
  63. my x = event -> x;
  64. my y = event -> y;
  65. my key = UNICODE_BULLET;
  66. my shiftKeyPressed = event -> shiftKeyPressed;
  67. my commandKeyPressed = event -> commandKeyPressed;
  68. my optionKeyPressed = event -> optionKeyPressed;
  69. my extraControlKeyPressed = event -> extraControlKeyPressed;
  70. }
  71. static void gui_drawingarea_cb_key (DemoEditor me, GuiDrawingArea_KeyEvent event) {
  72. if (! my graphics) return; // could be the case in the very beginning
  73. my clicked = false;
  74. my keyPressed = true;
  75. my x = 0;
  76. my y = 0;
  77. my key = event -> key;
  78. trace (my key);
  79. my shiftKeyPressed = event -> shiftKeyPressed;
  80. my commandKeyPressed = event -> commandKeyPressed;
  81. my optionKeyPressed = event -> optionKeyPressed;
  82. my extraControlKeyPressed = event -> extraControlKeyPressed;
  83. }
  84. static void gui_drawingarea_cb_resize (DemoEditor me, GuiDrawingArea_ResizeEvent event) {
  85. if (! my graphics) return; // could be the case in the very beginning
  86. trace (event -> width, U" ", event -> height);
  87. Graphics_setWsViewport (my graphics.get(), 0.0, event -> width, 0.0, event -> height);
  88. Graphics_setWsWindow (my graphics.get(), 0.0, 100.0, 0.0, 100.0);
  89. //Graphics_setViewport (my graphics.get(), 0.0, 100.0, 0.0, 100.0);
  90. Graphics_updateWs (my graphics.get());
  91. }
  92. void structDemoEditor :: v_createChildren () {
  93. drawingArea = GuiDrawingArea_createShown (our windowForm, 0, 0, 0, 0,
  94. gui_drawingarea_cb_expose, gui_drawingarea_cb_click, gui_drawingarea_cb_key, gui_drawingarea_cb_resize, this, 0);
  95. }
  96. void DemoEditor_init (DemoEditor me) {
  97. Editor_init (me, 0, 0, 1344, 756, U"", nullptr); // 70 percent of the standard 1920x1080 screen
  98. my graphics = Graphics_create_xmdrawingarea (my drawingArea);
  99. Graphics_setColour (my graphics.get(), Graphics_WHITE);
  100. Graphics_setWindow (my graphics.get(), 0.0, 1.0, 0.0, 1.0);
  101. Graphics_fillRectangle (my graphics.get(), 0.0, 1.0, 0.0, 1.0);
  102. Graphics_setColour (my graphics.get(), Graphics_BLACK);
  103. Graphics_startRecording (my graphics.get());
  104. Graphics_setWsViewport (my graphics.get(), 0.0, GuiControl_getWidth (my drawingArea),
  105. 0.0, GuiControl_getHeight (my drawingArea));
  106. Graphics_setWsWindow (my graphics.get(), 0.0, 100.0, 0.0, 100.0);
  107. Graphics_setViewport (my graphics.get(), 0.0, 100.0, 0.0, 100.0);
  108. Graphics_updateWs (my graphics.get());
  109. }
  110. autoDemoEditor DemoEditor_create () {
  111. try {
  112. autoDemoEditor me = Thing_new (DemoEditor);
  113. DemoEditor_init (me.get());
  114. return me;
  115. } catch (MelderError) {
  116. Melder_throw (U"Demo window not created.");
  117. }
  118. }
  119. void Demo_open () {
  120. if (Melder_batch) {
  121. /*
  122. * Batch scripts have to be able to run demos.
  123. */
  124. //Melder_batch = false;
  125. }
  126. if (! theReferenceToTheOnlyDemoEditor) {
  127. autoDemoEditor editor = DemoEditor_create ();
  128. Melder_assert (editor);
  129. //GuiObject_show (editor -> windowForm);
  130. editor -> praatPicture = Melder_calloc_f (structPraatPicture, 1);
  131. theCurrentPraatPicture = (PraatPicture) editor -> praatPicture;
  132. theCurrentPraatPicture -> graphics = editor -> graphics.get();
  133. theCurrentPraatPicture -> font = (int) kGraphics_font::HELVETICA;
  134. theCurrentPraatPicture -> fontSize = 10;
  135. theCurrentPraatPicture -> lineType = Graphics_DRAWN;
  136. theCurrentPraatPicture -> colour = Graphics_BLACK;
  137. theCurrentPraatPicture -> lineWidth = 1.0;
  138. theCurrentPraatPicture -> arrowSize = 1.0;
  139. theCurrentPraatPicture -> speckleSize = 1.0;
  140. theCurrentPraatPicture -> x1NDC = 0.0;
  141. theCurrentPraatPicture -> x2NDC = 100.0;
  142. theCurrentPraatPicture -> y1NDC = 0.0;
  143. theCurrentPraatPicture -> y2NDC = 100.0;
  144. theReferenceToTheOnlyDemoEditor = editor.get();
  145. editor.releaseToUser();
  146. }
  147. if (theReferenceToTheOnlyDemoEditor -> waitingForInput)
  148. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  149. U"Please click or type into the Demo window or close it.");
  150. theCurrentPraatPicture = (PraatPicture) theReferenceToTheOnlyDemoEditor -> praatPicture;
  151. }
  152. void Demo_close () {
  153. theCurrentPraatPicture = & theForegroundPraatPicture;
  154. }
  155. int Demo_windowTitle (conststring32 title) {
  156. autoDemoOpen demo;
  157. Thing_setName (theReferenceToTheOnlyDemoEditor, title);
  158. return 1;
  159. }
  160. int Demo_show () {
  161. if (! theReferenceToTheOnlyDemoEditor) return 0;
  162. autoDemoOpen demo;
  163. GuiThing_show (theReferenceToTheOnlyDemoEditor -> windowForm);
  164. GuiShell_drain (theReferenceToTheOnlyDemoEditor -> windowForm);
  165. return 1;
  166. }
  167. #if cocoa
  168. @interface DemoWindowTimer: NSObject
  169. - (void) timerCallback: (NSTimer *) timer;
  170. @end
  171. @implementation DemoWindowTimer
  172. - (void) timerCallback: (NSTimer *) timer {
  173. (void) timer;
  174. printf ("eureka\n");
  175. }
  176. @end
  177. DemoWindowTimer *theDemoWindowTimer;
  178. #endif
  179. void Demo_timer (double duration) {
  180. #if cocoa
  181. if (! theDemoWindowTimer)
  182. theDemoWindowTimer = [[DemoWindowTimer alloc] init];
  183. [NSTimer scheduledTimerWithTimeInterval: duration
  184. target: theDemoWindowTimer
  185. selector: @selector (timerCallback)
  186. userInfo: nil
  187. repeats: false];
  188. #endif
  189. }
  190. void Demo_waitForInput (Interpreter interpreter) {
  191. if (! theReferenceToTheOnlyDemoEditor) return;
  192. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  193. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  194. U"Please click or type into the Demo window or close it.");
  195. }
  196. //GuiObject_show (theReferenceToTheOnlyDemoEditor -> windowForm);
  197. theReferenceToTheOnlyDemoEditor -> clicked = false;
  198. theReferenceToTheOnlyDemoEditor -> keyPressed = false;
  199. theReferenceToTheOnlyDemoEditor -> waitingForInput = true;
  200. {// scope
  201. autoMelderSaveDefaultDir saveDir;
  202. bool wasBackgrounding = Melder_backgrounding;
  203. if (wasBackgrounding) praat_foreground ();
  204. try {
  205. #if gtk
  206. do {
  207. gtk_main_iteration ();
  208. } while (! theReferenceToTheOnlyDemoEditor -> clicked &&
  209. ! theReferenceToTheOnlyDemoEditor -> keyPressed &&
  210. ! theReferenceToTheOnlyDemoEditor -> userWantsToClose);
  211. #elif cocoa
  212. do {
  213. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  214. [theReferenceToTheOnlyDemoEditor -> windowForm -> d_cocoaShell flushWindow];
  215. Graphics_updateWs (theReferenceToTheOnlyDemoEditor -> graphics.get()); // make sure that even texts will be drawn
  216. NSEvent *nsEvent = [NSApp
  217. nextEventMatchingMask: NSAnyEventMask
  218. untilDate: [NSDate distantFuture] // wait
  219. inMode: NSDefaultRunLoopMode
  220. dequeue: YES
  221. ];
  222. Melder_assert (nsEvent);
  223. [NSApp sendEvent: nsEvent];
  224. [NSApp updateWindows]; // called automatically?
  225. [pool release];
  226. } while (! theReferenceToTheOnlyDemoEditor -> clicked &&
  227. ! theReferenceToTheOnlyDemoEditor -> keyPressed &&
  228. ! theReferenceToTheOnlyDemoEditor -> userWantsToClose);
  229. #elif defined (_WIN32)
  230. do {
  231. XEvent event;
  232. GuiNextEvent (& event);
  233. XtDispatchEvent (& event);
  234. } while (! theReferenceToTheOnlyDemoEditor -> clicked &&
  235. ! theReferenceToTheOnlyDemoEditor -> keyPressed &&
  236. ! theReferenceToTheOnlyDemoEditor -> userWantsToClose);
  237. #endif
  238. } catch (MelderError) {
  239. Melder_flushError (U"An error made it to the outer level in the Demo window; should not occur! Please write to paul.boersma@uva.nl");
  240. }
  241. if (wasBackgrounding) praat_background ();
  242. }
  243. theReferenceToTheOnlyDemoEditor -> waitingForInput = false;
  244. if (theReferenceToTheOnlyDemoEditor -> userWantsToClose) {
  245. Interpreter_stop (interpreter);
  246. forget (theReferenceToTheOnlyDemoEditor);
  247. Melder_throw (U"You interrupted the script.");
  248. }
  249. }
  250. void Demo_peekInput (Interpreter interpreter) {
  251. if (! theReferenceToTheOnlyDemoEditor) return;
  252. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  253. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  254. U"Please click or type into the Demo window or close it.");
  255. }
  256. //GuiObject_show (theReferenceToTheOnlyDemoEditor -> windowForm);
  257. theReferenceToTheOnlyDemoEditor -> clicked = false;
  258. theReferenceToTheOnlyDemoEditor -> keyPressed = false;
  259. theReferenceToTheOnlyDemoEditor -> x = 0;
  260. theReferenceToTheOnlyDemoEditor -> y = 0;
  261. theReferenceToTheOnlyDemoEditor -> key = U'\0';
  262. theReferenceToTheOnlyDemoEditor -> shiftKeyPressed = false;
  263. theReferenceToTheOnlyDemoEditor -> commandKeyPressed = false;
  264. theReferenceToTheOnlyDemoEditor -> optionKeyPressed = false;
  265. theReferenceToTheOnlyDemoEditor -> extraControlKeyPressed = false;
  266. theReferenceToTheOnlyDemoEditor -> waitingForInput = true;
  267. {// scope
  268. autoMelderSaveDefaultDir saveDir;
  269. //bool wasBackgrounding = Melder_backgrounding;
  270. //if (wasBackgrounding) praat_foreground ();
  271. try {
  272. #if gtk
  273. while (gtk_events_pending ()) {
  274. gtk_main_iteration ();
  275. }
  276. #elif cocoa
  277. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  278. [theReferenceToTheOnlyDemoEditor -> windowForm -> d_cocoaShell flushWindow];
  279. Graphics_updateWs (theReferenceToTheOnlyDemoEditor -> graphics.get()); // make sure that even texts will be drawn
  280. while (NSEvent *nsEvent = [NSApp
  281. nextEventMatchingMask: NSAnyEventMask
  282. untilDate: [NSDate distantPast] // don't wait
  283. inMode: NSDefaultRunLoopMode
  284. dequeue: YES])
  285. {
  286. [NSApp sendEvent: nsEvent];
  287. }
  288. [NSApp updateWindows]; // called automatically?
  289. [pool release];
  290. #elif defined (_WIN32)
  291. XEvent event;
  292. while (PeekMessage (& event, 0, 0, 0, PM_REMOVE)) {
  293. XtDispatchEvent (& event);
  294. }
  295. #endif
  296. } catch (MelderError) {
  297. Melder_flushError (U"An error made it to the outer level in the Demo window; should not occur! Please write to paul.boersma@uva.nl");
  298. }
  299. //if (wasBackgrounding) praat_background ();
  300. }
  301. theReferenceToTheOnlyDemoEditor -> waitingForInput = false;
  302. if (theReferenceToTheOnlyDemoEditor -> userWantsToClose) {
  303. Interpreter_stop (interpreter);
  304. forget (theReferenceToTheOnlyDemoEditor);
  305. Melder_throw (U"You interrupted the script.");
  306. }
  307. }
  308. bool Demo_clicked () {
  309. if (! theReferenceToTheOnlyDemoEditor) return false;
  310. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  311. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  312. U"Please click or type into the Demo window or close it.");
  313. }
  314. return theReferenceToTheOnlyDemoEditor -> clicked;
  315. }
  316. double Demo_x () {
  317. if (! theReferenceToTheOnlyDemoEditor) return undefined;
  318. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  319. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  320. U"Please click or type into the Demo window or close it.");
  321. }
  322. trace (U"NDC before: ", theReferenceToTheOnlyDemoEditor -> graphics -> d_x1NDC, U" ", theReferenceToTheOnlyDemoEditor -> graphics -> d_x2NDC);
  323. Graphics_setInner (theReferenceToTheOnlyDemoEditor -> graphics.get());
  324. trace (U"NDC after: ", theReferenceToTheOnlyDemoEditor -> graphics -> d_x1NDC, U" ", theReferenceToTheOnlyDemoEditor -> graphics -> d_x2NDC);
  325. double xWC, yWC;
  326. trace (U"DC: x ", theReferenceToTheOnlyDemoEditor -> x, U", y ", theReferenceToTheOnlyDemoEditor -> y);
  327. Graphics_DCtoWC (theReferenceToTheOnlyDemoEditor -> graphics.get(), theReferenceToTheOnlyDemoEditor -> x, theReferenceToTheOnlyDemoEditor -> y, & xWC, & yWC);
  328. trace (U"WC: x ", xWC, U", y ", yWC);
  329. Graphics_unsetInner (theReferenceToTheOnlyDemoEditor -> graphics.get());
  330. return xWC;
  331. }
  332. double Demo_y () {
  333. if (! theReferenceToTheOnlyDemoEditor) return undefined;
  334. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  335. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  336. U"Please click or type into the Demo window or close it.");
  337. }
  338. Graphics_setInner (theReferenceToTheOnlyDemoEditor -> graphics.get());
  339. double xWC, yWC;
  340. Graphics_DCtoWC (theReferenceToTheOnlyDemoEditor -> graphics.get(), theReferenceToTheOnlyDemoEditor -> x, theReferenceToTheOnlyDemoEditor -> y, & xWC, & yWC);
  341. Graphics_unsetInner (theReferenceToTheOnlyDemoEditor -> graphics.get());
  342. return yWC;
  343. }
  344. bool Demo_keyPressed () {
  345. if (! theReferenceToTheOnlyDemoEditor) return false;
  346. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  347. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  348. U"Please click or type into the Demo window or close it.");
  349. }
  350. return theReferenceToTheOnlyDemoEditor -> keyPressed;
  351. }
  352. char32 Demo_key () {
  353. if (! theReferenceToTheOnlyDemoEditor) return U'\0';
  354. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  355. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  356. U"Please click or type into the Demo window or close it.");
  357. }
  358. return theReferenceToTheOnlyDemoEditor -> key;
  359. }
  360. bool Demo_shiftKeyPressed () {
  361. if (! theReferenceToTheOnlyDemoEditor) return false;
  362. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  363. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  364. U"Please click or type into the Demo window or close it.");
  365. }
  366. return theReferenceToTheOnlyDemoEditor -> shiftKeyPressed;
  367. }
  368. bool Demo_commandKeyPressed () {
  369. if (! theReferenceToTheOnlyDemoEditor) return false;
  370. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  371. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  372. U"Please click or type into the Demo window or close it.");
  373. }
  374. return theReferenceToTheOnlyDemoEditor -> commandKeyPressed;
  375. }
  376. bool Demo_optionKeyPressed () {
  377. if (! theReferenceToTheOnlyDemoEditor) return false;
  378. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  379. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  380. U"Please click or type into the Demo window or close it.");
  381. }
  382. return theReferenceToTheOnlyDemoEditor -> optionKeyPressed;
  383. }
  384. bool Demo_extraControlKeyPressed () {
  385. if (! theReferenceToTheOnlyDemoEditor) return false;
  386. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  387. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  388. U"Please click or type into the Demo window or close it.");
  389. }
  390. return theReferenceToTheOnlyDemoEditor -> extraControlKeyPressed;
  391. }
  392. bool Demo_input (conststring32 keys) {
  393. if (! theReferenceToTheOnlyDemoEditor) return false;
  394. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  395. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  396. U"Please click or type into the Demo window or close it.");
  397. }
  398. return str32chr (keys, theReferenceToTheOnlyDemoEditor -> key) != nullptr;
  399. }
  400. bool Demo_clickedIn (double left, double right, double bottom, double top) {
  401. if (! theReferenceToTheOnlyDemoEditor || ! theReferenceToTheOnlyDemoEditor -> clicked) return false;
  402. if (theReferenceToTheOnlyDemoEditor -> waitingForInput) {
  403. Melder_throw (U"You cannot work with the Demo window while it is waiting for input. "
  404. U"Please click or type into the Demo window or close it.");
  405. }
  406. if (! theReferenceToTheOnlyDemoEditor -> clicked) return false;
  407. double xWC = Demo_x (), yWC = Demo_y ();
  408. return xWC >= left && xWC < right && yWC >= bottom && yWC < top;
  409. }
  410. /* End of file DemoEditor.cpp */