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 #if defined( _WIN32 ) && !defined(__MINGW32__)
40 extern void printScreen ();
42 do_print_dialog (const SGPropertyNode * arg)
49 extern void helpCb ();
51 do_help_dialog (const SGPropertyNode * arg)
59 SGCommandMgr::command_t command;
60 } deprecated_dialogs [] = {
61 #if defined(TR_HIRES_SNAP)
62 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
64 #if defined( _WIN32 ) && !defined(__MINGW32__)
65 { "old-print-dialog", do_print_dialog },
67 { "old-help-dialog", do_help_dialog },
72 add_deprecated_dialogs ()
74 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
75 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
76 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
77 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
78 deprecated_dialogs[i].command);
84 ////////////////////////////////////////////////////////////////////////
86 ////////////////////////////////////////////////////////////////////////
90 menu_callback (puObject * object)
92 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
93 gui->getMenuBar()->fireItem(object);
98 ////////////////////////////////////////////////////////////////////////
99 // Implementation of FGMenuBar.
100 ////////////////////////////////////////////////////////////////////////
103 FGMenuBar::FGMenuBar ()
109 FGMenuBar::~FGMenuBar ()
117 delete _menuBar; // FIXME: check if PUI owns the pointer
119 // FIXME: temporary commands to get at
120 // old, hard-coded dialogs.
121 add_deprecated_dialogs();
141 FGMenuBar::isVisible () const
147 FGMenuBar::fireItem (puObject * item)
149 const char * name = item->getLegend();
150 vector<SGBinding *> &bindings = _bindings[name];
151 int nBindings = bindings.size();
153 for (int i = 0; i < nBindings; i++)
158 FGMenuBar::make_menu (SGPropertyNode * node)
160 const char * name = strdup(node->getStringValue("label"));
161 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
163 int array_size = item_nodes.size();
165 char ** items = make_char_array(array_size);
166 puCallback * callbacks = make_callback_array(array_size);
168 for (unsigned int i = 0, j = item_nodes.size() - 1;
169 i < item_nodes.size();
172 // Set up the PUI entries for this item
173 items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
174 callbacks[j] = menu_callback;
176 // Load all the bindings for this item
177 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
178 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
180 for (unsigned int k = 0; k < bindings.size(); k++) {
182 SGPropertyNode_ptr binding;
183 while (dest->getChild("binding", m))
186 binding = dest->getChild("binding", m, true);
187 copyProperties(bindings[k], binding);
188 _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
192 _menuBar->add_submenu(name, items, callbacks);
196 FGMenuBar::make_menubar ()
198 SGPropertyNode *targetpath;
200 targetpath = fgGetNode("/sim/menubar/default",true);
201 // fgLoadProps("gui/menubar.xml", targetpath);
203 /* NOTE: there is no check to see whether there's any usable data at all
205 * This would also have the advantage of being able to create some kind of
206 * 'fallback' menu - just in case that either menubar.xml is empty OR that
207 * its XML data is not valid, that way we would avoid displaying an
208 * unusable menubar without any functionality - if we decided to add another
209 * char * element to the commands structure in
210 * $FG_SRC/src/Main/fgcommands.cxx
211 * we could additionally save each function's (short) description and use
212 * this as label for the fallback PUI menubar item labels - as a workaround
213 * one might simply use the internal fgcommands and put them into the
214 * fallback menu, so that the user is at least able to re-init the menu
215 * loading - just in case there was some malformed XML in it
216 * (it happend to me ...)
219 make_menubar(targetpath);
222 /* WARNING: We aren't yet doing any validation of what's found - but since
223 * this isn't done with menubar.xml either, it should not really matter
224 * right now. Although one should later on consider to validate the
225 * contents, whether they are representing a 'legal' menubar structure.
228 FGMenuBar::make_menubar(SGPropertyNode * props)
232 _menuBar = new puMenuBar;
234 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
235 for (unsigned int i = 0; i < menu_nodes.size(); i++)
236 make_menu(menu_nodes[i]);
239 make_object_map(props);
248 FGMenuBar::destroy_menubar ()
254 puDeleteObject(_menuBar);
258 // Delete all the character arrays
259 // we were forced to keep around for
261 SG_LOG(SG_GENERAL, SG_INFO, "Deleting char arrays");
262 for (i = 0; i < _char_arrays.size(); i++) {
263 for (int j = 0; _char_arrays[i][j] != 0; j++)
264 free(_char_arrays[i][j]); // added with strdup
265 delete[] _char_arrays[i];
268 // Delete all the callback arrays
269 // we were forced to keep around for
271 SG_LOG(SG_GENERAL, SG_INFO, "Deleting callback arrays");
272 for (i = 0; i < _callback_arrays.size(); i++)
273 delete[] _callback_arrays[i];
275 // Delete all those bindings
276 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings");
277 map<string,vector<SGBinding *> >::iterator it;
278 for (it = _bindings.begin(); it != _bindings.end(); it++) {
279 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings for " << it->first);
280 for ( i = 0; i < it->second.size(); i++ )
281 delete it->second[i];
284 SG_LOG(SG_GENERAL, SG_INFO, "Done.");
288 FGMenuBar::make_object_map(SGPropertyNode * node)
290 unsigned int menu_index = 0;
291 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
292 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
293 obj; obj = obj->getNextObject()) {
295 // skip puPopupMenus. They are also children of _menuBar,
296 // but we access them via getUserData() (see below)
297 if (!(obj->getType() & PUCLASS_ONESHOT))
300 if (menu_index >= menus.size()) {
301 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
302 << node->getPath() << "/menu[" << menu_index << ']');
306 SGPropertyNode *menu = menus.at(menu_index);
307 _objects[menu->getPath()] = obj;
308 add_enabled_listener(menu);
310 puGroup *popup = (puGroup *)obj->getUserData();
314 // the entries are for some reason reversed (last first), and we
315 // don't know yet how many there will be; so we collect first
316 vector<puObject *> e;
317 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
320 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
321 for (unsigned int i = 0; i < e.size(); i++) {
322 if (i >= items.size()) {
323 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
324 << menu->getPath() << "/item[" << i << ']');
327 SGPropertyNode *item = items.at(e.size() - i - 1);
328 _objects[item->getPath()] = e[i];
329 add_enabled_listener(item);
335 struct EnabledListener : SGPropertyChangeListener {
336 void valueChanged(SGPropertyNode *node) {
337 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
340 FGMenuBar *menubar = gui->getMenuBar();
342 menubar->enable_item(node->getParent(), node->getBoolValue());
347 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
349 if (!node->hasValue("enabled"))
350 node->setBoolValue("enabled", true);
352 enable_item(node, node->getBoolValue("enabled"));
353 node->getNode("enabled")->addChangeListener(new EnabledListener());
357 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
359 const char *path = node->getPath();
360 if (_objects.find(path) == _objects.end()) {
361 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
362 "non-existent menu item for node `" << path << '\'');
365 puObject *object = _objects[path];
375 FGMenuBar::make_char_array (int size)
377 char ** list = new char*[size+1];
378 for (int i = 0; i <= size; i++)
380 _char_arrays.push_back(list);
385 FGMenuBar::make_callback_array (int size)
387 puCallback * list = new puCallback[size+1];
388 for (int i = 0; i <= size; i++)
390 _callback_arrays.push_back(list);
394 // end of menubar.cxx