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"
26 #include "MenuBackend.h"
27 #include "Menubar_pimpl.h"
34 typedef vector<int>::size_type size_type;
36 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
37 extern LyXAction lyxaction;
40 static const int MENU_LABEL_SIZE = FL_NORMAL_SIZE;
41 static const int mheight = 30;
42 static const int mbheight= 22;
43 // where to place the menubar?
44 static const int yloc = (mheight - mbheight)/2; //air + bw;
45 static const int mbadd = 20; // menu button add (to width)
46 // Some space between buttons on the menubar
47 static const int air = 2;
48 static char const * menu_tabstop = "aa";
49 static char const * default_tabstop = "aaaaaaaa";
50 // We do not want to mix position values in a menu (like the index of
51 // a submenu) with the action numbers which convey actual information.
52 // Therefore we offset all the action values by an arbitrary large
54 static const int action_offset = 1000;
57 //Defined later, used in makeMenubar().
59 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
61 // This is used a few times below.
63 int string_width(string const & str)
65 return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
67 static_cast<int>(str.length()));
71 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
72 : owner_(view), menubackend_(&mb), current_group_(0)
74 for (MenuBackend::const_iterator menu = menubackend_->begin();
75 menu != menubackend_->end() ; ++menu) {
76 if (menu->menubar()) {
77 FL_OBJECT * group = fl_bgn_group();
80 fl_hide_object(group);
82 << "Menubar::Pimpl::Pimpl: "
83 << "creating and hiding group " << group
84 << " for menubar " << menu->name() << endl;
85 menubarmap_[menu->name()] = group;
91 void Menubar::Pimpl::makeMenubar(Menu const &menu)
93 FL_FORM * form = owner_->getForm();
96 // Create menu frame if there is non yet.
97 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
98 form->w, mheight, "");
99 fl_set_object_resize(frame, FL_RESIZE_ALL);
100 fl_set_object_gravity(frame, NorthWestGravity,
103 for (Menu::const_iterator i = menu.begin();
104 i != menu.end(); ++i) {
106 if (i->kind() != MenuItem::Submenu) {
107 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
108 " only submenus can appear in a menubar";
111 string label = i->label();
112 string shortcut = "#" + i->shortcut();
113 int width = string_width(label);
114 obj = fl_add_button(FL_MENU_BUTTON,
119 fl_set_object_boxtype(obj, FL_FLAT_BOX);
120 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
121 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
122 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
123 fl_set_object_resize(obj, FL_RESIZE_ALL);
124 fl_set_object_gravity(obj, NorthWestGravity,
126 moffset += obj->w + air;
127 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
128 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
130 boost::shared_ptr<ItemInfo> iteminfo(new ItemInfo(this,
131 new MenuItem(*i), obj));
132 buttonlist_.push_back(iteminfo);
133 obj->u_vdata = iteminfo.get();
138 void Menubar::Pimpl::set(string const & menu_name)
140 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
141 << "for menu `" << menu_name << "'" << endl;
143 if (menu_name != current_menu_name_) {
144 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
146 if (mbit == menubarmap_.end()) {
147 lyxerr << "ERROR:set: Unknown menu `" << menu_name
152 if (current_group_) {
153 lyxerr[Debug::GUI] << " hiding group "
154 << current_group_ << endl;
155 fl_hide_object(current_group_);
158 lyxerr[Debug::GUI] << " showing group "
159 << mbit->second << endl;
160 fl_show_object(mbit->second);
161 current_menu_name_ = menu_name;
162 current_group_ = mbit->second;
163 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
167 lyxerr [Debug::GUI] << "Menubar::Pimpl::set: Nothing to do."
171 void Menubar::Pimpl::openByName(string const & name)
173 if (menubackend_->getMenu(current_menu_name_).hasSubmenu(name)) {
174 for (ButtonList::const_iterator cit = buttonlist_.begin();
175 cit != buttonlist_.end(); ++cit) {
176 if ((*cit)->item_->submenu() == name) {
177 MenuCallback((*cit)->obj_, 1);
182 lyxerr << "Menubar::Pimpl::openByName: menu "
183 << name << " not found" << endl;
188 string const limit_string_length(string const & str)
190 string::size_type const max_item_length = 45;
192 if (str.size() > max_item_length)
193 return str.substr(0, max_item_length - 3) + "...";
200 int get_new_submenu(vector<int> & smn, Window win)
202 static size_type max_number_of_menus = 32;
203 if (smn.size() >= max_number_of_menus)
204 max_number_of_menus =
205 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
206 int menu = fl_newpup(win);
212 size_type const max_number_of_items = 25;
215 string const fixlabel(string const & str)
218 return subst(str, '%', '?');
220 return subst(str, "%", "%%");
225 void add_toc2(int menu, string const & extra_label,
226 vector<int> & smn, Window win,
227 vector<Buffer::TocItem> const & toc_list,
228 size_type from, size_type to, int depth)
230 int shortcut_count = 0;
231 if (to - from <= max_number_of_items) {
232 for (size_type i = from; i < to; ++i) {
233 int const action = lyxaction.
234 getPseudoAction(LFUN_GOTO_PARAGRAPH,
235 tostr(toc_list[i].par->id()));
236 string label(4 * max(0, toc_list[i].depth - depth),' ');
237 label += fixlabel(toc_list[i].str);
238 label = limit_string_length(label);
239 label += "%x" + tostr(action + action_offset);
240 if (i == to - 1 && depth == 0)
241 label += extra_label;
242 if (toc_list[i].depth == depth
243 && ++shortcut_count <= 9) {
245 fl_addtopup(menu, label.c_str(),
246 tostr(shortcut_count).c_str());
248 fl_addtopup(menu, label.c_str());
251 size_type pos = from;
255 if (count > max_number_of_items) {
256 int menu2 = get_new_submenu(smn, win);
257 add_toc2(menu2, extra_label, smn, win,
258 toc_list, pos, to, depth);
259 string label = _("More");
262 label += extra_label;
263 fl_addtopup(menu, label.c_str(), menu2);
266 size_type new_pos = pos+1;
267 while (new_pos < to &&
268 toc_list[new_pos].depth > depth)
271 int const action = lyxaction.
272 getPseudoAction(LFUN_GOTO_PARAGRAPH,
273 tostr(toc_list[pos].par->id()));
274 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
275 label += fixlabel(toc_list[pos].str);
276 label = limit_string_length(label);
277 if (new_pos == to && depth == 0)
278 label += extra_label;
280 if (toc_list[pos].depth == depth &&
281 ++shortcut_count <= 9)
282 shortcut = tostr(shortcut_count);
284 if (new_pos == pos + 1) {
285 label += "%x" + tostr(action + action_offset);
286 if (!shortcut.empty()) {
288 fl_addtopup(menu, label.c_str(),
291 fl_addtopup(menu, label.c_str());
293 int menu2 = get_new_submenu(smn, win);
294 add_toc2(menu2, extra_label, smn, win,
295 toc_list, pos, new_pos, depth+1);
297 if (!shortcut.empty()) {
299 fl_addtopup(menu, label.c_str(), menu2,
302 fl_addtopup(menu, label.c_str(), menu2);
310 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
311 vector<int> & smn, Window win)
314 //xgettext:no-c-format
315 static char const * MenuNames[3] = { N_("List of Figures%m"),
316 //xgettext:no-c-format
317 N_("List of Tables%m"),
318 //xgettext:no-c-format
319 N_("List of Algorithms%m") };
321 vector<vector<Buffer::TocItem> > toc_list =
322 owner_->buffer()->getTocList();
324 // Handle LOF/LOT/LOA
325 int max_nonempty = 0;
326 for (int i = 1; i <= 3; ++i)
327 if (!toc_list[i].empty())
330 for (int j = 1; j <= 3; ++j)
331 if (!toc_list[j].empty()) {
332 int menu2 = get_new_submenu(smn, win);
333 for (size_type i = 0; i < toc_list[j].size(); ++i) {
334 if (i > max_number_of_items) {
335 fl_addtopup(menu2, ". . .%d");
338 int const action = lyxaction.
339 getPseudoAction(LFUN_GOTO_PARAGRAPH,
340 tostr(toc_list[j][i].par->id()));
341 string label = fixlabel(toc_list[j][i].str);
342 label = limit_string_length(label);
343 label += "%x" + tostr(action + action_offset);
344 fl_addtopup(menu2, label.c_str());
346 if (j == max_nonempty) {
347 string label = _(MenuNames[j - 1]);
349 fl_addtopup(menu, label.c_str(), menu2);
351 fl_addtopup(menu, _(MenuNames[j - 1]), menu2);
355 if (max_nonempty == 0 && toc_list[0].empty()) {
356 fl_addtopup(menu, (_("No Table of Contents%i")
357 + extra_label).c_str());
361 add_toc2(menu, extra_label, smn, win,
362 toc_list[0], 0, toc_list[0].size(), 0);
364 #warning Fix Me! (Lgb)
365 Buffer::Lists toc_list = owner_->buffer()->getLists();
366 Buffer::Lists::const_iterator cit = toc_list.begin();
367 Buffer::Lists::const_iterator end = toc_list.end();
368 for (; cit != end; ++cit) {
369 // Handle this elsewhere
370 if (cit->first == "TOC") continue;
372 int menu2 = get_new_submenu(smn, win);
373 Buffer::SingleList::const_iterator ccit = cit->second.begin();
374 Buffer::SingleList::const_iterator eend = cit->second.end();
375 for (; ccit != eend; ++ccit) {
378 .getPseudoAction(LFUN_GOTO_PARAGRAPH,
379 tostr(ccit->par->id()));
380 string label = fixlabel(ccit->str);
381 label = limit_string_length(label);
382 label += "%x" + tostr(action + action_offset);
383 fl_addtopup(menu2, label.c_str());
385 string const m = cit->first + "%m";
386 fl_addtopup(menu, m.c_str(), menu2);
391 cit = toc_list.find("TOC");
393 string const tmp = _("No Table of contents%i") + extra_label;
394 fl_addtopup(menu, tmp.c_str());
397 add_toc2(menu, extra_label, smn, win,
398 cit->second, 0, cit->second.size(), 0);
404 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
405 string const & menu_name,
408 if (!menubackend_->hasMenu(menu_name)){
409 lyxerr << "ERROR:create_submenu: Unknown menu `"
410 << menu_name << "'" << endl;
414 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
416 int menu = get_new_submenu(smn, win);
417 fl_setpup_softedge(menu, true);
418 fl_setpup_bw(menu, -1);
419 lyxerr[Debug::GUI] << "Adding menu " << menu
420 << " in deletion list" << endl;
422 // Compute the size of the largest label (because xforms is
423 // not able to support shortcuts correctly...)
426 Menu::const_iterator end = md.end();
427 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
428 MenuItem const & item = (*i);
429 if (item.kind() == MenuItem::Command) {
430 string label = item.label() + '\t';
431 int width = string_width(label);
432 if (width > max_width) {
434 widest_label = label;
438 lyxerr[Debug::GUI] << "max_width=" << max_width
439 << ", widest_label=`" << widest_label
442 // Compute where to put separators
443 vector<string> extra_labels(md.size());
444 vector<string>::iterator it = extra_labels.begin();
445 vector<string>::iterator last = it;
446 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
447 if (i->kind() == MenuItem::Separator)
449 else if (!i->optional() ||
450 !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
453 it = extra_labels.begin();
454 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
455 MenuItem const & item = (*i);
456 string & extra_label = *it;
458 switch (item.kind()) {
459 case MenuItem::Command: {
460 LyXFunc::func_status flag =
461 view->getLyXFunc()->getStatus(item.action());
463 // handle optional entries.
464 if (item.optional() && (flag & LyXFunc::Disabled)) {
466 << "Skipping optional item "
467 << item.label() << endl;
471 // Get the keys bound to this action, but keep only the
473 string const accel = toplevel_keymap->findbinding(item.action());
474 // Build the menu label from all the info
475 string label = item.label();
477 if (!accel.empty()) {
478 // Try to be clever and add just enough
479 // tabs to align shortcuts.
482 while (string_width(label) < max_width + 5);
483 label += accel.substr(1,accel.find(']') - 1);
485 label += "%x" + tostr(item.action() + action_offset)
488 // Modify the entry using the function status
490 if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
492 if (flag & LyXFunc::ToggleOn)
494 if (flag & LyXFunc::ToggleOff)
498 // Finally the menu shortcut
499 string shortcut = item.shortcut();
501 if (!shortcut.empty()) {
502 shortcut += lowercase(shortcut[0]);
504 fl_addtopup(menu, label.c_str(),
507 fl_addtopup(menu, label.c_str());
509 lyxerr[Debug::GUI] << "Command: \""
510 << lyxaction.getActionName(item.action())
511 << "\", binding \"" << accel
512 << "\", shortcut \"" << shortcut
517 case MenuItem::Submenu: {
518 int submenu = create_submenu(win, view,
519 item.submenu(), smn);
522 string label = item.label();
523 label += extra_label + "%m";
524 string shortcut = item.shortcut();
525 if (!shortcut.empty()) {
526 shortcut += lowercase(shortcut[0]);
528 fl_addtopup(menu, label.c_str(),
529 submenu, shortcut.c_str());
532 fl_addtopup(menu, label.c_str(), submenu);
537 case MenuItem::Separator:
538 // already done, and if it was the first one,
539 // we just ignore it.
543 add_toc(menu, extra_label, smn, win);
546 case MenuItem::Documents:
547 case MenuItem::Lastfiles:
548 case MenuItem::ViewFormats:
549 case MenuItem::UpdateFormats:
550 case MenuItem::ExportFormats:
551 case MenuItem::ImportFormats:
552 lyxerr << "Menubar::Pimpl::create_submenu: "
553 "this should not happen" << endl;
563 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
565 Menubar::Pimpl::MenuCallback(ob, button);
569 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
571 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
572 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
573 // << "Val=(pimpl_=" << iteminfo->pimpl_
574 // << ", item_=" << iteminfo->item_
575 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
577 LyXView * view = iteminfo->pimpl_->owner_;
578 MenuItem const * item = iteminfo->item_.get();
580 /* get the splash out of the way. It would be nicer
581 * to only have this code at the start, but xforms
582 * makes it too ugly to do
584 view->getDialogs()->destroySplash();
587 // set the pseudo menu-button
588 fl_set_object_boxtype(ob, FL_DOWN_BOX);
589 fl_set_button(ob, 0);
590 fl_redraw_object(ob);
594 Assert(item->kind() == MenuItem::Submenu);
596 // set tabstop length
597 fl_set_tabstop(menu_tabstop);
598 vector<int> submenus;
599 int menu = iteminfo->pimpl_->
600 create_submenu(FL_ObjWin(ob), view,
601 item->submenu(), submenus);
604 fl_setpup_position(view->getForm()->x + ob->x,
605 view->getForm()->y + ob->y + ob->h + 10);
606 int choice = fl_dopup(menu);
609 // set the pseudo menu-button back
610 fl_set_object_boxtype(ob, FL_FLAT_BOX);
611 fl_redraw_object(ob);
614 // If the action value is too low, then it is not a
615 // valid action, but something else.
616 if (choice >= action_offset + 1) {
617 view->getLyXFunc()->Dispatch(choice - action_offset);
621 << "MenuCallback: ignoring bogus action "
626 lyxerr << "Error in MenuCallback" << endl;
628 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
629 // restore tabstop length
630 fl_set_tabstop(default_tabstop);