Ui.cpp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  1. /* Ui.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 <wctype.h>
  19. #include <ctype.h>
  20. #include "../kar/longchar.h"
  21. #include "machine.h"
  22. #include "GuiP.h"
  23. #include "Collection.h"
  24. #include "UiP.h"
  25. #include "Editor.h"
  26. #include "Graphics.h" // colours
  27. /***** class UiField: the things that have values in an UiForm dialog *****/
  28. Thing_implement (UiField, Thing, 0);
  29. void structUiField :: v_destroy () noexcept {
  30. our UiField_Parent :: v_destroy ();
  31. }
  32. static autoUiField UiField_create (_kUiField_type type, conststring32 name) {
  33. autoUiField me = Thing_new (UiField);
  34. char32 shortName [1+100], *p;
  35. my type = type;
  36. my formLabel = Melder_dup (name);
  37. str32ncpy (shortName, name, 100);
  38. shortName [100] = U'\0';
  39. /*
  40. Strip parentheses and colon off parameter name.
  41. */
  42. if (!! (p = (char32 *) str32chr (shortName, U'('))) {
  43. *p = U'\0';
  44. if (p - shortName > 0 && p [-1] == U' ')
  45. p [-1] = U'\0';
  46. }
  47. p = shortName;
  48. if (*p != U'\0' && p [str32len (p) - 1] == U':')
  49. p [str32len (p) - 1] = U'\0';
  50. Thing_setName (me.get(), shortName);
  51. return me;
  52. }
  53. /***** class UiOption: radio buttons and menu options *****/
  54. Thing_implement (UiOption, Thing, 0);
  55. static autoUiOption UiOption_create (conststring32 label) {
  56. autoUiOption me = Thing_new (UiOption);
  57. Thing_setName (me.get(), label);
  58. return me;
  59. }
  60. UiOption UiRadio_addButton (UiField me, conststring32 label) {
  61. if (! me) return nullptr;
  62. Melder_assert (my type == _kUiField_type::RADIO_ || my type == _kUiField_type::OPTIONMENU_);
  63. autoUiOption thee = UiOption_create (label);
  64. return my options. addItem_move (thee.move());
  65. }
  66. UiOption UiOptionMenu_addButton (UiField me, conststring32 label) {
  67. if (! me) return nullptr;
  68. Melder_assert (my type == _kUiField_type::RADIO_ || my type == _kUiField_type::OPTIONMENU_);
  69. autoUiOption thee = UiOption_create (label);
  70. return my options. addItem_move (thee.move());
  71. }
  72. /***** Things to do with UiField objects. *****/
  73. static void UiField_setDefault (UiField me) {
  74. switch (my type) {
  75. case _kUiField_type::REAL_:
  76. case _kUiField_type::REAL_OR_UNDEFINED_:
  77. case _kUiField_type::POSITIVE_:
  78. case _kUiField_type::INTEGER_:
  79. case _kUiField_type::NATURAL_:
  80. case _kUiField_type::WORD_:
  81. case _kUiField_type::SENTENCE_:
  82. case _kUiField_type::COLOUR_:
  83. case _kUiField_type::CHANNEL_:
  84. case _kUiField_type::TEXT_:
  85. case _kUiField_type::NUMVEC_:
  86. case _kUiField_type::NUMMAT_:
  87. {
  88. GuiText_setString (my text, my stringDefaultValue.get());
  89. }
  90. break;
  91. case _kUiField_type::BOOLEAN_:
  92. {
  93. GuiCheckButton_setValue (my checkButton, my integerDefaultValue);
  94. }
  95. break;
  96. case _kUiField_type::RADIO_:
  97. {
  98. for (int i = 1; i <= my options.size; i ++) {
  99. if (i == my integerDefaultValue) {
  100. UiOption b = my options.at [i];
  101. GuiRadioButton_set (b -> radioButton);
  102. }
  103. }
  104. }
  105. break;
  106. case _kUiField_type::OPTIONMENU_:
  107. {
  108. GuiOptionMenu_setValue (my optionMenu, my integerDefaultValue);
  109. }
  110. break;
  111. case _kUiField_type::LIST_:
  112. {
  113. GuiList_selectItem (my list, my integerDefaultValue);
  114. }
  115. }
  116. }
  117. static int colourToValue (UiField me, char32 *string) {
  118. char32 *p = string;
  119. Melder_skipHorizontalSpace (& p);
  120. *p = Melder_toLowerCase (*p);
  121. char32 first = *p;
  122. if (first == U'{') {
  123. my colourValue. red = Melder_atof (++ p);
  124. p = str32chr (p, U',');
  125. if (! p) return 0;
  126. my colourValue. green = Melder_atof (++ p);
  127. p = str32chr (p, U',');
  128. if (! p) return 0;
  129. my colourValue. blue = Melder_atof (++ p);
  130. } else {
  131. *p = Melder_toLowerCase (*p);
  132. if (str32equ (p, U"black")) my colourValue = Graphics_BLACK;
  133. else if (str32equ (p, U"white")) my colourValue = Graphics_WHITE;
  134. else if (str32equ (p, U"red")) my colourValue = Graphics_RED;
  135. else if (str32equ (p, U"green")) my colourValue = Graphics_GREEN;
  136. else if (str32equ (p, U"blue")) my colourValue = Graphics_BLUE;
  137. else if (str32equ (p, U"yellow")) my colourValue = Graphics_YELLOW;
  138. else if (str32equ (p, U"cyan")) my colourValue = Graphics_CYAN;
  139. else if (str32equ (p, U"magenta")) my colourValue = Graphics_MAGENTA;
  140. else if (str32equ (p, U"maroon")) my colourValue = Graphics_MAROON;
  141. else if (str32equ (p, U"lime")) my colourValue = Graphics_LIME;
  142. else if (str32equ (p, U"navy")) my colourValue = Graphics_NAVY;
  143. else if (str32equ (p, U"teal")) my colourValue = Graphics_TEAL;
  144. else if (str32equ (p, U"purple")) my colourValue = Graphics_PURPLE;
  145. else if (str32equ (p, U"olive")) my colourValue = Graphics_OLIVE;
  146. else if (str32equ (p, U"pink")) my colourValue = Graphics_PINK;
  147. else if (str32equ (p, U"silver")) my colourValue = Graphics_SILVER;
  148. else if (str32equ (p, U"grey")) my colourValue = Graphics_GREY;
  149. else { *p = first; return 0; }
  150. *p = first;
  151. }
  152. return 1;
  153. }
  154. static void UiField_widgetToValue (UiField me) {
  155. switch (my type)
  156. {
  157. case _kUiField_type::REAL_:
  158. case _kUiField_type::REAL_OR_UNDEFINED_:
  159. case _kUiField_type::POSITIVE_:
  160. {
  161. autostring32 text = GuiText_getString (my text); // the text as typed by the user
  162. Interpreter_numericExpression (nullptr, text.get(), & my realValue);
  163. if (isundef (my realValue) && my type != _kUiField_type::REAL_OR_UNDEFINED_)
  164. Melder_throw (U"“", my name.get(), U"” has the value \"undefined\".");
  165. if (my type == _kUiField_type::POSITIVE_ && my realValue <= 0.0)
  166. Melder_throw (U"“", my name.get(), U"” should be greater than 0.0.");
  167. if (my realVariable)
  168. *my realVariable = my realValue;
  169. }
  170. break;
  171. case _kUiField_type::INTEGER_:
  172. case _kUiField_type::NATURAL_:
  173. case _kUiField_type::CHANNEL_:
  174. {
  175. autostring32 text = GuiText_getString (my text);
  176. if (my type == _kUiField_type::CHANNEL_ && (str32equ (text.get(), U"Left") || str32equ (text.get(), U"Mono"))) {
  177. my integerValue = 1;
  178. } else if (my type == _kUiField_type::CHANNEL_ && (str32equ (text.get(), U"Right") || str32equ (text.get(), U"Stereo"))) {
  179. my integerValue = 2;
  180. } else {
  181. double realValue;
  182. Interpreter_numericExpression (nullptr, text.get(), & realValue);
  183. my integerValue = Melder_iround (realValue);
  184. Melder_require (my integerValue == realValue,
  185. U"“", my name.get(), U"” should be a whole number.");
  186. }
  187. if (my type == _kUiField_type::NATURAL_) {
  188. Melder_require (my integerValue >= 1,
  189. U"“", my name.get(), U"” should be a positive whole number.");
  190. }
  191. if (my type == _kUiField_type::CHANNEL_) {
  192. Melder_require (my integerValue >= 0,
  193. U"“", my name.get(), U"” should be a positive whole number or zero.");
  194. }
  195. if (my integerVariable)
  196. *my integerVariable = my integerValue;
  197. }
  198. break;
  199. case _kUiField_type::WORD_:
  200. {
  201. my stringValue = GuiText_getString (my text);
  202. Melder_require (*Melder_findEndOfInk (my stringValue.get()) == U'\0',
  203. U"“", my name.get(), U"” should be a single ink-word and cannot contain a space.");
  204. if (my stringVariable)
  205. *my stringVariable = my stringValue.get();
  206. }
  207. break;
  208. case _kUiField_type::SENTENCE_:
  209. case _kUiField_type::TEXT_:
  210. {
  211. my stringValue = GuiText_getString (my text);
  212. if (my stringVariable)
  213. *my stringVariable = my stringValue.get();
  214. }
  215. break;
  216. case _kUiField_type::NUMVEC_:
  217. {
  218. my stringValue = GuiText_getString (my text);
  219. VEC result;
  220. bool owned;
  221. Interpreter_numericVectorExpression (nullptr, my stringValue.get(), & result, & owned);
  222. if (owned) {
  223. my numericVectorValue. adoptFromAmbiguousOwner (result);
  224. } else {
  225. my numericVectorValue = VECcopy (result);
  226. }
  227. if (my numericVectorVariable)
  228. *my numericVectorVariable = my numericVectorValue.get();
  229. }
  230. break;
  231. case _kUiField_type::NUMMAT_:
  232. {
  233. my stringValue = GuiText_getString (my text);
  234. MAT result;
  235. bool owned;
  236. Interpreter_numericMatrixExpression (nullptr, my stringValue.get(), & result, & owned);
  237. if (owned) {
  238. my numericMatrixValue. adoptFromAmbiguousOwner (result);
  239. } else {
  240. my numericMatrixValue = matrixcopy (result);
  241. }
  242. if (my numericMatrixVariable)
  243. *my numericMatrixVariable = my numericMatrixValue.get();
  244. }
  245. break;
  246. case _kUiField_type::BOOLEAN_:
  247. {
  248. my integerValue = GuiCheckButton_getValue (my checkButton);
  249. if (my boolVariable)
  250. *my boolVariable = my integerValue;
  251. }
  252. break;
  253. case _kUiField_type::RADIO_:
  254. {
  255. my integerValue = 0;
  256. for (int i = 1; i <= my options.size; i ++) {
  257. UiOption b = my options.at [i];
  258. if (GuiRadioButton_getValue (b -> radioButton))
  259. my integerValue = i;
  260. }
  261. if (my integerValue == 0)
  262. Melder_throw (U"No option chosen for “", my name.get(), U"”.");
  263. if (my intVariable)
  264. *my intVariable = my integerValue - my subtract;
  265. if (my stringVariable)
  266. *my stringVariable = my options.at [my integerValue] -> name.get();
  267. }
  268. break;
  269. case _kUiField_type::OPTIONMENU_:
  270. {
  271. my integerValue = GuiOptionMenu_getValue (my optionMenu);
  272. if (my integerValue == 0)
  273. Melder_throw (U"No option chosen for “", my name.get(), U"”.");
  274. if (my intVariable)
  275. *my intVariable = my integerValue - my subtract;
  276. if (my stringVariable)
  277. *my stringVariable = my options.at [my integerValue] -> name.get();
  278. }
  279. break;
  280. case _kUiField_type::LIST_:
  281. {
  282. integer numberOfSelected, *selected = GuiList_getSelectedPositions (my list, & numberOfSelected); // BUG memory
  283. if (! selected) {
  284. Melder_warning (U"No items selected.");
  285. my integerValue = 1;
  286. } else {
  287. if (numberOfSelected > 1)
  288. Melder_warning (U"More than one item selected.");
  289. my integerValue = selected [1];
  290. NUMvector_free <integer> (selected, 1);
  291. }
  292. if (my integerVariable)
  293. *my integerVariable = my integerValue;
  294. if (my stringVariable)
  295. *my stringVariable = (char32 *) my strings [my integerValue];
  296. }
  297. break;
  298. case _kUiField_type::COLOUR_:
  299. {
  300. autostring32 string = GuiText_getString (my text);
  301. if (colourToValue (me, string.get())) {
  302. // do nothing
  303. } else {
  304. Interpreter_numericExpression (nullptr, string.get(), & my colourValue. red);
  305. my colourValue. green = my colourValue. blue = my colourValue. red;
  306. }
  307. if (my colourVariable)
  308. *my colourVariable = my colourValue;
  309. }
  310. }
  311. }
  312. /***** History mechanism. *****/
  313. static MelderString theHistory { };
  314. void UiHistory_write (conststring32 string) { MelderString_append (& theHistory, string); }
  315. void UiHistory_write_expandQuotes (conststring32 string) {
  316. if (! string) return;
  317. for (const char32 *p = & string [0]; *p != U'\0'; p ++) {
  318. if (*p == U'\"') MelderString_append (& theHistory, U"\"\""); else MelderString_appendCharacter (& theHistory, *p);
  319. }
  320. }
  321. void UiHistory_write_colonize (conststring32 string) {
  322. if (! string) return;
  323. for (const char32 *p = & string [0]; *p != U'\0'; p ++) {
  324. if (*p == U'.' && p [1] == U'.' && p [2] == U'.') {
  325. MelderString_append (& theHistory, U":");
  326. p += 2;
  327. } else {
  328. MelderString_appendCharacter (& theHistory, *p);
  329. }
  330. }
  331. }
  332. char32 *UiHistory_get () { return theHistory.string; }
  333. void UiHistory_clear () { MelderString_empty (& theHistory); }
  334. /***** class UiForm: dialog windows *****/
  335. Thing_implement (UiForm, Thing, 0);
  336. bool (*theAllowExecutionHookHint) (void *closure) = nullptr;
  337. void *theAllowExecutionClosureHint = nullptr;
  338. void Ui_setAllowExecutionHook (bool (*allowExecutionHook) (void *closure), void *allowExecutionClosure) {
  339. theAllowExecutionHookHint = allowExecutionHook;
  340. theAllowExecutionClosureHint = allowExecutionClosure;
  341. }
  342. void structUiForm :: v_destroy () noexcept {
  343. if (our d_dialogForm) {
  344. trace (U"form <<", our d_dialogForm -> name.get(), U">>, invoking-button title <<", our invokingButtonTitle.get(), U">>");
  345. GuiObject_destroy (our d_dialogForm -> d_widget); // BUG: make sure this destroys the shell
  346. }
  347. our UiForm_Parent :: v_destroy ();
  348. }
  349. static void gui_button_cb_revert (UiForm me, GuiButtonEvent /* event */) {
  350. for (int ifield = 1; ifield <= my numberOfFields; ifield ++)
  351. UiField_setDefault (my field [ifield].get());
  352. }
  353. static void gui_dialog_cb_close (UiForm me) {
  354. if (my cancelCallback) my cancelCallback (me, my buttonClosure);
  355. GuiThing_hide (my d_dialogForm);
  356. if (my destroyWhenUnmanaged) forget (me);
  357. }
  358. static void gui_button_cb_cancel (UiForm me, GuiButtonEvent /* event */) {
  359. if (my cancelCallback) my cancelCallback (me, my buttonClosure);
  360. GuiThing_hide (my d_dialogForm);
  361. if (my destroyWhenUnmanaged) forget (me);
  362. }
  363. static void UiForm_okOrApply (UiForm me, GuiButton button, int hide) {
  364. if (my allowExecutionHook && ! my allowExecutionHook (my allowExecutionClosure)) {
  365. Melder_flushError (U"Cannot execute command window “", my name.get(), U"”.");
  366. return;
  367. }
  368. try {
  369. for (int ifield = 1; ifield <= my numberOfFields; ifield ++)
  370. UiField_widgetToValue (my field [ifield].get());
  371. } catch (MelderError) {
  372. Melder_flushError (U"Please correct command window “", my name.get(), U"” or cancel.");
  373. return;
  374. }
  375. if (my okButton) GuiThing_setSensitive (my okButton, false);
  376. for (int i = 1; i <= my numberOfContinueButtons; i ++)
  377. if (my continueButtons [i])
  378. GuiThing_setSensitive (my continueButtons [i], false);
  379. if (my applyButton) GuiThing_setSensitive (my applyButton, false);
  380. if (my cancelButton) GuiThing_setSensitive (my cancelButton, false);
  381. if (my revertButton) GuiThing_setSensitive (my revertButton, false);
  382. if (my helpButton) GuiThing_setSensitive (my helpButton, false);
  383. #if defined (_WIN32)
  384. GdiFlush ();
  385. #endif
  386. if (my isPauseForm) {
  387. for (int i = 1; i <= my numberOfContinueButtons; i ++) {
  388. if (button == my continueButtons [i]) {
  389. my clickedContinueButton = i;
  390. }
  391. }
  392. }
  393. /*
  394. Keep the gate for error handling.
  395. */
  396. try {
  397. my okCallback (me, 0, nullptr, nullptr, nullptr, nullptr, false, my buttonClosure);
  398. /*
  399. Write everything to history. Before destruction!
  400. */
  401. if (! my isPauseForm) {
  402. UiHistory_write (U"\n");
  403. UiHistory_write_colonize (my invokingButtonTitle.get());
  404. int size = my numberOfFields;
  405. while (size >= 1 && my field [size] -> type == _kUiField_type::LABEL_)
  406. size --; // ignore trailing fields without a value
  407. int next = 0;
  408. for (int ifield = 1; ifield <= size; ifield ++) {
  409. UiField field = my field [ifield].get();
  410. switch (field -> type)
  411. {
  412. case _kUiField_type::REAL_:
  413. case _kUiField_type::REAL_OR_UNDEFINED_:
  414. case _kUiField_type::POSITIVE_:
  415. {
  416. UiHistory_write (next -- ? U", " : U" ");
  417. UiHistory_write (Melder_double (field -> realValue));
  418. }
  419. break;
  420. case _kUiField_type::INTEGER_:
  421. case _kUiField_type::NATURAL_:
  422. case _kUiField_type::CHANNEL_:
  423. {
  424. UiHistory_write (next -- ? U", " : U" ");
  425. UiHistory_write (Melder_integer (field -> integerValue));
  426. }
  427. break;
  428. case _kUiField_type::WORD_:
  429. case _kUiField_type::SENTENCE_:
  430. case _kUiField_type::TEXT_:
  431. {
  432. UiHistory_write (next -- ? U", \"" : U" \"");
  433. UiHistory_write_expandQuotes (field -> stringValue.get());
  434. UiHistory_write (U"\"");
  435. }
  436. break;
  437. case _kUiField_type::BOOLEAN_:
  438. {
  439. UiHistory_write (field -> integerValue ? (next -- ? U", \"yes\"" : U" \"yes\"") : (next -- ? U", \"no\"" : U" \"no\""));
  440. }
  441. break;
  442. case _kUiField_type::RADIO_:
  443. case _kUiField_type::OPTIONMENU_:
  444. {
  445. UiOption b = field -> options.at [field -> integerValue];
  446. UiHistory_write (next -- ? U", \"" : U" \"");
  447. UiHistory_write_expandQuotes (b -> name.get());
  448. UiHistory_write (U"\"");
  449. }
  450. break;
  451. case _kUiField_type::LIST_:
  452. {
  453. UiHistory_write (next -- ? U", \"" : U" \"");
  454. UiHistory_write_expandQuotes (field -> strings [field -> integerValue]);
  455. UiHistory_write (U"\"");
  456. }
  457. break;
  458. case _kUiField_type::COLOUR_:
  459. {
  460. UiHistory_write (next -- ? U", \"" : U" \"");
  461. UiHistory_write (Graphics_Colour_name (field -> colourValue));
  462. UiHistory_write (U"\"");
  463. }
  464. }
  465. }
  466. }
  467. if (hide) {
  468. GuiThing_hide (my d_dialogForm);
  469. if (my destroyWhenUnmanaged) {
  470. forget (me);
  471. return;
  472. }
  473. }
  474. } catch (MelderError) {
  475. /*
  476. If a solution has already been suggested, or the "error" was actually a conscious user action, do not add anything more.
  477. */
  478. if (! str32str (Melder_getError (), U"Please ") && ! str32str (Melder_getError (), U"You could ") &&
  479. ! str32str (Melder_getError (), U"You interrupted ") && ! str32str (Melder_getError (), U"Interrupted!"))
  480. {
  481. /*
  482. Otherwise, show a generic message.
  483. */
  484. if (str32str (Melder_getError (), U"Selection changed!")) {
  485. Melder_appendError (U"Please change the selection in the object list, or click Cancel in the command window “",
  486. my name.get(), U"”.");
  487. } else {
  488. Melder_appendError (U"Please change something in the command window “",
  489. my name.get(), U"”, or click Cancel in that window.");
  490. }
  491. }
  492. Melder_flushError ();
  493. }
  494. if (my okButton) GuiThing_setSensitive (my okButton, true);
  495. for (int i = 1; i <= my numberOfContinueButtons; i ++)
  496. if (my continueButtons [i])
  497. GuiThing_setSensitive (my continueButtons [i], true);
  498. if (my applyButton) GuiThing_setSensitive (my applyButton, true);
  499. if (my cancelButton) GuiThing_setSensitive (my cancelButton, true);
  500. if (my revertButton) GuiThing_setSensitive (my revertButton, true);
  501. if (my helpButton) GuiThing_setSensitive (my helpButton, true);
  502. }
  503. static void gui_button_cb_ok (UiForm me, GuiButtonEvent event) {
  504. UiForm_okOrApply (me, event -> button, true);
  505. }
  506. static void gui_button_cb_apply (UiForm me, GuiButtonEvent event) {
  507. UiForm_okOrApply (me, event -> button, false);
  508. }
  509. static void gui_button_cb_help (UiForm me, GuiButtonEvent /* event */) {
  510. Melder_help (my helpTitle.get());
  511. }
  512. autoUiForm UiForm_create (GuiWindow parent, conststring32 title,
  513. UiCallback okCallback, void *buttonClosure,
  514. conststring32 invokingButtonTitle, conststring32 helpTitle)
  515. {
  516. autoUiForm me = Thing_new (UiForm);
  517. my d_dialogParent = parent;
  518. Thing_setName (me.get(), title);
  519. my okCallback = okCallback;
  520. my buttonClosure = buttonClosure;
  521. my invokingButtonTitle = Melder_dup (invokingButtonTitle);
  522. my helpTitle = Melder_dup (helpTitle);
  523. return me;
  524. }
  525. void UiForm_setPauseForm (UiForm me,
  526. int numberOfContinueButtons, int defaultContinueButton, int cancelContinueButton,
  527. conststring32 continue1, conststring32 continue2, conststring32 continue3,
  528. conststring32 continue4, conststring32 continue5, conststring32 continue6,
  529. conststring32 continue7, conststring32 continue8, conststring32 continue9,
  530. conststring32 continue10,
  531. void (*cancelCallback) (UiForm dia, void *closure))
  532. {
  533. my isPauseForm = true;
  534. my numberOfContinueButtons = numberOfContinueButtons;
  535. my defaultContinueButton = defaultContinueButton;
  536. my cancelContinueButton = cancelContinueButton;
  537. my continueTexts [1] = continue1;
  538. my continueTexts [2] = continue2;
  539. my continueTexts [3] = continue3;
  540. my continueTexts [4] = continue4;
  541. my continueTexts [5] = continue5;
  542. my continueTexts [6] = continue6;
  543. my continueTexts [7] = continue7;
  544. my continueTexts [8] = continue8;
  545. my continueTexts [9] = continue9;
  546. my continueTexts [10] = continue10;
  547. my cancelCallback = cancelCallback;
  548. }
  549. static void commonOkCallback (UiForm /* dia */, integer /* narg */, Stackel /* args */, conststring32 /* sendingString */,
  550. Interpreter interpreter, conststring32 /* invokingButtonTitle */, bool /* modified */, void *closure)
  551. {
  552. EditorCommand cmd = (EditorCommand) closure;
  553. cmd -> commandCallback (cmd -> d_editor, cmd, cmd -> d_uiform.get(), 0, nullptr, nullptr, interpreter);
  554. }
  555. autoUiForm UiForm_createE (EditorCommand cmd, conststring32 title, conststring32 invokingButtonTitle, conststring32 helpTitle) {
  556. Editor editor = cmd -> d_editor;
  557. autoUiForm dia (UiForm_create (editor -> windowForm, title, commonOkCallback, cmd, invokingButtonTitle, helpTitle));
  558. dia -> command = cmd;
  559. return dia;
  560. }
  561. static UiField UiForm_addField (UiForm me, _kUiField_type type, conststring32 label) {
  562. if (my numberOfFields == MAXIMUM_NUMBER_OF_FIELDS)
  563. Melder_throw (U"Cannot have more than ", MAXIMUM_NUMBER_OF_FIELDS, U"fields in a form.");
  564. my field [++ my numberOfFields] = UiField_create (type, label);
  565. return my field [my numberOfFields].get();
  566. }
  567. UiField UiForm_addReal (UiForm me, double *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue) {
  568. UiField thee = UiForm_addField (me, _kUiField_type::REAL_, label);
  569. thy stringDefaultValue = Melder_dup (defaultValue);
  570. thy realVariable = variable;
  571. thy variableName = variableName;
  572. return thee;
  573. }
  574. UiField UiForm_addRealOrUndefined (UiForm me, double *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue) {
  575. UiField thee = UiForm_addField (me, _kUiField_type::REAL_OR_UNDEFINED_, label);
  576. thy stringDefaultValue = Melder_dup (defaultValue);
  577. thy realVariable = variable;
  578. thy variableName = variableName;
  579. return thee;
  580. }
  581. UiField UiForm_addPositive (UiForm me, double *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue) {
  582. UiField thee = UiForm_addField (me, _kUiField_type::POSITIVE_, label);
  583. thy stringDefaultValue = Melder_dup (defaultValue);
  584. thy realVariable = variable;
  585. thy variableName = variableName;
  586. return thee;
  587. }
  588. UiField UiForm_addInteger (UiForm me, integer *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue) {
  589. UiField thee = UiForm_addField (me, _kUiField_type::INTEGER_, label);
  590. thy stringDefaultValue = Melder_dup (defaultValue);
  591. thy integerVariable = variable;
  592. thy variableName = variableName;
  593. return thee;
  594. }
  595. UiField UiForm_addNatural (UiForm me, integer *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue) {
  596. UiField thee = UiForm_addField (me, _kUiField_type::NATURAL_, label);
  597. thy stringDefaultValue = Melder_dup (defaultValue);
  598. thy integerVariable = variable;
  599. thy variableName = variableName;
  600. return thee;
  601. }
  602. UiField UiForm_addWord (UiForm me, conststring32 *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue) {
  603. UiField thee = UiForm_addField (me, _kUiField_type::WORD_, label);
  604. thy stringDefaultValue = Melder_dup (defaultValue);
  605. thy stringVariable = variable;
  606. thy variableName = variableName;
  607. return thee;
  608. }
  609. UiField UiForm_addSentence (UiForm me, conststring32 *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue) {
  610. UiField thee = UiForm_addField (me, _kUiField_type::SENTENCE_, label);
  611. thy stringDefaultValue = Melder_dup (defaultValue);
  612. thy stringVariable = variable;
  613. thy variableName = variableName;
  614. return thee;
  615. }
  616. UiField UiForm_addLabel (UiForm me, conststring32 *variable, conststring32 label) {
  617. UiField thee = UiForm_addField (me, _kUiField_type::LABEL_, U""); // this field gets no name; so that the user can give it any title
  618. thy stringVariable = variable;
  619. thy stringValue = Melder_dup (label);
  620. return thee;
  621. }
  622. UiField UiForm_addBoolean (UiForm me, bool *variable, conststring32 variableName, conststring32 label, bool defaultValue) {
  623. UiField thee = UiForm_addField (me, _kUiField_type::BOOLEAN_, label);
  624. thy integerDefaultValue = defaultValue;
  625. thy boolVariable = variable;
  626. thy variableName = variableName;
  627. return thee;
  628. }
  629. UiField UiForm_addText (UiForm me, conststring32 *variable, conststring32 variableName, conststring32 name, conststring32 defaultValue) {
  630. UiField thee = UiForm_addField (me, _kUiField_type::TEXT_, name);
  631. thy stringDefaultValue = Melder_dup (defaultValue);
  632. thy stringVariable = variable;
  633. thy variableName = variableName;
  634. return thee;
  635. }
  636. UiField UiForm_addNumvec (UiForm me, constVEC *variable, conststring32 variableName, conststring32 name, conststring32 defaultValue) {
  637. UiField thee = UiForm_addField (me, _kUiField_type::NUMVEC_, name);
  638. thy stringDefaultValue = Melder_dup (defaultValue);
  639. thy numericVectorVariable = variable;
  640. thy variableName = variableName;
  641. return thee;
  642. }
  643. UiField UiForm_addNummat (UiForm me, constMAT *variable, conststring32 variableName, conststring32 name, conststring32 defaultValue) {
  644. UiField thee = UiForm_addField (me, _kUiField_type::NUMMAT_, name);
  645. thy stringDefaultValue = Melder_dup (defaultValue);
  646. thy numericMatrixVariable = variable;
  647. thy variableName = variableName;
  648. return thee;
  649. }
  650. UiField UiForm_addRadio (UiForm me, int *intVariable, conststring32 *stringVariable, conststring32 variableName, conststring32 label, int defaultValue, int base) {
  651. UiField thee = UiForm_addField (me, _kUiField_type::RADIO_, label);
  652. thy integerDefaultValue = defaultValue;
  653. thy intVariable = intVariable;
  654. thy stringVariable = stringVariable;
  655. thy variableName = variableName;
  656. thy subtract = ( base == 1 ? 0 : 1 );
  657. return thee;
  658. }
  659. UiField UiForm_addOptionMenu (UiForm me, int *intVariable, conststring32 *stringVariable, conststring32 variableName, conststring32 label, int defaultValue, int base) {
  660. UiField thee = UiForm_addField (me, _kUiField_type::OPTIONMENU_, label);
  661. thy integerDefaultValue = defaultValue;
  662. thy intVariable = intVariable;
  663. thy stringVariable = stringVariable;
  664. thy variableName = variableName;
  665. thy subtract = ( base == 1 ? 0 : 1 );
  666. return thee;
  667. }
  668. UiField UiForm_addList (UiForm me, integer *integerVariable, conststring32 *stringVariable, conststring32 variableName, conststring32 label, conststring32vector strings, integer defaultValue) {
  669. UiField thee = UiForm_addField (me, _kUiField_type::LIST_, label);
  670. thy strings = strings;
  671. thy integerDefaultValue = defaultValue;
  672. thy integerVariable = integerVariable;
  673. thy stringVariable = stringVariable;
  674. thy variableName = variableName;
  675. return thee;
  676. }
  677. UiField UiForm_addColour (UiForm me, Graphics_Colour *colourVariable, conststring32 variableName, conststring32 label, conststring32 defaultValue) {
  678. UiField thee = UiForm_addField (me, _kUiField_type::COLOUR_, label);
  679. thy stringDefaultValue = Melder_dup (defaultValue);
  680. thy colourVariable = colourVariable;
  681. thy variableName = variableName;
  682. return thee;
  683. }
  684. UiField UiForm_addChannel (UiForm me, integer *variable, conststring32 variableName, conststring32 label, conststring32 defaultValue) {
  685. UiField thee = UiForm_addField (me, _kUiField_type::CHANNEL_, label);
  686. thy stringDefaultValue = Melder_dup (defaultValue);
  687. thy integerVariable = variable;
  688. thy variableName = variableName;
  689. return thee;
  690. }
  691. #define DIALOG_X 150
  692. #define DIALOG_Y 70
  693. #define HELP_BUTTON_WIDTH 60
  694. #define STANDARDS_BUTTON_WIDTH 100
  695. #define REVERT_BUTTON_WIDTH 60
  696. #define STOP_BUTTON_WIDTH 50
  697. #define HELP_BUTTON_X 20
  698. #define LIST_HEIGHT 192
  699. static MelderString theFinishBuffer { };
  700. static void appendColon () {
  701. integer length = theFinishBuffer.length;
  702. if (length < 1) return;
  703. char32 lastCharacter = theFinishBuffer.string [length - 1];
  704. if (lastCharacter == U':' || lastCharacter == U'?' || lastCharacter == U'.') return;
  705. MelderString_appendCharacter (& theFinishBuffer, U':');
  706. }
  707. void UiForm_finish (UiForm me) {
  708. if (! my d_dialogParent && ! my isPauseForm) return;
  709. int size = my numberOfFields;
  710. int dialogHeight = 0, x = Gui_LEFT_DIALOG_SPACING, y;
  711. int textFieldHeight = Gui_TEXTFIELD_HEIGHT;
  712. int dialogWidth = 520, dialogCentre = dialogWidth / 2, fieldX = dialogCentre + Gui_LABEL_SPACING / 2;
  713. int labelWidth = fieldX - Gui_LABEL_SPACING - x, fieldWidth = labelWidth, halfFieldWidth = fieldWidth / 2 - 6;
  714. GuiForm form;
  715. /*
  716. Compute height. Cannot leave this to the default geometry management system.
  717. */
  718. for (integer ifield = 1; ifield <= my numberOfFields; ifield ++ ) {
  719. UiField thee = my field [ifield].get(), previous = my field [ifield - 1].get();
  720. dialogHeight +=
  721. ifield == 1 ? Gui_TOP_DIALOG_SPACING :
  722. thy type == _kUiField_type::RADIO_ || previous -> type == _kUiField_type::RADIO_ ? Gui_VERTICAL_DIALOG_SPACING_DIFFERENT :
  723. thy type >= _kUiField_type::LABELLED_TEXT_MIN_ && thy type <= _kUiField_type::LABELLED_TEXT_MAX_ && str32nequ (thy name.get(), U"right ", 6) &&
  724. previous -> type >= _kUiField_type::LABELLED_TEXT_MIN_ && previous -> type <= _kUiField_type::LABELLED_TEXT_MAX_ &&
  725. str32nequ (previous -> name.get(), U"left ", 5) ? - textFieldHeight : Gui_VERTICAL_DIALOG_SPACING_SAME;
  726. thy y = dialogHeight;
  727. dialogHeight +=
  728. thy type == _kUiField_type::BOOLEAN_ ? Gui_CHECKBUTTON_HEIGHT :
  729. thy type == _kUiField_type::RADIO_ ? thy options.size * Gui_RADIOBUTTON_HEIGHT +
  730. (thy options.size - 1) * Gui_RADIOBUTTON_SPACING :
  731. thy type == _kUiField_type::OPTIONMENU_ ? Gui_OPTIONMENU_HEIGHT :
  732. thy type == _kUiField_type::LIST_ ? LIST_HEIGHT :
  733. thy type == _kUiField_type::LABEL_ && thy stringValue [0] != U'\0' && thy stringValue [str32len (thy stringValue.get()) - 1] != U'.' &&
  734. ifield != my numberOfFields ? textFieldHeight
  735. #ifdef _WIN32
  736. - 6 :
  737. #else
  738. - 10 :
  739. #endif
  740. textFieldHeight;
  741. }
  742. dialogHeight += 2 * Gui_BOTTOM_DIALOG_SPACING + Gui_PUSHBUTTON_HEIGHT;
  743. my d_dialogForm = GuiDialog_create (my d_dialogParent, DIALOG_X, DIALOG_Y, dialogWidth, dialogHeight, my name.get(), gui_dialog_cb_close, me, 0);
  744. form = my d_dialogForm;
  745. for (integer ifield = 1; ifield <= size; ifield ++) {
  746. UiField field = my field [ifield].get();
  747. y = field -> y;
  748. switch (field -> type)
  749. {
  750. case _kUiField_type::REAL_:
  751. case _kUiField_type::REAL_OR_UNDEFINED_:
  752. case _kUiField_type::POSITIVE_:
  753. case _kUiField_type::INTEGER_:
  754. case _kUiField_type::NATURAL_:
  755. case _kUiField_type::WORD_:
  756. case _kUiField_type::SENTENCE_:
  757. case _kUiField_type::COLOUR_:
  758. case _kUiField_type::CHANNEL_:
  759. {
  760. int ylabel = y;
  761. #if defined (macintosh)
  762. ylabel += 3;
  763. #endif
  764. if (str32nequ (field -> name.get(), U"left ", 5)) {
  765. MelderString_copy (& theFinishBuffer, field -> formLabel.get() + 5);
  766. appendColon ();
  767. field -> label = GuiLabel_createShown (form, 0, x + labelWidth, ylabel, ylabel + textFieldHeight,
  768. theFinishBuffer.string, GuiLabel_RIGHT);
  769. field -> text = GuiText_createShown (form, fieldX, fieldX + halfFieldWidth, y, y + Gui_TEXTFIELD_HEIGHT, 0);
  770. } else if (str32nequ (field -> name.get(), U"right ", 6)) {
  771. field -> text = GuiText_createShown (form, fieldX + halfFieldWidth + 12, fieldX + fieldWidth,
  772. y, y + Gui_TEXTFIELD_HEIGHT, 0);
  773. } else {
  774. MelderString_copy (& theFinishBuffer, field -> formLabel.get());
  775. appendColon ();
  776. field -> label = GuiLabel_createShown (form, 0, x + labelWidth,
  777. ylabel, ylabel + textFieldHeight,
  778. theFinishBuffer.string, GuiLabel_RIGHT);
  779. field -> text = GuiText_createShown (form, fieldX, fieldX + fieldWidth, // or once the dialog is a Form: - Gui_RIGHT_DIALOG_SPACING,
  780. y, y + Gui_TEXTFIELD_HEIGHT, 0);
  781. }
  782. }
  783. break;
  784. case _kUiField_type::TEXT_:
  785. case _kUiField_type::NUMVEC_:
  786. case _kUiField_type::NUMMAT_:
  787. {
  788. field -> text = GuiText_createShown (form, x, x + dialogWidth - Gui_LEFT_DIALOG_SPACING - Gui_RIGHT_DIALOG_SPACING,
  789. y, y + Gui_TEXTFIELD_HEIGHT, 0);
  790. }
  791. break;
  792. case _kUiField_type::LABEL_:
  793. {
  794. MelderString_copy (& theFinishBuffer, field -> stringValue.get());
  795. field -> label = GuiLabel_createShown (form,
  796. x, dialogWidth /* allow to extend into the margin */, y + 5, y + 5 + textFieldHeight,
  797. theFinishBuffer.string, 0);
  798. }
  799. break;
  800. case _kUiField_type::RADIO_:
  801. {
  802. int ylabel = y;
  803. #if defined (macintosh)
  804. ylabel += 1;
  805. #endif
  806. MelderString_copy (& theFinishBuffer, field -> formLabel.get());
  807. appendColon ();
  808. field -> label = GuiLabel_createShown (form, x, x + labelWidth, ylabel, ylabel + Gui_RADIOBUTTON_HEIGHT,
  809. theFinishBuffer.string, GuiLabel_RIGHT);
  810. GuiRadioGroup_begin ();
  811. for (integer ibutton = 1; ibutton <= field -> options.size; ibutton ++) {
  812. UiOption button = field -> options.at [ibutton];
  813. MelderString_copy (& theFinishBuffer, button -> name.get());
  814. button -> radioButton = GuiRadioButton_createShown (form,
  815. fieldX, dialogWidth /* allow to extend into the margin */,
  816. y + (ibutton - 1) * (Gui_RADIOBUTTON_HEIGHT + Gui_RADIOBUTTON_SPACING),
  817. y + (ibutton - 1) * (Gui_RADIOBUTTON_HEIGHT + Gui_RADIOBUTTON_SPACING) + Gui_RADIOBUTTON_HEIGHT,
  818. theFinishBuffer.string, nullptr, nullptr, 0);
  819. }
  820. GuiRadioGroup_end ();
  821. }
  822. break;
  823. case _kUiField_type::OPTIONMENU_:
  824. {
  825. int ylabel = y;
  826. #if defined (macintosh)
  827. ylabel += 2;
  828. #endif
  829. MelderString_copy (& theFinishBuffer, field -> formLabel.get());
  830. appendColon ();
  831. field -> label = GuiLabel_createShown (form, x, x + labelWidth, ylabel, ylabel + Gui_OPTIONMENU_HEIGHT,
  832. theFinishBuffer.string, GuiLabel_RIGHT);
  833. field -> optionMenu = GuiOptionMenu_createShown (form, fieldX, fieldX + fieldWidth, y, y + Gui_OPTIONMENU_HEIGHT, 0);
  834. for (integer ibutton = 1; ibutton <= field -> options.size; ibutton ++) {
  835. UiOption button = field -> options.at [ibutton];
  836. MelderString_copy (& theFinishBuffer, button -> name.get());
  837. GuiOptionMenu_addOption (field -> optionMenu, theFinishBuffer.string);
  838. }
  839. }
  840. break;
  841. case _kUiField_type::BOOLEAN_:
  842. {
  843. MelderString_copy (& theFinishBuffer, field -> formLabel.get());
  844. /*field -> label = GuiLabel_createShown (form, x, x + labelWidth, y, y + Gui_CHECKBUTTON_HEIGHT,
  845. theFinishBuffer.string, GuiLabel_RIGHT); */
  846. field -> checkButton = GuiCheckButton_createShown (form,
  847. fieldX, dialogWidth /* allow to extend into the margin */, y, y + Gui_CHECKBUTTON_HEIGHT,
  848. theFinishBuffer.string, nullptr, nullptr, 0);
  849. }
  850. break;
  851. case _kUiField_type::LIST_:
  852. {
  853. int listWidth = my numberOfFields == 1 ? dialogWidth - fieldX : fieldWidth;
  854. MelderString_copy (& theFinishBuffer, field -> formLabel.get());
  855. appendColon ();
  856. field -> label = GuiLabel_createShown (form, x, x + labelWidth, y + 1, y + 21,
  857. theFinishBuffer.string, GuiLabel_RIGHT);
  858. field -> list = GuiList_create (form, fieldX, fieldX + listWidth, y, y + LIST_HEIGHT, false, theFinishBuffer.string);
  859. for (integer i = 1; i <= field -> strings.size; i ++) {
  860. GuiList_insertItem (field -> list, field -> strings [i], 0);
  861. }
  862. GuiThing_show (field -> list);
  863. }
  864. break;
  865. }
  866. }
  867. for (integer ifield = 1; ifield <= my numberOfFields; ifield ++)
  868. UiField_setDefault (my field [ifield].get());
  869. /*separator = XmCreateSeparatorGadget (column, "separator", nullptr, 0);*/
  870. y = dialogHeight - Gui_BOTTOM_DIALOG_SPACING - Gui_PUSHBUTTON_HEIGHT;
  871. if (my helpTitle) {
  872. my helpButton = GuiButton_createShown (form, HELP_BUTTON_X, HELP_BUTTON_X + HELP_BUTTON_WIDTH, y, y + Gui_PUSHBUTTON_HEIGHT,
  873. U"Help", gui_button_cb_help, me, 0);
  874. }
  875. bool commentsOnly = true;
  876. for (integer ifield = 1; ifield <= my numberOfFields; ifield ++) {
  877. if (my field [ifield] -> type != _kUiField_type::LABEL_) {
  878. commentsOnly = false;
  879. break;
  880. }
  881. }
  882. if (! commentsOnly) {
  883. if (my isPauseForm) {
  884. my revertButton = GuiButton_createShown (form,
  885. HELP_BUTTON_X, HELP_BUTTON_X + REVERT_BUTTON_WIDTH,
  886. y, y + Gui_PUSHBUTTON_HEIGHT, U"Revert", gui_button_cb_revert, me, 0);
  887. } else {
  888. my revertButton = GuiButton_createShown (form,
  889. HELP_BUTTON_X + HELP_BUTTON_WIDTH + Gui_HORIZONTAL_DIALOG_SPACING,
  890. HELP_BUTTON_X + HELP_BUTTON_WIDTH + Gui_HORIZONTAL_DIALOG_SPACING + STANDARDS_BUTTON_WIDTH,
  891. y, y + Gui_PUSHBUTTON_HEIGHT, U"Standards", gui_button_cb_revert, me, 0);
  892. }
  893. }
  894. if (my isPauseForm) {
  895. x = HELP_BUTTON_X + REVERT_BUTTON_WIDTH + Gui_HORIZONTAL_DIALOG_SPACING;
  896. if (my cancelContinueButton == 0) {
  897. my cancelButton = GuiButton_createShown (form, x, x + STOP_BUTTON_WIDTH, y, y + Gui_PUSHBUTTON_HEIGHT,
  898. U"Stop", gui_button_cb_cancel, me, GuiButton_CANCEL);
  899. x += STOP_BUTTON_WIDTH + 7;
  900. } else {
  901. x += 30;
  902. }
  903. int room = dialogWidth - Gui_RIGHT_DIALOG_SPACING - x;
  904. int roomPerContinueButton = room / my numberOfContinueButtons;
  905. int horizontalSpacing = my numberOfContinueButtons > 7 ? Gui_HORIZONTAL_DIALOG_SPACING - 2 * (my numberOfContinueButtons - 7) : Gui_HORIZONTAL_DIALOG_SPACING;
  906. int continueButtonWidth = roomPerContinueButton - horizontalSpacing;
  907. for (int i = 1; i <= my numberOfContinueButtons; i ++) {
  908. x = dialogWidth - Gui_RIGHT_DIALOG_SPACING - roomPerContinueButton * (my numberOfContinueButtons - i + 1) + horizontalSpacing;
  909. my continueButtons [i] = GuiButton_createShown (form, x, x + continueButtonWidth, y, y + Gui_PUSHBUTTON_HEIGHT,
  910. my continueTexts [i], gui_button_cb_ok, me, i == my defaultContinueButton ? GuiButton_DEFAULT : 0);
  911. }
  912. } else {
  913. x = dialogWidth - Gui_RIGHT_DIALOG_SPACING - Gui_OK_BUTTON_WIDTH - 2 * Gui_HORIZONTAL_DIALOG_SPACING
  914. - Gui_APPLY_BUTTON_WIDTH - Gui_CANCEL_BUTTON_WIDTH;
  915. my cancelButton = GuiButton_createShown (form, x, x + Gui_CANCEL_BUTTON_WIDTH, y, y + Gui_PUSHBUTTON_HEIGHT,
  916. U"Cancel", gui_button_cb_cancel, me, GuiButton_CANCEL);
  917. x = dialogWidth - Gui_RIGHT_DIALOG_SPACING - Gui_OK_BUTTON_WIDTH - Gui_HORIZONTAL_DIALOG_SPACING - Gui_APPLY_BUTTON_WIDTH;
  918. if (my numberOfFields > 1 || my field [1] -> type != _kUiField_type::LABEL_) {
  919. my applyButton = GuiButton_createShown (form, x, x + Gui_APPLY_BUTTON_WIDTH, y, y + Gui_PUSHBUTTON_HEIGHT,
  920. U"Apply", gui_button_cb_apply, me, 0);
  921. }
  922. x = dialogWidth - Gui_RIGHT_DIALOG_SPACING - Gui_OK_BUTTON_WIDTH;
  923. my okButton = GuiButton_createShown (form, x, x + Gui_OK_BUTTON_WIDTH, y, y + Gui_PUSHBUTTON_HEIGHT,
  924. my isPauseForm ? U"Continue" : U"OK", gui_button_cb_ok, me, GuiButton_DEFAULT);
  925. }
  926. /*GuiObject_show (separator);*/
  927. }
  928. void UiForm_destroyWhenUnmanaged (UiForm me) {
  929. my destroyWhenUnmanaged = true;
  930. }
  931. void UiForm_do (UiForm me, bool modified) {
  932. my allowExecutionHook = theAllowExecutionHookHint;
  933. my allowExecutionClosure = theAllowExecutionClosureHint;
  934. Melder_assert (my d_dialogForm);
  935. GuiThing_show (my d_dialogForm);
  936. if (modified)
  937. UiForm_okOrApply (me, nullptr, true);
  938. }
  939. static void UiField_api_header_C (UiField me, UiField next, bool isLastNonLabelField) {
  940. if (my type == _kUiField_type::LABEL_) {
  941. bool weAreFollowedByAWideField =
  942. next && (next -> type == _kUiField_type::TEXT_ || next -> type == _kUiField_type::NUMVEC_ || next -> type == _kUiField_type::NUMMAT_);
  943. bool weLabelTheFollowingField =
  944. weAreFollowedByAWideField &&
  945. Melder_stringMatchesCriterion (my stringValue.get(), kMelder_string::ENDS_WITH, U":", true);
  946. bool weAreAComment = ! weLabelTheFollowingField;
  947. if (weAreAComment) {
  948. MelderInfo_writeLine (U"\t/* ", my stringValue.get(), U" */");
  949. }
  950. return;
  951. }
  952. /*
  953. Write the type of the field.
  954. */
  955. bool isText = false, isBoolean = false, isEnum = false, isPositive = false;
  956. switch (my type)
  957. {
  958. case _kUiField_type::REAL_:
  959. case _kUiField_type::REAL_OR_UNDEFINED_:
  960. case _kUiField_type::POSITIVE_:
  961. {
  962. MelderInfo_write (U"\tdouble ");
  963. isPositive = ( my type == _kUiField_type::POSITIVE_);
  964. }
  965. break;
  966. case _kUiField_type::INTEGER_:
  967. case _kUiField_type::NATURAL_:
  968. case _kUiField_type::CHANNEL_:
  969. {
  970. MelderInfo_write (U"\tint64_t ");
  971. isPositive = ( my type == _kUiField_type::NATURAL_);
  972. }
  973. break;
  974. case _kUiField_type::WORD_:
  975. case _kUiField_type::SENTENCE_:
  976. case _kUiField_type::TEXT_:
  977. case _kUiField_type::COLOUR_:
  978. case _kUiField_type::LIST_:
  979. {
  980. MelderInfo_write (U"\tconst char *");
  981. isText = true;
  982. }
  983. break;
  984. case _kUiField_type::RADIO_:
  985. case _kUiField_type::OPTIONMENU_:
  986. {
  987. MelderInfo_write (U"\tconst char *");
  988. isText = true;
  989. isEnum = true;
  990. }
  991. break;
  992. case _kUiField_type::BOOLEAN_:
  993. {
  994. MelderInfo_write (U"\tint32_t ");
  995. isBoolean = true;
  996. }
  997. break;
  998. default:
  999. {
  1000. }
  1001. }
  1002. /*
  1003. Write the title of the field.
  1004. */
  1005. char32 cName [100], *q = & cName [0];
  1006. Melder_assert (my formLabel);
  1007. const char32 *p = & my formLabel [0];
  1008. *q ++ = Melder_toLowerCase (*p ++);
  1009. bool up = false;
  1010. for (; *p != U'\0'; p ++) {
  1011. if (*p == U'(') {
  1012. break;
  1013. } else if (*p == U'\'') {
  1014. continue;
  1015. } else if (*p == U' ' || *p == U'-') {
  1016. if (p [1] == U'(') { p ++; break; }
  1017. up = true;
  1018. } else if (*p == U'*') {
  1019. *q ++ = U'S';
  1020. *q ++ = U't';
  1021. *q ++ = U'a';
  1022. *q ++ = U'r';
  1023. } else if (up) {
  1024. *q ++ = Melder_toUpperCase (*p);
  1025. up = false;
  1026. } else {
  1027. *q ++ = *p;
  1028. }
  1029. }
  1030. *q = U'\0';
  1031. if (! my variableName)
  1032. Melder_warning (U"Missing variable name for field label: ", my formLabel.get());
  1033. MelderInfo_write (my variableName ? my variableName : cName);
  1034. if (! isLastNonLabelField) MelderInfo_write (U",");
  1035. /*
  1036. Get the units.
  1037. */
  1038. char32 units [100];
  1039. q = & units [0];
  1040. if (*p == U'(') {
  1041. for (p ++; *p != U'\0'; p ++) {
  1042. if (*p == U')') {
  1043. break;
  1044. } else {
  1045. *q ++ = *p;
  1046. }
  1047. }
  1048. }
  1049. *q = U'\0';
  1050. bool unitsAreAvailable = ( units [0] != U'\0' );
  1051. bool unitsContainRange = str32str (units, U"-");
  1052. /*
  1053. Get the example.
  1054. */
  1055. conststring32 example = my stringDefaultValue.get(); // BUG dangle
  1056. bool exampleIsAvailable = ( example && example [0] != U'\0' );
  1057. if (exampleIsAvailable) {
  1058. /*
  1059. Split up the default string.
  1060. */
  1061. char32 defaultValue [100], defaultComment [100];
  1062. str32cpy (defaultValue, my stringDefaultValue.get());
  1063. str32cpy (defaultComment, U"");
  1064. if (unitsAreAvailable) {
  1065. char32 *parenthesis = str32chr (defaultValue, U'(');
  1066. if (parenthesis && parenthesis - defaultValue > 1) {
  1067. parenthesis [-1] = U'\0';
  1068. str32cpy (defaultComment, parenthesis);
  1069. }
  1070. }
  1071. MelderInfo_write (U" // ");
  1072. if (isPositive) {
  1073. MelderInfo_write (U"positive, ");
  1074. }
  1075. if (unitsContainRange) {
  1076. MelderInfo_write (units, U", ");
  1077. }
  1078. MelderInfo_write (U"e.g. ");
  1079. if (isText) MelderInfo_write (U"\"");
  1080. MelderInfo_write (defaultValue);
  1081. if (isText) MelderInfo_write (U"\"");
  1082. if (unitsAreAvailable && ! unitsContainRange) {
  1083. MelderInfo_write (U" ", units);
  1084. }
  1085. if (defaultComment [0]) {
  1086. MelderInfo_write (U" ", defaultComment);
  1087. }
  1088. } else if (isBoolean) {
  1089. MelderInfo_write (U" // boolean, e.g. ");
  1090. MelderInfo_write (my integerDefaultValue, my integerDefaultValue ? U" (true)" : U" (false)");
  1091. } else if (isEnum) {
  1092. MelderInfo_write (U" // e.g. \"");
  1093. MelderInfo_write (my options.at [my integerDefaultValue] -> name.get());
  1094. MelderInfo_write (U"\"; other choice", ( my options.size > 2 ? U"s" : U"" ), U":");
  1095. bool firstWritten = false;
  1096. for (int i = 1; i <= my options.size; i ++) {
  1097. if (i == my integerDefaultValue) continue;
  1098. if (firstWritten) MelderInfo_write (U",");
  1099. MelderInfo_write (U" \"", my options.at [i] -> name.get(), U"\"");
  1100. firstWritten = true;
  1101. }
  1102. }
  1103. MelderInfo_writeLine (U"");
  1104. }
  1105. void UiForm_info (UiForm me, integer narg) {
  1106. if (narg == -1) {
  1107. /*
  1108. The C interface.
  1109. */
  1110. int lastNonLabelFieldNumber = 0;
  1111. for (int ifield = my numberOfFields; ifield > 0; ifield --) {
  1112. if (my field [ifield] -> type != _kUiField_type::LABEL_) {
  1113. lastNonLabelFieldNumber = ifield;
  1114. break;
  1115. }
  1116. }
  1117. for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
  1118. UiField_api_header_C (my field [ifield].get(), ifield == my numberOfFields ? nullptr : my field [ifield + 1].get(), ifield == lastNonLabelFieldNumber);
  1119. }
  1120. }
  1121. }
  1122. static void UiField_argToValue (UiField me, Stackel arg, Interpreter /* interpreter */) {
  1123. switch (my type)
  1124. {
  1125. case _kUiField_type::REAL_:
  1126. case _kUiField_type::REAL_OR_UNDEFINED_:
  1127. case _kUiField_type::POSITIVE_:
  1128. {
  1129. if (arg -> which != Stackel_NUMBER)
  1130. Melder_throw (U"Argument \"", my name.get(), U"\" should be a number, not ", arg -> whichText(), U".");
  1131. my realValue = arg -> number;
  1132. if (isundef (my realValue) && my type != _kUiField_type::REAL_OR_UNDEFINED_)
  1133. Melder_throw (U"Argument \"", my name.get(), U"\" has the value \"undefined\".");
  1134. if (my type == _kUiField_type::POSITIVE_ && my realValue <= 0.0)
  1135. Melder_throw (U"Argument \"", my name.get(), U"\" must be greater than 0.");
  1136. if (my realVariable)
  1137. *my realVariable = my realValue;
  1138. }
  1139. break;
  1140. case _kUiField_type::INTEGER_:
  1141. case _kUiField_type::NATURAL_:
  1142. case _kUiField_type::CHANNEL_:
  1143. {
  1144. if (arg -> which == Stackel_STRING) {
  1145. if (my type == _kUiField_type::CHANNEL_) {
  1146. if (str32equ (arg -> getString(), U"All") || str32equ (arg -> getString(), U"Average")) {
  1147. my integerValue = 0;
  1148. } else if (str32equ (arg -> getString(), U"Left") || str32equ (arg -> getString(), U"Mono")) {
  1149. my integerValue = 1;
  1150. } else if (str32equ (arg -> getString(), U"Right") || str32equ (arg -> getString(), U"Stereo")) {
  1151. my integerValue = 2;
  1152. } else {
  1153. Melder_throw (U"Channel argument \"", my name.get(),
  1154. U"\" can only be a number or one of the strings \"All\", \"Average\", \"Left\", \"Right\", \"Mono\" or \"Stereo\".");
  1155. }
  1156. } else {
  1157. Melder_throw (U"Argument \"", my name.get(), U"\" should be a number, not ", arg -> whichText(), U".");
  1158. }
  1159. } else if (arg -> which == Stackel_NUMBER) {
  1160. my integerValue = Melder_iround (arg -> number);
  1161. if (my type == _kUiField_type::NATURAL_ && my integerValue < 1)
  1162. Melder_throw (U"Argument \"", my name.get(), U"\" should be a positive whole number.");
  1163. } else {
  1164. Melder_throw (U"Argument \"", my name.get(), U"\" should be a number, not ", arg -> whichText(), U".");
  1165. }
  1166. if (my integerVariable)
  1167. *my integerVariable = my integerValue;
  1168. }
  1169. break;
  1170. case _kUiField_type::WORD_:
  1171. case _kUiField_type::SENTENCE_:
  1172. case _kUiField_type::TEXT_:
  1173. {
  1174. if (arg -> which != Stackel_STRING)
  1175. Melder_throw (U"Argument \"", my name.get(), U"\" should be a string, not ", arg -> whichText(), U".");
  1176. my stringValue = Melder_dup (arg -> getString());
  1177. if (my stringVariable)
  1178. *my stringVariable = my stringValue.get(); // BUG dangle
  1179. }
  1180. break;
  1181. case _kUiField_type::NUMVEC_:
  1182. {
  1183. if (arg -> which != Stackel_NUMERIC_VECTOR)
  1184. Melder_throw (U"Argument \"", my name.get(), U"\" should be a numeric vector, not ", arg -> whichText(), U".");
  1185. if (arg -> owned) {
  1186. my numericVectorValue. adoptFromAmbiguousOwner (arg -> numericVector);
  1187. arg -> owned = false;
  1188. } else {
  1189. my numericVectorValue = VECcopy (arg -> numericVector);
  1190. }
  1191. if (my numericVectorVariable)
  1192. *my numericVectorVariable = my numericVectorValue.get();
  1193. }
  1194. break;
  1195. case _kUiField_type::NUMMAT_:
  1196. {
  1197. if (arg -> which != Stackel_NUMERIC_MATRIX)
  1198. Melder_throw (U"Argument \"", my name.get(), U"\" should be a numeric matrix, not ", arg -> whichText(), U".");
  1199. if (arg -> owned) {
  1200. my numericMatrixValue. adoptFromAmbiguousOwner (arg -> numericMatrix);
  1201. arg -> owned = false;
  1202. } else {
  1203. my numericMatrixValue = matrixcopy (arg -> numericMatrix);
  1204. }
  1205. if (my numericMatrixVariable)
  1206. *my numericMatrixVariable = my numericMatrixValue.get();
  1207. }
  1208. break;
  1209. case _kUiField_type::BOOLEAN_:
  1210. {
  1211. if (arg -> which == Stackel_STRING) {
  1212. if (str32equ (arg -> getString(), U"no") || str32equ (arg -> getString(), U"off")) {
  1213. my integerValue = 0;
  1214. } else if (str32equ (arg -> getString(), U"yes") || str32equ (arg -> getString(), U"on")) {
  1215. my integerValue = 1;
  1216. } else {
  1217. Melder_throw (U"Boolean argument \"", my name.get(),
  1218. U"\" can only be a number or one of the strings \"yes\" or \"no\".");
  1219. }
  1220. } else if (arg -> which == Stackel_NUMBER) {
  1221. my integerValue = arg -> number == 0.0 ? 0.0 : 1.0;
  1222. } else {
  1223. Melder_throw (U"Boolean argument \"", my name.get(), U"\" should be a number (0 or 1), not ", arg -> whichText(), U".");
  1224. }
  1225. if (my boolVariable)
  1226. *my boolVariable = my integerValue;
  1227. }
  1228. break;
  1229. case _kUiField_type::RADIO_:
  1230. case _kUiField_type::OPTIONMENU_:
  1231. {
  1232. if (arg -> which != Stackel_STRING)
  1233. Melder_throw (U"Option argument \"", my name.get(), U"\" should be a string, not ", arg -> whichText(), U".");
  1234. my integerValue = 0;
  1235. for (int i = 1; i <= my options.size; i ++) {
  1236. UiOption b = my options.at [i];
  1237. if (str32equ (arg -> getString(), b -> name.get()))
  1238. my integerValue = i;
  1239. }
  1240. if (my integerValue == 0) {
  1241. /*
  1242. Retry with different case.
  1243. */
  1244. for (int i = 1; i <= my options.size; i ++) {
  1245. UiOption b = my options.at [i];
  1246. if (Melder_equ_firstCharacterCaseInsensitive (arg -> getString(), b -> name.get()))
  1247. my integerValue = i;
  1248. }
  1249. }
  1250. if (my integerValue == 0) {
  1251. if (my intVariable)
  1252. Melder_throw (U"Option argument \"", my name.get(), U"\" cannot have the value \"", arg -> getString(), U"\".");
  1253. if (my stringVariable) {
  1254. *my stringVariable = arg -> getString();
  1255. return;
  1256. }
  1257. }
  1258. if (my intVariable)
  1259. *my intVariable = my integerValue - my subtract;
  1260. if (my stringVariable)
  1261. *my stringVariable = my options.at [my integerValue] -> name.get();
  1262. }
  1263. break;
  1264. case _kUiField_type::LIST_:
  1265. {
  1266. if (arg -> which != Stackel_STRING)
  1267. Melder_throw (U"List argument \"", my name.get(), U"\" should be a string, not ", arg -> whichText(), U".");
  1268. integer i = 1;
  1269. for (; i <= my strings.size; i ++)
  1270. if (str32equ (arg -> getString(), my strings [i])) break;
  1271. if (i > my strings.size)
  1272. Melder_throw (U"List argument \"", my name.get(), U"\" cannot have the value \"", arg -> getString(), U"\".");
  1273. my integerValue = i;
  1274. if (my integerVariable)
  1275. *my integerVariable = my integerValue;
  1276. if (my stringVariable)
  1277. *my stringVariable = (char32 *) my strings [my integerValue];
  1278. }
  1279. break;
  1280. case _kUiField_type::COLOUR_:
  1281. {
  1282. if (arg -> which == Stackel_NUMBER) {
  1283. if (arg -> number < 0.0 || arg -> number > 1.0)
  1284. Melder_throw (U"Grey colour argument \"", my name.get(), U"\" has to lie between 0.0 and 1.0.");
  1285. my colourValue. red = my colourValue. green = my colourValue. blue = arg -> number;
  1286. } else if (arg -> which == Stackel_STRING) {
  1287. autostring32 string2 = Melder_dup (arg -> getString());
  1288. if (! colourToValue (me, string2.get()))
  1289. Melder_throw (U"Cannot compute a colour from \"", string2.get(), U"\".");
  1290. }
  1291. if (my colourVariable)
  1292. *my colourVariable = my colourValue;
  1293. }
  1294. break;
  1295. default:
  1296. {
  1297. Melder_throw (U"Unknown field type ", (int) my type, U".");
  1298. }
  1299. }
  1300. }
  1301. void UiForm_call (UiForm me, integer narg, Stackel args, Interpreter interpreter) {
  1302. integer size = my numberOfFields, iarg = 0;
  1303. //while (size >= 1 && my field [size] -> type == _kUiField_type::LABEL_)
  1304. // size --; // ignore trailing fields without a value
  1305. for (integer i = 1; i <= size; i ++) {
  1306. if (my field [i] -> type == _kUiField_type::LABEL_)
  1307. continue; // ignore non-trailing fields without a value
  1308. iarg ++;
  1309. if (iarg > narg)
  1310. Melder_throw (U"Command requires more than the given ", narg, U" arguments: no value for argument \"", my field [i] -> name.get(), U"\".");
  1311. UiField_argToValue (my field [i].get(), & args [iarg], interpreter);
  1312. }
  1313. if (iarg < narg)
  1314. Melder_throw (U"Command requires only ", iarg, U" arguments, not the ", narg, U" given.");
  1315. my okCallback (me, 0, nullptr, nullptr, interpreter, nullptr, false, my buttonClosure);
  1316. }
  1317. /*
  1318. DEPRECATED_2014 (i.e. remove in 2036)
  1319. */
  1320. static void UiField_stringToValue (UiField me, conststring32 string, Interpreter interpreter) {
  1321. /*
  1322. This belongs to the deprecated dots-based syntax described below at `UiForm_parseString`.
  1323. This is included for backward compatibility (until 2036),
  1324. but does not support newer expression types such as numeric vectors and matrices.
  1325. */
  1326. switch (my type)
  1327. {
  1328. case _kUiField_type::REAL_:
  1329. case _kUiField_type::REAL_OR_UNDEFINED_:
  1330. case _kUiField_type::POSITIVE_:
  1331. {
  1332. if (str32spn (string, U" \t") == str32len (string))
  1333. Melder_throw (U"Argument “", my name.get(), U"” empty.");
  1334. Interpreter_numericExpression (interpreter, string, & my realValue);
  1335. if (isundef (my realValue) && my type != _kUiField_type::REAL_OR_UNDEFINED_)
  1336. Melder_throw (U"\"", my name.get(), U"\" has the value \"undefined\".");
  1337. if (my type == _kUiField_type::POSITIVE_ && my realValue <= 0.0)
  1338. Melder_throw (U"\"", my name.get(), U"\" must be greater than 0.");
  1339. if (my realVariable)
  1340. *my realVariable = my realValue;
  1341. }
  1342. break;
  1343. case _kUiField_type::INTEGER_:
  1344. case _kUiField_type::NATURAL_:
  1345. case _kUiField_type::CHANNEL_: {
  1346. if (str32spn (string, U" \t") == str32len (string))
  1347. Melder_throw (U"Argument “", my name.get(), U"” empty.");
  1348. if (my type == _kUiField_type::CHANNEL_ && (str32equ (string, U"All") || str32equ (string, U"Average"))) {
  1349. my integerValue = 0;
  1350. } else if (my type == _kUiField_type::CHANNEL_ && (str32equ (string, U"Left") || str32equ (string, U"Mono"))) {
  1351. my integerValue = 1;
  1352. } else if (my type == _kUiField_type::CHANNEL_ && (str32equ (string, U"Right") || str32equ (string, U"Stereo"))) {
  1353. my integerValue = 2;
  1354. } else {
  1355. double realValue;
  1356. Interpreter_numericExpression (interpreter, string, & realValue);
  1357. my integerValue = Melder_iround (realValue);
  1358. }
  1359. if (my type == _kUiField_type::NATURAL_ && my integerValue < 1)
  1360. Melder_throw (U"\"", my name.get(), U"\" should be a positive whole number.");
  1361. if (my integerVariable)
  1362. *my integerVariable = my integerValue;
  1363. }
  1364. break;
  1365. case _kUiField_type::WORD_:
  1366. case _kUiField_type::SENTENCE_:
  1367. case _kUiField_type::TEXT_:
  1368. {
  1369. my stringValue = Melder_dup (string);
  1370. if (my stringVariable)
  1371. *my stringVariable = my stringValue.get(); // BUG dangle
  1372. }
  1373. break;
  1374. case _kUiField_type::BOOLEAN_:
  1375. {
  1376. if (! string [0])
  1377. Melder_throw (U"Empty argument for toggle button.");
  1378. my integerValue = string [0] == U'1' || string [0] == U'y' || string [0] == U'Y' ||
  1379. string [0] == U't' || string [0] == U'T';
  1380. if (my boolVariable)
  1381. *my boolVariable = my integerValue;
  1382. }
  1383. break;
  1384. case _kUiField_type::RADIO_:
  1385. case _kUiField_type::OPTIONMENU_:
  1386. {
  1387. my integerValue = 0;
  1388. for (int i = 1; i <= my options.size; i ++) {
  1389. UiOption b = my options.at [i];
  1390. if (str32equ (string, b -> name.get()))
  1391. my integerValue = i;
  1392. }
  1393. if (my integerValue == 0) {
  1394. /*
  1395. Retry with different case.
  1396. */
  1397. for (int i = 1; i <= my options.size; i ++) {
  1398. UiOption b = my options.at [i];
  1399. if (Melder_equ_firstCharacterCaseInsensitive (string, b -> name.get()))
  1400. my integerValue = i;
  1401. }
  1402. }
  1403. if (my integerValue == 0) {
  1404. Melder_throw (U"Field \"", my name.get(), U"\" must not have the value \"", string, U"\".");
  1405. }
  1406. if (my intVariable)
  1407. *my intVariable = my integerValue - my subtract;
  1408. if (my stringVariable)
  1409. *my stringVariable = my options.at [my integerValue] -> name.get();
  1410. }
  1411. break;
  1412. case _kUiField_type::LIST_:
  1413. {
  1414. integer i = 1;
  1415. for (; i <= my strings.size; i ++)
  1416. if (str32equ (string, my strings [i])) break;
  1417. if (i > my strings.size)
  1418. Melder_throw (U"Field \"", my name.get(), U"\" must not have the value \"", string, U"\".");
  1419. my integerValue = i;
  1420. if (my integerVariable)
  1421. *my integerVariable = my integerValue;
  1422. if (my stringVariable)
  1423. *my stringVariable = (char32 *) my strings [my integerValue];
  1424. }
  1425. break;
  1426. case _kUiField_type::COLOUR_:
  1427. {
  1428. autostring32 string2 = Melder_dup (string);
  1429. if (colourToValue (me, string2.get())) {
  1430. /* OK */
  1431. } else {
  1432. try {
  1433. Interpreter_numericExpression (interpreter, string2.get(), & my colourValue. red);
  1434. my colourValue. green = my colourValue. blue = my colourValue. red;
  1435. } catch (MelderError) {
  1436. Melder_clearError ();
  1437. Melder_throw (U"Cannot compute a colour from \"", string2.get(), U"\".");
  1438. }
  1439. }
  1440. if (my colourVariable) *my colourVariable = my colourValue;
  1441. }
  1442. break;
  1443. default:
  1444. {
  1445. Melder_throw (U"Unknown field type ", (int) my type, U".");
  1446. }
  1447. }
  1448. }
  1449. /*
  1450. DEPRECATED_2014 (i.e. remove in 2036)
  1451. */
  1452. void UiForm_parseString (UiForm me, conststring32 arguments, Interpreter interpreter) {
  1453. /*
  1454. This implements the dots-based scripting style
  1455. Create Sound from formula... sineWithNoise 1 0 1 44100 0.5 * sin (2*pi*377*x)
  1456. This was deprecated with the advent of the colon-based scripting style
  1457. Create Sound from formula: "sineWithNoise", 1, 0, 1, 44100, "0.5 * sin (2*pi*377*x)"
  1458. or
  1459. Create Sound from formula: "sineWithNoise", 1, 0, 1, 44100, ~ 0.5 * sin (2*pi*377*x)
  1460. in 2014, i.e. 22 years after Praat started.
  1461. If we want to conservatively support old scripts, we will have
  1462. to continue to support the dots-based scripting style until 2036.
  1463. */
  1464. int size = my numberOfFields;
  1465. while (size >= 1 && my field [size] -> type == _kUiField_type::LABEL_)
  1466. size --; // ignore trailing fields without a value
  1467. for (int i = 1; i < size; i ++) {
  1468. static char32 stringValue [3000];
  1469. int ichar = 0;
  1470. if (my field [i] -> type == _kUiField_type::LABEL_)
  1471. continue; // ignore non-trailing fields without a value
  1472. /*
  1473. Skip spaces until next argument.
  1474. */
  1475. while (*arguments == U' ' || *arguments == U'\t') arguments ++;
  1476. /*
  1477. The argument is everything up to the next space, or, if that starts with a double quote,
  1478. everything between this quote and the matching double quote;
  1479. in this case, the argument can represent a double quote by a sequence of two double quotes.
  1480. Example: the string
  1481. "I said ""hello"""
  1482. will be passed to the dialog as a single argument containing the text
  1483. I said "hello"
  1484. */
  1485. if (*arguments == U'\"') {
  1486. arguments ++; // do not include leading double quote
  1487. for (;;) {
  1488. if (*arguments == U'\0')
  1489. Melder_throw (U"Missing matching quote.");
  1490. if (*arguments == U'\"' && * ++ arguments != U'\"') break; // remember second quote
  1491. stringValue [ichar ++] = *arguments ++;
  1492. }
  1493. } else {
  1494. while (*arguments != U' ' && *arguments != U'\t' && *arguments != U'\0')
  1495. stringValue [ichar ++] = *arguments ++;
  1496. }
  1497. stringValue [ichar] = U'\0'; // trailing null character
  1498. try {
  1499. UiField_stringToValue (my field [i].get(), stringValue, interpreter);
  1500. } catch (MelderError) {
  1501. Melder_throw (U"Don't understand contents of field \"", my field [i] -> name.get(), U"\".");
  1502. }
  1503. }
  1504. /*
  1505. The last item is handled separately, because it consists of the rest of the line.
  1506. Leading spaces are skipped, but trailing spaces are included.
  1507. */
  1508. if (size > 0) {
  1509. while (*arguments == U' ' || *arguments == U'\t') arguments ++;
  1510. try {
  1511. UiField_stringToValue (my field [size].get(), arguments, interpreter);
  1512. } catch (MelderError) {
  1513. Melder_throw (U"Don't understand contents of field \"", my field [size] -> name.get(), U"\".");
  1514. }
  1515. }
  1516. my okCallback (me, 0, nullptr, nullptr, interpreter, nullptr, false, my buttonClosure);
  1517. }
  1518. void UiForm_parseStringE (EditorCommand cmd, integer narg, Stackel args, conststring32 arguments, Interpreter interpreter) {
  1519. if (args)
  1520. UiForm_call (cmd -> d_uiform.get(), narg, args, interpreter);
  1521. else
  1522. UiForm_parseString (cmd -> d_uiform.get(), arguments, interpreter);
  1523. }
  1524. static void fatalField (UiForm dia) {
  1525. Melder_fatal (U"Wrong field in command window \"", dia -> name.get(), U"\".");
  1526. }
  1527. void UiForm_setReal (UiForm me, double *p_variable, double value) {
  1528. for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
  1529. UiField field = my field [ifield].get();
  1530. if (field -> realVariable == p_variable) {
  1531. switch (field -> type)
  1532. {
  1533. case _kUiField_type::REAL_:
  1534. case _kUiField_type::REAL_OR_UNDEFINED_:
  1535. case _kUiField_type::POSITIVE_:
  1536. {
  1537. if (value == Melder_atof (field -> stringDefaultValue.get())) {
  1538. GuiText_setString (field -> text, field -> stringDefaultValue.get());
  1539. } else {
  1540. char32 s [40];
  1541. str32cpy (s, Melder_double (value));
  1542. /*
  1543. If the default is overtly real, the shown value should be as well.
  1544. */
  1545. if ((str32chr (field -> stringDefaultValue.get(), U'.') || str32chr (field -> stringDefaultValue.get(), U'e')) &&
  1546. ! (str32chr (s, U'.') || str32chr (s, U'e')))
  1547. {
  1548. str32cpy (s + str32len (s), U".0");
  1549. }
  1550. GuiText_setString (field -> text, s);
  1551. }
  1552. }
  1553. break;
  1554. default:
  1555. {
  1556. fatalField (me);
  1557. }
  1558. }
  1559. return;
  1560. }
  1561. }
  1562. Melder_fatal (U"Real field not found in command window \"", my name.get(), U"\".");
  1563. }
  1564. void UiForm_setRealAsString (UiForm me, double *p_variable, conststring32 stringValue) {
  1565. for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
  1566. UiField field = my field [ifield].get();
  1567. if (field -> realVariable == p_variable) {
  1568. switch (field -> type)
  1569. {
  1570. case _kUiField_type::REAL_:
  1571. case _kUiField_type::REAL_OR_UNDEFINED_:
  1572. case _kUiField_type::POSITIVE_:
  1573. {
  1574. GuiText_setString (field -> text, stringValue);
  1575. }
  1576. break;
  1577. default:
  1578. {
  1579. fatalField (me);
  1580. }
  1581. }
  1582. return;
  1583. }
  1584. }
  1585. Melder_fatal (U"Real field not found in command window \"", my name.get(), U"\".");
  1586. }
  1587. void UiForm_setInteger (UiForm me, integer *p_variable, integer value) {
  1588. for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
  1589. UiField field = my field [ifield].get();
  1590. if (field -> integerVariable == p_variable) {
  1591. switch (field -> type)
  1592. {
  1593. case _kUiField_type::INTEGER_:
  1594. case _kUiField_type::NATURAL_:
  1595. case _kUiField_type::CHANNEL_:
  1596. {
  1597. if (value == Melder_atoi (field -> stringDefaultValue.get())) {
  1598. GuiText_setString (field -> text, field -> stringDefaultValue.get());
  1599. } else {
  1600. GuiText_setString (field -> text, Melder_integer (value));
  1601. }
  1602. }
  1603. break;
  1604. case _kUiField_type::LIST_:
  1605. {
  1606. if (value < 1 || value > field -> strings.size)
  1607. value = 1; // guard against incorrect prefs file
  1608. GuiList_selectItem (field -> list, value);
  1609. }
  1610. break;
  1611. default:
  1612. {
  1613. fatalField (me);
  1614. }
  1615. }
  1616. return;
  1617. }
  1618. }
  1619. Melder_fatal (U"Integer field not found in command window \"", my name.get(), U"\".");
  1620. }
  1621. void UiForm_setIntegerAsString (UiForm me, integer *p_variable, conststring32 stringValue /* cattable */) {
  1622. for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
  1623. UiField field = my field [ifield].get();
  1624. if (field -> integerVariable == p_variable) {
  1625. switch (field -> type)
  1626. {
  1627. case _kUiField_type::INTEGER_:
  1628. case _kUiField_type::NATURAL_:
  1629. case _kUiField_type::CHANNEL_:
  1630. {
  1631. GuiText_setString (field -> text, stringValue);
  1632. }
  1633. break;
  1634. case _kUiField_type::LIST_:
  1635. {
  1636. integer i = 1;
  1637. for (; i <= field -> strings.size; i ++)
  1638. if (str32equ (stringValue, field -> strings [i])) break;
  1639. if (i > field -> strings.size)
  1640. i = 1; // guard against incorrect prefs file
  1641. GuiList_selectItem (field -> list, i);
  1642. }
  1643. break;
  1644. default:
  1645. {
  1646. fatalField (me);
  1647. }
  1648. }
  1649. return;
  1650. }
  1651. }
  1652. Melder_fatal (U"Integer field not found in command window \"", my name.get(), U"\".");
  1653. }
  1654. void UiForm_setBoolean (UiForm me, bool *p_variable, bool value) {
  1655. for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
  1656. UiField field = my field [ifield].get();
  1657. if (field -> boolVariable == p_variable) {
  1658. switch (field -> type)
  1659. {
  1660. case _kUiField_type::BOOLEAN_:
  1661. {
  1662. GuiCheckButton_setValue (field -> checkButton, value);
  1663. }
  1664. break;
  1665. default:
  1666. {
  1667. fatalField (me);
  1668. }
  1669. }
  1670. return;
  1671. }
  1672. }
  1673. Melder_fatal (U"Boolean field not found in command window \"", my name.get(), U"\".");
  1674. }
  1675. void UiForm_setOption (UiForm me, int *p_variable, int value) {
  1676. for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
  1677. UiField field = my field [ifield].get();
  1678. if (field -> intVariable == p_variable) {
  1679. switch (field -> type)
  1680. {
  1681. case _kUiField_type::RADIO_:
  1682. {
  1683. if (value < 1 || value > field -> options.size)
  1684. value = 1; // guard against incorrect prefs file
  1685. UiOption option = field -> options.at [value];
  1686. GuiRadioButton_set (option -> radioButton);
  1687. }
  1688. break;
  1689. case _kUiField_type::OPTIONMENU_:
  1690. {
  1691. if (value < 1 || value > field -> options.size)
  1692. value = 1; // guard against incorrect prefs file
  1693. GuiOptionMenu_setValue (field -> optionMenu, value);
  1694. }
  1695. break;
  1696. default:
  1697. {
  1698. fatalField (me);
  1699. }
  1700. }
  1701. return;
  1702. }
  1703. }
  1704. Melder_fatal (U"Option field not found in command window \"", my name.get(), U"\".");
  1705. }
  1706. void UiForm_setOptionAsString (UiForm me, int *p_variable, conststring32 stringValue) {
  1707. for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
  1708. UiField field = my field [ifield].get();
  1709. if (field -> intVariable == p_variable) {
  1710. switch (field -> type)
  1711. {
  1712. case _kUiField_type::RADIO_:
  1713. {
  1714. for (int i = 1; i <= field -> options.size; i ++) {
  1715. UiOption b = field -> options.at [i];
  1716. if (str32equ (stringValue, b -> name.get())) {
  1717. GuiRadioButton_set (b -> radioButton);
  1718. }
  1719. }
  1720. /* If not found: do nothing (guard against incorrect prefs file). */
  1721. }
  1722. break;
  1723. case _kUiField_type::OPTIONMENU_:
  1724. {
  1725. int optionValue = 0;
  1726. for (int i = 1; i <= field -> options.size; i ++) {
  1727. UiOption b = field -> options.at [i];
  1728. if (str32equ (stringValue, b -> name.get())) {
  1729. optionValue = i;
  1730. break;
  1731. }
  1732. }
  1733. GuiOptionMenu_setValue (field -> optionMenu, optionValue);
  1734. /* If not found: do nothing (guard against incorrect prefs file). */
  1735. }
  1736. break;
  1737. default:
  1738. {
  1739. fatalField (me);
  1740. }
  1741. }
  1742. return;
  1743. }
  1744. }
  1745. Melder_fatal (U"Option field not found in command window \"", my name.get(), U"\".");
  1746. }
  1747. void UiForm_setString (UiForm me, conststring32 *p_variable, conststring32 value /* cattable */) {
  1748. if (! value) value = U""; // accept null strings
  1749. for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
  1750. UiField field = my field [ifield].get();
  1751. if (field -> stringVariable == p_variable) {
  1752. switch (field -> type)
  1753. {
  1754. case _kUiField_type::WORD_:
  1755. case _kUiField_type::SENTENCE_:
  1756. case _kUiField_type::COLOUR_:
  1757. case _kUiField_type::TEXT_:
  1758. {
  1759. GuiText_setString (field -> text, value);
  1760. }
  1761. break;
  1762. case _kUiField_type::LABEL_:
  1763. {
  1764. GuiLabel_setText (field -> label, value);
  1765. }
  1766. break;
  1767. default:
  1768. {
  1769. fatalField (me);
  1770. }
  1771. }
  1772. return;
  1773. }
  1774. }
  1775. Melder_fatal (U"Text field not found in command window \"", my name.get(), U"\".");
  1776. }
  1777. void UiForm_setColourAsGreyValue (UiForm me, Graphics_Colour *p_variable, double greyValue) {
  1778. for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
  1779. UiField field = my field [ifield].get();
  1780. if (field -> colourVariable == p_variable) {
  1781. switch (field -> type)
  1782. {
  1783. case _kUiField_type::COLOUR_:
  1784. {
  1785. GuiText_setString (field -> text, Melder_double (greyValue));
  1786. }
  1787. break;
  1788. default:
  1789. {
  1790. fatalField (me);
  1791. }
  1792. }
  1793. return;
  1794. }
  1795. }
  1796. Melder_fatal (U"Colour field not found in command window \"", my name.get(), U"\".");
  1797. }
  1798. static UiField findField (UiForm me, conststring32 fieldName) {
  1799. for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
  1800. if (str32equ (fieldName, my field [ifield] -> name.get()))
  1801. return my field [ifield].get();
  1802. }
  1803. return nullptr;
  1804. }
  1805. static UiField findField_check (UiForm me, conststring32 fieldName) {
  1806. UiField result = findField (me, fieldName);
  1807. if (! result) {
  1808. Melder_throw (U"Cannot find field \"", fieldName, U"\" in form.\n"
  1809. U"The script may have changed while the form was open.\n"
  1810. U"Please click Cancel in the form and try again.");
  1811. }
  1812. return result;
  1813. }
  1814. double UiForm_getReal_check (UiForm me, conststring32 fieldName) {
  1815. UiField field = findField_check (me, fieldName);
  1816. switch (field -> type)
  1817. {
  1818. case _kUiField_type::REAL_:
  1819. case _kUiField_type::REAL_OR_UNDEFINED_:
  1820. case _kUiField_type::POSITIVE_:
  1821. {
  1822. return field -> realValue;
  1823. }
  1824. break;
  1825. default:
  1826. {
  1827. Melder_throw (U"Cannot find a real value in field \"", fieldName, U"\" in the form.\n"
  1828. U"The script may have changed while the form was open.\n"
  1829. U"Please click Cancel in the form and try again.");
  1830. }
  1831. }
  1832. return 0.0;
  1833. }
  1834. integer UiForm_getInteger (UiForm me, conststring32 fieldName) {
  1835. UiField field = findField (me, fieldName);
  1836. if (! field) Melder_fatal (U"(UiForm_getInteger:) No field \"", fieldName, U"\" in command window \"", my name.get(), U"\".");
  1837. switch (field -> type)
  1838. {
  1839. case _kUiField_type::INTEGER_:
  1840. case _kUiField_type::NATURAL_:
  1841. case _kUiField_type::CHANNEL_:
  1842. case _kUiField_type::BOOLEAN_:
  1843. case _kUiField_type::RADIO_:
  1844. case _kUiField_type::OPTIONMENU_:
  1845. case _kUiField_type::LIST_:
  1846. {
  1847. return field -> integerValue;
  1848. }
  1849. break;
  1850. default:
  1851. {
  1852. fatalField (me);
  1853. }
  1854. }
  1855. return 0;
  1856. }
  1857. integer UiForm_getInteger_check (UiForm me, conststring32 fieldName) {
  1858. UiField field = findField_check (me, fieldName);
  1859. switch (field -> type)
  1860. {
  1861. case _kUiField_type::INTEGER_:
  1862. case _kUiField_type::NATURAL_:
  1863. case _kUiField_type::CHANNEL_:
  1864. case _kUiField_type::BOOLEAN_:
  1865. case _kUiField_type::RADIO_:
  1866. case _kUiField_type::OPTIONMENU_:
  1867. case _kUiField_type::LIST_:
  1868. {
  1869. return field -> integerValue;
  1870. }
  1871. break;
  1872. default:
  1873. {
  1874. Melder_throw (U"Cannot find an integer value in field \"", fieldName, U"\" in the form.\n"
  1875. U"The script may have changed while the form was open.\n"
  1876. U"Please click Cancel in the form and try again.");
  1877. }
  1878. }
  1879. return 0;
  1880. }
  1881. char32 * UiForm_getString (UiForm me, conststring32 fieldName) {
  1882. UiField field = findField (me, fieldName);
  1883. if (! field) Melder_fatal (U"(UiForm_getString:) No field \"", fieldName, U"\" in command window \"", my name.get(), U"\".");
  1884. switch (field -> type)
  1885. {
  1886. case _kUiField_type::WORD_:
  1887. case _kUiField_type::SENTENCE_:
  1888. case _kUiField_type::TEXT_:
  1889. {
  1890. return field -> stringValue.get(); // BUG dangle
  1891. }
  1892. break;
  1893. case _kUiField_type::RADIO_:
  1894. case _kUiField_type::OPTIONMENU_:
  1895. {
  1896. UiOption b = field -> options.at [field -> integerValue];
  1897. return b -> name.get();
  1898. }
  1899. break;
  1900. case _kUiField_type::LIST_:
  1901. {
  1902. return (char32 *) field -> strings [field -> integerValue];
  1903. }
  1904. break;
  1905. default:
  1906. {
  1907. fatalField (me);
  1908. }
  1909. }
  1910. return nullptr;
  1911. }
  1912. char32 * UiForm_getString_check (UiForm me, conststring32 fieldName) {
  1913. UiField field = findField_check (me, fieldName);
  1914. switch (field -> type)
  1915. {
  1916. case _kUiField_type::WORD_:
  1917. case _kUiField_type::SENTENCE_:
  1918. case _kUiField_type::TEXT_:
  1919. {
  1920. return field -> stringValue.get();
  1921. }
  1922. break;
  1923. case _kUiField_type::RADIO_:
  1924. case _kUiField_type::OPTIONMENU_:
  1925. {
  1926. UiOption b = field -> options.at [field -> integerValue];
  1927. return b -> name.get();
  1928. }
  1929. break;
  1930. case _kUiField_type::LIST_:
  1931. {
  1932. return (char32 *) field -> strings [field -> integerValue];
  1933. }
  1934. break;
  1935. default:
  1936. {
  1937. Melder_throw (U"Cannot find a string in field \"", fieldName, U"\" in the form.\n"
  1938. U"The script may have changed while the form was open.\n"
  1939. U"Please click Cancel in the form and try again.");
  1940. }
  1941. }
  1942. return nullptr;
  1943. }
  1944. Graphics_Colour UiForm_getColour_check (UiForm me, conststring32 fieldName) {
  1945. UiField field = findField_check (me, fieldName);
  1946. switch (field -> type)
  1947. {
  1948. case _kUiField_type::COLOUR_: {
  1949. return field -> colourValue;
  1950. }
  1951. break;
  1952. default:
  1953. {
  1954. Melder_throw (U"Cannot find a real value in field \"", fieldName, U"\" in the form.\n"
  1955. U"The script may have changed while the form was open.\n"
  1956. U"Please click Cancel in the form and try again.");
  1957. }
  1958. }
  1959. return Graphics_BLACK;
  1960. }
  1961. void UiForm_Interpreter_addVariables (UiForm me, Interpreter interpreter) {
  1962. static MelderString lowerCaseFieldName { };
  1963. for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
  1964. UiField field = my field [ifield].get();
  1965. MelderString_copy (& lowerCaseFieldName, field -> name.get());
  1966. /*
  1967. Change e.g. "Number of people" to "number_of_people".
  1968. */
  1969. lowerCaseFieldName.string [0] = Melder_toLowerCase (lowerCaseFieldName.string [0]);
  1970. for (char32 *p = & lowerCaseFieldName.string [0]; *p != U'\0'; p ++) {
  1971. if (*p == U' ')
  1972. *p = U'_';
  1973. }
  1974. switch (field -> type)
  1975. {
  1976. case _kUiField_type::INTEGER_:
  1977. case _kUiField_type::NATURAL_:
  1978. case _kUiField_type::CHANNEL_:
  1979. case _kUiField_type::BOOLEAN_:
  1980. {
  1981. InterpreterVariable var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
  1982. var -> numericValue = field -> integerValue;
  1983. }
  1984. break;
  1985. case _kUiField_type::REAL_:
  1986. case _kUiField_type::REAL_OR_UNDEFINED_:
  1987. case _kUiField_type::POSITIVE_:
  1988. {
  1989. InterpreterVariable var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
  1990. var -> numericValue = field -> realValue;
  1991. }
  1992. break;
  1993. case _kUiField_type::RADIO_:
  1994. case _kUiField_type::OPTIONMENU_:
  1995. {
  1996. InterpreterVariable var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
  1997. var -> numericValue = field -> integerValue;
  1998. MelderString_appendCharacter (& lowerCaseFieldName, U'$');
  1999. var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
  2000. UiOption b = field -> options.at [field -> integerValue];
  2001. var -> stringValue = Melder_dup (b -> name.get());
  2002. }
  2003. break;
  2004. case _kUiField_type::LIST_:
  2005. {
  2006. InterpreterVariable var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
  2007. var -> numericValue = field -> integerValue;
  2008. MelderString_appendCharacter (& lowerCaseFieldName, U'$');
  2009. var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
  2010. var -> stringValue = Melder_dup (field -> strings [field -> integerValue]);
  2011. }
  2012. break;
  2013. case _kUiField_type::WORD_:
  2014. case _kUiField_type::SENTENCE_:
  2015. case _kUiField_type::TEXT_:
  2016. {
  2017. MelderString_appendCharacter (& lowerCaseFieldName, U'$');
  2018. InterpreterVariable var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
  2019. var -> stringValue = Melder_dup (field -> stringValue.get());
  2020. }
  2021. break;
  2022. case _kUiField_type::COLOUR_:
  2023. {
  2024. // to be implemented
  2025. }
  2026. break;
  2027. default:
  2028. {
  2029. }
  2030. }
  2031. }
  2032. }
  2033. int UiForm_getClickedContinueButton (UiForm me) {
  2034. return my clickedContinueButton;
  2035. }
  2036. /* End of file Ui.cpp */