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 extern BufferList bufferlist;
50 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
52 using namespace lyx::support;
61 // This is the global menu definition
62 MenuBackend menubackend;
65 MenuItem::MenuItem(Kind kind, string const & label,
66 string const & command, bool optional)
67 : kind_(kind), label_(label), optional_(optional)
84 action_ = lyxaction.LookupFunc(command);
86 if (action_ == LFUN_UNKNOWN_ACTION) {
87 lyxerr << "MenuItem(): LyX command `"
88 << command << "' does not exist." << endl;
91 lyxerr[Debug::GUI] << "Optional item "
95 submenuname_ = command;
101 MenuItem::MenuItem(Kind kind, string const & label, int action, bool optional)
102 : kind_(kind), label_(label), action_(action), submenuname_(),
107 MenuItem::~MenuItem()
111 void MenuItem::submenu(Menu * menu)
113 submenu_.reset(menu);
117 string const MenuItem::label() const
119 return token(label_, '|', 0);
123 string const MenuItem::shortcut() const
125 return token(label_, '|', 1);
128 string const MenuItem::binding() const
130 if (kind_ != Command)
133 // Get the keys bound to this action, but keep only the
135 string bindings = toplevel_keymap->findbinding(action_);
137 if (!bindings.empty()) {
138 return bindings.substr(1, bindings.find(']') - 1);
144 Menu & Menu::add(MenuItem const & i, LyXView const * view)
152 case MenuItem::Command:
155 view->getLyXFunc().getStatus(i.action());
157 || (status.disabled() && i.optional()))
160 items_.back().status(status);
163 case MenuItem::Submenu:
166 bool disabled = true;
167 for (const_iterator cit = i.submenu()->begin();
168 cit != i.submenu()->end(); ++cit) {
169 if ((cit->kind() == MenuItem::Command
170 || cit->kind() == MenuItem::Submenu)
171 && !cit->status().disabled()) {
176 if (!disabled || !i.optional()) {
178 items_.back().status().disabled(disabled);
185 case MenuItem::Separator:
187 && items_.back().kind() != MenuItem::Separator)
198 Menu & Menu::read(LyXLex & lex)
221 struct keyword_item menutags[md_last - 1] = {
222 { "branches", md_branches },
223 { "documents", md_documents },
224 { "end", md_endmenu },
225 { "exportformats", md_exportformats },
226 { "floatinsert", md_floatinsert },
227 { "floatlistinsert", md_floatlistinsert },
228 { "importformats", md_importformats },
230 { "lastfiles", md_lastfiles },
231 { "optitem", md_optitem },
232 { "optsubmenu", md_optsubmenu },
233 { "pasterecent", md_pasterecent },
234 { "separator", md_separator },
235 { "submenu", md_submenu },
237 { "updateformats", md_updateformats },
238 { "viewformats", md_viewformats }
241 lex.pushTable(menutags, md_last - 1);
242 if (lyxerr.debugging(Debug::PARSER))
243 lex.printTable(lyxerr);
246 bool optional = false;
248 while (lex.isOK() && !quit) {
252 // fallback to md_item
255 string const name = _(lex.getString());
257 string const command = lex.getString();
258 add(MenuItem(MenuItem::Command, name,
265 add(MenuItem(MenuItem::Separator));
269 add(MenuItem(MenuItem::Lastfiles));
273 add(MenuItem(MenuItem::Documents));
277 add(MenuItem(MenuItem::Toc));
281 add(MenuItem(MenuItem::ViewFormats));
284 case md_updateformats:
285 add(MenuItem(MenuItem::UpdateFormats));
288 case md_exportformats:
289 add(MenuItem(MenuItem::ExportFormats));
292 case md_importformats:
293 add(MenuItem(MenuItem::ImportFormats));
296 case md_floatlistinsert:
297 add(MenuItem(MenuItem::FloatListInsert));
301 add(MenuItem(MenuItem::FloatInsert));
305 add(MenuItem(MenuItem::PasteRecent));
309 add(MenuItem(MenuItem::Branches));
314 // fallback to md_submenu
317 string const mlabel = _(lex.getString());
319 string const mname = lex.getString();
320 add(MenuItem(MenuItem::Submenu, mlabel, mname,
331 lex.printError("Menu::read: "
332 "Unknown menu tag: `$$Token'");
341 void Menu::checkShortcuts() const
343 // This is a quadratic algorithm, but we do not care because
344 // menus are short enough
345 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
346 string shortcut = it1->shortcut();
347 if (shortcut.empty())
349 if (!contains(it1->label(), shortcut))
350 lyxerr << "Menu warning: menu entry \""
352 << "\" does not contain shortcut `"
353 << shortcut << "'." << endl;
354 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
355 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
356 lyxerr << "Menu warning: menu entries "
357 << '"' << it1->fulllabel()
358 << "\" and \"" << it2->fulllabel()
359 << "\" share the same shortcut."
369 class compare_format {
371 bool operator()(Format const * p1, Format const * p2) {
376 string const limit_string_length(string const & str)
378 string::size_type const max_item_length = 45;
380 if (str.size() > max_item_length)
381 return str.substr(0, max_item_length - 3) + "...";
387 void expandLastfiles(Menu & tomenu, LyXView const * view)
390 LastFiles::const_iterator lfit = lastfiles->begin();
391 LastFiles::const_iterator end = lastfiles->end();
393 for (; lfit != end && ii < 10; ++lfit, ++ii) {
394 string const label = tostr(ii) + ". "
395 + MakeDisplayPath((*lfit), 30)
397 int const action = lyxaction.
398 getPseudoAction(LFUN_FILE_OPEN,
400 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
404 void expandDocuments(Menu & tomenu, LyXView const * view)
406 typedef vector<string> Strings;
407 Strings const names = bufferlist.getFileNames();
410 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
411 LFUN_NOACTION), view);
416 Strings::const_iterator docit = names.begin();
417 Strings::const_iterator end = names.end();
418 for (; docit != end; ++docit, ++ii) {
420 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
421 string label = MakeDisplayPath(*docit, 20);
423 label = tostr(ii) + ". " + label + '|' + tostr(ii);
424 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
429 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
431 if (!view->buffer() && kind != MenuItem::ImportFormats) {
432 tomenu.add(MenuItem(MenuItem::Command,
433 _("No Documents Open!"), LFUN_NOACTION),
438 typedef vector<Format const *> Formats;
443 case MenuItem::ImportFormats:
444 formats = Importer::GetImportableFormats();
445 action = LFUN_IMPORT;
447 case MenuItem::ViewFormats:
448 formats = Exporter::GetExportableFormats(*view->buffer(), true);
449 action = LFUN_PREVIEW;
451 case MenuItem::UpdateFormats:
452 formats = Exporter::GetExportableFormats(*view->buffer(), true);
453 action = LFUN_UPDATE;
456 formats = Exporter::GetExportableFormats(*view->buffer(), false);
457 action = LFUN_EXPORT;
459 sort(formats.begin(), formats.end(), compare_format());
461 Formats::const_iterator fit = formats.begin();
462 Formats::const_iterator end = formats.end();
463 for (; fit != end ; ++fit) {
466 string label = (*fit)->prettyname();
467 // we need to hide the default graphic export formats
468 // from the external menu, because we need them only
469 // for the internal lyx-view and external latex run
470 if (label == "EPS" || label == "XPM" || label == "PNG")
473 if (kind == MenuItem::ImportFormats) {
474 if ((*fit)->name() == "text")
475 label = _("ASCII text as lines");
476 else if ((*fit)->name() == "textparagraph")
477 label = _("ASCII text as paragraphs");
480 if (!(*fit)->shortcut().empty())
481 label += '|' + (*fit)->shortcut();
482 int const action2 = lyxaction.
483 getPseudoAction(action, (*fit)->name());
484 tomenu.add(MenuItem(MenuItem::Command, label, action2),
490 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
492 if (!view->buffer()) {
493 tomenu.add(MenuItem(MenuItem::Command,
494 _("No Documents Open!"), LFUN_NOACTION),
499 FloatList const & floats =
500 view->buffer()->params().getLyXTextClass().floats();
501 FloatList::const_iterator cit = floats.begin();
502 FloatList::const_iterator end = floats.end();
503 for (; cit != end; ++cit) {
504 int const action = lyxaction
505 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
506 tomenu.add(MenuItem(MenuItem::Command,
507 _(cit->second.listName()), action),
513 void expandFloatInsert(Menu & tomenu, LyXView const * view)
515 if (!view->buffer()) {
516 tomenu.add(MenuItem(MenuItem::Command,
517 _("No Documents Open!"), LFUN_NOACTION),
522 FloatList const & floats =
523 view->buffer()->params().getLyXTextClass().floats();
524 FloatList::const_iterator cit = floats.begin();
525 FloatList::const_iterator end = floats.end();
526 for (; cit != end; ++cit) {
529 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
531 string const label = _(cit->second.name());
532 tomenu.add(MenuItem(MenuItem::Command, label, action),
538 Menu::size_type const max_number_of_items = 25;
540 void expandToc2(Menu & tomenu,
541 lyx::toc::Toc const & toc_list,
542 lyx::toc::Toc::size_type from,
543 lyx::toc::Toc::size_type to, int depth)
545 int shortcut_count = 0;
546 if (to - from <= max_number_of_items) {
547 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
548 int const action = toc_list[i].action();
549 string label(4 * max(0, toc_list[i].depth - depth),' ');
550 label += limit_string_length(toc_list[i].str);
551 if (toc_list[i].depth == depth
552 && ++shortcut_count <= 9) {
553 label += '|' + tostr(shortcut_count);
555 tomenu.add(MenuItem(MenuItem::Command, label, action));
558 lyx::toc::Toc::size_type pos = from;
560 lyx::toc::Toc::size_type new_pos = pos + 1;
561 while (new_pos < to &&
562 toc_list[new_pos].depth > depth)
565 int const action = toc_list[pos].action();
566 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
567 label += limit_string_length(toc_list[pos].str);
568 if (toc_list[pos].depth == depth &&
569 ++shortcut_count <= 9)
570 label += '|' + tostr(shortcut_count);
572 if (new_pos == pos + 1) {
573 tomenu.add(MenuItem(MenuItem::Command,
576 MenuItem item(MenuItem::Submenu, label);
577 item.submenu(new Menu);
578 expandToc2(*item.submenu(),
579 toc_list, pos, new_pos, depth + 1);
588 void expandToc(Menu & tomenu, LyXView const * view)
590 // To make things very cleanly, we would have to pass view to
591 // all MenuItem constructors and to expandToc2. However, we
592 // know that all the entries in a TOC will be have status_ ==
593 // OK, so we avoid this unnecessary overhead (JMarc)
595 if (!view->buffer()) {
596 tomenu.add(MenuItem(MenuItem::Command,
597 _("No Documents Open!"), LFUN_NOACTION),
602 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
603 lyx::toc::TocList::const_iterator cit = toc_list.begin();
604 lyx::toc::TocList::const_iterator end = toc_list.end();
605 for (; cit != end; ++cit) {
607 if (cit->first == "TOC")
610 // All the rest is for floats
611 Menu * menu = new Menu;
612 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
613 lyx::toc::Toc::const_iterator eend = cit->second.end();
614 for (; ccit != eend; ++ccit) {
615 string const label = limit_string_length(ccit->str);
616 menu->add(MenuItem(MenuItem::Command,
617 label, ccit->action()));
619 string const & floatName = cit->first;
620 // Is the _(...) really needed here? (Lgb)
621 MenuItem item(MenuItem::Submenu, _(floatName));
627 cit = toc_list.find("TOC");
629 tomenu.add(MenuItem(MenuItem::Command,
630 _("No Table of contents")),
633 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
638 void expandPasteRecent(Menu & tomenu, LyXView const * view)
640 if (!view || !view->buffer())
643 vector<string> const selL =
644 CutAndPaste::availableSelections(*view->buffer());
646 vector<string>::const_iterator cit = selL.begin();
647 vector<string>::const_iterator end = selL.end();
649 for (unsigned int index = 0; cit != end; ++cit, ++index) {
650 int const action = lyxaction.getPseudoAction(LFUN_PASTE,
652 tomenu.add(MenuItem(MenuItem::Command, *cit, action));
657 void expandBranches(Menu & tomenu, LyXView const * view)
659 if (!view || !view->buffer())
662 BufferParams const & params = view->buffer()->params();
664 std::list<Branch>::const_iterator cit = params.branchlist().begin();
665 std::list<Branch>::const_iterator end = params.branchlist().end();
667 for (int ii = 1; cit != end; ++cit, ++ii) {
668 string label = cit->getBranch();
669 int const action = lyxaction.
670 getPseudoAction(LFUN_INSERT_BRANCH,
673 label = tostr(ii) + ". " + label + "|" + tostr(ii);
674 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
682 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
683 LyXView const * view) const
685 for (Menu::const_iterator cit = frommenu.begin();
686 cit != frommenu.end() ; ++cit) {
687 switch (cit->kind()) {
688 case MenuItem::Lastfiles:
689 expandLastfiles(tomenu, view);
692 case MenuItem::Documents:
693 expandDocuments(tomenu, view);
696 case MenuItem::ImportFormats:
697 case MenuItem::ViewFormats:
698 case MenuItem::UpdateFormats:
699 case MenuItem::ExportFormats:
700 expandFormats(cit->kind(), tomenu, view);
703 case MenuItem::FloatListInsert:
704 expandFloatListInsert(tomenu, view);
707 case MenuItem::FloatInsert:
708 expandFloatInsert(tomenu, view);
711 case MenuItem::PasteRecent:
712 expandPasteRecent(tomenu, view);
715 case MenuItem::Branches:
716 expandBranches(tomenu, view);
720 expandToc(tomenu, view);
723 case MenuItem::Submenu: {
725 item.submenu(new Menu(cit->submenuname()));
726 expand(getMenu(cit->submenuname()),
727 *item.submenu(), view);
728 tomenu.add(item, view);
733 tomenu.add(*cit, view);
737 // we do not want the menu to end with a separator
739 && tomenu.items_.back().kind() == MenuItem::Separator)
740 tomenu.items_.pop_back();
742 // Check whether the shortcuts are unique
743 tomenu.checkShortcuts();
747 bool Menu::hasSubmenu(string const & name) const
749 return find_if(begin(), end(),
750 lyx::compare_memfun(&MenuItem::submenuname,
755 void MenuBackend::read(LyXLex & lex)
764 struct keyword_item menutags[md_last - 1] = {
765 { "end", md_endmenuset },
767 { "menubar", md_menubar }
771 if (compare_ascii_no_case(lex.getString(), "menuset")) {
772 lyxerr << "Menubackend::read: ERROR wrong token:`"
773 << lex.getString() << '\'' << endl;
776 lex.pushTable(menutags, md_last - 1);
777 if (lyxerr.debugging(Debug::PARSER))
778 lex.printTable(lyxerr);
782 while (lex.isOK() && !quit) {
789 string const name = lex.getString();
791 getMenu(name).read(lex);
803 lex.printError("menubackend::read: "
804 "Unknown menu tag: `$$Token'");
812 void MenuBackend::add(Menu const & menu)
814 menulist_.push_back(menu);
818 bool MenuBackend::hasMenu(string const & name) const
820 return find_if(begin(), end(),
821 lyx::compare_memfun(&Menu::name, name)) != end();
825 Menu const & MenuBackend::getMenu(string const & name) const
827 const_iterator cit = find_if(begin(), end(),
828 lyx::compare_memfun(&Menu::name, name));
830 lyxerr << "No submenu named " << name << endl;
831 BOOST_ASSERT(cit != end());
836 Menu & MenuBackend::getMenu(string const & name)
838 MenuList::iterator it =
839 find_if(menulist_.begin(), menulist_.end(),
840 lyx::compare_memfun(&Menu::name, name));
841 BOOST_ASSERT(it != menulist_.end());
846 Menu const & MenuBackend::getMenubar() const