8 #include <simgear/debug/logstream.hxx>
9 #include <simgear/structure/SGBinding.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 #if defined(TR_HIRES_SNAP)
30 extern void dumpHiResSnapShot ();
32 do_hires_snapshot_dialog (const SGPropertyNode * arg)
37 #endif // TR_HIRES_SNAP
39 extern void helpCb ();
41 do_help_dialog (const SGPropertyNode * arg)
49 SGCommandMgr::command_t command;
50 } deprecated_dialogs [] = {
51 #if defined(TR_HIRES_SNAP)
52 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
54 { "old-help-dialog", do_help_dialog },
59 add_deprecated_dialogs ()
61 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
62 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
63 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
64 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
65 deprecated_dialogs[i].command);
71 ////////////////////////////////////////////////////////////////////////
73 ////////////////////////////////////////////////////////////////////////
77 menu_callback (puObject * object)
79 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
80 gui->getMenuBar()->fireItem(object);
85 ////////////////////////////////////////////////////////////////////////
86 // Implementation of FGMenuBar.
87 ////////////////////////////////////////////////////////////////////////
90 FGMenuBar::FGMenuBar ()
96 FGMenuBar::~FGMenuBar ()
104 delete _menuBar; // FIXME: check if PUI owns the pointer
106 // FIXME: temporary commands to get at
107 // old, hard-coded dialogs.
108 add_deprecated_dialogs();
128 FGMenuBar::isVisible () const
134 FGMenuBar::fireItem (puObject * item)
136 const char * name = item->getLegend();
137 vector<SGBinding *> &bindings = _bindings[name];
138 int nBindings = bindings.size();
140 for (int i = 0; i < nBindings; i++)
145 FGMenuBar::make_menu (SGPropertyNode * node)
147 const char * name = strdup(node->getStringValue("label"));
148 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
150 int array_size = item_nodes.size();
152 char ** items = make_char_array(array_size);
153 puCallback * callbacks = make_callback_array(array_size);
155 for (unsigned int i = 0, j = item_nodes.size() - 1;
156 i < item_nodes.size();
159 // Set up the PUI entries for this item
160 items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
161 callbacks[j] = menu_callback;
163 // Load all the bindings for this item
164 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
165 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
167 for (unsigned int k = 0; k < bindings.size(); k++) {
169 SGPropertyNode_ptr binding;
170 while (dest->getChild("binding", m))
173 binding = dest->getChild("binding", m, true);
174 copyProperties(bindings[k], binding);
175 _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
179 _menuBar->add_submenu(name, items, callbacks);
183 FGMenuBar::make_menubar ()
185 SGPropertyNode *targetpath;
187 targetpath = fgGetNode("/sim/menubar/default",true);
188 // fgLoadProps("gui/menubar.xml", targetpath);
190 /* NOTE: there is no check to see whether there's any usable data at all
192 * This would also have the advantage of being able to create some kind of
193 * 'fallback' menu - just in case that either menubar.xml is empty OR that
194 * its XML data is not valid, that way we would avoid displaying an
195 * unusable menubar without any functionality - if we decided to add another
196 * char * element to the commands structure in
197 * $FG_SRC/src/Main/fgcommands.cxx
198 * we could additionally save each function's (short) description and use
199 * this as label for the fallback PUI menubar item labels - as a workaround
200 * one might simply use the internal fgcommands and put them into the
201 * fallback menu, so that the user is at least able to re-init the menu
202 * loading - just in case there was some malformed XML in it
203 * (it happend to me ...)
206 make_menubar(targetpath);
209 /* WARNING: We aren't yet doing any validation of what's found - but since
210 * this isn't done with menubar.xml either, it should not really matter
211 * right now. Although one should later on consider to validate the
212 * contents, whether they are representing a 'legal' menubar structure.
215 FGMenuBar::make_menubar(SGPropertyNode * props)
219 _menuBar = new puMenuBar;
221 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
222 for (unsigned int i = 0; i < menu_nodes.size(); i++)
223 make_menu(menu_nodes[i]);
226 make_object_map(props);
235 FGMenuBar::destroy_menubar ()
241 puDeleteObject(_menuBar);
245 // Delete all the character arrays
246 // we were forced to keep around for
248 SG_LOG(SG_GENERAL, SG_BULK, "Deleting char arrays");
249 for (i = 0; i < _char_arrays.size(); i++) {
250 for (int j = 0; _char_arrays[i][j] != 0; j++)
251 free(_char_arrays[i][j]); // added with strdup
252 delete[] _char_arrays[i];
255 // Delete all the callback arrays
256 // we were forced to keep around for
258 SG_LOG(SG_GENERAL, SG_BULK, "Deleting callback arrays");
259 for (i = 0; i < _callback_arrays.size(); i++)
260 delete[] _callback_arrays[i];
262 // Delete all those bindings
263 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings");
264 map<string,vector<SGBinding *> >::iterator it;
265 for (it = _bindings.begin(); it != _bindings.end(); it++) {
266 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings for " << it->first);
267 for ( i = 0; i < it->second.size(); i++ )
268 delete it->second[i];
271 SG_LOG(SG_GENERAL, SG_BULK, "Done.");
275 FGMenuBar::make_object_map(SGPropertyNode * node)
277 unsigned int menu_index = 0;
278 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
279 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
280 obj; obj = obj->getNextObject()) {
282 // skip puPopupMenus. They are also children of _menuBar,
283 // but we access them via getUserData() (see below)
284 if (!(obj->getType() & PUCLASS_ONESHOT))
287 if (menu_index >= menus.size()) {
288 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
289 << node->getPath() << "/menu[" << menu_index << ']');
293 SGPropertyNode *menu = menus.at(menu_index);
294 _objects[menu->getPath()] = obj;
295 add_enabled_listener(menu);
297 puGroup *popup = (puGroup *)obj->getUserData();
301 // the entries are for some reason reversed (last first), and we
302 // don't know yet how many there will be; so we collect first
303 vector<puObject *> e;
304 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
307 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
308 for (unsigned int i = 0; i < e.size(); i++) {
309 if (i >= items.size()) {
310 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
311 << menu->getPath() << "/item[" << i << ']');
314 SGPropertyNode *item = items.at(e.size() - i - 1);
315 _objects[item->getPath()] = e[i];
316 add_enabled_listener(item);
322 struct EnabledListener : SGPropertyChangeListener {
323 void valueChanged(SGPropertyNode *node) {
324 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
327 FGMenuBar *menubar = gui->getMenuBar();
329 menubar->enable_item(node->getParent(), node->getBoolValue());
334 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
336 if (!node->hasValue("enabled"))
337 node->setBoolValue("enabled", true);
339 enable_item(node, node->getBoolValue("enabled"));
340 node->getNode("enabled")->addChangeListener(new EnabledListener());
344 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
346 string path = node->getPath();
347 if (_objects.find(path) == _objects.end()) {
348 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
349 "non-existent menu item for node `" << path << '\'');
352 puObject *object = _objects[path];
362 FGMenuBar::make_char_array (int size)
364 char ** list = new char*[size+1];
365 for (int i = 0; i <= size; i++)
367 _char_arrays.push_back(list);
372 FGMenuBar::make_callback_array (int size)
374 puCallback * list = new puCallback[size+1];
375 for (int i = 0; i <= size; i++)
377 _callback_arrays.push_back(list);
381 // end of menubar.cxx