1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1999 The LyX Team.
8 *======================================================*/
11 #pragma implementation
17 #include "support/lstrings.h"
18 #include "support/LAssert.h"
20 #include "LyXAction.h"
25 #include "MenuBackend.h"
26 #include "Menubar_pimpl.h"
33 typedef vector<int>::size_type size_type;
35 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
36 extern LyXAction lyxaction;
39 static const int MENU_LABEL_SIZE = FL_NORMAL_SIZE;
40 static const int mheight = 30;
41 static const int mbheight= 22;
42 // where to place the menubar?
43 static const int yloc = (mheight - mbheight)/2; //air + bw;
44 static const int mbadd = 20; // menu button add (to width)
45 // Some space between buttons on the menubar
46 static const int air = 2;
47 static char const * menu_tabstop = "aa";
48 static char const * default_tabstop = "aaaaaaaa";
49 // We do not want to mix position values in a menu (like the index of
50 // a submenu) with the action numbers which convey actual information.
51 // Therefore we offset all the action values by an arbitrary large
53 static const int action_offset = 1000;
56 //Defined later, used in makeMenubar().
58 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
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()));
70 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
71 : owner_(view), menubackend_(&mb), current_group_(0)
73 for (MenuBackend::const_iterator menu = menubackend_->begin();
74 menu != menubackend_->end() ; ++menu) {
75 if (menu->menubar()) {
76 FL_OBJECT * group = fl_bgn_group();
79 fl_hide_object(group);
81 << "Menubar::Pimpl::Pimpl: "
82 << "creating and hiding group " << group
83 << " for menubar " << menu->name() << endl;
84 menubarmap_[menu->name()] = group;
90 void Menubar::Pimpl::makeMenubar(Menu const &menu)
92 FL_FORM * form = owner_->getForm();
95 // Create menu frame if there is non yet.
96 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
97 form->w, mheight, "");
98 fl_set_object_resize(frame, FL_RESIZE_ALL);
99 fl_set_object_gravity(frame, NorthWestGravity,
102 for (Menu::const_iterator i = menu.begin();
103 i != menu.end(); ++i) {
105 if (i->kind() != MenuItem::Submenu) {
106 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
107 " only submenus can appear in a menubar";
110 string label = i->label();
111 string shortcut = "#" + i->shortcut();
112 int width = string_width(label);
113 obj = fl_add_button(FL_MENU_BUTTON,
118 fl_set_object_boxtype(obj, FL_FLAT_BOX);
119 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
120 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
121 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
122 fl_set_object_resize(obj, FL_RESIZE_ALL);
123 fl_set_object_gravity(obj, NorthWestGravity,
125 moffset += obj->w + air;
126 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
127 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
129 boost::shared_ptr<ItemInfo> iteminfo(new ItemInfo(this,
130 new MenuItem(*i), obj));
131 buttonlist_.push_back(iteminfo);
132 obj->u_vdata = iteminfo.get();
137 void Menubar::Pimpl::set(string const & menu_name)
139 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
140 << "for menu `" << menu_name << "'" << endl;
142 if (menu_name != current_menu_name_) {
143 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
145 if (mbit == menubarmap_.end()) {
146 lyxerr << "ERROR:set: Unknown menu `" << menu_name
151 if (current_group_) {
152 lyxerr[Debug::GUI] << " hiding group "
153 << current_group_ << endl;
154 fl_hide_object(current_group_);
157 lyxerr[Debug::GUI] << " showing group "
158 << mbit->second << endl;
159 fl_show_object(mbit->second);
160 current_menu_name_ = menu_name;
161 current_group_ = mbit->second;
162 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
166 lyxerr [Debug::GUI] << "Menubar::Pimpl::set: Nothing to do."
170 void Menubar::Pimpl::openByName(string const & name)
172 if (menubackend_->getMenu(current_menu_name_).hasSubmenu(name)) {
173 for (ButtonList::const_iterator cit = buttonlist_.begin();
174 cit != buttonlist_.end(); ++cit) {
175 if ((*cit)->item_->submenu() == name) {
176 MenuCallback((*cit)->obj_, 1);
181 lyxerr << "Menubar::Pimpl::openByName: menu "
182 << name << " not found" << endl;
187 string const limit_string_length(string const & str)
189 string::size_type const max_item_length = 45;
191 if (str.size() > max_item_length)
192 return str.substr(0, max_item_length - 3) + "...";
199 int get_new_submenu(vector<int> & smn, Window win)
201 static size_type max_number_of_menus = 32;
202 if (smn.size() >= max_number_of_menus)
203 max_number_of_menus =
204 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
205 int menu = fl_newpup(win);
211 size_type const max_number_of_items = 25;
214 string const fixlabel(string const & str)
217 return subst(str, '%', '?');
219 return subst(str, "%", "%%");
224 void add_toc2(int menu, string const & extra_label,
225 vector<int> & smn, Window win,
226 vector<Buffer::TocItem> const & toc_list,
227 size_type from, size_type to, int depth)
229 int shortcut_count = 0;
230 if (to - from <= max_number_of_items) {
231 for (size_type i = from; i < to; ++i) {
232 int const action = lyxaction.
233 getPseudoAction(LFUN_GOTO_PARAGRAPH,
234 tostr(toc_list[i].par->id()));
235 string label(4 * max(0, toc_list[i].depth - depth),' ');
236 label += fixlabel(toc_list[i].str);
237 label = limit_string_length(label);
238 label += "%x" + tostr(action + action_offset);
239 if (i == to - 1 && depth == 0)
240 label += extra_label;
241 if (toc_list[i].depth == depth
242 && ++shortcut_count <= 9) {
244 fl_addtopup(menu, label.c_str(),
245 tostr(shortcut_count).c_str());
247 fl_addtopup(menu, label.c_str());
250 size_type pos = from;
254 if (count > max_number_of_items) {
255 int menu2 = get_new_submenu(smn, win);
256 add_toc2(menu2, extra_label, smn, win,
257 toc_list, pos, to, depth);
258 string label = _("More");
261 label += extra_label;
262 fl_addtopup(menu, label.c_str(), menu2);
265 size_type new_pos = pos+1;
266 while (new_pos < to &&
267 toc_list[new_pos].depth > depth)
270 int const action = lyxaction.
271 getPseudoAction(LFUN_GOTO_PARAGRAPH,
272 tostr(toc_list[pos].par->id()));
273 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
274 label += fixlabel(toc_list[pos].str);
275 label = limit_string_length(label);
276 if (new_pos == to && depth == 0)
277 label += extra_label;
279 if (toc_list[pos].depth == depth &&
280 ++shortcut_count <= 9)
281 shortcut = tostr(shortcut_count);
283 if (new_pos == pos + 1) {
284 label += "%x" + tostr(action + action_offset);
285 if (!shortcut.empty()) {
287 fl_addtopup(menu, label.c_str(),
290 fl_addtopup(menu, label.c_str());
292 int menu2 = get_new_submenu(smn, win);
293 add_toc2(menu2, extra_label, smn, win,
294 toc_list, pos, new_pos, depth+1);
296 if (!shortcut.empty()) {
298 fl_addtopup(menu, label.c_str(), menu2,
301 fl_addtopup(menu, label.c_str(), menu2);
309 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
310 vector<int> & smn, Window win)
313 //xgettext:no-c-format
314 static char const * MenuNames[3] = { N_("List of Figures%m"),
315 //xgettext:no-c-format
316 N_("List of Tables%m"),
317 //xgettext:no-c-format
318 N_("List of Algorithms%m") };
320 vector<vector<Buffer::TocItem> > toc_list =
321 owner_->buffer()->getTocList();
323 // Handle LOF/LOT/LOA
324 int max_nonempty = 0;
325 for (int i = 1; i <= 3; ++i)
326 if (!toc_list[i].empty())
329 for (int j = 1; j <= 3; ++j)
330 if (!toc_list[j].empty()) {
331 int menu2 = get_new_submenu(smn, win);
332 for (size_type i = 0; i < toc_list[j].size(); ++i) {
333 if (i > max_number_of_items) {
334 fl_addtopup(menu2, ". . .%d");
337 int const action = lyxaction.
338 getPseudoAction(LFUN_GOTO_PARAGRAPH,
339 tostr(toc_list[j][i].par->id()));
340 string label = fixlabel(toc_list[j][i].str);
341 label = limit_string_length(label);
342 label += "%x" + tostr(action + action_offset);
343 fl_addtopup(menu2, label.c_str());
345 if (j == max_nonempty) {
346 string label = _(MenuNames[j - 1]);
348 fl_addtopup(menu, label.c_str(), menu2);
350 fl_addtopup(menu, _(MenuNames[j - 1]), menu2);
354 if (max_nonempty == 0 && toc_list[0].empty()) {
355 fl_addtopup(menu, (_("No Table of Contents%i")
356 + extra_label).c_str());
360 add_toc2(menu, extra_label, smn, win,
361 toc_list[0], 0, toc_list[0].size(), 0);
363 #warning Fix Me! (Lgb)
364 map<string, vector<Buffer::TocItem> > toc_list =
365 owner_->buffer()->getTocList();
367 map<string, vector<Buffer::TocItem> >::const_iterator cit =
369 map<string, vector<Buffer::TocItem> >::const_iterator end =
371 for (; cit != end; ++cit) {
372 // Handle this elsewhere
373 if (cit->first == "TOC") continue;
375 int menu2 = get_new_submenu(smn, win);
376 vector<Buffer::TocItem>::const_iterator ccit =
378 vector<Buffer::TocItem>::const_iterator eend =
380 for (; ccit != eend; ++ccit) {
381 int const action = lyxaction.getPseudoAction(LFUN_GOTO_PARAGRAPH, tostr(ccit->par->id()));
382 string label = fixlabel(ccit->str);
383 label = limit_string_length(label);
384 label += "%x" + tostr(action + action_offset);
385 fl_addtopup(menu2, label.c_str());
386 lyxerr << "[" << cit->first << "] " << label << endl;
388 string const m = cit->first + "%m";
389 fl_addtopup(menu, m.c_str(), menu2);
394 cit = toc_list.find("TOC");
396 string const tmp = _("No Table of contents%i") + extra_label;
397 fl_addtopup(menu, tmp.c_str());
400 add_toc2(menu, extra_label, smn, win,
401 cit->second, 0, cit->second.size(), 0);
407 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
408 string const & menu_name,
411 if (!menubackend_->hasMenu(menu_name)){
412 lyxerr << "ERROR:create_submenu: Unknown menu `"
413 << menu_name << "'" << endl;
417 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
419 int menu = get_new_submenu(smn, win);
420 fl_setpup_softedge(menu, true);
421 fl_setpup_bw(menu, -1);
422 lyxerr[Debug::GUI] << "Adding menu " << menu
423 << " in deletion list" << endl;
425 // Compute the size of the largest label (because xforms is
426 // not able to support shortcuts correctly...)
429 Menu::const_iterator end = md.end();
430 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
431 MenuItem const & item = (*i);
432 if (item.kind() == MenuItem::Command) {
433 string label = item.label() + '\t';
434 int width = string_width(label);
435 if (width > max_width) {
437 widest_label = label;
441 lyxerr[Debug::GUI] << "max_width=" << max_width
442 << ", widest_label=`" << widest_label
445 // Compute where to put separators
446 vector<string> extra_labels(md.size());
447 vector<string>::iterator it = extra_labels.begin();
448 vector<string>::iterator last = it;
449 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
450 if (i->kind() == MenuItem::Separator)
452 else if (!i->optional() ||
453 !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
456 it = extra_labels.begin();
457 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
458 MenuItem const & item = (*i);
459 string & extra_label = *it;
461 switch (item.kind()) {
462 case MenuItem::Command: {
463 LyXFunc::func_status flag =
464 view->getLyXFunc()->getStatus(item.action());
466 // handle optional entries.
467 if (item.optional() && (flag & LyXFunc::Disabled)) {
469 << "Skipping optional item "
470 << item.label() << endl;
474 // Get the keys bound to this action, but keep only the
476 string const accel = toplevel_keymap->findbinding(item.action());
477 // Build the menu label from all the info
478 string label = item.label();
480 if (!accel.empty()) {
481 // Try to be clever and add just enough
482 // tabs to align shortcuts.
485 while (string_width(label) < max_width + 5);
486 label += accel.substr(1,accel.find(']') - 1);
488 label += "%x" + tostr(item.action() + action_offset)
491 // Modify the entry using the function status
493 if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
495 if (flag & LyXFunc::ToggleOn)
497 if (flag & LyXFunc::ToggleOff)
501 // Finally the menu shortcut
502 string shortcut = item.shortcut();
504 if (!shortcut.empty()) {
505 shortcut += lowercase(shortcut[0]);
507 fl_addtopup(menu, label.c_str(),
510 fl_addtopup(menu, label.c_str());
512 lyxerr[Debug::GUI] << "Command: \""
513 << lyxaction.getActionName(item.action())
514 << "\", binding \"" << accel
515 << "\", shortcut \"" << shortcut
520 case MenuItem::Submenu: {
521 int submenu = create_submenu(win, view,
522 item.submenu(), smn);
525 string label = item.label();
526 label += extra_label + "%m";
527 string shortcut = item.shortcut();
528 if (!shortcut.empty()) {
529 shortcut += lowercase(shortcut[0]);
531 fl_addtopup(menu, label.c_str(),
532 submenu, shortcut.c_str());
535 fl_addtopup(menu, label.c_str(), submenu);
540 case MenuItem::Separator:
541 // already done, and if it was the first one,
542 // we just ignore it.
546 add_toc(menu, extra_label, smn, win);
549 case MenuItem::Documents:
550 case MenuItem::Lastfiles:
551 case MenuItem::ViewFormats:
552 case MenuItem::UpdateFormats:
553 case MenuItem::ExportFormats:
554 case MenuItem::ImportFormats:
555 lyxerr << "Menubar::Pimpl::create_submenu: "
556 "this should not happen" << endl;
566 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
568 Menubar::Pimpl::MenuCallback(ob, button);
572 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
574 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
575 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
576 // << "Val=(pimpl_=" << iteminfo->pimpl_
577 // << ", item_=" << iteminfo->item_
578 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
580 LyXView * view = iteminfo->pimpl_->owner_;
581 MenuItem const * item = iteminfo->item_.get();
584 // set the pseudo menu-button
585 fl_set_object_boxtype(ob, FL_DOWN_BOX);
586 fl_set_button(ob, 0);
587 fl_redraw_object(ob);
591 Assert(item->kind() == MenuItem::Submenu);
593 // set tabstop length
594 fl_set_tabstop(menu_tabstop);
595 vector<int> submenus;
596 int menu = iteminfo->pimpl_->
597 create_submenu(FL_ObjWin(ob), view,
598 item->submenu(), submenus);
601 fl_setpup_position(view->getForm()->x + ob->x,
602 view->getForm()->y + ob->y + ob->h + 10);
603 int choice = fl_dopup(menu);
606 // set the pseudo menu-button back
607 fl_set_object_boxtype(ob, FL_FLAT_BOX);
608 fl_redraw_object(ob);
611 // If the action value is too low, then it is not a
612 // valid action, but something else.
613 if (choice >= action_offset + 1) {
614 view->getLyXFunc()->Dispatch(choice - action_offset);
618 << "MenuCallback: ignoring bogus action "
623 lyxerr << "Error in MenuCallback" << endl;
625 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
626 // restore tabstop length
627 fl_set_tabstop(default_tabstop);