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