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