UiPause.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* UiPause.cpp
  2. *
  3. * Copyright (C) 2009-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 "UiPause.h"
  19. #include "praatP.h"
  20. static autoUiForm thePauseForm;
  21. static UiField thePauseFormRadio = nullptr;
  22. static int thePauseForm_clicked = 0;
  23. static int theCancelContinueButton = 0;
  24. static int theEventLoopDepth = 0;
  25. static void thePauseFormOkCallback (UiForm /* sendingForm */, integer /* narg */, Stackel /* args */,
  26. conststring32 /* sendingString */, Interpreter /* interpreter */,
  27. conststring32 /* invokingButtonTitle */, bool /* modified */, void *closure) {
  28. /* BUG: should also restore praatP. editor. */
  29. /*
  30. * Get the data from the pause form.
  31. */
  32. thePauseForm_clicked = UiForm_getClickedContinueButton (thePauseForm.get());
  33. if (thePauseForm_clicked != theCancelContinueButton)
  34. UiForm_Interpreter_addVariables (thePauseForm.get(), (Interpreter) closure); // 'closure', not 'interpreter' or 'theInterpreter'!
  35. }
  36. static void thePauseFormCancelCallback (UiForm /* dia */, void * /* closure */) {
  37. if (theCancelContinueButton != 0) {
  38. thePauseForm_clicked = theCancelContinueButton;
  39. } else {
  40. thePauseForm_clicked = -1; // STOP button
  41. }
  42. }
  43. void UiPause_begin (GuiWindow topShell, conststring32 title, Interpreter interpreter) {
  44. if (theEventLoopDepth > 0)
  45. Melder_throw (U"Praat cannot have more than one pause form at a time.");
  46. thePauseForm = UiForm_create (topShell, Melder_cat (U"Pause: ", title),
  47. thePauseFormOkCallback, interpreter, // pass interpreter as closure!
  48. nullptr, nullptr);
  49. }
  50. void UiPause_real (conststring32 label, conststring32 defaultValue) {
  51. if (! thePauseForm)
  52. Melder_throw (U"The function \"real\" has to be between a \"beginPause\" and an \"endPause\".");
  53. UiForm_addReal (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
  54. }
  55. void UiPause_positive (conststring32 label, conststring32 defaultValue) {
  56. if (! thePauseForm)
  57. Melder_throw (U"The function \"positive\" has to be between a \"beginPause\" and an \"endPause\".");
  58. UiForm_addPositive (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
  59. }
  60. void UiPause_integer (conststring32 label, conststring32 defaultValue) {
  61. if (! thePauseForm)
  62. Melder_throw (U"The function \"integer\" has to be between a \"beginPause\" and an \"endPause\".");
  63. UiForm_addInteger (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
  64. }
  65. void UiPause_natural (conststring32 label, conststring32 defaultValue) {
  66. if (! thePauseForm)
  67. Melder_throw (U"The function \"natural\" has to be between a \"beginPause\" and an \"endPause\".");
  68. UiForm_addNatural (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
  69. }
  70. void UiPause_word (conststring32 label, conststring32 defaultValue) {
  71. if (! thePauseForm)
  72. Melder_throw (U"The function \"word\" has to be between a \"beginPause\" and an \"endPause\".");
  73. UiForm_addWord (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
  74. }
  75. void UiPause_sentence (conststring32 label, conststring32 defaultValue) {
  76. if (! thePauseForm)
  77. Melder_throw (U"The function \"sentence\" has to be between a \"beginPause\" and an \"endPause\".");
  78. UiForm_addSentence (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
  79. }
  80. void UiPause_text (conststring32 label, conststring32 defaultValue) {
  81. if (! thePauseForm)
  82. Melder_throw (U"The function \"text\" has to be between a \"beginPause\" and an \"endPause\".");
  83. UiForm_addText (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
  84. }
  85. void UiPause_boolean (conststring32 label, bool defaultValue) {
  86. if (! thePauseForm)
  87. Melder_throw (U"The function \"boolean\" has to be between a \"beginPause\" and an \"endPause\".");
  88. UiForm_addBoolean (thePauseForm.get(), nullptr, nullptr, label, defaultValue);
  89. }
  90. void UiPause_choice (conststring32 label, int defaultValue) {
  91. if (! thePauseForm)
  92. Melder_throw (U"The function \"choice\" has to be between a \"beginPause\" and an \"endPause\".");
  93. thePauseFormRadio = UiForm_addRadio (thePauseForm.get(), nullptr, nullptr, nullptr, label, defaultValue, 1);
  94. }
  95. void UiPause_optionMenu (conststring32 label, int defaultValue) {
  96. if (! thePauseForm)
  97. Melder_throw (U"The function \"optionMenu\" has to be between a \"beginPause\" and an \"endPause\".");
  98. thePauseFormRadio = UiForm_addOptionMenu (thePauseForm.get(), nullptr, nullptr, nullptr, label, defaultValue, 1);
  99. }
  100. void UiPause_option (conststring32 label) {
  101. if (! thePauseForm)
  102. Melder_throw (U"The function \"option\" has to be between a \"beginPause\" and an \"endPause\".");
  103. if (! thePauseFormRadio) {
  104. thePauseForm. reset();
  105. Melder_throw (U"Found the function \"option\" without a preceding \"choice\" or \"optionMenu\".");
  106. }
  107. UiOptionMenu_addButton (thePauseFormRadio, label);
  108. }
  109. void UiPause_comment (conststring32 label) {
  110. if (! thePauseForm)
  111. Melder_throw (U"The function \"comment\" has to be between a \"beginPause\" and an \"endPause\".");
  112. UiForm_addLabel (thePauseForm.get(), nullptr, label);
  113. }
  114. int UiPause_end (int numberOfContinueButtons, int defaultContinueButton, int cancelContinueButton,
  115. conststring32 continueText1, conststring32 continueText2, conststring32 continueText3,
  116. conststring32 continueText4, conststring32 continueText5, conststring32 continueText6,
  117. conststring32 continueText7, conststring32 continueText8, conststring32 continueText9,
  118. conststring32 continueText10, Interpreter interpreter)
  119. {
  120. if (! thePauseForm)
  121. Melder_throw (U"Found the function \"endPause\" without a preceding \"beginPause\".");
  122. UiForm_setPauseForm (thePauseForm.get(), numberOfContinueButtons, defaultContinueButton, cancelContinueButton,
  123. continueText1, continueText2, continueText3, continueText4, continueText5,
  124. continueText6, continueText7, continueText8, continueText9, continueText10,
  125. thePauseFormCancelCallback);
  126. theCancelContinueButton = cancelContinueButton;
  127. UiForm_finish (thePauseForm.get());
  128. bool wasBackgrounding = Melder_backgrounding;
  129. //if (theCurrentPraatApplication -> batch) goto end;
  130. if (wasBackgrounding) praat_foreground ();
  131. /*
  132. * Put the pause form on the screen.
  133. */
  134. UiForm_destroyWhenUnmanaged (thePauseForm.get());
  135. UiForm_do (thePauseForm.get(), false);
  136. /*
  137. * Wait for the user to click Stop or Continue.
  138. */
  139. {// scope
  140. autoMelderSaveDefaultDir saveDir;
  141. thePauseForm_clicked = 0;
  142. Melder_assert (theEventLoopDepth == 0);
  143. theEventLoopDepth ++;
  144. try {
  145. #if gtk
  146. do {
  147. gtk_main_iteration ();
  148. } while (! thePauseForm_clicked);
  149. #elif cocoa
  150. do {
  151. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  152. //[theDemoEditor -> windowForm -> d_cocoaWindow flushWindow];
  153. NSEvent *nsEvent = [NSApp
  154. nextEventMatchingMask: NSAnyEventMask
  155. untilDate: [NSDate distantFuture] // wait
  156. inMode: NSDefaultRunLoopMode
  157. dequeue: YES
  158. ];
  159. Melder_assert (nsEvent);
  160. [NSApp sendEvent: nsEvent];
  161. [NSApp updateWindows]; // called automatically?
  162. [pool release];
  163. } while (! thePauseForm_clicked);
  164. #elif motif
  165. do {
  166. XEvent event;
  167. GuiNextEvent (& event);
  168. XtDispatchEvent (& event);
  169. } while (! thePauseForm_clicked);
  170. #endif
  171. } catch (MelderError) {
  172. Melder_flushError (U"An error made it to the outer level in a pause window; should not occur! Please write to paul.boersma@uva.nl");
  173. }
  174. theEventLoopDepth --;
  175. }
  176. if (wasBackgrounding) praat_background ();
  177. /* BUG: should also restore praatP. editor. */
  178. thePauseForm. releaseToUser(); // undangle
  179. thePauseFormRadio = nullptr; // undangle
  180. if (thePauseForm_clicked == -1) {
  181. Interpreter_stop (interpreter);
  182. Melder_throw (U"You interrupted the script.");
  183. //Melder_flushError ();
  184. //Melder_clearError ();
  185. } else {
  186. //Melder_casual (U"Clicked ", thePauseForm_clicked);
  187. }
  188. return thePauseForm_clicked;
  189. }
  190. /* End of file UiPause.cpp */