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 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";
50 //Defined later, used in makeMenubar().
52 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
54 // This is used a few times below.
56 int string_width(string const & str)
58 return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
59 str.c_str(), str.length());
63 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
64 : owner_(view), menubackend_(&mb), current_group_(0)
66 for(MenuBackend::const_iterator menu = menubackend_->begin();
67 menu != menubackend_->end() ; ++menu) {
68 if (menu->menubar()) {
69 FL_OBJECT * group = fl_bgn_group();
72 fl_hide_object(group);
74 << "Menubar::Pimpl::Pimpl: "
75 << "creating and hiding group " << group
76 << " for menubar " << menu->name() << endl;
77 menubarmap_[menu->name()] = group;
83 Menubar::Pimpl::~Pimpl()
85 // Should we do something here?
89 void Menubar::Pimpl::makeMenubar(Menu const &menu)
91 FL_FORM * form = owner_->getForm();
94 // Create menu frame if there is non yet.
95 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
96 form->w, mheight, "");
97 fl_set_object_resize(frame, FL_RESIZE_ALL);
98 fl_set_object_gravity(frame, NorthWestGravity,
101 for (Menu::const_iterator i = menu.begin();
102 i != menu.end(); ++i) {
104 if (i->kind() != MenuItem::Submenu) {
105 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
106 " only submenus can appear in a menubar";
109 string label = i->label();
110 string shortcut = "#" + i->shortcut();
111 int width = string_width(label);
112 obj = fl_add_button(FL_TOUCH_BUTTON,
117 fl_set_object_boxtype(obj, FL_FLAT_BOX);
118 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
119 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
120 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
121 fl_set_object_resize(obj, FL_RESIZE_ALL);
122 fl_set_object_gravity(obj, NorthWestGravity,
124 moffset += obj->w + air;
125 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
126 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
128 ItemInfo * iteminfo = new ItemInfo(this,
129 new MenuItem(*i), obj);
130 buttonlist_.push_back(iteminfo);
131 obj->u_vdata = iteminfo;
136 void Menubar::Pimpl::set(string const & menu_name)
138 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
139 << "for menu `" << menu_name << "'" << endl;
141 if (menu_name != current_menu_name_) {
142 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
144 if (mbit == menubarmap_.end()) {
145 lyxerr << "ERROR:set: Unknown menu `" << menu_name
150 if (current_group_) {
151 lyxerr[Debug::GUI] << " hiding group "
152 << current_group_ << endl;
153 fl_hide_object(current_group_);
156 lyxerr[Debug::GUI] << " showing group "
157 << mbit->second << endl;
158 fl_show_object(mbit->second);
159 current_menu_name_ = menu_name;
160 current_group_ = mbit->second;
161 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
165 lyxerr [Debug::GUI] << "Menubar::Pimpl::set: Nothing to do."
169 void Menubar::Pimpl::openByName(string const & name)
171 for(ButtonList::const_iterator cit = buttonlist_.begin();
172 cit != buttonlist_.end(); ++cit) {
173 if ((*cit)->item_->submenu() == name) {
174 MenuCallback((*cit)->obj_, 1);
178 lyxerr << "Menubar::Pimpl::openByName: menu "
179 << name << " not found" << endl;
183 string limit_string_length(string const & str)
185 string::size_type const max_item_length = 45;
187 if (str.size() > max_item_length)
188 return str.substr(0, max_item_length - 3) + "...";
194 int get_new_submenu(vector<int> & smn, Window win)
196 static size_type max_number_of_menus = 32;
197 if (smn.size() >= max_number_of_menus)
198 max_number_of_menus = fl_setpup_maxpup(2*smn.size());
199 int menu = fl_newpup(win);
204 size_type const max_number_of_items = 25;
206 void add_toc2(int menu, string const & extra_label,
207 vector<int> & smn, Window win,
208 vector<Buffer::TocItem> const & toc_list,
209 size_type from, size_type to, int depth)
211 if (to - from <= max_number_of_items) {
212 for (size_type i = from; i < to; ++i) {
213 int action = lyxaction.
214 getPseudoAction(LFUN_GOTO_PARAGRAPH,
215 tostr(toc_list[i].par->id()));
216 string label(4 * max(0, toc_list[i].depth - depth),' ');
217 label += toc_list[i].str;
218 label = limit_string_length(label);
219 label += "%x" + tostr(action);
220 if (i == to - 1 && depth == 0)
221 label += extra_label;
222 fl_addtopup(menu, label.c_str());
225 size_type pos = from;
229 if (count > max_number_of_items) {
230 int menu2 = get_new_submenu(smn, win);
231 add_toc2(menu2, extra_label, smn, win,
232 toc_list, pos, to, depth);
233 string label = _("More");
236 label += extra_label;
237 fl_addtopup(menu, label.c_str(), menu2);
240 size_type new_pos = pos+1;
241 while (new_pos < to &&
242 toc_list[new_pos].depth > depth)
245 int action = lyxaction.
246 getPseudoAction(LFUN_GOTO_PARAGRAPH,
247 tostr(toc_list[pos].par->id()));
248 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
249 label += toc_list[pos].str;
250 label = limit_string_length(label);
251 if (new_pos == to && depth == 0)
252 label += extra_label;
254 if (new_pos == pos + 1) {
255 label += "%x" + tostr(action);
256 fl_addtopup(menu, label.c_str());
258 int menu2 = get_new_submenu(smn, win);
259 add_toc2(menu2, extra_label, smn, win,
260 toc_list, pos, new_pos, depth+1);
262 fl_addtopup(menu, label.c_str(), menu2);
269 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
270 vector<int> & smn, Window win)
272 //xgettext:no-c-format
273 static char const * MenuNames[3] = { N_("List of Figures%m"),
274 //xgettext:no-c-format
275 N_("List of Tables%m"),
276 //xgettext:no-c-format
277 N_("List of Algorithms%m") };
279 vector<vector<Buffer::TocItem> > toc_list =
280 owner_->buffer()->getTocList();
282 // Handle LOF/LOT/LOA
283 int max_nonempty = 0;
284 for (int i = 1; i <= 3; ++i)
285 if (!toc_list[i].empty())
288 for (int j = 1; j <= 3; ++j)
289 if (!toc_list[j].empty()) {
290 int menu2 = get_new_submenu(smn, win);
291 for (size_type i = 0; i < toc_list[j].size(); ++i) {
292 if (i > max_number_of_items) {
293 fl_addtopup(menu2, ". . .%d");
296 int action = lyxaction.
297 getPseudoAction(LFUN_GOTO_PARAGRAPH,
298 tostr(toc_list[j][i].par->id()));
300 limit_string_length(toc_list[j][i].str);
301 label += "%x" + tostr(action);
302 fl_addtopup(menu2, label.c_str());
304 if (j == max_nonempty) {
305 string label = _(MenuNames[j - 1]);
307 fl_addtopup(menu, label.c_str(), menu2);
309 fl_addtopup(menu, _(MenuNames[j - 1]), menu2);
313 if (max_nonempty == 0 && toc_list[0].empty()) {
314 fl_addtopup(menu,_("No Table of Contents%i"));
318 add_toc2(menu, extra_label, smn, win,
319 toc_list[0], 0, toc_list[0].size(), 0);
323 void add_references2(int menu, vector<int> & smn, Window win,
324 vector<string> const & label_list, string const & type)
326 size_type const max_number_of_items = 25;
327 size_type const max_number_of_items2 = 20;
328 string::size_type const max_item_length = 40;
329 string::size_type const max_item_length2 = 20;
331 if (label_list.size() <= max_number_of_items)
332 for (size_type i = 0; i < label_list.size(); ++i) {
333 int action = (type == "goto")
334 ? lyxaction.getPseudoAction(LFUN_REF_GOTO,
336 : lyxaction.getPseudoAction(LFUN_REF_INSERT,
339 string label = label_list[i];
340 if (label.size() > max_item_length)
341 label = label.substr(0, max_item_length-1) + "$";
342 label += "%x" + tostr(action);
343 fl_addtopup(menu, label.c_str());
347 for (size_type i = 0; i < label_list.size();
348 i += max_number_of_items2) {
350 if (count > max_number_of_items) {
351 fl_addtopup(menu, ". . .%d");
354 size_type j = min(label_list.size(),
355 i+max_number_of_items2);
358 label += (label_list[i].size() > max_item_length2)
359 ? label_list[i].substr(0, max_item_length2-1) + "$"
362 label += (label_list[j-1].size() > max_item_length2)
363 ? label_list[j-1].substr(0, max_item_length2-1) + "$"
364 : label += label_list[j-1];
366 int menu2 = get_new_submenu(smn, win);
367 for (size_type k = i; k < j; ++k) {
368 int action = (type == "goto")
369 ? lyxaction.getPseudoAction(LFUN_REF_GOTO,
371 : lyxaction.getPseudoAction(LFUN_REF_INSERT,
374 string label2 = label_list[k];
375 if (label2.size() > max_item_length)
376 label2 = label2.substr(0, max_item_length-1) + "$";
377 label2 += "%x" + tostr(action);
378 fl_addtopup(menu2, label2.c_str());
381 fl_addtopup(menu, label.c_str(), menu2);
387 void Menubar::Pimpl::add_references(int menu, string const & extra_label,
388 vector<int> & smn, Window win)
390 //xgettext:no-c-format
391 static char const * MenuNames[6] = { N_("Insert Reference%m"),
392 //xgettext:no-c-format
393 N_("Insert Page Number%m"),
394 //xgettext:no-c-format
396 //xgettext:no-c-format
397 N_("Insert vpageref%m"),
398 //xgettext:no-c-format
399 N_("Insert Pretty Ref%m"),
400 //xgettext:no-c-format
401 N_("Goto Reference%m") };
405 int const READONLY = 4;
407 static int MenuFlags[6] = {
410 EMPTY | READONLY | SGML,
411 EMPTY | READONLY | SGML,
412 EMPTY | READONLY | SGML,
415 static string const MenuTypes[6] = {
416 "ref", "pageref", "vref", "vpageref", "prettyref", "goto" };
418 vector<string> label_list = owner_->buffer()->getLabelList();
421 if (label_list.empty())
423 if (owner_->buffer()->isSGML())
425 if (owner_->buffer()->isReadonly())
428 int max_nonempty = -1;
429 for (int i = 0; i < 6; ++i)
430 if ((MenuFlags[i] & flag) == 0)
433 for (int i = 0; i < 6; ++i) {
434 if ((MenuFlags[i] & flag) == 0) {
435 string label = _(MenuNames[i]);
436 if (i == max_nonempty)
437 label += extra_label;
438 int menu2 = get_new_submenu(smn, win);
439 add_references2(menu2, smn, win, label_list,
441 fl_addtopup(menu, label.c_str(), menu2);
447 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
448 string const & menu_name,
451 if (!menubackend_->hasMenu(menu_name)){
452 lyxerr << "ERROR:create_submenu: Unknown menu `"
453 << menu_name << "'" << endl;
457 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
459 int menu = get_new_submenu(smn, win);
460 fl_setpup_softedge(menu, true);
461 fl_setpup_bw(menu, -1);
462 lyxerr[Debug::GUI] << "Adding menu " << menu
463 << " in deletion list" << endl;
465 // Compute the size of the largest label (because xforms is
466 // not able to support shortcuts correctly...)
469 Menu::const_iterator end = md.end();
470 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
471 MenuItem const & item = (*i);
472 if (item.kind() == MenuItem::Command) {
473 string label = item.label() + '\t';
474 int width = string_width(label);
475 if (width > max_width) {
477 widest_label = label;
481 lyxerr[Debug::GUI] << "max_width=" << max_width
482 << ", widest_label=`" << widest_label
485 // Compute where to put separators
486 vector<string> extra_labels(md.size());
487 vector<string>::iterator it = extra_labels.begin();
488 vector<string>::iterator last = it;
489 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
490 if (i->kind() == MenuItem::Separator)
492 else if (!i->optional() ||
493 !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
496 it = extra_labels.begin();
497 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
498 MenuItem const & item = (*i);
499 string & extra_label = *it;
501 switch(item.kind()) {
502 case MenuItem::Command: {
503 LyXFunc::func_status flag =
504 view->getLyXFunc()->getStatus(item.action());
506 // handle optional entries.
507 if (item.optional() && (flag & LyXFunc::Disabled)) {
509 << "Skipping optional item "
510 << item.label() << endl;
514 // Get the keys bound to this action, but keep only the
516 string accel = toplevel_keymap->findbinding(item.action());
517 // Build the menu label from all the info
518 string label = item.label();
520 if (!accel.empty()) {
521 // Try to be clever and add just enough
522 // tabs to align shortcuts.
525 while (string_width(label) < max_width);
526 label += accel.substr(1,accel.find(']') - 1);
528 label += "%x" + tostr(item.action()) + extra_label;
530 // Modify the entry using the function status
532 if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
534 if (flag & LyXFunc::ToggleOn)
536 if (flag & LyXFunc::ToggleOff)
540 // Finally the menu shortcut
541 string shortcut = item.shortcut();
543 if (!shortcut.empty()) {
544 shortcut += lowercase(shortcut[0]);
546 fl_addtopup(menu, label.c_str(),
549 fl_addtopup(menu, label.c_str());
551 lyxerr[Debug::GUI] << "Command: \""
552 << lyxaction.getActionName(item.action())
553 << "\", Binding " << accel
554 << ", shortcut " << shortcut
561 case MenuItem::Submenu: {
562 int submenu = create_submenu(win, view,
563 item.submenu(), smn);
566 string label = item.label();
567 label += extra_label + "%m";
568 string shortcut = item.shortcut();
569 if (!shortcut.empty()) {
570 shortcut += lowercase(shortcut[0]);
572 fl_addtopup(menu, label.c_str(),
573 submenu, shortcut.c_str());
576 fl_addtopup(menu, label.c_str(), submenu);
581 case MenuItem::Separator:
582 // already done, and if it was the first one,
583 // we just ignore it.
587 add_toc(menu, extra_label, smn, win);
590 case MenuItem::References:
591 add_references(menu, extra_label, smn, win);
594 case MenuItem::Documents:
595 case MenuItem::Lastfiles:
596 case MenuItem::ViewFormats:
597 case MenuItem::UpdateFormats:
598 case MenuItem::ExportFormats:
599 lyxerr << "Menubar::Pimpl::create_submenu: "
600 "this should not happen" << endl;
609 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
611 Menubar::Pimpl::MenuCallback(ob, button);
615 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
617 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
618 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
619 // << "Val=(pimpl_=" << iteminfo->pimpl_
620 // << ", item_=" << iteminfo->item_
621 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
623 LyXView * view = iteminfo->pimpl_->owner_;
624 MenuItem const * item = iteminfo->item_;
627 // set the pseudo menu-button
628 fl_set_object_boxtype(ob, FL_DOWN_BOX);
629 fl_set_button(ob, 0);
630 fl_redraw_object(ob);
634 Assert(item->kind() == MenuItem::Submenu);
636 // set tabstop length
637 fl_set_tabstop(menu_tabstop);
638 vector<int> submenus;
639 int menu = iteminfo->pimpl_->
640 create_submenu(FL_ObjWin(ob), view,
641 item->submenu(), submenus);
644 fl_setpup_position(view->getForm()->x + ob->x,
645 view->getForm()->y + ob->y + ob->h + 10);
646 int choice = fl_dopup(menu);
649 // set the pseudo menu-button back
650 fl_set_object_boxtype(ob, FL_FLAT_BOX);
651 fl_redraw_object(ob);
655 view->getLyXFunc()->Dispatch(choice);
659 lyxerr << "Error in MenuCallback" << endl;
661 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
662 // restore tabstop length
663 fl_set_tabstop(default_tabstop);