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;
50 // This is the global menu definition
51 MenuBackend menubackend;
54 MenuItem::MenuItem(Kind kind, string const & label,
55 string const & command, bool optional)
56 : kind_(kind), label_(label), optional_(optional)
72 action_ = lyxaction.LookupFunc(command);
74 if (action_ == LFUN_UNKNOWN_ACTION) {
75 lyxerr << "MenuItem(): LyX command `"
76 << command << "' does not exist." << endl;
79 lyxerr[Debug::GUI] << "Optional item "
83 submenuname_ = command;
89 MenuItem::MenuItem(Kind kind, string const & label, int action, bool optional)
90 : kind_(kind), label_(label), action_(action), submenuname_(),
99 void MenuItem::submenu(Menu * menu)
101 submenu_.reset(menu);
105 string const MenuItem::label() const
107 return token(label_, '|', 0);
111 string const MenuItem::shortcut() const
113 return token(label_, '|', 1);
116 string const MenuItem::binding() const
118 if (kind_ != Command)
121 // Get the keys bound to this action, but keep only the
123 string bindings = toplevel_keymap->findbinding(action_);
125 if (!bindings.empty()) {
126 return bindings.substr(1, bindings.find(']') - 1);
132 Menu & Menu::add(MenuItem const & i, LyXView const * view)
140 case MenuItem::Command:
143 view->getLyXFunc().getStatus(i.action());
145 || (status.disabled() && i.optional()))
148 items_.back().status(status);
151 case MenuItem::Submenu:
154 bool disabled = true;
155 for (const_iterator cit = i.submenu()->begin();
156 cit != i.submenu()->end(); ++cit) {
157 if ((cit->kind() == MenuItem::Command
158 || cit->kind() == MenuItem::Submenu)
159 && !cit->status().disabled()) {
164 if (!disabled || !i.optional()) {
166 items_.back().status().disabled(disabled);
173 case MenuItem::Separator:
175 && items_.back().kind() != MenuItem::Separator)
186 Menu & Menu::read(LyXLex & lex)
208 struct keyword_item menutags[md_last - 1] = {
209 { "documents", md_documents },
210 { "end", md_endmenu },
211 { "exportformats", md_exportformats },
212 { "floatinsert", md_floatinsert },
213 { "floatlistinsert", md_floatlistinsert },
214 { "importformats", md_importformats },
216 { "lastfiles", md_lastfiles },
217 { "optitem", md_optitem },
218 { "optsubmenu", md_optsubmenu },
219 { "pasterecent", md_pasterecent },
220 { "separator", md_separator },
221 { "submenu", md_submenu },
223 { "updateformats", md_updateformats },
224 { "viewformats", md_viewformats }
227 lex.pushTable(menutags, md_last - 1);
228 if (lyxerr.debugging(Debug::PARSER))
229 lex.printTable(lyxerr);
232 bool optional = false;
234 while (lex.isOK() && !quit) {
238 // fallback to md_item
241 string const name = _(lex.getString());
243 string const command = lex.getString();
244 add(MenuItem(MenuItem::Command, name,
251 add(MenuItem(MenuItem::Separator));
255 add(MenuItem(MenuItem::Lastfiles));
259 add(MenuItem(MenuItem::Documents));
263 add(MenuItem(MenuItem::Toc));
267 add(MenuItem(MenuItem::ViewFormats));
270 case md_updateformats:
271 add(MenuItem(MenuItem::UpdateFormats));
274 case md_exportformats:
275 add(MenuItem(MenuItem::ExportFormats));
278 case md_importformats:
279 add(MenuItem(MenuItem::ImportFormats));
282 case md_floatlistinsert:
283 add(MenuItem(MenuItem::FloatListInsert));
287 add(MenuItem(MenuItem::FloatInsert));
291 add(MenuItem(MenuItem::PasteRecent));
296 // fallback to md_submenu
299 string const mlabel = _(lex.getString());
301 string const mname = lex.getString();
302 add(MenuItem(MenuItem::Submenu, mlabel, mname,
313 lex.printError("Menu::read: "
314 "Unknown menu tag: `$$Token'");
323 void Menu::checkShortcuts() const
325 // This is a quadratic algorithm, but we do not care because
326 // it is used for debugging only.
327 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
328 string shortcut = it1->shortcut();
329 if (shortcut.empty())
331 if (!contains(it1->label(), shortcut))
332 lyxerr << "Menu warning: menu entry \""
334 << "\" does not contain shortcut `"
335 << shortcut << '\'' << endl;
336 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
337 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
338 lyxerr << "Menu warning: menu entries "
339 << '"' << it1->fulllabel()
340 << "\" and \"" << it2->fulllabel()
341 << "\" share the same shortcut."
351 class compare_format {
353 bool operator()(Format const * p1, Format const * p2) {
358 string const limit_string_length(string const & str)
360 string::size_type const max_item_length = 45;
362 if (str.size() > max_item_length)
363 return str.substr(0, max_item_length - 3) + "...";
369 void expandLastfiles(Menu & tomenu, LyXView const * view)
372 LastFiles::const_iterator lfit = lastfiles->begin();
373 LastFiles::const_iterator end = lastfiles->end();
375 for (; lfit != end && ii < 10; ++lfit, ++ii) {
376 string const label = tostr(ii) + ". "
377 + MakeDisplayPath((*lfit), 30)
379 int const action = lyxaction.
380 getPseudoAction(LFUN_FILE_OPEN,
382 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
386 void expandDocuments(Menu & tomenu, LyXView const * view)
388 typedef vector<string> Strings;
389 Strings const names = bufferlist.getFileNames();
392 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
393 LFUN_NOACTION), view);
398 Strings::const_iterator docit = names.begin();
399 Strings::const_iterator end = names.end();
400 for (; docit != end; ++docit, ++ii) {
402 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
403 string label = MakeDisplayPath(*docit, 20);
405 label = tostr(ii) + ". " + label + '|' + tostr(ii);
406 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
411 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
413 if (!view->buffer() && kind != MenuItem::ImportFormats) {
414 tomenu.add(MenuItem(MenuItem::Command,
415 _("No Documents Open!"), LFUN_NOACTION),
420 typedef vector<Format const *> Formats;
425 case MenuItem::ImportFormats:
426 formats = Importer::GetImportableFormats();
427 action = LFUN_IMPORT;
429 case MenuItem::ViewFormats:
430 formats = Exporter::GetExportableFormats(view->buffer(), true);
431 action = LFUN_PREVIEW;
433 case MenuItem::UpdateFormats:
434 formats = Exporter::GetExportableFormats(view->buffer(), true);
435 action = LFUN_UPDATE;
438 formats = Exporter::GetExportableFormats(view->buffer(), false);
439 action = LFUN_EXPORT;
441 sort(formats.begin(), formats.end(), compare_format());
443 Formats::const_iterator fit = formats.begin();
444 Formats::const_iterator end = formats.end();
445 for (; fit != end ; ++fit) {
448 string label = (*fit)->prettyname();
449 // we need to hide the default graphic export formats
450 // from the external menu, because we need them only
451 // for the internal lyx-view and external latex run
452 if (label == "EPS" || label == "XPM" || label == "PNG")
455 if (kind == MenuItem::ImportFormats) {
456 if ((*fit)->name() == "text")
457 label = _("ASCII text as lines");
458 else if ((*fit)->name() == "textparagraph")
459 label = _("ASCII text as paragraphs");
462 if (!(*fit)->shortcut().empty())
463 label += '|' + (*fit)->shortcut();
464 int const action2 = lyxaction.
465 getPseudoAction(action, (*fit)->name());
466 tomenu.add(MenuItem(MenuItem::Command, label, action2),
472 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
474 if (!view->buffer()) {
475 tomenu.add(MenuItem(MenuItem::Command,
476 _("No Documents Open!"), LFUN_NOACTION),
481 FloatList const & floats =
482 view->buffer()->params.getLyXTextClass().floats();
483 FloatList::const_iterator cit = floats.begin();
484 FloatList::const_iterator end = floats.end();
485 for (; cit != end; ++cit) {
486 int const action = lyxaction
487 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
488 tomenu.add(MenuItem(MenuItem::Command,
489 _(cit->second.listName()), action),
495 void expandFloatInsert(Menu & tomenu, LyXView const * view)
497 if (!view->buffer()) {
498 tomenu.add(MenuItem(MenuItem::Command,
499 _("No Documents Open!"), LFUN_NOACTION),
504 FloatList const & floats =
505 view->buffer()->params.getLyXTextClass().floats();
506 FloatList::const_iterator cit = floats.begin();
507 FloatList::const_iterator end = floats.end();
508 for (; cit != end; ++cit) {
511 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
513 string const label = _(cit->second.name());
514 tomenu.add(MenuItem(MenuItem::Command, label, action),
520 Menu::size_type const max_number_of_items = 25;
522 void expandToc2(Menu & tomenu, toc::Toc const & toc_list,
523 toc::Toc::size_type from, toc::Toc::size_type to, int depth)
525 int shortcut_count = 0;
526 if (to - from <= max_number_of_items) {
527 for (toc::Toc::size_type i = from; i < to; ++i) {
528 int const action = toc_list[i].action();
529 string label(4 * max(0, toc_list[i].depth - depth),' ');
530 label += limit_string_length(toc_list[i].str);
531 if (toc_list[i].depth == depth
532 && ++shortcut_count <= 9) {
533 label += '|' + tostr(shortcut_count);
535 tomenu.add(MenuItem(MenuItem::Command, label, action));
538 toc::Toc::size_type pos = from;
540 toc::Toc::size_type new_pos = pos + 1;
541 while (new_pos < to &&
542 toc_list[new_pos].depth > depth)
545 int const action = toc_list[pos].action();
546 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
547 label += limit_string_length(toc_list[pos].str);
548 if (toc_list[pos].depth == depth &&
549 ++shortcut_count <= 9)
550 label += '|' + tostr(shortcut_count);
552 if (new_pos == pos + 1) {
553 tomenu.add(MenuItem(MenuItem::Command,
556 MenuItem item(MenuItem::Submenu, label);
557 item.submenu(new Menu);
558 expandToc2(*item.submenu(),
559 toc_list, pos, new_pos, depth + 1);
568 void expandToc(Menu & tomenu, LyXView const * view)
570 // To make things very cleanly, we would have to pass view to
571 // all MenuItem constructors and to expandToc2. However, we
572 // know that all the entries in a TOC will be have status_ ==
573 // OK, so we avoid this unnecessary overhead (JMarc)
575 if (!view->buffer()) {
576 tomenu.add(MenuItem(MenuItem::Command,
577 _("No Documents Open!"), LFUN_NOACTION),
582 toc::TocList toc_list = toc::getTocList(view->buffer());
583 toc::TocList::const_iterator cit = toc_list.begin();
584 toc::TocList::const_iterator end = toc_list.end();
585 for (; cit != end; ++cit) {
587 if (cit->first == "TOC")
590 // All the rest is for floats
591 Menu * menu = new Menu;
592 toc::Toc::const_iterator ccit = cit->second.begin();
593 toc::Toc::const_iterator eend = cit->second.end();
594 for (; ccit != eend; ++ccit) {
595 string const label = limit_string_length(ccit->str);
596 menu->add(MenuItem(MenuItem::Command,
597 label, ccit->action()));
599 string const & floatName = cit->first;
600 // Is the _(...) really needed here? (Lgb)
601 MenuItem item(MenuItem::Submenu, _(floatName));
607 cit = toc_list.find("TOC");
609 tomenu.add(MenuItem(MenuItem::Command,
610 _("No Table of contents")),
613 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
618 void expandPasteRecent(Menu & tomenu, LyXView const * view)
620 vector<string> const selL =
621 CutAndPaste::availableSelections(*view->buffer());
623 vector<string>::const_iterator cit = selL.begin();
624 vector<string>::const_iterator end = selL.end();
626 for (unsigned int index = 0; cit != end; ++cit, ++index) {
627 int const action = lyxaction.getPseudoAction(LFUN_PASTE,
629 tomenu.add(MenuItem(MenuItem::Command, *cit, action));
637 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
638 LyXView const * view) const
640 for (Menu::const_iterator cit = frommenu.begin();
641 cit != frommenu.end() ; ++cit) {
642 switch (cit->kind()) {
643 case MenuItem::Lastfiles:
644 expandLastfiles(tomenu, view);
647 case MenuItem::Documents:
648 expandDocuments(tomenu, view);
651 case MenuItem::ImportFormats:
652 case MenuItem::ViewFormats:
653 case MenuItem::UpdateFormats:
654 case MenuItem::ExportFormats:
655 expandFormats(cit->kind(), tomenu, view);
658 case MenuItem::FloatListInsert:
659 expandFloatListInsert(tomenu, view);
662 case MenuItem::FloatInsert:
663 expandFloatInsert(tomenu, view);
666 case MenuItem::PasteRecent:
667 expandPasteRecent(tomenu, view);
671 expandToc(tomenu, view);
674 case MenuItem::Submenu: {
676 item.submenu(new Menu(cit->submenuname()));
677 expand(getMenu(cit->submenuname()),
678 *item.submenu(), view);
679 tomenu.add(item, view);
684 tomenu.add(*cit, view);
688 // we do not want the menu to end with a separator
690 && tomenu.items_.back().kind() == MenuItem::Separator)
691 tomenu.items_.pop_back();
693 // Check whether the shortcuts are unique
694 if (lyxerr.debugging(Debug::GUI))
695 tomenu.checkShortcuts();
699 bool Menu::hasSubmenu(string const & name) const
701 return find_if(begin(), end(),
702 lyx::compare_memfun(&MenuItem::submenuname,
707 void MenuBackend::read(LyXLex & lex)
716 struct keyword_item menutags[md_last - 1] = {
717 { "end", md_endmenuset },
719 { "menubar", md_menubar }
723 if (compare_ascii_no_case(lex.getString(), "menuset")) {
724 lyxerr << "Menubackend::read: ERROR wrong token:`"
725 << lex.getString() << '\'' << endl;
728 lex.pushTable(menutags, md_last - 1);
729 if (lyxerr.debugging(Debug::PARSER))
730 lex.printTable(lyxerr);
734 while (lex.isOK() && !quit) {
741 string const name = lex.getString();
743 getMenu(name).read(lex);
755 lex.printError("menubackend::read: "
756 "Unknown menu tag: `$$Token'");
764 void MenuBackend::add(Menu const & menu)
766 menulist_.push_back(menu);
770 bool MenuBackend::hasMenu(string const & name) const
772 return find_if(begin(), end(),
773 lyx::compare_memfun(&Menu::name, name)) != end();
777 Menu const & MenuBackend::getMenu(string const & name) const
779 const_iterator cit = find_if(begin(), end(),
780 lyx::compare_memfun(&Menu::name, name));
782 lyxerr << "No submenu named " << name << endl;
783 lyx::Assert(cit != end());
788 Menu & MenuBackend::getMenu(string const & name)
790 MenuList::iterator it =
791 find_if(menulist_.begin(), menulist_.end(),
792 lyx::compare_memfun(&Menu::name, name));
793 lyx::Assert(it != menulist_.end());
798 Menu const & MenuBackend::getMenubar() const