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