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"
19 #include "bufferparams.h"
21 #include "LyXAction.h"
25 #include "lastfiles.h"
27 #include "lyx_main.h" // for lastfiles
28 #include "bufferlist.h"
34 #include "FloatList.h"
36 #include "CutAndPaste.h"
37 #include "frontends/LyXView.h"
38 #include "support/LAssert.h"
39 #include "support/filetools.h"
40 #include "support/lyxfunctional.h"
41 #include "support/lstrings.h"
42 #include "support/tostr.h"
46 extern BufferList bufferlist;
47 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
49 using namespace lyx::support;
58 // This is the global menu definition
59 MenuBackend menubackend;
62 MenuItem::MenuItem(Kind kind, string const & label,
63 string const & command, bool optional)
64 : kind_(kind), label_(label), optional_(optional)
81 action_ = lyxaction.LookupFunc(command);
83 if (action_ == LFUN_UNKNOWN_ACTION) {
84 lyxerr << "MenuItem(): LyX command `"
85 << command << "' does not exist." << endl;
88 lyxerr[Debug::GUI] << "Optional item "
92 submenuname_ = command;
98 MenuItem::MenuItem(Kind kind, string const & label, int action, bool optional)
99 : kind_(kind), label_(label), action_(action), submenuname_(),
104 MenuItem::~MenuItem()
108 void MenuItem::submenu(Menu * menu)
110 submenu_.reset(menu);
114 string const MenuItem::label() const
116 return token(label_, '|', 0);
120 string const MenuItem::shortcut() const
122 return token(label_, '|', 1);
125 string const MenuItem::binding() const
127 if (kind_ != Command)
130 // Get the keys bound to this action, but keep only the
132 string bindings = toplevel_keymap->findbinding(action_);
134 if (!bindings.empty()) {
135 return bindings.substr(1, bindings.find(']') - 1);
141 Menu & Menu::add(MenuItem const & i, LyXView const * view)
149 case MenuItem::Command:
152 view->getLyXFunc().getStatus(i.action());
154 || (status.disabled() && i.optional()))
157 items_.back().status(status);
160 case MenuItem::Submenu:
163 bool disabled = true;
164 for (const_iterator cit = i.submenu()->begin();
165 cit != i.submenu()->end(); ++cit) {
166 if ((cit->kind() == MenuItem::Command
167 || cit->kind() == MenuItem::Submenu)
168 && !cit->status().disabled()) {
173 if (!disabled || !i.optional()) {
175 items_.back().status().disabled(disabled);
182 case MenuItem::Separator:
184 && items_.back().kind() != MenuItem::Separator)
195 Menu & Menu::read(LyXLex & lex)
218 struct keyword_item menutags[md_last - 1] = {
219 { "branches", md_branches },
220 { "documents", md_documents },
221 { "end", md_endmenu },
222 { "exportformats", md_exportformats },
223 { "floatinsert", md_floatinsert },
224 { "floatlistinsert", md_floatlistinsert },
225 { "importformats", md_importformats },
227 { "lastfiles", md_lastfiles },
228 { "optitem", md_optitem },
229 { "optsubmenu", md_optsubmenu },
230 { "pasterecent", md_pasterecent },
231 { "separator", md_separator },
232 { "submenu", md_submenu },
234 { "updateformats", md_updateformats },
235 { "viewformats", md_viewformats }
238 lex.pushTable(menutags, md_last - 1);
239 if (lyxerr.debugging(Debug::PARSER))
240 lex.printTable(lyxerr);
243 bool optional = false;
245 while (lex.isOK() && !quit) {
249 // fallback to md_item
252 string const name = _(lex.getString());
254 string const command = lex.getString();
255 add(MenuItem(MenuItem::Command, name,
262 add(MenuItem(MenuItem::Separator));
266 add(MenuItem(MenuItem::Lastfiles));
270 add(MenuItem(MenuItem::Documents));
274 add(MenuItem(MenuItem::Toc));
278 add(MenuItem(MenuItem::ViewFormats));
281 case md_updateformats:
282 add(MenuItem(MenuItem::UpdateFormats));
285 case md_exportformats:
286 add(MenuItem(MenuItem::ExportFormats));
289 case md_importformats:
290 add(MenuItem(MenuItem::ImportFormats));
293 case md_floatlistinsert:
294 add(MenuItem(MenuItem::FloatListInsert));
298 add(MenuItem(MenuItem::FloatInsert));
302 add(MenuItem(MenuItem::PasteRecent));
306 add(MenuItem(MenuItem::Branches));
311 // fallback to md_submenu
314 string const mlabel = _(lex.getString());
316 string const mname = lex.getString();
317 add(MenuItem(MenuItem::Submenu, mlabel, mname,
328 lex.printError("Menu::read: "
329 "Unknown menu tag: `$$Token'");
338 void Menu::checkShortcuts() const
340 // This is a quadratic algorithm, but we do not care because
341 // menus are short enough
342 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
343 string shortcut = it1->shortcut();
344 if (shortcut.empty())
346 if (!contains(it1->label(), shortcut))
347 lyxerr << "Menu warning: menu entry \""
349 << "\" does not contain shortcut `"
350 << shortcut << "'." << endl;
351 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
352 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
353 lyxerr << "Menu warning: menu entries "
354 << '"' << it1->fulllabel()
355 << "\" and \"" << it2->fulllabel()
356 << "\" share the same shortcut."
366 class compare_format {
368 bool operator()(Format const * p1, Format const * p2) {
373 string const limit_string_length(string const & str)
375 string::size_type const max_item_length = 45;
377 if (str.size() > max_item_length)
378 return str.substr(0, max_item_length - 3) + "...";
384 void expandLastfiles(Menu & tomenu, LyXView const * view)
387 LastFiles::const_iterator lfit = lastfiles->begin();
388 LastFiles::const_iterator end = lastfiles->end();
390 for (; lfit != end && ii < 10; ++lfit, ++ii) {
391 string const label = tostr(ii) + ". "
392 + MakeDisplayPath((*lfit), 30)
394 int const action = lyxaction.
395 getPseudoAction(LFUN_FILE_OPEN,
397 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
401 void expandDocuments(Menu & tomenu, LyXView const * view)
403 typedef vector<string> Strings;
404 Strings const names = bufferlist.getFileNames();
407 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
408 LFUN_NOACTION), view);
413 Strings::const_iterator docit = names.begin();
414 Strings::const_iterator end = names.end();
415 for (; docit != end; ++docit, ++ii) {
417 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
418 string label = MakeDisplayPath(*docit, 20);
420 label = tostr(ii) + ". " + label + '|' + tostr(ii);
421 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
426 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
428 if (!view->buffer() && kind != MenuItem::ImportFormats) {
429 tomenu.add(MenuItem(MenuItem::Command,
430 _("No Documents Open!"), LFUN_NOACTION),
435 typedef vector<Format const *> Formats;
440 case MenuItem::ImportFormats:
441 formats = Importer::GetImportableFormats();
442 action = LFUN_IMPORT;
444 case MenuItem::ViewFormats:
445 formats = Exporter::GetExportableFormats(*view->buffer(), true);
446 action = LFUN_PREVIEW;
448 case MenuItem::UpdateFormats:
449 formats = Exporter::GetExportableFormats(*view->buffer(), true);
450 action = LFUN_UPDATE;
453 formats = Exporter::GetExportableFormats(*view->buffer(), false);
454 action = LFUN_EXPORT;
456 sort(formats.begin(), formats.end(), compare_format());
458 Formats::const_iterator fit = formats.begin();
459 Formats::const_iterator end = formats.end();
460 for (; fit != end ; ++fit) {
463 string label = (*fit)->prettyname();
464 // we need to hide the default graphic export formats
465 // from the external menu, because we need them only
466 // for the internal lyx-view and external latex run
467 if (label == "EPS" || label == "XPM" || label == "PNG")
470 if (kind == MenuItem::ImportFormats) {
471 if ((*fit)->name() == "text")
472 label = _("ASCII text as lines");
473 else if ((*fit)->name() == "textparagraph")
474 label = _("ASCII text as paragraphs");
477 if (!(*fit)->shortcut().empty())
478 label += '|' + (*fit)->shortcut();
479 int const action2 = lyxaction.
480 getPseudoAction(action, (*fit)->name());
481 tomenu.add(MenuItem(MenuItem::Command, label, action2),
487 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
489 if (!view->buffer()) {
490 tomenu.add(MenuItem(MenuItem::Command,
491 _("No Documents Open!"), LFUN_NOACTION),
496 FloatList const & floats =
497 view->buffer()->params().getLyXTextClass().floats();
498 FloatList::const_iterator cit = floats.begin();
499 FloatList::const_iterator end = floats.end();
500 for (; cit != end; ++cit) {
501 int const action = lyxaction
502 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
503 tomenu.add(MenuItem(MenuItem::Command,
504 _(cit->second.listName()), action),
510 void expandFloatInsert(Menu & tomenu, LyXView const * view)
512 if (!view->buffer()) {
513 tomenu.add(MenuItem(MenuItem::Command,
514 _("No Documents Open!"), LFUN_NOACTION),
519 FloatList const & floats =
520 view->buffer()->params().getLyXTextClass().floats();
521 FloatList::const_iterator cit = floats.begin();
522 FloatList::const_iterator end = floats.end();
523 for (; cit != end; ++cit) {
526 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
528 string const label = _(cit->second.name());
529 tomenu.add(MenuItem(MenuItem::Command, label, action),
535 Menu::size_type const max_number_of_items = 25;
537 void expandToc2(Menu & tomenu,
538 lyx::toc::Toc const & toc_list,
539 lyx::toc::Toc::size_type from,
540 lyx::toc::Toc::size_type to, int depth)
542 int shortcut_count = 0;
543 if (to - from <= max_number_of_items) {
544 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
545 int const action = toc_list[i].action();
546 string label(4 * max(0, toc_list[i].depth - depth),' ');
547 label += limit_string_length(toc_list[i].str);
548 if (toc_list[i].depth == depth
549 && ++shortcut_count <= 9) {
550 label += '|' + tostr(shortcut_count);
552 tomenu.add(MenuItem(MenuItem::Command, label, action));
555 lyx::toc::Toc::size_type pos = from;
557 lyx::toc::Toc::size_type new_pos = pos + 1;
558 while (new_pos < to &&
559 toc_list[new_pos].depth > depth)
562 int const action = toc_list[pos].action();
563 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
564 label += limit_string_length(toc_list[pos].str);
565 if (toc_list[pos].depth == depth &&
566 ++shortcut_count <= 9)
567 label += '|' + tostr(shortcut_count);
569 if (new_pos == pos + 1) {
570 tomenu.add(MenuItem(MenuItem::Command,
573 MenuItem item(MenuItem::Submenu, label);
574 item.submenu(new Menu);
575 expandToc2(*item.submenu(),
576 toc_list, pos, new_pos, depth + 1);
585 void expandToc(Menu & tomenu, LyXView const * view)
587 // To make things very cleanly, we would have to pass view to
588 // all MenuItem constructors and to expandToc2. However, we
589 // know that all the entries in a TOC will be have status_ ==
590 // OK, so we avoid this unnecessary overhead (JMarc)
592 if (!view->buffer()) {
593 tomenu.add(MenuItem(MenuItem::Command,
594 _("No Documents Open!"), LFUN_NOACTION),
599 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
600 lyx::toc::TocList::const_iterator cit = toc_list.begin();
601 lyx::toc::TocList::const_iterator end = toc_list.end();
602 for (; cit != end; ++cit) {
604 if (cit->first == "TOC")
607 // All the rest is for floats
608 Menu * menu = new Menu;
609 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
610 lyx::toc::Toc::const_iterator eend = cit->second.end();
611 for (; ccit != eend; ++ccit) {
612 string const label = limit_string_length(ccit->str);
613 menu->add(MenuItem(MenuItem::Command,
614 label, ccit->action()));
616 string const & floatName = cit->first;
617 // Is the _(...) really needed here? (Lgb)
618 MenuItem item(MenuItem::Submenu, _(floatName));
624 cit = toc_list.find("TOC");
626 tomenu.add(MenuItem(MenuItem::Command,
627 _("No Table of contents")),
630 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
635 void expandPasteRecent(Menu & tomenu, LyXView const * view)
637 if (!view || !view->buffer())
640 vector<string> const selL =
641 CutAndPaste::availableSelections(*view->buffer());
643 vector<string>::const_iterator cit = selL.begin();
644 vector<string>::const_iterator end = selL.end();
646 for (unsigned int index = 0; cit != end; ++cit, ++index) {
647 int const action = lyxaction.getPseudoAction(LFUN_PASTE,
649 tomenu.add(MenuItem(MenuItem::Command, *cit, action));
654 void expandBranches(Menu & tomenu, LyXView const * view)
656 if (!view || !view->buffer())
659 BufferParams const & params = view->buffer()->params();
661 std::list<Branch>::const_iterator cit = params.branchlist.begin();
662 std::list<Branch>::const_iterator end = params.branchlist.end();
664 for (int ii = 1; cit != end; ++cit, ++ii) {
665 string label = cit->getBranch();
666 int const action = lyxaction.
667 getPseudoAction(LFUN_INSERT_BRANCH,
670 label = tostr(ii) + ". " + label + "|" + tostr(ii);
671 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
679 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
680 LyXView const * view) const
682 for (Menu::const_iterator cit = frommenu.begin();
683 cit != frommenu.end() ; ++cit) {
684 switch (cit->kind()) {
685 case MenuItem::Lastfiles:
686 expandLastfiles(tomenu, view);
689 case MenuItem::Documents:
690 expandDocuments(tomenu, view);
693 case MenuItem::ImportFormats:
694 case MenuItem::ViewFormats:
695 case MenuItem::UpdateFormats:
696 case MenuItem::ExportFormats:
697 expandFormats(cit->kind(), tomenu, view);
700 case MenuItem::FloatListInsert:
701 expandFloatListInsert(tomenu, view);
704 case MenuItem::FloatInsert:
705 expandFloatInsert(tomenu, view);
708 case MenuItem::PasteRecent:
709 expandPasteRecent(tomenu, view);
712 case MenuItem::Branches:
713 expandBranches(tomenu, view);
717 expandToc(tomenu, view);
720 case MenuItem::Submenu: {
722 item.submenu(new Menu(cit->submenuname()));
723 expand(getMenu(cit->submenuname()),
724 *item.submenu(), view);
725 tomenu.add(item, view);
730 tomenu.add(*cit, view);
734 // we do not want the menu to end with a separator
736 && tomenu.items_.back().kind() == MenuItem::Separator)
737 tomenu.items_.pop_back();
739 // Check whether the shortcuts are unique
740 tomenu.checkShortcuts();
744 bool Menu::hasSubmenu(string const & name) const
746 return find_if(begin(), end(),
747 lyx::compare_memfun(&MenuItem::submenuname,
752 void MenuBackend::read(LyXLex & lex)
761 struct keyword_item menutags[md_last - 1] = {
762 { "end", md_endmenuset },
764 { "menubar", md_menubar }
768 if (compare_ascii_no_case(lex.getString(), "menuset")) {
769 lyxerr << "Menubackend::read: ERROR wrong token:`"
770 << lex.getString() << '\'' << endl;
773 lex.pushTable(menutags, md_last - 1);
774 if (lyxerr.debugging(Debug::PARSER))
775 lex.printTable(lyxerr);
779 while (lex.isOK() && !quit) {
786 string const name = lex.getString();
788 getMenu(name).read(lex);
800 lex.printError("menubackend::read: "
801 "Unknown menu tag: `$$Token'");
809 void MenuBackend::add(Menu const & menu)
811 menulist_.push_back(menu);
815 bool MenuBackend::hasMenu(string const & name) const
817 return find_if(begin(), end(),
818 lyx::compare_memfun(&Menu::name, name)) != end();
822 Menu const & MenuBackend::getMenu(string const & name) const
824 const_iterator cit = find_if(begin(), end(),
825 lyx::compare_memfun(&Menu::name, name));
827 lyxerr << "No submenu named " << name << endl;
828 Assert(cit != end());
833 Menu & MenuBackend::getMenu(string const & name)
835 MenuList::iterator it =
836 find_if(menulist_.begin(), menulist_.end(),
837 lyx::compare_memfun(&Menu::name, name));
838 Assert(it != menulist_.end());
843 Menu const & MenuBackend::getMenubar() const