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);
374 #warning Fix Me! (Lgb)
376 Buffer::Lists toc_list = owner_->buffer()->getLists();
377 Buffer::Lists::const_iterator cit = toc_list.begin();
378 Buffer::Lists::const_iterator end = toc_list.end();
379 for (; cit != end; ++cit) {
380 // Handle this elsewhere
381 if (cit->first == "TOC") continue;
383 int menu2 = get_new_submenu(smn, win);
384 Buffer::SingleList::const_iterator ccit = cit->second.begin();
385 Buffer::SingleList::const_iterator eend = cit->second.end();
386 for (; ccit != eend; ++ccit) {
389 .getPseudoAction(LFUN_GOTO_PARAGRAPH,
390 tostr(ccit->par->id()));
391 string label = fixlabel(ccit->str);
392 label = limit_string_length(label);
393 label += "%x" + tostr(action + action_offset);
394 fl_addtopup(menu2, label.c_str());
396 string const m = cit->first + "%m";
397 fl_addtopup(menu, m.c_str(), menu2);
402 cit = toc_list.find("TOC");
404 string const tmp = _("No Table of contents%i") + extra_label;
405 fl_addtopup(menu, tmp.c_str());
408 add_toc2(menu, extra_label, smn, win,
409 cit->second, 0, cit->second.size(), 0);
415 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
416 string const & menu_name,
419 if (!menubackend_->hasMenu(menu_name)){
420 lyxerr << "ERROR:create_submenu: Unknown menu `"
421 << menu_name << "'" << endl;
425 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
427 int menu = get_new_submenu(smn, win);
428 fl_setpup_softedge(menu, true);
429 fl_setpup_bw(menu, -1);
430 lyxerr[Debug::GUI] << "Adding menu " << menu
431 << " in deletion list" << endl;
433 // Compute the size of the largest label (because xforms is
434 // not able to support shortcuts correctly...)
437 Menu::const_iterator end = md.end();
438 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
439 MenuItem const & item = (*i);
440 if (item.kind() == MenuItem::Command) {
441 string label = item.label() + '\t';
442 int width = string_width(label);
443 if (width > max_width) {
445 widest_label = label;
449 lyxerr[Debug::GUI] << "max_width=" << max_width
450 << ", widest_label=`" << widest_label
453 // Compute where to put separators
454 vector<string> extra_labels(md.size());
455 vector<string>::iterator it = extra_labels.begin();
456 vector<string>::iterator last = it;
457 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
458 if (i->kind() == MenuItem::Separator)
460 else if (!i->optional() ||
461 !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
464 it = extra_labels.begin();
465 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
466 MenuItem const & item = (*i);
467 string & extra_label = *it;
469 switch (item.kind()) {
470 case MenuItem::Command: {
471 LyXFunc::func_status flag =
472 view->getLyXFunc()->getStatus(item.action());
474 // handle optional entries.
475 if (item.optional() && (flag & LyXFunc::Disabled)) {
477 << "Skipping optional item "
478 << item.label() << endl;
482 // Get the keys bound to this action, but keep only the
484 string const accel = toplevel_keymap->findbinding(item.action());
485 // Build the menu label from all the info
486 string label = item.label();
488 if (!accel.empty()) {
489 // Try to be clever and add just enough
490 // tabs to align shortcuts.
493 while (string_width(label) < max_width + 5);
494 label += accel.substr(1,accel.find(']') - 1);
496 label += "%x" + tostr(item.action() + action_offset)
499 // Modify the entry using the function status
501 if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
503 if (flag & LyXFunc::ToggleOn)
505 if (flag & LyXFunc::ToggleOff)
509 // Finally the menu shortcut
510 string shortcut = item.shortcut();
512 if (!shortcut.empty()) {
513 shortcut += lowercase(shortcut[0]);
515 fl_addtopup(menu, label.c_str(),
518 fl_addtopup(menu, label.c_str());
520 lyxerr[Debug::GUI] << "Command: \""
521 << lyxaction.getActionName(item.action())
522 << "\", binding \"" << accel
523 << "\", shortcut \"" << shortcut
528 case MenuItem::Submenu: {
529 int submenu = create_submenu(win, view,
530 item.submenu(), smn);
533 string label = item.label();
534 label += extra_label + "%m";
535 string shortcut = item.shortcut();
536 if (!shortcut.empty()) {
537 shortcut += lowercase(shortcut[0]);
539 fl_addtopup(menu, label.c_str(),
540 submenu, shortcut.c_str());
543 fl_addtopup(menu, label.c_str(), submenu);
548 case MenuItem::Separator:
549 // already done, and if it was the first one,
550 // we just ignore it.
554 add_toc(menu, extra_label, smn, win);
557 case MenuItem::Documents:
558 case MenuItem::Lastfiles:
559 case MenuItem::ViewFormats:
560 case MenuItem::UpdateFormats:
561 case MenuItem::ExportFormats:
562 case MenuItem::ImportFormats:
563 lyxerr << "Menubar::Pimpl::create_submenu: "
564 "this should not happen" << endl;
574 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
576 Menubar::Pimpl::MenuCallback(ob, button);
580 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
582 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
583 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
584 // << "Val=(pimpl_=" << iteminfo->pimpl_
585 // << ", item_=" << iteminfo->item_
586 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
588 LyXView * view = iteminfo->pimpl_->owner_;
589 MenuItem const * item = iteminfo->item_.get();
591 /* get the splash out of the way. It would be nicer
592 * to only have this code at the start, but xforms
593 * makes it too ugly to do
595 view->getDialogs()->destroySplash();
598 // set the pseudo menu-button
599 fl_set_object_boxtype(ob, FL_DOWN_BOX);
600 fl_set_button(ob, 0);
601 fl_redraw_object(ob);
605 lyx::Assert(item->kind() == MenuItem::Submenu);
607 // set tabstop length
608 fl_set_tabstop(menu_tabstop);
609 vector<int> submenus;
610 int menu = iteminfo->pimpl_->
611 create_submenu(FL_ObjWin(ob), view,
612 item->submenu(), submenus);
615 fl_setpup_position(view->getForm()->x + ob->x,
616 view->getForm()->y + ob->y + ob->h + 10);
617 int choice = fl_dopup(menu);
620 // set the pseudo menu-button back
621 fl_set_object_boxtype(ob, FL_FLAT_BOX);
622 fl_redraw_object(ob);
625 // If the action value is too low, then it is not a
626 // valid action, but something else.
627 if (choice >= action_offset + 1) {
628 view->getLyXFunc()->Dispatch(choice - action_offset);
632 << "MenuCallback: ignoring bogus action "
637 lyxerr << "Error in MenuCallback" << endl;
639 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
640 // restore tabstop length
641 fl_set_tabstop(default_tabstop);