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());
81 ////////////////////////////////////////////////////////////////////////
82 // Implementation of FGPUIMenuBar.
83 ////////////////////////////////////////////////////////////////////////
86 FGPUIMenuBar::FGPUIMenuBar ()
92 FGPUIMenuBar::~FGPUIMenuBar ()
100 delete _menuBar; // FIXME: check if PUI owns the pointer
102 // FIXME: temporary commands to get at
103 // old, hard-coded dialogs.
104 add_deprecated_dialogs();
108 FGPUIMenuBar::show ()
116 FGPUIMenuBar::hide ()
124 FGPUIMenuBar::isVisible () const
130 FGPUIMenuBar::fireItem (puObject * item)
132 const char * name = item->getLegend();
133 vector<SGBinding *> &bindings = _bindings[name];
134 int nBindings = bindings.size();
136 for (int i = 0; i < nBindings; i++)
141 FGPUIMenuBar::make_menu (SGPropertyNode * node)
143 string s = getLocalizedLabel(node);
145 // hack: map at least some UTF-8 characters to Latin1, since FG fonts are
146 // Latin1 (or plain ASCII, which is a subset). This hack can be removed once
147 // the PLIB/OSG port is complete (OSG has full UTF-8 support! :) ).
148 FGLocale::utf8toLatin1(s);
150 const char* name = strdup(s.c_str());
152 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
154 int array_size = item_nodes.size();
156 char ** items = make_char_array(array_size);
157 puCallback * callbacks = make_callback_array(array_size);
159 for (unsigned int i = 0, j = item_nodes.size() - 1;
160 i < item_nodes.size();
163 // Set up the PUI entries for this item
164 string label = getLocalizedLabel(item_nodes[i]);
165 FGLocale::utf8toLatin1(label);
167 // append the keyboard hint to the menu entry
168 const char* key = item_nodes[i]->getStringValue("key", 0);
175 items[j] = strdup(label.c_str());
176 callbacks[j] = menu_callback;
178 // Load all the bindings for this item
179 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
180 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
182 for (unsigned int k = 0; k < bindings.size(); k++) {
184 SGPropertyNode_ptr binding;
185 while (dest->getChild("binding", m))
188 binding = dest->getChild("binding", m, true);
189 copyProperties(bindings[k], binding);
190 _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
194 _menuBar->add_submenu(name, items, callbacks);
198 FGPUIMenuBar::make_menubar ()
200 SGPropertyNode *targetpath;
202 targetpath = fgGetNode("/sim/menubar/default",true);
203 // fgLoadProps("gui/menubar.xml", targetpath);
205 /* NOTE: there is no check to see whether there's any usable data at all
207 * This would also have the advantage of being able to create some kind of
208 * 'fallback' menu - just in case that either menubar.xml is empty OR that
209 * its XML data is not valid, that way we would avoid displaying an
210 * unusable menubar without any functionality - if we decided to add another
211 * char * element to the commands structure in
212 * $FG_SRC/src/Main/fgcommands.cxx
213 * we could additionally save each function's (short) description and use
214 * this as label for the fallback PUI menubar item labels - as a workaround
215 * one might simply use the internal fgcommands and put them into the
216 * fallback menu, so that the user is at least able to re-init the menu
217 * loading - just in case there was some malformed XML in it
218 * (it happend to me ...)
221 make_menubar(targetpath);
224 /* WARNING: We aren't yet doing any validation of what's found - but since
225 * this isn't done with menubar.xml either, it should not really matter
226 * right now. Although one should later on consider to validate the
227 * contents, whether they are representing a 'legal' menubar structure.
230 FGPUIMenuBar::make_menubar(SGPropertyNode * props)
234 _menuBar = new puMenuBar;
236 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
237 for (unsigned int i = 0; i < menu_nodes.size(); i++)
238 make_menu(menu_nodes[i]);
241 make_object_map(props);
250 FGPUIMenuBar::destroy_menubar ()
256 puDeleteObject(_menuBar);
260 // Delete all the character arrays
261 // we were forced to keep around for
263 SG_LOG(SG_GENERAL, SG_BULK, "Deleting char arrays");
264 for (i = 0; i < _char_arrays.size(); i++) {
265 for (int j = 0; _char_arrays[i][j] != 0; j++)
266 free(_char_arrays[i][j]); // added with strdup
267 delete[] _char_arrays[i];
270 // Delete all the callback arrays
271 // we were forced to keep around for
273 SG_LOG(SG_GENERAL, SG_BULK, "Deleting callback arrays");
274 for (i = 0; i < _callback_arrays.size(); i++)
275 delete[] _callback_arrays[i];
277 // Delete all those bindings
278 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings");
279 map<string,vector<SGBinding *> >::iterator it;
280 for (it = _bindings.begin(); it != _bindings.end(); it++) {
281 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings for " << it->first);
282 for ( i = 0; i < it->second.size(); i++ )
283 delete it->second[i];
286 SG_LOG(SG_GENERAL, SG_BULK, "Done.");
290 FGPUIMenuBar::make_object_map(SGPropertyNode * node)
292 unsigned int menu_index = 0;
293 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
294 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
295 obj; obj = obj->getNextObject()) {
297 // skip puPopupMenus. They are also children of _menuBar,
298 // but we access them via getUserData() (see below)
299 if (!(obj->getType() & PUCLASS_ONESHOT))
302 if (menu_index >= menus.size()) {
303 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
304 << node->getPath() << "/menu[" << menu_index << ']');
308 SGPropertyNode *menu = menus.at(menu_index);
309 _objects[menu->getPath()] = obj;
310 add_enabled_listener(menu);
312 puGroup *popup = (puGroup *)obj->getUserData();
316 // the entries are for some reason reversed (last first), and we
317 // don't know yet how many there will be; so we collect first
318 vector<puObject *> e;
319 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
322 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
323 for (unsigned int i = 0; i < e.size(); i++) {
324 if (i >= items.size()) {
325 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
326 << menu->getPath() << "/item[" << i << ']');
329 SGPropertyNode *item = items.at(e.size() - i - 1);
330 _objects[item->getPath()] = e[i];
331 add_enabled_listener(item);
339 struct EnabledListener : SGPropertyChangeListener {
340 void valueChanged(SGPropertyNode *node) {
341 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
344 FGPUIMenuBar* menubar = static_cast<FGPUIMenuBar*>(gui->getMenuBar());
346 menubar->enable_item(node->getParent(), node->getBoolValue());
350 } // of anonymous namespace
353 FGPUIMenuBar::add_enabled_listener(SGPropertyNode * node)
355 if (!node->hasValue("enabled"))
356 node->setBoolValue("enabled", true);
358 enable_item(node, node->getBoolValue("enabled"));
359 node->getNode("enabled")->addChangeListener(new EnabledListener());
363 FGPUIMenuBar::enable_item(const SGPropertyNode * node, bool state)
365 string path = node->getPath();
366 if (_objects.find(path) == _objects.end()) {
367 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
368 "non-existent menu item for node `" << path << '\'');
371 puObject *object = _objects[path];
381 FGPUIMenuBar::make_char_array (int size)
383 char ** list = new char*[size+1];
384 for (int i = 0; i <= size; i++)
386 _char_arrays.push_back(list);
391 FGPUIMenuBar::make_callback_array (int size)
393 puCallback * list = new puCallback[size+1];
394 for (int i = 0; i <= size; i++)
396 _callback_arrays.push_back(list);
400 // end of menubar.cxx