1 // new_gui.hxx - XML-configured GUI subsystem.
4 #define __NEW_GUI_HXX 1
7 # error This library requires C++
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>
29 #include <Main/fg_props.hxx>
40 * XML-configured GUI subsystem.
42 * This subsystem manages the graphical user interface for FlightGear.
43 * It creates a menubar from the XML configuration file in
44 * $FG_ROOT/gui/menubar.xml, then stores the configuration properties
45 * for XML-configured dialog boxes found in $FG_ROOT/gui/dialogs/. It
46 * can show or hide the menubar, and can display any dialog by name.
48 class NewGUI : public SGSubsystem
63 * Initialize the GUI subsystem.
68 * Reinitialize the GUI subsystem. Reloads all XML dialogs.
70 virtual void reinit ();
73 * Bind properties for the GUI subsystem.
75 * Currently, this method binds the properties for showing and
81 * Unbind properties for the GUI subsystem.
83 virtual void unbind ();
86 * Update the GUI subsystem.
88 * Currently, this method is a no-op, because nothing the GUI
89 * subsystem does is time-dependent.
91 virtual void update (double delta_time_sec);
94 * Redraw the GUI picking up new GUI colors.
96 virtual void redraw ();
99 * Creates a new dialog box, using the same property format as the
100 * gui/dialogs configuration files. Does not display the
101 * resulting dialog. If a pre-existing dialog of the same name
102 * exists, it will be deleted. The node argument will be stored
103 * in the GUI subsystem using SGPropertNode_ptr reference counting.
104 * It should not be deleted by user code.
106 * @param node A property node containing the dialog definition
108 virtual void newDialog (SGPropertyNode* node);
111 * Display a dialog box.
113 * At initialization time, the subsystem reads all of the XML
114 * configuration files from the directory $FG_ROOT/gui/dialogs/.
115 * The configuration for each dialog specifies a name, and this
116 * method invokes the dialog with the name specified (if it
119 * @param name The name of the dialog box.
120 * @return true if the dialog exists, false otherwise.
122 virtual bool showDialog (const string &name);
126 * Close the currenty active dialog. This function is intended to
127 * be called from code (pui callbacks, for instance) that registers
128 * its dialog object as active via setActiveDialog(). Other
129 * user-level code should use the closeDialog(name) API.
131 * @return true if a dialog was active, false otherwise
133 virtual bool closeActiveDialog ();
136 * Close a named dialog, if it is open.
138 * @param name The name of the dialog box.
139 * @return true if the dialog was active, false otherwise.
141 virtual bool closeDialog (const string &name);
144 * Get dialog property tree's root node.
145 * @param name The name of the dialog box.
146 * @return node pointer if the dialog was found, zero otherwise.
148 virtual SGPropertyNode_ptr getDialogProperties (const string &name);
151 * Return a pointer to the current menubar.
153 virtual FGMenuBar * getMenuBar ();
156 * Ignore this method.
158 * This method is for internal use only, but it has to be public
159 * so that a non-class callback can see it.
161 virtual void setActiveDialog (FGDialog * dialog);
164 * Get the dialog currently active, if any.
166 * @return The active dialog, or 0 if none is active.
168 virtual FGDialog * getActiveDialog ();
172 * Get the named dialog if active.
174 * @return The named dialog, or 0 if it isn't active.
176 virtual FGDialog * getDialog (const string &name);
179 virtual FGColor *getColor (const char * name) const {
180 _citt_t it = _colors.find(name);
181 return (it != _colors.end()) ? it->second : NULL;
183 virtual FGColor *getColor (const string &name) const {
184 _citt_t it = _colors.find(name.c_str());
185 return (it != _colors.end()) ? it->second : NULL;
188 virtual puFont *getDefaultFont() { return _font; }
194 * Test if the menubar is visible.
196 * This method exists only for binding.
198 virtual bool getMenuBarVisible () const;
201 * Show or hide the menubar.
203 * This method exists only for binding.
205 virtual void setMenuBarVisible (bool visible);
207 virtual void setStyle ();
208 virtual void setupFont (SGPropertyNode *);
211 * Used by reinit() and redraw() to close all dialogs and to apply
212 * current GUI colors. If "reload" is false, reopens all dialogs.
213 * Otherwise reloads all XML dialog files from disk and reopens all
214 * but Nasal * generated dialogs, omitting dynamic widgets. (This
215 * is only useful for GUI development.)
217 virtual void reset (bool reload);
222 bool operator()(const char* s1, const char* s2) const {
223 return strcmp(s1, s2) < 0;
228 map<const char*,FGColor*, ltstr> _colors;
229 typedef map<const char*,FGColor*, ltstr>::iterator _itt_t;
230 typedef map<const char*,FGColor*, ltstr>::const_iterator _citt_t;
234 // Read all the configuration files in a directory.
235 void readDir (const char * path);
237 FGMenuBar * _menubar;
238 FGDialog * _active_dialog;
239 map<string,FGDialog *> _active_dialogs;
240 map<string,SGPropertyNode_ptr> _dialog_props;
247 FGColor() { clear(); }
248 FGColor(float r, float g, float b, float a = 1.0f) { set(r, g, b, a); }
249 FGColor(const SGPropertyNode *prop) { set(prop); }
250 FGColor(FGColor *c) {
251 if (c) set(c->_red, c->_green, c->_blue, c->_alpha);
254 inline void clear() { _red = _green = _blue = _alpha = -1.0f; }
255 // merges in non-negative components from property with children <red> etc.
256 bool merge(const SGPropertyNode *prop);
257 bool merge(const FGColor *color);
259 bool set(const SGPropertyNode *prop) { clear(); return merge(prop); };
260 bool set(const FGColor *color) { clear(); return merge(color); }
261 bool set(float r, float g, float b, float a = 1.0f) {
262 _red = r, _green = g, _blue = b, _alpha = a;
265 bool isValid() const {
266 return _red >= 0.0 && _green >= 0.0 && _blue >= 0.0;
270 inline void setRed(float red) { _red = red; }
271 inline void setGreen(float green) { _green = green; }
272 inline void setBlue(float blue) { _blue = blue; }
273 inline void setAlpha(float alpha) { _alpha = alpha; }
275 inline float red() const { return clamp(_red); }
276 inline float green() const { return clamp(_green); }
277 inline float blue() const { return clamp(_blue); }
278 inline float alpha() const { return _alpha < 0.0 ? 1.0 : clamp(_alpha); }
281 float _red, _green, _blue, _alpha;
284 float clamp(float f) const { return f < 0.0 ? 0.0 : f > 1.0 ? 1.0 : f; }
290 * A class to keep all fonts available for future use.
291 * This also assures a font isn't resident more than once.
295 // The parameters of a request to the cache.
298 const std::string name;
301 FntParams() : size(0.0f), slant(0.0f) {}
302 FntParams(const FntParams& rhs)
303 : name(rhs.name), size(rhs.size), slant(rhs.slant)
306 FntParams(const std::string& name_, float size_, float slant_)
307 : name(name_), size(size_), slant(slant_)
312 : public std::binary_function<const FntParams, const FntParams, bool>
314 bool operator() (const FntParams& f1, const FntParams& f2) const;
317 fnt(puFont *pu = 0) : pufont(pu), texfont(0) {}
318 ~fnt() { if (texfont) { delete pufont; delete texfont; } }
319 // Font used by plib GUI code
324 // Path to the font directory
327 typedef map<const string, fntTexFont*> TexFontMap;
328 typedef map<const FntParams, fnt*, FntParamsLess> PuFontMap;
329 TexFontMap _texFonts;
333 struct fnt *getfnt(const char *name, float size, float slant);
340 puFont *get(const char *name, float size=15.0, float slant=0.0);
341 puFont *get(SGPropertyNode *node);
343 fntTexFont *getTexFont(const char *name, float size=15.0, float slant=0.0);
345 SGPath getfntpath(const char *name);
347 * Preload all the fonts in the FlightGear font directory. It is
348 * important to load the font textures early, with the proper
349 * graphics context current, so that no plib (or our own) code
350 * tries to load a font from disk when there's no current graphics
353 bool initializeFonts();
357 #endif // __NEW_GUI_HXX