Debian Codegraph @

Nasal/gui/XMLDialog.nas

raw
1 # XML Dialog - XML dialog object without using PUI
2 # SPDX-FileCopyrightText: (C) 2022 James Turner <[email protected]>
3 # SPDX-License-Identifier: GPL-2.0-or-later
4
5 # alias this module to keep things somewhat readable
6 var cwidgets = canvas.gui.widgets;
7
8 # This is the Nasal peer of a dialog defined by XML
9 # it manages creating some top level Canvas (eg a Window) according
10 # to the XML data
11 var XMLDialog = {
12
13 init: func(dialogProps)
14 {
15 var d = me.dialog();
16 var sz = [d.width, d.height];
17 me._window = canvas.Window.new(sz, "dialog", d.name);
18
19 # FIXME: lookup translated dialog name, this is the
20 # key string here
21 me._window.setTitle(d.name);
22
23 var m = me;
24 me._window.del = func {
25 m.onWindowClosed();
26 }
27 },
28
29 didBuild: func()
30 {
31 var ourCanvas = me._window.getCanvas(1); # create, probably
32 ourCanvas.set("background", canvas.style.getColor("bg_color"));
33 var rootCanvasGroup = ourCanvas.createGroup();
34
35 # get the root XMLObject, almost certainly a container of
36 # some kind. (eg frame / group / scroll area)
37 var rootObject = me.dialog().root;
38
39 # show the root object inside our Canvas group. We could delay this
40 # until we are made visible to make things more efficient/lazy
41 rootObject.show(rootCanvasGroup);
42 ourCanvas.setLayout(rootObject.layoutItem());
43 },
44
45 # this is the callback from the canvas.Window: we request a close of
46 # the dialog, which will end up with us in 'onClosed'
47 onWindowClosed: func
48 {
49 logprint(LOG_WARN, "XMLDialog window was requested to closed");
50 me.dialog().close();
51
52 # hack: manually trigger onClose for now. This should happen automatically
53 # in response to dialog().close().
54 me.onClose();
55 },
56
57 onBringToFront: func()
58 {
59 me._window.raise();
60 },
61
62 onGeometryChanged: func()
63 {
64 var d = me.dialog();
65 logprint(LOG_INFO, "Dialog geometry is now:" ~ d.x ~ ", " ~ d.y ~ " w:" ~ d.width ~ ", h:" ~ d.height);
66 me._window.setPosition(d.x, d.y);
67 me._window.setSize(d.width, d.height);
68 },
69
70 onClose: func
71 {
72 logprint(LOG_WARN, "XMLDialog closed");
73
74 # call the base canvas.Window delete method, not
75 # our wrapper above.
76 call(canvas.Window.del, [], me._window);
77 }
78
79 };
80
81 # this is the callback function which C++ invokes, to create a peer
82 # for an XML dialog (and its C++ class). In the future, different kinds
83 # of dialog could be created if necessary (eg, with different window frame
84 # styles or frameless / non-draggable)
85 var _createDialogPeer = func(type)
86 {
87 logprint(LOG_INFO, "Creating dialog of type:" ~ type);
88 var z = gui.xml.Dialog.new({
89 parents: [XMLDialog]
90 });
91
92 return z;
93 };
94
95 var _getProp = func(objectProps, name, def)
96 {
97 var node = objectProps.getNode(name);
98 if (node == nil)
99 return def;
100 return node.getValue();
101 }
102
103 var _getBoolProp = func(objectProps, name, def)
104 {
105 var node = objectProps.getNode(name);
106 if (node == nil)
107 return def;
108 return node.getBoolValue();
109 }
110
111 var _getDoubleProp = func(objectProps, name, def)
112 {
113 var node = objectProps.getNode(name);
114 if (node == nil)
115 return def;
116 return node.getDoubleValue();
117 }
118
119
120 #################################################################################################
121
122 var XMLObjectBase =
123 {
124 _configValue: func(name, def = nil)
125 {
126 var node = me.config.getNode(name);
127 if (node == nil)
128 return def;
129 return node.getValue();
130 },
131
132 _configDouble: func(name, def = 0.0)
133 {
134 var node = me.config.getNode(name);
135 if (node == nil)
136 return def;
137 return node.getDoubleValue();
138 },
139
140 _configBool: func(name, def = 0)
141 {
142 var node = me.config.getNode(name);
143 if (node == nil)
144 return def;
145 return node.getBoolValue();
146 },
147
148 _applyLayoutConfig: func()
149 {
150 var halign = me._configValue("halign");
151 var valign = me._configValue("valign");
152 var l = me.layoutItem();
153 if (!l)
154 return;
155
156 if (halign or valign) {
157 var ha = 0;
158 if (halign == "left") {
159 ha = canvas.AlignLeft;
160 } elsif (halign == "center") {
161 ha = canvas.AlignHCenter;
162 } elsif (halign == "right") {
163 ha = canvas.AlignRight;
164 }
165
166 var va = 0;
167 if (valign == "top") {
168 va = canvas.AlignTop;
169 } elsif (valign == "center") {
170 va = canvas.AlignVCenter;
171 } elsif (valign == "bottom") {
172 va = canvas.AlignBottom;
173 }
174
175 l.setAlignment(ha + va);
176 }
177
178 if (ghosttype(l) == "canvas.Widget") {
179 var fixedWidth = me._configValue("width");
180 var fixedHeight = me._configValue("height");
181 if (fixedWidth or fixedHeight) {
182 # query existing values in case we only write to
183 # one axis, instead of both
184 var minSize = l.minimumSize();
185 var maxSize = l.maximumSize();
186 if (fixedWidth) {
187 minSize[0] = fixedWidth;
188 maxSize[0] = fixedWidth;
189 }
190 if (fixedHeight) {
191 minSize[1] = fixedHeight;
192 maxSize[1] = fixedHeight;
193 }
194 l.setMaximumSize(maxSize);
195 l.setMinimumSize(minSize);
196 }
197
198 var prefWidth = me._configValue("pref-width");
199 var prefHeight = me._configValue("pref-height");
200 if (prefWidth or prefHeight) {
201 var hint = l.sizeHint();
202 if (prefWidth) {
203 hint[0] = prefWidth;
204 }
205 if (prefHeight) {
206 hint[1] = prefHeight;
207 }
208
209 l.setSizeHint(hint);
210 }
211 } else {
212 # layout item is not a NasalWidget, so lacks public
213 # setters for these. Could extend the API if we need to
214 # cover these cases where XML dialogs specify sizes on
215 # layouts
216 }
217
218 },
219
220 view: func { return me._view; },
221 layoutItem : func { return me._layout; }
222 };
223
224 #################################################################################################
225 # XML object peers
226 #
227 # For each object defined in XML, we create a C++ object (PUICompatObject) and invoke
228 # the callback below (_createCompatObject) to create a corresponding Nasal peer. There is
229 # no requirement for a 1:1 mapping from XML types to the classes below.
230 #
231 # The Nasal peer class responds to callbacks from C++, especially showing and hiding, to
232 # create some visual representation of the object. It's important that this view of the
233 # object is only created on demand, so that invisible GUI elements do not create Canvas
234 # objects, which have a rendering cost.
235
236 var XMLButton =
237 {
238 init: func(objectProps)
239 {
240 me._isDefault = me._configBool("default");
241 me._label = me.configValue("legend");
242 },
243
244 show: func(viewParent)
245 {
246 me._view = cwidgets.Button.new(viewParent, canvas.style, {});
247 me._view.setText(me._label);
248 me._layout = me._view;
249
250 if (me._isDefault) {
251 me._view.setDefault(1);
252 }
253
254 # hook up the button to our bindings
255 me._view.listen("clicked", func me.activateBindings(); );
256 me._applyLayoutConfig();
257 }
258
259 };
260
261 var XMLCheckbox =
262 {
263 init: func(objectProps)
264 {
265 me._label = me.configValue("label");
266
267 },
268
269 show: func(viewParent)
270 {
271 me._view = cwidgets.CheckBox.new(viewParent, canvas.style, {});
272 me._view.setText(me._label);
273 me._layout = me._view;
274 me._applyLayoutConfig();
275 me.update();
276 return me._view;
277 },
278
279 update: func()
280 {
281 if (me._view == nil)
282 return;
283
284 var v = me.value;
285 if (v) {
286 me._view.setText(str(v));
287 }
288 }
289
290 };
291
292 var XMLLabel =
293 {
294 init: func(objectProps)
295 {
296 me._label = me.configValue("label");
297 },
298
299 show: func(viewParent)
300 {
301 me._view = cwidgets.Label.new(viewParent, canvas.style, {});
302 me._view.setText(me._label);
303 me._layout = me._view;
304 me._applyLayoutConfig();
305 me.update();
306 return me._view;
307 },
308
309 update: func()
310 {
311 if (me._view == nil)
312 return;
313
314 var v = me.value;
315 if (v) {
316 me._view.setText(str(v));
317 }
318 }
319 };
320
321 var XMLGroup =
322 {
323 init: func(objectProps)
324 {
325 me._layoutType = me.configValue("layout");
326 me._padding = me._configDouble("default-padding");
327 },
328
329 show: func(viewParent)
330 {
331 me._view = viewParent.createChild("group");
332
333 # check for layout on us
334 var layout = nil;
335 if (me._layoutType == "hbox") {
336 layout = canvas.HBoxLayout.new();
337 } elsif (me._layoutType == "vbox") {
338 layout = canvas.VBoxLayout.new();
339 } elsif (me._layoutType == "table") {
340 layout = canvas.GridLayout.new();
341 } else {
342 logprint(LOG_WARN, "Unknown layout type:" ~ me._layoutType);
343 }
344
345 if (layout != nil) {
346 me._layout = layout;
347 layout.setSpacing(me._padding);
348 me._applyLayoutConfig();
349 }
350
351 foreach (var c; me.children) {
352 # create view for each child
353 c.show(me._view);
354 if (layout != nil) {
355 var childItem = c.layoutItem();
356
357 if (me._layoutType == "table") {
358 var gpos = c.gridLocation();
359 layout.addItem(childItem, gpos.column, gpos.row, gpos.columnSpan, gpos.rowSpan);
360 } else {
361 layout.addItem(childItem);
362
363 # TODO: support 'equal' configBool here, set
364 # stretch factor as well?
365
366 # old layout.cxx code only implements stretch on
367 # hbox and vbox, so this is the correct equivalent place
368 # for compatability
369 if (c._configBool("stretch")) {
370 layout.setStretchFactor(childItem, 1.0);
371 }
372 }
373 } # of children show+layout iteration
374 # record the childView elsewhere?
375 }
376
377 return me._view;
378 },
379
380 update: func()
381 {
382 # re-create children is we are visible?
383 }
384 };
385
386 var XMLSlider =
387 {
388 init: func(objectProps)
389 {
390 # TODO: support vertical sliders
391 },
392
393 show: func(viewParent)
394 {
395 me._view = cwidgets.Slider.new(viewParent, canvas.style, {});
396 me._layout = me._view;
397 me._applyLayoutConfig();
398 me.update();
399 return me._view;
400 },
401
402 update: func()
403 {
404
405
406 }
407 };
408
409 var XMLDial =
410 {
411 init: func(objectProps)
412 {
413 logprint(LOG_INFO, "Init of XMLDial");
414
415
416 },
417
418 show: func(viewParent)
419 {
420 me._view = cwidgets.Dial.new(viewParent, canvas.style, {});
421 me._layout = me._view;
422 me._applyLayoutConfig();
423 me.update();
424 return me._view;
425 },
426
427 update: func()
428 {
429
430 }
431 };
432
433 var XMLTextEdit =
434 {
435 init: func(objectProps)
436 {
437 # TODO: config to restrict to numerical / decimal input, etc
438 },
439
440 show: func(viewParent)
441 {
442 me._view = cwidgets.LineEdit.new(viewParent, canvas.style, {});
443 me._layout = me._view;
444 me._applyLayoutConfig();
445 me.update();
446 return me._view;
447 },
448
449 update: func()
450 {
451 # don't overwrite if it has focus
452 if (me._view and !me._view.hasActiveFocus()) {
453 me._view.setText(str(me.value));
454 }
455 }
456 };
457
458 var XMLEmpty =
459 {
460 init: func(objectProps)
461 {
462 },
463
464 show: func(viewParent)
465 {
466 # TODO: add Nasal-Canvas API to create spacer items
467 # explicitly
468 me._view = canvas.createChild("empty", "group");
469 me._layout = canvas.Spacer.new();
470 me._applyLayoutConfig();
471
472 return me._view;
473 },
474
475 update: func()
476 {
477 me._view.setSize(me._size);
478 }
479 };
480
481 var XMLHRule =
482 {
483 show: func(viewParent)
484 {
485 var label = me.configValue("label");
486
487 me._view = cwidgets.HorizontalRule.new(viewParent, canvas.style, {});
488 if (label) {
489 me._view.setText(label);
490 }
491
492 me._layout = me._view;
493 me._applyLayoutConfig();
494 return me._view;
495 },
496
497 update: func()
498 {
499 # allow label to be updated live
500 var l = me.value();
501 if (l) {
502 me._view.setText(l);
503 }
504
505 me._view.setSize(me._size);
506 }
507 };
508
509 var XMLVRule =
510 {
511 show: func(viewParent)
512 {
513 me._view = cwidgets.VerticalRule.new(viewParent, canvas.style, {});
514 me._layout = me._view;
515 me._applyLayoutConfig();
516 return me._view;
517 }
518 };
519
520
521 var XMLComboBox =
522 {
523 init: func(objectProps)
524 {
525 },
526
527 show: func(viewParent)
528 {
529 # do we support a label or is that a seperate widget?
530
531 me._view = cwidgets.ComboBox.new(viewParent, canvas.style, {});
532 me._layout = me._view;
533 me._applyLayoutConfig();
534 me.update();
535 return me._view;
536 },
537
538 update: func()
539 {
540 if (me._view and !me._view.hasActiveFocus()) {
541 me._view.setSelectedByValue(me.value);
542 }
543 }
544 };
545
546 var XMLList = {
547 show: func(viewParent) {
548 me._view = canvas.gui.widgets.List.new(viewParent);
549
550 foreach (var valueNode; me.config.getChildren("value")) {
551 me._view.createItem(valueNode);
552 }
553 me._view.listen("selection-changed", func {
554 var propertyPath = me._configValue("property");
555 var selection = me._view.getSelectedItems();
556 if (propertyPath and size(selection)) {
557 props.globals.getNode(propertyPath, 1).setValue(selection[0].getData("text"));
558 }
559 me.activateBindings();
560 });
561
562 me._layout = me._view;
563 me._applyLayoutConfig();
564 return me._view;
565 },
566
567 update: func() {
568 me._view.setSize(me._size);
569 },
570
571 valueChanged: func() {
572 me._view.filter(me.value);
573 }
574 };
575
576 # this is the callback function invoked by C++ to build Nasal peers
577 # for the C++ objects defined by XML (PUICompatObject). It's primarly
578 # a factory method: once the peer object is created, all other behaviour
579 # is passed to it.
580 var _createCompatObject = func(type)
581 {
582 # default to a label
583 var widget = XMLLabel;
584
585 # FIXME use a hash for these :)
586 if (type == "button") {
587 widget = XMLButton;
588 }
589
590 if (type == "checkbox") {
591 widget = XMLCheckbox;
592 }
593
594 if (type == "slider") {
595 widget = XMLSlider;
596 }
597
598 if (type == "dial") {
599 widget = XMLDial;
600 }
601
602 if (type == "group") {
603 widget = XMLGroup;
604 }
605
606 if (type == "input") {
607 widget = XMLTextEdit;
608 }
609
610 if (type == "empty") {
611 widget = XMLEmptyWidget;
612 }
613
614 if (type == "hrule") {
615 widget = XMLHRule;
616 }
617
618 if (type == "vrule") {
619 widget = XMLVRule;
620 }
621
622 if (type == "combo") {
623 widget = XMLComboBox;
624 }
625
626 if (type == "list") {
627 widget = XMLList;
628 }
629
630 return gui.xml.Object.new({
631 parents: [widget, XMLObjectBase]
632 }, type);
633 };
634
635 logprint(LOG_INFO, "Loaded gui.XMLDialog");
636