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 #if QT_VERSION >= 0x040600
75 #include <QProxyStyle>
78 #include "support/shared_ptr.h"
84 using namespace lyx::support;
92 // MacOSX specific stuff is at the end.
99 /// The type of elements that can be in a menu
107 /** This type of item explains why something is unavailable. If this
108 menuitem is in a submenu, the submenu is enabled to make sure the
109 user sees the information. */
111 /** This type of item merely shows that there might be a list or
112 something alike at this position, but the list is still empty.
113 If this item is in a submenu, the submenu will not always be
116 /** This is the list of last opened file,
117 typically for the File menu. */
119 /** This is the list of opened Documents,
120 typically for the Documents menu. */
122 /** This is the bookmarks */
126 /** This is a list of viewable formats
127 typically for the File->View menu. */
129 /** This is a list of updatable formats
130 typically for the File->Update menu. */
132 /** This is a list of exportable formats
133 typically for the File->Export menu. */
135 /** This is a list of importable formats
136 typically for the File->Import menu. */
138 /** This is the list of elements available
139 * for insertion into document. */
141 /** This is the list of user-configurable
142 insets to insert into document */
144 /** This is the list of XML elements to
145 insert into the document */
147 /** This is the list of floats that we can
148 insert a list for. */
150 /** This is the list of floats that we can
153 /** This is the list of selections that can
158 /** Available branches in document */
160 /** Available indices in document */
162 /** Context menu for indices in document */
164 /** Available index lists in document */
166 /** Context menu for available indices lists in document */
168 /** Available citation styles for a given citation */
170 /** Available graphics groups */
172 /// Words suggested by the spellchecker.
174 /** Used Languages */
178 explicit MenuItem(Kind kind) : kind_(kind), optional_(false) {}
181 QString const & label,
182 QString const & submenu = QString(),
183 QString const & tooltip = QString(),
184 bool optional = false)
185 : kind_(kind), label_(label), submenuname_(submenu),
186 tooltip_(tooltip), optional_(optional)
188 LASSERT(kind == Submenu || kind == Help || kind == Info, /**/);
192 QString const & label,
193 FuncRequest const & func,
194 QString const & tooltip = QString(),
195 bool optional = false,
196 FuncRequest::Origin origin = FuncRequest::MENU)
197 : kind_(kind), label_(label), func_(func),
198 tooltip_(tooltip), optional_(optional)
200 func_.setOrigin(origin);
203 // shared_ptr<MenuDefinition> needs this apprently...
206 /// The label of a given menuitem
207 QString label() const
209 int const index = label_.lastIndexOf('|');
210 return index == -1 ? label_ : label_.left(index);
213 /// The keyboard shortcut (usually underlined in the entry)
214 QString shortcut() const
216 int const index = label_.lastIndexOf('|');
217 return index == -1 ? QString() : label_.mid(index + 1);
219 /// The complete label, with label and shortcut separated by a '|'
220 QString fulllabel() const { return label_; }
221 /// The kind of entry
222 Kind kind() const { return kind_; }
223 /// the action (if relevant)
224 FuncRequest const & func() const { return func_; }
226 QString const & tooltip() const { return tooltip_; }
227 /// returns true if the entry should be omitted when disabled
228 bool optional() const { return optional_; }
229 /// returns the status of the lfun associated with this entry
230 FuncStatus const & status() const { return status_; }
231 /// returns the status of the lfun associated with this entry
232 FuncStatus & status() { return status_; }
233 /// returns the status of the lfun associated with this entry
234 void status(FuncStatus const & status) { status_ = status; }
236 ///returns the binding associated to this action.
237 QString binding() const
239 if (kind_ != Command)
241 // Get the keys bound to this action, but keep only the
243 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func_);
245 return toqstr(bindings.begin()->print(KeySequence::ForGui));
247 LYXERR(Debug::KBMAP, "No binding for "
248 << lyxaction.getActionName(func_.action())
249 << '(' << func_.argument() << ')');
253 /// the description of the submenu (if relevant)
254 QString const & submenuname() const { return submenuname_; }
255 /// set the description of the submenu
256 void submenuname(QString const & name) { submenuname_ = name; }
258 bool hasSubmenu() const { return !submenu_.isEmpty(); }
260 MenuDefinition const & submenu() const { return submenu_.at(0); }
261 MenuDefinition & submenu() { return submenu_[0]; }
263 void setSubmenu(MenuDefinition const & menu)
266 submenu_.append(menu);
277 QString submenuname_;
284 /// contains 0 or 1 item.
285 QList<MenuDefinition> submenu_;
289 class MenuDefinition {
292 typedef std::vector<MenuItem> ItemList;
294 typedef ItemList::const_iterator const_iterator;
296 explicit MenuDefinition(QString const & name = QString()) : name_(name) {}
301 QString const & name() const { return name_; }
303 bool empty() const { return items_.empty(); }
304 /// Clear the menu content.
305 void clear() { items_.clear(); }
307 size_t size() const { return items_.size(); }
309 MenuItem const & operator[](size_t) const;
311 const_iterator begin() const { return items_.begin(); }
313 const_iterator end() const { return items_.end(); }
315 void cat(MenuDefinition const & other);
317 // search for func in this menu iteratively, and put menu
319 bool searchMenu(FuncRequest const & func, docstring_list & names)
322 bool hasFunc(FuncRequest const &) const;
323 /// Add the menu item unconditionally
324 void add(MenuItem const & item) { items_.push_back(item); }
325 /// Checks the associated FuncRequest status before adding the
327 void addWithStatusCheck(MenuItem const &);
328 // Check whether the menu shortcuts are unique
329 void checkShortcuts() const;
331 void expandLastfiles();
332 void expandDocuments();
333 void expandBookmarks();
334 void expandFormats(MenuItem::Kind kind, Buffer const * buf);
335 void expandFloatListInsert(Buffer const * buf);
336 void expandFloatInsert(Buffer const * buf);
337 void expandFlexInsert(Buffer const * buf, InsetLayout::InsetLyXType type);
338 void expandToc2(Toc const & toc_list, size_t from, size_t to, int depth);
339 void expandToc(Buffer const * buf);
340 void expandPasteRecent(Buffer const * buf);
341 void expandToolbars();
342 void expandBranches(Buffer const * buf);
343 void expandIndices(Buffer const * buf, bool listof = false);
344 void expandIndicesContext(Buffer const * buf, bool listof = false);
345 void expandCiteStyles(BufferView const *);
346 void expandGraphicsGroups(BufferView const *);
347 void expandSpellingSuggestions(BufferView const *);
348 void expandLanguageSelector(Buffer const * buf);
356 /// Helper for std::find_if
360 MenuNamesEqual(QString const & name) : name_(name) {}
361 bool operator()(MenuDefinition const & menu) const { return menu.name() == name_; }
368 typedef std::vector<MenuDefinition> MenuList;
370 typedef MenuList::const_iterator const_iterator;
372 typedef MenuList::iterator iterator;
374 /////////////////////////////////////////////////////////////////////
375 // MenuDefinition implementation
376 /////////////////////////////////////////////////////////////////////
378 void MenuDefinition::addWithStatusCheck(MenuItem const & i)
382 case MenuItem::Command: {
383 FuncStatus status = lyx::getStatus(i.func());
384 if (status.unknown() || (!status.enabled() && i.optional()))
387 items_.back().status(status);
391 case MenuItem::Submenu: {
392 bool enabled = false;
393 if (i.hasSubmenu()) {
394 for (const_iterator cit = i.submenu().begin();
395 cit != i.submenu().end(); ++cit) {
396 // Only these kind of items affect the status of the submenu
397 if ((cit->kind() == MenuItem::Command
398 || cit->kind() == MenuItem::Submenu
399 || cit->kind() == MenuItem::Help)
400 && cit->status().enabled()) {
406 if (enabled || !i.optional()) {
408 items_.back().status().setEnabled(enabled);
413 case MenuItem::Separator:
414 if (!items_.empty() && items_.back().kind() != MenuItem::Separator)
424 void MenuDefinition::read(Lexer & lex)
441 md_indiceslistscontext,
455 md_spellingsuggestions,
459 LexerKeyword menutags[] = {
460 { "bookmarks", md_bookmarks },
461 { "branches", md_branches },
462 { "charstyles", md_charstyles },
463 { "citestyles", md_citestyles },
464 { "custom", md_custom },
465 { "documents", md_documents },
466 { "elements", md_elements },
467 { "end", md_endmenu },
468 { "exportformats", md_exportformats },
469 { "floatinsert", md_floatinsert },
470 { "floatlistinsert", md_floatlistinsert },
471 { "graphicsgroups", md_graphicsgroups },
472 { "importformats", md_importformats },
473 { "indices", md_indices },
474 { "indicescontext", md_indicescontext },
475 { "indiceslists", md_indiceslists },
476 { "indiceslistscontext", md_indiceslistscontext },
478 { "languageselector", md_languageselector },
479 { "lastfiles", md_lastfiles },
480 { "optitem", md_optitem },
481 { "optsubmenu", md_optsubmenu },
482 { "pasterecent", md_pasterecent },
483 { "separator", md_separator },
484 { "spellingsuggestions", md_spellingsuggestions },
485 { "submenu", md_submenu },
487 { "toolbars", md_toolbars },
488 { "updateformats", md_updateformats },
489 { "viewformats", md_viewformats }
492 lex.pushTable(menutags);
493 lex.setContext("MenuDefinition::read: ");
496 bool optional = false;
498 while (lex.isOK() && !quit) {
502 // fallback to md_item
505 docstring const name = translateIfPossible(lex.getDocString());
507 string const command = lex.getString();
508 FuncRequest func = lyxaction.lookupFunc(command);
509 FuncRequest::Origin origin = FuncRequest::MENU;
510 if (name_.startsWith("context-toc-"))
511 origin = FuncRequest::TOC;
512 add(MenuItem(MenuItem::Command, toqstr(name), func, QString(), optional, origin));
518 add(MenuItem(MenuItem::Separator));
522 add(MenuItem(MenuItem::Lastfiles));
526 add(MenuItem(MenuItem::CharStyles));
530 add(MenuItem(MenuItem::Custom));
534 add(MenuItem(MenuItem::Elements));
538 add(MenuItem(MenuItem::Documents));
542 add(MenuItem(MenuItem::Bookmarks));
546 add(MenuItem(MenuItem::Toc));
550 add(MenuItem(MenuItem::ViewFormats));
553 case md_updateformats:
554 add(MenuItem(MenuItem::UpdateFormats));
557 case md_exportformats:
558 add(MenuItem(MenuItem::ExportFormats));
561 case md_importformats:
562 add(MenuItem(MenuItem::ImportFormats));
565 case md_floatlistinsert:
566 add(MenuItem(MenuItem::FloatListInsert));
570 add(MenuItem(MenuItem::FloatInsert));
574 add(MenuItem(MenuItem::PasteRecent));
578 add(MenuItem(MenuItem::Toolbars));
582 add(MenuItem(MenuItem::Branches));
586 add(MenuItem(MenuItem::CiteStyles));
589 case md_graphicsgroups:
590 add(MenuItem(MenuItem::GraphicsGroups));
593 case md_spellingsuggestions:
594 add(MenuItem(MenuItem::SpellingSuggestions));
597 case md_languageselector:
598 add(MenuItem(MenuItem::LanguageSelector));
602 add(MenuItem(MenuItem::Indices));
605 case md_indicescontext:
606 add(MenuItem(MenuItem::IndicesContext));
609 case md_indiceslists:
610 add(MenuItem(MenuItem::IndicesLists));
613 case md_indiceslistscontext:
614 add(MenuItem(MenuItem::IndicesListsContext));
619 // fallback to md_submenu
622 docstring const mlabel = translateIfPossible(lex.getDocString());
624 docstring const mname = lex.getDocString();
625 add(MenuItem(MenuItem::Submenu,
626 toqstr(mlabel), toqstr(mname), QString(), optional));
636 lex.printError("Unknown menu tag");
644 MenuItem const & MenuDefinition::operator[](size_type i) const
650 bool MenuDefinition::hasFunc(FuncRequest const & func) const
652 for (const_iterator it = begin(), et = end(); it != et; ++it)
653 if (it->func() == func)
659 void MenuDefinition::cat(MenuDefinition const & other)
661 const_iterator et = other.end();
662 for (const_iterator it = other.begin(); it != et; ++it)
667 void MenuDefinition::checkShortcuts() const
669 // This is a quadratic algorithm, but we do not care because
670 // menus are short enough
671 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
672 QString shortcut = it1->shortcut();
673 if (shortcut.isEmpty())
675 if (!it1->label().contains(shortcut))
676 LYXERR0("Menu warning: menu entry \""
678 << "\" does not contain shortcut `"
679 << shortcut << "'.");
680 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
681 if (!it2->shortcut().compare(shortcut, Qt::CaseInsensitive)) {
682 LYXERR0("Menu warning: menu entries "
683 << '"' << it1->fulllabel()
684 << "\" and \"" << it2->fulllabel()
685 << "\" share the same shortcut.");
692 bool MenuDefinition::searchMenu(FuncRequest const & func, docstring_list & names) const
694 const_iterator m = begin();
695 const_iterator m_end = end();
696 for (; m != m_end; ++m) {
697 if (m->kind() == MenuItem::Command && m->func() == func) {
698 names.push_back(qstring_to_ucs4(m->label()));
701 if (m->kind() == MenuItem::Submenu) {
702 names.push_back(qstring_to_ucs4(m->label()));
703 if (!m->hasSubmenu()) {
704 LYXERR(Debug::GUI, "Warning: non existing sub menu label="
705 << m->label() << " name=" << m->submenuname());
709 if (m->submenu().searchMenu(func, names))
718 bool compareFormat(Format const * p1, Format const * p2)
724 QString limitStringLength(docstring const & str)
726 size_t const max_item_length = 45;
728 if (str.size() > max_item_length)
729 return toqstr(str.substr(0, max_item_length - 3) + "...");
735 void MenuDefinition::expandGraphicsGroups(BufferView const * bv)
740 graphics::getGraphicsGroups(bv->buffer(), grp);
744 set<string>::const_iterator it = grp.begin();
745 set<string>::const_iterator end = grp.end();
746 add(MenuItem(MenuItem::Command, qt_("No Group"),
747 FuncRequest(LFUN_SET_GRAPHICS_GROUP)));
748 for (; it != end; it++) {
749 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(*it) + '|',
750 FuncRequest(LFUN_SET_GRAPHICS_GROUP, *it)));
755 void MenuDefinition::expandSpellingSuggestions(BufferView const * bv)
760 docstring_list suggestions;
761 Cursor const & cur = bv->cursor();
762 Paragraph const & par = cur.paragraph();
763 pos_type from = cur.pos();
765 SpellChecker::Result res = par.spellCheck(from, to, wl, suggestions, true, true);
767 case SpellChecker::UNKNOWN_WORD:
768 if (lyxrc.spellcheck_continuously) {
769 LYXERR(Debug::GUI, "Misspelled Word! Suggested Words = ");
770 docstring const & selection = cur.selectionAsString(false);
771 if (!cur.selection() || selection == wl.word()) {
773 size_t m = 10; // first submenu index
774 MenuItem item(MenuItem::Submenu, qt_("More Spelling Suggestions"));
775 item.setSubmenu(MenuDefinition(qt_("More Spelling Suggestions")));
776 for (; i != suggestions.size(); ++i) {
777 docstring const & suggestion = suggestions[i];
778 LYXERR(Debug::GUI, suggestion);
779 MenuItem w(MenuItem::Command, toqstr(suggestion),
780 FuncRequest(LFUN_WORD_REPLACE,
781 replace2string(suggestion,selection,
782 true, true, false, false)));
786 item.submenu().add(w);
791 add(MenuItem(MenuItem::Separator));
792 docstring const arg = wl.word() + " " + from_ascii(wl.lang()->lang());
793 add(MenuItem(MenuItem::Command, qt_("Add to personal dictionary|n"),
794 FuncRequest(LFUN_SPELLING_ADD, arg)));
795 add(MenuItem(MenuItem::Command, qt_("Ignore all|I"),
796 FuncRequest(LFUN_SPELLING_IGNORE, arg)));
800 case SpellChecker::LEARNED_WORD: {
801 LYXERR(Debug::GUI, "Learned Word.");
802 docstring const arg = wl.word() + " " + from_ascii(wl.lang()->lang());
803 add(MenuItem(MenuItem::Command, qt_("Remove from personal dictionary|r"),
804 FuncRequest(LFUN_SPELLING_REMOVE, arg)));
807 case SpellChecker::WORD_OK:
808 case SpellChecker::COMPOUND_WORD:
809 case SpellChecker::ROOT_FOUND:
810 case SpellChecker::IGNORED_WORD:
815 struct sortLanguageByName {
816 bool operator()(const Language * a, const Language * b) const {
817 return qt_(a->display()).localeAwareCompare(qt_(b->display())) < 0;
821 void MenuDefinition::expandLanguageSelector(Buffer const * buf)
826 std::set<Language const *> languages_buffer =
827 buf->masterBuffer()->getLanguages();
829 if (languages_buffer.size() < 2)
832 std::set<Language const *, sortLanguageByName> languages;
834 std::set<Language const *>::const_iterator const beg =
835 languages_buffer.begin();
836 for (std::set<Language const *>::const_iterator cit = beg;
837 cit != languages_buffer.end(); ++cit) {
838 languages.insert(*cit);
841 MenuItem item(MenuItem::Submenu, qt_("Language|L"));
842 item.setSubmenu(MenuDefinition(qt_("Language")));
843 QString morelangs = qt_("More Languages ...|M");
844 QStringList accelerators;
845 if (morelangs.contains('|'))
846 accelerators.append(morelangs.section('|', -1));
847 std::set<Language const *, sortLanguageByName>::const_iterator const begin = languages.begin();
848 for (std::set<Language const *, sortLanguageByName>::const_iterator cit = begin;
849 cit != languages.end(); ++cit) {
850 QString label = qt_((*cit)->display());
851 // try to add an accelerator
852 bool success = false;
853 // try capitals first
854 for (int i = 0; i < label.size(); ++i) {
855 QChar const ch = label[i];
858 if (!accelerators.contains(ch, Qt::CaseInsensitive)) {
859 label = label + toqstr("|") + ch;
860 accelerators.append(ch);
865 // if all capitals are taken, try the rest
867 for (int i = 0; i < label.size(); ++i) {
868 if (label[i].isSpace())
870 QString const ch = QString(label[i]);
871 if (!accelerators.contains(ch, Qt::CaseInsensitive)) {
872 label = label + toqstr("|") + ch;
873 accelerators.append(ch);
878 MenuItem w(MenuItem::Command, label,
879 FuncRequest(LFUN_LANGUAGE, (*cit)->lang()));
880 item.submenu().addWithStatusCheck(w);
882 item.submenu().add(MenuItem(MenuItem::Separator));
883 item.submenu().add(MenuItem(MenuItem::Command, morelangs,
884 FuncRequest(LFUN_DIALOG_SHOW, "character")));
889 void MenuDefinition::expandLastfiles()
891 LastFilesSection::LastFiles const & lf = theSession().lastFiles().lastFiles();
892 LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
896 for (; lfit != lf.end() && ii <= lyxrc.num_lastfiles; ++lfit, ++ii) {
897 string const file = lfit->absFileName();
898 QString const short_path = toqstr(makeDisplayPath(file, 30));
899 QString const long_path = toqstr(makeDisplayPath(file));
902 label = QString("%1. %2|%3").arg(ii).arg(short_path).arg(ii);
904 label = QString("%1. %2").arg(ii).arg(short_path);
905 add(MenuItem(MenuItem::Command, label,
906 FuncRequest(LFUN_FILE_OPEN, file), long_path));
911 void MenuDefinition::expandDocuments()
913 MenuItem item(MenuItem::Submenu, qt_("Hidden|H"));
914 item.setSubmenu(MenuDefinition(qt_("Hidden|H")));
916 Buffer * first = theBufferList().first();
918 add(MenuItem(MenuItem::Info, qt_("<No Documents Open>")));
924 if (!guiApp->currentView())
926 GuiWorkArea * wa = guiApp->currentView()->workArea(i);
929 Buffer const & b = wa->bufferView().buffer();
930 QString label = toqstr(b.fileName().displayName(20));
934 label = QString::number(i) + ". " + label + '|' + QString::number(i);
935 add(MenuItem(MenuItem::Command, label,
936 FuncRequest(LFUN_BUFFER_SWITCH, b.absFileName())));
943 // We cannot use a for loop as the buffer list cycles.
945 bool const shown = guiApp->currentView()
946 ? guiApp->currentView()->workArea(*b) : false;
948 QString label = toqstr(b->fileName().displayName(20));
952 label = QString::number(i) + ". " + label + '|' + QString::number(i);
953 item.submenu().add(MenuItem(MenuItem::Command, label,
954 FuncRequest(LFUN_BUFFER_SWITCH, b->absFileName())));
957 b = theBufferList().next(b);
958 } while (b != first);
960 if (!item.submenu().empty())
965 void MenuDefinition::expandBookmarks()
967 lyx::BookmarksSection const & bm = theSession().bookmarks();
970 for (size_t i = 1; i <= bm.size(); ++i) {
972 string const file = bm.bookmark(i).filename.absFileName();
973 QString const label = QString("%1. %2|%3").arg(i)
974 .arg(toqstr(makeDisplayPath(file, 20))).arg(i);
975 add(MenuItem(MenuItem::Command, label,
976 FuncRequest(LFUN_BOOKMARK_GOTO, convert<docstring>(i))));
981 add(MenuItem(MenuItem::Info, qt_("<No Bookmarks Saved Yet>")));
985 void MenuDefinition::expandFormats(MenuItem::Kind kind, Buffer const * buf)
987 if (!buf && kind != MenuItem::ImportFormats)
990 typedef vector<Format const *> Formats;
995 case MenuItem::ImportFormats:
996 formats = theConverters().importableFormats();
997 action = LFUN_BUFFER_IMPORT;
999 case MenuItem::ViewFormats:
1000 formats = buf->exportableFormats(true);
1001 action = LFUN_BUFFER_VIEW;
1003 case MenuItem::UpdateFormats:
1004 formats = buf->exportableFormats(true);
1005 action = LFUN_BUFFER_UPDATE;
1008 formats = buf->exportableFormats(false);
1009 action = LFUN_BUFFER_EXPORT;
1011 sort(formats.begin(), formats.end(), &compareFormat);
1013 bool const view_update = (kind == MenuItem::ViewFormats
1014 || kind == MenuItem::UpdateFormats);
1018 smenue = (kind == MenuItem::ViewFormats ?
1019 qt_("View (Other Formats)|F")
1020 : qt_("Update (Other Formats)|p"));
1021 MenuItem item(MenuItem::Submenu, smenue);
1022 item.setSubmenu(MenuDefinition(smenue));
1024 Formats::const_iterator fit = formats.begin();
1025 Formats::const_iterator end = formats.end();
1026 for (; fit != end ; ++fit) {
1027 if ((*fit)->dummy())
1030 docstring lab = from_utf8((*fit)->prettyname());
1031 docstring scut = from_utf8((*fit)->shortcut());
1032 docstring const tmplab = lab;
1035 lab += char_type('|') + scut;
1036 docstring lab_i18n = translateIfPossible(lab);
1037 bool const untranslated = (lab == lab_i18n);
1038 QString const shortcut = toqstr(split(lab_i18n, lab, '|'));
1039 QString label = toqstr(lab);
1041 // this might happen if the shortcut
1042 // has been redefined
1043 label = toqstr(translateIfPossible(tmplab));
1046 case MenuItem::ImportFormats:
1049 case MenuItem::ViewFormats:
1050 case MenuItem::UpdateFormats:
1051 if ((*fit)->name() == buf->getDefaultOutputFormat()) {
1052 docstring lbl = (kind == MenuItem::ViewFormats ?
1053 bformat(_("View [%1$s]|V"), qstring_to_ucs4(label))
1054 : bformat(_("Update [%1$s]|U"), qstring_to_ucs4(label)));
1055 MenuItem w(MenuItem::Command, toqstr(lbl),
1056 FuncRequest(action));
1060 case MenuItem::ExportFormats:
1061 if (!(*fit)->inExportMenu())
1065 LASSERT(false, /**/);
1068 if (!shortcut.isEmpty())
1069 label += '|' + shortcut;
1073 item.submenu().addWithStatusCheck(MenuItem(MenuItem::Command, label,
1074 FuncRequest(action, (*fit)->name())));
1076 item.submenu().add(MenuItem(MenuItem::Command, label,
1077 FuncRequest(action, (*fit)->name())));
1080 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1081 FuncRequest(action, (*fit)->name())));
1083 add(MenuItem(MenuItem::Command, label,
1084 FuncRequest(action, (*fit)->name())));
1092 void MenuDefinition::expandFloatListInsert(Buffer const * buf)
1097 FloatList const & floats = buf->params().documentClass().floats();
1098 FloatList::const_iterator cit = floats.begin();
1099 FloatList::const_iterator end = floats.end();
1101 for (; cit != end; ++cit) {
1102 if (!cit->second.usesFloatPkg()) {
1103 // Different floats could declare the same ListCommand. We only
1104 // want it on the list once, though.
1105 string const & list_cmd = cit->second.listCommand();
1106 if (list_cmd.empty())
1107 // we do not know how to generate such a list
1109 // This form of insert returns an iterator pointing to the newly
1110 // inserted element OR the existing element with that value, and
1111 // a bool indicating whether we inserted a new element. So we can
1112 // see if one is there and insert it if not all at once.
1113 pair<set<string>::iterator, bool> ret = seen.insert(list_cmd);
1117 string const & list_name = cit->second.listName();
1118 addWithStatusCheck(MenuItem(MenuItem::Command, qt_(list_name),
1119 FuncRequest(LFUN_FLOAT_LIST_INSERT, cit->second.floattype())));
1124 void MenuDefinition::expandFloatInsert(Buffer const * buf)
1129 FloatList const & floats = buf->params().documentClass().floats();
1130 FloatList::const_iterator cit = floats.begin();
1131 FloatList::const_iterator end = floats.end();
1132 for (; cit != end; ++cit) {
1134 QString const label = qt_(cit->second.name());
1135 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1136 FuncRequest(LFUN_FLOAT_INSERT,
1137 cit->second.floattype())));
1142 void MenuDefinition::expandFlexInsert(
1143 Buffer const * buf, InsetLayout::InsetLyXType type)
1148 TextClass::InsetLayouts const & insetLayouts =
1149 buf->params().documentClass().insetLayouts();
1150 TextClass::InsetLayouts::const_iterator cit = insetLayouts.begin();
1151 TextClass::InsetLayouts::const_iterator end = insetLayouts.end();
1152 for (; cit != end; ++cit) {
1153 if (cit->second.lyxtype() == type) {
1154 docstring label = cit->first;
1155 // we remove the "Flex:" prefix, if it is present
1156 if (prefixIs(label, from_utf8("Flex:")))
1157 label = label.substr(5);
1158 addWithStatusCheck(MenuItem(MenuItem::Command,
1159 toqstr(translateIfPossible(label)),
1160 FuncRequest(LFUN_FLEX_INSERT, Lexer::quoteString(label))));
1163 // FIXME This is a little clunky.
1164 if (items_.empty() && type == InsetLayout::CUSTOM)
1165 add(MenuItem(MenuItem::Help, qt_("No Custom Insets Defined!")));
1169 size_t const max_number_of_items = 25;
1171 void MenuDefinition::expandToc2(Toc const & toc_list,
1172 size_t from, size_t to, int depth)
1174 int shortcut_count = 0;
1176 // check whether depth is smaller than the smallest depth in toc.
1177 int min_depth = 1000;
1178 for (size_t i = from; i < to; ++i)
1179 min_depth = min(min_depth, toc_list[i].depth());
1180 if (min_depth > depth)
1183 if (to - from <= max_number_of_items) {
1184 for (size_t i = from; i < to; ++i) {
1185 QString label(4 * max(0, toc_list[i].depth() - depth), ' ');
1186 label += limitStringLength(toc_list[i].str());
1187 if (toc_list[i].depth() == depth) {
1189 if (shortcut_count < 9) {
1190 if (label.contains(QString::number(shortcut_count + 1)))
1191 label += QString::number(++shortcut_count);
1194 add(MenuItem(MenuItem::Command, label,
1195 FuncRequest(toc_list[i].action())));
1200 size_t new_pos = pos + 1;
1201 while (new_pos < to && toc_list[new_pos].depth() > depth)
1204 QString label(4 * max(0, toc_list[pos].depth() - depth), ' ');
1205 label += limitStringLength(toc_list[pos].str());
1206 if (toc_list[pos].depth() == depth) {
1208 if (shortcut_count < 9) {
1209 if (label.contains(QString::number(shortcut_count + 1)))
1210 label += QString::number(++shortcut_count);
1213 if (new_pos == pos + 1) {
1214 add(MenuItem(MenuItem::Command,
1215 label, FuncRequest(toc_list[pos].action())));
1218 sub.expandToc2(toc_list, pos, new_pos, depth + 1);
1219 MenuItem item(MenuItem::Submenu, label);
1220 item.setSubmenu(sub);
1229 void MenuDefinition::expandToc(Buffer const * buf)
1231 // To make things very cleanly, we would have to pass buf to
1232 // all MenuItem constructors and to expandToc2. However, we
1233 // know that all the entries in a TOC will be have status_ ==
1234 // OK, so we avoid this unnecessary overhead (JMarc)
1237 add(MenuItem(MenuItem::Info, qt_("<No Document Open>")));
1241 // Add an entry for the master doc if this is a child doc
1242 Buffer const * const master = buf->masterBuffer();
1243 if (buf != master) {
1244 ParIterator const pit = par_iterator_begin(master->inset());
1245 string const arg = convert<string>(pit->id());
1246 FuncRequest f(LFUN_PARAGRAPH_GOTO, arg);
1247 add(MenuItem(MenuItem::Command, qt_("Master Document"), f));
1250 MenuDefinition other_lists;
1252 FloatList const & floatlist = buf->params().documentClass().floats();
1253 TocList const & toc_list = buf->tocBackend().tocs();
1254 TocList::const_iterator cit = toc_list.begin();
1255 TocList::const_iterator end = toc_list.end();
1256 for (; cit != end; ++cit) {
1257 // Handle this later
1258 if (cit->first == "tableofcontents")
1261 MenuDefinition submenu;
1262 if (cit->second.size() >= 30) {
1263 FuncRequest f(LFUN_DIALOG_SHOW, "toc " + cit->first);
1264 submenu.add(MenuItem(MenuItem::Command, qt_("Open Navigator..."), f));
1266 TocIterator ccit = cit->second.begin();
1267 TocIterator eend = cit->second.end();
1268 for (; ccit != eend; ++ccit) {
1269 submenu.add(MenuItem(MenuItem::Command,
1270 limitStringLength(ccit->str()) + '|',
1271 FuncRequest(ccit->action())));
1275 MenuItem item(MenuItem::Submenu, guiName(cit->first, buf->params()));
1276 item.setSubmenu(submenu);
1277 if (floatlist.typeExist(cit->first) || cit->first == "child") {
1278 // Those two types deserve to be in the main menu.
1279 item.setSubmenu(submenu);
1282 other_lists.add(item);
1284 if (!other_lists.empty()) {
1285 MenuItem item(MenuItem::Submenu, qt_("Other Lists"));
1286 item.setSubmenu(other_lists);
1290 // Handle normal TOC
1291 cit = toc_list.find("tableofcontents");
1293 LYXERR(Debug::GUI, "No table of contents.");
1295 if (cit->second.size() > 0 )
1296 expandToc2(cit->second, 0, cit->second.size(), 0);
1298 add(MenuItem(MenuItem::Info, qt_("<Empty Table of Contents>")));
1303 void MenuDefinition::expandPasteRecent(Buffer const * buf)
1305 docstring_list const sel = cap::availableSelections(buf);
1307 docstring_list::const_iterator cit = sel.begin();
1308 docstring_list::const_iterator end = sel.end();
1310 for (unsigned int index = 0; cit != end; ++cit, ++index) {
1311 add(MenuItem(MenuItem::Command, toqstr(*cit) + '|',
1312 FuncRequest(LFUN_PASTE, convert<string>(index))));
1317 void MenuDefinition::expandToolbars()
1319 MenuDefinition other_lists;
1320 // extracts the toolbars from the backend
1321 Toolbars::Infos::const_iterator cit = guiApp->toolbars().begin();
1322 Toolbars::Infos::const_iterator end = guiApp->toolbars().end();
1323 for (; cit != end; ++cit) {
1324 MenuItem const item(MenuItem::Command, toqstr(cit->gui_name),
1325 FuncRequest(LFUN_TOOLBAR_TOGGLE, cit->name));
1326 if (guiApp->toolbars().isMainToolbar(cit->name))
1329 other_lists.add(item);
1332 if (!other_lists.empty()) {
1333 MenuItem item(MenuItem::Submenu, qt_("Other Toolbars"));
1334 item.setSubmenu(other_lists);
1340 void MenuDefinition::expandBranches(Buffer const * buf)
1345 BufferParams const & master_params = buf->masterBuffer()->params();
1346 BufferParams const & params = buf->params();
1347 if (params.branchlist().empty() && master_params.branchlist().empty() ) {
1348 add(MenuItem(MenuItem::Help, qt_("No Branches Set for Document!")));
1352 BranchList::const_iterator cit = master_params.branchlist().begin();
1353 BranchList::const_iterator end = master_params.branchlist().end();
1355 for (int ii = 1; cit != end; ++cit, ++ii) {
1356 docstring label = cit->branch();
1358 label = convert<docstring>(ii) + ". " + label
1359 + char_type('|') + convert<docstring>(ii);
1361 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1362 FuncRequest(LFUN_BRANCH_INSERT,
1366 if (buf == buf->masterBuffer())
1369 MenuDefinition child_branches;
1371 BranchList::const_iterator ccit = params.branchlist().begin();
1372 BranchList::const_iterator cend = params.branchlist().end();
1374 for (int ii = 1; ccit != cend; ++ccit, ++ii) {
1375 docstring label = ccit->branch();
1377 label = convert<docstring>(ii) + ". " + label
1378 + char_type('|') + convert<docstring>(ii);
1380 label += char_type('|');
1381 child_branches.addWithStatusCheck(MenuItem(MenuItem::Command,
1383 FuncRequest(LFUN_BRANCH_INSERT,
1387 if (!child_branches.empty()) {
1388 MenuItem item(MenuItem::Submenu, qt_("Child Document"));
1389 item.setSubmenu(child_branches);
1395 void MenuDefinition::expandIndices(Buffer const * buf, bool listof)
1400 BufferParams const & params = buf->masterBuffer()->params();
1401 if (!params.use_indices) {
1403 addWithStatusCheck(MenuItem(MenuItem::Command,
1404 qt_("Index List|I"),
1405 FuncRequest(LFUN_INDEX_PRINT,
1406 from_ascii("idx"))));
1408 addWithStatusCheck(MenuItem(MenuItem::Command,
1409 qt_("Index Entry|d"),
1410 FuncRequest(LFUN_INDEX_INSERT,
1411 from_ascii("idx"))));
1415 if (params.indiceslist().empty())
1418 IndicesList::const_iterator cit = params.indiceslist().begin();
1419 IndicesList::const_iterator end = params.indiceslist().end();
1421 for (int ii = 1; cit != end; ++cit, ++ii) {
1423 docstring const label =
1424 bformat(_("Index: %1$s"), cit->index());
1425 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1426 FuncRequest(LFUN_INDEX_PRINT, cit->shortcut())));
1428 docstring const label =
1429 bformat(_("Index Entry (%1$s)"), cit->index());
1430 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1431 FuncRequest(LFUN_INDEX_INSERT, cit->shortcut())));
1437 void MenuDefinition::expandIndicesContext(Buffer const * buf, bool listof)
1442 BufferParams const & params = buf->masterBuffer()->params();
1443 if (!params.use_indices || params.indiceslist().empty())
1446 IndicesList::const_iterator cit = params.indiceslist().begin();
1447 IndicesList::const_iterator end = params.indiceslist().end();
1449 for (int ii = 1; cit != end; ++cit, ++ii) {
1451 InsetCommandParams p(INDEX_PRINT_CODE);
1452 p["type"] = cit->shortcut();
1453 string const data = InsetCommand::params2string(p);
1454 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(cit->index()),
1455 FuncRequest(LFUN_INSET_MODIFY, data)));
1457 docstring const label =
1458 bformat(_("Index Entry (%1$s)"), cit->index());
1459 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1460 FuncRequest(LFUN_INSET_MODIFY,
1461 from_ascii("changetype ") + cit->shortcut())));
1467 void MenuDefinition::expandCiteStyles(BufferView const * bv)
1472 Inset const * inset = bv->cursor().nextInset();
1473 if (!inset || inset->lyxCode() != CITE_CODE) {
1474 add(MenuItem(MenuItem::Command,
1475 qt_("No Citation in Scope!"),
1476 FuncRequest(LFUN_NOACTION)));
1479 InsetCommand const * citinset =
1480 static_cast<InsetCommand const *>(inset);
1482 Buffer const * buf = &bv->buffer();
1483 docstring key = citinset->getParam("key");
1484 // we can only handle one key currently
1485 if (contains(key, ','))
1486 key = qstring_to_ucs4(toqstr(key).split(',')[0]);
1488 vector<CiteStyle> citeStyleList = citeStyles(buf->params().citeEngine());
1489 docstring_list citeStrings =
1490 buf->masterBibInfo().getCiteStrings(key, bv->buffer());
1492 docstring_list::const_iterator cit = citeStrings.begin();
1493 docstring_list::const_iterator end = citeStrings.end();
1495 for (int ii = 1; cit != end; ++cit, ++ii) {
1496 docstring label = *cit;
1498 CiteStyle cst = citeStyleList[ii - 1];
1500 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1501 FuncRequest(LFUN_INSET_MODIFY,
1502 "changetype " + from_utf8(citationStyleToString(cs)))));
1509 /////////////////////////////////////////////////////////////////////
1510 // Menu::Impl definition and implementation
1511 /////////////////////////////////////////////////////////////////////
1515 /// populates the menu or one of its submenu
1516 /// This is used as a recursive function
1517 void populate(QMenu & qMenu, MenuDefinition const & menu);
1519 /// Only needed for top level menus.
1520 MenuDefinition * top_level_menu;
1523 /// the name of this menu
1529 /// Get a MenuDefinition item label from the menu backend
1530 static QString label(MenuItem const & mi)
1532 QString label = mi.label();
1533 label.replace("&", "&&");
1535 QString shortcut = mi.shortcut();
1536 if (!shortcut.isEmpty()) {
1537 int pos = label.indexOf(shortcut);
1539 //label.insert(pos, 1, char_type('&'));
1540 label.replace(pos, 0, "&");
1543 QString const binding = mi.binding();
1544 if (!binding.isEmpty())
1545 label += '\t' + binding;
1550 void Menu::Impl::populate(QMenu & qMenu, MenuDefinition const & menu)
1552 LYXERR(Debug::GUI, "populating menu " << menu.name());
1553 if (menu.size() == 0) {
1554 LYXERR(Debug::GUI, "\tERROR: empty menu " << menu.name());
1557 LYXERR(Debug::GUI, " ***** menu entries " << menu.size());
1558 MenuDefinition::const_iterator m = menu.begin();
1559 MenuDefinition::const_iterator end = menu.end();
1560 for (; m != end; ++m) {
1561 if (m->kind() == MenuItem::Separator)
1562 qMenu.addSeparator();
1563 else if (m->kind() == MenuItem::Submenu) {
1564 QMenu * subMenu = qMenu.addMenu(label(*m));
1565 populate(*subMenu, m->submenu());
1566 subMenu->setEnabled(m->status().enabled());
1568 // we have a MenuItem::Command
1569 qMenu.addAction(new Action(view, QIcon(), label(*m),
1570 m->func(), m->tooltip(), &qMenu));
1575 #if defined(Q_WS_WIN) && (QT_VERSION >= 0x040600)
1576 class AlwaysMnemonicStyle : public QProxyStyle {
1578 int styleHint(StyleHint hint, const QStyleOption *opt = 0, const QWidget *widget = 0,
1579 QStyleHintReturn *returnData = 0) const
1581 if (hint == QStyle::SH_UnderlineShortcut)
1583 return QProxyStyle::styleHint(hint, opt, widget, returnData);
1588 /////////////////////////////////////////////////////////////////////
1589 // Menu implementation
1590 /////////////////////////////////////////////////////////////////////
1592 Menu::Menu(GuiView * gv, QString const & name, bool top_level, bool keyboard)
1593 : QMenu(gv), d(new Menu::Impl)
1595 #if defined(Q_WS_WIN) && (QT_VERSION >= 0x040600)
1597 setStyle(new AlwaysMnemonicStyle);
1601 d->top_level_menu = top_level? new MenuDefinition : 0;
1605 if (d->top_level_menu)
1606 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
1612 delete d->top_level_menu;
1617 void Menu::updateView()
1619 guiApp->menus().updateMenu(this);
1623 /////////////////////////////////////////////////////////////////////
1624 // Menus::Impl definition and implementation
1625 /////////////////////////////////////////////////////////////////////
1627 struct Menus::Impl {
1629 bool hasMenu(QString const &) const;
1631 MenuDefinition & getMenu(QString const &);
1633 MenuDefinition const & getMenu(QString const &) const;
1635 /// Expands some special entries of the menu
1636 /** The entries with the following kind are expanded to a
1637 sequence of Command MenuItems: Lastfiles, Documents,
1638 ViewFormats, ExportFormats, UpdateFormats, Branches, Indices
1640 void expand(MenuDefinition const & frommenu, MenuDefinition & tomenu,
1641 BufferView const *) const;
1643 /// Initialize specific MACOS X menubar
1644 void macxMenuBarInit(GuiView * view, QMenuBar * qmb);
1646 /// Mac special menu.
1647 /** This defines a menu whose entries list the FuncRequests
1648 that will be removed by expand() in other menus. This is
1649 used by the Qt/Mac code.
1651 NOTE: Qt does not remove the menu items when clearing a QMenuBar,
1652 such that the items will keep accessing the FuncRequests in
1653 the MenuDefinition. While Menus::Impl might be recreated,
1654 we keep mac_special_menu_ in memory by making it static.
1656 static MenuDefinition mac_special_menu_;
1661 MenuDefinition menubar_;
1663 typedef QMap<GuiView *, QHash<QString, Menu*> > NameMap;
1665 /// name to menu for \c menu() method.
1670 MenuDefinition Menus::Impl::mac_special_menu_;
1674 Here is what the Qt documentation says about how a menubar is chosen:
1676 1) If the window has a QMenuBar then it is used. 2) If the window
1677 is a modal then its menubar is used. If no menubar is specified
1678 then a default menubar is used (as documented below) 3) If the
1679 window has no parent then the default menubar is used (as
1682 The above 3 steps are applied all the way up the parent window
1683 chain until one of the above are satisifed. If all else fails a
1684 default menubar will be created, the default menubar on Qt/Mac is
1685 an empty menubar, however you can create a different default
1686 menubar by creating a parentless QMenuBar, the first one created
1687 will thus be designated the default menubar, and will be used
1688 whenever a default menubar is needed.
1690 Thus, for Qt/Mac, we add the menus to a free standing menubar, so
1691 that this menubar will be used also when one of LyX' dialogs has
1694 void Menus::Impl::macxMenuBarInit(GuiView * view, QMenuBar * qmb)
1696 /* Since Qt 4.2, the qt/mac menu code has special code for
1697 specifying the role of a menu entry. However, it does not
1698 work very well with our scheme of creating menus on demand,
1699 and therefore we need to put these entries in a special
1700 invisible menu. (JMarc)
1703 /* The entries of our special mac menu. If we add support for
1704 * special entries in Menus, we could imagine something
1706 * SpecialItem About " "About LyX" "dialog-show aboutlyx"
1707 * and therefore avoid hardcoding. I am not sure it is worth
1708 * the hassle, though. (JMarc)
1710 struct MacMenuEntry {
1714 QAction::MenuRole role;
1717 MacMenuEntry entries[] = {
1718 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
1719 QAction::AboutRole},
1720 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
1721 QAction::PreferencesRole},
1722 {LFUN_RECONFIGURE, "", "Reconfigure",
1723 QAction::ApplicationSpecificRole},
1724 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
1726 const size_t num_entries = sizeof(entries) / sizeof(entries[0]);
1728 // the special menu for Menus. Fill it up only once.
1729 if (mac_special_menu_.size() == 0) {
1730 for (size_t i = 0 ; i < num_entries ; ++i) {
1731 FuncRequest const func(entries[i].action,
1732 from_utf8(entries[i].arg));
1733 mac_special_menu_.add(MenuItem(MenuItem::Command,
1734 entries[i].label, func));
1738 // add the entries to a QMenu that will eventually be empty
1739 // and therefore invisible.
1740 QMenu * qMenu = qmb->addMenu("special");
1741 MenuDefinition::const_iterator cit = mac_special_menu_.begin();
1742 MenuDefinition::const_iterator end = mac_special_menu_.end();
1743 for (size_t i = 0 ; cit != end ; ++cit, ++i) {
1744 Action * action = new Action(view, QIcon(), cit->label(),
1745 cit->func(), QString(), qMenu);
1746 action->setMenuRole(entries[i].role);
1747 qMenu->addAction(action);
1752 void Menus::Impl::expand(MenuDefinition const & frommenu,
1753 MenuDefinition & tomenu, BufferView const * bv) const
1755 if (!tomenu.empty())
1758 for (MenuDefinition::const_iterator cit = frommenu.begin();
1759 cit != frommenu.end() ; ++cit) {
1760 Buffer const * buf = bv ? &bv->buffer() : 0;
1761 switch (cit->kind()) {
1762 case MenuItem::Lastfiles:
1763 tomenu.expandLastfiles();
1766 case MenuItem::Documents:
1767 tomenu.expandDocuments();
1770 case MenuItem::Bookmarks:
1771 tomenu.expandBookmarks();
1774 case MenuItem::ImportFormats:
1775 case MenuItem::ViewFormats:
1776 case MenuItem::UpdateFormats:
1777 case MenuItem::ExportFormats:
1778 tomenu.expandFormats(cit->kind(), buf);
1781 case MenuItem::CharStyles:
1782 tomenu.expandFlexInsert(buf, InsetLayout::CHARSTYLE);
1785 case MenuItem::Custom:
1786 tomenu.expandFlexInsert(buf, InsetLayout::CUSTOM);
1789 case MenuItem::Elements:
1790 tomenu.expandFlexInsert(buf, InsetLayout::ELEMENT);
1793 case MenuItem::FloatListInsert:
1794 tomenu.expandFloatListInsert(buf);
1797 case MenuItem::FloatInsert:
1798 tomenu.expandFloatInsert(buf);
1801 case MenuItem::PasteRecent:
1802 tomenu.expandPasteRecent(buf);
1805 case MenuItem::Toolbars:
1806 tomenu.expandToolbars();
1809 case MenuItem::Branches:
1810 tomenu.expandBranches(buf);
1813 case MenuItem::Indices:
1814 tomenu.expandIndices(buf);
1817 case MenuItem::IndicesContext:
1818 tomenu.expandIndicesContext(buf);
1821 case MenuItem::IndicesLists:
1822 tomenu.expandIndices(buf, true);
1825 case MenuItem::IndicesListsContext:
1826 tomenu.expandIndicesContext(buf, true);
1829 case MenuItem::CiteStyles:
1830 tomenu.expandCiteStyles(bv);
1834 tomenu.expandToc(buf);
1837 case MenuItem::GraphicsGroups:
1838 tomenu.expandGraphicsGroups(bv);
1841 case MenuItem::SpellingSuggestions:
1842 tomenu.expandSpellingSuggestions(bv);
1845 case MenuItem::LanguageSelector:
1846 tomenu.expandLanguageSelector(buf);
1849 case MenuItem::Submenu: {
1850 MenuItem item(*cit);
1851 item.setSubmenu(MenuDefinition(cit->submenuname()));
1852 expand(getMenu(cit->submenuname()), item.submenu(), bv);
1853 tomenu.addWithStatusCheck(item);
1857 case MenuItem::Info:
1858 case MenuItem::Help:
1859 case MenuItem::Separator:
1860 tomenu.addWithStatusCheck(*cit);
1863 case MenuItem::Command:
1864 if (!mac_special_menu_.hasFunc(cit->func()))
1865 tomenu.addWithStatusCheck(*cit);
1869 // we do not want the menu to end with a separator
1870 if (!tomenu.empty() && tomenu.items_.back().kind() == MenuItem::Separator)
1871 tomenu.items_.pop_back();
1873 // Check whether the shortcuts are unique
1874 tomenu.checkShortcuts();
1878 bool Menus::Impl::hasMenu(QString const & name) const
1880 return find_if(menulist_.begin(), menulist_.end(),
1881 MenuNamesEqual(name)) != menulist_.end();
1885 MenuDefinition const & Menus::Impl::getMenu(QString const & name) const
1887 const_iterator cit = find_if(menulist_.begin(), menulist_.end(),
1888 MenuNamesEqual(name));
1889 if (cit == menulist_.end())
1890 LYXERR0("No submenu named " << name);
1891 LASSERT(cit != menulist_.end(), /**/);
1896 MenuDefinition & Menus::Impl::getMenu(QString const & name)
1898 iterator it = find_if(menulist_.begin(), menulist_.end(),
1899 MenuNamesEqual(name));
1900 if (it == menulist_.end())
1901 LYXERR0("No submenu named " << name);
1902 LASSERT(it != menulist_.end(), /**/);
1907 /////////////////////////////////////////////////////////////////////
1911 /////////////////////////////////////////////////////////////////////
1913 Menus::Menus() : d(new Impl) {}
1929 void Menus::read(Lexer & lex)
1937 LexerKeyword menutags[] = {
1938 { "end", md_endmenuset },
1939 { "menu", md_menu },
1940 { "menubar", md_menubar }
1943 // consistency check
1944 if (compare_ascii_no_case(lex.getString(), "menuset"))
1945 LYXERR0("Menus::read: ERROR wrong token: `" << lex.getString() << '\'');
1947 lex.pushTable(menutags);
1948 lex.setContext("Menus::read");
1952 while (lex.isOK() && !quit) {
1953 switch (lex.lex()) {
1955 d->menubar_.read(lex);
1959 QString const name = toqstr(lex.getDocString());
1960 if (d->hasMenu(name))
1961 d->getMenu(name).read(lex);
1963 MenuDefinition menu(name);
1965 d->menulist_.push_back(menu);
1973 lex.printError("Unknown menu tag");
1981 bool Menus::searchMenu(FuncRequest const & func,
1982 docstring_list & names) const
1984 MenuDefinition menu;
1985 d->expand(d->menubar_, menu, 0);
1986 return menu.searchMenu(func, names);
1990 void Menus::fillMenuBar(QMenuBar * qmb, GuiView * view, bool initial)
1994 // setup special mac specific menu items, but only do this
1995 // the first time a QMenuBar is created. Otherwise Qt will
1996 // create duplicate items in the application menu. It seems
1997 // that Qt does not remove them when the QMenubar is cleared.
1998 LYXERR(Debug::GUI, "Creating Mac OS X special menu bar");
1999 d->macxMenuBarInit(view, qmb);
2002 // Clear all menubar contents before filling it.
2006 LYXERR(Debug::GUI, "populating menu bar" << d->menubar_.name());
2008 if (d->menubar_.size() == 0) {
2009 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
2010 << d->menubar_.name());
2013 LYXERR(Debug::GUI, "menu bar entries " << d->menubar_.size());
2015 MenuDefinition menu;
2016 BufferView * bv = 0;
2018 bv = view->currentBufferView();
2019 d->expand(d->menubar_, menu, bv);
2021 MenuDefinition::const_iterator m = menu.begin();
2022 MenuDefinition::const_iterator end = menu.end();
2024 for (; m != end; ++m) {
2026 if (m->kind() != MenuItem::Submenu) {
2027 LYXERR(Debug::GUI, "\tERROR: not a submenu " << m->label());
2031 LYXERR(Debug::GUI, "menu bar item " << m->label()
2032 << " is a submenu named " << m->submenuname());
2034 QString name = m->submenuname();
2035 if (!d->hasMenu(name)) {
2036 LYXERR(Debug::GUI, "\tERROR: " << name
2037 << " submenu has no menu!");
2041 Menu * menu = new Menu(view, m->submenuname(), true);
2042 menu->setTitle(label(*m));
2045 d->name_map_[view][name] = menu;
2050 void Menus::updateMenu(Menu * qmenu)
2052 LYXERR(Debug::GUI, "Triggered menu: " << qmenu->d->name);
2055 if (qmenu->d->name.isEmpty())
2058 docstring identifier = qstring_to_ucs4(qmenu->d->name);
2059 MenuDefinition fromLyxMenu(qmenu->d->name);
2060 while (!identifier.empty()) {
2061 docstring menu_name;
2062 identifier = split(identifier, menu_name, ';');
2064 if (!d->hasMenu(toqstr(menu_name))) {
2065 LYXERR(Debug::GUI, "\tWARNING: non existing menu: "
2070 fromLyxMenu.cat(d->getMenu(toqstr(menu_name)));
2071 fromLyxMenu.add(MenuItem(MenuItem::Separator));
2074 if (fromLyxMenu.empty()) {
2075 qmenu->addAction(qt_("No Action Defined!"));
2079 BufferView * bv = 0;
2081 bv = qmenu->d->view->currentBufferView();
2082 d->expand(fromLyxMenu, *qmenu->d->top_level_menu, bv);
2083 qmenu->d->populate(*qmenu, *qmenu->d->top_level_menu);
2087 Menu * Menus::menu(QString const & name, GuiView & view, bool keyboard)
2089 LYXERR(Debug::GUI, "Context menu requested: " << name);
2090 Menu * menu = d->name_map_[&view].value(name, 0);
2091 if (!menu && !name.startsWith("context-")) {
2092 LYXERR0("requested context menu not found: " << name);
2096 menu = new Menu(&view, name, true, keyboard);
2097 d->name_map_[&view][name] = menu;
2101 } // namespace frontend
2104 #include "moc_Menus.cpp"