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)
74 action_ = lyxaction.LookupFunc(command);
76 if (action_ == LFUN_UNKNOWN_ACTION) {
77 lyxerr << "MenuItem(): LyX command `"
78 << command << "' does not exist." << endl;
81 lyxerr[Debug::GUI] << "Optional item "
85 submenuname_ = command;
91 MenuItem::MenuItem(Kind kind, string const & label, int action, bool optional)
92 : kind_(kind), label_(label), action_(action), submenuname_(),
101 void MenuItem::submenu(Menu * menu)
103 submenu_.reset(menu);
107 string const MenuItem::label() const
109 return token(label_, '|', 0);
113 string const MenuItem::shortcut() const
115 return token(label_, '|', 1);
118 string const MenuItem::binding() const
120 if (kind_ != Command)
123 // Get the keys bound to this action, but keep only the
125 string bindings = toplevel_keymap->findbinding(action_);
127 if (!bindings.empty()) {
128 return bindings.substr(1, bindings.find(']') - 1);
134 Menu & Menu::add(MenuItem const & i, LyXView const * view)
142 case MenuItem::Command:
145 view->getLyXFunc().getStatus(i.action());
147 || (status.disabled() && i.optional()))
150 items_.back().status(status);
153 case MenuItem::Submenu:
156 bool disabled = true;
157 for (const_iterator cit = i.submenu()->begin();
158 cit != i.submenu()->end(); ++cit) {
159 if ((cit->kind() == MenuItem::Command
160 || cit->kind() == MenuItem::Submenu)
161 && !cit->status().disabled()) {
166 if (!disabled || !i.optional()) {
168 items_.back().status().disabled(disabled);
175 case MenuItem::Separator:
177 && items_.back().kind() != MenuItem::Separator)
188 Menu & Menu::read(LyXLex & lex)
210 struct keyword_item menutags[md_last - 1] = {
211 { "documents", md_documents },
212 { "end", md_endmenu },
213 { "exportformats", md_exportformats },
214 { "floatinsert", md_floatinsert },
215 { "floatlistinsert", md_floatlistinsert },
216 { "importformats", md_importformats },
218 { "lastfiles", md_lastfiles },
219 { "optitem", md_optitem },
220 { "optsubmenu", md_optsubmenu },
221 { "pasterecent", md_pasterecent },
222 { "separator", md_separator },
223 { "submenu", md_submenu },
225 { "updateformats", md_updateformats },
226 { "viewformats", md_viewformats }
229 lex.pushTable(menutags, md_last - 1);
230 if (lyxerr.debugging(Debug::PARSER))
231 lex.printTable(lyxerr);
234 bool optional = false;
236 while (lex.isOK() && !quit) {
240 // fallback to md_item
243 string const name = _(lex.getString());
245 string const command = lex.getString();
246 add(MenuItem(MenuItem::Command, name,
253 add(MenuItem(MenuItem::Separator));
257 add(MenuItem(MenuItem::Lastfiles));
261 add(MenuItem(MenuItem::Documents));
265 add(MenuItem(MenuItem::Toc));
269 add(MenuItem(MenuItem::ViewFormats));
272 case md_updateformats:
273 add(MenuItem(MenuItem::UpdateFormats));
276 case md_exportformats:
277 add(MenuItem(MenuItem::ExportFormats));
280 case md_importformats:
281 add(MenuItem(MenuItem::ImportFormats));
284 case md_floatlistinsert:
285 add(MenuItem(MenuItem::FloatListInsert));
289 add(MenuItem(MenuItem::FloatInsert));
293 add(MenuItem(MenuItem::PasteRecent));
298 // fallback to md_submenu
301 string const mlabel = _(lex.getString());
303 string const mname = lex.getString();
304 add(MenuItem(MenuItem::Submenu, mlabel, mname,
315 lex.printError("Menu::read: "
316 "Unknown menu tag: `$$Token'");
325 void Menu::checkShortcuts() const
327 // This is a quadratic algorithm, but we do not care because
328 // menus are short enough
329 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
330 string shortcut = it1->shortcut();
331 if (shortcut.empty())
333 if (!contains(it1->label(), shortcut))
334 lyxerr << "Menu warning: menu entry \""
336 << "\" does not contain shortcut `"
337 << shortcut << "'." << endl;
338 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
339 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
340 lyxerr << "Menu warning: menu entries "
341 << '"' << it1->fulllabel()
342 << "\" and \"" << it2->fulllabel()
343 << "\" share the same shortcut."
353 class compare_format {
355 bool operator()(Format const * p1, Format const * p2) {
360 string const limit_string_length(string const & str)
362 string::size_type const max_item_length = 45;
364 if (str.size() > max_item_length)
365 return str.substr(0, max_item_length - 3) + "...";
371 void expandLastfiles(Menu & tomenu, LyXView const * view)
374 LastFiles::const_iterator lfit = lastfiles->begin();
375 LastFiles::const_iterator end = lastfiles->end();
377 for (; lfit != end && ii < 10; ++lfit, ++ii) {
378 string const label = tostr(ii) + ". "
379 + MakeDisplayPath((*lfit), 30)
381 int const action = lyxaction.
382 getPseudoAction(LFUN_FILE_OPEN,
384 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
388 void expandDocuments(Menu & tomenu, LyXView const * view)
390 typedef vector<string> Strings;
391 Strings const names = bufferlist.getFileNames();
394 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
395 LFUN_NOACTION), view);
400 Strings::const_iterator docit = names.begin();
401 Strings::const_iterator end = names.end();
402 for (; docit != end; ++docit, ++ii) {
404 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
405 string label = MakeDisplayPath(*docit, 20);
407 label = tostr(ii) + ". " + label + '|' + tostr(ii);
408 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
413 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
415 if (!view->buffer() && kind != MenuItem::ImportFormats) {
416 tomenu.add(MenuItem(MenuItem::Command,
417 _("No Documents Open!"), LFUN_NOACTION),
422 typedef vector<Format const *> Formats;
427 case MenuItem::ImportFormats:
428 formats = Importer::GetImportableFormats();
429 action = LFUN_IMPORT;
431 case MenuItem::ViewFormats:
432 formats = Exporter::GetExportableFormats(view->buffer(), true);
433 action = LFUN_PREVIEW;
435 case MenuItem::UpdateFormats:
436 formats = Exporter::GetExportableFormats(view->buffer(), true);
437 action = LFUN_UPDATE;
440 formats = Exporter::GetExportableFormats(view->buffer(), false);
441 action = LFUN_EXPORT;
443 sort(formats.begin(), formats.end(), compare_format());
445 Formats::const_iterator fit = formats.begin();
446 Formats::const_iterator end = formats.end();
447 for (; fit != end ; ++fit) {
450 string label = (*fit)->prettyname();
451 // we need to hide the default graphic export formats
452 // from the external menu, because we need them only
453 // for the internal lyx-view and external latex run
454 if (label == "EPS" || label == "XPM" || label == "PNG")
457 if (kind == MenuItem::ImportFormats) {
458 if ((*fit)->name() == "text")
459 label = _("ASCII text as lines");
460 else if ((*fit)->name() == "textparagraph")
461 label = _("ASCII text as paragraphs");
464 if (!(*fit)->shortcut().empty())
465 label += '|' + (*fit)->shortcut();
466 int const action2 = lyxaction.
467 getPseudoAction(action, (*fit)->name());
468 tomenu.add(MenuItem(MenuItem::Command, label, action2),
474 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
476 if (!view->buffer()) {
477 tomenu.add(MenuItem(MenuItem::Command,
478 _("No Documents Open!"), LFUN_NOACTION),
483 FloatList const & floats =
484 view->buffer()->params.getLyXTextClass().floats();
485 FloatList::const_iterator cit = floats.begin();
486 FloatList::const_iterator end = floats.end();
487 for (; cit != end; ++cit) {
488 int const action = lyxaction
489 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
490 tomenu.add(MenuItem(MenuItem::Command,
491 _(cit->second.listName()), action),
497 void expandFloatInsert(Menu & tomenu, LyXView const * view)
499 if (!view->buffer()) {
500 tomenu.add(MenuItem(MenuItem::Command,
501 _("No Documents Open!"), LFUN_NOACTION),
506 FloatList const & floats =
507 view->buffer()->params.getLyXTextClass().floats();
508 FloatList::const_iterator cit = floats.begin();
509 FloatList::const_iterator end = floats.end();
510 for (; cit != end; ++cit) {
513 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
515 string const label = _(cit->second.name());
516 tomenu.add(MenuItem(MenuItem::Command, label, action),
522 Menu::size_type const max_number_of_items = 25;
524 void expandToc2(Menu & tomenu,
525 lyx::toc::Toc const & toc_list,
526 lyx::toc::Toc::size_type from,
527 lyx::toc::Toc::size_type to, int depth)
529 int shortcut_count = 0;
530 if (to - from <= max_number_of_items) {
531 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
532 int const action = toc_list[i].action();
533 string label(4 * max(0, toc_list[i].depth - depth),' ');
534 label += limit_string_length(toc_list[i].str);
535 if (toc_list[i].depth == depth
536 && ++shortcut_count <= 9) {
537 label += '|' + tostr(shortcut_count);
539 tomenu.add(MenuItem(MenuItem::Command, label, action));
542 lyx::toc::Toc::size_type pos = from;
544 lyx::toc::Toc::size_type new_pos = pos + 1;
545 while (new_pos < to &&
546 toc_list[new_pos].depth > depth)
549 int const action = toc_list[pos].action();
550 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
551 label += limit_string_length(toc_list[pos].str);
552 if (toc_list[pos].depth == depth &&
553 ++shortcut_count <= 9)
554 label += '|' + tostr(shortcut_count);
556 if (new_pos == pos + 1) {
557 tomenu.add(MenuItem(MenuItem::Command,
560 MenuItem item(MenuItem::Submenu, label);
561 item.submenu(new Menu);
562 expandToc2(*item.submenu(),
563 toc_list, pos, new_pos, depth + 1);
572 void expandToc(Menu & tomenu, LyXView const * view)
574 // To make things very cleanly, we would have to pass view to
575 // all MenuItem constructors and to expandToc2. However, we
576 // know that all the entries in a TOC will be have status_ ==
577 // OK, so we avoid this unnecessary overhead (JMarc)
579 if (!view->buffer()) {
580 tomenu.add(MenuItem(MenuItem::Command,
581 _("No Documents Open!"), LFUN_NOACTION),
586 lyx::toc::TocList toc_list = lyx::toc::getTocList(view->buffer());
587 lyx::toc::TocList::const_iterator cit = toc_list.begin();
588 lyx::toc::TocList::const_iterator end = toc_list.end();
589 for (; cit != end; ++cit) {
591 if (cit->first == "TOC")
594 // All the rest is for floats
595 Menu * menu = new Menu;
596 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
597 lyx::toc::Toc::const_iterator eend = cit->second.end();
598 for (; ccit != eend; ++ccit) {
599 string const label = limit_string_length(ccit->str);
600 menu->add(MenuItem(MenuItem::Command,
601 label, ccit->action()));
603 string const & floatName = cit->first;
604 // Is the _(...) really needed here? (Lgb)
605 MenuItem item(MenuItem::Submenu, _(floatName));
611 cit = toc_list.find("TOC");
613 tomenu.add(MenuItem(MenuItem::Command,
614 _("No Table of contents")),
617 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
622 void expandPasteRecent(Menu & tomenu, LyXView const * view)
624 vector<string> const selL =
625 CutAndPaste::availableSelections(*view->buffer());
627 vector<string>::const_iterator cit = selL.begin();
628 vector<string>::const_iterator end = selL.end();
630 for (unsigned int index = 0; cit != end; ++cit, ++index) {
631 int const action = lyxaction.getPseudoAction(LFUN_PASTE,
633 tomenu.add(MenuItem(MenuItem::Command, *cit, action));
641 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
642 LyXView const * view) const
644 for (Menu::const_iterator cit = frommenu.begin();
645 cit != frommenu.end() ; ++cit) {
646 switch (cit->kind()) {
647 case MenuItem::Lastfiles:
648 expandLastfiles(tomenu, view);
651 case MenuItem::Documents:
652 expandDocuments(tomenu, view);
655 case MenuItem::ImportFormats:
656 case MenuItem::ViewFormats:
657 case MenuItem::UpdateFormats:
658 case MenuItem::ExportFormats:
659 expandFormats(cit->kind(), tomenu, view);
662 case MenuItem::FloatListInsert:
663 expandFloatListInsert(tomenu, view);
666 case MenuItem::FloatInsert:
667 expandFloatInsert(tomenu, view);
670 case MenuItem::PasteRecent:
671 expandPasteRecent(tomenu, view);
675 expandToc(tomenu, view);
678 case MenuItem::Submenu: {
680 item.submenu(new Menu(cit->submenuname()));
681 expand(getMenu(cit->submenuname()),
682 *item.submenu(), view);
683 tomenu.add(item, view);
688 tomenu.add(*cit, view);
692 // we do not want the menu to end with a separator
694 && tomenu.items_.back().kind() == MenuItem::Separator)
695 tomenu.items_.pop_back();
697 // Check whether the shortcuts are unique
698 tomenu.checkShortcuts();
702 bool Menu::hasSubmenu(string const & name) const
704 return find_if(begin(), end(),
705 lyx::compare_memfun(&MenuItem::submenuname,
710 void MenuBackend::read(LyXLex & lex)
719 struct keyword_item menutags[md_last - 1] = {
720 { "end", md_endmenuset },
722 { "menubar", md_menubar }
726 if (compare_ascii_no_case(lex.getString(), "menuset")) {
727 lyxerr << "Menubackend::read: ERROR wrong token:`"
728 << lex.getString() << '\'' << endl;
731 lex.pushTable(menutags, md_last - 1);
732 if (lyxerr.debugging(Debug::PARSER))
733 lex.printTable(lyxerr);
737 while (lex.isOK() && !quit) {
744 string const name = lex.getString();
746 getMenu(name).read(lex);
758 lex.printError("menubackend::read: "
759 "Unknown menu tag: `$$Token'");
767 void MenuBackend::add(Menu const & menu)
769 menulist_.push_back(menu);
773 bool MenuBackend::hasMenu(string const & name) const
775 return find_if(begin(), end(),
776 lyx::compare_memfun(&Menu::name, name)) != end();
780 Menu const & MenuBackend::getMenu(string const & name) const
782 const_iterator cit = find_if(begin(), end(),
783 lyx::compare_memfun(&Menu::name, name));
785 lyxerr << "No submenu named " << name << endl;
786 Assert(cit != end());
791 Menu & MenuBackend::getMenu(string const & name)
793 MenuList::iterator it =
794 find_if(menulist_.begin(), menulist_.end(),
795 lyx::compare_memfun(&Menu::name, name));
796 Assert(it != menulist_.end());
801 Menu const & MenuBackend::getMenubar() const