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)
212 struct keyword_item menutags[md_last - 1] = {
213 { "branches", md_branches },
214 { "charstyles", md_charstyles },
215 { "documents", md_documents },
216 { "end", md_endmenu },
217 { "exportformats", md_exportformats },
218 { "floatinsert", md_floatinsert },
219 { "floatlistinsert", md_floatlistinsert },
220 { "importformats", md_importformats },
222 { "lastfiles", md_lastfiles },
223 { "optitem", md_optitem },
224 { "optsubmenu", md_optsubmenu },
225 { "pasterecent", md_pasterecent },
226 { "separator", md_separator },
227 { "submenu", md_submenu },
229 { "updateformats", md_updateformats },
230 { "viewformats", md_viewformats }
233 lex.pushTable(menutags, md_last - 1);
234 if (lyxerr.debugging(Debug::PARSER))
235 lex.printTable(lyxerr);
238 bool optional = false;
240 while (lex.isOK() && !quit) {
244 // fallback to md_item
247 string const name = _(lex.getString());
249 string const command = lex.getString();
250 FuncRequest func = lyxaction.lookupFunc(command);
251 add(MenuItem(MenuItem::Command, name, func, optional));
257 add(MenuItem(MenuItem::Separator));
261 add(MenuItem(MenuItem::Lastfiles));
265 add(MenuItem(MenuItem::CharStyles));
269 add(MenuItem(MenuItem::Documents));
273 add(MenuItem(MenuItem::Toc));
277 add(MenuItem(MenuItem::ViewFormats));
280 case md_updateformats:
281 add(MenuItem(MenuItem::UpdateFormats));
284 case md_exportformats:
285 add(MenuItem(MenuItem::ExportFormats));
288 case md_importformats:
289 add(MenuItem(MenuItem::ImportFormats));
292 case md_floatlistinsert:
293 add(MenuItem(MenuItem::FloatListInsert));
297 add(MenuItem(MenuItem::FloatInsert));
301 add(MenuItem(MenuItem::PasteRecent));
305 add(MenuItem(MenuItem::Branches));
310 // fallback to md_submenu
313 string const mlabel = _(lex.getString());
315 string const mname = lex.getString();
316 add(MenuItem(MenuItem::Submenu, mlabel, mname,
327 lex.printError("Menu::read: "
328 "Unknown menu tag: `$$Token'");
337 void Menu::checkShortcuts() const
339 // This is a quadratic algorithm, but we do not care because
340 // menus are short enough
341 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
342 string shortcut = it1->shortcut();
343 if (shortcut.empty())
345 if (!contains(it1->label(), shortcut))
346 lyxerr << "Menu warning: menu entry \""
348 << "\" does not contain shortcut `"
349 << shortcut << "'." << endl;
350 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
351 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
352 lyxerr << "Menu warning: menu entries "
353 << '"' << it1->fulllabel()
354 << "\" and \"" << it2->fulllabel()
355 << "\" share the same shortcut."
365 class compare_format {
367 bool operator()(Format const * p1, Format const * p2) {
372 string const limit_string_length(string const & str)
374 string::size_type const max_item_length = 45;
376 if (str.size() > max_item_length)
377 return str.substr(0, max_item_length - 3) + "...";
383 void expandLastfiles(Menu & tomenu, LyXView const * view)
385 LastFiles const & lastfiles = LyX::cref().lastfiles();
388 LastFiles::const_iterator lfit = lastfiles.begin();
389 LastFiles::const_iterator end = lastfiles.end();
391 for (; lfit != end && ii < 10; ++lfit, ++ii) {
392 string const label = tostr(ii) + ". "
393 + MakeDisplayPath((*lfit), 30)
395 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
399 void expandDocuments(Menu & tomenu, LyXView const * view)
401 typedef vector<string> Strings;
402 Strings const names = bufferlist.getFileNames();
405 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
406 FuncRequest(LFUN_NOACTION)), view);
411 Strings::const_iterator docit = names.begin();
412 Strings::const_iterator end = names.end();
413 for (; docit != end; ++docit, ++ii) {
414 string label = MakeDisplayPath(*docit, 20);
416 label = tostr(ii) + ". " + label + '|' + tostr(ii);
417 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_SWITCHBUFFER, *docit)), view);
422 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
424 if (!view->buffer() && kind != MenuItem::ImportFormats) {
425 tomenu.add(MenuItem(MenuItem::Command,
426 _("No Documents Open!"),
427 FuncRequest(LFUN_NOACTION)),
432 typedef vector<Format const *> Formats;
437 case MenuItem::ImportFormats:
438 formats = Importer::GetImportableFormats();
439 action = LFUN_IMPORT;
441 case MenuItem::ViewFormats:
442 formats = Exporter::GetExportableFormats(*view->buffer(), true);
443 action = LFUN_PREVIEW;
445 case MenuItem::UpdateFormats:
446 formats = Exporter::GetExportableFormats(*view->buffer(), true);
447 action = LFUN_UPDATE;
450 formats = Exporter::GetExportableFormats(*view->buffer(), false);
451 action = LFUN_EXPORT;
453 sort(formats.begin(), formats.end(), compare_format());
455 Formats::const_iterator fit = formats.begin();
456 Formats::const_iterator end = formats.end();
457 for (; fit != end ; ++fit) {
460 string label = (*fit)->prettyname();
461 // we need to hide the default graphic export formats
462 // from the external menu, because we need them only
463 // for the internal lyx-view and external latex run
464 if (label == "EPS" || label == "XPM" || label == "PNG")
467 if (kind == MenuItem::ImportFormats) {
468 if ((*fit)->name() == "text")
469 label = _("ASCII text as lines");
470 else if ((*fit)->name() == "textparagraph")
471 label = _("ASCII text as paragraphs");
474 if (!(*fit)->shortcut().empty())
475 label += '|' + (*fit)->shortcut();
477 tomenu.add(MenuItem(MenuItem::Command, label,
478 FuncRequest(action, (*fit)->name())),
484 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
486 if (!view->buffer()) {
487 tomenu.add(MenuItem(MenuItem::Command,
488 _("No Documents Open!"),
489 FuncRequest(LFUN_NOACTION)),
494 FloatList const & floats =
495 view->buffer()->params().getLyXTextClass().floats();
496 FloatList::const_iterator cit = floats.begin();
497 FloatList::const_iterator end = floats.end();
498 for (; cit != end; ++cit) {
499 tomenu.add(MenuItem(MenuItem::Command,
500 _(cit->second.listName()),
501 FuncRequest(LFUN_FLOAT_LIST,
502 cit->second.type())),
508 void expandFloatInsert(Menu & tomenu, LyXView const * view)
510 if (!view->buffer()) {
511 tomenu.add(MenuItem(MenuItem::Command,
512 _("No Documents Open!"),
513 FuncRequest(LFUN_NOACTION)),
518 FloatList const & floats =
519 view->buffer()->params().getLyXTextClass().floats();
520 FloatList::const_iterator cit = floats.begin();
521 FloatList::const_iterator end = floats.end();
522 for (; cit != end; ++cit) {
524 string const label = _(cit->second.name());
525 tomenu.add(MenuItem(MenuItem::Command, label,
526 FuncRequest(LFUN_INSET_FLOAT,
527 cit->second.type())),
533 void expandCharStyleInsert(Menu & tomenu, LyXView const * view)
535 if (!view->buffer()) {
536 tomenu.add(MenuItem(MenuItem::Command,
537 _("No Documents Open!"),
538 FuncRequest(LFUN_NOACTION)),
542 CharStyles & charstyles =
543 view->buffer()->params().getLyXTextClass().charstyles();
544 CharStyles::iterator cit = charstyles.begin();
545 CharStyles::iterator end = charstyles.end();
546 for (; cit != end; ++cit) {
547 string const label = cit->name;
548 tomenu.add(MenuItem(MenuItem::Command, label,
549 FuncRequest(LFUN_INSERT_CHARSTYLE,
555 Menu::size_type const max_number_of_items = 25;
557 void expandToc2(Menu & tomenu,
558 lyx::toc::Toc const & toc_list,
559 lyx::toc::Toc::size_type from,
560 lyx::toc::Toc::size_type to, int depth)
562 int shortcut_count = 0;
563 if (to - from <= max_number_of_items) {
564 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
565 string label(4 * max(0, toc_list[i].depth - depth),' ');
566 label += limit_string_length(toc_list[i].str);
567 if (toc_list[i].depth == depth
568 && shortcut_count < 9) {
569 if (label.find(tostr(shortcut_count + 1)) != string::npos)
570 label += '|' + tostr(++shortcut_count);
572 tomenu.add(MenuItem(MenuItem::Command, label,
573 FuncRequest(toc_list[i].action())));
576 lyx::toc::Toc::size_type pos = from;
578 lyx::toc::Toc::size_type new_pos = pos + 1;
579 while (new_pos < to &&
580 toc_list[new_pos].depth > depth)
583 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
584 label += limit_string_length(toc_list[pos].str);
585 if (toc_list[pos].depth == depth &&
586 shortcut_count < 9) {
587 if (label.find(tostr(shortcut_count + 1)) != string::npos)
588 label += '|' + tostr(++shortcut_count);
590 if (new_pos == pos + 1) {
591 tomenu.add(MenuItem(MenuItem::Command,
592 label, FuncRequest(toc_list[pos].action())));
594 MenuItem item(MenuItem::Submenu, label);
595 item.submenu(new Menu);
596 expandToc2(*item.submenu(),
597 toc_list, pos, new_pos, depth + 1);
606 void expandToc(Menu & tomenu, LyXView const * view)
608 // To make things very cleanly, we would have to pass view to
609 // all MenuItem constructors and to expandToc2. However, we
610 // know that all the entries in a TOC will be have status_ ==
611 // OK, so we avoid this unnecessary overhead (JMarc)
613 if (!view->buffer()) {
614 tomenu.add(MenuItem(MenuItem::Command,
615 _("No Documents Open!"),
616 FuncRequest(LFUN_NOACTION)),
621 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
622 lyx::toc::TocList::const_iterator cit = toc_list.begin();
623 lyx::toc::TocList::const_iterator end = toc_list.end();
624 for (; cit != end; ++cit) {
626 if (cit->first == "TOC")
629 // All the rest is for floats
630 auto_ptr<Menu> menu(new Menu);
631 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
632 lyx::toc::Toc::const_iterator eend = cit->second.end();
633 for (; ccit != eend; ++ccit) {
634 string const label = limit_string_length(ccit->str);
635 menu->add(MenuItem(MenuItem::Command,
637 FuncRequest(ccit->action())));
639 string const & floatName = cit->first;
640 // Is the _(...) really needed here? (Lgb)
641 MenuItem item(MenuItem::Submenu, _(floatName));
642 item.submenu(menu.release());
647 cit = toc_list.find("TOC");
649 tomenu.add(MenuItem(MenuItem::Command,
650 _("No Table of contents"),
654 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
659 void expandPasteRecent(Menu & tomenu, LyXView const * view)
661 if (!view || !view->buffer())
664 vector<string> const selL =
665 CutAndPaste::availableSelections(*view->buffer());
667 vector<string>::const_iterator cit = selL.begin();
668 vector<string>::const_iterator end = selL.end();
670 for (unsigned int index = 0; cit != end; ++cit, ++index) {
671 tomenu.add(MenuItem(MenuItem::Command, *cit,
672 FuncRequest(LFUN_PASTE, tostr(index))));
677 void expandBranches(Menu & tomenu, LyXView const * view)
679 if (!view || !view->buffer())
682 BufferParams const & params = view->buffer()->params();
684 std::list<Branch>::const_iterator cit = params.branchlist().begin();
685 std::list<Branch>::const_iterator end = params.branchlist().end();
687 for (int ii = 1; cit != end; ++cit, ++ii) {
688 string label = cit->getBranch();
690 label = tostr(ii) + ". " + label + "|" + tostr(ii);
691 tomenu.add(MenuItem(MenuItem::Command, label,
692 FuncRequest(LFUN_INSERT_BRANCH,
693 cit->getBranch())), view);
701 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
702 LyXView const * view) const
704 for (Menu::const_iterator cit = frommenu.begin();
705 cit != frommenu.end() ; ++cit) {
706 switch (cit->kind()) {
707 case MenuItem::Lastfiles:
708 expandLastfiles(tomenu, view);
711 case MenuItem::Documents:
712 expandDocuments(tomenu, view);
715 case MenuItem::ImportFormats:
716 case MenuItem::ViewFormats:
717 case MenuItem::UpdateFormats:
718 case MenuItem::ExportFormats:
719 expandFormats(cit->kind(), tomenu, view);
722 case MenuItem::CharStyles:
723 expandCharStyleInsert(tomenu, view);
726 case MenuItem::FloatListInsert:
727 expandFloatListInsert(tomenu, view);
730 case MenuItem::FloatInsert:
731 expandFloatInsert(tomenu, view);
734 case MenuItem::PasteRecent:
735 expandPasteRecent(tomenu, view);
738 case MenuItem::Branches:
739 expandBranches(tomenu, view);
743 expandToc(tomenu, view);
746 case MenuItem::Submenu: {
748 item.submenu(new Menu(cit->submenuname()));
749 expand(getMenu(cit->submenuname()),
750 *item.submenu(), view);
751 tomenu.add(item, view);
756 tomenu.add(*cit, view);
760 // we do not want the menu to end with a separator
762 && tomenu.items_.back().kind() == MenuItem::Separator)
763 tomenu.items_.pop_back();
765 // Check whether the shortcuts are unique
766 tomenu.checkShortcuts();
770 bool Menu::hasSubmenu(string const & name) const
772 return find_if(begin(), end(),
773 lyx::compare_memfun(&MenuItem::submenuname,
778 void MenuBackend::read(LyXLex & lex)
787 struct keyword_item menutags[md_last - 1] = {
788 { "end", md_endmenuset },
790 { "menubar", md_menubar }
794 if (compare_ascii_no_case(lex.getString(), "menuset")) {
795 lyxerr << "Menubackend::read: ERROR wrong token:`"
796 << lex.getString() << '\'' << endl;
799 lex.pushTable(menutags, md_last - 1);
800 if (lyxerr.debugging(Debug::PARSER))
801 lex.printTable(lyxerr);
805 while (lex.isOK() && !quit) {
812 string const name = lex.getString();
814 getMenu(name).read(lex);
826 lex.printError("menubackend::read: "
827 "Unknown menu tag: `$$Token'");
835 void MenuBackend::add(Menu const & menu)
837 menulist_.push_back(menu);
841 bool MenuBackend::hasMenu(string const & name) const
843 return find_if(begin(), end(),
844 lyx::compare_memfun(&Menu::name, name)) != end();
848 Menu const & MenuBackend::getMenu(string const & name) const
850 const_iterator cit = find_if(begin(), end(),
851 lyx::compare_memfun(&Menu::name, name));
853 lyxerr << "No submenu named " << name << endl;
854 BOOST_ASSERT(cit != end());
859 Menu & MenuBackend::getMenu(string const & name)
861 MenuList::iterator it =
862 find_if(menulist_.begin(), menulist_.end(),
863 lyx::compare_memfun(&Menu::name, name));
864 BOOST_ASSERT(it != menulist_.end());
869 Menu const & MenuBackend::getMenubar() const