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>
28 #include <Main/fg_props.hxx>
38 * XML-configured GUI subsystem.
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.
46 class NewGUI : public SGSubsystem
61 * Initialize the GUI subsystem.
66 * Reinitialize the GUI subsystem. Reloads all XML dialogs.
68 virtual void reinit ();
71 * Bind properties for the GUI subsystem.
73 * Currently, this method binds the properties for showing and
79 * Unbind properties for the GUI subsystem.
81 virtual void unbind ();
84 * Update the GUI subsystem.
86 * Currently, this method is a no-op, because nothing the GUI
87 * subsystem does is time-dependent.
89 virtual void update (double delta_time_sec);
92 * Redraw the GUI picking up new GUI colors.
94 virtual void redraw ();
97 * Creates a new dialog box, using the same property format as the
98 * gui/dialogs configuration files. Does not display the
99 * resulting dialog. If a pre-existing dialog of the same name
100 * exists, it will be deleted. The node argument will be stored
101 * in the GUI subsystem using SGPropertNode_ptr reference counting.
102 * It should not be deleted by user code.
104 * @param node A property node containing the dialog definition
106 virtual void newDialog (SGPropertyNode* node);
109 * Display a dialog box.
111 * At initialization time, the subsystem reads all of the XML
112 * configuration files from the directory $FG_ROOT/gui/dialogs/.
113 * The configuration for each dialog specifies a name, and this
114 * method invokes the dialog with the name specified (if it
117 * @param name The name of the dialog box.
118 * @return true if the dialog exists, false otherwise.
120 virtual bool showDialog (const string &name);
124 * Close the currenty active dialog. This function is intended to
125 * be called from code (pui callbacks, for instance) that registers
126 * its dialog object as active via setActiveDialog(). Other
127 * user-level code should use the closeDialog(name) API.
129 * @return true if a dialog was active, false otherwise
131 virtual bool closeActiveDialog ();
134 * Close a named dialog, if it is open.
136 * @param name The name of the dialog box.
137 * @return true if the dialog was active, false otherwise.
139 virtual bool closeDialog (const string &name);
142 * Get dialog property tree's root node.
143 * @param name The name of the dialog box.
144 * @return node pointer if the dialog was found, zero otherwise.
146 virtual SGPropertyNode_ptr getDialogProperties (const string &name);
149 * Return a pointer to the current menubar.
151 virtual FGMenuBar * getMenuBar ();
154 * Ignore this method.
156 * This method is for internal use only, but it has to be public
157 * so that a non-class callback can see it.
159 virtual void setActiveDialog (FGDialog * dialog);
162 * Get the dialog currently active, if any.
164 * @return The active dialog, or 0 if none is active.
166 virtual FGDialog * getActiveDialog ();
170 * Get the named dialog if active.
172 * @return The named dialog, or 0 if it isn't active.
174 virtual FGDialog * getDialog (const string &name);
177 virtual FGColor *getColor (const char * name) const {
178 _citt_t it = _colors.find(name);
179 return (it != _colors.end()) ? it->second : NULL;
181 virtual FGColor *getColor (const string &name) const {
182 _citt_t it = _colors.find(name.c_str());
183 return (it != _colors.end()) ? it->second : NULL;
186 virtual puFont *getDefaultFont() { return _font; }
192 * Test if the menubar is visible.
194 * This method exists only for binding.
196 virtual bool getMenuBarVisible () const;
199 * Show or hide the menubar.
201 * This method exists only for binding.
203 virtual void setMenuBarVisible (bool visible);
205 virtual void setStyle ();
206 virtual void setupFont (SGPropertyNode *);
209 * Used by reinit() and redraw() to close all dialogs and to apply
210 * current GUI colors. If "reload" is false, reopens all dialogs.
211 * Otherwise reloads all XML dialog files from disk and reopens all
212 * but Nasal * generated dialogs, omitting dynamic widgets. (This
213 * is only useful for GUI development.)
215 virtual void reset (bool reload);
220 bool operator()(const char* s1, const char* s2) const {
221 return strcmp(s1, s2) < 0;
226 map<const char*,FGColor*, ltstr> _colors;
227 typedef map<const char*,FGColor*, ltstr>::iterator _itt_t;
228 typedef map<const char*,FGColor*, ltstr>::const_iterator _citt_t;
232 // Read all the configuration files in a directory.
233 void readDir (const char * path);
235 FGMenuBar * _menubar;
236 FGDialog * _active_dialog;
237 map<string,FGDialog *> _active_dialogs;
238 map<string,SGPropertyNode_ptr> _dialog_props;
245 FGColor() { clear(); }
246 FGColor(float r, float g, float b, float a = 1.0f) { set(r, g, b, a); }
247 FGColor(const SGPropertyNode *prop) { set(prop); }
248 FGColor(FGColor *c) {
249 if (c) set(c->_red, c->_green, c->_blue, c->_alpha);
252 inline void clear() { _red = _green = _blue = _alpha = -1.0f; }
253 // merges in non-negative components from property with children <red> etc.
254 bool merge(const SGPropertyNode *prop);
255 bool merge(const FGColor *color);
257 bool set(const SGPropertyNode *prop) { clear(); return merge(prop); };
258 bool set(const FGColor *color) { clear(); return merge(color); }
259 bool set(float r, float g, float b, float a = 1.0f) {
260 _red = r, _green = g, _blue = b, _alpha = a;
263 bool isValid() const {
264 return _red >= 0.0 && _green >= 0.0 && _blue >= 0.0;
267 std::cerr << "red=" << _red << ", green=" << _green
268 << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
271 inline void setRed(float red) { _red = red; }
272 inline void setGreen(float green) { _green = green; }
273 inline void setBlue(float blue) { _blue = blue; }
274 inline void setAlpha(float alpha) { _alpha = alpha; }
276 inline float red() const { return clamp(_red); }
277 inline float green() const { return clamp(_green); }
278 inline float blue() const { return clamp(_blue); }
279 inline float alpha() const { return _alpha < 0.0 ? 1.0 : clamp(_alpha); }
282 float _red, _green, _blue, _alpha;
285 float clamp(float f) const { return f < 0.0 ? 0.0 : f > 1.0 ? 1.0 : f; }
291 * A small class to keep all fonts available for future use.
292 * This also assures a font isn't resident more than once.
297 fnt(puFont *pu = 0) : pufont(pu), texfont(0) {}
298 ~fnt() { if (texfont) { delete pufont; delete texfont; } }
304 map<const string,fnt *> _fonts;
305 typedef map<const string,fnt *>::const_iterator _itt_t;
307 struct fnt *getfnt(const char *name, float size, float slant);
313 puFont *get(const char *name, float size=15.0, float slant=0.0);
314 puFont *get(SGPropertyNode *node);
316 fntTexFont *getTexFont(const char *name, float size=15.0, float slant=0.0);
320 #endif // __NEW_GUI_HXX