| 1 | // FGPUIDialog.hxx - XML-configured dialog box. |
| 2 | /* |
| 3 | * SPDX-FileCopyrightText: 2012 (C) James Turner <[email protected]> |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include "dialog.hxx" |
| 10 | |
| 11 | // ensure we include this before puAux.h, so that |
| 12 | // #define _PU_H_ 1 has been done, and hence we don't |
| 13 | // include the un-modified system pu.h |
| 14 | #include "FlightGear_pu.h" |
| 15 | |
| 16 | #include <plib/puAux.h> |
| 17 | |
| 18 | #include <simgear/misc/sg_path.hxx> |
| 19 | #include <simgear/props/condition.hxx> |
| 20 | #include <simgear/props/props.hxx> |
| 21 | |
| 22 | #include <vector> |
| 23 | |
| 24 | |
| 25 | // ugly temporary workaround for plib's lack of user defined class ids FIXME |
| 26 | #define FGCLASS_LIST 0x00000001 |
| 27 | #define FGCLASS_AIRPORTLIST 0x00000002 |
| 28 | #define FGCLASS_PROPERTYLIST 0x00000004 |
| 29 | #define FGCLASS_WAYPOINTLIST 0x00000008 |
| 30 | #define FGCLASS_LOGLIST 0x00000010 |
| 31 | |
| 32 | class GUI_ID |
| 33 | { |
| 34 | public: |
| 35 | GUI_ID(int id) : id(id) {} |
| 36 | virtual ~GUI_ID() {} |
| 37 | int id; |
| 38 | }; |
| 39 | |
| 40 | class NewGUI; |
| 41 | class FGColor; |
| 42 | class puObject; |
| 43 | class puFont; |
| 44 | |
| 45 | /** |
| 46 | * An XML-configured dialog box. |
| 47 | * |
| 48 | * The GUI manager stores only the property tree for the dialog |
| 49 | * boxes. This class creates a PUI dialog box on demand from |
| 50 | * the properties in that tree. The manager recreates the dialog |
| 51 | * every time it needs to show it. |
| 52 | */ |
| 53 | class FGPUIDialog : public FGDialog |
| 54 | { |
| 55 | public: |
| 56 | /** |
| 57 | * Construct a new GUI widget configured by a property tree. |
| 58 | * |
| 59 | * The configuration properties are not part of the main |
| 60 | * FlightGear property tree; the GUI manager reads them |
| 61 | * from individual configuration files. |
| 62 | * |
| 63 | * @param props A property tree describing the dialog. |
| 64 | */ |
| 65 | FGPUIDialog(SGPropertyNode* props); |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * Destructor. |
| 70 | */ |
| 71 | virtual ~FGPUIDialog(); |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * Update the values of all GUI objects with a specific name, |
| 76 | * or all if an empty name is given (default). |
| 77 | * |
| 78 | * This method copies values from the FlightGear property tree to |
| 79 | * the GUI object(s). |
| 80 | * |
| 81 | * @param objectName The name of the GUI object(s) to update. |
| 82 | * Use the empty name for all objects. |
| 83 | */ |
| 84 | virtual void updateValues(const std::string& objectName = ""); |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * Apply the values of all GUI objects with a specific name, |
| 89 | * or all if an empty name is given (default). |
| 90 | * |
| 91 | * This method copies values from the GUI object(s) to the |
| 92 | * FlightGear property tree. |
| 93 | * |
| 94 | * @param objectName The name of the GUI object(s) to update. |
| 95 | * Use the empty name for all objects. |
| 96 | */ |
| 97 | virtual void applyValues(const std::string& objectName = ""); |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * Update state. Called on active dialogs before rendering. |
| 102 | */ |
| 103 | virtual void update(); |
| 104 | |
| 105 | void close() override; |
| 106 | |
| 107 | /** |
| 108 | * Recompute the dialog's layout |
| 109 | */ |
| 110 | void relayout(); |
| 111 | |
| 112 | |
| 113 | void setNeedsLayout() |
| 114 | { |
| 115 | _needsRelayout = true; |
| 116 | } |
| 117 | virtual const char* getName(); |
| 118 | virtual void bringToFront(); |
| 119 | |
| 120 | class ActiveWidget |
| 121 | { |
| 122 | public: |
| 123 | virtual void update() = 0; |
| 124 | }; |
| 125 | |
| 126 | void runCallback(const std::string& name, SGPropertyNode_ptr args) override; |
| 127 | |
| 128 | |
| 129 | private: |
| 130 | enum { |
| 131 | BACKGROUND = 0x01, |
| 132 | FOREGROUND = 0x02, |
| 133 | HIGHLIGHT = 0x04, |
| 134 | LABEL = 0x08, |
| 135 | LEGEND = 0x10, |
| 136 | MISC = 0x20, |
| 137 | EDITFIELD = 0x40 |
| 138 | }; |
| 139 | |
| 140 | // Show the dialog. |
| 141 | void display(SGPropertyNode* props); |
| 142 | |
| 143 | // Build the dialog or a subobject of it. |
| 144 | puObject* makeObject(SGPropertyNode* props, |
| 145 | int parentWidth, int parentHeight); |
| 146 | |
| 147 | // Common configuration for all GUI objects. |
| 148 | void setupObject(puObject* object, SGPropertyNode* props); |
| 149 | |
| 150 | // Common configuration for all GUI group objects. |
| 151 | void setupGroup(puGroup* group, SGPropertyNode* props, |
| 152 | int width, int height, bool makeFrame = false); |
| 153 | |
| 154 | // Set object colors: the "which" argument defines which color qualities |
| 155 | // (PUCOL_LABEL, etc.) should pick up the <color> property. |
| 156 | void setColor(puObject* object, SGPropertyNode* props, int which = 0); |
| 157 | |
| 158 | // return key code number for keystring |
| 159 | int getKeyCode(const char* keystring); |
| 160 | |
| 161 | /** |
| 162 | * Apply layout sizes to a tree of puObjects |
| 163 | */ |
| 164 | void applySize(puObject* object); |
| 165 | |
| 166 | // The top-level PUI object. |
| 167 | puObject* _object; |
| 168 | |
| 169 | // The GUI subsystem. |
| 170 | NewGUI* _gui; |
| 171 | |
| 172 | // The dialog font. Defaults to the global gui font, but can get |
| 173 | // overridden by a top level font definition. |
| 174 | puFont* _font; |
| 175 | |
| 176 | // The source xml tree, so that we can pass data back, such as the |
| 177 | // last position. |
| 178 | SGPropertyNode_ptr _props; |
| 179 | |
| 180 | bool _needsRelayout; |
| 181 | |
| 182 | // Nasal module. |
| 183 | std::string _module; |
| 184 | SGPropertyNode_ptr _nasal_close; |
| 185 | |
| 186 | std::string _name; |
| 187 | // PUI provides no way for userdata to be deleted automatically |
| 188 | // with a GUI object, so we have to keep track of all the special |
| 189 | // data we allocated and then free it manually when the dialog |
| 190 | // closes. |
| 191 | std::vector<void*> _info; |
| 192 | struct PropertyObject { |
| 193 | PropertyObject(std::string name, |
| 194 | puObject* object, |
| 195 | SGPropertyNode_ptr node); |
| 196 | std::string name; |
| 197 | puObject* object; |
| 198 | SGPropertyNode_ptr node; |
| 199 | }; |
| 200 | std::vector<PropertyObject*> _propertyObjects; |
| 201 | std::vector<PropertyObject*> _liveObjects; |
| 202 | |
| 203 | class ConditionalObject : public SGConditional |
| 204 | { |
| 205 | public: |
| 206 | ConditionalObject(const std::string& aName, puObject* aPu) : _name(aName), |
| 207 | _pu(aPu) |
| 208 | { |
| 209 | ; |
| 210 | } |
| 211 | |
| 212 | void update(FGPUIDialog* aDlg); |
| 213 | |
| 214 | private: |
| 215 | const std::string _name; |
| 216 | puObject* _pu; |
| 217 | }; |
| 218 | |
| 219 | typedef SGSharedPtr<ConditionalObject> ConditionalObjectRef; |
| 220 | std::vector<ConditionalObjectRef> _conditionalObjects; |
| 221 | |
| 222 | std::vector<ActiveWidget*> _activeWidgets; |
| 223 | }; |