]> git.mxchange.org Git - flightgear.git/blob - src/GUI/FGPUIDialog.hxx
Clean-up: move autosave.xml loading code to proper method
[flightgear.git] / src / GUI / FGPUIDialog.hxx
1 // FGPUIDialog.hxx - XML-configured dialog box.
2
3 #ifndef FG_PUI_DIALOG_HXX
4 #define FG_PUI_DIALOG_HXX 1
5
6 #include "dialog.hxx"
7
8 #include <plib/puAux.h>
9
10 #include <simgear/props/props.hxx>
11 #include <simgear/misc/sg_path.hxx>
12 #include <simgear/props/condition.hxx>
13
14 #include <vector>
15
16
17 // ugly temporary workaround for plib's lack of user defined class ids  FIXME
18 #define FGCLASS_LIST          0x00000001
19 #define FGCLASS_AIRPORTLIST   0x00000002
20 #define FGCLASS_PROPERTYLIST  0x00000004
21 #define FGCLASS_WAYPOINTLIST  0x00000008
22
23 class GUI_ID { public: GUI_ID(int id) : id(id) {} virtual ~GUI_ID() {} int id; };
24
25
26
27 class NewGUI;
28 class FGColor;
29 class puObject;
30 class puFont;
31
32 /**
33  * An XML-configured dialog box.
34  *
35  * The GUI manager stores only the property tree for the dialog
36  * boxes.  This class creates a PUI dialog box on demand from
37  * the properties in that tree.  The manager recreates the dialog
38  * every time it needs to show it.
39  */
40 class FGPUIDialog : public FGDialog
41 {
42 public:
43
44     /**
45      * Construct a new GUI widget configured by a property tree.
46      *
47      * The configuration properties are not part of the main
48      * FlightGear property tree; the GUI manager reads them
49      * from individual configuration files.
50      *
51      * @param props A property tree describing the dialog.
52      */
53     FGPUIDialog (SGPropertyNode * props);
54
55
56     /**
57      * Destructor.
58      */
59     virtual ~FGPUIDialog ();
60
61
62     /**
63      * Update the values of all GUI objects with a specific name,
64      * or all if name is 0 (default).
65      *
66      * This method copies values from the FlightGear property tree to
67      * the GUI object(s).
68      *
69      * @param objectName The name of the GUI object(s) to update.
70      *        Use the empty name for all unnamed objects.
71      */
72     virtual void updateValues (const char * objectName = 0);
73
74
75     /**
76      * Apply the values of all GUI objects with a specific name,
77      * or all if name is 0 (default)
78      *
79      * This method copies values from the GUI object(s) to the
80      * FlightGear property tree.
81      *
82      * @param objectName The name of the GUI object(s) to update.
83      *        Use the empty name for all unnamed objects.
84      */
85     virtual void applyValues (const char * objectName = 0);
86
87
88     /**
89      * Update state.  Called on active dialogs before rendering.
90      */
91     virtual void update ();
92
93     /**
94      * Recompute the dialog's layout
95      */
96     void relayout();
97     
98     
99     void setNeedsLayout() {
100       _needsRelayout = true;
101     }
102 private:
103
104     enum {
105         BACKGROUND = 0x01,
106         FOREGROUND = 0x02,
107         HIGHLIGHT = 0x04,
108         LABEL = 0x08,
109         LEGEND = 0x10,
110         MISC = 0x20,
111         EDITFIELD = 0x40
112     };
113
114     // Show the dialog.
115     void display (SGPropertyNode * props);
116
117     // Build the dialog or a subobject of it.
118     puObject * makeObject (SGPropertyNode * props,
119                            int parentWidth, int parentHeight);
120
121     // Common configuration for all GUI objects.
122     void setupObject (puObject * object, SGPropertyNode * props);
123
124     // Common configuration for all GUI group objects.
125     void setupGroup (puGroup * group, SGPropertyNode * props,
126                      int width, int height, bool makeFrame = false);
127
128     // Set object colors: the "which" argument defines which color qualities
129     // (PUCOL_LABEL, etc.) should pick up the <color> property.
130     void setColor(puObject * object, SGPropertyNode * props, int which = 0);
131
132     // return key code number for keystring
133     int getKeyCode(const char *keystring);
134
135     /**
136      * Apply layout sizes to a tree of puObjects
137      */
138     void applySize(puObject *object);
139
140     // The top-level PUI object.
141     puObject * _object;
142
143     // The GUI subsystem.
144     NewGUI * _gui;
145
146     // The dialog font. Defaults to the global gui font, but can get
147     // overridden by a top level font definition.
148     puFont * _font;
149
150     // The source xml tree, so that we can pass data back, such as the
151     // last position.
152     SGPropertyNode_ptr _props;
153
154     bool _needsRelayout;
155
156     // Nasal module.
157     std::string _module;
158     SGPropertyNode_ptr _nasal_close;
159
160     // PUI provides no way for userdata to be deleted automatically
161     // with a GUI object, so we have to keep track of all the special
162     // data we allocated and then free it manually when the dialog
163     // closes.
164     std::vector<void *> _info;
165     struct PropertyObject {
166         PropertyObject (const char * name,
167                         puObject * object,
168                         SGPropertyNode_ptr node);
169         std::string name;
170         puObject * object;
171         SGPropertyNode_ptr node;
172     };
173     std::vector<PropertyObject *> _propertyObjects;
174     std::vector<PropertyObject *> _liveObjects;
175     
176     class ConditionalObject : public SGConditional
177     {
178     public:
179       ConditionalObject(const std::string& aName, puObject* aPu) :
180         _name(aName),
181         _pu(aPu)
182       { ; }
183     
184       void update(FGPUIDialog* aDlg);
185     
186     private:
187       const std::string _name;
188       puObject* _pu;      
189     };
190     
191     typedef SGSharedPtr<ConditionalObject> ConditionalObjectRef;
192     std::vector<ConditionalObjectRef> _conditionalObjects;
193 };
194
195 //
196 // Custom subclass of puPopup to implement "draggable" windows in the
197 // interface.  Note that this is a subclass of puPopup, not
198 // puDialogBox.  Sadly, PUI (mis)uses subclassing to implement a
199 // boolean property: modality.  That means that we can't implement
200 // dragging of both puPopup windows and puDialogBoxes with the same
201 // code.  Rather than duplicate code, I've chosen to implement only
202 // "non-modal dragability" here.  Modal dialog boxes (like the exit
203 // confirmation) are not draggable.
204 //
205 class fgPopup : public puPopup {
206 public:
207     fgPopup(int x, int y, bool r = true, bool d = true) :
208             puPopup(x, y), _draggable(d), _resizable(r), _dragging(false)
209     {}
210     int checkHit(int b, int up, int x, int y);
211     int checkKey(int key, int updown);
212     int getHitObjects(puObject *, int x, int y);
213     puObject *getKeyObject(puObject *, int key);
214     puObject *getActiveInputField(puObject *);
215     void applySize(puObject *);
216 private:
217     enum { LEFT = 1, RIGHT = 2, TOP = 4, BOTTOM = 8 };
218     bool _draggable;
219     bool _resizable;
220     bool _dragging;
221     int _resizing;
222     int _start_cursor;
223     int _cursor;
224     int _dlgX, _dlgY, _dlgW, _dlgH;
225     int _startX, _startY;
226 };
227
228
229 class fgValueList {
230 public:
231     fgValueList(SGPropertyNode *p);
232     virtual ~fgValueList();
233     virtual void update();
234
235 protected:
236     char **_list;
237
238 private:
239     void make_list();
240     void destroy_list();
241     SGPropertyNode_ptr _props;
242 };
243
244
245 class fgList : public fgValueList, public puaList, public GUI_ID {
246 public:
247     fgList(int x1, int y1, int x2, int y2, SGPropertyNode *p, int sw) :
248             fgValueList(p), puaList(x1, y1, x2, y2, _list, sw), GUI_ID(FGCLASS_LIST) {}
249     void update();
250 };
251
252 class fgComboBox : public fgValueList, public puaComboBox {
253 public:
254     fgComboBox(int x1, int y1, int x2, int y2, SGPropertyNode *p, bool editable) :
255         fgValueList(p), 
256         puaComboBox(x1, y1, x2, y2, _list, editable),
257         _inHit(false)
258       {}
259         
260     void update();
261     
262     virtual void setSize(int w, int h);
263     
264     virtual int checkHit(int b, int up, int x, int y);
265     
266     virtual void recalc_bbox();
267 private:
268     bool _inHit;
269 };
270
271 class fgSelectBox : public fgValueList, public puaSelectBox {
272 public:
273     fgSelectBox(int x1, int y1, int x2, int y2, SGPropertyNode *p) :
274         fgValueList(p), puaSelectBox(x1, y1, x2, y2, _list) {}
275 };
276
277 #endif // __DIALOG_HXX