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/fg_props.hxx>
15 #include "new_gui.hxx"
16 #include "FGPUIMenuBar.hxx"
21 ////////////////////////////////////////////////////////////////////////
24 // Deprecated wrappers for old menu commands.
26 // DO NOT ADD TO THESE. THEY WILL BE DELETED SOON!
28 // These are defined in gui_funcs.cxx. They should be replaced with
29 // user-configured dialogs and new commands where necessary.
30 ////////////////////////////////////////////////////////////////////////
32 #if defined(TR_HIRES_SNAP)
33 extern void dumpHiResSnapShot ();
35 do_hires_snapshot_dialog (const SGPropertyNode * arg)
40 #endif // TR_HIRES_SNAP
44 SGCommandMgr::command_t command;
45 } deprecated_dialogs [] = {
46 #if defined(TR_HIRES_SNAP)
47 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
53 add_deprecated_dialogs ()
55 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
56 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
57 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
58 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
59 deprecated_dialogs[i].command);
65 ////////////////////////////////////////////////////////////////////////
67 ////////////////////////////////////////////////////////////////////////
71 menu_callback (puObject * object)
73 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
74 FGPUIMenuBar* mb = static_cast<FGPUIMenuBar*>(gui->getMenuBar());
80 ////////////////////////////////////////////////////////////////////////
81 // Implementation of FGPUIMenuBar.
82 ////////////////////////////////////////////////////////////////////////
85 FGPUIMenuBar::FGPUIMenuBar ()
91 FGPUIMenuBar::~FGPUIMenuBar ()
99 delete _menuBar; // FIXME: check if PUI owns the pointer
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 const char * name = strdup(node->getStringValue("label"));
143 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
145 int array_size = item_nodes.size();
147 char ** items = make_char_array(array_size);
148 puCallback * callbacks = make_callback_array(array_size);
150 for (unsigned int i = 0, j = item_nodes.size() - 1;
151 i < item_nodes.size();
154 // Set up the PUI entries for this item
155 items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
156 callbacks[j] = menu_callback;
158 // Load all the bindings for this item
159 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
160 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
162 for (unsigned int k = 0; k < bindings.size(); k++) {
164 SGPropertyNode_ptr binding;
165 while (dest->getChild("binding", m))
168 binding = dest->getChild("binding", m, true);
169 copyProperties(bindings[k], binding);
170 _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
174 _menuBar->add_submenu(name, items, callbacks);
178 FGPUIMenuBar::make_menubar ()
180 SGPropertyNode *targetpath;
182 targetpath = fgGetNode("/sim/menubar/default",true);
183 // fgLoadProps("gui/menubar.xml", targetpath);
185 /* NOTE: there is no check to see whether there's any usable data at all
187 * This would also have the advantage of being able to create some kind of
188 * 'fallback' menu - just in case that either menubar.xml is empty OR that
189 * its XML data is not valid, that way we would avoid displaying an
190 * unusable menubar without any functionality - if we decided to add another
191 * char * element to the commands structure in
192 * $FG_SRC/src/Main/fgcommands.cxx
193 * we could additionally save each function's (short) description and use
194 * this as label for the fallback PUI menubar item labels - as a workaround
195 * one might simply use the internal fgcommands and put them into the
196 * fallback menu, so that the user is at least able to re-init the menu
197 * loading - just in case there was some malformed XML in it
198 * (it happend to me ...)
201 make_menubar(targetpath);
204 /* WARNING: We aren't yet doing any validation of what's found - but since
205 * this isn't done with menubar.xml either, it should not really matter
206 * right now. Although one should later on consider to validate the
207 * contents, whether they are representing a 'legal' menubar structure.
210 FGPUIMenuBar::make_menubar(SGPropertyNode * props)
214 _menuBar = new puMenuBar;
216 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
217 for (unsigned int i = 0; i < menu_nodes.size(); i++)
218 make_menu(menu_nodes[i]);
221 make_object_map(props);
230 FGPUIMenuBar::destroy_menubar ()
236 puDeleteObject(_menuBar);
240 // Delete all the character arrays
241 // we were forced to keep around for
243 SG_LOG(SG_GENERAL, SG_BULK, "Deleting char arrays");
244 for (i = 0; i < _char_arrays.size(); i++) {
245 for (int j = 0; _char_arrays[i][j] != 0; j++)
246 free(_char_arrays[i][j]); // added with strdup
247 delete[] _char_arrays[i];
250 // Delete all the callback arrays
251 // we were forced to keep around for
253 SG_LOG(SG_GENERAL, SG_BULK, "Deleting callback arrays");
254 for (i = 0; i < _callback_arrays.size(); i++)
255 delete[] _callback_arrays[i];
257 // Delete all those bindings
258 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings");
259 map<string,vector<SGBinding *> >::iterator it;
260 for (it = _bindings.begin(); it != _bindings.end(); it++) {
261 SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings for " << it->first);
262 for ( i = 0; i < it->second.size(); i++ )
263 delete it->second[i];
266 SG_LOG(SG_GENERAL, SG_BULK, "Done.");
270 FGPUIMenuBar::make_object_map(SGPropertyNode * node)
272 unsigned int menu_index = 0;
273 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
274 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
275 obj; obj = obj->getNextObject()) {
277 // skip puPopupMenus. They are also children of _menuBar,
278 // but we access them via getUserData() (see below)
279 if (!(obj->getType() & PUCLASS_ONESHOT))
282 if (menu_index >= menus.size()) {
283 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
284 << node->getPath() << "/menu[" << menu_index << ']');
288 SGPropertyNode *menu = menus.at(menu_index);
289 _objects[menu->getPath()] = obj;
290 add_enabled_listener(menu);
292 puGroup *popup = (puGroup *)obj->getUserData();
296 // the entries are for some reason reversed (last first), and we
297 // don't know yet how many there will be; so we collect first
298 vector<puObject *> e;
299 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
302 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
303 for (unsigned int i = 0; i < e.size(); i++) {
304 if (i >= items.size()) {
305 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
306 << menu->getPath() << "/item[" << i << ']');
309 SGPropertyNode *item = items.at(e.size() - i - 1);
310 _objects[item->getPath()] = e[i];
311 add_enabled_listener(item);
317 struct EnabledListener : SGPropertyChangeListener {
318 void valueChanged(SGPropertyNode *node) {
319 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
322 FGPUIMenuBar* menubar = static_cast<FGPUIMenuBar*>(gui->getMenuBar());
324 menubar->enable_item(node->getParent(), node->getBoolValue());
329 FGPUIMenuBar::add_enabled_listener(SGPropertyNode * node)
331 if (!node->hasValue("enabled"))
332 node->setBoolValue("enabled", true);
334 enable_item(node, node->getBoolValue("enabled"));
335 node->getNode("enabled")->addChangeListener(new EnabledListener());
339 FGPUIMenuBar::enable_item(const SGPropertyNode * node, bool state)
341 string path = node->getPath();
342 if (_objects.find(path) == _objects.end()) {
343 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
344 "non-existent menu item for node `" << path << '\'');
347 puObject *object = _objects[path];
357 FGPUIMenuBar::make_char_array (int size)
359 char ** list = new char*[size+1];
360 for (int i = 0; i <= size; i++)
362 _char_arrays.push_back(list);
367 FGPUIMenuBar::make_callback_array (int size)
369 puCallback * list = new puCallback[size+1];
370 for (int i = 0; i <= size; i++)
372 _callback_arrays.push_back(list);
376 // end of menubar.cxx