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>
39 * XML-configured GUI subsystem.
41 * This subsystem manages the graphical user interface for FlightGear.
42 * It creates a menubar from the XML configuration file in
43 * $FG_ROOT/gui/menubar.xml, then stores the configuration properties
44 * for XML-configured dialog boxes found in $FG_ROOT/gui/dialogs/. It
45 * can show or hide the menubar, and can display any dialog by name.
47 class NewGUI : public SGSubsystem
62 * Initialize the GUI subsystem.
67 * Reinitialize the GUI subsystem. Reloads all XML dialogs.
69 virtual void reinit ();
72 * Bind properties for the GUI subsystem.
74 * Currently, this method binds the properties for showing and
80 * Unbind properties for the GUI subsystem.
82 virtual void unbind ();
85 * Update the GUI subsystem.
87 * Currently, this method is a no-op, because nothing the GUI
88 * subsystem does is time-dependent.
90 virtual void update (double delta_time_sec);
93 * Redraw the GUI picking up new GUI colors.
95 virtual void redraw ();
98 * Creates a new dialog box, using the same property format as the
99 * gui/dialogs configuration files. Does not display the
100 * resulting dialog. If a pre-existing dialog of the same name
101 * exists, it will be deleted. The node argument will be stored
102 * in the GUI subsystem using SGPropertNode_ptr reference counting.
103 * It should not be deleted by user code.
105 * @param node A property node containing the dialog definition
107 virtual void newDialog (SGPropertyNode* node);
110 * Display a dialog box.
112 * At initialization time, the subsystem reads all of the XML
113 * configuration files from the directory $FG_ROOT/gui/dialogs/.
114 * The configuration for each dialog specifies a name, and this
115 * method invokes the dialog with the name specified (if it
118 * @param name The name of the dialog box.
119 * @return true if the dialog exists, false otherwise.
121 virtual bool showDialog (const string &name);
125 * Close the currenty active dialog. This function is intended to
126 * be called from code (pui callbacks, for instance) that registers
127 * its dialog object as active via setActiveDialog(). Other
128 * user-level code should use the closeDialog(name) API.
130 * @return true if a dialog was active, false otherwise
132 virtual bool closeActiveDialog ();
135 * Close a named dialog, if it is open.
137 * @param name The name of the dialog box.
138 * @return true if the dialog was active, false otherwise.
140 virtual bool closeDialog (const string &name);
143 * Get dialog property tree's root node.
144 * @param name The name of the dialog box.
145 * @return node pointer if the dialog was found, zero otherwise.
147 virtual SGPropertyNode_ptr getDialogProperties (const string &name);
150 * Return a pointer to the current menubar.
152 virtual FGMenuBar * getMenuBar ();
155 * Ignore this method.
157 * This method is for internal use only, but it has to be public
158 * so that a non-class callback can see it.
160 virtual void setActiveDialog (FGDialog * dialog);
163 * Get the dialog currently active, if any.
165 * @return The active dialog, or 0 if none is active.
167 virtual FGDialog * getActiveDialog ();
171 * Get the named dialog if active.
173 * @return The named dialog, or 0 if it isn't active.
175 virtual FGDialog * getDialog (const string &name);
178 virtual FGColor *getColor (const char * name) const {
179 _citt_t it = _colors.find(name);
180 return (it != _colors.end()) ? it->second : NULL;
182 virtual FGColor *getColor (const string &name) const {
183 _citt_t it = _colors.find(name.c_str());
184 return (it != _colors.end()) ? it->second : NULL;
187 virtual puFont *getDefaultFont() { return _font; }
193 * Test if the menubar is visible.
195 * This method exists only for binding.
197 virtual bool getMenuBarVisible () const;
200 * Show or hide the menubar.
202 * This method exists only for binding.
204 virtual void setMenuBarVisible (bool visible);
206 virtual void setStyle ();
207 virtual void setupFont (SGPropertyNode *);
210 * Used by reinit() and redraw() to close all dialogs and to apply
211 * current GUI colors. If "reload" is false, reopens all dialogs.
212 * Otherwise reloads all XML dialog files from disk and reopens all
213 * but Nasal * generated dialogs, omitting dynamic widgets. (This
214 * is only useful for GUI development.)
216 virtual void reset (bool reload);
221 bool operator()(const char* s1, const char* s2) const {
222 return strcmp(s1, s2) < 0;
227 map<const char*,FGColor*, ltstr> _colors;
228 typedef map<const char*,FGColor*, ltstr>::iterator _itt_t;
229 typedef map<const char*,FGColor*, ltstr>::const_iterator _citt_t;
233 // Read all the configuration files in a directory.
234 void readDir (const char * path);
236 FGMenuBar * _menubar;
237 FGDialog * _active_dialog;
238 map<string,FGDialog *> _active_dialogs;
239 map<string,SGPropertyNode_ptr> _dialog_props;
246 FGColor() { clear(); }
247 FGColor(float r, float g, float b, float a = 1.0f) { set(r, g, b, a); }
248 FGColor(const SGPropertyNode *prop) { set(prop); }
249 FGColor(FGColor *c) {
250 if (c) set(c->_red, c->_green, c->_blue, c->_alpha);
253 inline void clear() { _red = _green = _blue = _alpha = -1.0f; }
254 // merges in non-negative components from property with children <red> etc.
255 bool merge(const SGPropertyNode *prop);
256 bool merge(const FGColor *color);
258 bool set(const SGPropertyNode *prop) { clear(); return merge(prop); };
259 bool set(const FGColor *color) { clear(); return merge(color); }
260 bool set(float r, float g, float b, float a = 1.0f) {
261 _red = r, _green = g, _blue = b, _alpha = a;
264 bool isValid() const {
265 return _red >= 0.0 && _green >= 0.0 && _blue >= 0.0;
268 std::cerr << "red=" << _red << ", green=" << _green
269 << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
272 inline void setRed(float red) { _red = red; }
273 inline void setGreen(float green) { _green = green; }
274 inline void setBlue(float blue) { _blue = blue; }
275 inline void setAlpha(float alpha) { _alpha = alpha; }
277 inline float red() const { return clamp(_red); }
278 inline float green() const { return clamp(_green); }
279 inline float blue() const { return clamp(_blue); }
280 inline float alpha() const { return _alpha < 0.0 ? 1.0 : clamp(_alpha); }
283 float _red, _green, _blue, _alpha;
286 float clamp(float f) const { return f < 0.0 ? 0.0 : f > 1.0 ? 1.0 : f; }
292 * A small class to keep all fonts available for future use.
293 * This also assures a font isn't resident more than once.
298 fnt(puFont *pu = 0) : pufont(pu), texfont(0) {}
299 ~fnt() { if (texfont) { delete pufont; delete texfont; } }
305 map<const string,fnt *> _fonts;
306 typedef map<const string,fnt *>::const_iterator _itt_t;
308 struct fnt *getfnt(const char *name, float size, float slant);
314 puFont *get(const char *name, float size=15.0, float slant=0.0);
315 puFont *get(SGPropertyNode *node);
317 fntTexFont *getTexFont(const char *name, float size=15.0, float slant=0.0);
319 SGPath getfntpath(const char *name);
323 #endif // __NEW_GUI_HXX