Debian Codegraph @

src/GUI/FGPUIDialog.cxx

raw symbols SCIP
1 // dialog.cxx: implementation of an XML-configurable dialog box.
2
3 #include "MouseCursor.hxx"
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7
8 #include <simgear/debug/BufferedLogCallback.hxx>
9 #include <simgear/props/props_io.hxx>
10 #include <simgear/scene/tsync/terrasync.hxx>
11 #include <simgear/structure/SGBinding.hxx>
12
13 #include <Main/fg_os.hxx>
14 #include <Main/fg_props.hxx>
15 #include <Main/globals.hxx>
16 #include <Scripting/NasalSys.hxx>
17
18 #include "AirportList.hxx"
19 #include "CanvasWidget.hxx"
20 #include "FGColor.hxx"
21 #include "FGFontCache.hxx"
22 #include "FGPUIDialog.hxx"
23 #include "MapWidget.hxx"
24 #include "WaypointList.hxx"
25 #include "layout.hxx"
26 #include "new_gui.hxx"
27 #include "property_list.hxx"
28
29 using std::string;
30
31 enum format_type { f_INVALID,
32 f_INT,
33 f_LONG,
34 f_FLOAT,
35 f_DOUBLE,
36 f_STRING };
37 static const int FORMAT_BUFSIZE = 255;
38 static const int RESIZE_MARGIN = 7;
39
40
41 /**
42 * Makes sure the format matches '%[ -+#]?\d*(\.\d*)?(l?[df]|s)', with
43 * only one number or string placeholder and otherwise arbitrary prefix
44 * and postfix containing only quoted percent signs (%%).
45 */
46 static format_type
47 validate_format(const char* f)
48 {
49 bool l = false;
50 format_type type;
51 for (; *f; f++) {
52 if (*f == '%') {
53 if (f[1] == '%')
54 f++;
55 else
56 break;
57 }
58 }
59 if (*f++ != '%')
60 return f_INVALID;
61 while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0')
62 f++;
63 while (*f && isdigit(*f))
64 f++;
65 if (*f == '.') {
66 f++;
67 while (*f && isdigit(*f))
68 f++;
69 }
70
71 if (*f == 'l')
72 l = true, f++;
73
74 if (*f == 'd') {
75 type = l ? f_LONG : f_INT;
76 } else if (*f == 'f')
77 type = l ? f_DOUBLE : f_FLOAT;
78 else if (*f == 's') {
79 if (l)
80 return f_INVALID;
81 type = f_STRING;
82 } else
83 return f_INVALID;
84
85 for (++f; *f; f++) {
86 if (*f == '%') {
87 if (f[1] == '%')
88 f++;
89 else
90 return f_INVALID;
91 }
92 }
93 return type;
94 }
95
96 ////////////////////////////////////////////////////////////////////////
97
98 //
99 // Custom subclass of puPopup to implement "draggable" windows in the
100 // interface. Note that this is a subclass of puPopup, not
101 // puDialogBox. Sadly, PUI (mis)uses subclassing to implement a
102 // boolean property: modality. That means that we can't implement
103 // dragging of both puPopup windows and puDialogBoxes with the same
104 // code. Rather than duplicate code, I've chosen to implement only
105 // "non-modal dragability" here. Modal dialog boxes (like the exit
106 // confirmation) are not draggable.
107 //
108 class fgPopup : public puPopup
109 {
110 public:
111 fgPopup(FGPUIDialog* parent_dialog, int x, int y, bool r = true, bool d = true) : puPopup(x, y), _draggable(d), parentDialog(parent_dialog), _resizable(r), _dragging(false)
112 {
113 propCurrentDialog = fgGetNode("/sim/gui/dialogs/current-dialog", true);
114 }
115 int checkHit(int b, int up, int x, int y);
116 int checkKey(int key, int updown);
117 int getHitObjects(puObject*, int x, int y);
118 bool checkHitCanvas(puObject*, int x, int y);
119 puObject* getKeyObject(puObject*, int key);
120 puObject* getActiveInputField(puObject*);
121 void applySize(puObject*);
122
123 private:
124 enum { LEFT = 1,
125 RIGHT = 2,
126 TOP = 4,
127 BOTTOM = 8 };
128 bool _draggable;
129 SGPropertyNode_ptr propCurrentDialog;
130 FGPUIDialog* parentDialog;
131 bool _resizable;
132 bool _dragging;
133 int _resizing;
134 FGMouseCursor::Cursor _start_cursor;
135 FGMouseCursor::Cursor _cursor;
136 int _dlgX, _dlgY, _dlgW, _dlgH;
137 int _startX, _startY;
138 };
139
140
141 class fgValueList
142 {
143 public:
144 fgValueList(SGPropertyNode* p);
145 virtual ~fgValueList();
146 virtual void update();
147
148 protected:
149 char** _list;
150
151 private:
152 void make_list();
153 void destroy_list();
154 SGPropertyNode_ptr _props;
155 };
156
157
158 class fgList : public fgValueList, public puaList, public GUI_ID
159 {
160 public:
161 fgList(int x1, int y1, int x2, int y2, SGPropertyNode* p, int sw) : fgValueList(p), puaList(x1, y1, x2, y2, _list, sw), GUI_ID(FGCLASS_LIST) {}
162 void update();
163 };
164
165 class fgComboBox : public fgValueList, public puaComboBox
166 {
167 public:
168 fgComboBox(int x1, int y1, int x2, int y2, SGPropertyNode* p, bool editable) : fgValueList(p),
169 puaComboBox(x1, y1, x2, y2, _list, editable),
170 _inHit(false)
171 {
172 }
173
174 void update(const std::string& v);
175
176 virtual void setSize(int w, int h);
177
178 virtual int checkHit(int b, int up, int x, int y);
179
180 virtual void recalc_bbox();
181
182 private:
183 bool _inHit;
184 };
185
186 class LogList : public puaList, public FGPUIDialog::ActiveWidget, public GUI_ID
187 {
188 public:
189 LogList(int x1, int y1, int x2, int y2, int sw) : puaList(x1, y1, x2, y2, sw),
190 GUI_ID(FGCLASS_LOGLIST)
191 {
192 m_buffer = NULL;
193 m_stamp = 0;
194 }
195
196 void setBuffer(simgear::BufferedLogCallback* buf);
197
198 virtual void update();
199
200 private:
201 std::vector<unsigned char*> m_items;
202 simgear::BufferedLogCallback* m_buffer;
203 unsigned int m_stamp;
204 };
205
206 class fgSelectBox : public fgValueList, public puaSelectBox
207 {
208 public:
209 fgSelectBox(int x1, int y1, int x2, int y2, SGPropertyNode* p) : fgValueList(p), puaSelectBox(x1, y1, x2, y2, _list) {}
210 };
211
212 ////////////////////////////////////////////////////////////////////////
213 // Implementation of GUIInfo.
214 ////////////////////////////////////////////////////////////////////////
215
216 /**
217 * User data for a GUI object.
218 */
219 struct GUIInfo {
220 GUIInfo(FGPUIDialog* d);
221 virtual ~GUIInfo();
222 void apply_format(SGPropertyNode*);
223
224 FGPUIDialog* dialog;
225 SGPropertyNode_ptr node;
226 std::vector<SGBinding*> bindings;
227 int key;
228 string label, legend, text, format;
229 format_type fmt_type;
230 };
231
232 GUIInfo::GUIInfo(FGPUIDialog* d) : dialog(d),
233 key(-1),
234 fmt_type(f_INVALID)
235 {
236 }
237
238 GUIInfo::~GUIInfo()
239 {
240 for (unsigned int i = 0; i < bindings.size(); i++) {
241 delete bindings[i];
242 bindings[i] = 0;
243 }
244 }
245
246 void GUIInfo::apply_format(SGPropertyNode* n)
247 {
248 char buf[FORMAT_BUFSIZE + 1];
249 if (fmt_type == f_INT)
250 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getIntValue());
251 else if (fmt_type == f_LONG)
252 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getLongValue());
253 else if (fmt_type == f_FLOAT)
254 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getFloatValue());
255 else if (fmt_type == f_DOUBLE)
256 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getDoubleValue());
257 else
258 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getStringValue().c_str());
259
260 buf[FORMAT_BUFSIZE] = '\0';
261 text = buf;
262 }
263
264
265 /**
266 * Key handler.
267 */
268 int fgPopup::checkKey(int key, int updown)
269 {
270 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
271 return false;
272
273 puObject* input = getActiveInputField(this);
274 if (input)
275 return input->checkKey(key, updown);
276
277 puObject* object = getKeyObject(this, key);
278 if (!object)
279 return puPopup::checkKey(key, updown);
280
281 // invokeCallback() isn't enough; we need to simulate a mouse button press
282 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
283 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
284 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
285 object->checkHit(PU_LEFT_BUTTON, PU_UP,
286 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
287 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
288 return true;
289 }
290
291 puObject* fgPopup::getKeyObject(puObject* object, int key)
292 {
293 puObject* ret;
294 if (object->getType() & PUCLASS_GROUP)
295 for (puObject* obj = ((puGroup*)object)->getFirstChild();
296 obj; obj = obj->getNextObject())
297 if ((ret = getKeyObject(obj, key)))
298 return ret;
299
300 GUIInfo* info = (GUIInfo*)object->getUserData();
301 if (info && info->key == key)
302 return object;
303
304 return 0;
305 }
306
307 puObject* fgPopup::getActiveInputField(puObject* object)
308 {
309 puObject* ret;
310 if (object->getType() & PUCLASS_GROUP)
311 for (puObject* obj = ((puGroup*)object)->getFirstChild();
312 obj; obj = obj->getNextObject())
313 if ((ret = getActiveInputField(obj)))
314 return ret;
315
316 if (object->getType() & (PUCLASS_INPUT | PUCLASS_LARGEINPUT) && ((puInput*)object)->isAcceptingInput())
317 return object;
318
319 return 0;
320 }
321
322 /**
323 * Mouse handler.
324 */
325 int fgPopup::checkHit(int button, int updown, int x, int y)
326 {
327 int result = 0;
328 // still need to ensure that the hit is within this window. The code used to check _dragging
329 // as well, however that had the unfortunate side effect of allowing clicks to pass through to
330 // the underlying window. RJH-07-08-18.
331 if (updown == PU_UP || updown == PU_DOWN) {
332 result = puPopup::checkHit(button, updown, x, y);
333 // set current dialog name on mouse down.
334 if (updown == PU_DOWN && result && parentDialog && propCurrentDialog) {
335 propCurrentDialog->setStringValue(parentDialog->getName());
336 }
337 }
338 if (!_draggable)
339 return result;
340
341 // This is annoying. We would really want a true result from the
342 // superclass to indicate "handled by child object", but all it
343 // tells us is that the pointer is inside the dialog. So do the
344 // intersection test (again) to make sure we don't start a drag
345 // when inside controls.
346
347 if (updown == PU_DOWN && !_dragging) {
348 if (!result)
349 return 0;
350 int global_drag = fgGetKeyModifiers() & KEYMOD_SHIFT;
351 int global_resize = fgGetKeyModifiers() & KEYMOD_CTRL;
352
353 int hit = getHitObjects(this, x, y);
354 if (hit & PUCLASS_LIST) // ctrl-click in property browser (toggle bool)
355 return result;
356 if (!global_resize && ((hit & (PUCLASS_BUTTON | PUCLASS_ONESHOT | PUCLASS_INPUT | PUCLASS_LARGEINPUT | PUCLASS_SCROLLBAR))
357 // The canvas should handle drag events on its own so exit
358 // here if mouse is over a CanvasWidget
359 || (!global_drag && checkHitCanvas(this, x, y)))) {
360 return result;
361 }
362
363 getPosition(&_dlgX, &_dlgY);
364 getSize(&_dlgW, &_dlgH);
365 _start_cursor = fgGetMouseCursor();
366 _dragging = true;
367 _startX = x;
368 _startY = y;
369
370 // check and prepare for resizing
371 static const FGMouseCursor::Cursor cursor[] = {
372 FGMouseCursor::CURSOR_ARROW,
373 FGMouseCursor::CURSOR_LEFT_SIDE,
374 FGMouseCursor::CURSOR_RIGHT_SIDE,
375 FGMouseCursor::CURSOR_NONE,
376 FGMouseCursor::CURSOR_TOP_SIDE,
377 FGMouseCursor::CURSOR_TOP_LEFT,
378 FGMouseCursor::CURSOR_TOP_RIGHT,
379 FGMouseCursor::CURSOR_NONE,
380 FGMouseCursor::CURSOR_BOTTOM_SIDE,
381 FGMouseCursor::CURSOR_BOTTOM_LEFT,
382 FGMouseCursor::CURSOR_BOTTOM_RIGHT,
383 FGMouseCursor::CURSOR_NONE};
384
385 _resizing = 0;
386 if (!global_drag && _resizable) {
387 int hmargin = global_resize ? _dlgW / 3 : RESIZE_MARGIN;
388 int vmargin = global_resize ? _dlgH / 3 : RESIZE_MARGIN;
389
390 if (y - _dlgY < vmargin)
391 _resizing |= BOTTOM;
392 else if (_dlgY + _dlgH - y < vmargin)
393 _resizing |= TOP;
394
395 if (x - _dlgX < hmargin)
396 _resizing |= LEFT;
397 else if (_dlgX + _dlgW - x < hmargin)
398 _resizing |= RIGHT;
399
400 if (!_resizing && global_resize)
401 _resizing = BOTTOM | RIGHT;
402
403 _cursor = cursor[_resizing];
404 if (_resizing && _resizable) {
405 fgSetMouseCursor(_cursor);
406 }
407 }
408
409 } else if (updown == PU_DRAG && _dragging) {
410 if (_resizing) {
411 GUIInfo* info = (GUIInfo*)getUserData();
412 if (_resizable && info && info->node) {
413 int w = _dlgW;
414 int h = _dlgH;
415 if (_resizing & LEFT)
416 w += _startX - x;
417 if (_resizing & RIGHT)
418 w += x - _startX;
419 if (_resizing & TOP)
420 h += y - _startY;
421 if (_resizing & BOTTOM)
422 h += _startY - y;
423
424 int prefw, prefh;
425 LayoutWidget wid(info->node);
426 wid.calcPrefSize(&prefw, &prefh);
427 if (w < prefw)
428 w = prefw;
429 if (h < prefh)
430 h = prefh;
431
432 int x = _dlgX;
433 int y = _dlgY;
434 if (_resizing & LEFT)
435 x += _dlgW - w;
436 if (_resizing & BOTTOM)
437 y += _dlgH - h;
438
439 wid.layout(x, y, w, h);
440 setSize(w, h);
441 setPosition(x, y);
442 applySize(static_cast<puObject*>(this));
443 getFirstChild()->setSize(w, h); // dialog background puFrame
444 }
445 } else {
446 int posX = x + _dlgX - _startX,
447 posY = y + _dlgY - _startY;
448 setPosition(posX, posY);
449
450 GUIInfo* info = (GUIInfo*)getUserData();
451 if (info && info->node) {
452 info->node->setIntValue("x", posX);
453 info->node->setIntValue("y", posY);
454 }
455 } // re-positioning
456
457 } else if (_dragging) {
458 fgSetMouseCursor(_start_cursor);
459 _dragging = false;
460 }
461 return result;
462 }
463
464 int fgPopup::getHitObjects(puObject* object, int x, int y)
465 {
466 if (!object->isVisible())
467 return 0;
468
469 int type = 0;
470 if (object->getType() & PUCLASS_GROUP)
471 for (puObject* obj = ((puGroup*)object)->getFirstChild();
472 obj; obj = obj->getNextObject())
473 type |= getHitObjects(obj, x, y);
474
475 int cx, cy, cw, ch;
476 object->getAbsolutePosition(&cx, &cy);
477 object->getSize(&cw, &ch);
478 if (x >= cx && x < cx + cw && y >= cy && y < cy + ch)
479 type |= object->getType();
480 return type;
481 }
482
483 bool fgPopup::checkHitCanvas(puObject* object, int x, int y)
484 {
485 if (!object->isVisible())
486 return 0;
487
488 if (object->getType() & PUCLASS_GROUP) {
489 for (puObject* obj = ((puGroup*)object)->getFirstChild();
490 obj;
491 obj = obj->getNextObject()) {
492 if (checkHitCanvas(obj, x, y))
493 return true;
494 }
495 }
496
497 int cx, cy, cw, ch;
498 object->getAbsolutePosition(&cx, &cy);
499 object->getSize(&cw, &ch);
500 if (x >= cx && x < cx + cw && y >= cy && y < cy + ch && dynamic_cast<CanvasWidget*>(object))
501 return true;
502 return false;
503 }
504
505 void fgPopup::applySize(puObject* object)
506 {
507 // compound plib widgets use setUserData() for internal purposes, so refuse
508 // to descend into anything that has other bits set than the following
509 const int validUserData = PUCLASS_VALUE | PUCLASS_OBJECT | PUCLASS_GROUP | PUCLASS_INTERFACE | PUCLASS_FRAME | PUCLASS_TEXT | PUCLASS_BUTTON | PUCLASS_ONESHOT | PUCLASS_INPUT | PUCLASS_ARROW | PUCLASS_DIAL | PUCLASS_POPUP;
510
511 int type = object->getType();
512 if (type & PUCLASS_GROUP && !(type & ~validUserData))
513 for (puObject* obj = ((puGroup*)object)->getFirstChild();
514 obj; obj = obj->getNextObject())
515 applySize(obj);
516
517 GUIInfo* info = (GUIInfo*)object->getUserData();
518 if (!info)
519 return;
520
521 SGPropertyNode* n = info->node;
522 if (!n) {
523 SG_LOG(SG_GENERAL, SG_ALERT, "fgPopup::applySize: no props");
524 return;
525 }
526 int x = n->getIntValue("x");
527 int y = n->getIntValue("y");
528 int w = n->getIntValue("width", 4);
529 int h = n->getIntValue("height", 4);
530 object->setPosition(x, y);
531 object->setSize(w, h);
532 }
533
534 ////////////////////////////////////////////////////////////////////////
535
536 void FGPUIDialog::ConditionalObject::update(FGPUIDialog* aDlg)
537 {
538 if (_name == "visible") {
539 bool wasVis = _pu->isVisible() != 0;
540 bool newVis = test();
541
542 if (newVis == wasVis) {
543 return;
544 }
545
546 if (newVis) { // puObject needs a setVisible. Oh well.
547 _pu->reveal();
548 } else {
549 _pu->hide();
550 }
551 } else if (_name == "enable") {
552 bool wasEnabled = _pu->isActive() != 0;
553 bool newEnable = test();
554
555 if (wasEnabled == newEnable) {
556 return;
557 }
558
559 if (newEnable) {
560 _pu->activate();
561 } else {
562 _pu->greyOut();
563 }
564 }
565
566 aDlg->setNeedsLayout();
567 }
568
569 ////////////////////////////////////////////////////////////////////////
570 // Callbacks.
571 ////////////////////////////////////////////////////////////////////////
572
573 /**
574 * Action callback.
575 */
576 static void
577 action_callback(puObject* object)
578 {
579 GUIInfo* info = (GUIInfo*)object->getUserData();
580 auto gui = globals->get_subsystem<NewGUI>();
581 gui->setActiveDialog(info->dialog);
582 int nBindings = info->bindings.size();
583 for (int i = 0; i < nBindings; i++) {
584 info->bindings[i]->fire();
585 if (gui->getActiveDialog() == 0)
586 break;
587 }
588 gui->setActiveDialog(0);
589 }
590
591 ////////////////////////////////////////////////////////////////////////
592 // Static helper functions.
593 ////////////////////////////////////////////////////////////////////////
594
595 /**
596 * Copy a property value to a PUI object.
597 */
598 static void
599 copy_to_pui(SGPropertyNode* node, puObject* object)
600 {
601 using namespace simgear;
602 GUIInfo* info = (GUIInfo*)object->getUserData();
603 if (!info) {
604 SG_LOG(SG_GENERAL, SG_ALERT, "dialog: widget without GUIInfo!");
605 return; // this can't really happen
606 }
607
608 // Treat puText objects specially, so their "values" can be set
609 // from properties.
610 if (object->getType() & PUCLASS_TEXT) {
611 if (info->fmt_type != f_INVALID)
612 info->apply_format(node);
613 else
614 info->text = node->getStringValue();
615
616 object->setLabel(info->text.c_str());
617 return;
618 }
619
620 switch (node->getType()) {
621 case props::BOOL:
622 case props::INT:
623 case props::LONG:
624 object->setValue(node->getIntValue());
625 break;
626 case props::FLOAT:
627 case props::DOUBLE:
628 object->setValue(node->getFloatValue());
629 break;
630 default:
631 info->text = node->getStringValue();
632 object->setValue(info->text.c_str());
633 break;
634 }
635 }
636
637
638 static void
639 copy_from_pui(puObject* object, SGPropertyNode* node)
640 {
641 using namespace simgear;
642 // puText objects are immutable, so should not be copied out
643 if (object->getType() & PUCLASS_TEXT)
644 return;
645
646 switch (node->getType()) {
647 case props::BOOL:
648 case props::INT:
649 case props::LONG:
650 node->setIntValue(object->getIntegerValue());
651 break;
652 case props::FLOAT:
653 node->setFloatValue(object->getFloatValue());
654 break;
655 case props::DOUBLE: {
656 // puObject only provides float, not double, which causes precision/rounding issues
657 // with some numerical values (try "114.2").
658 // Work around: obtain string value, and manually convert with proper double precision.
659 const char* s = object->getStringValue();
660 node->setDoubleValue(atof(s));
661 break;
662 }
663 default:
664 const char* s = object->getStringValue();
665 if (s)
666 node->setStringValue(s);
667 break;
668 }
669 }
670
671
672 ////////////////////////////////////////////////////////////////////////
673 // Implementation of FGDialog.
674 ////////////////////////////////////////////////////////////////////////
675
676 FGPUIDialog::FGPUIDialog(SGPropertyNode* props) : FGDialog(props),
677 _object(nullptr),
678 _gui(globals->get_subsystem<NewGUI>()),
679 _props(props),
680 _needsRelayout(false)
681 {
682 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
683 _name = props->getStringValue("name", "[unnamed]");
684 SGPropertyNode* nasal = props->getNode("nasal");
685 auto nas = globals->get_subsystem<FGNasalSys>();
686 if (nasal && nas) {
687 _nasal_close = nasal->getNode("close");
688 SGPropertyNode* open = nasal->getNode("open");
689 if (open) {
690 string s = open->getStringValue();
691 nas->createModule(_module.c_str(), _module.c_str(), s.c_str(), s.length(), props);
692 }
693 }
694 display(props);
695 }
696
697 FGPUIDialog::~FGPUIDialog()
698 {
699 int x, y;
700 _object->getAbsolutePosition(&x, &y);
701 _props->setIntValue("lastx", x);
702 _props->setIntValue("lasty", y);
703
704 auto nas = globals->get_subsystem<FGNasalSys>();
705 if (nas) {
706 if (_nasal_close) {
707 string s = _nasal_close->getStringValue();
708 nas->createModule(_module.c_str(), _module.c_str(), s.c_str(), s.length(), _props);
709 }
710 nas->deleteModule(_module.c_str());
711 }
712
713 puDeleteObject(_object);
714
715 unsigned int i;
716 // Delete all the info objects we
717 // were forced to keep around because
718 // PUI cannot delete its own user data.
719 for (i = 0; i < _info.size(); i++) {
720 delete (GUIInfo*)_info[i];
721 _info[i] = 0;
722 }
723 // Finally, delete the property links.
724 for (i = 0; i < _propertyObjects.size(); i++) {
725 delete _propertyObjects[i];
726 _propertyObjects[i] = 0;
727 }
728 }
729
730 void FGPUIDialog::bringToFront()
731 {
732 puMoveToLast(_object);
733 }
734 const char* FGPUIDialog::getName()
735 {
736 return _name.c_str();
737 }
738
739 void FGPUIDialog::runCallback(const std::string& name, SGPropertyNode_ptr args)
740 {
741 auto nas = globals->get_subsystem<FGNasalSys>();
742 if (!nas)
743 return;
744
745 SGPropertyNode* nasalNode = _props->getNode("nasal");
746 if (!nasalNode)
747 return;
748
749 auto callbackNode = nasalNode->getChild(name);
750 if (!callbackNode) {
751 SG_LOG(SG_GUI, SG_DEV_ALERT, "FGPUIDialog::runCallback: no Nasal callback '" << name << "' defined on dialog " << _name);
752 return;
753 }
754
755 string s = callbackNode->getStringValue();
756 string fileName = _module.c_str();
757 nas->handleCommand(_module.c_str(), fileName.c_str(), s.c_str(), args.get());
758 }
759
760 void FGPUIDialog::updateValues(const std::string& objectName)
761 {
762 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
763 const string& name = _propertyObjects[i]->name;
764
765 if (!objectName.empty() && name != objectName)
766 continue;
767
768 puObject* widget = _propertyObjects[i]->object;
769 int widgetType = widget->getType();
770 if (widgetType & PUCLASS_LIST) {
771 GUI_ID* gui_id = dynamic_cast<GUI_ID*>(widget);
772 if (gui_id && (gui_id->id & FGCLASS_LIST)) {
773 fgList* pl = static_cast<fgList*>(widget);
774 pl->update();
775 } else {
776 copy_to_pui(_propertyObjects[i]->node, widget);
777 }
778 } else if (widgetType & PUCLASS_COMBOBOX) {
779 fgComboBox* combo = static_cast<fgComboBox*>(widget);
780 combo->update(_propertyObjects[i]->node->getStringValue());
781 } else {
782 copy_to_pui(_propertyObjects[i]->node, widget);
783 }
784 } // of property objects iteration
785 }
786
787 void FGPUIDialog::applyValues(const std::string& objectName)
788 {
789 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
790 const string& name = _propertyObjects[i]->name;
791 if (!objectName.empty() && name != objectName)
792 continue;
793
794 copy_from_pui(_propertyObjects[i]->object,
795 _propertyObjects[i]->node);
796 }
797 }
798
799 void FGPUIDialog::update()
800 {
801 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
802 puObject* obj = _liveObjects[i]->object;
803 if (obj->getType() & PUCLASS_INPUT && ((puInput*)obj)->isAcceptingInput())
804 continue;
805
806 copy_to_pui(_liveObjects[i]->node, obj);
807 }
808
809 for (unsigned int j = 0; j < _conditionalObjects.size(); ++j) {
810 _conditionalObjects[j]->update(this);
811 }
812
813 for (unsigned int i = 0; i < _activeWidgets.size(); i++) {
814 _activeWidgets[i]->update();
815 }
816
817 if (_needsRelayout) {
818 relayout();
819 }
820 }
821
822 void FGPUIDialog::display(SGPropertyNode* props)
823 {
824 if (_object != 0) {
825 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
826 return;
827 }
828
829 // map from physical to logical units for PUI
830 const double ratio = fgGetDouble("/sim/rendering/gui-pixel-ratio", 1.0);
831 const int physicalWidth = fgGetInt("/sim/startup/xsize"),
832 physicalHeight = fgGetInt("/sim/startup/ysize");
833 const int screenw = static_cast<int>(physicalWidth / ratio),
834 screenh = static_cast<int>(physicalHeight / ratio);
835
836 bool userx = props->hasValue("x");
837 bool usery = props->hasValue("y");
838 bool userw = props->hasValue("width");
839 bool userh = props->hasValue("height");
840
841 // Let the layout widget work in the same property subtree.
842 LayoutWidget wid(props);
843
844 SGPropertyNode* fontnode = props->getNode("font");
845 if (fontnode) {
846 SGPropertyNode* property_node = fontnode->getChild("property");
847 if (property_node)
848 fontnode = globals->get_props()->getNode(property_node->getStringValue());
849 _font = FGFontCache::instance()->get(fontnode);
850 } else {
851 _font = _gui->getDefaultFont();
852 }
853 wid.setDefaultFont(_font, int(_font->getPointSize()));
854
855 int pw = 0, ph = 0;
856 int px, py, savex, savey;
857 if (!userw || !userh)
858 wid.calcPrefSize(&pw, &ph);
859 pw = props->getIntValue("width", pw);
860 ph = props->getIntValue("height", ph);
861 px = savex = props->getIntValue("x", (screenw - pw) / 2);
862 py = savey = props->getIntValue("y", (screenh - ph) / 2);
863
864 // Negative x/y coordinates are interpreted as distance from the top/right
865 // corner rather than bottom/left.
866 if (userx && px < 0)
867 px = screenw - pw + px;
868 if (usery && py < 0)
869 py = screenh - ph + py;
870
871 // Define "x", "y", "width" and/or "height" in the property tree if they
872 // are not specified in the configuration file.
873 wid.layout(px, py, pw, ph);
874
875 // Use the dimension and location properties as specified in the
876 // configuration file or from the layout widget.
877 _object = makeObject(props, screenw, screenh);
878
879 //if (_object->getParent())
880 // _object->getParent()-> //setActiveCallback(FGPUIDialog_active_callback);
881
882 // Remove automatically generated properties, so the layout looks
883 // the same next time around, or restore x and y to preserve negative coords.
884 if (userx)
885 props->setIntValue("x", savex);
886 else
887 props->removeChild("x");
888
889 if (usery)
890 props->setIntValue("y", savey);
891 else
892 props->removeChild("y");
893
894 if (!userw) props->removeChild("width");
895 if (!userh) props->removeChild("height");
896
897 if (_object != 0) {
898 _object->reveal();
899 } else {
900 SG_LOG(SG_GENERAL, SG_ALERT, "Widget " << props->getStringValue("name", "[unnamed]") << " does not contain a proper GUI definition");
901 }
902 }
903
904 puObject*
905 FGPUIDialog::makeObject(SGPropertyNode* props, int parentWidth, int parentHeight)
906 {
907 if (!props->getBoolValue("enabled", true))
908 return 0;
909
910 bool presetSize = props->hasValue("width") && props->hasValue("height");
911 int width = props->getIntValue("width", parentWidth);
912 int height = props->getIntValue("height", parentHeight);
913 int x = props->getIntValue("x", (parentWidth - width) / 2);
914 int y = props->getIntValue("y", (parentHeight - height) / 2);
915 string type = props->getNameString();
916
917 if (type.empty())
918 type = "dialog";
919
920 if (type == "dialog") {
921 puPopup* obj;
922 bool draggable = props->getBoolValue("draggable", true);
923 bool resizable = props->getBoolValue("resizable", false);
924 if (props->getBoolValue("modal", false))
925 obj = new puDialogBox(x, y);
926 else
927 obj = new fgPopup(this, x, y, resizable, draggable);
928 setupGroup(obj, props, width, height, true);
929 setColor(obj, props);
930 return obj;
931
932 } else if (type == "group") {
933 puGroup* obj = new puGroup(x, y);
934 setupGroup(obj, props, width, height, false);
935 setColor(obj, props);
936 return obj;
937
938 } else if (type == "frame") {
939 puGroup* obj = new puGroup(x, y);
940 setupGroup(obj, props, width, height, true);
941 setColor(obj, props);
942 return obj;
943
944 } else if (type == "hrule" || type == "vrule") {
945 puFrame* obj = new puFrame(x, y, x + width, y + height);
946 obj->setBorderThickness(0);
947 setupObject(obj, props);
948 setColor(obj, props, BACKGROUND | FOREGROUND | HIGHLIGHT);
949 return obj;
950
951 } else if (type == "list") {
952 int slider_width = props->getIntValue("slider", 20);
953 fgList* obj = new fgList(x, y, x + width, y + height, props, slider_width);
954 if (presetSize)
955 obj->setSize(width, height);
956 setupObject(obj, props);
957 setColor(obj, props);
958 return obj;
959
960 } else if (type == "airport-list") {
961 AirportList* obj = new AirportList(x, y, x + width, y + height);
962 if (presetSize)
963 obj->setSize(width, height);
964 setupObject(obj, props);
965 setColor(obj, props);
966 return obj;
967
968 } else if (type == "property-list") {
969 PropertyList* obj = new PropertyList(x, y, x + width, y + height,
970 globals->get_props(),
971 props->getBoolValue("readonly", false)
972 );
973 if (presetSize)
974 obj->setSize(width, height);
975 setupObject(obj, props);
976 setColor(obj, props);
977 return obj;
978
979 } else if (type == "input") {
980 puInput* obj = new puInput(x, y, x + width, y + height);
981 setupObject(obj, props);
982 setColor(obj, props, FOREGROUND | LABEL);
983 return obj;
984
985 } else if (type == "text") {
986 puText* obj = new puText(x, y);
987 setupObject(obj, props);
988
989 // Layed-out objects need their size set, and non-layout ones
990 // get a different placement.
991 if (presetSize)
992 obj->setSize(width, height);
993 else
994 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
995 setColor(obj, props, LABEL);
996 return obj;
997
998 } else if (type == "checkbox") {
999 puButton* obj;
1000 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
1001 setupObject(obj, props);
1002 setColor(obj, props, FOREGROUND | LABEL);
1003 return obj;
1004
1005 } else if (type == "radio") {
1006 puButton* obj;
1007 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
1008 setupObject(obj, props);
1009 setColor(obj, props, FOREGROUND | LABEL);
1010 return obj;
1011
1012 } else if (type == "button") {
1013 puButton* obj;
1014 string legend = props->getStringValue("legend", "[none]");
1015 if (props->getBoolValue("one-shot", true))
1016 obj = new puOneShot(x, y, legend.c_str());
1017 else
1018 obj = new puButton(x, y, legend.c_str());
1019 if (presetSize)
1020 obj->setSize(width, height);
1021 setupObject(obj, props);
1022 setColor(obj, props);
1023 return obj;
1024 } else if (type == "map") {
1025 MapWidget* mapWidget = new MapWidget(x, y, x + width, y + height);
1026 setupObject(mapWidget, props);
1027 _activeWidgets.push_back(mapWidget);
1028 return mapWidget;
1029 } else if (type == "canvas") {
1030 CanvasWidget* canvasWidget = new CanvasWidget(x, y,
1031 x + width, y + height,
1032 props,
1033 _module);
1034 setupObject(canvasWidget, props);
1035 return canvasWidget;
1036 } else if (type == "combo") {
1037 fgComboBox* obj = new fgComboBox(x, y, x + width, y + height, props,
1038 props->getBoolValue("editable", false));
1039 setupObject(obj, props);
1040 setColor(obj, props, EDITFIELD);
1041 return obj;
1042
1043 } else if (type == "slider") {
1044 bool vertical = props->getBoolValue("vertical", false);
1045 puSlider* obj = new puSlider(x, y, (vertical ? height : width), vertical);
1046 obj->setMinValue(props->getFloatValue("min", 0.0));
1047 obj->setMaxValue(props->getFloatValue("max", 1.0));
1048 obj->setStepSize(props->getFloatValue("step"));
1049 obj->setSliderFraction(props->getFloatValue("fraction"));
1050 #if PLIB_VERSION > 185
1051 obj->setPageStepSize(props->getFloatValue("pagestep"));
1052 #endif
1053 setupObject(obj, props);
1054 if (presetSize)
1055 obj->setSize(width, height);
1056 setColor(obj, props, FOREGROUND | LABEL);
1057 return obj;
1058
1059 } else if (type == "dial") {
1060 puDial* obj = new puDial(x, y, width);
1061 obj->setMinValue(props->getFloatValue("min", 0.0));
1062 obj->setMaxValue(props->getFloatValue("max", 1.0));
1063 obj->setWrap(props->getBoolValue("wrap", true));
1064 setupObject(obj, props);
1065 setColor(obj, props, FOREGROUND | LABEL);
1066 return obj;
1067
1068 } else if (type == "textbox") {
1069 int slider_width = props->getIntValue("slider", 20);
1070 int wrap = props->getBoolValue("wrap", true);
1071 #if PLIB_VERSION > 185
1072 puaLargeInput* obj = new puaLargeInput(x, y,
1073 x + width, x + height, 11, slider_width, wrap);
1074 #else
1075 puaLargeInput* obj = new puaLargeInput(x, y,
1076 x + width, x + height, 2, slider_width, wrap);
1077 #endif
1078
1079 if (props->getBoolValue("editable"))
1080 obj->enableInput();
1081 else
1082 obj->disableInput();
1083
1084 if (presetSize)
1085 obj->setSize(width, height);
1086 setupObject(obj, props);
1087 setColor(obj, props, FOREGROUND | LABEL);
1088
1089 int top = props->getIntValue("top-line", 0);
1090 obj->setTopLineInWindow(top < 0 ? unsigned(-1) >> 1 : top);
1091 return obj;
1092
1093 } else if (type == "select") {
1094 fgSelectBox* obj = new fgSelectBox(x, y, x + width, y + height, props);
1095 setupObject(obj, props);
1096 setColor(obj, props, EDITFIELD);
1097 return obj;
1098 } else if (type == "waypointlist") {
1099 ScrolledWaypointList* obj = new ScrolledWaypointList(x, y, width, height);
1100 setupObject(obj, props);
1101 return obj;
1102
1103 } else if (type == "loglist") {
1104 LogList* obj = new LogList(x, y, width, height, 20);
1105 string logClass = props->getStringValue("logclass");
1106 if (logClass == "terrasync") {
1107 auto tsync = globals->get_subsystem<simgear::SGTerraSync>();
1108 if (tsync) {
1109 obj->setBuffer(tsync->log());
1110 }
1111 } else {
1112 auto nasal = globals->get_subsystem<FGNasalSys>();
1113 obj->setBuffer(nasal->log());
1114 }
1115
1116 setupObject(obj, props);
1117 _activeWidgets.push_back(obj);
1118 setColor(obj, props, FOREGROUND | LABEL);
1119 return obj;
1120 } else {
1121 return 0;
1122 }
1123 }
1124
1125 void FGPUIDialog::setupObject(puObject* object, SGPropertyNode* props)
1126 {
1127 GUIInfo* info = new GUIInfo(this);
1128 object->setUserData(info);
1129 _info.push_back(info);
1130 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
1131 object->makeReturnDefault(props->getBoolValue("default"));
1132 info->node = props;
1133 if (props->hasValue("legend")) {
1134 info->legend = props->getStringValue("legend");
1135 object->setLegend(info->legend.c_str());
1136 }
1137
1138 if (props->hasValue("label")) {
1139 info->label = props->getStringValue("label");
1140 object->setLabel(info->label.c_str());
1141 }
1142
1143 if (props->hasValue("border"))
1144 object->setBorderThickness(props->getIntValue("border", 2));
1145
1146 if (SGPropertyNode* nft = props->getNode("font", false)) {
1147 if (nft) {
1148 SGPropertyNode* property_node = nft->getChild("property");
1149 if (property_node)
1150 nft = globals->get_props()->getNode(property_node->getStringValue());
1151 _font = FGFontCache::instance()->get(nft);
1152 } else {
1153 _font = _gui->getDefaultFont();
1154 }
1155 puFont* lfnt = FGFontCache::instance()->get(nft);
1156 object->setLabelFont(*lfnt);
1157 object->setLegendFont(*lfnt);
1158 } else {
1159 object->setLabelFont(*_font);
1160 }
1161
1162 if (props->hasChild("visible")) {
1163 ConditionalObject* cnd = new ConditionalObject("visible", object);
1164 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("visible")));
1165 _conditionalObjects.push_back(cnd);
1166 }
1167
1168 if (props->hasChild("enable")) {
1169 ConditionalObject* cnd = new ConditionalObject("enable", object);
1170 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("enable")));
1171 _conditionalObjects.push_back(cnd);
1172 }
1173
1174 string type = props->getNameString();
1175 if (type == "input" && props->getBoolValue("live"))
1176 object->setDownCallback(action_callback);
1177
1178 if (type == "text") {
1179 string format = props->getStringValue("format", "");
1180 if (!format.empty()) {
1181 info->fmt_type = validate_format(format.c_str());
1182 if (info->fmt_type != f_INVALID)
1183 info->format = format;
1184 else
1185 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '" << format << '\'');
1186 }
1187 }
1188
1189 if (props->hasValue("property")) {
1190 string name = props->getStringValue("name", "");
1191 string propname = props->getStringValue("property");
1192 SGPropertyNode_ptr node = fgGetNode(propname, true);
1193 if (type == "map") {
1194 // mapWidget binds to a sub-tree of properties, and
1195 // ignores the puValue mechanism, so special case things here
1196 MapWidget* mw = static_cast<MapWidget*>(object);
1197 mw->setProperty(node);
1198 } else {
1199 // normal widget, creating PropertyObject
1200 copy_to_pui(node, object);
1201 PropertyObject* po = new PropertyObject(name, object, node);
1202 _propertyObjects.push_back(po);
1203 if (props->getBoolValue("live"))
1204 _liveObjects.push_back(po);
1205 }
1206 }
1207
1208 const auto bindings = props->getChildren("binding");
1209 if (!bindings.empty()) {
1210 info->key = props->getIntValue("keynum", -1);
1211 if (props->hasValue("key"))
1212 info->key = getKeyCode(props->getStringValue("key", "").c_str());
1213
1214
1215 for (auto bindingNode : bindings) {
1216 string cmd = bindingNode->getStringValue("command");
1217 if (cmd == "nasal") {
1218 // we need to clone the binding node, so we can unique the
1219 // Nasal module. Otherwise we always modify the global dialog
1220 // definition, and cloned dialogs use the same Nasal module for
1221 // <nasal> bindings, which goes wrong. (Especially, the property
1222 // inspector)
1223
1224 // memory ownership works because SGBinding has a ref to its
1225 // argument node and holds onto it.
1226 SGPropertyNode_ptr copiedBinding = new SGPropertyNode;
1227 copyProperties(bindingNode, copiedBinding);
1228 copiedBinding->setStringValue("module", _module.c_str());
1229
1230 bindingNode = copiedBinding;
1231 }
1232
1233 info->bindings.push_back(new SGBinding(bindingNode, globals->get_props()));
1234 }
1235 object->setCallback(action_callback);
1236 }
1237 }
1238
1239 void FGPUIDialog::setupGroup(puGroup* group, SGPropertyNode* props,
1240 int width, int height, bool makeFrame)
1241 {
1242 setupObject(group, props);
1243
1244 if (makeFrame) {
1245 puFrame* f = new puFrame(0, 0, width, height);
1246 setColor(f, props);
1247 }
1248
1249 int nChildren = props->nChildren();
1250 for (int i = 0; i < nChildren; i++)
1251 makeObject(props->getChild(i), width, height);
1252
1253 group->close();
1254 }
1255
1256 void FGPUIDialog::setColor(puObject* object, SGPropertyNode* props, int which)
1257 {
1258 string type = props->getNameString();
1259 if (type.empty())
1260 type = "dialog";
1261 if (type == "textbox" && props->getBoolValue("editable"))
1262 type += "-editable";
1263
1264 FGColor c(_gui->getColor("background"));
1265 c.merge(_gui->getColor(type));
1266 c.merge(props->getNode("color"));
1267 if (c.isValid())
1268 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
1269
1270 const struct {
1271 int mask;
1272 int id;
1273 const char* name;
1274 const char* cname;
1275 } pucol[] = {
1276 {BACKGROUND, PUCOL_BACKGROUND, "background", "color-background"},
1277 {FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground"},
1278 {HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight"},
1279 {LABEL, PUCOL_LABEL, "label", "color-label"},
1280 {LEGEND, PUCOL_LEGEND, "legend", "color-legend"},
1281 {MISC, PUCOL_MISC, "misc", "color-misc"},
1282 {EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield"},
1283 };
1284
1285 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
1286
1287 for (int i = 0; i < numcol; i++) {
1288 bool dirty = false;
1289 c.clear();
1290 c.setAlpha(1.0);
1291
1292 dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
1293 if (which & pucol[i].mask)
1294 dirty |= c.merge(props->getNode("color"));
1295
1296 if ((pucol[i].mask == LABEL) && !c.isValid())
1297 dirty |= c.merge(_gui->getColor("label"));
1298
1299 dirty |= c.merge(props->getNode(pucol[i].cname));
1300
1301 if (c.isValid() && dirty)
1302 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
1303 }
1304 }
1305
1306
1307 static struct {
1308 const char* name;
1309 int key;
1310 } keymap[] = {
1311 {"backspace", 8},
1312 {"tab", 9},
1313 {"return", 13},
1314 {"enter", 13},
1315 {"esc", 27},
1316 {"escape", 27},
1317 {"space", ' '},
1318 {"&amp;", '&'},
1319 {"and", '&'},
1320 {"&lt;", '<'},
1321 {"&gt;", '>'},
1322 {"f1", PU_KEY_F1},
1323 {"f2", PU_KEY_F2},
1324 {"f3", PU_KEY_F3},
1325 {"f4", PU_KEY_F4},
1326 {"f5", PU_KEY_F5},
1327 {"f6", PU_KEY_F6},
1328 {"f7", PU_KEY_F7},
1329 {"f8", PU_KEY_F8},
1330 {"f9", PU_KEY_F9},
1331 {"f10", PU_KEY_F10},
1332 {"f11", PU_KEY_F11},
1333 {"f12", PU_KEY_F12},
1334 {"left", PU_KEY_LEFT},
1335 {"up", PU_KEY_UP},
1336 {"right", PU_KEY_RIGHT},
1337 {"down", PU_KEY_DOWN},
1338 {"pageup", PU_KEY_PAGE_UP},
1339 {"pagedn", PU_KEY_PAGE_DOWN},
1340 {"home", PU_KEY_HOME},
1341 {"end", PU_KEY_END},
1342 {"insert", PU_KEY_INSERT},
1343 {0, -1},
1344 };
1345
1346 int FGPUIDialog::getKeyCode(const char* str)
1347 {
1348 enum {
1349 CTRL = 0x1,
1350 SHIFT = 0x2,
1351 ALT = 0x4,
1352 };
1353
1354 while (*str == ' ')
1355 str++;
1356
1357 char* buf = new char[strlen(str) + 1];
1358 strcpy(buf, str);
1359 char* s = buf + strlen(buf);
1360 while (s > str && s[-1] == ' ')
1361 s--;
1362 *s = 0;
1363 s = buf;
1364
1365 int mod = 0;
1366 while (1) {
1367 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
1368 s += 5, mod |= CTRL;
1369 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
1370 s += 6, mod |= SHIFT;
1371 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
1372 s += 4, mod |= ALT;
1373 else
1374 break;
1375 }
1376
1377 int key = -1;
1378 if (strlen(s) == 1 && isascii(*s)) {
1379 key = *s;
1380 if (mod & SHIFT)
1381 key = toupper(key);
1382 if (mod & CTRL)
1383 key = toupper(key) - '@';
1384 /* if (mod & ALT)
1385 ; // Alt not propagated to the gui
1386 */
1387 } else {
1388 for (char* t = s; *t; t++)
1389 *t = tolower(*t);
1390 for (int i = 0; keymap[i].name; i++) {
1391 if (!strcmp(s, keymap[i].name)) {
1392 key = keymap[i].key;
1393 break;
1394 }
1395 }
1396 }
1397 delete[] buf;
1398 return key;
1399 }
1400
1401 void FGPUIDialog::relayout()
1402 {
1403 _needsRelayout = false;
1404
1405 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
1406 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
1407
1408 bool userx = _props->hasValue("x");
1409 bool usery = _props->hasValue("y");
1410 bool userw = _props->hasValue("width");
1411 bool userh = _props->hasValue("height");
1412
1413 // Let the layout widget work in the same property subtree.
1414 LayoutWidget wid(_props);
1415 wid.setDefaultFont(_font, int(_font->getPointSize()));
1416
1417 int pw = 0, ph = 0;
1418 int px, py, savex, savey;
1419 if (!userw || !userh) {
1420 wid.calcPrefSize(&pw, &ph);
1421 }
1422
1423 pw = _props->getIntValue("width", pw);
1424 ph = _props->getIntValue("height", ph);
1425 px = savex = _props->getIntValue("x", (screenw - pw) / 2);
1426 py = savey = _props->getIntValue("y", (screenh - ph) / 2);
1427
1428 // Negative x/y coordinates are interpreted as distance from the top/right
1429 // corner rather than bottom/left.
1430 if (userx && px < 0)
1431 px = screenw - pw + px;
1432 if (usery && py < 0)
1433 py = screenh - ph + py;
1434
1435 // Define "x", "y", "width" and/or "height" in the property tree if they
1436 // are not specified in the configuration file.
1437 wid.layout(px, py, pw, ph);
1438
1439 applySize(_object);
1440
1441 // Remove automatically generated properties, so the layout looks
1442 // the same next time around, or restore x and y to preserve negative coords.
1443 if (userx)
1444 _props->setIntValue("x", savex);
1445 else
1446 _props->removeChild("x");
1447
1448 if (usery)
1449 _props->setIntValue("y", savey);
1450 else
1451 _props->removeChild("y");
1452
1453 if (!userw) _props->removeChild("width");
1454 if (!userh) _props->removeChild("height");
1455 }
1456
1457 void FGPUIDialog::applySize(puObject* object)
1458 {
1459 // compound plib widgets use setUserData() for internal purposes, so refuse
1460 // to descend into anything that has other bits set than the following
1461 const int validUserData = PUCLASS_VALUE | PUCLASS_OBJECT | PUCLASS_GROUP | PUCLASS_INTERFACE | PUCLASS_FRAME | PUCLASS_TEXT | PUCLASS_BUTTON | PUCLASS_ONESHOT | PUCLASS_INPUT | PUCLASS_ARROW | PUCLASS_DIAL | PUCLASS_POPUP;
1462
1463 int type = object->getType();
1464 if ((type & PUCLASS_GROUP) && !(type & ~validUserData)) {
1465 puObject* c = ((puGroup*)object)->getFirstChild();
1466 for (; c != NULL; c = c->getNextObject()) {
1467 applySize(c);
1468 } // of child iteration
1469 } // of group object case
1470
1471 GUIInfo* info = (GUIInfo*)object->getUserData();
1472 if (!info)
1473 return;
1474
1475 SGPropertyNode* n = info->node;
1476 if (!n) {
1477 SG_LOG(SG_GENERAL, SG_ALERT, "FGDialog::applySize: no props");
1478 return;
1479 }
1480
1481 int x = n->getIntValue("x");
1482 int y = n->getIntValue("y");
1483 int w = n->getIntValue("width", 4);
1484 int h = n->getIntValue("height", 4);
1485 object->setPosition(x, y);
1486 object->setSize(w, h);
1487 }
1488
1489 void FGPUIDialog::close()
1490 {
1491 // no-op for PUI version
1492 }
1493
1494 ////////////////////////////////////////////////////////////////////////
1495 // Implementation of FGDialog::PropertyObject.
1496 ////////////////////////////////////////////////////////////////////////
1497
1498 FGPUIDialog::PropertyObject::PropertyObject(string n,
1499 puObject* o, SGPropertyNode_ptr p) : name(n),
1500 object(o),
1501 node(p)
1502 {
1503 }
1504
1505
1506 ////////////////////////////////////////////////////////////////////////
1507 // Implementation of fgValueList and derived pui widgets
1508 ////////////////////////////////////////////////////////////////////////
1509
1510
1511 fgValueList::fgValueList(SGPropertyNode* p) : _props(p)
1512 {
1513 make_list();
1514 }
1515
1516 void fgValueList::update()
1517 {
1518 destroy_list();
1519 make_list();
1520 }
1521
1522 fgValueList::~fgValueList()
1523 {
1524 destroy_list();
1525 }
1526
1527 void fgValueList::make_list()
1528 {
1529 SGPropertyNode_ptr values = _props;
1530 string vname = "value";
1531
1532 if (_props->hasChild("properties")) {
1533 // dynamic values, read from a property's children
1534 string path = _props->getStringValue("properties");
1535 values = fgGetNode(path, true);
1536 }
1537
1538 if (_props->hasChild("property-name")) {
1539 vname = _props->getStringValue("property-name");
1540 }
1541
1542 std::vector<SGPropertyNode_ptr> value_nodes = values->getChildren(vname);
1543 _list = new char*[value_nodes.size() + 1];
1544 unsigned int i;
1545 for (i = 0; i < value_nodes.size(); i++) {
1546 _list[i] = strdup((char*)value_nodes[i]->getStringValue().c_str());
1547 }
1548 _list[i] = 0;
1549 }
1550
1551 void fgValueList::destroy_list()
1552 {
1553 for (int i = 0; _list[i] != 0; i++)
1554 if (_list[i])
1555 free(_list[i]);
1556 delete[] _list;
1557 }
1558
1559
1560 void fgList::update()
1561 {
1562 fgValueList::update();
1563 int top = getTopItem();
1564 newList(_list);
1565 setTopItem(top);
1566 }
1567
1568 ////////////////////////////////////////////////////////////////////////
1569 // Implementation of fgLogList
1570 ////////////////////////////////////////////////////////////////////////
1571
1572 void LogList::update()
1573 {
1574 if (!m_buffer) return;
1575
1576 if (m_stamp != m_buffer->stamp()) {
1577 m_stamp = m_buffer->threadsafeCopy(m_items);
1578 m_items.push_back(NULL); // terminator value
1579 newList((char**)m_items.data());
1580 setTopItem(m_items.size() - 1); // scroll to bottom of list
1581 }
1582 }
1583
1584 void LogList::setBuffer(simgear::BufferedLogCallback* buf)
1585 {
1586 m_buffer = buf;
1587 m_stamp = m_buffer->stamp() - 1; // force an update
1588 update();
1589 }
1590
1591 ////////////////////////////////////////////////////////////////////////
1592 // Implementation of fgComboBox
1593 ////////////////////////////////////////////////////////////////////////
1594
1595 void fgComboBox::update(const std::string& val)
1596 {
1597 if (_inHit) {
1598 return;
1599 }
1600
1601 // even if the values match, we might still need to update
1602 // the current item. So no early return here.
1603 if (val != getStringValue()) {
1604 setValue(val.c_str());
1605 }
1606
1607 fgValueList::update();
1608 newList(_list);
1609 int currentItem = puaComboBox::getCurrentItem();
1610
1611 // look for the previous value, in the new list
1612 for (int i = 0; _list[i] != 0; i++) {
1613 if (_list[i] == val) {
1614 // don't set the current item again; this is important to avoid
1615 // recursion here if the combo callback ultimately causes another dialog-update
1616 if (i != currentItem) {
1617 puaComboBox::setCurrentItem(i);
1618 }
1619
1620 return;
1621 }
1622 } // of list values iteration
1623
1624 if (!val.empty()) {
1625 SG_LOG(SG_GUI, SG_WARN, "Couldn't find item in list:" << val);
1626 }
1627
1628 // cound't find current item, default to first
1629 if (currentItem != 0) {
1630 puaComboBox::setCurrentItem(0);
1631 }
1632 }
1633
1634 int fgComboBox::checkHit(int b, int up, int x, int y)
1635 {
1636 _inHit = true;
1637 int r = puaComboBox::checkHit(b, up, x, y);
1638 _inHit = false;
1639 return r;
1640 }
1641
1642 void fgComboBox::setSize(int w, int h)
1643 {
1644 puaComboBox::setSize(w, h);
1645 recalc_bbox();
1646 }
1647
1648 void fgComboBox::recalc_bbox()
1649 {
1650 // bug-fix for issue #192
1651 // http://code.google.com/p/flightgear-bugs/issues/detail?id=192
1652 // puaComboBox is including the height of its popupMenu in the height
1653 // computation, which breaks layout computations.
1654 // this implementation skips popup-menus
1655
1656 puBox contents;
1657 contents.empty();
1658
1659 for (puObject* bo = dlist; bo != NULL; bo = bo->getNextObject()) {
1660 if (bo->getType() & PUCLASS_POPUPMENU) {
1661 continue;
1662 }
1663
1664 contents.extend(bo->getBBox());
1665 }
1666
1667 abox.max[0] = abox.min[0] + contents.max[0];
1668 abox.max[1] = abox.min[1] + contents.max[1];
1669
1670 puObject::recalc_bbox();
1671 }
1672
1673 // end of FGPUIDialog.cxx