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/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 LastFiles const & lastfiles = LyX::cref().lastfiles();
434 LastFiles::const_iterator lfit = lastfiles.begin();
435 LastFiles::const_iterator end = lastfiles.end();
437 for (; lfit != end && ii < 10; ++lfit, ++ii) {
438 string const label = convert<string>(ii) + ". "
439 + MakeDisplayPath((*lfit), 30)
440 + '|' + convert<string>(ii);
441 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
446 void expandDocuments(Menu & tomenu, LyXView const * view)
448 typedef vector<string> Strings;
449 Strings const names = bufferlist.getFileNames();
452 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
453 FuncRequest(LFUN_NOACTION)), view);
458 Strings::const_iterator docit = names.begin();
459 Strings::const_iterator end = names.end();
460 for (; docit != end; ++docit, ++ii) {
461 string label = MakeDisplayPath(*docit, 20);
463 label = convert<string>(ii) + ". " + label + '|' + convert<string>(ii);
464 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_SWITCHBUFFER, *docit)), view);
469 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
471 if (!view->buffer() && kind != MenuItem::ImportFormats) {
472 tomenu.add(MenuItem(MenuItem::Command,
473 _("No Documents Open!"),
474 FuncRequest(LFUN_NOACTION)),
479 typedef vector<Format const *> Formats;
484 case MenuItem::ImportFormats:
485 formats = Importer::GetImportableFormats();
486 action = LFUN_IMPORT;
488 case MenuItem::ViewFormats:
489 formats = Exporter::GetExportableFormats(*view->buffer(), true);
490 action = LFUN_PREVIEW;
492 case MenuItem::UpdateFormats:
493 formats = Exporter::GetExportableFormats(*view->buffer(), true);
494 action = LFUN_UPDATE;
497 formats = Exporter::GetExportableFormats(*view->buffer(), false);
498 action = LFUN_EXPORT;
500 sort(formats.begin(), formats.end(), compare_format());
502 Formats::const_iterator fit = formats.begin();
503 Formats::const_iterator end = formats.end();
504 for (; fit != end ; ++fit) {
507 string label = (*fit)->prettyname();
508 // we need to hide the default graphic export formats
509 // from the external menu, because we need them only
510 // for the internal lyx-view and external latex run
511 if (label == "EPS" || label == "XPM" || label == "PNG")
514 if (kind == MenuItem::ImportFormats) {
515 if ((*fit)->name() == "text")
516 label = _("Plain Text as Lines");
517 else if ((*fit)->name() == "textparagraph")
518 label = _("Plain Text as Paragraphs");
521 if (!(*fit)->shortcut().empty())
522 label += '|' + (*fit)->shortcut();
524 tomenu.add(MenuItem(MenuItem::Command, label,
525 FuncRequest(action, (*fit)->name())),
531 void expandFloatListInsert(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) {
546 tomenu.add(MenuItem(MenuItem::Command,
547 _(cit->second.listName()),
548 FuncRequest(LFUN_FLOAT_LIST,
549 cit->second.type())),
555 void expandFloatInsert(Menu & tomenu, LyXView const * view)
557 if (!view->buffer()) {
558 tomenu.add(MenuItem(MenuItem::Command,
559 _("No Documents Open!"),
560 FuncRequest(LFUN_NOACTION)),
565 FloatList const & floats =
566 view->buffer()->params().getLyXTextClass().floats();
567 FloatList::const_iterator cit = floats.begin();
568 FloatList::const_iterator end = floats.end();
569 for (; cit != end; ++cit) {
571 string const label = _(cit->second.name());
572 tomenu.add(MenuItem(MenuItem::Command, label,
573 FuncRequest(LFUN_INSET_FLOAT,
574 cit->second.type())),
580 void expandCharStyleInsert(Menu & tomenu, LyXView const * view)
582 if (!view->buffer()) {
583 tomenu.add(MenuItem(MenuItem::Command,
584 _("No Documents Open!"),
585 FuncRequest(LFUN_NOACTION)),
589 CharStyles & charstyles =
590 view->buffer()->params().getLyXTextClass().charstyles();
591 CharStyles::iterator cit = charstyles.begin();
592 CharStyles::iterator end = charstyles.end();
593 for (; cit != end; ++cit) {
594 string const label = cit->name;
595 tomenu.add(MenuItem(MenuItem::Command, label,
596 FuncRequest(LFUN_INSERT_CHARSTYLE,
602 Menu::size_type const max_number_of_items = 25;
604 void expandToc2(Menu & tomenu,
605 lyx::toc::Toc const & toc_list,
606 lyx::toc::Toc::size_type from,
607 lyx::toc::Toc::size_type to, int depth)
609 int shortcut_count = 0;
611 // check whether depth is smaller than the smallest depth in toc.
612 int min_depth = 1000;
613 for (lyx::toc::Toc::size_type i = from; i < to; ++i)
614 min_depth = std::min(min_depth, toc_list[i].depth);
615 if (min_depth > depth)
619 if (to - from <= max_number_of_items) {
620 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
621 string label(4 * max(0, toc_list[i].depth - depth),' ');
622 label += limit_string_length(toc_list[i].str);
623 if (toc_list[i].depth == depth
624 && shortcut_count < 9) {
625 if (label.find(convert<string>(shortcut_count + 1)) != string::npos)
626 label += '|' + convert<string>(++shortcut_count);
628 tomenu.add(MenuItem(MenuItem::Command, label,
629 FuncRequest(toc_list[i].action())));
632 lyx::toc::Toc::size_type pos = from;
634 lyx::toc::Toc::size_type new_pos = pos + 1;
635 while (new_pos < to &&
636 toc_list[new_pos].depth > depth)
639 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
640 label += limit_string_length(toc_list[pos].str);
641 if (toc_list[pos].depth == depth &&
642 shortcut_count < 9) {
643 if (label.find(convert<string>(shortcut_count + 1)) != string::npos)
644 label += '|' + convert<string>(++shortcut_count);
646 if (new_pos == pos + 1) {
647 tomenu.add(MenuItem(MenuItem::Command,
648 label, FuncRequest(toc_list[pos].action())));
650 MenuItem item(MenuItem::Submenu, label);
651 item.submenu(new Menu);
652 expandToc2(*item.submenu(),
653 toc_list, pos, new_pos, depth + 1);
662 void expandToc(Menu & tomenu, LyXView const * view)
664 // To make things very cleanly, we would have to pass view to
665 // all MenuItem constructors and to expandToc2. However, we
666 // know that all the entries in a TOC will be have status_ ==
667 // OK, so we avoid this unnecessary overhead (JMarc)
669 if (!view->buffer()) {
670 tomenu.add(MenuItem(MenuItem::Command,
671 _("No Documents Open!"),
672 FuncRequest(LFUN_NOACTION)),
677 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
678 lyx::toc::TocList::const_iterator cit = toc_list.begin();
679 lyx::toc::TocList::const_iterator end = toc_list.end();
680 for (; cit != end; ++cit) {
682 if (cit->first == "TOC")
685 // All the rest is for floats
686 auto_ptr<Menu> menu(new Menu);
687 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
688 lyx::toc::Toc::const_iterator eend = cit->second.end();
689 for (; ccit != eend; ++ccit) {
690 string const label = limit_string_length(ccit->str);
691 menu->add(MenuItem(MenuItem::Command,
693 FuncRequest(ccit->action())));
695 string const & floatName = cit->first;
696 // Is the _(...) really needed here? (Lgb)
697 MenuItem item(MenuItem::Submenu, _(floatName));
698 item.submenu(menu.release());
703 cit = toc_list.find("TOC");
705 tomenu.add(MenuItem(MenuItem::Command,
706 _("No Table of contents"),
710 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
715 void expandPasteRecent(Menu & tomenu, LyXView const * view)
717 if (!view || !view->buffer())
720 vector<string> const sel =
721 lyx::cap::availableSelections(*view->buffer());
723 vector<string>::const_iterator cit = sel.begin();
724 vector<string>::const_iterator end = sel.end();
726 for (unsigned int index = 0; cit != end; ++cit, ++index) {
727 tomenu.add(MenuItem(MenuItem::Command, *cit,
728 FuncRequest(LFUN_PASTE, convert<string>(index))));
733 void expandBranches(Menu & tomenu, LyXView const * view)
735 if (!view || !view->buffer())
738 BufferParams const & params = view->buffer()->params();
740 std::list<Branch>::const_iterator cit = params.branchlist().begin();
741 std::list<Branch>::const_iterator end = params.branchlist().end();
743 for (int ii = 1; cit != end; ++cit, ++ii) {
744 string label = cit->getBranch();
746 label = convert<string>(ii) + ". " + label + "|" + convert<string>(ii);
747 tomenu.add(MenuItem(MenuItem::Command, label,
748 FuncRequest(LFUN_INSERT_BRANCH,
749 cit->getBranch())), view);
757 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
758 LyXView const * view) const
760 for (Menu::const_iterator cit = frommenu.begin();
761 cit != frommenu.end() ; ++cit) {
762 switch (cit->kind()) {
763 case MenuItem::Lastfiles:
764 expandLastfiles(tomenu, view);
767 case MenuItem::Documents:
768 expandDocuments(tomenu, view);
771 case MenuItem::ImportFormats:
772 case MenuItem::ViewFormats:
773 case MenuItem::UpdateFormats:
774 case MenuItem::ExportFormats:
775 expandFormats(cit->kind(), tomenu, view);
778 case MenuItem::CharStyles:
779 expandCharStyleInsert(tomenu, view);
782 case MenuItem::FloatListInsert:
783 expandFloatListInsert(tomenu, view);
786 case MenuItem::FloatInsert:
787 expandFloatInsert(tomenu, view);
790 case MenuItem::PasteRecent:
791 expandPasteRecent(tomenu, view);
794 case MenuItem::Branches:
795 expandBranches(tomenu, view);
799 expandToc(tomenu, view);
802 case MenuItem::Submenu: {
804 item.submenu(new Menu(cit->submenuname()));
805 expand(getMenu(cit->submenuname()),
806 *item.submenu(), view);
807 tomenu.add(item, view);
811 case MenuItem::Separator:
812 tomenu.add(*cit, view);
815 case MenuItem::Command:
817 || !specialmenu_->hasFunc(cit->func()))
818 tomenu.add(*cit, view);
822 // we do not want the menu to end with a separator
824 && tomenu.items_.back().kind() == MenuItem::Separator)
825 tomenu.items_.pop_back();
827 // Check whether the shortcuts are unique
828 tomenu.checkShortcuts();
832 void MenuBackend::read(LyXLex & lex)
841 struct keyword_item menutags[md_last - 1] = {
842 { "end", md_endmenuset },
844 { "menubar", md_menubar }
848 if (compare_ascii_no_case(lex.getString(), "menuset")) {
849 lyxerr << "Menubackend::read: ERROR wrong token:`"
850 << lex.getString() << '\'' << endl;
853 lex.pushTable(menutags, md_last - 1);
854 if (lyxerr.debugging(Debug::PARSER))
855 lex.printTable(lyxerr);
859 while (lex.isOK() && !quit) {
866 string const name = lex.getString();
868 getMenu(name).read(lex);
880 lex.printError("menubackend::read: "
881 "Unknown menu tag: `$$Token'");
889 void MenuBackend::add(Menu const & menu)
891 menulist_.push_back(menu);
895 bool MenuBackend::hasMenu(string const & name) const
897 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
901 Menu const & MenuBackend::getMenu(string const & name) const
903 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
905 lyxerr << "No submenu named " << name << endl;
906 BOOST_ASSERT(cit != end());
911 Menu & MenuBackend::getMenu(string const & name)
913 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
915 lyxerr << "No submenu named " << name << endl;
916 BOOST_ASSERT(it != end());
921 Menu const & MenuBackend::getMenubar() const