3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
11 * \author Martin Vermeer
13 * Full author contact details are available in file CREDITS.
18 #include "MenuBackend.h"
20 #include "BranchList.h"
22 #include "bufferlist.h"
23 #include "bufferparams.h"
24 #include "CutAndPaste.h"
28 #include "FloatList.h"
34 #include "LyXAction.h"
35 #include "lyx_main.h" // for lastfiles
38 #include "TocBackend.h"
39 #include "ToolbarBackend.h"
41 #include "support/filetools.h"
42 #include "support/lstrings.h"
43 #include "support/convert.h"
45 #include <boost/bind.hpp>
52 using support::compare_no_case;
53 using support::compare_ascii_no_case;
54 using support::contains;
55 using support::makeDisplayPath;
57 using support::uppercase;
73 class MenuNamesEqual : public std::unary_function<Menu, bool> {
75 MenuNamesEqual(docstring const & name)
77 bool operator()(Menu const & menu) const
79 return menu.name() == name_;
88 // This is the global menu definition
89 MenuBackend menubackend;
92 MenuItem::MenuItem(Kind kind)
93 : kind_(kind), optional_(false)
97 MenuItem::MenuItem(Kind kind, docstring const & label,
98 docstring const & submenu, bool optional)
99 : kind_(kind), label_(label),
100 submenuname_(submenu), optional_(optional)
102 BOOST_ASSERT(kind == Submenu);
106 MenuItem::MenuItem(Kind kind, docstring const & label,
107 FuncRequest const & func, bool optional)
108 : kind_(kind), label_(label), func_(func), optional_(optional)
110 func_.origin = FuncRequest::MENU;
114 MenuItem::~MenuItem()
118 void MenuItem::submenu(Menu * menu)
120 submenu_.reset(menu);
124 docstring const MenuItem::label() const
126 return token(label_, char_type('|'), 0);
130 docstring const MenuItem::shortcut() const
132 return token(label_, char_type('|'), 1);
136 docstring const MenuItem::binding() const
138 if (kind_ != Command)
141 // Get the keys bound to this action, but keep only the
143 kb_keymap::Bindings bindings = theTopLevelKeymap().findbindings(func_);
145 if (bindings.size()) {
146 return from_utf8(bindings.begin()->print());
150 << lyxaction.getActionName(func_.action)
151 << '(' << to_utf8(func_.argument()) << ')' << endl;
158 Menu & Menu::add(MenuItem const & i)
165 Menu & Menu::addWithStatusCheck(MenuItem const & i)
169 case MenuItem::Command: {
170 FuncStatus status = lyx::getStatus(i.func());
171 if (status.unknown() || (!status.enabled() && i.optional()))
174 items_.back().status(status);
178 case MenuItem::Submenu: {
180 bool enabled = false;
181 for (const_iterator cit = i.submenu()->begin();
182 cit != i.submenu()->end(); ++cit) {
183 if ((cit->kind() == MenuItem::Command
184 || cit->kind() == MenuItem::Submenu)
185 && cit->status().enabled()) {
190 if (enabled || !i.optional()) {
192 items_.back().status().enabled(enabled);
200 case MenuItem::Separator:
202 && items_.back().kind() != MenuItem::Separator)
214 Menu & Menu::read(LyXLex & lex)
240 struct keyword_item menutags[md_last - 1] = {
241 { "bookmarks", md_bookmarks },
242 { "branches", md_branches },
243 { "charstyles", md_charstyles },
244 { "documents", md_documents },
245 { "end", md_endmenu },
246 { "exportformats", md_exportformats },
247 { "floatinsert", md_floatinsert },
248 { "floatlistinsert", md_floatlistinsert },
249 { "importformats", md_importformats },
251 { "lastfiles", md_lastfiles },
252 { "optitem", md_optitem },
253 { "optsubmenu", md_optsubmenu },
254 { "pasterecent", md_pasterecent },
255 { "separator", md_separator },
256 { "submenu", md_submenu },
258 { "toolbars", md_toolbars },
259 { "updateformats", md_updateformats },
260 { "viewformats", md_viewformats }
263 lex.pushTable(menutags, md_last - 1);
264 if (lyxerr.debugging(Debug::PARSER))
265 lex.printTable(lyxerr);
268 bool optional = false;
270 while (lex.isOK() && !quit) {
274 // fallback to md_item
277 docstring const name = _(lex.getString());
279 string const command = lex.getString();
280 FuncRequest func = lyxaction.lookupFunc(command);
281 add(MenuItem(MenuItem::Command, name, func, optional));
287 add(MenuItem(MenuItem::Separator));
291 add(MenuItem(MenuItem::Lastfiles));
295 add(MenuItem(MenuItem::CharStyles));
299 add(MenuItem(MenuItem::Documents));
303 add(MenuItem(MenuItem::Bookmarks));
307 add(MenuItem(MenuItem::Toc));
311 add(MenuItem(MenuItem::ViewFormats));
314 case md_updateformats:
315 add(MenuItem(MenuItem::UpdateFormats));
318 case md_exportformats:
319 add(MenuItem(MenuItem::ExportFormats));
322 case md_importformats:
323 add(MenuItem(MenuItem::ImportFormats));
326 case md_floatlistinsert:
327 add(MenuItem(MenuItem::FloatListInsert));
331 add(MenuItem(MenuItem::FloatInsert));
335 add(MenuItem(MenuItem::PasteRecent));
339 add(MenuItem(MenuItem::Toolbars));
343 add(MenuItem(MenuItem::Branches));
348 // fallback to md_submenu
351 docstring const mlabel = _(lex.getString());
353 docstring const mname = from_utf8(lex.getString());
354 add(MenuItem(MenuItem::Submenu, mlabel, mname,
365 lex.printError("Menu::read: "
366 "Unknown menu tag: `$$Token'");
375 MenuItem const & Menu::operator[](size_type i) const
381 bool Menu::hasFunc(FuncRequest const & func) const
383 return find_if(begin(), end(),
384 bind(std::equal_to<FuncRequest>(),
385 bind(&MenuItem::func, _1),
389 void Menu::checkShortcuts() const
391 // This is a quadratic algorithm, but we do not care because
392 // menus are short enough
393 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
394 docstring shortcut = it1->shortcut();
395 if (shortcut.empty())
397 if (!contains(it1->label(), shortcut))
398 lyxerr << "Menu warning: menu entry \""
399 << to_utf8(it1->label())
400 << "\" does not contain shortcut `"
401 << to_utf8(shortcut) << "'." << endl;
402 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
403 if (!compare_no_case(it2->shortcut(), shortcut)) {
404 lyxerr << "Menu warning: menu entries "
405 << '"' << to_utf8(it1->fulllabel())
406 << "\" and \"" << to_utf8(it2->fulllabel())
407 << "\" share the same shortcut."
415 void MenuBackend::specialMenu(docstring const &name)
418 specialmenu_ = &getMenu(name);
424 class compare_format {
426 bool operator()(Format const * p1, Format const * p2) {
431 docstring const limit_string_length(docstring const & str)
433 docstring::size_type const max_item_length = 45;
435 if (str.size() > max_item_length)
436 return str.substr(0, max_item_length - 3) + "...";
442 void expandLastfiles(Menu & tomenu)
444 lyx::LastFilesSection::LastFiles const & lf = LyX::cref().session().lastFiles().lastFiles();
445 lyx::LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
449 for (; lfit != lf.end() && ii < 10; ++lfit, ++ii) {
450 docstring const label = convert<docstring>(ii) + ". "
451 + makeDisplayPath((*lfit), 30)
452 + char_type('|') + convert<docstring>(ii);
453 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))));
458 void expandDocuments(Menu & tomenu)
460 typedef vector<string> Strings;
461 Strings const names = theBufferList().getFileNames();
464 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
465 FuncRequest(LFUN_NOACTION)));
470 Strings::const_iterator docit = names.begin();
471 Strings::const_iterator end = names.end();
472 for (; docit != end; ++docit, ++ii) {
473 docstring label = makeDisplayPath(*docit, 20);
475 label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
476 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BUFFER_SWITCH, *docit)));
481 void expandBookmarks(Menu & tomenu)
483 lyx::BookmarksSection const & bm = LyX::cref().session().bookmarks();
485 for (size_t i = 1; i <= bm.size(); ++i) {
487 docstring const label = convert<docstring>(i) + ". "
488 + makeDisplayPath(bm.bookmark(i).filename, 20)
489 + char_type('|') + convert<docstring>(i);
490 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BOOKMARK_GOTO,
491 convert<docstring>(i))));
497 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
499 if (!buf && kind != MenuItem::ImportFormats) {
500 tomenu.add(MenuItem(MenuItem::Command,
501 _("No Documents Open!"),
502 FuncRequest(LFUN_NOACTION)));
506 typedef vector<Format const *> Formats;
511 case MenuItem::ImportFormats:
512 formats = Importer::GetImportableFormats();
513 action = LFUN_BUFFER_IMPORT;
515 case MenuItem::ViewFormats:
516 formats = Exporter::getExportableFormats(*buf, true);
517 action = LFUN_BUFFER_VIEW;
519 case MenuItem::UpdateFormats:
520 formats = Exporter::getExportableFormats(*buf, true);
521 action = LFUN_BUFFER_UPDATE;
524 formats = Exporter::getExportableFormats(*buf, false);
525 action = LFUN_BUFFER_EXPORT;
527 sort(formats.begin(), formats.end(), compare_format());
529 Formats::const_iterator fit = formats.begin();
530 Formats::const_iterator end = formats.end();
531 for (; fit != end ; ++fit) {
534 docstring label = from_utf8((*fit)->prettyname());
537 case MenuItem::ImportFormats:
538 if ((*fit)->name() == "text")
539 label = _("Plain Text as Lines");
540 else if ((*fit)->name() == "textparagraph")
541 label = _("Plain Text as Paragraphs");
544 case MenuItem::ViewFormats:
545 case MenuItem::ExportFormats:
546 case MenuItem::UpdateFormats:
547 if (!(*fit)->documentFormat())
554 if (!(*fit)->shortcut().empty())
555 label += char_type('|') + from_utf8((*fit)->shortcut());
558 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
559 FuncRequest(action, (*fit)->name())));
561 tomenu.add(MenuItem(MenuItem::Command, label,
562 FuncRequest(action, (*fit)->name())));
567 void expandFloatListInsert(Menu & tomenu, Buffer const * buf)
570 tomenu.add(MenuItem(MenuItem::Command,
571 _("No Documents Open!"),
572 FuncRequest(LFUN_NOACTION)));
576 FloatList const & floats =
577 buf->params().getLyXTextClass().floats();
578 FloatList::const_iterator cit = floats.begin();
579 FloatList::const_iterator end = floats.end();
580 for (; cit != end; ++cit) {
581 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
582 _(cit->second.listName()),
583 FuncRequest(LFUN_FLOAT_LIST,
584 cit->second.type())));
589 void expandFloatInsert(Menu & tomenu, Buffer const * buf)
592 tomenu.add(MenuItem(MenuItem::Command,
593 _("No Documents Open!"),
594 FuncRequest(LFUN_NOACTION)));
598 FloatList const & floats =
599 buf->params().getLyXTextClass().floats();
600 FloatList::const_iterator cit = floats.begin();
601 FloatList::const_iterator end = floats.end();
602 for (; cit != end; ++cit) {
604 docstring const label = _(cit->second.name());
605 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
606 FuncRequest(LFUN_FLOAT_INSERT,
607 cit->second.type())));
612 void expandCharStyleInsert(Menu & tomenu, Buffer const * buf)
615 tomenu.add(MenuItem(MenuItem::Command,
616 _("No Documents Open!"),
617 FuncRequest(LFUN_NOACTION)));
620 CharStyles & charstyles =
621 buf->params().getLyXTextClass().charstyles();
622 CharStyles::iterator cit = charstyles.begin();
623 CharStyles::iterator end = charstyles.end();
624 for (; cit != end; ++cit) {
625 docstring const label = from_utf8(cit->name);
626 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
627 FuncRequest(LFUN_CHARSTYLE_INSERT,
633 Menu::size_type const max_number_of_items = 25;
635 void expandToc2(Menu & tomenu,
636 TocBackend::Toc const & toc_list,
637 TocBackend::Toc::size_type from,
638 TocBackend::Toc::size_type to, int depth)
640 int shortcut_count = 0;
642 // check whether depth is smaller than the smallest depth in toc.
643 int min_depth = 1000;
644 for (TocBackend::Toc::size_type i = from; i < to; ++i)
645 min_depth = std::min(min_depth, toc_list[i].depth());
646 if (min_depth > depth)
650 if (to - from <= max_number_of_items) {
651 for (TocBackend::Toc::size_type i = from; i < to; ++i) {
652 docstring label(4 * max(0, toc_list[i].depth() - depth), char_type(' '));
653 label += limit_string_length(toc_list[i].str());
654 if (toc_list[i].depth() == depth
655 && shortcut_count < 9) {
656 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
657 label += char_type('|') + convert<docstring>(++shortcut_count);
659 tomenu.add(MenuItem(MenuItem::Command, label,
660 FuncRequest(toc_list[i].action())));
663 TocBackend::Toc::size_type pos = from;
665 TocBackend::Toc::size_type new_pos = pos + 1;
666 while (new_pos < to &&
667 toc_list[new_pos].depth() > depth)
670 docstring label(4 * max(0, toc_list[pos].depth() - depth), ' ');
671 label += limit_string_length(toc_list[pos].str());
672 if (toc_list[pos].depth() == depth &&
673 shortcut_count < 9) {
674 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
675 label += char_type('|') + convert<docstring>(++shortcut_count);
677 if (new_pos == pos + 1) {
678 tomenu.add(MenuItem(MenuItem::Command,
679 label, FuncRequest(toc_list[pos].action())));
681 MenuItem item(MenuItem::Submenu, label);
682 item.submenu(new Menu);
683 expandToc2(*item.submenu(),
684 toc_list, pos, new_pos, depth + 1);
693 void expandToc(Menu & tomenu, Buffer const * buf)
695 // To make things very cleanly, we would have to pass buf to
696 // all MenuItem constructors and to expandToc2. However, we
697 // know that all the entries in a TOC will be have status_ ==
698 // OK, so we avoid this unnecessary overhead (JMarc)
701 tomenu.add(MenuItem(MenuItem::Command,
702 _("No Documents Open!"),
703 FuncRequest(LFUN_NOACTION)));
707 FloatList const & floatlist = buf->params().getLyXTextClass().floats();
708 TocBackend::TocList const & toc_list = buf->tocBackend().tocs();
709 TocBackend::TocList::const_iterator cit = toc_list.begin();
710 TocBackend::TocList::const_iterator end = toc_list.end();
711 for (; cit != end; ++cit) {
713 if (cit->first == "tableofcontents")
716 // All the rest is for floats
717 auto_ptr<Menu> menu(new Menu);
718 TocBackend::Toc::const_iterator ccit = cit->second.begin();
719 TocBackend::Toc::const_iterator eend = cit->second.end();
720 for (; ccit != eend; ++ccit) {
721 docstring const label = limit_string_length(ccit->str());
722 menu->add(MenuItem(MenuItem::Command,
724 FuncRequest(ccit->action())));
726 string const & floatName = floatlist.getType(cit->first).listName();
727 MenuItem item(MenuItem::Submenu, _(floatName));
728 item.submenu(menu.release());
733 cit = toc_list.find("tableofcontents");
735 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
736 _("No Table of contents"),
739 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
744 void expandPasteRecent(Menu & tomenu, Buffer const * buf)
749 vector<docstring> const sel =
750 cap::availableSelections(*buf);
752 vector<docstring>::const_iterator cit = sel.begin();
753 vector<docstring>::const_iterator end = sel.end();
755 for (unsigned int index = 0; cit != end; ++cit, ++index) {
756 tomenu.add(MenuItem(MenuItem::Command, *cit,
757 FuncRequest(LFUN_PASTE, convert<string>(index))));
762 void expandToolbars(Menu & tomenu)
765 // extracts the toolbars from the backend
766 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
767 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
769 for (; cit != end; ++cit) {
770 docstring label = _(cit->gui_name);
771 // frontends are not supposed to turn on/off toolbars, if they can not
772 // update ToolbarBackend::flags. That is to say, ToolbarsBackend::flags
773 // should reflect the true state of toolbars.
775 // menu is displayed as
779 // in the case of auto.
780 if (cit->flags & ToolbarBackend::AUTO)
781 label += _(" (auto)");
782 tomenu.add(MenuItem(MenuItem::Command, label,
783 FuncRequest(LFUN_TOOLBAR_TOGGLE_STATE, cit->name)));
788 void expandBranches(Menu & tomenu, Buffer const * buf)
793 BufferParams const & params = buf->getMasterBuffer()->params();
795 BranchList::const_iterator cit = params.branchlist().begin();
796 BranchList::const_iterator end = params.branchlist().end();
798 for (int ii = 1; cit != end; ++cit, ++ii) {
799 docstring label = cit->getBranch();
801 label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
802 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
803 FuncRequest(LFUN_BRANCH_INSERT,
812 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
813 Buffer const * buf) const
818 for (Menu::const_iterator cit = frommenu.begin();
819 cit != frommenu.end() ; ++cit) {
820 switch (cit->kind()) {
821 case MenuItem::Lastfiles:
822 expandLastfiles(tomenu);
825 case MenuItem::Documents:
826 expandDocuments(tomenu);
829 case MenuItem::Bookmarks:
830 expandBookmarks(tomenu);
833 case MenuItem::ImportFormats:
834 case MenuItem::ViewFormats:
835 case MenuItem::UpdateFormats:
836 case MenuItem::ExportFormats:
837 expandFormats(cit->kind(), tomenu, buf);
840 case MenuItem::CharStyles:
841 expandCharStyleInsert(tomenu, buf);
844 case MenuItem::FloatListInsert:
845 expandFloatListInsert(tomenu, buf);
848 case MenuItem::FloatInsert:
849 expandFloatInsert(tomenu, buf);
852 case MenuItem::PasteRecent:
853 expandPasteRecent(tomenu, buf);
856 case MenuItem::Toolbars:
857 expandToolbars(tomenu);
860 case MenuItem::Branches:
861 expandBranches(tomenu, buf);
865 expandToc(tomenu, buf);
868 case MenuItem::Submenu: {
870 item.submenu(new Menu(cit->submenuname()));
871 expand(getMenu(cit->submenuname()),
872 *item.submenu(), buf);
873 tomenu.addWithStatusCheck(item);
877 case MenuItem::Separator:
878 tomenu.addWithStatusCheck(*cit);
881 case MenuItem::Command:
883 || !specialmenu_->hasFunc(cit->func()))
884 tomenu.addWithStatusCheck(*cit);
888 // we do not want the menu to end with a separator
890 && tomenu.items_.back().kind() == MenuItem::Separator)
891 tomenu.items_.pop_back();
893 // Check whether the shortcuts are unique
894 tomenu.checkShortcuts();
898 void MenuBackend::read(LyXLex & lex)
907 struct keyword_item menutags[md_last - 1] = {
908 { "end", md_endmenuset },
910 { "menubar", md_menubar }
914 if (compare_ascii_no_case(lex.getString(), "menuset")) {
915 lyxerr << "Menubackend::read: ERROR wrong token:`"
916 << lex.getString() << '\'' << endl;
919 lex.pushTable(menutags, md_last - 1);
920 if (lyxerr.debugging(Debug::PARSER))
921 lex.printTable(lyxerr);
925 while (lex.isOK() && !quit) {
932 docstring const name = from_utf8(lex.getString());
934 getMenu(name).read(lex);
946 lex.printError("menubackend::read: "
947 "Unknown menu tag: `$$Token'");
955 void MenuBackend::add(Menu const & menu)
957 menulist_.push_back(menu);
961 bool MenuBackend::hasMenu(docstring const & name) const
963 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
967 Menu const & MenuBackend::getMenu(docstring const & name) const
969 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
971 lyxerr << "No submenu named " << to_utf8(name) << endl;
972 BOOST_ASSERT(cit != end());
977 Menu & MenuBackend::getMenu(docstring const & name)
979 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
981 lyxerr << "No submenu named " << to_utf8(name) << endl;
982 BOOST_ASSERT(it != end());
987 Menu const & MenuBackend::getMenubar() const