Debian Codegraph @

sys/DataEditor.cpp

raw symbols SCIP
1 /* DataEditor.cpp
2 *
3 * Copyright (C) 1995-2022 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #define NAME_X 30
20 #define TEXT_X 250
21 #define BUTTON_X 250
22 #define LIST_Y (2 * Gui_TOP_DIALOG_SPACING + Gui_PUSHBUTTON_HEIGHT)
23 #define EDITOR_WIDTH 820
24 #define EDITOR_HEIGHT (Machine_getMenuBarBottom () + LIST_Y + kDataSubEditor_MAXNUM_ROWS * ROW_HEIGHT + 29)
25 #define ROW_HEIGHT 31
26
27 #define SCROLL_BAR_WIDTH Machine_getScrollBarWidth ()
28
29 #include "DataEditor.h"
30 #include "EditorM.h"
31 #include "Collection.h"
32 #include "machine.h"
33
34 static Data_Description Class_getDescription (ClassInfo table) {
35 return ((Daata) _Thing_dummyObject (table)) -> v_description ();
36 }
37
38 static void VectorEditor_create (DataEditor root, conststring32 title, void *address,
39 Data_Description description, integer minimum, integer maximum);
40
41 static void MatrixEditor_create (DataEditor root, conststring32 title, void *address,
42 Data_Description description, integer min1, integer max1, integer min2, integer max2);
43
44 static void StructEditor_create (DataEditor root, conststring32 title, void *address, Data_Description description);
45
46 static void ClassEditor_create (DataEditor root, conststring32 title, void *address, Data_Description description);
47
48 static inline conststring32 strip_d (conststring32 s) {
49 return s && s [0] == U'd' && s [1] == U'_' ? & s [2] : & s [0];
50 }
51
52 /********** DataSubEditor **********/
53
54 Thing_implement (DataSubEditor, Editor, 0);
55
56 void structDataSubEditor :: v9_destroy () noexcept {
57 //for (int i = 1; i <= kDataSubEditor_MAXNUM_ROWS; i ++)
58 // Melder_free (d_fieldData [i]. history);
59 if (our root)
60 for (integer i = our root -> children.size; i > 0; i --)
61 if (our root -> children.at [i] == this)
62 our root -> children. subtractItem_ref (i);
63 DataSubEditor_Parent :: v9_destroy ();
64 }
65
66 static void update (DataSubEditor me) {
67
68 /* Hide all the existing widgets. */
69
70 for (integer i = 1; i <= kDataSubEditor_MAXNUM_ROWS; i ++) {
71 my d_fieldData [i]. address = nullptr;
72 my d_fieldData [i]. description = nullptr;
73 GuiThing_hide (my d_fieldData [i]. label);
74 GuiThing_hide (my d_fieldData [i]. button);
75 GuiThing_hide (my d_fieldData [i]. text);
76 }
77
78 my d_irow = 0;
79 my v_showMembers ();
80 }
81
82 static Data_Description DataSubEditor_findNumberUse (DataSubEditor me, conststring32 number) {
83 Data_Description structDescription, result;
84 char32 string [100];
85 if (my classInfo == classMatrixEditor)
86 return nullptr; // no structs inside
87 if (my classInfo == classVectorEditor) {
88 if (my d_description -> type != structwa)
89 return nullptr; // no structs inside
90 structDescription = * (Data_Description *) my d_description -> tagType;
91 } else { /* StructEditor or ClassEditor or DataEditor. */
92 structDescription = my d_description;
93 }
94 Melder_sprint (string,100, number);
95 if ((result = Data_Description_findNumberUse (structDescription, string)) != nullptr)
96 return result;
97 Melder_sprint (string,100, number, U" - 1");
98 if ((result = Data_Description_findNumberUse (structDescription, string)) != nullptr)
99 return result;
100 return nullptr;
101 }
102
103 static void gui_button_cb_change (DataSubEditor me, GuiButtonEvent /* event */) {
104 int irow = 1;
105 for (; irow <= kDataSubEditor_MAXNUM_ROWS; irow ++) {
106 #if motif
107 const bool visible = XtIsManaged (my d_fieldData [irow]. text -> d_widget);
108 #elif gtk
109 gboolean visible;
110 g_object_get (G_OBJECT (my d_fieldData [irow]. text -> d_widget), "visible", & visible, nullptr);
111 #elif defined (macintosh)
112 const bool visible = ! [(GuiCocoaTextField *) my d_fieldData [irow]. text -> d_widget isHidden];
113 #else
114 const bool visible = false;
115 #endif
116 if (visible) {
117 int type = my d_fieldData [irow]. description -> type;
118 if (type > maxsingletypewa)
119 continue;
120 autostring32 text = GuiText_getString (my d_fieldData [irow]. text);
121 switch (type) {
122 case bytewa: {
123 signed char oldValue = * (signed char *) my d_fieldData [irow]. address, newValue = (signed char) Melder_atoi (text.get());
124 if (newValue != oldValue) {
125 Data_Description numberUse = DataSubEditor_findNumberUse (me, my d_fieldData [irow]. description -> name);
126 if (numberUse)
127 Melder_flushError (U"Changing field \"", strip_d (my d_fieldData [irow]. description -> name),
128 U"\" would damage the array \"", strip_d (numberUse -> name), U"\".");
129 else
130 * (signed char *) my d_fieldData [irow]. address = newValue;
131 }
132 } break;
133 case int16wa: {
134 int16 oldValue = * (int16 *) my d_fieldData [irow]. address;
135 int64 newValue = Melder_atoi (text.get());
136 if (newValue != oldValue) {
137 Data_Description numberUse = DataSubEditor_findNumberUse (me, my d_fieldData [irow]. description -> name);
138 if (numberUse)
139 Melder_flushError (U"Changing field \"", strip_d (my d_fieldData [irow]. description -> name),
140 U"\" would damage the array \"", strip_d (numberUse -> name), U"\".");
141 else if (newValue < INT16_MIN || newValue > INT16_MAX)
142 Melder_flushError (U"Field \"", strip_d (my d_fieldData [irow]. description -> name),
143 U"\" can have no values less than ", INT16_MIN, U" or greater than ", INT16_MAX, U".");
144 else
145 * (int16 *) my d_fieldData [irow]. address = (int16) newValue; // guarded conversion
146 }
147 } break;
148 case intwa: {
149 int oldValue = * (int *) my d_fieldData [irow]. address, newValue = (int) Melder_atoi (text.get());
150 if (newValue != oldValue) {
151 Data_Description numberUse = DataSubEditor_findNumberUse (me, my d_fieldData [irow]. description -> name);
152 if (numberUse)
153 Melder_flushError (U"Changing field \"", strip_d (my d_fieldData [irow]. description -> name),
154 U"\" would damage the array \"", strip_d (numberUse -> name), U"\".");
155 else
156 * (int *) my d_fieldData [irow]. address = newValue;
157 }
158 } break;
159 case integerwa: {
160 integer oldValue = * (integer *) my d_fieldData [irow]. address, newValue = Melder_atoi (text.get());
161 if (newValue != oldValue) {
162 Data_Description numberUse = DataSubEditor_findNumberUse (me, my d_fieldData [irow]. description -> name);
163 if (numberUse)
164 Melder_flushError (U"Changing field \"", strip_d (my d_fieldData [irow]. description -> name),
165 U"\" would damage the array \"", strip_d (numberUse -> name), U"\".");
166 else
167 * (integer *) my d_fieldData [irow]. address = newValue;
168 }
169 } break;
170 case ubytewa: {
171 * (unsigned char *) my d_fieldData [irow]. address = (uint8) Melder_atoi (text.get());
172 } break;
173 case uintwa: {
174 * (unsigned int *) my d_fieldData [irow]. address = (uint32) Melder_atoi (text.get());
175 } break;
176 case uintegerwa: {
177 * (uinteger *) my d_fieldData [irow]. address = (uinteger) Melder_atoi (text.get());
178 } break;
179 case floatwa: {
180 * (double *) my d_fieldData [irow]. address = Melder_atof (text.get());
181 } break;
182 case doublewa: {
183 * (double *) my d_fieldData [irow]. address = Melder_atof (text.get());
184 } break;
185 case complexwa: { dcomplex *x = (dcomplex *) my d_fieldData [irow]. address;
186 double re, im;
187 sscanf (Melder_peek32to8 (text.get()), "%lf + %lf i", & re, & im);
188 x -> real (re);
189 x -> imag (im);
190 } break;
191 case enumwa: {
192 if (Melder_length (text.get()) < 3)
193 goto error;
194 text [Melder_length (text.get()) - 1] = U'\0'; // remove trailing ">"
195 int value = ((int (*) (conststring32)) (my d_fieldData [irow]. description -> tagType)) (text.get() + 1); // skip leading "<"
196 if (value < 0)
197 goto error;
198 * (signed char *) my d_fieldData [irow]. address = (signed char) value;
199 } break;
200 case lenumwa: {
201 if (Melder_length (text.get()) < 3)
202 goto error;
203 text [Melder_length (text.get()) - 1] = U'\0'; // remove trailing ">"
204 int value = ((int (*) (conststring32)) (my d_fieldData [irow]. description -> tagType)) (text.get() + 1); // skip leading "<"
205 if (value < 0)
206 goto error;
207 * (signed short *) my d_fieldData [irow]. address = (signed short) value;
208 } break;
209 case booleanwa: {
210 bool value;
211 if (str32nequ (text.get(), U"<true>", 6))
212 value = true;
213 else if (str32nequ (text.get(), U"<false>", 7))
214 value = false;
215 else
216 goto error;
217 * (bool *) my d_fieldData [irow]. address = value;
218 } break;
219 case questionwa: {
220 bool value;
221 if (str32nequ (text.get(), U"<yes>", 5))
222 value = true;
223 else if (str32nequ (text.get(), U"<no>", 4))
224 value = false;
225 else
226 goto error;
227 * (bool *) my d_fieldData [irow]. address = value;
228 } break;
229 case stringwa:
230 case lstringwa: {
231 char32 *old = * (char32 **) my d_fieldData [irow]. address;
232 Melder_free (old);
233 * (char32 **) my d_fieldData [irow]. address = Melder_dup_f (text.get()).transfer();
234 } break;
235 default: break;
236 }
237 }
238 }
239 /*
240 Several collaborators have to be notified of this change:
241 1. The owner (creator) of our root DataEditor: so that she can notify other editors, if any.
242 2. All our sibling DataSubEditors.
243 */
244 Melder_assert (my root);
245 Editor_broadcastDataChanged (my root);
246 Melder_assert (my root);
247 update (me);
248 Melder_assert (my root);
249 for (int isub = 1; isub <= my root -> children.size; isub ++) {
250 DataSubEditor subeditor = my root -> children.at [isub];
251 if (subeditor != me)
252 update (subeditor);
253 }
254 return;
255 error:
256 Melder_flushError (U"Edit field \"", strip_d (my d_fieldData [irow]. description -> name), U"\" or click \"Cancel\".");
257 }
258
259 static void gui_button_cb_cancel (DataSubEditor me, GuiButtonEvent /* event */) {
260 update (me);
261 }
262
263 static void gui_cb_scroll (DataSubEditor me, GuiScrollBarEvent event) {
264 my d_topField = GuiScrollBar_getValue (event -> scrollBar) + 1;
265 update (me);
266 }
267
268 static void gui_button_cb_open (DataSubEditor me, GuiButtonEvent event) {
269 integer ifield = 0;
270 static MelderString name;
271 MelderString_empty (& name);
272
273 /* Identify the pressed button; it must be one of those created in the list. */
274
275 for (integer i = 1; i <= kDataSubEditor_MAXNUM_ROWS; i ++)
276 if (my d_fieldData [i]. button == event -> button) {
277 ifield = i;
278 break;
279 }
280 Melder_assert (ifield != 0);
281
282 /* Launch the appropriate subeditor. */
283
284 DataSubEditor_FieldData fieldData = & my d_fieldData [ifield];
285 if (! fieldData -> description) {
286 Melder_casual (U"Not yet implemented.");
287 return; // not yet implemented
288 }
289
290 if (fieldData -> description -> rank == 1 || fieldData -> description -> rank == 3 || fieldData -> description -> rank < 0) {
291 MelderString_append (& name, fieldData -> history.get(), U". ", strip_d (fieldData -> description -> name),
292 U" [", fieldData -> minimum, U"..", fieldData -> maximum, U"]");
293 VectorEditor_create (my root, name.string, fieldData -> address,
294 fieldData -> description, fieldData -> minimum, fieldData -> maximum);
295 } else if (fieldData -> description -> rank == 2) {
296 MelderString_append (& name, fieldData -> history.get(), U". ", strip_d (fieldData -> description -> name),
297 U" [", fieldData -> minimum, U"..", fieldData -> maximum, U"]");
298 MelderString_append (& name, U" [", fieldData -> min2, U"..", fieldData -> max2, U"]");
299 MatrixEditor_create (my root, name.string, fieldData -> address, fieldData -> description,
300 fieldData -> minimum, fieldData -> maximum, fieldData -> min2, fieldData -> max2);
301 } else if (fieldData -> description -> type == structwa) {
302 MelderString_append (& name, fieldData -> history.get(), U". ", strip_d (fieldData -> description -> name));
303 StructEditor_create (my root, name.string, fieldData -> address,
304 * (Data_Description *) fieldData -> description -> tagType);
305 } else if (fieldData -> description -> type == objectwa ||
306 fieldData -> description -> type == collectionofwa ||
307 fieldData -> description -> type == collectionwa) {
308 MelderString_append (& name, fieldData -> history.get(), U". ", strip_d (fieldData -> description -> name));
309 ClassEditor_create (my root, name.string, fieldData -> address,
310 Class_getDescription ((ClassInfo) fieldData -> description -> tagType));
311 } else /*if (fieldData -> description -> type == inheritwa)*/ {
312 ClassEditor_create (my root, fieldData -> history.get(), fieldData -> address,
313 fieldData -> description);
314 /* } else {
315 Melder_casual (
316 U"Strange editor \"", strip_d (fieldData -> description -> name),
317 U"\" required (type ", fieldData -> description -> type,
318 U", rank ", fieldData -> description -> rank,
319 U")."
320 );*/
321 }
322 }
323
324 void structDataSubEditor :: v_createChildren () {
325 int x = Gui_LEFT_DIALOG_SPACING, y = Gui_TOP_DIALOG_SPACING + Machine_getMenuBarBottom (), buttonWidth = 120;
326
327 GuiButton_createShown (our windowForm, x, x + buttonWidth, y, y + Gui_PUSHBUTTON_HEIGHT,
328 U"Change", gui_button_cb_change, this, 0);
329 x += buttonWidth + Gui_HORIZONTAL_DIALOG_SPACING;
330 GuiButton_createShown (our windowForm, x, x + buttonWidth, y, y + Gui_PUSHBUTTON_HEIGHT,
331 U"Cancel", gui_button_cb_cancel, this, 0);
332
333 y = Machine_getMenuBarBottom () + LIST_Y;
334 d_scrollBar = GuiScrollBar_createShown (our windowForm,
335 - SCROLL_BAR_WIDTH, 0, y, 0,
336 0, d_numberOfFields, 0, d_numberOfFields < kDataSubEditor_MAXNUM_ROWS ? d_numberOfFields : kDataSubEditor_MAXNUM_ROWS, 1, kDataSubEditor_MAXNUM_ROWS - 1,
337 gui_cb_scroll, this, 0
338 );
339
340 y += 10;
341 for (int i = 1; i <= kDataSubEditor_MAXNUM_ROWS; i ++) {
342 d_fieldData [i]. label = GuiLabel_create (our windowForm, 0, 200, y, y + Gui_TEXTFIELD_HEIGHT, U"label", 0); // no fixed x value: sometimes indent
343 d_fieldData [i]. button = GuiButton_create (our windowForm, BUTTON_X, BUTTON_X + buttonWidth, y, y + Gui_TEXTFIELD_HEIGHT,
344 U"Open", gui_button_cb_open, this, 0);
345 d_fieldData [i]. text = GuiText_create (our windowForm, TEXT_X, -30, y, y + Gui_TEXTFIELD_HEIGHT, 0);
346 d_fieldData [i]. y = y;
347 y += ROW_HEIGHT;
348 }
349 }
350
351 static void menu_cb_help (DataSubEditor, EDITOR_ARGS) { Melder_help (U"Inspect"); }
352
353 void structDataSubEditor :: v_createMenuItems_help (EditorMenu menu) {
354 DataSubEditor_Parent :: v_createMenuItems_help (menu);
355 EditorMenu_addCommand (menu, U"DataEditor help", '?', menu_cb_help);
356 }
357
358 static void DataSubEditor_init (DataSubEditor me, DataEditor root, conststring32 title, void *address, Data_Description description) {
359 my root = root;
360 if (me != root)
361 root -> children.addItem_ref (me);
362 my d_address = address;
363 my d_description = description;
364 my d_topField = 1;
365 my d_numberOfFields = my v_countFields ();
366 Editor_init (me, 0, 0, EDITOR_WIDTH, EDITOR_HEIGHT, title, nullptr);
367 update (me);
368 }
369
370 /********** StructEditor **********/
371
372 Thing_implement (StructEditor, DataSubEditor, 0);
373
374 integer structStructEditor :: v_countFields () {
375 return Data_Description_countMembers (d_description);
376 }
377
378 static conststring32 singleTypeToText (void *address, int type, void *tagType, MelderString *buffer) {
379 switch (type) {
380 case bytewa: MelderString_append (buffer, Melder_integer (* (signed char *) address)); break;
381 case int16wa: MelderString_append (buffer, Melder_integer (* (int16 *) address)); break;
382 case intwa: MelderString_append (buffer, Melder_integer (* (int *) address)); break;
383 case integerwa: MelderString_append (buffer, Melder_integer (* (integer *) address)); break;
384 case ubytewa: MelderString_append (buffer, Melder_integer (* (unsigned char *) address)); break;
385 case uintwa: MelderString_append (buffer, Melder_integer (* (unsigned int *) address)); break;
386 case uintegerwa: MelderString_append (buffer, Melder_integer (* (uinteger *) address)); break;
387 case floatwa: MelderString_append (buffer, Melder_single (* (double *) address)); break;
388 case doublewa: MelderString_append (buffer, Melder_double (* (double *) address)); break;
389 case complexwa: MelderString_append (buffer, Melder_dcomplex (* (dcomplex *) address)); break;
390 case enumwa: MelderString_append (buffer, U"<", ((conststring32 (*) (int)) tagType) (* (signed char *) address), U">"); break;
391 case lenumwa: MelderString_append (buffer, U"<", ((conststring32 (*) (int)) tagType) (* (signed short *) address), U">"); break;
392 case booleanwa: MelderString_append (buffer, * (bool *) address ? U"<true>" : U"<false>"); break;
393 case questionwa: MelderString_append (buffer, * (bool *) address ? U"<yes>" : U"<no>" ); break;
394 case stringwa:
395 case lstringwa: {
396 char32 *string = * (char32 **) address;
397 if (! string) {
398 MelderString_empty (buffer); // convert null string to empty string
399 return buffer -> string;
400 }
401 return string; // may be much longer than the usual size of 'buffer'
402 } break;
403 default: return U"(unknown)";
404 }
405 return buffer -> string; // Mind the special return for strings above.
406 }
407
408 static void showStructMember (
409 void *structAddress, /* The address of (the first member of) the struct. */
410 Data_Description structDescription, /* The description of (the first member of) the struct. */
411 Data_Description memberDescription, /* The description of the current member. */
412 DataSubEditor_FieldData fieldData, /* The widgets in which to show the info about the current member. */
413 char32 *history)
414 {
415 int type = memberDescription -> type, rank = memberDescription -> rank;
416 bool isSingleType = ( type <= maxsingletypewa && rank == 0 );
417 unsigned char *memberAddress = (unsigned char *) structAddress + memberDescription -> offset;
418 if (type == inheritwa) {
419 GuiLabel_setText (fieldData -> label,
420 Melder_cat (U"Class part \"", memberDescription -> name, U"\":"));
421 } else {
422 GuiLabel_setText (fieldData -> label,
423 Melder_cat (U" ", strip_d (memberDescription -> name),
424 ( rank == 0 ? U"" : rank == 1 || rank == 3 || rank < 0 ? U" [ ]" : U" [ ] [ ]" )));
425 }
426 //GuiControl_move (fieldData -> label, type == inheritwa ? 0 : NAME_X, fieldData -> y);
427 GuiThing_show (fieldData -> label);
428
429 /* Show the value (for a single type) or a button (for a composite type). */
430 if (isSingleType) {
431 #if motif
432 XtVaSetValues (fieldData -> text -> d_widget, XmNcolumns, 60, nullptr); // TODO: change to GuiObject_size
433 #endif
434 autoMelderString buffer;
435 conststring32 text = singleTypeToText (memberAddress, type, memberDescription -> tagType, & buffer);
436 GuiText_setString (fieldData -> text, text);
437 GuiThing_show (fieldData -> text);
438 fieldData -> address = memberAddress;
439 fieldData -> description = memberDescription;
440 fieldData -> rank = 0;
441 } else if (rank == 1) {
442 void *arrayAddress = * (void **) memberAddress;
443 integer minimum, maximum;
444 if (! arrayAddress)
445 return; // no button for empty fields
446 Data_Description_evaluateInteger (structAddress, structDescription,
447 memberDescription -> min1, & minimum);
448 Data_Description_evaluateInteger (structAddress, structDescription,
449 memberDescription -> max1, & maximum);
450 if (maximum < minimum)
451 return; // no button if no elements
452 fieldData -> address = arrayAddress; // indirect
453 fieldData -> description = memberDescription;
454 fieldData -> minimum = minimum; // normally 1
455 fieldData -> maximum = maximum;
456 fieldData -> rank = 1;
457 fieldData -> history = Melder_dup_f (history);
458 GuiThing_show (fieldData -> button);
459 } else if (rank < 0) {
460 /*
461 This represents an in-line array.
462 */
463 integer maximum; /* But: capacity = - rank */
464 Data_Description_evaluateInteger (structAddress, structDescription,
465 memberDescription -> max1, & maximum);
466 if (-- maximum < 0)
467 return; // subtract one for zero-based array; no button if no elements
468 fieldData -> address = memberAddress; /* Direct. */
469 fieldData -> description = memberDescription;
470 fieldData -> minimum = 0; // in-line arrays start with index 0
471 fieldData -> maximum = maximum; // probably between -1 and capacity - 1
472 fieldData -> rank = rank;
473 fieldData -> history = Melder_dup_f (history);
474 GuiThing_show (fieldData -> button);
475 } else if (rank == 3) {
476 /*
477 This represents an in-line set.
478 */
479 fieldData -> address = memberAddress; // direct
480 fieldData -> description = memberDescription;
481 fieldData -> minimum = str32equ (((conststring32 (*) (int)) memberDescription -> min1) (0), U"_") ? 1 : 0;
482 fieldData -> maximum = ((int (*) (conststring32)) memberDescription -> max1) (U"\n");
483 fieldData -> rank = rank;
484 fieldData -> history = Melder_dup_f (history);
485 GuiThing_show (fieldData -> button);
486 } else if (rank == 2) {
487 constMAT mat = * (constMAT *) memberAddress;
488 //Melder_casual (U"Showing matrix member with ", mat.nrow, U" rows and ", mat.ncol, U" columns.");
489 if (NUMisEmpty (mat))
490 return; // no button for empty fields
491 integer min1, max1, min2, max2;
492 Data_Description_evaluateInteger (structAddress, structDescription,
493 memberDescription -> min1, & min1);
494 Data_Description_evaluateInteger (structAddress, structDescription,
495 memberDescription -> max1, & max1);
496 Data_Description_evaluateInteger (structAddress, structDescription,
497 memberDescription -> min2, & min2);
498 Data_Description_evaluateInteger (structAddress, structDescription,
499 memberDescription -> max2, & max2);
500 if (max1 < min1 || max2 < min2)
501 return; // no button if no elements
502 fieldData -> address = memberAddress; // direct
503 fieldData -> description = memberDescription;
504 fieldData -> minimum = min1; // normally 1
505 fieldData -> maximum = max1;
506 fieldData -> min2 = min2;
507 fieldData -> max2 = max2;
508 fieldData -> rank = 2;
509 fieldData -> history = Melder_dup_f (history);
510 GuiThing_show (fieldData -> button);
511 } else if (type == structwa) { // in-line struct
512 fieldData -> address = memberAddress; // direct
513 fieldData -> description = memberDescription;
514 fieldData -> rank = 0;
515 fieldData -> history = Melder_dup_f (history);
516 GuiThing_show (fieldData -> button);
517 } else if (type == objectwa || type == collectionwa) {
518 fieldData -> address = * (Daata *) memberAddress; // indirect // FIXME: not guaranteed for auto objects
519 if (! fieldData -> address)
520 return; // no button if no object
521 fieldData -> description = memberDescription;
522 fieldData -> rank = 0;
523 fieldData -> history = Melder_dup_f (history);
524 GuiThing_show (fieldData -> button);
525 } else if (type == collectionofwa) {
526 fieldData -> address = (Daata) memberAddress; // direct // FIXME: not guaranteed for auto objects
527 //Melder_casual (U"Daata ", Melder_pointer (fieldData -> address));
528 //Melder_casual (U"Class ", ((Daata) fieldData -> address) -> classInfo -> className);
529 if (! fieldData -> address)
530 return; // no button if no object
531 fieldData -> description = memberDescription;
532 fieldData -> rank = 0;
533 fieldData -> history = Melder_dup_f (history);
534 GuiThing_show (fieldData -> button);
535 }
536 }
537
538 static void showStructMembers (DataSubEditor me, void *structAddress, Data_Description structDescription, int fromMember, char32 *history) {
539 integer i = 1;
540 Data_Description memberDescription = structDescription;
541 for (; i < fromMember && memberDescription -> name != nullptr; i ++, memberDescription ++)
542 (void) 0;
543 for (; memberDescription -> name != nullptr; memberDescription ++) {
544 if (++ my d_irow > kDataSubEditor_MAXNUM_ROWS)
545 return;
546 showStructMember (structAddress, structDescription, memberDescription, & my d_fieldData [my d_irow], history);
547 }
548 }
549
550 void structStructEditor :: v_showMembers () {
551 showStructMembers (this, our d_address, our d_description, our d_topField, our name.get());
552 }
553
554 static void StructEditor_init (StructEditor me, DataEditor root, conststring32 title, void *address, Data_Description description) {
555 DataSubEditor_init (me, root, title, address, description);
556 }
557
558 static void StructEditor_create (DataEditor root, conststring32 title, void *address, Data_Description description) {
559 try {
560 autoStructEditor me = Thing_new (StructEditor);
561 StructEditor_init (me.get(), root, title, address, description);
562 return me.releaseToUser();
563 } catch (MelderError) {
564 Melder_throw (U"Struct inspector window not created.");
565 }
566 }
567
568 /********** VectorEditor **********/
569
570 Thing_implement (VectorEditor, DataSubEditor, 0);
571
572 integer structVectorEditor :: v_countFields () {
573 integer numberOfElements = d_maximum - d_minimum + 1;
574 if (d_description -> type == structwa)
575 return numberOfElements * (Data_Description_countMembers (* (Data_Description *) d_description -> tagType) + 1);
576 else
577 return numberOfElements;
578 }
579
580 void structVectorEditor :: v_showMembers () {
581 int type = our d_description -> type;
582 bool isSingleType = ( type <= maxsingletypewa );
583 integer elementSize = ( type == structwa ?
584 Data_Description_countMembers (* (Data_Description *) d_description -> tagType) + 1 : 1 );
585 integer firstElement = d_minimum + (d_topField - 1) / elementSize;
586
587 for (integer ielement = firstElement; ielement <= d_maximum; ielement ++) {
588 int skip = ielement == firstElement ? (our d_topField - 1) % elementSize : 0;
589
590 if (++ our d_irow > kDataSubEditor_MAXNUM_ROWS) return;
591 DataSubEditor_FieldData fieldData = & our d_fieldData [d_irow];
592
593 if (isSingleType) {
594 /*
595 TODO: assume the address refers to a constVEC;
596 this is not yet true of Collections, and
597 indirect has to be changed to direct in showStructMember()
598 */
599 unsigned char *elementAddress = (unsigned char *) our d_address + (ielement - 1) * our d_description -> size;
600
601 GuiControl_move (fieldData -> label, 0, fieldData -> y);
602 GuiLabel_setText (fieldData -> label,
603 Melder_cat (strip_d (our d_description -> name), U" [",
604 ( our d_description -> rank == 3 ? ((conststring32 (*) (int)) our d_description -> min1) (ielement) : Melder_integer (ielement) ),
605 U"]"));
606 GuiThing_show (fieldData -> label);
607
608 autoMelderString buffer;
609 conststring32 text = singleTypeToText (elementAddress, type, our d_description -> tagType, & buffer);
610 #if motif
611 XtVaSetValues (fieldData -> text -> d_widget, XmNcolumns, 60, nullptr); // TODO: change to GuiObject_size
612 #endif
613 GuiText_setString (fieldData -> text, text);
614 GuiThing_show (fieldData -> text);
615 fieldData -> address = elementAddress;
616 fieldData -> description = d_description;
617 } else if (type == structwa) {
618 /*
619 TODO: assume the address refers to a constVEC;
620 this is not yet true of Collections, and
621 indirect has to be changed to direct in showStructMember()
622 */
623 unsigned char *elementAddress = (unsigned char *) our d_address + (ielement - 1) * our d_description -> size;
624
625 static MelderString history;
626 MelderString_copy (& history, our name.get());
627
628 /* Replace things like [1..100] by things like [19]. */
629
630 if (history.string [history.length - 1] == ']') {
631 char32 *openingBracket = str32rchr (history.string, U'[');
632 Melder_assert (openingBracket != nullptr);
633 * openingBracket = '\0';
634 history.length = openingBracket - history.string;
635 }
636 MelderString_append (& history, U"[", ielement, U"]");
637
638 if (skip) {
639 our d_irow --;
640 } else {
641 GuiControl_move (fieldData -> label, 0, fieldData -> y);
642 GuiLabel_setText (fieldData -> label,
643 Melder_cat (strip_d (d_description -> name), U" [", ielement, U"]: ---------------------------"));
644 GuiThing_show (fieldData -> label);
645 }
646 showStructMembers (this, elementAddress, * (Data_Description *) d_description -> tagType, skip, history.string);
647 } else if (type == objectwa) {
648 /*
649 TODO: assume the address refers to the items of a Collection (?)
650 */
651 unsigned char *elementAddress = (unsigned char *) our d_address + ielement * our d_description -> size;
652
653 static MelderString history;
654 MelderString_copy (& history, our name.get());
655 if (history.string [history.length - 1] == U']') {
656 char32 *openingBracket = str32rchr (history.string, U'[');
657 Melder_assert (openingBracket != nullptr);
658 * openingBracket = U'\0';
659 history.length = openingBracket - history.string;
660 }
661 MelderString_append (& history, U"[", ielement, U"]");
662
663 GuiControl_move (fieldData -> label, 0, fieldData -> y);
664 GuiLabel_setText (fieldData -> label, Melder_cat (strip_d (our d_description -> name), U" [", ielement, U"]"));
665 GuiThing_show (fieldData -> label);
666
667 Daata object = * (Daata *) elementAddress;
668 if (! object)
669 return; // no button if no object
670 if (! Class_getDescription (object -> classInfo))
671 return; // no button if no description for this class
672 fieldData -> address = object;
673 fieldData -> description = Class_getDescription (object -> classInfo);
674 fieldData -> rank = 0;
675 fieldData -> history = Melder_dup_f (history.string);
676 GuiThing_show (fieldData -> button);
677 }
678 }
679 }
680
681 static void VectorEditor_create (DataEditor root, conststring32 title, void *address,
682 Data_Description description, integer minimum, integer maximum)
683 {
684 try {
685 autoVectorEditor me = Thing_new (VectorEditor);
686 my d_minimum = minimum;
687 my d_maximum = maximum;
688 DataSubEditor_init (me.get(), root, title, address, description);
689 return me.releaseToUser();
690 } catch (MelderError) {
691 Melder_throw (U"Vector inspector window not created.");
692 }
693 }
694
695 /********** MatrixEditor **********/
696
697 Thing_implement (MatrixEditor, DataSubEditor, 0);
698
699 integer structMatrixEditor :: v_countFields () {
700 integer numberOfElements = (d_maximum - d_minimum + 1) * (d_max2 - d_min2 + 1);
701 if (d_description -> type == structwa)
702 return numberOfElements * (Data_Description_countMembers (* (Data_Description *) d_description -> tagType) + 1);
703 else
704 return numberOfElements;
705 }
706
707 void structMatrixEditor :: v_showMembers () {
708 int type = our d_description -> type;
709 bool isSingleType = ( type <= maxsingletypewa );
710 Melder_assert (isSingleType); // allow no struct matrices
711 integer rowSize = our d_max2 - our d_min2 + 1;
712 constMAT mat = * (constMAT *) d_address; // HACK: this could be a MAT or an INTMAT
713 Melder_assert (rowSize == mat.ncol); // HACK: this should work correctly even for an INTMAT
714 integer firstRow = d_minimum + (d_topField - 1) / rowSize;
715 integer firstColumn = d_min2 + (d_topField - 1 - (firstRow - d_minimum) * rowSize);
716
717 for (integer irow = firstRow; irow <= d_maximum; irow ++)
718 for (integer icolumn = irow == firstRow ? firstColumn : d_min2; icolumn <= d_max2; icolumn ++) {
719 unsigned char *elementAddress = (unsigned char *) mat.cells + ((irow - 1) * rowSize + (icolumn - 1)) * d_description -> size;
720 // not & mat [irow] [icol], because that HACK would not work for an INTMAT
721
722 if (++ d_irow > kDataSubEditor_MAXNUM_ROWS)
723 return;
724 DataSubEditor_FieldData fieldData = & d_fieldData [d_irow];
725
726 if (isSingleType) {
727 GuiControl_move (fieldData -> label, 0, fieldData -> y);
728 GuiLabel_setText (fieldData -> label, Melder_cat (strip_d (d_description -> name), U" [", irow, U"] [", icolumn, U"]"));
729 GuiThing_show (fieldData -> label);
730
731 autoMelderString buffer;
732 conststring32 text = singleTypeToText (elementAddress, type, d_description -> tagType, & buffer);
733 #if motif
734 XtVaSetValues (fieldData -> text -> d_widget, XmNcolumns, 60, nullptr); // TODO: change to GuiObject_size
735 #endif
736 GuiText_setString (fieldData -> text, text);
737 GuiThing_show (fieldData -> text);
738 fieldData -> address = elementAddress;
739 fieldData -> description = d_description;
740 }
741 }
742 }
743
744 static void MatrixEditor_create (DataEditor root, conststring32 title, void *address,
745 Data_Description description, integer min1, integer max1, integer min2, integer max2)
746 {
747 try {
748 autoMatrixEditor me = Thing_new (MatrixEditor);
749 my d_minimum = min1;
750 my d_maximum = max1;
751 my d_min2 = min2;
752 my d_max2 = max2;
753 DataSubEditor_init (me.get(), root, title, address, description);
754 return me.releaseToUser();
755 } catch (MelderError) {
756 Melder_throw (U"Matrix inspector window not created.");
757 }
758 }
759
760 /********** ClassEditor **********/
761
762 Thing_implement (ClassEditor, StructEditor, 0);
763
764 static void ClassEditor_showMembers_recursive (ClassEditor me, ClassInfo klas) {
765 ClassInfo parentClass = klas -> semanticParent;
766 Data_Description description = Class_getDescription (klas);
767 int classFieldsTraversed = 0;
768 while (Class_getDescription (parentClass) == description)
769 parentClass = parentClass -> semanticParent;
770 if (parentClass != classDaata) {
771 ClassEditor_showMembers_recursive (me, parentClass);
772 classFieldsTraversed = Data_Description_countMembers (Class_getDescription (parentClass));
773 //Melder_casual (U"ClassEditor_showMembers_recursive: classFieldsTraversed = ", classFieldsTraversed);
774 }
775 showStructMembers (me, my d_address, description, my d_irow ? 1 : my d_topField - classFieldsTraversed, my name.get());
776 }
777
778 void structClassEditor :: v_showMembers () {
779 ClassEditor_showMembers_recursive (this, ((Daata) d_address) -> classInfo);
780 }
781
782 static void ClassEditor_init (ClassEditor me, DataEditor root, conststring32 title, void *address, Data_Description description) {
783 if (! description)
784 Melder_throw (U"Class ", Thing_className ((Thing) address), U" cannot be inspected.");
785 StructEditor_init (me, root, title, address, description);
786 }
787
788 static void ClassEditor_create (DataEditor root, conststring32 title, void *address, Data_Description description) {
789 try {
790 autoClassEditor me = Thing_new (ClassEditor);
791 ClassEditor_init (me.get(), root, title, address, description);
792 return me.releaseToUser();
793 } catch (MelderError) {
794 Melder_throw (U"Class inspector window not created.");
795 }
796 }
797
798 /********** DataEditor **********/
799
800 Thing_implement (DataEditor, ClassEditor, 0);
801
802 static void DataEditor_destroyAllChildren (DataEditor me) {
803 /*
804 To destroy all children, we travel them from youngest to oldest,
805 because the array of children will change from under us:
806 */
807 for (int i = my children.size; i >= 1; i --) {
808 /*
809 An optimization coming!
810
811 Instead of
812 DataSubEditor child = my children [i];
813 forget (child);
814 we isolate the child from the parent before destroying the child,
815 so that the child won't try to remove the reference
816 that the parent has to her.
817 So first we make the parent forget the moribund child,
818 which prevents a dangling pointer:
819 */
820 DataSubEditor child = my children. subtractItem_ref (i);
821 /*
822 That was fast, because subtracting the last item involves no shifting
823 of the remaining items.
824
825 Second, we make the child forget the parent,
826 so that the child won't try to remove the reference
827 that the parent had to her (but no longer has):
828 */
829 child -> root = nullptr;
830 /*
831 The child is now fully isolated, so we are ready to destroy her:
832 */
833 forget (child);
834 /*
835 This procedure was an optimization because if we just destroyed each child,
836 each child would remove itself from the array by (1) searching for itself
837 and (2) shifting the remaining children, both of which have a complexity
838 that is linear in the number of children. So we would end up with quadratic complexity,
839 whereas the procedure that we use above has linear complexity.
840
841 This linear complexity makes this procedure good enough for `v9_destroy()`
842 (where obtaining linear complexity would have been easy anyway),
843 and nice enough for `v1_dataChanged()`.
844
845 Something to note is that this procedure doesn't care whether the autoCollection
846 `children` owns its items or not.
847 */
848 }
849 }
850
851 void structDataEditor :: v9_destroy () noexcept {
852 DataEditor_destroyAllChildren (this);
853 DataEditor_Parent :: v9_destroy ();
854 }
855
856 void structDataEditor :: v1_dataChanged (Editor sender) {
857 if (this == sender)
858 return;
859 /*
860 Someone *else* changed our data.
861 We know that the top-level data is still accessible,
862 so we update the top-level window to show the change:
863 */
864 update (this);
865 /*
866 Changing the data may have changed any part of the *structure* of the data,
867 so we do not know if the data visible in any of the subeditors is still valid.
868 We follow the most straightforward solution, which is to simply close all the child windows,
869 which guarantees the removal of all dangling visual representations:
870 */
871 DataEditor_destroyAllChildren (this);
872 }
873
874 autoDataEditor DataEditor_create (conststring32 title, Daata data) {
875 try {
876 ClassInfo klas = data -> classInfo;
877 if (Class_getDescription (klas) == nullptr)
878 Melder_throw (U"Class ", klas -> className, U" cannot be inspected.");
879 autoDataEditor me = Thing_new (DataEditor);
880 ClassEditor_init (me.get(), me.get(), title, data, Class_getDescription (klas));
881 return me;
882 } catch (MelderError) {
883 Melder_throw (U"Inspector window not created.");
884 }
885 }
886
887 /* End of file DataEditor.cpp */