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 ()
101 // FIXME: temporary commands to get at
102 // old, hard-coded dialogs.
103 add_deprecated_dialogs();
107 FGPUIMenuBar::show ()
115 FGPUIMenuBar::hide ()
123 FGPUIMenuBar::isVisible () const
129 FGPUIMenuBar::fireItem (puObject * item)
131 const char * name = item->getLegend();
132 vector<SGBinding *> &bindings = _bindings[name];
133 int nBindings = bindings.size();
135 for (int i = 0; i < nBindings; i++)
140 FGPUIMenuBar::make_menu (SGPropertyNode * node)
142 string s = getLocalizedLabel(node);
144 // hack: map at least some UTF-8 characters to Latin1, since FG fonts are
145 // Latin1 (or plain ASCII, which is a subset). This hack can be removed once
146 // the PLIB/OSG port is complete (OSG has full UTF-8 support! :) ).
147 FGLocale::utf8toLatin1(s);
149 const char* name = strdup(s.c_str());
151 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
153 int array_size = item_nodes.size();
155 char ** items = make_char_array(array_size);
156 puCallback * callbacks = make_callback_array(array_size);
158 for (unsigned int i = 0, j = item_nodes.size() - 1;
159 i < item_nodes.size();
162 // Set up the PUI entries for this item
163 string label = getLocalizedLabel(item_nodes[i]);
164 FGLocale::utf8toLatin1(label);
166 // append the keyboard hint to the menu entry
167 const char* key = item_nodes[i]->getStringValue("key", 0);
174 items[j] = strdup(label.c_str());
175 callbacks[j] = menu_callback;
177 // Load all the bindings for this item
178 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
179 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
181 for (unsigned int k = 0; k < bindings.size(); k++) {
183 SGPropertyNode_ptr binding;
184 while (dest->getChild("binding", m))
187 binding = dest->getChild("binding", m, true);
188 copyProperties(bindings[k], binding);
189 _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
193 _menuBar->add_submenu(name, items, callbacks);
197 FGPUIMenuBar::make_menubar ()
199 SGPropertyNode *targetpath;
201 targetpath = fgGetNode("/sim/menubar/default",true);
202 // fgLoadProps("gui/menubar.xml", targetpath);
204 /* NOTE: there is no check to see whether there's any usable data at all
206 * This would also have the advantage of being able to create some kind of
207 * 'fallback' menu - just in case that either menubar.xml is empty OR that
208 * its XML data is not valid, that way we would avoid displaying an
209 * unusable menubar without any functionality - if we decided to add another
210 * char * element to the commands structure in
211 * $FG_SRC/src/Main/fgcommands.cxx
212 * we could additionally save each function's (short) description and use
213 * this as label for the fallback PUI menubar item labels - as a workaround
214 * one might simply use the internal fgcommands and put them into the
215 * fallback menu, so that the user is at least able to re-init the menu
216 * loading - just in case there was some malformed XML in it
217 * (it happend to me ...)
220 make_menubar(targetpath);
223 /* WARNING: We aren't yet doing any validation of what's found - but since
224 * this isn't done with menubar.xml either, it should not really matter
225 * right now. Although one should later on consider to validate the
226 * contents, whether they are representing a 'legal' menubar structure.
229 FGPUIMenuBar::make_menubar(SGPropertyNode * props)
233 _menuBar = new puMenuBar;
235 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
236 for (unsigned int i = 0; i < menu_nodes.size(); i++)
237 make_menu(menu_nodes[i]);
240 make_object_map(props);
249 FGPUIMenuBar::destroy_menubar ()
255 puDeleteObject(_menuBar);
259 // Delete all the character arrays
260 // we were forced to keep around for
262 SG_LOG(SG_GENERAL, SG_BULK, "Deleting char arrays");
263 for (i = 0; i < _char_arrays.size(); i++) {
264 for (int j = 0; _char_arrays[i][j] != 0; j++) {
265 free(_char_arrays[i][j]); // added with strdup
266 _char_arrays[i][j] = 0;
268 delete[] _char_arrays[i];
272 // Delete all the callback arrays
273 // we were forced to keep around for
275 SG_LOG(SG_GENERAL, SG_BULK, "Deleting callback arrays");
276 for (i = 0; i < _callback_arrays.size(); i++)
277 delete[] _callback_arrays[i];
279 // Delete all those bindings
280 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings");
281 map<string,vector<SGBinding *> >::iterator it;
282 for (it = _bindings.begin(); it != _bindings.end(); it++) {
283 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings for " << it->first);
284 for ( i = 0; i < it->second.size(); i++ )
285 delete it->second[i];
290 _char_arrays.clear();
291 _callback_arrays.clear();
292 SG_LOG(SG_GENERAL, SG_BULK, "Done.");
296 FGPUIMenuBar::make_object_map(SGPropertyNode * node)
298 unsigned int menu_index = 0;
299 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
300 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
301 obj; obj = obj->getNextObject()) {
303 // skip puPopupMenus. They are also children of _menuBar,
304 // but we access them via getUserData() (see below)
305 if (!(obj->getType() & PUCLASS_ONESHOT))
308 if (menu_index >= menus.size()) {
309 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
310 << node->getPath() << "/menu[" << menu_index << ']');
314 SGPropertyNode *menu = menus.at(menu_index);
315 _objects[menu->getPath()] = obj;
316 add_enabled_listener(menu);
318 puGroup *popup = (puGroup *)obj->getUserData();
322 // the entries are for some reason reversed (last first), and we
323 // don't know yet how many there will be; so we collect first
324 vector<puObject *> e;
325 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
328 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
329 for (unsigned int i = 0; i < e.size(); i++) {
330 if (i >= items.size()) {
331 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
332 << menu->getPath() << "/item[" << i << ']');
335 SGPropertyNode *item = items.at(e.size() - i - 1);
336 _objects[item->getPath()] = e[i];
337 add_enabled_listener(item);
345 struct EnabledListener : SGPropertyChangeListener {
346 void valueChanged(SGPropertyNode *node) {
347 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
350 FGPUIMenuBar* menubar = static_cast<FGPUIMenuBar*>(gui->getMenuBar());
352 menubar->enable_item(node->getParent(), node->getBoolValue());
356 } // of anonymous namespace
359 FGPUIMenuBar::add_enabled_listener(SGPropertyNode * node)
361 if (!node->hasValue("enabled"))
362 node->setBoolValue("enabled", true);
364 enable_item(node, node->getBoolValue("enabled"));
365 node->getNode("enabled")->addChangeListener(new EnabledListener());
369 FGPUIMenuBar::enable_item(const SGPropertyNode * node, bool state)
371 string path = node->getPath();
372 if (_objects.find(path) == _objects.end()) {
373 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
374 "non-existent menu item for node `" << path << '\'');
377 puObject *object = _objects[path];
387 FGPUIMenuBar::make_char_array (int size)
389 char ** list = new char*[size+1];
390 for (int i = 0; i <= size; i++)
392 _char_arrays.push_back(list);
397 FGPUIMenuBar::make_callback_array (int size)
399 puCallback * list = new puCallback[size+1];
400 for (int i = 0; i <= size; i++)
402 _callback_arrays.push_back(list);
406 // end of menubar.cxx