| 1 | #ifdef HAVE_CONFIG_H |
| 2 | # include <config.h> |
| 3 | #endif |
| 4 | |
| 5 | #include <string.h> |
| 6 | #include <iostream> |
| 7 | #include <simgear/debug/logstream.hxx> |
| 8 | #include <simgear/structure/SGBinding.hxx> |
| 9 | #include <simgear/props/props_io.hxx> |
| 10 | |
| 11 | #include <Main/fg_props.hxx> |
| 12 | #include <Main/globals.hxx> |
| 13 | #include <Main/locale.hxx> |
| 14 | #include <Main/sentryIntegration.hxx> |
| 15 | |
| 16 | #include "new_gui.hxx" |
| 17 | #include "FGPUIMenuBar.hxx" |
| 18 | #include "FlightGear_pu.h" |
| 19 | |
| 20 | using std::map; |
| 21 | using std::string; |
| 22 | using std::unique_ptr; |
| 23 | using std::vector; |
| 24 | //////////////////////////////////////////////////////////////////////// |
| 25 | // FIXME!! |
| 26 | // |
| 27 | // Deprecated wrappers for old menu commands. |
| 28 | // |
| 29 | // DO NOT ADD TO THESE. THEY WILL BE DELETED SOON! |
| 30 | // |
| 31 | // These are defined in gui_funcs.cxx. They should be replaced with |
| 32 | // user-configured dialogs and new commands where necessary. |
| 33 | //////////////////////////////////////////////////////////////////////// |
| 34 | |
| 35 | #if defined(TR_HIRES_SNAP) |
| 36 | extern void dumpHiResSnapShot (); |
| 37 | static bool |
| 38 | do_hires_snapshot_dialog (const SGPropertyNode * arg, SGPropertyNode * root) |
| 39 | { |
| 40 | dumpHiResSnapShot(); |
| 41 | return true; |
| 42 | } |
| 43 | #endif // TR_HIRES_SNAP |
| 44 | |
| 45 | static struct { |
| 46 | const char * name; |
| 47 | SGCommandMgr::command_t command; |
| 48 | } deprecated_dialogs [] = { |
| 49 | #if defined(TR_HIRES_SNAP) |
| 50 | { "old-hires-snapshot-dialog", do_hires_snapshot_dialog }, |
| 51 | #endif |
| 52 | { 0, 0 } |
| 53 | }; |
| 54 | |
| 55 | static void |
| 56 | add_deprecated_dialogs () |
| 57 | { |
| 58 | SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:"); |
| 59 | for (int i = 0; deprecated_dialogs[i].name != 0; i++) { |
| 60 | SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name); |
| 61 | globals->get_commands()->addCommand(deprecated_dialogs[i].name, |
| 62 | deprecated_dialogs[i].command); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | //////////////////////////////////////////////////////////////////////// |
| 68 | // Static functions. |
| 69 | //////////////////////////////////////////////////////////////////////// |
| 70 | |
| 71 | |
| 72 | static void |
| 73 | menu_callback (puObject * object) |
| 74 | { |
| 75 | auto gui = globals->get_subsystem<NewGUI>(); |
| 76 | FGPUIMenuBar* mb = static_cast<FGPUIMenuBar*>(gui->getMenuBar()); |
| 77 | mb->fireItem(object); |
| 78 | } |
| 79 | |
| 80 | //////////////////////////////////////////////////////////////////////// |
| 81 | // Implementation of FGPUIMenuBar. |
| 82 | //////////////////////////////////////////////////////////////////////// |
| 83 | |
| 84 | |
| 85 | FGPUIMenuBar::FGPUIMenuBar () |
| 86 | : _visible(false), |
| 87 | _menuBar(0) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | FGPUIMenuBar::~FGPUIMenuBar () |
| 92 | { |
| 93 | destroy_menubar(); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | FGPUIMenuBar::init () |
| 98 | { |
| 99 | bool visible = _visible; |
| 100 | destroy_menubar(); |
| 101 | |
| 102 | make_menubar(); |
| 103 | // FIXME: temporary commands to get at |
| 104 | // old, hard-coded dialogs. |
| 105 | add_deprecated_dialogs(); |
| 106 | |
| 107 | // Keep menu visible during gui-redraw |
| 108 | if( visible ) |
| 109 | show(); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | FGPUIMenuBar::show () |
| 114 | { |
| 115 | _visible = true; |
| 116 | recomputeVisibility(); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | FGPUIMenuBar::hide () |
| 121 | { |
| 122 | _visible = false; |
| 123 | recomputeVisibility(); |
| 124 | } |
| 125 | |
| 126 | bool |
| 127 | FGPUIMenuBar::isVisible () const |
| 128 | { |
| 129 | return _visible; |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | FGPUIMenuBar::setHideIfOverlapsWindow(bool hide) |
| 134 | { |
| 135 | _hideOverlapping = hide; |
| 136 | recomputeVisibility(); |
| 137 | } |
| 138 | |
| 139 | bool |
| 140 | FGPUIMenuBar::getHideIfOverlapsWindow() const |
| 141 | { |
| 142 | return _hideOverlapping; |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | FGPUIMenuBar::recomputeVisibility() |
| 147 | { |
| 148 | if (_menuBar) { |
| 149 | const bool actualVis = _visible && (!_hideOverlapping); |
| 150 | if (actualVis) { |
| 151 | _menuBar->reveal(); |
| 152 | } else { |
| 153 | _menuBar->hide(); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | FGPUIMenuBar::fireItem (puObject * item) |
| 160 | { |
| 161 | const char * name = item->getLegend(); |
| 162 | const auto& bindings = |
| 163 | *static_cast< vector<unique_ptr<SGBinding>>* >(item->getUserData()); |
| 164 | flightgear::addSentryBreadcrumb("fire menu item:" + string{name}, "info"); |
| 165 | |
| 166 | for (const auto& binding: bindings) { |
| 167 | binding->fire(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | FGPUIMenuBar::make_menu (SGPropertyNode * node) |
| 173 | { |
| 174 | string s = getLocalizedLabel(node); |
| 175 | |
| 176 | // hack: map at least some UTF-8 characters to Latin1, since FG fonts are |
| 177 | // Latin1 (or plain ASCII, which is a subset). This hack can be removed once |
| 178 | // the PLIB/OSG port is complete (OSG has full UTF-8 support! :) ). |
| 179 | FGLocale::utf8toLatin1(s); |
| 180 | |
| 181 | const char* name = strdup(s.c_str()); |
| 182 | |
| 183 | vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item"); |
| 184 | |
| 185 | int array_size = item_nodes.size(); |
| 186 | |
| 187 | char ** items = make_char_array(array_size); |
| 188 | puCallback * callbacks = make_callback_array(array_size); |
| 189 | const vector<unique_ptr<SGBinding>> ** userdata = |
| 190 | make_userdata_array(array_size); |
| 191 | |
| 192 | for (unsigned int i = 0, j = item_nodes.size() - 1; |
| 193 | i < item_nodes.size(); |
| 194 | i++, j--) { |
| 195 | // Set up the PUI entries for this item |
| 196 | string label = getLocalizedLabel(item_nodes[i]); |
| 197 | FGLocale::utf8toLatin1(label); |
| 198 | |
| 199 | // append the keyboard hint to the menu entry |
| 200 | string key = item_nodes[i]->getStringValue("key", ""); |
| 201 | if (!key.empty()) |
| 202 | { |
| 203 | label.append(" <"); |
| 204 | label.append(key); |
| 205 | label.append(">"); |
| 206 | } |
| 207 | |
| 208 | items[j] = strdup(label.c_str()); |
| 209 | callbacks[j] = menu_callback; |
| 210 | // Add an element (vector) that will contain all bindings assigned |
| 211 | // to the menu entry. |
| 212 | auto& bindingsVec = _bindings.emplace_front(); |
| 213 | userdata[j] = &bindingsVec; |
| 214 | |
| 215 | // Load all the bindings for this item |
| 216 | vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding"); |
| 217 | SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true); |
| 218 | |
| 219 | for (unsigned int k = 0; k < bindings.size(); k++) { |
| 220 | unsigned int m = 0; |
| 221 | SGPropertyNode_ptr binding; |
| 222 | while (dest->getChild("binding", m)) |
| 223 | m++; |
| 224 | |
| 225 | binding = dest->getChild("binding", m, true); |
| 226 | copyProperties(bindings[k], binding); |
| 227 | bindingsVec.push_back( |
| 228 | std::make_unique<SGBinding>(binding, globals->get_props())); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | _menuBar->add_submenu(name, items, callbacks, (void **) userdata); |
| 233 | } |
| 234 | |
| 235 | void |
| 236 | FGPUIMenuBar::make_menubar () |
| 237 | { |
| 238 | SGPropertyNode *targetpath; |
| 239 | |
| 240 | targetpath = fgGetNode("/sim/menubar/default",true); |
| 241 | // fgLoadProps("gui/menubar.xml", targetpath); |
| 242 | |
| 243 | /* NOTE: there is no check to see whether there's any usable data at all |
| 244 | * |
| 245 | * This would also have the advantage of being able to create some kind of |
| 246 | * 'fallback' menu - just in case that either menubar.xml is empty OR that |
| 247 | * its XML data is not valid, that way we would avoid displaying an |
| 248 | * unusable menubar without any functionality - if we decided to add another |
| 249 | * char * element to the commands structure in |
| 250 | * $FG_SRC/src/Main/fgcommands.cxx |
| 251 | * we could additionally save each function's (short) description and use |
| 252 | * this as label for the fallback PUI menubar item labels - as a workaround |
| 253 | * one might simply use the internal fgcommands and put them into the |
| 254 | * fallback menu, so that the user is at least able to re-init the menu |
| 255 | * loading - just in case there was some malformed XML in it |
| 256 | * (it happend to me ...) |
| 257 | */ |
| 258 | |
| 259 | make_menubar(targetpath); |
| 260 | } |
| 261 | |
| 262 | /* WARNING: We aren't yet doing any validation of what's found - but since |
| 263 | * this isn't done with menubar.xml either, it should not really matter |
| 264 | * right now. Although one should later on consider to validate the |
| 265 | * contents, whether they are representing a 'legal' menubar structure. |
| 266 | */ |
| 267 | void |
| 268 | FGPUIMenuBar::make_menubar(SGPropertyNode * props) |
| 269 | { |
| 270 | // Just in case. |
| 271 | destroy_menubar(); |
| 272 | _menuBar = new puMenuBar; |
| 273 | |
| 274 | const vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu"); |
| 275 | for (const auto& menuNode: menu_nodes) { |
| 276 | make_menu(menuNode); |
| 277 | } |
| 278 | |
| 279 | _menuBar->close(); |
| 280 | make_object_map(props); |
| 281 | |
| 282 | if (_visible) |
| 283 | _menuBar->reveal(); |
| 284 | else |
| 285 | _menuBar->hide(); |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | FGPUIMenuBar::destroy_menubar () |
| 290 | { |
| 291 | if ( _menuBar == 0 ) |
| 292 | return; |
| 293 | |
| 294 | hide(); |
| 295 | puDeleteObject(_menuBar); |
| 296 | |
| 297 | unsigned int i; |
| 298 | |
| 299 | // Delete all the character arrays |
| 300 | // we were forced to keep around for |
| 301 | // plib. |
| 302 | SG_LOG(SG_GENERAL, SG_BULK, "Deleting char arrays"); |
| 303 | for (i = 0; i < _char_arrays.size(); i++) { |
| 304 | for (int j = 0; _char_arrays[i][j] != 0; j++) { |
| 305 | free(_char_arrays[i][j]); // added with strdup |
| 306 | _char_arrays[i][j] = 0; |
| 307 | } |
| 308 | delete[] _char_arrays[i]; |
| 309 | _char_arrays[i] = 0; |
| 310 | } |
| 311 | |
| 312 | // Delete all the callback arrays |
| 313 | // we were forced to keep around for |
| 314 | // plib. |
| 315 | SG_LOG(SG_GENERAL, SG_BULK, "Deleting callback arrays"); |
| 316 | for (i = 0; i < _callback_arrays.size(); i++) |
| 317 | delete[] _callback_arrays[i]; |
| 318 | |
| 319 | SG_LOG(SG_GENERAL, SG_BULK, "Deleting user data arrays"); |
| 320 | for (i = 0; i < _userdata_arrays.size(); i++) |
| 321 | delete[] _userdata_arrays[i]; |
| 322 | |
| 323 | _menuBar = NULL; |
| 324 | _bindings.clear(); |
| 325 | _char_arrays.clear(); |
| 326 | _callback_arrays.clear(); |
| 327 | _userdata_arrays.clear(); |
| 328 | SG_LOG(SG_GENERAL, SG_BULK, "Done."); |
| 329 | } |
| 330 | |
| 331 | void |
| 332 | FGPUIMenuBar::make_object_map(SGPropertyNode * node) |
| 333 | { |
| 334 | unsigned int menu_index = 0; |
| 335 | vector<SGPropertyNode_ptr> menus = node->getChildren("menu"); |
| 336 | for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild(); |
| 337 | obj; obj = obj->getNextObject()) { |
| 338 | |
| 339 | // skip puPopupMenus. They are also children of _menuBar, |
| 340 | // but we access them via getUserData() (see below) |
| 341 | if (!(obj->getType() & PUCLASS_ONESHOT)) |
| 342 | continue; |
| 343 | |
| 344 | if (menu_index >= menus.size()) { |
| 345 | SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: " |
| 346 | << node->getPath() << "/menu[" << menu_index << ']'); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | SGPropertyNode *menu = menus.at(menu_index); |
| 351 | _objects[menu->getPath()] = obj; |
| 352 | add_enabled_listener(menu); |
| 353 | |
| 354 | puGroup *popup = (puGroup *)obj->getUserData(); |
| 355 | if (!popup) |
| 356 | continue; |
| 357 | |
| 358 | // the entries are for some reason reversed (last first), and we |
| 359 | // don't know yet how many there will be; so we collect first |
| 360 | vector<puObject *> e; |
| 361 | for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject()) |
| 362 | e.push_back(me); |
| 363 | |
| 364 | vector<SGPropertyNode_ptr> items = menu->getChildren("item"); |
| 365 | for (unsigned int i = 0; i < e.size(); i++) { |
| 366 | if (i >= items.size()) { |
| 367 | SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: " |
| 368 | << menu->getPath() << "/item[" << i << ']'); |
| 369 | break; |
| 370 | } |
| 371 | SGPropertyNode *item = items.at(e.size() - i - 1); |
| 372 | _objects[item->getPath()] = e[i]; |
| 373 | add_enabled_listener(item); |
| 374 | } |
| 375 | menu_index++; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | namespace { |
| 380 | |
| 381 | struct EnabledListener : SGPropertyChangeListener { |
| 382 | void valueChanged(SGPropertyNode *node) { |
| 383 | auto gui = globals->get_subsystem<NewGUI>(); |
| 384 | if (!gui) |
| 385 | return; |
| 386 | FGPUIMenuBar* menubar = static_cast<FGPUIMenuBar*>(gui->getMenuBar()); |
| 387 | if (menubar) |
| 388 | menubar->enable_item(node->getParent(), node->getBoolValue()); |
| 389 | } |
| 390 | }; |
| 391 | |
| 392 | } // of anonymous namespace |
| 393 | |
| 394 | void |
| 395 | FGPUIMenuBar::add_enabled_listener(SGPropertyNode * node) |
| 396 | { |
| 397 | if (!node->hasValue("enabled")) |
| 398 | node->setBoolValue("enabled", true); |
| 399 | |
| 400 | enable_item(node, node->getBoolValue("enabled")); |
| 401 | // REVIEW: Memory Leak - 3,920 (3,136 direct, 784 indirect) bytes in 98 blocks are definitely lost |
| 402 | node->getNode("enabled")->addChangeListener(new EnabledListener()); |
| 403 | } |
| 404 | |
| 405 | bool |
| 406 | FGPUIMenuBar::enable_item(const SGPropertyNode * node, bool state) |
| 407 | { |
| 408 | string path = node->getPath(); |
| 409 | if (_objects.find(path) == _objects.end()) { |
| 410 | SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable " |
| 411 | "non-existent menu item for node `" << path << '\''); |
| 412 | return false; |
| 413 | } |
| 414 | puObject *object = _objects[path]; |
| 415 | if (state) |
| 416 | object->activate(); |
| 417 | else |
| 418 | object->greyOut(); |
| 419 | |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | char ** |
| 424 | FGPUIMenuBar::make_char_array (int size) |
| 425 | { |
| 426 | char ** list = new char*[size+1]; |
| 427 | for (int i = 0; i <= size; i++) |
| 428 | list[i] = 0; |
| 429 | _char_arrays.push_back(list); |
| 430 | return list; |
| 431 | } |
| 432 | |
| 433 | puCallback * |
| 434 | FGPUIMenuBar::make_callback_array (int size) |
| 435 | { |
| 436 | puCallback * list = new puCallback[size+1]; |
| 437 | for (int i = 0; i <= size; i++) |
| 438 | list[i] = 0; |
| 439 | _callback_arrays.push_back(list); |
| 440 | return list; |
| 441 | } |
| 442 | |
| 443 | const vector<unique_ptr<SGBinding>> ** |
| 444 | FGPUIMenuBar::make_userdata_array (int size) |
| 445 | { |
| 446 | auto list = new const vector<unique_ptr<SGBinding>>*[size+1]; |
| 447 | for (int i = 0; i <= size; i++) { |
| 448 | list[i] = nullptr; |
| 449 | } |
| 450 | _userdata_arrays.push_back(list); |
| 451 | return list; |
| 452 | } |
| 453 | |
| 454 | // end of menubar.cxx |