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
40 extern void helpCb ();
42 do_help_dialog (const SGPropertyNode * arg)
50 SGCommandMgr::command_t command;
51 } deprecated_dialogs [] = {
52 #if defined(TR_HIRES_SNAP)
53 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
55 { "old-help-dialog", do_help_dialog },
60 add_deprecated_dialogs ()
62 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
63 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
64 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
65 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
66 deprecated_dialogs[i].command);
72 ////////////////////////////////////////////////////////////////////////
74 ////////////////////////////////////////////////////////////////////////
78 menu_callback (puObject * object)
80 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
81 gui->getMenuBar()->fireItem(object);
86 ////////////////////////////////////////////////////////////////////////
87 // Implementation of FGMenuBar.
88 ////////////////////////////////////////////////////////////////////////
91 FGMenuBar::FGMenuBar ()
97 FGMenuBar::~FGMenuBar ()
105 delete _menuBar; // FIXME: check if PUI owns the pointer
107 // FIXME: temporary commands to get at
108 // old, hard-coded dialogs.
109 add_deprecated_dialogs();
129 FGMenuBar::isVisible () const
135 FGMenuBar::fireItem (puObject * item)
137 const char * name = item->getLegend();
138 vector<SGBinding *> &bindings = _bindings[name];
139 int nBindings = bindings.size();
141 for (int i = 0; i < nBindings; i++)
146 FGMenuBar::make_menu (SGPropertyNode * node)
148 const char * name = strdup(node->getStringValue("label"));
149 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
151 int array_size = item_nodes.size();
153 char ** items = make_char_array(array_size);
154 puCallback * callbacks = make_callback_array(array_size);
156 for (unsigned int i = 0, j = item_nodes.size() - 1;
157 i < item_nodes.size();
160 // Set up the PUI entries for this item
161 items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
162 callbacks[j] = menu_callback;
164 // Load all the bindings for this item
165 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
166 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
168 for (unsigned int k = 0; k < bindings.size(); k++) {
170 SGPropertyNode_ptr binding;
171 while (dest->getChild("binding", m))
174 binding = dest->getChild("binding", m, true);
175 copyProperties(bindings[k], binding);
176 _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
180 _menuBar->add_submenu(name, items, callbacks);
184 FGMenuBar::make_menubar ()
186 SGPropertyNode *targetpath;
188 targetpath = fgGetNode("/sim/menubar/default",true);
189 // fgLoadProps("gui/menubar.xml", targetpath);
191 /* NOTE: there is no check to see whether there's any usable data at all
193 * This would also have the advantage of being able to create some kind of
194 * 'fallback' menu - just in case that either menubar.xml is empty OR that
195 * its XML data is not valid, that way we would avoid displaying an
196 * unusable menubar without any functionality - if we decided to add another
197 * char * element to the commands structure in
198 * $FG_SRC/src/Main/fgcommands.cxx
199 * we could additionally save each function's (short) description and use
200 * this as label for the fallback PUI menubar item labels - as a workaround
201 * one might simply use the internal fgcommands and put them into the
202 * fallback menu, so that the user is at least able to re-init the menu
203 * loading - just in case there was some malformed XML in it
204 * (it happend to me ...)
207 make_menubar(targetpath);
210 /* WARNING: We aren't yet doing any validation of what's found - but since
211 * this isn't done with menubar.xml either, it should not really matter
212 * right now. Although one should later on consider to validate the
213 * contents, whether they are representing a 'legal' menubar structure.
216 FGMenuBar::make_menubar(SGPropertyNode * props)
220 _menuBar = new puMenuBar;
222 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
223 for (unsigned int i = 0; i < menu_nodes.size(); i++)
224 make_menu(menu_nodes[i]);
227 make_object_map(props);
236 FGMenuBar::destroy_menubar ()
242 puDeleteObject(_menuBar);
246 // Delete all the character arrays
247 // we were forced to keep around for
249 SG_LOG(SG_GENERAL, SG_BULK, "Deleting char arrays");
250 for (i = 0; i < _char_arrays.size(); i++) {
251 for (int j = 0; _char_arrays[i][j] != 0; j++)
252 free(_char_arrays[i][j]); // added with strdup
253 delete[] _char_arrays[i];
256 // Delete all the callback arrays
257 // we were forced to keep around for
259 SG_LOG(SG_GENERAL, SG_BULK, "Deleting callback arrays");
260 for (i = 0; i < _callback_arrays.size(); i++)
261 delete[] _callback_arrays[i];
263 // Delete all those bindings
264 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings");
265 map<string,vector<SGBinding *> >::iterator it;
266 for (it = _bindings.begin(); it != _bindings.end(); it++) {
267 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings for " << it->first);
268 for ( i = 0; i < it->second.size(); i++ )
269 delete it->second[i];
272 SG_LOG(SG_GENERAL, SG_BULK, "Done.");
276 FGMenuBar::make_object_map(SGPropertyNode * node)
278 unsigned int menu_index = 0;
279 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
280 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
281 obj; obj = obj->getNextObject()) {
283 // skip puPopupMenus. They are also children of _menuBar,
284 // but we access them via getUserData() (see below)
285 if (!(obj->getType() & PUCLASS_ONESHOT))
288 if (menu_index >= menus.size()) {
289 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
290 << node->getPath() << "/menu[" << menu_index << ']');
294 SGPropertyNode *menu = menus.at(menu_index);
295 _objects[menu->getPath()] = obj;
296 add_enabled_listener(menu);
298 puGroup *popup = (puGroup *)obj->getUserData();
302 // the entries are for some reason reversed (last first), and we
303 // don't know yet how many there will be; so we collect first
304 vector<puObject *> e;
305 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
308 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
309 for (unsigned int i = 0; i < e.size(); i++) {
310 if (i >= items.size()) {
311 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
312 << menu->getPath() << "/item[" << i << ']');
315 SGPropertyNode *item = items.at(e.size() - i - 1);
316 _objects[item->getPath()] = e[i];
317 add_enabled_listener(item);
323 struct EnabledListener : SGPropertyChangeListener {
324 void valueChanged(SGPropertyNode *node) {
325 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
328 FGMenuBar *menubar = gui->getMenuBar();
330 menubar->enable_item(node->getParent(), node->getBoolValue());
335 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
337 if (!node->hasValue("enabled"))
338 node->setBoolValue("enabled", true);
340 enable_item(node, node->getBoolValue("enabled"));
341 node->getNode("enabled")->addChangeListener(new EnabledListener());
345 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
347 string path = node->getPath();
348 if (_objects.find(path) == _objects.end()) {
349 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
350 "non-existent menu item for node `" << path << '\'');
353 puObject *object = _objects[path];
363 FGMenuBar::make_char_array (int size)
365 char ** list = new char*[size+1];
366 for (int i = 0; i <= size; i++)
368 _char_arrays.push_back(list);
373 FGMenuBar::make_callback_array (int size)
375 puCallback * list = new puCallback[size+1];
376 for (int i = 0; i <= size; i++)
378 _callback_arrays.push_back(list);
382 // end of menubar.cxx