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"
33 #include "FloatList.h"
35 #include "CutAndPaste.h"
36 #include "frontends/LyXView.h"
37 #include "support/LAssert.h"
38 #include "support/filetools.h"
39 #include "support/lyxfunctional.h"
40 #include "support/lstrings.h"
41 #include "support/tostr.h"
45 extern BufferList bufferlist;
46 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
48 using namespace lyx::support;
57 // This is the global menu definition
58 MenuBackend menubackend;
61 MenuItem::MenuItem(Kind kind, string const & label,
62 string const & command, bool optional)
63 : kind_(kind), label_(label), optional_(optional)
80 action_ = lyxaction.LookupFunc(command);
82 if (action_ == LFUN_UNKNOWN_ACTION) {
83 lyxerr << "MenuItem(): LyX command `"
84 << command << "' does not exist." << endl;
87 lyxerr[Debug::GUI] << "Optional item "
91 submenuname_ = command;
97 MenuItem::MenuItem(Kind kind, string const & label, int action, bool optional)
98 : kind_(kind), label_(label), action_(action), submenuname_(),
103 MenuItem::~MenuItem()
107 void MenuItem::submenu(Menu * menu)
109 submenu_.reset(menu);
113 string const MenuItem::label() const
115 return token(label_, '|', 0);
119 string const MenuItem::shortcut() const
121 return token(label_, '|', 1);
124 string const MenuItem::binding() const
126 if (kind_ != Command)
129 // Get the keys bound to this action, but keep only the
131 string bindings = toplevel_keymap->findbinding(action_);
133 if (!bindings.empty()) {
134 return bindings.substr(1, bindings.find(']') - 1);
140 Menu & Menu::add(MenuItem const & i, LyXView const * view)
148 case MenuItem::Command:
151 view->getLyXFunc().getStatus(i.action());
153 || (status.disabled() && i.optional()))
156 items_.back().status(status);
159 case MenuItem::Submenu:
162 bool disabled = true;
163 for (const_iterator cit = i.submenu()->begin();
164 cit != i.submenu()->end(); ++cit) {
165 if ((cit->kind() == MenuItem::Command
166 || cit->kind() == MenuItem::Submenu)
167 && !cit->status().disabled()) {
172 if (!disabled || !i.optional()) {
174 items_.back().status().disabled(disabled);
181 case MenuItem::Separator:
183 && items_.back().kind() != MenuItem::Separator)
194 Menu & Menu::read(LyXLex & lex)
217 struct keyword_item menutags[md_last - 1] = {
218 { "branches", md_branches },
219 { "documents", md_documents },
220 { "end", md_endmenu },
221 { "exportformats", md_exportformats },
222 { "floatinsert", md_floatinsert },
223 { "floatlistinsert", md_floatlistinsert },
224 { "importformats", md_importformats },
226 { "lastfiles", md_lastfiles },
227 { "optitem", md_optitem },
228 { "optsubmenu", md_optsubmenu },
229 { "pasterecent", md_pasterecent },
230 { "separator", md_separator },
231 { "submenu", md_submenu },
233 { "updateformats", md_updateformats },
234 { "viewformats", md_viewformats }
237 lex.pushTable(menutags, md_last - 1);
238 if (lyxerr.debugging(Debug::PARSER))
239 lex.printTable(lyxerr);
242 bool optional = false;
244 while (lex.isOK() && !quit) {
248 // fallback to md_item
251 string const name = _(lex.getString());
253 string const command = lex.getString();
254 add(MenuItem(MenuItem::Command, name,
261 add(MenuItem(MenuItem::Separator));
265 add(MenuItem(MenuItem::Lastfiles));
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)
386 LastFiles::const_iterator lfit = lastfiles->begin();
387 LastFiles::const_iterator end = lastfiles->end();
389 for (; lfit != end && ii < 10; ++lfit, ++ii) {
390 string const label = tostr(ii) + ". "
391 + MakeDisplayPath((*lfit), 30)
393 int const action = lyxaction.
394 getPseudoAction(LFUN_FILE_OPEN,
396 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
400 void expandDocuments(Menu & tomenu, LyXView const * view)
402 typedef vector<string> Strings;
403 Strings const names = bufferlist.getFileNames();
406 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
407 LFUN_NOACTION), view);
412 Strings::const_iterator docit = names.begin();
413 Strings::const_iterator end = names.end();
414 for (; docit != end; ++docit, ++ii) {
416 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
417 string label = MakeDisplayPath(*docit, 20);
419 label = tostr(ii) + ". " + label + '|' + tostr(ii);
420 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
425 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
427 if (!view->buffer() && kind != MenuItem::ImportFormats) {
428 tomenu.add(MenuItem(MenuItem::Command,
429 _("No Documents Open!"), LFUN_NOACTION),
434 typedef vector<Format const *> Formats;
439 case MenuItem::ImportFormats:
440 formats = Importer::GetImportableFormats();
441 action = LFUN_IMPORT;
443 case MenuItem::ViewFormats:
444 formats = Exporter::GetExportableFormats(*view->buffer(), true);
445 action = LFUN_PREVIEW;
447 case MenuItem::UpdateFormats:
448 formats = Exporter::GetExportableFormats(*view->buffer(), true);
449 action = LFUN_UPDATE;
452 formats = Exporter::GetExportableFormats(*view->buffer(), false);
453 action = LFUN_EXPORT;
455 sort(formats.begin(), formats.end(), compare_format());
457 Formats::const_iterator fit = formats.begin();
458 Formats::const_iterator end = formats.end();
459 for (; fit != end ; ++fit) {
462 string label = (*fit)->prettyname();
463 // we need to hide the default graphic export formats
464 // from the external menu, because we need them only
465 // for the internal lyx-view and external latex run
466 if (label == "EPS" || label == "XPM" || label == "PNG")
469 if (kind == MenuItem::ImportFormats) {
470 if ((*fit)->name() == "text")
471 label = _("ASCII text as lines");
472 else if ((*fit)->name() == "textparagraph")
473 label = _("ASCII text as paragraphs");
476 if (!(*fit)->shortcut().empty())
477 label += '|' + (*fit)->shortcut();
478 int const action2 = lyxaction.
479 getPseudoAction(action, (*fit)->name());
480 tomenu.add(MenuItem(MenuItem::Command, label, action2),
486 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
488 if (!view->buffer()) {
489 tomenu.add(MenuItem(MenuItem::Command,
490 _("No Documents Open!"), LFUN_NOACTION),
495 FloatList const & floats =
496 view->buffer()->params.getLyXTextClass().floats();
497 FloatList::const_iterator cit = floats.begin();
498 FloatList::const_iterator end = floats.end();
499 for (; cit != end; ++cit) {
500 int const action = lyxaction
501 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
502 tomenu.add(MenuItem(MenuItem::Command,
503 _(cit->second.listName()), action),
509 void expandFloatInsert(Menu & tomenu, LyXView const * view)
511 if (!view->buffer()) {
512 tomenu.add(MenuItem(MenuItem::Command,
513 _("No Documents Open!"), 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) {
525 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
527 string const label = _(cit->second.name());
528 tomenu.add(MenuItem(MenuItem::Command, label, action),
534 Menu::size_type const max_number_of_items = 25;
536 void expandToc2(Menu & tomenu,
537 lyx::toc::Toc const & toc_list,
538 lyx::toc::Toc::size_type from,
539 lyx::toc::Toc::size_type to, int depth)
541 int shortcut_count = 0;
542 if (to - from <= max_number_of_items) {
543 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
544 int const action = toc_list[i].action();
545 string label(4 * max(0, toc_list[i].depth - depth),' ');
546 label += limit_string_length(toc_list[i].str);
547 if (toc_list[i].depth == depth
548 && ++shortcut_count <= 9) {
549 label += '|' + tostr(shortcut_count);
551 tomenu.add(MenuItem(MenuItem::Command, label, action));
554 lyx::toc::Toc::size_type pos = from;
556 lyx::toc::Toc::size_type new_pos = pos + 1;
557 while (new_pos < to &&
558 toc_list[new_pos].depth > depth)
561 int const action = toc_list[pos].action();
562 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
563 label += limit_string_length(toc_list[pos].str);
564 if (toc_list[pos].depth == depth &&
565 ++shortcut_count <= 9)
566 label += '|' + tostr(shortcut_count);
568 if (new_pos == pos + 1) {
569 tomenu.add(MenuItem(MenuItem::Command,
572 MenuItem item(MenuItem::Submenu, label);
573 item.submenu(new Menu);
574 expandToc2(*item.submenu(),
575 toc_list, pos, new_pos, depth + 1);
584 void expandToc(Menu & tomenu, LyXView const * view)
586 // To make things very cleanly, we would have to pass view to
587 // all MenuItem constructors and to expandToc2. However, we
588 // know that all the entries in a TOC will be have status_ ==
589 // OK, so we avoid this unnecessary overhead (JMarc)
591 if (!view->buffer()) {
592 tomenu.add(MenuItem(MenuItem::Command,
593 _("No Documents Open!"), LFUN_NOACTION),
598 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
599 lyx::toc::TocList::const_iterator cit = toc_list.begin();
600 lyx::toc::TocList::const_iterator end = toc_list.end();
601 for (; cit != end; ++cit) {
603 if (cit->first == "TOC")
606 // All the rest is for floats
607 Menu * menu = new Menu;
608 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
609 lyx::toc::Toc::const_iterator eend = cit->second.end();
610 for (; ccit != eend; ++ccit) {
611 string const label = limit_string_length(ccit->str);
612 menu->add(MenuItem(MenuItem::Command,
613 label, ccit->action()));
615 string const & floatName = cit->first;
616 // Is the _(...) really needed here? (Lgb)
617 MenuItem item(MenuItem::Submenu, _(floatName));
623 cit = toc_list.find("TOC");
625 tomenu.add(MenuItem(MenuItem::Command,
626 _("No Table of contents")),
629 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
634 void expandPasteRecent(Menu & tomenu, LyXView const * view)
636 if (!view || !view->buffer())
639 vector<string> const selL =
640 CutAndPaste::availableSelections(*view->buffer());
642 vector<string>::const_iterator cit = selL.begin();
643 vector<string>::const_iterator end = selL.end();
645 for (unsigned int index = 0; cit != end; ++cit, ++index) {
646 int const action = lyxaction.getPseudoAction(LFUN_PASTE,
648 tomenu.add(MenuItem(MenuItem::Command, *cit, action));
653 void expandBranches(Menu & tomenu, LyXView const * view)
655 if (!view || !view->buffer())
658 BufferParams const & params = view->buffer()->params;
660 std::list<Branch>::const_iterator cit = params.branchlist.begin();
661 std::list<Branch>::const_iterator end = params.branchlist.end();
663 for (int ii = 1; cit != end; ++cit, ++ii) {
664 string label = cit->getBranch();
665 int const action = lyxaction.
666 getPseudoAction(LFUN_INSERT_BRANCH,
669 label = tostr(ii) + ". " + label + "|" + tostr(ii);
670 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
678 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
679 LyXView const * view) const
681 for (Menu::const_iterator cit = frommenu.begin();
682 cit != frommenu.end() ; ++cit) {
683 switch (cit->kind()) {
684 case MenuItem::Lastfiles:
685 expandLastfiles(tomenu, view);
688 case MenuItem::Documents:
689 expandDocuments(tomenu, view);
692 case MenuItem::ImportFormats:
693 case MenuItem::ViewFormats:
694 case MenuItem::UpdateFormats:
695 case MenuItem::ExportFormats:
696 expandFormats(cit->kind(), tomenu, view);
699 case MenuItem::FloatListInsert:
700 expandFloatListInsert(tomenu, view);
703 case MenuItem::FloatInsert:
704 expandFloatInsert(tomenu, view);
707 case MenuItem::PasteRecent:
708 expandPasteRecent(tomenu, view);
711 case MenuItem::Branches:
712 expandBranches(tomenu, view);
716 expandToc(tomenu, view);
719 case MenuItem::Submenu: {
721 item.submenu(new Menu(cit->submenuname()));
722 expand(getMenu(cit->submenuname()),
723 *item.submenu(), view);
724 tomenu.add(item, view);
729 tomenu.add(*cit, view);
733 // we do not want the menu to end with a separator
735 && tomenu.items_.back().kind() == MenuItem::Separator)
736 tomenu.items_.pop_back();
738 // Check whether the shortcuts are unique
739 tomenu.checkShortcuts();
743 bool Menu::hasSubmenu(string const & name) const
745 return find_if(begin(), end(),
746 lyx::compare_memfun(&MenuItem::submenuname,
751 void MenuBackend::read(LyXLex & lex)
760 struct keyword_item menutags[md_last - 1] = {
761 { "end", md_endmenuset },
763 { "menubar", md_menubar }
767 if (compare_ascii_no_case(lex.getString(), "menuset")) {
768 lyxerr << "Menubackend::read: ERROR wrong token:`"
769 << lex.getString() << '\'' << endl;
772 lex.pushTable(menutags, md_last - 1);
773 if (lyxerr.debugging(Debug::PARSER))
774 lex.printTable(lyxerr);
778 while (lex.isOK() && !quit) {
785 string const name = lex.getString();
787 getMenu(name).read(lex);
799 lex.printError("menubackend::read: "
800 "Unknown menu tag: `$$Token'");
808 void MenuBackend::add(Menu const & menu)
810 menulist_.push_back(menu);
814 bool MenuBackend::hasMenu(string const & name) const
816 return find_if(begin(), end(),
817 lyx::compare_memfun(&Menu::name, name)) != end();
821 Menu const & MenuBackend::getMenu(string const & name) const
823 const_iterator cit = find_if(begin(), end(),
824 lyx::compare_memfun(&Menu::name, name));
826 lyxerr << "No submenu named " << name << endl;
827 Assert(cit != end());
832 Menu & MenuBackend::getMenu(string const & name)
834 MenuList::iterator it =
835 find_if(menulist_.begin(), menulist_.end(),
836 lyx::compare_memfun(&Menu::name, name));
837 Assert(it != menulist_.end());
842 Menu const & MenuBackend::getMenubar() const