]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.hxx
- merge FGDialog::{update,apply}Value{,s} ... there's really no need to have
[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/pu.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 SG_USING_STD(vector);
19
20 class FGDialog;
21 class FGBinding;
22 class NewGUI;
23 class FGColor;
24
25
26 /**
27  * An XML-configured dialog box.
28  *
29  * The GUI manager stores only the property tree for the dialog
30  * boxes.  This class creates a PUI dialog box on demand from
31  * the properties in that tree.  The manager recreates the dialog
32  * every time it needs to show it.
33  */
34 class FGDialog
35 {
36 public:
37
38     /**
39      * Construct a new GUI widget configured by a property tree.
40      *
41      * The configuration properties are not part of the main
42      * FlightGear property tree; the GUI manager reads them
43      * from individual configuration files.
44      *
45      * @param props A property tree describing the dialog.
46      */
47     FGDialog (SGPropertyNode * props);
48
49
50     /**
51      * Destructor.
52      */
53     virtual ~FGDialog ();
54
55
56     /**
57      * Update the values of all GUI objects with a specific name,
58      * or all if name is 0 (default).
59      *
60      * This method copies values from the FlightGear property tree to
61      * the GUI object(s).
62      *
63      * @param objectName The name of the GUI object(s) to update.
64      *        Use the empty name for all unnamed objects.
65      */
66     virtual void updateValues (const char * objectName = 0);
67
68
69     /**
70      * Apply the values of all GUI objects with a specific name,
71      * or all if name is 0 (default)
72      *
73      * This method copies values from the GUI object(s) to the
74      * FlightGear property tree.
75      *
76      * @param objectName The name of the GUI object(s) to update.
77      *        Use the empty name for all unnamed objects.
78      */
79     virtual void applyValues (const char * objectName = 0);
80
81
82     /**
83      * Update state.  Called on active dialogs before rendering.
84      */
85     virtual void update ();
86
87 private:
88
89     enum {
90         BACKGROUND = 0x01,
91         FOREGROUND = 0x02,
92         HIGHLIGHT = 0x04,
93         LABEL = 0x08,
94         LEGEND = 0x10,
95         MISC = 0x20,
96         EDITFIELD = 0x40
97     };
98
99     // Private copy constructor to avoid unpleasant surprises.
100     FGDialog (const FGDialog &);
101
102     // Show the dialog.
103     void display (SGPropertyNode * props);
104
105     // Build the dialog or a subobject of it.
106     puObject * makeObject (SGPropertyNode * props,
107                            int parentWidth, int parentHeight);
108
109     // Common configuration for all GUI objects.
110     void setupObject (puObject * object, SGPropertyNode * props);
111
112     // Common configuration for all GUI group objects.
113     void setupGroup (puGroup * group, SGPropertyNode * props,
114                      int width, int height, bool makeFrame = false);
115
116     // Set object colors: the "which" argument defines which color qualities
117     // (PUCOL_LABEL, etc.) should pick up the <color> property.
118     void setColor(puObject * object, SGPropertyNode * props, int which = 0);
119
120     // return key code number for keystring
121     int getKeyCode(const char *keystring);
122
123     // The top-level PUI object.
124     puObject * _object;
125
126     // The GUI subsystem.
127     NewGUI * _gui;
128
129     // The dialog font. Defaults to the global gui font, but can get
130     // overridden by a top level font definition.
131     puFont * _font;
132
133     // The source xml tree, so that we can pass data back, such as the
134     // last position.
135     SGPropertyNode_ptr _props;
136
137     // Nasal module.
138     string _module;
139     SGPropertyNode_ptr _nasal_close;
140
141     // PUI provides no way for userdata to be deleted automatically
142     // with a GUI object, so we have to keep track of all the special
143     // data we allocated and then free it manually when the dialog
144     // closes.
145     vector<void *> _info;
146     struct PropertyObject {
147         PropertyObject (const char * name,
148                         puObject * object,
149                         SGPropertyNode_ptr node);
150         string name;
151         puObject * object;
152         SGPropertyNode_ptr node;
153     };
154     vector<PropertyObject *> _propertyObjects;
155     vector<PropertyObject *> _liveObjects;
156
157     // PUI doesn't copy arrays, so we have to allocate string arrays
158     // and then keep pointers so that we can delete them when the
159     // dialog closes. value_list() builds such a list from "value"
160     // children.
161     char ** make_char_array (int size);
162     char ** value_list(const SGPropertyNode * prop);
163     void destroy_char_array (char **array);
164     vector<char **> _char_arrays;
165 };
166
167 //
168 // Custom subclass of puPopup to implement "draggable" windows in the
169 // interface.  Note that this is a subclass of puPopup, not
170 // puDialogBox.  Sadly, PUI (mis)uses subclassing to implement a
171 // boolean property: modality.  That means that we can't implement
172 // dragging of both puPopup windows and puDialogBoxes with the same
173 // code.  Rather than duplicate code, I've chosen to implement only
174 // "non-modal dragability" here.  Modal dialog boxes (like the exit
175 // confirmation) are not draggable.
176 //
177 class fgPopup : public puPopup {
178 public:
179     fgPopup(int x, int y, bool d = true) : puPopup(x, y) { _dragging = false; _draggable = d;}
180     int checkHit(int b, int up, int x, int y);
181     int checkKey(int key, int updown);
182     int getHitObjects(puObject *, int x, int y);
183     puObject *getKeyObject(puObject *, int key);
184     puObject *getActiveInputField(puObject *);
185 private:
186     bool _draggable;
187     bool _dragging;
188     int _dX, _dY;
189 };
190
191 #endif // __DIALOG_HXX