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>
50 using lyx::support::compare_no_case;
51 using lyx::support::compare_ascii_no_case;
52 using lyx::support::contains;
53 using lyx::support::makeDisplayPath;
54 using lyx::support::token;
68 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
72 class MenuNamesEqual : public std::unary_function<Menu, bool> {
74 MenuNamesEqual(docstring const & name)
76 bool operator()(Menu const & menu) const
78 return menu.name() == name_;
87 // This is the global menu definition
88 MenuBackend menubackend;
91 MenuItem::MenuItem(Kind kind)
92 : kind_(kind), optional_(false)
96 MenuItem::MenuItem(Kind kind, docstring const & label,
97 docstring const & submenu, bool optional)
98 : kind_(kind), label_(label),
99 submenuname_(submenu), optional_(optional)
101 BOOST_ASSERT(kind == Submenu);
105 MenuItem::MenuItem(Kind kind, docstring const & label,
106 FuncRequest const & func, bool optional)
107 : kind_(kind), label_(label), func_(func), optional_(optional)
109 func_.origin = FuncRequest::UI;
113 MenuItem::~MenuItem()
117 void MenuItem::submenu(Menu * menu)
119 submenu_.reset(menu);
123 docstring const MenuItem::label() const
125 return token(label_, char_type('|'), 0);
129 docstring const MenuItem::shortcut() const
131 return token(label_, char_type('|'), 1);
135 docstring const MenuItem::binding() const
137 if (kind_ != Command)
140 // Get the keys bound to this action, but keep only the
142 kb_keymap::Bindings bindings = toplevel_keymap->findbindings(func_);
144 if (bindings.size()) {
145 return lyx::from_utf8(bindings.begin()->print());
149 << lyxaction.getActionName(func_.action)
150 << '(' << lyx::to_utf8(func_.argument()) << ')' << endl;
157 Menu & Menu::add(MenuItem const & i)
164 Menu & Menu::addWithStatusCheck(MenuItem const & i)
168 case MenuItem::Command: {
170 lyx::getStatus(i.func());
172 || (!status.enabled() && i.optional()))
175 items_.back().status(status);
179 case MenuItem::Submenu: {
181 bool enabled = false;
182 for (const_iterator cit = i.submenu()->begin();
183 cit != i.submenu()->end(); ++cit) {
184 if ((cit->kind() == MenuItem::Command
185 || cit->kind() == MenuItem::Submenu)
186 && cit->status().enabled()) {
191 if (enabled || !i.optional()) {
193 items_.back().status().enabled(enabled);
201 case MenuItem::Separator:
203 && items_.back().kind() != MenuItem::Separator)
215 Menu & Menu::read(LyXLex & lex)
239 struct keyword_item menutags[md_last - 1] = {
240 { "branches", md_branches },
241 { "charstyles", md_charstyles },
242 { "documents", md_documents },
243 { "end", md_endmenu },
244 { "exportformats", md_exportformats },
245 { "floatinsert", md_floatinsert },
246 { "floatlistinsert", md_floatlistinsert },
247 { "importformats", md_importformats },
249 { "lastfiles", md_lastfiles },
250 { "optitem", md_optitem },
251 { "optsubmenu", md_optsubmenu },
252 { "pasterecent", md_pasterecent },
253 { "separator", md_separator },
254 { "submenu", md_submenu },
256 { "updateformats", md_updateformats },
257 { "viewformats", md_viewformats }
260 lex.pushTable(menutags, md_last - 1);
261 if (lyxerr.debugging(Debug::PARSER))
262 lex.printTable(lyxerr);
265 bool optional = false;
267 while (lex.isOK() && !quit) {
271 // fallback to md_item
274 docstring const name = _(lex.getString());
276 string const command = lex.getString();
277 FuncRequest func = lyxaction.lookupFunc(command);
278 add(MenuItem(MenuItem::Command, name, func, optional));
284 add(MenuItem(MenuItem::Separator));
288 add(MenuItem(MenuItem::Lastfiles));
292 add(MenuItem(MenuItem::CharStyles));
296 add(MenuItem(MenuItem::Documents));
300 add(MenuItem(MenuItem::Toc));
304 add(MenuItem(MenuItem::ViewFormats));
307 case md_updateformats:
308 add(MenuItem(MenuItem::UpdateFormats));
311 case md_exportformats:
312 add(MenuItem(MenuItem::ExportFormats));
315 case md_importformats:
316 add(MenuItem(MenuItem::ImportFormats));
319 case md_floatlistinsert:
320 add(MenuItem(MenuItem::FloatListInsert));
324 add(MenuItem(MenuItem::FloatInsert));
328 add(MenuItem(MenuItem::PasteRecent));
332 add(MenuItem(MenuItem::Branches));
337 // fallback to md_submenu
340 docstring const mlabel = _(lex.getString());
342 docstring const mname = lyx::from_utf8(lex.getString());
343 add(MenuItem(MenuItem::Submenu, mlabel, mname,
354 lex.printError("Menu::read: "
355 "Unknown menu tag: `$$Token'");
364 MenuItem const & Menu::operator[](size_type i) const
370 bool Menu::hasFunc(FuncRequest const & func) const
372 return find_if(begin(), end(),
373 bind(std::equal_to<FuncRequest>(),
374 bind(&MenuItem::func, _1),
378 void Menu::checkShortcuts() const
380 // This is a quadratic algorithm, but we do not care because
381 // menus are short enough
382 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
383 docstring shortcut = it1->shortcut();
384 if (shortcut.empty())
386 if (!contains(it1->label(), shortcut))
387 lyxerr << "Menu warning: menu entry \""
388 << lyx::to_utf8(it1->label())
389 << "\" does not contain shortcut `"
390 << lyx::to_utf8(shortcut) << "'." << endl;
391 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
392 if (!compare_no_case(it2->shortcut(), shortcut)) {
393 lyxerr << "Menu warning: menu entries "
394 << '"' << lyx::to_utf8(it1->fulllabel())
395 << "\" and \"" << lyx::to_utf8(it2->fulllabel())
396 << "\" share the same shortcut."
404 void MenuBackend::specialMenu(docstring const &name)
407 specialmenu_ = &getMenu(name);
413 class compare_format {
415 bool operator()(Format const * p1, Format const * p2) {
420 docstring const limit_string_length(docstring const & str)
422 docstring::size_type const max_item_length = 45;
424 if (str.size() > max_item_length)
425 return str.substr(0, max_item_length - 3) + "...";
431 void expandLastfiles(Menu & tomenu)
433 lyx::Session::LastFiles const & lf = LyX::cref().session().lastFiles();
434 lyx::Session::LastFiles::const_iterator lfit = lf.begin();
438 for (; lfit != lf.end() && ii < 10; ++lfit, ++ii) {
439 docstring const label = convert<docstring>(ii) + ". "
440 + makeDisplayPath((*lfit), 30)
441 + char_type('|') + convert<docstring>(ii);
442 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))));
447 void expandDocuments(Menu & tomenu)
449 typedef vector<string> Strings;
450 Strings const names = theBufferList().getFileNames();
453 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
454 FuncRequest(LFUN_NOACTION)));
459 Strings::const_iterator docit = names.begin();
460 Strings::const_iterator end = names.end();
461 for (; docit != end; ++docit, ++ii) {
462 docstring label = makeDisplayPath(*docit, 20);
464 label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
465 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BUFFER_SWITCH, *docit)));
470 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
472 if (!buf && kind != MenuItem::ImportFormats) {
473 tomenu.add(MenuItem(MenuItem::Command,
474 _("No Documents Open!"),
475 FuncRequest(LFUN_NOACTION)));
479 typedef vector<Format const *> Formats;
484 case MenuItem::ImportFormats:
485 formats = Importer::GetImportableFormats();
486 action = LFUN_BUFFER_IMPORT;
488 case MenuItem::ViewFormats:
489 formats = Exporter::getExportableFormats(*buf, true);
490 action = LFUN_BUFFER_VIEW;
492 case MenuItem::UpdateFormats:
493 formats = Exporter::getExportableFormats(*buf, true);
494 action = LFUN_BUFFER_UPDATE;
497 formats = Exporter::getExportableFormats(*buf, false);
498 action = LFUN_BUFFER_EXPORT;
500 sort(formats.begin(), formats.end(), compare_format());
502 Formats::const_iterator fit = formats.begin();
503 Formats::const_iterator end = formats.end();
504 for (; fit != end ; ++fit) {
507 docstring label = lyx::from_utf8((*fit)->prettyname());
510 case MenuItem::ImportFormats:
511 if ((*fit)->name() == "text")
512 label = _("Plain Text as Lines");
513 else if ((*fit)->name() == "textparagraph")
514 label = _("Plain Text as Paragraphs");
517 case MenuItem::ViewFormats:
518 case MenuItem::ExportFormats:
519 case MenuItem::UpdateFormats:
520 if (!(*fit)->documentFormat())
527 if (!(*fit)->shortcut().empty())
528 label += char_type('|') + lyx::from_utf8((*fit)->shortcut());
531 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
532 FuncRequest(action, (*fit)->name())));
534 tomenu.add(MenuItem(MenuItem::Command, label,
535 FuncRequest(action, (*fit)->name())));
540 void expandFloatListInsert(Menu & tomenu, Buffer const * buf)
543 tomenu.add(MenuItem(MenuItem::Command,
544 _("No Documents Open!"),
545 FuncRequest(LFUN_NOACTION)));
549 FloatList const & floats =
550 buf->params().getLyXTextClass().floats();
551 FloatList::const_iterator cit = floats.begin();
552 FloatList::const_iterator end = floats.end();
553 for (; cit != end; ++cit) {
554 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
555 _(cit->second.listName()),
556 FuncRequest(LFUN_FLOAT_LIST,
557 cit->second.type())));
562 void expandFloatInsert(Menu & tomenu, Buffer const * buf)
565 tomenu.add(MenuItem(MenuItem::Command,
566 _("No Documents Open!"),
567 FuncRequest(LFUN_NOACTION)));
571 FloatList const & floats =
572 buf->params().getLyXTextClass().floats();
573 FloatList::const_iterator cit = floats.begin();
574 FloatList::const_iterator end = floats.end();
575 for (; cit != end; ++cit) {
577 docstring const label = _(cit->second.name());
578 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
579 FuncRequest(LFUN_FLOAT_INSERT,
580 cit->second.type())));
585 void expandCharStyleInsert(Menu & tomenu, Buffer const * buf)
588 tomenu.add(MenuItem(MenuItem::Command,
589 _("No Documents Open!"),
590 FuncRequest(LFUN_NOACTION)));
593 CharStyles & charstyles =
594 buf->params().getLyXTextClass().charstyles();
595 CharStyles::iterator cit = charstyles.begin();
596 CharStyles::iterator end = charstyles.end();
597 for (; cit != end; ++cit) {
598 docstring const label = lyx::from_utf8(cit->name);
599 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
600 FuncRequest(LFUN_CHARSTYLE_INSERT,
606 Menu::size_type const max_number_of_items = 25;
608 void expandToc2(Menu & tomenu,
609 lyx::toc::Toc const & toc_list,
610 lyx::toc::Toc::size_type from,
611 lyx::toc::Toc::size_type to, int depth)
613 int shortcut_count = 0;
615 // check whether depth is smaller than the smallest depth in toc.
616 int min_depth = 1000;
617 for (lyx::toc::Toc::size_type i = from; i < to; ++i)
618 min_depth = std::min(min_depth, toc_list[i].depth());
619 if (min_depth > depth)
623 if (to - from <= max_number_of_items) {
624 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
625 docstring label(4 * max(0, toc_list[i].depth() - depth), char_type(' '));
626 label += limit_string_length(toc_list[i].str());
627 if (toc_list[i].depth() == depth
628 && shortcut_count < 9) {
629 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
630 label += char_type('|') + convert<docstring>(++shortcut_count);
632 tomenu.add(MenuItem(MenuItem::Command, label,
633 FuncRequest(toc_list[i].action())));
636 lyx::toc::Toc::size_type pos = from;
638 lyx::toc::Toc::size_type new_pos = pos + 1;
639 while (new_pos < to &&
640 toc_list[new_pos].depth() > depth)
643 docstring label(4 * max(0, toc_list[pos].depth() - depth), ' ');
644 label += limit_string_length(toc_list[pos].str());
645 if (toc_list[pos].depth() == depth &&
646 shortcut_count < 9) {
647 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
648 label += char_type('|') + convert<docstring>(++shortcut_count);
650 if (new_pos == pos + 1) {
651 tomenu.add(MenuItem(MenuItem::Command,
652 label, FuncRequest(toc_list[pos].action())));
654 MenuItem item(MenuItem::Submenu, label);
655 item.submenu(new Menu);
656 expandToc2(*item.submenu(),
657 toc_list, pos, new_pos, depth + 1);
666 void expandToc(Menu & tomenu, Buffer const * buf)
668 // To make things very cleanly, we would have to pass buf to
669 // all MenuItem constructors and to expandToc2. However, we
670 // know that all the entries in a TOC will be have status_ ==
671 // OK, so we avoid this unnecessary overhead (JMarc)
674 tomenu.add(MenuItem(MenuItem::Command,
675 _("No Documents Open!"),
676 FuncRequest(LFUN_NOACTION)));
680 FloatList const & floatlist = buf->params().getLyXTextClass().floats();
681 lyx::toc::TocList const & toc_list = lyx::toc::getTocList(*buf);
682 lyx::toc::TocList::const_iterator cit = toc_list.begin();
683 lyx::toc::TocList::const_iterator end = toc_list.end();
684 for (; cit != end; ++cit) {
686 if (cit->first == "TOC")
689 // All the rest is for floats
690 auto_ptr<Menu> menu(new Menu);
691 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
692 lyx::toc::Toc::const_iterator eend = cit->second.end();
693 for (; ccit != eend; ++ccit) {
694 docstring const label = limit_string_length(ccit->str());
695 menu->add(MenuItem(MenuItem::Command,
697 FuncRequest(ccit->action())));
699 string const & floatName = floatlist.getType(cit->first).listName();
700 MenuItem item(MenuItem::Submenu, _(floatName));
701 item.submenu(menu.release());
706 cit = toc_list.find("TOC");
708 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
709 _("No Table of contents"),
712 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
717 void expandPasteRecent(Menu & tomenu, Buffer const * buf)
722 vector<docstring> const sel =
723 lyx::cap::availableSelections(*buf);
725 vector<docstring>::const_iterator cit = sel.begin();
726 vector<docstring>::const_iterator end = sel.end();
728 for (unsigned int index = 0; cit != end; ++cit, ++index) {
729 tomenu.add(MenuItem(MenuItem::Command, *cit,
730 FuncRequest(LFUN_PASTE, convert<string>(index))));
735 void expandBranches(Menu & tomenu, Buffer const * buf)
740 BufferParams const & params = buf->getMasterBuffer()->params();
742 BranchList::const_iterator cit = params.branchlist().begin();
743 BranchList::const_iterator end = params.branchlist().end();
745 for (int ii = 1; cit != end; ++cit, ++ii) {
746 docstring label = lyx::from_utf8(cit->getBranch());
748 label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
749 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
750 FuncRequest(LFUN_BRANCH_INSERT,
759 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
760 Buffer const * buf) const
765 for (Menu::const_iterator cit = frommenu.begin();
766 cit != frommenu.end() ; ++cit) {
767 switch (cit->kind()) {
768 case MenuItem::Lastfiles:
769 expandLastfiles(tomenu);
772 case MenuItem::Documents:
773 expandDocuments(tomenu);
776 case MenuItem::ImportFormats:
777 case MenuItem::ViewFormats:
778 case MenuItem::UpdateFormats:
779 case MenuItem::ExportFormats:
780 expandFormats(cit->kind(), tomenu, buf);
783 case MenuItem::CharStyles:
784 expandCharStyleInsert(tomenu, buf);
787 case MenuItem::FloatListInsert:
788 expandFloatListInsert(tomenu, buf);
791 case MenuItem::FloatInsert:
792 expandFloatInsert(tomenu, buf);
795 case MenuItem::PasteRecent:
796 expandPasteRecent(tomenu, buf);
799 case MenuItem::Branches:
800 expandBranches(tomenu, buf);
804 expandToc(tomenu, buf);
807 case MenuItem::Submenu: {
809 item.submenu(new Menu(cit->submenuname()));
810 expand(getMenu(cit->submenuname()),
811 *item.submenu(), buf);
812 tomenu.addWithStatusCheck(item);
816 case MenuItem::Separator:
817 tomenu.addWithStatusCheck(*cit);
820 case MenuItem::Command:
822 || !specialmenu_->hasFunc(cit->func()))
823 tomenu.addWithStatusCheck(*cit);
827 // we do not want the menu to end with a separator
829 && tomenu.items_.back().kind() == MenuItem::Separator)
830 tomenu.items_.pop_back();
832 // Check whether the shortcuts are unique
833 tomenu.checkShortcuts();
837 void MenuBackend::read(LyXLex & lex)
846 struct keyword_item menutags[md_last - 1] = {
847 { "end", md_endmenuset },
849 { "menubar", md_menubar }
853 if (compare_ascii_no_case(lex.getString(), "menuset")) {
854 lyxerr << "Menubackend::read: ERROR wrong token:`"
855 << lex.getString() << '\'' << endl;
858 lex.pushTable(menutags, md_last - 1);
859 if (lyxerr.debugging(Debug::PARSER))
860 lex.printTable(lyxerr);
864 while (lex.isOK() && !quit) {
871 docstring const name = lyx::from_utf8(lex.getString());
873 getMenu(name).read(lex);
885 lex.printError("menubackend::read: "
886 "Unknown menu tag: `$$Token'");
894 void MenuBackend::add(Menu const & menu)
896 menulist_.push_back(menu);
900 bool MenuBackend::hasMenu(docstring const & name) const
902 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
906 Menu const & MenuBackend::getMenu(docstring const & name) const
908 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
910 lyxerr << "No submenu named " << lyx::to_utf8(name) << endl;
911 BOOST_ASSERT(cit != end());
916 Menu & MenuBackend::getMenu(docstring const & name)
918 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
920 lyxerr << "No submenu named " << lyx::to_utf8(name) << endl;
921 BOOST_ASSERT(it != end());
926 Menu const & MenuBackend::getMenubar() const