8 #include <simgear/debug/logstream.hxx>
9 #include <simgear/structure/SGBinding.hxx>
10 #include <simgear/props/props_io.hxx>
12 #include <Main/globals.hxx>
14 #include "new_gui.hxx"
15 #include "menubar.hxx"
19 ////////////////////////////////////////////////////////////////////////
22 // Deprecated wrappers for old menu commands.
24 // DO NOT ADD TO THESE. THEY WILL BE DELETED SOON!
26 // These are defined in gui_funcs.cxx. They should be replaced with
27 // user-configured dialogs and new commands where necessary.
28 ////////////////////////////////////////////////////////////////////////
30 #if defined(TR_HIRES_SNAP)
31 extern void dumpHiResSnapShot ();
33 do_hires_snapshot_dialog (const SGPropertyNode * arg)
38 #endif // TR_HIRES_SNAP
42 SGCommandMgr::command_t command;
43 } deprecated_dialogs [] = {
44 #if defined(TR_HIRES_SNAP)
45 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
51 add_deprecated_dialogs ()
53 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
54 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
55 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
56 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
57 deprecated_dialogs[i].command);
63 ////////////////////////////////////////////////////////////////////////
65 ////////////////////////////////////////////////////////////////////////
69 menu_callback (puObject * object)
71 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
72 gui->getMenuBar()->fireItem(object);
77 ////////////////////////////////////////////////////////////////////////
78 // Implementation of FGMenuBar.
79 ////////////////////////////////////////////////////////////////////////
82 FGMenuBar::FGMenuBar ()
88 FGMenuBar::~FGMenuBar ()
96 delete _menuBar; // FIXME: check if PUI owns the pointer
98 // FIXME: temporary commands to get at
99 // old, hard-coded dialogs.
100 add_deprecated_dialogs();
120 FGMenuBar::isVisible () const
126 FGMenuBar::fireItem (puObject * item)
128 const char * name = item->getLegend();
129 vector<SGBinding *> &bindings = _bindings[name];
130 int nBindings = bindings.size();
132 for (int i = 0; i < nBindings; i++)
137 FGMenuBar::make_menu (SGPropertyNode * node)
139 const char * name = strdup(node->getStringValue("label"));
140 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
142 int array_size = item_nodes.size();
144 char ** items = make_char_array(array_size);
145 puCallback * callbacks = make_callback_array(array_size);
147 for (unsigned int i = 0, j = item_nodes.size() - 1;
148 i < item_nodes.size();
151 // Set up the PUI entries for this item
152 items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
153 callbacks[j] = menu_callback;
155 // Load all the bindings for this item
156 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
157 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
159 for (unsigned int k = 0; k < bindings.size(); k++) {
161 SGPropertyNode_ptr binding;
162 while (dest->getChild("binding", m))
165 binding = dest->getChild("binding", m, true);
166 copyProperties(bindings[k], binding);
167 _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
171 _menuBar->add_submenu(name, items, callbacks);
175 FGMenuBar::make_menubar ()
177 SGPropertyNode *targetpath;
179 targetpath = fgGetNode("/sim/menubar/default",true);
180 // fgLoadProps("gui/menubar.xml", targetpath);
182 /* NOTE: there is no check to see whether there's any usable data at all
184 * This would also have the advantage of being able to create some kind of
185 * 'fallback' menu - just in case that either menubar.xml is empty OR that
186 * its XML data is not valid, that way we would avoid displaying an
187 * unusable menubar without any functionality - if we decided to add another
188 * char * element to the commands structure in
189 * $FG_SRC/src/Main/fgcommands.cxx
190 * we could additionally save each function's (short) description and use
191 * this as label for the fallback PUI menubar item labels - as a workaround
192 * one might simply use the internal fgcommands and put them into the
193 * fallback menu, so that the user is at least able to re-init the menu
194 * loading - just in case there was some malformed XML in it
195 * (it happend to me ...)
198 make_menubar(targetpath);
201 /* WARNING: We aren't yet doing any validation of what's found - but since
202 * this isn't done with menubar.xml either, it should not really matter
203 * right now. Although one should later on consider to validate the
204 * contents, whether they are representing a 'legal' menubar structure.
207 FGMenuBar::make_menubar(SGPropertyNode * props)
211 _menuBar = new puMenuBar;
213 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
214 for (unsigned int i = 0; i < menu_nodes.size(); i++)
215 make_menu(menu_nodes[i]);
218 make_object_map(props);
227 FGMenuBar::destroy_menubar ()
233 puDeleteObject(_menuBar);
237 // Delete all the character arrays
238 // we were forced to keep around for
240 SG_LOG(SG_GENERAL, SG_BULK, "Deleting char arrays");
241 for (i = 0; i < _char_arrays.size(); i++) {
242 for (int j = 0; _char_arrays[i][j] != 0; j++)
243 free(_char_arrays[i][j]); // added with strdup
244 delete[] _char_arrays[i];
247 // Delete all the callback arrays
248 // we were forced to keep around for
250 SG_LOG(SG_GENERAL, SG_BULK, "Deleting callback arrays");
251 for (i = 0; i < _callback_arrays.size(); i++)
252 delete[] _callback_arrays[i];
254 // Delete all those bindings
255 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings");
256 map<string,vector<SGBinding *> >::iterator it;
257 for (it = _bindings.begin(); it != _bindings.end(); it++) {
258 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings for " << it->first);
259 for ( i = 0; i < it->second.size(); i++ )
260 delete it->second[i];
263 SG_LOG(SG_GENERAL, SG_BULK, "Done.");
267 FGMenuBar::make_object_map(SGPropertyNode * node)
269 unsigned int menu_index = 0;
270 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
271 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
272 obj; obj = obj->getNextObject()) {
274 // skip puPopupMenus. They are also children of _menuBar,
275 // but we access them via getUserData() (see below)
276 if (!(obj->getType() & PUCLASS_ONESHOT))
279 if (menu_index >= menus.size()) {
280 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
281 << node->getPath() << "/menu[" << menu_index << ']');
285 SGPropertyNode *menu = menus.at(menu_index);
286 _objects[menu->getPath()] = obj;
287 add_enabled_listener(menu);
289 puGroup *popup = (puGroup *)obj->getUserData();
293 // the entries are for some reason reversed (last first), and we
294 // don't know yet how many there will be; so we collect first
295 vector<puObject *> e;
296 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
299 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
300 for (unsigned int i = 0; i < e.size(); i++) {
301 if (i >= items.size()) {
302 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
303 << menu->getPath() << "/item[" << i << ']');
306 SGPropertyNode *item = items.at(e.size() - i - 1);
307 _objects[item->getPath()] = e[i];
308 add_enabled_listener(item);
314 struct EnabledListener : SGPropertyChangeListener {
315 void valueChanged(SGPropertyNode *node) {
316 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
319 FGMenuBar *menubar = gui->getMenuBar();
321 menubar->enable_item(node->getParent(), node->getBoolValue());
326 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
328 if (!node->hasValue("enabled"))
329 node->setBoolValue("enabled", true);
331 enable_item(node, node->getBoolValue("enabled"));
332 node->getNode("enabled")->addChangeListener(new EnabledListener());
336 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
338 string path = node->getPath();
339 if (_objects.find(path) == _objects.end()) {
340 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
341 "non-existent menu item for node `" << path << '\'');
344 puObject *object = _objects[path];
354 FGMenuBar::make_char_array (int size)
356 char ** list = new char*[size+1];
357 for (int i = 0; i <= size; i++)
359 _char_arrays.push_back(list);
364 FGMenuBar::make_callback_array (int size)
366 puCallback * list = new puCallback[size+1];
367 for (int i = 0; i <= size; i++)
369 _callback_arrays.push_back(list);
373 // end of menubar.cxx