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