2 * \file Menubar_pimpl.C
3 * Copyright 1999-2001 The LyX Team.
4 * See the file COPYING.
6 * \author Lars Gullik Bjønnes, larsbj@lyx.org
12 #pragma implementation
15 #include "Menubar_pimpl.h"
16 #include "MenuBackend.h"
17 #include "LyXAction.h"
21 #include "XFormsView.h"
23 #include "FloatList.h"
24 #include "support/lstrings.h"
25 #include "support/LAssert.h"
29 #include <boost/scoped_ptr.hpp>
39 typedef vector<int>::size_type size_type;
41 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
42 extern LyXAction lyxaction;
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
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
62 int const action_offset = 1000;
64 // This is used a few times below.
66 int string_width(string const & str)
68 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
70 static_cast<int>(str.length()));
78 //Defined later, used in makeMenubar().
80 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
82 Menubar::Pimpl::MenuCallback(ob, button);
88 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
89 : owner_(view), menubackend_(&mb), current_group_(0)
91 for (MenuBackend::const_iterator menu = menubackend_->begin();
92 menu != menubackend_->end() ; ++menu) {
93 if (menu->menubar()) {
94 FL_OBJECT * group = fl_bgn_group();
97 fl_hide_object(group);
99 << "Menubar::Pimpl::Pimpl: "
100 << "creating and hiding group " << group
101 << " for menubar " << menu->name() << endl;
102 menubarmap_[menu->name()] = group;
108 void Menubar::Pimpl::makeMenubar(Menu const & menu)
110 FL_FORM * form = static_cast<XFormsView *>(owner_)->getForm();
113 // Create menu frame if there is non yet.
114 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
115 form->w, mheight, "");
116 fl_set_object_resize(frame, FL_RESIZE_ALL);
117 fl_set_object_gravity(frame, NorthWestGravity,
120 Menu::const_iterator i = menu.begin();
121 Menu::const_iterator end = menu.end();
122 for (; i != end; ++i) {
124 if (i->kind() != MenuItem::Submenu) {
125 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
126 " only submenus can appear in a menubar"
130 string const label = i->label();
131 string const shortcut = "#" + i->shortcut();
132 int const width = string_width(label);
133 obj = fl_add_button(FL_MENU_BUTTON,
138 fl_set_object_boxtype(obj, FL_FLAT_BOX);
139 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
140 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
141 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
142 fl_set_object_resize(obj, FL_RESIZE_ALL);
143 fl_set_object_gravity(obj, NorthWestGravity,
145 moffset += obj->w + air;
146 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
147 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
149 boost::shared_ptr<ItemInfo>
150 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
151 buttonlist_.push_back(iteminfo);
152 obj->u_vdata = iteminfo.get();
158 void Menubar::Pimpl::set(string const & menu_name)
160 if (menu_name == current_menu_name_)
163 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
165 if (mbit == menubarmap_.end()) {
166 lyxerr << "ERROR:set: Unknown menu `" << menu_name
171 if (current_group_) {
172 lyxerr[Debug::GUI] << " hiding group "
173 << current_group_ << endl;
174 fl_hide_object(current_group_);
177 lyxerr[Debug::GUI] << " showing group "
178 << mbit->second << endl;
179 fl_show_object(mbit->second);
180 current_menu_name_ = menu_name;
181 current_group_ = mbit->second;
182 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
187 void Menubar::Pimpl::openByName(string const & name)
189 if (menubackend_->getMenu(current_menu_name_).hasSubmenu(name)) {
190 for (ButtonList::const_iterator cit = buttonlist_.begin();
191 cit != buttonlist_.end(); ++cit) {
192 if ((*cit)->item_->submenu() == name) {
193 MenuCallback((*cit)->obj_, 1);
198 lyxerr << "Menubar::Pimpl::openByName: menu "
199 << name << " not found" << endl;
206 string const limit_string_length(string const & str)
208 string::size_type const max_item_length = 45;
210 if (str.size() > max_item_length)
211 return str.substr(0, max_item_length - 3) + "...";
217 int get_new_submenu(vector<int> & smn, Window win)
219 static size_type max_number_of_menus = 32;
220 if (smn.size() >= max_number_of_menus)
221 max_number_of_menus =
222 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
223 int menu = fl_newpup(win);
229 size_type const max_number_of_items = 25;
232 string const fixlabel(string const & str)
234 #if FL_VERSION < 1 && FL_REVISION < 89
235 return subst(str, '%', '?');
237 return subst(str, "%", "%%");
243 void add_toc2(int menu, string const & extra_label,
244 vector<int> & smn, Window win,
245 vector<Buffer::TocItem> const & toc_list,
246 size_type from, size_type to, int depth)
248 int shortcut_count = 0;
249 if (to - from <= max_number_of_items) {
250 for (size_type i = from; i < to; ++i) {
251 int const action = lyxaction.
252 getPseudoAction(LFUN_GOTO_PARAGRAPH,
253 tostr(toc_list[i].par->id()));
254 string label(4 * max(0, toc_list[i].depth - depth),' ');
255 label += fixlabel(toc_list[i].str);
256 label = limit_string_length(label);
257 label += "%x" + tostr(action + action_offset);
258 if (i == to - 1 && depth == 0)
259 label += extra_label;
260 if (toc_list[i].depth == depth
261 && ++shortcut_count <= 9) {
263 fl_addtopup(menu, label.c_str(),
264 tostr(shortcut_count).c_str());
266 fl_addtopup(menu, label.c_str());
269 size_type pos = from;
273 if (count > max_number_of_items) {
274 int menu2 = get_new_submenu(smn, win);
275 add_toc2(menu2, extra_label, smn, win,
276 toc_list, pos, to, depth);
277 string label = _("More");
280 label += extra_label;
281 fl_addtopup(menu, label.c_str(), menu2);
284 size_type new_pos = pos+1;
285 while (new_pos < to &&
286 toc_list[new_pos].depth > depth)
289 int const action = lyxaction.
290 getPseudoAction(LFUN_GOTO_PARAGRAPH,
291 tostr(toc_list[pos].par->id()));
292 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
293 label += fixlabel(toc_list[pos].str);
294 label = limit_string_length(label);
295 if (new_pos == to && depth == 0)
296 label += extra_label;
298 if (toc_list[pos].depth == depth &&
299 ++shortcut_count <= 9)
300 shortcut = tostr(shortcut_count);
302 if (new_pos == pos + 1) {
303 label += "%x" + tostr(action + action_offset);
304 if (!shortcut.empty()) {
306 fl_addtopup(menu, label.c_str(),
309 fl_addtopup(menu, label.c_str());
311 int menu2 = get_new_submenu(smn, win);
312 add_toc2(menu2, extra_label, smn, win,
313 toc_list, pos, new_pos, depth+1);
315 if (!shortcut.empty()) {
317 fl_addtopup(menu, label.c_str(), menu2,
320 fl_addtopup(menu, label.c_str(), menu2);
330 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
331 vector<int> & smn, Window win)
333 Buffer::Lists toc_list = owner_->buffer()->getLists();
334 Buffer::Lists::const_iterator cit = toc_list.begin();
335 Buffer::Lists::const_iterator end = toc_list.end();
336 for (; cit != end; ++cit) {
337 // Handle this elsewhere
338 if (cit->first == "TOC") continue;
340 // All the rest is for floats
341 int menu_first_sub = get_new_submenu(smn, win);
342 int menu_current = menu_first_sub;
343 Buffer::SingleList::const_iterator ccit = cit->second.begin();
344 Buffer::SingleList::const_iterator eend = cit->second.end();
346 for (; ccit != eend; ++ccit) {
348 if (count > max_number_of_items) {
349 int menu_tmp = get_new_submenu(smn, win);
350 string label = _("More");
352 fl_addtopup(menu_current, label.c_str(), menu_tmp);
354 menu_current = menu_tmp;
358 .getPseudoAction(LFUN_GOTO_PARAGRAPH,
359 tostr(ccit->par->id()));
360 string label = fixlabel(ccit->str);
361 label = limit_string_length(label);
362 label += "%x" + tostr(action + action_offset);
363 fl_addtopup(menu_current, label.c_str());
365 string const m = floatList[cit->first]->second.name() + "%m";
366 fl_addtopup(menu, m.c_str(), menu_first_sub);
371 cit = toc_list.find("TOC");
373 string const tmp = _("No Table of contents%i") + extra_label;
374 fl_addtopup(menu, tmp.c_str());
377 add_toc2(menu, extra_label, smn, win,
378 cit->second, 0, cit->second.size(), 0);
383 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
384 string const & menu_name,
387 if (!menubackend_->hasMenu(menu_name)) {
388 lyxerr << "ERROR:create_submenu: Unknown menu `"
389 << menu_name << "'" << endl;
393 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
395 int const menu = get_new_submenu(smn, win);
396 fl_setpup_softedge(menu, true);
397 fl_setpup_bw(menu, -1);
398 lyxerr[Debug::GUI] << "Adding menu " << menu
399 << " in deletion list" << endl;
401 // Compute the size of the largest label (because xforms is
402 // not able to support shortcuts correctly...)
405 Menu::const_iterator end = md.end();
406 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
407 MenuItem const & item = (*i);
408 if (item.kind() == MenuItem::Command) {
409 string const label = item.label() + '\t';
410 int const width = string_width(label);
411 if (width > max_width) {
413 widest_label = label;
417 lyxerr[Debug::GUI] << "max_width=" << max_width
418 << ", widest_label=`" << widest_label
421 // Compute where to put separators
422 vector<string> extra_labels(md.size());
423 vector<string>::iterator it = extra_labels.begin();
424 vector<string>::iterator last = it;
425 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
426 if (i->kind() == MenuItem::Separator)
428 else if (!i->optional() ||
429 !(view->getLyXFunc()->getStatus(i->action()).disabled()))
432 it = extra_labels.begin();
433 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
434 MenuItem const & item = (*i);
435 string & extra_label = *it;
437 switch (item.kind()) {
438 case MenuItem::Command: {
439 FuncStatus const flag =
440 view->getLyXFunc()->getStatus(item.action());
441 // handle optional entries.
443 && (flag.disabled())) {
445 << "Skipping optional item "
446 << item.label() << endl;
450 // Get the keys bound to this action, but keep only the
452 string const accel = toplevel_keymap->findbinding(kb_action(item.action()));
453 // Build the menu label from all the info
454 string label = item.label();
456 if (!accel.empty()) {
457 // Try to be clever and add just enough
458 // tabs to align shortcuts.
461 while (string_width(label) < max_width + 5);
462 label += accel.substr(1,accel.find(']') - 1);
464 label += "%x" + tostr(item.action() + action_offset)
467 // Modify the entry using the function status
469 if (flag.onoff(true))
471 if (flag.onoff(false))
473 if (flag.disabled() || flag.unknown())
477 // Finally the menu shortcut
478 string shortcut = item.shortcut();
480 if (!shortcut.empty()) {
481 shortcut += lowercase(shortcut[0]);
483 fl_addtopup(menu, label.c_str(),
486 fl_addtopup(menu, label.c_str());
488 lyxerr[Debug::GUI] << "Command: \""
489 << lyxaction.getActionName(item.action())
490 << "\", binding \"" << accel
491 << "\", shortcut \"" << shortcut
496 case MenuItem::Submenu: {
497 int submenu = create_submenu(win, view,
498 item.submenu(), smn);
501 string label = item.label();
502 label += extra_label + "%m";
503 string shortcut = item.shortcut();
504 if (!shortcut.empty()) {
505 shortcut += lowercase(shortcut[0]);
507 fl_addtopup(menu, label.c_str(),
508 submenu, shortcut.c_str());
510 fl_addtopup(menu, label.c_str(), submenu);
515 case MenuItem::Separator:
516 // already done, and if it was the first one,
517 // we just ignore it.
521 add_toc(menu, extra_label, smn, win);
524 case MenuItem::Documents:
525 case MenuItem::Lastfiles:
526 case MenuItem::ViewFormats:
527 case MenuItem::UpdateFormats:
528 case MenuItem::ExportFormats:
529 case MenuItem::ImportFormats:
530 case MenuItem::FloatListInsert:
531 case MenuItem::FloatInsert:
532 lyxerr << "Menubar::Pimpl::create_submenu: "
533 "this should not happen" << endl;
542 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
544 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
545 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
546 // << "Val=(pimpl_=" << iteminfo->pimpl_
547 // << ", item_=" << iteminfo->item_
548 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
550 LyXView * view = iteminfo->pimpl_->owner_;
551 MenuItem const * item = iteminfo->item_.get();
554 // set the pseudo menu-button
555 fl_set_object_boxtype(ob, FL_DOWN_BOX);
556 fl_set_button(ob, 0);
557 fl_redraw_object(ob);
561 lyx::Assert(item->kind() == MenuItem::Submenu);
563 // set tabstop length
564 fl_set_tabstop(menu_tabstop);
565 vector<int> submenus;
566 int menu = iteminfo->pimpl_->
567 create_submenu(FL_ObjWin(ob), view,
568 item->submenu(), submenus);
571 fl_setpup_position(static_cast<XFormsView *>(view)->getForm()->x + ob->x,
572 static_cast<XFormsView *>(view)->getForm()->y + ob->y + ob->h + 10);
573 int choice = fl_dopup(menu);
576 // set the pseudo menu-button back
577 fl_set_object_boxtype(ob, FL_FLAT_BOX);
578 fl_redraw_object(ob);
581 // If the action value is too low, then it is not a
582 // valid action, but something else.
583 if (choice >= action_offset + 1) {
584 view->getLyXFunc()->verboseDispatch(choice - action_offset, true);
587 << "MenuCallback: ignoring bogus action "
591 lyxerr << "Error in MenuCallback" << endl;
594 for_each(submenus.begin(), submenus.end(), fl_freepup);
595 // restore tabstop length
596 fl_set_tabstop(default_tabstop);