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/lyxfunctional.h"
44 #include "support/lstrings.h"
45 #include "support/tostr.h"
49 using lyx::support::compare_ascii_no_case;
50 using lyx::support::contains;
51 using lyx::support::MakeDisplayPath;
52 using lyx::support::token;
62 extern BufferList bufferlist;
63 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
65 // This is the global menu definition
66 MenuBackend menubackend;
69 MenuItem::MenuItem(Kind kind)
70 : kind_(kind), optional_(false)
74 MenuItem::MenuItem(Kind kind, string const & label,
75 string const & submenu, bool optional)
76 : kind_(kind), label_(label),
77 submenuname_(submenu), optional_(optional)
79 BOOST_ASSERT(kind == Submenu);
83 MenuItem::MenuItem(Kind kind, string const & label,
84 FuncRequest const & func, bool optional)
85 : kind_(kind), label_(label), func_(func), optional_(optional)
93 void MenuItem::submenu(Menu * menu)
99 string const MenuItem::label() const
101 return token(label_, '|', 0);
105 string const MenuItem::shortcut() const
107 return token(label_, '|', 1);
111 string const MenuItem::binding() const
113 if (kind_ != Command)
116 // Get the keys bound to this action, but keep only the
118 string bindings = toplevel_keymap->findbinding(func_);
120 if (!bindings.empty()) {
121 return bindings.substr(1, bindings.find(']') - 1);
124 << "No bindings for "
125 << lyxaction.getActionName(func_.action)
126 << '(' << func_.argument << ')' << endl;
133 Menu & Menu::add(MenuItem const & i, LyXView const * view)
141 case MenuItem::Command:
144 view->getLyXFunc().getStatus(i.func());
146 || (status.disabled() && i.optional()))
149 items_.back().status(status);
152 case MenuItem::Submenu:
155 bool disabled = true;
156 for (const_iterator cit = i.submenu()->begin();
157 cit != i.submenu()->end(); ++cit) {
158 if ((cit->kind() == MenuItem::Command
159 || cit->kind() == MenuItem::Submenu)
160 && !cit->status().disabled()) {
165 if (!disabled || !i.optional()) {
167 items_.back().status().disabled(disabled);
174 case MenuItem::Separator:
176 && items_.back().kind() != MenuItem::Separator)
187 Menu & Menu::read(LyXLex & lex)
210 struct keyword_item menutags[md_last - 1] = {
211 { "branches", md_branches },
212 { "documents", md_documents },
213 { "end", md_endmenu },
214 { "exportformats", md_exportformats },
215 { "floatinsert", md_floatinsert },
216 { "floatlistinsert", md_floatlistinsert },
217 { "importformats", md_importformats },
219 { "lastfiles", md_lastfiles },
220 { "optitem", md_optitem },
221 { "optsubmenu", md_optsubmenu },
222 { "pasterecent", md_pasterecent },
223 { "separator", md_separator },
224 { "submenu", md_submenu },
226 { "updateformats", md_updateformats },
227 { "viewformats", md_viewformats }
230 lex.pushTable(menutags, md_last - 1);
231 if (lyxerr.debugging(Debug::PARSER))
232 lex.printTable(lyxerr);
235 bool optional = false;
237 while (lex.isOK() && !quit) {
241 // fallback to md_item
244 string const name = _(lex.getString());
246 string const command = lex.getString();
247 FuncRequest func = lyxaction.lookupFunc(command);
248 add(MenuItem(MenuItem::Command, name, func, optional));
254 add(MenuItem(MenuItem::Separator));
258 add(MenuItem(MenuItem::Lastfiles));
262 add(MenuItem(MenuItem::Documents));
266 add(MenuItem(MenuItem::Toc));
270 add(MenuItem(MenuItem::ViewFormats));
273 case md_updateformats:
274 add(MenuItem(MenuItem::UpdateFormats));
277 case md_exportformats:
278 add(MenuItem(MenuItem::ExportFormats));
281 case md_importformats:
282 add(MenuItem(MenuItem::ImportFormats));
285 case md_floatlistinsert:
286 add(MenuItem(MenuItem::FloatListInsert));
290 add(MenuItem(MenuItem::FloatInsert));
294 add(MenuItem(MenuItem::PasteRecent));
298 add(MenuItem(MenuItem::Branches));
303 // fallback to md_submenu
306 string const mlabel = _(lex.getString());
308 string const mname = lex.getString();
309 add(MenuItem(MenuItem::Submenu, mlabel, mname,
320 lex.printError("Menu::read: "
321 "Unknown menu tag: `$$Token'");
330 void Menu::checkShortcuts() const
332 // This is a quadratic algorithm, but we do not care because
333 // menus are short enough
334 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
335 string shortcut = it1->shortcut();
336 if (shortcut.empty())
338 if (!contains(it1->label(), shortcut))
339 lyxerr << "Menu warning: menu entry \""
341 << "\" does not contain shortcut `"
342 << shortcut << "'." << endl;
343 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
344 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
345 lyxerr << "Menu warning: menu entries "
346 << '"' << it1->fulllabel()
347 << "\" and \"" << it2->fulllabel()
348 << "\" share the same shortcut."
358 class compare_format {
360 bool operator()(Format const * p1, Format const * p2) {
365 string const limit_string_length(string const & str)
367 string::size_type const max_item_length = 45;
369 if (str.size() > max_item_length)
370 return str.substr(0, max_item_length - 3) + "...";
376 void expandLastfiles(Menu & tomenu, LyXView const * view)
379 LastFiles::const_iterator lfit = lastfiles->begin();
380 LastFiles::const_iterator end = lastfiles->end();
382 for (; lfit != end && ii < 10; ++lfit, ++ii) {
383 string const label = tostr(ii) + ". "
384 + MakeDisplayPath((*lfit), 30)
386 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
390 void expandDocuments(Menu & tomenu, LyXView const * view)
392 typedef vector<string> Strings;
393 Strings const names = bufferlist.getFileNames();
396 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
397 FuncRequest(LFUN_NOACTION)), view);
402 Strings::const_iterator docit = names.begin();
403 Strings::const_iterator end = names.end();
404 for (; docit != end; ++docit, ++ii) {
405 string label = MakeDisplayPath(*docit, 20);
407 label = tostr(ii) + ". " + label + '|' + tostr(ii);
408 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_SWITCHBUFFER, *docit)), view);
413 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
415 if (!view->buffer() && kind != MenuItem::ImportFormats) {
416 tomenu.add(MenuItem(MenuItem::Command,
417 _("No Documents Open!"),
418 FuncRequest(LFUN_NOACTION)),
423 typedef vector<Format const *> Formats;
428 case MenuItem::ImportFormats:
429 formats = Importer::GetImportableFormats();
430 action = LFUN_IMPORT;
432 case MenuItem::ViewFormats:
433 formats = Exporter::GetExportableFormats(*view->buffer(), true);
434 action = LFUN_PREVIEW;
436 case MenuItem::UpdateFormats:
437 formats = Exporter::GetExportableFormats(*view->buffer(), true);
438 action = LFUN_UPDATE;
441 formats = Exporter::GetExportableFormats(*view->buffer(), false);
442 action = LFUN_EXPORT;
444 sort(formats.begin(), formats.end(), compare_format());
446 Formats::const_iterator fit = formats.begin();
447 Formats::const_iterator end = formats.end();
448 for (; fit != end ; ++fit) {
451 string label = (*fit)->prettyname();
452 // we need to hide the default graphic export formats
453 // from the external menu, because we need them only
454 // for the internal lyx-view and external latex run
455 if (label == "EPS" || label == "XPM" || label == "PNG")
458 if (kind == MenuItem::ImportFormats) {
459 if ((*fit)->name() == "text")
460 label = _("ASCII text as lines");
461 else if ((*fit)->name() == "textparagraph")
462 label = _("ASCII text as paragraphs");
465 if (!(*fit)->shortcut().empty())
466 label += '|' + (*fit)->shortcut();
468 tomenu.add(MenuItem(MenuItem::Command, label,
469 FuncRequest(action, (*fit)->name())),
475 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
477 if (!view->buffer()) {
478 tomenu.add(MenuItem(MenuItem::Command,
479 _("No Documents Open!"),
480 FuncRequest(LFUN_NOACTION)),
485 FloatList const & floats =
486 view->buffer()->params().getLyXTextClass().floats();
487 FloatList::const_iterator cit = floats.begin();
488 FloatList::const_iterator end = floats.end();
489 for (; cit != end; ++cit) {
490 tomenu.add(MenuItem(MenuItem::Command,
491 _(cit->second.listName()),
492 FuncRequest(LFUN_FLOAT_LIST,
493 cit->second.type())),
499 void expandFloatInsert(Menu & tomenu, LyXView const * view)
501 if (!view->buffer()) {
502 tomenu.add(MenuItem(MenuItem::Command,
503 _("No Documents Open!"),
504 FuncRequest(LFUN_NOACTION)),
509 FloatList const & floats =
510 view->buffer()->params().getLyXTextClass().floats();
511 FloatList::const_iterator cit = floats.begin();
512 FloatList::const_iterator end = floats.end();
513 for (; cit != end; ++cit) {
515 string const label = _(cit->second.name());
516 tomenu.add(MenuItem(MenuItem::Command, label,
517 FuncRequest(LFUN_INSET_FLOAT,
518 cit->second.type())),
524 Menu::size_type const max_number_of_items = 25;
526 void expandToc2(Menu & tomenu,
527 lyx::toc::Toc const & toc_list,
528 lyx::toc::Toc::size_type from,
529 lyx::toc::Toc::size_type to, int depth)
531 int shortcut_count = 0;
532 if (to - from <= max_number_of_items) {
533 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
534 string label(4 * max(0, toc_list[i].depth - depth),' ');
535 label += limit_string_length(toc_list[i].str);
536 if (toc_list[i].depth == depth
537 && ++shortcut_count <= 9) {
538 label += '|' + tostr(shortcut_count);
540 tomenu.add(MenuItem(MenuItem::Command, label,
541 FuncRequest(toc_list[i].action())));
544 lyx::toc::Toc::size_type pos = from;
546 lyx::toc::Toc::size_type new_pos = pos + 1;
547 while (new_pos < to &&
548 toc_list[new_pos].depth > depth)
551 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
552 label += limit_string_length(toc_list[pos].str);
553 if (toc_list[pos].depth == depth &&
554 ++shortcut_count <= 9)
555 label += '|' + tostr(shortcut_count);
557 if (new_pos == pos + 1) {
558 tomenu.add(MenuItem(MenuItem::Command,
559 label, FuncRequest(toc_list[pos].action())));
561 MenuItem item(MenuItem::Submenu, label);
562 item.submenu(new Menu);
563 expandToc2(*item.submenu(),
564 toc_list, pos, new_pos, depth + 1);
573 void expandToc(Menu & tomenu, LyXView const * view)
575 // To make things very cleanly, we would have to pass view to
576 // all MenuItem constructors and to expandToc2. However, we
577 // know that all the entries in a TOC will be have status_ ==
578 // OK, so we avoid this unnecessary overhead (JMarc)
580 if (!view->buffer()) {
581 tomenu.add(MenuItem(MenuItem::Command,
582 _("No Documents Open!"),
583 FuncRequest(LFUN_NOACTION)),
588 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
589 lyx::toc::TocList::const_iterator cit = toc_list.begin();
590 lyx::toc::TocList::const_iterator end = toc_list.end();
591 for (; cit != end; ++cit) {
593 if (cit->first == "TOC")
596 // All the rest is for floats
597 Menu * menu = new Menu;
598 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
599 lyx::toc::Toc::const_iterator eend = cit->second.end();
600 for (; ccit != eend; ++ccit) {
601 string const label = limit_string_length(ccit->str);
602 menu->add(MenuItem(MenuItem::Command,
604 FuncRequest(ccit->action())));
606 string const & floatName = cit->first;
607 // Is the _(...) really needed here? (Lgb)
608 MenuItem item(MenuItem::Submenu, _(floatName));
614 cit = toc_list.find("TOC");
616 tomenu.add(MenuItem(MenuItem::Command,
617 _("No Table of contents"),
621 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
626 void expandPasteRecent(Menu & tomenu, LyXView const * view)
628 if (!view || !view->buffer())
631 vector<string> const selL =
632 CutAndPaste::availableSelections(*view->buffer());
634 vector<string>::const_iterator cit = selL.begin();
635 vector<string>::const_iterator end = selL.end();
637 for (unsigned int index = 0; cit != end; ++cit, ++index) {
638 tomenu.add(MenuItem(MenuItem::Command, *cit,
639 FuncRequest(LFUN_PASTE, tostr(index))));
644 void expandBranches(Menu & tomenu, LyXView const * view)
646 if (!view || !view->buffer())
649 BufferParams const & params = view->buffer()->params();
651 std::list<Branch>::const_iterator cit = params.branchlist().begin();
652 std::list<Branch>::const_iterator end = params.branchlist().end();
654 for (int ii = 1; cit != end; ++cit, ++ii) {
655 string label = cit->getBranch();
657 label = tostr(ii) + ". " + label + "|" + tostr(ii);
658 tomenu.add(MenuItem(MenuItem::Command, label,
659 FuncRequest(LFUN_INSERT_BRANCH,
660 cit->getBranch())), view);
668 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
669 LyXView const * view) const
671 for (Menu::const_iterator cit = frommenu.begin();
672 cit != frommenu.end() ; ++cit) {
673 switch (cit->kind()) {
674 case MenuItem::Lastfiles:
675 expandLastfiles(tomenu, view);
678 case MenuItem::Documents:
679 expandDocuments(tomenu, view);
682 case MenuItem::ImportFormats:
683 case MenuItem::ViewFormats:
684 case MenuItem::UpdateFormats:
685 case MenuItem::ExportFormats:
686 expandFormats(cit->kind(), tomenu, view);
689 case MenuItem::FloatListInsert:
690 expandFloatListInsert(tomenu, view);
693 case MenuItem::FloatInsert:
694 expandFloatInsert(tomenu, view);
697 case MenuItem::PasteRecent:
698 expandPasteRecent(tomenu, view);
701 case MenuItem::Branches:
702 expandBranches(tomenu, view);
706 expandToc(tomenu, view);
709 case MenuItem::Submenu: {
711 item.submenu(new Menu(cit->submenuname()));
712 expand(getMenu(cit->submenuname()),
713 *item.submenu(), view);
714 tomenu.add(item, view);
719 tomenu.add(*cit, view);
723 // we do not want the menu to end with a separator
725 && tomenu.items_.back().kind() == MenuItem::Separator)
726 tomenu.items_.pop_back();
728 // Check whether the shortcuts are unique
729 tomenu.checkShortcuts();
733 bool Menu::hasSubmenu(string const & name) const
735 return find_if(begin(), end(),
736 lyx::compare_memfun(&MenuItem::submenuname,
741 void MenuBackend::read(LyXLex & lex)
750 struct keyword_item menutags[md_last - 1] = {
751 { "end", md_endmenuset },
753 { "menubar", md_menubar }
757 if (compare_ascii_no_case(lex.getString(), "menuset")) {
758 lyxerr << "Menubackend::read: ERROR wrong token:`"
759 << lex.getString() << '\'' << endl;
762 lex.pushTable(menutags, md_last - 1);
763 if (lyxerr.debugging(Debug::PARSER))
764 lex.printTable(lyxerr);
768 while (lex.isOK() && !quit) {
775 string const name = lex.getString();
777 getMenu(name).read(lex);
789 lex.printError("menubackend::read: "
790 "Unknown menu tag: `$$Token'");
798 void MenuBackend::add(Menu const & menu)
800 menulist_.push_back(menu);
804 bool MenuBackend::hasMenu(string const & name) const
806 return find_if(begin(), end(),
807 lyx::compare_memfun(&Menu::name, name)) != end();
811 Menu const & MenuBackend::getMenu(string const & name) const
813 const_iterator cit = find_if(begin(), end(),
814 lyx::compare_memfun(&Menu::name, name));
816 lyxerr << "No submenu named " << name << endl;
817 BOOST_ASSERT(cit != end());
822 Menu & MenuBackend::getMenu(string const & name)
824 MenuList::iterator it =
825 find_if(menulist_.begin(), menulist_.end(),
826 lyx::compare_memfun(&Menu::name, name));
827 BOOST_ASSERT(it != menulist_.end());
832 Menu const & MenuBackend::getMenubar() const