]> git.lyx.org Git - features.git/blob - src/frontends/xforms/Menubar_pimpl.C
c5503dca2cd15f12c5d3fdf0402764193a8a0bfa
[features.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 "FloatList.h"
23 #include "support/lstrings.h"
24 #include "support/LAssert.h"
25 #include "gettext.h"
26 #include "debug.h"
27 #include "toc.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 extern LyXAction lyxaction;
44
45 namespace {
46
47 // Some constants
48 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
49 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
50 int const mheight = 30;
51 int const mbheight= 22;
52 // where to place the menubar?
53 int const yloc = (mheight - mbheight)/2; //air + bw;
54 int const mbadd = 20; // menu button add (to width)
55 // Some space between buttons on the menubar
56 int const air = 2;
57 char const * menu_tabstop = "aa";
58 char const * default_tabstop = "aaaaaaaa";
59 // We do not want to mix position values in a menu (like the index of
60 // a submenu) with the action numbers which convey actual information.
61 // Therefore we offset all the action values by an arbitrary large
62 // constant.
63 int const action_offset = 1000;
64
65 // This is used a few times below.
66 inline
67 int string_width(string const & str)
68 {
69         return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
70                                       str.c_str(),
71                                       static_cast<int>(str.length()));
72 }
73
74 } // namespace anon
75
76
77 extern "C" {
78
79         //Defined later, used in makeMenubar().
80         static
81         void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
82         {
83                 Menubar::Pimpl::MenuCallback(ob, button);
84         }
85
86 }
87
88
89 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
90         : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
91 {
92         makeMenubar(menubackend_->getMenubar());
93 }
94
95
96 void Menubar::Pimpl::makeMenubar(Menu const & menu)
97 {
98         FL_FORM * form = owner_->getForm();
99         int moffset = 0;
100
101         // Create menu frame if there is non yet.
102         FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
103                                          form->w, mheight, "");
104         fl_set_object_resize(frame, FL_RESIZE_ALL);
105         fl_set_object_gravity(frame, NorthWestGravity,
106                               NorthEastGravity);
107
108         Menu::const_iterator i = menu.begin();
109         Menu::const_iterator end = menu.end();
110         for (; i != end; ++i) {
111                 FL_OBJECT * obj;
112                 if (i->kind() != MenuItem::Submenu) {
113                         lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
114                                 " only submenus can appear in a menubar"
115                                << endl;
116                         continue;
117                 }
118                 string const label = i->label();
119                 string const shortcut = "#" + i->shortcut();
120                 int const width = string_width(label);
121                 obj = fl_add_button(FL_MENU_BUTTON,
122                                     air + moffset, yloc,
123                                     width + mbadd,
124                                     mbheight,
125                                     label.c_str());
126                 fl_set_object_boxtype(obj, FL_FLAT_BOX);
127                 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
128                 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
129                 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
130                 fl_set_object_resize(obj, FL_RESIZE_ALL);
131                 fl_set_object_gravity(obj, NorthWestGravity,
132                                       NorthWestGravity);
133                 moffset += obj->w + air;
134                 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
135                 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
136
137                 boost::shared_ptr<ItemInfo>
138                         iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
139                 buttonlist_.push_back(iteminfo);
140                 obj->u_vdata = iteminfo.get();
141         }
142
143 }
144
145
146 void Menubar::Pimpl::update()
147 {
148         // nothing yet
149 }
150
151
152 void Menubar::Pimpl::openByName(string const & name)
153 {
154         for (ButtonList::const_iterator cit = buttonlist_.begin();
155              cit != buttonlist_.end(); ++cit) {
156                 if ((*cit)->item_->submenuname() == name) {
157                         MenuCallback((*cit)->obj_, 1);
158                         return;
159                 }
160         }
161
162         lyxerr << "Menubar::Pimpl::openByName: menu "
163                << name << " not found" << endl;
164 }
165
166
167 namespace {
168
169 inline
170 string const limit_string_length(string const & str)
171 {
172         string::size_type const max_item_length = 45;
173
174         if (str.size() > max_item_length)
175                 return str.substr(0, max_item_length - 3) + "...";
176         else
177                 return str;
178 }
179
180
181 int get_new_submenu(vector<int> & smn, Window win)
182 {
183         static size_type max_number_of_menus = 32;
184         if (smn.size() >= max_number_of_menus)
185                 max_number_of_menus =
186                     fl_setpup_maxpup(static_cast<int>(2*smn.size()));
187         int menu = fl_newpup(win);
188         smn.push_back(menu);
189         return menu;
190 }
191
192
193 size_type const max_number_of_items = 25;
194
195 inline
196 string const fixlabel(string const & str)
197 {
198 #if FL_VERSION < 1 && FL_REVISION < 89
199         return subst(str, '%', '?');
200 #else
201         return subst(str, "%", "%%");
202 #endif
203 }
204
205
206
207 void add_toc2(int menu, string const & extra_label,
208               vector<int> & smn, Window win,
209               toc::Toc const & toc_list,
210               size_type from, size_type to, int depth)
211 {
212         int shortcut_count = 0;
213         if (to - from <= max_number_of_items) {
214                 for (size_type i = from; i < to; ++i) {
215                         int const action = toc_list[i].action();
216                         string label(4 * max(0, toc_list[i].depth - depth),' ');
217                         label += fixlabel(toc_list[i].str);
218                         label = limit_string_length(label);
219                         label += "%x" + tostr(action + action_offset);
220                         if (i == to - 1 && depth == 0)
221                                 label += extra_label;
222                         if (toc_list[i].depth == depth
223                             && ++shortcut_count <= 9) {
224                                 label += "%h";
225                                 fl_addtopup(menu, label.c_str(),
226                                             tostr(shortcut_count).c_str());
227                         } else
228                                 fl_addtopup(menu, label.c_str());
229                 }
230         } else {
231                 size_type pos = from;
232                 size_type count = 0;
233                 while (pos < to) {
234                         ++count;
235                         if (count > max_number_of_items) {
236                                 int menu2 = get_new_submenu(smn, win);
237                                 add_toc2(menu2, extra_label, smn, win,
238                                          toc_list, pos, to, depth);
239                                 string label = _("More");
240                                 label += "...%m";
241                                 if (depth == 0)
242                                         label += extra_label;
243                                 fl_addtopup(menu, label.c_str(), menu2);
244                                 break;
245                         }
246                         size_type new_pos = pos+1;
247                         while (new_pos < to &&
248                                toc_list[new_pos].depth > depth)
249                                 ++new_pos;
250
251                         int const action = toc_list[pos].action();
252                         string label(4 * max(0, toc_list[pos].depth - depth), ' ');
253                         label += fixlabel(toc_list[pos].str);
254                         label = limit_string_length(label);
255                         if (new_pos == to && depth == 0)
256                                 label += extra_label;
257                         string shortcut;
258                         if (toc_list[pos].depth == depth &&
259                             ++shortcut_count <= 9)
260                                 shortcut = tostr(shortcut_count);
261
262                         if (new_pos == pos + 1) {
263                                 label += "%x" + tostr(action + action_offset);
264                                 if (!shortcut.empty()) {
265                                         label += "%h";
266                                         fl_addtopup(menu, label.c_str(),
267                                                     shortcut.c_str());
268                                 } else
269                                         fl_addtopup(menu, label.c_str());
270                         } else {
271                                 int menu2 = get_new_submenu(smn, win);
272                                 add_toc2(menu2, extra_label, smn, win,
273                                          toc_list, pos, new_pos, depth+1);
274                                 label += "%m";
275                                 if (!shortcut.empty()) {
276                                         label += "%h";
277                                         fl_addtopup(menu, label.c_str(), menu2,
278                                                     shortcut.c_str());
279                                 } else
280                                         fl_addtopup(menu, label.c_str(), menu2);
281                         }
282                         pos = new_pos;
283                 }
284         }
285 }
286
287 } // namespace anon
288
289
290 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
291                              vector<int> & smn, Window win)
292 {
293         if (!owner_->buffer())
294                 return;
295         toc::TocList toc_list = toc::getTocList(owner_->buffer());
296         toc::TocList::const_iterator cit = toc_list.begin();
297         toc::TocList::const_iterator end = toc_list.end();
298         for (; cit != end; ++cit) {
299                 // Handle this elsewhere
300                 if (cit->first == "TOC") continue;
301
302                 // All the rest is for floats
303                 int menu_first_sub = get_new_submenu(smn, win);
304                 int menu_current = menu_first_sub;
305                 toc::Toc::const_iterator ccit = cit->second.begin();
306                 toc::Toc::const_iterator eend = cit->second.end();
307                 size_type count = 0;
308                 for (; ccit != eend; ++ccit) {
309                         ++count;
310                         if (count > max_number_of_items) {
311                                 int menu_tmp = get_new_submenu(smn, win);
312                                 string label = _("More");
313                                 label += "...%m";
314                                 fl_addtopup(menu_current, label.c_str(), menu_tmp);
315                                 count = 1;
316                                 menu_current = menu_tmp;
317                         }
318                         int const action = ccit->action();
319                         string label = fixlabel(ccit->str);
320                         label = limit_string_length(label);
321                         label += "%x" + tostr(action + action_offset);
322                         fl_addtopup(menu_current, label.c_str());
323                 }
324                 string const m = floatList[cit->first]->second.name() + "%m";
325                 fl_addtopup(menu, m.c_str(), menu_first_sub);
326         }
327
328
329         // Handle normal TOC
330         cit = toc_list.find("TOC");
331         if (cit == end) {
332                 string const tmp = _("No Table of contents%i") + extra_label;
333                 fl_addtopup(menu, tmp.c_str());
334                 return;
335         } else {
336                 add_toc2(menu, extra_label, smn, win,
337                          cit->second, 0, cit->second.size(), 0);
338         }
339 }
340
341
342 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
343                                    Menu const & menu, vector<int> & smn)
344 {
345         int const menuid = get_new_submenu(smn, win);
346         fl_setpup_softedge(menuid, true);
347         fl_setpup_bw(menuid, -1);
348         lyxerr[Debug::GUI] << "Adding menu " << menuid
349                            << " in deletion list" << endl;
350
351         // Compute the size of the largest label (because xforms is
352         // not able to support shortcuts correctly...)
353         int max_width = 0;
354         string widest_label;
355         Menu::const_iterator end = menu.end();
356         for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
357                 MenuItem const & item = (*i);
358                 if (item.kind() == MenuItem::Command) {
359                         string const label = item.label() + '\t';
360                         int const width = string_width(label);
361                         if (width > max_width) {
362                                 max_width = width;
363                                 widest_label = label;
364                         }
365                 }
366         }
367         lyxerr[Debug::GUI] << "max_width=" << max_width
368                            << ", widest_label=`" << widest_label
369                            << "'" << endl;
370
371         // Compute where to put separators
372         vector<string> extra_labels(menu.size());
373         vector<string>::iterator it = extra_labels.begin();
374         vector<string>::iterator last = it;
375         for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it)
376                 if (i->kind() == MenuItem::Separator)
377                         *last = "%l";
378                 else if (!i->optional() ||
379                          !(view->getLyXFunc()->getStatus(i->action()).disabled()))
380                         last = it;
381
382         it = extra_labels.begin();
383         for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it) {
384                 MenuItem const & item = (*i);
385                 string & extra_label = *it;
386
387                 switch (item.kind()) {
388                 case MenuItem::Command: {
389                         FuncStatus const flag =
390                                 view->getLyXFunc()->getStatus(item.action());
391                         // handle optional entries.
392                         if (item.optional()
393                             && (flag.disabled())) {
394                                 lyxerr[Debug::GUI]
395                                         << "Skipping optional item "
396                                         << item.label() << endl;
397                                 break;
398                         }
399
400                         // Get the keys bound to this action, but keep only the
401                         // first one later
402                         string const accel = toplevel_keymap->findbinding(kb_action(item.action()));
403                         // Build the menu label from all the info
404                         string label = item.label();
405
406                         if (!accel.empty()) {
407                                 // Try to be clever and add  just enough
408                                 // tabs to align shortcuts.
409                                 do
410                                         label += '\t';
411                                 while (string_width(label) < max_width + 5);
412                                 label += accel.substr(1,accel.find(']') - 1);
413                         }
414                         label += "%x" + tostr(item.action() + action_offset)
415                                 + extra_label;
416
417                         // Modify the entry using the function status
418                         string pupmode;
419                         if (flag.onoff(true))
420                                 pupmode += "%B";
421                         if (flag.onoff(false))
422                                 pupmode += "%b";
423                         if (flag.disabled() || flag.unknown())
424                                 pupmode += "%i";
425                         label += pupmode;
426
427                         // Finally the menu shortcut
428                         string shortcut = item.shortcut();
429
430                         if (!shortcut.empty()) {
431                                 shortcut += lowercase(shortcut[0]);
432                                 label += "%h";
433                                 fl_addtopup(menuid, label.c_str(),
434                                             shortcut.c_str());
435                         } else
436                                 fl_addtopup(menuid, label.c_str());
437
438                         lyxerr[Debug::GUI] << "Command: \""
439                                            << lyxaction.getActionName(item.action())
440                                            << "\", binding \"" << accel
441                                            << "\", shortcut \"" << shortcut
442                                            << "\"" << endl;
443                         break;
444                 }
445
446                 case MenuItem::Submenu: {
447                         int submenuid = create_submenu(win, view,
448                                                      item.submenu(), smn);
449                         if (submenuid == -1)
450                                 return -1;
451                         string label = item.label();
452                         label += extra_label + "%m";
453                         string shortcut = item.shortcut();
454                         if (!shortcut.empty()) {
455                                 shortcut += lowercase(shortcut[0]);
456                                 label += "%h";
457                                 fl_addtopup(menuid, label.c_str(),
458                                             submenuid, shortcut.c_str());
459                         } else {
460                                 fl_addtopup(menuid, label.c_str(), submenuid);
461                         }
462                         break;
463                 }
464
465                 case MenuItem::Separator:
466                         // already done, and if it was the first one,
467                         // we just ignore it.
468                         break;
469
470                 case MenuItem::Toc:
471                         add_toc(menuid, extra_label, smn, win);
472                         break;
473
474                 case MenuItem::Documents:
475                 case MenuItem::Lastfiles:
476                 case MenuItem::ViewFormats:
477                 case MenuItem::UpdateFormats:
478                 case MenuItem::ExportFormats:
479                 case MenuItem::ImportFormats:
480                 case MenuItem::FloatListInsert:
481                 case MenuItem::FloatInsert:
482                         lyxerr << "Menubar::Pimpl::create_submenu: "
483                                 "this should not happen" << endl;
484                         break;
485
486                 }
487         }
488         return menuid;
489 }
490
491
492 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
493 {
494         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
495 //      lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
496 //             << "Val=(pimpl_=" << iteminfo->pimpl_
497 //             << ", item_=" << iteminfo->item_
498 //             << ", obj_=" << iteminfo->obj_ << ")" <<endl;
499
500         XFormsView * view = iteminfo->pimpl_->owner_;
501         MenuItem const * item = iteminfo->item_.get();
502
503         if (button == 1) {
504                 // set the pseudo menu-button
505                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
506                 fl_set_button(ob, 0);
507                 fl_redraw_object(ob);
508         }
509
510         // Paranoia check
511         lyx::Assert(item->kind() == MenuItem::Submenu);
512
513         // set tabstop length
514         fl_set_tabstop(menu_tabstop);
515
516         MenuBackend const * menubackend_ = iteminfo->pimpl_->menubackend_;
517         Menu tomenu;
518         Menu const frommenu = menubackend_->getMenu(item->submenuname());
519         menubackend_->expand(frommenu, tomenu, view->buffer());
520         vector<int> submenus;
521         int menu = iteminfo->pimpl_->
522                 create_submenu(FL_ObjWin(ob), view, tomenu, submenus);
523         if (menu != -1) {
524                 // place popup
525                 fl_setpup_position(view->getForm()->x + ob->x,
526                                    view->getForm()->y + ob->y + ob->h + 10);
527                 int choice = fl_dopup(menu);
528
529                 if (button == 1) {
530                                 // set the pseudo menu-button back
531                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
532                         fl_redraw_object(ob);
533                 }
534
535                 // If the action value is too low, then it is not a
536                 // valid action, but something else.
537                 if (choice >= action_offset + 1) {
538                         view->getLyXFunc()->dispatch(choice - action_offset, true);
539                 } else {
540                         lyxerr[Debug::GUI]
541                                 << "MenuCallback: ignoring bogus action "
542                                 << choice << endl;
543                 }
544         } else {
545                 lyxerr << "Error in MenuCallback" << endl;
546         }
547
548         for_each(submenus.begin(), submenus.end(), fl_freepup);
549         // restore tabstop length
550         fl_set_tabstop(default_tabstop);
551
552 }