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;
204 explicit Menu(QString const & name = QString()) : name_(name) {}
209 QString const & name() const { return name_; }
211 bool empty() const { return items_.empty(); }
212 /// Clear the menu content.
213 void clear() { items_.clear(); }
215 size_t size() const { return items_.size(); }
217 MenuItem const & operator[](size_t) const;
219 const_iterator begin() const { return items_.begin(); }
221 const_iterator end() const { return items_.end(); }
223 // search for func in this menu iteratively, and put menu
225 bool searchMenu(FuncRequest const & func, std::vector<docstring> & names)
231 bool hasFunc(FuncRequest const &) const;
232 /// Add the menu item unconditionally
233 void add(MenuItem const & item) { items_.push_back(item); }
234 /// Checks the associated FuncRequest status before adding the
236 void addWithStatusCheck(MenuItem const &);
237 // Check whether the menu shortcuts are unique
238 void checkShortcuts() const;
240 void expandLastfiles();
241 void expandDocuments();
242 void expandBookmarks();
243 void expandFormats(MenuItem::Kind kind, Buffer const * buf);
244 void expandFloatListInsert(Buffer const * buf);
245 void expandFloatInsert(Buffer const * buf);
246 void expandFlexInsert(Buffer const * buf, std::string s);
247 void expandToc2(Toc const & toc_list, size_t from, size_t to, int depth);
248 void expandToc(Buffer const * buf);
249 void expandPasteRecent();
250 void expandToolbars();
251 void expandBranches(Buffer const * buf);
259 class GuiPopupMenu : public QMenu
263 GuiPopupMenu(GuiView * owner, MenuItem const & mi, bool top_level)
264 : QMenu(owner), owner_(owner), top_level_(top_level)
266 name_ = mi.submenuname();
270 /// populates the menu or one of its submenu
271 /// This is used as a recursive function
272 void populate(QMenu * qMenu, Menu * menu);
274 /// Get a Menu item label from the menu backend
275 QString label(MenuItem const & mi) const;
277 void showEvent(QShowEvent * ev)
280 guiApp->menus().updateMenu(name_);
281 QMenu::showEvent(ev);
284 bool const top_level_;
291 /// the name of this menu
299 MenuNamesEqual(QString const & name) : name_(name) {}
300 bool operator()(Menu const & menu) const { return menu.name() == name_; }
306 typedef std::vector<Menu> MenuList;
308 typedef MenuList::const_iterator const_iterator;
310 typedef MenuList::iterator iterator;
313 void GuiPopupMenu::populate(QMenu * qMenu, Menu * menu)
315 LYXERR(Debug::GUI, "populating menu " << fromqstr(menu->name()));
316 if (menu->size() == 0) {
317 LYXERR(Debug::GUI, "\tERROR: empty menu " << fromqstr(menu->name()));
320 LYXERR(Debug::GUI, " ***** menu entries " << menu->size());
322 Menu::const_iterator m = menu->begin();
323 Menu::const_iterator end = menu->end();
325 for (; m != end; ++m) {
327 if (m->kind() == MenuItem::Separator) {
329 qMenu->addSeparator();
330 LYXERR(Debug::GUI, "adding Menubar Separator");
332 } else if (m->kind() == MenuItem::Submenu) {
334 LYXERR(Debug::GUI, "** creating New Sub-Menu "
335 << fromqstr(label(*m)));
336 QMenu * subMenu = qMenu->addMenu(label(*m));
337 populate(subMenu, m->submenu());
339 } else { // we have a MenuItem::Command
341 LYXERR(Debug::GUI, "creating Menu Item "
342 << fromqstr(m->label()));
344 Action * action = new Action(*owner_,
345 QIcon(), label(*m), m->func(), QString());
346 qMenu->addAction(action);
352 QString GuiPopupMenu::label(MenuItem const & mi) const
354 QString label = mi.label();
355 label.replace("&", "&&");
357 QString shortcut = mi.shortcut();
358 if (!shortcut.isEmpty()) {
359 int pos = label.indexOf(shortcut);
361 //label.insert(pos, 1, char_type('&'));
362 label.replace(pos, 0, "&");
365 QString const binding = mi.binding();
366 if (!binding.isEmpty())
367 label += '\t' + binding;
376 void add(Menu const &);
378 bool hasMenu(QString const &) const;
380 Menu & getMenu(QString const &);
382 Menu const & getMenu(QString const &) const;
384 bool empty() const { return menulist_.empty(); }
385 /** This defines a menu whose entries list the FuncRequests
386 that will be removed by expand() in other menus. This is
387 used by the Qt/Mac code
389 void setSpecialMenu(Menu const & menu) { specialmenu_ = menu; }
391 Menu const & specialMenu() { return specialmenu_; }
393 /// Expands some special entries of the menu
394 /** The entries with the following kind are expanded to a
395 sequence of Command MenuItems: Lastfiles, Documents,
396 ViewFormats, ExportFormats, UpdateFormats, Branches
398 void expand(Menu const & frommenu, Menu & tomenu,
399 Buffer const *) const;
401 const_iterator begin() const { return menulist_.begin(); }
403 iterator begin() { return menulist_.begin(); }
405 const_iterator end() const { return menulist_.end(); }
407 iterator end() { return menulist_.end(); }
416 /// Initialize specific MACOS X menubar
417 void macxMenuBarInit(GuiView * view);
419 typedef QHash<QString, GuiPopupMenu *> NameMap;
421 /// name to menu for \c menu() method.
426 Menus::Menus(): d(new Impl) {}
429 bool Menus::searchMenu(FuncRequest const & func,
430 vector<docstring> & names) const
432 return d->menubar_.searchMenu(func, names);
436 void Menus::fillMenuBar(GuiView * view)
438 // Clear all menubar contents before filling it.
439 view->menuBar()->clear();
442 // setup special mac specific menu item
443 macxMenuBarInit(view);
446 LYXERR(Debug::GUI, "populating menu bar" << fromqstr(d->menubar_.name()));
448 if (d->menubar_.size() == 0) {
449 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
450 << fromqstr(d->menubar_.name()));
454 LYXERR(Debug::GUI, "menu bar entries "
455 << d->menubar_.size());
459 d->expand(d->menubar_, menu, view->buffer());
461 Menu::const_iterator m = menu.begin();
462 Menu::const_iterator end = menu.end();
464 for (; m != end; ++m) {
466 if (m->kind() != MenuItem::Submenu) {
467 LYXERR(Debug::GUI, "\tERROR: not a submenu " << fromqstr(m->label()));
471 LYXERR(Debug::GUI, "menu bar item " << fromqstr(m->label())
472 << " is a submenu named " << fromqstr(m->submenuname()));
474 QString name = m->submenuname();
475 if (!d->hasMenu(name)) {
476 LYXERR(Debug::GUI, "\tERROR: " << fromqstr(name)
477 << " submenu has no menu!");
481 GuiPopupMenu * qmenu = new GuiPopupMenu(view, *m, true);
482 view->menuBar()->addMenu(qmenu);
484 d->name_map_[name] = qmenu;
489 void Menus::updateMenu(QString const & name)
491 GuiPopupMenu * qmenu = d->name_map_[name];
492 LYXERR(Debug::GUI, "GuiPopupMenu::updateView()"
493 << "\tTriggered menu: " << fromqstr(qmenu->name_));
496 if (qmenu->name_.isEmpty())
499 // Here, We make sure that theLyXFunc points to the correct LyXView.
500 theLyXFunc().setLyXView(qmenu->owner_);
502 Menu const & fromLyxMenu = d->getMenu(qmenu->name_);
503 d->expand(fromLyxMenu, qmenu->topLevelMenu_, qmenu->owner_->buffer());
505 if (!d->hasMenu(qmenu->topLevelMenu_.name())) {
506 LYXERR(Debug::GUI, "\tWARNING: menu seems empty"
507 << fromqstr(qmenu->topLevelMenu_.name()));
509 qmenu->populate(qmenu, &qmenu->topLevelMenu_);
513 QMenu * Menus::menu(QString const & name)
515 LYXERR(Debug::GUI, "Context menu requested: " << fromqstr(name));
516 GuiPopupMenu * menu = d->name_map_.value(name, 0);
518 LYXERR0("resquested context menu not found: " << fromqstr(name));
523 /// Some special Qt/Mac support hacks
526 Here is what the Qt documentation says about how a menubar is chosen:
528 1) If the window has a QMenuBar then it is used. 2) If the window
529 is a modal then its menubar is used. If no menubar is specified
530 then a default menubar is used (as documented below) 3) If the
531 window has no parent then the default menubar is used (as
534 The above 3 steps are applied all the way up the parent window
535 chain until one of the above are satisifed. If all else fails a
536 default menubar will be created, the default menubar on Qt/Mac is
537 an empty menubar, however you can create a different default
538 menubar by creating a parentless QMenuBar, the first one created
539 will thus be designated the default menubar, and will be used
540 whenever a default menubar is needed.
542 Thus, for Qt/Mac, we add the menus to a free standing menubar, so
543 that this menubar will be used also when one of LyX' dialogs has
547 void Menus::Impl::macxMenuBarInit(GuiView * view)
549 // The Mac menubar initialisation must be done only once!
550 static bool done = false;
555 /* Since Qt 4.2, the qt/mac menu code has special code for
556 specifying the role of a menu entry. However, it does not
557 work very well with our scheme of creating menus on demand,
558 and therefore we need to put these entries in a special
559 invisible menu. (JMarc)
562 /* The entries of our special mac menu. If we add support for
563 * special entries in Menus, we could imagine something
565 * SpecialItem About " "About LyX" "dialog-show aboutlyx"
566 * and therefore avoid hardcoding. I am not sure it is worth
567 * the hassle, though. (JMarc)
569 struct MacMenuEntry {
573 QAction::MenuRole role;
576 MacMenuEntry entries[] = {
577 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
579 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
580 QAction::PreferencesRole},
581 {LFUN_RECONFIGURE, "", "Reconfigure",
582 QAction::ApplicationSpecificRole},
583 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
585 const size_t num_entries = sizeof(entries) / sizeof(entries[0]);
587 // the special menu for Menus.
589 for (size_t i = 0 ; i < num_entries ; ++i) {
590 FuncRequest const func(entries[i].action,
591 from_utf8(entries[i].arg));
592 special.add(MenuItem(MenuItem::Command, entries[i].label, func));
594 setSpecialMenu(special);
596 // add the entries to a QMenu that will eventually be empty
597 // and therefore invisible.
598 QMenu * qMenu = view->menuBar()->addMenu("special");
600 // we do not use 'special' because it is a temporary variable,
601 // whereas Menus::specialMenu points to a persistent copy.
602 Menu::const_iterator cit = specialMenu().begin();
603 Menu::const_iterator end = specialMenu().end();
604 for (size_t i = 0 ; cit != end ; ++cit, ++i) {
605 Action * action = new Action(*view, QIcon(), cit->label(),
606 cit->func(), QString());
607 action->setMenuRole(entries[i].role);
608 qMenu->addAction(action);
613 MenuItem::MenuItem(Kind kind)
614 : kind_(kind), optional_(false)
618 MenuItem::MenuItem(Kind kind, QString const & label,
619 QString const & submenu, bool optional)
620 : kind_(kind), label_(label),
621 submenuname_(submenu), optional_(optional)
623 BOOST_ASSERT(kind == Submenu);
627 MenuItem::MenuItem(Kind kind, QString const & label,
628 FuncRequest const & func, bool optional)
629 : kind_(kind), label_(label), func_(func), optional_(optional)
631 func_.origin = FuncRequest::MENU;
635 MenuItem::~MenuItem()
639 void MenuItem::setSubmenu(Menu * menu)
641 submenu_.reset(menu);
645 QString MenuItem::label() const
647 return label_.split('|')[0];
651 QString MenuItem::shortcut() const
653 return label_.contains('|') ? label_.split('|')[1] : QString();
657 QString MenuItem::binding() const
659 if (kind_ != Command)
662 // Get the keys bound to this action, but keep only the
664 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func_);
667 return toqstr(bindings.begin()->print(KeySequence::ForGui));
669 LYXERR(Debug::KBMAP, "No binding for "
670 << lyxaction.getActionName(func_.action)
671 << '(' << func_.argument() << ')');
676 void Menu::addWithStatusCheck(MenuItem const & i)
680 case MenuItem::Command: {
681 FuncStatus status = lyx::getStatus(i.func());
682 if (status.unknown() || (!status.enabled() && i.optional()))
685 items_.back().status(status);
689 case MenuItem::Submenu: {
691 bool enabled = false;
692 for (const_iterator cit = i.submenu()->begin();
693 cit != i.submenu()->end(); ++cit) {
694 if ((cit->kind() == MenuItem::Command
695 || cit->kind() == MenuItem::Submenu)
696 && cit->status().enabled()) {
701 if (enabled || !i.optional()) {
703 items_.back().status().enabled(enabled);
711 case MenuItem::Separator:
712 if (!items_.empty() && items_.back().kind() != MenuItem::Separator)
722 void Menu::read(Lexer & lex)
750 struct keyword_item menutags[md_last - 1] = {
751 { "bookmarks", md_bookmarks },
752 { "branches", md_branches },
753 { "charstyles", md_charstyles },
754 { "custom", md_custom },
755 { "documents", md_documents },
756 { "elements", md_elements },
757 { "end", md_endmenu },
758 { "exportformats", md_exportformats },
759 { "floatinsert", md_floatinsert },
760 { "floatlistinsert", md_floatlistinsert },
761 { "importformats", md_importformats },
763 { "lastfiles", md_lastfiles },
764 { "optitem", md_optitem },
765 { "optsubmenu", md_optsubmenu },
766 { "pasterecent", md_pasterecent },
767 { "separator", md_separator },
768 { "submenu", md_submenu },
770 { "toolbars", md_toolbars },
771 { "updateformats", md_updateformats },
772 { "viewformats", md_viewformats }
775 lex.pushTable(menutags, md_last - 1);
776 if (lyxerr.debugging(Debug::PARSER))
777 lex.printTable(lyxerr);
780 bool optional = false;
782 while (lex.isOK() && !quit) {
786 // fallback to md_item
789 docstring const name = translateIfPossible(lex.getDocString());
791 string const command = lex.getString();
792 FuncRequest func = lyxaction.lookupFunc(command);
793 add(MenuItem(MenuItem::Command, toqstr(name), func, optional));
799 add(MenuItem(MenuItem::Separator));
803 add(MenuItem(MenuItem::Lastfiles));
807 add(MenuItem(MenuItem::CharStyles));
811 add(MenuItem(MenuItem::Custom));
815 add(MenuItem(MenuItem::Elements));
819 add(MenuItem(MenuItem::Documents));
823 add(MenuItem(MenuItem::Bookmarks));
827 add(MenuItem(MenuItem::Toc));
831 add(MenuItem(MenuItem::ViewFormats));
834 case md_updateformats:
835 add(MenuItem(MenuItem::UpdateFormats));
838 case md_exportformats:
839 add(MenuItem(MenuItem::ExportFormats));
842 case md_importformats:
843 add(MenuItem(MenuItem::ImportFormats));
846 case md_floatlistinsert:
847 add(MenuItem(MenuItem::FloatListInsert));
851 add(MenuItem(MenuItem::FloatInsert));
855 add(MenuItem(MenuItem::PasteRecent));
859 add(MenuItem(MenuItem::Toolbars));
863 add(MenuItem(MenuItem::Branches));
868 // fallback to md_submenu
871 docstring const mlabel = translateIfPossible(lex.getDocString());
873 docstring const mname = lex.getDocString();
874 add(MenuItem(MenuItem::Submenu,
875 toqstr(mlabel), toqstr(mname), optional));
885 lex.printError("Menu::read: "
886 "Unknown menu tag: `$$Token'");
894 MenuItem const & Menu::operator[](size_type i) const
900 bool Menu::hasFunc(FuncRequest const & func) const
902 for (const_iterator it = begin(), et = end(); it != et; ++it)
903 if (it->func() == func)
909 void Menu::checkShortcuts() const
911 // This is a quadratic algorithm, but we do not care because
912 // menus are short enough
913 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
914 QString shortcut = it1->shortcut();
915 if (shortcut.isEmpty())
917 if (!it1->label().contains(shortcut))
918 lyxerr << "Menu warning: menu entry \""
919 << fromqstr(it1->label())
920 << "\" does not contain shortcut `"
921 << fromqstr(shortcut) << "'." << endl;
922 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
923 if (!it2->shortcut().compare(shortcut, Qt::CaseInsensitive)) {
924 lyxerr << "Menu warning: menu entries "
925 << '"' << fromqstr(it1->fulllabel())
926 << "\" and \"" << fromqstr(it2->fulllabel())
927 << "\" share the same shortcut."
935 bool Menu::searchMenu(FuncRequest const & func, vector<docstring> & names) const
937 const_iterator m = begin();
938 const_iterator m_end = end();
939 for (; m != m_end; ++m) {
940 if (m->kind() == MenuItem::Command && m->func() == func) {
941 names.push_back(qstring_to_ucs4(m->label()));
944 if (m->kind() == MenuItem::Submenu) {
945 names.push_back(qstring_to_ucs4(m->label()));
946 Menu const & submenu = *m->submenu();
947 if (submenu.searchMenu(func, names))
958 bool compareFormat(Format const * p1, Format const * p2)
964 QString limitStringLength(docstring const & str)
966 size_t const max_item_length = 45;
968 if (str.size() > max_item_length)
969 return toqstr(str.substr(0, max_item_length - 3) + "...");
977 void Menu::expandLastfiles()
979 LastFilesSection::LastFiles const & lf = LyX::cref().session().lastFiles().lastFiles();
980 LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
984 for (; lfit != lf.end() && ii < 10; ++lfit, ++ii) {
985 string const file = lfit->absFilename();
986 QString const label = QString("%1. %2|%3").arg(ii)
987 .arg(toqstr(makeDisplayPath(file, 30))).arg(ii);
988 add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, file)));
993 void Menu::expandDocuments()
995 Buffer * first = theBufferList().first();
1000 // We cannot use a for loop as the buffer list cycles.
1002 QString label = toqstr(b->fileName().displayName(20));
1006 label = QString::number(ii) + ". " + label + '|' + QString::number(ii);
1007 add(MenuItem(MenuItem::Command, label,
1008 FuncRequest(LFUN_BUFFER_SWITCH, b->absFileName())));
1010 b = theBufferList().next(b);
1012 } while (b != first);
1014 add(MenuItem(MenuItem::Command, qt_("No Documents Open!"),
1015 FuncRequest(LFUN_NOACTION)));
1020 void Menu::expandBookmarks()
1022 lyx::BookmarksSection const & bm = LyX::cref().session().bookmarks();
1024 for (size_t i = 1; i <= bm.size(); ++i) {
1025 if (bm.isValid(i)) {
1026 string const file = bm.bookmark(i).filename.absFilename();
1027 QString const label = QString("%1. %2|%3").arg(i)
1028 .arg(toqstr(makeDisplayPath(file, 20))).arg(i);
1029 add(MenuItem(MenuItem::Command, label,
1030 FuncRequest(LFUN_BOOKMARK_GOTO, convert<docstring>(i))));
1036 void Menu::expandFormats(MenuItem::Kind kind, Buffer const * buf)
1038 if (!buf && kind != MenuItem::ImportFormats) {
1039 add(MenuItem(MenuItem::Command,
1040 qt_("No Document Open!"),
1041 FuncRequest(LFUN_NOACTION)));
1045 typedef vector<Format const *> Formats;
1050 case MenuItem::ImportFormats:
1051 formats = theConverters().importableFormats();
1052 action = LFUN_BUFFER_IMPORT;
1054 case MenuItem::ViewFormats:
1055 formats = buf->exportableFormats(true);
1056 action = LFUN_BUFFER_VIEW;
1058 case MenuItem::UpdateFormats:
1059 formats = buf->exportableFormats(true);
1060 action = LFUN_BUFFER_UPDATE;
1063 formats = buf->exportableFormats(false);
1064 action = LFUN_BUFFER_EXPORT;
1066 sort(formats.begin(), formats.end(), &compareFormat);
1068 Formats::const_iterator fit = formats.begin();
1069 Formats::const_iterator end = formats.end();
1070 for (; fit != end ; ++fit) {
1071 if ((*fit)->dummy())
1073 QString label = toqstr((*fit)->prettyname());
1074 QString const shortcut = toqstr((*fit)->shortcut());
1077 case MenuItem::ImportFormats:
1078 // FIXME: This is a hack, we should rather solve
1079 // FIXME: bug 2488 instead.
1080 if ((*fit)->name() == "text")
1081 label = qt_("Plain Text");
1082 else if ((*fit)->name() == "textparagraph")
1083 label = qt_("Plain Text, Join Lines");
1086 case MenuItem::ViewFormats:
1087 case MenuItem::ExportFormats:
1088 case MenuItem::UpdateFormats:
1089 if (!(*fit)->documentFormat())
1093 BOOST_ASSERT(false);
1096 // FIXME: if we had proper support for translating the
1097 // format names defined in configure.py, there would
1098 // not be a need to check whether the shortcut is
1099 // correct. If we add it uncondiitonally, it would
1100 // create useless warnings on bad shortcuts
1101 if (!shortcut.isEmpty() && label.contains(shortcut))
1102 label += '|' + shortcut;
1105 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1106 FuncRequest(action, (*fit)->name())));
1108 add(MenuItem(MenuItem::Command, label,
1109 FuncRequest(action, (*fit)->name())));
1114 void Menu::expandFloatListInsert(Buffer const * buf)
1117 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
1118 FuncRequest(LFUN_NOACTION)));
1122 FloatList const & floats = buf->params().documentClass().floats();
1123 FloatList::const_iterator cit = floats.begin();
1124 FloatList::const_iterator end = floats.end();
1125 for (; cit != end; ++cit) {
1126 addWithStatusCheck(MenuItem(MenuItem::Command,
1127 qt_(cit->second.listName()),
1128 FuncRequest(LFUN_FLOAT_LIST,
1129 cit->second.type())));
1134 void Menu::expandFloatInsert(Buffer const * buf)
1137 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
1138 FuncRequest(LFUN_NOACTION)));
1142 FloatList const & floats = buf->params().documentClass().floats();
1143 FloatList::const_iterator cit = floats.begin();
1144 FloatList::const_iterator end = floats.end();
1145 for (; cit != end; ++cit) {
1147 QString const label = qt_(cit->second.name());
1148 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1149 FuncRequest(LFUN_FLOAT_INSERT,
1150 cit->second.type())));
1155 void Menu::expandFlexInsert(Buffer const * buf, string s)
1158 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
1159 FuncRequest(LFUN_NOACTION)));
1162 TextClass::InsetLayouts const & insetLayouts =
1163 buf->params().documentClass().insetLayouts();
1164 TextClass::InsetLayouts::const_iterator cit = insetLayouts.begin();
1165 TextClass::InsetLayouts::const_iterator end = insetLayouts.end();
1166 for (; cit != end; ++cit) {
1167 docstring const label = cit->first;
1168 if (cit->second.lyxtype() == s)
1169 addWithStatusCheck(MenuItem(MenuItem::Command,
1170 toqstr(label), FuncRequest(LFUN_FLEX_INSERT,
1176 size_t const max_number_of_items = 25;
1178 void Menu::expandToc2(Toc const & toc_list,
1179 size_t from, size_t to, int depth)
1181 int shortcut_count = 0;
1183 // check whether depth is smaller than the smallest depth in toc.
1184 int min_depth = 1000;
1185 for (size_t i = from; i < to; ++i)
1186 min_depth = min(min_depth, toc_list[i].depth());
1187 if (min_depth > depth)
1190 if (to - from <= max_number_of_items) {
1191 for (size_t i = from; i < to; ++i) {
1192 QString label(4 * max(0, toc_list[i].depth() - depth), ' ');
1193 label += limitStringLength(toc_list[i].str());
1194 if (toc_list[i].depth() == depth
1195 && shortcut_count < 9) {
1196 if (label.contains(QString::number(shortcut_count + 1)))
1197 label += '|' + QString::number(++shortcut_count);
1199 add(MenuItem(MenuItem::Command, label,
1200 FuncRequest(toc_list[i].action())));
1205 size_t new_pos = pos + 1;
1206 while (new_pos < to &&
1207 toc_list[new_pos].depth() > depth)
1210 QString label(4 * max(0, toc_list[pos].depth() - depth), ' ');
1211 label += limitStringLength(toc_list[pos].str());
1212 if (toc_list[pos].depth() == depth &&
1213 shortcut_count < 9) {
1214 if (label.contains(QString::number(shortcut_count + 1)))
1215 label += '|' + QString::number(++shortcut_count);
1217 if (new_pos == pos + 1) {
1218 add(MenuItem(MenuItem::Command,
1219 label, FuncRequest(toc_list[pos].action())));
1221 MenuItem item(MenuItem::Submenu, label);
1222 item.setSubmenu(new Menu);
1223 item.submenu()->expandToc2(toc_list, pos, new_pos, depth + 1);
1232 void Menu::expandToc(Buffer const * buf)
1234 // To make things very cleanly, we would have to pass buf to
1235 // all MenuItem constructors and to expandToc2. However, we
1236 // know that all the entries in a TOC will be have status_ ==
1237 // OK, so we avoid this unnecessary overhead (JMarc)
1240 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
1241 FuncRequest(LFUN_NOACTION)));
1245 Buffer* cbuf = const_cast<Buffer*>(buf);
1246 cbuf->tocBackend().update();
1247 cbuf->structureChanged();
1249 // Add an entry for the master doc if this is a child doc
1250 Buffer const * const master = buf->masterBuffer();
1251 if (buf != master) {
1252 ParIterator const pit = par_iterator_begin(master->inset());
1253 string const arg = convert<string>(pit->id());
1254 FuncRequest f(LFUN_PARAGRAPH_GOTO, arg);
1255 add(MenuItem(MenuItem::Command, qt_("Master Document"), f));
1258 FloatList const & floatlist = buf->params().documentClass().floats();
1259 TocList const & toc_list = buf->tocBackend().tocs();
1260 TocList::const_iterator cit = toc_list.begin();
1261 TocList::const_iterator end = toc_list.end();
1262 for (; cit != end; ++cit) {
1263 // Handle this later
1264 if (cit->first == "tableofcontents")
1267 // All the rest is for floats
1268 auto_ptr<Menu> menu(new Menu);
1269 TocIterator ccit = cit->second.begin();
1270 TocIterator eend = cit->second.end();
1271 for (; ccit != eend; ++ccit) {
1272 QString const label = limitStringLength(ccit->str());
1273 menu->add(MenuItem(MenuItem::Command, label,
1274 FuncRequest(ccit->action())));
1276 string const & floatName = floatlist.getType(cit->first).listName();
1278 if (!floatName.empty())
1279 label = qt_(floatName);
1280 // BUG3633: listings is not a proper float so its name
1281 // is not shown in floatlist.
1282 else if (cit->first == "equation")
1283 label = qt_("List of Equations");
1284 else if (cit->first == "index")
1285 label = qt_("List of Indexes");
1286 else if (cit->first == "listing")
1287 label = qt_("List of Listings");
1288 else if (cit->first == "marginalnote")
1289 label = qt_("List of Marginal notes");
1290 else if (cit->first == "note")
1291 label = qt_("List of Notes");
1292 else if (cit->first == "footnote")
1293 label = qt_("List of Foot notes");
1294 else if (cit->first == "label")
1295 label = qt_("Labels and References");
1296 else if (cit->first == "citation")
1297 label = qt_("List of Citations");
1298 // this should not happen now, but if something else like
1299 // listings is added later, this can avoid an empty menu name.
1301 label = qt_("Other floats");
1302 MenuItem item(MenuItem::Submenu, label);
1303 item.setSubmenu(menu.release());
1307 // Handle normal TOC
1308 cit = toc_list.find("tableofcontents");
1310 addWithStatusCheck(MenuItem(MenuItem::Command,
1311 qt_("No Table of contents"),
1314 expandToc2(cit->second, 0, cit->second.size(), 0);
1319 void Menu::expandPasteRecent()
1321 vector<docstring> const sel = cap::availableSelections();
1323 vector<docstring>::const_iterator cit = sel.begin();
1324 vector<docstring>::const_iterator end = sel.end();
1326 for (unsigned int index = 0; cit != end; ++cit, ++index) {
1327 add(MenuItem(MenuItem::Command, toqstr(*cit),
1328 FuncRequest(LFUN_PASTE, convert<string>(index))));
1333 void Menu::expandToolbars()
1336 // extracts the toolbars from the backend
1337 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
1338 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
1340 for (; cit != end; ++cit) {
1341 QString label = qt_(cit->gui_name);
1342 // frontends are not supposed to turn on/off toolbars,
1343 // if they cannot update ToolbarBackend::flags. That
1344 // is to say, ToolbarsBackend::flags should reflect
1345 // the true state of toolbars.
1347 // menu is displayed as
1351 // in the case of auto.
1352 if (cit->flags & ToolbarInfo::AUTO)
1353 label += qt_(" (auto)");
1354 add(MenuItem(MenuItem::Command, label,
1355 FuncRequest(LFUN_TOOLBAR_TOGGLE, cit->name + " allowauto")));
1360 void Menu::expandBranches(Buffer const * buf)
1363 add(MenuItem(MenuItem::Command,
1364 qt_("No Document Open!"),
1365 FuncRequest(LFUN_NOACTION)));
1369 BufferParams const & params = buf->masterBuffer()->params();
1370 if (params.branchlist().empty()) {
1371 add(MenuItem(MenuItem::Command,
1372 qt_("No Branch in Document!"),
1373 FuncRequest(LFUN_NOACTION)));
1377 BranchList::const_iterator cit = params.branchlist().begin();
1378 BranchList::const_iterator end = params.branchlist().end();
1380 for (int ii = 1; cit != end; ++cit, ++ii) {
1381 docstring label = cit->getBranch();
1383 label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
1384 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1385 FuncRequest(LFUN_BRANCH_INSERT,
1386 cit->getBranch())));
1391 void Menus::Impl::expand(Menu const & frommenu, Menu & tomenu,
1392 Buffer const * buf) const
1394 if (!tomenu.empty())
1397 for (Menu::const_iterator cit = frommenu.begin();
1398 cit != frommenu.end() ; ++cit) {
1399 switch (cit->kind()) {
1400 case MenuItem::Lastfiles:
1401 tomenu.expandLastfiles();
1404 case MenuItem::Documents:
1405 tomenu.expandDocuments();
1408 case MenuItem::Bookmarks:
1409 tomenu.expandBookmarks();
1412 case MenuItem::ImportFormats:
1413 case MenuItem::ViewFormats:
1414 case MenuItem::UpdateFormats:
1415 case MenuItem::ExportFormats:
1416 tomenu.expandFormats(cit->kind(), buf);
1419 case MenuItem::CharStyles:
1420 tomenu.expandFlexInsert(buf, "charstyle");
1423 case MenuItem::Custom:
1424 tomenu.expandFlexInsert(buf, "custom");
1427 case MenuItem::Elements:
1428 tomenu.expandFlexInsert(buf, "element");
1431 case MenuItem::FloatListInsert:
1432 tomenu.expandFloatListInsert(buf);
1435 case MenuItem::FloatInsert:
1436 tomenu.expandFloatInsert(buf);
1439 case MenuItem::PasteRecent:
1440 tomenu.expandPasteRecent();
1443 case MenuItem::Toolbars:
1444 tomenu.expandToolbars();
1447 case MenuItem::Branches:
1448 tomenu.expandBranches(buf);
1452 tomenu.expandToc(buf);
1455 case MenuItem::Submenu: {
1456 MenuItem item(*cit);
1457 item.setSubmenu(new Menu(cit->submenuname()));
1458 expand(getMenu(cit->submenuname()),
1459 *item.submenu(), buf);
1460 tomenu.addWithStatusCheck(item);
1464 case MenuItem::Separator:
1465 tomenu.addWithStatusCheck(*cit);
1468 case MenuItem::Command:
1469 if (!specialmenu_.hasFunc(cit->func()))
1470 tomenu.addWithStatusCheck(*cit);
1474 // we do not want the menu to end with a separator
1476 && tomenu.items_.back().kind() == MenuItem::Separator)
1477 tomenu.items_.pop_back();
1479 // Check whether the shortcuts are unique
1480 tomenu.checkShortcuts();
1484 void Menus::read(Lexer & lex)
1493 struct keyword_item menutags[md_last - 1] = {
1494 { "end", md_endmenuset },
1495 { "menu", md_menu },
1496 { "menubar", md_menubar }
1500 if (compare_ascii_no_case(lex.getString(), "menuset")) {
1501 lyxerr << "Menubackend::read: ERROR wrong token:`"
1502 << lex.getString() << '\'' << endl;
1505 lex.pushTable(menutags, md_last - 1);
1506 if (lyxerr.debugging(Debug::PARSER))
1507 lex.printTable(lyxerr);
1511 while (lex.isOK() && !quit) {
1512 switch (lex.lex()) {
1514 d->menubar_.read(lex);
1518 QString const name = toqstr(lex.getDocString());
1519 if (d->hasMenu(name)) {
1520 d->getMenu(name).read(lex);
1532 lex.printError("menubackend::read: "
1533 "Unknown menu tag: `$$Token'");
1541 void Menus::Impl::add(Menu const & menu)
1543 menulist_.push_back(menu);
1547 bool Menus::Impl::hasMenu(QString const & name) const
1549 return find_if(begin(), end(), MenuNamesEqual(name)) != end();
1553 Menu const & Menus::Impl::getMenu(QString const & name) const
1555 const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
1557 lyxerr << "No submenu named " << fromqstr(name) << endl;
1558 BOOST_ASSERT(cit != end());
1563 Menu & Menus::Impl::getMenu(QString const & name)
1565 iterator it = find_if(begin(), end(), MenuNamesEqual(name));
1567 lyxerr << "No submenu named " << fromqstr(name) << endl;
1568 BOOST_ASSERT(it != end());
1573 } // namespace frontend
1576 #include "Menus_moc.cpp"