3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Asger Alstrup
8 * \author Lars Gullik Bjønnes
9 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * \author Martin Vermeer
14 * Full author contact details are available in file CREDITS.
22 #include "GuiApplication.h"
24 #include "qt_helpers.h"
26 #include "BiblioInfo.h"
27 #include "BranchList.h"
29 #include "BufferList.h"
30 #include "BufferParams.h"
31 #include "BufferView.h"
32 #include "Converter.h"
33 #include "CutAndPaste.h"
35 #include "FloatList.h"
37 #include "FuncRequest.h"
38 #include "FuncStatus.h"
39 #include "IndicesList.h"
43 #include "LyXAction.h"
46 #include "Paragraph.h"
47 #include "ParIterator.h"
49 #include "SpellChecker.h"
50 #include "TextClass.h"
51 #include "TocBackend.h"
53 #include "WordLangTuple.h"
55 #include "insets/Inset.h"
56 #include "insets/InsetCitation.h"
57 #include "insets/InsetGraphics.h"
59 #include "support/lassert.h"
60 #include "support/convert.h"
61 #include "support/debug.h"
62 #include "support/docstring_list.h"
63 #include "support/filetools.h"
64 #include "support/gettext.h"
65 #include "support/lstrings.h"
73 #include "support/shared_ptr.h"
79 using namespace lyx::support;
87 // MacOSX specific stuff is at the end.
94 /// The type of elements that can be in a menu
102 /** This type of item explains why something is unavailable. If this
103 menuitem is in a submenu, the submenu is enabled to make sure the
104 user sees the information. */
106 /** This type of item merely shows that there might be a list or
107 something alike at this position, but the list is still empty.
108 If this item is in a submenu, the submenu will not always be
111 /** This is the list of last opened file,
112 typically for the File menu. */
114 /** This is the list of opened Documents,
115 typically for the Documents menu. */
117 /** This is the bookmarks */
121 /** This is a list of viewable formats
122 typically for the File->View menu. */
124 /** This is a list of updatable formats
125 typically for the File->Update menu. */
127 /** This is a list of exportable formats
128 typically for the File->Export menu. */
130 /** This is a list of importable formats
131 typically for the File->Export menu. */
133 /** This is the list of elements available
134 * for insertion into document. */
136 /** This is the list of user-configurable
137 insets to insert into document */
139 /** This is the list of XML elements to
140 insert into the document */
142 /** This is the list of floats that we can
143 insert a list for. */
145 /** This is the list of floats that we can
148 /** This is the list of selections that can
153 /** Available branches in document */
155 /** Available indices in document */
157 /** Context menu for indices in document */
159 /** Available index lists in document */
161 /** Context menu for available indices lists in document */
163 /** Available citation styles for a given citation */
165 /** Available graphics groups */
167 /// Words suggested by the spellchecker.
169 /** Used Languages */
173 explicit MenuItem(Kind kind) : kind_(kind), optional_(false) {}
176 QString const & label,
177 QString const & submenu = QString(),
178 bool optional = false)
179 : kind_(kind), label_(label), submenuname_(submenu), optional_(optional)
181 LASSERT(kind == Submenu || kind == Help || kind == Info, /**/);
185 QString const & label,
186 FuncRequest const & func,
187 bool optional = false,
188 FuncRequest::Origin origin = FuncRequest::MENU)
189 : kind_(kind), label_(label), func_(func), optional_(optional)
191 func_.setOrigin(origin);
194 // shared_ptr<MenuDefinition> needs this apprently...
197 /// The label of a given menuitem
198 QString label() const
200 int const index = label_.lastIndexOf('|');
201 return index == -1 ? label_ : label_.left(index);
204 /// The keyboard shortcut (usually underlined in the entry)
205 QString shortcut() const
207 int const index = label_.lastIndexOf('|');
208 return index == -1 ? QString() : label_.mid(index + 1);
210 /// The complete label, with label and shortcut separated by a '|'
211 QString fulllabel() const { return label_;}
212 /// The kind of entry
213 Kind kind() const { return kind_; }
214 /// the action (if relevant)
215 FuncRequest const & func() const { return func_; }
216 /// returns true if the entry should be omitted when disabled
217 bool optional() const { return optional_; }
218 /// returns the status of the lfun associated with this entry
219 FuncStatus const & status() const { return status_; }
220 /// returns the status of the lfun associated with this entry
221 FuncStatus & status() { return status_; }
222 /// returns the status of the lfun associated with this entry
223 void status(FuncStatus const & status) { status_ = status; }
225 ///returns the binding associated to this action.
226 QString binding() const
228 if (kind_ != Command)
230 // Get the keys bound to this action, but keep only the
232 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func_);
234 return toqstr(bindings.begin()->print(KeySequence::ForGui));
236 LYXERR(Debug::KBMAP, "No binding for "
237 << lyxaction.getActionName(func_.action())
238 << '(' << func_.argument() << ')');
242 /// the description of the submenu (if relevant)
243 QString const & submenuname() const { return submenuname_; }
244 /// set the description of the submenu
245 void submenuname(QString const & name) { submenuname_ = name; }
247 bool hasSubmenu() const { return !submenu_.isEmpty(); }
249 MenuDefinition const & submenu() const { return submenu_.at(0); }
250 MenuDefinition & submenu() { return submenu_[0]; }
252 void setSubmenu(MenuDefinition const & menu)
255 submenu_.append(menu);
266 QString submenuname_;
271 /// contains 0 or 1 item.
272 QList<MenuDefinition> submenu_;
276 class MenuDefinition {
279 typedef std::vector<MenuItem> ItemList;
281 typedef ItemList::const_iterator const_iterator;
283 explicit MenuDefinition(QString const & name = QString()) : name_(name) {}
288 QString const & name() const { return name_; }
290 bool empty() const { return items_.empty(); }
291 /// Clear the menu content.
292 void clear() { items_.clear(); }
294 size_t size() const { return items_.size(); }
296 MenuItem const & operator[](size_t) const;
298 const_iterator begin() const { return items_.begin(); }
300 const_iterator end() const { return items_.end(); }
302 // search for func in this menu iteratively, and put menu
304 bool searchMenu(FuncRequest const & func, docstring_list & names)
307 bool hasFunc(FuncRequest const &) const;
308 /// Add the menu item unconditionally
309 void add(MenuItem const & item) { items_.push_back(item); }
310 /// Checks the associated FuncRequest status before adding the
312 void addWithStatusCheck(MenuItem const &);
313 // Check whether the menu shortcuts are unique
314 void checkShortcuts() const;
316 void expandLastfiles();
317 void expandDocuments();
318 void expandBookmarks();
319 void expandFormats(MenuItem::Kind kind, Buffer const * buf);
320 void expandFloatListInsert(Buffer const * buf);
321 void expandFloatInsert(Buffer const * buf);
322 void expandFlexInsert(Buffer const * buf, InsetLayout::InsetLyXType type);
323 void expandToc2(Toc const & toc_list, size_t from, size_t to, int depth);
324 void expandToc(Buffer const * buf);
325 void expandPasteRecent(Buffer const * buf);
326 void expandToolbars();
327 void expandBranches(Buffer const * buf);
328 void expandIndices(Buffer const * buf, bool listof = false);
329 void expandIndicesContext(Buffer const * buf, bool listof = false);
330 void expandCiteStyles(BufferView const *);
331 void expandGraphicsGroups(BufferView const *);
332 void expandSpellingSuggestions(BufferView const *);
333 void expandLanguageSelector(Buffer const * buf);
341 /// Helper for std::find_if
345 MenuNamesEqual(QString const & name) : name_(name) {}
346 bool operator()(MenuDefinition const & menu) const { return menu.name() == name_; }
353 typedef std::vector<MenuDefinition> MenuList;
355 typedef MenuList::const_iterator const_iterator;
357 typedef MenuList::iterator iterator;
359 /////////////////////////////////////////////////////////////////////
360 // MenuDefinition implementation
361 /////////////////////////////////////////////////////////////////////
363 void MenuDefinition::addWithStatusCheck(MenuItem const & i)
367 case MenuItem::Command: {
368 FuncStatus status = lyx::getStatus(i.func());
369 if (status.unknown() || (!status.enabled() && i.optional()))
372 items_.back().status(status);
376 case MenuItem::Submenu: {
377 bool enabled = false;
378 if (i.hasSubmenu()) {
379 for (const_iterator cit = i.submenu().begin();
380 cit != i.submenu().end(); ++cit) {
381 // Only these kind of items affect the status of the submenu
382 if ((cit->kind() == MenuItem::Command
383 || cit->kind() == MenuItem::Submenu
384 || cit->kind() == MenuItem::Help)
385 && cit->status().enabled()) {
391 if (enabled || !i.optional()) {
393 items_.back().status().setEnabled(enabled);
398 case MenuItem::Separator:
399 if (!items_.empty() && items_.back().kind() != MenuItem::Separator)
409 void MenuDefinition::read(Lexer & lex)
426 md_indiceslistscontext,
440 md_spellingsuggestions,
444 LexerKeyword menutags[] = {
445 { "bookmarks", md_bookmarks },
446 { "branches", md_branches },
447 { "charstyles", md_charstyles },
448 { "citestyles", md_citestyles },
449 { "custom", md_custom },
450 { "documents", md_documents },
451 { "elements", md_elements },
452 { "end", md_endmenu },
453 { "exportformats", md_exportformats },
454 { "floatinsert", md_floatinsert },
455 { "floatlistinsert", md_floatlistinsert },
456 { "graphicsgroups", md_graphicsgroups },
457 { "importformats", md_importformats },
458 { "indices", md_indices },
459 { "indicescontext", md_indicescontext },
460 { "indiceslists", md_indiceslists },
461 { "indiceslistscontext", md_indiceslistscontext },
463 { "languageselector", md_languageselector },
464 { "lastfiles", md_lastfiles },
465 { "optitem", md_optitem },
466 { "optsubmenu", md_optsubmenu },
467 { "pasterecent", md_pasterecent },
468 { "separator", md_separator },
469 { "spellingsuggestions", md_spellingsuggestions },
470 { "submenu", md_submenu },
472 { "toolbars", md_toolbars },
473 { "updateformats", md_updateformats },
474 { "viewformats", md_viewformats }
477 lex.pushTable(menutags);
478 lex.setContext("MenuDefinition::read: ");
481 bool optional = false;
483 while (lex.isOK() && !quit) {
487 // fallback to md_item
490 docstring const name = translateIfPossible(lex.getDocString());
492 string const command = lex.getString();
493 FuncRequest func = lyxaction.lookupFunc(command);
494 FuncRequest::Origin origin = FuncRequest::MENU;
495 if (name_.startsWith("context-toc-"))
496 origin = FuncRequest::TOC;
497 add(MenuItem(MenuItem::Command, toqstr(name), func, optional, origin));
503 add(MenuItem(MenuItem::Separator));
507 add(MenuItem(MenuItem::Lastfiles));
511 add(MenuItem(MenuItem::CharStyles));
515 add(MenuItem(MenuItem::Custom));
519 add(MenuItem(MenuItem::Elements));
523 add(MenuItem(MenuItem::Documents));
527 add(MenuItem(MenuItem::Bookmarks));
531 add(MenuItem(MenuItem::Toc));
535 add(MenuItem(MenuItem::ViewFormats));
538 case md_updateformats:
539 add(MenuItem(MenuItem::UpdateFormats));
542 case md_exportformats:
543 add(MenuItem(MenuItem::ExportFormats));
546 case md_importformats:
547 add(MenuItem(MenuItem::ImportFormats));
550 case md_floatlistinsert:
551 add(MenuItem(MenuItem::FloatListInsert));
555 add(MenuItem(MenuItem::FloatInsert));
559 add(MenuItem(MenuItem::PasteRecent));
563 add(MenuItem(MenuItem::Toolbars));
567 add(MenuItem(MenuItem::Branches));
571 add(MenuItem(MenuItem::CiteStyles));
574 case md_graphicsgroups:
575 add(MenuItem(MenuItem::GraphicsGroups));
578 case md_spellingsuggestions:
579 add(MenuItem(MenuItem::SpellingSuggestions));
582 case md_languageselector:
583 add(MenuItem(MenuItem::LanguageSelector));
587 add(MenuItem(MenuItem::Indices));
590 case md_indicescontext:
591 add(MenuItem(MenuItem::IndicesContext));
594 case md_indiceslists:
595 add(MenuItem(MenuItem::IndicesLists));
598 case md_indiceslistscontext:
599 add(MenuItem(MenuItem::IndicesListsContext));
604 // fallback to md_submenu
607 docstring const mlabel = translateIfPossible(lex.getDocString());
609 docstring const mname = lex.getDocString();
610 add(MenuItem(MenuItem::Submenu,
611 toqstr(mlabel), toqstr(mname), optional));
621 lex.printError("Unknown menu tag");
629 MenuItem const & MenuDefinition::operator[](size_type i) const
635 bool MenuDefinition::hasFunc(FuncRequest const & func) const
637 for (const_iterator it = begin(), et = end(); it != et; ++it)
638 if (it->func() == func)
644 void MenuDefinition::checkShortcuts() const
646 // This is a quadratic algorithm, but we do not care because
647 // menus are short enough
648 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
649 QString shortcut = it1->shortcut();
650 if (shortcut.isEmpty())
652 if (!it1->label().contains(shortcut))
653 LYXERR0("Menu warning: menu entry \""
655 << "\" does not contain shortcut `"
656 << shortcut << "'.");
657 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
658 if (!it2->shortcut().compare(shortcut, Qt::CaseInsensitive)) {
659 LYXERR0("Menu warning: menu entries "
660 << '"' << it1->fulllabel()
661 << "\" and \"" << it2->fulllabel()
662 << "\" share the same shortcut.");
669 bool MenuDefinition::searchMenu(FuncRequest const & func, docstring_list & names) const
671 const_iterator m = begin();
672 const_iterator m_end = end();
673 for (; m != m_end; ++m) {
674 if (m->kind() == MenuItem::Command && m->func() == func) {
675 names.push_back(qstring_to_ucs4(m->label()));
678 if (m->kind() == MenuItem::Submenu) {
679 names.push_back(qstring_to_ucs4(m->label()));
680 if (!m->hasSubmenu()) {
681 LYXERR(Debug::GUI, "Warning: non existing sub menu label="
682 << m->label() << " name=" << m->submenuname());
686 if (m->submenu().searchMenu(func, names))
695 bool compareFormat(Format const * p1, Format const * p2)
701 QString limitStringLength(docstring const & str)
703 size_t const max_item_length = 45;
705 if (str.size() > max_item_length)
706 return toqstr(str.substr(0, max_item_length - 3) + "...");
712 void MenuDefinition::expandGraphicsGroups(BufferView const * bv)
717 graphics::getGraphicsGroups(bv->buffer(), grp);
721 set<string>::const_iterator it = grp.begin();
722 set<string>::const_iterator end = grp.end();
723 add(MenuItem(MenuItem::Command, qt_("No Group"),
724 FuncRequest(LFUN_SET_GRAPHICS_GROUP)));
725 for (; it != end; it++) {
726 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(*it) + '|',
727 FuncRequest(LFUN_SET_GRAPHICS_GROUP, *it)));
732 void MenuDefinition::expandSpellingSuggestions(BufferView const * bv)
737 docstring_list suggestions;
738 pos_type from = bv->cursor().pos();
740 Paragraph const & par = bv->cursor().paragraph();
741 SpellChecker::Result res = par.spellCheck(from, to, wl, suggestions, true, true);
743 case SpellChecker::UNKNOWN_WORD:
744 if (lyxrc.spellcheck_continuously) {
745 LYXERR(Debug::GUI, "Misspelled Word! Suggested Words = ");
747 MenuItem item(MenuItem::Submenu, qt_("More Spelling Suggestions"));
748 item.setSubmenu(MenuDefinition(qt_("More Spelling Suggestions")));
749 for (; i != suggestions.size(); ++i) {
750 docstring const & suggestion = suggestions[i];
751 LYXERR(Debug::GUI, suggestion);
752 MenuItem w(MenuItem::Command, toqstr(suggestion),
753 FuncRequest(LFUN_WORD_REPLACE, suggestion));
757 item.submenu().add(w);
762 add(MenuItem(MenuItem::Separator));
763 docstring const arg = wl.word() + " " + from_ascii(wl.lang()->lang());
764 add(MenuItem(MenuItem::Command, qt_("Add to personal dictionary|n"),
765 FuncRequest(LFUN_SPELLING_ADD, arg)));
766 add(MenuItem(MenuItem::Command, qt_("Ignore all|I"),
767 FuncRequest(LFUN_SPELLING_IGNORE, arg)));
770 case SpellChecker::LEARNED_WORD: {
771 LYXERR(Debug::GUI, "Learned Word.");
772 docstring const arg = wl.word() + " " + from_ascii(wl.lang()->lang());
773 add(MenuItem(MenuItem::Command, qt_("Remove from personal dictionary|r"),
774 FuncRequest(LFUN_SPELLING_REMOVE, arg)));
777 case SpellChecker::WORD_OK:
778 case SpellChecker::COMPOUND_WORD:
779 case SpellChecker::ROOT_FOUND:
780 case SpellChecker::IGNORED_WORD:
785 struct sortLanguageByName {
786 bool operator()(const Language * a, const Language * b) const {
787 return qt_(a->display()).localeAwareCompare(qt_(b->display())) < 0;
791 void MenuDefinition::expandLanguageSelector(Buffer const * buf)
796 std::set<Language const *> languages_buffer =
797 buf->masterBuffer()->getLanguages();
799 if (languages_buffer.size() < 2)
802 std::set<Language const *, sortLanguageByName> languages;
804 std::set<Language const *>::const_iterator const beg =
805 languages_buffer.begin();
806 for (std::set<Language const *>::const_iterator cit = beg;
807 cit != languages_buffer.end(); ++cit) {
808 languages.insert(*cit);
811 MenuItem item(MenuItem::Submenu, qt_("Language|L"));
812 item.setSubmenu(MenuDefinition(qt_("Language")));
813 QString morelangs = qt_("More Languages ...|M");
814 QStringList accelerators;
815 if (morelangs.contains('|'))
816 accelerators.append(morelangs.section('|', -1));
817 std::set<Language const *, sortLanguageByName>::const_iterator const begin = languages.begin();
818 for (std::set<Language const *, sortLanguageByName>::const_iterator cit = begin;
819 cit != languages.end(); ++cit) {
820 QString label = qt_((*cit)->display());
821 // try to add an accelerator
822 bool success = false;
823 // try capitals first
824 for (int i = 0; i < label.size(); ++i) {
825 QChar const ch = label[i];
828 if (!accelerators.contains(ch, Qt::CaseInsensitive)) {
829 label = label + toqstr("|") + ch;
830 accelerators.append(ch);
835 // if all capitals are taken, try the rest
837 for (int i = 0; i < label.size(); ++i) {
838 if (label[i].isSpace())
840 QString const ch = QString(label[i]);
841 if (!accelerators.contains(ch, Qt::CaseInsensitive)) {
842 label = label + toqstr("|") + ch;
843 accelerators.append(ch);
848 MenuItem w(MenuItem::Command, label,
849 FuncRequest(LFUN_LANGUAGE, (*cit)->lang()));
850 item.submenu().addWithStatusCheck(w);
852 item.submenu().add(MenuItem(MenuItem::Separator));
853 item.submenu().add(MenuItem(MenuItem::Command, morelangs,
854 FuncRequest(LFUN_DIALOG_SHOW, "character")));
859 void MenuDefinition::expandLastfiles()
861 LastFilesSection::LastFiles const & lf = theSession().lastFiles().lastFiles();
862 LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
866 for (; lfit != lf.end() && ii <= lyxrc.num_lastfiles; ++lfit, ++ii) {
867 string const file = lfit->absFileName();
870 label = QString("%1. %2|%3").arg(ii)
871 .arg(toqstr(makeDisplayPath(file, 30))).arg(ii);
873 label = QString("%1. %2").arg(ii)
874 .arg(toqstr(makeDisplayPath(file, 30)));
875 add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, file)));
880 void MenuDefinition::expandDocuments()
882 MenuItem item(MenuItem::Submenu, qt_("Hidden|H"));
883 item.setSubmenu(MenuDefinition(qt_("Hidden|H")));
885 Buffer * first = theBufferList().first();
891 // We cannot use a for loop as the buffer list cycles.
893 QString label = toqstr(b->fileName().displayName(20));
896 bool const shown = guiApp->currentView()
897 ? guiApp->currentView()->workArea(*b) : false;
898 int ii = shown ? vis : invis;
900 label = QString::number(ii) + ". " + label + '|' + QString::number(ii);
902 add(MenuItem(MenuItem::Command, label,
903 FuncRequest(LFUN_BUFFER_SWITCH, b->absFileName())));
906 item.submenu().add(MenuItem(MenuItem::Command, label,
907 FuncRequest(LFUN_BUFFER_SWITCH, b->absFileName())));
910 b = theBufferList().next(b);
911 } while (b != first);
912 if (!item.submenu().empty())
915 add(MenuItem(MenuItem::Info, qt_("<No Documents Open>")));
919 void MenuDefinition::expandBookmarks()
921 lyx::BookmarksSection const & bm = theSession().bookmarks();
924 for (size_t i = 1; i <= bm.size(); ++i) {
926 string const file = bm.bookmark(i).filename.absFileName();
927 QString const label = QString("%1. %2|%3").arg(i)
928 .arg(toqstr(makeDisplayPath(file, 20))).arg(i);
929 add(MenuItem(MenuItem::Command, label,
930 FuncRequest(LFUN_BOOKMARK_GOTO, convert<docstring>(i))));
935 add(MenuItem(MenuItem::Info, qt_("<No Bookmarks Saved Yet>")));
939 void MenuDefinition::expandFormats(MenuItem::Kind kind, Buffer const * buf)
941 if (!buf && kind != MenuItem::ImportFormats)
944 typedef vector<Format const *> Formats;
949 case MenuItem::ImportFormats:
950 formats = theConverters().importableFormats();
951 action = LFUN_BUFFER_IMPORT;
953 case MenuItem::ViewFormats:
954 formats = buf->exportableFormats(true);
955 action = LFUN_BUFFER_VIEW;
957 case MenuItem::UpdateFormats:
958 formats = buf->exportableFormats(true);
959 action = LFUN_BUFFER_UPDATE;
962 formats = buf->exportableFormats(false);
963 action = LFUN_BUFFER_EXPORT;
965 sort(formats.begin(), formats.end(), &compareFormat);
967 bool const view_update = (kind == MenuItem::ViewFormats
968 || kind == MenuItem::UpdateFormats);
972 smenue = (kind == MenuItem::ViewFormats ?
973 qt_("View (Other Formats)|F")
974 : qt_("Update (Other Formats)|p"));
975 MenuItem item(MenuItem::Submenu, smenue);
976 item.setSubmenu(MenuDefinition(smenue));
978 Formats::const_iterator fit = formats.begin();
979 Formats::const_iterator end = formats.end();
980 for (; fit != end ; ++fit) {
984 docstring lab = from_utf8((*fit)->prettyname());
985 docstring scut = from_utf8((*fit)->shortcut());
986 docstring const tmplab = lab;
989 lab += char_type('|') + scut;
990 docstring lab_i18n = translateIfPossible(lab);
991 bool const untranslated = (lab == lab_i18n);
992 QString const shortcut = toqstr(split(lab_i18n, lab, '|'));
993 QString label = toqstr(lab);
995 // this might happen if the shortcut
996 // has been redefined
997 label = toqstr(translateIfPossible(tmplab));
1000 case MenuItem::ImportFormats:
1003 case MenuItem::ViewFormats:
1004 case MenuItem::UpdateFormats:
1005 if ((*fit)->name() == buf->getDefaultOutputFormat()) {
1006 docstring lbl = (kind == MenuItem::ViewFormats ?
1007 bformat(_("View [%1$s]|V"), qstring_to_ucs4(label))
1008 : bformat(_("Update [%1$s]|U"), qstring_to_ucs4(label)));
1009 MenuItem w(MenuItem::Command, toqstr(lbl),
1010 FuncRequest(action, (*fit)->name()));
1014 case MenuItem::ExportFormats:
1015 if (!(*fit)->documentFormat())
1019 LASSERT(false, /**/);
1022 if (!shortcut.isEmpty())
1023 label += '|' + shortcut;
1027 item.submenu().addWithStatusCheck(MenuItem(MenuItem::Command, label,
1028 FuncRequest(action, (*fit)->name())));
1030 item.submenu().add(MenuItem(MenuItem::Command, label,
1031 FuncRequest(action, (*fit)->name())));
1034 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1035 FuncRequest(action, (*fit)->name())));
1037 add(MenuItem(MenuItem::Command, label,
1038 FuncRequest(action, (*fit)->name())));
1046 void MenuDefinition::expandFloatListInsert(Buffer const * buf)
1051 FloatList const & floats = buf->params().documentClass().floats();
1052 FloatList::const_iterator cit = floats.begin();
1053 FloatList::const_iterator end = floats.end();
1054 for (; cit != end; ++cit) {
1055 addWithStatusCheck(MenuItem(MenuItem::Command,
1056 qt_(cit->second.listName()),
1057 FuncRequest(LFUN_FLOAT_LIST_INSERT,
1058 cit->second.floattype())));
1063 void MenuDefinition::expandFloatInsert(Buffer const * buf)
1068 FloatList const & floats = buf->params().documentClass().floats();
1069 FloatList::const_iterator cit = floats.begin();
1070 FloatList::const_iterator end = floats.end();
1071 for (; cit != end; ++cit) {
1073 QString const label = qt_(cit->second.name());
1074 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1075 FuncRequest(LFUN_FLOAT_INSERT,
1076 cit->second.floattype())));
1081 void MenuDefinition::expandFlexInsert(
1082 Buffer const * buf, InsetLayout::InsetLyXType type)
1087 TextClass::InsetLayouts const & insetLayouts =
1088 buf->params().documentClass().insetLayouts();
1089 TextClass::InsetLayouts::const_iterator cit = insetLayouts.begin();
1090 TextClass::InsetLayouts::const_iterator end = insetLayouts.end();
1091 for (; cit != end; ++cit) {
1092 if (cit->second.lyxtype() == type) {
1093 docstring label = cit->first;
1094 // we remove the "Flex:" prefix, if it is present
1095 if (prefixIs(label, from_utf8("Flex:")))
1096 label = label.substr(5);
1097 addWithStatusCheck(MenuItem(MenuItem::Command,
1098 toqstr(translateIfPossible(label)),
1099 FuncRequest(LFUN_FLEX_INSERT, Lexer::quoteString(label))));
1102 // FIXME This is a little clunky.
1103 if (items_.empty() && type == InsetLayout::CUSTOM)
1104 add(MenuItem(MenuItem::Help, qt_("No Custom Insets Defined!")));
1108 size_t const max_number_of_items = 25;
1110 void MenuDefinition::expandToc2(Toc const & toc_list,
1111 size_t from, size_t to, int depth)
1113 int shortcut_count = 0;
1115 // check whether depth is smaller than the smallest depth in toc.
1116 int min_depth = 1000;
1117 for (size_t i = from; i < to; ++i)
1118 min_depth = min(min_depth, toc_list[i].depth());
1119 if (min_depth > depth)
1122 if (to - from <= max_number_of_items) {
1123 for (size_t i = from; i < to; ++i) {
1124 QString label(4 * max(0, toc_list[i].depth() - depth), ' ');
1125 label += limitStringLength(toc_list[i].str());
1126 if (toc_list[i].depth() == depth) {
1128 if (shortcut_count < 9) {
1129 if (label.contains(QString::number(shortcut_count + 1)))
1130 label += QString::number(++shortcut_count);
1133 add(MenuItem(MenuItem::Command, label,
1134 FuncRequest(toc_list[i].action())));
1139 size_t new_pos = pos + 1;
1140 while (new_pos < to && toc_list[new_pos].depth() > depth)
1143 QString label(4 * max(0, toc_list[pos].depth() - depth), ' ');
1144 label += limitStringLength(toc_list[pos].str());
1145 if (toc_list[pos].depth() == depth) {
1147 if (shortcut_count < 9) {
1148 if (label.contains(QString::number(shortcut_count + 1)))
1149 label += QString::number(++shortcut_count);
1152 if (new_pos == pos + 1) {
1153 add(MenuItem(MenuItem::Command,
1154 label, FuncRequest(toc_list[pos].action())));
1157 sub.expandToc2(toc_list, pos, new_pos, depth + 1);
1158 MenuItem item(MenuItem::Submenu, label);
1159 item.setSubmenu(sub);
1168 void MenuDefinition::expandToc(Buffer const * buf)
1170 // To make things very cleanly, we would have to pass buf to
1171 // all MenuItem constructors and to expandToc2. However, we
1172 // know that all the entries in a TOC will be have status_ ==
1173 // OK, so we avoid this unnecessary overhead (JMarc)
1176 add(MenuItem(MenuItem::Info, qt_("<No Document Open>")));
1180 // Add an entry for the master doc if this is a child doc
1181 Buffer const * const master = buf->masterBuffer();
1182 if (buf != master) {
1183 ParIterator const pit = par_iterator_begin(master->inset());
1184 string const arg = convert<string>(pit->id());
1185 FuncRequest f(LFUN_PARAGRAPH_GOTO, arg);
1186 add(MenuItem(MenuItem::Command, qt_("Master Document"), f));
1189 MenuDefinition other_lists;
1191 FloatList const & floatlist = buf->params().documentClass().floats();
1192 TocList const & toc_list = buf->tocBackend().tocs();
1193 TocList::const_iterator cit = toc_list.begin();
1194 TocList::const_iterator end = toc_list.end();
1195 for (; cit != end; ++cit) {
1196 // Handle this later
1197 if (cit->first == "tableofcontents")
1200 MenuDefinition submenu;
1201 if (cit->second.size() >= 30) {
1202 FuncRequest f(LFUN_DIALOG_SHOW, "toc " + cit->first);
1203 submenu.add(MenuItem(MenuItem::Command, qt_("Open Navigator..."), f));
1205 TocIterator ccit = cit->second.begin();
1206 TocIterator eend = cit->second.end();
1207 for (; ccit != eend; ++ccit) {
1208 submenu.add(MenuItem(MenuItem::Command,
1209 limitStringLength(ccit->str()) + '|',
1210 FuncRequest(ccit->action())));
1214 MenuItem item(MenuItem::Submenu, guiName(cit->first, buf->params()));
1215 item.setSubmenu(submenu);
1216 if (floatlist.typeExist(cit->first) || cit->first == "child") {
1217 // Those two types deserve to be in the main menu.
1218 item.setSubmenu(submenu);
1221 other_lists.add(item);
1223 if (!other_lists.empty()) {
1224 MenuItem item(MenuItem::Submenu, qt_("Other Lists"));
1225 item.setSubmenu(other_lists);
1229 // Handle normal TOC
1230 cit = toc_list.find("tableofcontents");
1232 LYXERR(Debug::GUI, "No table of contents.");
1234 if (cit->second.size() > 0 )
1235 expandToc2(cit->second, 0, cit->second.size(), 0);
1237 add(MenuItem(MenuItem::Info, qt_("<Empty Table of Contents>")));
1242 void MenuDefinition::expandPasteRecent(Buffer const * buf)
1244 docstring_list const sel = cap::availableSelections(buf);
1246 docstring_list::const_iterator cit = sel.begin();
1247 docstring_list::const_iterator end = sel.end();
1249 for (unsigned int index = 0; cit != end; ++cit, ++index) {
1250 add(MenuItem(MenuItem::Command, toqstr(*cit) + '|',
1251 FuncRequest(LFUN_PASTE, convert<string>(index))));
1256 void MenuDefinition::expandToolbars()
1258 MenuDefinition other_lists;
1259 // extracts the toolbars from the backend
1260 Toolbars::Infos::const_iterator cit = guiApp->toolbars().begin();
1261 Toolbars::Infos::const_iterator end = guiApp->toolbars().end();
1262 for (; cit != end; ++cit) {
1263 MenuItem const item(MenuItem::Command, toqstr(cit->gui_name),
1264 FuncRequest(LFUN_TOOLBAR_TOGGLE, cit->name));
1265 if (guiApp->toolbars().isMainToolbar(cit->name))
1268 other_lists.add(item);
1271 if (!other_lists.empty()) {
1272 MenuItem item(MenuItem::Submenu, qt_("Other Toolbars"));
1273 item.setSubmenu(other_lists);
1279 void MenuDefinition::expandBranches(Buffer const * buf)
1284 BufferParams const & master_params = buf->masterBuffer()->params();
1285 BufferParams const & params = buf->params();
1286 if (params.branchlist().empty() && master_params.branchlist().empty() ) {
1287 add(MenuItem(MenuItem::Help, qt_("No Branches Set for Document!")));
1291 BranchList::const_iterator cit = master_params.branchlist().begin();
1292 BranchList::const_iterator end = master_params.branchlist().end();
1294 for (int ii = 1; cit != end; ++cit, ++ii) {
1295 docstring label = cit->branch();
1297 label = convert<docstring>(ii) + ". " + label
1298 + char_type('|') + convert<docstring>(ii);
1300 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1301 FuncRequest(LFUN_BRANCH_INSERT,
1305 if (buf == buf->masterBuffer())
1308 MenuDefinition child_branches;
1310 BranchList::const_iterator ccit = params.branchlist().begin();
1311 BranchList::const_iterator cend = params.branchlist().end();
1313 for (int ii = 1; ccit != cend; ++ccit, ++ii) {
1314 docstring label = ccit->branch();
1316 label = convert<docstring>(ii) + ". " + label
1317 + char_type('|') + convert<docstring>(ii);
1319 label += char_type('|');
1320 child_branches.addWithStatusCheck(MenuItem(MenuItem::Command,
1322 FuncRequest(LFUN_BRANCH_INSERT,
1326 if (!child_branches.empty()) {
1327 MenuItem item(MenuItem::Submenu, qt_("Child Document"));
1328 item.setSubmenu(child_branches);
1334 void MenuDefinition::expandIndices(Buffer const * buf, bool listof)
1339 BufferParams const & params = buf->masterBuffer()->params();
1340 if (!params.use_indices) {
1342 addWithStatusCheck(MenuItem(MenuItem::Command,
1343 qt_("Index List|I"),
1344 FuncRequest(LFUN_INDEX_PRINT,
1345 from_ascii("idx"))));
1347 addWithStatusCheck(MenuItem(MenuItem::Command,
1348 qt_("Index Entry|d"),
1349 FuncRequest(LFUN_INDEX_INSERT,
1350 from_ascii("idx"))));
1354 if (params.indiceslist().empty())
1357 IndicesList::const_iterator cit = params.indiceslist().begin();
1358 IndicesList::const_iterator end = params.indiceslist().end();
1360 for (int ii = 1; cit != end; ++cit, ++ii) {
1362 docstring const label =
1363 bformat(_("Index: %1$s"), cit->index());
1364 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1365 FuncRequest(LFUN_INDEX_PRINT, cit->shortcut())));
1367 docstring const label =
1368 bformat(_("Index Entry (%1$s)"), cit->index());
1369 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1370 FuncRequest(LFUN_INDEX_INSERT, cit->shortcut())));
1376 void MenuDefinition::expandIndicesContext(Buffer const * buf, bool listof)
1381 BufferParams const & params = buf->masterBuffer()->params();
1382 if (!params.use_indices || params.indiceslist().empty())
1385 IndicesList::const_iterator cit = params.indiceslist().begin();
1386 IndicesList::const_iterator end = params.indiceslist().end();
1388 for (int ii = 1; cit != end; ++cit, ++ii) {
1390 InsetCommandParams p(INDEX_PRINT_CODE);
1391 p["type"] = cit->shortcut();
1392 string const data = InsetCommand::params2string(p);
1393 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(cit->index()),
1394 FuncRequest(LFUN_INSET_MODIFY, data)));
1396 docstring const label =
1397 bformat(_("Index Entry (%1$s)"), cit->index());
1398 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1399 FuncRequest(LFUN_INSET_MODIFY,
1400 from_ascii("changetype ") + cit->shortcut())));
1406 void MenuDefinition::expandCiteStyles(BufferView const * bv)
1411 Inset const * inset = bv->cursor().nextInset();
1412 if (!inset || inset->lyxCode() != CITE_CODE) {
1413 add(MenuItem(MenuItem::Command,
1414 qt_("No Citation in Scope!"),
1415 FuncRequest(LFUN_NOACTION)));
1418 InsetCommand const * citinset =
1419 static_cast<InsetCommand const *>(inset);
1421 Buffer const * buf = &bv->buffer();
1422 docstring key = citinset->getParam("key");
1423 // we can only handle one key currently
1424 if (contains(key, ','))
1425 key = qstring_to_ucs4(toqstr(key).split(',')[0]);
1427 vector<CiteStyle> citeStyleList = citeStyles(buf->params().citeEngine());
1428 docstring_list citeStrings =
1429 buf->masterBibInfo().getCiteStrings(key, bv->buffer());
1431 docstring_list::const_iterator cit = citeStrings.begin();
1432 docstring_list::const_iterator end = citeStrings.end();
1434 for (int ii = 1; cit != end; ++cit, ++ii) {
1435 docstring label = *cit;
1437 CiteStyle cst = citeStyleList[ii - 1];
1439 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1440 FuncRequest(LFUN_INSET_MODIFY,
1441 "changetype " + from_utf8(citationStyleToString(cs)))));
1448 /////////////////////////////////////////////////////////////////////
1449 // Menu::Impl definition and implementation
1450 /////////////////////////////////////////////////////////////////////
1454 /// populates the menu or one of its submenu
1455 /// This is used as a recursive function
1456 void populate(QMenu & qMenu, MenuDefinition const & menu);
1458 /// Only needed for top level menus.
1459 MenuDefinition * top_level_menu;
1462 /// the name of this menu
1468 /// Get a MenuDefinition item label from the menu backend
1469 static QString label(MenuItem const & mi)
1471 QString label = mi.label();
1472 label.replace("&", "&&");
1474 QString shortcut = mi.shortcut();
1475 if (!shortcut.isEmpty()) {
1476 int pos = label.indexOf(shortcut);
1478 //label.insert(pos, 1, char_type('&'));
1479 label.replace(pos, 0, "&");
1482 QString const binding = mi.binding();
1483 if (!binding.isEmpty())
1484 label += '\t' + binding;
1489 void Menu::Impl::populate(QMenu & qMenu, MenuDefinition const & menu)
1491 LYXERR(Debug::GUI, "populating menu " << menu.name());
1492 if (menu.size() == 0) {
1493 LYXERR(Debug::GUI, "\tERROR: empty menu " << menu.name());
1496 LYXERR(Debug::GUI, " ***** menu entries " << menu.size());
1497 MenuDefinition::const_iterator m = menu.begin();
1498 MenuDefinition::const_iterator end = menu.end();
1499 for (; m != end; ++m) {
1500 if (m->kind() == MenuItem::Separator)
1501 qMenu.addSeparator();
1502 else if (m->kind() == MenuItem::Submenu) {
1503 QMenu * subMenu = qMenu.addMenu(label(*m));
1504 populate(*subMenu, m->submenu());
1505 subMenu->setEnabled(m->status().enabled());
1507 // we have a MenuItem::Command
1508 qMenu.addAction(new Action(view, QIcon(), label(*m),
1509 m->func(), QString(), &qMenu));
1514 /////////////////////////////////////////////////////////////////////
1515 // Menu implementation
1516 /////////////////////////////////////////////////////////////////////
1518 Menu::Menu(GuiView * gv, QString const & name, bool top_level)
1519 : QMenu(gv), d(new Menu::Impl)
1521 d->top_level_menu = top_level? new MenuDefinition : 0;
1525 if (d->top_level_menu)
1526 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
1532 delete d->top_level_menu;
1537 void Menu::updateView()
1539 guiApp->menus().updateMenu(this);
1543 /////////////////////////////////////////////////////////////////////
1544 // Menus::Impl definition and implementation
1545 /////////////////////////////////////////////////////////////////////
1547 struct Menus::Impl {
1549 bool hasMenu(QString const &) const;
1551 MenuDefinition & getMenu(QString const &);
1553 MenuDefinition const & getMenu(QString const &) const;
1555 /// Expands some special entries of the menu
1556 /** The entries with the following kind are expanded to a
1557 sequence of Command MenuItems: Lastfiles, Documents,
1558 ViewFormats, ExportFormats, UpdateFormats, Branches, Indices
1560 void expand(MenuDefinition const & frommenu, MenuDefinition & tomenu,
1561 BufferView const *) const;
1563 /// Initialize specific MACOS X menubar
1564 void macxMenuBarInit(GuiView * view, QMenuBar * qmb);
1566 /// Mac special menu.
1567 /** This defines a menu whose entries list the FuncRequests
1568 that will be removed by expand() in other menus. This is
1569 used by the Qt/Mac code.
1571 NOTE: Qt does not remove the menu items when clearing a QMenuBar,
1572 such that the items will keep accessing the FuncRequests in
1573 the MenuDefinition. While Menus::Impl might be recreated,
1574 we keep mac_special_menu_ in memory by making it static.
1576 static MenuDefinition mac_special_menu_;
1581 MenuDefinition menubar_;
1583 typedef QMap<GuiView *, QHash<QString, Menu*> > NameMap;
1585 /// name to menu for \c menu() method.
1590 MenuDefinition Menus::Impl::mac_special_menu_;
1594 Here is what the Qt documentation says about how a menubar is chosen:
1596 1) If the window has a QMenuBar then it is used. 2) If the window
1597 is a modal then its menubar is used. If no menubar is specified
1598 then a default menubar is used (as documented below) 3) If the
1599 window has no parent then the default menubar is used (as
1602 The above 3 steps are applied all the way up the parent window
1603 chain until one of the above are satisifed. If all else fails a
1604 default menubar will be created, the default menubar on Qt/Mac is
1605 an empty menubar, however you can create a different default
1606 menubar by creating a parentless QMenuBar, the first one created
1607 will thus be designated the default menubar, and will be used
1608 whenever a default menubar is needed.
1610 Thus, for Qt/Mac, we add the menus to a free standing menubar, so
1611 that this menubar will be used also when one of LyX' dialogs has
1614 void Menus::Impl::macxMenuBarInit(GuiView * view, QMenuBar * qmb)
1616 /* Since Qt 4.2, the qt/mac menu code has special code for
1617 specifying the role of a menu entry. However, it does not
1618 work very well with our scheme of creating menus on demand,
1619 and therefore we need to put these entries in a special
1620 invisible menu. (JMarc)
1623 /* The entries of our special mac menu. If we add support for
1624 * special entries in Menus, we could imagine something
1626 * SpecialItem About " "About LyX" "dialog-show aboutlyx"
1627 * and therefore avoid hardcoding. I am not sure it is worth
1628 * the hassle, though. (JMarc)
1630 struct MacMenuEntry {
1634 QAction::MenuRole role;
1637 MacMenuEntry entries[] = {
1638 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
1639 QAction::AboutRole},
1640 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
1641 QAction::PreferencesRole},
1642 {LFUN_RECONFIGURE, "", "Reconfigure",
1643 QAction::ApplicationSpecificRole},
1644 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
1646 const size_t num_entries = sizeof(entries) / sizeof(entries[0]);
1648 // the special menu for Menus. Fill it up only once.
1649 if (mac_special_menu_.size() == 0) {
1650 for (size_t i = 0 ; i < num_entries ; ++i) {
1651 FuncRequest const func(entries[i].action,
1652 from_utf8(entries[i].arg));
1653 mac_special_menu_.add(MenuItem(MenuItem::Command,
1654 entries[i].label, func));
1658 // add the entries to a QMenu that will eventually be empty
1659 // and therefore invisible.
1660 QMenu * qMenu = qmb->addMenu("special");
1661 MenuDefinition::const_iterator cit = mac_special_menu_.begin();
1662 MenuDefinition::const_iterator end = mac_special_menu_.end();
1663 for (size_t i = 0 ; cit != end ; ++cit, ++i) {
1664 Action * action = new Action(view, QIcon(), cit->label(),
1665 cit->func(), QString(), qMenu);
1666 action->setMenuRole(entries[i].role);
1667 qMenu->addAction(action);
1672 void Menus::Impl::expand(MenuDefinition const & frommenu,
1673 MenuDefinition & tomenu, BufferView const * bv) const
1675 if (!tomenu.empty())
1678 for (MenuDefinition::const_iterator cit = frommenu.begin();
1679 cit != frommenu.end() ; ++cit) {
1680 Buffer const * buf = bv ? &bv->buffer() : 0;
1681 switch (cit->kind()) {
1682 case MenuItem::Lastfiles:
1683 tomenu.expandLastfiles();
1686 case MenuItem::Documents:
1687 tomenu.expandDocuments();
1690 case MenuItem::Bookmarks:
1691 tomenu.expandBookmarks();
1694 case MenuItem::ImportFormats:
1695 case MenuItem::ViewFormats:
1696 case MenuItem::UpdateFormats:
1697 case MenuItem::ExportFormats:
1698 tomenu.expandFormats(cit->kind(), buf);
1701 case MenuItem::CharStyles:
1702 tomenu.expandFlexInsert(buf, InsetLayout::CHARSTYLE);
1705 case MenuItem::Custom:
1706 tomenu.expandFlexInsert(buf, InsetLayout::CUSTOM);
1709 case MenuItem::Elements:
1710 tomenu.expandFlexInsert(buf, InsetLayout::ELEMENT);
1713 case MenuItem::FloatListInsert:
1714 tomenu.expandFloatListInsert(buf);
1717 case MenuItem::FloatInsert:
1718 tomenu.expandFloatInsert(buf);
1721 case MenuItem::PasteRecent:
1722 tomenu.expandPasteRecent(buf);
1725 case MenuItem::Toolbars:
1726 tomenu.expandToolbars();
1729 case MenuItem::Branches:
1730 tomenu.expandBranches(buf);
1733 case MenuItem::Indices:
1734 tomenu.expandIndices(buf);
1737 case MenuItem::IndicesContext:
1738 tomenu.expandIndicesContext(buf);
1741 case MenuItem::IndicesLists:
1742 tomenu.expandIndices(buf, true);
1745 case MenuItem::IndicesListsContext:
1746 tomenu.expandIndicesContext(buf, true);
1749 case MenuItem::CiteStyles:
1750 tomenu.expandCiteStyles(bv);
1754 tomenu.expandToc(buf);
1757 case MenuItem::GraphicsGroups:
1758 tomenu.expandGraphicsGroups(bv);
1761 case MenuItem::SpellingSuggestions:
1762 tomenu.expandSpellingSuggestions(bv);
1765 case MenuItem::LanguageSelector:
1766 tomenu.expandLanguageSelector(buf);
1769 case MenuItem::Submenu: {
1770 MenuItem item(*cit);
1771 item.setSubmenu(MenuDefinition(cit->submenuname()));
1772 expand(getMenu(cit->submenuname()), item.submenu(), bv);
1773 tomenu.addWithStatusCheck(item);
1777 case MenuItem::Info:
1778 case MenuItem::Help:
1779 case MenuItem::Separator:
1780 tomenu.addWithStatusCheck(*cit);
1783 case MenuItem::Command:
1784 if (!mac_special_menu_.hasFunc(cit->func()))
1785 tomenu.addWithStatusCheck(*cit);
1789 // we do not want the menu to end with a separator
1790 if (!tomenu.empty() && tomenu.items_.back().kind() == MenuItem::Separator)
1791 tomenu.items_.pop_back();
1793 // Check whether the shortcuts are unique
1794 tomenu.checkShortcuts();
1798 bool Menus::Impl::hasMenu(QString const & name) const
1800 return find_if(menulist_.begin(), menulist_.end(),
1801 MenuNamesEqual(name)) != menulist_.end();
1805 MenuDefinition const & Menus::Impl::getMenu(QString const & name) const
1807 const_iterator cit = find_if(menulist_.begin(), menulist_.end(),
1808 MenuNamesEqual(name));
1809 if (cit == menulist_.end())
1810 LYXERR0("No submenu named " << name);
1811 LASSERT(cit != menulist_.end(), /**/);
1816 MenuDefinition & Menus::Impl::getMenu(QString const & name)
1818 iterator it = find_if(menulist_.begin(), menulist_.end(),
1819 MenuNamesEqual(name));
1820 if (it == menulist_.end())
1821 LYXERR0("No submenu named " << name);
1822 LASSERT(it != menulist_.end(), /**/);
1827 /////////////////////////////////////////////////////////////////////
1831 /////////////////////////////////////////////////////////////////////
1833 Menus::Menus() : d(new Impl) {}
1849 void Menus::read(Lexer & lex)
1857 LexerKeyword menutags[] = {
1858 { "end", md_endmenuset },
1859 { "menu", md_menu },
1860 { "menubar", md_menubar }
1863 // consistency check
1864 if (compare_ascii_no_case(lex.getString(), "menuset"))
1865 LYXERR0("Menus::read: ERROR wrong token: `" << lex.getString() << '\'');
1867 lex.pushTable(menutags);
1868 lex.setContext("Menus::read");
1872 while (lex.isOK() && !quit) {
1873 switch (lex.lex()) {
1875 d->menubar_.read(lex);
1879 QString const name = toqstr(lex.getDocString());
1880 if (d->hasMenu(name))
1881 d->getMenu(name).read(lex);
1883 MenuDefinition menu(name);
1885 d->menulist_.push_back(menu);
1893 lex.printError("Unknown menu tag");
1901 bool Menus::searchMenu(FuncRequest const & func,
1902 docstring_list & names) const
1904 MenuDefinition menu;
1905 d->expand(d->menubar_, menu, 0);
1906 return menu.searchMenu(func, names);
1910 void Menus::fillMenuBar(QMenuBar * qmb, GuiView * view, bool initial)
1914 // setup special mac specific menu items, but only do this
1915 // the first time a QMenuBar is created. Otherwise Qt will
1916 // create duplicate items in the application menu. It seems
1917 // that Qt does not remove them when the QMenubar is cleared.
1918 LYXERR(Debug::GUI, "Creating Mac OS X special menu bar");
1919 d->macxMenuBarInit(view, qmb);
1922 // Clear all menubar contents before filling it.
1926 LYXERR(Debug::GUI, "populating menu bar" << d->menubar_.name());
1928 if (d->menubar_.size() == 0) {
1929 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
1930 << d->menubar_.name());
1933 LYXERR(Debug::GUI, "menu bar entries " << d->menubar_.size());
1935 MenuDefinition menu;
1936 BufferView * bv = 0;
1938 bv = view->currentBufferView();
1939 d->expand(d->menubar_, menu, bv);
1941 MenuDefinition::const_iterator m = menu.begin();
1942 MenuDefinition::const_iterator end = menu.end();
1944 for (; m != end; ++m) {
1946 if (m->kind() != MenuItem::Submenu) {
1947 LYXERR(Debug::GUI, "\tERROR: not a submenu " << m->label());
1951 LYXERR(Debug::GUI, "menu bar item " << m->label()
1952 << " is a submenu named " << m->submenuname());
1954 QString name = m->submenuname();
1955 if (!d->hasMenu(name)) {
1956 LYXERR(Debug::GUI, "\tERROR: " << name
1957 << " submenu has no menu!");
1961 Menu * menu = new Menu(view, m->submenuname(), true);
1962 menu->setTitle(label(*m));
1965 d->name_map_[view][name] = menu;
1970 void Menus::updateMenu(Menu * qmenu)
1972 LYXERR(Debug::GUI, "Triggered menu: " << qmenu->d->name);
1975 if (qmenu->d->name.isEmpty())
1978 if (!d->hasMenu(qmenu->d->name)) {
1979 qmenu->addAction(qt_("No Action Defined!"));
1980 LYXERR(Debug::GUI, "\tWARNING: non existing menu: "
1985 MenuDefinition const & fromLyxMenu = d->getMenu(qmenu->d->name);
1986 BufferView * bv = 0;
1988 bv = qmenu->d->view->currentBufferView();
1989 d->expand(fromLyxMenu, *qmenu->d->top_level_menu, bv);
1990 qmenu->d->populate(*qmenu, *qmenu->d->top_level_menu);
1994 Menu * Menus::menu(QString const & name, GuiView & view)
1996 LYXERR(Debug::GUI, "Context menu requested: " << name);
1997 Menu * menu = d->name_map_[&view].value(name, 0);
1998 if (!menu && !name.startsWith("context-")) {
1999 LYXERR0("requested context menu not found: " << name);
2003 menu = new Menu(&view, name, true);
2004 d->name_map_[&view][name] = menu;
2008 } // namespace frontend
2011 #include "moc_Menus.cpp"