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 "bufferparams.h"
23 #include "CutAndPaste.h"
27 #include "FloatList.h"
33 #include "LyXAction.h"
34 #include "lyx_main.h" // for lastfiles
39 #include "frontends/Application.h"
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 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, LyXView const * view)
168 case MenuItem::Command: {
170 view->getLyXFunc().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 string const limit_string_length(string const & str)
422 string::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, LyXView const * view)
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) + lyx::from_ascii(". ")
440 + makeDisplayPath((*lfit), 30)
441 + char_type('|') + convert<docstring>(ii);
442 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
447 void expandDocuments(Menu & tomenu, LyXView const * view)
449 typedef vector<string> Strings;
450 Strings const names = theApp->bufferList().getFileNames();
453 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
454 FuncRequest(LFUN_NOACTION)), view);
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) + lyx::from_ascii(". ") + label + char_type('|') + convert<docstring>(ii);
465 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BUFFER_SWITCH, *docit)), view);
470 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
472 if (!view->buffer() && kind != MenuItem::ImportFormats) {
473 tomenu.add(MenuItem(MenuItem::Command,
474 _("No Documents Open!"),
475 FuncRequest(LFUN_NOACTION)),
480 typedef vector<Format const *> Formats;
485 case MenuItem::ImportFormats:
486 formats = Importer::GetImportableFormats();
487 action = LFUN_BUFFER_IMPORT;
489 case MenuItem::ViewFormats:
490 formats = Exporter::getExportableFormats(*view->buffer(), true);
491 action = LFUN_BUFFER_VIEW;
493 case MenuItem::UpdateFormats:
494 formats = Exporter::getExportableFormats(*view->buffer(), true);
495 action = LFUN_BUFFER_UPDATE;
498 formats = Exporter::getExportableFormats(*view->buffer(), false);
499 action = LFUN_BUFFER_EXPORT;
501 sort(formats.begin(), formats.end(), compare_format());
503 Formats::const_iterator fit = formats.begin();
504 Formats::const_iterator end = formats.end();
505 for (; fit != end ; ++fit) {
508 docstring label = lyx::from_utf8((*fit)->prettyname());
511 case MenuItem::ImportFormats:
512 if ((*fit)->name() == "text")
513 label = _("Plain Text as Lines");
514 else if ((*fit)->name() == "textparagraph")
515 label = _("Plain Text as Paragraphs");
516 label += lyx::from_ascii("...");
518 case MenuItem::ViewFormats:
519 case MenuItem::ExportFormats:
520 case MenuItem::UpdateFormats:
521 if (!(*fit)->documentFormat())
528 if (!(*fit)->shortcut().empty())
529 label += char_type('|') + lyx::from_utf8((*fit)->shortcut());
531 tomenu.add(MenuItem(MenuItem::Command, label,
532 FuncRequest(action, (*fit)->name())),
538 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
540 if (!view->buffer()) {
541 tomenu.add(MenuItem(MenuItem::Command,
542 _("No Documents Open!"),
543 FuncRequest(LFUN_NOACTION)),
548 FloatList const & floats =
549 view->buffer()->params().getLyXTextClass().floats();
550 FloatList::const_iterator cit = floats.begin();
551 FloatList::const_iterator end = floats.end();
552 for (; cit != end; ++cit) {
553 tomenu.add(MenuItem(MenuItem::Command,
554 _(cit->second.listName()),
555 FuncRequest(LFUN_FLOAT_LIST,
556 cit->second.type())),
562 void expandFloatInsert(Menu & tomenu, LyXView const * view)
564 if (!view->buffer()) {
565 tomenu.add(MenuItem(MenuItem::Command,
566 _("No Documents Open!"),
567 FuncRequest(LFUN_NOACTION)),
572 FloatList const & floats =
573 view->buffer()->params().getLyXTextClass().floats();
574 FloatList::const_iterator cit = floats.begin();
575 FloatList::const_iterator end = floats.end();
576 for (; cit != end; ++cit) {
578 docstring const label = _(cit->second.name());
579 tomenu.add(MenuItem(MenuItem::Command, label,
580 FuncRequest(LFUN_FLOAT_INSERT,
581 cit->second.type())),
587 void expandCharStyleInsert(Menu & tomenu, LyXView const * view)
589 if (!view->buffer()) {
590 tomenu.add(MenuItem(MenuItem::Command,
591 _("No Documents Open!"),
592 FuncRequest(LFUN_NOACTION)),
596 CharStyles & charstyles =
597 view->buffer()->params().getLyXTextClass().charstyles();
598 CharStyles::iterator cit = charstyles.begin();
599 CharStyles::iterator end = charstyles.end();
600 for (; cit != end; ++cit) {
601 docstring const label = lyx::from_utf8(cit->name);
602 tomenu.add(MenuItem(MenuItem::Command, label,
603 FuncRequest(LFUN_CHARSTYLE_INSERT,
609 Menu::size_type const max_number_of_items = 25;
611 void expandToc2(Menu & tomenu,
612 lyx::toc::Toc const & toc_list,
613 lyx::toc::Toc::size_type from,
614 lyx::toc::Toc::size_type to, int depth)
616 int shortcut_count = 0;
618 // check whether depth is smaller than the smallest depth in toc.
619 int min_depth = 1000;
620 for (lyx::toc::Toc::size_type i = from; i < to; ++i)
621 min_depth = std::min(min_depth, toc_list[i].depth());
622 if (min_depth > depth)
626 if (to - from <= max_number_of_items) {
627 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
628 docstring label(4 * max(0, toc_list[i].depth() - depth), char_type(' '));
629 label += lyx::from_utf8(limit_string_length(toc_list[i].str()));
630 if (toc_list[i].depth() == depth
631 && shortcut_count < 9) {
632 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
633 label += char_type('|') + convert<docstring>(++shortcut_count);
635 tomenu.add(MenuItem(MenuItem::Command, label,
636 FuncRequest(toc_list[i].action())));
639 lyx::toc::Toc::size_type pos = from;
641 lyx::toc::Toc::size_type new_pos = pos + 1;
642 while (new_pos < to &&
643 toc_list[new_pos].depth() > depth)
646 docstring label(4 * max(0, toc_list[pos].depth() - depth), ' ');
647 label += lyx::from_utf8(limit_string_length(toc_list[pos].str()));
648 if (toc_list[pos].depth() == depth &&
649 shortcut_count < 9) {
650 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
651 label += char_type('|') + convert<docstring>(++shortcut_count);
653 if (new_pos == pos + 1) {
654 tomenu.add(MenuItem(MenuItem::Command,
655 label, FuncRequest(toc_list[pos].action())));
657 MenuItem item(MenuItem::Submenu, label);
658 item.submenu(new Menu);
659 expandToc2(*item.submenu(),
660 toc_list, pos, new_pos, depth + 1);
669 void expandToc(Menu & tomenu, LyXView const * view)
671 // To make things very cleanly, we would have to pass view to
672 // all MenuItem constructors and to expandToc2. However, we
673 // know that all the entries in a TOC will be have status_ ==
674 // OK, so we avoid this unnecessary overhead (JMarc)
677 Buffer const * buf = view->buffer();
679 tomenu.add(MenuItem(MenuItem::Command,
680 _("No Documents Open!"),
681 FuncRequest(LFUN_NOACTION)),
686 FloatList const & floatlist = buf->params().getLyXTextClass().floats();
687 lyx::toc::TocList const & toc_list = lyx::toc::getTocList(*buf);
688 lyx::toc::TocList::const_iterator cit = toc_list.begin();
689 lyx::toc::TocList::const_iterator end = toc_list.end();
690 for (; cit != end; ++cit) {
692 if (cit->first == "TOC")
695 // All the rest is for floats
696 auto_ptr<Menu> menu(new Menu);
697 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
698 lyx::toc::Toc::const_iterator eend = cit->second.end();
699 for (; ccit != eend; ++ccit) {
700 docstring const label = lyx::from_utf8(limit_string_length(ccit->str()));
701 menu->add(MenuItem(MenuItem::Command,
703 FuncRequest(ccit->action())));
705 string const & floatName = floatlist.getType(cit->first).listName();
706 MenuItem item(MenuItem::Submenu, _(floatName));
707 item.submenu(menu.release());
712 cit = toc_list.find("TOC");
714 tomenu.add(MenuItem(MenuItem::Command,
715 _("No Table of contents"),
719 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
724 void expandPasteRecent(Menu & tomenu, LyXView const * view)
726 if (!view || !view->buffer())
729 vector<string> const sel =
730 lyx::cap::availableSelections(*view->buffer());
732 vector<string>::const_iterator cit = sel.begin();
733 vector<string>::const_iterator end = sel.end();
735 for (unsigned int index = 0; cit != end; ++cit, ++index) {
736 tomenu.add(MenuItem(MenuItem::Command, lyx::from_utf8(*cit),
737 FuncRequest(LFUN_PASTE, convert<string>(index))));
742 void expandBranches(Menu & tomenu, LyXView const * view)
744 if (!view || !view->buffer())
747 BufferParams const & params = view->buffer()->getMasterBuffer()->params();
749 BranchList::const_iterator cit = params.branchlist().begin();
750 BranchList::const_iterator end = params.branchlist().end();
752 for (int ii = 1; cit != end; ++cit, ++ii) {
753 docstring label = lyx::from_utf8(cit->getBranch());
755 label = convert<docstring>(ii) + lyx::from_ascii(". ") + label + char_type('|') + convert<docstring>(ii);
756 tomenu.add(MenuItem(MenuItem::Command, label,
757 FuncRequest(LFUN_BRANCH_INSERT,
758 cit->getBranch())), view);
766 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
767 LyXView const * view) const
772 for (Menu::const_iterator cit = frommenu.begin();
773 cit != frommenu.end() ; ++cit) {
774 switch (cit->kind()) {
775 case MenuItem::Lastfiles:
776 expandLastfiles(tomenu, view);
779 case MenuItem::Documents:
780 expandDocuments(tomenu, view);
783 case MenuItem::ImportFormats:
784 case MenuItem::ViewFormats:
785 case MenuItem::UpdateFormats:
786 case MenuItem::ExportFormats:
787 expandFormats(cit->kind(), tomenu, view);
790 case MenuItem::CharStyles:
791 expandCharStyleInsert(tomenu, view);
794 case MenuItem::FloatListInsert:
795 expandFloatListInsert(tomenu, view);
798 case MenuItem::FloatInsert:
799 expandFloatInsert(tomenu, view);
802 case MenuItem::PasteRecent:
803 expandPasteRecent(tomenu, view);
806 case MenuItem::Branches:
807 expandBranches(tomenu, view);
811 expandToc(tomenu, view);
814 case MenuItem::Submenu: {
816 item.submenu(new Menu(cit->submenuname()));
817 expand(getMenu(cit->submenuname()),
818 *item.submenu(), view);
819 tomenu.add(item, view);
823 case MenuItem::Separator:
824 tomenu.add(*cit, view);
827 case MenuItem::Command:
829 || !specialmenu_->hasFunc(cit->func()))
830 tomenu.add(*cit, view);
834 // we do not want the menu to end with a separator
836 && tomenu.items_.back().kind() == MenuItem::Separator)
837 tomenu.items_.pop_back();
839 // Check whether the shortcuts are unique
840 tomenu.checkShortcuts();
844 void MenuBackend::read(LyXLex & lex)
853 struct keyword_item menutags[md_last - 1] = {
854 { "end", md_endmenuset },
856 { "menubar", md_menubar }
860 if (compare_ascii_no_case(lex.getString(), "menuset")) {
861 lyxerr << "Menubackend::read: ERROR wrong token:`"
862 << lex.getString() << '\'' << endl;
865 lex.pushTable(menutags, md_last - 1);
866 if (lyxerr.debugging(Debug::PARSER))
867 lex.printTable(lyxerr);
871 while (lex.isOK() && !quit) {
878 docstring const name = lyx::from_utf8(lex.getString());
880 getMenu(name).read(lex);
892 lex.printError("menubackend::read: "
893 "Unknown menu tag: `$$Token'");
901 void MenuBackend::add(Menu const & menu)
903 menulist_.push_back(menu);
907 bool MenuBackend::hasMenu(docstring const & name) const
909 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
913 Menu const & MenuBackend::getMenu(docstring const & name) const
915 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
917 lyxerr << "No submenu named " << lyx::to_utf8(name) << endl;
918 BOOST_ASSERT(cit != end());
923 Menu & MenuBackend::getMenu(docstring const & name)
925 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
927 lyxerr << "No submenu named " << lyx::to_utf8(name) << endl;
928 BOOST_ASSERT(it != end());
933 Menu const & MenuBackend::getMenubar() const