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"
47 #include "Paragraph.h"
48 #include "ParIterator.h"
50 #include "SpellChecker.h"
51 #include "TextClass.h"
52 #include "TocBackend.h"
54 #include "WordLangTuple.h"
56 #include "insets/Inset.h"
57 #include "insets/InsetCitation.h"
58 #include "insets/InsetGraphics.h"
60 #include "support/lassert.h"
61 #include "support/convert.h"
62 #include "support/debug.h"
63 #include "support/docstring_list.h"
64 #include "support/filetools.h"
65 #include "support/gettext.h"
66 #include "support/lstrings.h"
74 #include "support/shared_ptr.h"
80 using namespace lyx::support;
88 // MacOSX specific stuff is at the end.
95 /// The type of elements that can be in a menu
103 /** This type of item explains why something is unavailable. If this
104 menuitem is in a submenu, the submenu is enabled to make sure the
105 user sees the information. */
107 /** This type of item merely shows that there might be a list or
108 something alike at this position, but the list is still empty.
109 If this item is in a submenu, the submenu will not always be
112 /** This is the list of last opened file,
113 typically for the File menu. */
115 /** This is the list of opened Documents,
116 typically for the Documents menu. */
118 /** This is the bookmarks */
122 /** This is a list of viewable formats
123 typically for the File->View menu. */
125 /** This is a list of updatable formats
126 typically for the File->Update menu. */
128 /** This is a list of exportable formats
129 typically for the File->Export menu. */
131 /** This is a list of importable formats
132 typically for the File->Export menu. */
134 /** This is the list of elements available
135 * for insertion into document. */
137 /** This is the list of user-configurable
138 insets to insert into document */
140 /** This is the list of XML elements to
141 insert into the document */
143 /** This is the list of floats that we can
144 insert a list for. */
146 /** This is the list of floats that we can
149 /** This is the list of selections that can
154 /** Available branches in document */
156 /** Available indices in document */
158 /** Context menu for indices in document */
160 /** Available index lists in document */
162 /** Context menu for available indices lists in document */
164 /** Available citation styles for a given citation */
166 /** Available graphics groups */
168 /// Words suggested by the spellchecker.
170 /** Used Languages */
174 explicit MenuItem(Kind kind) : kind_(kind), optional_(false) {}
177 QString const & label,
178 QString const & submenu = QString(),
179 bool optional = false)
180 : kind_(kind), label_(label), submenuname_(submenu), optional_(optional)
182 LASSERT(kind == Submenu || kind == Help || kind == Info, /**/);
186 QString const & label,
187 FuncRequest const & func,
188 bool optional = false,
189 FuncRequest::Origin origin = FuncRequest::MENU)
190 : kind_(kind), label_(label), func_(func), optional_(optional)
192 func_.setOrigin(origin);
195 // shared_ptr<MenuDefinition> needs this apprently...
198 /// The label of a given menuitem
199 QString label() const
201 int const index = label_.lastIndexOf('|');
202 return index == -1 ? label_ : label_.left(index);
205 /// The keyboard shortcut (usually underlined in the entry)
206 QString shortcut() const
208 int const index = label_.lastIndexOf('|');
209 return index == -1 ? QString() : label_.mid(index + 1);
211 /// The complete label, with label and shortcut separated by a '|'
212 QString fulllabel() const { return label_;}
213 /// The kind of entry
214 Kind kind() const { return kind_; }
215 /// the action (if relevant)
216 FuncRequest const & func() const { return func_; }
217 /// returns true if the entry should be omitted when disabled
218 bool optional() const { return optional_; }
219 /// returns the status of the lfun associated with this entry
220 FuncStatus const & status() const { return status_; }
221 /// returns the status of the lfun associated with this entry
222 FuncStatus & status() { return status_; }
223 /// returns the status of the lfun associated with this entry
224 void status(FuncStatus const & status) { status_ = status; }
226 ///returns the binding associated to this action.
227 QString binding() const
229 if (kind_ != Command)
231 // Get the keys bound to this action, but keep only the
233 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func_);
235 return toqstr(bindings.begin()->print(KeySequence::ForGui));
237 LYXERR(Debug::KBMAP, "No binding for "
238 << lyxaction.getActionName(func_.action())
239 << '(' << func_.argument() << ')');
243 /// the description of the submenu (if relevant)
244 QString const & submenuname() const { return submenuname_; }
245 /// set the description of the submenu
246 void submenuname(QString const & name) { submenuname_ = name; }
248 bool hasSubmenu() const { return !submenu_.isEmpty(); }
250 MenuDefinition const & submenu() const { return submenu_.at(0); }
251 MenuDefinition & submenu() { return submenu_[0]; }
253 void setSubmenu(MenuDefinition const & menu)
256 submenu_.append(menu);
267 QString submenuname_;
272 /// contains 0 or 1 item.
273 QList<MenuDefinition> submenu_;
277 class MenuDefinition {
280 typedef std::vector<MenuItem> ItemList;
282 typedef ItemList::const_iterator const_iterator;
284 explicit MenuDefinition(QString const & name = QString()) : name_(name) {}
289 QString const & name() const { return name_; }
291 bool empty() const { return items_.empty(); }
292 /// Clear the menu content.
293 void clear() { items_.clear(); }
295 size_t size() const { return items_.size(); }
297 MenuItem const & operator[](size_t) const;
299 const_iterator begin() const { return items_.begin(); }
301 const_iterator end() const { return items_.end(); }
303 void cat(MenuDefinition const & other);
305 // search for func in this menu iteratively, and put menu
307 bool searchMenu(FuncRequest const & func, docstring_list & names)
310 bool hasFunc(FuncRequest const &) const;
311 /// Add the menu item unconditionally
312 void add(MenuItem const & item) { items_.push_back(item); }
313 /// Checks the associated FuncRequest status before adding the
315 void addWithStatusCheck(MenuItem const &);
316 // Check whether the menu shortcuts are unique
317 void checkShortcuts() const;
319 void expandLastfiles();
320 void expandDocuments();
321 void expandBookmarks();
322 void expandFormats(MenuItem::Kind kind, Buffer const * buf);
323 void expandFloatListInsert(Buffer const * buf);
324 void expandFloatInsert(Buffer const * buf);
325 void expandFlexInsert(Buffer const * buf, InsetLayout::InsetLyXType type);
326 void expandToc2(Toc const & toc_list, size_t from, size_t to, int depth);
327 void expandToc(Buffer const * buf);
328 void expandPasteRecent(Buffer const * buf);
329 void expandToolbars();
330 void expandBranches(Buffer const * buf);
331 void expandIndices(Buffer const * buf, bool listof = false);
332 void expandIndicesContext(Buffer const * buf, bool listof = false);
333 void expandCiteStyles(BufferView const *);
334 void expandGraphicsGroups(BufferView const *);
335 void expandSpellingSuggestions(BufferView const *);
336 void expandLanguageSelector(Buffer const * buf);
344 /// Helper for std::find_if
348 MenuNamesEqual(QString const & name) : name_(name) {}
349 bool operator()(MenuDefinition const & menu) const { return menu.name() == name_; }
356 typedef std::vector<MenuDefinition> MenuList;
358 typedef MenuList::const_iterator const_iterator;
360 typedef MenuList::iterator iterator;
362 /////////////////////////////////////////////////////////////////////
363 // MenuDefinition implementation
364 /////////////////////////////////////////////////////////////////////
366 void MenuDefinition::addWithStatusCheck(MenuItem const & i)
370 case MenuItem::Command: {
371 FuncStatus status = lyx::getStatus(i.func());
372 if (status.unknown() || (!status.enabled() && i.optional()))
375 items_.back().status(status);
379 case MenuItem::Submenu: {
380 bool enabled = false;
381 if (i.hasSubmenu()) {
382 for (const_iterator cit = i.submenu().begin();
383 cit != i.submenu().end(); ++cit) {
384 // Only these kind of items affect the status of the submenu
385 if ((cit->kind() == MenuItem::Command
386 || cit->kind() == MenuItem::Submenu
387 || cit->kind() == MenuItem::Help)
388 && cit->status().enabled()) {
394 if (enabled || !i.optional()) {
396 items_.back().status().setEnabled(enabled);
401 case MenuItem::Separator:
402 if (!items_.empty() && items_.back().kind() != MenuItem::Separator)
412 void MenuDefinition::read(Lexer & lex)
429 md_indiceslistscontext,
443 md_spellingsuggestions,
447 LexerKeyword menutags[] = {
448 { "bookmarks", md_bookmarks },
449 { "branches", md_branches },
450 { "charstyles", md_charstyles },
451 { "citestyles", md_citestyles },
452 { "custom", md_custom },
453 { "documents", md_documents },
454 { "elements", md_elements },
455 { "end", md_endmenu },
456 { "exportformats", md_exportformats },
457 { "floatinsert", md_floatinsert },
458 { "floatlistinsert", md_floatlistinsert },
459 { "graphicsgroups", md_graphicsgroups },
460 { "importformats", md_importformats },
461 { "indices", md_indices },
462 { "indicescontext", md_indicescontext },
463 { "indiceslists", md_indiceslists },
464 { "indiceslistscontext", md_indiceslistscontext },
466 { "languageselector", md_languageselector },
467 { "lastfiles", md_lastfiles },
468 { "optitem", md_optitem },
469 { "optsubmenu", md_optsubmenu },
470 { "pasterecent", md_pasterecent },
471 { "separator", md_separator },
472 { "spellingsuggestions", md_spellingsuggestions },
473 { "submenu", md_submenu },
475 { "toolbars", md_toolbars },
476 { "updateformats", md_updateformats },
477 { "viewformats", md_viewformats }
480 lex.pushTable(menutags);
481 lex.setContext("MenuDefinition::read: ");
484 bool optional = false;
486 while (lex.isOK() && !quit) {
490 // fallback to md_item
493 docstring const name = translateIfPossible(lex.getDocString());
495 string const command = lex.getString();
496 FuncRequest func = lyxaction.lookupFunc(command);
497 FuncRequest::Origin origin = FuncRequest::MENU;
498 if (name_.startsWith("context-toc-"))
499 origin = FuncRequest::TOC;
500 add(MenuItem(MenuItem::Command, toqstr(name), func, optional, origin));
506 add(MenuItem(MenuItem::Separator));
510 add(MenuItem(MenuItem::Lastfiles));
514 add(MenuItem(MenuItem::CharStyles));
518 add(MenuItem(MenuItem::Custom));
522 add(MenuItem(MenuItem::Elements));
526 add(MenuItem(MenuItem::Documents));
530 add(MenuItem(MenuItem::Bookmarks));
534 add(MenuItem(MenuItem::Toc));
538 add(MenuItem(MenuItem::ViewFormats));
541 case md_updateformats:
542 add(MenuItem(MenuItem::UpdateFormats));
545 case md_exportformats:
546 add(MenuItem(MenuItem::ExportFormats));
549 case md_importformats:
550 add(MenuItem(MenuItem::ImportFormats));
553 case md_floatlistinsert:
554 add(MenuItem(MenuItem::FloatListInsert));
558 add(MenuItem(MenuItem::FloatInsert));
562 add(MenuItem(MenuItem::PasteRecent));
566 add(MenuItem(MenuItem::Toolbars));
570 add(MenuItem(MenuItem::Branches));
574 add(MenuItem(MenuItem::CiteStyles));
577 case md_graphicsgroups:
578 add(MenuItem(MenuItem::GraphicsGroups));
581 case md_spellingsuggestions:
582 add(MenuItem(MenuItem::SpellingSuggestions));
585 case md_languageselector:
586 add(MenuItem(MenuItem::LanguageSelector));
590 add(MenuItem(MenuItem::Indices));
593 case md_indicescontext:
594 add(MenuItem(MenuItem::IndicesContext));
597 case md_indiceslists:
598 add(MenuItem(MenuItem::IndicesLists));
601 case md_indiceslistscontext:
602 add(MenuItem(MenuItem::IndicesListsContext));
607 // fallback to md_submenu
610 docstring const mlabel = translateIfPossible(lex.getDocString());
612 docstring const mname = lex.getDocString();
613 add(MenuItem(MenuItem::Submenu,
614 toqstr(mlabel), toqstr(mname), optional));
624 lex.printError("Unknown menu tag");
632 MenuItem const & MenuDefinition::operator[](size_type i) const
638 bool MenuDefinition::hasFunc(FuncRequest const & func) const
640 for (const_iterator it = begin(), et = end(); it != et; ++it)
641 if (it->func() == func)
647 void MenuDefinition::cat(MenuDefinition const & other)
649 const_iterator et = other.end();
650 for (const_iterator it = other.begin(); it != et; ++it)
655 void MenuDefinition::checkShortcuts() const
657 // This is a quadratic algorithm, but we do not care because
658 // menus are short enough
659 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
660 QString shortcut = it1->shortcut();
661 if (shortcut.isEmpty())
663 if (!it1->label().contains(shortcut))
664 LYXERR0("Menu warning: menu entry \""
666 << "\" does not contain shortcut `"
667 << shortcut << "'.");
668 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
669 if (!it2->shortcut().compare(shortcut, Qt::CaseInsensitive)) {
670 LYXERR0("Menu warning: menu entries "
671 << '"' << it1->fulllabel()
672 << "\" and \"" << it2->fulllabel()
673 << "\" share the same shortcut.");
680 bool MenuDefinition::searchMenu(FuncRequest const & func, docstring_list & names) const
682 const_iterator m = begin();
683 const_iterator m_end = end();
684 for (; m != m_end; ++m) {
685 if (m->kind() == MenuItem::Command && m->func() == func) {
686 names.push_back(qstring_to_ucs4(m->label()));
689 if (m->kind() == MenuItem::Submenu) {
690 names.push_back(qstring_to_ucs4(m->label()));
691 if (!m->hasSubmenu()) {
692 LYXERR(Debug::GUI, "Warning: non existing sub menu label="
693 << m->label() << " name=" << m->submenuname());
697 if (m->submenu().searchMenu(func, names))
706 bool compareFormat(Format const * p1, Format const * p2)
712 QString limitStringLength(docstring const & str)
714 size_t const max_item_length = 45;
716 if (str.size() > max_item_length)
717 return toqstr(str.substr(0, max_item_length - 3) + "...");
723 void MenuDefinition::expandGraphicsGroups(BufferView const * bv)
728 graphics::getGraphicsGroups(bv->buffer(), grp);
732 set<string>::const_iterator it = grp.begin();
733 set<string>::const_iterator end = grp.end();
734 add(MenuItem(MenuItem::Command, qt_("No Group"),
735 FuncRequest(LFUN_SET_GRAPHICS_GROUP)));
736 for (; it != end; it++) {
737 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(*it) + '|',
738 FuncRequest(LFUN_SET_GRAPHICS_GROUP, *it)));
743 void MenuDefinition::expandSpellingSuggestions(BufferView const * bv)
748 docstring_list suggestions;
749 pos_type from = bv->cursor().pos();
751 Paragraph const & par = bv->cursor().paragraph();
752 SpellChecker::Result res = par.spellCheck(from, to, wl, suggestions, true, true);
754 case SpellChecker::UNKNOWN_WORD:
755 if (lyxrc.spellcheck_continuously) {
756 LYXERR(Debug::GUI, "Misspelled Word! Suggested Words = ");
758 size_t m = 10; // first submenu index
759 MenuItem item(MenuItem::Submenu, qt_("More Spelling Suggestions"));
760 item.setSubmenu(MenuDefinition(qt_("More Spelling Suggestions")));
761 for (; i != suggestions.size(); ++i) {
762 docstring const & suggestion = suggestions[i];
763 LYXERR(Debug::GUI, suggestion);
764 MenuItem w(MenuItem::Command, toqstr(suggestion),
765 FuncRequest(LFUN_WORD_REPLACE, suggestion));
769 item.submenu().add(w);
774 add(MenuItem(MenuItem::Separator));
775 docstring const arg = wl.word() + " " + from_ascii(wl.lang()->lang());
776 add(MenuItem(MenuItem::Command, qt_("Add to personal dictionary|n"),
777 FuncRequest(LFUN_SPELLING_ADD, arg)));
778 add(MenuItem(MenuItem::Command, qt_("Ignore all|I"),
779 FuncRequest(LFUN_SPELLING_IGNORE, arg)));
782 case SpellChecker::LEARNED_WORD: {
783 LYXERR(Debug::GUI, "Learned Word.");
784 docstring const arg = wl.word() + " " + from_ascii(wl.lang()->lang());
785 add(MenuItem(MenuItem::Command, qt_("Remove from personal dictionary|r"),
786 FuncRequest(LFUN_SPELLING_REMOVE, arg)));
789 case SpellChecker::WORD_OK:
790 case SpellChecker::COMPOUND_WORD:
791 case SpellChecker::ROOT_FOUND:
792 case SpellChecker::IGNORED_WORD:
797 struct sortLanguageByName {
798 bool operator()(const Language * a, const Language * b) const {
799 return qt_(a->display()).localeAwareCompare(qt_(b->display())) < 0;
803 void MenuDefinition::expandLanguageSelector(Buffer const * buf)
808 std::set<Language const *> languages_buffer =
809 buf->masterBuffer()->getLanguages();
811 if (languages_buffer.size() < 2)
814 std::set<Language const *, sortLanguageByName> languages;
816 std::set<Language const *>::const_iterator const beg =
817 languages_buffer.begin();
818 for (std::set<Language const *>::const_iterator cit = beg;
819 cit != languages_buffer.end(); ++cit) {
820 languages.insert(*cit);
823 MenuItem item(MenuItem::Submenu, qt_("Language|L"));
824 item.setSubmenu(MenuDefinition(qt_("Language")));
825 QString morelangs = qt_("More Languages ...|M");
826 QStringList accelerators;
827 if (morelangs.contains('|'))
828 accelerators.append(morelangs.section('|', -1));
829 std::set<Language const *, sortLanguageByName>::const_iterator const begin = languages.begin();
830 for (std::set<Language const *, sortLanguageByName>::const_iterator cit = begin;
831 cit != languages.end(); ++cit) {
832 QString label = qt_((*cit)->display());
833 // try to add an accelerator
834 bool success = false;
835 // try capitals first
836 for (int i = 0; i < label.size(); ++i) {
837 QChar const ch = label[i];
840 if (!accelerators.contains(ch, Qt::CaseInsensitive)) {
841 label = label + toqstr("|") + ch;
842 accelerators.append(ch);
847 // if all capitals are taken, try the rest
849 for (int i = 0; i < label.size(); ++i) {
850 if (label[i].isSpace())
852 QString const ch = QString(label[i]);
853 if (!accelerators.contains(ch, Qt::CaseInsensitive)) {
854 label = label + toqstr("|") + ch;
855 accelerators.append(ch);
860 MenuItem w(MenuItem::Command, label,
861 FuncRequest(LFUN_LANGUAGE, (*cit)->lang()));
862 item.submenu().addWithStatusCheck(w);
864 item.submenu().add(MenuItem(MenuItem::Separator));
865 item.submenu().add(MenuItem(MenuItem::Command, morelangs,
866 FuncRequest(LFUN_DIALOG_SHOW, "character")));
871 void MenuDefinition::expandLastfiles()
873 LastFilesSection::LastFiles const & lf = theSession().lastFiles().lastFiles();
874 LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
878 for (; lfit != lf.end() && ii <= lyxrc.num_lastfiles; ++lfit, ++ii) {
879 string const file = lfit->absFileName();
882 label = QString("%1. %2|%3").arg(ii)
883 .arg(toqstr(makeDisplayPath(file, 30))).arg(ii);
885 label = QString("%1. %2").arg(ii)
886 .arg(toqstr(makeDisplayPath(file, 30)));
887 add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, file)));
892 void MenuDefinition::expandDocuments()
894 MenuItem item(MenuItem::Submenu, qt_("Hidden|H"));
895 item.setSubmenu(MenuDefinition(qt_("Hidden|H")));
897 Buffer * first = theBufferList().first();
899 add(MenuItem(MenuItem::Info, qt_("<No Documents Open>")));
905 if (!guiApp->currentView())
907 GuiWorkArea * wa = guiApp->currentView()->workArea(i);
910 Buffer const & b = wa->bufferView().buffer();
911 QString label = toqstr(b.fileName().displayName(20));
915 label = QString::number(i) + ". " + label + '|' + QString::number(i);
916 add(MenuItem(MenuItem::Command, label,
917 FuncRequest(LFUN_BUFFER_SWITCH, b.absFileName())));
924 // We cannot use a for loop as the buffer list cycles.
926 bool const shown = guiApp->currentView()
927 ? guiApp->currentView()->workArea(*b) : false;
929 QString label = toqstr(b->fileName().displayName(20));
933 label = QString::number(i) + ". " + label + '|' + QString::number(i);
934 item.submenu().add(MenuItem(MenuItem::Command, label,
935 FuncRequest(LFUN_BUFFER_SWITCH, b->absFileName())));
938 b = theBufferList().next(b);
939 } while (b != first);
941 if (!item.submenu().empty())
946 void MenuDefinition::expandBookmarks()
948 lyx::BookmarksSection const & bm = theSession().bookmarks();
951 for (size_t i = 1; i <= bm.size(); ++i) {
953 string const file = bm.bookmark(i).filename.absFileName();
954 QString const label = QString("%1. %2|%3").arg(i)
955 .arg(toqstr(makeDisplayPath(file, 20))).arg(i);
956 add(MenuItem(MenuItem::Command, label,
957 FuncRequest(LFUN_BOOKMARK_GOTO, convert<docstring>(i))));
962 add(MenuItem(MenuItem::Info, qt_("<No Bookmarks Saved Yet>")));
966 void MenuDefinition::expandFormats(MenuItem::Kind kind, Buffer const * buf)
968 if (!buf && kind != MenuItem::ImportFormats)
971 typedef vector<Format const *> Formats;
976 case MenuItem::ImportFormats:
977 formats = theConverters().importableFormats();
978 action = LFUN_BUFFER_IMPORT;
980 case MenuItem::ViewFormats:
981 formats = buf->exportableFormats(true);
982 action = LFUN_BUFFER_VIEW;
984 case MenuItem::UpdateFormats:
985 formats = buf->exportableFormats(true);
986 action = LFUN_BUFFER_UPDATE;
989 formats = buf->exportableFormats(false);
990 action = LFUN_BUFFER_EXPORT;
992 sort(formats.begin(), formats.end(), &compareFormat);
994 bool const view_update = (kind == MenuItem::ViewFormats
995 || kind == MenuItem::UpdateFormats);
999 smenue = (kind == MenuItem::ViewFormats ?
1000 qt_("View (Other Formats)|F")
1001 : qt_("Update (Other Formats)|p"));
1002 MenuItem item(MenuItem::Submenu, smenue);
1003 item.setSubmenu(MenuDefinition(smenue));
1005 Formats::const_iterator fit = formats.begin();
1006 Formats::const_iterator end = formats.end();
1007 for (; fit != end ; ++fit) {
1008 if ((*fit)->dummy())
1011 docstring lab = from_utf8((*fit)->prettyname());
1012 docstring scut = from_utf8((*fit)->shortcut());
1013 docstring const tmplab = lab;
1016 lab += char_type('|') + scut;
1017 docstring lab_i18n = translateIfPossible(lab);
1018 bool const untranslated = (lab == lab_i18n);
1019 QString const shortcut = toqstr(split(lab_i18n, lab, '|'));
1020 QString label = toqstr(lab);
1022 // this might happen if the shortcut
1023 // has been redefined
1024 label = toqstr(translateIfPossible(tmplab));
1027 case MenuItem::ImportFormats:
1030 case MenuItem::ViewFormats:
1031 case MenuItem::UpdateFormats:
1032 if ((*fit)->name() == buf->getDefaultOutputFormat()) {
1033 docstring lbl = (kind == MenuItem::ViewFormats ?
1034 bformat(_("View [%1$s]|V"), qstring_to_ucs4(label))
1035 : bformat(_("Update [%1$s]|U"), qstring_to_ucs4(label)));
1036 MenuItem w(MenuItem::Command, toqstr(lbl),
1037 FuncRequest(action, (*fit)->name()));
1041 case MenuItem::ExportFormats:
1042 if (!(*fit)->documentFormat())
1046 LASSERT(false, /**/);
1049 if (!shortcut.isEmpty())
1050 label += '|' + shortcut;
1054 item.submenu().addWithStatusCheck(MenuItem(MenuItem::Command, label,
1055 FuncRequest(action, (*fit)->name())));
1057 item.submenu().add(MenuItem(MenuItem::Command, label,
1058 FuncRequest(action, (*fit)->name())));
1061 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1062 FuncRequest(action, (*fit)->name())));
1064 add(MenuItem(MenuItem::Command, label,
1065 FuncRequest(action, (*fit)->name())));
1073 void MenuDefinition::expandFloatListInsert(Buffer const * buf)
1078 FloatList const & floats = buf->params().documentClass().floats();
1079 FloatList::const_iterator cit = floats.begin();
1080 FloatList::const_iterator end = floats.end();
1081 for (; cit != end; ++cit) {
1082 addWithStatusCheck(MenuItem(MenuItem::Command,
1083 qt_(cit->second.listName()),
1084 FuncRequest(LFUN_FLOAT_LIST_INSERT,
1085 cit->second.floattype())));
1090 void MenuDefinition::expandFloatInsert(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();
1098 for (; cit != end; ++cit) {
1100 QString const label = qt_(cit->second.name());
1101 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1102 FuncRequest(LFUN_FLOAT_INSERT,
1103 cit->second.floattype())));
1108 void MenuDefinition::expandFlexInsert(
1109 Buffer const * buf, InsetLayout::InsetLyXType type)
1114 TextClass::InsetLayouts const & insetLayouts =
1115 buf->params().documentClass().insetLayouts();
1116 TextClass::InsetLayouts::const_iterator cit = insetLayouts.begin();
1117 TextClass::InsetLayouts::const_iterator end = insetLayouts.end();
1118 for (; cit != end; ++cit) {
1119 if (cit->second.lyxtype() == type) {
1120 docstring label = cit->first;
1121 // we remove the "Flex:" prefix, if it is present
1122 if (prefixIs(label, from_utf8("Flex:")))
1123 label = label.substr(5);
1124 addWithStatusCheck(MenuItem(MenuItem::Command,
1125 toqstr(translateIfPossible(label)),
1126 FuncRequest(LFUN_FLEX_INSERT, Lexer::quoteString(label))));
1129 // FIXME This is a little clunky.
1130 if (items_.empty() && type == InsetLayout::CUSTOM)
1131 add(MenuItem(MenuItem::Help, qt_("No Custom Insets Defined!")));
1135 size_t const max_number_of_items = 25;
1137 void MenuDefinition::expandToc2(Toc const & toc_list,
1138 size_t from, size_t to, int depth)
1140 int shortcut_count = 0;
1142 // check whether depth is smaller than the smallest depth in toc.
1143 int min_depth = 1000;
1144 for (size_t i = from; i < to; ++i)
1145 min_depth = min(min_depth, toc_list[i].depth());
1146 if (min_depth > depth)
1149 if (to - from <= max_number_of_items) {
1150 for (size_t i = from; i < to; ++i) {
1151 QString label(4 * max(0, toc_list[i].depth() - depth), ' ');
1152 label += limitStringLength(toc_list[i].str());
1153 if (toc_list[i].depth() == depth) {
1155 if (shortcut_count < 9) {
1156 if (label.contains(QString::number(shortcut_count + 1)))
1157 label += QString::number(++shortcut_count);
1160 add(MenuItem(MenuItem::Command, label,
1161 FuncRequest(toc_list[i].action())));
1166 size_t new_pos = pos + 1;
1167 while (new_pos < to && toc_list[new_pos].depth() > depth)
1170 QString label(4 * max(0, toc_list[pos].depth() - depth), ' ');
1171 label += limitStringLength(toc_list[pos].str());
1172 if (toc_list[pos].depth() == depth) {
1174 if (shortcut_count < 9) {
1175 if (label.contains(QString::number(shortcut_count + 1)))
1176 label += QString::number(++shortcut_count);
1179 if (new_pos == pos + 1) {
1180 add(MenuItem(MenuItem::Command,
1181 label, FuncRequest(toc_list[pos].action())));
1184 sub.expandToc2(toc_list, pos, new_pos, depth + 1);
1185 MenuItem item(MenuItem::Submenu, label);
1186 item.setSubmenu(sub);
1195 void MenuDefinition::expandToc(Buffer const * buf)
1197 // To make things very cleanly, we would have to pass buf to
1198 // all MenuItem constructors and to expandToc2. However, we
1199 // know that all the entries in a TOC will be have status_ ==
1200 // OK, so we avoid this unnecessary overhead (JMarc)
1203 add(MenuItem(MenuItem::Info, qt_("<No Document Open>")));
1207 // Add an entry for the master doc if this is a child doc
1208 Buffer const * const master = buf->masterBuffer();
1209 if (buf != master) {
1210 ParIterator const pit = par_iterator_begin(master->inset());
1211 string const arg = convert<string>(pit->id());
1212 FuncRequest f(LFUN_PARAGRAPH_GOTO, arg);
1213 add(MenuItem(MenuItem::Command, qt_("Master Document"), f));
1216 MenuDefinition other_lists;
1218 FloatList const & floatlist = buf->params().documentClass().floats();
1219 TocList const & toc_list = buf->tocBackend().tocs();
1220 TocList::const_iterator cit = toc_list.begin();
1221 TocList::const_iterator end = toc_list.end();
1222 for (; cit != end; ++cit) {
1223 // Handle this later
1224 if (cit->first == "tableofcontents")
1227 MenuDefinition submenu;
1228 if (cit->second.size() >= 30) {
1229 FuncRequest f(LFUN_DIALOG_SHOW, "toc " + cit->first);
1230 submenu.add(MenuItem(MenuItem::Command, qt_("Open Navigator..."), f));
1232 TocIterator ccit = cit->second.begin();
1233 TocIterator eend = cit->second.end();
1234 for (; ccit != eend; ++ccit) {
1235 submenu.add(MenuItem(MenuItem::Command,
1236 limitStringLength(ccit->str()) + '|',
1237 FuncRequest(ccit->action())));
1241 MenuItem item(MenuItem::Submenu, guiName(cit->first, buf->params()));
1242 item.setSubmenu(submenu);
1243 if (floatlist.typeExist(cit->first) || cit->first == "child") {
1244 // Those two types deserve to be in the main menu.
1245 item.setSubmenu(submenu);
1248 other_lists.add(item);
1250 if (!other_lists.empty()) {
1251 MenuItem item(MenuItem::Submenu, qt_("Other Lists"));
1252 item.setSubmenu(other_lists);
1256 // Handle normal TOC
1257 cit = toc_list.find("tableofcontents");
1259 LYXERR(Debug::GUI, "No table of contents.");
1261 if (cit->second.size() > 0 )
1262 expandToc2(cit->second, 0, cit->second.size(), 0);
1264 add(MenuItem(MenuItem::Info, qt_("<Empty Table of Contents>")));
1269 void MenuDefinition::expandPasteRecent(Buffer const * buf)
1271 docstring_list const sel = cap::availableSelections(buf);
1273 docstring_list::const_iterator cit = sel.begin();
1274 docstring_list::const_iterator end = sel.end();
1276 for (unsigned int index = 0; cit != end; ++cit, ++index) {
1277 add(MenuItem(MenuItem::Command, toqstr(*cit) + '|',
1278 FuncRequest(LFUN_PASTE, convert<string>(index))));
1283 void MenuDefinition::expandToolbars()
1285 MenuDefinition other_lists;
1286 // extracts the toolbars from the backend
1287 Toolbars::Infos::const_iterator cit = guiApp->toolbars().begin();
1288 Toolbars::Infos::const_iterator end = guiApp->toolbars().end();
1289 for (; cit != end; ++cit) {
1290 MenuItem const item(MenuItem::Command, toqstr(cit->gui_name),
1291 FuncRequest(LFUN_TOOLBAR_TOGGLE, cit->name));
1292 if (guiApp->toolbars().isMainToolbar(cit->name))
1295 other_lists.add(item);
1298 if (!other_lists.empty()) {
1299 MenuItem item(MenuItem::Submenu, qt_("Other Toolbars"));
1300 item.setSubmenu(other_lists);
1306 void MenuDefinition::expandBranches(Buffer const * buf)
1311 BufferParams const & master_params = buf->masterBuffer()->params();
1312 BufferParams const & params = buf->params();
1313 if (params.branchlist().empty() && master_params.branchlist().empty() ) {
1314 add(MenuItem(MenuItem::Help, qt_("No Branches Set for Document!")));
1318 BranchList::const_iterator cit = master_params.branchlist().begin();
1319 BranchList::const_iterator end = master_params.branchlist().end();
1321 for (int ii = 1; cit != end; ++cit, ++ii) {
1322 docstring label = cit->branch();
1324 label = convert<docstring>(ii) + ". " + label
1325 + char_type('|') + convert<docstring>(ii);
1327 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1328 FuncRequest(LFUN_BRANCH_INSERT,
1332 if (buf == buf->masterBuffer())
1335 MenuDefinition child_branches;
1337 BranchList::const_iterator ccit = params.branchlist().begin();
1338 BranchList::const_iterator cend = params.branchlist().end();
1340 for (int ii = 1; ccit != cend; ++ccit, ++ii) {
1341 docstring label = ccit->branch();
1343 label = convert<docstring>(ii) + ". " + label
1344 + char_type('|') + convert<docstring>(ii);
1346 label += char_type('|');
1347 child_branches.addWithStatusCheck(MenuItem(MenuItem::Command,
1349 FuncRequest(LFUN_BRANCH_INSERT,
1353 if (!child_branches.empty()) {
1354 MenuItem item(MenuItem::Submenu, qt_("Child Document"));
1355 item.setSubmenu(child_branches);
1361 void MenuDefinition::expandIndices(Buffer const * buf, bool listof)
1366 BufferParams const & params = buf->masterBuffer()->params();
1367 if (!params.use_indices) {
1369 addWithStatusCheck(MenuItem(MenuItem::Command,
1370 qt_("Index List|I"),
1371 FuncRequest(LFUN_INDEX_PRINT,
1372 from_ascii("idx"))));
1374 addWithStatusCheck(MenuItem(MenuItem::Command,
1375 qt_("Index Entry|d"),
1376 FuncRequest(LFUN_INDEX_INSERT,
1377 from_ascii("idx"))));
1381 if (params.indiceslist().empty())
1384 IndicesList::const_iterator cit = params.indiceslist().begin();
1385 IndicesList::const_iterator end = params.indiceslist().end();
1387 for (int ii = 1; cit != end; ++cit, ++ii) {
1389 docstring const label =
1390 bformat(_("Index: %1$s"), cit->index());
1391 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1392 FuncRequest(LFUN_INDEX_PRINT, cit->shortcut())));
1394 docstring const label =
1395 bformat(_("Index Entry (%1$s)"), cit->index());
1396 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1397 FuncRequest(LFUN_INDEX_INSERT, cit->shortcut())));
1403 void MenuDefinition::expandIndicesContext(Buffer const * buf, bool listof)
1408 BufferParams const & params = buf->masterBuffer()->params();
1409 if (!params.use_indices || params.indiceslist().empty())
1412 IndicesList::const_iterator cit = params.indiceslist().begin();
1413 IndicesList::const_iterator end = params.indiceslist().end();
1415 for (int ii = 1; cit != end; ++cit, ++ii) {
1417 InsetCommandParams p(INDEX_PRINT_CODE);
1418 p["type"] = cit->shortcut();
1419 string const data = InsetCommand::params2string(p);
1420 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(cit->index()),
1421 FuncRequest(LFUN_INSET_MODIFY, data)));
1423 docstring const label =
1424 bformat(_("Index Entry (%1$s)"), cit->index());
1425 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1426 FuncRequest(LFUN_INSET_MODIFY,
1427 from_ascii("changetype ") + cit->shortcut())));
1433 void MenuDefinition::expandCiteStyles(BufferView const * bv)
1438 Inset const * inset = bv->cursor().nextInset();
1439 if (!inset || inset->lyxCode() != CITE_CODE) {
1440 add(MenuItem(MenuItem::Command,
1441 qt_("No Citation in Scope!"),
1442 FuncRequest(LFUN_NOACTION)));
1445 InsetCommand const * citinset =
1446 static_cast<InsetCommand const *>(inset);
1448 Buffer const * buf = &bv->buffer();
1449 docstring key = citinset->getParam("key");
1450 // we can only handle one key currently
1451 if (contains(key, ','))
1452 key = qstring_to_ucs4(toqstr(key).split(',')[0]);
1454 vector<CiteStyle> citeStyleList = citeStyles(buf->params().citeEngine());
1455 docstring_list citeStrings =
1456 buf->masterBibInfo().getCiteStrings(key, bv->buffer());
1458 docstring_list::const_iterator cit = citeStrings.begin();
1459 docstring_list::const_iterator end = citeStrings.end();
1461 for (int ii = 1; cit != end; ++cit, ++ii) {
1462 docstring label = *cit;
1464 CiteStyle cst = citeStyleList[ii - 1];
1466 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1467 FuncRequest(LFUN_INSET_MODIFY,
1468 "changetype " + from_utf8(citationStyleToString(cs)))));
1475 /////////////////////////////////////////////////////////////////////
1476 // Menu::Impl definition and implementation
1477 /////////////////////////////////////////////////////////////////////
1481 /// populates the menu or one of its submenu
1482 /// This is used as a recursive function
1483 void populate(QMenu & qMenu, MenuDefinition const & menu);
1485 /// Only needed for top level menus.
1486 MenuDefinition * top_level_menu;
1489 /// the name of this menu
1495 /// Get a MenuDefinition item label from the menu backend
1496 static QString label(MenuItem const & mi)
1498 QString label = mi.label();
1499 label.replace("&", "&&");
1501 QString shortcut = mi.shortcut();
1502 if (!shortcut.isEmpty()) {
1503 int pos = label.indexOf(shortcut);
1505 //label.insert(pos, 1, char_type('&'));
1506 label.replace(pos, 0, "&");
1509 QString const binding = mi.binding();
1510 if (!binding.isEmpty())
1511 label += '\t' + binding;
1516 void Menu::Impl::populate(QMenu & qMenu, MenuDefinition const & menu)
1518 LYXERR(Debug::GUI, "populating menu " << menu.name());
1519 if (menu.size() == 0) {
1520 LYXERR(Debug::GUI, "\tERROR: empty menu " << menu.name());
1523 LYXERR(Debug::GUI, " ***** menu entries " << menu.size());
1524 MenuDefinition::const_iterator m = menu.begin();
1525 MenuDefinition::const_iterator end = menu.end();
1526 for (; m != end; ++m) {
1527 if (m->kind() == MenuItem::Separator)
1528 qMenu.addSeparator();
1529 else if (m->kind() == MenuItem::Submenu) {
1530 QMenu * subMenu = qMenu.addMenu(label(*m));
1531 populate(*subMenu, m->submenu());
1532 subMenu->setEnabled(m->status().enabled());
1534 // we have a MenuItem::Command
1535 qMenu.addAction(new Action(view, QIcon(), label(*m),
1536 m->func(), QString(), &qMenu));
1541 /////////////////////////////////////////////////////////////////////
1542 // Menu implementation
1543 /////////////////////////////////////////////////////////////////////
1545 Menu::Menu(GuiView * gv, QString const & name, bool top_level)
1546 : QMenu(gv), d(new Menu::Impl)
1548 d->top_level_menu = top_level? new MenuDefinition : 0;
1552 if (d->top_level_menu)
1553 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
1559 delete d->top_level_menu;
1564 void Menu::updateView()
1566 guiApp->menus().updateMenu(this);
1570 /////////////////////////////////////////////////////////////////////
1571 // Menus::Impl definition and implementation
1572 /////////////////////////////////////////////////////////////////////
1574 struct Menus::Impl {
1576 bool hasMenu(QString const &) const;
1578 MenuDefinition & getMenu(QString const &);
1580 MenuDefinition const & getMenu(QString const &) const;
1582 /// Expands some special entries of the menu
1583 /** The entries with the following kind are expanded to a
1584 sequence of Command MenuItems: Lastfiles, Documents,
1585 ViewFormats, ExportFormats, UpdateFormats, Branches, Indices
1587 void expand(MenuDefinition const & frommenu, MenuDefinition & tomenu,
1588 BufferView const *) const;
1590 /// Initialize specific MACOS X menubar
1591 void macxMenuBarInit(GuiView * view, QMenuBar * qmb);
1593 /// Mac special menu.
1594 /** This defines a menu whose entries list the FuncRequests
1595 that will be removed by expand() in other menus. This is
1596 used by the Qt/Mac code.
1598 NOTE: Qt does not remove the menu items when clearing a QMenuBar,
1599 such that the items will keep accessing the FuncRequests in
1600 the MenuDefinition. While Menus::Impl might be recreated,
1601 we keep mac_special_menu_ in memory by making it static.
1603 static MenuDefinition mac_special_menu_;
1608 MenuDefinition menubar_;
1610 typedef QMap<GuiView *, QHash<QString, Menu*> > NameMap;
1612 /// name to menu for \c menu() method.
1617 MenuDefinition Menus::Impl::mac_special_menu_;
1621 Here is what the Qt documentation says about how a menubar is chosen:
1623 1) If the window has a QMenuBar then it is used. 2) If the window
1624 is a modal then its menubar is used. If no menubar is specified
1625 then a default menubar is used (as documented below) 3) If the
1626 window has no parent then the default menubar is used (as
1629 The above 3 steps are applied all the way up the parent window
1630 chain until one of the above are satisifed. If all else fails a
1631 default menubar will be created, the default menubar on Qt/Mac is
1632 an empty menubar, however you can create a different default
1633 menubar by creating a parentless QMenuBar, the first one created
1634 will thus be designated the default menubar, and will be used
1635 whenever a default menubar is needed.
1637 Thus, for Qt/Mac, we add the menus to a free standing menubar, so
1638 that this menubar will be used also when one of LyX' dialogs has
1641 void Menus::Impl::macxMenuBarInit(GuiView * view, QMenuBar * qmb)
1643 /* Since Qt 4.2, the qt/mac menu code has special code for
1644 specifying the role of a menu entry. However, it does not
1645 work very well with our scheme of creating menus on demand,
1646 and therefore we need to put these entries in a special
1647 invisible menu. (JMarc)
1650 /* The entries of our special mac menu. If we add support for
1651 * special entries in Menus, we could imagine something
1653 * SpecialItem About " "About LyX" "dialog-show aboutlyx"
1654 * and therefore avoid hardcoding. I am not sure it is worth
1655 * the hassle, though. (JMarc)
1657 struct MacMenuEntry {
1661 QAction::MenuRole role;
1664 MacMenuEntry entries[] = {
1665 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
1666 QAction::AboutRole},
1667 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
1668 QAction::PreferencesRole},
1669 {LFUN_RECONFIGURE, "", "Reconfigure",
1670 QAction::ApplicationSpecificRole},
1671 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
1673 const size_t num_entries = sizeof(entries) / sizeof(entries[0]);
1675 // the special menu for Menus. Fill it up only once.
1676 if (mac_special_menu_.size() == 0) {
1677 for (size_t i = 0 ; i < num_entries ; ++i) {
1678 FuncRequest const func(entries[i].action,
1679 from_utf8(entries[i].arg));
1680 mac_special_menu_.add(MenuItem(MenuItem::Command,
1681 entries[i].label, func));
1685 // add the entries to a QMenu that will eventually be empty
1686 // and therefore invisible.
1687 QMenu * qMenu = qmb->addMenu("special");
1688 MenuDefinition::const_iterator cit = mac_special_menu_.begin();
1689 MenuDefinition::const_iterator end = mac_special_menu_.end();
1690 for (size_t i = 0 ; cit != end ; ++cit, ++i) {
1691 Action * action = new Action(view, QIcon(), cit->label(),
1692 cit->func(), QString(), qMenu);
1693 action->setMenuRole(entries[i].role);
1694 qMenu->addAction(action);
1699 void Menus::Impl::expand(MenuDefinition const & frommenu,
1700 MenuDefinition & tomenu, BufferView const * bv) const
1702 if (!tomenu.empty())
1705 for (MenuDefinition::const_iterator cit = frommenu.begin();
1706 cit != frommenu.end() ; ++cit) {
1707 Buffer const * buf = bv ? &bv->buffer() : 0;
1708 switch (cit->kind()) {
1709 case MenuItem::Lastfiles:
1710 tomenu.expandLastfiles();
1713 case MenuItem::Documents:
1714 tomenu.expandDocuments();
1717 case MenuItem::Bookmarks:
1718 tomenu.expandBookmarks();
1721 case MenuItem::ImportFormats:
1722 case MenuItem::ViewFormats:
1723 case MenuItem::UpdateFormats:
1724 case MenuItem::ExportFormats:
1725 tomenu.expandFormats(cit->kind(), buf);
1728 case MenuItem::CharStyles:
1729 tomenu.expandFlexInsert(buf, InsetLayout::CHARSTYLE);
1732 case MenuItem::Custom:
1733 tomenu.expandFlexInsert(buf, InsetLayout::CUSTOM);
1736 case MenuItem::Elements:
1737 tomenu.expandFlexInsert(buf, InsetLayout::ELEMENT);
1740 case MenuItem::FloatListInsert:
1741 tomenu.expandFloatListInsert(buf);
1744 case MenuItem::FloatInsert:
1745 tomenu.expandFloatInsert(buf);
1748 case MenuItem::PasteRecent:
1749 tomenu.expandPasteRecent(buf);
1752 case MenuItem::Toolbars:
1753 tomenu.expandToolbars();
1756 case MenuItem::Branches:
1757 tomenu.expandBranches(buf);
1760 case MenuItem::Indices:
1761 tomenu.expandIndices(buf);
1764 case MenuItem::IndicesContext:
1765 tomenu.expandIndicesContext(buf);
1768 case MenuItem::IndicesLists:
1769 tomenu.expandIndices(buf, true);
1772 case MenuItem::IndicesListsContext:
1773 tomenu.expandIndicesContext(buf, true);
1776 case MenuItem::CiteStyles:
1777 tomenu.expandCiteStyles(bv);
1781 tomenu.expandToc(buf);
1784 case MenuItem::GraphicsGroups:
1785 tomenu.expandGraphicsGroups(bv);
1788 case MenuItem::SpellingSuggestions:
1789 tomenu.expandSpellingSuggestions(bv);
1792 case MenuItem::LanguageSelector:
1793 tomenu.expandLanguageSelector(buf);
1796 case MenuItem::Submenu: {
1797 MenuItem item(*cit);
1798 item.setSubmenu(MenuDefinition(cit->submenuname()));
1799 expand(getMenu(cit->submenuname()), item.submenu(), bv);
1800 tomenu.addWithStatusCheck(item);
1804 case MenuItem::Info:
1805 case MenuItem::Help:
1806 case MenuItem::Separator:
1807 tomenu.addWithStatusCheck(*cit);
1810 case MenuItem::Command:
1811 if (!mac_special_menu_.hasFunc(cit->func()))
1812 tomenu.addWithStatusCheck(*cit);
1816 // we do not want the menu to end with a separator
1817 if (!tomenu.empty() && tomenu.items_.back().kind() == MenuItem::Separator)
1818 tomenu.items_.pop_back();
1820 // Check whether the shortcuts are unique
1821 tomenu.checkShortcuts();
1825 bool Menus::Impl::hasMenu(QString const & name) const
1827 return find_if(menulist_.begin(), menulist_.end(),
1828 MenuNamesEqual(name)) != menulist_.end();
1832 MenuDefinition const & Menus::Impl::getMenu(QString const & name) const
1834 const_iterator cit = find_if(menulist_.begin(), menulist_.end(),
1835 MenuNamesEqual(name));
1836 if (cit == menulist_.end())
1837 LYXERR0("No submenu named " << name);
1838 LASSERT(cit != menulist_.end(), /**/);
1843 MenuDefinition & Menus::Impl::getMenu(QString const & name)
1845 iterator it = find_if(menulist_.begin(), menulist_.end(),
1846 MenuNamesEqual(name));
1847 if (it == menulist_.end())
1848 LYXERR0("No submenu named " << name);
1849 LASSERT(it != menulist_.end(), /**/);
1854 /////////////////////////////////////////////////////////////////////
1858 /////////////////////////////////////////////////////////////////////
1860 Menus::Menus() : d(new Impl) {}
1876 void Menus::read(Lexer & lex)
1884 LexerKeyword menutags[] = {
1885 { "end", md_endmenuset },
1886 { "menu", md_menu },
1887 { "menubar", md_menubar }
1890 // consistency check
1891 if (compare_ascii_no_case(lex.getString(), "menuset"))
1892 LYXERR0("Menus::read: ERROR wrong token: `" << lex.getString() << '\'');
1894 lex.pushTable(menutags);
1895 lex.setContext("Menus::read");
1899 while (lex.isOK() && !quit) {
1900 switch (lex.lex()) {
1902 d->menubar_.read(lex);
1906 QString const name = toqstr(lex.getDocString());
1907 if (d->hasMenu(name))
1908 d->getMenu(name).read(lex);
1910 MenuDefinition menu(name);
1912 d->menulist_.push_back(menu);
1920 lex.printError("Unknown menu tag");
1928 bool Menus::searchMenu(FuncRequest const & func,
1929 docstring_list & names) const
1931 MenuDefinition menu;
1932 d->expand(d->menubar_, menu, 0);
1933 return menu.searchMenu(func, names);
1937 void Menus::fillMenuBar(QMenuBar * qmb, GuiView * view, bool initial)
1941 // setup special mac specific menu items, but only do this
1942 // the first time a QMenuBar is created. Otherwise Qt will
1943 // create duplicate items in the application menu. It seems
1944 // that Qt does not remove them when the QMenubar is cleared.
1945 LYXERR(Debug::GUI, "Creating Mac OS X special menu bar");
1946 d->macxMenuBarInit(view, qmb);
1949 // Clear all menubar contents before filling it.
1953 LYXERR(Debug::GUI, "populating menu bar" << d->menubar_.name());
1955 if (d->menubar_.size() == 0) {
1956 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
1957 << d->menubar_.name());
1960 LYXERR(Debug::GUI, "menu bar entries " << d->menubar_.size());
1962 MenuDefinition menu;
1963 BufferView * bv = 0;
1965 bv = view->currentBufferView();
1966 d->expand(d->menubar_, menu, bv);
1968 MenuDefinition::const_iterator m = menu.begin();
1969 MenuDefinition::const_iterator end = menu.end();
1971 for (; m != end; ++m) {
1973 if (m->kind() != MenuItem::Submenu) {
1974 LYXERR(Debug::GUI, "\tERROR: not a submenu " << m->label());
1978 LYXERR(Debug::GUI, "menu bar item " << m->label()
1979 << " is a submenu named " << m->submenuname());
1981 QString name = m->submenuname();
1982 if (!d->hasMenu(name)) {
1983 LYXERR(Debug::GUI, "\tERROR: " << name
1984 << " submenu has no menu!");
1988 Menu * menu = new Menu(view, m->submenuname(), true);
1989 menu->setTitle(label(*m));
1992 d->name_map_[view][name] = menu;
1997 void Menus::updateMenu(Menu * qmenu)
1999 LYXERR(Debug::GUI, "Triggered menu: " << qmenu->d->name);
2002 if (qmenu->d->name.isEmpty())
2005 docstring identifier = qstring_to_ucs4(qmenu->d->name);
2006 MenuDefinition fromLyxMenu(qmenu->d->name);
2007 while (!identifier.empty()) {
2008 docstring menu_name;
2009 identifier = split(identifier, menu_name, ';');
2011 if (!d->hasMenu(toqstr(menu_name))) {
2012 LYXERR(Debug::GUI, "\tWARNING: non existing menu: "
2017 fromLyxMenu.cat(d->getMenu(toqstr(menu_name)));
2018 fromLyxMenu.add(MenuItem(MenuItem::Separator));
2021 if (fromLyxMenu.empty()) {
2022 qmenu->addAction(qt_("No Action Defined!"));
2026 BufferView * bv = 0;
2028 bv = qmenu->d->view->currentBufferView();
2029 d->expand(fromLyxMenu, *qmenu->d->top_level_menu, bv);
2030 qmenu->d->populate(*qmenu, *qmenu->d->top_level_menu);
2034 Menu * Menus::menu(QString const & name, GuiView & view)
2036 LYXERR(Debug::GUI, "Context menu requested: " << name);
2037 Menu * menu = d->name_map_[&view].value(name, 0);
2038 if (!menu && !name.startsWith("context-")) {
2039 LYXERR0("requested context menu not found: " << name);
2043 menu = new Menu(&view, name, true);
2044 d->name_map_[&view][name] = menu;
2048 } // namespace frontend
2051 #include "moc_Menus.cpp"