1 // dialog.cxx: implementation of an XML-configurable dialog box.
3 #include <Input/input.hxx>
9 #include "AirportList.hxx"
12 int fgPopup::checkHit(int button, int updown, int x, int y)
14 int result = puPopup::checkHit(button, updown, x, y);
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.
22 if(updown == PU_DOWN && !_dragging) {
26 int hit = getHitObjects(this, x, y);
27 if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
31 getPosition(&px, &py);
35 } else if(updown == PU_DRAG && _dragging) {
36 setPosition(x + _dX, y + _dY);
43 int fgPopup::getHitObjects(puObject *object, int x, int y)
46 if(object->getType() & PUCLASS_GROUP)
47 for (puObject *obj = ((puGroup *)object)->getFirstChild();
48 obj; obj = obj->getNextObject())
49 type |= getHitObjects(obj, x, y);
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();
61 ////////////////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////////////////
66 * User data for a GUI object.
70 GUIInfo (FGDialog * d);
74 vector <FGBinding *> bindings;
82 action_callback (puObject * object)
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)
93 gui->setActiveDialog(0);
98 ////////////////////////////////////////////////////////////////////////
99 // Static helper functions.
100 ////////////////////////////////////////////////////////////////////////
103 * Copy a property value to a PUI object.
106 copy_to_pui (SGPropertyNode * node, puObject * object)
108 // Treat puText objects specially, so their "values" can be set
110 if(object->getType() & PUCLASS_TEXT) {
111 object->setLabel(node->getStringValue());
115 switch (node->getType()) {
116 case SGPropertyNode::BOOL:
117 case SGPropertyNode::INT:
118 case SGPropertyNode::LONG:
119 object->setValue(node->getIntValue());
121 case SGPropertyNode::FLOAT:
122 case SGPropertyNode::DOUBLE:
123 object->setValue(node->getFloatValue());
126 object->setValue(node->getStringValue());
133 copy_from_pui (puObject * object, SGPropertyNode * node)
135 // puText objects are immutable, so should not be copied out
136 if(object->getType() & PUCLASS_TEXT)
139 switch (node->getType()) {
140 case SGPropertyNode::BOOL:
141 case SGPropertyNode::INT:
142 case SGPropertyNode::LONG:
143 node->setIntValue(object->getIntegerValue());
145 case SGPropertyNode::FLOAT:
146 case SGPropertyNode::DOUBLE:
147 node->setFloatValue(object->getFloatValue());
150 // Special case to handle lists, as getStringValue cannot be overridden
151 if(object->getType() & PUCLASS_LIST)
153 node->setStringValue(((puList *) object)->getListStringValue());
157 node->setStringValue(object->getStringValue());
165 ////////////////////////////////////////////////////////////////////////
166 // Implementation of GUIInfo.
167 ////////////////////////////////////////////////////////////////////////
169 GUIInfo::GUIInfo (FGDialog * d)
176 for (unsigned int i = 0; i < bindings.size(); i++) {
184 ////////////////////////////////////////////////////////////////////////
185 // Implementation of FGDialog.
186 ////////////////////////////////////////////////////////////////////////
188 FGDialog::FGDialog (SGPropertyNode * props)
194 FGDialog::~FGDialog ()
196 puDeleteObject(_object);
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];
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];
218 // Finally, delete the property links.
219 for (i = 0; i < _propertyObjects.size(); i++) {
220 delete _propertyObjects[i];
221 _propertyObjects[i] = 0;
226 FGDialog::updateValue (const char * objectName)
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);
237 FGDialog::applyValue (const char * objectName)
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);
247 FGDialog::updateValues ()
249 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
250 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
254 FGDialog::applyValues ()
256 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
257 copy_from_pui(_propertyObjects[i]->object,
258 _propertyObjects[i]->node);
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())
269 copy_to_pui(_liveObjects[i]->node, obj);
274 FGDialog::display (SGPropertyNode * props)
277 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
281 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
282 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
284 bool userx = props->hasValue("x");
285 bool usery = props->hasValue("y");
286 bool userw = props->hasValue("width");
287 bool userh = props->hasValue("height");
289 LayoutWidget wid(props);
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);
299 _object = makeObject(props, screenw, screenh);
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");
311 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
312 << props->getStringValue("name", "[unnamed]")
313 << " does not contain a proper GUI definition");
318 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
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);
326 string type = props->getName();
330 if (type == "dialog") {
332 if (props->getBoolValue("modal", false))
333 dialog = new puDialogBox(x, y);
335 dialog = new fgPopup(x, y);
336 setupGroup(dialog, props, width, height, true);
338 } else if (type == "group") {
339 puGroup * group = new puGroup(x, y);
340 setupGroup(group, props, width, height, false);
342 } else if (type == "list") {
343 puList * list = new puList(x, y, x + width, y + height);
344 setupObject(list, props);
346 } else if (type == "airport-list") {
347 AirportList * list = new AirportList(x, y, x + width, y + height);
348 setupObject(list, props);
350 } else if (type == "input") {
351 puInput * input = new puInput(x, y, x + width, y + height);
352 setupObject(input, props);
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);
362 } else if (type == "checkbox") {
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);
368 } else if (type == "radio") {
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);
374 } else if (type == "button") {
376 const char * legend = props->getStringValue("legend", "[none]");
377 if (props->getBoolValue("one-shot", true))
378 b = new puOneShot(x, y, legend);
380 b = new puButton(x, y, legend);
382 b->setSize(width, height);
383 setupObject(b, props);
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();
391 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
393 new puComboBox(x, y, x + width, y + height, entries,
394 props->getBoolValue("editable", false));
395 setupObject(combo, props);
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);
404 slider->setSize(width, height);
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);
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"))
420 if (props->getBoolValue("editable")==false)
421 puTextBox->disableInput();
423 puTextBox->enableInput();
425 setupObject(puTextBox,props);
427 } else if (type == "select") {
428 vector<SGPropertyNode_ptr> value_nodes;
429 SGPropertyNode * selection_node =
430 fgGetNode(props->getChild("selection")->getStringValue(), true);
432 for (int q = 0; q < selection_node->nChildren(); q++)
433 value_nodes.push_back(selection_node->getChild(q));
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();
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);
450 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
452 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
454 if (props->hasValue("legend"))
455 object->setLegend(props->getStringValue("legend"));
457 if (props->hasValue("label"))
458 object->setLabel(props->getStringValue("label"));
460 if (props->hasValue("property")) {
461 const char * name = props->getStringValue("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);
473 vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
474 if (nodes.size() > 0) {
475 GUIInfo * info = new GUIInfo(this);
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);
484 object->makeReturnDefault(props->getBoolValue("default"));
488 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
489 int width, int height, bool makeFrame)
491 setupObject(group, props);
494 puFrame* f = new puFrame(0, 0, width, height);
495 f->setColorScheme(0.8, 0.8, 0.9, 0.85);
498 int nChildren = props->nChildren();
499 for (int i = 0; i < nChildren; i++)
500 makeObject(props->getChild(i), width, height);
505 FGDialog::make_char_array (int size)
507 char ** list = new char*[size+1];
508 for (int i = 0; i <= size; i++)
510 _char_arrays.push_back(list);
516 ////////////////////////////////////////////////////////////////////////
517 // Implementation of FGDialog::PropertyObject.
518 ////////////////////////////////////////////////////////////////////////
520 FGDialog::PropertyObject::PropertyObject (const char * n,
522 SGPropertyNode_ptr p)