Ui.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #ifndef _Ui_h_
  2. #define _Ui_h_
  3. /* Ui.h
  4. *
  5. * Copyright (C) 1992-2011,2012,2013,2015,2017,2018 Paul Boersma
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "Graphics.h"
  21. #include "Gui.h"
  22. #include "Interpreter.h"
  23. Thing_declare (EditorCommand);
  24. /* Forms for getting arguments from the user. */
  25. /* Example of usage:
  26. {
  27. static autoUiForm dia;
  28. if (! dia) {
  29. UiField radio;
  30. dia = UiForm_create
  31. (topShell, // the parent GuiWindow of the dialog window
  32. U"Create a new person", // the window title
  33. DO_Person_create, // the function to call when the user clicks OK
  34. nullptr, // the last argument to the OK routine (also for the other buttons); could be a ScriptEditor, or an EditorCommand, or an Interpreter, or nullptr
  35. U"Create person...", // the invoking button title
  36. U"Create person..."); // the help string; may be nullptr
  37. static integer age;
  38. UiForm_addNatural (dia.get(), & age, U"age", U"Age (years)", U"18");
  39. static double length;
  40. UiForm_addPositive (dia.get(), & length, U"length", U"Length (metres)", U"1.68 (= average)");
  41. static bool beard;
  42. UiForm_addBoolean (dia.get(), & beard, U"beard", U"Beard", false);
  43. static int sex;
  44. radio = UiForm_addRadio (dia.get(), & sex, U"sex", U"Sex", 1);
  45. UiRadio_addButton (radio, U"Female");
  46. UiRadio_addButton (radio, U"Male");
  47. UiForm_addWord (dia.get(), colour, U"colour", U"Colour", U"black");
  48. UiForm_addLabel (dia.get(), U"features", U"Some less conspicuous features:");
  49. static integer numberOfBirthMarks;
  50. UiForm_addNatural (dia.get(), & numberOfBirthMarks, U"numberOfBirthMarks", U"Number of birth marks", U"28");
  51. static char *favouriteGreeting;
  52. UiForm_addSentence (dia.get(), & favouriteGreeting, U"favouriteGreeting", U"Favourite greeting", U"Good morning");
  53. UiForm_finish (dia.get());
  54. }
  55. UiForm_setReal (dia.get(), & length, myLength);
  56. UiForm_setInteger (dia.get(), & numberOfBirthMarks, 30);
  57. UiForm_do (dia.get(), false); // show dialog box
  58. }
  59. Real, Positive, Integer, Natural, Channel, Word, and Sentence show a label and an editable text field.
  60. Radio shows a label and has Button children stacked below it.
  61. OptionMenu shows a label and has Button children in a menu.
  62. Label only shows its value.
  63. Text, Numvec and Nummat show an editable text field over the whole width of the form.
  64. Boolean shows a labeled toggle button which is on (true) or off (false).
  65. Button does the same inside a radio box or option menu.
  66. List shows a scrollable list.
  67. Colour shows a label and an editable text field for a grey value between 0.0 and 1.0, a colour name, or {r,g,b}.
  68. Channel shows a label and an editable text field for a natural number or one of the texts "Left", "Right", "Mono" or "Stereo".
  69. As shown in the example, Real, Positive, Integer, and Natural may contain extra text;
  70. this text is considered a comment.
  71. When you click "Standards", the standard values (including comments)
  72. are restored to all items in the form.
  73. */
  74. Thing_define (UiOption, Thing) {
  75. GuiRadioButton radioButton;
  76. GuiObject menuItem;
  77. };
  78. enum class _kUiField_type {
  79. REAL_ = 1,
  80. REAL_OR_UNDEFINED_ = 2,
  81. POSITIVE_ = 3,
  82. INTEGER_ = 4,
  83. NATURAL_ = 5,
  84. WORD_ = 6,
  85. SENTENCE_ = 7,
  86. COLOUR_ = 8,
  87. CHANNEL_ = 9,
  88. LABEL_ = 10,
  89. TEXT_ = 11,
  90. NUMVEC_ = 12,
  91. NUMMAT_ = 13,
  92. BOOLEAN_ = 14,
  93. RADIO_ = 15,
  94. OPTIONMENU_ = 16,
  95. LIST_ = 17,
  96. LABELLED_TEXT_MIN_ = 1,
  97. LABELLED_TEXT_MAX_ = 9
  98. };
  99. Thing_define (UiField, Thing) {
  100. _kUiField_type type;
  101. autostring32 formLabel;
  102. double realValue;
  103. integer integerValue, integerDefaultValue;
  104. autostring32 stringValue, stringDefaultValue;
  105. autoVEC numericVectorValue;
  106. autoMAT numericMatrixValue;
  107. Graphics_Colour colourValue;
  108. OrderedOf<structUiOption> options;
  109. conststring32vector strings;
  110. GuiLabel label;
  111. GuiText text;
  112. GuiCheckButton checkButton;
  113. GuiRadioButton radioButton;
  114. GuiList list;
  115. GuiOptionMenu optionMenu;
  116. int y;
  117. conststring32 variableName; // a reference to a name known at compile time, for use by the Praat library
  118. double *realVariable;
  119. integer *integerVariable;
  120. int *intVariable;
  121. bool *boolVariable;
  122. conststring32 *stringVariable;
  123. Graphics_Colour *colourVariable;
  124. constVEC *numericVectorVariable;
  125. constMAT *numericMatrixVariable;
  126. int subtract;
  127. void v_destroy () noexcept
  128. override;
  129. };
  130. #define UiCallback_ARGS \
  131. UiForm _sendingForm, integer _narg, Stackel _args, conststring32 _sendingString, Interpreter interpreter, conststring32 _invokingButtonTitle, bool _modified, void *_closure
  132. typedef void (*UiCallback) (UiCallback_ARGS);
  133. #define MAXIMUM_NUMBER_OF_FIELDS 50
  134. #define MAXIMUM_NUMBER_OF_CONTINUE_BUTTONS 10
  135. Thing_define (UiForm, Thing) {
  136. EditorCommand command;
  137. GuiWindow d_dialogParent;
  138. autostring32 invokingButtonTitle, helpTitle;
  139. UiCallback okCallback;
  140. void *buttonClosure;
  141. /*
  142. In case the validity of the form depends on the selected objects.
  143. */
  144. bool (*allowExecutionHook) (void *closure);
  145. void *allowExecutionClosure;
  146. /*
  147. In case the form is built by specifying buttons (rather than a system-built-in file dialog).
  148. */
  149. GuiDialog d_dialogForm;
  150. void (*applyCallback) (UiForm dia, void *closure);
  151. void (*cancelCallback) (UiForm dia, void *closure);
  152. int numberOfContinueButtons, defaultContinueButton, cancelContinueButton, clickedContinueButton;
  153. conststring32 continueTexts [1 + MAXIMUM_NUMBER_OF_CONTINUE_BUTTONS]; // references to strings owned by a script
  154. int numberOfFields;
  155. autoUiField field [1 + MAXIMUM_NUMBER_OF_FIELDS];
  156. GuiButton okButton, cancelButton, revertButton, helpButton, applyButton, continueButtons [1 + MAXIMUM_NUMBER_OF_CONTINUE_BUTTONS];
  157. bool destroyWhenUnmanaged, isPauseForm;
  158. /*
  159. In case the form contains a file.
  160. */
  161. structMelderFile file;
  162. int shiftKeyPressed;
  163. bool allowMultipleFiles; // for input
  164. void v_destroy () noexcept
  165. override;
  166. };
  167. /* The following functions work on the screen and from batch. */
  168. autoUiForm UiForm_create (GuiWindow parent, conststring32 title,
  169. UiCallback okCallback, void *buttonClosure,
  170. conststring32 invokingButtonTitle, conststring32 helpTitle);
  171. UiField UiForm_addReal (UiForm me, double *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue);
  172. UiField UiForm_addRealOrUndefined (UiForm me, double *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue);
  173. UiField UiForm_addPositive (UiForm me, double *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue);
  174. UiField UiForm_addInteger (UiForm me, integer *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue);
  175. UiField UiForm_addNatural (UiForm me, integer *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue);
  176. UiField UiForm_addWord (UiForm me, conststring32 *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue);
  177. UiField UiForm_addSentence (UiForm me, conststring32 *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue);
  178. UiField UiForm_addLabel (UiForm me, conststring32 *variable, conststring32 label);
  179. UiField UiForm_addBoolean (UiForm me, bool *variable, conststring32 variableName, conststring32 label, bool defaultValue);
  180. UiField UiForm_addText (UiForm me, conststring32 *variable, conststring32 variableName, conststring32 name, conststring32 defaultValue);
  181. UiField UiForm_addNumvec (UiForm me, constVEC *variable, conststring32 variableName, conststring32 name, conststring32 defaultValue);
  182. UiField UiForm_addNummat (UiForm me, constMAT *variable, conststring32 variableName, conststring32 name, conststring32 defaultValue);
  183. UiField UiForm_addRadio (UiForm me, int *intVariable, conststring32 *stringVariable, conststring32 variableName, conststring32 label, int defaultValue, int base);
  184. UiOption UiRadio_addButton (UiField me, conststring32 label);
  185. UiField UiForm_addOptionMenu (UiForm me, int *intVariable, conststring32 *stringVariable, conststring32 variableName, conststring32 label, int defaultValue, int base);
  186. UiOption UiOptionMenu_addButton (UiField me, conststring32 label);
  187. UiField UiForm_addList (UiForm me, integer *integerVariable, conststring32 *stringVariable, conststring32 variableName, conststring32 label, conststring32vector strings, integer defaultValue);
  188. UiField UiForm_addColour (UiForm me, Graphics_Colour *colourVariable, conststring32 variableName, conststring32 label, conststring32 defaultValue);
  189. UiField UiForm_addChannel (UiForm me, integer *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue);
  190. void UiForm_finish (UiForm me);
  191. void UiForm_destroyWhenUnmanaged (UiForm me);
  192. void UiForm_setPauseForm (UiForm me,
  193. int numberOfContinueButtons, int defaultContinueButton, int cancelContinueButton,
  194. conststring32 continue1, conststring32 continue2, conststring32 continue3,
  195. conststring32 continue4, conststring32 continue5, conststring32 continue6,
  196. conststring32 continue7, conststring32 continue8, conststring32 continue9,
  197. conststring32 continue10,
  198. void (*cancelCallback) (UiForm dia, void *closure));
  199. /* The following nine functions set values in widgets. */
  200. /* Do not call from batch. */
  201. /* 'fieldName' is name from UiForm_addXXXXXX (), */
  202. /* without anything from and including the first " (" or ":". */
  203. /* Real, RealOrUndefined, and Positive fields: */
  204. void UiForm_setReal (UiForm me, double *p_variable, double value);
  205. void UiForm_setRealAsString (UiForm me, double *p_variable, conststring32 stringValue /* cattable */);
  206. /* Integer, Natural, Channel, and List fields: */
  207. void UiForm_setInteger (UiForm me, integer *p_variable, integer value);
  208. void UiForm_setIntegerAsString (UiForm me, integer *p_variable, conststring32 stringValue /* cattable */);
  209. /* Word, Sentence, Text, and Label fields: */
  210. void UiForm_setString (UiForm me, conststring32 *p_variable, conststring32 text /* cattable */);
  211. /* Boolean fields: */
  212. void UiForm_setBoolean (UiForm me, bool *p_variable, bool value);
  213. /* Radio and OptionMenu fields: */
  214. void UiForm_setOption (UiForm me, int *p_variable, int value);
  215. void UiForm_setOptionAsString (UiForm me, int *p_variable, conststring32 stringValue /* cattable */);
  216. /* Colour fields: */
  217. void UiForm_setColourAsGreyValue (UiForm me, Graphics_Colour *p_variable, double greyValue);
  218. void UiForm_do (UiForm me, bool modified);
  219. /*
  220. Function:
  221. put the form on the screen.
  222. Behaviour:
  223. If the user clicks "OK",
  224. the form will call the `okCallback` that was registered with UiForm_create ().
  225. If the `okCallback` then returns 1, the form will disappear from the screen;
  226. if it returns 0, the form will stay on the screen; this can be used
  227. for enabling the user to repair mistakes in the form.
  228. If the user clicks "Apply",
  229. the form will call the `okCallback` that was registered with UiForm_create (),
  230. and the form disappears from the screen.
  231. If the user clicks "Cancel", the form disappears from the screen.
  232. If the user clicks "Help", the form calls `help` with the `helpTitle`
  233. and stays on the screen.
  234. When the form disappears from the screen, the values in the fields
  235. will remain until the next invocation of UiForm_do () for the same form.
  236. Arguments:
  237. the above behaviour describes the action when `modified` is false.
  238. If `modified` is true, the user does not have to click OK.
  239. The form will still appear on the screen,
  240. but the `okCallback` will be called immediately.
  241. */
  242. void UiForm_info (UiForm me, integer narg);
  243. /*
  244. The `okCallback` can use the following seven functions to ask arguments.
  245. The field names are the `label` or `name` arguments to UiForm_addXXXXXX (),
  246. without anything from parentheses or from a colon.
  247. These functions work from the GUI as well as from a script.
  248. */
  249. integer UiForm_getInteger (UiForm me, conststring32 fieldName); // Integer, Natural, Boolean, Radio, List
  250. char32 * UiForm_getString (UiForm me, conststring32 fieldName); // Word, Sentence, Text, Numvec, Nummat, Radio, List
  251. MelderFile UiForm_getFile (UiForm me, conststring32 fieldName); // FileIn, FileOut
  252. double UiForm_getReal_check (UiForm me, conststring32 fieldName);
  253. integer UiForm_getInteger_check (UiForm me, conststring32 fieldName);
  254. char32 * UiForm_getString_check (UiForm me, conststring32 fieldName);
  255. Graphics_Colour UiForm_getColour_check (UiForm me, conststring32 fieldName);
  256. void UiForm_call (UiForm me, integer narg, Stackel args, Interpreter interpreter);
  257. void UiForm_parseString (UiForm me, conststring32 arguments, Interpreter interpreter);
  258. autoUiForm UiInfile_create (GuiWindow parent, conststring32 title,
  259. UiCallback okCallback, void *okClosure,
  260. conststring32 invokingButtonTitle, conststring32 helpTitle, bool allowMultipleFiles);
  261. autoUiForm UiOutfile_create (GuiWindow parent, conststring32 title,
  262. UiCallback okCallback, void *okClosure,
  263. conststring32 invokingButtonTitle, conststring32 helpTitle);
  264. void UiInfile_do (UiForm me);
  265. void UiOutfile_do (UiForm me, conststring32 defaultName);
  266. MelderFile UiFile_getFile (UiForm me);
  267. void UiFile_hide ();
  268. /*
  269. Hides the visible UiFile that was opened most recently.
  270. Normally, file selectors stay open until their okCallback has completed.
  271. However, the okCallback may initiate an event loop allowing the user
  272. to interact with the application, for instance in Melder_pause ().
  273. In order that the user does not have to hide the modal file selector
  274. manually (by clicking the Cancel button), the application can call
  275. UiFile_hide () before Melder_pause ().
  276. */
  277. void UiHistory_write (conststring32 string);
  278. void UiHistory_write_expandQuotes (conststring32 string);
  279. void UiHistory_write_colonize (conststring32 string);
  280. char32 *UiHistory_get ();
  281. void UiHistory_clear ();
  282. void Ui_setAllowExecutionHook (bool (*allowExecutionHook) (void *closure), void *allowExecutionClosure);
  283. void UiForm_Interpreter_addVariables (UiForm me, Interpreter interpreter);
  284. int UiForm_getClickedContinueButton (UiForm me);
  285. /* End of file Ui.h */
  286. #endif