]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.cxx
Boris Koenig:
[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 #include "puList.hxx"
9 #include "AirportList.hxx"
10 #include "layout.hxx"
11
12 int fgPopup::checkHit(int button, int updown, int x, int y)
13 {
14     int result = puPopup::checkHit(button, updown, x, y);
15
16     // This is annoying.  We would really want a true result from the
17     // superclass to indicate "handled by child object", but all it
18     // tells us is that the pointer is inside the dialog.  So do the
19     // intersection test (again) to make sure we don't start a drag
20     // when inside controls.
21     if(!result) return result;
22     puObject* child = getFirstChild();
23     if(child) child = child->getNextObject(); // Skip the puFrame
24     while(child) {
25         int cx, cy, cw, ch;
26         child->getAbsolutePosition(&cx, &cy);
27         child->getSize(&cw, &ch);
28         if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
29             return result;
30         child = child->getNextObject();
31     }
32
33     // Finally, handle the mouse event
34     if(updown == PU_DOWN) {
35         int px, py;
36         getPosition(&px, &py);
37         _dragging = true;
38         _dX = px - x;
39         _dY = py - y;
40     } else if(updown == PU_DRAG && _dragging) {
41         setPosition(x + _dX, y + _dY);
42     } else {
43         _dragging = false;
44     }
45     return 1;
46 }
47
48 \f
49 ////////////////////////////////////////////////////////////////////////
50 // Callbacks.
51 ////////////////////////////////////////////////////////////////////////
52
53 /**
54  * User data for a GUI object.
55  */
56 struct GUIInfo
57 {
58     GUIInfo (FGDialog * d);
59     virtual ~GUIInfo ();
60
61     FGDialog * dialog;
62     vector <FGBinding *> bindings;
63 };
64
65
66 /**
67  * Action callback.
68  */
69 static void
70 action_callback (puObject * object)
71 {
72     GUIInfo * info = (GUIInfo *)object->getUserData();
73     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
74     gui->setActiveDialog(info->dialog);
75     int nBindings = info->bindings.size();
76     for (int i = 0; i < nBindings; i++) {
77         info->bindings[i]->fire();
78         if (gui->getActiveDialog() == 0)
79             break;
80     }
81     gui->setActiveDialog(0);
82 }
83
84
85 \f
86 ////////////////////////////////////////////////////////////////////////
87 // Static helper functions.
88 ////////////////////////////////////////////////////////////////////////
89
90 /**
91  * Copy a property value to a PUI object.
92  */
93 static void
94 copy_to_pui (SGPropertyNode * node, puObject * object)
95 {
96     // Treat puText objects specially, so their "values" can be set
97     // from properties.
98     if(object->getType() & PUCLASS_TEXT) {
99         object->setLabel(node->getStringValue());
100         return;
101     }
102
103     switch (node->getType()) {
104     case SGPropertyNode::BOOL:
105     case SGPropertyNode::INT:
106     case SGPropertyNode::LONG:
107         object->setValue(node->getIntValue());
108         break;
109     case SGPropertyNode::FLOAT:
110     case SGPropertyNode::DOUBLE:
111         object->setValue(node->getFloatValue());
112         break;
113     default:
114         object->setValue(node->getStringValue());
115         break;
116     }
117 }
118
119
120 static void
121 copy_from_pui (puObject * object, SGPropertyNode * node)
122 {
123     // puText objects are immutable, so should not be copied out
124     if(object->getType() & PUCLASS_TEXT)
125         return;
126
127     switch (node->getType()) {
128     case SGPropertyNode::BOOL:
129     case SGPropertyNode::INT:
130     case SGPropertyNode::LONG:
131         node->setIntValue(object->getIntegerValue());
132         break;
133     case SGPropertyNode::FLOAT:
134     case SGPropertyNode::DOUBLE:
135         node->setFloatValue(object->getFloatValue());
136         break;
137     default:
138         node->setStringValue(object->getStringValue());
139         break;
140     }
141 }
142
143
144 \f
145 ////////////////////////////////////////////////////////////////////////
146 // Implementation of GUIInfo.
147 ////////////////////////////////////////////////////////////////////////
148
149 GUIInfo::GUIInfo (FGDialog * d)
150     : dialog(d)
151 {
152 }
153
154 GUIInfo::~GUIInfo ()
155 {
156     for (unsigned int i = 0; i < bindings.size(); i++) {
157         delete bindings[i];
158         bindings[i] = 0;
159     }
160 }
161
162
163 \f
164 ////////////////////////////////////////////////////////////////////////
165 // Implementation of FGDialog.
166 ////////////////////////////////////////////////////////////////////////
167
168 FGDialog::FGDialog (SGPropertyNode * props)
169     : _object(0)
170 {
171     display(props);
172 }
173
174 FGDialog::~FGDialog ()
175 {
176     puDeleteObject(_object);
177
178     unsigned int i;
179
180                                 // Delete all the arrays we made
181                                 // and were forced to keep around
182                                 // because PUI won't do its own
183                                 // memory management.
184     for (i = 0; i < _char_arrays.size(); i++) {
185         for (int j = 0; _char_arrays[i][j] != 0; j++)
186             free(_char_arrays[i][j]); // added with strdup
187         delete[] _char_arrays[i];
188     }
189
190                                 // Delete all the info objects we
191                                 // were forced to keep around because
192                                 // PUI cannot delete its own user data.
193     for (i = 0; i < _info.size(); i++) {
194         delete (GUIInfo *)_info[i];
195         _info[i] = 0;
196     }
197
198                                 // Finally, delete the property links.
199     for (i = 0; i < _propertyObjects.size(); i++) {
200         delete _propertyObjects[i];
201         _propertyObjects[i] = 0;
202     }
203 }
204
205 void
206 FGDialog::updateValue (const char * objectName)
207 {
208     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
209         const string &name = _propertyObjects[i]->name;
210         if (name == objectName)
211             copy_to_pui(_propertyObjects[i]->node,
212                         _propertyObjects[i]->object);
213     }
214 }
215
216 void
217 FGDialog::applyValue (const char * objectName)
218 {
219     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
220         if (_propertyObjects[i]->name == objectName)
221             copy_from_pui(_propertyObjects[i]->object,
222                           _propertyObjects[i]->node);
223     }
224 }
225
226 void
227 FGDialog::updateValues ()
228 {
229     for (unsigned int i = 0; i < _propertyObjects.size(); i++)
230         copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
231 }
232
233 void
234 FGDialog::applyValues ()
235 {
236     for (unsigned int i = 0; i < _propertyObjects.size(); i++)
237         copy_from_pui(_propertyObjects[i]->object,
238                       _propertyObjects[i]->node);
239 }
240
241 void
242 FGDialog::update ()
243 {
244     for (unsigned int i = 0; i < _liveObjects.size(); i++)
245         copy_to_pui(_liveObjects[i]->node, _liveObjects[i]->object);
246 }
247
248 void
249 FGDialog::display (SGPropertyNode * props)
250 {
251     if (_object != 0) {
252         SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
253         return;
254     }
255
256     int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
257     int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
258
259     bool userx = props->hasValue("x");
260     bool usery = props->hasValue("y");
261     bool userw = props->hasValue("width");
262     bool userh = props->hasValue("height");
263
264     LayoutWidget wid(props);
265     int pw=0, ph=0;
266     if(!userw || !userh)
267         wid.calcPrefSize(&pw, &ph);
268     pw = props->getIntValue("width", pw);
269     ph = props->getIntValue("height", ph);
270     int px = props->getIntValue("x", (screenw - pw) / 2);
271     int py = props->getIntValue("y", (screenh - ph) / 2);
272     wid.layout(px, py, pw, ph);
273
274     _object = makeObject(props, screenw, screenh);
275
276     // Remove automatically generated properties, so the layout looks
277     // the same next time around.
278     if(!userx) props->removeChild("x");
279     if(!usery) props->removeChild("y");
280     if(!userw) props->removeChild("width");
281     if(!userh) props->removeChild("height");
282
283     if (_object != 0) {
284         _object->reveal();
285     } else {
286         SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
287                << props->getStringValue("name", "[unnamed]")
288                << " does not contain a proper GUI definition");
289     }
290 }
291
292 puObject *
293 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
294 {
295     bool presetSize = props->hasValue("width") && props->hasValue("height");
296     int width = props->getIntValue("width", parentWidth);
297     int height = props->getIntValue("height", parentHeight);
298     int x = props->getIntValue("x", (parentWidth - width) / 2);
299     int y = props->getIntValue("y", (parentHeight - height) / 2);
300
301     string type = props->getName();
302     if (type == "")
303         type = "dialog";
304
305     if (type == "dialog") {
306         puPopup * dialog;
307         if (props->getBoolValue("modal", false))
308             dialog = new puDialogBox(x, y);
309         else
310             dialog = new fgPopup(x, y);
311         setupGroup(dialog, props, width, height, true);
312         return dialog;
313     } else if (type == "group") {
314         puGroup * group = new puGroup(x, y);
315         setupGroup(group, props, width, height, false);
316         return group;
317     } else if (type == "list") {
318         puList * list = new puList(x, y, x + width, y + height);
319         setupObject(list, props);
320         return list;
321     } else if (type == "airport-list") {
322         AirportList * list = new AirportList(x, y, x + width, y + height);
323         setupObject(list, props);
324         return list;
325     } else if (type == "input") {
326         puInput * input = new puInput(x, y, x + width, y + height);
327         setupObject(input, props);
328         return input;
329     } else if (type == "text") {
330         puText * text = new puText(x, y);
331         setupObject(text, props);
332         // Layed-out objects need their size set, and non-layout ones
333         // get a different placement.
334         if(presetSize) text->setSize(width, height);
335         else text->setLabelPlace(PUPLACE_LABEL_DEFAULT);
336         return text;
337     } else if (type == "checkbox") {
338         puButton * b;
339         b = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
340         b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
341         setupObject(b, props);
342         return b;
343     } else if (type == "radio") {
344         puButton * b;
345         b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
346         b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
347         setupObject(b, props);
348         return b;
349     } else if (type == "button") {
350         puButton * b;
351         const char * legend = props->getStringValue("legend", "[none]");
352         if (props->getBoolValue("one-shot", true))
353             b = new puOneShot(x, y, legend);
354         else
355             b = new puButton(x, y, legend);
356         if(presetSize)
357             b->setSize(width, height);
358         setupObject(b, props);
359         return b;
360     } else if (type == "combo") {
361         vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
362         char ** entries = make_char_array(value_nodes.size());
363         for (unsigned int i = 0, j = value_nodes.size() - 1;
364              i < value_nodes.size();
365              i++, j--)
366             entries[i] = strdup((char *)value_nodes[i]->getStringValue());
367         puComboBox * combo =
368             new puComboBox(x, y, x + width, y + height, entries,
369                            props->getBoolValue("editable", false));
370         setupObject(combo, props);
371         return combo;
372     } else if (type == "slider") {
373         bool vertical = props->getBoolValue("vertical", false);
374         puSlider * slider = new puSlider(x, y, (vertical ? height : width));
375         slider->setMinValue(props->getFloatValue("min", 0.0));
376         slider->setMaxValue(props->getFloatValue("max", 1.0));
377         setupObject(slider, props);
378         if(presetSize)
379             slider->setSize(width, height);
380         return slider;
381     } else if (type == "dial") {
382         puDial * dial = new puDial(x, y, width);
383         dial->setMinValue(props->getFloatValue("min", 0.0));
384         dial->setMaxValue(props->getFloatValue("max", 1.0));
385         dial->setWrap(props->getBoolValue("wrap", true));
386         setupObject(dial, props);
387         return dial;
388     } else if (type == "textbox") {
389        int slider_width = props->getIntValue("slider", parentHeight);
390        if (slider_width==0) slider_width=20;
391        puLargeInput * puTextBox =
392                      new puLargeInput(x, y, x+width, x+height, 2, slider_width);
393        if  (props->hasValue("editable"))
394        {
395           if (props->getBoolValue("editable")==false)
396              puTextBox->disableInput();
397           else
398              puTextBox->enableInput();
399        }
400        setupObject(puTextBox,props);
401        return puTextBox; 
402     } else if (type == "select") {
403         vector<SGPropertyNode_ptr> value_nodes;
404         SGPropertyNode * selection_node =
405                 fgGetNode(props->getChild("selection")->getStringValue(), true);
406
407         for (int q = 0; q < selection_node->nChildren(); q++)
408             value_nodes.push_back(selection_node->getChild(q));
409
410         char ** entries = make_char_array(value_nodes.size());
411         for (unsigned int i = 0, j = value_nodes.size() - 1;
412              i < value_nodes.size();
413              i++, j--)
414             entries[i] = strdup((char *)value_nodes[i]->getName());
415         puSelectBox * select =
416             new puSelectBox(x, y, x + width, y + height, entries);
417         setupObject(select, props);
418         return select;
419     } else {
420         return 0;
421     }
422 }
423
424 void
425 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
426 {
427     object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
428
429     if (props->hasValue("legend"))
430         object->setLegend(props->getStringValue("legend"));
431
432     if (props->hasValue("label"))
433         object->setLabel(props->getStringValue("label"));
434
435     if (props->hasValue("property")) {
436         const char * name = props->getStringValue("name");
437         if (name == 0)
438             name = "";
439         const char * propname = props->getStringValue("property");
440         SGPropertyNode_ptr node = fgGetNode(propname, true);
441         copy_to_pui(node, object);
442         PropertyObject* po = new PropertyObject(name, object, node);
443         _propertyObjects.push_back(po);
444         if(props->getBoolValue("live"))
445             _liveObjects.push_back(po);
446     }
447
448     vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
449     if (nodes.size() > 0) {
450         GUIInfo * info = new GUIInfo(this);
451
452         for (unsigned int i = 0; i < nodes.size(); i++)
453             info->bindings.push_back(new FGBinding(nodes[i]));
454         object->setCallback(action_callback);
455         object->setUserData(info);
456         _info.push_back(info);
457     }
458
459     object->makeReturnDefault(props->getBoolValue("default"));
460 }
461
462 void
463 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
464                     int width, int height, bool makeFrame)
465 {
466     setupObject(group, props);
467
468     if (makeFrame) {
469         puFrame* f = new puFrame(0, 0, width, height);
470         f->setColorScheme(0.8, 0.8, 0.9, 0.85);
471     }
472
473     int nChildren = props->nChildren();
474     for (int i = 0; i < nChildren; i++)
475         makeObject(props->getChild(i), width, height);
476     group->close();
477 }
478
479 char **
480 FGDialog::make_char_array (int size)
481 {
482     char ** list = new char*[size+1];
483     for (int i = 0; i <= size; i++)
484         list[i] = 0;
485     _char_arrays.push_back(list);
486     return list;
487 }
488
489
490 \f
491 ////////////////////////////////////////////////////////////////////////
492 // Implementation of FGDialog::PropertyObject.
493 ////////////////////////////////////////////////////////////////////////
494
495 FGDialog::PropertyObject::PropertyObject (const char * n,
496                                            puObject * o,
497                                            SGPropertyNode_ptr p)
498     : name(n),
499       object(o),
500       node(p)
501 {
502 }
503
504
505 // end of dialog.cxx