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, string const & label,
70 string const & command, bool optional)
71 : kind_(kind), label_(label), optional_(optional)
88 action_ = lyxaction.LookupFunc(command);
90 if (action_ == LFUN_UNKNOWN_ACTION) {
91 lyxerr << "MenuItem(): LyX command `"
92 << command << "' does not exist." << endl;
95 lyxerr[Debug::GUI] << "Optional item "
99 submenuname_ = command;
105 MenuItem::MenuItem(Kind kind, string const & label, int action, bool optional)
106 : kind_(kind), label_(label), action_(action), submenuname_(),
111 MenuItem::~MenuItem()
115 void MenuItem::submenu(Menu * menu)
117 submenu_.reset(menu);
121 string const MenuItem::label() const
123 return token(label_, '|', 0);
127 string const MenuItem::shortcut() const
129 return token(label_, '|', 1);
132 string const MenuItem::binding() const
134 if (kind_ != Command)
137 // Get the keys bound to this action, but keep only the
139 string bindings = toplevel_keymap->findbinding(action_);
141 if (!bindings.empty()) {
142 return bindings.substr(1, bindings.find(']') - 1);
148 Menu & Menu::add(MenuItem const & i, LyXView const * view)
156 case MenuItem::Command:
159 view->getLyXFunc().getStatus(i.action());
161 || (status.disabled() && i.optional()))
164 items_.back().status(status);
167 case MenuItem::Submenu:
170 bool disabled = true;
171 for (const_iterator cit = i.submenu()->begin();
172 cit != i.submenu()->end(); ++cit) {
173 if ((cit->kind() == MenuItem::Command
174 || cit->kind() == MenuItem::Submenu)
175 && !cit->status().disabled()) {
180 if (!disabled || !i.optional()) {
182 items_.back().status().disabled(disabled);
189 case MenuItem::Separator:
191 && items_.back().kind() != MenuItem::Separator)
202 Menu & Menu::read(LyXLex & lex)
225 struct keyword_item menutags[md_last - 1] = {
226 { "branches", md_branches },
227 { "documents", md_documents },
228 { "end", md_endmenu },
229 { "exportformats", md_exportformats },
230 { "floatinsert", md_floatinsert },
231 { "floatlistinsert", md_floatlistinsert },
232 { "importformats", md_importformats },
234 { "lastfiles", md_lastfiles },
235 { "optitem", md_optitem },
236 { "optsubmenu", md_optsubmenu },
237 { "pasterecent", md_pasterecent },
238 { "separator", md_separator },
239 { "submenu", md_submenu },
241 { "updateformats", md_updateformats },
242 { "viewformats", md_viewformats }
245 lex.pushTable(menutags, md_last - 1);
246 if (lyxerr.debugging(Debug::PARSER))
247 lex.printTable(lyxerr);
250 bool optional = false;
252 while (lex.isOK() && !quit) {
256 // fallback to md_item
259 string const name = _(lex.getString());
261 string const command = lex.getString();
262 add(MenuItem(MenuItem::Command, name,
269 add(MenuItem(MenuItem::Separator));
273 add(MenuItem(MenuItem::Lastfiles));
277 add(MenuItem(MenuItem::Documents));
281 add(MenuItem(MenuItem::Toc));
285 add(MenuItem(MenuItem::ViewFormats));
288 case md_updateformats:
289 add(MenuItem(MenuItem::UpdateFormats));
292 case md_exportformats:
293 add(MenuItem(MenuItem::ExportFormats));
296 case md_importformats:
297 add(MenuItem(MenuItem::ImportFormats));
300 case md_floatlistinsert:
301 add(MenuItem(MenuItem::FloatListInsert));
305 add(MenuItem(MenuItem::FloatInsert));
309 add(MenuItem(MenuItem::PasteRecent));
313 add(MenuItem(MenuItem::Branches));
318 // fallback to md_submenu
321 string const mlabel = _(lex.getString());
323 string const mname = lex.getString();
324 add(MenuItem(MenuItem::Submenu, mlabel, mname,
335 lex.printError("Menu::read: "
336 "Unknown menu tag: `$$Token'");
345 void Menu::checkShortcuts() const
347 // This is a quadratic algorithm, but we do not care because
348 // menus are short enough
349 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
350 string shortcut = it1->shortcut();
351 if (shortcut.empty())
353 if (!contains(it1->label(), shortcut))
354 lyxerr << "Menu warning: menu entry \""
356 << "\" does not contain shortcut `"
357 << shortcut << "'." << endl;
358 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
359 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
360 lyxerr << "Menu warning: menu entries "
361 << '"' << it1->fulllabel()
362 << "\" and \"" << it2->fulllabel()
363 << "\" share the same shortcut."
373 class compare_format {
375 bool operator()(Format const * p1, Format const * p2) {
380 string const limit_string_length(string const & str)
382 string::size_type const max_item_length = 45;
384 if (str.size() > max_item_length)
385 return str.substr(0, max_item_length - 3) + "...";
391 void expandLastfiles(Menu & tomenu, LyXView const * view)
394 LastFiles::const_iterator lfit = lastfiles->begin();
395 LastFiles::const_iterator end = lastfiles->end();
397 for (; lfit != end && ii < 10; ++lfit, ++ii) {
398 string const label = tostr(ii) + ". "
399 + MakeDisplayPath((*lfit), 30)
401 int const action = lyxaction.
402 getPseudoAction(LFUN_FILE_OPEN,
404 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
408 void expandDocuments(Menu & tomenu, LyXView const * view)
410 typedef vector<string> Strings;
411 Strings const names = bufferlist.getFileNames();
414 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
415 LFUN_NOACTION), view);
420 Strings::const_iterator docit = names.begin();
421 Strings::const_iterator end = names.end();
422 for (; docit != end; ++docit, ++ii) {
424 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
425 string label = MakeDisplayPath(*docit, 20);
427 label = tostr(ii) + ". " + label + '|' + tostr(ii);
428 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
433 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
435 if (!view->buffer() && kind != MenuItem::ImportFormats) {
436 tomenu.add(MenuItem(MenuItem::Command,
437 _("No Documents Open!"), LFUN_NOACTION),
442 typedef vector<Format const *> Formats;
447 case MenuItem::ImportFormats:
448 formats = Importer::GetImportableFormats();
449 action = LFUN_IMPORT;
451 case MenuItem::ViewFormats:
452 formats = Exporter::GetExportableFormats(*view->buffer(), true);
453 action = LFUN_PREVIEW;
455 case MenuItem::UpdateFormats:
456 formats = Exporter::GetExportableFormats(*view->buffer(), true);
457 action = LFUN_UPDATE;
460 formats = Exporter::GetExportableFormats(*view->buffer(), false);
461 action = LFUN_EXPORT;
463 sort(formats.begin(), formats.end(), compare_format());
465 Formats::const_iterator fit = formats.begin();
466 Formats::const_iterator end = formats.end();
467 for (; fit != end ; ++fit) {
470 string label = (*fit)->prettyname();
471 // we need to hide the default graphic export formats
472 // from the external menu, because we need them only
473 // for the internal lyx-view and external latex run
474 if (label == "EPS" || label == "XPM" || label == "PNG")
477 if (kind == MenuItem::ImportFormats) {
478 if ((*fit)->name() == "text")
479 label = _("ASCII text as lines");
480 else if ((*fit)->name() == "textparagraph")
481 label = _("ASCII text as paragraphs");
484 if (!(*fit)->shortcut().empty())
485 label += '|' + (*fit)->shortcut();
486 int const action2 = lyxaction.
487 getPseudoAction(action, (*fit)->name());
488 tomenu.add(MenuItem(MenuItem::Command, label, action2),
494 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
496 if (!view->buffer()) {
497 tomenu.add(MenuItem(MenuItem::Command,
498 _("No Documents Open!"), LFUN_NOACTION),
503 FloatList const & floats =
504 view->buffer()->params().getLyXTextClass().floats();
505 FloatList::const_iterator cit = floats.begin();
506 FloatList::const_iterator end = floats.end();
507 for (; cit != end; ++cit) {
508 int const action = lyxaction
509 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
510 tomenu.add(MenuItem(MenuItem::Command,
511 _(cit->second.listName()), action),
517 void expandFloatInsert(Menu & tomenu, LyXView const * view)
519 if (!view->buffer()) {
520 tomenu.add(MenuItem(MenuItem::Command,
521 _("No Documents Open!"), LFUN_NOACTION),
526 FloatList const & floats =
527 view->buffer()->params().getLyXTextClass().floats();
528 FloatList::const_iterator cit = floats.begin();
529 FloatList::const_iterator end = floats.end();
530 for (; cit != end; ++cit) {
533 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
535 string const label = _(cit->second.name());
536 tomenu.add(MenuItem(MenuItem::Command, label, action),
542 Menu::size_type const max_number_of_items = 25;
544 void expandToc2(Menu & tomenu,
545 lyx::toc::Toc const & toc_list,
546 lyx::toc::Toc::size_type from,
547 lyx::toc::Toc::size_type to, int depth)
549 int shortcut_count = 0;
550 if (to - from <= max_number_of_items) {
551 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
552 int const action = toc_list[i].action();
553 string label(4 * max(0, toc_list[i].depth - depth),' ');
554 label += limit_string_length(toc_list[i].str);
555 if (toc_list[i].depth == depth
556 && ++shortcut_count <= 9) {
557 label += '|' + tostr(shortcut_count);
559 tomenu.add(MenuItem(MenuItem::Command, label, action));
562 lyx::toc::Toc::size_type pos = from;
564 lyx::toc::Toc::size_type new_pos = pos + 1;
565 while (new_pos < to &&
566 toc_list[new_pos].depth > depth)
569 int const action = toc_list[pos].action();
570 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
571 label += limit_string_length(toc_list[pos].str);
572 if (toc_list[pos].depth == depth &&
573 ++shortcut_count <= 9)
574 label += '|' + tostr(shortcut_count);
576 if (new_pos == pos + 1) {
577 tomenu.add(MenuItem(MenuItem::Command,
580 MenuItem item(MenuItem::Submenu, label);
581 item.submenu(new Menu);
582 expandToc2(*item.submenu(),
583 toc_list, pos, new_pos, depth + 1);
592 void expandToc(Menu & tomenu, LyXView const * view)
594 // To make things very cleanly, we would have to pass view to
595 // all MenuItem constructors and to expandToc2. However, we
596 // know that all the entries in a TOC will be have status_ ==
597 // OK, so we avoid this unnecessary overhead (JMarc)
599 if (!view->buffer()) {
600 tomenu.add(MenuItem(MenuItem::Command,
601 _("No Documents Open!"), LFUN_NOACTION),
606 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
607 lyx::toc::TocList::const_iterator cit = toc_list.begin();
608 lyx::toc::TocList::const_iterator end = toc_list.end();
609 for (; cit != end; ++cit) {
611 if (cit->first == "TOC")
614 // All the rest is for floats
615 Menu * menu = new Menu;
616 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
617 lyx::toc::Toc::const_iterator eend = cit->second.end();
618 for (; ccit != eend; ++ccit) {
619 string const label = limit_string_length(ccit->str);
620 menu->add(MenuItem(MenuItem::Command,
621 label, ccit->action()));
623 string const & floatName = cit->first;
624 // Is the _(...) really needed here? (Lgb)
625 MenuItem item(MenuItem::Submenu, _(floatName));
631 cit = toc_list.find("TOC");
633 tomenu.add(MenuItem(MenuItem::Command,
634 _("No Table of contents")),
637 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
642 void expandPasteRecent(Menu & tomenu, LyXView const * view)
644 if (!view || !view->buffer())
647 vector<string> const selL =
648 CutAndPaste::availableSelections(*view->buffer());
650 vector<string>::const_iterator cit = selL.begin();
651 vector<string>::const_iterator end = selL.end();
653 for (unsigned int index = 0; cit != end; ++cit, ++index) {
654 int const action = lyxaction.getPseudoAction(LFUN_PASTE,
656 tomenu.add(MenuItem(MenuItem::Command, *cit, action));
661 void expandBranches(Menu & tomenu, LyXView const * view)
663 if (!view || !view->buffer())
666 BufferParams const & params = view->buffer()->params();
668 std::list<Branch>::const_iterator cit = params.branchlist().begin();
669 std::list<Branch>::const_iterator end = params.branchlist().end();
671 for (int ii = 1; cit != end; ++cit, ++ii) {
672 string label = cit->getBranch();
673 int const action = lyxaction.
674 getPseudoAction(LFUN_INSERT_BRANCH,
677 label = tostr(ii) + ". " + label + "|" + tostr(ii);
678 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
686 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
687 LyXView const * view) const
689 for (Menu::const_iterator cit = frommenu.begin();
690 cit != frommenu.end() ; ++cit) {
691 switch (cit->kind()) {
692 case MenuItem::Lastfiles:
693 expandLastfiles(tomenu, view);
696 case MenuItem::Documents:
697 expandDocuments(tomenu, view);
700 case MenuItem::ImportFormats:
701 case MenuItem::ViewFormats:
702 case MenuItem::UpdateFormats:
703 case MenuItem::ExportFormats:
704 expandFormats(cit->kind(), tomenu, view);
707 case MenuItem::FloatListInsert:
708 expandFloatListInsert(tomenu, view);
711 case MenuItem::FloatInsert:
712 expandFloatInsert(tomenu, view);
715 case MenuItem::PasteRecent:
716 expandPasteRecent(tomenu, view);
719 case MenuItem::Branches:
720 expandBranches(tomenu, view);
724 expandToc(tomenu, view);
727 case MenuItem::Submenu: {
729 item.submenu(new Menu(cit->submenuname()));
730 expand(getMenu(cit->submenuname()),
731 *item.submenu(), view);
732 tomenu.add(item, view);
737 tomenu.add(*cit, view);
741 // we do not want the menu to end with a separator
743 && tomenu.items_.back().kind() == MenuItem::Separator)
744 tomenu.items_.pop_back();
746 // Check whether the shortcuts are unique
747 tomenu.checkShortcuts();
751 bool Menu::hasSubmenu(string const & name) const
753 return find_if(begin(), end(),
754 lyx::compare_memfun(&MenuItem::submenuname,
759 void MenuBackend::read(LyXLex & lex)
768 struct keyword_item menutags[md_last - 1] = {
769 { "end", md_endmenuset },
771 { "menubar", md_menubar }
775 if (compare_ascii_no_case(lex.getString(), "menuset")) {
776 lyxerr << "Menubackend::read: ERROR wrong token:`"
777 << lex.getString() << '\'' << endl;
780 lex.pushTable(menutags, md_last - 1);
781 if (lyxerr.debugging(Debug::PARSER))
782 lex.printTable(lyxerr);
786 while (lex.isOK() && !quit) {
793 string const name = lex.getString();
795 getMenu(name).read(lex);
807 lex.printError("menubackend::read: "
808 "Unknown menu tag: `$$Token'");
816 void MenuBackend::add(Menu const & menu)
818 menulist_.push_back(menu);
822 bool MenuBackend::hasMenu(string const & name) const
824 return find_if(begin(), end(),
825 lyx::compare_memfun(&Menu::name, name)) != end();
829 Menu const & MenuBackend::getMenu(string const & name) const
831 const_iterator cit = find_if(begin(), end(),
832 lyx::compare_memfun(&Menu::name, name));
834 lyxerr << "No submenu named " << name << endl;
835 BOOST_ASSERT(cit != end());
840 Menu & MenuBackend::getMenu(string const & name)
842 MenuList::iterator it =
843 find_if(menulist_.begin(), menulist_.end(),
844 lyx::compare_memfun(&Menu::name, name));
845 BOOST_ASSERT(it != menulist_.end());
850 Menu const & MenuBackend::getMenubar() const