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);
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_("Invisible"));
883 item.setSubmenu(MenuDefinition(qt_("Invisible")));
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 const label = cit->first;
1094 addWithStatusCheck(MenuItem(MenuItem::Command,
1095 toqstr(translateIfPossible(label)),
1096 FuncRequest(LFUN_FLEX_INSERT, Lexer::quoteString(label))));
1099 // FIXME This is a little clunky.
1100 if (items_.empty() && type == InsetLayout::CUSTOM)
1101 add(MenuItem(MenuItem::Help, qt_("No Custom Insets Defined!")));
1105 size_t const max_number_of_items = 25;
1107 void MenuDefinition::expandToc2(Toc const & toc_list,
1108 size_t from, size_t to, int depth)
1110 int shortcut_count = 0;
1112 // check whether depth is smaller than the smallest depth in toc.
1113 int min_depth = 1000;
1114 for (size_t i = from; i < to; ++i)
1115 min_depth = min(min_depth, toc_list[i].depth());
1116 if (min_depth > depth)
1119 if (to - from <= max_number_of_items) {
1120 for (size_t i = from; i < to; ++i) {
1121 QString label(4 * max(0, toc_list[i].depth() - depth), ' ');
1122 label += limitStringLength(toc_list[i].str());
1123 if (toc_list[i].depth() == depth) {
1125 if (shortcut_count < 9) {
1126 if (label.contains(QString::number(shortcut_count + 1)))
1127 label += QString::number(++shortcut_count);
1130 add(MenuItem(MenuItem::Command, label,
1131 FuncRequest(toc_list[i].action())));
1136 size_t new_pos = pos + 1;
1137 while (new_pos < to && toc_list[new_pos].depth() > depth)
1140 QString label(4 * max(0, toc_list[pos].depth() - depth), ' ');
1141 label += limitStringLength(toc_list[pos].str());
1142 if (toc_list[pos].depth() == depth) {
1144 if (shortcut_count < 9) {
1145 if (label.contains(QString::number(shortcut_count + 1)))
1146 label += QString::number(++shortcut_count);
1149 if (new_pos == pos + 1) {
1150 add(MenuItem(MenuItem::Command,
1151 label, FuncRequest(toc_list[pos].action())));
1154 sub.expandToc2(toc_list, pos, new_pos, depth + 1);
1155 MenuItem item(MenuItem::Submenu, label);
1156 item.setSubmenu(sub);
1165 void MenuDefinition::expandToc(Buffer const * buf)
1167 // To make things very cleanly, we would have to pass buf to
1168 // all MenuItem constructors and to expandToc2. However, we
1169 // know that all the entries in a TOC will be have status_ ==
1170 // OK, so we avoid this unnecessary overhead (JMarc)
1173 add(MenuItem(MenuItem::Info, qt_("<No Document Open>")));
1177 // Add an entry for the master doc if this is a child doc
1178 Buffer const * const master = buf->masterBuffer();
1179 if (buf != master) {
1180 ParIterator const pit = par_iterator_begin(master->inset());
1181 string const arg = convert<string>(pit->id());
1182 FuncRequest f(LFUN_PARAGRAPH_GOTO, arg);
1183 add(MenuItem(MenuItem::Command, qt_("Master Document"), f));
1186 MenuDefinition other_lists;
1188 FloatList const & floatlist = buf->params().documentClass().floats();
1189 TocList const & toc_list = buf->tocBackend().tocs();
1190 TocList::const_iterator cit = toc_list.begin();
1191 TocList::const_iterator end = toc_list.end();
1192 for (; cit != end; ++cit) {
1193 // Handle this later
1194 if (cit->first == "tableofcontents")
1197 MenuDefinition submenu;
1198 if (cit->second.size() >= 30) {
1199 FuncRequest f(LFUN_DIALOG_SHOW, "toc " + cit->first);
1200 submenu.add(MenuItem(MenuItem::Command, qt_("Open Navigator..."), f));
1202 TocIterator ccit = cit->second.begin();
1203 TocIterator eend = cit->second.end();
1204 for (; ccit != eend; ++ccit) {
1205 submenu.add(MenuItem(MenuItem::Command,
1206 limitStringLength(ccit->str()) + '|',
1207 FuncRequest(ccit->action())));
1211 MenuItem item(MenuItem::Submenu, guiName(cit->first, buf->params()));
1212 item.setSubmenu(submenu);
1213 if (floatlist.typeExist(cit->first) || cit->first == "child") {
1214 // Those two types deserve to be in the main menu.
1215 item.setSubmenu(submenu);
1218 other_lists.add(item);
1220 if (!other_lists.empty()) {
1221 MenuItem item(MenuItem::Submenu, qt_("Other Lists"));
1222 item.setSubmenu(other_lists);
1226 // Handle normal TOC
1227 cit = toc_list.find("tableofcontents");
1229 LYXERR(Debug::GUI, "No table of contents.");
1231 if (cit->second.size() > 0 )
1232 expandToc2(cit->second, 0, cit->second.size(), 0);
1234 add(MenuItem(MenuItem::Info, qt_("<Empty Table of Contents>")));
1239 void MenuDefinition::expandPasteRecent(Buffer const * buf)
1241 docstring_list const sel = cap::availableSelections(buf);
1243 docstring_list::const_iterator cit = sel.begin();
1244 docstring_list::const_iterator end = sel.end();
1246 for (unsigned int index = 0; cit != end; ++cit, ++index) {
1247 add(MenuItem(MenuItem::Command, toqstr(*cit) + '|',
1248 FuncRequest(LFUN_PASTE, convert<string>(index))));
1253 void MenuDefinition::expandToolbars()
1255 MenuDefinition other_lists;
1256 // extracts the toolbars from the backend
1257 Toolbars::Infos::const_iterator cit = guiApp->toolbars().begin();
1258 Toolbars::Infos::const_iterator end = guiApp->toolbars().end();
1259 for (; cit != end; ++cit) {
1260 MenuItem const item(MenuItem::Command, toqstr(cit->gui_name),
1261 FuncRequest(LFUN_TOOLBAR_TOGGLE, cit->name));
1262 if (guiApp->toolbars().isMainToolbar(cit->name))
1265 other_lists.add(item);
1268 if (!other_lists.empty()) {
1269 MenuItem item(MenuItem::Submenu, qt_("Other Toolbars"));
1270 item.setSubmenu(other_lists);
1276 void MenuDefinition::expandBranches(Buffer const * buf)
1281 BufferParams const & master_params = buf->masterBuffer()->params();
1282 BufferParams const & params = buf->params();
1283 if (params.branchlist().empty() && master_params.branchlist().empty() ) {
1284 add(MenuItem(MenuItem::Help, qt_("No Branches Set for Document!")));
1288 BranchList::const_iterator cit = master_params.branchlist().begin();
1289 BranchList::const_iterator end = master_params.branchlist().end();
1291 for (int ii = 1; cit != end; ++cit, ++ii) {
1292 docstring label = cit->branch();
1294 label = convert<docstring>(ii) + ". " + label
1295 + char_type('|') + convert<docstring>(ii);
1297 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1298 FuncRequest(LFUN_BRANCH_INSERT,
1302 if (buf == buf->masterBuffer())
1305 MenuDefinition child_branches;
1307 BranchList::const_iterator ccit = params.branchlist().begin();
1308 BranchList::const_iterator cend = params.branchlist().end();
1310 for (int ii = 1; ccit != cend; ++ccit, ++ii) {
1311 docstring label = ccit->branch();
1313 label = convert<docstring>(ii) + ". " + label
1314 + char_type('|') + convert<docstring>(ii);
1316 label += char_type('|');
1317 child_branches.addWithStatusCheck(MenuItem(MenuItem::Command,
1319 FuncRequest(LFUN_BRANCH_INSERT,
1323 if (!child_branches.empty()) {
1324 MenuItem item(MenuItem::Submenu, qt_("Child Document"));
1325 item.setSubmenu(child_branches);
1331 void MenuDefinition::expandIndices(Buffer const * buf, bool listof)
1336 BufferParams const & params = buf->masterBuffer()->params();
1337 if (!params.use_indices) {
1339 addWithStatusCheck(MenuItem(MenuItem::Command,
1340 qt_("Index List|I"),
1341 FuncRequest(LFUN_INDEX_PRINT,
1342 from_ascii("idx"))));
1344 addWithStatusCheck(MenuItem(MenuItem::Command,
1345 qt_("Index Entry|d"),
1346 FuncRequest(LFUN_INDEX_INSERT,
1347 from_ascii("idx"))));
1351 if (params.indiceslist().empty())
1354 IndicesList::const_iterator cit = params.indiceslist().begin();
1355 IndicesList::const_iterator end = params.indiceslist().end();
1357 for (int ii = 1; cit != end; ++cit, ++ii) {
1359 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(cit->index()),
1360 FuncRequest(LFUN_INDEX_PRINT,
1363 docstring label = _("Index Entry");
1364 label += " (" + cit->index() + ")";
1365 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1366 FuncRequest(LFUN_INDEX_INSERT,
1373 void MenuDefinition::expandIndicesContext(Buffer const * buf, bool listof)
1378 BufferParams const & params = buf->masterBuffer()->params();
1379 if (!params.use_indices || params.indiceslist().empty())
1382 IndicesList::const_iterator cit = params.indiceslist().begin();
1383 IndicesList::const_iterator end = params.indiceslist().end();
1385 for (int ii = 1; cit != end; ++cit, ++ii) {
1387 InsetCommandParams p(INDEX_PRINT_CODE);
1388 p["type"] = cit->shortcut();
1389 string const data = InsetCommand::params2string("index_print", p);
1390 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(cit->index()),
1391 FuncRequest(LFUN_INSET_MODIFY, data)));
1393 docstring label = _("Index Entry");
1394 label += " (" + cit->index() + ")";
1395 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1396 FuncRequest(LFUN_INSET_MODIFY,
1397 from_ascii("changetype ") + cit->shortcut())));
1403 void MenuDefinition::expandCiteStyles(BufferView const * bv)
1408 Inset const * inset = bv->cursor().nextInset();
1409 if (!inset || inset->lyxCode() != CITE_CODE) {
1410 add(MenuItem(MenuItem::Command,
1411 qt_("No Citation in Scope!"),
1412 FuncRequest(LFUN_NOACTION)));
1415 InsetCommand const * citinset =
1416 static_cast<InsetCommand const *>(inset);
1418 Buffer const * buf = &bv->buffer();
1419 docstring key = citinset->getParam("key");
1420 // we can only handle one key currently
1421 if (contains(key, ','))
1422 key = qstring_to_ucs4(toqstr(key).split(',')[0]);
1424 vector<CiteStyle> citeStyleList = citeStyles(buf->params().citeEngine());
1425 docstring_list citeStrings =
1426 buf->masterBibInfo().getCiteStrings(key, bv->buffer());
1428 docstring_list::const_iterator cit = citeStrings.begin();
1429 docstring_list::const_iterator end = citeStrings.end();
1431 for (int ii = 1; cit != end; ++cit, ++ii) {
1432 docstring label = *cit;
1434 CiteStyle cst = citeStyleList[ii - 1];
1436 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1437 FuncRequest(LFUN_INSET_MODIFY,
1438 "changetype " + from_utf8(citationStyleToString(cs)))));
1445 /////////////////////////////////////////////////////////////////////
1446 // Menu::Impl definition and implementation
1447 /////////////////////////////////////////////////////////////////////
1451 /// populates the menu or one of its submenu
1452 /// This is used as a recursive function
1453 void populate(QMenu & qMenu, MenuDefinition const & menu);
1455 /// Only needed for top level menus.
1456 MenuDefinition * top_level_menu;
1459 /// the name of this menu
1465 /// Get a MenuDefinition item label from the menu backend
1466 static QString label(MenuItem const & mi)
1468 QString label = mi.label();
1469 label.replace("&", "&&");
1471 QString shortcut = mi.shortcut();
1472 if (!shortcut.isEmpty()) {
1473 int pos = label.indexOf(shortcut);
1475 //label.insert(pos, 1, char_type('&'));
1476 label.replace(pos, 0, "&");
1479 QString const binding = mi.binding();
1480 if (!binding.isEmpty())
1481 label += '\t' + binding;
1486 void Menu::Impl::populate(QMenu & qMenu, MenuDefinition const & menu)
1488 LYXERR(Debug::GUI, "populating menu " << menu.name());
1489 if (menu.size() == 0) {
1490 LYXERR(Debug::GUI, "\tERROR: empty menu " << menu.name());
1493 LYXERR(Debug::GUI, " ***** menu entries " << menu.size());
1494 MenuDefinition::const_iterator m = menu.begin();
1495 MenuDefinition::const_iterator end = menu.end();
1496 for (; m != end; ++m) {
1497 if (m->kind() == MenuItem::Separator)
1498 qMenu.addSeparator();
1499 else if (m->kind() == MenuItem::Submenu) {
1500 QMenu * subMenu = qMenu.addMenu(label(*m));
1501 populate(*subMenu, m->submenu());
1502 subMenu->setEnabled(m->status().enabled());
1504 // we have a MenuItem::Command
1505 qMenu.addAction(new Action(view, QIcon(), label(*m),
1506 m->func(), QString(), &qMenu));
1511 /////////////////////////////////////////////////////////////////////
1512 // Menu implementation
1513 /////////////////////////////////////////////////////////////////////
1515 Menu::Menu(GuiView * gv, QString const & name, bool top_level)
1516 : QMenu(gv), d(new Menu::Impl)
1518 d->top_level_menu = top_level? new MenuDefinition : 0;
1522 if (d->top_level_menu)
1523 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
1529 delete d->top_level_menu;
1534 void Menu::updateView()
1536 guiApp->menus().updateMenu(this);
1540 /////////////////////////////////////////////////////////////////////
1541 // Menus::Impl definition and implementation
1542 /////////////////////////////////////////////////////////////////////
1544 struct Menus::Impl {
1546 bool hasMenu(QString const &) const;
1548 MenuDefinition & getMenu(QString const &);
1550 MenuDefinition const & getMenu(QString const &) const;
1552 /// Expands some special entries of the menu
1553 /** The entries with the following kind are expanded to a
1554 sequence of Command MenuItems: Lastfiles, Documents,
1555 ViewFormats, ExportFormats, UpdateFormats, Branches, Indices
1557 void expand(MenuDefinition const & frommenu, MenuDefinition & tomenu,
1558 BufferView const *) const;
1560 /// Initialize specific MACOS X menubar
1561 void macxMenuBarInit(GuiView * view, QMenuBar * qmb);
1563 /// Mac special menu.
1564 /** This defines a menu whose entries list the FuncRequests
1565 that will be removed by expand() in other menus. This is
1566 used by the Qt/Mac code.
1568 NOTE: Qt does not remove the menu items when clearing a QMenuBar,
1569 such that the items will keep accessing the FuncRequests in
1570 the MenuDefinition. While Menus::Impl might be recreated,
1571 we keep mac_special_menu_ in memory by making it static.
1573 static MenuDefinition mac_special_menu_;
1578 MenuDefinition menubar_;
1580 typedef QMap<GuiView *, QHash<QString, Menu*> > NameMap;
1582 /// name to menu for \c menu() method.
1587 MenuDefinition Menus::Impl::mac_special_menu_;
1591 Here is what the Qt documentation says about how a menubar is chosen:
1593 1) If the window has a QMenuBar then it is used. 2) If the window
1594 is a modal then its menubar is used. If no menubar is specified
1595 then a default menubar is used (as documented below) 3) If the
1596 window has no parent then the default menubar is used (as
1599 The above 3 steps are applied all the way up the parent window
1600 chain until one of the above are satisifed. If all else fails a
1601 default menubar will be created, the default menubar on Qt/Mac is
1602 an empty menubar, however you can create a different default
1603 menubar by creating a parentless QMenuBar, the first one created
1604 will thus be designated the default menubar, and will be used
1605 whenever a default menubar is needed.
1607 Thus, for Qt/Mac, we add the menus to a free standing menubar, so
1608 that this menubar will be used also when one of LyX' dialogs has
1611 void Menus::Impl::macxMenuBarInit(GuiView * view, QMenuBar * qmb)
1613 /* Since Qt 4.2, the qt/mac menu code has special code for
1614 specifying the role of a menu entry. However, it does not
1615 work very well with our scheme of creating menus on demand,
1616 and therefore we need to put these entries in a special
1617 invisible menu. (JMarc)
1620 /* The entries of our special mac menu. If we add support for
1621 * special entries in Menus, we could imagine something
1623 * SpecialItem About " "About LyX" "dialog-show aboutlyx"
1624 * and therefore avoid hardcoding. I am not sure it is worth
1625 * the hassle, though. (JMarc)
1627 struct MacMenuEntry {
1631 QAction::MenuRole role;
1634 MacMenuEntry entries[] = {
1635 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
1636 QAction::AboutRole},
1637 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
1638 QAction::PreferencesRole},
1639 {LFUN_RECONFIGURE, "", "Reconfigure",
1640 QAction::ApplicationSpecificRole},
1641 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
1643 const size_t num_entries = sizeof(entries) / sizeof(entries[0]);
1645 // the special menu for Menus. Fill it up only once.
1646 if (mac_special_menu_.size() == 0) {
1647 for (size_t i = 0 ; i < num_entries ; ++i) {
1648 FuncRequest const func(entries[i].action,
1649 from_utf8(entries[i].arg));
1650 mac_special_menu_.add(MenuItem(MenuItem::Command,
1651 entries[i].label, func));
1655 // add the entries to a QMenu that will eventually be empty
1656 // and therefore invisible.
1657 QMenu * qMenu = qmb->addMenu("special");
1658 MenuDefinition::const_iterator cit = mac_special_menu_.begin();
1659 MenuDefinition::const_iterator end = mac_special_menu_.end();
1660 for (size_t i = 0 ; cit != end ; ++cit, ++i) {
1661 Action * action = new Action(view, QIcon(), cit->label(),
1662 cit->func(), QString(), qMenu);
1663 action->setMenuRole(entries[i].role);
1664 qMenu->addAction(action);
1669 void Menus::Impl::expand(MenuDefinition const & frommenu,
1670 MenuDefinition & tomenu, BufferView const * bv) const
1672 if (!tomenu.empty())
1675 for (MenuDefinition::const_iterator cit = frommenu.begin();
1676 cit != frommenu.end() ; ++cit) {
1677 Buffer const * buf = bv ? &bv->buffer() : 0;
1678 switch (cit->kind()) {
1679 case MenuItem::Lastfiles:
1680 tomenu.expandLastfiles();
1683 case MenuItem::Documents:
1684 tomenu.expandDocuments();
1687 case MenuItem::Bookmarks:
1688 tomenu.expandBookmarks();
1691 case MenuItem::ImportFormats:
1692 case MenuItem::ViewFormats:
1693 case MenuItem::UpdateFormats:
1694 case MenuItem::ExportFormats:
1695 tomenu.expandFormats(cit->kind(), buf);
1698 case MenuItem::CharStyles:
1699 tomenu.expandFlexInsert(buf, InsetLayout::CHARSTYLE);
1702 case MenuItem::Custom:
1703 tomenu.expandFlexInsert(buf, InsetLayout::CUSTOM);
1706 case MenuItem::Elements:
1707 tomenu.expandFlexInsert(buf, InsetLayout::ELEMENT);
1710 case MenuItem::FloatListInsert:
1711 tomenu.expandFloatListInsert(buf);
1714 case MenuItem::FloatInsert:
1715 tomenu.expandFloatInsert(buf);
1718 case MenuItem::PasteRecent:
1719 tomenu.expandPasteRecent(buf);
1722 case MenuItem::Toolbars:
1723 tomenu.expandToolbars();
1726 case MenuItem::Branches:
1727 tomenu.expandBranches(buf);
1730 case MenuItem::Indices:
1731 tomenu.expandIndices(buf);
1734 case MenuItem::IndicesContext:
1735 tomenu.expandIndicesContext(buf);
1738 case MenuItem::IndicesLists:
1739 tomenu.expandIndices(buf, true);
1742 case MenuItem::IndicesListsContext:
1743 tomenu.expandIndicesContext(buf, true);
1746 case MenuItem::CiteStyles:
1747 tomenu.expandCiteStyles(bv);
1751 tomenu.expandToc(buf);
1754 case MenuItem::GraphicsGroups:
1755 tomenu.expandGraphicsGroups(bv);
1758 case MenuItem::SpellingSuggestions:
1759 tomenu.expandSpellingSuggestions(bv);
1762 case MenuItem::LanguageSelector:
1763 tomenu.expandLanguageSelector(buf);
1766 case MenuItem::Submenu: {
1767 MenuItem item(*cit);
1768 item.setSubmenu(MenuDefinition(cit->submenuname()));
1769 expand(getMenu(cit->submenuname()), item.submenu(), bv);
1770 tomenu.addWithStatusCheck(item);
1774 case MenuItem::Info:
1775 case MenuItem::Help:
1776 case MenuItem::Separator:
1777 tomenu.addWithStatusCheck(*cit);
1780 case MenuItem::Command:
1781 if (!mac_special_menu_.hasFunc(cit->func()))
1782 tomenu.addWithStatusCheck(*cit);
1786 // we do not want the menu to end with a separator
1787 if (!tomenu.empty() && tomenu.items_.back().kind() == MenuItem::Separator)
1788 tomenu.items_.pop_back();
1790 // Check whether the shortcuts are unique
1791 tomenu.checkShortcuts();
1795 bool Menus::Impl::hasMenu(QString const & name) const
1797 return find_if(menulist_.begin(), menulist_.end(),
1798 MenuNamesEqual(name)) != menulist_.end();
1802 MenuDefinition const & Menus::Impl::getMenu(QString const & name) const
1804 const_iterator cit = find_if(menulist_.begin(), menulist_.end(),
1805 MenuNamesEqual(name));
1806 if (cit == menulist_.end())
1807 LYXERR0("No submenu named " << name);
1808 LASSERT(cit != menulist_.end(), /**/);
1813 MenuDefinition & Menus::Impl::getMenu(QString const & name)
1815 iterator it = find_if(menulist_.begin(), menulist_.end(),
1816 MenuNamesEqual(name));
1817 if (it == menulist_.end())
1818 LYXERR0("No submenu named " << name);
1819 LASSERT(it != menulist_.end(), /**/);
1824 /////////////////////////////////////////////////////////////////////
1828 /////////////////////////////////////////////////////////////////////
1830 Menus::Menus() : d(new Impl) {}
1846 void Menus::read(Lexer & lex)
1854 LexerKeyword menutags[] = {
1855 { "end", md_endmenuset },
1856 { "menu", md_menu },
1857 { "menubar", md_menubar }
1860 // consistency check
1861 if (compare_ascii_no_case(lex.getString(), "menuset"))
1862 LYXERR0("Menus::read: ERROR wrong token: `" << lex.getString() << '\'');
1864 lex.pushTable(menutags);
1865 lex.setContext("Menus::read");
1869 while (lex.isOK() && !quit) {
1870 switch (lex.lex()) {
1872 d->menubar_.read(lex);
1876 QString const name = toqstr(lex.getDocString());
1877 if (d->hasMenu(name))
1878 d->getMenu(name).read(lex);
1880 MenuDefinition menu(name);
1882 d->menulist_.push_back(menu);
1890 lex.printError("Unknown menu tag");
1898 bool Menus::searchMenu(FuncRequest const & func,
1899 docstring_list & names) const
1901 MenuDefinition menu;
1902 d->expand(d->menubar_, menu, 0);
1903 return menu.searchMenu(func, names);
1907 void Menus::fillMenuBar(QMenuBar * qmb, GuiView * view, bool initial)
1911 // setup special mac specific menu items, but only do this
1912 // the first time a QMenuBar is created. Otherwise Qt will
1913 // create duplicate items in the application menu. It seems
1914 // that Qt does not remove them when the QMenubar is cleared.
1915 LYXERR(Debug::GUI, "Creating Mac OS X special menu bar");
1916 d->macxMenuBarInit(view, qmb);
1919 // Clear all menubar contents before filling it.
1923 LYXERR(Debug::GUI, "populating menu bar" << d->menubar_.name());
1925 if (d->menubar_.size() == 0) {
1926 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
1927 << d->menubar_.name());
1930 LYXERR(Debug::GUI, "menu bar entries " << d->menubar_.size());
1932 MenuDefinition menu;
1933 BufferView * bv = 0;
1935 bv = view->currentBufferView();
1936 d->expand(d->menubar_, menu, bv);
1938 MenuDefinition::const_iterator m = menu.begin();
1939 MenuDefinition::const_iterator end = menu.end();
1941 for (; m != end; ++m) {
1943 if (m->kind() != MenuItem::Submenu) {
1944 LYXERR(Debug::GUI, "\tERROR: not a submenu " << m->label());
1948 LYXERR(Debug::GUI, "menu bar item " << m->label()
1949 << " is a submenu named " << m->submenuname());
1951 QString name = m->submenuname();
1952 if (!d->hasMenu(name)) {
1953 LYXERR(Debug::GUI, "\tERROR: " << name
1954 << " submenu has no menu!");
1958 Menu * menu = new Menu(view, m->submenuname(), true);
1959 menu->setTitle(label(*m));
1962 d->name_map_[view][name] = menu;
1967 void Menus::updateMenu(Menu * qmenu)
1969 LYXERR(Debug::GUI, "Triggered menu: " << qmenu->d->name);
1972 if (qmenu->d->name.isEmpty())
1975 if (!d->hasMenu(qmenu->d->name)) {
1976 qmenu->addAction(qt_("No Action Defined!"));
1977 LYXERR(Debug::GUI, "\tWARNING: non existing menu: "
1982 MenuDefinition const & fromLyxMenu = d->getMenu(qmenu->d->name);
1983 BufferView * bv = 0;
1985 bv = qmenu->d->view->currentBufferView();
1986 d->expand(fromLyxMenu, *qmenu->d->top_level_menu, bv);
1987 qmenu->d->populate(*qmenu, *qmenu->d->top_level_menu);
1991 Menu * Menus::menu(QString const & name, GuiView & view)
1993 LYXERR(Debug::GUI, "Context menu requested: " << name);
1994 Menu * menu = d->name_map_[&view].value(name, 0);
1995 if (!menu && !name.startsWith("context-")) {
1996 LYXERR0("requested context menu not found: " << name);
2000 menu = new Menu(&view, name, true);
2001 d->name_map_[&view][name] = menu;
2005 } // namespace frontend
2008 #include "moc_Menus.cpp"