]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.cxx
Melchior: Make line wrapping in textboxes configurable, and enable it by default
[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     sgVec4 color = {0.8, 0.8, 0.9, 0.85};
394     SGPropertyNode *ncs = props->getNode("color", false);
395     if ( ncs ) {
396        color[0] = ncs->getFloatValue("red", 0.8);
397        color[1] = ncs->getFloatValue("green", 0.8);
398        color[2] = ncs->getFloatValue("blue", 0.9);
399        color[3] = ncs->getFloatValue("alpha", 0.85);
400     }
401
402     string type = props->getName();
403     if (type == "")
404         type = "dialog";
405
406     if (type == "dialog") {
407         puPopup * dialog;
408         if (props->getBoolValue("modal", false))
409             dialog = new puDialogBox(x, y);
410         else
411             dialog = new fgPopup(x, y);
412         setupGroup(dialog, props, width, height, color, true);
413         return dialog;
414     } else if (type == "group") {
415         puGroup * group = new puGroup(x, y);
416         setupGroup(group, props, width, height, color, false);
417         return group;
418     } else if (type == "list") {
419         puList * list = new puList(x, y, x + width, y + height);
420         setupObject(list, props);
421         return list;
422     } else if (type == "airport-list") {
423         AirportList * list = new AirportList(x, y, x + width, y + height);
424         setupObject(list, props);
425         return list;
426     } else if (type == "input") {
427         puInput * input = new puInput(x, y, x + width, y + height);
428         setupObject(input, props);
429         return input;
430     } else if (type == "text") {
431         puText * text = new puText(x, y);
432         setupObject(text, props);
433
434         if (props->getNode("format")) {
435             SGPropertyNode *live = props->getNode("live");
436             if (live && live->getBoolValue())
437                 text->setRenderCallback(format_callback, props);
438             else
439                 format_callback(text, x, y, props);
440         }
441         // Layed-out objects need their size set, and non-layout ones
442         // get a different placement.
443         if(presetSize) text->setSize(width, height);
444         else text->setLabelPlace(PUPLACE_LABEL_DEFAULT);
445         return text;
446     } else if (type == "checkbox") {
447         puButton * b;
448         b = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
449         b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
450         setupObject(b, props);
451         return b;
452     } else if (type == "radio") {
453         puButton * b;
454         b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
455         b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
456         setupObject(b, props);
457         return b;
458     } else if (type == "button") {
459         puButton * b;
460         const char * legend = props->getStringValue("legend", "[none]");
461         if (props->getBoolValue("one-shot", true))
462             b = new puOneShot(x, y, legend);
463         else
464             b = new puButton(x, y, legend);
465         if(presetSize)
466             b->setSize(width, height);
467         setupObject(b, props);
468         return b;
469     } else if (type == "combo") {
470         vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
471         char ** entries = make_char_array(value_nodes.size());
472         for (unsigned int i = 0, j = value_nodes.size() - 1;
473              i < value_nodes.size();
474              i++, j--)
475             entries[i] = strdup((char *)value_nodes[i]->getStringValue());
476         puComboBox * combo =
477             new puComboBox(x, y, x + width, y + height, entries,
478                            props->getBoolValue("editable", false));
479         setupObject(combo, props);
480         return combo;
481     } else if (type == "slider") {
482         bool vertical = props->getBoolValue("vertical", false);
483         puSlider * slider = new puSlider(x, y, (vertical ? height : width));
484         slider->setMinValue(props->getFloatValue("min", 0.0));
485         slider->setMaxValue(props->getFloatValue("max", 1.0));
486         setupObject(slider, props);
487         if(presetSize)
488             slider->setSize(width, height);
489         return slider;
490     } else if (type == "dial") {
491         puDial * dial = new puDial(x, y, width);
492         dial->setMinValue(props->getFloatValue("min", 0.0));
493         dial->setMaxValue(props->getFloatValue("max", 1.0));
494         dial->setWrap(props->getBoolValue("wrap", true));
495         setupObject(dial, props);
496         return dial;
497     } else if (type == "textbox") {
498        int slider_width = props->getIntValue("slider", parentHeight);
499        int wrap = props->getBoolValue("wrap", true)==true;
500        if (slider_width==0) slider_width=20;
501        puLargeInput * puTextBox =
502                      new puLargeInput(x, y, x+width, x+height, 2, slider_width, wrap);
503        if  (props->hasValue("editable"))
504        {
505           if (props->getBoolValue("editable")==false)
506              puTextBox->disableInput();
507           else
508              puTextBox->enableInput();
509        }
510        setupObject(puTextBox,props);
511        return puTextBox;
512     } else if (type == "select") {
513         vector<SGPropertyNode_ptr> value_nodes;
514         SGPropertyNode * selection_node =
515                 fgGetNode(props->getChild("selection")->getStringValue(), true);
516
517         for (int q = 0; q < selection_node->nChildren(); q++)
518             value_nodes.push_back(selection_node->getChild(q));
519
520         char ** entries = make_char_array(value_nodes.size());
521         for (unsigned int i = 0, j = value_nodes.size() - 1;
522              i < value_nodes.size();
523              i++, j--)
524             entries[i] = strdup((char *)value_nodes[i]->getName());
525         puSelectBox * select =
526             new puSelectBox(x, y, x + width, y + height, entries);
527         setupObject(select, props);
528         return select;
529     } else {
530         return 0;
531     }
532 }
533
534 void
535 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
536 {
537     object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
538
539     if (props->hasValue("legend"))
540         object->setLegend(props->getStringValue("legend"));
541
542     if (props->hasValue("label"))
543         object->setLabel(props->getStringValue("label"));
544
545     if (props->hasValue("property")) {
546         const char * name = props->getStringValue("name");
547         if (name == 0)
548             name = "";
549         const char * propname = props->getStringValue("property");
550         SGPropertyNode_ptr node = fgGetNode(propname, true);
551         copy_to_pui(node, object);
552         PropertyObject* po = new PropertyObject(name, object, node);
553         _propertyObjects.push_back(po);
554         if(props->getBoolValue("live"))
555             _liveObjects.push_back(po);
556     }
557
558     vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
559     if (nodes.size() > 0) {
560         GUIInfo * info = new GUIInfo(this);
561
562         for (unsigned int i = 0; i < nodes.size(); i++)
563             info->bindings.push_back(new FGBinding(nodes[i]));
564         object->setCallback(action_callback);
565         object->setUserData(info);
566         _info.push_back(info);
567     }
568
569     object->makeReturnDefault(props->getBoolValue("default"));
570 }
571
572 void
573 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
574                     int width, int height, sgVec4 color, bool makeFrame)
575 {
576     setupObject(group, props);
577
578     if (makeFrame) {
579         puFrame* f = new puFrame(0, 0, width, height);
580         f->setColorScheme(color[0], color[1], color[2], color[3]);
581     }
582
583     int nChildren = props->nChildren();
584     for (int i = 0; i < nChildren; i++)
585         makeObject(props->getChild(i), width, height);
586     group->close();
587 }
588
589 char **
590 FGDialog::make_char_array (int size)
591 {
592     char ** list = new char*[size+1];
593     for (int i = 0; i <= size; i++)
594         list[i] = 0;
595     _char_arrays.push_back(list);
596     return list;
597 }
598
599
600 \f
601 ////////////////////////////////////////////////////////////////////////
602 // Implementation of FGDialog::PropertyObject.
603 ////////////////////////////////////////////////////////////////////////
604
605 FGDialog::PropertyObject::PropertyObject (const char * n,
606                                            puObject * o,
607                                            SGPropertyNode_ptr p)
608     : name(n),
609       object(o),
610       node(p)
611 {
612 }
613
614
615 // end of dialog.cxx