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