8 #include <simgear/debug/logstream.hxx>
9 #include <simgear/structure/SGBinding.hxx>
10 #include <simgear/props/props_io.hxx>
12 #include <Main/globals.hxx>
13 #include <Main/locale.hxx>
14 #include <Main/fg_props.hxx>
16 #include "new_gui.hxx"
17 #include "FGPUIMenuBar.hxx"
22 ////////////////////////////////////////////////////////////////////////
25 // Deprecated wrappers for old menu commands.
27 // DO NOT ADD TO THESE. THEY WILL BE DELETED SOON!
29 // These are defined in gui_funcs.cxx. They should be replaced with
30 // user-configured dialogs and new commands where necessary.
31 ////////////////////////////////////////////////////////////////////////
33 #if defined(TR_HIRES_SNAP)
34 extern void dumpHiResSnapShot ();
36 do_hires_snapshot_dialog (const SGPropertyNode * arg)
41 #endif // TR_HIRES_SNAP
45 SGCommandMgr::command_t command;
46 } deprecated_dialogs [] = {
47 #if defined(TR_HIRES_SNAP)
48 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
54 add_deprecated_dialogs ()
56 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
57 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
58 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
59 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
60 deprecated_dialogs[i].command);
66 ////////////////////////////////////////////////////////////////////////
68 ////////////////////////////////////////////////////////////////////////
72 menu_callback (puObject * object)
74 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
75 FGPUIMenuBar* mb = static_cast<FGPUIMenuBar*>(gui->getMenuBar());
79 ////////////////////////////////////////////////////////////////////////
80 // Implementation of FGPUIMenuBar.
81 ////////////////////////////////////////////////////////////////////////
84 FGPUIMenuBar::FGPUIMenuBar ()
90 FGPUIMenuBar::~FGPUIMenuBar ()
98 bool visible = _visible;
102 // FIXME: temporary commands to get at
103 // old, hard-coded dialogs.
104 add_deprecated_dialogs();
106 // Keep menu visible during gui-redraw
112 FGPUIMenuBar::show ()
120 FGPUIMenuBar::hide ()
128 FGPUIMenuBar::isVisible () const
134 FGPUIMenuBar::fireItem (puObject * item)
136 const char * name = item->getLegend();
137 vector<SGBinding *> &bindings = _bindings[name];
138 int nBindings = bindings.size();
140 for (int i = 0; i < nBindings; i++)
145 FGPUIMenuBar::make_menu (SGPropertyNode * node)
147 string s = getLocalizedLabel(node);
149 // hack: map at least some UTF-8 characters to Latin1, since FG fonts are
150 // Latin1 (or plain ASCII, which is a subset). This hack can be removed once
151 // the PLIB/OSG port is complete (OSG has full UTF-8 support! :) ).
152 FGLocale::utf8toLatin1(s);
154 const char* name = strdup(s.c_str());
156 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
158 int array_size = item_nodes.size();
160 char ** items = make_char_array(array_size);
161 puCallback * callbacks = make_callback_array(array_size);
163 for (unsigned int i = 0, j = item_nodes.size() - 1;
164 i < item_nodes.size();
167 // Set up the PUI entries for this item
168 string label = getLocalizedLabel(item_nodes[i]);
169 FGLocale::utf8toLatin1(label);
171 // append the keyboard hint to the menu entry
172 const char* key = item_nodes[i]->getStringValue("key", 0);
179 items[j] = strdup(label.c_str());
180 callbacks[j] = menu_callback;
182 // Load all the bindings for this item
183 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
184 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
186 for (unsigned int k = 0; k < bindings.size(); k++) {
188 SGPropertyNode_ptr binding;
189 while (dest->getChild("binding", m))
192 binding = dest->getChild("binding", m, true);
193 copyProperties(bindings[k], binding);
194 _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
198 _menuBar->add_submenu(name, items, callbacks);
202 FGPUIMenuBar::make_menubar ()
204 SGPropertyNode *targetpath;
206 targetpath = fgGetNode("/sim/menubar/default",true);
207 // fgLoadProps("gui/menubar.xml", targetpath);
209 /* NOTE: there is no check to see whether there's any usable data at all
211 * This would also have the advantage of being able to create some kind of
212 * 'fallback' menu - just in case that either menubar.xml is empty OR that
213 * its XML data is not valid, that way we would avoid displaying an
214 * unusable menubar without any functionality - if we decided to add another
215 * char * element to the commands structure in
216 * $FG_SRC/src/Main/fgcommands.cxx
217 * we could additionally save each function's (short) description and use
218 * this as label for the fallback PUI menubar item labels - as a workaround
219 * one might simply use the internal fgcommands and put them into the
220 * fallback menu, so that the user is at least able to re-init the menu
221 * loading - just in case there was some malformed XML in it
222 * (it happend to me ...)
225 make_menubar(targetpath);
228 /* WARNING: We aren't yet doing any validation of what's found - but since
229 * this isn't done with menubar.xml either, it should not really matter
230 * right now. Although one should later on consider to validate the
231 * contents, whether they are representing a 'legal' menubar structure.
234 FGPUIMenuBar::make_menubar(SGPropertyNode * props)
238 _menuBar = new puMenuBar;
240 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
241 for (unsigned int i = 0; i < menu_nodes.size(); i++)
242 make_menu(menu_nodes[i]);
245 make_object_map(props);
254 FGPUIMenuBar::destroy_menubar ()
260 puDeleteObject(_menuBar);
264 // Delete all the character arrays
265 // we were forced to keep around for
267 SG_LOG(SG_GENERAL, SG_BULK, "Deleting char arrays");
268 for (i = 0; i < _char_arrays.size(); i++) {
269 for (int j = 0; _char_arrays[i][j] != 0; j++) {
270 free(_char_arrays[i][j]); // added with strdup
271 _char_arrays[i][j] = 0;
273 delete[] _char_arrays[i];
277 // Delete all the callback arrays
278 // we were forced to keep around for
280 SG_LOG(SG_GENERAL, SG_BULK, "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_BULK, "Deleting bindings");
286 map<string,vector<SGBinding *> >::iterator it;
287 for (it = _bindings.begin(); it != _bindings.end(); it++) {
288 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings for " << it->first);
289 for ( i = 0; i < it->second.size(); i++ )
290 delete it->second[i];
295 _char_arrays.clear();
296 _callback_arrays.clear();
297 SG_LOG(SG_GENERAL, SG_BULK, "Done.");
301 FGPUIMenuBar::make_object_map(SGPropertyNode * node)
303 unsigned int menu_index = 0;
304 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
305 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
306 obj; obj = obj->getNextObject()) {
308 // skip puPopupMenus. They are also children of _menuBar,
309 // but we access them via getUserData() (see below)
310 if (!(obj->getType() & PUCLASS_ONESHOT))
313 if (menu_index >= menus.size()) {
314 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
315 << node->getPath() << "/menu[" << menu_index << ']');
319 SGPropertyNode *menu = menus.at(menu_index);
320 _objects[menu->getPath()] = obj;
321 add_enabled_listener(menu);
323 puGroup *popup = (puGroup *)obj->getUserData();
327 // the entries are for some reason reversed (last first), and we
328 // don't know yet how many there will be; so we collect first
329 vector<puObject *> e;
330 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
333 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
334 for (unsigned int i = 0; i < e.size(); i++) {
335 if (i >= items.size()) {
336 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
337 << menu->getPath() << "/item[" << i << ']');
340 SGPropertyNode *item = items.at(e.size() - i - 1);
341 _objects[item->getPath()] = e[i];
342 add_enabled_listener(item);
350 struct EnabledListener : SGPropertyChangeListener {
351 void valueChanged(SGPropertyNode *node) {
352 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
355 FGPUIMenuBar* menubar = static_cast<FGPUIMenuBar*>(gui->getMenuBar());
357 menubar->enable_item(node->getParent(), node->getBoolValue());
361 } // of anonymous namespace
364 FGPUIMenuBar::add_enabled_listener(SGPropertyNode * node)
366 if (!node->hasValue("enabled"))
367 node->setBoolValue("enabled", true);
369 enable_item(node, node->getBoolValue("enabled"));
370 node->getNode("enabled")->addChangeListener(new EnabledListener());
374 FGPUIMenuBar::enable_item(const SGPropertyNode * node, bool state)
376 string path = node->getPath();
377 if (_objects.find(path) == _objects.end()) {
378 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
379 "non-existent menu item for node `" << path << '\'');
382 puObject *object = _objects[path];
392 FGPUIMenuBar::make_char_array (int size)
394 char ** list = new char*[size+1];
395 for (int i = 0; i <= size; i++)
397 _char_arrays.push_back(list);
402 FGPUIMenuBar::make_callback_array (int size)
404 puCallback * list = new puCallback[size+1];
405 for (int i = 0; i <= size; i++)
407 _callback_arrays.push_back(list);
411 // end of menubar.cxx