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"
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 #if defined(TR_HIRES_SNAP)
32 extern void dumpHiResSnapShot ();
34 do_hires_snapshot_dialog (const SGPropertyNode * arg)
39 #endif // TR_HIRES_SNAP
43 SGCommandMgr::command_t command;
44 } deprecated_dialogs [] = {
45 #if defined(TR_HIRES_SNAP)
46 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
52 add_deprecated_dialogs ()
54 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
55 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
56 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
57 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
58 deprecated_dialogs[i].command);
64 ////////////////////////////////////////////////////////////////////////
66 ////////////////////////////////////////////////////////////////////////
70 menu_callback (puObject * object)
72 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
73 gui->getMenuBar()->fireItem(object);
78 ////////////////////////////////////////////////////////////////////////
79 // Implementation of FGMenuBar.
80 ////////////////////////////////////////////////////////////////////////
83 FGMenuBar::FGMenuBar ()
89 FGMenuBar::~FGMenuBar ()
97 delete _menuBar; // FIXME: check if PUI owns the pointer
99 // FIXME: temporary commands to get at
100 // old, hard-coded dialogs.
101 add_deprecated_dialogs();
121 FGMenuBar::isVisible () const
127 FGMenuBar::fireItem (puObject * item)
129 const char * name = item->getLegend();
130 vector<SGBinding *> &bindings = _bindings[name];
131 int nBindings = bindings.size();
133 for (int i = 0; i < nBindings; i++)
138 FGMenuBar::make_menu (SGPropertyNode * node)
140 const char * name = strdup(node->getStringValue("label"));
141 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
143 int array_size = item_nodes.size();
145 char ** items = make_char_array(array_size);
146 puCallback * callbacks = make_callback_array(array_size);
148 for (unsigned int i = 0, j = item_nodes.size() - 1;
149 i < item_nodes.size();
152 // Set up the PUI entries for this item
153 items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
154 callbacks[j] = menu_callback;
156 // Load all the bindings for this item
157 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
158 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
160 for (unsigned int k = 0; k < bindings.size(); k++) {
162 SGPropertyNode_ptr binding;
163 while (dest->getChild("binding", m))
166 binding = dest->getChild("binding", m, true);
167 copyProperties(bindings[k], binding);
168 _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
172 _menuBar->add_submenu(name, items, callbacks);
176 FGMenuBar::make_menubar ()
178 SGPropertyNode *targetpath;
180 targetpath = fgGetNode("/sim/menubar/default",true);
181 // fgLoadProps("gui/menubar.xml", targetpath);
183 /* NOTE: there is no check to see whether there's any usable data at all
185 * This would also have the advantage of being able to create some kind of
186 * 'fallback' menu - just in case that either menubar.xml is empty OR that
187 * its XML data is not valid, that way we would avoid displaying an
188 * unusable menubar without any functionality - if we decided to add another
189 * char * element to the commands structure in
190 * $FG_SRC/src/Main/fgcommands.cxx
191 * we could additionally save each function's (short) description and use
192 * this as label for the fallback PUI menubar item labels - as a workaround
193 * one might simply use the internal fgcommands and put them into the
194 * fallback menu, so that the user is at least able to re-init the menu
195 * loading - just in case there was some malformed XML in it
196 * (it happend to me ...)
199 make_menubar(targetpath);
202 /* WARNING: We aren't yet doing any validation of what's found - but since
203 * this isn't done with menubar.xml either, it should not really matter
204 * right now. Although one should later on consider to validate the
205 * contents, whether they are representing a 'legal' menubar structure.
208 FGMenuBar::make_menubar(SGPropertyNode * props)
212 _menuBar = new puMenuBar;
214 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
215 for (unsigned int i = 0; i < menu_nodes.size(); i++)
216 make_menu(menu_nodes[i]);
219 make_object_map(props);
228 FGMenuBar::destroy_menubar ()
234 puDeleteObject(_menuBar);
238 // Delete all the character arrays
239 // we were forced to keep around for
241 SG_LOG(SG_GENERAL, SG_BULK, "Deleting char arrays");
242 for (i = 0; i < _char_arrays.size(); i++) {
243 for (int j = 0; _char_arrays[i][j] != 0; j++)
244 free(_char_arrays[i][j]); // added with strdup
245 delete[] _char_arrays[i];
248 // Delete all the callback arrays
249 // we were forced to keep around for
251 SG_LOG(SG_GENERAL, SG_BULK, "Deleting callback arrays");
252 for (i = 0; i < _callback_arrays.size(); i++)
253 delete[] _callback_arrays[i];
255 // Delete all those bindings
256 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings");
257 map<string,vector<SGBinding *> >::iterator it;
258 for (it = _bindings.begin(); it != _bindings.end(); it++) {
259 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings for " << it->first);
260 for ( i = 0; i < it->second.size(); i++ )
261 delete it->second[i];
264 SG_LOG(SG_GENERAL, SG_BULK, "Done.");
268 FGMenuBar::make_object_map(SGPropertyNode * node)
270 unsigned int menu_index = 0;
271 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
272 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
273 obj; obj = obj->getNextObject()) {
275 // skip puPopupMenus. They are also children of _menuBar,
276 // but we access them via getUserData() (see below)
277 if (!(obj->getType() & PUCLASS_ONESHOT))
280 if (menu_index >= menus.size()) {
281 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
282 << node->getPath() << "/menu[" << menu_index << ']');
286 SGPropertyNode *menu = menus.at(menu_index);
287 _objects[menu->getPath()] = obj;
288 add_enabled_listener(menu);
290 puGroup *popup = (puGroup *)obj->getUserData();
294 // the entries are for some reason reversed (last first), and we
295 // don't know yet how many there will be; so we collect first
296 vector<puObject *> e;
297 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
300 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
301 for (unsigned int i = 0; i < e.size(); i++) {
302 if (i >= items.size()) {
303 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
304 << menu->getPath() << "/item[" << i << ']');
307 SGPropertyNode *item = items.at(e.size() - i - 1);
308 _objects[item->getPath()] = e[i];
309 add_enabled_listener(item);
315 struct EnabledListener : SGPropertyChangeListener {
316 void valueChanged(SGPropertyNode *node) {
317 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
320 FGMenuBar *menubar = gui->getMenuBar();
322 menubar->enable_item(node->getParent(), node->getBoolValue());
327 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
329 if (!node->hasValue("enabled"))
330 node->setBoolValue("enabled", true);
332 enable_item(node, node->getBoolValue("enabled"));
333 node->getNode("enabled")->addChangeListener(new EnabledListener());
337 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
339 string path = node->getPath();
340 if (_objects.find(path) == _objects.end()) {
341 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
342 "non-existent menu item for node `" << path << '\'');
345 puObject *object = _objects[path];
355 FGMenuBar::make_char_array (int size)
357 char ** list = new char*[size+1];
358 for (int i = 0; i <= size; i++)
360 _char_arrays.push_back(list);
365 FGMenuBar::make_callback_array (int size)
367 puCallback * list = new puCallback[size+1];
368 for (int i = 0; i <= size; i++)
370 _callback_arrays.push_back(list);
374 // end of menubar.cxx