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