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