]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Menubar_pimpl.C
disable submenus which contain only disabled items
[lyx.git] / src / frontends / xforms / Menubar_pimpl.C
1 /**
2  * \file Menubar_pimpl.C
3  * Copyright 1999-2001 The LyX Team.
4  * See the file COPYING.
5  *
6  * \author  Lars Gullik Bjønnes, larsbj@lyx.org
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "Menubar_pimpl.h"
16 #include "MenuBackend.h"
17 #include "LyXAction.h"
18 #include "kbmap.h"
19 #include "Dialogs.h"
20 #include "XFormsView.h"
21 #include "lyxfunc.h"
22 #include "support/lstrings.h"
23 #include "support/LAssert.h"
24 #include "gettext.h"
25 #include "debug.h"
26 #include FORMS_H_LOCATION
27
28 #include <boost/scoped_ptr.hpp>
29
30 #include <algorithm>
31
32 using std::endl;
33 using std::vector;
34 using std::max;
35 using std::min;
36 using std::for_each;
37
38 typedef vector<int>::size_type size_type;
39
40 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
41 extern LyXAction lyxaction;
42
43 namespace {
44
45 // Some constants
46 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
47 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
48 int const mheight = 30;
49 int const mbheight= 22;
50 // where to place the menubar?
51 int const yloc = (mheight - mbheight)/2; //air + bw;
52 int const mbadd = 20; // menu button add (to width)
53 // Some space between buttons on the menubar
54 int const air = 2;
55 char const * menu_tabstop = "aa";
56 char const * default_tabstop = "aaaaaaaa";
57 // We do not want to mix position values in a menu (like the index of
58 // a submenu) with the action numbers which convey actual information.
59 // Therefore we offset all the action values by an arbitrary large
60 // constant.
61 int const action_offset = 1000;
62
63 // This is used a few times below.
64 inline
65 int string_width(string const & str)
66 {
67         return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
68                                       str.c_str(),
69                                       static_cast<int>(str.length()));
70 }
71
72 } // namespace anon
73
74
75 extern "C" {
76
77         //Defined later, used in makeMenubar().
78         static
79         void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
80         {
81                 Menubar::Pimpl::MenuCallback(ob, button);
82         }
83
84 }
85
86
87 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
88         : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
89 {
90         makeMenubar(menubackend_->getMenubar());
91 }
92
93
94 void Menubar::Pimpl::makeMenubar(Menu const & menu)
95 {
96         FL_FORM * form = owner_->getForm();
97         int moffset = 0;
98
99         // Create menu frame if there is non yet.
100         FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
101                                          form->w, mheight, "");
102         fl_set_object_resize(frame, FL_RESIZE_ALL);
103         fl_set_object_gravity(frame, NorthWestGravity,
104                               NorthEastGravity);
105
106         Menu::const_iterator i = menu.begin();
107         Menu::const_iterator end = menu.end();
108         for (; i != end; ++i) {
109                 FL_OBJECT * obj;
110                 if (i->kind() != MenuItem::Submenu) {
111                         lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
112                                 " only submenus can appear in a menubar"
113                                << endl;
114                         continue;
115                 }
116                 string const label = i->label();
117                 string const shortcut = "#" + i->shortcut();
118                 int const width = string_width(label);
119                 obj = fl_add_button(FL_MENU_BUTTON,
120                                     air + moffset, yloc,
121                                     width + mbadd,
122                                     mbheight,
123                                     label.c_str());
124                 fl_set_object_boxtype(obj, FL_FLAT_BOX);
125                 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
126                 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
127                 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
128                 fl_set_object_resize(obj, FL_RESIZE_ALL);
129                 fl_set_object_gravity(obj, NorthWestGravity,
130                                       NorthWestGravity);
131                 moffset += obj->w + air;
132                 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
133                 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
134
135                 boost::shared_ptr<ItemInfo>
136                         iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
137                 buttonlist_.push_back(iteminfo);
138                 obj->u_vdata = iteminfo.get();
139         }
140
141 }
142
143
144 void Menubar::Pimpl::update()
145 {
146         // nothing yet
147 }
148
149
150 void Menubar::Pimpl::openByName(string const & name)
151 {
152         for (ButtonList::const_iterator cit = buttonlist_.begin();
153              cit != buttonlist_.end(); ++cit) {
154                 if ((*cit)->item_->submenuname() == name) {
155                         MenuCallback((*cit)->obj_, 1);
156                         return;
157                 }
158         }
159
160         lyxerr << "Menubar::Pimpl::openByName: menu "
161                << name << " not found" << endl;
162 }
163
164
165 namespace {
166
167 Menu::size_type const max_number_of_items = 25;
168
169 int get_new_submenu(vector<int> & smn, Window win)
170 {
171         static size_type max_number_of_menus = 32;
172         if (smn.size() >= max_number_of_menus)
173                 max_number_of_menus =
174                     fl_setpup_maxpup(static_cast<int>(2*smn.size()));
175         int menu = fl_newpup(win);
176         fl_setpup_softedge(menu, true);
177         fl_setpup_bw(menu, -1);
178         lyxerr[Debug::GUI] << "Adding menu " << menu
179                            << " in deletion list" << endl;
180         smn.push_back(menu);
181         return menu;
182 }
183
184
185 string const fixlabel(string const & str)
186 {
187 #if FL_VERSION < 1 && FL_REVISION < 89
188         return subst(str, '%', '?');
189 #else
190         return subst(str, "%", "%%");
191 #endif
192 }
193
194
195
196 } // namespace anon
197
198
199
200 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
201                                    Menu const & menu, vector<int> & smn,
202                                    bool & all_disabled)
203 {
204         const int menuid = get_new_submenu(smn, win);
205         lyxerr[Debug::GUI] << "Menubar::Pimpl::create_submenu: creating "
206                            << menu.name() << " as menuid=" << menuid << endl;
207
208         // Compute the size of the largest label (because xforms is
209         // not able to support shortcuts correctly...)
210         int max_width = 0;
211         string widest_label;
212         Menu::const_iterator end = menu.end();
213         for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
214                 MenuItem const & item = (*i);
215                 if (item.kind() == MenuItem::Command) {
216                         string const label = item.label() + '\t';
217                         int const width = string_width(label);
218                         if (width > max_width) {
219                                 max_width = width;
220                                 widest_label = label;
221                         }
222                 }
223         }
224         lyxerr[Debug::GUI] << "max_width=" << max_width
225                            << ", widest_label=`" << widest_label
226                            << "'" << endl;
227
228         // Compute where to put separators
229         vector<string> extra_labels(menu.size());
230         vector<string>::iterator it = extra_labels.begin();
231         vector<string>::iterator last = it;
232         for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it)
233                 if (i->kind() == MenuItem::Separator)
234                         *last = "%l";
235                 else if (!i->optional() ||
236                          !(view->getLyXFunc()->getStatus(i->action()).disabled()))
237                         last = it;
238
239         it = extra_labels.begin();
240         size_type count = 0;
241         int curmenuid = menuid;
242         for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it) {
243                 MenuItem const & item = (*i);
244                 string & extra_label = *it;
245
246                 ++count;
247                 if (count > max_number_of_items) {
248                         int tmpmenuid = get_new_submenu(smn, win);
249                         lyxerr[Debug::GUI] << "Too many items, creating "
250                                            << "new menu " << tmpmenuid << endl;
251                         string label = _("More");
252                         label += "...%m";
253                         fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
254                         count = 1;
255                         curmenuid = tmpmenuid;
256                 }
257
258                 switch (item.kind()) {
259                 case MenuItem::Command: {
260                         FuncStatus const flag =
261                                 view->getLyXFunc()->getStatus(item.action());
262                         // handle optional entries.
263                         if (item.optional()
264                             && (flag.disabled())) {
265                                 lyxerr[Debug::GUI]
266                                         << "Skipping optional item "
267                                         << item.label() << endl;
268                                 break;
269                         }
270
271                         // Get the keys bound to this action, but keep only the
272                         // first one later
273                         string const accel =
274                                 toplevel_keymap->findbinding(item.action());
275                         // Build the menu label from all the info
276                         string label = fixlabel(item.label());
277
278                         if (!accel.empty()) {
279                                 // Try to be clever and add  just enough
280                                 // tabs to align shortcuts.
281                                 do
282                                         label += '\t';
283                                 while (string_width(label) < max_width + 5);
284                                 label += accel.substr(1,accel.find(']') - 1);
285                         }
286                         label += "%x" + tostr(item.action() + action_offset)
287                                 + extra_label;
288
289                         // Modify the entry using the function status
290                         string pupmode;
291                         if (flag.onoff(true))
292                                 pupmode += "%B";
293                         if (flag.onoff(false))
294                                 pupmode += "%b";
295                         if (flag.disabled() || flag.unknown())
296                                 pupmode += "%i";
297                         else
298                                 all_disabled = false;
299                         label += pupmode;
300
301                         // Finally the menu shortcut
302                         string shortcut = item.shortcut();
303
304                         if (!shortcut.empty()) {
305                                 shortcut += lowercase(shortcut[0]);
306                                 label += "%h";
307                                 fl_addtopup(curmenuid, label.c_str(),
308                                             shortcut.c_str());
309                         } else
310                                 fl_addtopup(curmenuid, label.c_str());
311
312                         lyxerr[Debug::GUI] << "Command: \""
313                                            << lyxaction.getActionName(item.action())
314                                            << "\", binding \"" << accel
315                                            << "\", shortcut \"" << shortcut
316                                            << "\" (added to menu"
317                                            << curmenuid << ")" << endl;
318                         break;
319                 }
320
321                 case MenuItem::Submenu: {
322                         int submenuid = create_submenu(win, view,
323                                                        *item.submenu(), smn,
324                                                        all_disabled);
325                         if (submenuid == -1)
326                                 return -1;
327                         string label = fixlabel(item.label());
328                         label += extra_label + "%m";
329                         if (all_disabled)
330                                 label += "%i";
331                         string shortcut = item.shortcut();
332                         if (!shortcut.empty()) {
333                                 shortcut += lowercase(shortcut[0]);
334                                 label += "%h";
335                                 fl_addtopup(curmenuid, label.c_str(),
336                                             submenuid, shortcut.c_str());
337                         } else {
338                                 fl_addtopup(curmenuid, label.c_str(),
339                                             submenuid);
340                         }
341                         break;
342                 }
343
344                 case MenuItem::Separator:
345                         // already done, and if it was the first one,
346                         // we just ignore it.
347                         break;
348
349
350                 default:
351                         lyxerr << "Menubar::Pimpl::create_submenu: "
352                                 "this should not happen" << endl;
353                         break;
354                 }
355         }
356         return menuid;
357 }
358
359
360 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
361 {
362         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
363         XFormsView * view = iteminfo->pimpl_->owner_;
364         MenuItem const * item = iteminfo->item_.get();
365
366         if (button == 1) {
367                 // set the pseudo menu-button
368                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
369                 fl_set_button(ob, 0);
370                 fl_redraw_object(ob);
371         }
372
373         // Paranoia check
374         lyx::Assert(item->kind() == MenuItem::Submenu);
375
376         // set tabstop length
377         fl_set_tabstop(menu_tabstop);
378
379         MenuBackend const * menubackend_ = iteminfo->pimpl_->menubackend_;
380         Menu tomenu;
381         Menu const frommenu = menubackend_->getMenu(item->submenuname());
382         menubackend_->expand(frommenu, tomenu, view->buffer());
383         vector<int> submenus;
384         bool all_disabled = true;
385         int menu = iteminfo->pimpl_->
386                 create_submenu(FL_ObjWin(ob), view, tomenu,
387                                submenus, all_disabled);
388         if (menu != -1) {
389                 // place popup
390                 fl_setpup_position(view->getForm()->x + ob->x,
391                                    view->getForm()->y + ob->y + ob->h + 10);
392                 int choice = fl_dopup(menu);
393
394                 if (button == 1) {
395                                 // set the pseudo menu-button back
396                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
397                         fl_redraw_object(ob);
398                 }
399
400                 // If the action value is too low, then it is not a
401                 // valid action, but something else.
402                 if (choice >= action_offset + 1) {
403                         view->getLyXFunc()->dispatch(choice - action_offset, true);
404                 } else {
405                         lyxerr[Debug::GUI]
406                                 << "MenuCallback: ignoring bogus action "
407                                 << choice << endl;
408                 }
409         } else {
410                 lyxerr << "Error in MenuCallback" << endl;
411         }
412
413         for_each(submenus.begin(), submenus.end(), fl_freepup);
414         // restore tabstop length
415         fl_set_tabstop(default_tabstop);
416
417 }