]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.cxx
Make some allowances for a dialog closing in the middle of a callback.
[flightgear.git] / src / GUI / dialog.cxx
1 // dialog.cxx: implementation of an XML-configurable dialog box.
2
3 #include <Input/input.hxx>
4
5 #include "dialog.hxx"
6 #include "new_gui.hxx"
7
8
9 \f
10 ////////////////////////////////////////////////////////////////////////
11 // Callbacks.
12 ////////////////////////////////////////////////////////////////////////
13
14 /**
15  * Action callback.
16  */
17 static void
18 action_callback (puObject * object)
19 {
20     GUIInfo * info = (GUIInfo *)object->getUserData();
21     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
22     gui->setCurrentWidget(info->widget);
23     int nBindings = info->bindings.size();
24     for (int i = 0; i < nBindings; i++) {
25         info->bindings[i]->fire();
26         if (gui->getCurrentWidget() == 0)
27             break;
28     }
29     gui->setCurrentWidget(0);
30 }
31
32
33 \f
34 ////////////////////////////////////////////////////////////////////////
35 // Static helper functions.
36 ////////////////////////////////////////////////////////////////////////
37
38 /**
39  * Copy a property value to a PUI object.
40  */
41 static void
42 copy_to_pui (SGPropertyNode * node, puObject * object)
43 {
44     switch (node->getType()) {
45     case SGPropertyNode::BOOL:
46     case SGPropertyNode::INT:
47     case SGPropertyNode::LONG:
48         object->setValue(node->getIntValue());
49         break;
50     case SGPropertyNode::FLOAT:
51     case SGPropertyNode::DOUBLE:
52         object->setValue(node->getFloatValue());
53         break;
54     default:
55         object->setValue(node->getStringValue());
56         break;
57     }
58 }
59
60
61 static void
62 copy_from_pui (puObject * object, SGPropertyNode * node)
63 {
64     switch (node->getType()) {
65     case SGPropertyNode::BOOL:
66     case SGPropertyNode::INT:
67     case SGPropertyNode::LONG:
68         node->setIntValue(object->getIntegerValue());
69         break;
70     case SGPropertyNode::FLOAT:
71     case SGPropertyNode::DOUBLE:
72         node->setFloatValue(object->getFloatValue());
73         break;
74     default:
75         node->setStringValue(object->getStringValue());
76         break;
77     }
78 }
79
80
81 \f
82 ////////////////////////////////////////////////////////////////////////
83 // Implementation of GUIInfo.
84 ////////////////////////////////////////////////////////////////////////
85
86 GUIInfo::GUIInfo (FGDialog * w)
87     : widget(w)
88 {
89 }
90
91 GUIInfo::~GUIInfo ()
92 {
93     for (int i = 0; i < bindings.size(); i++) {
94         delete bindings[i];
95         bindings[i] = 0;
96     }
97 }
98
99
100 \f
101 ////////////////////////////////////////////////////////////////////////
102 // Implementation of FGDialog.
103 ////////////////////////////////////////////////////////////////////////
104
105 FGDialog::FGDialog (SGPropertyNode_ptr props)
106     : _object(0)
107 {
108     display(props);
109 }
110
111 FGDialog::~FGDialog ()
112 {
113     delete _object;
114
115     int i;
116     for (i = 0; i < _info.size(); i++) {
117         delete _info[i];
118         _info[i] = 0;
119     }
120
121     for (i = 0; i < _propertyObjects.size(); i++) {
122         delete _propertyObjects[i];
123         _propertyObjects[i] = 0;
124     }
125 }
126
127 void
128 FGDialog::updateValue (const char * objectName)
129 {
130     for (int i = 0; i < _propertyObjects.size(); i++) {
131         const string &name = _propertyObjects[i]->name;
132         if (name == objectName)
133             copy_to_pui(_propertyObjects[i]->node,
134                         _propertyObjects[i]->object);
135     }
136 }
137
138 void
139 FGDialog::applyValue (const char * objectName)
140 {
141     for (int i = 0; i < _propertyObjects.size(); i++) {
142         if (_propertyObjects[i]->name == objectName)
143             copy_from_pui(_propertyObjects[i]->object,
144                           _propertyObjects[i]->node);
145     }
146 }
147
148 void
149 FGDialog::updateValues ()
150 {
151     for (int i = 0; i < _propertyObjects.size(); i++)
152         copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
153 }
154
155 void
156 FGDialog::applyValues ()
157 {
158     for (int i = 0; i < _propertyObjects.size(); i++)
159         copy_from_pui(_propertyObjects[i]->object,
160                       _propertyObjects[i]->node);
161 }
162
163 void
164 FGDialog::display (SGPropertyNode_ptr props)
165 {
166     if (_object != 0) {
167         SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
168         return;
169     }
170
171     _object = makeObject(props, 1024, 768);
172
173     if (_object != 0) {
174         _object->reveal();
175     } else {
176         SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
177                << props->getStringValue("name", "[unnamed]")
178                << " does not contain a proper GUI definition");
179     }
180 }
181
182 puObject *
183 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
184 {
185     int width = props->getIntValue("width", parentWidth);
186     int height = props->getIntValue("height", parentHeight);
187
188     int x = props->getIntValue("x", (parentWidth - width) / 2);
189     int y = props->getIntValue("y", (parentHeight - height) / 2);
190
191     string type = props->getName();
192     if (type == "")
193         type = props->getStringValue("type");
194     if (type == "") {
195         SG_LOG(SG_GENERAL, SG_ALERT, "No type specified for GUI object");
196         return 0;
197     }
198
199     if (type == "dialog") {
200         puPopup * dialog;
201         if (props->getBoolValue("modal", false))
202             dialog = new puDialogBox(x, y);
203         else
204             dialog = new puPopup(x, y);
205         setupGroup(dialog, props, width, height, true);
206         return dialog;
207     } else if (type == "group") {
208         puGroup * group = new puGroup(x, y);
209         setupGroup(group, props, width, height, false);
210         return group;
211     } else if (type == "input") {
212         puInput * input = new puInput(x, y, x + width, y + height);
213         setupObject(input, props);
214         return input;
215     } else if (type == "text") {
216         puText * text = new puText(x, y);
217         setupObject(text, props);
218         return text;
219     } else if (type == "checkbox") {
220         puButton * b;
221         b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
222         setupObject(b, props);
223         return b;
224     } else if (type == "button") {
225         puButton * b;
226         const char * legend = props->getStringValue("legend", "[none]");
227         if (props->getBoolValue("one-shot", true))
228             b = new puOneShot(x, y, legend);
229         else
230             b = new puButton(x, y, legend);
231         setupObject(b, props);
232         return b;
233     } else if (type == "combo") {
234         vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
235         char ** entries = new char*[value_nodes.size()+1];
236         for (int i = 0, j = value_nodes.size() - 1;
237              i < value_nodes.size();
238              i++, j--)
239             entries[i] = (char *)value_nodes[i]->getStringValue();
240         entries[value_nodes.size()] = 0;
241         puComboBox * combo =
242             new puComboBox(x, y, x + width, y + height, entries,
243                            props->getBoolValue("editable", false));
244 //         delete entries;
245         setupObject(combo, props);
246         return combo;
247     } else {
248         return 0;
249     }
250 }
251
252 void
253 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
254 {
255     if (props->hasValue("legend"))
256         object->setLegend(props->getStringValue("legend"));
257
258     if (props->hasValue("label"))
259         object->setLabel(props->getStringValue("label"));
260
261     if (props->hasValue("property")) {
262         const char * name = props->getStringValue("name");
263         if (name == 0)
264             name = "";
265         const char * propname = props->getStringValue("property");
266         SGPropertyNode_ptr node = fgGetNode(propname, true);
267         copy_to_pui(node, object);
268         if (name != 0)
269             _propertyObjects.push_back(new PropertyObject(name, object, node));
270     }
271
272     vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
273     if (nodes.size() > 0) {
274         GUIInfo * info = new GUIInfo(this);
275
276         for (int i = 0; i < nodes.size(); i++)
277             info->bindings.push_back(new FGBinding(nodes[i]));
278         object->setCallback(action_callback);
279         object->setUserData(info);
280         _info.push_back(info);
281     }
282
283     object->makeReturnDefault(props->getBoolValue("default"));
284 }
285
286 void
287 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
288                     int width, int height, bool makeFrame)
289 {
290     setupObject(group, props);
291
292     if (makeFrame)
293         new puFrame(0, 0, width, height);
294
295     int nChildren = props->nChildren();
296     for (int i = 0; i < nChildren; i++)
297         makeObject(props->getChild(i), width, height);
298     group->close();
299 }
300
301
302 \f
303 ////////////////////////////////////////////////////////////////////////
304 // Implementation of FGDialog::PropertyObject.
305 ////////////////////////////////////////////////////////////////////////
306
307 FGDialog::PropertyObject::PropertyObject (const char * n,
308                                            puObject * o,
309                                            SGPropertyNode_ptr p)
310     : name(n),
311       object(o),
312       node(p)
313 {
314 }
315
316
317 // end of dialog.cxx