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"
33 #include "lastfiles.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/tostr.h"
46 #include <boost/bind.hpp>
50 using lyx::support::compare_ascii_no_case;
51 using lyx::support::contains;
52 using lyx::support::MakeDisplayPath;
53 using lyx::support::token;
67 extern BufferList bufferlist;
68 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
72 class MenuNamesEqual : public std::unary_function<Menu, bool> {
74 MenuNamesEqual(string 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, string const & label,
97 string const & submenu, bool optional)
98 : kind_(kind), label_(label),
99 submenuname_(submenu), optional_(optional)
101 BOOST_ASSERT(kind == Submenu);
105 MenuItem::MenuItem(Kind kind, string const & label,
106 FuncRequest const & func, bool optional)
107 : kind_(kind), label_(label), func_(func), optional_(optional)
111 MenuItem::~MenuItem()
115 void MenuItem::submenu(Menu * menu)
117 submenu_.reset(menu);
121 string const MenuItem::label() const
123 return token(label_, '|', 0);
127 string const MenuItem::shortcut() const
129 return token(label_, '|', 1);
133 string const MenuItem::binding() const
135 if (kind_ != Command)
138 // Get the keys bound to this action, but keep only the
140 kb_keymap::Bindings bindings = toplevel_keymap->findbindings(func_);
142 if (bindings.size()) {
143 return bindings.begin()->print();
147 << lyxaction.getActionName(func_.action)
148 << '(' << func_.argument << ')' << endl;
155 Menu & Menu::add(MenuItem const & i, LyXView const * view)
164 case MenuItem::Command: {
166 view->getLyXFunc().getStatus(i.func());
168 || (!status.enabled() && i.optional()))
171 items_.back().status(status);
175 case MenuItem::Submenu: {
177 bool enabled = false;
178 for (const_iterator cit = i.submenu()->begin();
179 cit != i.submenu()->end(); ++cit) {
180 if ((cit->kind() == MenuItem::Command
181 || cit->kind() == MenuItem::Submenu)
182 && cit->status().enabled()) {
187 if (enabled || !i.optional()) {
189 items_.back().status().enabled(enabled);
197 case MenuItem::Separator:
199 && items_.back().kind() != MenuItem::Separator)
211 Menu & Menu::read(LyXLex & lex)
235 struct keyword_item menutags[md_last - 1] = {
236 { "branches", md_branches },
237 { "charstyles", md_charstyles },
238 { "documents", md_documents },
239 { "end", md_endmenu },
240 { "exportformats", md_exportformats },
241 { "floatinsert", md_floatinsert },
242 { "floatlistinsert", md_floatlistinsert },
243 { "importformats", md_importformats },
245 { "lastfiles", md_lastfiles },
246 { "optitem", md_optitem },
247 { "optsubmenu", md_optsubmenu },
248 { "pasterecent", md_pasterecent },
249 { "separator", md_separator },
250 { "submenu", md_submenu },
252 { "updateformats", md_updateformats },
253 { "viewformats", md_viewformats }
256 lex.pushTable(menutags, md_last - 1);
257 if (lyxerr.debugging(Debug::PARSER))
258 lex.printTable(lyxerr);
261 bool optional = false;
263 while (lex.isOK() && !quit) {
267 // fallback to md_item
270 string const name = _(lex.getString());
272 string const command = lex.getString();
273 FuncRequest func = lyxaction.lookupFunc(command);
274 add(MenuItem(MenuItem::Command, name, func, optional));
280 add(MenuItem(MenuItem::Separator));
284 add(MenuItem(MenuItem::Lastfiles));
288 add(MenuItem(MenuItem::CharStyles));
292 add(MenuItem(MenuItem::Documents));
296 add(MenuItem(MenuItem::Toc));
300 add(MenuItem(MenuItem::ViewFormats));
303 case md_updateformats:
304 add(MenuItem(MenuItem::UpdateFormats));
307 case md_exportformats:
308 add(MenuItem(MenuItem::ExportFormats));
311 case md_importformats:
312 add(MenuItem(MenuItem::ImportFormats));
315 case md_floatlistinsert:
316 add(MenuItem(MenuItem::FloatListInsert));
320 add(MenuItem(MenuItem::FloatInsert));
324 add(MenuItem(MenuItem::PasteRecent));
328 add(MenuItem(MenuItem::Branches));
333 // fallback to md_submenu
336 string const mlabel = _(lex.getString());
338 string const mname = lex.getString();
339 add(MenuItem(MenuItem::Submenu, mlabel, mname,
350 lex.printError("Menu::read: "
351 "Unknown menu tag: `$$Token'");
360 MenuItem const & Menu::operator[](size_type i) const
366 bool Menu::hasFunc(FuncRequest const & func) const
368 return find_if(begin(), end(),
369 bind(std::equal_to<FuncRequest>(),
370 bind(&MenuItem::func, _1),
374 void Menu::checkShortcuts() const
376 // This is a quadratic algorithm, but we do not care because
377 // menus are short enough
378 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
379 string shortcut = it1->shortcut();
380 if (shortcut.empty())
382 if (!contains(it1->label(), shortcut))
383 lyxerr << "Menu warning: menu entry \""
385 << "\" does not contain shortcut `"
386 << shortcut << "'." << endl;
387 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
388 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
389 lyxerr << "Menu warning: menu entries "
390 << '"' << it1->fulllabel()
391 << "\" and \"" << it2->fulllabel()
392 << "\" share the same shortcut."
400 void MenuBackend::specialMenu(string const &name)
403 specialmenu_ = &getMenu(name);
409 class compare_format {
411 bool operator()(Format const * p1, Format const * p2) {
416 string const limit_string_length(string const & str)
418 string::size_type const max_item_length = 45;
420 if (str.size() > max_item_length)
421 return str.substr(0, max_item_length - 3) + "...";
427 void expandLastfiles(Menu & tomenu, LyXView const * view)
429 LastFiles const & lastfiles = LyX::cref().lastfiles();
432 LastFiles::const_iterator lfit = lastfiles.begin();
433 LastFiles::const_iterator end = lastfiles.end();
435 for (; lfit != end && ii < 10; ++lfit, ++ii) {
436 string const label = tostr(ii) + ". "
437 + MakeDisplayPath((*lfit), 30)
439 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
443 void expandDocuments(Menu & tomenu, LyXView const * view)
445 typedef vector<string> Strings;
446 Strings const names = bufferlist.getFileNames();
449 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
450 FuncRequest(LFUN_NOACTION)), view);
455 Strings::const_iterator docit = names.begin();
456 Strings::const_iterator end = names.end();
457 for (; docit != end; ++docit, ++ii) {
458 string label = MakeDisplayPath(*docit, 20);
460 label = tostr(ii) + ". " + label + '|' + tostr(ii);
461 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_SWITCHBUFFER, *docit)), view);
466 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
468 if (!view->buffer() && kind != MenuItem::ImportFormats) {
469 tomenu.add(MenuItem(MenuItem::Command,
470 _("No Documents Open!"),
471 FuncRequest(LFUN_NOACTION)),
476 typedef vector<Format const *> Formats;
481 case MenuItem::ImportFormats:
482 formats = Importer::GetImportableFormats();
483 action = LFUN_IMPORT;
485 case MenuItem::ViewFormats:
486 formats = Exporter::GetExportableFormats(*view->buffer(), true);
487 action = LFUN_PREVIEW;
489 case MenuItem::UpdateFormats:
490 formats = Exporter::GetExportableFormats(*view->buffer(), true);
491 action = LFUN_UPDATE;
494 formats = Exporter::GetExportableFormats(*view->buffer(), false);
495 action = LFUN_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 string label = (*fit)->prettyname();
505 // we need to hide the default graphic export formats
506 // from the external menu, because we need them only
507 // for the internal lyx-view and external latex run
508 if (label == "EPS" || label == "XPM" || label == "PNG")
511 if (kind == MenuItem::ImportFormats) {
512 if ((*fit)->name() == "text")
513 label = _("ASCII text as lines");
514 else if ((*fit)->name() == "textparagraph")
515 label = _("ASCII text as paragraphs");
518 if (!(*fit)->shortcut().empty())
519 label += '|' + (*fit)->shortcut();
521 tomenu.add(MenuItem(MenuItem::Command, label,
522 FuncRequest(action, (*fit)->name())),
528 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
530 if (!view->buffer()) {
531 tomenu.add(MenuItem(MenuItem::Command,
532 _("No Documents Open!"),
533 FuncRequest(LFUN_NOACTION)),
538 FloatList const & floats =
539 view->buffer()->params().getLyXTextClass().floats();
540 FloatList::const_iterator cit = floats.begin();
541 FloatList::const_iterator end = floats.end();
542 for (; cit != end; ++cit) {
543 tomenu.add(MenuItem(MenuItem::Command,
544 _(cit->second.listName()),
545 FuncRequest(LFUN_FLOAT_LIST,
546 cit->second.type())),
552 void expandFloatInsert(Menu & tomenu, LyXView const * view)
554 if (!view->buffer()) {
555 tomenu.add(MenuItem(MenuItem::Command,
556 _("No Documents Open!"),
557 FuncRequest(LFUN_NOACTION)),
562 FloatList const & floats =
563 view->buffer()->params().getLyXTextClass().floats();
564 FloatList::const_iterator cit = floats.begin();
565 FloatList::const_iterator end = floats.end();
566 for (; cit != end; ++cit) {
568 string const label = _(cit->second.name());
569 tomenu.add(MenuItem(MenuItem::Command, label,
570 FuncRequest(LFUN_INSET_FLOAT,
571 cit->second.type())),
577 void expandCharStyleInsert(Menu & tomenu, LyXView const * view)
579 if (!view->buffer()) {
580 tomenu.add(MenuItem(MenuItem::Command,
581 _("No Documents Open!"),
582 FuncRequest(LFUN_NOACTION)),
586 CharStyles & charstyles =
587 view->buffer()->params().getLyXTextClass().charstyles();
588 CharStyles::iterator cit = charstyles.begin();
589 CharStyles::iterator end = charstyles.end();
590 for (; cit != end; ++cit) {
591 string const label = cit->name;
592 tomenu.add(MenuItem(MenuItem::Command, label,
593 FuncRequest(LFUN_INSERT_CHARSTYLE,
599 Menu::size_type const max_number_of_items = 25;
601 void expandToc2(Menu & tomenu,
602 lyx::toc::Toc const & toc_list,
603 lyx::toc::Toc::size_type from,
604 lyx::toc::Toc::size_type to, int depth)
606 int shortcut_count = 0;
607 if (to - from <= max_number_of_items) {
608 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
609 string label(4 * max(0, toc_list[i].depth - depth),' ');
610 label += limit_string_length(toc_list[i].str);
611 if (toc_list[i].depth == depth
612 && shortcut_count < 9) {
613 if (label.find(tostr(shortcut_count + 1)) != string::npos)
614 label += '|' + tostr(++shortcut_count);
616 tomenu.add(MenuItem(MenuItem::Command, label,
617 FuncRequest(toc_list[i].action())));
620 lyx::toc::Toc::size_type pos = from;
622 lyx::toc::Toc::size_type new_pos = pos + 1;
623 while (new_pos < to &&
624 toc_list[new_pos].depth > depth)
627 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
628 label += limit_string_length(toc_list[pos].str);
629 if (toc_list[pos].depth == depth &&
630 shortcut_count < 9) {
631 if (label.find(tostr(shortcut_count + 1)) != string::npos)
632 label += '|' + tostr(++shortcut_count);
634 if (new_pos == pos + 1) {
635 tomenu.add(MenuItem(MenuItem::Command,
636 label, FuncRequest(toc_list[pos].action())));
638 MenuItem item(MenuItem::Submenu, label);
639 item.submenu(new Menu);
640 expandToc2(*item.submenu(),
641 toc_list, pos, new_pos, depth + 1);
650 void expandToc(Menu & tomenu, LyXView const * view)
652 // To make things very cleanly, we would have to pass view to
653 // all MenuItem constructors and to expandToc2. However, we
654 // know that all the entries in a TOC will be have status_ ==
655 // OK, so we avoid this unnecessary overhead (JMarc)
657 if (!view->buffer()) {
658 tomenu.add(MenuItem(MenuItem::Command,
659 _("No Documents Open!"),
660 FuncRequest(LFUN_NOACTION)),
665 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
666 lyx::toc::TocList::const_iterator cit = toc_list.begin();
667 lyx::toc::TocList::const_iterator end = toc_list.end();
668 for (; cit != end; ++cit) {
670 if (cit->first == "TOC")
673 // All the rest is for floats
674 auto_ptr<Menu> menu(new Menu);
675 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
676 lyx::toc::Toc::const_iterator eend = cit->second.end();
677 for (; ccit != eend; ++ccit) {
678 string const label = limit_string_length(ccit->str);
679 menu->add(MenuItem(MenuItem::Command,
681 FuncRequest(ccit->action())));
683 string const & floatName = cit->first;
684 // Is the _(...) really needed here? (Lgb)
685 MenuItem item(MenuItem::Submenu, _(floatName));
686 item.submenu(menu.release());
691 cit = toc_list.find("TOC");
693 tomenu.add(MenuItem(MenuItem::Command,
694 _("No Table of contents"),
698 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
703 void expandPasteRecent(Menu & tomenu, LyXView const * view)
705 if (!view || !view->buffer())
708 vector<string> const sel =
709 lyx::cap::availableSelections(*view->buffer());
711 vector<string>::const_iterator cit = sel.begin();
712 vector<string>::const_iterator end = sel.end();
714 for (unsigned int index = 0; cit != end; ++cit, ++index) {
715 tomenu.add(MenuItem(MenuItem::Command, *cit,
716 FuncRequest(LFUN_PASTE, tostr(index))));
721 void expandBranches(Menu & tomenu, LyXView const * view)
723 if (!view || !view->buffer())
726 BufferParams const & params = view->buffer()->params();
728 std::list<Branch>::const_iterator cit = params.branchlist().begin();
729 std::list<Branch>::const_iterator end = params.branchlist().end();
731 for (int ii = 1; cit != end; ++cit, ++ii) {
732 string label = cit->getBranch();
734 label = tostr(ii) + ". " + label + "|" + tostr(ii);
735 tomenu.add(MenuItem(MenuItem::Command, label,
736 FuncRequest(LFUN_INSERT_BRANCH,
737 cit->getBranch())), view);
745 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
746 LyXView const * view) const
748 for (Menu::const_iterator cit = frommenu.begin();
749 cit != frommenu.end() ; ++cit) {
750 switch (cit->kind()) {
751 case MenuItem::Lastfiles:
752 expandLastfiles(tomenu, view);
755 case MenuItem::Documents:
756 expandDocuments(tomenu, view);
759 case MenuItem::ImportFormats:
760 case MenuItem::ViewFormats:
761 case MenuItem::UpdateFormats:
762 case MenuItem::ExportFormats:
763 expandFormats(cit->kind(), tomenu, view);
766 case MenuItem::CharStyles:
767 expandCharStyleInsert(tomenu, view);
770 case MenuItem::FloatListInsert:
771 expandFloatListInsert(tomenu, view);
774 case MenuItem::FloatInsert:
775 expandFloatInsert(tomenu, view);
778 case MenuItem::PasteRecent:
779 expandPasteRecent(tomenu, view);
782 case MenuItem::Branches:
783 expandBranches(tomenu, view);
787 expandToc(tomenu, view);
790 case MenuItem::Submenu: {
792 item.submenu(new Menu(cit->submenuname()));
793 expand(getMenu(cit->submenuname()),
794 *item.submenu(), view);
795 tomenu.add(item, view);
799 case MenuItem::Separator:
800 tomenu.add(*cit, view);
803 case MenuItem::Command:
805 || !specialmenu_->hasFunc(cit->func()))
806 tomenu.add(*cit, view);
810 // we do not want the menu to end with a separator
812 && tomenu.items_.back().kind() == MenuItem::Separator)
813 tomenu.items_.pop_back();
815 // Check whether the shortcuts are unique
816 tomenu.checkShortcuts();
820 void MenuBackend::read(LyXLex & lex)
829 struct keyword_item menutags[md_last - 1] = {
830 { "end", md_endmenuset },
832 { "menubar", md_menubar }
836 if (compare_ascii_no_case(lex.getString(), "menuset")) {
837 lyxerr << "Menubackend::read: ERROR wrong token:`"
838 << lex.getString() << '\'' << endl;
841 lex.pushTable(menutags, md_last - 1);
842 if (lyxerr.debugging(Debug::PARSER))
843 lex.printTable(lyxerr);
847 while (lex.isOK() && !quit) {
854 string const name = lex.getString();
856 getMenu(name).read(lex);
868 lex.printError("menubackend::read: "
869 "Unknown menu tag: `$$Token'");
877 void MenuBackend::add(Menu const & menu)
879 menulist_.push_back(menu);
883 bool MenuBackend::hasMenu(string const & name) const
885 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
889 Menu const & MenuBackend::getMenu(string const & name) const
891 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
893 lyxerr << "No submenu named " << name << endl;
894 BOOST_ASSERT(cit != end());
899 Menu & MenuBackend::getMenu(string const & name)
901 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
903 lyxerr << "No submenu named " << name << endl;
904 BOOST_ASSERT(it != end());
909 Menu const & MenuBackend::getMenubar() const