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