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 string bindings = toplevel_keymap->findbinding(func_);
142 if (!bindings.empty()) {
143 return bindings.substr(1, bindings.find(']') - 1);
146 << "No bindings for "
147 << lyxaction.getActionName(func_.action)
148 << '(' << func_.argument << ')' << endl;
155 Menu & Menu::add(MenuItem const & i, LyXView const * view)
163 case MenuItem::Command:
166 view->getLyXFunc().getStatus(i.func());
168 || (status.disabled() && i.optional()))
171 items_.back().status(status);
174 case MenuItem::Submenu:
177 bool disabled = true;
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().disabled()) {
187 if (!disabled || !i.optional()) {
189 items_.back().status().disabled(disabled);
196 case MenuItem::Separator:
198 && items_.back().kind() != MenuItem::Separator)
209 Menu & Menu::read(LyXLex & lex)
233 struct keyword_item menutags[md_last - 1] = {
234 { "branches", md_branches },
235 { "charstyles", md_charstyles },
236 { "documents", md_documents },
237 { "end", md_endmenu },
238 { "exportformats", md_exportformats },
239 { "floatinsert", md_floatinsert },
240 { "floatlistinsert", md_floatlistinsert },
241 { "importformats", md_importformats },
243 { "lastfiles", md_lastfiles },
244 { "optitem", md_optitem },
245 { "optsubmenu", md_optsubmenu },
246 { "pasterecent", md_pasterecent },
247 { "separator", md_separator },
248 { "submenu", md_submenu },
250 { "updateformats", md_updateformats },
251 { "viewformats", md_viewformats }
254 lex.pushTable(menutags, md_last - 1);
255 if (lyxerr.debugging(Debug::PARSER))
256 lex.printTable(lyxerr);
259 bool optional = false;
261 while (lex.isOK() && !quit) {
265 // fallback to md_item
268 string const name = _(lex.getString());
270 string const command = lex.getString();
271 FuncRequest func = lyxaction.lookupFunc(command);
272 add(MenuItem(MenuItem::Command, name, func, optional));
278 add(MenuItem(MenuItem::Separator));
282 add(MenuItem(MenuItem::Lastfiles));
286 add(MenuItem(MenuItem::CharStyles));
290 add(MenuItem(MenuItem::Documents));
294 add(MenuItem(MenuItem::Toc));
298 add(MenuItem(MenuItem::ViewFormats));
301 case md_updateformats:
302 add(MenuItem(MenuItem::UpdateFormats));
305 case md_exportformats:
306 add(MenuItem(MenuItem::ExportFormats));
309 case md_importformats:
310 add(MenuItem(MenuItem::ImportFormats));
313 case md_floatlistinsert:
314 add(MenuItem(MenuItem::FloatListInsert));
318 add(MenuItem(MenuItem::FloatInsert));
322 add(MenuItem(MenuItem::PasteRecent));
326 add(MenuItem(MenuItem::Branches));
331 // fallback to md_submenu
334 string const mlabel = _(lex.getString());
336 string const mname = lex.getString();
337 add(MenuItem(MenuItem::Submenu, mlabel, mname,
348 lex.printError("Menu::read: "
349 "Unknown menu tag: `$$Token'");
358 void Menu::checkShortcuts() const
360 // This is a quadratic algorithm, but we do not care because
361 // menus are short enough
362 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
363 string shortcut = it1->shortcut();
364 if (shortcut.empty())
366 if (!contains(it1->label(), shortcut))
367 lyxerr << "Menu warning: menu entry \""
369 << "\" does not contain shortcut `"
370 << shortcut << "'." << endl;
371 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
372 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
373 lyxerr << "Menu warning: menu entries "
374 << '"' << it1->fulllabel()
375 << "\" and \"" << it2->fulllabel()
376 << "\" share the same shortcut."
386 class compare_format {
388 bool operator()(Format const * p1, Format const * p2) {
393 string const limit_string_length(string const & str)
395 string::size_type const max_item_length = 45;
397 if (str.size() > max_item_length)
398 return str.substr(0, max_item_length - 3) + "...";
404 void expandLastfiles(Menu & tomenu, LyXView const * view)
406 LastFiles const & lastfiles = LyX::cref().lastfiles();
409 LastFiles::const_iterator lfit = lastfiles.begin();
410 LastFiles::const_iterator end = lastfiles.end();
412 for (; lfit != end && ii < 10; ++lfit, ++ii) {
413 string const label = tostr(ii) + ". "
414 + MakeDisplayPath((*lfit), 30)
416 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
420 void expandDocuments(Menu & tomenu, LyXView const * view)
422 typedef vector<string> Strings;
423 Strings const names = bufferlist.getFileNames();
426 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
427 FuncRequest(LFUN_NOACTION)), view);
432 Strings::const_iterator docit = names.begin();
433 Strings::const_iterator end = names.end();
434 for (; docit != end; ++docit, ++ii) {
435 string label = MakeDisplayPath(*docit, 20);
437 label = tostr(ii) + ". " + label + '|' + tostr(ii);
438 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_SWITCHBUFFER, *docit)), view);
443 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
445 if (!view->buffer() && kind != MenuItem::ImportFormats) {
446 tomenu.add(MenuItem(MenuItem::Command,
447 _("No Documents Open!"),
448 FuncRequest(LFUN_NOACTION)),
453 typedef vector<Format const *> Formats;
458 case MenuItem::ImportFormats:
459 formats = Importer::GetImportableFormats();
460 action = LFUN_IMPORT;
462 case MenuItem::ViewFormats:
463 formats = Exporter::GetExportableFormats(*view->buffer(), true);
464 action = LFUN_PREVIEW;
466 case MenuItem::UpdateFormats:
467 formats = Exporter::GetExportableFormats(*view->buffer(), true);
468 action = LFUN_UPDATE;
471 formats = Exporter::GetExportableFormats(*view->buffer(), false);
472 action = LFUN_EXPORT;
474 sort(formats.begin(), formats.end(), compare_format());
476 Formats::const_iterator fit = formats.begin();
477 Formats::const_iterator end = formats.end();
478 for (; fit != end ; ++fit) {
481 string label = (*fit)->prettyname();
482 // we need to hide the default graphic export formats
483 // from the external menu, because we need them only
484 // for the internal lyx-view and external latex run
485 if (label == "EPS" || label == "XPM" || label == "PNG")
488 if (kind == MenuItem::ImportFormats) {
489 if ((*fit)->name() == "text")
490 label = _("ASCII text as lines");
491 else if ((*fit)->name() == "textparagraph")
492 label = _("ASCII text as paragraphs");
495 if (!(*fit)->shortcut().empty())
496 label += '|' + (*fit)->shortcut();
498 tomenu.add(MenuItem(MenuItem::Command, label,
499 FuncRequest(action, (*fit)->name())),
505 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
507 if (!view->buffer()) {
508 tomenu.add(MenuItem(MenuItem::Command,
509 _("No Documents Open!"),
510 FuncRequest(LFUN_NOACTION)),
515 FloatList const & floats =
516 view->buffer()->params().getLyXTextClass().floats();
517 FloatList::const_iterator cit = floats.begin();
518 FloatList::const_iterator end = floats.end();
519 for (; cit != end; ++cit) {
520 tomenu.add(MenuItem(MenuItem::Command,
521 _(cit->second.listName()),
522 FuncRequest(LFUN_FLOAT_LIST,
523 cit->second.type())),
529 void expandFloatInsert(Menu & tomenu, LyXView const * view)
531 if (!view->buffer()) {
532 tomenu.add(MenuItem(MenuItem::Command,
533 _("No Documents Open!"),
534 FuncRequest(LFUN_NOACTION)),
539 FloatList const & floats =
540 view->buffer()->params().getLyXTextClass().floats();
541 FloatList::const_iterator cit = floats.begin();
542 FloatList::const_iterator end = floats.end();
543 for (; cit != end; ++cit) {
545 string const label = _(cit->second.name());
546 tomenu.add(MenuItem(MenuItem::Command, label,
547 FuncRequest(LFUN_INSET_FLOAT,
548 cit->second.type())),
554 void expandCharStyleInsert(Menu & tomenu, LyXView const * view)
556 if (!view->buffer()) {
557 tomenu.add(MenuItem(MenuItem::Command,
558 _("No Documents Open!"),
559 FuncRequest(LFUN_NOACTION)),
563 CharStyles & charstyles =
564 view->buffer()->params().getLyXTextClass().charstyles();
565 CharStyles::iterator cit = charstyles.begin();
566 CharStyles::iterator end = charstyles.end();
567 for (; cit != end; ++cit) {
568 string const label = cit->name;
569 tomenu.add(MenuItem(MenuItem::Command, label,
570 FuncRequest(LFUN_INSERT_CHARSTYLE,
576 Menu::size_type const max_number_of_items = 25;
578 void expandToc2(Menu & tomenu,
579 lyx::toc::Toc const & toc_list,
580 lyx::toc::Toc::size_type from,
581 lyx::toc::Toc::size_type to, int depth)
583 int shortcut_count = 0;
584 if (to - from <= max_number_of_items) {
585 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
586 string label(4 * max(0, toc_list[i].depth - depth),' ');
587 label += limit_string_length(toc_list[i].str);
588 if (toc_list[i].depth == depth
589 && shortcut_count < 9) {
590 if (label.find(tostr(shortcut_count + 1)) != string::npos)
591 label += '|' + tostr(++shortcut_count);
593 tomenu.add(MenuItem(MenuItem::Command, label,
594 FuncRequest(toc_list[i].action())));
597 lyx::toc::Toc::size_type pos = from;
599 lyx::toc::Toc::size_type new_pos = pos + 1;
600 while (new_pos < to &&
601 toc_list[new_pos].depth > depth)
604 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
605 label += limit_string_length(toc_list[pos].str);
606 if (toc_list[pos].depth == depth &&
607 shortcut_count < 9) {
608 if (label.find(tostr(shortcut_count + 1)) != string::npos)
609 label += '|' + tostr(++shortcut_count);
611 if (new_pos == pos + 1) {
612 tomenu.add(MenuItem(MenuItem::Command,
613 label, FuncRequest(toc_list[pos].action())));
615 MenuItem item(MenuItem::Submenu, label);
616 item.submenu(new Menu);
617 expandToc2(*item.submenu(),
618 toc_list, pos, new_pos, depth + 1);
627 void expandToc(Menu & tomenu, LyXView const * view)
629 // To make things very cleanly, we would have to pass view to
630 // all MenuItem constructors and to expandToc2. However, we
631 // know that all the entries in a TOC will be have status_ ==
632 // OK, so we avoid this unnecessary overhead (JMarc)
634 if (!view->buffer()) {
635 tomenu.add(MenuItem(MenuItem::Command,
636 _("No Documents Open!"),
637 FuncRequest(LFUN_NOACTION)),
642 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
643 lyx::toc::TocList::const_iterator cit = toc_list.begin();
644 lyx::toc::TocList::const_iterator end = toc_list.end();
645 for (; cit != end; ++cit) {
647 if (cit->first == "TOC")
650 // All the rest is for floats
651 auto_ptr<Menu> menu(new Menu);
652 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
653 lyx::toc::Toc::const_iterator eend = cit->second.end();
654 for (; ccit != eend; ++ccit) {
655 string const label = limit_string_length(ccit->str);
656 menu->add(MenuItem(MenuItem::Command,
658 FuncRequest(ccit->action())));
660 string const & floatName = cit->first;
661 // Is the _(...) really needed here? (Lgb)
662 MenuItem item(MenuItem::Submenu, _(floatName));
663 item.submenu(menu.release());
668 cit = toc_list.find("TOC");
670 tomenu.add(MenuItem(MenuItem::Command,
671 _("No Table of contents"),
675 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
680 void expandPasteRecent(Menu & tomenu, LyXView const * view)
682 if (!view || !view->buffer())
685 vector<string> const selL =
686 CutAndPaste::availableSelections(*view->buffer());
688 vector<string>::const_iterator cit = selL.begin();
689 vector<string>::const_iterator end = selL.end();
691 for (unsigned int index = 0; cit != end; ++cit, ++index) {
692 tomenu.add(MenuItem(MenuItem::Command, *cit,
693 FuncRequest(LFUN_PASTE, tostr(index))));
698 void expandBranches(Menu & tomenu, LyXView const * view)
700 if (!view || !view->buffer())
703 BufferParams const & params = view->buffer()->params();
705 std::list<Branch>::const_iterator cit = params.branchlist().begin();
706 std::list<Branch>::const_iterator end = params.branchlist().end();
708 for (int ii = 1; cit != end; ++cit, ++ii) {
709 string label = cit->getBranch();
711 label = tostr(ii) + ". " + label + "|" + tostr(ii);
712 tomenu.add(MenuItem(MenuItem::Command, label,
713 FuncRequest(LFUN_INSERT_BRANCH,
714 cit->getBranch())), view);
722 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
723 LyXView const * view) const
725 for (Menu::const_iterator cit = frommenu.begin();
726 cit != frommenu.end() ; ++cit) {
727 switch (cit->kind()) {
728 case MenuItem::Lastfiles:
729 expandLastfiles(tomenu, view);
732 case MenuItem::Documents:
733 expandDocuments(tomenu, view);
736 case MenuItem::ImportFormats:
737 case MenuItem::ViewFormats:
738 case MenuItem::UpdateFormats:
739 case MenuItem::ExportFormats:
740 expandFormats(cit->kind(), tomenu, view);
743 case MenuItem::CharStyles:
744 expandCharStyleInsert(tomenu, view);
747 case MenuItem::FloatListInsert:
748 expandFloatListInsert(tomenu, view);
751 case MenuItem::FloatInsert:
752 expandFloatInsert(tomenu, view);
755 case MenuItem::PasteRecent:
756 expandPasteRecent(tomenu, view);
759 case MenuItem::Branches:
760 expandBranches(tomenu, view);
764 expandToc(tomenu, view);
767 case MenuItem::Submenu: {
769 item.submenu(new Menu(cit->submenuname()));
770 expand(getMenu(cit->submenuname()),
771 *item.submenu(), view);
772 tomenu.add(item, view);
777 tomenu.add(*cit, view);
781 // we do not want the menu to end with a separator
783 && tomenu.items_.back().kind() == MenuItem::Separator)
784 tomenu.items_.pop_back();
786 // Check whether the shortcuts are unique
787 tomenu.checkShortcuts();
791 bool Menu::hasSubmenu(string const & name) const
794 return find_if(begin(), end(),
795 bind(std::equal_to<string>(),
796 bind(&MenuItem::submenuname, _1),
799 // I would have prefered this, but I am not sure if it
800 // makes a difference. (Lgb)
802 make_transform_iterator(begin(),
803 bind(&MenuItem::submenuname, _1)),
804 make_transform_iterator(end(),
805 bind(&MenuItem::submenuname, _1)),
812 void MenuBackend::read(LyXLex & lex)
821 struct keyword_item menutags[md_last - 1] = {
822 { "end", md_endmenuset },
824 { "menubar", md_menubar }
828 if (compare_ascii_no_case(lex.getString(), "menuset")) {
829 lyxerr << "Menubackend::read: ERROR wrong token:`"
830 << lex.getString() << '\'' << endl;
833 lex.pushTable(menutags, md_last - 1);
834 if (lyxerr.debugging(Debug::PARSER))
835 lex.printTable(lyxerr);
839 while (lex.isOK() && !quit) {
846 string const name = lex.getString();
848 getMenu(name).read(lex);
860 lex.printError("menubackend::read: "
861 "Unknown menu tag: `$$Token'");
869 void MenuBackend::add(Menu const & menu)
871 menulist_.push_back(menu);
875 bool MenuBackend::hasMenu(string const & name) const
877 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
881 Menu const & MenuBackend::getMenu(string const & name) const
883 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
885 lyxerr << "No submenu named " << name << endl;
886 BOOST_ASSERT(cit != end());
891 Menu & MenuBackend::getMenu(string const & name)
893 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
895 lyxerr << "No submenu named " << name << endl;
896 BOOST_ASSERT(it != end());
901 Menu const & MenuBackend::getMenubar() const