]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Menus.cpp
various cleanup and simplifications
[lyx.git] / src / frontends / qt4 / Menus.cpp
1 /**
2  * \file qt4/Menus.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Asger Alstrup
8  * \author Lars Gullik Bjønnes
9  * \author Jean-Marc Lasgouttes
10  * \author André Pönitz
11  * \author Dekel Tsur
12  * \author Martin Vermeer
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #include <config.h>
18
19 #include "Menus.h"
20
21 #include "Action.h"
22 #include "GuiApplication.h"
23 #include "GuiView.h"
24
25 #include "BranchList.h"
26 #include "Buffer.h"
27 #include "BufferList.h"
28 #include "BufferParams.h"
29 #include "Converter.h"
30 #include "CutAndPaste.h"
31 #include "Floating.h"
32 #include "FloatList.h"
33 #include "Format.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
36 #include "KeyMap.h"
37 #include "Lexer.h"
38 #include "LyXAction.h"
39 #include "LyX.h" // for lastfiles
40 #include "LyXFunc.h"
41 #include "Paragraph.h"
42 #include "qt_helpers.h"
43 #include "Session.h"
44 #include "TextClass.h"
45 #include "TocBackend.h"
46 #include "ToolbarBackend.h"
47
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"
53
54 #include <QCursor>
55 #include <QHash>
56 #include <QMenuBar>
57 #include <QString>
58
59 #include <boost/shared_ptr.hpp>
60
61 #include <algorithm>
62 #include <ostream>
63 #include <vector>
64
65 using namespace std;
66 using namespace lyx::support;
67
68
69 namespace lyx {
70 namespace frontend {
71
72 namespace {
73
74 // MacOSX specific stuff is at the end.
75
76 class Menu;
77
78 ///
79 class MenuItem {
80 public:
81         /// The type of elements that can be in a menu
82         enum Kind {
83                 ///
84                 Command,
85                 ///
86                 Submenu,
87                 ///
88                 Separator,
89                 /** This is the list of last opened file,
90                     typically for the File menu. */
91                 Lastfiles,
92                 /** This is the list of opened Documents,
93                     typically for the Documents menu. */
94                 Documents,
95                 /** This is the bookmarks */
96                 Bookmarks,
97                 ///
98                 Toc,
99                 /** This is a list of viewable formats
100                     typically for the File->View menu. */
101                 ViewFormats,
102                 /** This is a list of updatable formats
103                     typically for the File->Update menu. */
104                 UpdateFormats,
105                 /** This is a list of exportable formats
106                     typically for the File->Export menu. */
107                 ExportFormats,
108                 /** This is a list of importable formats
109                     typically for the File->Export menu. */
110                 ImportFormats,
111                 /** This is the list of elements available
112                  * for insertion into document. */
113                 CharStyles,
114                 /** This is the list of user-configurable
115                 insets to insert into document */
116                 Custom,
117                 /** This is the list of XML elements to
118                 insert into the document */
119                 Elements,
120                 /** This is the list of floats that we can
121                     insert a list for. */
122                 FloatListInsert,
123                 /** This is the list of floats that we can
124                     insert. */
125                 FloatInsert,
126                 /** This is the list of selections that can
127                     be pasted. */
128                 PasteRecent,
129                 /** toolbars */
130                 Toolbars,
131                 /** Available branches in document */
132                 Branches
133         };
134
135         explicit MenuItem(Kind kind);
136
137         MenuItem(Kind kind,
138                  QString const & label,
139                  QString const & submenu = QString(),
140                  bool optional = false);
141
142         MenuItem(Kind kind,
143                  QString const & label,
144                  FuncRequest const & func,
145                  bool optional = false);
146
147         /// This one is just to please boost::shared_ptr<>
148         ~MenuItem();
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; }
173         ///
174         Menu * submenu() const { return submenu_.get(); }
175         ///
176         void setSubmenu(Menu * menu);
177
178 private:
179         ///
180         Kind kind_;
181         ///
182         QString label_;
183         ///
184         FuncRequest func_;
185         ///
186         QString submenuname_;
187         ///
188         bool optional_;
189         ///
190         FuncStatus status_;
191         ///
192         boost::shared_ptr<Menu> submenu_;
193 };
194
195 ///
196 class Menu {
197 public:
198         ///
199         typedef std::vector<MenuItem> ItemList;
200         ///
201         typedef ItemList::const_iterator const_iterator;
202         ///
203         explicit Menu(QString const & name = QString()) : name_(name) {}
204
205         ///
206         void read(Lexer &);
207         ///
208         QString const & name() const { return name_; }
209         ///
210         bool empty() const { return items_.empty(); }
211         /// Clear the menu content.
212         void clear() { items_.clear(); }
213         ///
214         size_t size() const { return items_.size(); }
215         ///
216         MenuItem const & operator[](size_t) const;
217         ///
218         const_iterator begin() const { return items_.begin(); }
219         ///
220         const_iterator end() const { return items_.end(); }
221         
222         // search for func in this menu iteratively, and put menu
223         // names in a stack.
224         bool searchMenu(FuncRequest const & func, std::vector<docstring> & names)
225                 const;
226         ///
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
231         /// menu item.
232         void addWithStatusCheck(MenuItem const &);
233         // Check whether the menu shortcuts are unique
234         void checkShortcuts() const;
235         ///
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);
248         ///
249         ItemList items_;
250         ///
251         QString name_;
252 };
253
254 /// a submenu
255 class GuiPopupMenu : public QMenu
256 {
257 public:
258         ///
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())
262         {
263                 setTitle(label(mi));
264         }
265
266         /// populates the menu or one of its submenu
267         /// This is used as a recursive function
268         void populate(QMenu * qMenu, Menu * menu);
269
270         /// Get a Menu item label from the menu backend
271         QString label(MenuItem const & mi) const;
272
273         void showEvent(QShowEvent * ev)
274         {
275                 if (top_level_menu)
276                         guiApp->menus().updateMenu(name);
277                 QMenu::showEvent(ev);
278         }
279
280         /// Only needed for top level menus.
281         Menu * top_level_menu;
282         /// our owning view
283         GuiView * view;
284         /// the name of this menu
285         QString name;
286 };
287
288
289 class MenuNamesEqual
290 {
291 public:
292         MenuNamesEqual(QString const & name) : name_(name) {}
293         bool operator()(Menu const & menu) const { return menu.name() == name_; }
294 private:
295         QString name_;
296 };
297
298 ///
299 typedef std::vector<Menu> MenuList;
300 ///
301 typedef MenuList::const_iterator const_iterator;
302 ///
303 typedef MenuList::iterator iterator;
304
305
306 void GuiPopupMenu::populate(QMenu * qMenu, Menu * menu)
307 {
308         LYXERR(Debug::GUI, "populating menu " << fromqstr(menu->name()));
309         if (menu->size() == 0) {
310                 LYXERR(Debug::GUI, "\tERROR: empty menu " << fromqstr(menu->name()));
311                 return;
312         }
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());
322                 } else {
323                         // we have a MenuItem::Command
324                         qMenu->addAction(new Action(*view, QIcon(), label(*m), m->func(),
325                                 QString()));
326                 }
327         }
328 }
329
330
331 QString GuiPopupMenu::label(MenuItem const & mi) const
332 {
333         QString label = mi.label();
334         label.replace("&", "&&");
335
336         QString shortcut = mi.shortcut();
337         if (!shortcut.isEmpty()) {
338                 int pos = label.indexOf(shortcut);
339                 if (pos != -1)
340                         //label.insert(pos, 1, char_type('&'));
341                         label.replace(pos, 0, "&");
342         }
343
344         QString const binding = mi.binding();
345         if (!binding.isEmpty())
346                 label += '\t' + binding;
347
348         return label;
349 }
350
351 } // namespace anon
352
353
354 struct Menus::Impl {
355         ///
356         void add(Menu const &);
357         ///
358         bool hasMenu(QString const &) const;
359         ///
360         Menu & getMenu(QString const &);
361         ///
362         Menu const & getMenu(QString const &) const;
363
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
368         */
369         void expand(Menu const & frommenu, Menu & tomenu,
370                     Buffer const *) const;
371
372         /// Initialize specific MACOS X menubar
373         void macxMenuBarInit(GuiView * view);
374
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
379         */
380         Menu specialmenu_;
381
382         ///
383         MenuList menulist_;
384         ///
385         Menu menubar_;
386
387         typedef QHash<QString, GuiPopupMenu *> NameMap;
388
389         /// name to menu for \c menu() method.
390         NameMap name_map_;
391 };
392
393
394 Menus::Menus(): d(new Impl) {}
395
396
397 bool Menus::searchMenu(FuncRequest const & func,
398         vector<docstring> & names) const
399 {
400         return d->menubar_.searchMenu(func, names);
401 }
402
403
404 void Menus::fillMenuBar(GuiView * view)
405 {
406         // Clear all menubar contents before filling it.
407         view->menuBar()->clear();
408         
409 #ifdef Q_WS_MACX
410         // setup special mac specific menu item
411         macxMenuBarInit(view);
412 #endif
413
414         LYXERR(Debug::GUI, "populating menu bar" << fromqstr(d->menubar_.name()));
415
416         if (d->menubar_.size() == 0) {
417                 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
418                         << fromqstr(d->menubar_.name()));
419                 return;
420         }
421         else {
422                 LYXERR(Debug::GUI, "menu bar entries "
423                         << d->menubar_.size());
424         }
425
426         Menu menu;
427         d->expand(d->menubar_, menu, view->buffer());
428
429         Menu::const_iterator m = menu.begin();
430         Menu::const_iterator end = menu.end();
431
432         for (; m != end; ++m) {
433
434                 if (m->kind() != MenuItem::Submenu) {
435                         LYXERR(Debug::GUI, "\tERROR: not a submenu " << fromqstr(m->label()));
436                         continue;
437                 }
438
439                 LYXERR(Debug::GUI, "menu bar item " << fromqstr(m->label())
440                         << " is a submenu named " << fromqstr(m->submenuname()));
441
442                 QString name = m->submenuname();
443                 if (!d->hasMenu(name)) {
444                         LYXERR(Debug::GUI, "\tERROR: " << fromqstr(name)
445                                 << " submenu has no menu!");
446                         continue;
447                 }
448
449                 GuiPopupMenu * qmenu = new GuiPopupMenu(view, *m, true);
450                 view->menuBar()->addMenu(qmenu);
451
452                 d->name_map_[name] = qmenu;
453         }
454 }
455
456
457 void Menus::updateMenu(QString const & name)
458 {
459         GuiPopupMenu * qmenu = d->name_map_[name];
460         LYXERR(Debug::GUI, "GuiPopupMenu::updateView()"
461                 << "\tTriggered menu: " << fromqstr(qmenu->name));
462         qmenu->clear();
463
464         if (qmenu->name.isEmpty())
465                 return;
466
467         // Here, We make sure that theLyXFunc points to the correct LyXView.
468         theLyXFunc().setLyXView(qmenu->view);
469
470         Menu const & fromLyxMenu = d->getMenu(qmenu->name);
471         d->expand(fromLyxMenu, *qmenu->top_level_menu, qmenu->view->buffer());
472
473         if (!d->hasMenu(qmenu->top_level_menu->name())) {
474                 LYXERR(Debug::GUI, "\tWARNING: menu seems empty"
475                         << fromqstr(qmenu->top_level_menu->name()));
476         }
477         qmenu->populate(qmenu, qmenu->top_level_menu);
478 }
479
480
481 QMenu * Menus::menu(QString const & name)
482 {
483         LYXERR(Debug::GUI, "Context menu requested: " << fromqstr(name));
484         GuiPopupMenu * menu = d->name_map_.value(name, 0);
485         if (!menu)
486                 LYXERR0("resquested context menu not found: " << fromqstr(name));
487         return menu;
488 }
489
490
491 /// Some special Qt/Mac support hacks
492
493 /*
494   Here is what the Qt documentation says about how a menubar is chosen:
495
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
500      documented below).
501
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.
509
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
512   focus. (JMarc)
513 */
514
515 void Menus::Impl::macxMenuBarInit(GuiView * view)
516 {
517         // The Mac menubar initialisation must be done only once!
518         static bool done = false;
519         if (done)
520                 return;
521         done = true;
522
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)
528         */
529
530         /* The entries of our special mac menu. If we add support for
531          * special entries in Menus, we could imagine something
532          * like
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)
536          */
537         struct MacMenuEntry {
538                 kb_action action;
539                 char const * arg;
540                 char const * label;
541                 QAction::MenuRole role;
542         };
543
544         MacMenuEntry entries[] = {
545                 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
546                  QAction::AboutRole},
547                 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
548                  QAction::PreferencesRole},
549                 {LFUN_RECONFIGURE, "", "Reconfigure",
550                  QAction::ApplicationSpecificRole},
551                 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
552         };
553         const size_t num_entries = sizeof(entries) / sizeof(entries[0]);
554
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));
560         }
561
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);
572         }
573 }
574
575
576 MenuItem::MenuItem(Kind kind)
577         : kind_(kind), optional_(false)
578 {}
579
580
581 MenuItem::MenuItem(Kind kind, QString const & label,
582                    QString const & submenu, bool optional)
583         : kind_(kind), label_(label),
584           submenuname_(submenu), optional_(optional)
585 {
586         BOOST_ASSERT(kind == Submenu);
587 }
588
589
590 MenuItem::MenuItem(Kind kind, QString const & label,
591                    FuncRequest const & func, bool optional)
592         : kind_(kind), label_(label), func_(func), optional_(optional)
593 {
594         func_.origin = FuncRequest::MENU;
595 }
596
597
598 MenuItem::~MenuItem()
599 {}
600
601
602 void MenuItem::setSubmenu(Menu * menu)
603 {
604         submenu_.reset(menu);
605 }
606
607
608 QString MenuItem::label() const
609 {
610         return label_.split('|')[0];
611 }
612
613
614 QString MenuItem::shortcut() const
615 {
616         return label_.contains('|') ? label_.split('|')[1] : QString();
617 }
618
619
620 QString MenuItem::binding() const
621 {
622         if (kind_ != Command)
623                 return QString();
624
625         // Get the keys bound to this action, but keep only the
626         // first one later
627         KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func_);
628
629         if (bindings.size())
630                 return toqstr(bindings.begin()->print(KeySequence::ForGui));
631
632         LYXERR(Debug::KBMAP, "No binding for "
633                 << lyxaction.getActionName(func_.action)
634                 << '(' << func_.argument() << ')');
635         return QString();
636 }
637
638
639 void Menu::addWithStatusCheck(MenuItem const & i)
640 {
641         switch (i.kind()) {
642
643         case MenuItem::Command: {
644                 FuncStatus status = lyx::getStatus(i.func());
645                 if (status.unknown() || (!status.enabled() && i.optional()))
646                         break;
647                 items_.push_back(i);
648                 items_.back().status(status);
649                 break;
650         }
651
652         case MenuItem::Submenu: {
653                 if (i.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()) {
660                                         enabled = true;
661                                         break;
662                                 }
663                         }
664                         if (enabled || !i.optional()) {
665                                 items_.push_back(i);
666                                 items_.back().status().enabled(enabled);
667                         }
668                 }
669                 else
670                         items_.push_back(i);
671                 break;
672         }
673
674         case MenuItem::Separator:
675                 if (!items_.empty() && items_.back().kind() != MenuItem::Separator)
676                         items_.push_back(i);
677                 break;
678
679         default:
680                 items_.push_back(i);
681         }
682 }
683
684
685 void Menu::read(Lexer & lex)
686 {
687         enum Menutags {
688                 md_item = 1,
689                 md_branches,
690                 md_documents,
691                 md_bookmarks,
692                 md_charstyles,
693                 md_custom,
694                 md_elements,
695                 md_endmenu,
696                 md_exportformats,
697                 md_importformats,
698                 md_lastfiles,
699                 md_optitem,
700                 md_optsubmenu,
701                 md_separator,
702                 md_submenu,
703                 md_toc,
704                 md_updateformats,
705                 md_viewformats,
706                 md_floatlistinsert,
707                 md_floatinsert,
708                 md_pasterecent,
709                 md_toolbars,
710                 md_last
711         };
712
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 },
725                 { "item", md_item },
726                 { "lastfiles", md_lastfiles },
727                 { "optitem", md_optitem },
728                 { "optsubmenu", md_optsubmenu },
729                 { "pasterecent", md_pasterecent },
730                 { "separator", md_separator },
731                 { "submenu", md_submenu },
732                 { "toc", md_toc },
733                 { "toolbars", md_toolbars },
734                 { "updateformats", md_updateformats },
735                 { "viewformats", md_viewformats }
736         };
737
738         lex.pushTable(menutags, md_last - 1);
739         if (lyxerr.debugging(Debug::PARSER))
740                 lex.printTable(lyxerr);
741
742         bool quit = false;
743         bool optional = false;
744
745         while (lex.isOK() && !quit) {
746                 switch (lex.lex()) {
747                 case md_optitem:
748                         optional = true;
749                         // fallback to md_item
750                 case md_item: {
751                         lex.next(true);
752                         docstring const name = translateIfPossible(lex.getDocString());
753                         lex.next(true);
754                         string const command = lex.getString();
755                         FuncRequest func = lyxaction.lookupFunc(command);
756                         add(MenuItem(MenuItem::Command, toqstr(name), func, optional));
757                         optional = false;
758                         break;
759                 }
760
761                 case md_separator:
762                         add(MenuItem(MenuItem::Separator));
763                         break;
764
765                 case md_lastfiles:
766                         add(MenuItem(MenuItem::Lastfiles));
767                         break;
768
769                 case md_charstyles:
770                         add(MenuItem(MenuItem::CharStyles));
771                         break;
772
773                 case md_custom:
774                         add(MenuItem(MenuItem::Custom));
775                         break;
776
777                 case md_elements:
778                         add(MenuItem(MenuItem::Elements));
779                         break;
780
781                 case md_documents:
782                         add(MenuItem(MenuItem::Documents));
783                         break;
784
785                 case md_bookmarks:
786                         add(MenuItem(MenuItem::Bookmarks));
787                         break;
788
789                 case md_toc:
790                         add(MenuItem(MenuItem::Toc));
791                         break;
792
793                 case md_viewformats:
794                         add(MenuItem(MenuItem::ViewFormats));
795                         break;
796
797                 case md_updateformats:
798                         add(MenuItem(MenuItem::UpdateFormats));
799                         break;
800
801                 case md_exportformats:
802                         add(MenuItem(MenuItem::ExportFormats));
803                         break;
804
805                 case md_importformats:
806                         add(MenuItem(MenuItem::ImportFormats));
807                         break;
808
809                 case md_floatlistinsert:
810                         add(MenuItem(MenuItem::FloatListInsert));
811                         break;
812
813                 case md_floatinsert:
814                         add(MenuItem(MenuItem::FloatInsert));
815                         break;
816
817                 case md_pasterecent:
818                         add(MenuItem(MenuItem::PasteRecent));
819                         break;
820
821                 case md_toolbars:
822                         add(MenuItem(MenuItem::Toolbars));
823                         break;
824
825                 case md_branches:
826                         add(MenuItem(MenuItem::Branches));
827                         break;
828
829                 case md_optsubmenu:
830                         optional = true;
831                         // fallback to md_submenu
832                 case md_submenu: {
833                         lex.next(true);
834                         docstring const mlabel = translateIfPossible(lex.getDocString());
835                         lex.next(true);
836                         docstring const mname = lex.getDocString();
837                         add(MenuItem(MenuItem::Submenu,
838                                 toqstr(mlabel), toqstr(mname), optional));
839                         optional = false;
840                         break;
841                 }
842
843                 case md_endmenu:
844                         quit = true;
845                         break;
846
847                 default:
848                         lex.printError("Menu::read: "
849                                        "Unknown menu tag: `$$Token'");
850                         break;
851                 }
852         }
853         lex.popTable();
854 }
855
856
857 MenuItem const & Menu::operator[](size_type i) const
858 {
859         return items_[i];
860 }
861
862
863 bool Menu::hasFunc(FuncRequest const & func) const
864 {
865         for (const_iterator it = begin(), et = end(); it != et; ++it)
866                 if (it->func() == func)
867                         return true;
868         return false;
869 }
870
871
872 void Menu::checkShortcuts() const
873 {
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())
879                         continue;
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."
891                                        << endl;
892                         }
893                 }
894         }
895 }
896
897
898 bool Menu::searchMenu(FuncRequest const & func, vector<docstring> & names) const
899 {
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()));
905                         return true;
906                 }
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))
911                                 return true;
912                         names.pop_back();
913                 }
914         }
915         return false;
916 }
917
918
919 namespace {
920
921 bool compareFormat(Format const * p1, Format const * p2)
922 {
923         return *p1 < *p2;
924 }
925
926
927 QString limitStringLength(docstring const & str)
928 {
929         size_t const max_item_length = 45;
930
931         if (str.size() > max_item_length)
932                 return toqstr(str.substr(0, max_item_length - 3) + "...");
933
934         return toqstr(str);
935 }
936
937 } // namespace anon
938
939
940 void Menu::expandLastfiles()
941 {
942         LastFilesSection::LastFiles const & lf = LyX::cref().session().lastFiles().lastFiles();
943         LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
944
945         int ii = 1;
946
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)));
952         }
953 }
954
955
956 void Menu::expandDocuments()
957 {
958         Buffer * first = theBufferList().first();
959         if (first) {
960                 Buffer * b = first;
961                 int ii = 1;
962                 
963                 // We cannot use a for loop as the buffer list cycles.
964                 do {
965                         QString label = toqstr(b->fileName().displayName(20));
966                         if (!b->isClean())
967                                 label += "*";
968                         if (ii < 10)
969                                 label = QString::number(ii) + ". " + label + '|' + QString::number(ii);
970                         add(MenuItem(MenuItem::Command, label,
971                                 FuncRequest(LFUN_BUFFER_SWITCH, b->absFileName())));
972                         
973                         b = theBufferList().next(b);
974                         ++ii;
975                 } while (b != first); 
976         } else {
977                 add(MenuItem(MenuItem::Command, qt_("No Documents Open!"),
978                            FuncRequest(LFUN_NOACTION)));
979         }
980 }
981
982
983 void Menu::expandBookmarks()
984 {
985         lyx::BookmarksSection const & bm = LyX::cref().session().bookmarks();
986
987         for (size_t i = 1; i <= bm.size(); ++i) {
988                 if (bm.isValid(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))));
994                 }
995         }
996 }
997
998
999 void Menu::expandFormats(MenuItem::Kind kind, Buffer const * buf)
1000 {
1001         if (!buf && kind != MenuItem::ImportFormats) {
1002                 add(MenuItem(MenuItem::Command,
1003                                     qt_("No Document Open!"),
1004                                     FuncRequest(LFUN_NOACTION)));
1005                 return;
1006         }
1007
1008         typedef vector<Format const *> Formats;
1009         Formats formats;
1010         kb_action action;
1011
1012         switch (kind) {
1013         case MenuItem::ImportFormats:
1014                 formats = theConverters().importableFormats();
1015                 action = LFUN_BUFFER_IMPORT;
1016                 break;
1017         case MenuItem::ViewFormats:
1018                 formats = buf->exportableFormats(true);
1019                 action = LFUN_BUFFER_VIEW;
1020                 break;
1021         case MenuItem::UpdateFormats:
1022                 formats = buf->exportableFormats(true);
1023                 action = LFUN_BUFFER_UPDATE;
1024                 break;
1025         default:
1026                 formats = buf->exportableFormats(false);
1027                 action = LFUN_BUFFER_EXPORT;
1028         }
1029         sort(formats.begin(), formats.end(), &compareFormat);
1030
1031         Formats::const_iterator fit = formats.begin();
1032         Formats::const_iterator end = formats.end();
1033         for (; fit != end ; ++fit) {
1034                 if ((*fit)->dummy())
1035                         continue;
1036                 QString label = toqstr((*fit)->prettyname());
1037                 QString const shortcut = toqstr((*fit)->shortcut());
1038
1039                 switch (kind) {
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");
1047                         label += "...";
1048                         break;
1049                 case MenuItem::ViewFormats:
1050                 case MenuItem::ExportFormats:
1051                 case MenuItem::UpdateFormats:
1052                         if (!(*fit)->documentFormat())
1053                                 continue;
1054                         break;
1055                 default:
1056                         BOOST_ASSERT(false);
1057                         break;
1058                 }
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;
1066
1067                 if (buf)
1068                         addWithStatusCheck(MenuItem(MenuItem::Command, label,
1069                                 FuncRequest(action, (*fit)->name())));
1070                 else
1071                         add(MenuItem(MenuItem::Command, label,
1072                                 FuncRequest(action, (*fit)->name())));
1073         }
1074 }
1075
1076
1077 void Menu::expandFloatListInsert(Buffer const * buf)
1078 {
1079         if (!buf) {
1080                 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
1081                                     FuncRequest(LFUN_NOACTION)));
1082                 return;
1083         }
1084
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())));
1093         }
1094 }
1095
1096
1097 void Menu::expandFloatInsert(Buffer const * buf)
1098 {
1099         if (!buf) {
1100                 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
1101                                     FuncRequest(LFUN_NOACTION)));
1102                 return;
1103         }
1104
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) {
1109                 // normal float
1110                 QString const label = qt_(cit->second.name());
1111                 addWithStatusCheck(MenuItem(MenuItem::Command, label,
1112                                     FuncRequest(LFUN_FLOAT_INSERT,
1113                                                 cit->second.type())));
1114         }
1115 }
1116
1117
1118 void Menu::expandFlexInsert(Buffer const * buf, string s)
1119 {
1120         if (!buf) {
1121                 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
1122                                     FuncRequest(LFUN_NOACTION)));
1123                 return;
1124         }
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,
1134                                                 label)));
1135         }
1136 }
1137
1138
1139 size_t const max_number_of_items = 25;
1140
1141 void Menu::expandToc2(Toc const & toc_list,
1142                 size_t from, size_t to, int depth)
1143 {
1144         int shortcut_count = 0;
1145
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)
1151                 depth = min_depth;
1152
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);
1161                         }
1162                         add(MenuItem(MenuItem::Command, label,
1163                                             FuncRequest(toc_list[i].action())));
1164                 }
1165         } else {
1166                 size_t pos = from;
1167                 while (pos < to) {
1168                         size_t new_pos = pos + 1;
1169                         while (new_pos < to &&
1170                                toc_list[new_pos].depth() > depth)
1171                                 ++new_pos;
1172
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);
1179                         }
1180                         if (new_pos == pos + 1) {
1181                                 add(MenuItem(MenuItem::Command,
1182                                                     label, FuncRequest(toc_list[pos].action())));
1183                         } else {
1184                                 MenuItem item(MenuItem::Submenu, label);
1185                                 item.setSubmenu(new Menu);
1186                                 item.submenu()->expandToc2(toc_list, pos, new_pos, depth + 1);
1187                                 add(item);
1188                         }
1189                         pos = new_pos;
1190                 }
1191         }
1192 }
1193
1194
1195 void Menu::expandToc(Buffer const * buf)
1196 {
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)
1201
1202         if (!buf) {
1203                 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
1204                                     FuncRequest(LFUN_NOACTION)));
1205                 return;
1206         }
1207
1208         Buffer* cbuf = const_cast<Buffer*>(buf);
1209         cbuf->tocBackend().update();
1210         cbuf->structureChanged();
1211
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));
1219         }
1220
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")
1228                         continue;
1229
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())));
1238                 }
1239                 string const & floatName = floatlist.getType(cit->first).listName();
1240                 QString label;
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.
1263                 else
1264                         label = qt_("Other floats");
1265                 MenuItem item(MenuItem::Submenu, label);
1266                 item.setSubmenu(menu.release());
1267                 add(item);
1268         }
1269
1270         // Handle normal TOC
1271         cit = toc_list.find("tableofcontents");
1272         if (cit == end) {
1273                 addWithStatusCheck(MenuItem(MenuItem::Command,
1274                                     qt_("No Table of contents"),
1275                                     FuncRequest()));
1276         } else {
1277                 expandToc2(cit->second, 0, cit->second.size(), 0);
1278         }
1279 }
1280
1281
1282 void Menu::expandPasteRecent()
1283 {
1284         vector<docstring> const sel = cap::availableSelections();
1285
1286         vector<docstring>::const_iterator cit = sel.begin();
1287         vector<docstring>::const_iterator end = sel.end();
1288
1289         for (unsigned int index = 0; cit != end; ++cit, ++index) {
1290                 add(MenuItem(MenuItem::Command, toqstr(*cit),
1291                                     FuncRequest(LFUN_PASTE, convert<string>(index))));
1292         }
1293 }
1294
1295
1296 void Menu::expandToolbars()
1297 {
1298         //
1299         // extracts the toolbars from the backend
1300         ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
1301         ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
1302
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.
1309                 //
1310                 // menu is displayed as
1311                 //       on/off review
1312                 // and
1313                 //              review (auto)
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")));
1319         }
1320 }
1321
1322
1323 void Menu::expandBranches(Buffer const * buf)
1324 {
1325         if (!buf) {
1326                 add(MenuItem(MenuItem::Command,
1327                                     qt_("No Document Open!"),
1328                                     FuncRequest(LFUN_NOACTION)));
1329                 return;
1330         }
1331
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)));
1337                 return;
1338         }
1339
1340         BranchList::const_iterator cit = params.branchlist().begin();
1341         BranchList::const_iterator end = params.branchlist().end();
1342
1343         for (int ii = 1; cit != end; ++cit, ++ii) {
1344                 docstring label = cit->getBranch();
1345                 if (ii < 10)
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())));
1350         }
1351 }
1352
1353
1354 void Menus::Impl::expand(Menu const & frommenu, Menu & tomenu,
1355                          Buffer const * buf) const
1356 {
1357         if (!tomenu.empty())
1358                 tomenu.clear();
1359
1360         for (Menu::const_iterator cit = frommenu.begin();
1361              cit != frommenu.end() ; ++cit) {
1362                 switch (cit->kind()) {
1363                 case MenuItem::Lastfiles:
1364                         tomenu.expandLastfiles();
1365                         break;
1366
1367                 case MenuItem::Documents:
1368                         tomenu.expandDocuments();
1369                         break;
1370
1371                 case MenuItem::Bookmarks:
1372                         tomenu.expandBookmarks();
1373                         break;
1374
1375                 case MenuItem::ImportFormats:
1376                 case MenuItem::ViewFormats:
1377                 case MenuItem::UpdateFormats:
1378                 case MenuItem::ExportFormats:
1379                         tomenu.expandFormats(cit->kind(), buf);
1380                         break;
1381
1382                 case MenuItem::CharStyles:
1383                         tomenu.expandFlexInsert(buf, "charstyle");
1384                         break;
1385
1386                 case MenuItem::Custom:
1387                         tomenu.expandFlexInsert(buf, "custom");
1388                         break;
1389
1390                 case MenuItem::Elements:
1391                         tomenu.expandFlexInsert(buf, "element");
1392                         break;
1393
1394                 case MenuItem::FloatListInsert:
1395                         tomenu.expandFloatListInsert(buf);
1396                         break;
1397
1398                 case MenuItem::FloatInsert:
1399                         tomenu.expandFloatInsert(buf);
1400                         break;
1401
1402                 case MenuItem::PasteRecent:
1403                         tomenu.expandPasteRecent();
1404                         break;
1405
1406                 case MenuItem::Toolbars:
1407                         tomenu.expandToolbars();
1408                         break;
1409
1410                 case MenuItem::Branches:
1411                         tomenu.expandBranches(buf);
1412                         break;
1413
1414                 case MenuItem::Toc:
1415                         tomenu.expandToc(buf);
1416                         break;
1417
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);
1424                 }
1425                 break;
1426
1427                 case MenuItem::Separator:
1428                         tomenu.addWithStatusCheck(*cit);
1429                         break;
1430
1431                 case MenuItem::Command:
1432                         if (!specialmenu_.hasFunc(cit->func()))
1433                                 tomenu.addWithStatusCheck(*cit);
1434                 }
1435         }
1436
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();
1440
1441         // Check whether the shortcuts are unique
1442         tomenu.checkShortcuts();
1443 }
1444
1445
1446 void Menus::read(Lexer & lex)
1447 {
1448         enum Menutags {
1449                 md_menu = 1,
1450                 md_menubar,
1451                 md_endmenuset,
1452                 md_last
1453         };
1454
1455         struct keyword_item menutags[md_last - 1] = {
1456                 { "end", md_endmenuset },
1457                 { "menu", md_menu },
1458                 { "menubar", md_menubar }
1459         };
1460
1461         //consistency check
1462         if (compare_ascii_no_case(lex.getString(), "menuset")) {
1463                 lyxerr << "Menubackend::read: ERROR wrong token:`"
1464                        << lex.getString() << '\'' << endl;
1465         }
1466
1467         lex.pushTable(menutags, md_last - 1);
1468         if (lyxerr.debugging(Debug::PARSER))
1469                 lex.printTable(lyxerr);
1470
1471         bool quit = false;
1472
1473         while (lex.isOK() && !quit) {
1474                 switch (lex.lex()) {
1475                 case md_menubar:
1476                         d->menubar_.read(lex);
1477                         break;
1478                 case md_menu: {
1479                         lex.next(true);
1480                         QString const name = toqstr(lex.getDocString());
1481                         if (d->hasMenu(name))
1482                                 d->getMenu(name).read(lex);
1483                         else {
1484                                 Menu menu(name);
1485                                 menu.read(lex);
1486                                 d->add(menu);
1487                         }
1488                         break;
1489                 }
1490                 case md_endmenuset:
1491                         quit = true;
1492                         break;
1493                 default:
1494                         lex.printError("menubackend::read: "
1495                                        "Unknown menu tag: `$$Token'");
1496                         break;
1497                 }
1498         }
1499         lex.popTable();
1500 }
1501
1502
1503 void Menus::Impl::add(Menu const & menu)
1504 {
1505         menulist_.push_back(menu);
1506 }
1507
1508
1509 bool Menus::Impl::hasMenu(QString const & name) const
1510 {
1511         return find_if(menulist_.begin(), menulist_.end(),
1512                 MenuNamesEqual(name)) != menulist_.end();
1513 }
1514
1515
1516 Menu const & Menus::Impl::getMenu(QString const & name) const
1517 {
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());
1523         return (*cit);
1524 }
1525
1526
1527 Menu & Menus::Impl::getMenu(QString const & name)
1528 {
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());
1534         return (*it);
1535 }
1536
1537
1538 } // namespace frontend
1539 } // namespace lyx
1540
1541 #include "Menus_moc.cpp"