8 #include <simgear/debug/logstream.hxx>
10 #include <Input/input.hxx>
11 #include <Main/globals.hxx>
13 #include "new_gui.hxx"
14 #include "menubar.hxx"
18 ////////////////////////////////////////////////////////////////////////
21 // Deprecated wrappers for old menu commands.
23 // DO NOT ADD TO THESE. THEY WILL BE DELETED SOON!
25 // These are defined in gui_funcs.cxx. They should be replaced with
26 // user-configured dialogs and new commands where necessary.
27 ////////////////////////////////////////////////////////////////////////
29 extern void reInit (puObject *);
31 do_reinit_dialog (const SGPropertyNode * arg)
37 #if defined(TR_HIRES_SNAP)
38 extern void dumpHiResSnapShot (puObject *);
40 do_hires_snapshot_dialog (const SGPropertyNode * arg)
45 #endif // TR_HIRES_SNAP
47 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
48 extern void printScreen (puObject *);
50 do_print_dialog (const SGPropertyNode * arg)
57 extern void prop_pickerView (puObject *);
59 do_properties_dialog (const SGPropertyNode * arg)
65 extern void fgLatLonFormatToggle (puObject *);
67 do_lat_lon_format_dialog (const SGPropertyNode * arg)
69 fgLatLonFormatToggle(0);
73 extern void helpCb (puObject *);
75 do_help_dialog (const SGPropertyNode * arg)
83 SGCommandMgr::command_t command;
84 } deprecated_dialogs [] = {
85 { "old-reinit-dialog", do_reinit_dialog },
86 #if defined(TR_HIRES_SNAP)
87 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
89 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
90 { "old-print-dialog", do_print_dialog },
92 { "old-properties-dialog", do_properties_dialog },
93 { "old-lat-lon-format-dialog", do_lat_lon_format_dialog },
94 { "old-help-dialog", do_help_dialog },
99 add_deprecated_dialogs ()
101 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
102 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
103 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
104 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
105 deprecated_dialogs[i].command);
111 ////////////////////////////////////////////////////////////////////////
113 ////////////////////////////////////////////////////////////////////////
117 menu_callback (puObject * object)
119 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
120 gui->getMenuBar()->fireItem(object);
125 ////////////////////////////////////////////////////////////////////////
126 // Implementation of FGMenuBar.
127 ////////////////////////////////////////////////////////////////////////
130 FGMenuBar::FGMenuBar ()
136 FGMenuBar::~FGMenuBar ()
144 delete _menuBar; // FIXME: check if PUI owns the pointer
146 // FIXME: temporary commands to get at
147 // old, hard-coded dialogs.
148 add_deprecated_dialogs();
168 FGMenuBar::isVisible () const
174 FGMenuBar::fireItem (puObject * item)
176 const char * name = item->getLegend();
177 vector<FGBinding *> &bindings = _bindings[name];
178 int nBindings = bindings.size();
180 for (int i = 0; i < nBindings; i++)
185 FGMenuBar::make_menu (SGPropertyNode * node)
187 const char * name = strdup(node->getStringValue("label"));
188 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
190 int array_size = item_nodes.size();
192 char ** items = make_char_array(array_size);
193 puCallback * callbacks = make_callback_array(array_size);
195 for (unsigned int i = 0, j = item_nodes.size() - 1;
196 i < item_nodes.size();
199 // Set up the PUI entries for this item
200 items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
201 callbacks[j] = menu_callback;
203 // Load all the bindings for this item
204 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
205 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
207 for (unsigned int k = 0; k < bindings.size(); k++) {
209 SGPropertyNode *binding;
210 while (dest->getChild("binding", m))
213 binding = dest->getChild("binding", m, true);
214 copyProperties(bindings[k], binding);
215 _bindings[items[j]].push_back(new FGBinding(binding));
219 _menuBar->add_submenu(name, items, callbacks);
223 FGMenuBar::make_menubar ()
225 SGPropertyNode *targetpath;
227 targetpath = fgGetNode("/sim/menubar/default",true);
228 // fgLoadProps("gui/menubar.xml", targetpath);
230 /* NOTE: there is no check to see whether there's any usable data at all
232 * This would also have the advantage of being able to create some kind of
233 * 'fallback' menu - just in case that either menubar.xml is empty OR that
234 * its XML data is not valid, that way we would avoid displaying an
235 * unusable menubar without any functionality - if we decided to add another
236 * char * element to the commands structure in
237 * $FG_SRC/src/Main/fgcommands.cxx
238 * we could additionally save each function's (short) description and use
239 * this as label for the fallback PUI menubar item labels - as a workaround
240 * one might simply use the internal fgcommands and put them into the
241 * fallback menu, so that the user is at least able to re-init the menu
242 * loading - just in case there was some malformed XML in it
243 * (it happend to me ...)
246 make_menubar(targetpath);
249 /* WARNING: We aren't yet doing any validation of what's found - but since
250 * this isn't done with menubar.xml either, it should not really matter
251 * right now. Although one should later on consider to validate the
252 * contents, whether they are representing a 'legal' menubar structure.
255 FGMenuBar::make_menubar(SGPropertyNode * props)
259 _menuBar = new puMenuBar;
261 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
262 for (unsigned int i = 0; i < menu_nodes.size(); i++)
263 make_menu(menu_nodes[i]);
266 make_object_map(props);
275 FGMenuBar::destroy_menubar ()
281 puDeleteObject(_menuBar);
285 // Delete all the character arrays
286 // we were forced to keep around for
288 SG_LOG(SG_GENERAL, SG_INFO, "Deleting char arrays");
289 for (i = 0; i < _char_arrays.size(); i++) {
290 for (int j = 0; _char_arrays[i][j] != 0; j++)
291 free(_char_arrays[i][j]); // added with strdup
292 delete[] _char_arrays[i];
295 // Delete all the callback arrays
296 // we were forced to keep around for
298 SG_LOG(SG_GENERAL, SG_INFO, "Deleting callback arrays");
299 for (i = 0; i < _callback_arrays.size(); i++)
300 delete[] _callback_arrays[i];
302 // Delete all those bindings
303 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings");
304 map<string,vector<FGBinding *> >::iterator it;
305 it = _bindings.begin();
306 for (it = _bindings.begin(); it != _bindings.end(); it++) {
307 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings for " << it->first);
308 for ( i = 0; i < it->second.size(); i++ )
309 delete it->second[i];
312 SG_LOG(SG_GENERAL, SG_INFO, "Done.");
316 FGMenuBar::make_object_map(SGPropertyNode * node)
318 unsigned int menu_index = 0;
319 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
320 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
321 obj; obj = obj->getNextObject()) {
323 // skip puPopupMenus. They are also children of _menuBar,
324 // but we access them via getUserData() (see below)
325 if (!(obj->getType() & PUCLASS_ONESHOT))
328 if (menu_index >= menus.size()) {
329 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
330 << node->getPath() << "/menu[" << menu_index << ']');
334 SGPropertyNode *menu = menus.at(menu_index);
335 _objects[menu->getPath()] = obj;
336 add_enabled_listener(menu);
338 puGroup *popup = (puGroup *)obj->getUserData();
342 // the entries are for some reason reversed (last first), and we
343 // don't know yet how many there will be; so we collect first
344 vector<puObject *> e;
345 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
348 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
349 for (unsigned int i = 0; i < e.size(); i++) {
350 if (i >= items.size()) {
351 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
352 << menu->getPath() << "/item[" << i << ']');
355 SGPropertyNode *item = items.at(e.size() - i - 1);
356 _objects[item->getPath()] = e[i];
357 add_enabled_listener(item);
363 struct EnabledListener : SGPropertyChangeListener {
364 void valueChanged(SGPropertyNode *node) {
365 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
368 FGMenuBar *menubar = gui->getMenuBar();
370 menubar->enable_item(node->getParent(), node->getBoolValue());
375 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
377 if (!node->hasValue("enabled"))
378 node->setBoolValue("enabled", true);
380 enable_item(node, node->getBoolValue("enabled"));
381 node->getNode("enabled")->addChangeListener(new EnabledListener());
385 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
387 const char *path = node->getPath();
388 if (_objects.find(path) == _objects.end()) {
389 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
390 "non-existent menu item for node `" << path << '\'');
393 puObject *object = _objects[path];
403 FGMenuBar::make_char_array (int size)
405 char ** list = new char*[size+1];
406 for (int i = 0; i <= size; i++)
408 _char_arrays.push_back(list);
413 FGMenuBar::make_callback_array (int size)
415 puCallback * list = new puCallback[size+1];
416 for (int i = 0; i <= size; i++)
418 _callback_arrays.push_back(list);
422 // end of menubar.cxx