1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1999 The LyX Team.
8 *======================================================*/
15 #pragma implementation
18 #include "support/lstrings.h"
19 #include "support/LAssert.h"
21 #include "LyXAction.h"
27 #include "MenuBackend.h"
28 #include "Menubar_pimpl.h"
35 typedef vector<int>::size_type size_type;
37 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
38 extern LyXAction lyxaction;
43 const int MENU_LABEL_SIZE = FL_NORMAL_SIZE;
44 const int mheight = 30;
45 const int mbheight= 22;
46 // where to place the menubar?
47 const int yloc = (mheight - mbheight)/2; //air + bw;
48 const int mbadd = 20; // menu button add (to width)
49 // Some space between buttons on the menubar
51 char const * menu_tabstop = "aa";
52 char const * default_tabstop = "aaaaaaaa";
53 // We do not want to mix position values in a menu (like the index of
54 // a submenu) with the action numbers which convey actual information.
55 // Therefore we offset all the action values by an arbitrary large
57 const int action_offset = 1000;
59 // This is used a few times below.
61 int string_width(string const & str)
63 return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
65 static_cast<int>(str.length()));
71 //Defined later, used in makeMenubar().
73 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
76 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
77 : owner_(view), menubackend_(&mb), current_group_(0)
79 for (MenuBackend::const_iterator menu = menubackend_->begin();
80 menu != menubackend_->end() ; ++menu) {
81 if (menu->menubar()) {
82 FL_OBJECT * group = fl_bgn_group();
85 fl_hide_object(group);
87 << "Menubar::Pimpl::Pimpl: "
88 << "creating and hiding group " << group
89 << " for menubar " << menu->name() << endl;
90 menubarmap_[menu->name()] = group;
96 void Menubar::Pimpl::makeMenubar(Menu const &menu)
98 FL_FORM * form = owner_->getForm();
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,
108 for (Menu::const_iterator i = menu.begin();
109 i != menu.end(); ++i) {
111 if (i->kind() != MenuItem::Submenu) {
112 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
113 " only submenus can appear in a menubar";
116 string label = i->label();
117 string shortcut = "#" + i->shortcut();
118 int width = string_width(label);
119 obj = fl_add_button(FL_MENU_BUTTON,
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, FL_NORMAL_STYLE);
128 fl_set_object_resize(obj, FL_RESIZE_ALL);
129 fl_set_object_gravity(obj, 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);
135 boost::shared_ptr<ItemInfo> iteminfo(new ItemInfo(this,
136 new MenuItem(*i), obj));
137 buttonlist_.push_back(iteminfo);
138 obj->u_vdata = iteminfo.get();
143 void Menubar::Pimpl::set(string const & menu_name)
145 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
146 << "for menu `" << menu_name << "'" << endl;
148 if (menu_name != current_menu_name_) {
149 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
151 if (mbit == menubarmap_.end()) {
152 lyxerr << "ERROR:set: Unknown menu `" << menu_name
157 if (current_group_) {
158 lyxerr[Debug::GUI] << " hiding group "
159 << current_group_ << endl;
160 fl_hide_object(current_group_);
163 lyxerr[Debug::GUI] << " showing group "
164 << mbit->second << endl;
165 fl_show_object(mbit->second);
166 current_menu_name_ = menu_name;
167 current_group_ = mbit->second;
168 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
172 lyxerr [Debug::GUI] << "Menubar::Pimpl::set: Nothing to do."
176 void Menubar::Pimpl::openByName(string const & name)
178 if (menubackend_->getMenu(current_menu_name_).hasSubmenu(name)) {
179 for (ButtonList::const_iterator cit = buttonlist_.begin();
180 cit != buttonlist_.end(); ++cit) {
181 if ((*cit)->item_->submenu() == name) {
182 MenuCallback((*cit)->obj_, 1);
187 lyxerr << "Menubar::Pimpl::openByName: menu "
188 << name << " not found" << endl;
195 string const limit_string_length(string const & str)
197 string::size_type const max_item_length = 45;
199 if (str.size() > max_item_length)
200 return str.substr(0, max_item_length - 3) + "...";
206 int get_new_submenu(vector<int> & smn, Window win)
208 static size_type max_number_of_menus = 32;
209 if (smn.size() >= max_number_of_menus)
210 max_number_of_menus =
211 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
212 int menu = fl_newpup(win);
218 size_type const max_number_of_items = 25;
221 string const fixlabel(string const & str)
224 return subst(str, '%', '?');
226 return subst(str, "%", "%%");
233 void add_toc2(int menu, string const & extra_label,
234 vector<int> & smn, Window win,
235 vector<Buffer::TocItem> const & toc_list,
236 size_type from, size_type to, int depth)
238 int shortcut_count = 0;
239 if (to - from <= max_number_of_items) {
240 for (size_type i = from; i < to; ++i) {
241 int const action = lyxaction.
242 getPseudoAction(LFUN_GOTO_PARAGRAPH,
243 tostr(toc_list[i].par->id()));
244 string label(4 * max(0, toc_list[i].depth - depth),' ');
245 label += fixlabel(toc_list[i].str);
246 label = limit_string_length(label);
247 label += "%x" + tostr(action + action_offset);
248 if (i == to - 1 && depth == 0)
249 label += extra_label;
250 if (toc_list[i].depth == depth
251 && ++shortcut_count <= 9) {
253 fl_addtopup(menu, label.c_str(),
254 tostr(shortcut_count).c_str());
256 fl_addtopup(menu, label.c_str());
259 size_type pos = from;
263 if (count > max_number_of_items) {
264 int menu2 = get_new_submenu(smn, win);
265 add_toc2(menu2, extra_label, smn, win,
266 toc_list, pos, to, depth);
267 string label = _("More");
270 label += extra_label;
271 fl_addtopup(menu, label.c_str(), menu2);
274 size_type new_pos = pos+1;
275 while (new_pos < to &&
276 toc_list[new_pos].depth > depth)
279 int const action = lyxaction.
280 getPseudoAction(LFUN_GOTO_PARAGRAPH,
281 tostr(toc_list[pos].par->id()));
282 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
283 label += fixlabel(toc_list[pos].str);
284 label = limit_string_length(label);
285 if (new_pos == to && depth == 0)
286 label += extra_label;
288 if (toc_list[pos].depth == depth &&
289 ++shortcut_count <= 9)
290 shortcut = tostr(shortcut_count);
292 if (new_pos == pos + 1) {
293 label += "%x" + tostr(action + action_offset);
294 if (!shortcut.empty()) {
296 fl_addtopup(menu, label.c_str(),
299 fl_addtopup(menu, label.c_str());
301 int menu2 = get_new_submenu(smn, win);
302 add_toc2(menu2, extra_label, smn, win,
303 toc_list, pos, new_pos, depth+1);
305 if (!shortcut.empty()) {
307 fl_addtopup(menu, label.c_str(), menu2,
310 fl_addtopup(menu, label.c_str(), menu2);
318 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
319 vector<int> & smn, Window win)
322 //xgettext:no-c-format
323 static char const * MenuNames[3] = { N_("List of Figures%m"),
324 //xgettext:no-c-format
325 N_("List of Tables%m"),
326 //xgettext:no-c-format
327 N_("List of Algorithms%m") };
329 vector<vector<Buffer::TocItem> > toc_list =
330 owner_->buffer()->getTocList();
332 // Handle LOF/LOT/LOA
333 int max_nonempty = 0;
334 for (int i = 1; i <= 3; ++i)
335 if (!toc_list[i].empty())
338 for (int j = 1; j <= 3; ++j)
339 if (!toc_list[j].empty()) {
340 int menu2 = get_new_submenu(smn, win);
341 for (size_type i = 0; i < toc_list[j].size(); ++i) {
342 if (i > max_number_of_items) {
343 fl_addtopup(menu2, ". . .%d");
346 int const action = lyxaction.
347 getPseudoAction(LFUN_GOTO_PARAGRAPH,
348 tostr(toc_list[j][i].par->id()));
349 string label = fixlabel(toc_list[j][i].str);
350 label = limit_string_length(label);
351 label += "%x" + tostr(action + action_offset);
352 fl_addtopup(menu2, label.c_str());
354 if (j == max_nonempty) {
355 string label = _(MenuNames[j - 1]);
357 fl_addtopup(menu, label.c_str(), menu2);
359 fl_addtopup(menu, _(MenuNames[j - 1]), menu2);
363 if (max_nonempty == 0 && toc_list[0].empty()) {
364 fl_addtopup(menu, (_("No Table of Contents%i")
365 + extra_label).c_str());
369 add_toc2(menu, extra_label, smn, win,
370 toc_list[0], 0, toc_list[0].size(), 0);
372 #warning Fix Me! (Lgb)
373 Buffer::Lists toc_list = owner_->buffer()->getLists();
374 Buffer::Lists::const_iterator cit = toc_list.begin();
375 Buffer::Lists::const_iterator end = toc_list.end();
376 for (; cit != end; ++cit) {
377 // Handle this elsewhere
378 if (cit->first == "TOC") continue;
380 int menu2 = get_new_submenu(smn, win);
381 Buffer::SingleList::const_iterator ccit = cit->second.begin();
382 Buffer::SingleList::const_iterator eend = cit->second.end();
383 for (; ccit != eend; ++ccit) {
386 .getPseudoAction(LFUN_GOTO_PARAGRAPH,
387 tostr(ccit->par->id()));
388 string label = fixlabel(ccit->str);
389 label = limit_string_length(label);
390 label += "%x" + tostr(action + action_offset);
391 fl_addtopup(menu2, label.c_str());
393 string const m = cit->first + "%m";
394 fl_addtopup(menu, m.c_str(), menu2);
399 cit = toc_list.find("TOC");
401 string const tmp = _("No Table of contents%i") + extra_label;
402 fl_addtopup(menu, tmp.c_str());
405 add_toc2(menu, extra_label, smn, win,
406 cit->second, 0, cit->second.size(), 0);
412 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
413 string const & menu_name,
416 if (!menubackend_->hasMenu(menu_name)){
417 lyxerr << "ERROR:create_submenu: Unknown menu `"
418 << menu_name << "'" << endl;
422 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
424 int menu = get_new_submenu(smn, win);
425 fl_setpup_softedge(menu, true);
426 fl_setpup_bw(menu, -1);
427 lyxerr[Debug::GUI] << "Adding menu " << menu
428 << " in deletion list" << endl;
430 // Compute the size of the largest label (because xforms is
431 // not able to support shortcuts correctly...)
434 Menu::const_iterator end = md.end();
435 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
436 MenuItem const & item = (*i);
437 if (item.kind() == MenuItem::Command) {
438 string label = item.label() + '\t';
439 int width = string_width(label);
440 if (width > max_width) {
442 widest_label = label;
446 lyxerr[Debug::GUI] << "max_width=" << max_width
447 << ", widest_label=`" << widest_label
450 // Compute where to put separators
451 vector<string> extra_labels(md.size());
452 vector<string>::iterator it = extra_labels.begin();
453 vector<string>::iterator last = it;
454 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
455 if (i->kind() == MenuItem::Separator)
457 else if (!i->optional() ||
458 !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
461 it = extra_labels.begin();
462 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
463 MenuItem const & item = (*i);
464 string & extra_label = *it;
466 switch (item.kind()) {
467 case MenuItem::Command: {
468 LyXFunc::func_status flag =
469 view->getLyXFunc()->getStatus(item.action());
471 // handle optional entries.
472 if (item.optional() && (flag & LyXFunc::Disabled)) {
474 << "Skipping optional item "
475 << item.label() << endl;
479 // Get the keys bound to this action, but keep only the
481 string const accel = toplevel_keymap->findbinding(item.action());
482 // Build the menu label from all the info
483 string label = item.label();
485 if (!accel.empty()) {
486 // Try to be clever and add just enough
487 // tabs to align shortcuts.
490 while (string_width(label) < max_width + 5);
491 label += accel.substr(1,accel.find(']') - 1);
493 label += "%x" + tostr(item.action() + action_offset)
496 // Modify the entry using the function status
498 if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
500 if (flag & LyXFunc::ToggleOn)
502 if (flag & LyXFunc::ToggleOff)
506 // Finally the menu shortcut
507 string shortcut = item.shortcut();
509 if (!shortcut.empty()) {
510 shortcut += lowercase(shortcut[0]);
512 fl_addtopup(menu, label.c_str(),
515 fl_addtopup(menu, label.c_str());
517 lyxerr[Debug::GUI] << "Command: \""
518 << lyxaction.getActionName(item.action())
519 << "\", binding \"" << accel
520 << "\", shortcut \"" << shortcut
525 case MenuItem::Submenu: {
526 int submenu = create_submenu(win, view,
527 item.submenu(), smn);
530 string label = item.label();
531 label += extra_label + "%m";
532 string shortcut = item.shortcut();
533 if (!shortcut.empty()) {
534 shortcut += lowercase(shortcut[0]);
536 fl_addtopup(menu, label.c_str(),
537 submenu, shortcut.c_str());
540 fl_addtopup(menu, label.c_str(), submenu);
545 case MenuItem::Separator:
546 // already done, and if it was the first one,
547 // we just ignore it.
551 add_toc(menu, extra_label, smn, win);
554 case MenuItem::Documents:
555 case MenuItem::Lastfiles:
556 case MenuItem::ViewFormats:
557 case MenuItem::UpdateFormats:
558 case MenuItem::ExportFormats:
559 case MenuItem::ImportFormats:
560 lyxerr << "Menubar::Pimpl::create_submenu: "
561 "this should not happen" << endl;
571 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
573 Menubar::Pimpl::MenuCallback(ob, button);
577 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
579 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
580 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
581 // << "Val=(pimpl_=" << iteminfo->pimpl_
582 // << ", item_=" << iteminfo->item_
583 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
585 LyXView * view = iteminfo->pimpl_->owner_;
586 MenuItem const * item = iteminfo->item_.get();
588 /* get the splash out of the way. It would be nicer
589 * to only have this code at the start, but xforms
590 * makes it too ugly to do
592 view->getDialogs()->destroySplash();
595 // set the pseudo menu-button
596 fl_set_object_boxtype(ob, FL_DOWN_BOX);
597 fl_set_button(ob, 0);
598 fl_redraw_object(ob);
602 Assert(item->kind() == MenuItem::Submenu);
604 // set tabstop length
605 fl_set_tabstop(menu_tabstop);
606 vector<int> submenus;
607 int menu = iteminfo->pimpl_->
608 create_submenu(FL_ObjWin(ob), view,
609 item->submenu(), submenus);
612 fl_setpup_position(view->getForm()->x + ob->x,
613 view->getForm()->y + ob->y + ob->h + 10);
614 int choice = fl_dopup(menu);
617 // set the pseudo menu-button back
618 fl_set_object_boxtype(ob, FL_FLAT_BOX);
619 fl_redraw_object(ob);
622 // If the action value is too low, then it is not a
623 // valid action, but something else.
624 if (choice >= action_offset + 1) {
625 view->getLyXFunc()->Dispatch(choice - action_offset);
629 << "MenuCallback: ignoring bogus action "
634 lyxerr << "Error in MenuCallback" << endl;
636 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
637 // restore tabstop length
638 fl_set_tabstop(default_tabstop);