1 // new_gui.cxx: implementation of XML-configurable GUI support.
10 #include <simgear/misc/exception.hxx>
11 #include <Main/fg_props.hxx>
29 fgLoadProps("gui.xml", &props);
30 } catch (const sg_exception &ex) {
31 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing gui.xml: "
36 int nChildren = props.nChildren();
37 for (int i = 0; i < nChildren; i++) {
38 SGPropertyNode_ptr child = props.getChild(i);
39 if (!child->hasValue("name")) {
40 SG_LOG(SG_INPUT, SG_WARN, "GUI node " << child->getName()
41 << " has no name; skipping.");
43 string name = child->getStringValue("name");
44 SG_LOG(SG_INPUT, SG_BULK, "Saving GUI node " << name);
45 _objects[name] = child;
51 NewGUI::update (double delta_time_sec)
57 close_callback (puObject * object)
59 ((NewGUI *)object->getUserData())->closeActiveObject();
63 NewGUI::closeActiveObject ()
70 NewGUI::display (const string &name)
72 if (_activeObject != 0) {
73 SG_LOG(SG_GENERAL, SG_ALERT, "Another GUI object is still active");
77 if (_objects.find(name) == _objects.end()) {
78 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
81 SGPropertyNode_ptr props = _objects[name];
83 _activeObject = makeObject(props, 1024, 768);
85 if (_activeObject != 0) {
86 _activeObject->reveal();
88 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name
89 << " does not contain a proper GUI definition");
94 NewGUI::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
96 int width = props->getIntValue("width", parentWidth);
97 int height = props->getIntValue("height", parentHeight);
99 int x = props->getIntValue("x", (parentWidth - width) / 2);
100 int y = props->getIntValue("y", (parentHeight - height) / 2);
102 string type = props->getName();
104 if (type == "dialog") {
106 if (props->getBoolValue("modal", false))
107 dialog = new puDialogBox(x, y);
109 dialog = new puPopup(x, y);
110 setupGroup(dialog, props, width, height, true);
112 } else if (type == "group") {
113 puGroup * group = new puGroup(x, y);
114 setupGroup(group, props, width, height, false);
116 } else if (type == "input") {
117 puInput * input = new puInput(x, y, x + width, y + height);
118 setupObject(input, props);
120 } else if (type == "text") {
121 puText * text = new puText(x, y);
122 setupObject(text, props);
124 } else if (type == "button") {
126 const char * legend = props->getStringValue("legend", "[none]");
127 if (props->getBoolValue("one-shot", true))
128 b = new puOneShot(x, y, legend);
130 b = new puButton(x, y, legend);
131 setupObject(b, props);
132 b->setCallback(close_callback);
133 b->setUserData(this);
141 NewGUI::setupObject (puObject * object, SGPropertyNode * props)
143 if (props->hasValue("legend"))
144 object->setLegend(props->getStringValue("legend"));
146 if (props->hasValue("label"))
147 object->setLabel(props->getStringValue("label"));
149 if (props->hasValue("default-value-prop"))
150 object->setValue(fgGetString(props->getStringValue("default-value-prop")));
152 object->makeReturnDefault(props->getBoolValue("default"));
156 NewGUI::setupGroup (puGroup * group, SGPropertyNode * props,
157 int width, int height, bool makeFrame)
159 setupObject(group, props);
162 new puFrame(0, 0, width, height);
164 int nChildren = props->nChildren();
165 for (int i = 0; i < nChildren; i++)
166 makeObject(props->getChild(i), width, height);
170 // end of new_gui.cxx