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"
25 #include "BranchList.h"
27 #include "BufferList.h"
28 #include "BufferParams.h"
29 #include "Converter.h"
30 #include "CutAndPaste.h"
32 #include "FloatList.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
38 #include "LyXAction.h"
39 #include "LyX.h" // for lastfiles
41 #include "Paragraph.h"
42 #include "qt_helpers.h"
44 #include "TextClass.h"
45 #include "TocBackend.h"
46 #include "ToolbarBackend.h"
48 #include "support/convert.h"
49 #include "support/debug.h"
50 #include "support/filetools.h"
51 #include "support/gettext.h"
52 #include "support/lstrings.h"
59 #include <boost/shared_ptr.hpp>
66 using namespace lyx::support;
74 // MacOSX specific stuff is at the end.
81 /// The type of elements that can be in a menu
89 /** This is the list of last opened file,
90 typically for the File menu. */
92 /** This is the list of opened Documents,
93 typically for the Documents menu. */
95 /** This is the bookmarks */
99 /** This is a list of viewable formats
100 typically for the File->View menu. */
102 /** This is a list of updatable formats
103 typically for the File->Update menu. */
105 /** This is a list of exportable formats
106 typically for the File->Export menu. */
108 /** This is a list of importable formats
109 typically for the File->Export menu. */
111 /** This is the list of elements available
112 * for insertion into document. */
114 /** This is the list of user-configurable
115 insets to insert into document */
117 /** This is the list of XML elements to
118 insert into the document */
120 /** This is the list of floats that we can
121 insert a list for. */
123 /** This is the list of floats that we can
126 /** This is the list of selections that can
131 /** Available branches in document */
135 explicit MenuItem(Kind kind);
138 QString const & label,
139 QString const & submenu = QString(),
140 bool optional = false);
143 QString const & label,
144 FuncRequest const & func,
145 bool optional = false);
147 /// This one is just to please boost::shared_ptr<>
149 /// The label of a given menuitem
150 QString label() const;
151 /// The keyboard shortcut (usually underlined in the entry)
152 QString shortcut() const;
153 /// The complete label, with label and shortcut separated by a '|'
154 QString fulllabel() const { return label_;}
155 /// The kind of entry
156 Kind kind() const { return kind_; }
157 /// the action (if relevant)
158 FuncRequest const & func() const { return func_; }
159 /// returns true if the entry should be ommited when disabled
160 bool optional() const { return optional_; }
161 /// returns the status of the lfun associated with this entry
162 FuncStatus const & status() const { return status_; }
163 /// returns the status of the lfun associated with this entry
164 FuncStatus & status() { return status_; }
165 /// returns the status of the lfun associated with this entry
166 void status(FuncStatus const & status) { status_ = status; }
167 ///returns the binding associated to this action.
168 QString binding() const;
169 /// the description of the submenu (if relevant)
170 QString const & submenuname() const { return submenuname_; }
171 /// set the description of the submenu
172 void submenuname(QString const & name) { submenuname_ = name; }
174 Menu * submenu() const { return submenu_.get(); }
176 void setSubmenu(Menu * menu);
186 QString submenuname_;
192 boost::shared_ptr<Menu> submenu_;
199 typedef std::vector<MenuItem> ItemList;
201 typedef ItemList::const_iterator const_iterator;
203 explicit Menu(QString const & name = QString()) : name_(name) {}
208 QString const & name() const { return name_; }
210 bool empty() const { return items_.empty(); }
211 /// Clear the menu content.
212 void clear() { items_.clear(); }
214 size_t size() const { return items_.size(); }
216 MenuItem const & operator[](size_t) const;
218 const_iterator begin() const { return items_.begin(); }
220 const_iterator end() const { return items_.end(); }
222 // search for func in this menu iteratively, and put menu
224 bool searchMenu(FuncRequest const & func, std::vector<docstring> & names)
227 bool hasFunc(FuncRequest const &) const;
228 /// Add the menu item unconditionally
229 void add(MenuItem const & item) { items_.push_back(item); }
230 /// Checks the associated FuncRequest status before adding the
232 void addWithStatusCheck(MenuItem const &);
233 // Check whether the menu shortcuts are unique
234 void checkShortcuts() const;
236 void expandLastfiles();
237 void expandDocuments();
238 void expandBookmarks();
239 void expandFormats(MenuItem::Kind kind, Buffer const * buf);
240 void expandFloatListInsert(Buffer const * buf);
241 void expandFloatInsert(Buffer const * buf);
242 void expandFlexInsert(Buffer const * buf, std::string s);
243 void expandToc2(Toc const & toc_list, size_t from, size_t to, int depth);
244 void expandToc(Buffer const * buf);
245 void expandPasteRecent();
246 void expandToolbars();
247 void expandBranches(Buffer const * buf);
255 class GuiPopupMenu : public QMenu
259 GuiPopupMenu(GuiView * gv, MenuItem const & mi, bool top_level)
260 : QMenu(gv), top_level_menu(top_level? new Menu : 0), view(gv),
261 name(mi.submenuname())
266 /// populates the menu or one of its submenu
267 /// This is used as a recursive function
268 void populate(QMenu * qMenu, Menu * menu);
270 /// Get a Menu item label from the menu backend
271 QString label(MenuItem const & mi) const;
273 void showEvent(QShowEvent * ev)
276 guiApp->menus().updateMenu(name);
277 QMenu::showEvent(ev);
280 /// Only needed for top level menus.
281 Menu * top_level_menu;
284 /// the name of this menu
292 MenuNamesEqual(QString const & name) : name_(name) {}
293 bool operator()(Menu const & menu) const { return menu.name() == name_; }
299 typedef std::vector<Menu> MenuList;
301 typedef MenuList::const_iterator const_iterator;
303 typedef MenuList::iterator iterator;
306 void GuiPopupMenu::populate(QMenu * qMenu, Menu * menu)
308 LYXERR(Debug::GUI, "populating menu " << fromqstr(menu->name()));
309 if (menu->size() == 0) {
310 LYXERR(Debug::GUI, "\tERROR: empty menu " << fromqstr(menu->name()));
313 LYXERR(Debug::GUI, " ***** menu entries " << menu->size());
314 Menu::const_iterator m = menu->begin();
315 Menu::const_iterator end = menu->end();
316 for (; m != end; ++m) {
317 if (m->kind() == MenuItem::Separator)
318 qMenu->addSeparator();
319 else if (m->kind() == MenuItem::Submenu) {
320 QMenu * subMenu = qMenu->addMenu(label(*m));
321 populate(subMenu, m->submenu());
323 // we have a MenuItem::Command
324 qMenu->addAction(new Action(*view, QIcon(), label(*m), m->func(),
331 QString GuiPopupMenu::label(MenuItem const & mi) const
333 QString label = mi.label();
334 label.replace("&", "&&");
336 QString shortcut = mi.shortcut();
337 if (!shortcut.isEmpty()) {
338 int pos = label.indexOf(shortcut);
340 //label.insert(pos, 1, char_type('&'));
341 label.replace(pos, 0, "&");
344 QString const binding = mi.binding();
345 if (!binding.isEmpty())
346 label += '\t' + binding;
356 void add(Menu const &);
358 bool hasMenu(QString const &) const;
360 Menu & getMenu(QString const &);
362 Menu const & getMenu(QString const &) const;
364 /// Expands some special entries of the menu
365 /** The entries with the following kind are expanded to a
366 sequence of Command MenuItems: Lastfiles, Documents,
367 ViewFormats, ExportFormats, UpdateFormats, Branches
369 void expand(Menu const & frommenu, Menu & tomenu,
370 Buffer const *) const;
372 /// Initialize specific MACOS X menubar
373 void macxMenuBarInit(GuiView * view);
375 /// Mac special menu.
376 /** This defines a menu whose entries list the FuncRequests
377 that will be removed by expand() in other menus. This is
378 used by the Qt/Mac code
387 typedef QHash<QString, GuiPopupMenu *> NameMap;
389 /// name to menu for \c menu() method.
394 Menus::Menus(): d(new Impl) {}
397 bool Menus::searchMenu(FuncRequest const & func,
398 vector<docstring> & names) const
400 return d->menubar_.searchMenu(func, names);
404 void Menus::fillMenuBar(GuiView * view)
406 // Clear all menubar contents before filling it.
407 view->menuBar()->clear();
410 // setup special mac specific menu item
411 macxMenuBarInit(view);
414 LYXERR(Debug::GUI, "populating menu bar" << fromqstr(d->menubar_.name()));
416 if (d->menubar_.size() == 0) {
417 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
418 << fromqstr(d->menubar_.name()));
422 LYXERR(Debug::GUI, "menu bar entries "
423 << d->menubar_.size());
427 d->expand(d->menubar_, menu, view->buffer());
429 Menu::const_iterator m = menu.begin();
430 Menu::const_iterator end = menu.end();
432 for (; m != end; ++m) {
434 if (m->kind() != MenuItem::Submenu) {
435 LYXERR(Debug::GUI, "\tERROR: not a submenu " << fromqstr(m->label()));
439 LYXERR(Debug::GUI, "menu bar item " << fromqstr(m->label())
440 << " is a submenu named " << fromqstr(m->submenuname()));
442 QString name = m->submenuname();
443 if (!d->hasMenu(name)) {
444 LYXERR(Debug::GUI, "\tERROR: " << fromqstr(name)
445 << " submenu has no menu!");
449 GuiPopupMenu * qmenu = new GuiPopupMenu(view, *m, true);
450 view->menuBar()->addMenu(qmenu);
452 d->name_map_[name] = qmenu;
457 void Menus::updateMenu(QString const & name)
459 GuiPopupMenu * qmenu = d->name_map_[name];
460 LYXERR(Debug::GUI, "GuiPopupMenu::updateView()"
461 << "\tTriggered menu: " << fromqstr(qmenu->name));
464 if (qmenu->name.isEmpty())
467 // Here, We make sure that theLyXFunc points to the correct LyXView.
468 theLyXFunc().setLyXView(qmenu->view);
470 Menu const & fromLyxMenu = d->getMenu(qmenu->name);
471 d->expand(fromLyxMenu, *qmenu->top_level_menu, qmenu->view->buffer());
473 if (!d->hasMenu(qmenu->top_level_menu->name())) {
474 LYXERR(Debug::GUI, "\tWARNING: menu seems empty"
475 << fromqstr(qmenu->top_level_menu->name()));
477 qmenu->populate(qmenu, qmenu->top_level_menu);
481 QMenu * Menus::menu(QString const & name)
483 LYXERR(Debug::GUI, "Context menu requested: " << fromqstr(name));
484 GuiPopupMenu * menu = d->name_map_.value(name, 0);
486 LYXERR0("resquested context menu not found: " << fromqstr(name));
491 /// Some special Qt/Mac support hacks
494 Here is what the Qt documentation says about how a menubar is chosen:
496 1) If the window has a QMenuBar then it is used. 2) If the window
497 is a modal then its menubar is used. If no menubar is specified
498 then a default menubar is used (as documented below) 3) If the
499 window has no parent then the default menubar is used (as
502 The above 3 steps are applied all the way up the parent window
503 chain until one of the above are satisifed. If all else fails a
504 default menubar will be created, the default menubar on Qt/Mac is
505 an empty menubar, however you can create a different default
506 menubar by creating a parentless QMenuBar, the first one created
507 will thus be designated the default menubar, and will be used
508 whenever a default menubar is needed.
510 Thus, for Qt/Mac, we add the menus to a free standing menubar, so
511 that this menubar will be used also when one of LyX' dialogs has
515 void Menus::Impl::macxMenuBarInit(GuiView * view)
517 // The Mac menubar initialisation must be done only once!
518 static bool done = false;
523 /* Since Qt 4.2, the qt/mac menu code has special code for
524 specifying the role of a menu entry. However, it does not
525 work very well with our scheme of creating menus on demand,
526 and therefore we need to put these entries in a special
527 invisible menu. (JMarc)
530 /* The entries of our special mac menu. If we add support for
531 * special entries in Menus, we could imagine something
533 * SpecialItem About " "About LyX" "dialog-show aboutlyx"
534 * and therefore avoid hardcoding. I am not sure it is worth
535 * the hassle, though. (JMarc)
537 struct MacMenuEntry {
541 QAction::MenuRole role;
544 MacMenuEntry entries[] = {
545 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
547 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
548 QAction::PreferencesRole},
549 {LFUN_RECONFIGURE, "", "Reconfigure",
550 QAction::ApplicationSpecificRole},
551 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
553 const size_t num_entries = sizeof(entries) / sizeof(entries[0]);
555 // the special menu for Menus.
556 for (size_t i = 0 ; i < num_entries ; ++i) {
557 FuncRequest const func(entries[i].action,
558 from_utf8(entries[i].arg));
559 specialmenu_.add(MenuItem(MenuItem::Command, entries[i].label, func));
562 // add the entries to a QMenu that will eventually be empty
563 // and therefore invisible.
564 QMenu * qMenu = view->menuBar()->addMenu("special");
565 Menu::const_iterator cit = specialmenu_.begin();
566 Menu::const_iterator end = specialmenu_.end();
567 for (size_t i = 0 ; cit != end ; ++cit, ++i) {
568 Action * action = new Action(*view, QIcon(), cit->label(),
569 cit->func(), QString());
570 action->setMenuRole(entries[i].role);
571 qMenu->addAction(action);
576 MenuItem::MenuItem(Kind kind)
577 : kind_(kind), optional_(false)
581 MenuItem::MenuItem(Kind kind, QString const & label,
582 QString const & submenu, bool optional)
583 : kind_(kind), label_(label),
584 submenuname_(submenu), optional_(optional)
586 BOOST_ASSERT(kind == Submenu);
590 MenuItem::MenuItem(Kind kind, QString const & label,
591 FuncRequest const & func, bool optional)
592 : kind_(kind), label_(label), func_(func), optional_(optional)
594 func_.origin = FuncRequest::MENU;
598 MenuItem::~MenuItem()
602 void MenuItem::setSubmenu(Menu * menu)
604 submenu_.reset(menu);
608 QString MenuItem::label() const
610 return label_.split('|')[0];
614 QString MenuItem::shortcut() const
616 return label_.contains('|') ? label_.split('|')[1] : QString();
620 QString MenuItem::binding() const
622 if (kind_ != Command)
625 // Get the keys bound to this action, but keep only the
627 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func_);
630 return toqstr(bindings.begin()->print(KeySequence::ForGui));
632 LYXERR(Debug::KBMAP, "No binding for "
633 << lyxaction.getActionName(func_.action)
634 << '(' << func_.argument() << ')');
639 void Menu::addWithStatusCheck(MenuItem const & i)
643 case MenuItem::Command: {
644 FuncStatus status = lyx::getStatus(i.func());
645 if (status.unknown() || (!status.enabled() && i.optional()))
648 items_.back().status(status);
652 case MenuItem::Submenu: {
654 bool enabled = false;
655 for (const_iterator cit = i.submenu()->begin();
656 cit != i.submenu()->end(); ++cit) {
657 if ((cit->kind() == MenuItem::Command
658 || cit->kind() == MenuItem::Submenu)
659 && cit->status().enabled()) {
664 if (enabled || !i.optional()) {
666 items_.back().status().enabled(enabled);
674 case MenuItem::Separator:
675 if (!items_.empty() && items_.back().kind() != MenuItem::Separator)
685 void Menu::read(Lexer & lex)
713 struct keyword_item menutags[md_last - 1] = {
714 { "bookmarks", md_bookmarks },
715 { "branches", md_branches },
716 { "charstyles", md_charstyles },
717 { "custom", md_custom },
718 { "documents", md_documents },
719 { "elements", md_elements },
720 { "end", md_endmenu },
721 { "exportformats", md_exportformats },
722 { "floatinsert", md_floatinsert },
723 { "floatlistinsert", md_floatlistinsert },
724 { "importformats", md_importformats },
726 { "lastfiles", md_lastfiles },
727 { "optitem", md_optitem },
728 { "optsubmenu", md_optsubmenu },
729 { "pasterecent", md_pasterecent },
730 { "separator", md_separator },
731 { "submenu", md_submenu },
733 { "toolbars", md_toolbars },
734 { "updateformats", md_updateformats },
735 { "viewformats", md_viewformats }
738 lex.pushTable(menutags, md_last - 1);
739 if (lyxerr.debugging(Debug::PARSER))
740 lex.printTable(lyxerr);
743 bool optional = false;
745 while (lex.isOK() && !quit) {
749 // fallback to md_item
752 docstring const name = translateIfPossible(lex.getDocString());
754 string const command = lex.getString();
755 FuncRequest func = lyxaction.lookupFunc(command);
756 add(MenuItem(MenuItem::Command, toqstr(name), func, optional));
762 add(MenuItem(MenuItem::Separator));
766 add(MenuItem(MenuItem::Lastfiles));
770 add(MenuItem(MenuItem::CharStyles));
774 add(MenuItem(MenuItem::Custom));
778 add(MenuItem(MenuItem::Elements));
782 add(MenuItem(MenuItem::Documents));
786 add(MenuItem(MenuItem::Bookmarks));
790 add(MenuItem(MenuItem::Toc));
794 add(MenuItem(MenuItem::ViewFormats));
797 case md_updateformats:
798 add(MenuItem(MenuItem::UpdateFormats));
801 case md_exportformats:
802 add(MenuItem(MenuItem::ExportFormats));
805 case md_importformats:
806 add(MenuItem(MenuItem::ImportFormats));
809 case md_floatlistinsert:
810 add(MenuItem(MenuItem::FloatListInsert));
814 add(MenuItem(MenuItem::FloatInsert));
818 add(MenuItem(MenuItem::PasteRecent));
822 add(MenuItem(MenuItem::Toolbars));
826 add(MenuItem(MenuItem::Branches));
831 // fallback to md_submenu
834 docstring const mlabel = translateIfPossible(lex.getDocString());
836 docstring const mname = lex.getDocString();
837 add(MenuItem(MenuItem::Submenu,
838 toqstr(mlabel), toqstr(mname), optional));
848 lex.printError("Menu::read: "
849 "Unknown menu tag: `$$Token'");
857 MenuItem const & Menu::operator[](size_type i) const
863 bool Menu::hasFunc(FuncRequest const & func) const
865 for (const_iterator it = begin(), et = end(); it != et; ++it)
866 if (it->func() == func)
872 void Menu::checkShortcuts() const
874 // This is a quadratic algorithm, but we do not care because
875 // menus are short enough
876 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
877 QString shortcut = it1->shortcut();
878 if (shortcut.isEmpty())
880 if (!it1->label().contains(shortcut))
881 lyxerr << "Menu warning: menu entry \""
882 << fromqstr(it1->label())
883 << "\" does not contain shortcut `"
884 << fromqstr(shortcut) << "'." << endl;
885 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
886 if (!it2->shortcut().compare(shortcut, Qt::CaseInsensitive)) {
887 lyxerr << "Menu warning: menu entries "
888 << '"' << fromqstr(it1->fulllabel())
889 << "\" and \"" << fromqstr(it2->fulllabel())
890 << "\" share the same shortcut."
898 bool Menu::searchMenu(FuncRequest const & func, vector<docstring> & names) const
900 const_iterator m = begin();
901 const_iterator m_end = end();
902 for (; m != m_end; ++m) {
903 if (m->kind() == MenuItem::Command && m->func() == func) {
904 names.push_back(qstring_to_ucs4(m->label()));
907 if (m->kind() == MenuItem::Submenu) {
908 names.push_back(qstring_to_ucs4(m->label()));
909 Menu const & submenu = *m->submenu();
910 if (submenu.searchMenu(func, names))
921 bool compareFormat(Format const * p1, Format const * p2)
927 QString limitStringLength(docstring const & str)
929 size_t const max_item_length = 45;
931 if (str.size() > max_item_length)
932 return toqstr(str.substr(0, max_item_length - 3) + "...");
940 void Menu::expandLastfiles()
942 LastFilesSection::LastFiles const & lf = LyX::cref().session().lastFiles().lastFiles();
943 LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
947 for (; lfit != lf.end() && ii < 10; ++lfit, ++ii) {
948 string const file = lfit->absFilename();
949 QString const label = QString("%1. %2|%3").arg(ii)
950 .arg(toqstr(makeDisplayPath(file, 30))).arg(ii);
951 add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, file)));
956 void Menu::expandDocuments()
958 Buffer * first = theBufferList().first();
963 // We cannot use a for loop as the buffer list cycles.
965 QString label = toqstr(b->fileName().displayName(20));
969 label = QString::number(ii) + ". " + label + '|' + QString::number(ii);
970 add(MenuItem(MenuItem::Command, label,
971 FuncRequest(LFUN_BUFFER_SWITCH, b->absFileName())));
973 b = theBufferList().next(b);
975 } while (b != first);
977 add(MenuItem(MenuItem::Command, qt_("No Documents Open!"),
978 FuncRequest(LFUN_NOACTION)));
983 void Menu::expandBookmarks()
985 lyx::BookmarksSection const & bm = LyX::cref().session().bookmarks();
987 for (size_t i = 1; i <= bm.size(); ++i) {
989 string const file = bm.bookmark(i).filename.absFilename();
990 QString const label = QString("%1. %2|%3").arg(i)
991 .arg(toqstr(makeDisplayPath(file, 20))).arg(i);
992 add(MenuItem(MenuItem::Command, label,
993 FuncRequest(LFUN_BOOKMARK_GOTO, convert<docstring>(i))));
999 void Menu::expandFormats(MenuItem::Kind kind, Buffer const * buf)
1001 if (!buf && kind != MenuItem::ImportFormats) {
1002 add(MenuItem(MenuItem::Command,
1003 qt_("No Document Open!"),
1004 FuncRequest(LFUN_NOACTION)));
1008 typedef vector<Format const *> Formats;
1013 case MenuItem::ImportFormats:
1014 formats = theConverters().importableFormats();
1015 action = LFUN_BUFFER_IMPORT;
1017 case MenuItem::ViewFormats:
1018 formats = buf->exportableFormats(true);
1019 action = LFUN_BUFFER_VIEW;
1021 case MenuItem::UpdateFormats:
1022 formats = buf->exportableFormats(true);
1023 action = LFUN_BUFFER_UPDATE;
1026 formats = buf->exportableFormats(false);
1027 action = LFUN_BUFFER_EXPORT;
1029 sort(formats.begin(), formats.end(), &compareFormat);
1031 Formats::const_iterator fit = formats.begin();
1032 Formats::const_iterator end = formats.end();
1033 for (; fit != end ; ++fit) {
1034 if ((*fit)->dummy())
1036 QString label = toqstr((*fit)->prettyname());
1037 QString const shortcut = toqstr((*fit)->shortcut());
1040 case MenuItem::ImportFormats:
1041 // FIXME: This is a hack, we should rather solve
1042 // FIXME: bug 2488 instead.
1043 if ((*fit)->name() == "text")
1044 label = qt_("Plain Text");
1045 else if ((*fit)->name() == "textparagraph")
1046 label = qt_("Plain Text, Join Lines");
1049 case MenuItem::ViewFormats:
1050 case MenuItem::ExportFormats:
1051 case MenuItem::UpdateFormats:
1052 if (!(*fit)->documentFormat())
1056 BOOST_ASSERT(false);
1059 // FIXME: if we had proper support for translating the
1060 // format names defined in configure.py, there would
1061 // not be a need to check whether the shortcut is
1062 // correct. If we add it uncondiitonally, it would
1063 // create useless warnings on bad shortcuts
1064 if (!shortcut.isEmpty() && label.contains(shortcut))
1065 label += '|' + shortcut;
1068 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1069 FuncRequest(action, (*fit)->name())));
1071 add(MenuItem(MenuItem::Command, label,
1072 FuncRequest(action, (*fit)->name())));
1077 void Menu::expandFloatListInsert(Buffer const * buf)
1080 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
1081 FuncRequest(LFUN_NOACTION)));
1085 FloatList const & floats = buf->params().documentClass().floats();
1086 FloatList::const_iterator cit = floats.begin();
1087 FloatList::const_iterator end = floats.end();
1088 for (; cit != end; ++cit) {
1089 addWithStatusCheck(MenuItem(MenuItem::Command,
1090 qt_(cit->second.listName()),
1091 FuncRequest(LFUN_FLOAT_LIST,
1092 cit->second.type())));
1097 void Menu::expandFloatInsert(Buffer const * buf)
1100 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
1101 FuncRequest(LFUN_NOACTION)));
1105 FloatList const & floats = buf->params().documentClass().floats();
1106 FloatList::const_iterator cit = floats.begin();
1107 FloatList::const_iterator end = floats.end();
1108 for (; cit != end; ++cit) {
1110 QString const label = qt_(cit->second.name());
1111 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1112 FuncRequest(LFUN_FLOAT_INSERT,
1113 cit->second.type())));
1118 void Menu::expandFlexInsert(Buffer const * buf, string s)
1121 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
1122 FuncRequest(LFUN_NOACTION)));
1125 TextClass::InsetLayouts const & insetLayouts =
1126 buf->params().documentClass().insetLayouts();
1127 TextClass::InsetLayouts::const_iterator cit = insetLayouts.begin();
1128 TextClass::InsetLayouts::const_iterator end = insetLayouts.end();
1129 for (; cit != end; ++cit) {
1130 docstring const label = cit->first;
1131 if (cit->second.lyxtype() == s)
1132 addWithStatusCheck(MenuItem(MenuItem::Command,
1133 toqstr(label), FuncRequest(LFUN_FLEX_INSERT,
1139 size_t const max_number_of_items = 25;
1141 void Menu::expandToc2(Toc const & toc_list,
1142 size_t from, size_t to, int depth)
1144 int shortcut_count = 0;
1146 // check whether depth is smaller than the smallest depth in toc.
1147 int min_depth = 1000;
1148 for (size_t i = from; i < to; ++i)
1149 min_depth = min(min_depth, toc_list[i].depth());
1150 if (min_depth > depth)
1153 if (to - from <= max_number_of_items) {
1154 for (size_t i = from; i < to; ++i) {
1155 QString label(4 * max(0, toc_list[i].depth() - depth), ' ');
1156 label += limitStringLength(toc_list[i].str());
1157 if (toc_list[i].depth() == depth
1158 && shortcut_count < 9) {
1159 if (label.contains(QString::number(shortcut_count + 1)))
1160 label += '|' + QString::number(++shortcut_count);
1162 add(MenuItem(MenuItem::Command, label,
1163 FuncRequest(toc_list[i].action())));
1168 size_t new_pos = pos + 1;
1169 while (new_pos < to &&
1170 toc_list[new_pos].depth() > depth)
1173 QString label(4 * max(0, toc_list[pos].depth() - depth), ' ');
1174 label += limitStringLength(toc_list[pos].str());
1175 if (toc_list[pos].depth() == depth &&
1176 shortcut_count < 9) {
1177 if (label.contains(QString::number(shortcut_count + 1)))
1178 label += '|' + QString::number(++shortcut_count);
1180 if (new_pos == pos + 1) {
1181 add(MenuItem(MenuItem::Command,
1182 label, FuncRequest(toc_list[pos].action())));
1184 MenuItem item(MenuItem::Submenu, label);
1185 item.setSubmenu(new Menu);
1186 item.submenu()->expandToc2(toc_list, pos, new_pos, depth + 1);
1195 void Menu::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::Command, qt_("No Document Open!"),
1204 FuncRequest(LFUN_NOACTION)));
1208 Buffer* cbuf = const_cast<Buffer*>(buf);
1209 cbuf->tocBackend().update();
1210 cbuf->structureChanged();
1212 // Add an entry for the master doc if this is a child doc
1213 Buffer const * const master = buf->masterBuffer();
1214 if (buf != master) {
1215 ParIterator const pit = par_iterator_begin(master->inset());
1216 string const arg = convert<string>(pit->id());
1217 FuncRequest f(LFUN_PARAGRAPH_GOTO, arg);
1218 add(MenuItem(MenuItem::Command, qt_("Master Document"), f));
1221 FloatList const & floatlist = buf->params().documentClass().floats();
1222 TocList const & toc_list = buf->tocBackend().tocs();
1223 TocList::const_iterator cit = toc_list.begin();
1224 TocList::const_iterator end = toc_list.end();
1225 for (; cit != end; ++cit) {
1226 // Handle this later
1227 if (cit->first == "tableofcontents")
1230 // All the rest is for floats
1231 auto_ptr<Menu> menu(new Menu);
1232 TocIterator ccit = cit->second.begin();
1233 TocIterator eend = cit->second.end();
1234 for (; ccit != eend; ++ccit) {
1235 QString const label = limitStringLength(ccit->str());
1236 menu->add(MenuItem(MenuItem::Command, label,
1237 FuncRequest(ccit->action())));
1239 string const & floatName = floatlist.getType(cit->first).listName();
1241 if (!floatName.empty())
1242 label = qt_(floatName);
1243 // BUG3633: listings is not a proper float so its name
1244 // is not shown in floatlist.
1245 else if (cit->first == "equation")
1246 label = qt_("List of Equations");
1247 else if (cit->first == "index")
1248 label = qt_("List of Indexes");
1249 else if (cit->first == "listing")
1250 label = qt_("List of Listings");
1251 else if (cit->first == "marginalnote")
1252 label = qt_("List of Marginal notes");
1253 else if (cit->first == "note")
1254 label = qt_("List of Notes");
1255 else if (cit->first == "footnote")
1256 label = qt_("List of Foot notes");
1257 else if (cit->first == "label")
1258 label = qt_("Labels and References");
1259 else if (cit->first == "citation")
1260 label = qt_("List of Citations");
1261 // this should not happen now, but if something else like
1262 // listings is added later, this can avoid an empty menu name.
1264 label = qt_("Other floats");
1265 MenuItem item(MenuItem::Submenu, label);
1266 item.setSubmenu(menu.release());
1270 // Handle normal TOC
1271 cit = toc_list.find("tableofcontents");
1273 addWithStatusCheck(MenuItem(MenuItem::Command,
1274 qt_("No Table of contents"),
1277 expandToc2(cit->second, 0, cit->second.size(), 0);
1282 void Menu::expandPasteRecent()
1284 vector<docstring> const sel = cap::availableSelections();
1286 vector<docstring>::const_iterator cit = sel.begin();
1287 vector<docstring>::const_iterator end = sel.end();
1289 for (unsigned int index = 0; cit != end; ++cit, ++index) {
1290 add(MenuItem(MenuItem::Command, toqstr(*cit),
1291 FuncRequest(LFUN_PASTE, convert<string>(index))));
1296 void Menu::expandToolbars()
1299 // extracts the toolbars from the backend
1300 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
1301 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
1303 for (; cit != end; ++cit) {
1304 QString label = qt_(cit->gui_name);
1305 // frontends are not supposed to turn on/off toolbars,
1306 // if they cannot update ToolbarBackend::flags. That
1307 // is to say, ToolbarsBackend::flags should reflect
1308 // the true state of toolbars.
1310 // menu is displayed as
1314 // in the case of auto.
1315 if (cit->flags & ToolbarInfo::AUTO)
1316 label += qt_(" (auto)");
1317 add(MenuItem(MenuItem::Command, label,
1318 FuncRequest(LFUN_TOOLBAR_TOGGLE, cit->name + " allowauto")));
1323 void Menu::expandBranches(Buffer const * buf)
1326 add(MenuItem(MenuItem::Command,
1327 qt_("No Document Open!"),
1328 FuncRequest(LFUN_NOACTION)));
1332 BufferParams const & params = buf->masterBuffer()->params();
1333 if (params.branchlist().empty()) {
1334 add(MenuItem(MenuItem::Command,
1335 qt_("No Branch in Document!"),
1336 FuncRequest(LFUN_NOACTION)));
1340 BranchList::const_iterator cit = params.branchlist().begin();
1341 BranchList::const_iterator end = params.branchlist().end();
1343 for (int ii = 1; cit != end; ++cit, ++ii) {
1344 docstring label = cit->getBranch();
1346 label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
1347 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1348 FuncRequest(LFUN_BRANCH_INSERT,
1349 cit->getBranch())));
1354 void Menus::Impl::expand(Menu const & frommenu, Menu & tomenu,
1355 Buffer const * buf) const
1357 if (!tomenu.empty())
1360 for (Menu::const_iterator cit = frommenu.begin();
1361 cit != frommenu.end() ; ++cit) {
1362 switch (cit->kind()) {
1363 case MenuItem::Lastfiles:
1364 tomenu.expandLastfiles();
1367 case MenuItem::Documents:
1368 tomenu.expandDocuments();
1371 case MenuItem::Bookmarks:
1372 tomenu.expandBookmarks();
1375 case MenuItem::ImportFormats:
1376 case MenuItem::ViewFormats:
1377 case MenuItem::UpdateFormats:
1378 case MenuItem::ExportFormats:
1379 tomenu.expandFormats(cit->kind(), buf);
1382 case MenuItem::CharStyles:
1383 tomenu.expandFlexInsert(buf, "charstyle");
1386 case MenuItem::Custom:
1387 tomenu.expandFlexInsert(buf, "custom");
1390 case MenuItem::Elements:
1391 tomenu.expandFlexInsert(buf, "element");
1394 case MenuItem::FloatListInsert:
1395 tomenu.expandFloatListInsert(buf);
1398 case MenuItem::FloatInsert:
1399 tomenu.expandFloatInsert(buf);
1402 case MenuItem::PasteRecent:
1403 tomenu.expandPasteRecent();
1406 case MenuItem::Toolbars:
1407 tomenu.expandToolbars();
1410 case MenuItem::Branches:
1411 tomenu.expandBranches(buf);
1415 tomenu.expandToc(buf);
1418 case MenuItem::Submenu: {
1419 MenuItem item(*cit);
1420 item.setSubmenu(new Menu(cit->submenuname()));
1421 expand(getMenu(cit->submenuname()),
1422 *item.submenu(), buf);
1423 tomenu.addWithStatusCheck(item);
1427 case MenuItem::Separator:
1428 tomenu.addWithStatusCheck(*cit);
1431 case MenuItem::Command:
1432 if (!specialmenu_.hasFunc(cit->func()))
1433 tomenu.addWithStatusCheck(*cit);
1437 // we do not want the menu to end with a separator
1438 if (!tomenu.empty() && tomenu.items_.back().kind() == MenuItem::Separator)
1439 tomenu.items_.pop_back();
1441 // Check whether the shortcuts are unique
1442 tomenu.checkShortcuts();
1446 void Menus::read(Lexer & lex)
1455 struct keyword_item menutags[md_last - 1] = {
1456 { "end", md_endmenuset },
1457 { "menu", md_menu },
1458 { "menubar", md_menubar }
1462 if (compare_ascii_no_case(lex.getString(), "menuset")) {
1463 lyxerr << "Menubackend::read: ERROR wrong token:`"
1464 << lex.getString() << '\'' << endl;
1467 lex.pushTable(menutags, md_last - 1);
1468 if (lyxerr.debugging(Debug::PARSER))
1469 lex.printTable(lyxerr);
1473 while (lex.isOK() && !quit) {
1474 switch (lex.lex()) {
1476 d->menubar_.read(lex);
1480 QString const name = toqstr(lex.getDocString());
1481 if (d->hasMenu(name))
1482 d->getMenu(name).read(lex);
1494 lex.printError("menubackend::read: "
1495 "Unknown menu tag: `$$Token'");
1503 void Menus::Impl::add(Menu const & menu)
1505 menulist_.push_back(menu);
1509 bool Menus::Impl::hasMenu(QString const & name) const
1511 return find_if(menulist_.begin(), menulist_.end(),
1512 MenuNamesEqual(name)) != menulist_.end();
1516 Menu const & Menus::Impl::getMenu(QString const & name) const
1518 const_iterator cit = find_if(menulist_.begin(), menulist_.end(),
1519 MenuNamesEqual(name));
1520 if (cit == menulist_.end())
1521 lyxerr << "No submenu named " << fromqstr(name) << endl;
1522 BOOST_ASSERT(cit != menulist_.end());
1527 Menu & Menus::Impl::getMenu(QString const & name)
1529 iterator it = find_if(menulist_.begin(), menulist_.end(),
1530 MenuNamesEqual(name));
1531 if (it == menulist_.end())
1532 lyxerr << "No submenu named " << fromqstr(name) << endl;
1533 BOOST_ASSERT(it != menulist_.end());
1538 } // namespace frontend
1541 #include "Menus_moc.cpp"