Debian Codegraph @

sys/Ui.cpp

raw
1 /* Ui.cpp
2 *
3 * Copyright (C) 1992-2026 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 3 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
19 #include "../kar/longchar.h"
20 #include "machine.h"
21 #include "GuiP.h"
22 #include "Collection.h"
23 #include "UiP.h"
24 #include "ScriptEditor.h"
25 #include "Graphics.h" // colours
26 #include "NUM2.h" // get elements of ranges
27
28 #include "enums_getText.h"
29 #include "Ui_enums.h"
30 #include "enums_getValue.h"
31 #include "Ui_enums.h"
32
33 kUi_realMatrixFormat theRealMatrixFormat;
34 kUi_stringArrayFormat theStringArrayFormat;
35
36 void Ui_prefs () {
37 Preferences_addEnum (U"Ui.realMatrixFormat", & theRealMatrixFormat, kUi_realMatrixFormat, (int) kUi_realMatrixFormat::DEFAULT);
38 Preferences_addEnum (U"Ui.stringArrayFormat", & theStringArrayFormat, kUi_stringArrayFormat, (int) kUi_stringArrayFormat::DEFAULT);
39 }
40
41 static conststring32 formatNumericMatrix (constMAT cells, kUi_realMatrixFormat format) {
42 static MelderString buffer;
43 MelderString_empty (& buffer);
44 switch (format) {
45 case kUi_realMatrixFormat::ONE_ROW_PER_LINE_: {
46 for (integer irow = 1; irow <= cells.nrow; irow ++) {
47 for (integer icol = 1; icol <= cells.ncol; icol ++) {
48 MelderString_append (& buffer, cells [irow] [icol]);
49 if (icol < cells.ncol)
50 MelderString_appendCharacter (& buffer, U' ');
51 }
52 if (irow < cells.nrow)
53 MelderString_appendCharacter (& buffer, U'\n');
54 }
55 } break; case kUi_realMatrixFormat::FORMULA_: {
56 if (NUMisEmpty (cells)) {
57 MelderString_append (& buffer, U"zero## (0, 0)");
58 } else {
59 MelderString_append (& buffer, U"{ ");
60 for (integer irow = 1; irow <= cells.nrow; irow ++) {
61 MelderString_append (& buffer, U"{ ");
62 for (integer icol = 1; icol <= cells.ncol; icol ++) {
63 MelderString_append (& buffer, cells [irow] [icol]);
64 if (icol < cells.ncol)
65 MelderString_append (& buffer, U", ");
66 }
67 MelderString_append (& buffer, U" }");
68 if (irow < cells.nrow)
69 MelderString_append (& buffer, U", ");
70 }
71 MelderString_append (& buffer, U" }");
72 }
73 } break; case kUi_realMatrixFormat::UNDEFINED: {
74 Melder_crash (U"Unknown numeric matrix format.");
75 }
76 }
77 return buffer.string;
78 }
79
80 static void MelderString_appendQuoted (MelderString *buffer, conststring32 string) {
81 MelderString_appendCharacter (buffer, U'"');
82 for (const char32 *p = & string [0]; *p != U'\0'; p ++)
83 if (*p == U'"')
84 MelderString_append (buffer, U"\"\"");
85 else
86 MelderString_appendCharacter (buffer, *p);
87 MelderString_appendCharacter (buffer, U'"');
88 }
89
90 static conststring32 formatStringArray (constSTRVEC strings, kUi_stringArrayFormat format) {
91 static MelderString buffer;
92 MelderString_empty (& buffer);
93 switch (format) {
94 case kUi_stringArrayFormat::WHITESPACE_SEPARATED_: {
95 for (integer i = 1; i <= strings.size; i ++) {
96 if (str32chr (strings [i], U'"') || Melder_findHorizontalOrVerticalSpace (strings [i]))
97 MelderString_appendQuoted (& buffer, strings [i]);
98 else
99 MelderString_append (& buffer, strings [i]);
100 if (i < strings.size)
101 MelderString_appendCharacter (& buffer, U' ');
102 }
103 } break; case kUi_stringArrayFormat::COMMA_SEPARATED_: {
104 for (integer i = 1; i <= strings.size; i ++) {
105 if (str32chr (strings [i], U'"') || str32chr (strings [i], U','))
106 MelderString_appendQuoted (& buffer, strings [i]);
107 else
108 MelderString_append (& buffer, strings [i]);
109 if (i < strings.size)
110 MelderString_appendCharacter (& buffer, U',');
111 }
112 } break; case kUi_stringArrayFormat::SEMICOLON_SEPARATED_: {
113 for (integer i = 1; i <= strings.size; i ++) {
114 if (str32chr (strings [i], U'"') || str32chr (strings [i], U';'))
115 MelderString_appendQuoted (& buffer, strings [i]);
116 else
117 MelderString_append (& buffer, strings [i]);
118 if (i < strings.size)
119 MelderString_appendCharacter (& buffer, U';');
120 }
121 } break; case kUi_stringArrayFormat::PIPE_SEPARATED_: {
122 for (integer i = 1; i <= strings.size; i ++) {
123 if (str32chr (strings [i], U'"') || str32chr (strings [i], U'|'))
124 MelderString_appendQuoted (& buffer, strings [i]);
125 else
126 MelderString_append (& buffer, strings [i]);
127 if (i < strings.size)
128 MelderString_appendCharacter (& buffer, U'|');
129 }
130 } break; case kUi_stringArrayFormat::ONE_PER_LINE_: {
131 for (integer i = 1; i <= strings.size; i ++) {
132 MelderString_append (& buffer, strings [i]);
133 if (i < strings.size)
134 MelderString_appendCharacter (& buffer, U'\n');
135 }
136 } break; case kUi_stringArrayFormat::FORMULA_: {
137 if (NUMisEmpty (strings)) {
138 MelderString_append (& buffer, U"empty$# (0)");
139 } else {
140 MelderString_append (& buffer, U"{ ");
141 for (integer i = 1; i <= strings.size; i ++) {
142 MelderString_appendQuoted (& buffer, strings [i]);
143 if (i < strings.size)
144 MelderString_append (& buffer, U", ");
145 }
146 MelderString_append (& buffer, U" }");
147 }
148 } break; case kUi_stringArrayFormat::UNDEFINED: {
149 Melder_crash (U"Unknown string array format.");
150 }
151 }
152 return buffer.string;
153 }
154
155 /***** class UiField: the things that have values in an UiForm dialog *****/
156
157 Thing_implement (UiField, Thing, 0);
158
159 static autoUiField UiField_create (_kUiField_type type, conststring32 labelTextOrNull) {
160 autoUiField me = Thing_new (UiField);
161 my type = type;
162 if (labelTextOrNull) {
163 /*
164 The label text is for instance "Pitch ceiling (Hz)".
165 */
166 my labelText = Melder_dup (labelTextOrNull);
167
168 /*
169 The name is for instance "Pitch ceiling".
170 */
171 char32 shortName [1+100], *p;
172 str32ncpy (shortName, labelTextOrNull, 100);
173 shortName [100] = U'\0';
174 /*
175 Strip parentheses and the occasional colon off the parameter name.
176 */
177 if (!! (p = (char32 *) str32chr (shortName, U'('))) {
178 *p = U'\0';
179 if (p - shortName > 0 && p [-1] == U' ')
180 p [-1] = U'\0';
181 }
182 p = shortName;
183 if (*p != U'\0' && p [Melder_length (p) - 1] == U':')
184 p [Melder_length (p) - 1] = U'\0'; // unlikely
185 Thing_setName (me.get(), shortName);
186 }
187 return me;
188 }
189
190 /***** class UiOption: radio buttons and menu options *****/
191
192 Thing_implement (UiOption, Thing, 0);
193
194 static autoUiOption UiOption_create (conststring32 optionText) {
195 autoUiOption me = Thing_new (UiOption);
196 Thing_setName (me.get(), optionText);
197 return me;
198 }
199
200 UiOption UiForm_addOption (UiForm me, conststring32 optionText) {
201 if (! me)
202 return nullptr;
203 UiField you = my referenceToLatestUsedChoiceOrOptionMenu;
204 if (! you)
205 return nullptr;
206 Melder_assert (your type == _kUiField_type::CHOICE_ || your type == _kUiField_type::OPTIONMENU_);
207 autoUiOption option = UiOption_create (optionText);
208 return your options. addItem_move (option.move());
209 }
210
211 /***** Things to do with UiField objects. *****/
212
213 static void UiField_setDefault (UiField me) {
214 switch (my type) {
215 case _kUiField_type::HEADING_:
216 case _kUiField_type::COMMENT_:
217 case _kUiField_type::CAPTION_:
218 {
219 // do nothing
220 }
221 break;
222 case _kUiField_type::REAL_:
223 case _kUiField_type::REAL_OR_UNDEFINED_:
224 case _kUiField_type::NONNEGATIVE_:
225 case _kUiField_type::POSITIVE_:
226 case _kUiField_type::INTEGER_:
227 case _kUiField_type::NATURAL0_:
228 case _kUiField_type::NATURAL1_:
229 case _kUiField_type::WORD_:
230 case _kUiField_type::SENTENCE_:
231 case _kUiField_type::COLOUR_:
232 case _kUiField_type::CHANNEL_:
233 case _kUiField_type::TEXT_:
234 case _kUiField_type::FORMULA_:
235 case _kUiField_type::INFILE_:
236 case _kUiField_type::OUTFILE_:
237 case _kUiField_type::FOLDER_:
238 {
239 GuiText_setString (my text, my stringDefaultValue.get());
240 }
241 break;
242 case _kUiField_type::REALVECTOR_:
243 case _kUiField_type::NONNEGATIVEVECTOR_:
244 case _kUiField_type::POSITIVEVECTOR_:
245 {
246 GuiOptionMenu_setValue (my optionMenu, (int) my realVectorDefaultFormat);
247 GuiText_setString (my text, my stringDefaultValue.get());
248 }
249 break;
250 case _kUiField_type::INTEGERVECTOR_:
251 case _kUiField_type::NATURAL0VECTOR_:
252 case _kUiField_type::NATURAL1VECTOR_:
253 {
254 GuiOptionMenu_setValue (my optionMenu, (int) my integerVectorDefaultFormat);
255 GuiText_setString (my text, my stringDefaultValue.get());
256 }
257 break;
258 case _kUiField_type::REALMATRIX_:
259 {
260 theRealMatrixFormat = (kUi_realMatrixFormat) GuiOptionMenu_getValue (my optionMenu);
261 GuiText_setString (my text, formatNumericMatrix (my numericMatrixDefaultValue.get(), theRealMatrixFormat));
262 }
263 break;
264 case _kUiField_type::STRINGARRAY_:
265 {
266 theStringArrayFormat = (kUi_stringArrayFormat) GuiOptionMenu_getValue (my optionMenu);
267 GuiText_setString (my text, formatStringArray (my stringArrayDefaultValue.get(), theStringArrayFormat));
268 }
269 break;
270 case _kUiField_type::BOOLEAN_:
271 {
272 GuiCheckButton_setValue (my checkButton, my integerDefaultValue);
273 }
274 break;
275 case _kUiField_type::CHOICE_:
276 {
277 for (int i = 1; i <= my options.size; i ++) {
278 if (i == my integerDefaultValue) {
279 UiOption b = my options.at [i];
280 GuiRadioButton_set (b -> radioButton);
281 }
282 }
283 }
284 break;
285 case _kUiField_type::OPTIONMENU_:
286 {
287 GuiOptionMenu_setValue (my optionMenu, my integerDefaultValue);
288 }
289 break;
290 case _kUiField_type::LIST_:
291 {
292 if (my integerDefaultValue > 0)
293 GuiList_selectItem (my list, my integerDefaultValue);
294 }
295 }
296 }
297
298 static void UiField_widgetToValue (UiField me) {
299 switch (my type)
300 {
301 case _kUiField_type::HEADING_:
302 case _kUiField_type::COMMENT_:
303 case _kUiField_type::CAPTION_:
304 {
305 // do nothing
306 }
307 break;
308 case _kUiField_type::REAL_:
309 case _kUiField_type::REAL_OR_UNDEFINED_:
310 case _kUiField_type::NONNEGATIVE_:
311 case _kUiField_type::POSITIVE_:
312 {
313 autostring32 text = GuiText_getString (my text); // the text as typed by the user
314 Interpreter_numericExpression (nullptr, text.get(), & my realValue);
315 if (isundef (my realValue) && my type != _kUiField_type::REAL_OR_UNDEFINED_)
316 Melder_throw (U"“", my name.get(), U"” has the value “undefined”.");
317 if (my type == _kUiField_type::NONNEGATIVE_ && my realValue < 0.0)
318 Melder_throw (U"“", my name.get(), U"” should be greater than or equal to 0.0.");
319 if (my type == _kUiField_type::POSITIVE_ && my realValue <= 0.0)
320 Melder_throw (U"“", my name.get(), U"” should be greater than 0.0.");
321 if (my realVariable)
322 *my realVariable = my realValue;
323 }
324 break;
325 case _kUiField_type::INTEGER_:
326 case _kUiField_type::NATURAL0_:
327 case _kUiField_type::NATURAL1_:
328 case _kUiField_type::CHANNEL_:
329 {
330 autostring32 text = GuiText_getString (my text);
331 if (my type == _kUiField_type::CHANNEL_ && (str32equ (text.get(), U"Left") || str32equ (text.get(), U"Mono"))) {
332 my integerValue = 1;
333 } else if (my type == _kUiField_type::CHANNEL_ && (str32equ (text.get(), U"Right") || str32equ (text.get(), U"Stereo"))) {
334 my integerValue = 2;
335 } else {
336 double realValue;
337 Interpreter_numericExpression (nullptr, text.get(), & realValue);
338 my integerValue = Melder_iround (realValue);
339 Melder_require (my integerValue == realValue,
340 U"“", my name.get(), U"” should be a whole number.");
341 }
342 if (my type == _kUiField_type::NATURAL0_) {
343 Melder_require (my integerValue >= 0,
344 U"“", my name.get(), U"” should be a nonnegative whole number.");
345 }
346 if (my type == _kUiField_type::NATURAL1_) {
347 Melder_require (my integerValue >= 1,
348 U"“", my name.get(), U"” should be a positive whole number.");
349 }
350 if (my type == _kUiField_type::CHANNEL_) {
351 Melder_require (my integerValue >= 0,
352 U"“", my name.get(), U"” should be a positive whole number or zero.");
353 }
354 if (my integerVariable)
355 *my integerVariable = my integerValue;
356 }
357 break;
358 case _kUiField_type::WORD_:
359 {
360 my stringValue = GuiText_getString (my text);
361 Melder_require (*Melder_findEndOfInk (my stringValue.get()) == U'\0',
362 U"“", my name.get(), U"” should be a single ink-word and cannot contain a space.");
363 if (my stringVariable)
364 *my stringVariable = my stringValue.get();
365 }
366 break;
367 case _kUiField_type::SENTENCE_:
368 case _kUiField_type::TEXT_:
369 case _kUiField_type::FORMULA_:
370 case _kUiField_type::INFILE_:
371 case _kUiField_type::OUTFILE_:
372 case _kUiField_type::FOLDER_:
373 {
374 my stringValue = GuiText_getString (my text);
375 if (my stringVariable)
376 *my stringVariable = my stringValue.get();
377 }
378 break;
379 case _kUiField_type::REALVECTOR_:
380 case _kUiField_type::NONNEGATIVEVECTOR_:
381 case _kUiField_type::POSITIVEVECTOR_:
382 {
383 autostring32 stringValue = GuiText_getString (my text);
384 kUi_realVectorFormat format = (kUi_realVectorFormat) GuiOptionMenu_getValue (my optionMenu);
385 switch (format) {
386 case kUi_realVectorFormat::WHITESPACE_SEPARATED_: {
387 my realVectorValue = splitByWhitespace_VEC (stringValue.get());
388 } break; case kUi_realVectorFormat::FORMULA_: {
389 VEC result;
390 bool ownedByInterpreter;
391 Interpreter_numericVectorExpression (nullptr, stringValue.get(), & result, & ownedByInterpreter);
392 if (ownedByInterpreter) {
393 my realVectorValue. adoptFromAmbiguousOwner (result);
394 } else {
395 my realVectorValue = copy_VEC (result);
396 }
397 } break; case kUi_realVectorFormat::UNDEFINED: {
398 Melder_crash (U"Unknown real vector format.");
399 }
400 }
401 if (my type == _kUiField_type::NONNEGATIVEVECTOR_)
402 for (integer i = 1; i <= my realVectorValue.size; i ++)
403 if (my realVectorValue [i] < 0.0)
404 Melder_throw (U"Element ", i, U" of vector “", my name.get(), U"” is ", my realVectorValue [i], U" but should be greater than or equal to 0.0.");
405 if (my type == _kUiField_type::POSITIVEVECTOR_)
406 for (integer i = 1; i <= my realVectorValue.size; i ++)
407 if (my realVectorValue [i] <= 0.0)
408 Melder_throw (U"Element ", i, U" of vector “", my name.get(), U"” is ", my realVectorValue [i], U" but should be greater than 0.0.");
409 if (my realVectorVariable)
410 *my realVectorVariable = my realVectorValue.get();
411 }
412 break;
413 case _kUiField_type::INTEGERVECTOR_:
414 case _kUiField_type::NATURAL0VECTOR_:
415 case _kUiField_type::NATURAL1VECTOR_:
416 {
417 autostring32 stringValue = GuiText_getString (my text);
418 kUi_integerVectorFormat format = (kUi_integerVectorFormat) GuiOptionMenu_getValue (my optionMenu);
419 switch (format) {
420 case kUi_integerVectorFormat::WHITESPACE_SEPARATED_: {
421 my integerVectorValue = iround_INTVEC (splitByWhitespace_VEC (stringValue.get()).get());
422 } break; case kUi_integerVectorFormat::RANGES_: {
423 my integerVectorValue = splitByWhitespaceWithRanges_INTVEC (stringValue.get());
424 } break; case kUi_integerVectorFormat::FORMULA_: {
425 VEC result;
426 bool ownedByInterpreter;
427 Interpreter_numericVectorExpression (nullptr, stringValue.get(), & result, & ownedByInterpreter);
428 my integerVectorValue = raw_INTVEC (result.size);
429 for (integer i = 1; i <= result.size; i ++) {
430 my integerVectorValue [i] = Melder_iround (result [i]);
431 Melder_require (my integerVectorValue [i] == result [i],
432 U"Element ", i, U" of vector “", my name.get(), U"” is ", result [i], U" but should be a whole number.");
433 }
434 } break; case kUi_integerVectorFormat::UNDEFINED: {
435 Melder_crash (U"Unknown integer vector format.");
436 }
437 }
438 if (my type == _kUiField_type::NATURAL0VECTOR_)
439 for (integer i = 1; i <= my integerVectorValue.size; i ++)
440 if (my integerVectorValue [i] < 0)
441 Melder_throw (U"Element ", i, U" of vector “", my name.get(), U"” is ", my integerVectorValue [i], U" but should be greater than or equal to 0.");
442 if (my type == _kUiField_type::NATURAL1VECTOR_)
443 for (integer i = 1; i <= my integerVectorValue.size; i ++)
444 if (my integerVectorValue [i] <= 0)
445 Melder_throw (U"Element ", i, U" of vector “", my name.get(), U"” is ", my integerVectorValue [i], U" but should be greater than 0.");
446 if (my integerVectorVariable)
447 *my integerVectorVariable = my integerVectorValue.get();
448 }
449 break;
450 case _kUiField_type::REALMATRIX_:
451 {
452 autostring32 stringValue = GuiText_getString (my text);
453 kUi_realMatrixFormat format = (kUi_realMatrixFormat) GuiOptionMenu_getValue (my optionMenu);
454 switch (format) {
455 case kUi_realMatrixFormat::ONE_ROW_PER_LINE_: {
456 my numericMatrixValue = splitByLinesAndWhitespace_MAT (stringValue.get());
457 } break; case kUi_realMatrixFormat::FORMULA_: {
458 MAT result;
459 bool ownedByInterpreter;
460 Interpreter_numericMatrixExpression (nullptr, stringValue.get(), & result, & ownedByInterpreter);
461 if (ownedByInterpreter) {
462 my numericMatrixValue. adoptFromAmbiguousOwner (result);
463 } else {
464 my numericMatrixValue = copy_MAT (result);
465 }
466 } break; case kUi_realMatrixFormat::UNDEFINED: {
467 Melder_crash (U"Unknown matrix format.");
468 }
469 }
470 if (my numericMatrixVariable)
471 *my numericMatrixVariable = my numericMatrixValue.get();
472 }
473 break;
474 case _kUiField_type::STRINGARRAY_:
475 {
476 autostring32 stringValue = GuiText_getString (my text);
477 theStringArrayFormat = (kUi_stringArrayFormat) GuiOptionMenu_getValue (my optionMenu);
478 switch (theStringArrayFormat) {
479 case kUi_stringArrayFormat::WHITESPACE_SEPARATED_: {
480 my stringArrayValue = splitByWhitespace_STRVEC (stringValue.get());
481 } break; case kUi_stringArrayFormat::COMMA_SEPARATED_: {
482 my stringArrayValue = splitBy_STRVEC (stringValue.get(), U",");
483 } break; case kUi_stringArrayFormat::SEMICOLON_SEPARATED_: {
484 my stringArrayValue = splitBy_STRVEC (stringValue.get(), U";");
485 } break; case kUi_stringArrayFormat::PIPE_SEPARATED_: {
486 my stringArrayValue = splitBy_STRVEC (stringValue.get(), U"|");
487 } break; case kUi_stringArrayFormat::ONE_PER_LINE_: {
488 my stringArrayValue = splitBy_STRVEC (stringValue.get(), U"\n");
489 } break; case kUi_stringArrayFormat::FORMULA_: {
490 if (stringValue [0] == U'\0') {
491 my stringArrayValue = autoSTRVEC(); // interpret the empty string as zero elements, as for all other formats
492 } else {
493 STRVEC result;
494 bool ownedByInterpreter;
495 Interpreter_stringArrayExpression (nullptr, stringValue.get(), & result, & ownedByInterpreter);
496 if (ownedByInterpreter) {
497 my stringArrayValue. adoptFromAmbiguousOwner (result);
498 } else {
499 my stringArrayValue = copy_STRVEC (result);
500 }
501 }
502 } break; case kUi_stringArrayFormat::UNDEFINED: {
503 Melder_crash (U"Unknown string array format.");
504 }
505 }
506 if (my stringArrayVariable)
507 *my stringArrayVariable = my stringArrayValue.get();
508 }
509 break;
510 case _kUiField_type::BOOLEAN_:
511 {
512 my integerValue = GuiCheckButton_getValue (my checkButton);
513 if (my boolVariable)
514 *my boolVariable = my integerValue;
515 }
516 break;
517 case _kUiField_type::CHOICE_:
518 {
519 my integerValue = 0;
520 for (int i = 1; i <= my options.size; i ++) {
521 UiOption b = my options.at [i];
522 if (GuiRadioButton_getValue (b -> radioButton))
523 my integerValue = i;
524 }
525 if (my integerValue == 0)
526 Melder_throw (U"No option chosen for “", my name.get(), U"”.");
527 if (my intVariable)
528 *my intVariable = int (my integerValue) - my subtract;
529 if (my stringVariable)
530 *my stringVariable = my options.at [my integerValue] -> name.get();
531 }
532 break;
533 case _kUiField_type::OPTIONMENU_:
534 {
535 my integerValue = GuiOptionMenu_getValue (my optionMenu);
536 if (my integerValue == 0)
537 Melder_throw (U"No option chosen for “", my name.get(), U"”.");
538 if (my intVariable)
539 *my intVariable = int (my integerValue) - my subtract;
540 if (my stringVariable)
541 *my stringVariable = my options.at [my integerValue] -> name.get();
542 }
543 break;
544 case _kUiField_type::LIST_:
545 {
546 autoINTVEC selected = GuiList_getSelectedPositions (my list);
547 if (selected.size == 0) {
548 Melder_warning (U"No items selected.");
549 my integerValue = 1;
550 } else {
551 if (selected.size > 1)
552 Melder_warning (U"More than one item selected.");
553 my integerValue = selected [1];
554 }
555 if (my integerValue >= 1 && my integerValue <= my strings.size)
556 my previouslySelectedListString = Melder_dup (my strings [my integerValue]);
557 if (my integerVariable)
558 *my integerVariable = my integerValue;
559 if (my stringVariable)
560 *my stringVariable = (char32 *) my strings [my integerValue];
561 }
562 break;
563 case _kUiField_type::COLOUR_:
564 {
565 autostring32 string = GuiText_getString (my text);
566 MelderColour colour = MelderColour_fromColourNameOrRGBString (string.get());
567 if (colour.valid()) {
568 my colourValue = colour;
569 } else {
570 double greyValue;
571 Interpreter_numericExpression (nullptr, string.get(), & greyValue);
572 my colourValue = MelderColour (greyValue);
573 }
574 if (my colourVariable)
575 *my colourVariable = my colourValue;
576 }
577 }
578 }
579
580 /***** History mechanism. *****/
581
582 static MelderString theHistory;
583 void UiHistory_write (conststring32 string) {
584 MelderString_append (& theHistory, string);
585 }
586 void UiHistory_write_expandQuotes (conststring32 string) {
587 if (! string)
588 return;
589 for (const char32 *p = & string [0]; *p != U'\0'; p ++) {
590 if (*p == U'"')
591 MelderString_append (& theHistory, U"\"\"");
592 else
593 MelderString_appendCharacter (& theHistory, *p);
594 }
595 }
596 void UiHistory_write_colonize (conststring32 string) {
597 if (! string)
598 return;
599 for (const char32 *p = & string [0]; *p != U'\0'; p ++) {
600 if (*p == U'.' && p [1] == U'.' && p [2] == U'.') {
601 MelderString_append (& theHistory, U":");
602 p += 2;
603 } else {
604 MelderString_appendCharacter (& theHistory, *p);
605 }
606 }
607 }
608 char32 *UiHistory_get () {
609 return theHistory.string;
610 }
611 void UiHistory_clear () {
612 MelderString_empty (& theHistory);
613 }
614
615 /***** class UiForm: dialog windows *****/
616
617 Thing_implement (UiForm, Thing, 0);
618
619 bool (*theAllowExecutionHookHint) (void *closure) = nullptr;
620 void *theAllowExecutionClosureHint = nullptr;
621
622 void Ui_setAllowExecutionHook (bool (*allowExecutionHook) (void *closure), void *allowExecutionClosure) {
623 theAllowExecutionHookHint = allowExecutionHook;
624 theAllowExecutionClosureHint = allowExecutionClosure;
625 }
626
627 void structUiForm :: v9_destroy () noexcept {
628 if (our d_dialogForm) {
629 trace (U"form <<", our d_dialogForm -> name.get(), U">>, invoking-button title <<", our invokingButtonTitle.get(), U">>");
630 GuiObject_destroy (our d_dialogForm -> d_widget); // BUG: make sure this destroys the shell
631 }
632 our UiForm_Parent :: v9_destroy ();
633 }
634
635 static void gui_button_cb_revert (UiForm me, GuiButtonEvent /* event */) {
636 for (int ifield = 1; ifield <= my numberOfFields; ifield ++)
637 UiField_setDefault (my field [ifield].get());
638 }
639
640 static void gui_dialog_cb_close (UiForm me) {
641 if (my cancelCallback)
642 my cancelCallback (me, my buttonClosure);
643 GuiThing_hide (my d_dialogForm);
644 if (my destroyWhenUnmanaged)
645 forget (me);
646 }
647 static void gui_button_cb_cancel (UiForm me, GuiButtonEvent /* event */) {
648 if (my cancelCallback)
649 my cancelCallback (me, my buttonClosure);
650 GuiThing_hide (my d_dialogForm);
651 if (my destroyWhenUnmanaged)
652 forget (me);
653 }
654
655 static void UiForm_okOrApply (UiForm me, GuiButton button, int hide) {
656 if (my allowExecutionHook && ! my allowExecutionHook (my allowExecutionClosure)) {
657 Melder_flushError (U"Cannot execute command window “", my name.get(), U"”.");
658 return;
659 }
660 try {
661 for (int ifield = 1; ifield <= my numberOfFields; ifield ++)
662 UiField_widgetToValue (my field [ifield].get());
663 } catch (MelderError) {
664 Melder_flushError (U"Please correct command window “", my name.get(), U"” or cancel.");
665 return;
666 }
667 if (my okButton) GuiThing_setSensitive (my okButton, false);
668 if (my applyButton) GuiThing_setSensitive (my applyButton, false);
669 if (my cancelButton) GuiThing_setSensitive (my cancelButton, false);
670 if (my revertButton) GuiThing_setSensitive (my revertButton, false);
671 if (my helpButton) GuiThing_setSensitive (my helpButton, false);
672 for (int i = 1; i <= my numberOfContinueButtons; i ++)
673 if (my continueButtons [i])
674 GuiThing_setSensitive (my continueButtons [i], false);
675
676 #if defined (_WIN32)
677 GdiFlush ();
678 #endif
679 if (my isPauseForm) {
680 for (int i = 1; i <= my numberOfContinueButtons; i ++)
681 if (button == my continueButtons [i])
682 my clickedContinueButton = i;
683 }
684 /*
685 Keep the gate for error handling.
686 */
687 try {
688 my okCallback (me, 0, nullptr, nullptr, nullptr, nullptr, false, my buttonClosure, my optionalEditor);
689 /*
690 Write everything to history. Before destruction!
691 */
692 if (! my isPauseForm) {
693 UiHistory_write (U"\n");
694 UiHistory_write_colonize (my invokingButtonTitle.get());
695 int size = my numberOfFields;
696 while (size >= 1 && _kUiField_type_isComment (my field [size] -> type))
697 size --; // ignore trailing fields without a value
698 int next = 0;
699 for (int ifield = 1; ifield <= size; ifield ++) {
700 UiField field = my field [ifield].get();
701 switch (field -> type)
702 {
703 case _kUiField_type::HEADING_:
704 case _kUiField_type::COMMENT_:
705 case _kUiField_type::CAPTION_:
706 {
707 // do nothing
708 }
709 break;
710 case _kUiField_type::REAL_:
711 case _kUiField_type::REAL_OR_UNDEFINED_:
712 case _kUiField_type::NONNEGATIVE_:
713 case _kUiField_type::POSITIVE_:
714 {
715 UiHistory_write (next -- ? U", " : U" ");
716 UiHistory_write (Melder_double (field -> realValue));
717 }
718 break;
719 case _kUiField_type::INTEGER_:
720 case _kUiField_type::NATURAL0_:
721 case _kUiField_type::NATURAL1_:
722 case _kUiField_type::CHANNEL_:
723 {
724 UiHistory_write (next -- ? U", " : U" ");
725 UiHistory_write (Melder_integer (field -> integerValue));
726 }
727 break;
728 case _kUiField_type::WORD_:
729 case _kUiField_type::SENTENCE_:
730 case _kUiField_type::TEXT_:
731 case _kUiField_type::FORMULA_:
732 case _kUiField_type::INFILE_:
733 case _kUiField_type::OUTFILE_:
734 case _kUiField_type::FOLDER_:
735 {
736 UiHistory_write (next -- ? U", \"" : U" \"");
737 UiHistory_write_expandQuotes (field -> stringValue.get());
738 UiHistory_write (U"\"");
739 }
740 break;
741 case _kUiField_type:: REALVECTOR_:
742 case _kUiField_type:: NONNEGATIVEVECTOR_:
743 case _kUiField_type:: POSITIVEVECTOR_:
744 {
745 if (NUMisEmpty (field -> realVectorValue.get())) {
746 UiHistory_write (next -- ? U", zero# (0)" : U" zero# (0)");
747 } else {
748 UiHistory_write (next -- ? U", { " : U" { ");
749 for (integer i = 1; i <= field -> realVectorValue.size; i ++) {
750 UiHistory_write (Melder_double (field -> realVectorValue [i]));
751 UiHistory_write (i == field -> realVectorValue.size ? U" }" : U", ");
752 }
753 }
754 } break;
755 case _kUiField_type:: INTEGERVECTOR_:
756 case _kUiField_type:: NATURAL0VECTOR_:
757 case _kUiField_type:: NATURAL1VECTOR_:
758 {
759 if (NUMisEmpty (field -> integerVectorValue.get())) {
760 UiHistory_write (next -- ? U", zero# (0)" : U" zero# (0)");
761 } else {
762 UiHistory_write (next -- ? U", { " : U" { ");
763 for (integer i = 1; i <= field -> integerVectorValue.size; i ++) {
764 UiHistory_write (Melder_integer (field -> integerVectorValue [i]));
765 UiHistory_write (i == field -> integerVectorValue.size ? U" }" : U", ");
766 }
767 }
768 } break;
769 case _kUiField_type:: REALMATRIX_:
770 {
771 if (NUMisEmpty (field -> numericMatrixValue.get())) {
772 UiHistory_write (next -- ? U", zero## (0, 0)" : U" zero## (0, 0)");
773 } else {
774 UiHistory_write (next -- ? U", { " : U" { ");
775 for (integer irow = 1; irow <= field -> numericMatrixValue.nrow; irow ++) {
776 UiHistory_write (U"{ ");
777 for (integer icol = 1; icol <= field -> numericMatrixValue.ncol; icol ++) {
778 UiHistory_write (Melder_double (field -> numericMatrixValue [irow] [icol]));
779 UiHistory_write (icol == field -> numericMatrixValue.ncol ? U" }" : U", ");
780 }
781 UiHistory_write (irow == field -> numericMatrixValue.nrow ? U" }" : U", ");
782 }
783 }
784 } break;
785 case _kUiField_type:: STRINGARRAY_:
786 {
787 if (NUMisEmpty (field -> stringArrayValue.get())) {
788 UiHistory_write_expandQuotes (next -- ? U", empty$# (0)" : U" empty$# (0)");
789 } else {
790 UiHistory_write (next -- ? U", { " : U" { ");
791 for (integer i = 1; i <= field -> stringArrayValue.size; i ++) {
792 UiHistory_write (U"\"");
793 UiHistory_write_expandQuotes (field -> stringArrayValue [i].get());
794 UiHistory_write (U"\"");
795 UiHistory_write (i == field -> stringArrayValue.size ? U" }" : U", ");
796 }
797 }
798 } break;
799 case _kUiField_type::BOOLEAN_:
800 {
801 UiHistory_write (field -> integerValue ? (next -- ? U", \"yes\"" : U" \"yes\"") : (next -- ? U", \"no\"" : U" \"no\""));
802 }
803 break;
804 case _kUiField_type::CHOICE_:
805 case _kUiField_type::OPTIONMENU_:
806 {
807 UiOption b = field -> options.at [field -> integerValue];
808 UiHistory_write (next -- ? U", \"" : U" \"");
809 UiHistory_write_expandQuotes (b -> name.get());
810 UiHistory_write (U"\"");
811 }
812 break;
813 case _kUiField_type::LIST_:
814 {
815 UiHistory_write (next -- ? U", \"" : U" \"");
816 UiHistory_write_expandQuotes (field -> strings [field -> integerValue]);
817 UiHistory_write (U"\"");
818 }
819 break;
820 case _kUiField_type::COLOUR_:
821 {
822 conststring32 colourString = MelderColour_namePrettyOrNull (field -> colourValue);
823 if (colourString) {
824 UiHistory_write (next -- ? U", \"" : U" \"");
825 UiHistory_write (colourString);
826 UiHistory_write (U"\"");
827 } else if (field -> colourValue. isGrey()) {
828 UiHistory_write (next -- ? U", " : U" ");
829 UiHistory_write (Melder_double (field -> colourValue. red));
830 } else {
831 colourString = MelderColour_nameRGB (field -> colourValue);
832 UiHistory_write (next -- ? U", " : U" ");
833 UiHistory_write (colourString);
834 }
835 }
836 }
837 }
838 }
839 if (hide) {
840 GuiThing_hide (my d_dialogForm);
841 if (my destroyWhenUnmanaged) {
842 forget (me);
843 return;
844 }
845 }
846 } catch (MelderError) {
847 /*
848 If a solution has already been suggested, or the "error" was actually a conscious user action, do not add anything more.
849 */
850 if (! str32str (Melder_getError (), U"Please ") && ! str32str (Melder_getError (), U"You could ") &&
851 ! str32str (Melder_getError (), U"You interrupted ") && ! str32str (Melder_getError (), U"Interrupted!"))
852 {
853 /*
854 Otherwise, show a generic message.
855 */
856 if (str32str (Melder_getError (), U"Selection changed!")) {
857 Melder_appendError (U"Please change the selection in the object list, or click Cancel in the command window “",
858 my name.get(), U"”.");
859 } else {
860 Melder_appendError (U"Please change something in the command window “",
861 my name.get(), U"”, or click Cancel in that window.");
862 }
863 }
864 Melder_flushError ();
865 }
866 if (my okButton) GuiThing_setSensitive (my okButton, true);
867 if (my applyButton) GuiThing_setSensitive (my applyButton, true);
868 if (my cancelButton) GuiThing_setSensitive (my cancelButton, true);
869 if (my revertButton) GuiThing_setSensitive (my revertButton, true);
870 if (my helpButton) GuiThing_setSensitive (my helpButton, true);
871 for (int i = 1; i <= my numberOfContinueButtons; i ++)
872 if (my continueButtons [i])
873 GuiThing_setSensitive (my continueButtons [i], true);
874 }
875
876 static void gui_button_cb_ok (UiForm me, GuiButtonEvent event) {
877 UiForm_okOrApply (me, event -> button, true);
878 }
879
880 static void gui_dialog_cb_default (UiForm me) {
881 UiForm_okOrApply (me,
882 my defaultContinueButton >= 1 && my defaultContinueButton <= my numberOfContinueButtons ? my continueButtons [my defaultContinueButton] : nullptr,
883 true
884 );
885 }
886
887 static void gui_button_cb_apply (UiForm me, GuiButtonEvent event) {
888 UiForm_okOrApply (me, event -> button, false);
889 }
890
891 static void gui_button_cb_help (UiForm me, GuiButtonEvent /* event */) {
892 Melder_help (my helpTitle.get());
893 }
894
895 static void gui_button_cb_browseInfile (UiField me, GuiButtonEvent /* event */) {
896 autoStringSet chosenFilePath = GuiFileSelect_getInfileNames (nullptr, U"Open file", false);
897 if (chosenFilePath->size != 0)
898 GuiText_setString (my text, chosenFilePath->at [1] -> string.get());
899 }
900 static void gui_button_cb_browseOutfile (UiField me, GuiButtonEvent /* event */) {
901 autostring32 chosenFilePath = GuiFileSelect_getOutfileName (nullptr, U"Save file", U"");
902 if (chosenFilePath)
903 GuiText_setString (my text, chosenFilePath.get());
904 }
905 static void gui_button_cb_browseFolder (UiField me, GuiButtonEvent /* event */) {
906 autostring32 chosenFolderPath = GuiFileSelect_getFolderName (nullptr, U"Choose folder");
907 if (chosenFolderPath)
908 GuiText_setString (my text, chosenFolderPath.get());
909 }
910
911 autoUiForm UiForm_create (GuiWindow parent, Editor optionalEditor, conststring32 title,
912 UiCallback okCallback, void *buttonClosure,
913 conststring32 invokingButtonTitle, conststring32 helpTitle)
914 {
915 autoUiForm me = Thing_new (UiForm);
916 my d_dialogParent = parent;
917 my optionalEditor = optionalEditor;
918 Thing_setName (me.get(), title);
919 my okCallback = okCallback;
920 my buttonClosure = buttonClosure;
921 my invokingButtonTitle = Melder_dup (invokingButtonTitle);
922 my helpTitle = Melder_dup (helpTitle);
923 return me;
924 }
925
926 void UiForm_setPauseForm (UiForm me,
927 int numberOfContinueButtons, int defaultContinueButton, int cancelContinueButton,
928 conststring32 continue1, conststring32 continue2, conststring32 continue3,
929 conststring32 continue4, conststring32 continue5, conststring32 continue6,
930 conststring32 continue7, conststring32 continue8, conststring32 continue9,
931 conststring32 continue10,
932 void (*cancelCallback) (UiForm dia, void *closure))
933 {
934 my isPauseForm = true;
935 my numberOfContinueButtons = numberOfContinueButtons;
936 my defaultContinueButton = defaultContinueButton;
937 my cancelContinueButton = cancelContinueButton;
938 my continueTexts [1] = Melder_dup (continue1);
939 my continueTexts [2] = Melder_dup (continue2);
940 my continueTexts [3] = Melder_dup (continue3);
941 my continueTexts [4] = Melder_dup (continue4);
942 my continueTexts [5] = Melder_dup (continue5);
943 my continueTexts [6] = Melder_dup (continue6);
944 my continueTexts [7] = Melder_dup (continue7);
945 my continueTexts [8] = Melder_dup (continue8);
946 my continueTexts [9] = Melder_dup (continue9);
947 my continueTexts [10] = Melder_dup (continue10);
948 my cancelCallback = cancelCallback;
949 }
950
951 static void commonOkCallback (UiForm /* dia */, integer /* narg */, Stackel /* args */, conststring32 /* sendingString */,
952 Interpreter interpreter, conststring32 /* invokingButtonTitle */, bool /* modified */, void *closure, Editor /* optionalEditor */)
953 {
954 EditorCommand cmd = (EditorCommand) closure;
955 cmd -> commandCallback (cmd -> sender, cmd, cmd -> d_uiform.get(), 0, nullptr, nullptr, interpreter);
956 }
957
958 autoUiForm UiForm_createE (EditorCommand cmd, conststring32 title, conststring32 invokingButtonTitle, conststring32 helpTitle) {
959 Editor editor = cmd -> d_editor;
960 autoUiForm dia = UiForm_create (editor -> windowForm, editor, title, commonOkCallback, cmd, invokingButtonTitle, helpTitle);
961 dia -> command = cmd;
962 return dia;
963 }
964
965 static UiField UiForm_addField (UiForm me, _kUiField_type type, conststring32 labelText) {
966 if (my numberOfFields == MAXIMUM_NUMBER_OF_FIELDS)
967 Melder_throw (U"Cannot have more than ", MAXIMUM_NUMBER_OF_FIELDS, U"fields in a form.");
968 my field [++ my numberOfFields] = UiField_create (type, labelText);
969 return my field [my numberOfFields].get();
970 }
971
972 UiField UiForm_addReal (UiForm me, double *variable, conststring32 variableName,
973 conststring32 labelText, conststring32 defaultValue)
974 {
975 UiField thee = UiForm_addField (me, _kUiField_type::REAL_, labelText);
976 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
977 thy stringDefaultValue = Melder_dup (defaultValue);
978 thy realVariable = variable;
979 thy variableName = variableName;
980 return thee;
981 }
982
983 UiField UiForm_addRealOrUndefined (UiForm me, double *variable, conststring32 variableName,
984 conststring32 labelText, conststring32 defaultValue)
985 {
986 UiField thee = UiForm_addField (me, _kUiField_type::REAL_OR_UNDEFINED_, labelText);
987 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
988 thy stringDefaultValue = Melder_dup (defaultValue);
989 thy realVariable = variable;
990 thy variableName = variableName;
991 return thee;
992 }
993
994 UiField UiForm_addNonnegative (UiForm me, double *variable, conststring32 variableName,
995 conststring32 labelText, conststring32 defaultValue)
996 {
997 UiField thee = UiForm_addField (me, _kUiField_type::NONNEGATIVE_, labelText);
998 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
999 thy stringDefaultValue = Melder_dup (defaultValue);
1000 thy realVariable = variable;
1001 thy variableName = variableName;
1002 return thee;
1003 }
1004
1005 UiField UiForm_addPositive (UiForm me, double *variable, conststring32 variableName,
1006 conststring32 labelText, conststring32 defaultValue)
1007 {
1008 UiField thee = UiForm_addField (me, _kUiField_type::POSITIVE_, labelText);
1009 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1010 thy stringDefaultValue = Melder_dup (defaultValue);
1011 thy realVariable = variable;
1012 thy variableName = variableName;
1013 return thee;
1014 }
1015
1016 UiField UiForm_addInteger (UiForm me, integer *variable, conststring32 variableName,
1017 conststring32 labelText, conststring32 defaultValue)
1018 {
1019 UiField thee = UiForm_addField (me, _kUiField_type::INTEGER_, labelText);
1020 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1021 thy stringDefaultValue = Melder_dup (defaultValue);
1022 thy integerVariable = variable;
1023 thy variableName = variableName;
1024 return thee;
1025 }
1026
1027 UiField UiForm_addNatural0 (UiForm me, integer *variable, conststring32 variableName,
1028 conststring32 labelText, conststring32 defaultValue)
1029 {
1030 UiField thee = UiForm_addField (me, _kUiField_type::NATURAL0_, labelText);
1031 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1032 thy stringDefaultValue = Melder_dup (defaultValue);
1033 thy integerVariable = variable;
1034 thy variableName = variableName;
1035 return thee;
1036 }
1037
1038 UiField UiForm_addNatural1 (UiForm me, integer *variable, conststring32 variableName,
1039 conststring32 labelText, conststring32 defaultValue)
1040 {
1041 UiField thee = UiForm_addField (me, _kUiField_type::NATURAL1_, labelText);
1042 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1043 thy stringDefaultValue = Melder_dup (defaultValue);
1044 thy integerVariable = variable;
1045 thy variableName = variableName;
1046 return thee;
1047 }
1048
1049 UiField UiForm_addWord (UiForm me, conststring32 *variable, conststring32 variableName,
1050 conststring32 labelText, conststring32 defaultValue)
1051 {
1052 UiField thee = UiForm_addField (me, _kUiField_type::WORD_, labelText);
1053 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1054 thy stringDefaultValue = Melder_dup (defaultValue);
1055 thy stringVariable = variable;
1056 thy variableName = variableName;
1057 return thee;
1058 }
1059
1060 UiField UiForm_addSentence (UiForm me, conststring32 *variable, conststring32 variableName,
1061 conststring32 labelText, conststring32 defaultValue)
1062 {
1063 UiField thee = UiForm_addField (me, _kUiField_type::SENTENCE_, labelText);
1064 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1065 thy stringDefaultValue = Melder_dup (defaultValue);
1066 thy stringVariable = variable;
1067 thy variableName = variableName;
1068 return thee;
1069 }
1070
1071 UiField UiForm_addHeading (UiForm me, conststring32 *variable, conststring32 labelText) {
1072 UiField thee = UiForm_addField (me, _kUiField_type::HEADING_, U""); // this field gets no name; so that the user can give it any title
1073 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1074 thy stringVariable = variable;
1075 thy stringValue = Melder_dup (labelText);
1076 return thee;
1077 }
1078
1079 UiField UiForm_addComment (UiForm me, conststring32 *variable, conststring32 labelText) {
1080 UiField thee = UiForm_addField (me, _kUiField_type::COMMENT_, U""); // this field gets no name; so that the user can give it any title
1081 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1082 thy stringVariable = variable;
1083 thy stringValue = Melder_dup (labelText);
1084 return thee;
1085 }
1086
1087 UiField UiForm_addCaption (UiForm me, conststring32 *variable, conststring32 labelText) {
1088 UiField thee = UiForm_addField (me, _kUiField_type::CAPTION_, U""); // this field gets no name; so that the user can give it any title
1089 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1090 thy stringVariable = variable;
1091 thy stringValue = Melder_dup (labelText);
1092 return thee;
1093 }
1094
1095 UiField UiForm_addBoolean (UiForm me, bool *variable, conststring32 variableName,
1096 conststring32 labelText, bool defaultValue)
1097 {
1098 UiField thee = UiForm_addField (me, _kUiField_type::BOOLEAN_, labelText);
1099 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1100 thy integerDefaultValue = defaultValue;
1101 thy boolVariable = variable;
1102 thy variableName = variableName;
1103 return thee;
1104 }
1105
1106 UiField UiForm_addText (UiForm me, conststring32 *variable, conststring32 variableName,
1107 conststring32 labelText, conststring32 defaultValue, integer numberOfLines)
1108 {
1109 UiField thee = UiForm_addField (me, _kUiField_type::TEXT_, labelText);
1110 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1111 thy stringDefaultValue = Melder_dup (defaultValue);
1112 thy stringVariable = variable;
1113 thy variableName = variableName;
1114 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1115 return thee;
1116 }
1117
1118 UiField UiForm_addFormula (UiForm me, conststring32 *variable, conststring32 variableName,
1119 conststring32 labelText, conststring32 defaultValue, integer numberOfLines)
1120 {
1121 UiField thee = UiForm_addField (me, _kUiField_type::FORMULA_, labelText);
1122 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1123 thy stringDefaultValue = Melder_dup (defaultValue);
1124 thy stringVariable = variable;
1125 thy variableName = variableName;
1126 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1127 return thee;
1128 }
1129
1130 UiField UiForm_addInfile (UiForm me, conststring32 *variable, conststring32 variableName,
1131 conststring32 labelText, conststring32 defaultValue, integer numberOfLines)
1132 {
1133 UiField thee = UiForm_addField (me, _kUiField_type::INFILE_, labelText);
1134 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1135 thy stringDefaultValue = Melder_dup (defaultValue);
1136 thy stringVariable = variable;
1137 thy variableName = variableName;
1138 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1139 return thee;
1140 }
1141
1142 UiField UiForm_addOutfile (UiForm me, conststring32 *variable, conststring32 variableName,
1143 conststring32 labelText, conststring32 defaultValue, integer numberOfLines)
1144 {
1145 UiField thee = UiForm_addField (me, _kUiField_type::OUTFILE_, labelText);
1146 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1147 thy stringDefaultValue = Melder_dup (defaultValue);
1148 thy stringVariable = variable;
1149 thy variableName = variableName;
1150 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1151 return thee;
1152 }
1153
1154 UiField UiForm_addFolder (UiForm me, conststring32 *variable, conststring32 variableName,
1155 conststring32 labelText, conststring32 defaultValue, integer numberOfLines)
1156 {
1157 UiField thee = UiForm_addField (me, _kUiField_type::FOLDER_, labelText);
1158 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1159 thy stringDefaultValue = Melder_dup (defaultValue);
1160 thy stringVariable = variable;
1161 thy variableName = variableName;
1162 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1163 return thee;
1164 }
1165
1166 UiField UiForm_addRealVector (UiForm me, constVEC *variable, conststring32 variableName,
1167 conststring32 labelText, kUi_realVectorFormat defaultFormat, conststring32 defaultValue, integer numberOfLines)
1168 {
1169 UiField thee = UiForm_addField (me, _kUiField_type::REALVECTOR_, labelText);
1170 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1171 thy realVectorDefaultFormat = defaultFormat;
1172 thy stringDefaultValue = Melder_dup (defaultValue);
1173 thy realVectorVariable = variable;
1174 thy variableName = variableName;
1175 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1176 return thee;
1177 }
1178
1179 UiField UiForm_addNonnegativeVector (UiForm me, constVEC *variable, conststring32 variableName,
1180 conststring32 labelText, kUi_realVectorFormat defaultFormat, conststring32 defaultValue, integer numberOfLines)
1181 {
1182 UiField thee = UiForm_addField (me, _kUiField_type::NONNEGATIVEVECTOR_, labelText);
1183 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1184 thy realVectorDefaultFormat = defaultFormat;
1185 thy stringDefaultValue = Melder_dup (defaultValue);
1186 thy realVectorVariable = variable;
1187 thy variableName = variableName;
1188 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1189 return thee;
1190 }
1191
1192 UiField UiForm_addPositiveVector (UiForm me, constVEC *variable, conststring32 variableName,
1193 conststring32 labelText, kUi_realVectorFormat defaultFormat, conststring32 defaultValue, integer numberOfLines)
1194 {
1195 UiField thee = UiForm_addField (me, _kUiField_type::POSITIVEVECTOR_, labelText);
1196 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1197 thy realVectorDefaultFormat = defaultFormat;
1198 thy stringDefaultValue = Melder_dup (defaultValue);
1199 thy realVectorVariable = variable;
1200 thy variableName = variableName;
1201 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1202 return thee;
1203 }
1204
1205 UiField UiForm_addIntegerVector (UiForm me, constINTVEC *variable, conststring32 variableName,
1206 conststring32 labelText, kUi_integerVectorFormat defaultFormat, conststring32 defaultValue, integer numberOfLines)
1207 {
1208 UiField thee = UiForm_addField (me, _kUiField_type::INTEGERVECTOR_, labelText);
1209 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1210 thy integerVectorDefaultFormat = defaultFormat;
1211 thy stringDefaultValue = Melder_dup (defaultValue);
1212 thy integerVectorVariable = variable;
1213 thy variableName = variableName;
1214 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1215 return thee;
1216 }
1217
1218 UiField UiForm_addNatural0Vector (UiForm me, constINTVEC *variable, conststring32 variableName,
1219 conststring32 labelText, kUi_integerVectorFormat defaultFormat, conststring32 defaultValue, integer numberOfLines)
1220 {
1221 UiField thee = UiForm_addField (me, _kUiField_type::NATURAL0VECTOR_, labelText);
1222 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1223 thy integerVectorDefaultFormat = defaultFormat;
1224 thy stringDefaultValue = Melder_dup (defaultValue);
1225 thy integerVectorVariable = variable;
1226 thy variableName = variableName;
1227 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1228 return thee;
1229 }
1230
1231 UiField UiForm_addNatural1Vector (UiForm me, constINTVEC *variable, conststring32 variableName,
1232 conststring32 labelText, kUi_integerVectorFormat defaultFormat, conststring32 defaultValue, integer numberOfLines)
1233 {
1234 UiField thee = UiForm_addField (me, _kUiField_type::NATURAL1VECTOR_, labelText);
1235 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1236 thy integerVectorDefaultFormat = defaultFormat;
1237 thy stringDefaultValue = Melder_dup (defaultValue);
1238 thy integerVectorVariable = variable;
1239 thy variableName = variableName;
1240 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1241 return thee;
1242 }
1243
1244 UiField UiForm_addRealMatrix (UiForm me, constMAT *variable, conststring32 variableName,
1245 conststring32 labelText, constMATVU defaultValue, integer numberOfLines)
1246 {
1247 UiField thee = UiForm_addField (me, _kUiField_type::REALMATRIX_, labelText);
1248 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1249 thy numericMatrixDefaultValue = copy_MAT (defaultValue);
1250 thy numericMatrixVariable = variable;
1251 thy variableName = variableName;
1252 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1253 return thee;
1254 }
1255
1256 UiField UiForm_addStringArray (UiForm me, constSTRVEC *variable, conststring32 variableName,
1257 conststring32 labelText, constSTRVEC defaultValue, integer numberOfLines)
1258 {
1259 UiField thee = UiForm_addField (me, _kUiField_type::STRINGARRAY_, labelText);
1260 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1261 thy stringArrayDefaultValue = copy_STRVEC (defaultValue);
1262 thy stringArrayFormat = theStringArrayFormat;
1263 thy stringArrayVariable = variable;
1264 thy variableName = variableName;
1265 thy numberOfLines = Melder_clipped (1_integer, numberOfLines, 33_integer);
1266 return thee;
1267 }
1268
1269 UiField UiForm_addChoice (UiForm me, int *intVariable, conststring32 *stringVariable, conststring32 variableName,
1270 conststring32 labelText, int defaultValue, int base)
1271 {
1272 UiField thee = UiForm_addField (me, _kUiField_type::CHOICE_, labelText);
1273 my referenceToLatestUsedChoiceOrOptionMenu = thee;
1274 thy integerDefaultValue = defaultValue;
1275 thy intVariable = intVariable;
1276 thy stringVariable = stringVariable;
1277 thy variableName = variableName;
1278 thy subtract = ( base == 1 ? 0 : 1 );
1279 return thee;
1280 }
1281
1282 UiField UiForm_addChoiceEnum (UiForm me, int *intVariable, conststring32 *stringVariable, conststring32 variableName,
1283 conststring32 labelText, int defaultValue, int base, enum_generic_getValue getValueFunction)
1284 {
1285 UiField thee = UiForm_addField (me, _kUiField_type::CHOICE_, labelText);
1286 my referenceToLatestUsedChoiceOrOptionMenu = thee;
1287 thy integerDefaultValue = defaultValue;
1288 thy intVariable = intVariable;
1289 thy stringVariable = stringVariable;
1290 thy variableName = variableName;
1291 thy subtract = ( base == 1 ? 0 : 1 );
1292 thy getValueFunction = getValueFunction;
1293 return thee;
1294 }
1295
1296 UiField UiForm_addOptionMenu (UiForm me, int *intVariable, conststring32 *stringVariable, conststring32 variableName,
1297 conststring32 labelText, int defaultValue, int base)
1298 {
1299 UiField thee = UiForm_addField (me, _kUiField_type::OPTIONMENU_, labelText);
1300 my referenceToLatestUsedChoiceOrOptionMenu = thee;
1301 thy integerDefaultValue = defaultValue;
1302 thy intVariable = intVariable;
1303 thy stringVariable = stringVariable;
1304 thy variableName = variableName;
1305 thy subtract = ( base == 1 ? 0 : 1 );
1306 return thee;
1307 }
1308
1309 UiField UiForm_addOptionMenuEnum (UiForm me, int *intVariable, conststring32 *stringVariable, conststring32 variableName,
1310 conststring32 labelText, int defaultValue, int base, enum_generic_getValue getValueFunction)
1311 {
1312 UiField thee = UiForm_addField (me, _kUiField_type::OPTIONMENU_, labelText);
1313 my referenceToLatestUsedChoiceOrOptionMenu = thee;
1314 thy integerDefaultValue = defaultValue;
1315 thy intVariable = intVariable;
1316 thy stringVariable = stringVariable;
1317 thy variableName = variableName;
1318 thy subtract = ( base == 1 ? 0 : 1 );
1319 thy getValueFunction = getValueFunction;
1320 return thee;
1321 }
1322
1323 UiField UiForm_addList (UiForm me, integer *integerVariable, conststring32 *stringVariable, conststring32 variableName,
1324 conststring32 labelText, constSTRVEC strings, integer defaultValue)
1325 {
1326 UiField thee = UiForm_addField (me, _kUiField_type::LIST_, labelText);
1327 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1328 thy strings = strings;
1329 thy integerDefaultValue = defaultValue;
1330 thy integerVariable = integerVariable;
1331 thy stringVariable = stringVariable;
1332 thy variableName = variableName;
1333 return thee;
1334 }
1335
1336 UiField UiForm_addColour (UiForm me, MelderColour *colourVariable, conststring32 variableName,
1337 conststring32 labelText, conststring32 defaultValue)
1338 {
1339 UiField thee = UiForm_addField (me, _kUiField_type::COLOUR_, labelText);
1340 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1341 thy stringDefaultValue = Melder_dup (defaultValue);
1342 thy colourVariable = colourVariable;
1343 thy variableName = variableName;
1344 return thee;
1345 }
1346
1347 UiField UiForm_addChannel (UiForm me, integer *variable, conststring32 variableName,
1348 conststring32 labelText, conststring32 defaultValue)
1349 {
1350 UiField thee = UiForm_addField (me, _kUiField_type::CHANNEL_, labelText);
1351 my referenceToLatestUsedChoiceOrOptionMenu = nullptr;
1352 thy stringDefaultValue = Melder_dup (defaultValue);
1353 thy integerVariable = variable;
1354 thy variableName = variableName;
1355 return thee;
1356 }
1357
1358 #define DIALOG_X 150
1359 #define DIALOG_Y 70
1360 #define HELP_BUTTON_WIDTH 60
1361 #define STANDARDS_BUTTON_WIDTH 100
1362 #define REVERT_BUTTON_WIDTH 60
1363 #define STOP_BUTTON_WIDTH 50
1364 #define HELP_BUTTON_X 20
1365 #define LIST_HEIGHT 192
1366
1367 static MelderString theFinishBuffer;
1368
1369 static void appendColon () {
1370 integer length = theFinishBuffer.length;
1371 if (length < 1)
1372 return;
1373 char32 lastCharacter = theFinishBuffer.string [length - 1];
1374 if (lastCharacter == U':' || lastCharacter == U'?' || lastCharacter == U'.')
1375 return;
1376 MelderString_appendCharacter (& theFinishBuffer, U':');
1377 }
1378
1379 static int multiLineTextHeight (integer numberOfLines) {
1380 if (numberOfLines <= 1)
1381 return Gui_TEXTFIELD_HEIGHT;
1382 #if defined (macintosh)
1383 return numberOfLines * (Gui_TEXTFIELD_HEIGHT - 9) + 21; // 15 is the minimum, but 21 gives some feedback about what is outside (2020-10-19)
1384 #elif defined (_WIN32)
1385 return numberOfLines * (Gui_TEXTFIELD_HEIGHT - 4) + 21; // 21 is the mininum (2020-10-19)
1386 #elif defined (linux)
1387 return numberOfLines * (Gui_TEXTFIELD_HEIGHT - 8) + 15; // 13 is the minimum, but if there is no horizontal scrollbar, there is a line more (2020-10-19)
1388 #else
1389 return numberOfLines * Gui_TEXTFIELD_HEIGHT;
1390 #endif
1391 }
1392
1393 void UiForm_finish (UiForm me) {
1394 if (! my d_dialogParent && ! my isPauseForm)
1395 return;
1396
1397 int size = my numberOfFields;
1398 int dialogHeight = 0;
1399 const int textFieldHeight = Gui_TEXTFIELD_HEIGHT;
1400 const int headerLabelHeight = textFieldHeight
1401 #ifdef _WIN32
1402 - 6;
1403 #else
1404 - 10;
1405 #endif
1406 const int commentContinuationHeight = headerLabelHeight - 5;
1407 int dialogWidth = 520, dialogCentre = dialogWidth / 2, fieldX = dialogCentre + Gui_LABEL_SPACING / 2;
1408 int labelWidth = fieldX - Gui_LABEL_SPACING - Gui_LEFT_DIALOG_SPACING, fieldWidth = labelWidth, halfFieldWidth = fieldWidth / 2 - 6;
1409
1410 GuiForm form;
1411 bool okButtonIsDefault = true;
1412
1413 /*
1414 Compute height. Cannot leave this to the default geometry management system.
1415 */
1416 for (integer ifield = 1; ifield <= my numberOfFields; ifield ++ ) {
1417 UiField thee = my field [ifield].get(), previous = my field [ifield - 1].get();
1418 dialogHeight +=
1419 ifield == 1 ?
1420 Gui_TOP_DIALOG_SPACING
1421 : thy type == _kUiField_type::CHOICE_ || previous -> type == _kUiField_type::CHOICE_ ?
1422 Gui_VERTICAL_DIALOG_SPACING_DIFFERENT
1423 : thy type >= _kUiField_type::LABELLED_TEXT_MIN_ && thy type <= _kUiField_type::LABELLED_TEXT_MAX_ && str32nequ (thy name.get(), U"right ", 6) &&
1424 previous -> type >= _kUiField_type::LABELLED_TEXT_MIN_ && previous -> type <= _kUiField_type::LABELLED_TEXT_MAX_ &&
1425 str32nequ (previous -> name.get(), U"left ", 5) ?
1426 - textFieldHeight
1427 :
1428 Gui_VERTICAL_DIALOG_SPACING_SAME;
1429 const bool thouHastVerticallyAddedLabel =
1430 thy type == _kUiField_type::TEXT_ || thy type == _kUiField_type::FORMULA_ ||
1431 thy type == _kUiField_type::INFILE_ || thy type == _kUiField_type::OUTFILE_ || thy type == _kUiField_type::FOLDER_ ||
1432 thy type == _kUiField_type::REALVECTOR_ || thy type == _kUiField_type::NONNEGATIVEVECTOR_ || thy type == _kUiField_type::POSITIVEVECTOR_ ||
1433 thy type == _kUiField_type::INTEGERVECTOR_ || thy type == _kUiField_type::NATURAL0VECTOR_ || thy type == _kUiField_type::NATURAL1VECTOR_ ||
1434 thy type == _kUiField_type::REALMATRIX_ ||
1435 thy type == _kUiField_type::STRINGARRAY_
1436 ;
1437 if (thouHastVerticallyAddedLabel)
1438 dialogHeight += (headerLabelHeight + Gui_VERTICAL_DIALOG_SPACING_SAME) * !! thy labelText;
1439 thy y = dialogHeight;
1440 dialogHeight +=
1441 thy type == _kUiField_type::BOOLEAN_ ?
1442 Gui_CHECKBUTTON_HEIGHT
1443 : thy type == _kUiField_type::CHOICE_ ?
1444 thy options.size * Gui_RADIOBUTTON_HEIGHT + (thy options.size - 1) * Gui_RADIOBUTTON_SPACING
1445 : thy type == _kUiField_type::OPTIONMENU_ ?
1446 Gui_OPTIONMENU_HEIGHT
1447 : thy type == _kUiField_type::LIST_ ?
1448 LIST_HEIGHT
1449 : thy type == _kUiField_type::HEADING_ && thy stringValue [0] != U'\0' &&
1450 thy stringValue [Melder_length (thy stringValue.get()) - 1] != U'.' && ifield != my numberOfFields ?
1451 headerLabelHeight
1452 : thy type == _kUiField_type::COMMENT_ && thy stringValue [0] != U'\0' &&
1453 thy stringValue [Melder_length (thy stringValue.get()) - 1] == U':' && ifield != my numberOfFields ?
1454 headerLabelHeight
1455 : thy type == _kUiField_type::COMMENT_ && thy stringValue [0] != U'\0' &&
1456 thy stringValue [Melder_length (thy stringValue.get()) - 1] != U'.' && ifield != my numberOfFields ?
1457 commentContinuationHeight
1458 : thy type == _kUiField_type::CAPTION_ && thy stringValue [0] != U'\0' &&
1459 thy stringValue [Melder_length (thy stringValue.get()) - 1] != U'.' && ifield != my numberOfFields ?
1460 headerLabelHeight
1461 : thouHastVerticallyAddedLabel ?
1462 multiLineTextHeight (thy numberOfLines)
1463 :
1464 textFieldHeight;
1465 }
1466 dialogHeight += 2 * Gui_BOTTOM_DIALOG_SPACING + Gui_PUSHBUTTON_HEIGHT;
1467 my d_dialogForm = GuiDialog_create (my d_dialogParent, DIALOG_X, DIALOG_Y, dialogWidth, dialogHeight,
1468 my name.get(), gui_dialog_cb_close, me, GuiDialog_Modality::MODELESS);
1469 GuiDialog_setDefaultCallback (my d_dialogForm, gui_dialog_cb_default, me);
1470
1471 form = my d_dialogForm;
1472
1473 for (integer ifield = 1; ifield <= size; ifield ++) {
1474 UiField thee = my field [ifield].get();
1475 switch (thy type)
1476 {
1477 case _kUiField_type::REAL_:
1478 case _kUiField_type::REAL_OR_UNDEFINED_:
1479 case _kUiField_type::NONNEGATIVE_:
1480 case _kUiField_type::POSITIVE_:
1481 case _kUiField_type::INTEGER_:
1482 case _kUiField_type::NATURAL0_:
1483 case _kUiField_type::NATURAL1_:
1484 case _kUiField_type::WORD_:
1485 case _kUiField_type::SENTENCE_:
1486 case _kUiField_type::COLOUR_:
1487 case _kUiField_type::CHANNEL_:
1488 {
1489 int ylabel = thy y;
1490 #if defined (macintosh)
1491 ylabel += 3;
1492 #endif
1493 if (str32nequ (thy name.get(), U"left ", 5)) {
1494 MelderString_copy (& theFinishBuffer, thy labelText.get() + 5);
1495 appendColon ();
1496 thy label = GuiLabel_createShown (form, 0, Gui_LEFT_DIALOG_SPACING + labelWidth, ylabel, ylabel + textFieldHeight,
1497 theFinishBuffer.string, GuiLabel_RIGHT);
1498 thy text = GuiText_createShown (form, fieldX, fieldX + halfFieldWidth, thy y, thy y + Gui_TEXTFIELD_HEIGHT, 0);
1499 } else if (str32nequ (thy name.get(), U"right ", 6)) {
1500 thy text = GuiText_createShown (form, fieldX + halfFieldWidth + 12, fieldX + fieldWidth,
1501 thy y, thy y + Gui_TEXTFIELD_HEIGHT, 0);
1502 } else {
1503 MelderString_copy (& theFinishBuffer, thy labelText.get());
1504 appendColon ();
1505 thy label = GuiLabel_createShown (form, 0, Gui_LEFT_DIALOG_SPACING + labelWidth,
1506 ylabel, ylabel + textFieldHeight,
1507 theFinishBuffer.string, GuiLabel_RIGHT
1508 );
1509 thy text = GuiText_createShown (form, fieldX, fieldX + fieldWidth, // or once the dialog is a Form: - Gui_RIGHT_DIALOG_SPACING,
1510 thy y, thy y + Gui_TEXTFIELD_HEIGHT, 0);
1511 }
1512 }
1513 break;
1514 case _kUiField_type::TEXT_:
1515 {
1516 MelderString_copy (& theFinishBuffer, thy labelText.get());
1517 appendColon ();
1518 const int ylabel = thy y + 5 - headerLabelHeight - Gui_VERTICAL_DIALOG_SPACING_SAME;
1519 thy label = GuiLabel_createShown (form,
1520 Gui_LEFT_DIALOG_SPACING, dialogWidth /* allow to extend into the margin */,
1521 ylabel, ylabel + textFieldHeight,
1522 theFinishBuffer.string, 0
1523 );
1524 thy text = GuiText_createShown (form, Gui_LEFT_DIALOG_SPACING, dialogWidth - Gui_RIGHT_DIALOG_SPACING,
1525 thy y, thy y + multiLineTextHeight (thy numberOfLines), GuiText_INKWRAP | GuiText_SCROLLED);
1526 }
1527 break;
1528 case _kUiField_type::REALVECTOR_:
1529 case _kUiField_type::NONNEGATIVEVECTOR_:
1530 case _kUiField_type::POSITIVEVECTOR_:
1531 {
1532 MelderString_copy (& theFinishBuffer, thy labelText.get());
1533 appendColon ();
1534 const int ylabel = thy y + 5 - headerLabelHeight - Gui_VERTICAL_DIALOG_SPACING_SAME;
1535 thy label = GuiLabel_createShown (form,
1536 Gui_LEFT_DIALOG_SPACING, dialogWidth /* allow to extend into the margin */,
1537 ylabel, ylabel + textFieldHeight,
1538 theFinishBuffer.string, 0
1539 );
1540 thy optionMenu = GuiOptionMenu_createShown (form,
1541 dialogWidth - Gui_LEFT_DIALOG_SPACING - 200, dialogWidth - Gui_LEFT_DIALOG_SPACING,
1542 thy y - Gui_OPTIONMENU_HEIGHT, thy y, 0
1543 );
1544 for (int format = (int) kUi_realVectorFormat::MIN; format <= (int) kUi_realVectorFormat::MAX; format ++)
1545 GuiOptionMenu_addOption (thy optionMenu, kUi_realVectorFormat_getText ((kUi_realVectorFormat) format));
1546 //GuiOptionMenu_setValue (thy optionMenu, (int) thy realVectorDefaultFormat); // SUPERFLUOUS
1547 thy text = GuiText_createShown (form, Gui_LEFT_DIALOG_SPACING, dialogWidth - Gui_RIGHT_DIALOG_SPACING,
1548 thy y, thy y + multiLineTextHeight (thy numberOfLines), GuiText_INKWRAP | GuiText_SCROLLED);
1549 }
1550 break;
1551 case _kUiField_type::INTEGERVECTOR_:
1552 case _kUiField_type::NATURAL0VECTOR_:
1553 case _kUiField_type::NATURAL1VECTOR_:
1554 {
1555 MelderString_copy (& theFinishBuffer, thy labelText.get());
1556 appendColon ();
1557 const int ylabel = thy y + 5 - headerLabelHeight - Gui_VERTICAL_DIALOG_SPACING_SAME;
1558 thy label = GuiLabel_createShown (form,
1559 Gui_LEFT_DIALOG_SPACING, dialogWidth /* allow to extend into the margin */,
1560 ylabel, ylabel + textFieldHeight,
1561 theFinishBuffer.string, 0
1562 );
1563 thy optionMenu = GuiOptionMenu_createShown (form,
1564 dialogWidth - Gui_LEFT_DIALOG_SPACING - 200, dialogWidth - Gui_LEFT_DIALOG_SPACING,
1565 thy y - Gui_OPTIONMENU_HEIGHT, thy y, 0
1566 );
1567 for (int format = (int) kUi_integerVectorFormat::MIN; format <= (int) kUi_integerVectorFormat::MAX; format ++)
1568 GuiOptionMenu_addOption (thy optionMenu, kUi_integerVectorFormat_getText ((kUi_integerVectorFormat) format));
1569 //GuiOptionMenu_setValue (thy optionMenu, (int) thy integerVectorDefaultFormat); // SUPERFLUOUS
1570 thy text = GuiText_createShown (form, Gui_LEFT_DIALOG_SPACING, dialogWidth - Gui_RIGHT_DIALOG_SPACING,
1571 thy y, thy y + multiLineTextHeight (thy numberOfLines), GuiText_INKWRAP | GuiText_SCROLLED);
1572 }
1573 break;
1574 case _kUiField_type::REALMATRIX_:
1575 {
1576 MelderString_copy (& theFinishBuffer, thy labelText.get());
1577 appendColon ();
1578 const int ylabel = thy y + 5 - headerLabelHeight - Gui_VERTICAL_DIALOG_SPACING_SAME;
1579 thy label = GuiLabel_createShown (form,
1580 Gui_LEFT_DIALOG_SPACING, dialogWidth /* allow to extend into the margin */,
1581 ylabel, ylabel + textFieldHeight,
1582 theFinishBuffer.string, 0
1583 );
1584 thy text = GuiText_createShown (form, Gui_LEFT_DIALOG_SPACING, dialogWidth - Gui_RIGHT_DIALOG_SPACING,
1585 thy y, thy y + multiLineTextHeight (thy numberOfLines), GuiText_SCROLLED);
1586 thy optionMenu = GuiOptionMenu_createShown (form,
1587 dialogWidth - Gui_LEFT_DIALOG_SPACING - 200, dialogWidth - Gui_LEFT_DIALOG_SPACING,
1588 thy y - Gui_OPTIONMENU_HEIGHT, thy y, 0
1589 );
1590 for (int format = (int) kUi_realMatrixFormat::MIN; format <= (int) kUi_realMatrixFormat::MAX; format ++)
1591 GuiOptionMenu_addOption (thy optionMenu, kUi_realMatrixFormat_getText ((kUi_realMatrixFormat) format));
1592 GuiOptionMenu_setValue (thy optionMenu, (int) theRealMatrixFormat);
1593 okButtonIsDefault = false; // because otherwise, the Enter key would be ambiguous
1594 }
1595 break;
1596 case _kUiField_type::STRINGARRAY_:
1597 {
1598 MelderString_copy (& theFinishBuffer, thy labelText.get());
1599 appendColon ();
1600 const int ylabel = thy y + 5 - headerLabelHeight - Gui_VERTICAL_DIALOG_SPACING_SAME;
1601 thy label = GuiLabel_createShown (form,
1602 Gui_LEFT_DIALOG_SPACING, dialogWidth /* allow to extend into the margin */,
1603 ylabel, ylabel + textFieldHeight,
1604 theFinishBuffer.string, 0
1605 );
1606 thy text = GuiText_createShown (form, Gui_LEFT_DIALOG_SPACING, dialogWidth - Gui_RIGHT_DIALOG_SPACING,
1607 thy y, thy y + multiLineTextHeight (thy numberOfLines), GuiText_INKWRAP | GuiText_SCROLLED);
1608 thy optionMenu = GuiOptionMenu_createShown (form,
1609 dialogWidth - Gui_LEFT_DIALOG_SPACING - 200, dialogWidth - Gui_LEFT_DIALOG_SPACING,
1610 thy y - Gui_OPTIONMENU_HEIGHT, thy y, 0
1611 );
1612 for (int format = (int) kUi_stringArrayFormat::MIN; format <= (int) kUi_stringArrayFormat::MAX; format ++)
1613 GuiOptionMenu_addOption (thy optionMenu, kUi_stringArrayFormat_getText ((kUi_stringArrayFormat) format));
1614 GuiOptionMenu_setValue (thy optionMenu, (int) theStringArrayFormat);
1615 }
1616 break;
1617 case _kUiField_type::FORMULA_:
1618 {
1619 MelderString_copy (& theFinishBuffer, thy labelText.get());
1620 appendColon ();
1621 const int ylabel = thy y + 5 - headerLabelHeight - Gui_VERTICAL_DIALOG_SPACING_SAME;
1622 thy label = GuiLabel_createShown (form,
1623 Gui_LEFT_DIALOG_SPACING, dialogWidth /* allow to extend into the margin */,
1624 ylabel, ylabel + textFieldHeight,
1625 theFinishBuffer.string, 0
1626 );
1627 thy text = GuiText_createShown (form, Gui_LEFT_DIALOG_SPACING, dialogWidth - Gui_RIGHT_DIALOG_SPACING,
1628 thy y, thy y + multiLineTextHeight (thy numberOfLines), GuiText_INKWRAP | GuiText_SCROLLED);
1629 }
1630 break;
1631 case _kUiField_type::INFILE_:
1632 {
1633 MelderString_copy (& theFinishBuffer, thy labelText.get());
1634 appendColon ();
1635 const int ylabel = thy y + 5 - headerLabelHeight - Gui_VERTICAL_DIALOG_SPACING_SAME;
1636 thy label = GuiLabel_createShown (form,
1637 Gui_LEFT_DIALOG_SPACING, dialogWidth - Gui_LEFT_DIALOG_SPACING - 100,
1638 ylabel, ylabel + textFieldHeight,
1639 theFinishBuffer.string, 0
1640 );
1641 thy text = GuiText_createShown (form, Gui_LEFT_DIALOG_SPACING, dialogWidth - Gui_RIGHT_DIALOG_SPACING,
1642 thy y, thy y + multiLineTextHeight (thy numberOfLines), GuiText_CHARWRAP | GuiText_SCROLLED);
1643 thy pushButton = GuiButton_createShown (form,
1644 dialogWidth - Gui_LEFT_DIALOG_SPACING - 100, dialogWidth - Gui_LEFT_DIALOG_SPACING,
1645 thy y - 3 - Gui_PUSHBUTTON_HEIGHT, thy y - 3, U"Browse...", gui_button_cb_browseInfile, thee, 0
1646 );
1647 }
1648 break;
1649 case _kUiField_type::OUTFILE_:
1650 {
1651 MelderString_copy (& theFinishBuffer, thy labelText.get());
1652 appendColon ();
1653 const int ylabel = thy y + 5 - headerLabelHeight - Gui_VERTICAL_DIALOG_SPACING_SAME;
1654 thy label = GuiLabel_createShown (form,
1655 Gui_LEFT_DIALOG_SPACING, dialogWidth - Gui_LEFT_DIALOG_SPACING - 100,
1656 ylabel, ylabel + textFieldHeight,
1657 theFinishBuffer.string, 0
1658 );
1659 thy text = GuiText_createShown (form, Gui_LEFT_DIALOG_SPACING, dialogWidth - Gui_RIGHT_DIALOG_SPACING,
1660 thy y, thy y + multiLineTextHeight (thy numberOfLines), GuiText_CHARWRAP | GuiText_SCROLLED);
1661 thy pushButton = GuiButton_createShown (form,
1662 dialogWidth - Gui_LEFT_DIALOG_SPACING - 100, dialogWidth - Gui_LEFT_DIALOG_SPACING,
1663 thy y - 3 - Gui_PUSHBUTTON_HEIGHT, thy y - 3, U"Browse...", gui_button_cb_browseOutfile, thee, 0
1664 );
1665 }
1666 break;
1667 case _kUiField_type::FOLDER_:
1668 {
1669 MelderString_copy (& theFinishBuffer, thy labelText.get());
1670 appendColon ();
1671 const int ylabel = thy y + 5 - headerLabelHeight - Gui_VERTICAL_DIALOG_SPACING_SAME;
1672 thy label = GuiLabel_createShown (form,
1673 Gui_LEFT_DIALOG_SPACING, dialogWidth - Gui_LEFT_DIALOG_SPACING - 100,
1674 ylabel, ylabel + textFieldHeight,
1675 theFinishBuffer.string, 0
1676 );
1677 thy text = GuiText_createShown (form, Gui_LEFT_DIALOG_SPACING, dialogWidth - Gui_RIGHT_DIALOG_SPACING,
1678 thy y, thy y + multiLineTextHeight (thy numberOfLines), GuiText_CHARWRAP | GuiText_SCROLLED);
1679 thy pushButton = GuiButton_createShown (form,
1680 dialogWidth - Gui_LEFT_DIALOG_SPACING - 100, dialogWidth - Gui_LEFT_DIALOG_SPACING,
1681 thy y - 3 - Gui_PUSHBUTTON_HEIGHT, thy y - 3, U"Browse...", gui_button_cb_browseFolder, thee, 0
1682 );
1683 }
1684 break;
1685 case _kUiField_type::HEADING_:
1686 {
1687 int ylabel = thy y;
1688 #if defined (macintosh)
1689 ylabel += 2;
1690 #endif
1691 MelderString_copy (& theFinishBuffer, thy stringValue.get());
1692 thy label = GuiLabel_createShown (form,
1693 Gui_LEFT_DIALOG_SPACING, dialogWidth /* allow to extend into the margin */,
1694 ylabel + 5, ylabel + 5 + textFieldHeight,
1695 theFinishBuffer.string, GuiLabel_BOLD
1696 );
1697 }
1698 break;
1699 case _kUiField_type::COMMENT_:
1700 {
1701 int ylabel = thy y;
1702 #if defined (macintosh)
1703 ylabel += 0;
1704 #endif
1705 MelderString_copy (& theFinishBuffer, thy stringValue.get());
1706 thy label = GuiLabel_createShown (form,
1707 Gui_LEFT_DIALOG_SPACING, dialogWidth /* allow to extend into the margin */,
1708 ylabel + 5, ylabel + 5 + textFieldHeight,
1709 theFinishBuffer.string, 0
1710 );
1711 }
1712 break;
1713 case _kUiField_type::CAPTION_:
1714 {
1715 int ylabel = thy y;
1716 #if defined (macintosh)
1717 ylabel += 0;
1718 #endif
1719 MelderString_copy (& theFinishBuffer, thy stringValue.get());
1720 thy label = GuiLabel_createShown (form,
1721 Gui_LEFT_DIALOG_SPACING, dialogWidth /* allow to extend into the margin */,
1722 ylabel - 10, ylabel - 10 + textFieldHeight,
1723 theFinishBuffer.string, GuiLabel_RIGHT
1724 );
1725 }
1726 break;
1727 case _kUiField_type::CHOICE_:
1728 {
1729 int ylabel = thy y;
1730 #if defined (macintosh)
1731 ylabel += 1;
1732 #endif
1733 MelderString_copy (& theFinishBuffer, thy labelText.get());
1734 appendColon ();
1735 thy label = GuiLabel_createShown (form, Gui_LEFT_DIALOG_SPACING, Gui_LEFT_DIALOG_SPACING + labelWidth, ylabel, ylabel + Gui_RADIOBUTTON_HEIGHT,
1736 theFinishBuffer.string, GuiLabel_RIGHT);
1737 GuiRadioGroup_begin ();
1738 for (integer ibutton = 1; ibutton <= thy options.size; ibutton ++) {
1739 UiOption button = thy options.at [ibutton];
1740 MelderString_copy (& theFinishBuffer, button -> name.get());
1741 button -> radioButton = GuiRadioButton_createShown (form,
1742 fieldX, dialogWidth /* allow to extend into the margin */,
1743 thy y + (ibutton - 1) * (Gui_RADIOBUTTON_HEIGHT + Gui_RADIOBUTTON_SPACING),
1744 thy y + (ibutton - 1) * (Gui_RADIOBUTTON_HEIGHT + Gui_RADIOBUTTON_SPACING) + Gui_RADIOBUTTON_HEIGHT,
1745 theFinishBuffer.string, nullptr, nullptr, 0
1746 );
1747 }
1748 GuiRadioGroup_end ();
1749 }
1750 break;
1751 case _kUiField_type::OPTIONMENU_:
1752 {
1753 int ylabel = thy y;
1754 #if defined (macintosh)
1755 ylabel += 2;
1756 #endif
1757 MelderString_copy (& theFinishBuffer, thy labelText.get());
1758 appendColon ();
1759 thy label = GuiLabel_createShown (form, Gui_LEFT_DIALOG_SPACING, Gui_LEFT_DIALOG_SPACING + labelWidth, ylabel, ylabel + Gui_OPTIONMENU_HEIGHT,
1760 theFinishBuffer.string, GuiLabel_RIGHT);
1761 thy optionMenu = GuiOptionMenu_createShown (form, fieldX, fieldX + fieldWidth, thy y, thy y + Gui_OPTIONMENU_HEIGHT, 0);
1762 for (integer ibutton = 1; ibutton <= thy options.size; ibutton ++) {
1763 UiOption button = thy options.at [ibutton];
1764 MelderString_copy (& theFinishBuffer, button -> name.get());
1765 GuiOptionMenu_addOption (thy optionMenu, theFinishBuffer.string);
1766 }
1767 }
1768 break;
1769 case _kUiField_type::BOOLEAN_:
1770 {
1771 MelderString_copy (& theFinishBuffer, thy labelText.get());
1772 /*field -> label = GuiLabel_createShown (form, x, x + labelWidth, thy y, thy y + Gui_CHECKBUTTON_HEIGHT,
1773 theFinishBuffer.string, GuiLabel_RIGHT); */
1774 thy checkButton = GuiCheckButton_createShown (form,
1775 fieldX, dialogWidth /* allow to extend into the margin */, thy y, thy y + Gui_CHECKBUTTON_HEIGHT,
1776 theFinishBuffer.string, nullptr, nullptr, 0
1777 );
1778 }
1779 break;
1780 case _kUiField_type::LIST_:
1781 {
1782 int listWidth = my numberOfFields == 1 ? dialogWidth - fieldX : fieldWidth;
1783 MelderString_copy (& theFinishBuffer, thy labelText.get());
1784 appendColon ();
1785 thy label = GuiLabel_createShown (form, Gui_LEFT_DIALOG_SPACING, Gui_LEFT_DIALOG_SPACING + labelWidth, thy y + 1, thy y + 21,
1786 theFinishBuffer.string, GuiLabel_RIGHT);
1787 thy list = GuiList_create (form, fieldX, fieldX + listWidth, thy y, thy y + LIST_HEIGHT, false, theFinishBuffer.string);
1788 for (integer i = 1; i <= thy strings.size; i ++)
1789 GuiList_insertItem (thy list, thy strings [i], 0);
1790 GuiThing_show (thy list);
1791 }
1792 break;
1793 }
1794 }
1795 for (integer ifield = 1; ifield <= my numberOfFields; ifield ++)
1796 UiField_setDefault (my field [ifield].get());
1797 /*separator = XmCreateSeparatorGadget (column, "separator", nullptr, 0);*/
1798 const int y = dialogHeight - Gui_BOTTOM_DIALOG_SPACING - Gui_PUSHBUTTON_HEIGHT;
1799 if (my helpTitle) {
1800 my helpButton = GuiButton_createShown (form, HELP_BUTTON_X, HELP_BUTTON_X + HELP_BUTTON_WIDTH, y, y + Gui_PUSHBUTTON_HEIGHT,
1801 U"Help", gui_button_cb_help, me, 0);
1802 }
1803 bool commentsOnly = true;
1804 for (integer ifield = 1; ifield <= my numberOfFields; ifield ++) {
1805 if (! _kUiField_type_isComment (my field [ifield] -> type)) {
1806 commentsOnly = false;
1807 break;
1808 }
1809 }
1810 if (! commentsOnly) {
1811 if (my isPauseForm) {
1812 my revertButton = GuiButton_createShown (form,
1813 HELP_BUTTON_X, HELP_BUTTON_X + REVERT_BUTTON_WIDTH,
1814 y, y + Gui_PUSHBUTTON_HEIGHT, U"Undo", gui_button_cb_revert, me, 0
1815 );
1816 } else {
1817 my revertButton = GuiButton_createShown (form,
1818 HELP_BUTTON_X + HELP_BUTTON_WIDTH + Gui_HORIZONTAL_DIALOG_SPACING,
1819 HELP_BUTTON_X + HELP_BUTTON_WIDTH + Gui_HORIZONTAL_DIALOG_SPACING + STANDARDS_BUTTON_WIDTH,
1820 y, y + Gui_PUSHBUTTON_HEIGHT, U"Standards", gui_button_cb_revert, me, 0
1821 );
1822 }
1823 }
1824 if (my isPauseForm) {
1825 int x = HELP_BUTTON_X + REVERT_BUTTON_WIDTH + Gui_HORIZONTAL_DIALOG_SPACING;
1826 if (my cancelContinueButton == 0) {
1827 my cancelButton = GuiButton_createShown (form, x, x + STOP_BUTTON_WIDTH, y, y + Gui_PUSHBUTTON_HEIGHT,
1828 U"Stop", gui_button_cb_cancel, me, GuiButton_CANCEL);
1829 x += STOP_BUTTON_WIDTH + 7;
1830 } else {
1831 x += 30;
1832 }
1833 int room = dialogWidth - Gui_RIGHT_DIALOG_SPACING - x;
1834 int roomPerContinueButton = room / my numberOfContinueButtons;
1835 int horizontalSpacing = (
1836 my numberOfContinueButtons > 7 ?
1837 Gui_HORIZONTAL_DIALOG_SPACING - 2 * (my numberOfContinueButtons - 7)
1838 :
1839 Gui_HORIZONTAL_DIALOG_SPACING
1840 );
1841 int continueButtonWidth = roomPerContinueButton - horizontalSpacing;
1842 for (int i = 1; i <= my numberOfContinueButtons; i ++) {
1843 x = dialogWidth - Gui_RIGHT_DIALOG_SPACING - roomPerContinueButton * (my numberOfContinueButtons - i + 1) + horizontalSpacing;
1844 my continueButtons [i] = GuiButton_createShown (form, x, x + continueButtonWidth, y, y + Gui_PUSHBUTTON_HEIGHT,
1845 my continueTexts [i].get(), gui_button_cb_ok, me,
1846 i == my defaultContinueButton && okButtonIsDefault ? GuiButton_DEFAULT : i == my cancelContinueButton ? GuiButton_CANCEL : 0
1847 );
1848 }
1849 } else {
1850 int x = dialogWidth - Gui_RIGHT_DIALOG_SPACING - Gui_OK_BUTTON_WIDTH - 2 * Gui_HORIZONTAL_DIALOG_SPACING
1851 - Gui_APPLY_BUTTON_WIDTH - Gui_CANCEL_BUTTON_WIDTH;
1852 my cancelButton = GuiButton_createShown (form, x, x + Gui_CANCEL_BUTTON_WIDTH, y, y + Gui_PUSHBUTTON_HEIGHT,
1853 U"Cancel", gui_button_cb_cancel, me, GuiButton_CANCEL);
1854 x = dialogWidth - Gui_RIGHT_DIALOG_SPACING - Gui_OK_BUTTON_WIDTH - Gui_HORIZONTAL_DIALOG_SPACING - Gui_APPLY_BUTTON_WIDTH;
1855 if (my numberOfFields > 1 || ! _kUiField_type_isComment (my field [1] -> type))
1856 my applyButton = GuiButton_createShown (form, x, x + Gui_APPLY_BUTTON_WIDTH, y, y + Gui_PUSHBUTTON_HEIGHT,
1857 U"Apply", gui_button_cb_apply, me, 0);
1858 x = dialogWidth - Gui_RIGHT_DIALOG_SPACING - Gui_OK_BUTTON_WIDTH;
1859 my okButton = GuiButton_createShown (form, x, x + Gui_OK_BUTTON_WIDTH, y, y + Gui_PUSHBUTTON_HEIGHT,
1860 my isPauseForm ? U"Continue" : U"OK", gui_button_cb_ok, me, okButtonIsDefault ? GuiButton_DEFAULT : 0);
1861 }
1862 /*GuiObject_show (separator);*/
1863 }
1864
1865 void UiForm_destroyWhenUnmanaged (UiForm me) {
1866 my destroyWhenUnmanaged = true;
1867 }
1868
1869 void UiForm_do (UiForm me, bool modified) {
1870 my allowExecutionHook = theAllowExecutionHookHint;
1871 my allowExecutionClosure = theAllowExecutionClosureHint;
1872 Melder_assert (my d_dialogForm);
1873 GuiThing_show (my d_dialogForm);
1874 if (modified)
1875 UiForm_okOrApply (me, nullptr, true);
1876 }
1877
1878 static void UiField_api_header_C (UiField me, UiField next, bool isLastNonLabelField) {
1879 if (my labelText && str32chr (my labelText.get(), U':'))
1880 trace (U"Form label with colon: ", my labelText.get());
1881 if (_kUiField_type_isComment (my type)) {
1882 const bool weAreFollowedByAWideField =
1883 next && (next -> type == _kUiField_type::TEXT_ || next -> type == _kUiField_type::FORMULA_ ||
1884 next -> type == _kUiField_type::INFILE_ || next -> type == _kUiField_type::OUTFILE_ ||
1885 next -> type == _kUiField_type::FOLDER_ ||
1886 next -> type == _kUiField_type::REALMATRIX_ ||
1887 next -> type == _kUiField_type::STRINGARRAY_);
1888 const bool weEndInAColon = Melder_endsWith (my stringValue.get(), U":");
1889 if (weEndInAColon)
1890 Melder_casual (U"Label with colon: ", my stringValue.get());
1891 const bool weLabelTheFollowingField = weAreFollowedByAWideField && weEndInAColon;
1892 const bool weAreAComment = ! weLabelTheFollowingField;
1893 if (weAreAComment)
1894 MelderInfo_writeLine (U"\t/* ", my stringValue.get(), U" */");
1895 return;
1896 }
1897
1898 /*
1899 Write the type of the field.
1900 */
1901 bool isText = false, isBoolean = false, isEnum = false, isNonnegative = false, isPositive = false;
1902 switch (my type)
1903 {
1904 case _kUiField_type::REAL_:
1905 case _kUiField_type::REAL_OR_UNDEFINED_:
1906 case _kUiField_type::NONNEGATIVE_:
1907 case _kUiField_type::POSITIVE_:
1908 {
1909 MelderInfo_write (U"\tdouble ");
1910 isNonnegative = ( my type == _kUiField_type::NONNEGATIVE_ );
1911 isPositive = ( my type == _kUiField_type::POSITIVE_ );
1912 }
1913 break;
1914 case _kUiField_type::INTEGER_:
1915 case _kUiField_type::NATURAL0_:
1916 case _kUiField_type::NATURAL1_:
1917 case _kUiField_type::CHANNEL_:
1918 {
1919 MelderInfo_write (U"\tint64_t ");
1920 isNonnegative = ( my type == _kUiField_type::NATURAL0_ );
1921 isPositive = ( my type == _kUiField_type::NATURAL1_ );
1922 }
1923 break;
1924 case _kUiField_type::WORD_:
1925 case _kUiField_type::SENTENCE_:
1926 case _kUiField_type::TEXT_:
1927 case _kUiField_type::FORMULA_:
1928 case _kUiField_type::INFILE_:
1929 case _kUiField_type::OUTFILE_:
1930 case _kUiField_type::FOLDER_:
1931 case _kUiField_type::COLOUR_:
1932 case _kUiField_type::LIST_:
1933 {
1934 MelderInfo_write (U"\tconst char *");
1935 isText = true;
1936 }
1937 break;
1938 case _kUiField_type::CHOICE_:
1939 case _kUiField_type::OPTIONMENU_:
1940 {
1941 MelderInfo_write (U"\tconst char *");
1942 isText = true;
1943 isEnum = true;
1944 }
1945 break;
1946 case _kUiField_type::BOOLEAN_:
1947 {
1948 MelderInfo_write (U"\tint32_t ");
1949 isBoolean = true;
1950 }
1951 break;
1952 default:
1953 {
1954 }
1955 }
1956
1957 /*
1958 Write the title of the field.
1959 */
1960 char32 cName [100], *q = & cName [0];
1961 Melder_assert (my labelText);
1962 const char32 *p = & my labelText [0];
1963 *q ++ = Melder_toLowerCase (*p ++);
1964 bool up = false;
1965 for (; *p != U'\0'; p ++) {
1966 if (*p == U'(') {
1967 break;
1968 } else if (*p == U'\'') {
1969 continue;
1970 } else if (*p == U' ' || *p == U'-') {
1971 if (p [1] == U'(') { p ++; break; }
1972 up = true;
1973 } else if (*p == U'*') {
1974 *q ++ = U'S';
1975 *q ++ = U't';
1976 *q ++ = U'a';
1977 *q ++ = U'r';
1978 } else if (up) {
1979 *q ++ = Melder_toUpperCase (*p);
1980 up = false;
1981 } else {
1982 *q ++ = *p;
1983 }
1984 }
1985 *q = U'\0';
1986 if (! my variableName)
1987 Melder_warning (U"Missing variable name for field label: ", my labelText.get());
1988 MelderInfo_write (my variableName ? my variableName : cName);
1989 if (! isLastNonLabelField)
1990 MelderInfo_write (U",");
1991
1992 /*
1993 Get the units.
1994 */
1995 char32 units [100];
1996 q = & units [0];
1997 if (*p == U'(') {
1998 for (p ++; *p != U'\0'; p ++) {
1999 if (*p == U')') {
2000 break;
2001 } else {
2002 *q ++ = *p;
2003 }
2004 }
2005 }
2006 *q = U'\0';
2007 const bool unitsAreAvailable = ( units [0] != U'\0' );
2008 const bool unitsContainRange = str32str (units, U"-");
2009
2010 /*
2011 Get the example.
2012 */
2013 conststring32 example = my stringDefaultValue.get(); // BUG dangle
2014 const bool exampleIsAvailable = ( example && example [0] != U'\0' );
2015
2016 if (exampleIsAvailable) {
2017 /*
2018 Split up the default string.
2019 */
2020 char32 defaultValue [100], defaultComment [100];
2021 str32cpy (defaultValue, my stringDefaultValue.get());
2022 str32cpy (defaultComment, U"");
2023 if (unitsAreAvailable) {
2024 char32 *parenthesis = str32chr (defaultValue, U'(');
2025 if (parenthesis && parenthesis - defaultValue > 1) {
2026 parenthesis [-1] = U'\0';
2027 str32cpy (defaultComment, parenthesis);
2028 }
2029 }
2030
2031 MelderInfo_write (U" // ");
2032 if (isNonnegative)
2033 MelderInfo_write (U"nonnegative, ");
2034 if (isPositive)
2035 MelderInfo_write (U"positive, ");
2036 if (unitsContainRange)
2037 MelderInfo_write (units, U", ");
2038 MelderInfo_write (U"e.g. ");
2039 if (isText)
2040 MelderInfo_write (U"\"");
2041 MelderInfo_write (defaultValue);
2042 if (isText)
2043 MelderInfo_write (U"\"");
2044 if (unitsAreAvailable && ! unitsContainRange) {
2045 MelderInfo_write (U" ", units);
2046 }
2047 if (defaultComment [0]) {
2048 MelderInfo_write (U" ", defaultComment);
2049 }
2050 } else if (isBoolean) {
2051 MelderInfo_write (U" // boolean, e.g. ");
2052 MelderInfo_write (my integerDefaultValue, my integerDefaultValue ? U" (true)" : U" (false)");
2053 } else if (isEnum) {
2054 MelderInfo_write (U" // e.g. \"");
2055 MelderInfo_write (my options.at [my integerDefaultValue] -> name.get());
2056 MelderInfo_write (U"\"; other choice", ( my options.size > 2 ? U"s" : U"" ), U":");
2057 bool firstWritten = false;
2058 for (int i = 1; i <= my options.size; i ++) {
2059 if (i == my integerDefaultValue)
2060 continue;
2061 if (firstWritten)
2062 MelderInfo_write (U",");
2063 MelderInfo_write (U" \"", my options.at [i] -> name.get(), U"\"");
2064 firstWritten = true;
2065 }
2066 }
2067 MelderInfo_writeLine (U"");
2068 }
2069
2070 void UiForm_info (UiForm me, integer narg) {
2071 if (narg == -1) {
2072 /*
2073 The C interface.
2074 */
2075 int lastNonLabelFieldNumber = 0;
2076 for (int ifield = my numberOfFields; ifield > 0; ifield --) {
2077 if (! _kUiField_type_isComment (my field [ifield] -> type)) {
2078 lastNonLabelFieldNumber = ifield;
2079 break;
2080 }
2081 }
2082 for (int ifield = 1; ifield <= my numberOfFields; ifield ++)
2083 UiField_api_header_C (my field [ifield].get(), ifield == my numberOfFields ? nullptr : my field [ifield + 1].get(), ifield == lastNonLabelFieldNumber);
2084 }
2085 }
2086
2087 static void UiField_argToValue (UiField me, Stackel arg, Interpreter /* interpreter */) {
2088 switch (my type)
2089 {
2090 case _kUiField_type::REAL_:
2091 case _kUiField_type::REAL_OR_UNDEFINED_:
2092 case _kUiField_type::NONNEGATIVE_:
2093 case _kUiField_type::POSITIVE_:
2094 {
2095 if (arg -> which != Stackel_NUMBER)
2096 Melder_throw (U"Argument “", my name.get(), U"” should be a number, not ", arg -> whichText(), U".");
2097 my realValue = arg -> number;
2098 if (isundef (my realValue) && my type != _kUiField_type::REAL_OR_UNDEFINED_)
2099 Melder_throw (U"Argument “", my name.get(), U"” has the value “undefined”.");
2100 if (my type == _kUiField_type::NONNEGATIVE_ && my realValue < 0.0)
2101 Melder_throw (U"Argument “", my name.get(), U"” must be greater than or equal to 0.0.");
2102 if (my type == _kUiField_type::POSITIVE_ && my realValue <= 0.0)
2103 Melder_throw (U"Argument “", my name.get(), U"” must be greater than 0.0.");
2104 if (my realVariable)
2105 *my realVariable = my realValue;
2106 }
2107 break;
2108 case _kUiField_type::INTEGER_:
2109 case _kUiField_type::NATURAL0_:
2110 case _kUiField_type::NATURAL1_:
2111 case _kUiField_type::CHANNEL_:
2112 {
2113 if (arg -> which == Stackel_STRING) {
2114 if (my type == _kUiField_type::CHANNEL_) {
2115 if (str32equ (arg -> getString(), U"All") || str32equ (arg -> getString(), U"Average")) {
2116 my integerValue = 0;
2117 } else if (str32equ (arg -> getString(), U"Left") || str32equ (arg -> getString(), U"Mono")) {
2118 my integerValue = 1;
2119 } else if (str32equ (arg -> getString(), U"Right") || str32equ (arg -> getString(), U"Stereo")) {
2120 my integerValue = 2;
2121 } else {
2122 Melder_throw (U"Channel argument “", my name.get(),
2123 U"” can only be a number or one of the strings “All”, “Average”, “Left”, “Right”, “Mono” or “Stereo”.");
2124 }
2125 } else {
2126 Melder_throw (U"Argument “", my name.get(), U"” should be a number, not ", arg -> whichText(), U".");
2127 }
2128 } else if (arg -> which == Stackel_NUMBER) {
2129 double realValue = arg -> number;
2130 my integerValue = Melder_iround (realValue);
2131 Melder_require (my integerValue == realValue,
2132 U"Argument “", my name.get(), U"” should be a whole number.");
2133 if (my type == _kUiField_type::NATURAL0_ && my integerValue < 0)
2134 Melder_throw (U"Argument “", my name.get(), U"” should be a nonnegative whole number.");
2135 if (my type == _kUiField_type::NATURAL1_ && my integerValue < 1)
2136 Melder_throw (U"Argument “", my name.get(), U"” should be a positive whole number.");
2137 } else {
2138 Melder_throw (U"Argument “", my name.get(), U"” should be a number, not ", arg -> whichText(), U".");
2139 }
2140 if (my integerVariable)
2141 *my integerVariable = my integerValue;
2142 }
2143 break;
2144 case _kUiField_type::WORD_:
2145 case _kUiField_type::SENTENCE_:
2146 case _kUiField_type::TEXT_:
2147 case _kUiField_type::FORMULA_:
2148 case _kUiField_type::INFILE_:
2149 case _kUiField_type::OUTFILE_:
2150 case _kUiField_type::FOLDER_:
2151 {
2152 if (arg -> which != Stackel_STRING)
2153 Melder_throw (U"Argument “", my name.get(), U"” should be a string, not ", arg -> whichText(), U".");
2154 my stringValue = Melder_dup (arg -> getString());
2155 if (my stringVariable)
2156 *my stringVariable = my stringValue.get(); // BUG dangle
2157 }
2158 break;
2159 case _kUiField_type::REALVECTOR_:
2160 case _kUiField_type::NONNEGATIVEVECTOR_:
2161 case _kUiField_type::POSITIVEVECTOR_:
2162 {
2163 if (arg -> which != Stackel_NUMERIC_VECTOR && arg -> which != Stackel_STRING)
2164 Melder_throw (U"Argument “", my name.get(), U"” should be a numeric vector, not ", arg -> whichText(), U".");
2165 if (arg -> which == Stackel_STRING) {
2166 my realVectorValue = splitByWhitespace_VEC (arg -> getString());
2167 } else if (arg -> owned) {
2168 my realVectorValue. adoptFromAmbiguousOwner (arg -> numericVector);
2169 arg -> owned = false;
2170 } else {
2171 my realVectorValue = copy_VEC (arg -> numericVector);
2172 }
2173 if (my type == _kUiField_type::NONNEGATIVEVECTOR_)
2174 for (integer i = 1; i <= my realVectorValue.size; i ++)
2175 if (my realVectorValue [i] < 0.0)
2176 Melder_throw (U"Element ", i, U" of vector “", my name.get(), U"” is ", my realVectorValue [i], U" but should be greater than or equal to 0.0.");
2177 if (my type == _kUiField_type::POSITIVEVECTOR_)
2178 for (integer i = 1; i <= my realVectorValue.size; i ++)
2179 if (my realVectorValue [i] <= 0.0)
2180 Melder_throw (U"Element ", i, U" of vector “", my name.get(), U"” is ", my realVectorValue [i], U" but should be greater than 0.0.");
2181 if (my realVectorVariable)
2182 *my realVectorVariable = my realVectorValue.get();
2183 }
2184 break;
2185 case _kUiField_type::INTEGERVECTOR_:
2186 case _kUiField_type::NATURAL0VECTOR_:
2187 case _kUiField_type::NATURAL1VECTOR_:
2188 {
2189 if (arg -> which != Stackel_NUMERIC_VECTOR && arg -> which != Stackel_STRING)
2190 Melder_throw (U"Argument “", my name.get(), U"” should be a numeric vector, not ", arg -> whichText(), U".");
2191 if (arg -> which == Stackel_STRING) {
2192 my integerVectorValue = splitByWhitespaceWithRanges_INTVEC (arg -> getString());
2193 } else {
2194 my integerVectorValue = raw_INTVEC (arg -> numericVector.size);
2195 for (integer i = 1; i <= arg -> numericVector.size; i ++) {
2196 my integerVectorValue [i] = Melder_iround (arg -> numericVector [i]);
2197 Melder_require (my integerVectorValue [i] == arg -> numericVector [i],
2198 U"Element ", i, U" of vector “", my name.get(), U"” is ", arg -> numericVector [i], U" but should be a whole number.");
2199 }
2200 }
2201 if (my type == _kUiField_type::NATURAL0VECTOR_)
2202 for (integer i = 1; i <= my integerVectorValue.size; i ++)
2203 if (my integerVectorValue [i] < 0)
2204 Melder_throw (U"Element ", i, U" of vector “", my name.get(), U"” is ", my integerVectorValue [i], U" but should be greater than or equal to 0.");
2205 if (my type == _kUiField_type::NATURAL1VECTOR_)
2206 for (integer i = 1; i <= my integerVectorValue.size; i ++)
2207 if (my integerVectorValue [i] <= 0)
2208 Melder_throw (U"Element ", i, U" of vector “", my name.get(), U"” is ", my integerVectorValue [i], U" but should be greater than 0.");
2209 if (my integerVectorVariable)
2210 *my integerVectorVariable = my integerVectorValue.get();
2211 }
2212 break;
2213 case _kUiField_type::REALMATRIX_:
2214 {
2215 if (arg -> which != Stackel_NUMERIC_MATRIX)
2216 Melder_throw (U"Argument “", my name.get(), U"” should be a numeric matrix, not ", arg -> whichText(), U".");
2217 if (arg -> owned) {
2218 my numericMatrixValue. adoptFromAmbiguousOwner (arg -> numericMatrix);
2219 arg -> owned = false;
2220 } else {
2221 my numericMatrixValue = copy_MAT (arg -> numericMatrix);
2222 }
2223 if (my numericMatrixVariable)
2224 *my numericMatrixVariable = my numericMatrixValue.get();
2225 }
2226 break;
2227 case _kUiField_type::STRINGARRAY_:
2228 {
2229 if (arg -> which != Stackel_STRING_ARRAY && arg -> which != Stackel_STRING)
2230 Melder_throw (U"Argument “", my name.get(), U"” should be a string array, not ", arg -> whichText(), U".");
2231 if (arg -> which == Stackel_STRING) {
2232 my stringArrayValue = splitByWhitespace_STRVEC (arg -> getString());
2233 } else if (arg -> owned) {
2234 my stringArrayValue. adoptFromAmbiguousOwner (arg -> stringArray);
2235 arg -> owned = false;
2236 } else {
2237 my stringArrayValue = copy_STRVEC (arg -> stringArray);
2238 }
2239 if (my stringArrayVariable)
2240 *my stringArrayVariable = my stringArrayValue.get();
2241 }
2242 break;
2243 case _kUiField_type::BOOLEAN_:
2244 {
2245 if (arg -> which == Stackel_STRING) {
2246 if (str32equ (arg -> getString(), U"no") || str32equ (arg -> getString(), U"off")) {
2247 my integerValue = 0;
2248 } else if (str32equ (arg -> getString(), U"yes") || str32equ (arg -> getString(), U"on")) {
2249 my integerValue = 1;
2250 } else {
2251 Melder_throw (U"Boolean argument “", my name.get(),
2252 U"” can only be a number or one of the strings “yes” or “no”.");
2253 }
2254 } else if (arg -> which == Stackel_NUMBER) {
2255 my integerValue = ( arg -> number == 0.0 ? 0.0 : 1.0 );
2256 } else {
2257 Melder_throw (U"Boolean argument “", my name.get(), U"” should be a number (0 or 1), not ", arg -> whichText(), U".");
2258 }
2259 if (my boolVariable)
2260 *my boolVariable = my integerValue;
2261 }
2262 break;
2263 case _kUiField_type::CHOICE_:
2264 case _kUiField_type::OPTIONMENU_:
2265 {
2266 if (arg -> which != Stackel_STRING)
2267 Melder_throw (U"Option argument “", my name.get(), U"” should be a string, not ", arg -> whichText(), U".");
2268 if (my getValueFunction) {
2269 int value = my getValueFunction (arg -> getString());
2270 if (value == -1)
2271 my integerValue = 0; // zero
2272 else
2273 my integerValue = my getValueFunction (arg -> getString()) + my subtract; // 1 or greater
2274 } else {
2275 my integerValue = 0;
2276 for (int i = 1; i <= my options.size; i ++) {
2277 UiOption b = my options.at [i];
2278 if (str32equ (arg -> getString(), b -> name.get()))
2279 my integerValue = i;
2280 }
2281 if (my integerValue == 0) {
2282 /*
2283 Retry with different case.
2284 */
2285 for (int i = 1; i <= my options.size; i ++) {
2286 UiOption b = my options.at [i];
2287 if (Melder_equ_firstCharacterCaseInsensitive (arg -> getString(), b -> name.get()))
2288 my integerValue = i;
2289 }
2290 }
2291 }
2292 if (my integerValue == 0) {
2293 if (my intVariable)
2294 Melder_throw (U"Option argument “", my name.get(), U"” cannot have the value “", arg -> getString(), U"”.");
2295 if (my stringVariable) {
2296 *my stringVariable = arg -> getString();
2297 return;
2298 }
2299 }
2300 if (my intVariable)
2301 *my intVariable = int (my integerValue) - my subtract;
2302 if (my stringVariable)
2303 *my stringVariable = my options.at [my integerValue] -> name.get();
2304 }
2305 break;
2306 case _kUiField_type::LIST_:
2307 {
2308 if (arg -> which != Stackel_STRING)
2309 Melder_throw (U"List argument “", my name.get(), U"” should be a string, not ", arg -> whichText(), U".");
2310 integer i = 1;
2311 for (; i <= my strings.size; i ++)
2312 if (str32equ (arg -> getString(), my strings [i]))
2313 break;
2314 if (i > my strings.size) {
2315 /*
2316 Not found. If the result should be only a number or only a string, it's an error.
2317 If the result should be both a number and a string, then the number can be 0 (signalling "not found")
2318 and the string provides a out-of-list alternative that could be translated to an existing one.
2319 */
2320 if (!! my integerVariable != !! my stringVariable)
2321 Melder_throw (U"List argument “", my name.get(), U"” cannot have the value “", arg -> getString(), U"”.");
2322 my integerValue = 0;
2323 *my integerVariable = my integerValue;
2324 *my stringVariable = arg -> getString(); // FIXME: is the lifetime of arg long enough?
2325 } else {
2326 my integerValue = i;
2327 if (my integerVariable)
2328 *my integerVariable = my integerValue;
2329 if (my stringVariable)
2330 *my stringVariable = (char32 *) my strings [my integerValue]; // FIXME: const problem
2331 }
2332 }
2333 break;
2334 case _kUiField_type::COLOUR_:
2335 {
2336 if (arg -> which == Stackel_NUMBER) {
2337 if (arg -> number < 0.0 || arg -> number > 1.0)
2338 Melder_throw (U"Grey colour argument “", my name.get(), U"” has to lie between 0.0 and 1.0.");
2339 my colourValue. red = my colourValue. green = my colourValue. blue = arg -> number;
2340 } else if (arg -> which == Stackel_STRING) {
2341 autostring32 string2 = Melder_dup (arg -> getString());
2342 MelderColour colour = MelderColour_fromColourNameOrRGBString (string2.get());
2343 if (colour.valid())
2344 my colourValue = colour;
2345 else
2346 Melder_throw (U"Cannot compute a colour from “", string2.get(), U"”.");
2347 } else if (arg -> which == Stackel_NUMERIC_VECTOR) {
2348 my colourValue = MelderColour (arg -> numericVector);
2349 } else
2350 Melder_throw (U"Colour argument “", my name.get(), U"” should be a string or number or numeric vector, not ", arg -> whichText(), U".");
2351 if (my colourVariable)
2352 *my colourVariable = my colourValue;
2353 }
2354 break;
2355 default:
2356 {
2357 Melder_throw (U"Unknown field type ", (int) my type, U".");
2358 }
2359 }
2360 }
2361
2362 void UiForm_call (UiForm me, integer narg, Stackel args, Interpreter interpreter) {
2363 integer size = my numberOfFields, iarg = 0;
2364 //while (size >= 1 && my field [size] -> type == _kUiField_type::LABEL_)
2365 // size --; // ignore trailing fields without a value
2366 for (integer ifield = 1; ifield <= size; ifield ++) {
2367 if (_kUiField_type_isComment (my field [ifield] -> type))
2368 continue; // ignore non-trailing fields without a value
2369 iarg ++;
2370 if (iarg > narg)
2371 Melder_throw (U"Command requires more than the given ", narg, U" arguments: argument “", my field [ifield] -> name.get(), U"” not given.");
2372 UiField_argToValue (my field [ifield].get(), & args [iarg], interpreter);
2373 }
2374 if (iarg < narg)
2375 Melder_throw (U"Command requires only ", iarg, U" arguments, not the ", narg, U" given.");
2376 my okCallback (me, 0, nullptr, nullptr, interpreter, nullptr, false, my buttonClosure, my optionalEditor);
2377 }
2378
2379 /*
2380 DEPRECATED_2014 (i.e. remove in 2036)
2381 */
2382 static void UiField_stringToValue (UiField me, conststring32 string, Interpreter interpreter) {
2383 /*
2384 This belongs to the deprecated dots-based syntax described below at `UiForm_parseString`.
2385 This is included for backward compatibility (until 2036),
2386 but does not support newer expression types such as numeric vectors and matrices.
2387 */
2388 switch (my type)
2389 {
2390 case _kUiField_type::REAL_:
2391 case _kUiField_type::REAL_OR_UNDEFINED_:
2392 case _kUiField_type::NONNEGATIVE_:
2393 case _kUiField_type::POSITIVE_:
2394 {
2395 if (str32spn (string, U" \t") == Melder_length (string))
2396 Melder_throw (U"Argument “", my name.get(), U"” empty.");
2397 Interpreter_numericExpression (interpreter, string, & my realValue);
2398 if (isundef (my realValue) && my type != _kUiField_type::REAL_OR_UNDEFINED_)
2399 Melder_throw (U"“", my name.get(), U"” has the value “undefined”.");
2400 if (my type == _kUiField_type::NONNEGATIVE_ && my realValue < 0.0)
2401 Melder_throw (U"“", my name.get(), U"” must be greater than or equal to 0.0.");
2402 if (my type == _kUiField_type::POSITIVE_ && my realValue <= 0.0)
2403 Melder_throw (U"“", my name.get(), U"” must be greater than 0.0.");
2404 if (my realVariable)
2405 *my realVariable = my realValue;
2406 }
2407 break;
2408 case _kUiField_type::INTEGER_:
2409 case _kUiField_type::NATURAL0_:
2410 case _kUiField_type::NATURAL1_:
2411 case _kUiField_type::CHANNEL_: {
2412 if (str32spn (string, U" \t") == Melder_length (string))
2413 Melder_throw (U"Argument “", my name.get(), U"” empty.");
2414 if (my type == _kUiField_type::CHANNEL_ && (str32equ (string, U"All") || str32equ (string, U"Average"))) {
2415 my integerValue = 0;
2416 } else if (my type == _kUiField_type::CHANNEL_ && (str32equ (string, U"Left") || str32equ (string, U"Mono"))) {
2417 my integerValue = 1;
2418 } else if (my type == _kUiField_type::CHANNEL_ && (str32equ (string, U"Right") || str32equ (string, U"Stereo"))) {
2419 my integerValue = 2;
2420 } else {
2421 double realValue;
2422 Interpreter_numericExpression (interpreter, string, & realValue);
2423 my integerValue = Melder_iround (realValue);
2424 }
2425 if (my type == _kUiField_type::NATURAL0_ && my integerValue < 0)
2426 Melder_throw (U"“", my name.get(), U"” should be a nonnegative whole number.");
2427 if (my type == _kUiField_type::NATURAL1_ && my integerValue < 1)
2428 Melder_throw (U"“", my name.get(), U"” should be a positive whole number.");
2429 if (my integerVariable)
2430 *my integerVariable = my integerValue;
2431 }
2432 break;
2433 case _kUiField_type::WORD_:
2434 case _kUiField_type::SENTENCE_:
2435 case _kUiField_type::TEXT_:
2436 case _kUiField_type::FORMULA_:
2437 case _kUiField_type::INFILE_:
2438 case _kUiField_type::OUTFILE_:
2439 case _kUiField_type::FOLDER_:
2440 {
2441 my stringValue = Melder_dup (string);
2442 if (my stringVariable)
2443 *my stringVariable = my stringValue.get(); // BUG dangle
2444 }
2445 break;
2446 case _kUiField_type::REALVECTOR_:
2447 case _kUiField_type::NONNEGATIVEVECTOR_:
2448 case _kUiField_type::POSITIVEVECTOR_:
2449 {
2450 my realVectorValue = splitByWhitespace_VEC (string);
2451 if (my type == _kUiField_type::NONNEGATIVEVECTOR_)
2452 for (integer i = 1; i <= my realVectorValue.size; i ++)
2453 if (my realVectorValue [i] < 0.0)
2454 Melder_throw (U"Element ", i, U" of vector “", my name.get(), U"” is ", my realVectorValue [i], U" but should be greater than or equal to 0.0.");
2455 if (my type == _kUiField_type::POSITIVEVECTOR_)
2456 for (integer i = 1; i <= my realVectorValue.size; i ++)
2457 if (my realVectorValue [i] <= 0.0)
2458 Melder_throw (U"Element ", i, U" of vector “", my name.get(), U"” is ", my realVectorValue [i], U" but should be greater than 0.0.");
2459 if (my realVectorVariable)
2460 *my realVectorVariable = my realVectorValue.get();
2461 }
2462 break;
2463 case _kUiField_type::INTEGERVECTOR_:
2464 case _kUiField_type::NATURAL0VECTOR_:
2465 case _kUiField_type::NATURAL1VECTOR_:
2466 {
2467 my integerVectorValue = splitByWhitespaceWithRanges_INTVEC (string);
2468 if (my type == _kUiField_type::NATURAL0VECTOR_)
2469 for (integer i = 1; i <= my integerVectorValue.size; i ++)
2470 if (my integerVectorValue [i] < 0)
2471 Melder_throw (U"Element ", i, U" of vector “", my name.get(), U"” is ", my integerVectorValue [i], U" but should be greater than or equal to 0.");
2472 if (my type == _kUiField_type::NATURAL1VECTOR_)
2473 for (integer i = 1; i <= my integerVectorValue.size; i ++)
2474 if (my integerVectorValue [i] <= 0)
2475 Melder_throw (U"Element ", i, U" of vector “", my name.get(), U"” is ", my integerVectorValue [i], U" but should be greater than 0.");
2476 if (my integerVectorVariable)
2477 *my integerVectorVariable = my integerVectorValue.get();
2478 }
2479 break;
2480 case _kUiField_type::STRINGARRAY_:
2481 my stringArrayValue = splitByWhitespace_STRVEC (string);
2482 if (my stringArrayVariable)
2483 *my stringArrayVariable = my stringArrayValue.get();
2484 break;
2485 case _kUiField_type::BOOLEAN_:
2486 {
2487 if (! string [0])
2488 Melder_throw (U"Empty argument for toggle button.");
2489 my integerValue = string [0] == U'1' || string [0] == U'y' || string [0] == U'Y' ||
2490 string [0] == U't' || string [0] == U'T';
2491 if (my boolVariable)
2492 *my boolVariable = my integerValue;
2493 }
2494 break;
2495 case _kUiField_type::CHOICE_:
2496 case _kUiField_type::OPTIONMENU_:
2497 {
2498 if (my getValueFunction) {
2499 int value = my getValueFunction (string);
2500 if (value == -1)
2501 my integerValue = 0; // zero
2502 else
2503 my integerValue = my getValueFunction (string) + my subtract; // 1 or greater
2504 } else {
2505 my integerValue = 0;
2506 for (int i = 1; i <= my options.size; i ++) {
2507 UiOption b = my options.at [i];
2508 if (str32equ (string, b -> name.get()))
2509 my integerValue = i;
2510 }
2511 if (my integerValue == 0) {
2512 /*
2513 Retry with different case.
2514 */
2515 for (int i = 1; i <= my options.size; i ++) {
2516 UiOption b = my options.at [i];
2517 if (Melder_equ_firstCharacterCaseInsensitive (string, b -> name.get()))
2518 my integerValue = i;
2519 }
2520 }
2521 }
2522 if (my integerValue == 0)
2523 Melder_throw (U"Field “", my name.get(), U"” must not have the value “", string, U"”.");
2524 if (my intVariable)
2525 *my intVariable = int (my integerValue) - my subtract;
2526 if (my stringVariable)
2527 *my stringVariable = my options.at [my integerValue] -> name.get();
2528 }
2529 break;
2530 case _kUiField_type::LIST_:
2531 {
2532 integer i = 1;
2533 for (; i <= my strings.size; i ++)
2534 if (str32equ (string, my strings [i]))
2535 break;
2536 if (i > my strings.size) {
2537 /*
2538 Not found. If the result should be only a number or only a string, it's an error.
2539 If the result should be both a number and a string, then the number can be 0 (signalling "not found")
2540 and the string provides a out-of-list alternative that could be translated to an existing one.
2541 */
2542 if (!! my integerVariable != !! my stringVariable)
2543 Melder_throw (U"List argument “", my name.get(), U"” cannot have the value “", string, U"”.");
2544 my integerValue = 0;
2545 *my integerVariable = my integerValue;
2546 *my stringVariable = string;
2547 } else {
2548 my integerValue = i;
2549 if (my integerVariable)
2550 *my integerVariable = my integerValue;
2551 if (my stringVariable)
2552 *my stringVariable = (char32 *) my strings [my integerValue]; // FIXME: const problem
2553 }
2554 }
2555 break;
2556 case _kUiField_type::COLOUR_:
2557 {
2558 autostring32 string2 = Melder_dup (string);
2559 MelderColour colour = MelderColour_fromColourNameOrRGBString (string2.get());
2560 if (colour.valid()) {
2561 my colourValue = colour;
2562 } else {
2563 try {
2564 double greyValue;
2565 Interpreter_numericExpression (interpreter, string2.get(), & greyValue);
2566 my colourValue = MelderColour (greyValue);
2567 } catch (MelderError) {
2568 Melder_clearError ();
2569 Melder_throw (U"Cannot compute a colour from “", string2.get(), U"”.");
2570 }
2571 }
2572 if (my colourVariable)
2573 *my colourVariable = my colourValue;
2574 }
2575 break;
2576 default:
2577 {
2578 Melder_throw (U"Unknown field type ", (int) my type, U".");
2579 }
2580 }
2581 }
2582
2583 /*
2584 DEPRECATED_2014 (i.e. remove in 2036)
2585 */
2586 void UiForm_parseString (UiForm me, conststring32 arguments, Interpreter optionalInterpreter) {
2587 /*
2588 This implements the dots-based scripting style
2589 Create Sound from formula... sineWithNoise 1 0 1 44100 0.5 * sin (2*pi*377*x)
2590 This was deprecated with the advent of the colon-based scripting style
2591 Create Sound from formula: "sineWithNoise", 1, 0, 1, 44100, "0.5 * sin (2*pi*377*x)"
2592 or
2593 Create Sound from formula: "sineWithNoise", 1, 0, 1, 44100, ~ 0.5 * sin (2*pi*377*x)
2594 in 2014, i.e. 22 years after Praat started.
2595 If we want to conservatively support old scripts, we will have
2596 to continue to support the dots-based scripting style until 2036.
2597 */
2598 int size = my numberOfFields;
2599 while (size >= 1 && _kUiField_type_isComment (my field [size] -> type))
2600 size --; // ignore trailing fields without a value
2601 for (int ifield = 1; ifield < size; ifield ++) {
2602 static char32 stringValue [3000];
2603 int ichar = 0;
2604 if (_kUiField_type_isComment (my field [ifield] -> type))
2605 continue; // ignore non-trailing fields without a value
2606 Melder_skipHorizontalOrVerticalSpace (& arguments); // go to next argument
2607 /*
2608 The argument is everything up to the next space, or, if that starts with a double quote,
2609 everything between this quote and the matching double quote;
2610 in this case, the argument can represent a double quote by a sequence of two double quotes.
2611 Example: the string
2612 "I said ""hello"""
2613 will be passed to the dialog as a single argument containing the text
2614 I said "hello"
2615 */
2616 if (*arguments == U'"') {
2617 arguments ++; // do not include leading double quote
2618 for (;;) {
2619 if (*arguments == U'\0')
2620 Melder_throw (U"Missing matching quote.");
2621 if (*arguments == U'"' && * ++ arguments != U'"')
2622 break; // remember second quote
2623 stringValue [ichar ++] = *arguments ++;
2624 }
2625 } else {
2626 while (*arguments != U' ' && *arguments != U'\t' && *arguments != U'\0')
2627 stringValue [ichar ++] = *arguments ++;
2628 }
2629 stringValue [ichar] = U'\0'; // trailing null character
2630 try {
2631 UiField_stringToValue (my field [ifield].get(), stringValue, optionalInterpreter);
2632 } catch (MelderError) {
2633 Melder_throw (U"Don't understand contents of field “", my field [ifield] -> name.get(), U"”.");
2634 }
2635 }
2636 /*
2637 The last item is handled separately, because it consists of the rest of the line.
2638 Leading spaces are skipped, but trailing spaces are included.
2639 */
2640 if (size > 0) {
2641 Melder_skipHorizontalOrVerticalSpace (& arguments); // rid leading spaces
2642 try {
2643 UiField_stringToValue (my field [size].get(), arguments, optionalInterpreter);
2644 } catch (MelderError) {
2645 Melder_throw (U"Don't understand contents of field “", my field [size] -> name.get(), U"”.");
2646 }
2647 }
2648 my okCallback (me, 0, nullptr, nullptr, optionalInterpreter, nullptr, false, my buttonClosure, my optionalEditor);
2649 }
2650
2651 void UiForm_parseStringE (EditorCommand cmd, integer narg, Stackel args, conststring32 arguments, Interpreter interpreter) {
2652 if (args)
2653 UiForm_call (cmd -> d_uiform.get(), narg, args, interpreter);
2654 else
2655 UiForm_parseString (cmd -> d_uiform.get(), arguments, interpreter);
2656 }
2657
2658 static void fatalField (UiForm dia) {
2659 Melder_crash (U"Wrong field in command window “", dia -> name.get(), U"”.");
2660 }
2661
2662 void UiForm_setReal (UiForm me, double *p_variable, double value) {
2663 for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
2664 UiField field = my field [ifield].get();
2665 if (field -> realVariable == p_variable) {
2666 switch (field -> type)
2667 {
2668 case _kUiField_type::REAL_:
2669 case _kUiField_type::REAL_OR_UNDEFINED_:
2670 case _kUiField_type::NONNEGATIVE_:
2671 case _kUiField_type::POSITIVE_:
2672 {
2673 if (value == Melder_atof (field -> stringDefaultValue.get())) {
2674 GuiText_setString (field -> text, field -> stringDefaultValue.get());
2675 } else {
2676 char32 s [40];
2677 str32cpy (s, Melder_double (value));
2678 /*
2679 If the default is overtly real, the shown value should be as well.
2680 */
2681 if ((str32chr (field -> stringDefaultValue.get(), U'.') || str32chr (field -> stringDefaultValue.get(), U'e')) &&
2682 ! (str32chr (s, U'.') || str32chr (s, U'e')))
2683 {
2684 str32cpy (s + Melder_length (s), U".0");
2685 }
2686 GuiText_setString (field -> text, s);
2687 }
2688 }
2689 break;
2690 default:
2691 {
2692 fatalField (me);
2693 }
2694 }
2695 return;
2696 }
2697 }
2698 Melder_crash (U"Real field not found in command window “", my name.get(), U"”.");
2699 }
2700
2701 void UiForm_setRealAsString (UiForm me, double *p_variable, conststring32 stringValue) {
2702 for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
2703 UiField field = my field [ifield].get();
2704 if (field -> realVariable == p_variable) {
2705 switch (field -> type)
2706 {
2707 case _kUiField_type::REAL_:
2708 case _kUiField_type::REAL_OR_UNDEFINED_:
2709 case _kUiField_type::NONNEGATIVE_:
2710 case _kUiField_type::POSITIVE_:
2711 {
2712 GuiText_setString (field -> text, stringValue);
2713 }
2714 break;
2715 default:
2716 {
2717 fatalField (me);
2718 }
2719 }
2720 return;
2721 }
2722 }
2723 Melder_crash (U"Real field not found in command window “", my name.get(), U"”.");
2724 }
2725
2726 void UiForm_setInteger (UiForm me, integer *p_variable, integer value) {
2727 for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
2728 UiField field = my field [ifield].get();
2729 if (field -> integerVariable == p_variable) {
2730 switch (field -> type)
2731 {
2732 case _kUiField_type::INTEGER_:
2733 case _kUiField_type::NATURAL0_:
2734 case _kUiField_type::NATURAL1_:
2735 case _kUiField_type::CHANNEL_:
2736 {
2737 if (value == Melder_atoi (field -> stringDefaultValue.get())) {
2738 GuiText_setString (field -> text, field -> stringDefaultValue.get());
2739 } else {
2740 GuiText_setString (field -> text, Melder_integer (value));
2741 }
2742 }
2743 break;
2744 case _kUiField_type::LIST_:
2745 {
2746 if (value < 1 || value > field -> strings.size)
2747 value = 1; // guard against incorrect prefs file
2748 GuiList_selectItem (field -> list, value);
2749 }
2750 break;
2751 default:
2752 {
2753 fatalField (me);
2754 }
2755 }
2756 return;
2757 }
2758 }
2759 Melder_crash (U"Integer field not found in command window “", my name.get(), U"”.");
2760 }
2761
2762 void UiForm_setIntegerAsString (UiForm me, integer *p_variable, conststring32 stringValue /* cattable */) {
2763 for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
2764 UiField field = my field [ifield].get();
2765 if (field -> integerVariable == p_variable) {
2766 switch (field -> type)
2767 {
2768 case _kUiField_type::INTEGER_:
2769 case _kUiField_type::NATURAL0_:
2770 case _kUiField_type::NATURAL1_:
2771 case _kUiField_type::CHANNEL_:
2772 {
2773 GuiText_setString (field -> text, stringValue);
2774 }
2775 break;
2776 case _kUiField_type::LIST_:
2777 {
2778 integer i = 1;
2779 for (; i <= field -> strings.size; i ++)
2780 if (str32equ (stringValue, field -> strings [i])) break;
2781 if (i > field -> strings.size)
2782 i = 1; // guard against incorrect prefs file
2783 GuiList_selectItem (field -> list, i);
2784 }
2785 break;
2786 default:
2787 {
2788 fatalField (me);
2789 }
2790 }
2791 return;
2792 }
2793 }
2794 Melder_crash (U"Integer field not found in command window “", my name.get(), U"”.");
2795 }
2796
2797 void UiForm_setBoolean (UiForm me, bool *p_variable, bool value) {
2798 for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
2799 UiField field = my field [ifield].get();
2800 if (field -> boolVariable == p_variable) {
2801 switch (field -> type)
2802 {
2803 case _kUiField_type::BOOLEAN_:
2804 {
2805 GuiCheckButton_setValue (field -> checkButton, value);
2806 }
2807 break;
2808 default:
2809 {
2810 fatalField (me);
2811 }
2812 }
2813 return;
2814 }
2815 }
2816 Melder_crash (U"Boolean field not found in command window “", my name.get(), U"”.");
2817 }
2818
2819 void UiForm_setOption (UiForm me, int *p_variable, int value) {
2820 for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
2821 UiField field = my field [ifield].get();
2822 if (field -> intVariable == p_variable) {
2823 switch (field -> type)
2824 {
2825 case _kUiField_type::CHOICE_:
2826 {
2827 if (value < 1 || value > field -> options.size)
2828 value = 1; // guard against incorrect prefs file
2829 UiOption option = field -> options.at [value];
2830 GuiRadioButton_set (option -> radioButton);
2831 }
2832 break;
2833 case _kUiField_type::OPTIONMENU_:
2834 {
2835 if (value < 1 || value > field -> options.size)
2836 value = 1; // guard against incorrect prefs file
2837 GuiOptionMenu_setValue (field -> optionMenu, value);
2838 }
2839 break;
2840 default:
2841 {
2842 fatalField (me);
2843 }
2844 }
2845 return;
2846 }
2847 }
2848 Melder_crash (U"Option field not found in command window “", my name.get(), U"”.");
2849 }
2850
2851 void UiForm_setOptionAsString (UiForm me, int *p_variable, conststring32 stringValue) {
2852 for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
2853 UiField field = my field [ifield].get();
2854 if (field -> intVariable == p_variable) {
2855 switch (field -> type)
2856 {
2857 case _kUiField_type::CHOICE_:
2858 {
2859 for (int i = 1; i <= field -> options.size; i ++) {
2860 UiOption b = field -> options.at [i];
2861 if (str32equ (stringValue, b -> name.get())) {
2862 GuiRadioButton_set (b -> radioButton);
2863 }
2864 }
2865 /* If not found: do nothing (guard against incorrect prefs file). */
2866 }
2867 break;
2868 case _kUiField_type::OPTIONMENU_:
2869 {
2870 int optionValue = 0;
2871 for (int i = 1; i <= field -> options.size; i ++) {
2872 UiOption b = field -> options.at [i];
2873 if (str32equ (stringValue, b -> name.get())) {
2874 optionValue = i;
2875 break;
2876 }
2877 }
2878 GuiOptionMenu_setValue (field -> optionMenu, optionValue);
2879 /* If not found: do nothing (guard against incorrect prefs file). */
2880 }
2881 break;
2882 default:
2883 {
2884 fatalField (me);
2885 }
2886 }
2887 return;
2888 }
2889 }
2890 Melder_crash (U"Option field not found in command window “", my name.get(), U"”.");
2891 }
2892
2893 void UiForm_setString (UiForm me, conststring32 *p_variable, conststring32 value /* cattable */) {
2894 if (! value) value = U""; // accept null strings
2895 for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
2896 UiField field = my field [ifield].get();
2897 if (field -> stringVariable == p_variable) {
2898 switch (field -> type)
2899 {
2900 case _kUiField_type::WORD_:
2901 case _kUiField_type::SENTENCE_:
2902 case _kUiField_type::COLOUR_:
2903 case _kUiField_type::TEXT_:
2904 case _kUiField_type::FORMULA_:
2905 case _kUiField_type::INFILE_:
2906 case _kUiField_type::OUTFILE_:
2907 case _kUiField_type::FOLDER_:
2908 {
2909 GuiText_setString (field -> text, value);
2910 }
2911 break;
2912 case _kUiField_type::HEADING_:
2913 case _kUiField_type::COMMENT_:
2914 case _kUiField_type::CAPTION_:
2915 {
2916 GuiLabel_setText (field -> label, value);
2917 }
2918 break;
2919 default:
2920 {
2921 fatalField (me);
2922 }
2923 }
2924 return;
2925 }
2926 }
2927 Melder_crash (U"Text field not found in command window “", my name.get(), U"”.");
2928 }
2929
2930 void UiForm_setList (UiForm me, integer *p_integerVariable, conststring32 *p_stringVariable,
2931 constSTRVEC strings, integer defaultValue) {
2932 for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
2933 UiField field = my field [ifield].get();
2934 if (field -> integerVariable == p_integerVariable || field -> stringVariable == p_stringVariable) {
2935 switch (field -> type)
2936 {
2937 case _kUiField_type::LIST_:
2938 {
2939 field -> strings = strings;
2940 GuiList_deleteAllItems (field -> list);
2941 for (integer i = 1; i <= strings.size; i ++)
2942 GuiList_insertItem (field -> list, strings [i], 0);
2943
2944 integer currentlySelectedStringIndex = defaultValue;
2945 if (field -> previouslySelectedListString)
2946 for (integer i = 1; i <= strings.size; i ++) {
2947 if (str32equ (strings [i], field -> previouslySelectedListString.get())) {
2948 currentlySelectedStringIndex = i;
2949 break;
2950 }
2951 }
2952
2953 if (currentlySelectedStringIndex < 1 || currentlySelectedStringIndex > strings.size)
2954 currentlySelectedStringIndex = 1;
2955 if (strings.size > 0)
2956 GuiList_selectItem (field -> list, currentlySelectedStringIndex);
2957 }
2958 break;
2959 default:
2960 {
2961 fatalField (me);
2962 }
2963 }
2964 return;
2965 }
2966 }
2967 Melder_crash (U"List field not found in command window “", my name.get(), U"”.");
2968 }
2969
2970 void UiForm_setColourAsGreyValue (UiForm me, MelderColour *p_variable, double greyValue) {
2971 for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
2972 UiField field = my field [ifield].get();
2973 if (field -> colourVariable == p_variable) {
2974 switch (field -> type)
2975 {
2976 case _kUiField_type::COLOUR_:
2977 {
2978 GuiText_setString (field -> text, Melder_double (greyValue));
2979 }
2980 break;
2981 default:
2982 {
2983 fatalField (me);
2984 }
2985 }
2986 return;
2987 }
2988 }
2989 Melder_crash (U"Colour field not found in command window “", my name.get(), U"”.");
2990 }
2991
2992 static UiField findField (UiForm me, conststring32 fieldName) {
2993 for (int ifield = 1; ifield <= my numberOfFields; ifield ++)
2994 if (str32equ (fieldName, my field [ifield] -> name.get()))
2995 return my field [ifield].get();
2996 return nullptr;
2997 }
2998
2999 static UiField findField_check (UiForm me, conststring32 fieldName) {
3000 UiField result = findField (me, fieldName);
3001 if (! result)
3002 Melder_throw (U"Cannot find field “", fieldName, U"” in form.\n"
3003 U"The script may have changed while the form was open.\n"
3004 U"Please click Cancel in the form and try again.");
3005 return result;
3006 }
3007
3008 double UiForm_getReal_check (UiForm me, conststring32 fieldName) {
3009 UiField field = findField_check (me, fieldName);
3010 switch (field -> type)
3011 {
3012 case _kUiField_type::REAL_:
3013 case _kUiField_type::REAL_OR_UNDEFINED_:
3014 case _kUiField_type::NONNEGATIVE_:
3015 case _kUiField_type::POSITIVE_:
3016 {
3017 return field -> realValue;
3018 }
3019 break;
3020 default:
3021 {
3022 Melder_throw (U"Cannot find a real value in field “", fieldName, U"” in the form.\n"
3023 U"The script may have changed while the form was open.\n"
3024 U"Please click Cancel in the form and try again.");
3025 }
3026 }
3027 return 0.0;
3028 }
3029
3030 integer UiForm_getInteger (UiForm me, conststring32 fieldName) {
3031 UiField field = findField (me, fieldName);
3032 if (! field)
3033 Melder_crash (U"(UiForm_getInteger:) No field “", fieldName, U"” in command window “", my name.get(), U"”.");
3034 switch (field -> type)
3035 {
3036 case _kUiField_type::INTEGER_:
3037 case _kUiField_type::NATURAL0_:
3038 case _kUiField_type::NATURAL1_:
3039 case _kUiField_type::CHANNEL_:
3040 case _kUiField_type::BOOLEAN_:
3041 case _kUiField_type::CHOICE_:
3042 case _kUiField_type::OPTIONMENU_:
3043 case _kUiField_type::LIST_:
3044 {
3045 return field -> integerValue;
3046 }
3047 break;
3048 default:
3049 {
3050 fatalField (me);
3051 }
3052 }
3053 return 0;
3054 }
3055
3056 integer UiForm_getInteger_check (UiForm me, conststring32 fieldName) {
3057 UiField field = findField_check (me, fieldName);
3058 switch (field -> type)
3059 {
3060 case _kUiField_type::INTEGER_:
3061 case _kUiField_type::NATURAL0_:
3062 case _kUiField_type::NATURAL1_:
3063 case _kUiField_type::CHANNEL_:
3064 case _kUiField_type::BOOLEAN_:
3065 case _kUiField_type::CHOICE_:
3066 case _kUiField_type::OPTIONMENU_:
3067 case _kUiField_type::LIST_:
3068 {
3069 return field -> integerValue;
3070 }
3071 break;
3072 default:
3073 {
3074 Melder_throw (U"Cannot find an integer value in field “", fieldName, U"” in the form.\n"
3075 U"The script may have changed while the form was open.\n"
3076 U"Please click Cancel in the form and try again.");
3077 }
3078 }
3079 return 0;
3080 }
3081
3082 char32 * UiForm_getString (UiForm me, conststring32 fieldName) {
3083 UiField field = findField (me, fieldName);
3084 if (! field)
3085 Melder_crash (U"(UiForm_getString:) No field “", fieldName, U"” in command window “", my name.get(), U"”.");
3086 switch (field -> type)
3087 {
3088 case _kUiField_type::WORD_:
3089 case _kUiField_type::SENTENCE_:
3090 case _kUiField_type::TEXT_:
3091 case _kUiField_type::FORMULA_:
3092 case _kUiField_type::INFILE_:
3093 case _kUiField_type::OUTFILE_:
3094 case _kUiField_type::FOLDER_:
3095 {
3096 return field -> stringValue.get(); // BUG dangle
3097 }
3098 break;
3099 case _kUiField_type::CHOICE_:
3100 case _kUiField_type::OPTIONMENU_:
3101 {
3102 UiOption b = field -> options.at [field -> integerValue];
3103 return b -> name.get();
3104 }
3105 break;
3106 case _kUiField_type::LIST_:
3107 {
3108 return (char32 *) field -> strings [field -> integerValue];
3109 }
3110 break;
3111 default:
3112 {
3113 fatalField (me);
3114 }
3115 }
3116 return nullptr;
3117 }
3118
3119 char32 * UiForm_getString_check (UiForm me, conststring32 fieldName) {
3120 UiField field = findField_check (me, fieldName);
3121 switch (field -> type)
3122 {
3123 case _kUiField_type::WORD_:
3124 case _kUiField_type::SENTENCE_:
3125 case _kUiField_type::TEXT_:
3126 case _kUiField_type::INFILE_:
3127 case _kUiField_type::OUTFILE_:
3128 case _kUiField_type::FOLDER_:
3129 {
3130 return field -> stringValue.get();
3131 }
3132 break;
3133 case _kUiField_type::CHOICE_:
3134 case _kUiField_type::OPTIONMENU_:
3135 {
3136 UiOption b = field -> options.at [field -> integerValue];
3137 return b -> name.get();
3138 }
3139 break;
3140 case _kUiField_type::LIST_:
3141 {
3142 return (char32 *) field -> strings [field -> integerValue];
3143 }
3144 break;
3145 default:
3146 {
3147 Melder_throw (U"Cannot find a string in field “", fieldName, U"” in the form.\n"
3148 U"The script may have changed while the form was open.\n"
3149 U"Please click Cancel in the form and try again.");
3150 }
3151 }
3152 return nullptr;
3153 }
3154
3155 VEC UiForm_getRealVector (UiForm me, conststring32 fieldName) {
3156 UiField field = findField (me, fieldName);
3157 if (! field)
3158 Melder_crash (U"(UiForm_getRealVector:) No field “", fieldName, U"” in command window “", my name.get(), U"”.");
3159 switch (field -> type)
3160 {
3161 case _kUiField_type::REALVECTOR_:
3162 case _kUiField_type::NONNEGATIVEVECTOR_:
3163 case _kUiField_type::POSITIVEVECTOR_:
3164 {
3165 return field -> realVectorValue.get();
3166 }
3167 break;
3168 default:
3169 {
3170 fatalField (me);
3171 }
3172 }
3173 return VEC();
3174 }
3175
3176 INTVEC UiForm_getIntegerVector (UiForm me, conststring32 fieldName) {
3177 UiField field = findField (me, fieldName);
3178 if (! field)
3179 Melder_crash (U"(UiForm_getIntegerVector:) No field “", fieldName, U"” in command window “", my name.get(), U"”.");
3180 switch (field -> type)
3181 {
3182 case _kUiField_type::INTEGERVECTOR_:
3183 case _kUiField_type::NATURAL0VECTOR_:
3184 case _kUiField_type::NATURAL1VECTOR_:
3185 {
3186 return field -> integerVectorValue.get();
3187 }
3188 break;
3189 default:
3190 {
3191 fatalField (me);
3192 }
3193 }
3194 return INTVEC();
3195 }
3196
3197 MelderColour UiForm_getColour_check (UiForm me, conststring32 fieldName) {
3198 UiField field = findField_check (me, fieldName);
3199 switch (field -> type)
3200 {
3201 case _kUiField_type::COLOUR_: {
3202 return field -> colourValue;
3203 }
3204 break;
3205 default:
3206 {
3207 Melder_throw (U"Cannot find a colour value in field “", fieldName, U"” in the form.\n"
3208 U"The script may have changed while the form was open.\n"
3209 U"Please click Cancel in the form and try again.");
3210 }
3211 }
3212 return Melder_BLACK;
3213 }
3214
3215 void UiForm_Interpreter_addVariables (UiForm me, Interpreter interpreter) {
3216 static MelderString lowerCaseFieldName;
3217 for (int ifield = 1; ifield <= my numberOfFields; ifield ++) {
3218 UiField field = my field [ifield].get();
3219 MelderString_copy (& lowerCaseFieldName, field -> name.get());
3220 /*
3221 Change e.g. "Number of people" to "number_of_people".
3222 */
3223 lowerCaseFieldName.string [0] = Melder_toLowerCase (lowerCaseFieldName.string [0]);
3224 for (char32 *p = & lowerCaseFieldName.string [0]; *p != U'\0'; p ++) {
3225 if (Melder_isHorizontalSpace (*p))
3226 *p = U'_';
3227 }
3228 switch (field -> type)
3229 {
3230 case _kUiField_type::REAL_:
3231 case _kUiField_type::REAL_OR_UNDEFINED_:
3232 case _kUiField_type::NONNEGATIVE_:
3233 case _kUiField_type::POSITIVE_:
3234 {
3235 InterpreterVariable var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
3236 var -> numericValue = field -> realValue;
3237 }
3238 break;
3239 case _kUiField_type::INTEGER_:
3240 case _kUiField_type::NATURAL0_:
3241 case _kUiField_type::NATURAL1_:
3242 case _kUiField_type::CHANNEL_:
3243 case _kUiField_type::BOOLEAN_:
3244 {
3245 InterpreterVariable var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
3246 var -> numericValue = field -> integerValue;
3247 }
3248 break;
3249 case _kUiField_type::CHOICE_:
3250 case _kUiField_type::OPTIONMENU_:
3251 {
3252 InterpreterVariable var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
3253 var -> numericValue = field -> integerValue;
3254 MelderString_appendCharacter (& lowerCaseFieldName, U'$');
3255 var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
3256 UiOption b = field -> options.at [field -> integerValue];
3257 var -> stringValue = Melder_dup (b -> name.get());
3258 }
3259 break;
3260 case _kUiField_type::LIST_:
3261 {
3262 InterpreterVariable var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
3263 var -> numericValue = field -> integerValue;
3264 MelderString_appendCharacter (& lowerCaseFieldName, U'$');
3265 var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
3266 var -> stringValue = Melder_dup (field -> strings [field -> integerValue]);
3267 }
3268 break;
3269 case _kUiField_type::WORD_:
3270 case _kUiField_type::SENTENCE_:
3271 case _kUiField_type::TEXT_:
3272 case _kUiField_type::FORMULA_:
3273 case _kUiField_type::INFILE_:
3274 case _kUiField_type::OUTFILE_:
3275 case _kUiField_type::FOLDER_:
3276 {
3277 MelderString_appendCharacter (& lowerCaseFieldName, U'$');
3278 InterpreterVariable var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
3279 var -> stringValue = Melder_dup (field -> stringValue.get());
3280 }
3281 break;
3282 case _kUiField_type::REALVECTOR_:
3283 case _kUiField_type::NONNEGATIVEVECTOR_:
3284 case _kUiField_type::POSITIVEVECTOR_:
3285 {
3286 MelderString_appendCharacter (& lowerCaseFieldName, U'#');
3287 InterpreterVariable var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
3288 var -> numericVectorValue = copy_VEC (field -> realVectorValue.get()); // TODO: can we move this instead of copying it?
3289 }
3290 break;
3291 case _kUiField_type::INTEGERVECTOR_:
3292 case _kUiField_type::NATURAL0VECTOR_:
3293 case _kUiField_type::NATURAL1VECTOR_:
3294 {
3295 MelderString_appendCharacter (& lowerCaseFieldName, U'#');
3296 InterpreterVariable var = Interpreter_lookUpVariable (interpreter, lowerCaseFieldName.string);
3297 var -> numericVectorValue = cast_VEC (field -> integerVectorValue.get());
3298 }
3299 break;
3300 case _kUiField_type::COLOUR_:
3301 {
3302 // to be implemented
3303 }
3304 break;
3305 default:
3306 {
3307 }
3308 }
3309 }
3310 }
3311
3312 int UiForm_getClickedContinueButton (UiForm me) {
3313 return my clickedContinueButton;
3314 }
3315
3316 /* End of file Ui.cpp */