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