X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FGUI%2Fmenubar.cxx;h=513277dfc0f97f1dbd5b75feff4166996033b95e;hb=5cad5aa7da2476ca8323a61f81dea59676dca085;hp=43e30fd8126bd9ab7183235c2a277f2ef4d7c624;hpb=d819c42184df5db486d43939b625162eb4ecb76a;p=flightgear.git diff --git a/src/GUI/menubar.cxx b/src/GUI/menubar.cxx index 43e30fd81..513277dfc 100644 --- a/src/GUI/menubar.cxx +++ b/src/GUI/menubar.cxx @@ -1,12 +1,14 @@ +#ifdef HAVE_CONFIG_H +# include +#endif + #include #include #include #include +#include #include
-#include
- -#include #include "new_gui.hxx" #include "menubar.hxx" @@ -24,175 +26,31 @@ // user-configured dialogs and new commands where necessary. //////////////////////////////////////////////////////////////////////// -extern void saveFlight (puObject *); -static bool -do_save_dialog (const SGPropertyNode * arg) -{ - saveFlight(0); - return true; -} - -extern void loadFlight (puObject *); -static bool -do_load_dialog (const SGPropertyNode * arg) -{ - loadFlight(0); - return true; -} - -extern void reInit (puObject *); -static bool -do_reinit_dialog (const SGPropertyNode * arg) -{ - reInit(0); - return true; -} - #if defined(TR_HIRES_SNAP) -extern void dumpHiResSnapShot (puObject *); +extern void dumpHiResSnapShot (); static bool do_hires_snapshot_dialog (const SGPropertyNode * arg) { - dumpHiResSnapShot(0); + dumpHiResSnapShot(); return true; } #endif // TR_HIRES_SNAP #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__) -extern void printScreen (puObject *); +extern void printScreen (); static bool do_print_dialog (const SGPropertyNode * arg) { - printScreen(0); + printScreen(); return true; } #endif -extern void PilotOffsetAdjust (puObject *); -static bool -do_pilot_offset_dialog (const SGPropertyNode * arg) -{ - PilotOffsetAdjust(0); - return true; -} - -extern void fgHUDalphaAdjust (puObject *); -static bool -do_hud_alpha_dialog (const SGPropertyNode * arg) -{ - fgHUDalphaAdjust(0); - return true; -} - -extern void prop_pickerView (puObject *); -static bool -do_properties_dialog (const SGPropertyNode * arg) -{ - prop_pickerView(0); - return true; -} - -extern void fgPresetAirport (puObject *); -static bool -do_preset_airport_dialog (const SGPropertyNode * arg) -{ - fgPresetAirport(0); - return true; -} - -extern void fgPresetRunway (puObject *); -static bool -do_preset_runway_dialog (const SGPropertyNode * arg) -{ - fgPresetRunway(0); - return true; -} - -extern void fgPresetOffsetDistance (puObject *); -static bool -do_preset_offset_distance_dialog (const SGPropertyNode * arg) -{ - fgPresetOffsetDistance(0); - return true; -} - -extern void fgPresetAltitude (puObject *); -static bool -do_preset_altitude_dialog (const SGPropertyNode * arg) -{ - fgPresetAltitude(0); - return true; -} - -extern void fgPresetGlideslope (puObject *); -static bool -do_preset_glideslope_dialog (const SGPropertyNode * arg) -{ - fgPresetGlideslope(0); - return true; -} - -extern void fgPresetAirspeed (puObject *); -static bool -do_preset_airspeed_dialog (const SGPropertyNode * arg) -{ - fgPresetAirspeed(0); - return true; -} - -extern void fgPresetCommit (puObject *); -static bool -do_preset_commit_dialog (const SGPropertyNode * arg) -{ - fgPresetCommit(0); - return true; -} - -extern void AddWayPoint (puObject *); -static bool -do_ap_add_waypoint_dialog (const SGPropertyNode * arg) -{ - AddWayPoint(0); - return true; -} - -extern void PopWayPoint (puObject *); -static bool -do_ap_pop_waypoint_dialog (const SGPropertyNode * arg) -{ - PopWayPoint(0); - return true; -} - -extern void ClearRoute (puObject *); -static bool -do_ap_clear_route_dialog (const SGPropertyNode * arg) -{ - ClearRoute(0); - return true; -} - -extern void fgAPAdjust (puObject *); -static bool -do_ap_adjust_dialog (const SGPropertyNode * arg) -{ - fgAPAdjust(0); - return true; -} - -extern void fgLatLonFormatToggle (puObject *); -static bool -do_lat_lon_format_dialog (const SGPropertyNode * arg) -{ - fgLatLonFormatToggle(0); - return true; -} - -extern void helpCb (puObject *); +extern void helpCb (); static bool do_help_dialog (const SGPropertyNode * arg) { - helpCb(0); + helpCb(); return true; } @@ -200,30 +58,12 @@ static struct { const char * name; SGCommandMgr::command_t command; } deprecated_dialogs [] = { - { "old-save-dialog", do_save_dialog }, - { "old-load-dialog", do_load_dialog }, - { "old-reinit_dialog", do_reinit_dialog }, #if defined(TR_HIRES_SNAP) { "old-hires-snapshot-dialog", do_hires_snapshot_dialog }, #endif #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__) { "old-print-dialog", do_print_dialog }, #endif - { "old-pilot-offset-dialog", do_pilot_offset_dialog }, - { "old-hud-alpha-dialog", do_hud_alpha_dialog }, - { "old-properties-dialog", do_properties_dialog }, - { "old-preset-airport-dialog", do_preset_airport_dialog }, - { "old-preset-runway-dialog", do_preset_runway_dialog }, - { "old-preset-offset-distance-dialog", do_preset_offset_distance_dialog }, - { "old-preset-altitude-dialog", do_preset_altitude_dialog }, - { "old-preset-glideslope-dialog", do_preset_glideslope_dialog }, - { "old-preset-airspeed-dialog", do_preset_airspeed_dialog }, - { "old-preset-commit-dialog", do_preset_commit_dialog }, - { "old-ap-add-waypoint-dialog", do_ap_add_waypoint_dialog }, - { "old-ap-pop-waypoint-dialog", do_ap_pop_waypoint_dialog }, - { "old-ap-clear-route-dialog", do_ap_clear_route_dialog }, - { "old-ap-adjust-dialog", do_ap_adjust_dialog }, - { "old-lat-lon-format-dialog", do_lat_lon_format_dialog }, { "old-help-dialog", do_help_dialog }, { 0, 0 } }; @@ -268,48 +108,14 @@ FGMenuBar::FGMenuBar () FGMenuBar::~FGMenuBar () { - hide(); - puDeleteObject(_menuBar); - - int i; - - // Delete all the character arrays - // we were forced to keep around for - // plib. - std::cerr << "Deleting char arrays\n"; - for (i = 0; i < _char_arrays.size(); i++) { - for (int j = 0; _char_arrays[i][j] != 0; j++) - free(_char_arrays[i][j]); // added with strdup - delete _char_arrays[i]; - } - - // Delete all the callback arrays - // we were forced to keep around for - // plib. - std::cerr << "Deleting callback arrays\n"; - for (i = 0; i < _callback_arrays.size(); i++) - delete _callback_arrays[i]; - - // Delete all those bindings - std::cerr << "Deleting bindings\n"; - map >::iterator it; - it = _bindings.begin(); - for (it = _bindings.begin(); it != _bindings.end(); it++) { - std::cerr << "Deleting bindings for " << it->first << std::endl; - for (int i = 0; i < it->second.size(); i++) - delete it->second[i]; - } - - std::cerr << "Done.\n"; + destroy_menubar(); } void FGMenuBar::init () { - if (_menuBar != 0) // FIXME: check if PUI owns the pointer - delete _menuBar; + delete _menuBar; // FIXME: check if PUI owns the pointer make_menubar(); - // FIXME: temporary commands to get at // old, hard-coded dialogs. add_deprecated_dialogs(); @@ -341,7 +147,7 @@ void FGMenuBar::fireItem (puObject * item) { const char * name = item->getLegend(); - vector &bindings = _bindings[name]; + vector &bindings = _bindings[name]; int nBindings = bindings.size(); for (int i = 0; i < nBindings; i++) @@ -359,20 +165,28 @@ FGMenuBar::make_menu (SGPropertyNode * node) char ** items = make_char_array(array_size); puCallback * callbacks = make_callback_array(array_size); - for (int i = 0, j = item_nodes.size() - 1; + for (unsigned int i = 0, j = item_nodes.size() - 1; i < item_nodes.size(); i++, j--) { - + // Set up the PUI entries for this item items[j] = strdup((char *)item_nodes[i]->getStringValue("label")); callbacks[j] = menu_callback; // Load all the bindings for this item - vector binding_nodes = - item_nodes[i]->getChildren("binding"); - - for (int k = 0; k < binding_nodes.size(); k++) - _bindings[items[j]].push_back(new FGBinding(binding_nodes[k])); + vector bindings = item_nodes[i]->getChildren("binding"); + SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true); + + for (unsigned int k = 0; k < bindings.size(); k++) { + unsigned int m = 0; + SGPropertyNode_ptr binding; + while (dest->getChild("binding", m)) + m++; + + binding = dest->getChild("binding", m, true); + copyProperties(bindings[k], binding); + _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props())); + } } _menuBar->add_submenu(name, items, callbacks); @@ -381,21 +195,182 @@ FGMenuBar::make_menu (SGPropertyNode * node) void FGMenuBar::make_menubar () { + SGPropertyNode *targetpath; + + targetpath = fgGetNode("/sim/menubar/default",true); + // fgLoadProps("gui/menubar.xml", targetpath); + + /* NOTE: there is no check to see whether there's any usable data at all + * + * This would also have the advantage of being able to create some kind of + * 'fallback' menu - just in case that either menubar.xml is empty OR that + * its XML data is not valid, that way we would avoid displaying an + * unusable menubar without any functionality - if we decided to add another + * char * element to the commands structure in + * $FG_SRC/src/Main/fgcommands.cxx + * we could additionally save each function's (short) description and use + * this as label for the fallback PUI menubar item labels - as a workaround + * one might simply use the internal fgcommands and put them into the + * fallback menu, so that the user is at least able to re-init the menu + * loading - just in case there was some malformed XML in it + * (it happend to me ...) + */ + + make_menubar(targetpath); +} + +/* WARNING: We aren't yet doing any validation of what's found - but since + * this isn't done with menubar.xml either, it should not really matter + * right now. Although one should later on consider to validate the + * contents, whether they are representing a 'legal' menubar structure. + */ +void +FGMenuBar::make_menubar(SGPropertyNode * props) +{ + // Just in case. + destroy_menubar(); _menuBar = new puMenuBar; - SGPropertyNode props; - fgLoadProps("gui/menubar.xml", &props); - vector menu_nodes = props.getChildren("menu"); - for (int i = 0; i < menu_nodes.size(); i++) + vector menu_nodes = props->getChildren("menu"); + for (unsigned int i = 0; i < menu_nodes.size(); i++) make_menu(menu_nodes[i]); _menuBar->close(); + make_object_map(props); + if (_visible) _menuBar->reveal(); else _menuBar->hide(); } +void +FGMenuBar::destroy_menubar () +{ + if ( _menuBar == 0 ) + return; + + hide(); + puDeleteObject(_menuBar); + + unsigned int i; + + // Delete all the character arrays + // we were forced to keep around for + // plib. + SG_LOG(SG_GENERAL, SG_INFO, "Deleting char arrays"); + for (i = 0; i < _char_arrays.size(); i++) { + for (int j = 0; _char_arrays[i][j] != 0; j++) + free(_char_arrays[i][j]); // added with strdup + delete[] _char_arrays[i]; + } + + // Delete all the callback arrays + // we were forced to keep around for + // plib. + SG_LOG(SG_GENERAL, SG_INFO, "Deleting callback arrays"); + for (i = 0; i < _callback_arrays.size(); i++) + delete[] _callback_arrays[i]; + + // Delete all those bindings + SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings"); + map >::iterator it; + for (it = _bindings.begin(); it != _bindings.end(); it++) { + SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings for " << it->first); + for ( i = 0; i < it->second.size(); i++ ) + delete it->second[i]; + } + + SG_LOG(SG_GENERAL, SG_INFO, "Done."); +} + +void +FGMenuBar::make_object_map(SGPropertyNode * node) +{ + unsigned int menu_index = 0; + vector menus = node->getChildren("menu"); + for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild(); + obj; obj = obj->getNextObject()) { + + // skip puPopupMenus. They are also children of _menuBar, + // but we access them via getUserData() (see below) + if (!(obj->getType() & PUCLASS_ONESHOT)) + continue; + + if (menu_index >= menus.size()) { + SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: " + << node->getPath() << "/menu[" << menu_index << ']'); + return; + } + + SGPropertyNode *menu = menus.at(menu_index); + _objects[menu->getPath()] = obj; + add_enabled_listener(menu); + + puGroup *popup = (puGroup *)obj->getUserData(); + if (!popup) + continue; + + // the entries are for some reason reversed (last first), and we + // don't know yet how many there will be; so we collect first + vector e; + for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject()) + e.push_back(me); + + vector items = menu->getChildren("item"); + for (unsigned int i = 0; i < e.size(); i++) { + if (i >= items.size()) { + SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: " + << menu->getPath() << "/item[" << i << ']'); + break; + } + SGPropertyNode *item = items.at(e.size() - i - 1); + _objects[item->getPath()] = e[i]; + add_enabled_listener(item); + } + menu_index++; + } +} + +struct EnabledListener : SGPropertyChangeListener { + void valueChanged(SGPropertyNode *node) { + NewGUI * gui = (NewGUI *)globals->get_subsystem("gui"); + if (!gui) + return; + FGMenuBar *menubar = gui->getMenuBar(); + if (menubar) + menubar->enable_item(node->getParent(), node->getBoolValue()); + } +}; + +void +FGMenuBar::add_enabled_listener(SGPropertyNode * node) +{ + if (!node->hasValue("enabled")) + node->setBoolValue("enabled", true); + + enable_item(node, node->getBoolValue("enabled")); + node->getNode("enabled")->addChangeListener(new EnabledListener()); +} + +bool +FGMenuBar::enable_item(const SGPropertyNode * node, bool state) +{ + const char *path = node->getPath(); + if (_objects.find(path) == _objects.end()) { + SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable " + "non-existent menu item for node `" << path << '\''); + return false; + } + puObject *object = _objects[path]; + if (state) + object->activate(); + else + object->greyOut(); + + return true; +} + char ** FGMenuBar::make_char_array (int size) {