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"
36 typedef vector<int>::size_type size_type;
38 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
39 extern LyXAction lyxaction;
44 const int MENU_LABEL_SIZE = FL_NORMAL_SIZE;
45 const int mheight = 30;
46 const int mbheight= 22;
47 // where to place the menubar?
48 const int yloc = (mheight - mbheight)/2; //air + bw;
49 const int mbadd = 20; // menu button add (to width)
50 // Some space between buttons on the menubar
52 char const * menu_tabstop = "aa";
53 char const * default_tabstop = "aaaaaaaa";
54 // We do not want to mix position values in a menu (like the index of
55 // a submenu) with the action numbers which convey actual information.
56 // Therefore we offset all the action values by an arbitrary large
58 const int action_offset = 1000;
60 // This is used a few times below.
62 int string_width(string const & str)
64 return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
66 static_cast<int>(str.length()));
72 //Defined later, used in makeMenubar().
74 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
77 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
78 : owner_(view), menubackend_(&mb), current_group_(0)
80 for (MenuBackend::const_iterator menu = menubackend_->begin();
81 menu != menubackend_->end() ; ++menu) {
82 if (menu->menubar()) {
83 FL_OBJECT * group = fl_bgn_group();
86 fl_hide_object(group);
88 << "Menubar::Pimpl::Pimpl: "
89 << "creating and hiding group " << group
90 << " for menubar " << menu->name() << endl;
91 menubarmap_[menu->name()] = group;
97 void Menubar::Pimpl::makeMenubar(Menu const &menu)
99 FL_FORM * form = owner_->getForm();
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,
109 for (Menu::const_iterator i = menu.begin();
110 i != menu.end(); ++i) {
112 if (i->kind() != MenuItem::Submenu) {
113 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
114 " only submenus can appear in a menubar";
117 string label = i->label();
118 string shortcut = "#" + i->shortcut();
119 int width = string_width(label);
120 obj = fl_add_button(FL_MENU_BUTTON,
125 fl_set_object_boxtype(obj, FL_FLAT_BOX);
126 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
127 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
128 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
129 fl_set_object_resize(obj, FL_RESIZE_ALL);
130 fl_set_object_gravity(obj, NorthWestGravity,
132 moffset += obj->w + air;
133 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
134 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
136 boost::shared_ptr<ItemInfo> iteminfo(new ItemInfo(this,
137 new MenuItem(*i), obj));
138 buttonlist_.push_back(iteminfo);
139 obj->u_vdata = iteminfo.get();
144 void Menubar::Pimpl::set(string const & menu_name)
146 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
147 << "for menu `" << menu_name << "'" << endl;
149 if (menu_name != current_menu_name_) {
150 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
152 if (mbit == menubarmap_.end()) {
153 lyxerr << "ERROR:set: Unknown menu `" << menu_name
158 if (current_group_) {
159 lyxerr[Debug::GUI] << " hiding group "
160 << current_group_ << endl;
161 fl_hide_object(current_group_);
164 lyxerr[Debug::GUI] << " showing group "
165 << mbit->second << endl;
166 fl_show_object(mbit->second);
167 current_menu_name_ = menu_name;
168 current_group_ = mbit->second;
169 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
173 lyxerr [Debug::GUI] << "Menubar::Pimpl::set: Nothing to do."
177 void Menubar::Pimpl::openByName(string const & name)
179 if (menubackend_->getMenu(current_menu_name_).hasSubmenu(name)) {
180 for (ButtonList::const_iterator cit = buttonlist_.begin();
181 cit != buttonlist_.end(); ++cit) {
182 if ((*cit)->item_->submenu() == name) {
183 MenuCallback((*cit)->obj_, 1);
188 lyxerr << "Menubar::Pimpl::openByName: menu "
189 << name << " not found" << endl;
196 string const limit_string_length(string const & str)
198 string::size_type const max_item_length = 45;
200 if (str.size() > max_item_length)
201 return str.substr(0, max_item_length - 3) + "...";
207 int get_new_submenu(vector<int> & smn, Window win)
209 static size_type max_number_of_menus = 32;
210 if (smn.size() >= max_number_of_menus)
211 max_number_of_menus =
212 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
213 int menu = fl_newpup(win);
219 size_type const max_number_of_items = 25;
222 string const fixlabel(string const & str)
225 return subst(str, '%', '?');
227 return subst(str, "%", "%%");
234 void add_toc2(int menu, string const & extra_label,
235 vector<int> & smn, Window win,
236 vector<Buffer::TocItem> const & toc_list,
237 size_type from, size_type to, int depth)
239 int shortcut_count = 0;
240 if (to - from <= max_number_of_items) {
241 for (size_type i = from; i < to; ++i) {
242 int const action = lyxaction.
243 getPseudoAction(LFUN_GOTO_PARAGRAPH,
244 tostr(toc_list[i].par->id()));
245 string label(4 * max(0, toc_list[i].depth - depth),' ');
246 label += fixlabel(toc_list[i].str);
247 label = limit_string_length(label);
248 label += "%x" + tostr(action + action_offset);
249 if (i == to - 1 && depth == 0)
250 label += extra_label;
251 if (toc_list[i].depth == depth
252 && ++shortcut_count <= 9) {
254 fl_addtopup(menu, label.c_str(),
255 tostr(shortcut_count).c_str());
257 fl_addtopup(menu, label.c_str());
260 size_type pos = from;
264 if (count > max_number_of_items) {
265 int menu2 = get_new_submenu(smn, win);
266 add_toc2(menu2, extra_label, smn, win,
267 toc_list, pos, to, depth);
268 string label = _("More");
271 label += extra_label;
272 fl_addtopup(menu, label.c_str(), menu2);
275 size_type new_pos = pos+1;
276 while (new_pos < to &&
277 toc_list[new_pos].depth > depth)
280 int const action = lyxaction.
281 getPseudoAction(LFUN_GOTO_PARAGRAPH,
282 tostr(toc_list[pos].par->id()));
283 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
284 label += fixlabel(toc_list[pos].str);
285 label = limit_string_length(label);
286 if (new_pos == to && depth == 0)
287 label += extra_label;
289 if (toc_list[pos].depth == depth &&
290 ++shortcut_count <= 9)
291 shortcut = tostr(shortcut_count);
293 if (new_pos == pos + 1) {
294 label += "%x" + tostr(action + action_offset);
295 if (!shortcut.empty()) {
297 fl_addtopup(menu, label.c_str(),
300 fl_addtopup(menu, label.c_str());
302 int menu2 = get_new_submenu(smn, win);
303 add_toc2(menu2, extra_label, smn, win,
304 toc_list, pos, new_pos, depth+1);
306 if (!shortcut.empty()) {
308 fl_addtopup(menu, label.c_str(), menu2,
311 fl_addtopup(menu, label.c_str(), menu2);
319 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
320 vector<int> & smn, Window win)
323 //xgettext:no-c-format
324 static char const * MenuNames[3] = { N_("List of Figures%m"),
325 //xgettext:no-c-format
326 N_("List of Tables%m"),
327 //xgettext:no-c-format
328 N_("List of Algorithms%m") };
330 vector<vector<Buffer::TocItem> > toc_list =
331 owner_->buffer()->getTocList();
333 // Handle LOF/LOT/LOA
334 int max_nonempty = 0;
335 for (int i = 1; i <= 3; ++i)
336 if (!toc_list[i].empty())
339 for (int j = 1; j <= 3; ++j)
340 if (!toc_list[j].empty()) {
341 int menu2 = get_new_submenu(smn, win);
342 for (size_type i = 0; i < toc_list[j].size(); ++i) {
343 if (i > max_number_of_items) {
344 fl_addtopup(menu2, ". . .%d");
347 int const action = lyxaction.
348 getPseudoAction(LFUN_GOTO_PARAGRAPH,
349 tostr(toc_list[j][i].par->id()));
350 string label = fixlabel(toc_list[j][i].str);
351 label = limit_string_length(label);
352 label += "%x" + tostr(action + action_offset);
353 fl_addtopup(menu2, label.c_str());
355 if (j == max_nonempty) {
356 string label = _(MenuNames[j - 1]);
358 fl_addtopup(menu, label.c_str(), menu2);
360 fl_addtopup(menu, _(MenuNames[j - 1]), menu2);
364 if (max_nonempty == 0 && toc_list[0].empty()) {
365 fl_addtopup(menu, (_("No Table of Contents%i")
366 + extra_label).c_str());
370 add_toc2(menu, extra_label, smn, win,
371 toc_list[0], 0, toc_list[0].size(), 0);
373 #warning Fix Me! (Lgb)
374 Buffer::Lists toc_list = owner_->buffer()->getLists();
375 Buffer::Lists::const_iterator cit = toc_list.begin();
376 Buffer::Lists::const_iterator end = toc_list.end();
377 for (; cit != end; ++cit) {
378 // Handle this elsewhere
379 if (cit->first == "TOC") continue;
381 int menu2 = get_new_submenu(smn, win);
382 Buffer::SingleList::const_iterator ccit = cit->second.begin();
383 Buffer::SingleList::const_iterator eend = cit->second.end();
384 for (; ccit != eend; ++ccit) {
387 .getPseudoAction(LFUN_GOTO_PARAGRAPH,
388 tostr(ccit->par->id()));
389 string label = fixlabel(ccit->str);
390 label = limit_string_length(label);
391 label += "%x" + tostr(action + action_offset);
392 fl_addtopup(menu2, label.c_str());
394 string const m = cit->first + "%m";
395 fl_addtopup(menu, m.c_str(), menu2);
400 cit = toc_list.find("TOC");
402 string const tmp = _("No Table of contents%i") + extra_label;
403 fl_addtopup(menu, tmp.c_str());
406 add_toc2(menu, extra_label, smn, win,
407 cit->second, 0, cit->second.size(), 0);
413 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
414 string const & menu_name,
417 if (!menubackend_->hasMenu(menu_name)){
418 lyxerr << "ERROR:create_submenu: Unknown menu `"
419 << menu_name << "'" << endl;
423 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
425 int menu = get_new_submenu(smn, win);
426 fl_setpup_softedge(menu, true);
427 fl_setpup_bw(menu, -1);
428 lyxerr[Debug::GUI] << "Adding menu " << menu
429 << " in deletion list" << endl;
431 // Compute the size of the largest label (because xforms is
432 // not able to support shortcuts correctly...)
435 Menu::const_iterator end = md.end();
436 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
437 MenuItem const & item = (*i);
438 if (item.kind() == MenuItem::Command) {
439 string label = item.label() + '\t';
440 int width = string_width(label);
441 if (width > max_width) {
443 widest_label = label;
447 lyxerr[Debug::GUI] << "max_width=" << max_width
448 << ", widest_label=`" << widest_label
451 // Compute where to put separators
452 vector<string> extra_labels(md.size());
453 vector<string>::iterator it = extra_labels.begin();
454 vector<string>::iterator last = it;
455 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
456 if (i->kind() == MenuItem::Separator)
458 else if (!i->optional() ||
459 !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
462 it = extra_labels.begin();
463 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
464 MenuItem const & item = (*i);
465 string & extra_label = *it;
467 switch (item.kind()) {
468 case MenuItem::Command: {
469 LyXFunc::func_status flag =
470 view->getLyXFunc()->getStatus(item.action());
472 // handle optional entries.
473 if (item.optional() && (flag & LyXFunc::Disabled)) {
475 << "Skipping optional item "
476 << item.label() << endl;
480 // Get the keys bound to this action, but keep only the
482 string const accel = toplevel_keymap->findbinding(item.action());
483 // Build the menu label from all the info
484 string label = item.label();
486 if (!accel.empty()) {
487 // Try to be clever and add just enough
488 // tabs to align shortcuts.
491 while (string_width(label) < max_width + 5);
492 label += accel.substr(1,accel.find(']') - 1);
494 label += "%x" + tostr(item.action() + action_offset)
497 // Modify the entry using the function status
499 if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
501 if (flag & LyXFunc::ToggleOn)
503 if (flag & LyXFunc::ToggleOff)
507 // Finally the menu shortcut
508 string shortcut = item.shortcut();
510 if (!shortcut.empty()) {
511 shortcut += lowercase(shortcut[0]);
513 fl_addtopup(menu, label.c_str(),
516 fl_addtopup(menu, label.c_str());
518 lyxerr[Debug::GUI] << "Command: \""
519 << lyxaction.getActionName(item.action())
520 << "\", binding \"" << accel
521 << "\", shortcut \"" << shortcut
526 case MenuItem::Submenu: {
527 int submenu = create_submenu(win, view,
528 item.submenu(), smn);
531 string label = item.label();
532 label += extra_label + "%m";
533 string shortcut = item.shortcut();
534 if (!shortcut.empty()) {
535 shortcut += lowercase(shortcut[0]);
537 fl_addtopup(menu, label.c_str(),
538 submenu, shortcut.c_str());
541 fl_addtopup(menu, label.c_str(), submenu);
546 case MenuItem::Separator:
547 // already done, and if it was the first one,
548 // we just ignore it.
552 add_toc(menu, extra_label, smn, win);
555 case MenuItem::Documents:
556 case MenuItem::Lastfiles:
557 case MenuItem::ViewFormats:
558 case MenuItem::UpdateFormats:
559 case MenuItem::ExportFormats:
560 case MenuItem::ImportFormats:
561 lyxerr << "Menubar::Pimpl::create_submenu: "
562 "this should not happen" << endl;
572 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
574 Menubar::Pimpl::MenuCallback(ob, button);
578 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
580 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
581 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
582 // << "Val=(pimpl_=" << iteminfo->pimpl_
583 // << ", item_=" << iteminfo->item_
584 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
586 LyXView * view = iteminfo->pimpl_->owner_;
587 MenuItem const * item = iteminfo->item_.get();
589 /* get the splash out of the way. It would be nicer
590 * to only have this code at the start, but xforms
591 * makes it too ugly to do
593 view->getDialogs()->destroySplash();
596 // set the pseudo menu-button
597 fl_set_object_boxtype(ob, FL_DOWN_BOX);
598 fl_set_button(ob, 0);
599 fl_redraw_object(ob);
603 Assert(item->kind() == MenuItem::Submenu);
605 // set tabstop length
606 fl_set_tabstop(menu_tabstop);
607 vector<int> submenus;
608 int menu = iteminfo->pimpl_->
609 create_submenu(FL_ObjWin(ob), view,
610 item->submenu(), submenus);
613 fl_setpup_position(view->getForm()->x + ob->x,
614 view->getForm()->y + ob->y + ob->h + 10);
615 int choice = fl_dopup(menu);
618 // set the pseudo menu-button back
619 fl_set_object_boxtype(ob, FL_FLAT_BOX);
620 fl_redraw_object(ob);
623 // If the action value is too low, then it is not a
624 // valid action, but something else.
625 if (choice >= action_offset + 1) {
626 view->getLyXFunc()->Dispatch(choice - action_offset);
630 << "MenuCallback: ignoring bogus action "
635 lyxerr << "Error in MenuCallback" << endl;
637 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
638 // restore tabstop length
639 fl_set_tabstop(default_tabstop);