1 // dialog.cxx: implementation of an XML-configurable dialog box.
3 #include <Input/input.hxx>
10 ////////////////////////////////////////////////////////////////////////
12 ////////////////////////////////////////////////////////////////////////
18 action_callback (puObject * object)
20 GUIInfo * info = (GUIInfo *)object->getUserData();
21 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
22 gui->setCurrentWidget(info->widget);
23 for (int i = 0; i < info->bindings.size(); i++)
24 info->bindings[i]->fire();
25 gui->setCurrentWidget(0);
30 ////////////////////////////////////////////////////////////////////////
31 // Static helper functions.
32 ////////////////////////////////////////////////////////////////////////
35 * Copy a property value to a PUI object.
38 copy_to_pui (SGPropertyNode * node, puObject * object)
40 switch (node->getType()) {
41 case SGPropertyNode::BOOL:
42 case SGPropertyNode::INT:
43 case SGPropertyNode::LONG:
44 object->setValue(node->getIntValue());
46 case SGPropertyNode::FLOAT:
47 case SGPropertyNode::DOUBLE:
48 object->setValue(node->getFloatValue());
51 object->setValue(node->getStringValue());
58 copy_from_pui (puObject * object, SGPropertyNode * node)
60 switch (node->getType()) {
61 case SGPropertyNode::BOOL:
62 case SGPropertyNode::INT:
63 case SGPropertyNode::LONG:
64 node->setIntValue(object->getIntegerValue());
66 case SGPropertyNode::FLOAT:
67 case SGPropertyNode::DOUBLE:
68 node->setFloatValue(object->getFloatValue());
71 node->setStringValue(object->getStringValue());
78 ////////////////////////////////////////////////////////////////////////
79 // Implementation of GUIInfo.
80 ////////////////////////////////////////////////////////////////////////
82 GUIInfo::GUIInfo (FGDialog * w)
89 for (int i = 0; i < bindings.size(); i++) {
97 ////////////////////////////////////////////////////////////////////////
98 // Implementation of FGDialog.
99 ////////////////////////////////////////////////////////////////////////
101 FGDialog::FGDialog (SGPropertyNode_ptr props)
107 FGDialog::~FGDialog ()
112 for (i = 0; i < _info.size(); i++) {
117 for (i = 0; i < _propertyObjects.size(); i++) {
118 delete _propertyObjects[i];
119 _propertyObjects[i] = 0;
124 FGDialog::updateValue (const char * objectName)
126 for (int i = 0; i < _propertyObjects.size(); i++) {
127 const string &name = _propertyObjects[i]->name;
128 if (name == objectName)
129 copy_to_pui(_propertyObjects[i]->node,
130 _propertyObjects[i]->object);
135 FGDialog::applyValue (const char * objectName)
137 for (int i = 0; i < _propertyObjects.size(); i++) {
138 if (_propertyObjects[i]->name == objectName)
139 copy_from_pui(_propertyObjects[i]->object,
140 _propertyObjects[i]->node);
145 FGDialog::updateValues ()
147 for (int i = 0; i < _propertyObjects.size(); i++)
148 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
152 FGDialog::applyValues ()
154 for (int i = 0; i < _propertyObjects.size(); i++)
155 copy_from_pui(_propertyObjects[i]->object,
156 _propertyObjects[i]->node);
160 FGDialog::display (SGPropertyNode_ptr props)
163 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
167 _object = makeObject(props, 1024, 768);
172 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
173 << props->getStringValue("name", "[unnamed]")
174 << " does not contain a proper GUI definition");
179 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
181 int width = props->getIntValue("width", parentWidth);
182 int height = props->getIntValue("height", parentHeight);
184 int x = props->getIntValue("x", (parentWidth - width) / 2);
185 int y = props->getIntValue("y", (parentHeight - height) / 2);
187 string type = props->getName();
189 type = props->getStringValue("type");
191 SG_LOG(SG_GENERAL, SG_ALERT, "No type specified for GUI object");
195 if (type == "dialog") {
197 if (props->getBoolValue("modal", false))
198 dialog = new puDialogBox(x, y);
200 dialog = new puPopup(x, y);
201 setupGroup(dialog, props, width, height, true);
203 } else if (type == "group") {
204 puGroup * group = new puGroup(x, y);
205 setupGroup(group, props, width, height, false);
207 } else if (type == "input") {
208 puInput * input = new puInput(x, y, x + width, y + height);
209 setupObject(input, props);
211 } else if (type == "text") {
212 puText * text = new puText(x, y);
213 setupObject(text, props);
215 } else if (type == "checkbox") {
217 b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
218 setupObject(b, props);
220 } else if (type == "button") {
222 const char * legend = props->getStringValue("legend", "[none]");
223 if (props->getBoolValue("one-shot", true))
224 b = new puOneShot(x, y, legend);
226 b = new puButton(x, y, legend);
227 setupObject(b, props);
235 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
237 if (props->hasValue("legend"))
238 object->setLegend(props->getStringValue("legend"));
240 if (props->hasValue("label"))
241 object->setLabel(props->getStringValue("label"));
243 if (props->hasValue("property")) {
244 const char * name = props->getStringValue("name");
247 const char * propname = props->getStringValue("property");
248 SGPropertyNode_ptr node = fgGetNode(propname, true);
249 copy_to_pui(node, object);
251 _propertyObjects.push_back(new PropertyObject(name, object, node));
254 vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
255 if (nodes.size() > 0) {
256 GUIInfo * info = new GUIInfo(this);
258 for (int i = 0; i < nodes.size(); i++)
259 info->bindings.push_back(new FGBinding(nodes[i]));
260 object->setCallback(action_callback);
261 object->setUserData(info);
262 _info.push_back(info);
265 object->makeReturnDefault(props->getBoolValue("default"));
269 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
270 int width, int height, bool makeFrame)
272 setupObject(group, props);
275 new puFrame(0, 0, width, height);
277 int nChildren = props->nChildren();
278 for (int i = 0; i < nChildren; i++)
279 makeObject(props->getChild(i), width, height);
285 ////////////////////////////////////////////////////////////////////////
286 // Implementation of FGDialog::PropertyObject.
287 ////////////////////////////////////////////////////////////////////////
289 FGDialog::PropertyObject::PropertyObject (const char * n,
291 SGPropertyNode_ptr p)