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/LyXView.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 BufferList bufferlist;
71 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
75 class MenuNamesEqual : public std::unary_function<Menu, bool> {
77 MenuNamesEqual(docstring const & name)
79 bool operator()(Menu const & menu) const
81 return menu.name() == name_;
90 // This is the global menu definition
91 MenuBackend menubackend;
94 MenuItem::MenuItem(Kind kind)
95 : kind_(kind), optional_(false)
99 MenuItem::MenuItem(Kind kind, docstring const & label,
100 docstring const & submenu, bool optional)
101 : kind_(kind), label_(label),
102 submenuname_(submenu), optional_(optional)
104 BOOST_ASSERT(kind == Submenu);
108 MenuItem::MenuItem(Kind kind, docstring const & label,
109 FuncRequest const & func, bool optional)
110 : kind_(kind), label_(label), func_(func), optional_(optional)
112 func_.origin = FuncRequest::UI;
116 MenuItem::~MenuItem()
120 void MenuItem::submenu(Menu * menu)
122 submenu_.reset(menu);
126 docstring const MenuItem::label() const
128 return token(label_, char_type('|'), 0);
132 docstring const MenuItem::shortcut() const
134 return token(label_, char_type('|'), 1);
138 docstring const MenuItem::binding() const
140 if (kind_ != Command)
143 // Get the keys bound to this action, but keep only the
145 kb_keymap::Bindings bindings = toplevel_keymap->findbindings(func_);
147 if (bindings.size()) {
148 return lyx::from_utf8(bindings.begin()->print());
152 << lyxaction.getActionName(func_.action)
153 << '(' << lyx::to_utf8(func_.argument()) << ')' << endl;
160 Menu & Menu::add(MenuItem const & i, LyXView const * view)
169 case MenuItem::Command: {
171 view->getLyXFunc().getStatus(i.func());
173 || (!status.enabled() && i.optional()))
176 items_.back().status(status);
180 case MenuItem::Submenu: {
182 bool enabled = false;
183 for (const_iterator cit = i.submenu()->begin();
184 cit != i.submenu()->end(); ++cit) {
185 if ((cit->kind() == MenuItem::Command
186 || cit->kind() == MenuItem::Submenu)
187 && cit->status().enabled()) {
192 if (enabled || !i.optional()) {
194 items_.back().status().enabled(enabled);
202 case MenuItem::Separator:
204 && items_.back().kind() != MenuItem::Separator)
216 Menu & Menu::read(LyXLex & lex)
240 struct keyword_item menutags[md_last - 1] = {
241 { "branches", md_branches },
242 { "charstyles", md_charstyles },
243 { "documents", md_documents },
244 { "end", md_endmenu },
245 { "exportformats", md_exportformats },
246 { "floatinsert", md_floatinsert },
247 { "floatlistinsert", md_floatlistinsert },
248 { "importformats", md_importformats },
250 { "lastfiles", md_lastfiles },
251 { "optitem", md_optitem },
252 { "optsubmenu", md_optsubmenu },
253 { "pasterecent", md_pasterecent },
254 { "separator", md_separator },
255 { "submenu", md_submenu },
257 { "updateformats", md_updateformats },
258 { "viewformats", md_viewformats }
261 lex.pushTable(menutags, md_last - 1);
262 if (lyxerr.debugging(Debug::PARSER))
263 lex.printTable(lyxerr);
266 bool optional = false;
268 while (lex.isOK() && !quit) {
272 // fallback to md_item
275 docstring const name = _(lex.getString());
277 string const command = lex.getString();
278 FuncRequest func = lyxaction.lookupFunc(command);
279 add(MenuItem(MenuItem::Command, name, func, optional));
285 add(MenuItem(MenuItem::Separator));
289 add(MenuItem(MenuItem::Lastfiles));
293 add(MenuItem(MenuItem::CharStyles));
297 add(MenuItem(MenuItem::Documents));
301 add(MenuItem(MenuItem::Toc));
305 add(MenuItem(MenuItem::ViewFormats));
308 case md_updateformats:
309 add(MenuItem(MenuItem::UpdateFormats));
312 case md_exportformats:
313 add(MenuItem(MenuItem::ExportFormats));
316 case md_importformats:
317 add(MenuItem(MenuItem::ImportFormats));
320 case md_floatlistinsert:
321 add(MenuItem(MenuItem::FloatListInsert));
325 add(MenuItem(MenuItem::FloatInsert));
329 add(MenuItem(MenuItem::PasteRecent));
333 add(MenuItem(MenuItem::Branches));
338 // fallback to md_submenu
341 docstring const mlabel = _(lex.getString());
343 docstring const mname = lyx::from_utf8(lex.getString());
344 add(MenuItem(MenuItem::Submenu, mlabel, mname,
355 lex.printError("Menu::read: "
356 "Unknown menu tag: `$$Token'");
365 MenuItem const & Menu::operator[](size_type i) const
371 bool Menu::hasFunc(FuncRequest const & func) const
373 return find_if(begin(), end(),
374 bind(std::equal_to<FuncRequest>(),
375 bind(&MenuItem::func, _1),
379 void Menu::checkShortcuts() const
381 // This is a quadratic algorithm, but we do not care because
382 // menus are short enough
383 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
384 docstring shortcut = it1->shortcut();
385 if (shortcut.empty())
387 if (!contains(it1->label(), shortcut))
388 lyxerr << "Menu warning: menu entry \""
389 << lyx::to_utf8(it1->label())
390 << "\" does not contain shortcut `"
391 << lyx::to_utf8(shortcut) << "'." << endl;
392 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
393 if (!compare_no_case(it2->shortcut(), shortcut)) {
394 lyxerr << "Menu warning: menu entries "
395 << '"' << lyx::to_utf8(it1->fulllabel())
396 << "\" and \"" << lyx::to_utf8(it2->fulllabel())
397 << "\" share the same shortcut."
405 void MenuBackend::specialMenu(docstring const &name)
408 specialmenu_ = &getMenu(name);
414 class compare_format {
416 bool operator()(Format const * p1, Format const * p2) {
421 string const limit_string_length(string const & str)
423 string::size_type const max_item_length = 45;
425 if (str.size() > max_item_length)
426 return str.substr(0, max_item_length - 3) + "...";
432 void expandLastfiles(Menu & tomenu, LyXView const * view)
434 lyx::Session::LastFiles const & lf = LyX::cref().session().lastFiles();
435 lyx::Session::LastFiles::const_iterator lfit = lf.begin();
439 for (; lfit != lf.end() && ii < 10; ++lfit, ++ii) {
440 docstring const label = convert<docstring>(ii) + lyx::from_ascii(". ")
441 + makeDisplayPath((*lfit), 30)
442 + char_type('|') + convert<docstring>(ii);
443 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
448 void expandDocuments(Menu & tomenu, LyXView const * view)
450 typedef vector<string> Strings;
451 Strings const names = bufferlist.getFileNames();
454 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
455 FuncRequest(LFUN_NOACTION)), view);
460 Strings::const_iterator docit = names.begin();
461 Strings::const_iterator end = names.end();
462 for (; docit != end; ++docit, ++ii) {
463 docstring label = makeDisplayPath(*docit, 20);
465 label = convert<docstring>(ii) + lyx::from_ascii(". ") + label + char_type('|') + convert<docstring>(ii);
466 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BUFFER_SWITCH, *docit)), view);
471 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
473 if (!view->buffer() && kind != MenuItem::ImportFormats) {
474 tomenu.add(MenuItem(MenuItem::Command,
475 _("No Documents Open!"),
476 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(*view->buffer(), true);
492 action = LFUN_BUFFER_VIEW;
494 case MenuItem::UpdateFormats:
495 formats = Exporter::getExportableFormats(*view->buffer(), true);
496 action = LFUN_BUFFER_UPDATE;
499 formats = Exporter::getExportableFormats(*view->buffer(), 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());
532 tomenu.add(MenuItem(MenuItem::Command, label,
533 FuncRequest(action, (*fit)->name())),
539 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
541 if (!view->buffer()) {
542 tomenu.add(MenuItem(MenuItem::Command,
543 _("No Documents Open!"),
544 FuncRequest(LFUN_NOACTION)),
549 FloatList const & floats =
550 view->buffer()->params().getLyXTextClass().floats();
551 FloatList::const_iterator cit = floats.begin();
552 FloatList::const_iterator end = floats.end();
553 for (; cit != end; ++cit) {
554 tomenu.add(MenuItem(MenuItem::Command,
555 _(cit->second.listName()),
556 FuncRequest(LFUN_FLOAT_LIST,
557 cit->second.type())),
563 void expandFloatInsert(Menu & tomenu, LyXView const * view)
565 if (!view->buffer()) {
566 tomenu.add(MenuItem(MenuItem::Command,
567 _("No Documents Open!"),
568 FuncRequest(LFUN_NOACTION)),
573 FloatList const & floats =
574 view->buffer()->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.add(MenuItem(MenuItem::Command, label,
581 FuncRequest(LFUN_FLOAT_INSERT,
582 cit->second.type())),
588 void expandCharStyleInsert(Menu & tomenu, LyXView const * view)
590 if (!view->buffer()) {
591 tomenu.add(MenuItem(MenuItem::Command,
592 _("No Documents Open!"),
593 FuncRequest(LFUN_NOACTION)),
597 CharStyles & charstyles =
598 view->buffer()->params().getLyXTextClass().charstyles();
599 CharStyles::iterator cit = charstyles.begin();
600 CharStyles::iterator end = charstyles.end();
601 for (; cit != end; ++cit) {
602 docstring const label = lyx::from_utf8(cit->name);
603 tomenu.add(MenuItem(MenuItem::Command, label,
604 FuncRequest(LFUN_CHARSTYLE_INSERT,
610 Menu::size_type const max_number_of_items = 25;
612 void expandToc2(Menu & tomenu,
613 lyx::toc::Toc const & toc_list,
614 lyx::toc::Toc::size_type from,
615 lyx::toc::Toc::size_type to, int depth)
617 int shortcut_count = 0;
619 // check whether depth is smaller than the smallest depth in toc.
620 int min_depth = 1000;
621 for (lyx::toc::Toc::size_type i = from; i < to; ++i)
622 min_depth = std::min(min_depth, toc_list[i].depth());
623 if (min_depth > depth)
627 if (to - from <= max_number_of_items) {
628 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
629 docstring label(4 * max(0, toc_list[i].depth() - depth), char_type(' '));
630 label += lyx::from_utf8(limit_string_length(toc_list[i].str()));
631 if (toc_list[i].depth() == depth
632 && shortcut_count < 9) {
633 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
634 label += char_type('|') + convert<docstring>(++shortcut_count);
636 tomenu.add(MenuItem(MenuItem::Command, label,
637 FuncRequest(toc_list[i].action())));
640 lyx::toc::Toc::size_type pos = from;
642 lyx::toc::Toc::size_type new_pos = pos + 1;
643 while (new_pos < to &&
644 toc_list[new_pos].depth() > depth)
647 docstring label(4 * max(0, toc_list[pos].depth() - depth), ' ');
648 label += lyx::from_utf8(limit_string_length(toc_list[pos].str()));
649 if (toc_list[pos].depth() == depth &&
650 shortcut_count < 9) {
651 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
652 label += char_type('|') + convert<docstring>(++shortcut_count);
654 if (new_pos == pos + 1) {
655 tomenu.add(MenuItem(MenuItem::Command,
656 label, FuncRequest(toc_list[pos].action())));
658 MenuItem item(MenuItem::Submenu, label);
659 item.submenu(new Menu);
660 expandToc2(*item.submenu(),
661 toc_list, pos, new_pos, depth + 1);
670 void expandToc(Menu & tomenu, LyXView const * view)
672 // To make things very cleanly, we would have to pass view to
673 // all MenuItem constructors and to expandToc2. However, we
674 // know that all the entries in a TOC will be have status_ ==
675 // OK, so we avoid this unnecessary overhead (JMarc)
678 Buffer const * buf = view->buffer();
680 tomenu.add(MenuItem(MenuItem::Command,
681 _("No Documents Open!"),
682 FuncRequest(LFUN_NOACTION)),
687 FloatList const & floatlist = buf->params().getLyXTextClass().floats();
688 lyx::toc::TocList const & toc_list = lyx::toc::getTocList(*buf);
689 lyx::toc::TocList::const_iterator cit = toc_list.begin();
690 lyx::toc::TocList::const_iterator end = toc_list.end();
691 for (; cit != end; ++cit) {
693 if (cit->first == "TOC")
696 // All the rest is for floats
697 auto_ptr<Menu> menu(new Menu);
698 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
699 lyx::toc::Toc::const_iterator eend = cit->second.end();
700 for (; ccit != eend; ++ccit) {
701 docstring const label = lyx::from_utf8(limit_string_length(ccit->str()));
702 menu->add(MenuItem(MenuItem::Command,
704 FuncRequest(ccit->action())));
706 string const & floatName = floatlist.getType(cit->first).listName();
707 MenuItem item(MenuItem::Submenu, _(floatName));
708 item.submenu(menu.release());
713 cit = toc_list.find("TOC");
715 tomenu.add(MenuItem(MenuItem::Command,
716 _("No Table of contents"),
720 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
725 void expandPasteRecent(Menu & tomenu, LyXView const * view)
727 if (!view || !view->buffer())
730 vector<string> const sel =
731 lyx::cap::availableSelections(*view->buffer());
733 vector<string>::const_iterator cit = sel.begin();
734 vector<string>::const_iterator end = sel.end();
736 for (unsigned int index = 0; cit != end; ++cit, ++index) {
737 tomenu.add(MenuItem(MenuItem::Command, lyx::from_utf8(*cit),
738 FuncRequest(LFUN_PASTE, convert<string>(index))));
743 void expandBranches(Menu & tomenu, LyXView const * view)
745 if (!view || !view->buffer())
748 BufferParams const & params = view->buffer()->getMasterBuffer()->params();
750 BranchList::const_iterator cit = params.branchlist().begin();
751 BranchList::const_iterator end = params.branchlist().end();
753 for (int ii = 1; cit != end; ++cit, ++ii) {
754 docstring label = lyx::from_utf8(cit->getBranch());
756 label = convert<docstring>(ii) + lyx::from_ascii(". ") + label + char_type('|') + convert<docstring>(ii);
757 tomenu.add(MenuItem(MenuItem::Command, label,
758 FuncRequest(LFUN_BRANCH_INSERT,
759 cit->getBranch())), view);
767 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
768 LyXView const * view) const
773 for (Menu::const_iterator cit = frommenu.begin();
774 cit != frommenu.end() ; ++cit) {
775 switch (cit->kind()) {
776 case MenuItem::Lastfiles:
777 expandLastfiles(tomenu, view);
780 case MenuItem::Documents:
781 expandDocuments(tomenu, view);
784 case MenuItem::ImportFormats:
785 case MenuItem::ViewFormats:
786 case MenuItem::UpdateFormats:
787 case MenuItem::ExportFormats:
788 expandFormats(cit->kind(), tomenu, view);
791 case MenuItem::CharStyles:
792 expandCharStyleInsert(tomenu, view);
795 case MenuItem::FloatListInsert:
796 expandFloatListInsert(tomenu, view);
799 case MenuItem::FloatInsert:
800 expandFloatInsert(tomenu, view);
803 case MenuItem::PasteRecent:
804 expandPasteRecent(tomenu, view);
807 case MenuItem::Branches:
808 expandBranches(tomenu, view);
812 expandToc(tomenu, view);
815 case MenuItem::Submenu: {
817 item.submenu(new Menu(cit->submenuname()));
818 expand(getMenu(cit->submenuname()),
819 *item.submenu(), view);
820 tomenu.add(item, view);
824 case MenuItem::Separator:
825 tomenu.add(*cit, view);
828 case MenuItem::Command:
830 || !specialmenu_->hasFunc(cit->func()))
831 tomenu.add(*cit, view);
835 // we do not want the menu to end with a separator
837 && tomenu.items_.back().kind() == MenuItem::Separator)
838 tomenu.items_.pop_back();
840 // Check whether the shortcuts are unique
841 tomenu.checkShortcuts();
845 void MenuBackend::read(LyXLex & lex)
854 struct keyword_item menutags[md_last - 1] = {
855 { "end", md_endmenuset },
857 { "menubar", md_menubar }
861 if (compare_ascii_no_case(lex.getString(), "menuset")) {
862 lyxerr << "Menubackend::read: ERROR wrong token:`"
863 << lex.getString() << '\'' << endl;
866 lex.pushTable(menutags, md_last - 1);
867 if (lyxerr.debugging(Debug::PARSER))
868 lex.printTable(lyxerr);
872 while (lex.isOK() && !quit) {
879 docstring const name = lyx::from_utf8(lex.getString());
881 getMenu(name).read(lex);
893 lex.printError("menubackend::read: "
894 "Unknown menu tag: `$$Token'");
902 void MenuBackend::add(Menu const & menu)
904 menulist_.push_back(menu);
908 bool MenuBackend::hasMenu(docstring const & name) const
910 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
914 Menu const & MenuBackend::getMenu(docstring const & name) const
916 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
918 lyxerr << "No submenu named " << lyx::to_utf8(name) << endl;
919 BOOST_ASSERT(cit != end());
924 Menu & MenuBackend::getMenu(docstring const & name)
926 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
928 lyxerr << "No submenu named " << lyx::to_utf8(name) << endl;
929 BOOST_ASSERT(it != end());
934 Menu const & MenuBackend::getMenubar() const