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 helpCb (puObject *);
67 do_help_dialog (const SGPropertyNode * arg)
75 SGCommandMgr::command_t command;
76 } deprecated_dialogs [] = {
77 { "old-reinit-dialog", do_reinit_dialog },
78 #if defined(TR_HIRES_SNAP)
79 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
81 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
82 { "old-print-dialog", do_print_dialog },
84 { "old-properties-dialog", do_properties_dialog },
85 { "old-help-dialog", do_help_dialog },
90 add_deprecated_dialogs ()
92 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
93 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
94 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
95 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
96 deprecated_dialogs[i].command);
102 ////////////////////////////////////////////////////////////////////////
104 ////////////////////////////////////////////////////////////////////////
108 menu_callback (puObject * object)
110 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
111 gui->getMenuBar()->fireItem(object);
116 ////////////////////////////////////////////////////////////////////////
117 // Implementation of FGMenuBar.
118 ////////////////////////////////////////////////////////////////////////
121 FGMenuBar::FGMenuBar ()
127 FGMenuBar::~FGMenuBar ()
135 delete _menuBar; // FIXME: check if PUI owns the pointer
137 // FIXME: temporary commands to get at
138 // old, hard-coded dialogs.
139 add_deprecated_dialogs();
159 FGMenuBar::isVisible () const
165 FGMenuBar::fireItem (puObject * item)
167 const char * name = item->getLegend();
168 vector<FGBinding *> &bindings = _bindings[name];
169 int nBindings = bindings.size();
171 for (int i = 0; i < nBindings; i++)
176 FGMenuBar::make_menu (SGPropertyNode * node)
178 const char * name = strdup(node->getStringValue("label"));
179 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
181 int array_size = item_nodes.size();
183 char ** items = make_char_array(array_size);
184 puCallback * callbacks = make_callback_array(array_size);
186 for (unsigned int i = 0, j = item_nodes.size() - 1;
187 i < item_nodes.size();
190 // Set up the PUI entries for this item
191 items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
192 callbacks[j] = menu_callback;
194 // Load all the bindings for this item
195 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
196 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
198 for (unsigned int k = 0; k < bindings.size(); k++) {
200 SGPropertyNode *binding;
201 while (dest->getChild("binding", m))
204 binding = dest->getChild("binding", m, true);
205 copyProperties(bindings[k], binding);
206 _bindings[items[j]].push_back(new FGBinding(binding));
210 _menuBar->add_submenu(name, items, callbacks);
214 FGMenuBar::make_menubar ()
216 SGPropertyNode *targetpath;
218 targetpath = fgGetNode("/sim/menubar/default",true);
219 // fgLoadProps("gui/menubar.xml", targetpath);
221 /* NOTE: there is no check to see whether there's any usable data at all
223 * This would also have the advantage of being able to create some kind of
224 * 'fallback' menu - just in case that either menubar.xml is empty OR that
225 * its XML data is not valid, that way we would avoid displaying an
226 * unusable menubar without any functionality - if we decided to add another
227 * char * element to the commands structure in
228 * $FG_SRC/src/Main/fgcommands.cxx
229 * we could additionally save each function's (short) description and use
230 * this as label for the fallback PUI menubar item labels - as a workaround
231 * one might simply use the internal fgcommands and put them into the
232 * fallback menu, so that the user is at least able to re-init the menu
233 * loading - just in case there was some malformed XML in it
234 * (it happend to me ...)
237 make_menubar(targetpath);
240 /* WARNING: We aren't yet doing any validation of what's found - but since
241 * this isn't done with menubar.xml either, it should not really matter
242 * right now. Although one should later on consider to validate the
243 * contents, whether they are representing a 'legal' menubar structure.
246 FGMenuBar::make_menubar(SGPropertyNode * props)
250 _menuBar = new puMenuBar;
252 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
253 for (unsigned int i = 0; i < menu_nodes.size(); i++)
254 make_menu(menu_nodes[i]);
257 make_object_map(props);
266 FGMenuBar::destroy_menubar ()
272 puDeleteObject(_menuBar);
276 // Delete all the character arrays
277 // we were forced to keep around for
279 SG_LOG(SG_GENERAL, SG_INFO, "Deleting char arrays");
280 for (i = 0; i < _char_arrays.size(); i++) {
281 for (int j = 0; _char_arrays[i][j] != 0; j++)
282 free(_char_arrays[i][j]); // added with strdup
283 delete[] _char_arrays[i];
286 // Delete all the callback arrays
287 // we were forced to keep around for
289 SG_LOG(SG_GENERAL, SG_INFO, "Deleting callback arrays");
290 for (i = 0; i < _callback_arrays.size(); i++)
291 delete[] _callback_arrays[i];
293 // Delete all those bindings
294 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings");
295 map<string,vector<FGBinding *> >::iterator it;
296 it = _bindings.begin();
297 for (it = _bindings.begin(); it != _bindings.end(); it++) {
298 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings for " << it->first);
299 for ( i = 0; i < it->second.size(); i++ )
300 delete it->second[i];
303 SG_LOG(SG_GENERAL, SG_INFO, "Done.");
307 FGMenuBar::make_object_map(SGPropertyNode * node)
309 unsigned int menu_index = 0;
310 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
311 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
312 obj; obj = obj->getNextObject()) {
314 // skip puPopupMenus. They are also children of _menuBar,
315 // but we access them via getUserData() (see below)
316 if (!(obj->getType() & PUCLASS_ONESHOT))
319 if (menu_index >= menus.size()) {
320 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
321 << node->getPath() << "/menu[" << menu_index << ']');
325 SGPropertyNode *menu = menus.at(menu_index);
326 _objects[menu->getPath()] = obj;
327 add_enabled_listener(menu);
329 puGroup *popup = (puGroup *)obj->getUserData();
333 // the entries are for some reason reversed (last first), and we
334 // don't know yet how many there will be; so we collect first
335 vector<puObject *> e;
336 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
339 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
340 for (unsigned int i = 0; i < e.size(); i++) {
341 if (i >= items.size()) {
342 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
343 << menu->getPath() << "/item[" << i << ']');
346 SGPropertyNode *item = items.at(e.size() - i - 1);
347 _objects[item->getPath()] = e[i];
348 add_enabled_listener(item);
354 struct EnabledListener : SGPropertyChangeListener {
355 void valueChanged(SGPropertyNode *node) {
356 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
359 FGMenuBar *menubar = gui->getMenuBar();
361 menubar->enable_item(node->getParent(), node->getBoolValue());
366 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
368 if (!node->hasValue("enabled"))
369 node->setBoolValue("enabled", true);
371 enable_item(node, node->getBoolValue("enabled"));
372 node->getNode("enabled")->addChangeListener(new EnabledListener());
376 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
378 const char *path = node->getPath();
379 if (_objects.find(path) == _objects.end()) {
380 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
381 "non-existent menu item for node `" << path << '\'');
384 puObject *object = _objects[path];
394 FGMenuBar::make_char_array (int size)
396 char ** list = new char*[size+1];
397 for (int i = 0; i <= size; i++)
399 _char_arrays.push_back(list);
404 FGMenuBar::make_callback_array (int size)
406 puCallback * list = new puCallback[size+1];
407 for (int i = 0; i <= size; i++)
409 _callback_arrays.push_back(list);
413 // end of menubar.cxx