]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.cxx
Simplify looking up the GUI subsystem.
[flightgear.git] / src / GUI / dialog.cxx
1 // dialog.cxx: implementation of an XML-configurable dialog box.
2
3 #include <Input/input.hxx>
4
5 #include "dialog.hxx"
6 #include "new_gui.hxx"
7
8
9 \f
10 ////////////////////////////////////////////////////////////////////////
11 // Callbacks.
12 ////////////////////////////////////////////////////////////////////////
13
14 /**
15  * Action callback.
16  */
17 static void
18 action_callback (puObject * object)
19 {
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);
26 }
27
28
29 \f
30 ////////////////////////////////////////////////////////////////////////
31 // Implementation of GUIInfo.
32 ////////////////////////////////////////////////////////////////////////
33
34 GUIInfo::GUIInfo (FGDialog * w)
35     : widget(w)
36 {
37 }
38
39 GUIInfo::~GUIInfo ()
40 {
41     for (int i = 0; i < bindings.size(); i++) {
42         delete bindings[i];
43         bindings[i] = 0;
44     }
45 }
46
47
48 \f
49 ////////////////////////////////////////////////////////////////////////
50 // Implementation of FGDialog.
51 ////////////////////////////////////////////////////////////////////////
52
53 FGDialog::FGDialog (SGPropertyNode_ptr props)
54     : _object(0)
55 {
56     display(props);
57 }
58
59 FGDialog::~FGDialog ()
60 {
61     delete _object;
62
63     int i;
64     for (i = 0; i < _info.size(); i++) {
65         delete _info[i];
66         _info[i] = 0;
67     }
68
69     for (i = 0; i < _propertyObjects.size(); i++) {
70         delete _propertyObjects[i];
71         _propertyObjects[i] = 0;
72     }
73 }
74
75 void
76 FGDialog::updateValue (const char * objectName)
77 {
78     for (int i = 0; i < _propertyObjects.size(); i++) {
79         if (_propertyObjects[i]->name == objectName)
80             _propertyObjects[i]->object
81                 ->setValue(_propertyObjects[i]->node->getStringValue());
82     }
83 }
84
85 void
86 FGDialog::applyValue (const char * objectName)
87 {
88     for (int i = 0; i < _propertyObjects.size(); i++) {
89         if (_propertyObjects[i]->name == objectName)
90             _propertyObjects[i]->node
91                 ->setStringValue(_propertyObjects[i]
92                                  ->object->getStringValue());
93     }
94 }
95
96 void
97 FGDialog::updateValues ()
98 {
99     for (int i = 0; i < _propertyObjects.size(); i++) {
100         puObject * object = _propertyObjects[i]->object;
101         SGPropertyNode_ptr node = _propertyObjects[i]->node;
102         object->setValue(node->getStringValue());
103     }
104 }
105
106 void
107 FGDialog::applyValues ()
108 {
109     for (int i = 0; i < _propertyObjects.size(); i++) {
110         puObject * object = _propertyObjects[i]->object;
111         SGPropertyNode_ptr node = _propertyObjects[i]->node;
112         node->setStringValue(object->getStringValue());
113     }
114 }
115
116 void
117 FGDialog::display (SGPropertyNode_ptr props)
118 {
119     if (_object != 0) {
120         SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
121         return;
122     }
123
124     _object = makeObject(props, 1024, 768);
125
126     if (_object != 0) {
127         _object->reveal();
128     } else {
129         SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
130                << props->getStringValue("name", "[unnamed]")
131                << " does not contain a proper GUI definition");
132     }
133 }
134
135 puObject *
136 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
137 {
138     int width = props->getIntValue("width", parentWidth);
139     int height = props->getIntValue("height", parentHeight);
140
141     int x = props->getIntValue("x", (parentWidth - width) / 2);
142     int y = props->getIntValue("y", (parentHeight - height) / 2);
143
144     string type = props->getName();
145     if (type == "")
146         type = props->getStringValue("type");
147     if (type == "") {
148         SG_LOG(SG_GENERAL, SG_ALERT, "No type specified for GUI object");
149         return 0;
150     }
151
152     if (type == "dialog") {
153         puPopup * dialog;
154         if (props->getBoolValue("modal", false))
155             dialog = new puDialogBox(x, y);
156         else
157             dialog = new puPopup(x, y);
158         setupGroup(dialog, props, width, height, true);
159         return dialog;
160     } else if (type == "group") {
161         puGroup * group = new puGroup(x, y);
162         setupGroup(group, props, width, height, false);
163         return group;
164     } else if (type == "input") {
165         puInput * input = new puInput(x, y, x + width, y + height);
166         setupObject(input, props);
167         return input;
168     } else if (type == "text") {
169         puText * text = new puText(x, y);
170         setupObject(text, props);
171         return text;
172     } else if (type == "button") {
173         puButton * b;
174         const char * legend = props->getStringValue("legend", "[none]");
175         if (props->getBoolValue("one-shot", true))
176             b = new puOneShot(x, y, legend);
177         else
178             b = new puButton(x, y, legend);
179         setupObject(b, props);
180         return b;
181     } else {
182         return 0;
183     }
184 }
185
186 void
187 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
188 {
189     if (props->hasValue("legend"))
190         object->setLegend(props->getStringValue("legend"));
191
192     if (props->hasValue("label"))
193         object->setLabel(props->getStringValue("label"));
194
195     if (props->hasValue("property")) {
196         const char * name = props->getStringValue("name");
197         if (name == 0)
198             name = "";
199         const char * propname = props->getStringValue("property");
200         SGPropertyNode_ptr node = fgGetNode(propname, true);
201         object->setValue(node->getStringValue());
202         if (name != 0)
203             _propertyObjects.push_back(new PropertyObject(name, object, node));
204     }
205
206     vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
207     if (nodes.size() > 0) {
208         GUIInfo * info = new GUIInfo(this);
209
210         for (int i = 0; i < nodes.size(); i++)
211             info->bindings.push_back(new FGBinding(nodes[i]));
212         object->setCallback(action_callback);
213         object->setUserData(info);
214         _info.push_back(info);
215     }
216
217     object->makeReturnDefault(props->getBoolValue("default"));
218 }
219
220 void
221 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
222                     int width, int height, bool makeFrame)
223 {
224     setupObject(group, props);
225
226     if (makeFrame)
227         new puFrame(0, 0, width, height);
228
229     int nChildren = props->nChildren();
230     for (int i = 0; i < nChildren; i++)
231         makeObject(props->getChild(i), width, height);
232     group->close();
233 }
234
235
236 \f
237 ////////////////////////////////////////////////////////////////////////
238 // Implementation of FGDialog::PropertyObject.
239 ////////////////////////////////////////////////////////////////////////
240
241 FGDialog::PropertyObject::PropertyObject (const char * n,
242                                            puObject * o,
243                                            SGPropertyNode_ptr p)
244     : name(n),
245       object(o),
246       node(p)
247 {
248 }
249
250
251 // end of dialog.cxx