1 // dialog.cxx: implementation of an XML-configurable dialog box.
7 #include <simgear/structure/SGBinding.hxx>
8 #include <simgear/props/props_io.hxx>
10 #include <Scripting/NasalSys.hxx>
11 #include <Main/fg_os.hxx>
12 #include <Main/globals.hxx>
13 #include <Main/fg_props.hxx>
15 #include "FGPUIDialog.hxx"
16 #include "new_gui.hxx"
17 #include "AirportList.hxx"
18 #include "property_list.hxx"
20 #include "WaypointList.hxx"
21 #include "CanvasWidget.hxx"
22 #include "MapWidget.hxx"
23 #include "FGFontCache.hxx"
24 #include "FGColor.hxx"
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;
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 (%%).
37 validate_format(const char *f)
51 while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0')
53 while (*f && isdigit(*f))
57 while (*f && isdigit(*f))
65 type = l ? f_LONG : f_INT;
67 type = l ? f_DOUBLE : f_FLOAT;
87 ////////////////////////////////////////////////////////////////////////
88 // Implementation of GUIInfo.
89 ////////////////////////////////////////////////////////////////////////
92 * User data for a GUI object.
96 GUIInfo(FGPUIDialog *d);
98 void apply_format(SGPropertyNode *);
101 SGPropertyNode_ptr node;
102 vector <SGBinding *> bindings;
104 string label, legend, text, format;
105 format_type fmt_type;
108 GUIInfo::GUIInfo (FGPUIDialog *d) :
117 for (unsigned int i = 0; i < bindings.size(); i++) {
123 void GUIInfo::apply_format(SGPropertyNode *n)
125 char buf[FORMAT_BUFSIZE + 1];
126 if (fmt_type == f_INT)
127 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getIntValue());
128 else if (fmt_type == f_LONG)
129 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getLongValue());
130 else if (fmt_type == f_FLOAT)
131 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getFloatValue());
132 else if (fmt_type == f_DOUBLE)
133 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getDoubleValue());
135 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getStringValue());
137 buf[FORMAT_BUFSIZE] = '\0';
145 int fgPopup::checkKey(int key, int updown)
147 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
150 puObject *input = getActiveInputField(this);
152 return input->checkKey(key, updown);
154 puObject *object = getKeyObject(this, key);
156 return puPopup::checkKey(key, updown);
158 // invokeCallback() isn't enough; we need to simulate a mouse button press
159 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
160 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
161 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
162 object->checkHit(PU_LEFT_BUTTON, PU_UP,
163 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
164 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
168 puObject *fgPopup::getKeyObject(puObject *object, int key)
171 if (object->getType() & PUCLASS_GROUP)
172 for (puObject *obj = ((puGroup *)object)->getFirstChild();
173 obj; obj = obj->getNextObject())
174 if ((ret = getKeyObject(obj, key)))
177 GUIInfo *info = (GUIInfo *)object->getUserData();
178 if (info && info->key == key)
184 puObject *fgPopup::getActiveInputField(puObject *object)
187 if (object->getType() & PUCLASS_GROUP)
188 for (puObject *obj = ((puGroup *)object)->getFirstChild();
189 obj; obj = obj->getNextObject())
190 if ((ret = getActiveInputField(obj)))
193 if (object->getType() & (PUCLASS_INPUT|PUCLASS_LARGEINPUT)
194 && ((puInput *)object)->isAcceptingInput())
203 int fgPopup::checkHit(int button, int updown, int x, int y)
206 if (updown != PU_DRAG && !_dragging)
207 result = puPopup::checkHit(button, updown, x, y);
212 // This is annoying. We would really want a true result from the
213 // superclass to indicate "handled by child object", but all it
214 // tells us is that the pointer is inside the dialog. So do the
215 // intersection test (again) to make sure we don't start a drag
216 // when inside controls.
218 if (updown == PU_DOWN && !_dragging) {
221 int global_drag = fgGetKeyModifiers() & KEYMOD_SHIFT;
222 int global_resize = fgGetKeyModifiers() & KEYMOD_CTRL;
224 int hit = getHitObjects(this, x, y);
225 if (hit & PUCLASS_LIST) // ctrl-click in property browser (toggle bool)
228 && ( (hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
229 |PUCLASS_LARGEINPUT|PUCLASS_SCROLLBAR))
230 // The canvas should handle drag events on its own so exit
231 // here if mouse is over a CanvasWidget
232 || (!global_drag && checkHitCanvas(this, x, y))
238 getPosition(&_dlgX, &_dlgY);
239 getSize(&_dlgW, &_dlgH);
240 _start_cursor = fgGetMouseCursor();
245 // check and prepare for resizing
246 static const int cursor[] = {
247 MOUSE_CURSOR_POINTER, MOUSE_CURSOR_LEFTSIDE, MOUSE_CURSOR_RIGHTSIDE, 0,
248 MOUSE_CURSOR_TOPSIDE, MOUSE_CURSOR_TOPLEFT, MOUSE_CURSOR_TOPRIGHT, 0,
249 MOUSE_CURSOR_BOTTOMSIDE, MOUSE_CURSOR_BOTTOMLEFT, MOUSE_CURSOR_BOTTOMRIGHT, 0,
253 if (!global_drag && _resizable) {
254 int hmargin = global_resize ? _dlgW / 3 : RESIZE_MARGIN;
255 int vmargin = global_resize ? _dlgH / 3 : RESIZE_MARGIN;
257 if (y - _dlgY < vmargin)
259 else if (_dlgY + _dlgH - y < vmargin)
262 if (x - _dlgX < hmargin)
264 else if (_dlgX + _dlgW - x < hmargin)
267 if (!_resizing && global_resize)
268 _resizing = BOTTOM|RIGHT;
270 _cursor = cursor[_resizing];
271 if (_resizing && _resizable)
272 fgSetMouseCursor(_cursor);
275 } else if (updown == PU_DRAG && _dragging) {
277 GUIInfo *info = (GUIInfo *)getUserData();
278 if (_resizable && info && info->node) {
281 if (_resizing & LEFT)
283 if (_resizing & RIGHT)
287 if (_resizing & BOTTOM)
291 LayoutWidget wid(info->node);
292 wid.calcPrefSize(&prefw, &prefh);
300 if (_resizing & LEFT)
302 if (_resizing & BOTTOM)
305 wid.layout(x, y, w, h);
308 applySize(static_cast<puObject *>(this));
309 getFirstChild()->setSize(w, h); // dialog background puFrame
312 int posX = x + _dlgX - _startX,
313 posY = y + _dlgY - _startY;
314 setPosition(posX, posY);
316 GUIInfo *info = (GUIInfo *)getUserData();
317 if (info && info->node) {
318 info->node->setIntValue("x", posX);
319 info->node->setIntValue("y", posY);
323 } else if (_dragging) {
324 fgSetMouseCursor(_start_cursor);
330 int fgPopup::getHitObjects(puObject *object, int x, int y)
332 if (!object->isVisible())
336 if (object->getType() & PUCLASS_GROUP)
337 for (puObject *obj = ((puGroup *)object)->getFirstChild();
338 obj; obj = obj->getNextObject())
339 type |= getHitObjects(obj, x, y);
342 object->getAbsolutePosition(&cx, &cy);
343 object->getSize(&cw, &ch);
344 if (x >= cx && x < cx + cw && y >= cy && y < cy + ch)
345 type |= object->getType();
349 bool fgPopup::checkHitCanvas(puObject* object, int x, int y)
351 if( !object->isVisible() )
354 if( object->getType() & PUCLASS_GROUP )
356 for( puObject* obj = ((puGroup*)object)->getFirstChild();
358 obj = obj->getNextObject() )
360 if( checkHitCanvas(obj, x, y) )
366 object->getAbsolutePosition(&cx, &cy);
367 object->getSize(&cw, &ch);
368 if( x >= cx && x < cx + cw
369 && y >= cy && y < cy + ch
370 && dynamic_cast<CanvasWidget*>(object) )
375 void fgPopup::applySize(puObject *object)
377 // compound plib widgets use setUserData() for internal purposes, so refuse
378 // to descend into anything that has other bits set than the following
379 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
380 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
381 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
383 int type = object->getType();
384 if (type & PUCLASS_GROUP && !(type & ~validUserData))
385 for (puObject *obj = ((puGroup *)object)->getFirstChild();
386 obj; obj = obj->getNextObject())
389 GUIInfo *info = (GUIInfo *)object->getUserData();
393 SGPropertyNode *n = info->node;
395 SG_LOG(SG_GENERAL, SG_ALERT, "fgPopup::applySize: no props");
398 int x = n->getIntValue("x");
399 int y = n->getIntValue("y");
400 int w = n->getIntValue("width", 4);
401 int h = n->getIntValue("height", 4);
402 object->setPosition(x, y);
403 object->setSize(w, h);
406 ////////////////////////////////////////////////////////////////////////
408 void FGPUIDialog::ConditionalObject::update(FGPUIDialog* aDlg)
410 if (_name == "visible") {
411 bool wasVis = _pu->isVisible() != 0;
412 bool newVis = test();
414 if (newVis == wasVis) {
418 if (newVis) { // puObject needs a setVisible. Oh well.
423 } else if (_name == "enable") {
424 bool wasEnabled = _pu->isActive() != 0;
425 bool newEnable = test();
427 if (wasEnabled == newEnable) {
438 aDlg->setNeedsLayout();
441 ////////////////////////////////////////////////////////////////////////
443 ////////////////////////////////////////////////////////////////////////
449 action_callback (puObject *object)
451 GUIInfo *info = (GUIInfo *)object->getUserData();
452 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
453 gui->setActiveDialog(info->dialog);
454 int nBindings = info->bindings.size();
455 for (int i = 0; i < nBindings; i++) {
456 info->bindings[i]->fire();
457 if (gui->getActiveDialog() == 0)
460 gui->setActiveDialog(0);
464 ////////////////////////////////////////////////////////////////////////
465 // Static helper functions.
466 ////////////////////////////////////////////////////////////////////////
469 * Copy a property value to a PUI object.
472 copy_to_pui (SGPropertyNode *node, puObject *object)
474 using namespace simgear;
475 GUIInfo *info = (GUIInfo *)object->getUserData();
477 SG_LOG(SG_GENERAL, SG_ALERT, "dialog: widget without GUIInfo!");
478 return; // this can't really happen
481 // Treat puText objects specially, so their "values" can be set
483 if (object->getType() & PUCLASS_TEXT) {
484 if (info->fmt_type != f_INVALID)
485 info->apply_format(node);
487 info->text = node->getStringValue();
489 object->setLabel(info->text.c_str());
493 switch (node->getType()) {
497 object->setValue(node->getIntValue());
501 object->setValue(node->getFloatValue());
504 info->text = node->getStringValue();
505 object->setValue(info->text.c_str());
512 copy_from_pui (puObject *object, SGPropertyNode *node)
514 using namespace simgear;
515 // puText objects are immutable, so should not be copied out
516 if (object->getType() & PUCLASS_TEXT)
519 switch (node->getType()) {
523 node->setIntValue(object->getIntegerValue());
526 node->setFloatValue(object->getFloatValue());
530 // puObject only provides float, not double, which causes precision/rounding issues
531 // with some numerical values (try "114.2").
532 // Work around: obtain string value, and manually convert with proper double precision.
533 const char *s = object->getStringValue();
534 node->setDoubleValue(atof(s));
538 const char *s = object->getStringValue();
540 node->setStringValue(s);
546 ////////////////////////////////////////////////////////////////////////
547 // Implementation of FGDialog.
548 ////////////////////////////////////////////////////////////////////////
550 FGPUIDialog::FGPUIDialog (SGPropertyNode *props) :
553 _gui((NewGUI *)globals->get_subsystem("gui")),
555 _needsRelayout(false)
557 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
559 SGPropertyNode *nasal = props->getNode("nasal");
561 _nasal_close = nasal->getNode("close");
562 SGPropertyNode *open = nasal->getNode("open");
564 const char *s = open->getStringValue();
565 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
567 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
573 FGPUIDialog::~FGPUIDialog ()
576 _object->getAbsolutePosition(&x, &y);
577 _props->setIntValue("lastx", x);
578 _props->setIntValue("lasty", y);
580 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
583 const char *s = _nasal_close->getStringValue();
584 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
586 nas->deleteModule(_module.c_str());
589 puDeleteObject(_object);
592 // Delete all the info objects we
593 // were forced to keep around because
594 // PUI cannot delete its own user data.
595 for (i = 0; i < _info.size(); i++) {
596 delete (GUIInfo *)_info[i];
599 // Finally, delete the property links.
600 for (i = 0; i < _propertyObjects.size(); i++) {
601 delete _propertyObjects[i];
602 _propertyObjects[i] = 0;
607 FGPUIDialog::updateValues (const char *objectName)
609 if (objectName && !objectName[0])
612 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
613 const string &name = _propertyObjects[i]->name;
614 if (objectName && name != objectName) {
618 puObject *widget = _propertyObjects[i]->object;
619 int widgetType = widget->getType();
620 if (widgetType & PUCLASS_LIST) {
621 GUI_ID* gui_id = dynamic_cast<GUI_ID *>(widget);
622 if (gui_id && (gui_id->id & FGCLASS_LIST)) {
623 fgList *pl = static_cast<fgList*>(widget);
626 copy_to_pui(_propertyObjects[i]->node, widget);
628 } else if (widgetType & PUCLASS_COMBOBOX) {
629 fgComboBox* combo = static_cast<fgComboBox*>(widget);
632 copy_to_pui(_propertyObjects[i]->node, widget);
634 } // of property objects iteration
638 FGPUIDialog::applyValues (const char *objectName)
640 if (objectName && !objectName[0])
643 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
644 const string &name = _propertyObjects[i]->name;
645 if (objectName && name != objectName)
648 copy_from_pui(_propertyObjects[i]->object,
649 _propertyObjects[i]->node);
654 FGPUIDialog::update ()
656 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
657 puObject *obj = _liveObjects[i]->object;
658 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
661 copy_to_pui(_liveObjects[i]->node, obj);
664 for (unsigned int j=0; j < _conditionalObjects.size(); ++j) {
665 _conditionalObjects[j]->update(this);
668 if (_needsRelayout) {
674 FGPUIDialog::display (SGPropertyNode *props)
677 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
681 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
682 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
684 bool userx = props->hasValue("x");
685 bool usery = props->hasValue("y");
686 bool userw = props->hasValue("width");
687 bool userh = props->hasValue("height");
689 // Let the layout widget work in the same property subtree.
690 LayoutWidget wid(props);
692 SGPropertyNode *fontnode = props->getNode("font");
694 FGFontCache *fc = globals->get_fontcache();
695 _font = fc->get(fontnode);
697 _font = _gui->getDefaultFont();
699 wid.setDefaultFont(_font, int(_font->getPointSize()));
702 int px, py, savex, savey;
703 if (!userw || !userh)
704 wid.calcPrefSize(&pw, &ph);
705 pw = props->getIntValue("width", pw);
706 ph = props->getIntValue("height", ph);
707 px = savex = props->getIntValue("x", (screenw - pw) / 2);
708 py = savey = props->getIntValue("y", (screenh - ph) / 2);
710 // Negative x/y coordinates are interpreted as distance from the top/right
711 // corner rather than bottom/left.
713 px = screenw - pw + px;
715 py = screenh - ph + py;
717 // Define "x", "y", "width" and/or "height" in the property tree if they
718 // are not specified in the configuration file.
719 wid.layout(px, py, pw, ph);
721 // Use the dimension and location properties as specified in the
722 // configuration file or from the layout widget.
723 _object = makeObject(props, screenw, screenh);
725 // Remove automatically generated properties, so the layout looks
726 // the same next time around, or restore x and y to preserve negative coords.
728 props->setIntValue("x", savex);
730 props->removeChild("x");
733 props->setIntValue("y", savey);
735 props->removeChild("y");
737 if (!userw) props->removeChild("width");
738 if (!userh) props->removeChild("height");
743 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
744 << props->getStringValue("name", "[unnamed]")
745 << " does not contain a proper GUI definition");
750 FGPUIDialog::makeObject (SGPropertyNode *props, int parentWidth, int parentHeight)
752 if (!props->getBoolValue("enabled", true))
755 bool presetSize = props->hasValue("width") && props->hasValue("height");
756 int width = props->getIntValue("width", parentWidth);
757 int height = props->getIntValue("height", parentHeight);
758 int x = props->getIntValue("x", (parentWidth - width) / 2);
759 int y = props->getIntValue("y", (parentHeight - height) / 2);
760 string type = props->getName();
765 if (type == "dialog") {
767 bool draggable = props->getBoolValue("draggable", true);
768 bool resizable = props->getBoolValue("resizable", false);
769 if (props->getBoolValue("modal", false))
770 obj = new puDialogBox(x, y);
772 obj = new fgPopup(x, y, resizable, draggable);
773 setupGroup(obj, props, width, height, true);
774 setColor(obj, props);
777 } else if (type == "group") {
778 puGroup *obj = new puGroup(x, y);
779 setupGroup(obj, props, width, height, false);
780 setColor(obj, props);
783 } else if (type == "frame") {
784 puGroup *obj = new puGroup(x, y);
785 setupGroup(obj, props, width, height, true);
786 setColor(obj, props);
789 } else if (type == "hrule" || type == "vrule") {
790 puFrame *obj = new puFrame(x, y, x + width, y + height);
791 obj->setBorderThickness(0);
792 setupObject(obj, props);
793 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
796 } else if (type == "list") {
797 int slider_width = props->getIntValue("slider", 20);
798 fgList *obj = new fgList(x, y, x + width, y + height, props, slider_width);
800 obj->setSize(width, height);
801 setupObject(obj, props);
802 setColor(obj, props);
805 } else if (type == "airport-list") {
806 AirportList *obj = new AirportList(x, y, x + width, y + height);
808 obj->setSize(width, height);
809 setupObject(obj, props);
810 setColor(obj, props);
813 } else if (type == "property-list") {
814 PropertyList *obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
816 obj->setSize(width, height);
817 setupObject(obj, props);
818 setColor(obj, props);
821 } else if (type == "input") {
822 puInput *obj = new puInput(x, y, x + width, y + height);
823 setupObject(obj, props);
824 setColor(obj, props, FOREGROUND|LABEL);
827 } else if (type == "text") {
828 puText *obj = new puText(x, y);
829 setupObject(obj, props);
831 // Layed-out objects need their size set, and non-layout ones
832 // get a different placement.
834 obj->setSize(width, height);
836 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
837 setColor(obj, props, LABEL);
840 } else if (type == "checkbox") {
842 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
843 setupObject(obj, props);
844 setColor(obj, props, FOREGROUND|LABEL);
847 } else if (type == "radio") {
849 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
850 setupObject(obj, props);
851 setColor(obj, props, FOREGROUND|LABEL);
854 } else if (type == "button") {
856 const char *legend = props->getStringValue("legend", "[none]");
857 if (props->getBoolValue("one-shot", true))
858 obj = new puOneShot(x, y, legend);
860 obj = new puButton(x, y, legend);
862 obj->setSize(width, height);
863 setupObject(obj, props);
864 setColor(obj, props);
866 } else if (type == "map") {
867 MapWidget* mapWidget = new MapWidget(x, y, x + width, y + height);
868 setupObject(mapWidget, props);
870 } else if (type == "canvas") {
871 CanvasWidget* canvasWidget = new CanvasWidget( x, y,
872 x + width, y + height,
875 setupObject(canvasWidget, props);
877 } else if (type == "combo") {
878 fgComboBox *obj = new fgComboBox(x, y, x + width, y + height, props,
879 props->getBoolValue("editable", false));
880 setupObject(obj, props);
881 setColor(obj, props, EDITFIELD);
884 } else if (type == "slider") {
885 bool vertical = props->getBoolValue("vertical", false);
886 puSlider *obj = new puSlider(x, y, (vertical ? height : width), vertical);
887 obj->setMinValue(props->getFloatValue("min", 0.0));
888 obj->setMaxValue(props->getFloatValue("max", 1.0));
889 obj->setStepSize(props->getFloatValue("step"));
890 obj->setSliderFraction(props->getFloatValue("fraction"));
891 #if PLIB_VERSION > 185
892 obj->setPageStepSize(props->getFloatValue("pagestep"));
894 setupObject(obj, props);
896 obj->setSize(width, height);
897 setColor(obj, props, FOREGROUND|LABEL);
900 } else if (type == "dial") {
901 puDial *obj = new puDial(x, y, width);
902 obj->setMinValue(props->getFloatValue("min", 0.0));
903 obj->setMaxValue(props->getFloatValue("max", 1.0));
904 obj->setWrap(props->getBoolValue("wrap", true));
905 setupObject(obj, props);
906 setColor(obj, props, FOREGROUND|LABEL);
909 } else if (type == "textbox") {
910 int slider_width = props->getIntValue("slider", 20);
911 int wrap = props->getBoolValue("wrap", true);
912 #if PLIB_VERSION > 185
913 puaLargeInput *obj = new puaLargeInput(x, y,
914 x + width, x + height, 11, slider_width, wrap);
916 puaLargeInput *obj = new puaLargeInput(x, y,
917 x + width, x + height, 2, slider_width, wrap);
920 if (props->getBoolValue("editable"))
926 obj->setSize(width, height);
927 setupObject(obj, props);
928 setColor(obj, props, FOREGROUND|LABEL);
930 int top = props->getIntValue("top-line", 0);
931 obj->setTopLineInWindow(top < 0 ? unsigned(-1) >> 1 : top);
934 } else if (type == "select") {
935 fgSelectBox *obj = new fgSelectBox(x, y, x + width, y + height, props);
936 setupObject(obj, props);
937 setColor(obj, props, EDITFIELD);
939 } else if (type == "waypointlist") {
940 ScrolledWaypointList* obj = new ScrolledWaypointList(x, y, width, height);
941 setupObject(obj, props);
949 FGPUIDialog::setupObject (puObject *object, SGPropertyNode *props)
951 GUIInfo *info = new GUIInfo(this);
952 object->setUserData(info);
953 _info.push_back(info);
954 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
955 object->makeReturnDefault(props->getBoolValue("default"));
958 if (props->hasValue("legend")) {
959 info->legend = props->getStringValue("legend");
960 object->setLegend(info->legend.c_str());
963 if (props->hasValue("label")) {
964 info->label = props->getStringValue("label");
965 object->setLabel(info->label.c_str());
968 if (props->hasValue("border"))
969 object->setBorderThickness( props->getIntValue("border", 2) );
971 if (SGPropertyNode *nft = props->getNode("font", false)) {
972 FGFontCache *fc = globals->get_fontcache();
973 puFont *lfnt = fc->get(nft);
974 object->setLabelFont(*lfnt);
975 object->setLegendFont(*lfnt);
977 object->setLabelFont(*_font);
980 if (props->hasChild("visible")) {
981 ConditionalObject* cnd = new ConditionalObject("visible", object);
982 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("visible")));
983 _conditionalObjects.push_back(cnd);
986 if (props->hasChild("enable")) {
987 ConditionalObject* cnd = new ConditionalObject("enable", object);
988 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("enable")));
989 _conditionalObjects.push_back(cnd);
992 string type = props->getName();
993 if (type == "input" && props->getBoolValue("live"))
994 object->setDownCallback(action_callback);
996 if (type == "text") {
997 const char *format = props->getStringValue("format", 0);
999 info->fmt_type = validate_format(format);
1000 if (info->fmt_type != f_INVALID)
1001 info->format = format;
1003 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
1008 if (props->hasValue("property")) {
1009 const char *name = props->getStringValue("name");
1012 const char *propname = props->getStringValue("property");
1013 SGPropertyNode_ptr node = fgGetNode(propname, true);
1014 if (type == "map") {
1015 // mapWidget binds to a sub-tree of properties, and
1016 // ignores the puValue mechanism, so special case things here
1017 MapWidget* mw = static_cast<MapWidget*>(object);
1018 mw->setProperty(node);
1020 // normal widget, creating PropertyObject
1021 copy_to_pui(node, object);
1022 PropertyObject *po = new PropertyObject(name, object, node);
1023 _propertyObjects.push_back(po);
1024 if (props->getBoolValue("live"))
1025 _liveObjects.push_back(po);
1031 SGPropertyNode *dest = fgGetNode("/sim/bindings/gui", true);
1032 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
1033 if (bindings.size() > 0) {
1034 info->key = props->getIntValue("keynum", -1);
1035 if (props->hasValue("key"))
1036 info->key = getKeyCode(props->getStringValue("key", ""));
1038 for (unsigned int i = 0; i < bindings.size(); i++) {
1040 SGPropertyNode_ptr binding;
1041 while (dest->getChild("binding", j))
1044 const char *cmd = bindings[i]->getStringValue("command");
1045 if (!strcmp(cmd, "nasal"))
1046 bindings[i]->setStringValue("module", _module.c_str());
1048 binding = dest->getChild("binding", j, true);
1049 copyProperties(bindings[i], binding);
1050 info->bindings.push_back(new SGBinding(binding, globals->get_props()));
1052 object->setCallback(action_callback);
1057 FGPUIDialog::setupGroup(puGroup *group, SGPropertyNode *props,
1058 int width, int height, bool makeFrame)
1060 setupObject(group, props);
1063 puFrame* f = new puFrame(0, 0, width, height);
1067 int nChildren = props->nChildren();
1068 for (int i = 0; i < nChildren; i++)
1069 makeObject(props->getChild(i), width, height);
1074 FGPUIDialog::setColor(puObject *object, SGPropertyNode *props, int which)
1076 string type = props->getName();
1079 if (type == "textbox" && props->getBoolValue("editable"))
1080 type += "-editable";
1082 FGColor c(_gui->getColor("background"));
1083 c.merge(_gui->getColor(type));
1084 c.merge(props->getNode("color"));
1086 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
1094 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
1095 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
1096 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
1097 { LABEL, PUCOL_LABEL, "label", "color-label" },
1098 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
1099 { MISC, PUCOL_MISC, "misc", "color-misc" },
1100 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
1103 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
1105 for (int i = 0; i < numcol; i++) {
1110 dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
1111 if (which & pucol[i].mask)
1112 dirty |= c.merge(props->getNode("color"));
1114 if ((pucol[i].mask == LABEL) && !c.isValid())
1115 dirty |= c.merge(_gui->getColor("label"));
1117 dirty |= c.merge(props->getNode(pucol[i].cname));
1119 if (c.isValid() && dirty)
1120 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
1149 {"f10", PU_KEY_F10},
1150 {"f11", PU_KEY_F11},
1151 {"f12", PU_KEY_F12},
1152 {"left", PU_KEY_LEFT},
1154 {"right", PU_KEY_RIGHT},
1155 {"down", PU_KEY_DOWN},
1156 {"pageup", PU_KEY_PAGE_UP},
1157 {"pagedn", PU_KEY_PAGE_DOWN},
1158 {"home", PU_KEY_HOME},
1159 {"end", PU_KEY_END},
1160 {"insert", PU_KEY_INSERT},
1165 FGPUIDialog::getKeyCode(const char *str)
1176 char *buf = new char[strlen(str) + 1];
1178 char *s = buf + strlen(buf);
1179 while (s > str && s[-1] == ' ')
1186 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
1187 s += 5, mod |= CTRL;
1188 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
1189 s += 6, mod |= SHIFT;
1190 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
1197 if (strlen(s) == 1 && isascii(*s)) {
1202 key = toupper(key) - '@';
1204 ; // Alt not propagated to the gui
1207 for (char *t = s; *t; t++)
1209 for (int i = 0; keymap[i].name; i++) {
1210 if (!strcmp(s, keymap[i].name)) {
1211 key = keymap[i].key;
1220 void FGPUIDialog::relayout()
1222 _needsRelayout = false;
1224 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
1225 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
1227 bool userx = _props->hasValue("x");
1228 bool usery = _props->hasValue("y");
1229 bool userw = _props->hasValue("width");
1230 bool userh = _props->hasValue("height");
1232 // Let the layout widget work in the same property subtree.
1233 LayoutWidget wid(_props);
1234 wid.setDefaultFont(_font, int(_font->getPointSize()));
1237 int px, py, savex, savey;
1238 if (!userw || !userh) {
1239 wid.calcPrefSize(&pw, &ph);
1242 pw = _props->getIntValue("width", pw);
1243 ph = _props->getIntValue("height", ph);
1244 px = savex = _props->getIntValue("x", (screenw - pw) / 2);
1245 py = savey = _props->getIntValue("y", (screenh - ph) / 2);
1247 // Negative x/y coordinates are interpreted as distance from the top/right
1248 // corner rather than bottom/left.
1249 if (userx && px < 0)
1250 px = screenw - pw + px;
1251 if (usery && py < 0)
1252 py = screenh - ph + py;
1254 // Define "x", "y", "width" and/or "height" in the property tree if they
1255 // are not specified in the configuration file.
1256 wid.layout(px, py, pw, ph);
1260 // Remove automatically generated properties, so the layout looks
1261 // the same next time around, or restore x and y to preserve negative coords.
1263 _props->setIntValue("x", savex);
1265 _props->removeChild("x");
1268 _props->setIntValue("y", savey);
1270 _props->removeChild("y");
1272 if (!userw) _props->removeChild("width");
1273 if (!userh) _props->removeChild("height");
1277 FGPUIDialog::applySize(puObject *object)
1279 // compound plib widgets use setUserData() for internal purposes, so refuse
1280 // to descend into anything that has other bits set than the following
1281 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
1282 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
1283 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
1285 int type = object->getType();
1286 if ((type & PUCLASS_GROUP) && !(type & ~validUserData)) {
1287 puObject* c = ((puGroup *)object)->getFirstChild();
1288 for (; c != NULL; c = c->getNextObject()) {
1290 } // of child iteration
1291 } // of group object case
1293 GUIInfo *info = (GUIInfo *)object->getUserData();
1297 SGPropertyNode *n = info->node;
1299 SG_LOG(SG_GENERAL, SG_ALERT, "FGDialog::applySize: no props");
1303 int x = n->getIntValue("x");
1304 int y = n->getIntValue("y");
1305 int w = n->getIntValue("width", 4);
1306 int h = n->getIntValue("height", 4);
1307 object->setPosition(x, y);
1308 object->setSize(w, h);
1311 ////////////////////////////////////////////////////////////////////////
1312 // Implementation of FGDialog::PropertyObject.
1313 ////////////////////////////////////////////////////////////////////////
1315 FGPUIDialog::PropertyObject::PropertyObject(const char *n,
1316 puObject *o, SGPropertyNode_ptr p) :
1325 ////////////////////////////////////////////////////////////////////////
1326 // Implementation of fgValueList and derived pui widgets
1327 ////////////////////////////////////////////////////////////////////////
1330 fgValueList::fgValueList(SGPropertyNode *p) :
1337 fgValueList::update()
1343 fgValueList::~fgValueList()
1349 fgValueList::make_list()
1351 SGPropertyNode_ptr values = _props;
1352 const char* vname = "value";
1354 if (_props->hasChild("properties")) {
1355 // dynamic values, read from a property's children
1356 const char* path = _props->getStringValue("properties");
1357 values = fgGetNode(path, true);
1360 if (_props->hasChild("property-name")) {
1361 vname = _props->getStringValue("property-name");
1364 vector<SGPropertyNode_ptr> value_nodes = values->getChildren(vname);
1365 _list = new char *[value_nodes.size() + 1];
1367 for (i = 0; i < value_nodes.size(); i++) {
1368 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
1374 fgValueList::destroy_list()
1376 for (int i = 0; _list[i] != 0; i++)
1387 fgValueList::update();
1388 int top = getTopItem();
1393 void fgComboBox::update()
1399 std::string curValue(getStringValue());
1400 fgValueList::update();
1402 int currentItem = puaComboBox::getCurrentItem();
1405 // look for the previous value, in the new list
1406 for (int i = 0; _list[i] != 0; i++) {
1407 if (_list[i] == curValue) {
1408 // don't set the current item again; this is important to avoid
1409 // recursion here if the combo callback ultimately causes another dialog-update
1410 if (i != currentItem) {
1411 puaComboBox::setCurrentItem(i);
1416 } // of list values iteration
1418 // cound't find current item, default to first
1419 if (currentItem != 0) {
1420 puaComboBox::setCurrentItem(0);
1424 int fgComboBox::checkHit(int b, int up, int x, int y)
1427 int r = puaComboBox::checkHit(b, up, x, y);
1432 void fgComboBox::setSize(int w, int h)
1434 puaComboBox::setSize(w, h);
1438 void fgComboBox::recalc_bbox()
1440 // bug-fix for issue #192
1441 // http://code.google.com/p/flightgear-bugs/issues/detail?id=192
1442 // puaComboBox is including the height of its popupMenu in the height
1443 // computation, which breaks layout computations.
1444 // this implementation skips popup-menus
1449 for (puObject *bo = dlist; bo != NULL; bo = bo -> getNextObject()) {
1450 if (bo->getType() & PUCLASS_POPUPMENU) {
1454 contents.extend (bo -> getBBox()) ;
1457 abox.max[0] = abox.min[0] + contents.max[0] ;
1458 abox.max[1] = abox.min[1] + contents.max[1] ;
1460 puObject::recalc_bbox () ;
1464 // end of FGPUIDialog.cxx