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 helpCb (puObject *);
59 do_help_dialog (const SGPropertyNode * arg)
67 SGCommandMgr::command_t command;
68 } deprecated_dialogs [] = {
69 { "old-reinit-dialog", do_reinit_dialog },
70 #if defined(TR_HIRES_SNAP)
71 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
73 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
74 { "old-print-dialog", do_print_dialog },
76 { "old-help-dialog", do_help_dialog },
81 add_deprecated_dialogs ()
83 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
84 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
85 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
86 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
87 deprecated_dialogs[i].command);
93 ////////////////////////////////////////////////////////////////////////
95 ////////////////////////////////////////////////////////////////////////
99 menu_callback (puObject * object)
101 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
102 gui->getMenuBar()->fireItem(object);
107 ////////////////////////////////////////////////////////////////////////
108 // Implementation of FGMenuBar.
109 ////////////////////////////////////////////////////////////////////////
112 FGMenuBar::FGMenuBar ()
118 FGMenuBar::~FGMenuBar ()
126 delete _menuBar; // FIXME: check if PUI owns the pointer
128 // FIXME: temporary commands to get at
129 // old, hard-coded dialogs.
130 add_deprecated_dialogs();
150 FGMenuBar::isVisible () const
156 FGMenuBar::fireItem (puObject * item)
158 const char * name = item->getLegend();
159 vector<SGBinding *> &bindings = _bindings[name];
160 int nBindings = bindings.size();
162 for (int i = 0; i < nBindings; i++)
167 FGMenuBar::make_menu (SGPropertyNode * node)
169 const char * name = strdup(node->getStringValue("label"));
170 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
172 int array_size = item_nodes.size();
174 char ** items = make_char_array(array_size);
175 puCallback * callbacks = make_callback_array(array_size);
177 for (unsigned int i = 0, j = item_nodes.size() - 1;
178 i < item_nodes.size();
181 // Set up the PUI entries for this item
182 items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
183 callbacks[j] = menu_callback;
185 // Load all the bindings for this item
186 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
187 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
189 for (unsigned int k = 0; k < bindings.size(); k++) {
191 SGPropertyNode *binding;
192 while (dest->getChild("binding", m))
195 binding = dest->getChild("binding", m, true);
196 copyProperties(bindings[k], binding);
197 _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
201 _menuBar->add_submenu(name, items, callbacks);
205 FGMenuBar::make_menubar ()
207 SGPropertyNode *targetpath;
209 targetpath = fgGetNode("/sim/menubar/default",true);
210 // fgLoadProps("gui/menubar.xml", targetpath);
212 /* NOTE: there is no check to see whether there's any usable data at all
214 * This would also have the advantage of being able to create some kind of
215 * 'fallback' menu - just in case that either menubar.xml is empty OR that
216 * its XML data is not valid, that way we would avoid displaying an
217 * unusable menubar without any functionality - if we decided to add another
218 * char * element to the commands structure in
219 * $FG_SRC/src/Main/fgcommands.cxx
220 * we could additionally save each function's (short) description and use
221 * this as label for the fallback PUI menubar item labels - as a workaround
222 * one might simply use the internal fgcommands and put them into the
223 * fallback menu, so that the user is at least able to re-init the menu
224 * loading - just in case there was some malformed XML in it
225 * (it happend to me ...)
228 make_menubar(targetpath);
231 /* WARNING: We aren't yet doing any validation of what's found - but since
232 * this isn't done with menubar.xml either, it should not really matter
233 * right now. Although one should later on consider to validate the
234 * contents, whether they are representing a 'legal' menubar structure.
237 FGMenuBar::make_menubar(SGPropertyNode * props)
241 _menuBar = new puMenuBar;
243 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
244 for (unsigned int i = 0; i < menu_nodes.size(); i++)
245 make_menu(menu_nodes[i]);
248 make_object_map(props);
257 FGMenuBar::destroy_menubar ()
263 puDeleteObject(_menuBar);
267 // Delete all the character arrays
268 // we were forced to keep around for
270 SG_LOG(SG_GENERAL, SG_INFO, "Deleting char arrays");
271 for (i = 0; i < _char_arrays.size(); i++) {
272 for (int j = 0; _char_arrays[i][j] != 0; j++)
273 free(_char_arrays[i][j]); // added with strdup
274 delete[] _char_arrays[i];
277 // Delete all the callback arrays
278 // we were forced to keep around for
280 SG_LOG(SG_GENERAL, SG_INFO, "Deleting callback arrays");
281 for (i = 0; i < _callback_arrays.size(); i++)
282 delete[] _callback_arrays[i];
284 // Delete all those bindings
285 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings");
286 map<string,vector<SGBinding *> >::iterator it;
287 it = _bindings.begin();
288 for (it = _bindings.begin(); it != _bindings.end(); it++) {
289 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings for " << it->first);
290 for ( i = 0; i < it->second.size(); i++ )
291 delete it->second[i];
294 SG_LOG(SG_GENERAL, SG_INFO, "Done.");
298 FGMenuBar::make_object_map(SGPropertyNode * node)
300 unsigned int menu_index = 0;
301 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
302 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
303 obj; obj = obj->getNextObject()) {
305 // skip puPopupMenus. They are also children of _menuBar,
306 // but we access them via getUserData() (see below)
307 if (!(obj->getType() & PUCLASS_ONESHOT))
310 if (menu_index >= menus.size()) {
311 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
312 << node->getPath() << "/menu[" << menu_index << ']');
316 SGPropertyNode *menu = menus.at(menu_index);
317 _objects[menu->getPath()] = obj;
318 add_enabled_listener(menu);
320 puGroup *popup = (puGroup *)obj->getUserData();
324 // the entries are for some reason reversed (last first), and we
325 // don't know yet how many there will be; so we collect first
326 vector<puObject *> e;
327 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
330 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
331 for (unsigned int i = 0; i < e.size(); i++) {
332 if (i >= items.size()) {
333 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
334 << menu->getPath() << "/item[" << i << ']');
337 SGPropertyNode *item = items.at(e.size() - i - 1);
338 _objects[item->getPath()] = e[i];
339 add_enabled_listener(item);
345 struct EnabledListener : SGPropertyChangeListener {
346 void valueChanged(SGPropertyNode *node) {
347 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
350 FGMenuBar *menubar = gui->getMenuBar();
352 menubar->enable_item(node->getParent(), node->getBoolValue());
357 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
359 if (!node->hasValue("enabled"))
360 node->setBoolValue("enabled", true);
362 enable_item(node, node->getBoolValue("enabled"));
363 node->getNode("enabled")->addChangeListener(new EnabledListener());
367 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
369 const char *path = node->getPath();
370 if (_objects.find(path) == _objects.end()) {
371 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
372 "non-existent menu item for node `" << path << '\'');
375 puObject *object = _objects[path];
385 FGMenuBar::make_char_array (int size)
387 char ** list = new char*[size+1];
388 for (int i = 0; i <= size; i++)
390 _char_arrays.push_back(list);
395 FGMenuBar::make_callback_array (int size)
397 puCallback * list = new puCallback[size+1];
398 for (int i = 0; i <= size; i++)
400 _callback_arrays.push_back(list);
404 // end of menubar.cxx