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