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 "frontends/Application.h"
42 #include "support/filetools.h"
43 #include "support/lstrings.h"
44 #include "support/convert.h"
46 #include <boost/bind.hpp>
52 using lyx::support::compare_no_case;
53 using lyx::support::compare_ascii_no_case;
54 using lyx::support::contains;
55 using lyx::support::makeDisplayPath;
56 using lyx::support::token;
70 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
74 class MenuNamesEqual : public std::unary_function<Menu, bool> {
76 MenuNamesEqual(docstring const & name)
78 bool operator()(Menu const & menu) const
80 return menu.name() == name_;
89 // This is the global menu definition
90 MenuBackend menubackend;
93 MenuItem::MenuItem(Kind kind)
94 : kind_(kind), optional_(false)
98 MenuItem::MenuItem(Kind kind, docstring const & label,
99 docstring const & submenu, bool optional)
100 : kind_(kind), label_(label),
101 submenuname_(submenu), optional_(optional)
103 BOOST_ASSERT(kind == Submenu);
107 MenuItem::MenuItem(Kind kind, docstring const & label,
108 FuncRequest const & func, bool optional)
109 : kind_(kind), label_(label), func_(func), optional_(optional)
111 func_.origin = FuncRequest::UI;
115 MenuItem::~MenuItem()
119 void MenuItem::submenu(Menu * menu)
121 submenu_.reset(menu);
125 docstring const MenuItem::label() const
127 return token(label_, char_type('|'), 0);
131 docstring const MenuItem::shortcut() const
133 return token(label_, char_type('|'), 1);
137 docstring const MenuItem::binding() const
139 if (kind_ != Command)
142 // Get the keys bound to this action, but keep only the
144 kb_keymap::Bindings bindings = toplevel_keymap->findbindings(func_);
146 if (bindings.size()) {
147 return lyx::from_utf8(bindings.begin()->print());
151 << lyxaction.getActionName(func_.action)
152 << '(' << lyx::to_utf8(func_.argument()) << ')' << endl;
159 Menu & Menu::add(MenuItem const & i)
166 Menu & Menu::addWithStatusCheck(MenuItem const & i)
170 case MenuItem::Command: {
172 theApp->lyxFunc().getStatus(i.func());
174 || (!status.enabled() && i.optional()))
177 items_.back().status(status);
181 case MenuItem::Submenu: {
183 bool enabled = false;
184 for (const_iterator cit = i.submenu()->begin();
185 cit != i.submenu()->end(); ++cit) {
186 if ((cit->kind() == MenuItem::Command
187 || cit->kind() == MenuItem::Submenu)
188 && cit->status().enabled()) {
193 if (enabled || !i.optional()) {
195 items_.back().status().enabled(enabled);
203 case MenuItem::Separator:
205 && items_.back().kind() != MenuItem::Separator)
217 Menu & Menu::read(LyXLex & lex)
241 struct keyword_item menutags[md_last - 1] = {
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 { "updateformats", md_updateformats },
259 { "viewformats", md_viewformats }
262 lex.pushTable(menutags, md_last - 1);
263 if (lyxerr.debugging(Debug::PARSER))
264 lex.printTable(lyxerr);
267 bool optional = false;
269 while (lex.isOK() && !quit) {
273 // fallback to md_item
276 docstring const name = _(lex.getString());
278 string const command = lex.getString();
279 FuncRequest func = lyxaction.lookupFunc(command);
280 add(MenuItem(MenuItem::Command, name, func, optional));
286 add(MenuItem(MenuItem::Separator));
290 add(MenuItem(MenuItem::Lastfiles));
294 add(MenuItem(MenuItem::CharStyles));
298 add(MenuItem(MenuItem::Documents));
302 add(MenuItem(MenuItem::Toc));
306 add(MenuItem(MenuItem::ViewFormats));
309 case md_updateformats:
310 add(MenuItem(MenuItem::UpdateFormats));
313 case md_exportformats:
314 add(MenuItem(MenuItem::ExportFormats));
317 case md_importformats:
318 add(MenuItem(MenuItem::ImportFormats));
321 case md_floatlistinsert:
322 add(MenuItem(MenuItem::FloatListInsert));
326 add(MenuItem(MenuItem::FloatInsert));
330 add(MenuItem(MenuItem::PasteRecent));
334 add(MenuItem(MenuItem::Branches));
339 // fallback to md_submenu
342 docstring const mlabel = _(lex.getString());
344 docstring const mname = lyx::from_utf8(lex.getString());
345 add(MenuItem(MenuItem::Submenu, mlabel, mname,
356 lex.printError("Menu::read: "
357 "Unknown menu tag: `$$Token'");
366 MenuItem const & Menu::operator[](size_type i) const
372 bool Menu::hasFunc(FuncRequest const & func) const
374 return find_if(begin(), end(),
375 bind(std::equal_to<FuncRequest>(),
376 bind(&MenuItem::func, _1),
380 void Menu::checkShortcuts() const
382 // This is a quadratic algorithm, but we do not care because
383 // menus are short enough
384 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
385 docstring shortcut = it1->shortcut();
386 if (shortcut.empty())
388 if (!contains(it1->label(), shortcut))
389 lyxerr << "Menu warning: menu entry \""
390 << lyx::to_utf8(it1->label())
391 << "\" does not contain shortcut `"
392 << lyx::to_utf8(shortcut) << "'." << endl;
393 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
394 if (!compare_no_case(it2->shortcut(), shortcut)) {
395 lyxerr << "Menu warning: menu entries "
396 << '"' << lyx::to_utf8(it1->fulllabel())
397 << "\" and \"" << lyx::to_utf8(it2->fulllabel())
398 << "\" share the same shortcut."
406 void MenuBackend::specialMenu(docstring const &name)
409 specialmenu_ = &getMenu(name);
415 class compare_format {
417 bool operator()(Format const * p1, Format const * p2) {
422 string const limit_string_length(string const & str)
424 string::size_type const max_item_length = 45;
426 if (str.size() > max_item_length)
427 return str.substr(0, max_item_length - 3) + "...";
433 void expandLastfiles(Menu & tomenu)
435 lyx::Session::LastFiles const & lf = LyX::cref().session().lastFiles();
436 lyx::Session::LastFiles::const_iterator lfit = lf.begin();
440 for (; lfit != lf.end() && ii < 10; ++lfit, ++ii) {
441 docstring const label = convert<docstring>(ii) + lyx::from_ascii(". ")
442 + makeDisplayPath((*lfit), 30)
443 + char_type('|') + convert<docstring>(ii);
444 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))));
449 void expandDocuments(Menu & tomenu)
451 typedef vector<string> Strings;
452 Strings const names = theApp->bufferList().getFileNames();
455 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
456 FuncRequest(LFUN_NOACTION)));
461 Strings::const_iterator docit = names.begin();
462 Strings::const_iterator end = names.end();
463 for (; docit != end; ++docit, ++ii) {
464 docstring label = makeDisplayPath(*docit, 20);
466 label = convert<docstring>(ii) + lyx::from_ascii(". ") + label + char_type('|') + convert<docstring>(ii);
467 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BUFFER_SWITCH, *docit)));
472 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
474 if (!buf && kind != MenuItem::ImportFormats) {
475 tomenu.add(MenuItem(MenuItem::Command,
476 _("No Documents Open!"),
477 FuncRequest(LFUN_NOACTION)));
481 typedef vector<Format const *> Formats;
486 case MenuItem::ImportFormats:
487 formats = Importer::GetImportableFormats();
488 action = LFUN_BUFFER_IMPORT;
490 case MenuItem::ViewFormats:
491 formats = Exporter::getExportableFormats(*buf, true);
492 action = LFUN_BUFFER_VIEW;
494 case MenuItem::UpdateFormats:
495 formats = Exporter::getExportableFormats(*buf, true);
496 action = LFUN_BUFFER_UPDATE;
499 formats = Exporter::getExportableFormats(*buf, false);
500 action = LFUN_BUFFER_EXPORT;
502 sort(formats.begin(), formats.end(), compare_format());
504 Formats::const_iterator fit = formats.begin();
505 Formats::const_iterator end = formats.end();
506 for (; fit != end ; ++fit) {
509 docstring label = lyx::from_utf8((*fit)->prettyname());
512 case MenuItem::ImportFormats:
513 if ((*fit)->name() == "text")
514 label = _("Plain Text as Lines");
515 else if ((*fit)->name() == "textparagraph")
516 label = _("Plain Text as Paragraphs");
517 label += lyx::from_ascii("...");
519 case MenuItem::ViewFormats:
520 case MenuItem::ExportFormats:
521 case MenuItem::UpdateFormats:
522 if (!(*fit)->documentFormat())
529 if (!(*fit)->shortcut().empty())
530 label += char_type('|') + lyx::from_utf8((*fit)->shortcut());
533 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
534 FuncRequest(action, (*fit)->name())));
536 tomenu.add(MenuItem(MenuItem::Command, label,
537 FuncRequest(action, (*fit)->name())));
542 void expandFloatListInsert(Menu & tomenu, Buffer const * buf)
545 tomenu.add(MenuItem(MenuItem::Command,
546 _("No Documents Open!"),
547 FuncRequest(LFUN_NOACTION)));
551 FloatList const & floats =
552 buf->params().getLyXTextClass().floats();
553 FloatList::const_iterator cit = floats.begin();
554 FloatList::const_iterator end = floats.end();
555 for (; cit != end; ++cit) {
556 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
557 _(cit->second.listName()),
558 FuncRequest(LFUN_FLOAT_LIST,
559 cit->second.type())));
564 void expandFloatInsert(Menu & tomenu, Buffer const * buf)
567 tomenu.add(MenuItem(MenuItem::Command,
568 _("No Documents Open!"),
569 FuncRequest(LFUN_NOACTION)));
573 FloatList const & floats =
574 buf->params().getLyXTextClass().floats();
575 FloatList::const_iterator cit = floats.begin();
576 FloatList::const_iterator end = floats.end();
577 for (; cit != end; ++cit) {
579 docstring const label = _(cit->second.name());
580 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
581 FuncRequest(LFUN_FLOAT_INSERT,
582 cit->second.type())));
587 void expandCharStyleInsert(Menu & tomenu, Buffer const * buf)
590 tomenu.add(MenuItem(MenuItem::Command,
591 _("No Documents Open!"),
592 FuncRequest(LFUN_NOACTION)));
595 CharStyles & charstyles =
596 buf->params().getLyXTextClass().charstyles();
597 CharStyles::iterator cit = charstyles.begin();
598 CharStyles::iterator end = charstyles.end();
599 for (; cit != end; ++cit) {
600 docstring const label = lyx::from_utf8(cit->name);
601 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
602 FuncRequest(LFUN_CHARSTYLE_INSERT,
608 Menu::size_type const max_number_of_items = 25;
610 void expandToc2(Menu & tomenu,
611 lyx::toc::Toc const & toc_list,
612 lyx::toc::Toc::size_type from,
613 lyx::toc::Toc::size_type to, int depth)
615 int shortcut_count = 0;
617 // check whether depth is smaller than the smallest depth in toc.
618 int min_depth = 1000;
619 for (lyx::toc::Toc::size_type i = from; i < to; ++i)
620 min_depth = std::min(min_depth, toc_list[i].depth());
621 if (min_depth > depth)
625 if (to - from <= max_number_of_items) {
626 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
627 docstring label(4 * max(0, toc_list[i].depth() - depth), char_type(' '));
628 label += lyx::from_utf8(limit_string_length(toc_list[i].str()));
629 if (toc_list[i].depth() == depth
630 && shortcut_count < 9) {
631 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
632 label += char_type('|') + convert<docstring>(++shortcut_count);
634 tomenu.add(MenuItem(MenuItem::Command, label,
635 FuncRequest(toc_list[i].action())));
638 lyx::toc::Toc::size_type pos = from;
640 lyx::toc::Toc::size_type new_pos = pos + 1;
641 while (new_pos < to &&
642 toc_list[new_pos].depth() > depth)
645 docstring label(4 * max(0, toc_list[pos].depth() - depth), ' ');
646 label += lyx::from_utf8(limit_string_length(toc_list[pos].str()));
647 if (toc_list[pos].depth() == depth &&
648 shortcut_count < 9) {
649 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
650 label += char_type('|') + convert<docstring>(++shortcut_count);
652 if (new_pos == pos + 1) {
653 tomenu.add(MenuItem(MenuItem::Command,
654 label, FuncRequest(toc_list[pos].action())));
656 MenuItem item(MenuItem::Submenu, label);
657 item.submenu(new Menu);
658 expandToc2(*item.submenu(),
659 toc_list, pos, new_pos, depth + 1);
668 void expandToc(Menu & tomenu, Buffer const * buf)
670 // To make things very cleanly, we would have to pass buf to
671 // all MenuItem constructors and to expandToc2. However, we
672 // know that all the entries in a TOC will be have status_ ==
673 // OK, so we avoid this unnecessary overhead (JMarc)
676 tomenu.add(MenuItem(MenuItem::Command,
677 _("No Documents Open!"),
678 FuncRequest(LFUN_NOACTION)));
682 FloatList const & floatlist = buf->params().getLyXTextClass().floats();
683 lyx::toc::TocList const & toc_list = lyx::toc::getTocList(*buf);
684 lyx::toc::TocList::const_iterator cit = toc_list.begin();
685 lyx::toc::TocList::const_iterator end = toc_list.end();
686 for (; cit != end; ++cit) {
688 if (cit->first == "TOC")
691 // All the rest is for floats
692 auto_ptr<Menu> menu(new Menu);
693 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
694 lyx::toc::Toc::const_iterator eend = cit->second.end();
695 for (; ccit != eend; ++ccit) {
696 docstring const label = lyx::from_utf8(limit_string_length(ccit->str()));
697 menu->add(MenuItem(MenuItem::Command,
699 FuncRequest(ccit->action())));
701 string const & floatName = floatlist.getType(cit->first).listName();
702 MenuItem item(MenuItem::Submenu, _(floatName));
703 item.submenu(menu.release());
708 cit = toc_list.find("TOC");
710 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
711 _("No Table of contents"),
714 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
719 void expandPasteRecent(Menu & tomenu, Buffer const * buf)
724 vector<string> const sel =
725 lyx::cap::availableSelections(*buf);
727 vector<string>::const_iterator cit = sel.begin();
728 vector<string>::const_iterator end = sel.end();
730 for (unsigned int index = 0; cit != end; ++cit, ++index) {
731 tomenu.add(MenuItem(MenuItem::Command, lyx::from_utf8(*cit),
732 FuncRequest(LFUN_PASTE, convert<string>(index))));
737 void expandBranches(Menu & tomenu, Buffer const * buf)
742 BufferParams const & params = buf->getMasterBuffer()->params();
744 BranchList::const_iterator cit = params.branchlist().begin();
745 BranchList::const_iterator end = params.branchlist().end();
747 for (int ii = 1; cit != end; ++cit, ++ii) {
748 docstring label = lyx::from_utf8(cit->getBranch());
750 label = convert<docstring>(ii) + lyx::from_ascii(". ") + label + char_type('|') + convert<docstring>(ii);
751 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
752 FuncRequest(LFUN_BRANCH_INSERT,
761 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
762 Buffer const * buf) const
767 for (Menu::const_iterator cit = frommenu.begin();
768 cit != frommenu.end() ; ++cit) {
769 switch (cit->kind()) {
770 case MenuItem::Lastfiles:
771 expandLastfiles(tomenu);
774 case MenuItem::Documents:
775 expandDocuments(tomenu);
778 case MenuItem::ImportFormats:
779 case MenuItem::ViewFormats:
780 case MenuItem::UpdateFormats:
781 case MenuItem::ExportFormats:
782 expandFormats(cit->kind(), tomenu, buf);
785 case MenuItem::CharStyles:
786 expandCharStyleInsert(tomenu, buf);
789 case MenuItem::FloatListInsert:
790 expandFloatListInsert(tomenu, buf);
793 case MenuItem::FloatInsert:
794 expandFloatInsert(tomenu, buf);
797 case MenuItem::PasteRecent:
798 expandPasteRecent(tomenu, buf);
801 case MenuItem::Branches:
802 expandBranches(tomenu, buf);
806 expandToc(tomenu, buf);
809 case MenuItem::Submenu: {
811 item.submenu(new Menu(cit->submenuname()));
812 expand(getMenu(cit->submenuname()),
813 *item.submenu(), buf);
814 tomenu.addWithStatusCheck(item);
818 case MenuItem::Separator:
819 tomenu.addWithStatusCheck(*cit);
822 case MenuItem::Command:
824 || !specialmenu_->hasFunc(cit->func()))
825 tomenu.addWithStatusCheck(*cit);
829 // we do not want the menu to end with a separator
831 && tomenu.items_.back().kind() == MenuItem::Separator)
832 tomenu.items_.pop_back();
834 // Check whether the shortcuts are unique
835 tomenu.checkShortcuts();
839 void MenuBackend::read(LyXLex & lex)
848 struct keyword_item menutags[md_last - 1] = {
849 { "end", md_endmenuset },
851 { "menubar", md_menubar }
855 if (compare_ascii_no_case(lex.getString(), "menuset")) {
856 lyxerr << "Menubackend::read: ERROR wrong token:`"
857 << lex.getString() << '\'' << endl;
860 lex.pushTable(menutags, md_last - 1);
861 if (lyxerr.debugging(Debug::PARSER))
862 lex.printTable(lyxerr);
866 while (lex.isOK() && !quit) {
873 docstring const name = lyx::from_utf8(lex.getString());
875 getMenu(name).read(lex);
887 lex.printError("menubackend::read: "
888 "Unknown menu tag: `$$Token'");
896 void MenuBackend::add(Menu const & menu)
898 menulist_.push_back(menu);
902 bool MenuBackend::hasMenu(docstring const & name) const
904 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
908 Menu const & MenuBackend::getMenu(docstring const & name) const
910 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
912 lyxerr << "No submenu named " << lyx::to_utf8(name) << endl;
913 BOOST_ASSERT(cit != end());
918 Menu & MenuBackend::getMenu(docstring const & name)
920 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
922 lyxerr << "No submenu named " << lyx::to_utf8(name) << endl;
923 BOOST_ASSERT(it != end());
928 Menu const & MenuBackend::getMenubar() const