]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.hxx
Lazy GUI dialog loading, for slightly faster startup.
[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 #include <simgear/props/props.hxx>
7 #include <simgear/structure/subsystem_mgr.hxx>
8 #include <simgear/misc/sg_path.hxx>
9
10 #include <vector>
11 #include <map>
12 #include <memory> // for auto_ptr on some systems
13
14 class FGMenuBar;
15 class FGDialog;
16 class FGColor;
17 class FGFontCache;
18 class puFont;
19
20 /**
21  * XML-configured GUI subsystem.
22  *
23  * This subsystem manages the graphical user interface for FlightGear.
24  * It creates a menubar from the XML configuration file in
25  * $FG_ROOT/gui/menubar.xml, then stores the configuration properties
26  * for XML-configured dialog boxes found in $FG_ROOT/gui/dialogs/.  It
27  * can show or hide the menubar, and can display any dialog by name.
28  */
29 class NewGUI : public SGSubsystem
30 {
31 public:
32
33     /**
34      * Constructor.
35      */
36     NewGUI ();
37
38     /**
39      * Destructor.
40      */
41     virtual ~NewGUI ();
42
43     /**
44      * Initialize the GUI subsystem.
45      */
46     virtual void init ();
47
48     /**
49      * Reinitialize the GUI subsystem. Reloads all XML dialogs.
50      */
51     virtual void reinit ();
52
53     /**
54      * Bind properties for the GUI subsystem.
55      *
56      * Currently, this method binds the properties for showing and
57      * hiding the menu.
58      */
59     virtual void bind ();
60
61     /**
62      * Unbind properties for the GUI subsystem.
63      */
64     virtual void unbind ();
65
66     /**
67      * Update the GUI subsystem.
68      *
69      * Currently, this method is a no-op, because nothing the GUI
70      * subsystem does is time-dependent.
71      */
72     virtual void update (double delta_time_sec);
73
74     /**
75      * Redraw the GUI picking up new GUI colors.
76      */
77     virtual void redraw ();
78
79     /**
80      * Creates a new dialog box, using the same property format as the
81      * gui/dialogs configuration files.  Does not display the
82      * resulting dialog.  If a pre-existing dialog of the same name
83      * exists, it will be deleted.  The node argument will be stored
84      * in the GUI subsystem using SGPropertNode_ptr reference counting.
85      * It should not be deleted by user code.
86      *
87      * @param node A property node containing the dialog definition
88      */
89     virtual void newDialog (SGPropertyNode* node);
90
91     /**
92      * Display a dialog box.
93      *
94      * At initialization time, the subsystem reads all of the XML
95      * configuration files from the directory $FG_ROOT/gui/dialogs/.
96      * The configuration for each dialog specifies a name, and this
97      * method invokes the dialog with the name specified (if it
98      * exists).
99      *
100      * @param name The name of the dialog box.
101      * @return true if the dialog exists, false otherwise.
102      */
103     virtual bool showDialog (const std::string &name);
104
105
106     /**
107      * Close the currenty active dialog.  This function is intended to
108      * be called from code (pui callbacks, for instance) that registers
109      * its dialog object as active via setActiveDialog().  Other
110      * user-level code should use the closeDialog(name) API.
111      *
112      * @return true if a dialog was active, false otherwise
113      */
114     virtual bool closeActiveDialog ();
115
116     /**
117      * Close a named dialog, if it is open.
118      *
119      * @param name The name of the dialog box.
120      * @return true if the dialog was active, false otherwise.
121      */
122     virtual bool closeDialog (const std::string &name);
123
124     /**
125      * Get dialog property tree's root node.
126      * @param name The name of the dialog box.
127      * @return node pointer if the dialog was found, zero otherwise.
128      */
129     virtual SGPropertyNode_ptr getDialogProperties (const std::string &name);
130
131     /**
132      * Return a pointer to the current menubar.
133      */
134     virtual FGMenuBar * getMenuBar ();
135
136     /**
137      * Ignore this method.
138      *
139      * This method is for internal use only, but it has to be public
140      * so that a non-class callback can see it.
141      */
142     virtual void setActiveDialog (FGDialog * dialog);
143
144     /**
145      * Get the dialog currently active, if any.
146      *
147      * @return The active dialog, or 0 if none is active.
148      */
149     virtual FGDialog * getActiveDialog ();
150
151
152     /**
153      * Get the named dialog if active.
154      *
155      * @return The named dialog, or 0 if it isn't active.
156      */
157     virtual FGDialog * getDialog (const std::string &name);
158
159
160     virtual FGColor *getColor (const char * name) const {
161         _citt_t it = _colors.find(name);
162         return (it != _colors.end()) ? it->second : NULL;
163     }
164     virtual FGColor *getColor (const std::string &name) const {
165         _citt_t it = _colors.find(name.c_str());
166         return (it != _colors.end()) ? it->second : NULL;
167     }
168
169     virtual puFont *getDefaultFont() { return _font; }
170
171
172 protected:
173
174     /**
175      * Test if the menubar is visible.
176      *
177      * This method exists only for binding.
178      */
179     virtual bool getMenuBarVisible () const;
180
181     /**
182      * Show or hide the menubar.
183      *
184      * This method exists only for binding.
185      */
186     virtual void setMenuBarVisible (bool visible);
187
188     virtual void setStyle ();
189     virtual void setupFont (SGPropertyNode *);
190
191     /**
192      * Used by reinit() and redraw() to close all dialogs and to apply
193      * current GUI colors. If "reload" is false, reopens all dialogs.
194      * Otherwise reloads all XML dialog files from disk and reopens all
195      * but Nasal * generated dialogs, omitting dynamic widgets. (This
196      * is only useful for GUI development.)
197      */
198     virtual void reset (bool reload);
199
200 private:
201     struct ltstr
202     {
203         bool operator()(const char* s1, const char* s2) const {
204             return strcmp(s1, s2) < 0;
205         }
206     };
207
208     puFont *_font;
209     typedef std::map<const char*,FGColor*, ltstr> ColourDict;
210     ColourDict _colors;
211     typedef ColourDict::iterator _itt_t;
212     typedef ColourDict::const_iterator _citt_t;
213
214     void clear_colors();
215
216     // Read all the configuration files in a directory.
217     void readDir (const SGPath& path);
218
219     std::auto_ptr<FGMenuBar> _menubar;
220     FGDialog * _active_dialog;
221     std::map<std::string,FGDialog *> _active_dialogs;
222   
223     typedef std::map<std::string, SGPath> NamePathDict;
224     // mapping from dialog names to the corresponding XML property list
225     // which defines them
226     NamePathDict _dialog_names;
227   
228     // cache of loaded dialog proeprties
229     typedef std::map<std::string,SGPropertyNode_ptr> NameDialogDict;
230     NameDialogDict _dialog_props;
231
232 };
233
234
235 #endif // __NEW_GUI_HXX
236