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
40 #include "support/filetools.h"
41 #include "support/lstrings.h"
42 #include "support/convert.h"
44 #include <boost/bind.hpp>
51 using support::compare_no_case;
52 using support::compare_ascii_no_case;
53 using support::contains;
54 using support::makeDisplayPath;
71 class MenuNamesEqual : public std::unary_function<Menu, bool> {
73 MenuNamesEqual(docstring const & name)
75 bool operator()(Menu const & menu) const
77 return menu.name() == name_;
86 // This is the global menu definition
87 MenuBackend menubackend;
90 MenuItem::MenuItem(Kind kind)
91 : kind_(kind), optional_(false)
95 MenuItem::MenuItem(Kind kind, docstring const & label,
96 docstring const & submenu, bool optional)
97 : kind_(kind), label_(label),
98 submenuname_(submenu), optional_(optional)
100 BOOST_ASSERT(kind == Submenu);
104 MenuItem::MenuItem(Kind kind, docstring const & label,
105 FuncRequest const & func, bool optional)
106 : kind_(kind), label_(label), func_(func), optional_(optional)
108 func_.origin = FuncRequest::UI;
112 MenuItem::~MenuItem()
116 void MenuItem::submenu(Menu * menu)
118 submenu_.reset(menu);
122 docstring const MenuItem::label() const
124 return token(label_, char_type('|'), 0);
128 docstring const MenuItem::shortcut() const
130 return token(label_, char_type('|'), 1);
134 docstring const MenuItem::binding() const
136 if (kind_ != Command)
139 // Get the keys bound to this action, but keep only the
141 kb_keymap::Bindings bindings = theTopLevelKeymap().findbindings(func_);
143 if (bindings.size()) {
144 return from_utf8(bindings.begin()->print());
148 << lyxaction.getActionName(func_.action)
149 << '(' << to_utf8(func_.argument()) << ')' << endl;
156 Menu & Menu::add(MenuItem const & i)
163 Menu & Menu::addWithStatusCheck(MenuItem const & i)
167 case MenuItem::Command: {
168 FuncStatus status = lyx::getStatus(i.func());
169 if (status.unknown() || (!status.enabled() && i.optional()))
172 items_.back().status(status);
176 case MenuItem::Submenu: {
178 bool enabled = false;
179 for (const_iterator cit = i.submenu()->begin();
180 cit != i.submenu()->end(); ++cit) {
181 if ((cit->kind() == MenuItem::Command
182 || cit->kind() == MenuItem::Submenu)
183 && cit->status().enabled()) {
188 if (enabled || !i.optional()) {
190 items_.back().status().enabled(enabled);
198 case MenuItem::Separator:
200 && items_.back().kind() != MenuItem::Separator)
212 Menu & Menu::read(LyXLex & lex)
237 struct keyword_item menutags[md_last - 1] = {
238 { "branches", md_branches },
239 { "charstyles", md_charstyles },
240 { "documents", md_documents },
241 { "bookmarks", md_bookmarks },
242 { "end", md_endmenu },
243 { "exportformats", md_exportformats },
244 { "floatinsert", md_floatinsert },
245 { "floatlistinsert", md_floatlistinsert },
246 { "importformats", md_importformats },
248 { "lastfiles", md_lastfiles },
249 { "optitem", md_optitem },
250 { "optsubmenu", md_optsubmenu },
251 { "pasterecent", md_pasterecent },
252 { "separator", md_separator },
253 { "submenu", md_submenu },
255 { "updateformats", md_updateformats },
256 { "viewformats", md_viewformats }
259 lex.pushTable(menutags, md_last - 1);
260 if (lyxerr.debugging(Debug::PARSER))
261 lex.printTable(lyxerr);
264 bool optional = false;
266 while (lex.isOK() && !quit) {
270 // fallback to md_item
273 docstring const name = _(lex.getString());
275 string const command = lex.getString();
276 FuncRequest func = lyxaction.lookupFunc(command);
277 add(MenuItem(MenuItem::Command, name, func, optional));
283 add(MenuItem(MenuItem::Separator));
287 add(MenuItem(MenuItem::Lastfiles));
291 add(MenuItem(MenuItem::CharStyles));
295 add(MenuItem(MenuItem::Documents));
299 add(MenuItem(MenuItem::Bookmarks));
303 add(MenuItem(MenuItem::Toc));
307 add(MenuItem(MenuItem::ViewFormats));
310 case md_updateformats:
311 add(MenuItem(MenuItem::UpdateFormats));
314 case md_exportformats:
315 add(MenuItem(MenuItem::ExportFormats));
318 case md_importformats:
319 add(MenuItem(MenuItem::ImportFormats));
322 case md_floatlistinsert:
323 add(MenuItem(MenuItem::FloatListInsert));
327 add(MenuItem(MenuItem::FloatInsert));
331 add(MenuItem(MenuItem::PasteRecent));
335 add(MenuItem(MenuItem::Branches));
340 // fallback to md_submenu
343 docstring const mlabel = _(lex.getString());
345 docstring const mname = from_utf8(lex.getString());
346 add(MenuItem(MenuItem::Submenu, mlabel, mname,
357 lex.printError("Menu::read: "
358 "Unknown menu tag: `$$Token'");
367 MenuItem const & Menu::operator[](size_type i) const
373 bool Menu::hasFunc(FuncRequest const & func) const
375 return find_if(begin(), end(),
376 bind(std::equal_to<FuncRequest>(),
377 bind(&MenuItem::func, _1),
381 void Menu::checkShortcuts() const
383 // This is a quadratic algorithm, but we do not care because
384 // menus are short enough
385 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
386 docstring shortcut = it1->shortcut();
387 if (shortcut.empty())
389 if (!contains(it1->label(), shortcut))
390 lyxerr << "Menu warning: menu entry \""
391 << to_utf8(it1->label())
392 << "\" does not contain shortcut `"
393 << to_utf8(shortcut) << "'." << endl;
394 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
395 if (!compare_no_case(it2->shortcut(), shortcut)) {
396 lyxerr << "Menu warning: menu entries "
397 << '"' << to_utf8(it1->fulllabel())
398 << "\" and \"" << to_utf8(it2->fulllabel())
399 << "\" share the same shortcut."
407 void MenuBackend::specialMenu(docstring const &name)
410 specialmenu_ = &getMenu(name);
416 class compare_format {
418 bool operator()(Format const * p1, Format const * p2) {
423 docstring const limit_string_length(docstring const & str)
425 docstring::size_type const max_item_length = 45;
427 if (str.size() > max_item_length)
428 return str.substr(0, max_item_length - 3) + "...";
434 void expandLastfiles(Menu & tomenu)
436 lyx::LastFilesSection::LastFiles const & lf = LyX::cref().session().lastFiles().lastFiles();
437 lyx::LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
441 for (; lfit != lf.end() && ii < 10; ++lfit, ++ii) {
442 docstring const label = convert<docstring>(ii) + ". "
443 + makeDisplayPath((*lfit), 30)
444 + char_type('|') + convert<docstring>(ii);
445 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))));
450 void expandDocuments(Menu & tomenu)
452 typedef vector<string> Strings;
453 Strings const names = theBufferList().getFileNames();
456 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
457 FuncRequest(LFUN_NOACTION)));
462 Strings::const_iterator docit = names.begin();
463 Strings::const_iterator end = names.end();
464 for (; docit != end; ++docit, ++ii) {
465 docstring label = makeDisplayPath(*docit, 20);
467 label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
468 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BUFFER_SWITCH, *docit)));
473 void expandBookmarks(Menu & tomenu)
475 lyx::BookmarksSection const & bm = LyX::cref().session().bookmarks();
477 for (size_t i = 1; i <= bm.size(); ++i) {
479 docstring const label = convert<docstring>(i) + ". "
480 + makeDisplayPath(bm.bookmark(i).filename, 20)
481 + char_type('|') + convert<docstring>(i);
482 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BOOKMARK_GOTO,
483 convert<docstring>(i))));
489 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
491 if (!buf && kind != MenuItem::ImportFormats) {
492 tomenu.add(MenuItem(MenuItem::Command,
493 _("No Documents Open!"),
494 FuncRequest(LFUN_NOACTION)));
498 typedef vector<Format const *> Formats;
503 case MenuItem::ImportFormats:
504 formats = Importer::GetImportableFormats();
505 action = LFUN_BUFFER_IMPORT;
507 case MenuItem::ViewFormats:
508 formats = Exporter::getExportableFormats(*buf, true);
509 action = LFUN_BUFFER_VIEW;
511 case MenuItem::UpdateFormats:
512 formats = Exporter::getExportableFormats(*buf, true);
513 action = LFUN_BUFFER_UPDATE;
516 formats = Exporter::getExportableFormats(*buf, false);
517 action = LFUN_BUFFER_EXPORT;
519 sort(formats.begin(), formats.end(), compare_format());
521 Formats::const_iterator fit = formats.begin();
522 Formats::const_iterator end = formats.end();
523 for (; fit != end ; ++fit) {
526 docstring label = from_utf8((*fit)->prettyname());
529 case MenuItem::ImportFormats:
530 if ((*fit)->name() == "text")
531 label = _("Plain Text as Lines");
532 else if ((*fit)->name() == "textparagraph")
533 label = _("Plain Text as Paragraphs");
536 case MenuItem::ViewFormats:
537 case MenuItem::ExportFormats:
538 case MenuItem::UpdateFormats:
539 if (!(*fit)->documentFormat())
546 if (!(*fit)->shortcut().empty())
547 label += char_type('|') + from_utf8((*fit)->shortcut());
550 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
551 FuncRequest(action, (*fit)->name())));
553 tomenu.add(MenuItem(MenuItem::Command, label,
554 FuncRequest(action, (*fit)->name())));
559 void expandFloatListInsert(Menu & tomenu, Buffer const * buf)
562 tomenu.add(MenuItem(MenuItem::Command,
563 _("No Documents Open!"),
564 FuncRequest(LFUN_NOACTION)));
568 FloatList const & floats =
569 buf->params().getLyXTextClass().floats();
570 FloatList::const_iterator cit = floats.begin();
571 FloatList::const_iterator end = floats.end();
572 for (; cit != end; ++cit) {
573 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
574 _(cit->second.listName()),
575 FuncRequest(LFUN_FLOAT_LIST,
576 cit->second.type())));
581 void expandFloatInsert(Menu & tomenu, Buffer const * buf)
584 tomenu.add(MenuItem(MenuItem::Command,
585 _("No Documents Open!"),
586 FuncRequest(LFUN_NOACTION)));
590 FloatList const & floats =
591 buf->params().getLyXTextClass().floats();
592 FloatList::const_iterator cit = floats.begin();
593 FloatList::const_iterator end = floats.end();
594 for (; cit != end; ++cit) {
596 docstring const label = _(cit->second.name());
597 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
598 FuncRequest(LFUN_FLOAT_INSERT,
599 cit->second.type())));
604 void expandCharStyleInsert(Menu & tomenu, Buffer const * buf)
607 tomenu.add(MenuItem(MenuItem::Command,
608 _("No Documents Open!"),
609 FuncRequest(LFUN_NOACTION)));
612 CharStyles & charstyles =
613 buf->params().getLyXTextClass().charstyles();
614 CharStyles::iterator cit = charstyles.begin();
615 CharStyles::iterator end = charstyles.end();
616 for (; cit != end; ++cit) {
617 docstring const label = from_utf8(cit->name);
618 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
619 FuncRequest(LFUN_CHARSTYLE_INSERT,
625 Menu::size_type const max_number_of_items = 25;
627 void expandToc2(Menu & tomenu,
628 lyx::toc::Toc const & toc_list,
629 lyx::toc::Toc::size_type from,
630 lyx::toc::Toc::size_type to, int depth)
632 int shortcut_count = 0;
634 // check whether depth is smaller than the smallest depth in toc.
635 int min_depth = 1000;
636 for (lyx::toc::Toc::size_type i = from; i < to; ++i)
637 min_depth = std::min(min_depth, toc_list[i].depth());
638 if (min_depth > depth)
642 if (to - from <= max_number_of_items) {
643 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
644 docstring label(4 * max(0, toc_list[i].depth() - depth), char_type(' '));
645 label += limit_string_length(toc_list[i].str());
646 if (toc_list[i].depth() == depth
647 && shortcut_count < 9) {
648 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
649 label += char_type('|') + convert<docstring>(++shortcut_count);
651 tomenu.add(MenuItem(MenuItem::Command, label,
652 FuncRequest(toc_list[i].action())));
655 lyx::toc::Toc::size_type pos = from;
657 lyx::toc::Toc::size_type new_pos = pos + 1;
658 while (new_pos < to &&
659 toc_list[new_pos].depth() > depth)
662 docstring label(4 * max(0, toc_list[pos].depth() - depth), ' ');
663 label += limit_string_length(toc_list[pos].str());
664 if (toc_list[pos].depth() == depth &&
665 shortcut_count < 9) {
666 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
667 label += char_type('|') + convert<docstring>(++shortcut_count);
669 if (new_pos == pos + 1) {
670 tomenu.add(MenuItem(MenuItem::Command,
671 label, FuncRequest(toc_list[pos].action())));
673 MenuItem item(MenuItem::Submenu, label);
674 item.submenu(new Menu);
675 expandToc2(*item.submenu(),
676 toc_list, pos, new_pos, depth + 1);
685 void expandToc(Menu & tomenu, Buffer const * buf)
687 // To make things very cleanly, we would have to pass buf to
688 // all MenuItem constructors and to expandToc2. However, we
689 // know that all the entries in a TOC will be have status_ ==
690 // OK, so we avoid this unnecessary overhead (JMarc)
693 tomenu.add(MenuItem(MenuItem::Command,
694 _("No Documents Open!"),
695 FuncRequest(LFUN_NOACTION)));
699 FloatList const & floatlist = buf->params().getLyXTextClass().floats();
700 lyx::toc::TocList const & toc_list = lyx::toc::getTocList(*buf);
701 lyx::toc::TocList::const_iterator cit = toc_list.begin();
702 lyx::toc::TocList::const_iterator end = toc_list.end();
703 for (; cit != end; ++cit) {
705 if (cit->first == "TOC")
708 // All the rest is for floats
709 auto_ptr<Menu> menu(new Menu);
710 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
711 lyx::toc::Toc::const_iterator eend = cit->second.end();
712 for (; ccit != eend; ++ccit) {
713 docstring const label = limit_string_length(ccit->str());
714 menu->add(MenuItem(MenuItem::Command,
716 FuncRequest(ccit->action())));
718 string const & floatName = floatlist.getType(cit->first).listName();
719 MenuItem item(MenuItem::Submenu, _(floatName));
720 item.submenu(menu.release());
725 cit = toc_list.find("TOC");
727 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
728 _("No Table of contents"),
731 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
736 void expandPasteRecent(Menu & tomenu, Buffer const * buf)
741 vector<docstring> const sel =
742 cap::availableSelections(*buf);
744 vector<docstring>::const_iterator cit = sel.begin();
745 vector<docstring>::const_iterator end = sel.end();
747 for (unsigned int index = 0; cit != end; ++cit, ++index) {
748 tomenu.add(MenuItem(MenuItem::Command, *cit,
749 FuncRequest(LFUN_PASTE, convert<string>(index))));
754 void expandBranches(Menu & tomenu, Buffer const * buf)
759 BufferParams const & params = buf->getMasterBuffer()->params();
761 BranchList::const_iterator cit = params.branchlist().begin();
762 BranchList::const_iterator end = params.branchlist().end();
764 for (int ii = 1; cit != end; ++cit, ++ii) {
765 docstring label = from_utf8(cit->getBranch());
767 label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
768 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
769 FuncRequest(LFUN_BRANCH_INSERT,
778 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
779 Buffer const * buf) const
784 for (Menu::const_iterator cit = frommenu.begin();
785 cit != frommenu.end() ; ++cit) {
786 switch (cit->kind()) {
787 case MenuItem::Lastfiles:
788 expandLastfiles(tomenu);
791 case MenuItem::Documents:
792 expandDocuments(tomenu);
795 case MenuItem::Bookmarks:
796 expandBookmarks(tomenu);
799 case MenuItem::ImportFormats:
800 case MenuItem::ViewFormats:
801 case MenuItem::UpdateFormats:
802 case MenuItem::ExportFormats:
803 expandFormats(cit->kind(), tomenu, buf);
806 case MenuItem::CharStyles:
807 expandCharStyleInsert(tomenu, buf);
810 case MenuItem::FloatListInsert:
811 expandFloatListInsert(tomenu, buf);
814 case MenuItem::FloatInsert:
815 expandFloatInsert(tomenu, buf);
818 case MenuItem::PasteRecent:
819 expandPasteRecent(tomenu, buf);
822 case MenuItem::Branches:
823 expandBranches(tomenu, buf);
827 expandToc(tomenu, buf);
830 case MenuItem::Submenu: {
832 item.submenu(new Menu(cit->submenuname()));
833 expand(getMenu(cit->submenuname()),
834 *item.submenu(), buf);
835 tomenu.addWithStatusCheck(item);
839 case MenuItem::Separator:
840 tomenu.addWithStatusCheck(*cit);
843 case MenuItem::Command:
845 || !specialmenu_->hasFunc(cit->func()))
846 tomenu.addWithStatusCheck(*cit);
850 // we do not want the menu to end with a separator
852 && tomenu.items_.back().kind() == MenuItem::Separator)
853 tomenu.items_.pop_back();
855 // Check whether the shortcuts are unique
856 tomenu.checkShortcuts();
860 void MenuBackend::read(LyXLex & lex)
869 struct keyword_item menutags[md_last - 1] = {
870 { "end", md_endmenuset },
872 { "menubar", md_menubar }
876 if (compare_ascii_no_case(lex.getString(), "menuset")) {
877 lyxerr << "Menubackend::read: ERROR wrong token:`"
878 << lex.getString() << '\'' << endl;
881 lex.pushTable(menutags, md_last - 1);
882 if (lyxerr.debugging(Debug::PARSER))
883 lex.printTable(lyxerr);
887 while (lex.isOK() && !quit) {
894 docstring const name = from_utf8(lex.getString());
896 getMenu(name).read(lex);
908 lex.printError("menubackend::read: "
909 "Unknown menu tag: `$$Token'");
917 void MenuBackend::add(Menu const & menu)
919 menulist_.push_back(menu);
923 bool MenuBackend::hasMenu(docstring const & name) const
925 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
929 Menu const & MenuBackend::getMenu(docstring const & name) const
931 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
933 lyxerr << "No submenu named " << to_utf8(name) << endl;
934 BOOST_ASSERT(cit != end());
939 Menu & MenuBackend::getMenu(docstring const & name)
941 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
943 lyxerr << "No submenu named " << to_utf8(name) << endl;
944 BOOST_ASSERT(it != end());
949 Menu const & MenuBackend::getMenubar() const