8 #include <simgear/debug/logstream.hxx>
10 #include <Autopilot/auto_gui.hxx>
11 #include <Input/input.hxx>
12 #include <Main/globals.hxx>
13 #include <Main/fg_props.hxx>
15 #include "new_gui.hxx"
16 #include "menubar.hxx"
20 ////////////////////////////////////////////////////////////////////////
23 // Deprecated wrappers for old menu commands.
25 // DO NOT ADD TO THESE. THEY WILL BE DELETED SOON!
27 // These are defined in gui_funcs.cxx. They should be replaced with
28 // user-configured dialogs and new commands where necessary.
29 ////////////////////////////////////////////////////////////////////////
31 extern void reInit (puObject *);
33 do_reinit_dialog (const SGPropertyNode * arg)
39 #if defined(TR_HIRES_SNAP)
40 extern void dumpHiResSnapShot (puObject *);
42 do_hires_snapshot_dialog (const SGPropertyNode * arg)
47 #endif // TR_HIRES_SNAP
49 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
50 extern void printScreen (puObject *);
52 do_print_dialog (const SGPropertyNode * arg)
59 extern void prop_pickerView (puObject *);
61 do_properties_dialog (const SGPropertyNode * arg)
67 extern void AddWayPoint (puObject *);
69 do_ap_add_waypoint_dialog (const SGPropertyNode * arg)
75 extern void PopWayPoint (puObject *);
77 do_ap_pop_waypoint_dialog (const SGPropertyNode * arg)
83 extern void ClearRoute (puObject *);
85 do_ap_clear_route_dialog (const SGPropertyNode * arg)
92 extern void fgAPAdjust (puObject *);
94 do_ap_adjust_dialog (const SGPropertyNode * arg)
101 extern void fgLatLonFormatToggle (puObject *);
103 do_lat_lon_format_dialog (const SGPropertyNode * arg)
105 fgLatLonFormatToggle(0);
109 extern void helpCb (puObject *);
111 do_help_dialog (const SGPropertyNode * arg)
119 SGCommandMgr::command_t command;
120 } deprecated_dialogs [] = {
121 { "old-reinit-dialog", do_reinit_dialog },
122 #if defined(TR_HIRES_SNAP)
123 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
125 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
126 { "old-print-dialog", do_print_dialog },
128 { "old-properties-dialog", do_properties_dialog },
129 { "old-ap-add-waypoint-dialog", do_ap_add_waypoint_dialog },
130 { "old-ap-pop-waypoint-dialog", do_ap_pop_waypoint_dialog },
131 { "old-ap-clear-route-dialog", do_ap_clear_route_dialog },
132 { "old-lat-lon-format-dialog", do_lat_lon_format_dialog },
133 { "old-help-dialog", do_help_dialog },
138 add_deprecated_dialogs ()
140 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
141 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
142 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
143 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
144 deprecated_dialogs[i].command);
150 ////////////////////////////////////////////////////////////////////////
152 ////////////////////////////////////////////////////////////////////////
156 menu_callback (puObject * object)
158 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
159 gui->getMenuBar()->fireItem(object);
164 ////////////////////////////////////////////////////////////////////////
165 // Implementation of FGMenuBar.
166 ////////////////////////////////////////////////////////////////////////
169 FGMenuBar::FGMenuBar ()
175 FGMenuBar::~FGMenuBar ()
183 delete _menuBar; // FIXME: check if PUI owns the pointer
185 // FIXME: temporary commands to get at
186 // old, hard-coded dialogs.
187 add_deprecated_dialogs();
207 FGMenuBar::isVisible () const
213 FGMenuBar::fireItem (puObject * item)
215 const char * name = item->getLegend();
216 vector<FGBinding *> &bindings = _bindings[name];
217 int nBindings = bindings.size();
219 for (int i = 0; i < nBindings; i++)
224 FGMenuBar::make_menu (SGPropertyNode * node)
226 const char * name = strdup(node->getStringValue("label"));
227 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
229 int array_size = item_nodes.size();
231 char ** items = make_char_array(array_size);
232 puCallback * callbacks = make_callback_array(array_size);
234 for (unsigned int i = 0, j = item_nodes.size() - 1;
235 i < item_nodes.size();
238 // Set up the PUI entries for this item
239 items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
240 callbacks[j] = menu_callback;
242 // Load all the bindings for this item
243 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
244 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
246 for (unsigned int k = 0; k < bindings.size(); k++) {
248 SGPropertyNode *binding;
249 while (dest->getChild("binding", m))
252 binding = dest->getChild("binding", m, true);
253 copyProperties(bindings[k], binding);
254 _bindings[items[j]].push_back(new FGBinding(binding));
258 _menuBar->add_submenu(name, items, callbacks);
262 FGMenuBar::make_menubar ()
264 SGPropertyNode *targetpath;
266 targetpath = fgGetNode("/sim/menubar/default",true);
267 // fgLoadProps("gui/menubar.xml", targetpath);
269 /* NOTE: there is no check to see whether there's any usable data at all
271 * This would also have the advantage of being able to create some kind of
272 * 'fallback' menu - just in case that either menubar.xml is empty OR that
273 * its XML data is not valid, that way we would avoid displaying an
274 * unusable menubar without any functionality - if we decided to add another
275 * char * element to the commands structure in
276 * $FG_SRC/src/Main/fgcommands.cxx
277 * we could additionally save each function's (short) description and use
278 * this as label for the fallback PUI menubar item labels - as a workaround
279 * one might simply use the internal fgcommands and put them into the
280 * fallback menu, so that the user is at least able to re-init the menu
281 * loading - just in case there was some malformed XML in it
282 * (it happend to me ...)
285 make_menubar(targetpath);
288 /* WARNING: We aren't yet doing any validation of what's found - but since
289 * this isn't done with menubar.xml either, it should not really matter
290 * right now. Although one should later on consider to validate the
291 * contents, whether they are representing a 'legal' menubar structure.
294 FGMenuBar::make_menubar(SGPropertyNode * props)
298 _menuBar = new puMenuBar;
300 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
301 for (unsigned int i = 0; i < menu_nodes.size(); i++)
302 make_menu(menu_nodes[i]);
305 make_object_map(props);
314 FGMenuBar::destroy_menubar ()
320 puDeleteObject(_menuBar);
324 // Delete all the character arrays
325 // we were forced to keep around for
327 SG_LOG(SG_GENERAL, SG_INFO, "Deleting char arrays");
328 for (i = 0; i < _char_arrays.size(); i++) {
329 for (int j = 0; _char_arrays[i][j] != 0; j++)
330 free(_char_arrays[i][j]); // added with strdup
331 delete[] _char_arrays[i];
334 // Delete all the callback arrays
335 // we were forced to keep around for
337 SG_LOG(SG_GENERAL, SG_INFO, "Deleting callback arrays");
338 for (i = 0; i < _callback_arrays.size(); i++)
339 delete[] _callback_arrays[i];
341 // Delete all those bindings
342 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings");
343 map<string,vector<FGBinding *> >::iterator it;
344 it = _bindings.begin();
345 for (it = _bindings.begin(); it != _bindings.end(); it++) {
346 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings for " << it->first);
347 for ( i = 0; i < it->second.size(); i++ )
348 delete it->second[i];
351 SG_LOG(SG_GENERAL, SG_INFO, "Done.");
355 FGMenuBar::make_object_map(SGPropertyNode * node)
357 unsigned int menu_index = 0;
358 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
359 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
360 obj; obj = obj->getNextObject()) {
362 // skip puPopupMenus. They are also children of _menuBar,
363 // but we access them via getUserData() (see below)
364 if (!(obj->getType() & PUCLASS_ONESHOT))
367 if (menu_index >= menus.size()) {
368 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
369 << node->getPath() << "/menu[" << menu_index << ']');
373 SGPropertyNode *menu = menus.at(menu_index);
374 _objects[menu->getPath()] = obj;
375 add_enabled_listener(menu);
377 puGroup *popup = (puGroup *)obj->getUserData();
381 // the entries are for some reason reversed (last first), and we
382 // don't know yet how many there will be; so we collect first
383 vector<puObject *> e;
384 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
387 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
388 for (unsigned int i = 0; i < e.size(); i++) {
389 if (i >= items.size()) {
390 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
391 << menu->getPath() << "/item[" << i << ']');
394 SGPropertyNode *item = items.at(e.size() - i - 1);
395 _objects[item->getPath()] = e[i];
396 add_enabled_listener(item);
402 struct EnabledListener : SGPropertyChangeListener {
403 void valueChanged(SGPropertyNode *node) {
404 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
407 FGMenuBar *menubar = gui->getMenuBar();
409 menubar->enable_item(node->getParent(), node->getBoolValue());
414 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
416 if (!node->hasValue("enabled"))
417 node->setBoolValue("enabled", true);
419 enable_item(node, node->getBoolValue("enabled"));
420 node->getNode("enabled")->addChangeListener(new EnabledListener());
424 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
426 const char *path = node->getPath();
427 if (_objects.find(path) == _objects.end()) {
428 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
429 "non-existent menu item for node `" << path << '\'');
432 puObject *object = _objects[path];
442 FGMenuBar::make_char_array (int size)
444 char ** list = new char*[size+1];
445 for (int i = 0; i <= size; i++)
447 _char_arrays.push_back(list);
452 FGMenuBar::make_callback_array (int size)
454 puCallback * list = new puCallback[size+1];
455 for (int i = 0; i <= size; i++)
457 _callback_arrays.push_back(list);
461 // end of menubar.cxx