]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.cxx
Moved some of the low level scene graph construction code over to simgear.
[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  * User data for a GUI object.
16  */
17 struct GUIInfo
18 {
19     GUIInfo (FGDialog * d);
20     virtual ~GUIInfo ();
21
22     FGDialog * dialog;
23     vector <FGBinding *> bindings;
24 };
25
26
27 /**
28  * Action callback.
29  */
30 static void
31 action_callback (puObject * object)
32 {
33     GUIInfo * info = (GUIInfo *)object->getUserData();
34     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
35     gui->setActiveDialog(info->dialog);
36     int nBindings = info->bindings.size();
37     for (int i = 0; i < nBindings; i++) {
38         info->bindings[i]->fire();
39         if (gui->getActiveDialog() == 0)
40             break;
41     }
42     gui->setActiveDialog(0);
43 }
44
45
46 \f
47 ////////////////////////////////////////////////////////////////////////
48 // Static helper functions.
49 ////////////////////////////////////////////////////////////////////////
50
51 /**
52  * Copy a property value to a PUI object.
53  */
54 static void
55 copy_to_pui (SGPropertyNode * node, puObject * object)
56 {
57     switch (node->getType()) {
58     case SGPropertyNode::BOOL:
59     case SGPropertyNode::INT:
60     case SGPropertyNode::LONG:
61         object->setValue(node->getIntValue());
62         break;
63     case SGPropertyNode::FLOAT:
64     case SGPropertyNode::DOUBLE:
65         object->setValue(node->getFloatValue());
66         break;
67     default:
68         object->setValue(node->getStringValue());
69         break;
70     }
71 }
72
73
74 static void
75 copy_from_pui (puObject * object, SGPropertyNode * node)
76 {
77     switch (node->getType()) {
78     case SGPropertyNode::BOOL:
79     case SGPropertyNode::INT:
80     case SGPropertyNode::LONG:
81         node->setIntValue(object->getIntegerValue());
82         break;
83     case SGPropertyNode::FLOAT:
84     case SGPropertyNode::DOUBLE:
85         node->setFloatValue(object->getFloatValue());
86         break;
87     default:
88         node->setStringValue(object->getStringValue());
89         break;
90     }
91 }
92
93
94 \f
95 ////////////////////////////////////////////////////////////////////////
96 // Implementation of GUIInfo.
97 ////////////////////////////////////////////////////////////////////////
98
99 GUIInfo::GUIInfo (FGDialog * d)
100     : dialog(d)
101 {
102 }
103
104 GUIInfo::~GUIInfo ()
105 {
106     for (unsigned int i = 0; i < bindings.size(); i++) {
107         delete bindings[i];
108         bindings[i] = 0;
109     }
110 }
111
112
113 \f
114 ////////////////////////////////////////////////////////////////////////
115 // Implementation of FGDialog.
116 ////////////////////////////////////////////////////////////////////////
117
118 FGDialog::FGDialog (SGPropertyNode * props)
119     : _object(0)
120 {
121     display(props);
122 }
123
124 FGDialog::~FGDialog ()
125 {
126     puDeleteObject(_object);
127
128     unsigned int i;
129
130                                 // Delete all the arrays we made
131                                 // and were forced to keep around
132                                 // because PUI won't do its own
133                                 // memory management.
134     for (i = 0; i < _char_arrays.size(); i++) {
135         for (int j = 0; _char_arrays[i][j] != 0; j++)
136             free(_char_arrays[i][j]); // added with strdup
137         delete _char_arrays[i];
138     }
139
140                                 // Delete all the info objects we
141                                 // were forced to keep around because
142                                 // PUI cannot delete its own user data.
143     for (i = 0; i < _info.size(); i++) {
144         delete (GUIInfo *)_info[i];
145         _info[i] = 0;
146     }
147
148                                 // Finally, delete the property links.
149     for (i = 0; i < _propertyObjects.size(); i++) {
150         delete _propertyObjects[i];
151         _propertyObjects[i] = 0;
152     }
153 }
154
155 void
156 FGDialog::updateValue (const char * objectName)
157 {
158     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
159         const string &name = _propertyObjects[i]->name;
160         if (name == objectName)
161             copy_to_pui(_propertyObjects[i]->node,
162                         _propertyObjects[i]->object);
163     }
164 }
165
166 void
167 FGDialog::applyValue (const char * objectName)
168 {
169     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
170         if (_propertyObjects[i]->name == objectName)
171             copy_from_pui(_propertyObjects[i]->object,
172                           _propertyObjects[i]->node);
173     }
174 }
175
176 void
177 FGDialog::updateValues ()
178 {
179     for (unsigned int i = 0; i < _propertyObjects.size(); i++)
180         copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
181 }
182
183 void
184 FGDialog::applyValues ()
185 {
186     for (unsigned int i = 0; i < _propertyObjects.size(); i++)
187         copy_from_pui(_propertyObjects[i]->object,
188                       _propertyObjects[i]->node);
189 }
190
191 void
192 FGDialog::display (SGPropertyNode * props)
193 {
194     if (_object != 0) {
195         SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
196         return;
197     }
198
199     _object = makeObject(props,
200                       globals->get_props()->getIntValue("/sim/startup/xsize"),
201                       globals->get_props()->getIntValue("/sim/startup/ysize"));
202
203     if (_object != 0) {
204         _object->reveal();
205     } else {
206         SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
207                << props->getStringValue("name", "[unnamed]")
208                << " does not contain a proper GUI definition");
209     }
210 }
211
212 puObject *
213 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
214 {
215     int width = props->getIntValue("width", parentWidth);
216     int height = props->getIntValue("height", parentHeight);
217
218     int x = props->getIntValue("x", (parentWidth - width) / 2);
219     int y = props->getIntValue("y", (parentHeight - height) / 2);
220
221     string type = props->getName();
222     if (type == "")
223         type = "dialog";
224
225     if (type == "dialog") {
226         puPopup * dialog;
227         if (props->getBoolValue("modal", false))
228             dialog = new puDialogBox(x, y);
229         else
230             dialog = new puPopup(x, y);
231         setupGroup(dialog, props, width, height, true);
232         return dialog;
233     } else if (type == "group") {
234         puGroup * group = new puGroup(x, y);
235         setupGroup(group, props, width, height, false);
236         return group;
237     } else if (type == "input") {
238         puInput * input = new puInput(x, y, x + width, y + height);
239         setupObject(input, props);
240         return input;
241     } else if (type == "text") {
242         puText * text = new puText(x, y);
243         setupObject(text, props);
244         return text;
245     } else if (type == "checkbox") {
246         puButton * b;
247         b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
248         setupObject(b, props);
249         return b;
250     } else if (type == "button") {
251         puButton * b;
252         const char * legend = props->getStringValue("legend", "[none]");
253         if (props->getBoolValue("one-shot", true))
254             b = new puOneShot(x, y, legend);
255         else
256             b = new puButton(x, y, legend);
257         setupObject(b, props);
258         return b;
259     } else if (type == "combo") {
260         vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
261         char ** entries = make_char_array(value_nodes.size());
262         for (unsigned int i = 0, j = value_nodes.size() - 1;
263              i < value_nodes.size();
264              i++, j--)
265             entries[i] = strdup((char *)value_nodes[i]->getStringValue());
266         puComboBox * combo =
267             new puComboBox(x, y, x + width, y + height, entries,
268                            props->getBoolValue("editable", false));
269         setupObject(combo, props);
270         return combo;
271     } else if (type == "slider") {
272         bool vertical = props->getBoolValue("vertical", false);
273         puSlider * slider = new puSlider(x, y, (vertical ? height : width));
274         slider->setMinValue(props->getFloatValue("min", 0.0));
275         slider->setMaxValue(props->getFloatValue("max", 1.0));
276         setupObject(slider, props);
277         return slider;
278     } else if (type == "dial") {
279         puDial * dial = new puDial(x, y, width);
280         dial->setMinValue(props->getFloatValue("min", 0.0));
281         dial->setMaxValue(props->getFloatValue("max", 1.0));
282         dial->setWrap(props->getBoolValue("wrap", true));
283         setupObject(dial, props);
284         return dial;
285     } else if (type == "select") {
286         vector<SGPropertyNode_ptr> value_nodes;
287         SGPropertyNode * selection_node =
288                 fgGetNode(props->getChild("selection")->getStringValue(), true);
289
290         for (int q = 0; q < selection_node->nChildren(); q++)
291             value_nodes.push_back(selection_node->getChild(q));
292
293         char ** entries = make_char_array(value_nodes.size());
294         for (unsigned int i = 0, j = value_nodes.size() - 1;
295              i < value_nodes.size();
296              i++, j--)
297             entries[i] = strdup((char *)value_nodes[i]->getName());
298         puSelectBox * select =
299             new puSelectBox(x, y, x + width, y + height, entries);
300         setupObject(select, props);
301         return select;
302     } else {
303         return 0;
304     }
305 }
306
307 void
308 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
309 {
310     if (props->hasValue("legend"))
311         object->setLegend(props->getStringValue("legend"));
312
313     if (props->hasValue("label"))
314         object->setLabel(props->getStringValue("label"));
315
316     if (props->hasValue("property")) {
317         const char * name = props->getStringValue("name");
318         if (name == 0)
319             name = "";
320         const char * propname = props->getStringValue("property");
321         SGPropertyNode_ptr node = fgGetNode(propname, true);
322         copy_to_pui(node, object);
323         if (name != 0)
324             _propertyObjects.push_back(new PropertyObject(name, object, node));
325     }
326
327     vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
328     if (nodes.size() > 0) {
329         GUIInfo * info = new GUIInfo(this);
330
331         for (unsigned int i = 0; i < nodes.size(); i++)
332             info->bindings.push_back(new FGBinding(nodes[i]));
333         object->setCallback(action_callback);
334         object->setUserData(info);
335         _info.push_back(info);
336     }
337
338     object->makeReturnDefault(props->getBoolValue("default"));
339 }
340
341 void
342 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
343                     int width, int height, bool makeFrame)
344 {
345     setupObject(group, props);
346
347     if (makeFrame)
348         new puFrame(0, 0, width, height);
349
350     int nChildren = props->nChildren();
351     for (int i = 0; i < nChildren; i++)
352         makeObject(props->getChild(i), width, height);
353     group->close();
354 }
355
356 char **
357 FGDialog::make_char_array (int size)
358 {
359     char ** list = new char*[size+1];
360     for (int i = 0; i <= size; i++)
361         list[i] = 0;
362     _char_arrays.push_back(list);
363     return list;
364 }
365
366
367 \f
368 ////////////////////////////////////////////////////////////////////////
369 // Implementation of FGDialog::PropertyObject.
370 ////////////////////////////////////////////////////////////////////////
371
372 FGDialog::PropertyObject::PropertyObject (const char * n,
373                                            puObject * o,
374                                            SGPropertyNode_ptr p)
375     : name(n),
376       object(o),
377       node(p)
378 {
379 }
380
381
382 // end of dialog.cxx