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