| 1 | /* |
| 2 | * SPDX-FileName: FGPUIMenuBar.hxx |
| 3 | * SPDX-FileComment: XML-configured menu bar |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include <GUI/menubar.hxx> |
| 10 | |
| 11 | #include <forward_list> |
| 12 | #include <map> |
| 13 | #include <memory> |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
| 17 | |
| 18 | // forward decls, avoid pulling in PLIB headers here |
| 19 | class puMenuBar; |
| 20 | class puObject; |
| 21 | class SGPropertyNode; |
| 22 | class SGBinding; |
| 23 | |
| 24 | typedef void (*puCallback)(class puObject*); |
| 25 | |
| 26 | /** |
| 27 | * XML-configured PUI menu bar. |
| 28 | * |
| 29 | * This class creates a menu bar from a tree of XML properties. These |
| 30 | * properties are not part of the main FlightGear property tree, but |
| 31 | * are read from a separate file ($FG_ROOT/gui/menubar.xml). |
| 32 | * |
| 33 | */ |
| 34 | class FGPUIMenuBar : public FGMenuBar |
| 35 | { |
| 36 | public: |
| 37 | /** |
| 38 | * Constructor. |
| 39 | */ |
| 40 | FGPUIMenuBar(); |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Destructor. |
| 45 | */ |
| 46 | virtual ~FGPUIMenuBar(); |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Initialize the menu bar from $FG_ROOT/gui/menubar.xml |
| 51 | */ |
| 52 | virtual void init(); |
| 53 | |
| 54 | /** |
| 55 | * Make the menu bar visible. |
| 56 | */ |
| 57 | virtual void show(); |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * Make the menu bar invisible. |
| 62 | */ |
| 63 | virtual void hide(); |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * Test whether the menu bar is visible. |
| 68 | */ |
| 69 | virtual bool isVisible() const; |
| 70 | |
| 71 | void setHideIfOverlapsWindow(bool hide) override; |
| 72 | |
| 73 | bool getHideIfOverlapsWindow() const override; |
| 74 | |
| 75 | /** |
| 76 | * IGNORE THIS METHOD!!! |
| 77 | * |
| 78 | * This is necessary only because plib does not provide any easy |
| 79 | * way to attach user data to a menu item. FlightGear should not |
| 80 | * have to know about PUI internals, but this method allows the |
| 81 | * callback to pass the menu item one-shot on to the current menu. |
| 82 | */ |
| 83 | virtual void fireItem(puObject* item); |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * create a menubar based on a PropertyList within the PropertyTree |
| 88 | */ |
| 89 | void make_menubar(SGPropertyNode* props); |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * destroy a menubar based on a PropertyList within the PropertyTree |
| 94 | */ |
| 95 | void destroy_menubar(); |
| 96 | |
| 97 | |
| 98 | /** |
| 99 | * Disable/enable menu titles and entries |
| 100 | */ |
| 101 | bool enable_item(const SGPropertyNode* item, bool state); |
| 102 | |
| 103 | |
| 104 | private: |
| 105 | // Make a single menu. |
| 106 | void make_menu(SGPropertyNode* node); |
| 107 | |
| 108 | // Make the top-level menubar. |
| 109 | void make_menubar(); |
| 110 | |
| 111 | // Create a property-path -> puObject map for menu node |
| 112 | void make_object_map(SGPropertyNode* node); |
| 113 | |
| 114 | // Add <enabled> listener that enables/disables menu entries. |
| 115 | void add_enabled_listener(SGPropertyNode* node); |
| 116 | |
| 117 | void recomputeVisibility(); |
| 118 | |
| 119 | // Is the menu visible? |
| 120 | bool _visible; |
| 121 | bool _hideOverlapping = false; |
| 122 | |
| 123 | // The top-level menubar itself. |
| 124 | puMenuBar* _menuBar; |
| 125 | |
| 126 | // Each element contains the list of bindings for a particular menu entry. |
| 127 | // Not an std::vector because we want the addresses of previous elements |
| 128 | // to remain valid when we add new ones. |
| 129 | std::forward_list<std::vector<std::unique_ptr<SGBinding>>> _bindings; |
| 130 | |
| 131 | // These are hoops that we have to jump through because PUI doesn't |
| 132 | // do memory management for lists. We have to allocate the arrays, |
| 133 | // hang onto pointers, and then delete them when the menubar is |
| 134 | // freed. |
| 135 | char** make_char_array(int size); |
| 136 | puCallback* make_callback_array(int size); |
| 137 | // The return value points to an array where each element is a pointer to a |
| 138 | // vector that gives the list of bindings assigned to a given menu entry. |
| 139 | const std::vector<std::unique_ptr<SGBinding>>** make_userdata_array(int size); |
| 140 | std::vector<char**> _char_arrays; |
| 141 | std::vector<puCallback*> _callback_arrays; |
| 142 | std::vector<const std::vector<std::unique_ptr<SGBinding>>**> _userdata_arrays; |
| 143 | |
| 144 | // A map for {menu node path}->puObject translation. |
| 145 | std::map<std::string, puObject*> _objects; |
| 146 | }; |