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 "GuiWorkArea.h"
25 #include "qt_helpers.h"
27 #include "BiblioInfo.h"
28 #include "BranchList.h"
30 #include "BufferList.h"
31 #include "BufferParams.h"
32 #include "BufferView.h"
33 #include "Converter.h"
34 #include "CutAndPaste.h"
36 #include "FloatList.h"
38 #include "FuncRequest.h"
39 #include "FuncStatus.h"
40 #include "IndicesList.h"
44 #include "LyXAction.h"
48 #include "Paragraph.h"
49 #include "ParIterator.h"
51 #include "SpellChecker.h"
52 #include "TextClass.h"
53 #include "TocBackend.h"
55 #include "WordLangTuple.h"
57 #include "insets/Inset.h"
58 #include "insets/InsetCitation.h"
59 #include "insets/InsetGraphics.h"
61 #include "support/lassert.h"
62 #include "support/convert.h"
63 #include "support/debug.h"
64 #include "support/docstring_list.h"
65 #include "support/filetools.h"
66 #include "support/gettext.h"
67 #include "support/lstrings.h"
74 #include <QProxyStyle>
76 #include "support/shared_ptr.h"
82 using namespace lyx::support;
90 // MacOSX specific stuff is at the end.
97 /// The type of elements that can be in a menu
105 /** This type of item explains why something is unavailable. If this
106 menuitem is in a submenu, the submenu is enabled to make sure the
107 user sees the information. */
109 /** This type of item merely shows that there might be a list or
110 something alike at this position, but the list is still empty.
111 If this item is in a submenu, the submenu will not always be
114 /** This is the list of last opened file,
115 typically for the File menu. */
117 /** This is the list of opened Documents,
118 typically for the Documents menu. */
120 /** This is the bookmarks */
124 /** This is a list of viewable formats
125 typically for the File->View menu. */
127 /** This is a list of updatable formats
128 typically for the File->Update menu. */
130 /** This is a list of exportable formats
131 typically for the File->Export menu. */
133 /** This is a list of importable formats
134 typically for the File->Import menu. */
136 /** This is the list of elements available
137 * for insertion into document. */
139 /** This is the list of user-configurable
140 insets to insert into document */
142 /** This is the list of XML elements to
143 insert into the document */
145 /** This is the list of floats that we can
146 insert a list for. */
148 /** This is the list of floats that we can
151 /** This is the list of selections that can
156 /** Available branches in document */
158 /** Available indices in document */
160 /** Context menu for indices in document */
162 /** Available index lists in document */
164 /** Context menu for available indices lists in document */
166 /** Available citation styles for a given citation */
168 /** Available graphics groups */
170 /// Words suggested by the spellchecker.
172 /** Used Languages */
176 explicit MenuItem(Kind kind) : kind_(kind), optional_(false) {}
179 QString const & label,
180 QString const & submenu = QString(),
181 QString const & tooltip = QString(),
182 bool optional = false)
183 : kind_(kind), label_(label), submenuname_(submenu),
184 tooltip_(tooltip), optional_(optional)
186 LASSERT(kind == Submenu || kind == Help || kind == Info, /**/);
190 QString const & label,
191 FuncRequest const & func,
192 QString const & tooltip = QString(),
193 bool optional = false,
194 FuncRequest::Origin origin = FuncRequest::MENU)
195 : kind_(kind), label_(label), func_(func),
196 tooltip_(tooltip), optional_(optional)
198 func_.setOrigin(origin);
201 // shared_ptr<MenuDefinition> needs this apprently...
204 /// The label of a given menuitem
205 QString label() const
207 int const index = label_.lastIndexOf('|');
208 return index == -1 ? label_ : label_.left(index);
211 /// The keyboard shortcut (usually underlined in the entry)
212 QString shortcut() const
214 int const index = label_.lastIndexOf('|');
215 return index == -1 ? QString() : label_.mid(index + 1);
217 /// The complete label, with label and shortcut separated by a '|'
218 QString fulllabel() const { return label_; }
219 /// The kind of entry
220 Kind kind() const { return kind_; }
221 /// the action (if relevant)
222 FuncRequest const & func() const { return func_; }
224 QString const & tooltip() const { return tooltip_; }
225 /// returns true if the entry should be omitted when disabled
226 bool optional() const { return optional_; }
227 /// returns the status of the lfun associated with this entry
228 FuncStatus const & status() const { return status_; }
229 /// returns the status of the lfun associated with this entry
230 FuncStatus & status() { return status_; }
231 /// returns the status of the lfun associated with this entry
232 void status(FuncStatus const & status) { status_ = status; }
234 ///returns the binding associated to this action.
235 QString binding() const
237 if (kind_ != Command)
239 // Get the keys bound to this action, but keep only the
241 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func_);
243 return toqstr(bindings.begin()->print(KeySequence::ForGui));
245 LYXERR(Debug::KBMAP, "No binding for "
246 << lyxaction.getActionName(func_.action())
247 << '(' << func_.argument() << ')');
251 /// the description of the submenu (if relevant)
252 QString const & submenuname() const { return submenuname_; }
253 /// set the description of the submenu
254 void submenuname(QString const & name) { submenuname_ = name; }
256 bool hasSubmenu() const { return !submenu_.isEmpty(); }
258 MenuDefinition const & submenu() const { return submenu_.at(0); }
259 MenuDefinition & submenu() { return submenu_[0]; }
261 void setSubmenu(MenuDefinition const & menu)
264 submenu_.append(menu);
275 QString submenuname_;
282 /// contains 0 or 1 item.
283 QList<MenuDefinition> submenu_;
287 class MenuDefinition {
290 typedef std::vector<MenuItem> ItemList;
292 typedef ItemList::const_iterator const_iterator;
294 explicit MenuDefinition(QString const & name = QString()) : name_(name) {}
299 QString const & name() const { return name_; }
301 bool empty() const { return items_.empty(); }
302 /// Clear the menu content.
303 void clear() { items_.clear(); }
305 size_t size() const { return items_.size(); }
307 MenuItem const & operator[](size_t) const;
309 const_iterator begin() const { return items_.begin(); }
311 const_iterator end() const { return items_.end(); }
313 void cat(MenuDefinition const & other);
315 // search for func in this menu iteratively, and put menu
317 bool searchMenu(FuncRequest const & func, docstring_list & names)
320 bool hasFunc(FuncRequest const &) const;
321 /// Add the menu item unconditionally
322 void add(MenuItem const & item) { items_.push_back(item); }
323 /// Checks the associated FuncRequest status before adding the
325 void addWithStatusCheck(MenuItem const &);
326 // Check whether the menu shortcuts are unique
327 void checkShortcuts() const;
329 void expandLastfiles();
330 void expandDocuments();
331 void expandBookmarks();
332 void expandFormats(MenuItem::Kind kind, Buffer const * buf);
333 void expandFloatListInsert(Buffer const * buf);
334 void expandFloatInsert(Buffer const * buf);
335 void expandFlexInsert(Buffer const * buf, InsetLayout::InsetLyXType type);
336 void expandToc2(Toc const & toc_list, size_t from, size_t to, int depth);
337 void expandToc(Buffer const * buf);
338 void expandPasteRecent(Buffer const * buf);
339 void expandToolbars();
340 void expandBranches(Buffer const * buf);
341 void expandIndices(Buffer const * buf, bool listof = false);
342 void expandIndicesContext(Buffer const * buf, bool listof = false);
343 void expandCiteStyles(BufferView const *);
344 void expandGraphicsGroups(BufferView const *);
345 void expandSpellingSuggestions(BufferView const *);
346 void expandLanguageSelector(Buffer const * buf);
354 /// Helper for std::find_if
358 MenuNamesEqual(QString const & name) : name_(name) {}
359 bool operator()(MenuDefinition const & menu) const { return menu.name() == name_; }
366 typedef std::vector<MenuDefinition> MenuList;
368 typedef MenuList::const_iterator const_iterator;
370 typedef MenuList::iterator iterator;
372 /////////////////////////////////////////////////////////////////////
373 // MenuDefinition implementation
374 /////////////////////////////////////////////////////////////////////
376 void MenuDefinition::addWithStatusCheck(MenuItem const & i)
380 case MenuItem::Command: {
381 FuncStatus status = lyx::getStatus(i.func());
382 if (status.unknown() || (!status.enabled() && i.optional()))
385 items_.back().status(status);
389 case MenuItem::Submenu: {
390 bool enabled = false;
391 if (i.hasSubmenu()) {
392 for (const_iterator cit = i.submenu().begin();
393 cit != i.submenu().end(); ++cit) {
394 // Only these kind of items affect the status of the submenu
395 if ((cit->kind() == MenuItem::Command
396 || cit->kind() == MenuItem::Submenu
397 || cit->kind() == MenuItem::Help)
398 && cit->status().enabled()) {
404 if (enabled || !i.optional()) {
406 items_.back().status().setEnabled(enabled);
411 case MenuItem::Separator:
412 if (!items_.empty() && items_.back().kind() != MenuItem::Separator)
422 void MenuDefinition::read(Lexer & lex)
439 md_indiceslistscontext,
453 md_spellingsuggestions,
457 LexerKeyword menutags[] = {
458 { "bookmarks", md_bookmarks },
459 { "branches", md_branches },
460 { "charstyles", md_charstyles },
461 { "citestyles", md_citestyles },
462 { "custom", md_custom },
463 { "documents", md_documents },
464 { "elements", md_elements },
465 { "end", md_endmenu },
466 { "exportformats", md_exportformats },
467 { "floatinsert", md_floatinsert },
468 { "floatlistinsert", md_floatlistinsert },
469 { "graphicsgroups", md_graphicsgroups },
470 { "importformats", md_importformats },
471 { "indices", md_indices },
472 { "indicescontext", md_indicescontext },
473 { "indiceslists", md_indiceslists },
474 { "indiceslistscontext", md_indiceslistscontext },
476 { "languageselector", md_languageselector },
477 { "lastfiles", md_lastfiles },
478 { "optitem", md_optitem },
479 { "optsubmenu", md_optsubmenu },
480 { "pasterecent", md_pasterecent },
481 { "separator", md_separator },
482 { "spellingsuggestions", md_spellingsuggestions },
483 { "submenu", md_submenu },
485 { "toolbars", md_toolbars },
486 { "updateformats", md_updateformats },
487 { "viewformats", md_viewformats }
490 lex.pushTable(menutags);
491 lex.setContext("MenuDefinition::read: ");
494 bool optional = false;
496 while (lex.isOK() && !quit) {
500 // fallback to md_item
503 docstring const name = translateIfPossible(lex.getDocString());
505 string const command = lex.getString();
506 FuncRequest func = lyxaction.lookupFunc(command);
507 FuncRequest::Origin origin = FuncRequest::MENU;
508 if (name_.startsWith("context-toc-"))
509 origin = FuncRequest::TOC;
510 add(MenuItem(MenuItem::Command, toqstr(name), func, QString(), optional, origin));
516 add(MenuItem(MenuItem::Separator));
520 add(MenuItem(MenuItem::Lastfiles));
524 add(MenuItem(MenuItem::CharStyles));
528 add(MenuItem(MenuItem::Custom));
532 add(MenuItem(MenuItem::Elements));
536 add(MenuItem(MenuItem::Documents));
540 add(MenuItem(MenuItem::Bookmarks));
544 add(MenuItem(MenuItem::Toc));
548 add(MenuItem(MenuItem::ViewFormats));
551 case md_updateformats:
552 add(MenuItem(MenuItem::UpdateFormats));
555 case md_exportformats:
556 add(MenuItem(MenuItem::ExportFormats));
559 case md_importformats:
560 add(MenuItem(MenuItem::ImportFormats));
563 case md_floatlistinsert:
564 add(MenuItem(MenuItem::FloatListInsert));
568 add(MenuItem(MenuItem::FloatInsert));
572 add(MenuItem(MenuItem::PasteRecent));
576 add(MenuItem(MenuItem::Toolbars));
580 add(MenuItem(MenuItem::Branches));
584 add(MenuItem(MenuItem::CiteStyles));
587 case md_graphicsgroups:
588 add(MenuItem(MenuItem::GraphicsGroups));
591 case md_spellingsuggestions:
592 add(MenuItem(MenuItem::SpellingSuggestions));
595 case md_languageselector:
596 add(MenuItem(MenuItem::LanguageSelector));
600 add(MenuItem(MenuItem::Indices));
603 case md_indicescontext:
604 add(MenuItem(MenuItem::IndicesContext));
607 case md_indiceslists:
608 add(MenuItem(MenuItem::IndicesLists));
611 case md_indiceslistscontext:
612 add(MenuItem(MenuItem::IndicesListsContext));
617 // fallback to md_submenu
620 docstring const mlabel = translateIfPossible(lex.getDocString());
622 docstring const mname = lex.getDocString();
623 add(MenuItem(MenuItem::Submenu,
624 toqstr(mlabel), toqstr(mname), QString(), optional));
634 lex.printError("Unknown menu tag");
642 MenuItem const & MenuDefinition::operator[](size_type i) const
648 bool MenuDefinition::hasFunc(FuncRequest const & func) const
650 for (const_iterator it = begin(), et = end(); it != et; ++it)
651 if (it->func() == func)
657 void MenuDefinition::cat(MenuDefinition const & other)
659 const_iterator et = other.end();
660 for (const_iterator it = other.begin(); it != et; ++it)
665 void MenuDefinition::checkShortcuts() const
667 // This is a quadratic algorithm, but we do not care because
668 // menus are short enough
669 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
670 QString shortcut = it1->shortcut();
671 if (shortcut.isEmpty())
673 if (!it1->label().contains(shortcut))
674 LYXERR0("Menu warning: menu entry \""
676 << "\" does not contain shortcut `"
677 << shortcut << "'.");
678 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
679 if (!it2->shortcut().compare(shortcut, Qt::CaseInsensitive)) {
680 LYXERR0("Menu warning: menu entries "
681 << '"' << it1->fulllabel()
682 << "\" and \"" << it2->fulllabel()
683 << "\" share the same shortcut.");
690 bool MenuDefinition::searchMenu(FuncRequest const & func, docstring_list & names) const
692 const_iterator m = begin();
693 const_iterator m_end = end();
694 for (; m != m_end; ++m) {
695 if (m->kind() == MenuItem::Command && m->func() == func) {
696 names.push_back(qstring_to_ucs4(m->label()));
699 if (m->kind() == MenuItem::Submenu) {
700 names.push_back(qstring_to_ucs4(m->label()));
701 if (!m->hasSubmenu()) {
702 LYXERR(Debug::GUI, "Warning: non existing sub menu label="
703 << m->label() << " name=" << m->submenuname());
707 if (m->submenu().searchMenu(func, names))
716 bool compareFormat(Format const * p1, Format const * p2)
722 QString limitStringLength(docstring const & str)
724 size_t const max_item_length = 45;
726 if (str.size() > max_item_length)
727 return toqstr(str.substr(0, max_item_length - 3) + "...");
733 void MenuDefinition::expandGraphicsGroups(BufferView const * bv)
738 graphics::getGraphicsGroups(bv->buffer(), grp);
742 set<string>::const_iterator it = grp.begin();
743 set<string>::const_iterator end = grp.end();
744 add(MenuItem(MenuItem::Command, qt_("No Group"),
745 FuncRequest(LFUN_SET_GRAPHICS_GROUP)));
746 for (; it != end; it++) {
747 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(*it) + '|',
748 FuncRequest(LFUN_SET_GRAPHICS_GROUP, *it)));
753 void MenuDefinition::expandSpellingSuggestions(BufferView const * bv)
758 docstring_list suggestions;
759 Cursor const & cur = bv->cursor();
760 Paragraph const & par = cur.paragraph();
761 pos_type from = cur.pos();
763 SpellChecker::Result res = par.spellCheck(from, to, wl, suggestions, true, true);
765 case SpellChecker::UNKNOWN_WORD:
766 if (lyxrc.spellcheck_continuously) {
767 LYXERR(Debug::GUI, "Misspelled Word! Suggested Words = ");
768 docstring const & selection = cur.selectionAsString(false);
769 if (!cur.selection() || selection == wl.word()) {
771 size_t m = 10; // first submenu index
772 MenuItem item(MenuItem::Submenu, qt_("More Spelling Suggestions"));
773 item.setSubmenu(MenuDefinition(qt_("More Spelling Suggestions")));
774 for (; i != suggestions.size(); ++i) {
775 docstring const & suggestion = suggestions[i];
776 LYXERR(Debug::GUI, suggestion);
777 MenuItem w(MenuItem::Command, toqstr(suggestion),
778 FuncRequest(LFUN_WORD_REPLACE,
779 replace2string(suggestion,selection,
780 true, true, false, false)));
784 item.submenu().add(w);
789 add(MenuItem(MenuItem::Separator));
790 docstring const arg = wl.word() + " " + from_ascii(wl.lang()->lang());
791 add(MenuItem(MenuItem::Command, qt_("Add to personal dictionary|n"),
792 FuncRequest(LFUN_SPELLING_ADD, arg)));
793 add(MenuItem(MenuItem::Command, qt_("Ignore all|I"),
794 FuncRequest(LFUN_SPELLING_IGNORE, arg)));
798 case SpellChecker::LEARNED_WORD: {
799 LYXERR(Debug::GUI, "Learned Word.");
800 docstring const arg = wl.word() + " " + from_ascii(wl.lang()->lang());
801 add(MenuItem(MenuItem::Command, qt_("Remove from personal dictionary|r"),
802 FuncRequest(LFUN_SPELLING_REMOVE, arg)));
805 case SpellChecker::WORD_OK:
806 case SpellChecker::COMPOUND_WORD:
807 case SpellChecker::ROOT_FOUND:
808 case SpellChecker::IGNORED_WORD:
813 struct sortLanguageByName {
814 bool operator()(const Language * a, const Language * b) const {
815 return qt_(a->display()).localeAwareCompare(qt_(b->display())) < 0;
819 void MenuDefinition::expandLanguageSelector(Buffer const * buf)
824 std::set<Language const *> languages_buffer =
825 buf->masterBuffer()->getLanguages();
827 if (languages_buffer.size() < 2)
830 std::set<Language const *, sortLanguageByName> languages;
832 std::set<Language const *>::const_iterator const beg =
833 languages_buffer.begin();
834 for (std::set<Language const *>::const_iterator cit = beg;
835 cit != languages_buffer.end(); ++cit) {
836 languages.insert(*cit);
839 MenuItem item(MenuItem::Submenu, qt_("Language|L"));
840 item.setSubmenu(MenuDefinition(qt_("Language")));
841 QString morelangs = qt_("More Languages ...|M");
842 QStringList accelerators;
843 if (morelangs.contains('|'))
844 accelerators.append(morelangs.section('|', -1));
845 std::set<Language const *, sortLanguageByName>::const_iterator const begin = languages.begin();
846 for (std::set<Language const *, sortLanguageByName>::const_iterator cit = begin;
847 cit != languages.end(); ++cit) {
848 QString label = qt_((*cit)->display());
849 // try to add an accelerator
850 bool success = false;
851 // try capitals first
852 for (int i = 0; i < label.size(); ++i) {
853 QChar const ch = label[i];
856 if (!accelerators.contains(ch, Qt::CaseInsensitive)) {
857 label = label + toqstr("|") + ch;
858 accelerators.append(ch);
863 // if all capitals are taken, try the rest
865 for (int i = 0; i < label.size(); ++i) {
866 if (label[i].isSpace())
868 QString const ch = QString(label[i]);
869 if (!accelerators.contains(ch, Qt::CaseInsensitive)) {
870 label = label + toqstr("|") + ch;
871 accelerators.append(ch);
876 MenuItem w(MenuItem::Command, label,
877 FuncRequest(LFUN_LANGUAGE, (*cit)->lang()));
878 item.submenu().addWithStatusCheck(w);
880 item.submenu().add(MenuItem(MenuItem::Separator));
881 item.submenu().add(MenuItem(MenuItem::Command, morelangs,
882 FuncRequest(LFUN_DIALOG_SHOW, "character")));
887 void MenuDefinition::expandLastfiles()
889 LastFilesSection::LastFiles const & lf = theSession().lastFiles().lastFiles();
890 LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
894 for (; lfit != lf.end() && ii <= lyxrc.num_lastfiles; ++lfit, ++ii) {
895 string const file = lfit->absFileName();
896 QString const short_path = toqstr(makeDisplayPath(file, 30));
897 QString const long_path = toqstr(makeDisplayPath(file));
900 label = QString("%1. %2|%3").arg(ii).arg(short_path).arg(ii);
902 label = QString("%1. %2").arg(ii).arg(short_path);
903 add(MenuItem(MenuItem::Command, label,
904 FuncRequest(LFUN_FILE_OPEN, file), long_path));
909 void MenuDefinition::expandDocuments()
911 MenuItem item(MenuItem::Submenu, qt_("Hidden|H"));
912 item.setSubmenu(MenuDefinition(qt_("Hidden|H")));
914 Buffer * first = theBufferList().first();
916 add(MenuItem(MenuItem::Info, qt_("<No Documents Open>")));
922 if (!guiApp->currentView())
924 GuiWorkArea * wa = guiApp->currentView()->workArea(i);
927 Buffer const & b = wa->bufferView().buffer();
928 QString label = toqstr(b.fileName().displayName(20));
932 label = QString::number(i) + ". " + label + '|' + QString::number(i);
933 add(MenuItem(MenuItem::Command, label,
934 FuncRequest(LFUN_BUFFER_SWITCH, b.absFileName())));
941 // We cannot use a for loop as the buffer list cycles.
943 bool const shown = guiApp->currentView()
944 ? guiApp->currentView()->workArea(*b) : false;
946 QString label = toqstr(b->fileName().displayName(20));
950 label = QString::number(i) + ". " + label + '|' + QString::number(i);
951 item.submenu().add(MenuItem(MenuItem::Command, label,
952 FuncRequest(LFUN_BUFFER_SWITCH, b->absFileName())));
955 b = theBufferList().next(b);
956 } while (b != first);
958 if (!item.submenu().empty())
963 void MenuDefinition::expandBookmarks()
965 lyx::BookmarksSection const & bm = theSession().bookmarks();
968 for (size_t i = 1; i <= bm.size(); ++i) {
970 string const file = bm.bookmark(i).filename.absFileName();
971 QString const label = QString("%1. %2|%3").arg(i)
972 .arg(toqstr(makeDisplayPath(file, 20))).arg(i);
973 add(MenuItem(MenuItem::Command, label,
974 FuncRequest(LFUN_BOOKMARK_GOTO, convert<docstring>(i))));
979 add(MenuItem(MenuItem::Info, qt_("<No Bookmarks Saved Yet>")));
983 void MenuDefinition::expandFormats(MenuItem::Kind kind, Buffer const * buf)
985 if (!buf && kind != MenuItem::ImportFormats)
988 typedef vector<Format const *> Formats;
993 case MenuItem::ImportFormats:
994 formats = theConverters().importableFormats();
995 action = LFUN_BUFFER_IMPORT;
997 case MenuItem::ViewFormats:
998 formats = buf->exportableFormats(true);
999 action = LFUN_BUFFER_VIEW;
1001 case MenuItem::UpdateFormats:
1002 formats = buf->exportableFormats(true);
1003 action = LFUN_BUFFER_UPDATE;
1006 formats = buf->exportableFormats(false);
1007 action = LFUN_BUFFER_EXPORT;
1009 sort(formats.begin(), formats.end(), &compareFormat);
1011 bool const view_update = (kind == MenuItem::ViewFormats
1012 || kind == MenuItem::UpdateFormats);
1016 smenue = (kind == MenuItem::ViewFormats ?
1017 qt_("View (Other Formats)|F")
1018 : qt_("Update (Other Formats)|p"));
1019 MenuItem item(MenuItem::Submenu, smenue);
1020 item.setSubmenu(MenuDefinition(smenue));
1022 Formats::const_iterator fit = formats.begin();
1023 Formats::const_iterator end = formats.end();
1024 for (; fit != end ; ++fit) {
1025 if ((*fit)->dummy())
1028 docstring lab = from_utf8((*fit)->prettyname());
1029 docstring scut = from_utf8((*fit)->shortcut());
1030 docstring const tmplab = lab;
1033 lab += char_type('|') + scut;
1034 docstring lab_i18n = translateIfPossible(lab);
1035 bool const untranslated = (lab == lab_i18n);
1036 QString const shortcut = toqstr(split(lab_i18n, lab, '|'));
1037 QString label = toqstr(lab);
1039 // this might happen if the shortcut
1040 // has been redefined
1041 label = toqstr(translateIfPossible(tmplab));
1044 case MenuItem::ImportFormats:
1047 case MenuItem::ViewFormats:
1048 case MenuItem::UpdateFormats:
1049 if ((*fit)->name() == buf->getDefaultOutputFormat()) {
1050 docstring lbl = (kind == MenuItem::ViewFormats ?
1051 bformat(_("View [%1$s]|V"), qstring_to_ucs4(label))
1052 : bformat(_("Update [%1$s]|U"), qstring_to_ucs4(label)));
1053 MenuItem w(MenuItem::Command, toqstr(lbl),
1054 FuncRequest(action));
1058 case MenuItem::ExportFormats:
1059 if (!(*fit)->inExportMenu())
1063 LASSERT(false, /**/);
1066 if (!shortcut.isEmpty())
1067 label += '|' + shortcut;
1071 item.submenu().addWithStatusCheck(MenuItem(MenuItem::Command, label,
1072 FuncRequest(action, (*fit)->name())));
1074 item.submenu().add(MenuItem(MenuItem::Command, label,
1075 FuncRequest(action, (*fit)->name())));
1078 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1079 FuncRequest(action, (*fit)->name())));
1081 add(MenuItem(MenuItem::Command, label,
1082 FuncRequest(action, (*fit)->name())));
1090 void MenuDefinition::expandFloatListInsert(Buffer const * buf)
1095 FloatList const & floats = buf->params().documentClass().floats();
1096 FloatList::const_iterator cit = floats.begin();
1097 FloatList::const_iterator end = floats.end();
1099 for (; cit != end; ++cit) {
1100 if (!cit->second.usesFloatPkg()) {
1101 // Different floats could declare the same ListCommand. We only
1102 // want it on the list once, though.
1103 string const & list_cmd = cit->second.listCommand();
1104 if (list_cmd.empty())
1105 // we do not know how to generate such a list
1107 // This form of insert returns an iterator pointing to the newly
1108 // inserted element OR the existing element with that value, and
1109 // a bool indicating whether we inserted a new element. So we can
1110 // see if one is there and insert it if not all at once.
1111 pair<set<string>::iterator, bool> ret = seen.insert(list_cmd);
1115 string const & list_name = cit->second.listName();
1116 addWithStatusCheck(MenuItem(MenuItem::Command, qt_(list_name),
1117 FuncRequest(LFUN_FLOAT_LIST_INSERT, cit->second.floattype())));
1122 void MenuDefinition::expandFloatInsert(Buffer const * buf)
1127 FloatList const & floats = buf->params().documentClass().floats();
1128 FloatList::const_iterator cit = floats.begin();
1129 FloatList::const_iterator end = floats.end();
1130 for (; cit != end; ++cit) {
1132 QString const label = qt_(cit->second.name());
1133 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1134 FuncRequest(LFUN_FLOAT_INSERT,
1135 cit->second.floattype())));
1140 void MenuDefinition::expandFlexInsert(
1141 Buffer const * buf, InsetLayout::InsetLyXType type)
1146 TextClass::InsetLayouts const & insetLayouts =
1147 buf->params().documentClass().insetLayouts();
1148 TextClass::InsetLayouts::const_iterator cit = insetLayouts.begin();
1149 TextClass::InsetLayouts::const_iterator end = insetLayouts.end();
1150 for (; cit != end; ++cit) {
1151 if (cit->second.lyxtype() == type) {
1152 docstring label = cit->first;
1153 // we remove the "Flex:" prefix, if it is present
1154 if (prefixIs(label, from_utf8("Flex:")))
1155 label = label.substr(5);
1156 addWithStatusCheck(MenuItem(MenuItem::Command,
1157 toqstr(translateIfPossible(label)),
1158 FuncRequest(LFUN_FLEX_INSERT, Lexer::quoteString(label))));
1161 // FIXME This is a little clunky.
1162 if (items_.empty() && type == InsetLayout::CUSTOM)
1163 add(MenuItem(MenuItem::Help, qt_("No Custom Insets Defined!")));
1167 size_t const max_number_of_items = 25;
1169 void MenuDefinition::expandToc2(Toc const & toc_list,
1170 size_t from, size_t to, int depth)
1172 int shortcut_count = 0;
1174 // check whether depth is smaller than the smallest depth in toc.
1175 int min_depth = 1000;
1176 for (size_t i = from; i < to; ++i)
1177 min_depth = min(min_depth, toc_list[i].depth());
1178 if (min_depth > depth)
1181 if (to - from <= max_number_of_items) {
1182 for (size_t i = from; i < to; ++i) {
1183 QString label(4 * max(0, toc_list[i].depth() - depth), ' ');
1184 label += limitStringLength(toc_list[i].str());
1185 if (toc_list[i].depth() == depth) {
1187 if (shortcut_count < 9) {
1188 if (label.contains(QString::number(shortcut_count + 1)))
1189 label += QString::number(++shortcut_count);
1192 add(MenuItem(MenuItem::Command, label,
1193 FuncRequest(toc_list[i].action())));
1198 size_t new_pos = pos + 1;
1199 while (new_pos < to && toc_list[new_pos].depth() > depth)
1202 QString label(4 * max(0, toc_list[pos].depth() - depth), ' ');
1203 label += limitStringLength(toc_list[pos].str());
1204 if (toc_list[pos].depth() == depth) {
1206 if (shortcut_count < 9) {
1207 if (label.contains(QString::number(shortcut_count + 1)))
1208 label += QString::number(++shortcut_count);
1211 if (new_pos == pos + 1) {
1212 add(MenuItem(MenuItem::Command,
1213 label, FuncRequest(toc_list[pos].action())));
1216 sub.expandToc2(toc_list, pos, new_pos, depth + 1);
1217 MenuItem item(MenuItem::Submenu, label);
1218 item.setSubmenu(sub);
1227 void MenuDefinition::expandToc(Buffer const * buf)
1229 // To make things very cleanly, we would have to pass buf to
1230 // all MenuItem constructors and to expandToc2. However, we
1231 // know that all the entries in a TOC will be have status_ ==
1232 // OK, so we avoid this unnecessary overhead (JMarc)
1235 add(MenuItem(MenuItem::Info, qt_("<No Document Open>")));
1239 // Add an entry for the master doc if this is a child doc
1240 Buffer const * const master = buf->masterBuffer();
1241 if (buf != master) {
1242 ParIterator const pit = par_iterator_begin(master->inset());
1243 string const arg = convert<string>(pit->id());
1244 FuncRequest f(LFUN_PARAGRAPH_GOTO, arg);
1245 add(MenuItem(MenuItem::Command, qt_("Master Document"), f));
1248 MenuDefinition other_lists;
1250 FloatList const & floatlist = buf->params().documentClass().floats();
1251 TocList const & toc_list = buf->tocBackend().tocs();
1252 TocList::const_iterator cit = toc_list.begin();
1253 TocList::const_iterator end = toc_list.end();
1254 for (; cit != end; ++cit) {
1255 // Handle this later
1256 if (cit->first == "tableofcontents")
1259 MenuDefinition submenu;
1260 if (cit->second.size() >= 30) {
1261 FuncRequest f(LFUN_DIALOG_SHOW, "toc " + cit->first);
1262 submenu.add(MenuItem(MenuItem::Command, qt_("Open Navigator..."), f));
1264 TocIterator ccit = cit->second.begin();
1265 TocIterator eend = cit->second.end();
1266 for (; ccit != eend; ++ccit) {
1267 submenu.add(MenuItem(MenuItem::Command,
1268 limitStringLength(ccit->str()) + '|',
1269 FuncRequest(ccit->action())));
1273 MenuItem item(MenuItem::Submenu, guiName(cit->first, buf->params()));
1274 item.setSubmenu(submenu);
1275 if (floatlist.typeExist(cit->first) || cit->first == "child") {
1276 // Those two types deserve to be in the main menu.
1277 item.setSubmenu(submenu);
1280 other_lists.add(item);
1282 if (!other_lists.empty()) {
1283 MenuItem item(MenuItem::Submenu, qt_("Other Lists"));
1284 item.setSubmenu(other_lists);
1288 // Handle normal TOC
1289 cit = toc_list.find("tableofcontents");
1291 LYXERR(Debug::GUI, "No table of contents.");
1293 if (cit->second.size() > 0 )
1294 expandToc2(cit->second, 0, cit->second.size(), 0);
1296 add(MenuItem(MenuItem::Info, qt_("<Empty Table of Contents>")));
1301 void MenuDefinition::expandPasteRecent(Buffer const * buf)
1303 docstring_list const sel = cap::availableSelections(buf);
1305 docstring_list::const_iterator cit = sel.begin();
1306 docstring_list::const_iterator end = sel.end();
1308 for (unsigned int index = 0; cit != end; ++cit, ++index) {
1309 add(MenuItem(MenuItem::Command, toqstr(*cit) + '|',
1310 FuncRequest(LFUN_PASTE, convert<string>(index))));
1315 void MenuDefinition::expandToolbars()
1317 MenuDefinition other_lists;
1318 // extracts the toolbars from the backend
1319 Toolbars::Infos::const_iterator cit = guiApp->toolbars().begin();
1320 Toolbars::Infos::const_iterator end = guiApp->toolbars().end();
1321 for (; cit != end; ++cit) {
1322 MenuItem const item(MenuItem::Command, toqstr(cit->gui_name),
1323 FuncRequest(LFUN_TOOLBAR_TOGGLE, cit->name));
1324 if (guiApp->toolbars().isMainToolbar(cit->name))
1327 other_lists.add(item);
1330 if (!other_lists.empty()) {
1331 MenuItem item(MenuItem::Submenu, qt_("Other Toolbars"));
1332 item.setSubmenu(other_lists);
1338 void MenuDefinition::expandBranches(Buffer const * buf)
1343 BufferParams const & master_params = buf->masterBuffer()->params();
1344 BufferParams const & params = buf->params();
1345 if (params.branchlist().empty() && master_params.branchlist().empty() ) {
1346 add(MenuItem(MenuItem::Help, qt_("No Branches Set for Document!")));
1350 BranchList::const_iterator cit = master_params.branchlist().begin();
1351 BranchList::const_iterator end = master_params.branchlist().end();
1353 for (int ii = 1; cit != end; ++cit, ++ii) {
1354 docstring label = cit->branch();
1356 label = convert<docstring>(ii) + ". " + label
1357 + char_type('|') + convert<docstring>(ii);
1359 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1360 FuncRequest(LFUN_BRANCH_INSERT,
1364 if (buf == buf->masterBuffer())
1367 MenuDefinition child_branches;
1369 BranchList::const_iterator ccit = params.branchlist().begin();
1370 BranchList::const_iterator cend = params.branchlist().end();
1372 for (int ii = 1; ccit != cend; ++ccit, ++ii) {
1373 docstring label = ccit->branch();
1375 label = convert<docstring>(ii) + ". " + label
1376 + char_type('|') + convert<docstring>(ii);
1378 label += char_type('|');
1379 child_branches.addWithStatusCheck(MenuItem(MenuItem::Command,
1381 FuncRequest(LFUN_BRANCH_INSERT,
1385 if (!child_branches.empty()) {
1386 MenuItem item(MenuItem::Submenu, qt_("Child Document"));
1387 item.setSubmenu(child_branches);
1393 void MenuDefinition::expandIndices(Buffer const * buf, bool listof)
1398 BufferParams const & params = buf->masterBuffer()->params();
1399 if (!params.use_indices) {
1401 addWithStatusCheck(MenuItem(MenuItem::Command,
1402 qt_("Index List|I"),
1403 FuncRequest(LFUN_INDEX_PRINT,
1404 from_ascii("idx"))));
1406 addWithStatusCheck(MenuItem(MenuItem::Command,
1407 qt_("Index Entry|d"),
1408 FuncRequest(LFUN_INDEX_INSERT,
1409 from_ascii("idx"))));
1413 if (params.indiceslist().empty())
1416 IndicesList::const_iterator cit = params.indiceslist().begin();
1417 IndicesList::const_iterator end = params.indiceslist().end();
1419 for (int ii = 1; cit != end; ++cit, ++ii) {
1421 docstring const label =
1422 bformat(_("Index: %1$s"), cit->index());
1423 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1424 FuncRequest(LFUN_INDEX_PRINT, cit->shortcut())));
1426 docstring const label =
1427 bformat(_("Index Entry (%1$s)"), cit->index());
1428 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1429 FuncRequest(LFUN_INDEX_INSERT, cit->shortcut())));
1435 void MenuDefinition::expandIndicesContext(Buffer const * buf, bool listof)
1440 BufferParams const & params = buf->masterBuffer()->params();
1441 if (!params.use_indices || params.indiceslist().empty())
1444 IndicesList::const_iterator cit = params.indiceslist().begin();
1445 IndicesList::const_iterator end = params.indiceslist().end();
1447 for (int ii = 1; cit != end; ++cit, ++ii) {
1449 InsetCommandParams p(INDEX_PRINT_CODE);
1450 p["type"] = cit->shortcut();
1451 string const data = InsetCommand::params2string(p);
1452 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(cit->index()),
1453 FuncRequest(LFUN_INSET_MODIFY, data)));
1455 docstring const label =
1456 bformat(_("Index Entry (%1$s)"), cit->index());
1457 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1458 FuncRequest(LFUN_INSET_MODIFY,
1459 from_ascii("changetype ") + cit->shortcut())));
1465 void MenuDefinition::expandCiteStyles(BufferView const * bv)
1470 Inset const * inset = bv->cursor().nextInset();
1471 if (!inset || inset->lyxCode() != CITE_CODE) {
1472 add(MenuItem(MenuItem::Command,
1473 qt_("No Citation in Scope!"),
1474 FuncRequest(LFUN_NOACTION)));
1477 InsetCommand const * citinset =
1478 static_cast<InsetCommand const *>(inset);
1480 Buffer const * buf = &bv->buffer();
1481 docstring key = citinset->getParam("key");
1482 // we can only handle one key currently
1483 if (contains(key, ','))
1484 key = qstring_to_ucs4(toqstr(key).split(',')[0]);
1486 vector<CiteStyle> citeStyleList = citeStyles(buf->params().citeEngine());
1487 docstring_list citeStrings =
1488 buf->masterBibInfo().getCiteStrings(key, bv->buffer());
1490 docstring_list::const_iterator cit = citeStrings.begin();
1491 docstring_list::const_iterator end = citeStrings.end();
1493 for (int ii = 1; cit != end; ++cit, ++ii) {
1494 docstring label = *cit;
1496 CiteStyle cst = citeStyleList[ii - 1];
1498 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1499 FuncRequest(LFUN_INSET_MODIFY,
1500 "changetype " + from_utf8(citationStyleToString(cs)))));
1507 /////////////////////////////////////////////////////////////////////
1508 // Menu::Impl definition and implementation
1509 /////////////////////////////////////////////////////////////////////
1513 /// populates the menu or one of its submenu
1514 /// This is used as a recursive function
1515 void populate(QMenu & qMenu, MenuDefinition const & menu);
1517 /// Only needed for top level menus.
1518 MenuDefinition * top_level_menu;
1521 /// the name of this menu
1527 /// Get a MenuDefinition item label from the menu backend
1528 static QString label(MenuItem const & mi)
1530 QString label = mi.label();
1531 label.replace("&", "&&");
1533 QString shortcut = mi.shortcut();
1534 if (!shortcut.isEmpty()) {
1535 int pos = label.indexOf(shortcut);
1537 //label.insert(pos, 1, char_type('&'));
1538 label.replace(pos, 0, "&");
1541 QString const binding = mi.binding();
1542 if (!binding.isEmpty())
1543 label += '\t' + binding;
1548 void Menu::Impl::populate(QMenu & qMenu, MenuDefinition const & menu)
1550 LYXERR(Debug::GUI, "populating menu " << menu.name());
1551 if (menu.size() == 0) {
1552 LYXERR(Debug::GUI, "\tERROR: empty menu " << menu.name());
1555 LYXERR(Debug::GUI, " ***** menu entries " << menu.size());
1556 MenuDefinition::const_iterator m = menu.begin();
1557 MenuDefinition::const_iterator end = menu.end();
1558 for (; m != end; ++m) {
1559 if (m->kind() == MenuItem::Separator)
1560 qMenu.addSeparator();
1561 else if (m->kind() == MenuItem::Submenu) {
1562 QMenu * subMenu = qMenu.addMenu(label(*m));
1563 populate(*subMenu, m->submenu());
1564 subMenu->setEnabled(m->status().enabled());
1566 // we have a MenuItem::Command
1567 qMenu.addAction(new Action(view, QIcon(), label(*m),
1568 m->func(), m->tooltip(), &qMenu));
1574 class AlwaysMnemonicStyle : public QProxyStyle {
1576 int styleHint(StyleHint hint, const QStyleOption *opt = 0, const QWidget *widget = 0,
1577 QStyleHintReturn *returnData = 0) const
1579 if (hint == QStyle::SH_UnderlineShortcut)
1581 return QProxyStyle::styleHint(hint, opt, widget, returnData);
1586 /////////////////////////////////////////////////////////////////////
1587 // Menu implementation
1588 /////////////////////////////////////////////////////////////////////
1590 Menu::Menu(GuiView * gv, QString const & name, bool top_level, bool keyboard)
1591 : QMenu(gv), d(new Menu::Impl)
1595 setStyle(new AlwaysMnemonicStyle);
1597 d->top_level_menu = top_level? new MenuDefinition : 0;
1601 if (d->top_level_menu)
1602 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
1608 delete d->top_level_menu;
1613 void Menu::updateView()
1615 guiApp->menus().updateMenu(this);
1619 /////////////////////////////////////////////////////////////////////
1620 // Menus::Impl definition and implementation
1621 /////////////////////////////////////////////////////////////////////
1623 struct Menus::Impl {
1625 bool hasMenu(QString const &) const;
1627 MenuDefinition & getMenu(QString const &);
1629 MenuDefinition const & getMenu(QString const &) const;
1631 /// Expands some special entries of the menu
1632 /** The entries with the following kind are expanded to a
1633 sequence of Command MenuItems: Lastfiles, Documents,
1634 ViewFormats, ExportFormats, UpdateFormats, Branches, Indices
1636 void expand(MenuDefinition const & frommenu, MenuDefinition & tomenu,
1637 BufferView const *) const;
1639 /// Initialize specific MACOS X menubar
1640 void macxMenuBarInit(GuiView * view, QMenuBar * qmb);
1642 /// Mac special menu.
1643 /** This defines a menu whose entries list the FuncRequests
1644 that will be removed by expand() in other menus. This is
1645 used by the Qt/Mac code.
1647 NOTE: Qt does not remove the menu items when clearing a QMenuBar,
1648 such that the items will keep accessing the FuncRequests in
1649 the MenuDefinition. While Menus::Impl might be recreated,
1650 we keep mac_special_menu_ in memory by making it static.
1652 static MenuDefinition mac_special_menu_;
1657 MenuDefinition menubar_;
1659 typedef QMap<GuiView *, QHash<QString, Menu*> > NameMap;
1661 /// name to menu for \c menu() method.
1666 MenuDefinition Menus::Impl::mac_special_menu_;
1670 Here is what the Qt documentation says about how a menubar is chosen:
1672 1) If the window has a QMenuBar then it is used. 2) If the window
1673 is a modal then its menubar is used. If no menubar is specified
1674 then a default menubar is used (as documented below) 3) If the
1675 window has no parent then the default menubar is used (as
1678 The above 3 steps are applied all the way up the parent window
1679 chain until one of the above are satisifed. If all else fails a
1680 default menubar will be created, the default menubar on Qt/Mac is
1681 an empty menubar, however you can create a different default
1682 menubar by creating a parentless QMenuBar, the first one created
1683 will thus be designated the default menubar, and will be used
1684 whenever a default menubar is needed.
1686 Thus, for Qt/Mac, we add the menus to a free standing menubar, so
1687 that this menubar will be used also when one of LyX' dialogs has
1690 void Menus::Impl::macxMenuBarInit(GuiView * view, QMenuBar * qmb)
1692 /* Since Qt 4.2, the qt/mac menu code has special code for
1693 specifying the role of a menu entry. However, it does not
1694 work very well with our scheme of creating menus on demand,
1695 and therefore we need to put these entries in a special
1696 invisible menu. (JMarc)
1699 /* The entries of our special mac menu. If we add support for
1700 * special entries in Menus, we could imagine something
1702 * SpecialItem About " "About LyX" "dialog-show aboutlyx"
1703 * and therefore avoid hardcoding. I am not sure it is worth
1704 * the hassle, though. (JMarc)
1706 struct MacMenuEntry {
1710 QAction::MenuRole role;
1713 MacMenuEntry entries[] = {
1714 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
1715 QAction::AboutRole},
1716 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
1717 QAction::PreferencesRole},
1718 {LFUN_RECONFIGURE, "", "Reconfigure",
1719 QAction::ApplicationSpecificRole},
1720 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
1722 const size_t num_entries = sizeof(entries) / sizeof(entries[0]);
1724 // the special menu for Menus. Fill it up only once.
1725 if (mac_special_menu_.size() == 0) {
1726 for (size_t i = 0 ; i < num_entries ; ++i) {
1727 FuncRequest const func(entries[i].action,
1728 from_utf8(entries[i].arg));
1729 mac_special_menu_.add(MenuItem(MenuItem::Command,
1730 entries[i].label, func));
1734 // add the entries to a QMenu that will eventually be empty
1735 // and therefore invisible.
1736 QMenu * qMenu = qmb->addMenu("special");
1737 MenuDefinition::const_iterator cit = mac_special_menu_.begin();
1738 MenuDefinition::const_iterator end = mac_special_menu_.end();
1739 for (size_t i = 0 ; cit != end ; ++cit, ++i) {
1740 Action * action = new Action(view, QIcon(), cit->label(),
1741 cit->func(), QString(), qMenu);
1742 action->setMenuRole(entries[i].role);
1743 qMenu->addAction(action);
1748 void Menus::Impl::expand(MenuDefinition const & frommenu,
1749 MenuDefinition & tomenu, BufferView const * bv) const
1751 if (!tomenu.empty())
1754 for (MenuDefinition::const_iterator cit = frommenu.begin();
1755 cit != frommenu.end() ; ++cit) {
1756 Buffer const * buf = bv ? &bv->buffer() : 0;
1757 switch (cit->kind()) {
1758 case MenuItem::Lastfiles:
1759 tomenu.expandLastfiles();
1762 case MenuItem::Documents:
1763 tomenu.expandDocuments();
1766 case MenuItem::Bookmarks:
1767 tomenu.expandBookmarks();
1770 case MenuItem::ImportFormats:
1771 case MenuItem::ViewFormats:
1772 case MenuItem::UpdateFormats:
1773 case MenuItem::ExportFormats:
1774 tomenu.expandFormats(cit->kind(), buf);
1777 case MenuItem::CharStyles:
1778 tomenu.expandFlexInsert(buf, InsetLayout::CHARSTYLE);
1781 case MenuItem::Custom:
1782 tomenu.expandFlexInsert(buf, InsetLayout::CUSTOM);
1785 case MenuItem::Elements:
1786 tomenu.expandFlexInsert(buf, InsetLayout::ELEMENT);
1789 case MenuItem::FloatListInsert:
1790 tomenu.expandFloatListInsert(buf);
1793 case MenuItem::FloatInsert:
1794 tomenu.expandFloatInsert(buf);
1797 case MenuItem::PasteRecent:
1798 tomenu.expandPasteRecent(buf);
1801 case MenuItem::Toolbars:
1802 tomenu.expandToolbars();
1805 case MenuItem::Branches:
1806 tomenu.expandBranches(buf);
1809 case MenuItem::Indices:
1810 tomenu.expandIndices(buf);
1813 case MenuItem::IndicesContext:
1814 tomenu.expandIndicesContext(buf);
1817 case MenuItem::IndicesLists:
1818 tomenu.expandIndices(buf, true);
1821 case MenuItem::IndicesListsContext:
1822 tomenu.expandIndicesContext(buf, true);
1825 case MenuItem::CiteStyles:
1826 tomenu.expandCiteStyles(bv);
1830 tomenu.expandToc(buf);
1833 case MenuItem::GraphicsGroups:
1834 tomenu.expandGraphicsGroups(bv);
1837 case MenuItem::SpellingSuggestions:
1838 tomenu.expandSpellingSuggestions(bv);
1841 case MenuItem::LanguageSelector:
1842 tomenu.expandLanguageSelector(buf);
1845 case MenuItem::Submenu: {
1846 MenuItem item(*cit);
1847 item.setSubmenu(MenuDefinition(cit->submenuname()));
1848 expand(getMenu(cit->submenuname()), item.submenu(), bv);
1849 tomenu.addWithStatusCheck(item);
1853 case MenuItem::Info:
1854 case MenuItem::Help:
1855 case MenuItem::Separator:
1856 tomenu.addWithStatusCheck(*cit);
1859 case MenuItem::Command:
1860 if (!mac_special_menu_.hasFunc(cit->func()))
1861 tomenu.addWithStatusCheck(*cit);
1865 // we do not want the menu to end with a separator
1866 if (!tomenu.empty() && tomenu.items_.back().kind() == MenuItem::Separator)
1867 tomenu.items_.pop_back();
1869 // Check whether the shortcuts are unique
1870 tomenu.checkShortcuts();
1874 bool Menus::Impl::hasMenu(QString const & name) const
1876 return find_if(menulist_.begin(), menulist_.end(),
1877 MenuNamesEqual(name)) != menulist_.end();
1881 MenuDefinition const & Menus::Impl::getMenu(QString const & name) const
1883 const_iterator cit = find_if(menulist_.begin(), menulist_.end(),
1884 MenuNamesEqual(name));
1885 if (cit == menulist_.end())
1886 LYXERR0("No submenu named " << name);
1887 LASSERT(cit != menulist_.end(), /**/);
1892 MenuDefinition & Menus::Impl::getMenu(QString const & name)
1894 iterator it = find_if(menulist_.begin(), menulist_.end(),
1895 MenuNamesEqual(name));
1896 if (it == menulist_.end())
1897 LYXERR0("No submenu named " << name);
1898 LASSERT(it != menulist_.end(), /**/);
1903 /////////////////////////////////////////////////////////////////////
1907 /////////////////////////////////////////////////////////////////////
1909 Menus::Menus() : d(new Impl) {}
1925 void Menus::read(Lexer & lex)
1933 LexerKeyword menutags[] = {
1934 { "end", md_endmenuset },
1935 { "menu", md_menu },
1936 { "menubar", md_menubar }
1939 // consistency check
1940 if (compare_ascii_no_case(lex.getString(), "menuset"))
1941 LYXERR0("Menus::read: ERROR wrong token: `" << lex.getString() << '\'');
1943 lex.pushTable(menutags);
1944 lex.setContext("Menus::read");
1948 while (lex.isOK() && !quit) {
1949 switch (lex.lex()) {
1951 d->menubar_.read(lex);
1955 QString const name = toqstr(lex.getDocString());
1956 if (d->hasMenu(name))
1957 d->getMenu(name).read(lex);
1959 MenuDefinition menu(name);
1961 d->menulist_.push_back(menu);
1969 lex.printError("Unknown menu tag");
1977 bool Menus::searchMenu(FuncRequest const & func,
1978 docstring_list & names) const
1980 MenuDefinition menu;
1981 d->expand(d->menubar_, menu, 0);
1982 return menu.searchMenu(func, names);
1986 void Menus::fillMenuBar(QMenuBar * qmb, GuiView * view, bool initial)
1990 // setup special mac specific menu items, but only do this
1991 // the first time a QMenuBar is created. Otherwise Qt will
1992 // create duplicate items in the application menu. It seems
1993 // that Qt does not remove them when the QMenubar is cleared.
1994 LYXERR(Debug::GUI, "Creating Mac OS X special menu bar");
1995 d->macxMenuBarInit(view, qmb);
1998 // Clear all menubar contents before filling it.
2002 LYXERR(Debug::GUI, "populating menu bar" << d->menubar_.name());
2004 if (d->menubar_.size() == 0) {
2005 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
2006 << d->menubar_.name());
2009 LYXERR(Debug::GUI, "menu bar entries " << d->menubar_.size());
2011 MenuDefinition menu;
2012 BufferView * bv = 0;
2014 bv = view->currentBufferView();
2015 d->expand(d->menubar_, menu, bv);
2017 MenuDefinition::const_iterator m = menu.begin();
2018 MenuDefinition::const_iterator end = menu.end();
2020 for (; m != end; ++m) {
2022 if (m->kind() != MenuItem::Submenu) {
2023 LYXERR(Debug::GUI, "\tERROR: not a submenu " << m->label());
2027 LYXERR(Debug::GUI, "menu bar item " << m->label()
2028 << " is a submenu named " << m->submenuname());
2030 QString name = m->submenuname();
2031 if (!d->hasMenu(name)) {
2032 LYXERR(Debug::GUI, "\tERROR: " << name
2033 << " submenu has no menu!");
2037 Menu * menu = new Menu(view, m->submenuname(), true);
2038 menu->setTitle(label(*m));
2041 d->name_map_[view][name] = menu;
2046 void Menus::updateMenu(Menu * qmenu)
2048 LYXERR(Debug::GUI, "Triggered menu: " << qmenu->d->name);
2051 if (qmenu->d->name.isEmpty())
2054 docstring identifier = qstring_to_ucs4(qmenu->d->name);
2055 MenuDefinition fromLyxMenu(qmenu->d->name);
2056 while (!identifier.empty()) {
2057 docstring menu_name;
2058 identifier = split(identifier, menu_name, ';');
2060 if (!d->hasMenu(toqstr(menu_name))) {
2061 LYXERR(Debug::GUI, "\tWARNING: non existing menu: "
2066 fromLyxMenu.cat(d->getMenu(toqstr(menu_name)));
2067 fromLyxMenu.add(MenuItem(MenuItem::Separator));
2070 if (fromLyxMenu.empty()) {
2071 qmenu->addAction(qt_("No Action Defined!"));
2075 BufferView * bv = 0;
2077 bv = qmenu->d->view->currentBufferView();
2078 d->expand(fromLyxMenu, *qmenu->d->top_level_menu, bv);
2079 qmenu->d->populate(*qmenu, *qmenu->d->top_level_menu);
2083 Menu * Menus::menu(QString const & name, GuiView & view, bool keyboard)
2085 LYXERR(Debug::GUI, "Context menu requested: " << name);
2086 Menu * menu = d->name_map_[&view].value(name, 0);
2087 if (!menu && !name.startsWith("context-")) {
2088 LYXERR0("requested context menu not found: " << name);
2092 menu = new Menu(&view, name, true, keyboard);
2093 d->name_map_[&view][name] = menu;
2097 } // namespace frontend
2100 #include "moc_Menus.cpp"