1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
10 * ====================================================== */
14 #include "MenuBackend.h"
16 #include "LyXAction.h"
20 #include "lastfiles.h"
22 #include "lyx_main.h" // for lastfiles
23 #include "bufferlist.h"
28 #include "FloatList.h"
30 #include "CutAndPaste.h"
31 #include "frontends/LyXView.h"
32 #include "support/LAssert.h"
33 #include "support/filetools.h"
34 #include "support/lyxfunctional.h"
35 #include "support/lstrings.h"
36 #include "support/tostr.h"
40 extern BufferList bufferlist;
41 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
43 using namespace lyx::support;
52 // This is the global menu definition
53 MenuBackend menubackend;
56 MenuItem::MenuItem(Kind kind, string const & label,
57 string const & command, bool optional)
58 : kind_(kind), label_(label), optional_(optional)
75 action_ = lyxaction.LookupFunc(command);
77 if (action_ == LFUN_UNKNOWN_ACTION) {
78 lyxerr << "MenuItem(): LyX command `"
79 << command << "' does not exist." << endl;
82 lyxerr[Debug::GUI] << "Optional item "
86 submenuname_ = command;
92 MenuItem::MenuItem(Kind kind, string const & label, int action, bool optional)
93 : kind_(kind), label_(label), action_(action), submenuname_(),
102 void MenuItem::submenu(Menu * menu)
104 submenu_.reset(menu);
108 string const MenuItem::label() const
110 return token(label_, '|', 0);
114 string const MenuItem::shortcut() const
116 return token(label_, '|', 1);
119 string const MenuItem::binding() const
121 if (kind_ != Command)
124 // Get the keys bound to this action, but keep only the
126 string bindings = toplevel_keymap->findbinding(action_);
128 if (!bindings.empty()) {
129 return bindings.substr(1, bindings.find(']') - 1);
135 Menu & Menu::add(MenuItem const & i, LyXView const * view)
143 case MenuItem::Command:
146 view->getLyXFunc().getStatus(i.action());
148 || (status.disabled() && i.optional()))
151 items_.back().status(status);
154 case MenuItem::Submenu:
157 bool disabled = true;
158 for (const_iterator cit = i.submenu()->begin();
159 cit != i.submenu()->end(); ++cit) {
160 if ((cit->kind() == MenuItem::Command
161 || cit->kind() == MenuItem::Submenu)
162 && !cit->status().disabled()) {
167 if (!disabled || !i.optional()) {
169 items_.back().status().disabled(disabled);
176 case MenuItem::Separator:
178 && items_.back().kind() != MenuItem::Separator)
189 Menu & Menu::read(LyXLex & lex)
212 struct keyword_item menutags[md_last - 1] = {
213 { "branches", md_branches },
214 { "documents", md_documents },
215 { "end", md_endmenu },
216 { "exportformats", md_exportformats },
217 { "floatinsert", md_floatinsert },
218 { "floatlistinsert", md_floatlistinsert },
219 { "importformats", md_importformats },
221 { "lastfiles", md_lastfiles },
222 { "optitem", md_optitem },
223 { "optsubmenu", md_optsubmenu },
224 { "pasterecent", md_pasterecent },
225 { "separator", md_separator },
226 { "submenu", md_submenu },
228 { "updateformats", md_updateformats },
229 { "viewformats", md_viewformats }
232 lex.pushTable(menutags, md_last - 1);
233 if (lyxerr.debugging(Debug::PARSER))
234 lex.printTable(lyxerr);
237 bool optional = false;
239 while (lex.isOK() && !quit) {
243 // fallback to md_item
246 string const name = _(lex.getString());
248 string const command = lex.getString();
249 add(MenuItem(MenuItem::Command, name,
256 add(MenuItem(MenuItem::Separator));
260 add(MenuItem(MenuItem::Lastfiles));
264 add(MenuItem(MenuItem::Documents));
268 add(MenuItem(MenuItem::Toc));
272 add(MenuItem(MenuItem::ViewFormats));
275 case md_updateformats:
276 add(MenuItem(MenuItem::UpdateFormats));
279 case md_exportformats:
280 add(MenuItem(MenuItem::ExportFormats));
283 case md_importformats:
284 add(MenuItem(MenuItem::ImportFormats));
287 case md_floatlistinsert:
288 add(MenuItem(MenuItem::FloatListInsert));
292 add(MenuItem(MenuItem::FloatInsert));
296 add(MenuItem(MenuItem::PasteRecent));
300 add(MenuItem(MenuItem::Branches));
305 // fallback to md_submenu
308 string const mlabel = _(lex.getString());
310 string const mname = lex.getString();
311 add(MenuItem(MenuItem::Submenu, mlabel, mname,
322 lex.printError("Menu::read: "
323 "Unknown menu tag: `$$Token'");
332 void Menu::checkShortcuts() const
334 // This is a quadratic algorithm, but we do not care because
335 // menus are short enough
336 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
337 string shortcut = it1->shortcut();
338 if (shortcut.empty())
340 if (!contains(it1->label(), shortcut))
341 lyxerr << "Menu warning: menu entry \""
343 << "\" does not contain shortcut `"
344 << shortcut << "'." << endl;
345 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
346 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
347 lyxerr << "Menu warning: menu entries "
348 << '"' << it1->fulllabel()
349 << "\" and \"" << it2->fulllabel()
350 << "\" share the same shortcut."
360 class compare_format {
362 bool operator()(Format const * p1, Format const * p2) {
367 string const limit_string_length(string const & str)
369 string::size_type const max_item_length = 45;
371 if (str.size() > max_item_length)
372 return str.substr(0, max_item_length - 3) + "...";
378 void expandLastfiles(Menu & tomenu, LyXView const * view)
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 int const action = lyxaction.
389 getPseudoAction(LFUN_FILE_OPEN,
391 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
395 void expandDocuments(Menu & tomenu, LyXView const * view)
397 typedef vector<string> Strings;
398 Strings const names = bufferlist.getFileNames();
401 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
402 LFUN_NOACTION), view);
407 Strings::const_iterator docit = names.begin();
408 Strings::const_iterator end = names.end();
409 for (; docit != end; ++docit, ++ii) {
411 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
412 string label = MakeDisplayPath(*docit, 20);
414 label = tostr(ii) + ". " + label + '|' + tostr(ii);
415 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
420 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
422 if (!view->buffer() && kind != MenuItem::ImportFormats) {
423 tomenu.add(MenuItem(MenuItem::Command,
424 _("No Documents Open!"), LFUN_NOACTION),
429 typedef vector<Format const *> Formats;
434 case MenuItem::ImportFormats:
435 formats = Importer::GetImportableFormats();
436 action = LFUN_IMPORT;
438 case MenuItem::ViewFormats:
439 formats = Exporter::GetExportableFormats(view->buffer(), true);
440 action = LFUN_PREVIEW;
442 case MenuItem::UpdateFormats:
443 formats = Exporter::GetExportableFormats(view->buffer(), true);
444 action = LFUN_UPDATE;
447 formats = Exporter::GetExportableFormats(view->buffer(), false);
448 action = LFUN_EXPORT;
450 sort(formats.begin(), formats.end(), compare_format());
452 Formats::const_iterator fit = formats.begin();
453 Formats::const_iterator end = formats.end();
454 for (; fit != end ; ++fit) {
457 string label = (*fit)->prettyname();
458 // we need to hide the default graphic export formats
459 // from the external menu, because we need them only
460 // for the internal lyx-view and external latex run
461 if (label == "EPS" || label == "XPM" || label == "PNG")
464 if (kind == MenuItem::ImportFormats) {
465 if ((*fit)->name() == "text")
466 label = _("ASCII text as lines");
467 else if ((*fit)->name() == "textparagraph")
468 label = _("ASCII text as paragraphs");
471 if (!(*fit)->shortcut().empty())
472 label += '|' + (*fit)->shortcut();
473 int const action2 = lyxaction.
474 getPseudoAction(action, (*fit)->name());
475 tomenu.add(MenuItem(MenuItem::Command, label, action2),
481 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
483 if (!view->buffer()) {
484 tomenu.add(MenuItem(MenuItem::Command,
485 _("No Documents Open!"), LFUN_NOACTION),
490 FloatList const & floats =
491 view->buffer()->params.getLyXTextClass().floats();
492 FloatList::const_iterator cit = floats.begin();
493 FloatList::const_iterator end = floats.end();
494 for (; cit != end; ++cit) {
495 int const action = lyxaction
496 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
497 tomenu.add(MenuItem(MenuItem::Command,
498 _(cit->second.listName()), action),
504 void expandFloatInsert(Menu & tomenu, LyXView const * view)
506 if (!view->buffer()) {
507 tomenu.add(MenuItem(MenuItem::Command,
508 _("No Documents Open!"), LFUN_NOACTION),
513 FloatList const & floats =
514 view->buffer()->params.getLyXTextClass().floats();
515 FloatList::const_iterator cit = floats.begin();
516 FloatList::const_iterator end = floats.end();
517 for (; cit != end; ++cit) {
520 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
522 string const label = _(cit->second.name());
523 tomenu.add(MenuItem(MenuItem::Command, label, action),
529 Menu::size_type const max_number_of_items = 25;
531 void expandToc2(Menu & tomenu,
532 lyx::toc::Toc const & toc_list,
533 lyx::toc::Toc::size_type from,
534 lyx::toc::Toc::size_type to, int depth)
536 int shortcut_count = 0;
537 if (to - from <= max_number_of_items) {
538 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
539 int const action = toc_list[i].action();
540 string label(4 * max(0, toc_list[i].depth - depth),' ');
541 label += limit_string_length(toc_list[i].str);
542 if (toc_list[i].depth == depth
543 && ++shortcut_count <= 9) {
544 label += '|' + tostr(shortcut_count);
546 tomenu.add(MenuItem(MenuItem::Command, label, action));
549 lyx::toc::Toc::size_type pos = from;
551 lyx::toc::Toc::size_type new_pos = pos + 1;
552 while (new_pos < to &&
553 toc_list[new_pos].depth > depth)
556 int const action = toc_list[pos].action();
557 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
558 label += limit_string_length(toc_list[pos].str);
559 if (toc_list[pos].depth == depth &&
560 ++shortcut_count <= 9)
561 label += '|' + tostr(shortcut_count);
563 if (new_pos == pos + 1) {
564 tomenu.add(MenuItem(MenuItem::Command,
567 MenuItem item(MenuItem::Submenu, label);
568 item.submenu(new Menu);
569 expandToc2(*item.submenu(),
570 toc_list, pos, new_pos, depth + 1);
579 void expandToc(Menu & tomenu, LyXView const * view)
581 // To make things very cleanly, we would have to pass view to
582 // all MenuItem constructors and to expandToc2. However, we
583 // know that all the entries in a TOC will be have status_ ==
584 // OK, so we avoid this unnecessary overhead (JMarc)
586 if (!view->buffer()) {
587 tomenu.add(MenuItem(MenuItem::Command,
588 _("No Documents Open!"), LFUN_NOACTION),
593 lyx::toc::TocList toc_list = lyx::toc::getTocList(view->buffer());
594 lyx::toc::TocList::const_iterator cit = toc_list.begin();
595 lyx::toc::TocList::const_iterator end = toc_list.end();
596 for (; cit != end; ++cit) {
598 if (cit->first == "TOC")
601 // All the rest is for floats
602 Menu * menu = new Menu;
603 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
604 lyx::toc::Toc::const_iterator eend = cit->second.end();
605 for (; ccit != eend; ++ccit) {
606 string const label = limit_string_length(ccit->str);
607 menu->add(MenuItem(MenuItem::Command,
608 label, 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")),
624 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
629 void expandPasteRecent(Menu & tomenu, LyXView const * view)
631 if (!view || !view->buffer())
634 vector<string> const selL =
635 CutAndPaste::availableSelections(*view->buffer());
637 vector<string>::const_iterator cit = selL.begin();
638 vector<string>::const_iterator end = selL.end();
640 for (unsigned int index = 0; cit != end; ++cit, ++index) {
641 int const action = lyxaction.getPseudoAction(LFUN_PASTE,
643 tomenu.add(MenuItem(MenuItem::Command, *cit, action));
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();
660 int const action = lyxaction.
661 getPseudoAction(LFUN_INSERT_BRANCH,
664 label = tostr(ii) + ". " + label + "|" + tostr(ii);
665 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
673 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
674 LyXView const * view) const
676 for (Menu::const_iterator cit = frommenu.begin();
677 cit != frommenu.end() ; ++cit) {
678 switch (cit->kind()) {
679 case MenuItem::Lastfiles:
680 expandLastfiles(tomenu, view);
683 case MenuItem::Documents:
684 expandDocuments(tomenu, view);
687 case MenuItem::ImportFormats:
688 case MenuItem::ViewFormats:
689 case MenuItem::UpdateFormats:
690 case MenuItem::ExportFormats:
691 expandFormats(cit->kind(), tomenu, view);
694 case MenuItem::FloatListInsert:
695 expandFloatListInsert(tomenu, view);
698 case MenuItem::FloatInsert:
699 expandFloatInsert(tomenu, view);
702 case MenuItem::PasteRecent:
703 expandPasteRecent(tomenu, view);
706 case MenuItem::Branches:
707 expandBranches(tomenu, view);
711 expandToc(tomenu, view);
714 case MenuItem::Submenu: {
716 item.submenu(new Menu(cit->submenuname()));
717 expand(getMenu(cit->submenuname()),
718 *item.submenu(), view);
719 tomenu.add(item, view);
724 tomenu.add(*cit, view);
728 // we do not want the menu to end with a separator
730 && tomenu.items_.back().kind() == MenuItem::Separator)
731 tomenu.items_.pop_back();
733 // Check whether the shortcuts are unique
734 tomenu.checkShortcuts();
738 bool Menu::hasSubmenu(string const & name) const
740 return find_if(begin(), end(),
741 lyx::compare_memfun(&MenuItem::submenuname,
746 void MenuBackend::read(LyXLex & lex)
755 struct keyword_item menutags[md_last - 1] = {
756 { "end", md_endmenuset },
758 { "menubar", md_menubar }
762 if (compare_ascii_no_case(lex.getString(), "menuset")) {
763 lyxerr << "Menubackend::read: ERROR wrong token:`"
764 << lex.getString() << '\'' << endl;
767 lex.pushTable(menutags, md_last - 1);
768 if (lyxerr.debugging(Debug::PARSER))
769 lex.printTable(lyxerr);
773 while (lex.isOK() && !quit) {
780 string const name = lex.getString();
782 getMenu(name).read(lex);
794 lex.printError("menubackend::read: "
795 "Unknown menu tag: `$$Token'");
803 void MenuBackend::add(Menu const & menu)
805 menulist_.push_back(menu);
809 bool MenuBackend::hasMenu(string const & name) const
811 return find_if(begin(), end(),
812 lyx::compare_memfun(&Menu::name, name)) != end();
816 Menu const & MenuBackend::getMenu(string const & name) const
818 const_iterator cit = find_if(begin(), end(),
819 lyx::compare_memfun(&Menu::name, name));
821 lyxerr << "No submenu named " << name << endl;
822 Assert(cit != end());
827 Menu & MenuBackend::getMenu(string const & name)
829 MenuList::iterator it =
830 find_if(menulist_.begin(), menulist_.end(),
831 lyx::compare_memfun(&Menu::name, name));
832 Assert(it != menulist_.end());
837 Menu const & MenuBackend::getMenubar() const