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>
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)
109 func_.origin = FuncRequest::UI;
113 MenuItem::~MenuItem()
117 void MenuItem::submenu(Menu * menu)
119 submenu_.reset(menu);
123 string const MenuItem::label() const
125 return token(label_, '|', 0);
129 string const MenuItem::shortcut() const
131 return token(label_, '|', 1);
135 string 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 bindings.begin()->print();
149 << lyxaction.getActionName(func_.action)
150 << '(' << func_.argument << ')' << endl;
157 Menu & Menu::add(MenuItem const & i, LyXView const * view)
166 case MenuItem::Command: {
168 view->getLyXFunc().getStatus(i.func());
170 || (!status.enabled() && i.optional()))
173 items_.back().status(status);
177 case MenuItem::Submenu: {
179 bool enabled = false;
180 for (const_iterator cit = i.submenu()->begin();
181 cit != i.submenu()->end(); ++cit) {
182 if ((cit->kind() == MenuItem::Command
183 || cit->kind() == MenuItem::Submenu)
184 && cit->status().enabled()) {
189 if (enabled || !i.optional()) {
191 items_.back().status().enabled(enabled);
199 case MenuItem::Separator:
201 && items_.back().kind() != MenuItem::Separator)
213 Menu & Menu::read(LyXLex & lex)
237 struct keyword_item menutags[md_last - 1] = {
238 { "branches", md_branches },
239 { "charstyles", md_charstyles },
240 { "documents", md_documents },
241 { "end", md_endmenu },
242 { "exportformats", md_exportformats },
243 { "floatinsert", md_floatinsert },
244 { "floatlistinsert", md_floatlistinsert },
245 { "importformats", md_importformats },
247 { "lastfiles", md_lastfiles },
248 { "optitem", md_optitem },
249 { "optsubmenu", md_optsubmenu },
250 { "pasterecent", md_pasterecent },
251 { "separator", md_separator },
252 { "submenu", md_submenu },
254 { "updateformats", md_updateformats },
255 { "viewformats", md_viewformats }
258 lex.pushTable(menutags, md_last - 1);
259 if (lyxerr.debugging(Debug::PARSER))
260 lex.printTable(lyxerr);
263 bool optional = false;
265 while (lex.isOK() && !quit) {
269 // fallback to md_item
272 string const name = _(lex.getString());
274 string const command = lex.getString();
275 FuncRequest func = lyxaction.lookupFunc(command);
276 add(MenuItem(MenuItem::Command, name, func, optional));
282 add(MenuItem(MenuItem::Separator));
286 add(MenuItem(MenuItem::Lastfiles));
290 add(MenuItem(MenuItem::CharStyles));
294 add(MenuItem(MenuItem::Documents));
298 add(MenuItem(MenuItem::Toc));
302 add(MenuItem(MenuItem::ViewFormats));
305 case md_updateformats:
306 add(MenuItem(MenuItem::UpdateFormats));
309 case md_exportformats:
310 add(MenuItem(MenuItem::ExportFormats));
313 case md_importformats:
314 add(MenuItem(MenuItem::ImportFormats));
317 case md_floatlistinsert:
318 add(MenuItem(MenuItem::FloatListInsert));
322 add(MenuItem(MenuItem::FloatInsert));
326 add(MenuItem(MenuItem::PasteRecent));
330 add(MenuItem(MenuItem::Branches));
335 // fallback to md_submenu
338 string const mlabel = _(lex.getString());
340 string const mname = lex.getString();
341 add(MenuItem(MenuItem::Submenu, mlabel, mname,
352 lex.printError("Menu::read: "
353 "Unknown menu tag: `$$Token'");
362 MenuItem const & Menu::operator[](size_type i) const
368 bool Menu::hasFunc(FuncRequest const & func) const
370 return find_if(begin(), end(),
371 bind(std::equal_to<FuncRequest>(),
372 bind(&MenuItem::func, _1),
376 void Menu::checkShortcuts() const
378 // This is a quadratic algorithm, but we do not care because
379 // menus are short enough
380 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
381 string shortcut = it1->shortcut();
382 if (shortcut.empty())
384 if (!contains(it1->label(), shortcut))
385 lyxerr << "Menu warning: menu entry \""
387 << "\" does not contain shortcut `"
388 << shortcut << "'." << endl;
389 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
390 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
391 lyxerr << "Menu warning: menu entries "
392 << '"' << it1->fulllabel()
393 << "\" and \"" << it2->fulllabel()
394 << "\" share the same shortcut."
402 void MenuBackend::specialMenu(string const &name)
405 specialmenu_ = &getMenu(name);
411 class compare_format {
413 bool operator()(Format const * p1, Format const * p2) {
418 string const limit_string_length(string const & str)
420 string::size_type const max_item_length = 45;
422 if (str.size() > max_item_length)
423 return str.substr(0, max_item_length - 3) + "...";
429 void expandLastfiles(Menu & tomenu, LyXView const * view)
431 lyx::Session::LastFiles const & lf = LyX::cref().session().lastFiles();
432 lyx::Session::LastFiles::const_iterator lfit = lf.begin();
436 for (; lfit != lf.end() && ii < 10; ++lfit, ++ii) {
437 string const label = convert<string>(ii) + ". "
438 + makeDisplayPath((*lfit), 30)
439 + '|' + convert<string>(ii);
440 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
445 void expandDocuments(Menu & tomenu, LyXView const * view)
447 typedef vector<string> Strings;
448 Strings const names = bufferlist.getFileNames();
451 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
452 FuncRequest(LFUN_NOACTION)), view);
457 Strings::const_iterator docit = names.begin();
458 Strings::const_iterator end = names.end();
459 for (; docit != end; ++docit, ++ii) {
460 string label = makeDisplayPath(*docit, 20);
462 label = convert<string>(ii) + ". " + label + '|' + convert<string>(ii);
463 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BUFFER_SWITCH, *docit)), view);
468 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
470 if (!view->buffer() && kind != MenuItem::ImportFormats) {
471 tomenu.add(MenuItem(MenuItem::Command,
472 _("No Documents Open!"),
473 FuncRequest(LFUN_NOACTION)),
478 typedef vector<Format const *> Formats;
483 case MenuItem::ImportFormats:
484 formats = Importer::GetImportableFormats();
485 action = LFUN_BUFFER_IMPORT;
487 case MenuItem::ViewFormats:
488 formats = Exporter::getExportableFormats(*view->buffer(), true);
489 action = LFUN_BUFFER_VIEW;
491 case MenuItem::UpdateFormats:
492 formats = Exporter::getExportableFormats(*view->buffer(), true);
493 action = LFUN_BUFFER_UPDATE;
496 formats = Exporter::getExportableFormats(*view->buffer(), false);
497 action = LFUN_BUFFER_EXPORT;
499 sort(formats.begin(), formats.end(), compare_format());
501 Formats::const_iterator fit = formats.begin();
502 Formats::const_iterator end = formats.end();
503 for (; fit != end ; ++fit) {
506 string label = (*fit)->prettyname();
509 case MenuItem::ImportFormats:
510 if ((*fit)->name() == "text")
511 label = _("Plain Text as Lines");
512 else if ((*fit)->name() == "textparagraph")
513 label = _("Plain Text as Paragraphs");
516 case MenuItem::ViewFormats:
517 case MenuItem::ExportFormats:
518 case MenuItem::UpdateFormats:
519 if (!(*fit)->documentFormat())
526 if (!(*fit)->shortcut().empty())
527 label += '|' + (*fit)->shortcut();
529 tomenu.add(MenuItem(MenuItem::Command, label,
530 FuncRequest(action, (*fit)->name())),
536 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
538 if (!view->buffer()) {
539 tomenu.add(MenuItem(MenuItem::Command,
540 _("No Documents Open!"),
541 FuncRequest(LFUN_NOACTION)),
546 FloatList const & floats =
547 view->buffer()->params().getLyXTextClass().floats();
548 FloatList::const_iterator cit = floats.begin();
549 FloatList::const_iterator end = floats.end();
550 for (; cit != end; ++cit) {
551 tomenu.add(MenuItem(MenuItem::Command,
552 _(cit->second.listName()),
553 FuncRequest(LFUN_FLOAT_LIST,
554 cit->second.type())),
560 void expandFloatInsert(Menu & tomenu, LyXView const * view)
562 if (!view->buffer()) {
563 tomenu.add(MenuItem(MenuItem::Command,
564 _("No Documents Open!"),
565 FuncRequest(LFUN_NOACTION)),
570 FloatList const & floats =
571 view->buffer()->params().getLyXTextClass().floats();
572 FloatList::const_iterator cit = floats.begin();
573 FloatList::const_iterator end = floats.end();
574 for (; cit != end; ++cit) {
576 string const label = _(cit->second.name());
577 tomenu.add(MenuItem(MenuItem::Command, label,
578 FuncRequest(LFUN_FLOAT_INSERT,
579 cit->second.type())),
585 void expandCharStyleInsert(Menu & tomenu, LyXView const * view)
587 if (!view->buffer()) {
588 tomenu.add(MenuItem(MenuItem::Command,
589 _("No Documents Open!"),
590 FuncRequest(LFUN_NOACTION)),
594 CharStyles & charstyles =
595 view->buffer()->params().getLyXTextClass().charstyles();
596 CharStyles::iterator cit = charstyles.begin();
597 CharStyles::iterator end = charstyles.end();
598 for (; cit != end; ++cit) {
599 string const label = cit->name;
600 tomenu.add(MenuItem(MenuItem::Command, label,
601 FuncRequest(LFUN_CHARSTYLE_INSERT,
607 Menu::size_type const max_number_of_items = 25;
609 void expandToc2(Menu & tomenu,
610 lyx::toc::Toc const & toc_list,
611 lyx::toc::Toc::size_type from,
612 lyx::toc::Toc::size_type to, int depth)
614 int shortcut_count = 0;
616 // check whether depth is smaller than the smallest depth in toc.
617 int min_depth = 1000;
618 for (lyx::toc::Toc::size_type i = from; i < to; ++i)
619 min_depth = std::min(min_depth, toc_list[i].depth());
620 if (min_depth > depth)
624 if (to - from <= max_number_of_items) {
625 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
626 string label(4 * max(0, toc_list[i].depth() - depth),' ');
627 label += limit_string_length(toc_list[i].str());
628 if (toc_list[i].depth() == depth
629 && shortcut_count < 9) {
630 if (label.find(convert<string>(shortcut_count + 1)) != string::npos)
631 label += '|' + convert<string>(++shortcut_count);
633 tomenu.add(MenuItem(MenuItem::Command, label,
634 FuncRequest(toc_list[i].action())));
637 lyx::toc::Toc::size_type pos = from;
639 lyx::toc::Toc::size_type new_pos = pos + 1;
640 while (new_pos < to &&
641 toc_list[new_pos].depth() > depth)
644 string label(4 * max(0, toc_list[pos].depth() - depth), ' ');
645 label += limit_string_length(toc_list[pos].str());
646 if (toc_list[pos].depth() == depth &&
647 shortcut_count < 9) {
648 if (label.find(convert<string>(shortcut_count + 1)) != string::npos)
649 label += '|' + convert<string>(++shortcut_count);
651 if (new_pos == pos + 1) {
652 tomenu.add(MenuItem(MenuItem::Command,
653 label, FuncRequest(toc_list[pos].action())));
655 MenuItem item(MenuItem::Submenu, label);
656 item.submenu(new Menu);
657 expandToc2(*item.submenu(),
658 toc_list, pos, new_pos, depth + 1);
667 void expandToc(Menu & tomenu, LyXView const * view)
669 // To make things very cleanly, we would have to pass view to
670 // all MenuItem constructors and to expandToc2. However, we
671 // know that all the entries in a TOC will be have status_ ==
672 // OK, so we avoid this unnecessary overhead (JMarc)
675 Buffer const * buf = view->buffer();
677 tomenu.add(MenuItem(MenuItem::Command,
678 _("No Documents Open!"),
679 FuncRequest(LFUN_NOACTION)),
684 FloatList const & floatlist = buf->params().getLyXTextClass().floats();
685 lyx::toc::TocList const & toc_list = lyx::toc::getTocList(*buf);
686 lyx::toc::TocList::const_iterator cit = toc_list.begin();
687 lyx::toc::TocList::const_iterator end = toc_list.end();
688 for (; cit != end; ++cit) {
690 if (cit->first == "TOC")
693 // All the rest is for floats
694 auto_ptr<Menu> menu(new Menu);
695 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
696 lyx::toc::Toc::const_iterator eend = cit->second.end();
697 for (; ccit != eend; ++ccit) {
698 string const label = limit_string_length(ccit->str());
699 menu->add(MenuItem(MenuItem::Command,
701 FuncRequest(ccit->action())));
703 string const & floatName = floatlist.getType(cit->first).listName();
704 MenuItem item(MenuItem::Submenu, _(floatName));
705 item.submenu(menu.release());
710 cit = toc_list.find("TOC");
712 tomenu.add(MenuItem(MenuItem::Command,
713 _("No Table of contents"),
717 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
722 void expandPasteRecent(Menu & tomenu, LyXView const * view)
724 if (!view || !view->buffer())
727 vector<string> const sel =
728 lyx::cap::availableSelections(*view->buffer());
730 vector<string>::const_iterator cit = sel.begin();
731 vector<string>::const_iterator end = sel.end();
733 for (unsigned int index = 0; cit != end; ++cit, ++index) {
734 tomenu.add(MenuItem(MenuItem::Command, *cit,
735 FuncRequest(LFUN_PASTE, convert<string>(index))));
740 void expandBranches(Menu & tomenu, LyXView const * view)
742 if (!view || !view->buffer())
745 BufferParams const & params = view->buffer()->getMasterBuffer()->params();
747 BranchList::const_iterator cit = params.branchlist().begin();
748 BranchList::const_iterator end = params.branchlist().end();
750 for (int ii = 1; cit != end; ++cit, ++ii) {
751 string label = cit->getBranch();
753 label = convert<string>(ii) + ". " + label + "|" + convert<string>(ii);
754 tomenu.add(MenuItem(MenuItem::Command, label,
755 FuncRequest(LFUN_BRANCH_INSERT,
756 cit->getBranch())), view);
764 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
765 LyXView const * view) const
770 for (Menu::const_iterator cit = frommenu.begin();
771 cit != frommenu.end() ; ++cit) {
772 switch (cit->kind()) {
773 case MenuItem::Lastfiles:
774 expandLastfiles(tomenu, view);
777 case MenuItem::Documents:
778 expandDocuments(tomenu, view);
781 case MenuItem::ImportFormats:
782 case MenuItem::ViewFormats:
783 case MenuItem::UpdateFormats:
784 case MenuItem::ExportFormats:
785 expandFormats(cit->kind(), tomenu, view);
788 case MenuItem::CharStyles:
789 expandCharStyleInsert(tomenu, view);
792 case MenuItem::FloatListInsert:
793 expandFloatListInsert(tomenu, view);
796 case MenuItem::FloatInsert:
797 expandFloatInsert(tomenu, view);
800 case MenuItem::PasteRecent:
801 expandPasteRecent(tomenu, view);
804 case MenuItem::Branches:
805 expandBranches(tomenu, view);
809 expandToc(tomenu, view);
812 case MenuItem::Submenu: {
814 item.submenu(new Menu(cit->submenuname()));
815 expand(getMenu(cit->submenuname()),
816 *item.submenu(), view);
817 tomenu.add(item, view);
821 case MenuItem::Separator:
822 tomenu.add(*cit, view);
825 case MenuItem::Command:
827 || !specialmenu_->hasFunc(cit->func()))
828 tomenu.add(*cit, view);
832 // we do not want the menu to end with a separator
834 && tomenu.items_.back().kind() == MenuItem::Separator)
835 tomenu.items_.pop_back();
837 // Check whether the shortcuts are unique
838 tomenu.checkShortcuts();
842 void MenuBackend::read(LyXLex & lex)
851 struct keyword_item menutags[md_last - 1] = {
852 { "end", md_endmenuset },
854 { "menubar", md_menubar }
858 if (compare_ascii_no_case(lex.getString(), "menuset")) {
859 lyxerr << "Menubackend::read: ERROR wrong token:`"
860 << lex.getString() << '\'' << endl;
863 lex.pushTable(menutags, md_last - 1);
864 if (lyxerr.debugging(Debug::PARSER))
865 lex.printTable(lyxerr);
869 while (lex.isOK() && !quit) {
876 string const name = lex.getString();
878 getMenu(name).read(lex);
890 lex.printError("menubackend::read: "
891 "Unknown menu tag: `$$Token'");
899 void MenuBackend::add(Menu const & menu)
901 menulist_.push_back(menu);
905 bool MenuBackend::hasMenu(string const & name) const
907 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
911 Menu const & MenuBackend::getMenu(string const & name) const
913 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
915 lyxerr << "No submenu named " << name << endl;
916 BOOST_ASSERT(cit != end());
921 Menu & MenuBackend::getMenu(string const & name)
923 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
925 lyxerr << "No submenu named " << name << endl;
926 BOOST_ASSERT(it != end());
931 Menu const & MenuBackend::getMenubar() const