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);
123 lyxerr << "No bindings for "
124 << lyxaction.getActionName(func_.action)
125 << '(' << func_.argument << ')' << endl;
132 Menu & Menu::add(MenuItem const & i, LyXView const * view)
140 case MenuItem::Command:
143 view->getLyXFunc().getStatus(i.func());
145 || (status.disabled() && i.optional()))
148 items_.back().status(status);
151 case MenuItem::Submenu:
154 bool disabled = true;
155 for (const_iterator cit = i.submenu()->begin();
156 cit != i.submenu()->end(); ++cit) {
157 if ((cit->kind() == MenuItem::Command
158 || cit->kind() == MenuItem::Submenu)
159 && !cit->status().disabled()) {
164 if (!disabled || !i.optional()) {
166 items_.back().status().disabled(disabled);
173 case MenuItem::Separator:
175 && items_.back().kind() != MenuItem::Separator)
186 Menu & Menu::read(LyXLex & lex)
209 struct keyword_item menutags[md_last - 1] = {
210 { "branches", md_branches },
211 { "documents", md_documents },
212 { "end", md_endmenu },
213 { "exportformats", md_exportformats },
214 { "floatinsert", md_floatinsert },
215 { "floatlistinsert", md_floatlistinsert },
216 { "importformats", md_importformats },
218 { "lastfiles", md_lastfiles },
219 { "optitem", md_optitem },
220 { "optsubmenu", md_optsubmenu },
221 { "pasterecent", md_pasterecent },
222 { "separator", md_separator },
223 { "submenu", md_submenu },
225 { "updateformats", md_updateformats },
226 { "viewformats", md_viewformats }
229 lex.pushTable(menutags, md_last - 1);
230 if (lyxerr.debugging(Debug::PARSER))
231 lex.printTable(lyxerr);
234 bool optional = false;
236 while (lex.isOK() && !quit) {
240 // fallback to md_item
243 string const name = _(lex.getString());
245 string const command = lex.getString();
246 FuncRequest func = lyxaction.lookupFunc(command);
247 add(MenuItem(MenuItem::Command, name, func, optional));
253 add(MenuItem(MenuItem::Separator));
257 add(MenuItem(MenuItem::Lastfiles));
261 add(MenuItem(MenuItem::Documents));
265 add(MenuItem(MenuItem::Toc));
269 add(MenuItem(MenuItem::ViewFormats));
272 case md_updateformats:
273 add(MenuItem(MenuItem::UpdateFormats));
276 case md_exportformats:
277 add(MenuItem(MenuItem::ExportFormats));
280 case md_importformats:
281 add(MenuItem(MenuItem::ImportFormats));
284 case md_floatlistinsert:
285 add(MenuItem(MenuItem::FloatListInsert));
289 add(MenuItem(MenuItem::FloatInsert));
293 add(MenuItem(MenuItem::PasteRecent));
297 add(MenuItem(MenuItem::Branches));
302 // fallback to md_submenu
305 string const mlabel = _(lex.getString());
307 string const mname = lex.getString();
308 add(MenuItem(MenuItem::Submenu, mlabel, mname,
319 lex.printError("Menu::read: "
320 "Unknown menu tag: `$$Token'");
329 void Menu::checkShortcuts() const
331 // This is a quadratic algorithm, but we do not care because
332 // menus are short enough
333 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
334 string shortcut = it1->shortcut();
335 if (shortcut.empty())
337 if (!contains(it1->label(), shortcut))
338 lyxerr << "Menu warning: menu entry \""
340 << "\" does not contain shortcut `"
341 << shortcut << "'." << endl;
342 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
343 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
344 lyxerr << "Menu warning: menu entries "
345 << '"' << it1->fulllabel()
346 << "\" and \"" << it2->fulllabel()
347 << "\" share the same shortcut."
357 class compare_format {
359 bool operator()(Format const * p1, Format const * p2) {
364 string const limit_string_length(string const & str)
366 string::size_type const max_item_length = 45;
368 if (str.size() > max_item_length)
369 return str.substr(0, max_item_length - 3) + "...";
375 void expandLastfiles(Menu & tomenu, LyXView const * view)
378 LastFiles::const_iterator lfit = lastfiles->begin();
379 LastFiles::const_iterator end = lastfiles->end();
381 for (; lfit != end && ii < 10; ++lfit, ++ii) {
382 string const label = tostr(ii) + ". "
383 + MakeDisplayPath((*lfit), 30)
385 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
389 void expandDocuments(Menu & tomenu, LyXView const * view)
391 typedef vector<string> Strings;
392 Strings const names = bufferlist.getFileNames();
395 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
396 FuncRequest(LFUN_NOACTION)), view);
401 Strings::const_iterator docit = names.begin();
402 Strings::const_iterator end = names.end();
403 for (; docit != end; ++docit, ++ii) {
404 string label = MakeDisplayPath(*docit, 20);
406 label = tostr(ii) + ". " + label + '|' + tostr(ii);
407 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_SWITCHBUFFER, *docit)), view);
412 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
414 if (!view->buffer() && kind != MenuItem::ImportFormats) {
415 tomenu.add(MenuItem(MenuItem::Command,
416 _("No Documents Open!"),
417 FuncRequest(LFUN_NOACTION)),
422 typedef vector<Format const *> Formats;
427 case MenuItem::ImportFormats:
428 formats = Importer::GetImportableFormats();
429 action = LFUN_IMPORT;
431 case MenuItem::ViewFormats:
432 formats = Exporter::GetExportableFormats(*view->buffer(), true);
433 action = LFUN_PREVIEW;
435 case MenuItem::UpdateFormats:
436 formats = Exporter::GetExportableFormats(*view->buffer(), true);
437 action = LFUN_UPDATE;
440 formats = Exporter::GetExportableFormats(*view->buffer(), false);
441 action = LFUN_EXPORT;
443 sort(formats.begin(), formats.end(), compare_format());
445 Formats::const_iterator fit = formats.begin();
446 Formats::const_iterator end = formats.end();
447 for (; fit != end ; ++fit) {
450 string label = (*fit)->prettyname();
451 // we need to hide the default graphic export formats
452 // from the external menu, because we need them only
453 // for the internal lyx-view and external latex run
454 if (label == "EPS" || label == "XPM" || label == "PNG")
457 if (kind == MenuItem::ImportFormats) {
458 if ((*fit)->name() == "text")
459 label = _("ASCII text as lines");
460 else if ((*fit)->name() == "textparagraph")
461 label = _("ASCII text as paragraphs");
464 if (!(*fit)->shortcut().empty())
465 label += '|' + (*fit)->shortcut();
467 tomenu.add(MenuItem(MenuItem::Command, label,
468 FuncRequest(action, (*fit)->name())),
474 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
476 if (!view->buffer()) {
477 tomenu.add(MenuItem(MenuItem::Command,
478 _("No Documents Open!"),
479 FuncRequest(LFUN_NOACTION)),
484 FloatList const & floats =
485 view->buffer()->params().getLyXTextClass().floats();
486 FloatList::const_iterator cit = floats.begin();
487 FloatList::const_iterator end = floats.end();
488 for (; cit != end; ++cit) {
489 tomenu.add(MenuItem(MenuItem::Command,
490 _(cit->second.listName()),
491 FuncRequest(LFUN_FLOAT_LIST,
492 cit->second.type())),
498 void expandFloatInsert(Menu & tomenu, LyXView const * view)
500 if (!view->buffer()) {
501 tomenu.add(MenuItem(MenuItem::Command,
502 _("No Documents Open!"),
503 FuncRequest(LFUN_NOACTION)),
508 FloatList const & floats =
509 view->buffer()->params().getLyXTextClass().floats();
510 FloatList::const_iterator cit = floats.begin();
511 FloatList::const_iterator end = floats.end();
512 for (; cit != end; ++cit) {
514 string const label = _(cit->second.name());
515 tomenu.add(MenuItem(MenuItem::Command, label,
516 FuncRequest(LFUN_INSET_FLOAT,
517 cit->second.type())),
523 Menu::size_type const max_number_of_items = 25;
525 void expandToc2(Menu & tomenu,
526 lyx::toc::Toc const & toc_list,
527 lyx::toc::Toc::size_type from,
528 lyx::toc::Toc::size_type to, int depth)
530 int shortcut_count = 0;
531 if (to - from <= max_number_of_items) {
532 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
533 string label(4 * max(0, toc_list[i].depth - depth),' ');
534 label += limit_string_length(toc_list[i].str);
535 if (toc_list[i].depth == depth
536 && ++shortcut_count <= 9) {
537 label += '|' + tostr(shortcut_count);
539 tomenu.add(MenuItem(MenuItem::Command, label,
540 FuncRequest(toc_list[i].action())));
543 lyx::toc::Toc::size_type pos = from;
545 lyx::toc::Toc::size_type new_pos = pos + 1;
546 while (new_pos < to &&
547 toc_list[new_pos].depth > depth)
550 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
551 label += limit_string_length(toc_list[pos].str);
552 if (toc_list[pos].depth == depth &&
553 ++shortcut_count <= 9)
554 label += '|' + tostr(shortcut_count);
556 if (new_pos == pos + 1) {
557 tomenu.add(MenuItem(MenuItem::Command,
558 label, FuncRequest(toc_list[pos].action())));
560 MenuItem item(MenuItem::Submenu, label);
561 item.submenu(new Menu);
562 expandToc2(*item.submenu(),
563 toc_list, pos, new_pos, depth + 1);
572 void expandToc(Menu & tomenu, LyXView const * view)
574 // To make things very cleanly, we would have to pass view to
575 // all MenuItem constructors and to expandToc2. However, we
576 // know that all the entries in a TOC will be have status_ ==
577 // OK, so we avoid this unnecessary overhead (JMarc)
579 if (!view->buffer()) {
580 tomenu.add(MenuItem(MenuItem::Command,
581 _("No Documents Open!"),
582 FuncRequest(LFUN_NOACTION)),
587 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
588 lyx::toc::TocList::const_iterator cit = toc_list.begin();
589 lyx::toc::TocList::const_iterator end = toc_list.end();
590 for (; cit != end; ++cit) {
592 if (cit->first == "TOC")
595 // All the rest is for floats
596 Menu * menu = new Menu;
597 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
598 lyx::toc::Toc::const_iterator eend = cit->second.end();
599 for (; ccit != eend; ++ccit) {
600 string const label = limit_string_length(ccit->str);
601 menu->add(MenuItem(MenuItem::Command,
603 FuncRequest(ccit->action())));
605 string const & floatName = cit->first;
606 // Is the _(...) really needed here? (Lgb)
607 MenuItem item(MenuItem::Submenu, _(floatName));
613 cit = toc_list.find("TOC");
615 tomenu.add(MenuItem(MenuItem::Command,
616 _("No Table of contents"),
620 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
625 void expandPasteRecent(Menu & tomenu, LyXView const * view)
627 if (!view || !view->buffer())
630 vector<string> const selL =
631 CutAndPaste::availableSelections(*view->buffer());
633 vector<string>::const_iterator cit = selL.begin();
634 vector<string>::const_iterator end = selL.end();
636 for (unsigned int index = 0; cit != end; ++cit, ++index) {
637 tomenu.add(MenuItem(MenuItem::Command, *cit,
638 FuncRequest(LFUN_PASTE, tostr(index))));
643 void expandBranches(Menu & tomenu, LyXView const * view)
645 if (!view || !view->buffer())
648 BufferParams const & params = view->buffer()->params();
650 std::list<Branch>::const_iterator cit = params.branchlist().begin();
651 std::list<Branch>::const_iterator end = params.branchlist().end();
653 for (int ii = 1; cit != end; ++cit, ++ii) {
654 string label = cit->getBranch();
656 label = tostr(ii) + ". " + label + "|" + tostr(ii);
657 tomenu.add(MenuItem(MenuItem::Command, label,
658 FuncRequest(LFUN_INSERT_BRANCH,
659 cit->getBranch())), view);
667 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
668 LyXView const * view) const
670 for (Menu::const_iterator cit = frommenu.begin();
671 cit != frommenu.end() ; ++cit) {
672 switch (cit->kind()) {
673 case MenuItem::Lastfiles:
674 expandLastfiles(tomenu, view);
677 case MenuItem::Documents:
678 expandDocuments(tomenu, view);
681 case MenuItem::ImportFormats:
682 case MenuItem::ViewFormats:
683 case MenuItem::UpdateFormats:
684 case MenuItem::ExportFormats:
685 expandFormats(cit->kind(), tomenu, view);
688 case MenuItem::FloatListInsert:
689 expandFloatListInsert(tomenu, view);
692 case MenuItem::FloatInsert:
693 expandFloatInsert(tomenu, view);
696 case MenuItem::PasteRecent:
697 expandPasteRecent(tomenu, view);
700 case MenuItem::Branches:
701 expandBranches(tomenu, view);
705 expandToc(tomenu, view);
708 case MenuItem::Submenu: {
710 item.submenu(new Menu(cit->submenuname()));
711 expand(getMenu(cit->submenuname()),
712 *item.submenu(), view);
713 tomenu.add(item, view);
718 tomenu.add(*cit, view);
722 // we do not want the menu to end with a separator
724 && tomenu.items_.back().kind() == MenuItem::Separator)
725 tomenu.items_.pop_back();
727 // Check whether the shortcuts are unique
728 tomenu.checkShortcuts();
732 bool Menu::hasSubmenu(string const & name) const
734 return find_if(begin(), end(),
735 lyx::compare_memfun(&MenuItem::submenuname,
740 void MenuBackend::read(LyXLex & lex)
749 struct keyword_item menutags[md_last - 1] = {
750 { "end", md_endmenuset },
752 { "menubar", md_menubar }
756 if (compare_ascii_no_case(lex.getString(), "menuset")) {
757 lyxerr << "Menubackend::read: ERROR wrong token:`"
758 << lex.getString() << '\'' << endl;
761 lex.pushTable(menutags, md_last - 1);
762 if (lyxerr.debugging(Debug::PARSER))
763 lex.printTable(lyxerr);
767 while (lex.isOK() && !quit) {
774 string const name = lex.getString();
776 getMenu(name).read(lex);
788 lex.printError("menubackend::read: "
789 "Unknown menu tag: `$$Token'");
797 void MenuBackend::add(Menu const & menu)
799 menulist_.push_back(menu);
803 bool MenuBackend::hasMenu(string const & name) const
805 return find_if(begin(), end(),
806 lyx::compare_memfun(&Menu::name, name)) != end();
810 Menu const & MenuBackend::getMenu(string const & name) const
812 const_iterator cit = find_if(begin(), end(),
813 lyx::compare_memfun(&Menu::name, name));
815 lyxerr << "No submenu named " << name << endl;
816 BOOST_ASSERT(cit != end());
821 Menu & MenuBackend::getMenu(string const & name)
823 MenuList::iterator it =
824 find_if(menulist_.begin(), menulist_.end(),
825 lyx::compare_memfun(&Menu::name, name));
826 BOOST_ASSERT(it != menulist_.end());
831 Menu const & MenuBackend::getMenubar() const