]> git.mxchange.org Git - flightgear.git/blob - src/GUI/FGPUIMenuBar.cxx
Move viewer-related sources to separate folder.
[flightgear.git] / src / GUI / FGPUIMenuBar.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #include <string.h>
6 #include <iostream>
7 #include <plib/pu.h>
8 #include <simgear/debug/logstream.hxx>
9 #include <simgear/structure/SGBinding.hxx>
10 #include <simgear/props/props_io.hxx>
11
12 #include <Main/globals.hxx>
13 #include <Main/locale.hxx>
14 #include <Main/fg_props.hxx>
15
16 #include "new_gui.hxx"
17 #include "FGPUIMenuBar.hxx"
18
19 using std::vector;
20 using std::string;
21 using std::map;\f
22 ////////////////////////////////////////////////////////////////////////
23 // FIXME!!
24 //
25 // Deprecated wrappers for old menu commands.
26 //
27 // DO NOT ADD TO THESE.  THEY WILL BE DELETED SOON!
28 //
29 // These are defined in gui_funcs.cxx.  They should be replaced with
30 // user-configured dialogs and new commands where necessary.
31 ////////////////////////////////////////////////////////////////////////
32
33 #if defined(TR_HIRES_SNAP)
34 extern void dumpHiResSnapShot ();
35 static bool
36 do_hires_snapshot_dialog (const SGPropertyNode * arg)
37 {
38     dumpHiResSnapShot();
39     return true;
40 }
41 #endif // TR_HIRES_SNAP
42
43 static struct {
44     const char * name;
45     SGCommandMgr::command_t command;
46 } deprecated_dialogs [] = {
47 #if defined(TR_HIRES_SNAP)
48     { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
49 #endif
50     { 0, 0 }
51 };
52
53 static void
54 add_deprecated_dialogs ()
55 {
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);
61   }
62 }
63
64
65 \f
66 ////////////////////////////////////////////////////////////////////////
67 // Static functions.
68 ////////////////////////////////////////////////////////////////////////
69
70
71 static void
72 menu_callback (puObject * object)
73 {
74     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
75     FGPUIMenuBar* mb = static_cast<FGPUIMenuBar*>(gui->getMenuBar());
76     mb->fireItem(object);
77 }
78
79
80 \f
81 ////////////////////////////////////////////////////////////////////////
82 // Implementation of FGPUIMenuBar.
83 ////////////////////////////////////////////////////////////////////////
84
85
86 FGPUIMenuBar::FGPUIMenuBar ()
87     : _visible(false),
88       _menuBar(0)
89 {
90 }
91
92 FGPUIMenuBar::~FGPUIMenuBar ()
93 {
94     destroy_menubar();
95 }
96
97 void
98 FGPUIMenuBar::init ()
99 {
100     delete _menuBar;            // FIXME: check if PUI owns the pointer
101     make_menubar();
102                                 // FIXME: temporary commands to get at
103                                 // old, hard-coded dialogs.
104     add_deprecated_dialogs();
105 }
106
107 void
108 FGPUIMenuBar::show ()
109 {
110     if (_menuBar != 0)
111         _menuBar->reveal();
112     _visible = true;
113 }
114
115 void
116 FGPUIMenuBar::hide ()
117 {
118     if (_menuBar != 0)
119         _menuBar->hide();
120     _visible = false;
121 }
122
123 bool
124 FGPUIMenuBar::isVisible () const
125 {
126     return _visible;
127 }
128
129 void
130 FGPUIMenuBar::fireItem (puObject * item)
131 {
132     const char * name = item->getLegend();
133     vector<SGBinding *> &bindings = _bindings[name];
134     int nBindings = bindings.size();
135
136     for (int i = 0; i < nBindings; i++)
137         bindings[i]->fire();
138 }
139
140 void
141 FGPUIMenuBar::make_menu (SGPropertyNode * node)
142 {
143     string s = getLocalizedLabel(node);
144
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);
149
150     const char* name = strdup(s.c_str());
151
152     vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
153
154     int array_size = item_nodes.size();
155
156     char ** items = make_char_array(array_size);
157     puCallback * callbacks = make_callback_array(array_size);
158
159     for (unsigned int i = 0, j = item_nodes.size() - 1;
160          i < item_nodes.size();
161          i++, j--) {
162
163                                 // Set up the PUI entries for this item
164         string label = getLocalizedLabel(item_nodes[i]);
165         FGLocale::utf8toLatin1(label);
166
167         // append the keyboard hint to the menu entry
168         const char* key = item_nodes[i]->getStringValue("key", 0);
169         if (key)
170         {
171             label.append("           <");
172             label.append(key);
173             label.append(">");
174         }
175         items[j] = strdup(label.c_str());
176         callbacks[j] = menu_callback;
177
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);
181
182         for (unsigned int k = 0; k < bindings.size(); k++) {
183             unsigned int m = 0;
184             SGPropertyNode_ptr binding;
185             while (dest->getChild("binding", m))
186                 m++;
187
188             binding = dest->getChild("binding", m, true);
189             copyProperties(bindings[k], binding);
190             _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
191         }
192     }
193
194     _menuBar->add_submenu(name, items, callbacks);
195 }
196
197 void
198 FGPUIMenuBar::make_menubar ()
199 {
200     SGPropertyNode *targetpath;
201    
202     targetpath = fgGetNode("/sim/menubar/default",true);
203     // fgLoadProps("gui/menubar.xml", targetpath);
204     
205     /* NOTE: there is no check to see whether there's any usable data at all
206      *
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 ...)
219      */
220     
221     make_menubar(targetpath);
222 }
223
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.
228  */
229 void
230 FGPUIMenuBar::make_menubar(SGPropertyNode * props) 
231 {    
232     // Just in case.
233     destroy_menubar();
234     _menuBar = new puMenuBar;
235
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]);
239
240     _menuBar->close();
241     make_object_map(props);
242
243     if (_visible)
244         _menuBar->reveal();
245     else
246         _menuBar->hide();
247 }
248
249 void
250 FGPUIMenuBar::destroy_menubar ()
251 {
252     if ( _menuBar == 0 )
253         return;
254
255     hide();
256     puDeleteObject(_menuBar);
257
258     unsigned int i;
259
260                                 // Delete all the character arrays
261                                 // we were forced to keep around for
262                                 // plib.
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];
268     }
269
270                                 // Delete all the callback arrays
271                                 // we were forced to keep around for
272                                 // plib.
273     SG_LOG(SG_GENERAL, SG_BULK, "Deleting callback arrays");
274     for (i = 0; i < _callback_arrays.size(); i++)
275         delete[] _callback_arrays[i];
276
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];
284     }
285
286     SG_LOG(SG_GENERAL, SG_BULK, "Done.");
287 }
288
289 void
290 FGPUIMenuBar::make_object_map(SGPropertyNode * node)
291 {
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()) {
296
297         // skip puPopupMenus. They are also children of _menuBar,
298         // but we access them via getUserData()  (see below)
299         if (!(obj->getType() & PUCLASS_ONESHOT))
300             continue;
301
302         if (menu_index >= menus.size()) {
303             SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
304                     << node->getPath() << "/menu[" << menu_index << ']');
305             return;
306         }
307
308         SGPropertyNode *menu = menus.at(menu_index);
309         _objects[menu->getPath()] = obj;
310         add_enabled_listener(menu);
311
312         puGroup *popup = (puGroup *)obj->getUserData();
313         if (!popup)
314             continue;
315
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())
320             e.push_back(me);
321
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 << ']');
327                 break;
328             }
329             SGPropertyNode *item = items.at(e.size() - i - 1);
330             _objects[item->getPath()] = e[i];
331             add_enabled_listener(item);
332         }
333         menu_index++;
334     }
335 }
336
337 namespace { 
338   
339   struct EnabledListener : SGPropertyChangeListener {
340     void valueChanged(SGPropertyNode *node) {
341         NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
342         if (!gui)
343             return;
344         FGPUIMenuBar* menubar = static_cast<FGPUIMenuBar*>(gui->getMenuBar());
345         if (menubar)
346             menubar->enable_item(node->getParent(), node->getBoolValue());
347     }
348 };
349
350 } // of anonymous namespace
351
352 void
353 FGPUIMenuBar::add_enabled_listener(SGPropertyNode * node)
354 {
355     if (!node->hasValue("enabled"))
356         node->setBoolValue("enabled", true);
357
358     enable_item(node, node->getBoolValue("enabled"));
359     node->getNode("enabled")->addChangeListener(new EnabledListener());
360 }
361
362 bool
363 FGPUIMenuBar::enable_item(const SGPropertyNode * node, bool state)
364 {
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 << '\'');
369         return false;
370     }
371     puObject *object = _objects[path];
372     if (state)
373         object->activate();
374     else
375         object->greyOut();
376
377     return true;
378 }
379
380 char **
381 FGPUIMenuBar::make_char_array (int size)
382 {
383     char ** list = new char*[size+1];
384     for (int i = 0; i <= size; i++)
385         list[i] = 0;
386     _char_arrays.push_back(list);
387     return list;
388 }
389
390 puCallback *
391 FGPUIMenuBar::make_callback_array (int size)
392 {
393     puCallback * list = new puCallback[size+1];
394     for (int i = 0; i <= size; i++)
395         list[i] = 0;
396     _callback_arrays.push_back(list);
397     return list;
398 }
399
400 // end of menubar.cxx