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)
70 : kind_(kind), optional_(false)
74 MenuItem::MenuItem(Kind kind, string const & label,
75 string const & submenu, bool optional)
76 : kind_(kind), label_(label),
77 submenuname_(submenu), optional_(optional)
79 BOOST_ASSERT(kind == Submenu);
83 MenuItem::MenuItem(Kind kind, string const & label,
84 FuncRequest const & func, bool optional)
85 : kind_(kind), label_(label), func_(func), optional_(optional)
93 void MenuItem::submenu(Menu * menu)
99 string const MenuItem::label() const
101 return token(label_, '|', 0);
105 string const MenuItem::shortcut() const
107 return token(label_, '|', 1);
111 string const MenuItem::binding() const
113 if (kind_ != Command)
116 // Get the keys bound to this action, but keep only the
118 string bindings = toplevel_keymap->findbinding(func_);
120 if (!bindings.empty()) {
121 return bindings.substr(1, bindings.find(']') - 1);
124 << "No bindings for "
125 << lyxaction.getActionName(func_.action)
126 << '(' << func_.argument << ')' << endl;
133 Menu & Menu::add(MenuItem const & i, LyXView const * view)
141 case MenuItem::Command:
144 view->getLyXFunc().getStatus(i.func());
146 || (status.disabled() && i.optional()))
149 items_.back().status(status);
152 case MenuItem::Submenu:
155 bool disabled = true;
156 for (const_iterator cit = i.submenu()->begin();
157 cit != i.submenu()->end(); ++cit) {
158 if ((cit->kind() == MenuItem::Command
159 || cit->kind() == MenuItem::Submenu)
160 && !cit->status().disabled()) {
165 if (!disabled || !i.optional()) {
167 items_.back().status().disabled(disabled);
174 case MenuItem::Separator:
176 && items_.back().kind() != MenuItem::Separator)
187 Menu & Menu::read(LyXLex & lex)
210 struct keyword_item menutags[md_last - 1] = {
211 { "branches", md_branches },
212 { "documents", md_documents },
213 { "end", md_endmenu },
214 { "exportformats", md_exportformats },
215 { "floatinsert", md_floatinsert },
216 { "floatlistinsert", md_floatlistinsert },
217 { "importformats", md_importformats },
219 { "lastfiles", md_lastfiles },
220 { "optitem", md_optitem },
221 { "optsubmenu", md_optsubmenu },
222 { "pasterecent", md_pasterecent },
223 { "separator", md_separator },
224 { "submenu", md_submenu },
226 { "updateformats", md_updateformats },
227 { "viewformats", md_viewformats }
230 lex.pushTable(menutags, md_last - 1);
231 if (lyxerr.debugging(Debug::PARSER))
232 lex.printTable(lyxerr);
235 bool optional = false;
237 while (lex.isOK() && !quit) {
241 // fallback to md_item
244 string const name = _(lex.getString());
246 string const command = lex.getString();
247 FuncRequest func = lyxaction.lookupFunc(command);
248 add(MenuItem(MenuItem::Command, name, func, optional));
254 add(MenuItem(MenuItem::Separator));
258 add(MenuItem(MenuItem::Lastfiles));
262 add(MenuItem(MenuItem::Documents));
266 add(MenuItem(MenuItem::Toc));
270 add(MenuItem(MenuItem::ViewFormats));
273 case md_updateformats:
274 add(MenuItem(MenuItem::UpdateFormats));
277 case md_exportformats:
278 add(MenuItem(MenuItem::ExportFormats));
281 case md_importformats:
282 add(MenuItem(MenuItem::ImportFormats));
285 case md_floatlistinsert:
286 add(MenuItem(MenuItem::FloatListInsert));
290 add(MenuItem(MenuItem::FloatInsert));
294 add(MenuItem(MenuItem::PasteRecent));
298 add(MenuItem(MenuItem::Branches));
303 // fallback to md_submenu
306 string const mlabel = _(lex.getString());
308 string const mname = lex.getString();
309 add(MenuItem(MenuItem::Submenu, mlabel, mname,
320 lex.printError("Menu::read: "
321 "Unknown menu tag: `$$Token'");
330 void Menu::checkShortcuts() const
332 // This is a quadratic algorithm, but we do not care because
333 // menus are short enough
334 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
335 string shortcut = it1->shortcut();
336 if (shortcut.empty())
338 if (!contains(it1->label(), shortcut))
339 lyxerr << "Menu warning: menu entry \""
341 << "\" does not contain shortcut `"
342 << shortcut << "'." << endl;
343 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
344 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
345 lyxerr << "Menu warning: menu entries "
346 << '"' << it1->fulllabel()
347 << "\" and \"" << it2->fulllabel()
348 << "\" share the same shortcut."
358 class compare_format {
360 bool operator()(Format const * p1, Format const * p2) {
365 string const limit_string_length(string const & str)
367 string::size_type const max_item_length = 45;
369 if (str.size() > max_item_length)
370 return str.substr(0, max_item_length - 3) + "...";
376 void expandLastfiles(Menu & tomenu, LyXView const * view)
378 LastFiles const & lastfiles = LyX::cref().lastfiles();
381 LastFiles::const_iterator lfit = lastfiles.begin();
382 LastFiles::const_iterator end = lastfiles.end();
384 for (; lfit != end && ii < 10; ++lfit, ++ii) {
385 string const label = tostr(ii) + ". "
386 + MakeDisplayPath((*lfit), 30)
388 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
392 void expandDocuments(Menu & tomenu, LyXView const * view)
394 typedef vector<string> Strings;
395 Strings const names = bufferlist.getFileNames();
398 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
399 FuncRequest(LFUN_NOACTION)), view);
404 Strings::const_iterator docit = names.begin();
405 Strings::const_iterator end = names.end();
406 for (; docit != end; ++docit, ++ii) {
407 string label = MakeDisplayPath(*docit, 20);
409 label = tostr(ii) + ". " + label + '|' + tostr(ii);
410 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_SWITCHBUFFER, *docit)), view);
415 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
417 if (!view->buffer() && kind != MenuItem::ImportFormats) {
418 tomenu.add(MenuItem(MenuItem::Command,
419 _("No Documents Open!"),
420 FuncRequest(LFUN_NOACTION)),
425 typedef vector<Format const *> Formats;
430 case MenuItem::ImportFormats:
431 formats = Importer::GetImportableFormats();
432 action = LFUN_IMPORT;
434 case MenuItem::ViewFormats:
435 formats = Exporter::GetExportableFormats(*view->buffer(), true);
436 action = LFUN_PREVIEW;
438 case MenuItem::UpdateFormats:
439 formats = Exporter::GetExportableFormats(*view->buffer(), true);
440 action = LFUN_UPDATE;
443 formats = Exporter::GetExportableFormats(*view->buffer(), false);
444 action = LFUN_EXPORT;
446 sort(formats.begin(), formats.end(), compare_format());
448 Formats::const_iterator fit = formats.begin();
449 Formats::const_iterator end = formats.end();
450 for (; fit != end ; ++fit) {
453 string label = (*fit)->prettyname();
454 // we need to hide the default graphic export formats
455 // from the external menu, because we need them only
456 // for the internal lyx-view and external latex run
457 if (label == "EPS" || label == "XPM" || label == "PNG")
460 if (kind == MenuItem::ImportFormats) {
461 if ((*fit)->name() == "text")
462 label = _("ASCII text as lines");
463 else if ((*fit)->name() == "textparagraph")
464 label = _("ASCII text as paragraphs");
467 if (!(*fit)->shortcut().empty())
468 label += '|' + (*fit)->shortcut();
470 tomenu.add(MenuItem(MenuItem::Command, label,
471 FuncRequest(action, (*fit)->name())),
477 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
479 if (!view->buffer()) {
480 tomenu.add(MenuItem(MenuItem::Command,
481 _("No Documents Open!"),
482 FuncRequest(LFUN_NOACTION)),
487 FloatList const & floats =
488 view->buffer()->params().getLyXTextClass().floats();
489 FloatList::const_iterator cit = floats.begin();
490 FloatList::const_iterator end = floats.end();
491 for (; cit != end; ++cit) {
492 tomenu.add(MenuItem(MenuItem::Command,
493 _(cit->second.listName()),
494 FuncRequest(LFUN_FLOAT_LIST,
495 cit->second.type())),
501 void expandFloatInsert(Menu & tomenu, LyXView const * view)
503 if (!view->buffer()) {
504 tomenu.add(MenuItem(MenuItem::Command,
505 _("No Documents Open!"),
506 FuncRequest(LFUN_NOACTION)),
511 FloatList const & floats =
512 view->buffer()->params().getLyXTextClass().floats();
513 FloatList::const_iterator cit = floats.begin();
514 FloatList::const_iterator end = floats.end();
515 for (; cit != end; ++cit) {
517 string const label = _(cit->second.name());
518 tomenu.add(MenuItem(MenuItem::Command, label,
519 FuncRequest(LFUN_INSET_FLOAT,
520 cit->second.type())),
526 Menu::size_type const max_number_of_items = 25;
528 void expandToc2(Menu & tomenu,
529 lyx::toc::Toc const & toc_list,
530 lyx::toc::Toc::size_type from,
531 lyx::toc::Toc::size_type to, int depth)
533 int shortcut_count = 0;
534 if (to - from <= max_number_of_items) {
535 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
536 string label(4 * max(0, toc_list[i].depth - depth),' ');
537 label += limit_string_length(toc_list[i].str);
538 if (toc_list[i].depth == depth
539 && shortcut_count < 9) {
540 if (label.find(tostr(shortcut_count + 1)) != string::npos)
541 label += '|' + tostr(++shortcut_count);
543 tomenu.add(MenuItem(MenuItem::Command, label,
544 FuncRequest(toc_list[i].action())));
547 lyx::toc::Toc::size_type pos = from;
549 lyx::toc::Toc::size_type new_pos = pos + 1;
550 while (new_pos < to &&
551 toc_list[new_pos].depth > depth)
554 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
555 label += limit_string_length(toc_list[pos].str);
556 if (toc_list[pos].depth == depth &&
557 shortcut_count < 9) {
558 if (label.find(tostr(shortcut_count + 1)) != string::npos)
559 label += '|' + tostr(++shortcut_count);
561 if (new_pos == pos + 1) {
562 tomenu.add(MenuItem(MenuItem::Command,
563 label, FuncRequest(toc_list[pos].action())));
565 MenuItem item(MenuItem::Submenu, label);
566 item.submenu(new Menu);
567 expandToc2(*item.submenu(),
568 toc_list, pos, new_pos, depth + 1);
577 void expandToc(Menu & tomenu, LyXView const * view)
579 // To make things very cleanly, we would have to pass view to
580 // all MenuItem constructors and to expandToc2. However, we
581 // know that all the entries in a TOC will be have status_ ==
582 // OK, so we avoid this unnecessary overhead (JMarc)
584 if (!view->buffer()) {
585 tomenu.add(MenuItem(MenuItem::Command,
586 _("No Documents Open!"),
587 FuncRequest(LFUN_NOACTION)),
592 lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
593 lyx::toc::TocList::const_iterator cit = toc_list.begin();
594 lyx::toc::TocList::const_iterator end = toc_list.end();
595 for (; cit != end; ++cit) {
597 if (cit->first == "TOC")
600 // All the rest is for floats
601 Menu * menu = new Menu;
602 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
603 lyx::toc::Toc::const_iterator eend = cit->second.end();
604 for (; ccit != eend; ++ccit) {
605 string const label = limit_string_length(ccit->str);
606 menu->add(MenuItem(MenuItem::Command,
608 FuncRequest(ccit->action())));
610 string const & floatName = cit->first;
611 // Is the _(...) really needed here? (Lgb)
612 MenuItem item(MenuItem::Submenu, _(floatName));
618 cit = toc_list.find("TOC");
620 tomenu.add(MenuItem(MenuItem::Command,
621 _("No Table of contents"),
625 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
630 void expandPasteRecent(Menu & tomenu, LyXView const * view)
632 if (!view || !view->buffer())
635 vector<string> const selL =
636 CutAndPaste::availableSelections(*view->buffer());
638 vector<string>::const_iterator cit = selL.begin();
639 vector<string>::const_iterator end = selL.end();
641 for (unsigned int index = 0; cit != end; ++cit, ++index) {
642 tomenu.add(MenuItem(MenuItem::Command, *cit,
643 FuncRequest(LFUN_PASTE, tostr(index))));
648 void expandBranches(Menu & tomenu, LyXView const * view)
650 if (!view || !view->buffer())
653 BufferParams const & params = view->buffer()->params();
655 std::list<Branch>::const_iterator cit = params.branchlist().begin();
656 std::list<Branch>::const_iterator end = params.branchlist().end();
658 for (int ii = 1; cit != end; ++cit, ++ii) {
659 string label = cit->getBranch();
661 label = tostr(ii) + ". " + label + "|" + tostr(ii);
662 tomenu.add(MenuItem(MenuItem::Command, label,
663 FuncRequest(LFUN_INSERT_BRANCH,
664 cit->getBranch())), view);
672 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
673 LyXView const * view) const
675 for (Menu::const_iterator cit = frommenu.begin();
676 cit != frommenu.end() ; ++cit) {
677 switch (cit->kind()) {
678 case MenuItem::Lastfiles:
679 expandLastfiles(tomenu, view);
682 case MenuItem::Documents:
683 expandDocuments(tomenu, view);
686 case MenuItem::ImportFormats:
687 case MenuItem::ViewFormats:
688 case MenuItem::UpdateFormats:
689 case MenuItem::ExportFormats:
690 expandFormats(cit->kind(), tomenu, view);
693 case MenuItem::FloatListInsert:
694 expandFloatListInsert(tomenu, view);
697 case MenuItem::FloatInsert:
698 expandFloatInsert(tomenu, view);
701 case MenuItem::PasteRecent:
702 expandPasteRecent(tomenu, view);
705 case MenuItem::Branches:
706 expandBranches(tomenu, view);
710 expandToc(tomenu, view);
713 case MenuItem::Submenu: {
715 item.submenu(new Menu(cit->submenuname()));
716 expand(getMenu(cit->submenuname()),
717 *item.submenu(), view);
718 tomenu.add(item, view);
723 tomenu.add(*cit, view);
727 // we do not want the menu to end with a separator
729 && tomenu.items_.back().kind() == MenuItem::Separator)
730 tomenu.items_.pop_back();
732 // Check whether the shortcuts are unique
733 tomenu.checkShortcuts();
737 bool Menu::hasSubmenu(string const & name) const
739 return find_if(begin(), end(),
740 lyx::compare_memfun(&MenuItem::submenuname,
745 void MenuBackend::read(LyXLex & lex)
754 struct keyword_item menutags[md_last - 1] = {
755 { "end", md_endmenuset },
757 { "menubar", md_menubar }
761 if (compare_ascii_no_case(lex.getString(), "menuset")) {
762 lyxerr << "Menubackend::read: ERROR wrong token:`"
763 << lex.getString() << '\'' << endl;
766 lex.pushTable(menutags, md_last - 1);
767 if (lyxerr.debugging(Debug::PARSER))
768 lex.printTable(lyxerr);
772 while (lex.isOK() && !quit) {
779 string const name = lex.getString();
781 getMenu(name).read(lex);
793 lex.printError("menubackend::read: "
794 "Unknown menu tag: `$$Token'");
802 void MenuBackend::add(Menu const & menu)
804 menulist_.push_back(menu);
808 bool MenuBackend::hasMenu(string const & name) const
810 return find_if(begin(), end(),
811 lyx::compare_memfun(&Menu::name, name)) != end();
815 Menu const & MenuBackend::getMenu(string const & name) const
817 const_iterator cit = find_if(begin(), end(),
818 lyx::compare_memfun(&Menu::name, name));
820 lyxerr << "No submenu named " << name << endl;
821 BOOST_ASSERT(cit != end());
826 Menu & MenuBackend::getMenu(string const & name)
828 MenuList::iterator it =
829 find_if(menulist_.begin(), menulist_.end(),
830 lyx::compare_memfun(&Menu::name, name));
831 BOOST_ASSERT(it != menulist_.end());
836 Menu const & MenuBackend::getMenubar() const