]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.hxx
- abstract out reading colors from the property tree into a routine.
[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
23
24 /**
25  * An XML-configured dialog box.
26  *
27  * The GUI manager stores only the property tree for the dialog
28  * boxes.  This class creates a PUI dialog box on demand from
29  * the properties in that tree.  The manager recreates the dialog
30  * every time it needs to show it.
31  */
32 class FGDialog
33 {
34 public:
35
36     /**
37      * Construct a new GUI widget configured by a property tree.
38      *
39      * The configuration properties are not part of the main
40      * FlightGear property tree; the GUI manager reads them
41      * from individual configuration files.
42      *
43      * @param props A property tree describing the dialog.
44      */
45     FGDialog (SGPropertyNode * props);
46
47
48     /**
49      * Destructor.
50      */
51     virtual ~FGDialog ();
52
53
54     /**
55      * Update the values of all GUI objects with a specific name.
56      *
57      * This method copies values from the FlightGear property tree to
58      * the GUI object(s).
59      *
60      * @param objectName The name of the GUI object(s) to update.
61      *        Use the empty name for all unnamed objects.
62      */
63     virtual void updateValue (const char * objectName);
64
65
66     /**
67      * Apply the values of all GUI objects with a specific name.
68      *
69      * This method copies values from the GUI object(s) to the
70      * FlightGear property tree.
71      *
72      * @param objectName The name of the GUI object(s) to update.
73      *        Use the empty name for all unnamed objects.
74      */
75     virtual void applyValue (const char * objectName);
76
77
78     /**
79      * Update the values of all GUI objects.
80      *
81      * This method copies values from the FlightGear property tree to
82      * the GUI objects.
83      */
84     virtual void updateValues ();
85
86
87     /**
88      * Apply the values of all GUI objects.
89      *
90      * This method copies from the GUI objects to the FlightGear
91      * property tree properties.
92      */
93     virtual void applyValues ();
94
95
96     /**
97      * Update state.  Called on active dialogs before rendering.
98      */
99     virtual void update ();
100
101 private:
102
103     // Private copy constructor to avoid unpleasant surprises.
104     FGDialog (const FGDialog &);
105
106     // Show the dialog.
107     void display (SGPropertyNode * props);
108
109     // Build the dialog or a subobject of it.
110     puObject * makeObject (SGPropertyNode * props,
111                            int parentWidth, int parentHeight);
112
113     // Common configuration for all GUI objects.
114     void setupObject (puObject * object, SGPropertyNode * props);
115
116     // Common configuration for all GUI group objects.
117     void setupGroup (puGroup * group, SGPropertyNode * props,
118                      int width, int height, sgVec4 color,
119                      bool makeFrame = false);
120
121     // Read color properties and merge them into color vector.
122     void getColor(const SGPropertyNode * prop, sgVec4 color);
123
124     // The top-level PUI object.
125     puObject * _object;
126
127     // PUI provides no way for userdata to be deleted automatically
128     // with a GUI object, so we have to keep track of all the special
129     // data we allocated and then free it manually when the dialog
130     // closes.
131     vector<void *> _info;
132     struct PropertyObject {
133         PropertyObject (const char * name,
134                         puObject * object,
135                         SGPropertyNode_ptr node);
136         string name;
137         puObject * object;
138         SGPropertyNode_ptr node;
139     };
140     vector<PropertyObject *> _propertyObjects;
141     vector<PropertyObject *> _liveObjects;
142
143     // PUI doesn't copy arrays, so we have to allocate string arrays
144     // and then keep pointers so that we can delete them when the
145     // dialog closes.
146     char ** make_char_array (int size);
147     vector<char **> _char_arrays;
148
149     SGPath _font_path;
150     sgVec4 _fgcolor;
151     sgVec4 _bgcolor;
152 };
153
154 //
155 // Custom subclass of puPopup to implement "draggable" windows in the
156 // interface.  Note that this is a subclass of puPopup, not
157 // puDialogBox.  Sadly, PUI (mis)uses subclassing to implement a
158 // boolean property: modality.  That means that we can't implement
159 // dragging of both puPopup windows and puDialogBoxes with the same
160 // code.  Rather than duplicate code, I've chosen to implement only
161 // "non-modal dragability" here.  Modal dialog boxes (like the exit
162 // confirmation) are not draggable.
163 //
164 class fgPopup : public puPopup {
165 public:
166     fgPopup(int x, int y, bool d = true) : puPopup(x, y) { _dragging = false; _draggable = d;}
167     int checkHit(int b, int up, int x, int y);
168     int getHitObjects(puObject *, int x, int y);
169 private:
170     bool _draggable;
171     bool _dragging;
172     int _dX, _dY;
173 };
174
175 #endif // __DIALOG_HXX