1 // dialog.cxx: implementation of an XML-configurable dialog box.
3 #include <stdlib.h> // atof()
5 #include <Input/input.hxx>
11 #include "AirportList.hxx"
14 int fgPopup::checkHit(int button, int updown, int x, int y)
16 int result = puPopup::checkHit(button, updown, x, y);
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.
24 if(updown == PU_DOWN && !_dragging) {
28 int hit = getHitObjects(this, x, y);
29 if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
33 getPosition(&px, &py);
37 } else if(updown == PU_DRAG && _dragging) {
38 setPosition(x + _dX, y + _dY);
45 int fgPopup::getHitObjects(puObject *object, int x, int y)
48 if(object->getType() & PUCLASS_GROUP)
49 for (puObject *obj = ((puGroup *)object)->getFirstChild();
50 obj; obj = obj->getNextObject())
51 type |= getHitObjects(obj, x, y);
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();
63 ////////////////////////////////////////////////////////////////////////
65 ////////////////////////////////////////////////////////////////////////
68 * User data for a GUI object.
72 GUIInfo (FGDialog * d);
76 vector <FGBinding *> bindings;
84 action_callback (puObject * object)
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)
95 gui->setActiveDialog(0);
100 format_callback(puObject *obj, int dx, int dy, void *n)
102 SGPropertyNode *node = (SGPropertyNode *)n;
103 const char *format = node->getStringValue("format"), *f = format;
105 // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
116 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
118 while (*f && isdigit(*f))
122 while (*f && isdigit(*f))
145 const char *src = obj->getLabel();
148 float value = atof(src);
149 snprintf(buf, 256, format, value);
151 snprintf(buf, 256, format, src);
156 SGPropertyNode *result = node->getNode("formatted", true);
157 result->setStringValue(buf);
158 obj->setLabel(result->getStringValue());
163 ////////////////////////////////////////////////////////////////////////
164 // Static helper functions.
165 ////////////////////////////////////////////////////////////////////////
168 * Copy a property value to a PUI object.
171 copy_to_pui (SGPropertyNode * node, puObject * object)
173 // Treat puText objects specially, so their "values" can be set
175 if(object->getType() & PUCLASS_TEXT) {
176 object->setLabel(node->getStringValue());
180 switch (node->getType()) {
181 case SGPropertyNode::BOOL:
182 case SGPropertyNode::INT:
183 case SGPropertyNode::LONG:
184 object->setValue(node->getIntValue());
186 case SGPropertyNode::FLOAT:
187 case SGPropertyNode::DOUBLE:
188 object->setValue(node->getFloatValue());
191 object->setValue(node->getStringValue());
198 copy_from_pui (puObject * object, SGPropertyNode * node)
200 // puText objects are immutable, so should not be copied out
201 if(object->getType() & PUCLASS_TEXT)
204 switch (node->getType()) {
205 case SGPropertyNode::BOOL:
206 case SGPropertyNode::INT:
207 case SGPropertyNode::LONG:
208 node->setIntValue(object->getIntegerValue());
210 case SGPropertyNode::FLOAT:
211 case SGPropertyNode::DOUBLE:
212 node->setFloatValue(object->getFloatValue());
215 // Special case to handle lists, as getStringValue cannot be overridden
216 if(object->getType() & PUCLASS_LIST)
218 node->setStringValue(((puList *) object)->getListStringValue());
222 node->setStringValue(object->getStringValue());
230 ////////////////////////////////////////////////////////////////////////
231 // Implementation of GUIInfo.
232 ////////////////////////////////////////////////////////////////////////
234 GUIInfo::GUIInfo (FGDialog * d)
241 for (unsigned int i = 0; i < bindings.size(); i++) {
249 ////////////////////////////////////////////////////////////////////////
250 // Implementation of FGDialog.
251 ////////////////////////////////////////////////////////////////////////
253 FGDialog::FGDialog (SGPropertyNode * props)
259 FGDialog::~FGDialog ()
261 puDeleteObject(_object);
265 // Delete all the arrays we made
266 // and were forced to keep around
267 // because PUI won't do its own
268 // memory management.
269 for (i = 0; i < _char_arrays.size(); i++) {
270 for (int j = 0; _char_arrays[i][j] != 0; j++)
271 free(_char_arrays[i][j]); // added with strdup
272 delete[] _char_arrays[i];
275 // Delete all the info objects we
276 // were forced to keep around because
277 // PUI cannot delete its own user data.
278 for (i = 0; i < _info.size(); i++) {
279 delete (GUIInfo *)_info[i];
283 // Finally, delete the property links.
284 for (i = 0; i < _propertyObjects.size(); i++) {
285 delete _propertyObjects[i];
286 _propertyObjects[i] = 0;
291 FGDialog::updateValue (const char * objectName)
293 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
294 const string &name = _propertyObjects[i]->name;
295 if (name == objectName)
296 copy_to_pui(_propertyObjects[i]->node,
297 _propertyObjects[i]->object);
302 FGDialog::applyValue (const char * objectName)
304 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
305 if (_propertyObjects[i]->name == objectName)
306 copy_from_pui(_propertyObjects[i]->object,
307 _propertyObjects[i]->node);
312 FGDialog::updateValues ()
314 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
315 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
319 FGDialog::applyValues ()
321 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
322 copy_from_pui(_propertyObjects[i]->object,
323 _propertyObjects[i]->node);
329 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
330 puObject *obj = _liveObjects[i]->object;
331 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
334 copy_to_pui(_liveObjects[i]->node, obj);
339 FGDialog::display (SGPropertyNode * props)
342 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
346 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
347 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
349 bool userx = props->hasValue("x");
350 bool usery = props->hasValue("y");
351 bool userw = props->hasValue("width");
352 bool userh = props->hasValue("height");
354 LayoutWidget wid(props);
357 wid.calcPrefSize(&pw, &ph);
358 pw = props->getIntValue("width", pw);
359 ph = props->getIntValue("height", ph);
360 int px = props->getIntValue("x", (screenw - pw) / 2);
361 int py = props->getIntValue("y", (screenh - ph) / 2);
362 wid.layout(px, py, pw, ph);
364 _object = makeObject(props, screenw, screenh);
366 // Remove automatically generated properties, so the layout looks
367 // the same next time around.
368 if(!userx) props->removeChild("x");
369 if(!usery) props->removeChild("y");
370 if(!userw) props->removeChild("width");
371 if(!userh) props->removeChild("height");
376 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
377 << props->getStringValue("name", "[unnamed]")
378 << " does not contain a proper GUI definition");
383 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
385 bool presetSize = props->hasValue("width") && props->hasValue("height");
386 int width = props->getIntValue("width", parentWidth);
387 int height = props->getIntValue("height", parentHeight);
388 int x = props->getIntValue("x", (parentWidth - width) / 2);
389 int y = props->getIntValue("y", (parentHeight - height) / 2);
391 string type = props->getName();
395 if (type == "dialog") {
397 if (props->getBoolValue("modal", false))
398 dialog = new puDialogBox(x, y);
400 dialog = new fgPopup(x, y);
401 setupGroup(dialog, props, width, height, true);
403 } else if (type == "group") {
404 puGroup * group = new puGroup(x, y);
405 setupGroup(group, props, width, height, false);
407 } else if (type == "list") {
408 puList * list = new puList(x, y, x + width, y + height);
409 setupObject(list, props);
411 } else if (type == "airport-list") {
412 AirportList * list = new AirportList(x, y, x + width, y + height);
413 setupObject(list, props);
415 } else if (type == "input") {
416 puInput * input = new puInput(x, y, x + width, y + height);
417 setupObject(input, props);
419 } else if (type == "text") {
420 puText * text = new puText(x, y);
421 setupObject(text, props);
423 if (props->getNode("format"))
424 text->setRenderCallback(format_callback, props);
425 // Layed-out objects need their size set, and non-layout ones
426 // get a different placement.
427 if(presetSize) text->setSize(width, height);
428 else text->setLabelPlace(PUPLACE_LABEL_DEFAULT);
430 } else if (type == "checkbox") {
432 b = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
433 b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
434 setupObject(b, props);
436 } else if (type == "radio") {
438 b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
439 b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
440 setupObject(b, props);
442 } else if (type == "button") {
444 const char * legend = props->getStringValue("legend", "[none]");
445 if (props->getBoolValue("one-shot", true))
446 b = new puOneShot(x, y, legend);
448 b = new puButton(x, y, legend);
450 b->setSize(width, height);
451 setupObject(b, props);
453 } else if (type == "combo") {
454 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
455 char ** entries = make_char_array(value_nodes.size());
456 for (unsigned int i = 0, j = value_nodes.size() - 1;
457 i < value_nodes.size();
459 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
461 new puComboBox(x, y, x + width, y + height, entries,
462 props->getBoolValue("editable", false));
463 setupObject(combo, props);
465 } else if (type == "slider") {
466 bool vertical = props->getBoolValue("vertical", false);
467 puSlider * slider = new puSlider(x, y, (vertical ? height : width));
468 slider->setMinValue(props->getFloatValue("min", 0.0));
469 slider->setMaxValue(props->getFloatValue("max", 1.0));
470 setupObject(slider, props);
472 slider->setSize(width, height);
474 } else if (type == "dial") {
475 puDial * dial = new puDial(x, y, width);
476 dial->setMinValue(props->getFloatValue("min", 0.0));
477 dial->setMaxValue(props->getFloatValue("max", 1.0));
478 dial->setWrap(props->getBoolValue("wrap", true));
479 setupObject(dial, props);
481 } else if (type == "textbox") {
482 int slider_width = props->getIntValue("slider", parentHeight);
483 if (slider_width==0) slider_width=20;
484 puLargeInput * puTextBox =
485 new puLargeInput(x, y, x+width, x+height, 2, slider_width);
486 if (props->hasValue("editable"))
488 if (props->getBoolValue("editable")==false)
489 puTextBox->disableInput();
491 puTextBox->enableInput();
493 setupObject(puTextBox,props);
495 } else if (type == "select") {
496 vector<SGPropertyNode_ptr> value_nodes;
497 SGPropertyNode * selection_node =
498 fgGetNode(props->getChild("selection")->getStringValue(), true);
500 for (int q = 0; q < selection_node->nChildren(); q++)
501 value_nodes.push_back(selection_node->getChild(q));
503 char ** entries = make_char_array(value_nodes.size());
504 for (unsigned int i = 0, j = value_nodes.size() - 1;
505 i < value_nodes.size();
507 entries[i] = strdup((char *)value_nodes[i]->getName());
508 puSelectBox * select =
509 new puSelectBox(x, y, x + width, y + height, entries);
510 setupObject(select, props);
518 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
520 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
522 if (props->hasValue("legend"))
523 object->setLegend(props->getStringValue("legend"));
525 if (props->hasValue("label"))
526 object->setLabel(props->getStringValue("label"));
528 if (props->hasValue("property")) {
529 const char * name = props->getStringValue("name");
532 const char * propname = props->getStringValue("property");
533 SGPropertyNode_ptr node = fgGetNode(propname, true);
534 copy_to_pui(node, object);
535 PropertyObject* po = new PropertyObject(name, object, node);
536 _propertyObjects.push_back(po);
537 if(props->getBoolValue("live"))
538 _liveObjects.push_back(po);
541 vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
542 if (nodes.size() > 0) {
543 GUIInfo * info = new GUIInfo(this);
545 for (unsigned int i = 0; i < nodes.size(); i++)
546 info->bindings.push_back(new FGBinding(nodes[i]));
547 object->setCallback(action_callback);
548 object->setUserData(info);
549 _info.push_back(info);
552 object->makeReturnDefault(props->getBoolValue("default"));
556 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
557 int width, int height, bool makeFrame)
559 setupObject(group, props);
562 puFrame* f = new puFrame(0, 0, width, height);
563 f->setColorScheme(0.8, 0.8, 0.9, 0.85);
566 int nChildren = props->nChildren();
567 for (int i = 0; i < nChildren; i++)
568 makeObject(props->getChild(i), width, height);
573 FGDialog::make_char_array (int size)
575 char ** list = new char*[size+1];
576 for (int i = 0; i <= size; i++)
578 _char_arrays.push_back(list);
584 ////////////////////////////////////////////////////////////////////////
585 // Implementation of FGDialog::PropertyObject.
586 ////////////////////////////////////////////////////////////////////////
588 FGDialog::PropertyObject::PropertyObject (const char * n,
590 SGPropertyNode_ptr p)