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 void Menu::checkShortcuts() const
362 // This is a quadratic algorithm, but we do not care because
363 // menus are short enough
364 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
365 string shortcut = it1->shortcut();
366 if (shortcut.empty())
368 if (!contains(it1->label(), shortcut))
369 lyxerr << "Menu warning: menu entry \""
371 << "\" does not contain shortcut `"
372 << shortcut << "'." << endl;
373 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
374 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
375 lyxerr << "Menu warning: menu entries "
376 << '"' << it1->fulllabel()
377 << "\" and \"" << it2->fulllabel()
378 << "\" share the same shortcut."
388 class compare_format {
390 bool operator()(Format const * p1, Format const * p2) {
395 string const limit_string_length(string const & str)
397 string::size_type const max_item_length = 45;
399 if (str.size() > max_item_length)
400 return str.substr(0, max_item_length - 3) + "...";
406 void expandLastfiles(Menu & tomenu, LyXView const * view)
408 LastFiles const & lastfiles = LyX::cref().lastfiles();
411 LastFiles::const_iterator lfit = lastfiles.begin();
412 LastFiles::const_iterator end = lastfiles.end();
414 for (; lfit != end && ii < 10; ++lfit, ++ii) {
415 string const label = tostr(ii) + ". "
416 + MakeDisplayPath((*lfit), 30)
418 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
422 void expandDocuments(Menu & tomenu, LyXView const * view)
424 typedef vector<string> Strings;
425 Strings const names = bufferlist.getFileNames();
428 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
429 FuncRequest(LFUN_NOACTION)), view);
434 Strings::const_iterator docit = names.begin();
435 Strings::const_iterator end = names.end();
436 for (; docit != end; ++docit, ++ii) {
437 string label = MakeDisplayPath(*docit, 20);
439 label = tostr(ii) + ". " + label + '|' + tostr(ii);
440 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_SWITCHBUFFER, *docit)), view);
445 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
447 if (!view->buffer() && kind != MenuItem::ImportFormats) {
448 tomenu.add(MenuItem(MenuItem::Command,
449 _("No Documents Open!"),
450 FuncRequest(LFUN_NOACTION)),
455 typedef vector<Format const *> Formats;
460 case MenuItem::ImportFormats:
461 formats = Importer::GetImportableFormats();
462 action = LFUN_IMPORT;
464 case MenuItem::ViewFormats:
465 formats = Exporter::GetExportableFormats(*view->buffer(), true);
466 action = LFUN_PREVIEW;
468 case MenuItem::UpdateFormats:
469 formats = Exporter::GetExportableFormats(*view->buffer(), true);
470 action = LFUN_UPDATE;
473 formats = Exporter::GetExportableFormats(*view->buffer(), false);
474 action = LFUN_EXPORT;
476 sort(formats.begin(), formats.end(), compare_format());
478 Formats::const_iterator fit = formats.begin();
479 Formats::const_iterator end = formats.end();
480 for (; fit != end ; ++fit) {
483 string label = (*fit)->prettyname();
484 // we need to hide the default graphic export formats
485 // from the external menu, because we need them only
486 // for the internal lyx-view and external latex run
487 if (label == "EPS" || label == "XPM" || label == "PNG")
490 if (kind == MenuItem::ImportFormats) {
491 if ((*fit)->name() == "text")
492 label = _("ASCII text as lines");
493 else if ((*fit)->name() == "textparagraph")
494 label = _("ASCII text as paragraphs");
497 if (!(*fit)->shortcut().empty())
498 label += '|' + (*fit)->shortcut();
500 tomenu.add(MenuItem(MenuItem::Command, label,
501 FuncRequest(action, (*fit)->name())),
507 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
509 if (!view->buffer()) {
510 tomenu.add(MenuItem(MenuItem::Command,
511 _("No Documents Open!"),
512 FuncRequest(LFUN_NOACTION)),
517 FloatList const & floats =
518 view->buffer()->params().getLyXTextClass().floats();
519 FloatList::const_iterator cit = floats.begin();
520 FloatList::const_iterator end = floats.end();
521 for (; cit != end; ++cit) {
522 tomenu.add(MenuItem(MenuItem::Command,
523 _(cit->second.listName()),
524 FuncRequest(LFUN_FLOAT_LIST,
525 cit->second.type())),
531 void expandFloatInsert(Menu & tomenu, LyXView const * view)
533 if (!view->buffer()) {
534 tomenu.add(MenuItem(MenuItem::Command,
535 _("No Documents Open!"),
536 FuncRequest(LFUN_NOACTION)),
541 FloatList const & floats =
542 view->buffer()->params().getLyXTextClass().floats();
543 FloatList::const_iterator cit = floats.begin();
544 FloatList::const_iterator end = floats.end();
545 for (; cit != end; ++cit) {
547 string const label = _(cit->second.name());
548 tomenu.add(MenuItem(MenuItem::Command, label,
549 FuncRequest(LFUN_INSET_FLOAT,
550 cit->second.type())),
556 void expandCharStyleInsert(Menu & tomenu, LyXView const * view)
558 if (!view->buffer()) {
559 tomenu.add(MenuItem(MenuItem::Command,
560 _("No Documents Open!"),
561 FuncRequest(LFUN_NOACTION)),
565 CharStyles & charstyles =
566 view->buffer()->params().getLyXTextClass().charstyles();
567 CharStyles::iterator cit = charstyles.begin();
568 CharStyles::iterator end = charstyles.end();
569 for (; cit != end; ++cit) {
570 string const label = cit->name;
571 tomenu.add(MenuItem(MenuItem::Command, label,
572 FuncRequest(LFUN_INSERT_CHARSTYLE,
578 Menu::size_type const max_number_of_items = 25;
580 void expandToc2(Menu & tomenu,
581 lyx::toc::Toc const & toc_list,
582 lyx::toc::Toc::size_type from,
583 lyx::toc::Toc::size_type to, int depth)
585 int shortcut_count = 0;
586 if (to - from <= max_number_of_items) {
587 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
588 string label(4 * max(0, toc_list[i].depth - depth),' ');
589 label += limit_string_length(toc_list[i].str);
590 if (toc_list[i].depth == depth
591 && shortcut_count < 9) {
592 if (label.find(tostr(shortcut_count + 1)) != string::npos)
593 label += '|' + tostr(++shortcut_count);
595 tomenu.add(MenuItem(MenuItem::Command, label,
596 FuncRequest(toc_list[i].action())));
599 lyx::toc::Toc::size_type pos = from;
601 lyx::toc::Toc::size_type new_pos = pos + 1;
602 while (new_pos < to &&
603 toc_list[new_pos].depth > depth)
606 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
607 label += limit_string_length(toc_list[pos].str);
608 if (toc_list[pos].depth == depth &&
609 shortcut_count < 9) {
610 if (label.find(tostr(shortcut_count + 1)) != string::npos)
611 label += '|' + tostr(++shortcut_count);
613 if (new_pos == pos + 1) {
614 tomenu.add(MenuItem(MenuItem::Command,
615 label, FuncRequest(toc_list[pos].action())));
617 MenuItem item(MenuItem::Submenu, label);
618 item.submenu(new Menu);
619 expandToc2(*item.submenu(),
620 toc_list, pos, new_pos, depth + 1);
629 void expandToc(Menu & tomenu, LyXView const * view)
631 // To make things very cleanly, we would have to pass view to
632 // all MenuItem constructors and to expandToc2. However, we
633 // know that all the entries in a TOC will be have status_ ==
634 // OK, so we avoid this unnecessary overhead (JMarc)
636 if (!view->buffer()) {
637 tomenu.add(MenuItem(MenuItem::Command,
638 _("No Documents Open!"),
639 FuncRequest(LFUN_NOACTION)),
644 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
645 lyx::toc::TocList::const_iterator cit = toc_list.begin();
646 lyx::toc::TocList::const_iterator end = toc_list.end();
647 for (; cit != end; ++cit) {
649 if (cit->first == "TOC")
652 // All the rest is for floats
653 auto_ptr<Menu> menu(new Menu);
654 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
655 lyx::toc::Toc::const_iterator eend = cit->second.end();
656 for (; ccit != eend; ++ccit) {
657 string const label = limit_string_length(ccit->str);
658 menu->add(MenuItem(MenuItem::Command,
660 FuncRequest(ccit->action())));
662 string const & floatName = cit->first;
663 // Is the _(...) really needed here? (Lgb)
664 MenuItem item(MenuItem::Submenu, _(floatName));
665 item.submenu(menu.release());
670 cit = toc_list.find("TOC");
672 tomenu.add(MenuItem(MenuItem::Command,
673 _("No Table of contents"),
677 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
682 void expandPasteRecent(Menu & tomenu, LyXView const * view)
684 if (!view || !view->buffer())
687 vector<string> const sel =
688 lyx::cap::availableSelections(*view->buffer());
690 vector<string>::const_iterator cit = sel.begin();
691 vector<string>::const_iterator end = sel.end();
693 for (unsigned int index = 0; cit != end; ++cit, ++index) {
694 tomenu.add(MenuItem(MenuItem::Command, *cit,
695 FuncRequest(LFUN_PASTE, tostr(index))));
700 void expandBranches(Menu & tomenu, LyXView const * view)
702 if (!view || !view->buffer())
705 BufferParams const & params = view->buffer()->params();
707 std::list<Branch>::const_iterator cit = params.branchlist().begin();
708 std::list<Branch>::const_iterator end = params.branchlist().end();
710 for (int ii = 1; cit != end; ++cit, ++ii) {
711 string label = cit->getBranch();
713 label = tostr(ii) + ". " + label + "|" + tostr(ii);
714 tomenu.add(MenuItem(MenuItem::Command, label,
715 FuncRequest(LFUN_INSERT_BRANCH,
716 cit->getBranch())), view);
724 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
725 LyXView const * view) const
727 for (Menu::const_iterator cit = frommenu.begin();
728 cit != frommenu.end() ; ++cit) {
729 switch (cit->kind()) {
730 case MenuItem::Lastfiles:
731 expandLastfiles(tomenu, view);
734 case MenuItem::Documents:
735 expandDocuments(tomenu, view);
738 case MenuItem::ImportFormats:
739 case MenuItem::ViewFormats:
740 case MenuItem::UpdateFormats:
741 case MenuItem::ExportFormats:
742 expandFormats(cit->kind(), tomenu, view);
745 case MenuItem::CharStyles:
746 expandCharStyleInsert(tomenu, view);
749 case MenuItem::FloatListInsert:
750 expandFloatListInsert(tomenu, view);
753 case MenuItem::FloatInsert:
754 expandFloatInsert(tomenu, view);
757 case MenuItem::PasteRecent:
758 expandPasteRecent(tomenu, view);
761 case MenuItem::Branches:
762 expandBranches(tomenu, view);
766 expandToc(tomenu, view);
769 case MenuItem::Submenu: {
771 item.submenu(new Menu(cit->submenuname()));
772 expand(getMenu(cit->submenuname()),
773 *item.submenu(), view);
774 tomenu.add(item, view);
779 tomenu.add(*cit, view);
783 // we do not want the menu to end with a separator
785 && tomenu.items_.back().kind() == MenuItem::Separator)
786 tomenu.items_.pop_back();
788 // Check whether the shortcuts are unique
789 tomenu.checkShortcuts();
793 bool Menu::hasSubmenu(string const & name) const
796 return find_if(begin(), end(),
797 bind(std::equal_to<string>(),
798 bind(&MenuItem::submenuname, _1),
801 // I would have prefered this, but I am not sure if it
802 // makes a difference. (Lgb)
804 make_transform_iterator(begin(),
805 bind(&MenuItem::submenuname, _1)),
806 make_transform_iterator(end(),
807 bind(&MenuItem::submenuname, _1)),
814 void MenuBackend::read(LyXLex & lex)
823 struct keyword_item menutags[md_last - 1] = {
824 { "end", md_endmenuset },
826 { "menubar", md_menubar }
830 if (compare_ascii_no_case(lex.getString(), "menuset")) {
831 lyxerr << "Menubackend::read: ERROR wrong token:`"
832 << lex.getString() << '\'' << endl;
835 lex.pushTable(menutags, md_last - 1);
836 if (lyxerr.debugging(Debug::PARSER))
837 lex.printTable(lyxerr);
841 while (lex.isOK() && !quit) {
848 string const name = lex.getString();
850 getMenu(name).read(lex);
862 lex.printError("menubackend::read: "
863 "Unknown menu tag: `$$Token'");
871 void MenuBackend::add(Menu const & menu)
873 menulist_.push_back(menu);
877 bool MenuBackend::hasMenu(string const & name) const
879 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
883 Menu const & MenuBackend::getMenu(string const & name) const
885 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
887 lyxerr << "No submenu named " << name << endl;
888 BOOST_ASSERT(cit != end());
893 Menu & MenuBackend::getMenu(string const & name)
895 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
897 lyxerr << "No submenu named " << name << endl;
898 BOOST_ASSERT(it != end());
903 Menu const & MenuBackend::getMenubar() const