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 "LyXAction.h"
24 #include "lastfiles.h"
26 #include "lyx_main.h" // for lastfiles
27 #include "bufferlist.h"
32 #include "FloatList.h"
34 #include "CutAndPaste.h"
35 #include "frontends/LyXView.h"
36 #include "support/LAssert.h"
37 #include "support/filetools.h"
38 #include "support/lyxfunctional.h"
39 #include "support/lstrings.h"
40 #include "support/tostr.h"
44 extern BufferList bufferlist;
45 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
47 using namespace lyx::support;
56 // This is the global menu definition
57 MenuBackend menubackend;
60 MenuItem::MenuItem(Kind kind, string const & label,
61 string const & command, bool optional)
62 : kind_(kind), label_(label), optional_(optional)
79 action_ = lyxaction.LookupFunc(command);
81 if (action_ == LFUN_UNKNOWN_ACTION) {
82 lyxerr << "MenuItem(): LyX command `"
83 << command << "' does not exist." << endl;
86 lyxerr[Debug::GUI] << "Optional item "
90 submenuname_ = command;
96 MenuItem::MenuItem(Kind kind, string const & label, int action, bool optional)
97 : kind_(kind), label_(label), action_(action), submenuname_(),
102 MenuItem::~MenuItem()
106 void MenuItem::submenu(Menu * menu)
108 submenu_.reset(menu);
112 string const MenuItem::label() const
114 return token(label_, '|', 0);
118 string const MenuItem::shortcut() const
120 return token(label_, '|', 1);
123 string const MenuItem::binding() const
125 if (kind_ != Command)
128 // Get the keys bound to this action, but keep only the
130 string bindings = toplevel_keymap->findbinding(action_);
132 if (!bindings.empty()) {
133 return bindings.substr(1, bindings.find(']') - 1);
139 Menu & Menu::add(MenuItem const & i, LyXView const * view)
147 case MenuItem::Command:
150 view->getLyXFunc().getStatus(i.action());
152 || (status.disabled() && i.optional()))
155 items_.back().status(status);
158 case MenuItem::Submenu:
161 bool disabled = true;
162 for (const_iterator cit = i.submenu()->begin();
163 cit != i.submenu()->end(); ++cit) {
164 if ((cit->kind() == MenuItem::Command
165 || cit->kind() == MenuItem::Submenu)
166 && !cit->status().disabled()) {
171 if (!disabled || !i.optional()) {
173 items_.back().status().disabled(disabled);
180 case MenuItem::Separator:
182 && items_.back().kind() != MenuItem::Separator)
193 Menu & Menu::read(LyXLex & lex)
216 struct keyword_item menutags[md_last - 1] = {
217 { "branches", md_branches },
218 { "documents", md_documents },
219 { "end", md_endmenu },
220 { "exportformats", md_exportformats },
221 { "floatinsert", md_floatinsert },
222 { "floatlistinsert", md_floatlistinsert },
223 { "importformats", md_importformats },
225 { "lastfiles", md_lastfiles },
226 { "optitem", md_optitem },
227 { "optsubmenu", md_optsubmenu },
228 { "pasterecent", md_pasterecent },
229 { "separator", md_separator },
230 { "submenu", md_submenu },
232 { "updateformats", md_updateformats },
233 { "viewformats", md_viewformats }
236 lex.pushTable(menutags, md_last - 1);
237 if (lyxerr.debugging(Debug::PARSER))
238 lex.printTable(lyxerr);
241 bool optional = false;
243 while (lex.isOK() && !quit) {
247 // fallback to md_item
250 string const name = _(lex.getString());
252 string const command = lex.getString();
253 add(MenuItem(MenuItem::Command, name,
260 add(MenuItem(MenuItem::Separator));
264 add(MenuItem(MenuItem::Lastfiles));
268 add(MenuItem(MenuItem::Documents));
272 add(MenuItem(MenuItem::Toc));
276 add(MenuItem(MenuItem::ViewFormats));
279 case md_updateformats:
280 add(MenuItem(MenuItem::UpdateFormats));
283 case md_exportformats:
284 add(MenuItem(MenuItem::ExportFormats));
287 case md_importformats:
288 add(MenuItem(MenuItem::ImportFormats));
291 case md_floatlistinsert:
292 add(MenuItem(MenuItem::FloatListInsert));
296 add(MenuItem(MenuItem::FloatInsert));
300 add(MenuItem(MenuItem::PasteRecent));
304 add(MenuItem(MenuItem::Branches));
309 // fallback to md_submenu
312 string const mlabel = _(lex.getString());
314 string const mname = lex.getString();
315 add(MenuItem(MenuItem::Submenu, mlabel, mname,
326 lex.printError("Menu::read: "
327 "Unknown menu tag: `$$Token'");
336 void Menu::checkShortcuts() const
338 // This is a quadratic algorithm, but we do not care because
339 // menus are short enough
340 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
341 string shortcut = it1->shortcut();
342 if (shortcut.empty())
344 if (!contains(it1->label(), shortcut))
345 lyxerr << "Menu warning: menu entry \""
347 << "\" does not contain shortcut `"
348 << shortcut << "'." << endl;
349 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
350 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
351 lyxerr << "Menu warning: menu entries "
352 << '"' << it1->fulllabel()
353 << "\" and \"" << it2->fulllabel()
354 << "\" share the same shortcut."
364 class compare_format {
366 bool operator()(Format const * p1, Format const * p2) {
371 string const limit_string_length(string const & str)
373 string::size_type const max_item_length = 45;
375 if (str.size() > max_item_length)
376 return str.substr(0, max_item_length - 3) + "...";
382 void expandLastfiles(Menu & tomenu, LyXView const * view)
385 LastFiles::const_iterator lfit = lastfiles->begin();
386 LastFiles::const_iterator end = lastfiles->end();
388 for (; lfit != end && ii < 10; ++lfit, ++ii) {
389 string const label = tostr(ii) + ". "
390 + MakeDisplayPath((*lfit), 30)
392 int const action = lyxaction.
393 getPseudoAction(LFUN_FILE_OPEN,
395 tomenu.add(MenuItem(MenuItem::Command, label, action), 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 LFUN_NOACTION), view);
411 Strings::const_iterator docit = names.begin();
412 Strings::const_iterator end = names.end();
413 for (; docit != end; ++docit, ++ii) {
415 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
416 string label = MakeDisplayPath(*docit, 20);
418 label = tostr(ii) + ". " + label + '|' + tostr(ii);
419 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
424 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
426 if (!view->buffer() && kind != MenuItem::ImportFormats) {
427 tomenu.add(MenuItem(MenuItem::Command,
428 _("No Documents Open!"), LFUN_NOACTION),
433 typedef vector<Format const *> Formats;
438 case MenuItem::ImportFormats:
439 formats = Importer::GetImportableFormats();
440 action = LFUN_IMPORT;
442 case MenuItem::ViewFormats:
443 formats = Exporter::GetExportableFormats(view->buffer(), true);
444 action = LFUN_PREVIEW;
446 case MenuItem::UpdateFormats:
447 formats = Exporter::GetExportableFormats(view->buffer(), true);
448 action = LFUN_UPDATE;
451 formats = Exporter::GetExportableFormats(view->buffer(), false);
452 action = LFUN_EXPORT;
454 sort(formats.begin(), formats.end(), compare_format());
456 Formats::const_iterator fit = formats.begin();
457 Formats::const_iterator end = formats.end();
458 for (; fit != end ; ++fit) {
461 string label = (*fit)->prettyname();
462 // we need to hide the default graphic export formats
463 // from the external menu, because we need them only
464 // for the internal lyx-view and external latex run
465 if (label == "EPS" || label == "XPM" || label == "PNG")
468 if (kind == MenuItem::ImportFormats) {
469 if ((*fit)->name() == "text")
470 label = _("ASCII text as lines");
471 else if ((*fit)->name() == "textparagraph")
472 label = _("ASCII text as paragraphs");
475 if (!(*fit)->shortcut().empty())
476 label += '|' + (*fit)->shortcut();
477 int const action2 = lyxaction.
478 getPseudoAction(action, (*fit)->name());
479 tomenu.add(MenuItem(MenuItem::Command, label, action2),
485 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
487 if (!view->buffer()) {
488 tomenu.add(MenuItem(MenuItem::Command,
489 _("No Documents Open!"), 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 int const action = lyxaction
500 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
501 tomenu.add(MenuItem(MenuItem::Command,
502 _(cit->second.listName()), action),
508 void expandFloatInsert(Menu & tomenu, LyXView const * view)
510 if (!view->buffer()) {
511 tomenu.add(MenuItem(MenuItem::Command,
512 _("No Documents Open!"), LFUN_NOACTION),
517 FloatList const & floats =
518 view->buffer()->params.getLyXTextClass().floats();
519 FloatList::const_iterator cit = floats.begin();
520 FloatList::const_iterator end = floats.end();
521 for (; cit != end; ++cit) {
524 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
526 string const label = _(cit->second.name());
527 tomenu.add(MenuItem(MenuItem::Command, label, action),
533 Menu::size_type const max_number_of_items = 25;
535 void expandToc2(Menu & tomenu,
536 lyx::toc::Toc const & toc_list,
537 lyx::toc::Toc::size_type from,
538 lyx::toc::Toc::size_type to, int depth)
540 int shortcut_count = 0;
541 if (to - from <= max_number_of_items) {
542 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
543 int const action = toc_list[i].action();
544 string label(4 * max(0, toc_list[i].depth - depth),' ');
545 label += limit_string_length(toc_list[i].str);
546 if (toc_list[i].depth == depth
547 && ++shortcut_count <= 9) {
548 label += '|' + tostr(shortcut_count);
550 tomenu.add(MenuItem(MenuItem::Command, label, action));
553 lyx::toc::Toc::size_type pos = from;
555 lyx::toc::Toc::size_type new_pos = pos + 1;
556 while (new_pos < to &&
557 toc_list[new_pos].depth > depth)
560 int const action = toc_list[pos].action();
561 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
562 label += limit_string_length(toc_list[pos].str);
563 if (toc_list[pos].depth == depth &&
564 ++shortcut_count <= 9)
565 label += '|' + tostr(shortcut_count);
567 if (new_pos == pos + 1) {
568 tomenu.add(MenuItem(MenuItem::Command,
571 MenuItem item(MenuItem::Submenu, label);
572 item.submenu(new Menu);
573 expandToc2(*item.submenu(),
574 toc_list, pos, new_pos, depth + 1);
583 void expandToc(Menu & tomenu, LyXView const * view)
585 // To make things very cleanly, we would have to pass view to
586 // all MenuItem constructors and to expandToc2. However, we
587 // know that all the entries in a TOC will be have status_ ==
588 // OK, so we avoid this unnecessary overhead (JMarc)
590 if (!view->buffer()) {
591 tomenu.add(MenuItem(MenuItem::Command,
592 _("No Documents Open!"), LFUN_NOACTION),
597 lyx::toc::TocList toc_list = lyx::toc::getTocList(view->buffer());
598 lyx::toc::TocList::const_iterator cit = toc_list.begin();
599 lyx::toc::TocList::const_iterator end = toc_list.end();
600 for (; cit != end; ++cit) {
602 if (cit->first == "TOC")
605 // All the rest is for floats
606 Menu * menu = new Menu;
607 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
608 lyx::toc::Toc::const_iterator eend = cit->second.end();
609 for (; ccit != eend; ++ccit) {
610 string const label = limit_string_length(ccit->str);
611 menu->add(MenuItem(MenuItem::Command,
612 label, ccit->action()));
614 string const & floatName = cit->first;
615 // Is the _(...) really needed here? (Lgb)
616 MenuItem item(MenuItem::Submenu, _(floatName));
622 cit = toc_list.find("TOC");
624 tomenu.add(MenuItem(MenuItem::Command,
625 _("No Table of contents")),
628 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
633 void expandPasteRecent(Menu & tomenu, LyXView const * view)
635 if (!view || !view->buffer())
638 vector<string> const selL =
639 CutAndPaste::availableSelections(*view->buffer());
641 vector<string>::const_iterator cit = selL.begin();
642 vector<string>::const_iterator end = selL.end();
644 for (unsigned int index = 0; cit != end; ++cit, ++index) {
645 int const action = lyxaction.getPseudoAction(LFUN_PASTE,
647 tomenu.add(MenuItem(MenuItem::Command, *cit, action));
652 void expandBranches(Menu & tomenu, LyXView const * view)
654 if (!view || !view->buffer())
657 BufferParams const & params = view->buffer()->params;
659 std::list<Branch>::const_iterator cit = params.branchlist.begin();
660 std::list<Branch>::const_iterator end = params.branchlist.end();
662 for (int ii = 1; cit != end; ++cit, ++ii) {
663 string label = cit->getBranch();
664 int const action = lyxaction.
665 getPseudoAction(LFUN_INSERT_BRANCH,
668 label = tostr(ii) + ". " + label + "|" + tostr(ii);
669 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
677 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
678 LyXView const * view) const
680 for (Menu::const_iterator cit = frommenu.begin();
681 cit != frommenu.end() ; ++cit) {
682 switch (cit->kind()) {
683 case MenuItem::Lastfiles:
684 expandLastfiles(tomenu, view);
687 case MenuItem::Documents:
688 expandDocuments(tomenu, view);
691 case MenuItem::ImportFormats:
692 case MenuItem::ViewFormats:
693 case MenuItem::UpdateFormats:
694 case MenuItem::ExportFormats:
695 expandFormats(cit->kind(), tomenu, view);
698 case MenuItem::FloatListInsert:
699 expandFloatListInsert(tomenu, view);
702 case MenuItem::FloatInsert:
703 expandFloatInsert(tomenu, view);
706 case MenuItem::PasteRecent:
707 expandPasteRecent(tomenu, view);
710 case MenuItem::Branches:
711 expandBranches(tomenu, view);
715 expandToc(tomenu, view);
718 case MenuItem::Submenu: {
720 item.submenu(new Menu(cit->submenuname()));
721 expand(getMenu(cit->submenuname()),
722 *item.submenu(), view);
723 tomenu.add(item, view);
728 tomenu.add(*cit, view);
732 // we do not want the menu to end with a separator
734 && tomenu.items_.back().kind() == MenuItem::Separator)
735 tomenu.items_.pop_back();
737 // Check whether the shortcuts are unique
738 tomenu.checkShortcuts();
742 bool Menu::hasSubmenu(string const & name) const
744 return find_if(begin(), end(),
745 lyx::compare_memfun(&MenuItem::submenuname,
750 void MenuBackend::read(LyXLex & lex)
759 struct keyword_item menutags[md_last - 1] = {
760 { "end", md_endmenuset },
762 { "menubar", md_menubar }
766 if (compare_ascii_no_case(lex.getString(), "menuset")) {
767 lyxerr << "Menubackend::read: ERROR wrong token:`"
768 << lex.getString() << '\'' << endl;
771 lex.pushTable(menutags, md_last - 1);
772 if (lyxerr.debugging(Debug::PARSER))
773 lex.printTable(lyxerr);
777 while (lex.isOK() && !quit) {
784 string const name = lex.getString();
786 getMenu(name).read(lex);
798 lex.printError("menubackend::read: "
799 "Unknown menu tag: `$$Token'");
807 void MenuBackend::add(Menu const & menu)
809 menulist_.push_back(menu);
813 bool MenuBackend::hasMenu(string const & name) const
815 return find_if(begin(), end(),
816 lyx::compare_memfun(&Menu::name, name)) != end();
820 Menu const & MenuBackend::getMenu(string const & name) const
822 const_iterator cit = find_if(begin(), end(),
823 lyx::compare_memfun(&Menu::name, name));
825 lyxerr << "No submenu named " << name << endl;
826 Assert(cit != end());
831 Menu & MenuBackend::getMenu(string const & name)
833 MenuList::iterator it =
834 find_if(menulist_.begin(), menulist_.end(),
835 lyx::compare_memfun(&Menu::name, name));
836 Assert(it != menulist_.end());
841 Menu const & MenuBackend::getMenubar() const