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;
63 extern BufferList bufferlist;
64 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
66 // This is the global menu definition
67 MenuBackend menubackend;
70 MenuItem::MenuItem(Kind kind)
71 : kind_(kind), optional_(false)
75 MenuItem::MenuItem(Kind kind, string const & label,
76 string const & submenu, bool optional)
77 : kind_(kind), label_(label),
78 submenuname_(submenu), optional_(optional)
80 BOOST_ASSERT(kind == Submenu);
84 MenuItem::MenuItem(Kind kind, string const & label,
85 FuncRequest const & func, bool optional)
86 : kind_(kind), label_(label), func_(func), optional_(optional)
94 void MenuItem::submenu(Menu * menu)
100 string const MenuItem::label() const
102 return token(label_, '|', 0);
106 string const MenuItem::shortcut() const
108 return token(label_, '|', 1);
112 string const MenuItem::binding() const
114 if (kind_ != Command)
117 // Get the keys bound to this action, but keep only the
119 string bindings = toplevel_keymap->findbinding(func_);
121 if (!bindings.empty()) {
122 return bindings.substr(1, bindings.find(']') - 1);
125 << "No bindings for "
126 << lyxaction.getActionName(func_.action)
127 << '(' << func_.argument << ')' << endl;
134 Menu & Menu::add(MenuItem const & i, LyXView const * view)
142 case MenuItem::Command:
145 view->getLyXFunc().getStatus(i.func());
147 || (status.disabled() && i.optional()))
150 items_.back().status(status);
153 case MenuItem::Submenu:
156 bool disabled = true;
157 for (const_iterator cit = i.submenu()->begin();
158 cit != i.submenu()->end(); ++cit) {
159 if ((cit->kind() == MenuItem::Command
160 || cit->kind() == MenuItem::Submenu)
161 && !cit->status().disabled()) {
166 if (!disabled || !i.optional()) {
168 items_.back().status().disabled(disabled);
175 case MenuItem::Separator:
177 && items_.back().kind() != MenuItem::Separator)
188 Menu & Menu::read(LyXLex & lex)
211 struct keyword_item menutags[md_last - 1] = {
212 { "branches", md_branches },
213 { "documents", md_documents },
214 { "end", md_endmenu },
215 { "exportformats", md_exportformats },
216 { "floatinsert", md_floatinsert },
217 { "floatlistinsert", md_floatlistinsert },
218 { "importformats", md_importformats },
220 { "lastfiles", md_lastfiles },
221 { "optitem", md_optitem },
222 { "optsubmenu", md_optsubmenu },
223 { "pasterecent", md_pasterecent },
224 { "separator", md_separator },
225 { "submenu", md_submenu },
227 { "updateformats", md_updateformats },
228 { "viewformats", md_viewformats }
231 lex.pushTable(menutags, md_last - 1);
232 if (lyxerr.debugging(Debug::PARSER))
233 lex.printTable(lyxerr);
236 bool optional = false;
238 while (lex.isOK() && !quit) {
242 // fallback to md_item
245 string const name = _(lex.getString());
247 string const command = lex.getString();
248 FuncRequest func = lyxaction.lookupFunc(command);
249 add(MenuItem(MenuItem::Command, name, func, optional));
255 add(MenuItem(MenuItem::Separator));
259 add(MenuItem(MenuItem::Lastfiles));
263 add(MenuItem(MenuItem::Documents));
267 add(MenuItem(MenuItem::Toc));
271 add(MenuItem(MenuItem::ViewFormats));
274 case md_updateformats:
275 add(MenuItem(MenuItem::UpdateFormats));
278 case md_exportformats:
279 add(MenuItem(MenuItem::ExportFormats));
282 case md_importformats:
283 add(MenuItem(MenuItem::ImportFormats));
286 case md_floatlistinsert:
287 add(MenuItem(MenuItem::FloatListInsert));
291 add(MenuItem(MenuItem::FloatInsert));
295 add(MenuItem(MenuItem::PasteRecent));
299 add(MenuItem(MenuItem::Branches));
304 // fallback to md_submenu
307 string const mlabel = _(lex.getString());
309 string const mname = lex.getString();
310 add(MenuItem(MenuItem::Submenu, mlabel, mname,
321 lex.printError("Menu::read: "
322 "Unknown menu tag: `$$Token'");
331 void Menu::checkShortcuts() const
333 // This is a quadratic algorithm, but we do not care because
334 // menus are short enough
335 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
336 string shortcut = it1->shortcut();
337 if (shortcut.empty())
339 if (!contains(it1->label(), shortcut))
340 lyxerr << "Menu warning: menu entry \""
342 << "\" does not contain shortcut `"
343 << shortcut << "'." << endl;
344 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
345 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
346 lyxerr << "Menu warning: menu entries "
347 << '"' << it1->fulllabel()
348 << "\" and \"" << it2->fulllabel()
349 << "\" share the same shortcut."
359 class compare_format {
361 bool operator()(Format const * p1, Format const * p2) {
366 string const limit_string_length(string const & str)
368 string::size_type const max_item_length = 45;
370 if (str.size() > max_item_length)
371 return str.substr(0, max_item_length - 3) + "...";
377 void expandLastfiles(Menu & tomenu, LyXView const * view)
379 LastFiles const & lastfiles = LyX::cref().lastfiles();
382 LastFiles::const_iterator lfit = lastfiles.begin();
383 LastFiles::const_iterator end = lastfiles.end();
385 for (; lfit != end && ii < 10; ++lfit, ++ii) {
386 string const label = tostr(ii) + ". "
387 + MakeDisplayPath((*lfit), 30)
389 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
393 void expandDocuments(Menu & tomenu, LyXView const * view)
395 typedef vector<string> Strings;
396 Strings const names = bufferlist.getFileNames();
399 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
400 FuncRequest(LFUN_NOACTION)), view);
405 Strings::const_iterator docit = names.begin();
406 Strings::const_iterator end = names.end();
407 for (; docit != end; ++docit, ++ii) {
408 string label = MakeDisplayPath(*docit, 20);
410 label = tostr(ii) + ". " + label + '|' + tostr(ii);
411 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_SWITCHBUFFER, *docit)), view);
416 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
418 if (!view->buffer() && kind != MenuItem::ImportFormats) {
419 tomenu.add(MenuItem(MenuItem::Command,
420 _("No Documents Open!"),
421 FuncRequest(LFUN_NOACTION)),
426 typedef vector<Format const *> Formats;
431 case MenuItem::ImportFormats:
432 formats = Importer::GetImportableFormats();
433 action = LFUN_IMPORT;
435 case MenuItem::ViewFormats:
436 formats = Exporter::GetExportableFormats(*view->buffer(), true);
437 action = LFUN_PREVIEW;
439 case MenuItem::UpdateFormats:
440 formats = Exporter::GetExportableFormats(*view->buffer(), true);
441 action = LFUN_UPDATE;
444 formats = Exporter::GetExportableFormats(*view->buffer(), false);
445 action = LFUN_EXPORT;
447 sort(formats.begin(), formats.end(), compare_format());
449 Formats::const_iterator fit = formats.begin();
450 Formats::const_iterator end = formats.end();
451 for (; fit != end ; ++fit) {
454 string label = (*fit)->prettyname();
455 // we need to hide the default graphic export formats
456 // from the external menu, because we need them only
457 // for the internal lyx-view and external latex run
458 if (label == "EPS" || label == "XPM" || label == "PNG")
461 if (kind == MenuItem::ImportFormats) {
462 if ((*fit)->name() == "text")
463 label = _("ASCII text as lines");
464 else if ((*fit)->name() == "textparagraph")
465 label = _("ASCII text as paragraphs");
468 if (!(*fit)->shortcut().empty())
469 label += '|' + (*fit)->shortcut();
471 tomenu.add(MenuItem(MenuItem::Command, label,
472 FuncRequest(action, (*fit)->name())),
478 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
480 if (!view->buffer()) {
481 tomenu.add(MenuItem(MenuItem::Command,
482 _("No Documents Open!"),
483 FuncRequest(LFUN_NOACTION)),
488 FloatList const & floats =
489 view->buffer()->params().getLyXTextClass().floats();
490 FloatList::const_iterator cit = floats.begin();
491 FloatList::const_iterator end = floats.end();
492 for (; cit != end; ++cit) {
493 tomenu.add(MenuItem(MenuItem::Command,
494 _(cit->second.listName()),
495 FuncRequest(LFUN_FLOAT_LIST,
496 cit->second.type())),
502 void expandFloatInsert(Menu & tomenu, LyXView const * view)
504 if (!view->buffer()) {
505 tomenu.add(MenuItem(MenuItem::Command,
506 _("No Documents Open!"),
507 FuncRequest(LFUN_NOACTION)),
512 FloatList const & floats =
513 view->buffer()->params().getLyXTextClass().floats();
514 FloatList::const_iterator cit = floats.begin();
515 FloatList::const_iterator end = floats.end();
516 for (; cit != end; ++cit) {
518 string const label = _(cit->second.name());
519 tomenu.add(MenuItem(MenuItem::Command, label,
520 FuncRequest(LFUN_INSET_FLOAT,
521 cit->second.type())),
527 Menu::size_type const max_number_of_items = 25;
529 void expandToc2(Menu & tomenu,
530 lyx::toc::Toc const & toc_list,
531 lyx::toc::Toc::size_type from,
532 lyx::toc::Toc::size_type to, int depth)
534 int shortcut_count = 0;
535 if (to - from <= max_number_of_items) {
536 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
537 string label(4 * max(0, toc_list[i].depth - depth),' ');
538 label += limit_string_length(toc_list[i].str);
539 if (toc_list[i].depth == depth
540 && shortcut_count < 9) {
541 if (label.find(tostr(shortcut_count + 1)) != string::npos)
542 label += '|' + tostr(++shortcut_count);
544 tomenu.add(MenuItem(MenuItem::Command, label,
545 FuncRequest(toc_list[i].action())));
548 lyx::toc::Toc::size_type pos = from;
550 lyx::toc::Toc::size_type new_pos = pos + 1;
551 while (new_pos < to &&
552 toc_list[new_pos].depth > depth)
555 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
556 label += limit_string_length(toc_list[pos].str);
557 if (toc_list[pos].depth == depth &&
558 shortcut_count < 9) {
559 if (label.find(tostr(shortcut_count + 1)) != string::npos)
560 label += '|' + tostr(++shortcut_count);
562 if (new_pos == pos + 1) {
563 tomenu.add(MenuItem(MenuItem::Command,
564 label, FuncRequest(toc_list[pos].action())));
566 MenuItem item(MenuItem::Submenu, label);
567 item.submenu(new Menu);
568 expandToc2(*item.submenu(),
569 toc_list, pos, new_pos, depth + 1);
578 void expandToc(Menu & tomenu, LyXView const * view)
580 // To make things very cleanly, we would have to pass view to
581 // all MenuItem constructors and to expandToc2. However, we
582 // know that all the entries in a TOC will be have status_ ==
583 // OK, so we avoid this unnecessary overhead (JMarc)
585 if (!view->buffer()) {
586 tomenu.add(MenuItem(MenuItem::Command,
587 _("No Documents Open!"),
588 FuncRequest(LFUN_NOACTION)),
593 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
594 lyx::toc::TocList::const_iterator cit = toc_list.begin();
595 lyx::toc::TocList::const_iterator end = toc_list.end();
596 for (; cit != end; ++cit) {
598 if (cit->first == "TOC")
601 // All the rest is for floats
602 auto_ptr<Menu> menu(new Menu);
603 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
604 lyx::toc::Toc::const_iterator eend = cit->second.end();
605 for (; ccit != eend; ++ccit) {
606 string const label = limit_string_length(ccit->str);
607 menu->add(MenuItem(MenuItem::Command,
609 FuncRequest(ccit->action())));
611 string const & floatName = cit->first;
612 // Is the _(...) really needed here? (Lgb)
613 MenuItem item(MenuItem::Submenu, _(floatName));
614 item.submenu(menu.release());
619 cit = toc_list.find("TOC");
621 tomenu.add(MenuItem(MenuItem::Command,
622 _("No Table of contents"),
626 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
631 void expandPasteRecent(Menu & tomenu, LyXView const * view)
633 if (!view || !view->buffer())
636 vector<string> const selL =
637 CutAndPaste::availableSelections(*view->buffer());
639 vector<string>::const_iterator cit = selL.begin();
640 vector<string>::const_iterator end = selL.end();
642 for (unsigned int index = 0; cit != end; ++cit, ++index) {
643 tomenu.add(MenuItem(MenuItem::Command, *cit,
644 FuncRequest(LFUN_PASTE, tostr(index))));
649 void expandBranches(Menu & tomenu, LyXView const * view)
651 if (!view || !view->buffer())
654 BufferParams const & params = view->buffer()->params();
656 std::list<Branch>::const_iterator cit = params.branchlist().begin();
657 std::list<Branch>::const_iterator end = params.branchlist().end();
659 for (int ii = 1; cit != end; ++cit, ++ii) {
660 string label = cit->getBranch();
662 label = tostr(ii) + ". " + label + "|" + tostr(ii);
663 tomenu.add(MenuItem(MenuItem::Command, label,
664 FuncRequest(LFUN_INSERT_BRANCH,
665 cit->getBranch())), view);
673 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
674 LyXView const * view) const
676 for (Menu::const_iterator cit = frommenu.begin();
677 cit != frommenu.end() ; ++cit) {
678 switch (cit->kind()) {
679 case MenuItem::Lastfiles:
680 expandLastfiles(tomenu, view);
683 case MenuItem::Documents:
684 expandDocuments(tomenu, view);
687 case MenuItem::ImportFormats:
688 case MenuItem::ViewFormats:
689 case MenuItem::UpdateFormats:
690 case MenuItem::ExportFormats:
691 expandFormats(cit->kind(), tomenu, view);
694 case MenuItem::FloatListInsert:
695 expandFloatListInsert(tomenu, view);
698 case MenuItem::FloatInsert:
699 expandFloatInsert(tomenu, view);
702 case MenuItem::PasteRecent:
703 expandPasteRecent(tomenu, view);
706 case MenuItem::Branches:
707 expandBranches(tomenu, view);
711 expandToc(tomenu, view);
714 case MenuItem::Submenu: {
716 item.submenu(new Menu(cit->submenuname()));
717 expand(getMenu(cit->submenuname()),
718 *item.submenu(), view);
719 tomenu.add(item, view);
724 tomenu.add(*cit, view);
728 // we do not want the menu to end with a separator
730 && tomenu.items_.back().kind() == MenuItem::Separator)
731 tomenu.items_.pop_back();
733 // Check whether the shortcuts are unique
734 tomenu.checkShortcuts();
738 bool Menu::hasSubmenu(string const & name) const
740 return find_if(begin(), end(),
741 lyx::compare_memfun(&MenuItem::submenuname,
746 void MenuBackend::read(LyXLex & lex)
755 struct keyword_item menutags[md_last - 1] = {
756 { "end", md_endmenuset },
758 { "menubar", md_menubar }
762 if (compare_ascii_no_case(lex.getString(), "menuset")) {
763 lyxerr << "Menubackend::read: ERROR wrong token:`"
764 << lex.getString() << '\'' << endl;
767 lex.pushTable(menutags, md_last - 1);
768 if (lyxerr.debugging(Debug::PARSER))
769 lex.printTable(lyxerr);
773 while (lex.isOK() && !quit) {
780 string const name = lex.getString();
782 getMenu(name).read(lex);
794 lex.printError("menubackend::read: "
795 "Unknown menu tag: `$$Token'");
803 void MenuBackend::add(Menu const & menu)
805 menulist_.push_back(menu);
809 bool MenuBackend::hasMenu(string const & name) const
811 return find_if(begin(), end(),
812 lyx::compare_memfun(&Menu::name, name)) != end();
816 Menu const & MenuBackend::getMenu(string const & name) const
818 const_iterator cit = find_if(begin(), end(),
819 lyx::compare_memfun(&Menu::name, name));
821 lyxerr << "No submenu named " << name << endl;
822 BOOST_ASSERT(cit != end());
827 Menu & MenuBackend::getMenu(string const & name)
829 MenuList::iterator it =
830 find_if(menulist_.begin(), menulist_.end(),
831 lyx::compare_memfun(&Menu::name, name));
832 BOOST_ASSERT(it != menulist_.end());
837 Menu const & MenuBackend::getMenubar() const