]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.hxx
Fix a segmentation fault when switching to a new style and selecting a new dialog...
[flightgear.git] / src / GUI / new_gui.hxx
1 // new_gui.hxx - XML-configured GUI subsystem.
2
3 #ifndef __NEW_GUI_HXX
4 #define __NEW_GUI_HXX 1
5
6 #ifndef __cplusplus
7 # error This library requires C++
8 #endif
9
10 #ifdef HAVE_CONFIG_H
11 #  include <config.h>
12 #endif
13
14 #include <plib/pu.h>
15
16 #include <simgear/compiler.h>   // for SG_USING_STD
17 #include <simgear/props/props.hxx>
18 #include <simgear/structure/subsystem_mgr.hxx>
19 #include <simgear/misc/sg_path.hxx>
20
21 #include <vector>
22 #include <map>
23
24 SG_USING_STD(vector);
25 SG_USING_STD(map);
26 SG_USING_STD(string);
27
28 #include <Main/fg_props.hxx>
29
30 class FGMenuBar;
31 class FGDialog;
32 class FGBinding;
33 class FGColor;
34 class FGFontCache;
35
36
37 /**
38  * XML-configured GUI subsystem.
39  *
40  * This subsystem manages the graphical user interface for FlightGear.
41  * It creates a menubar from the XML configuration file in
42  * $FG_ROOT/gui/menubar.xml, then stores the configuration properties
43  * for XML-configured dialog boxes found in $FG_ROOT/gui/dialogs/.  It
44  * can show or hide the menubar, and can display any dialog by name.
45  */
46 class NewGUI : public SGSubsystem
47 {
48 public:
49
50     /**
51      * Constructor.
52      */
53     NewGUI ();
54
55     /**
56      * Destructor.
57      */
58     virtual ~NewGUI ();
59
60     /**
61      * Initialize the GUI subsystem.
62      */
63     virtual void init ();
64
65     /**
66      * Reinitialize the GUI subsystem.
67      */
68     virtual void reinit ();
69
70     /**
71      * Bind properties for the GUI subsystem.
72      *
73      * Currently, this method binds the properties for showing and
74      * hiding the menu.
75      */
76     virtual void bind ();
77
78     /**
79      * Unbind properties for the GUI subsystem.
80      */
81     virtual void unbind ();
82
83     /**
84      * Update the GUI subsystem.
85      *
86      * Currently, this method is a no-op, because nothing the GUI
87      * subsystem does is time-dependent.
88      */
89     virtual void update (double delta_time_sec);
90
91     /**
92      * Creates a new dialog box, using the same property format as the
93      * gui/dialogs configuration files.  Does not display the
94      * resulting dialog.  If a pre-existing dialog of the same name
95      * exists, it will be deleted.  The node argument will be stored
96      * in the GUI subsystem using SGPropertNode_ptr reference counting.
97      * It should not be deleted by user code.
98      *
99      * @param node A property node containing the dialog definition
100      */
101     virtual void newDialog (SGPropertyNode* node);
102
103     /**
104      * Display a dialog box.
105      *
106      * At initialization time, the subsystem reads all of the XML
107      * configuration files from the directory $FG_ROOT/gui/dialogs/.
108      * The configuration for each dialog specifies a name, and this
109      * method invokes the dialog with the name specified (if it
110      * exists).
111      *
112      * @param name The name of the dialog box.
113      * @return true if the dialog exists, false otherwise.
114      */
115     virtual bool showDialog (const string &name);
116
117
118     /**
119      * Close the currenty active dialog.  This function is intended to
120      * be called from code (pui callbacks, for instance) that registers
121      * its dialog object as active via setActiveDialog().  Other
122      * user-level code should use the closeDialog(name) API.
123      *
124      * @return true if a dialog was active, false otherwise
125      */
126     virtual bool closeActiveDialog ();
127
128     /**
129      * Close a named dialog, if it is open.
130      *
131      * @param name The name of the dialog box.
132      * @return true if the dialog was active, false otherwise.
133      */
134     virtual bool closeDialog (const string &name);
135
136     /**
137      * Return a pointer to the current menubar.
138      */
139     virtual FGMenuBar * getMenuBar ();
140
141
142     /**
143      * Ignore this method.
144      *
145      * This method is for internal use only, but it has to be public
146      * so that a non-class callback can see it.
147      */
148     virtual void setActiveDialog (FGDialog * dialog);
149
150     /**
151      * Get the dialog currently active, if any.
152      *
153      * @return The active dialog, or 0 if none is active.
154      */
155     virtual FGDialog * getActiveDialog ();
156
157     virtual FGColor *getColor (const char * name) const {
158         _itt_t it = _colors.find(name);
159         return (it != _colors.end()) ? it->second : NULL;
160     }
161     virtual FGColor *getColor (const string &name) const {
162         _itt_t it = _colors.find(name.c_str());
163         return (it != _colors.end()) ? it->second : NULL;
164     }
165
166     virtual puFont *getDefaultFont() { return &_font; }
167
168
169     /**
170      * menu wide font cache, accessible from other classes as well.
171      */
172     FGFontCache *get_fontcache() { return _fontcache; }
173
174 protected:
175
176     FGFontCache * _fontcache;
177
178     /**
179      * Test if the menubar is visible.
180      *
181      * This method exists only for binding.
182      */
183     virtual bool getMenuBarVisible () const;
184
185     /**
186      * Show or hide the menubar.
187      *
188      * This method exists only for binding.
189      */
190     virtual void setMenuBarVisible (bool visible);
191
192     virtual void setStyle ();
193     virtual void setupFont (SGPropertyNode *);
194
195 private:
196     fntTexFont _tex_font;
197     puFont _font;
198     map<const char*,FGColor*> _colors;
199     typedef map<const char*,FGColor*>::const_iterator _itt_t;
200
201     // Free all allocated memory.
202     void clear ();
203
204     // Read all the configuration files in a directory.
205     void readDir (const char * path);
206
207     FGMenuBar * _menubar;
208     FGDialog * _active_dialog;
209     map<string,FGDialog *> _active_dialogs;
210     map<string,SGPropertyNode_ptr> _dialog_props;
211
212 };
213
214
215 class FGColor {
216 public:
217     FGColor() { clear(); }
218     FGColor(float r, float g, float b, float a = 1.0f) { set(r, g, b, a); }
219     FGColor(const SGPropertyNode *prop) { set(prop); }
220     FGColor(FGColor *c) { 
221         if (c) set(c->_red, c->_green, c->_blue, c->_alpha);
222     }
223
224     inline void clear() { _red = _green = _blue = _alpha = -1.0f; }
225     // merges in non-negative components from property with children <red> etc.
226     bool merge(const SGPropertyNode *prop);
227     bool merge(const FGColor *color);
228
229     bool set(const SGPropertyNode *prop) { clear(); return merge(prop); };
230     bool set(const FGColor *color) { clear(); return merge(color); }
231     bool set(float r, float g, float b, float a = 1.0f) {
232         _red = r, _green = g, _blue = b, _alpha = a;
233         return true;
234     }
235     bool isValid() const {
236         return _red >= 0.0 && _green >= 0.0 && _blue >= 0.0;
237     }
238     void print() const {
239         std::cerr << "red=" << _red << ", green=" << _green
240         << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
241     }
242
243     inline void setRed(float red) { _red = red; }
244     inline void setGreen(float green) { _green = green; }
245     inline void setBlue(float blue) { _blue = blue; }
246     inline void setAlpha(float alpha) { _alpha = alpha; }
247
248     inline float red() const { return clamp(_red); }
249     inline float green() const { return clamp(_green); }
250     inline float blue() const { return clamp(_blue); }
251     inline float alpha() const { return _alpha < 0.0 ? 1.0 : clamp(_alpha); }
252
253 protected:
254     float _red, _green, _blue, _alpha;
255
256 private:
257     float clamp(float f) const { return f < 0.0 ? 0.0 : f > 1.0 ? 1.0 : f; }
258 };
259
260
261
262 /**
263  * A small class to keep all fonts available for future use.
264  * This also assures a font isn't resident more than once.
265  */
266 class FGFontCache {
267 private:
268     SGPath _path;
269
270     map<const char*,puFont*> _fonts;
271     typedef map<const char*,puFont*>::iterator _itt_t;
272
273 public:
274     FGFontCache();
275     ~FGFontCache();
276
277     puFont *get(const char *name, float size=15.0, float slant=0.0);
278     puFont *get(SGPropertyNode *node);
279 };
280
281
282 #endif // __NEW_GUI_HXX
283