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)
236 struct keyword_item menutags[md_last - 1] = {
237 { "branches", md_branches },
238 { "charstyles", md_charstyles },
239 { "documents", md_documents },
240 { "end", md_endmenu },
241 { "exportformats", md_exportformats },
242 { "floatinsert", md_floatinsert },
243 { "floatlistinsert", md_floatlistinsert },
244 { "importformats", md_importformats },
246 { "lastfiles", md_lastfiles },
247 { "optitem", md_optitem },
248 { "optsubmenu", md_optsubmenu },
249 { "pasterecent", md_pasterecent },
250 { "separator", md_separator },
251 { "submenu", md_submenu },
253 { "updateformats", md_updateformats },
254 { "viewformats", md_viewformats }
257 lex.pushTable(menutags, md_last - 1);
258 if (lyxerr.debugging(Debug::PARSER))
259 lex.printTable(lyxerr);
262 bool optional = false;
264 while (lex.isOK() && !quit) {
268 // fallback to md_item
271 docstring const name = _(lex.getString());
273 string const command = lex.getString();
274 FuncRequest func = lyxaction.lookupFunc(command);
275 add(MenuItem(MenuItem::Command, name, func, optional));
281 add(MenuItem(MenuItem::Separator));
285 add(MenuItem(MenuItem::Lastfiles));
289 add(MenuItem(MenuItem::CharStyles));
293 add(MenuItem(MenuItem::Documents));
297 add(MenuItem(MenuItem::Toc));
301 add(MenuItem(MenuItem::ViewFormats));
304 case md_updateformats:
305 add(MenuItem(MenuItem::UpdateFormats));
308 case md_exportformats:
309 add(MenuItem(MenuItem::ExportFormats));
312 case md_importformats:
313 add(MenuItem(MenuItem::ImportFormats));
316 case md_floatlistinsert:
317 add(MenuItem(MenuItem::FloatListInsert));
321 add(MenuItem(MenuItem::FloatInsert));
325 add(MenuItem(MenuItem::PasteRecent));
329 add(MenuItem(MenuItem::Branches));
334 // fallback to md_submenu
337 docstring const mlabel = _(lex.getString());
339 docstring const mname = from_utf8(lex.getString());
340 add(MenuItem(MenuItem::Submenu, mlabel, mname,
351 lex.printError("Menu::read: "
352 "Unknown menu tag: `$$Token'");
361 MenuItem const & Menu::operator[](size_type i) const
367 bool Menu::hasFunc(FuncRequest const & func) const
369 return find_if(begin(), end(),
370 bind(std::equal_to<FuncRequest>(),
371 bind(&MenuItem::func, _1),
375 void Menu::checkShortcuts() const
377 // This is a quadratic algorithm, but we do not care because
378 // menus are short enough
379 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
380 docstring shortcut = it1->shortcut();
381 if (shortcut.empty())
383 if (!contains(it1->label(), shortcut))
384 lyxerr << "Menu warning: menu entry \""
385 << to_utf8(it1->label())
386 << "\" does not contain shortcut `"
387 << to_utf8(shortcut) << "'." << endl;
388 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
389 if (!compare_no_case(it2->shortcut(), shortcut)) {
390 lyxerr << "Menu warning: menu entries "
391 << '"' << to_utf8(it1->fulllabel())
392 << "\" and \"" << to_utf8(it2->fulllabel())
393 << "\" share the same shortcut."
401 void MenuBackend::specialMenu(docstring const &name)
404 specialmenu_ = &getMenu(name);
410 class compare_format {
412 bool operator()(Format const * p1, Format const * p2) {
417 docstring const limit_string_length(docstring const & str)
419 docstring::size_type const max_item_length = 45;
421 if (str.size() > max_item_length)
422 return str.substr(0, max_item_length - 3) + "...";
428 void expandLastfiles(Menu & tomenu)
430 lyx::Session::LastFiles const & lf = LyX::cref().session().lastFiles();
431 lyx::Session::LastFiles::const_iterator lfit = lf.begin();
435 for (; lfit != lf.end() && ii < 10; ++lfit, ++ii) {
436 docstring const label = convert<docstring>(ii) + ". "
437 + makeDisplayPath((*lfit), 30)
438 + char_type('|') + convert<docstring>(ii);
439 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))));
444 void expandDocuments(Menu & tomenu)
446 typedef vector<string> Strings;
447 Strings const names = theBufferList().getFileNames();
450 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
451 FuncRequest(LFUN_NOACTION)));
456 Strings::const_iterator docit = names.begin();
457 Strings::const_iterator end = names.end();
458 for (; docit != end; ++docit, ++ii) {
459 docstring label = makeDisplayPath(*docit, 20);
461 label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
462 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BUFFER_SWITCH, *docit)));
467 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
469 if (!buf && kind != MenuItem::ImportFormats) {
470 tomenu.add(MenuItem(MenuItem::Command,
471 _("No Documents Open!"),
472 FuncRequest(LFUN_NOACTION)));
476 typedef vector<Format const *> Formats;
481 case MenuItem::ImportFormats:
482 formats = Importer::GetImportableFormats();
483 action = LFUN_BUFFER_IMPORT;
485 case MenuItem::ViewFormats:
486 formats = Exporter::getExportableFormats(*buf, true);
487 action = LFUN_BUFFER_VIEW;
489 case MenuItem::UpdateFormats:
490 formats = Exporter::getExportableFormats(*buf, true);
491 action = LFUN_BUFFER_UPDATE;
494 formats = Exporter::getExportableFormats(*buf, false);
495 action = LFUN_BUFFER_EXPORT;
497 sort(formats.begin(), formats.end(), compare_format());
499 Formats::const_iterator fit = formats.begin();
500 Formats::const_iterator end = formats.end();
501 for (; fit != end ; ++fit) {
504 docstring label = from_utf8((*fit)->prettyname());
507 case MenuItem::ImportFormats:
508 if ((*fit)->name() == "text")
509 label = _("Plain Text as Lines");
510 else if ((*fit)->name() == "textparagraph")
511 label = _("Plain Text as Paragraphs");
514 case MenuItem::ViewFormats:
515 case MenuItem::ExportFormats:
516 case MenuItem::UpdateFormats:
517 if (!(*fit)->documentFormat())
524 if (!(*fit)->shortcut().empty())
525 label += char_type('|') + from_utf8((*fit)->shortcut());
528 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
529 FuncRequest(action, (*fit)->name())));
531 tomenu.add(MenuItem(MenuItem::Command, label,
532 FuncRequest(action, (*fit)->name())));
537 void expandFloatListInsert(Menu & tomenu, Buffer const * buf)
540 tomenu.add(MenuItem(MenuItem::Command,
541 _("No Documents Open!"),
542 FuncRequest(LFUN_NOACTION)));
546 FloatList const & floats =
547 buf->params().getLyXTextClass().floats();
548 FloatList::const_iterator cit = floats.begin();
549 FloatList::const_iterator end = floats.end();
550 for (; cit != end; ++cit) {
551 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
552 _(cit->second.listName()),
553 FuncRequest(LFUN_FLOAT_LIST,
554 cit->second.type())));
559 void expandFloatInsert(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) {
574 docstring const label = _(cit->second.name());
575 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
576 FuncRequest(LFUN_FLOAT_INSERT,
577 cit->second.type())));
582 void expandCharStyleInsert(Menu & tomenu, Buffer const * buf)
585 tomenu.add(MenuItem(MenuItem::Command,
586 _("No Documents Open!"),
587 FuncRequest(LFUN_NOACTION)));
590 CharStyles & charstyles =
591 buf->params().getLyXTextClass().charstyles();
592 CharStyles::iterator cit = charstyles.begin();
593 CharStyles::iterator end = charstyles.end();
594 for (; cit != end; ++cit) {
595 docstring const label = from_utf8(cit->name);
596 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
597 FuncRequest(LFUN_CHARSTYLE_INSERT,
603 Menu::size_type const max_number_of_items = 25;
605 void expandToc2(Menu & tomenu,
606 lyx::toc::Toc const & toc_list,
607 lyx::toc::Toc::size_type from,
608 lyx::toc::Toc::size_type to, int depth)
610 int shortcut_count = 0;
612 // check whether depth is smaller than the smallest depth in toc.
613 int min_depth = 1000;
614 for (lyx::toc::Toc::size_type i = from; i < to; ++i)
615 min_depth = std::min(min_depth, toc_list[i].depth());
616 if (min_depth > depth)
620 if (to - from <= max_number_of_items) {
621 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
622 docstring label(4 * max(0, toc_list[i].depth() - depth), char_type(' '));
623 label += limit_string_length(toc_list[i].str());
624 if (toc_list[i].depth() == depth
625 && shortcut_count < 9) {
626 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
627 label += char_type('|') + convert<docstring>(++shortcut_count);
629 tomenu.add(MenuItem(MenuItem::Command, label,
630 FuncRequest(toc_list[i].action())));
633 lyx::toc::Toc::size_type pos = from;
635 lyx::toc::Toc::size_type new_pos = pos + 1;
636 while (new_pos < to &&
637 toc_list[new_pos].depth() > depth)
640 docstring label(4 * max(0, toc_list[pos].depth() - depth), ' ');
641 label += limit_string_length(toc_list[pos].str());
642 if (toc_list[pos].depth() == depth &&
643 shortcut_count < 9) {
644 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
645 label += char_type('|') + convert<docstring>(++shortcut_count);
647 if (new_pos == pos + 1) {
648 tomenu.add(MenuItem(MenuItem::Command,
649 label, FuncRequest(toc_list[pos].action())));
651 MenuItem item(MenuItem::Submenu, label);
652 item.submenu(new Menu);
653 expandToc2(*item.submenu(),
654 toc_list, pos, new_pos, depth + 1);
663 void expandToc(Menu & tomenu, Buffer const * buf)
665 // To make things very cleanly, we would have to pass buf to
666 // all MenuItem constructors and to expandToc2. However, we
667 // know that all the entries in a TOC will be have status_ ==
668 // OK, so we avoid this unnecessary overhead (JMarc)
671 tomenu.add(MenuItem(MenuItem::Command,
672 _("No Documents Open!"),
673 FuncRequest(LFUN_NOACTION)));
677 FloatList const & floatlist = buf->params().getLyXTextClass().floats();
678 lyx::toc::TocList const & toc_list = lyx::toc::getTocList(*buf);
679 lyx::toc::TocList::const_iterator cit = toc_list.begin();
680 lyx::toc::TocList::const_iterator end = toc_list.end();
681 for (; cit != end; ++cit) {
683 if (cit->first == "TOC")
686 // All the rest is for floats
687 auto_ptr<Menu> menu(new Menu);
688 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
689 lyx::toc::Toc::const_iterator eend = cit->second.end();
690 for (; ccit != eend; ++ccit) {
691 docstring const label = limit_string_length(ccit->str());
692 menu->add(MenuItem(MenuItem::Command,
694 FuncRequest(ccit->action())));
696 string const & floatName = floatlist.getType(cit->first).listName();
697 MenuItem item(MenuItem::Submenu, _(floatName));
698 item.submenu(menu.release());
703 cit = toc_list.find("TOC");
705 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
706 _("No Table of contents"),
709 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
714 void expandPasteRecent(Menu & tomenu, Buffer const * buf)
719 vector<docstring> const sel =
720 cap::availableSelections(*buf);
722 vector<docstring>::const_iterator cit = sel.begin();
723 vector<docstring>::const_iterator end = sel.end();
725 for (unsigned int index = 0; cit != end; ++cit, ++index) {
726 tomenu.add(MenuItem(MenuItem::Command, *cit,
727 FuncRequest(LFUN_PASTE, convert<string>(index))));
732 void expandBranches(Menu & tomenu, Buffer const * buf)
737 BufferParams const & params = buf->getMasterBuffer()->params();
739 BranchList::const_iterator cit = params.branchlist().begin();
740 BranchList::const_iterator end = params.branchlist().end();
742 for (int ii = 1; cit != end; ++cit, ++ii) {
743 docstring label = from_utf8(cit->getBranch());
745 label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
746 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
747 FuncRequest(LFUN_BRANCH_INSERT,
756 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
757 Buffer const * buf) const
762 for (Menu::const_iterator cit = frommenu.begin();
763 cit != frommenu.end() ; ++cit) {
764 switch (cit->kind()) {
765 case MenuItem::Lastfiles:
766 expandLastfiles(tomenu);
769 case MenuItem::Documents:
770 expandDocuments(tomenu);
773 case MenuItem::ImportFormats:
774 case MenuItem::ViewFormats:
775 case MenuItem::UpdateFormats:
776 case MenuItem::ExportFormats:
777 expandFormats(cit->kind(), tomenu, buf);
780 case MenuItem::CharStyles:
781 expandCharStyleInsert(tomenu, buf);
784 case MenuItem::FloatListInsert:
785 expandFloatListInsert(tomenu, buf);
788 case MenuItem::FloatInsert:
789 expandFloatInsert(tomenu, buf);
792 case MenuItem::PasteRecent:
793 expandPasteRecent(tomenu, buf);
796 case MenuItem::Branches:
797 expandBranches(tomenu, buf);
801 expandToc(tomenu, buf);
804 case MenuItem::Submenu: {
806 item.submenu(new Menu(cit->submenuname()));
807 expand(getMenu(cit->submenuname()),
808 *item.submenu(), buf);
809 tomenu.addWithStatusCheck(item);
813 case MenuItem::Separator:
814 tomenu.addWithStatusCheck(*cit);
817 case MenuItem::Command:
819 || !specialmenu_->hasFunc(cit->func()))
820 tomenu.addWithStatusCheck(*cit);
824 // we do not want the menu to end with a separator
826 && tomenu.items_.back().kind() == MenuItem::Separator)
827 tomenu.items_.pop_back();
829 // Check whether the shortcuts are unique
830 tomenu.checkShortcuts();
834 void MenuBackend::read(LyXLex & lex)
843 struct keyword_item menutags[md_last - 1] = {
844 { "end", md_endmenuset },
846 { "menubar", md_menubar }
850 if (compare_ascii_no_case(lex.getString(), "menuset")) {
851 lyxerr << "Menubackend::read: ERROR wrong token:`"
852 << lex.getString() << '\'' << endl;
855 lex.pushTable(menutags, md_last - 1);
856 if (lyxerr.debugging(Debug::PARSER))
857 lex.printTable(lyxerr);
861 while (lex.isOK() && !quit) {
868 docstring const name = from_utf8(lex.getString());
870 getMenu(name).read(lex);
882 lex.printError("menubackend::read: "
883 "Unknown menu tag: `$$Token'");
891 void MenuBackend::add(Menu const & menu)
893 menulist_.push_back(menu);
897 bool MenuBackend::hasMenu(docstring const & name) const
899 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
903 Menu const & MenuBackend::getMenu(docstring const & name) const
905 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
907 lyxerr << "No submenu named " << to_utf8(name) << endl;
908 BOOST_ASSERT(cit != end());
913 Menu & MenuBackend::getMenu(docstring const & name)
915 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
917 lyxerr << "No submenu named " << to_utf8(name) << endl;
918 BOOST_ASSERT(it != end());
923 Menu const & MenuBackend::getMenubar() const