]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Menus.cpp
* compile fix for mac
[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 #include "qt_helpers.h"
25
26 #include "BranchList.h"
27 #include "Buffer.h"
28 #include "BufferList.h"
29 #include "BufferParams.h"
30 #include "Converter.h"
31 #include "CutAndPaste.h"
32 #include "Floating.h"
33 #include "FloatList.h"
34 #include "Format.h"
35 #include "FuncRequest.h"
36 #include "FuncStatus.h"
37 #include "KeyMap.h"
38 #include "Lexer.h"
39 #include "LyXAction.h"
40 #include "LyX.h" // for lastfiles
41 #include "LyXFunc.h"
42 #include "Paragraph.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) : kind_(kind), optional_(false) {}
136
137         MenuItem(Kind kind,
138                  QString const & label,
139                  QString const & submenu = QString(),
140                  bool optional = false)
141                 : kind_(kind), label_(label), submenuname_(submenu), optional_(optional)
142         {
143                 BOOST_ASSERT(kind == Submenu);
144         }
145
146         MenuItem(Kind kind,
147                  QString const & label,
148                  FuncRequest const & func,
149                  bool optional = false)
150                 : kind_(kind), label_(label), func_(func), optional_(optional)
151         {
152                 func_.origin = FuncRequest::MENU;
153         }
154
155         // boost::shared_ptr<Menu> needs this apprently...
156         ~MenuItem() {}
157
158         /// The label of a given menuitem
159         QString label() const { return label_.split('|')[0]; }
160
161         /// The keyboard shortcut (usually underlined in the entry)
162         QString shortcut() const
163         {
164                 return label_.contains('|') ? label_.split('|')[1] : QString();
165         }
166         /// The complete label, with label and shortcut separated by a '|'
167         QString fulllabel() const { return label_;}
168         /// The kind of entry
169         Kind kind() const { return kind_; }
170         /// the action (if relevant)
171         FuncRequest const & func() const { return func_; }
172         /// returns true if the entry should be ommited when disabled
173         bool optional() const { return optional_; }
174         /// returns the status of the lfun associated with this entry
175         FuncStatus const & status() const { return status_; }
176         /// returns the status of the lfun associated with this entry
177         FuncStatus & status() { return status_; }
178         /// returns the status of the lfun associated with this entry
179         void status(FuncStatus const & status) { status_ = status; }
180
181         ///returns the binding associated to this action.
182         QString binding() const
183         {
184                 if (kind_ != Command)
185                         return QString();
186                 // Get the keys bound to this action, but keep only the
187                 // first one later
188                 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func_);
189                 if (bindings.size())
190                         return toqstr(bindings.begin()->print(KeySequence::ForGui));
191
192                 LYXERR(Debug::KBMAP, "No binding for "
193                         << lyxaction.getActionName(func_.action)
194                         << '(' << func_.argument() << ')');
195                 return QString();
196         }
197
198         /// the description of the  submenu (if relevant)
199         QString const & submenuname() const { return submenuname_; }
200         /// set the description of the  submenu
201         void submenuname(QString const & name) { submenuname_ = name; }
202         ///
203         Menu * submenu() const { return submenu_.get(); }
204         ///
205         void setSubmenu(Menu * menu) { submenu_.reset(menu); }
206
207 private:
208         ///
209         Kind kind_;
210         ///
211         QString label_;
212         ///
213         FuncRequest func_;
214         ///
215         QString submenuname_;
216         ///
217         bool optional_;
218         ///
219         FuncStatus status_;
220         ///
221         boost::shared_ptr<Menu> submenu_;
222 };
223
224 ///
225 class Menu {
226 public:
227         ///
228         typedef std::vector<MenuItem> ItemList;
229         ///
230         typedef ItemList::const_iterator const_iterator;
231         ///
232         explicit Menu(QString const & name = QString()) : name_(name) {}
233
234         ///
235         void read(Lexer &);
236         ///
237         QString const & name() const { return name_; }
238         ///
239         bool empty() const { return items_.empty(); }
240         /// Clear the menu content.
241         void clear() { items_.clear(); }
242         ///
243         size_t size() const { return items_.size(); }
244         ///
245         MenuItem const & operator[](size_t) const;
246         ///
247         const_iterator begin() const { return items_.begin(); }
248         ///
249         const_iterator end() const { return items_.end(); }
250         
251         // search for func in this menu iteratively, and put menu
252         // names in a stack.
253         bool searchMenu(FuncRequest const & func, std::vector<docstring> & names)
254                 const;
255         ///
256         bool hasFunc(FuncRequest const &) const;
257         /// Add the menu item unconditionally
258         void add(MenuItem const & item) { items_.push_back(item); }
259         /// Checks the associated FuncRequest status before adding the
260         /// menu item.
261         void addWithStatusCheck(MenuItem const &);
262         // Check whether the menu shortcuts are unique
263         void checkShortcuts() const;
264         ///
265         void expandLastfiles();
266         void expandDocuments();
267         void expandBookmarks();
268         void expandFormats(MenuItem::Kind kind, Buffer const * buf);
269         void expandFloatListInsert(Buffer const * buf);
270         void expandFloatInsert(Buffer const * buf);
271         void expandFlexInsert(Buffer const * buf, std::string s);
272         void expandToc2(Toc const & toc_list, size_t from, size_t to, int depth);
273         void expandToc(Buffer const * buf);
274         void expandPasteRecent();
275         void expandToolbars();
276         void expandBranches(Buffer const * buf);
277         ///
278         ItemList items_;
279         ///
280         QString name_;
281 };
282
283 /// a submenu
284 class GuiPopupMenu : public QMenu
285 {
286 public:
287         ///
288         GuiPopupMenu(GuiView * gv, MenuItem const & mi, bool top_level)
289                 : QMenu(gv), top_level_menu(top_level? new Menu : 0), view(gv),
290                 name(mi.submenuname())
291         {
292                 setTitle(label(mi));
293         }
294
295         ~GuiPopupMenu() { delete top_level_menu; }
296
297         /// populates the menu or one of its submenu
298         /// This is used as a recursive function
299         void populate(QMenu * qMenu, Menu * menu);
300
301         /// Get a Menu item label from the menu backend
302         QString label(MenuItem const & mi) const;
303
304         void showEvent(QShowEvent * ev)
305         {
306                 if (top_level_menu)
307                         guiApp->menus().updateMenu(name);
308                 QMenu::showEvent(ev);
309         }
310
311         /// Only needed for top level menus.
312         Menu * top_level_menu;
313         /// our owning view
314         GuiView * view;
315         /// the name of this menu
316         QString name;
317 };
318
319 /// Helper for std::find_if
320 class MenuNamesEqual
321 {
322 public:
323         MenuNamesEqual(QString const & name) : name_(name) {}
324         bool operator()(Menu const & menu) const { return menu.name() == name_; }
325 private:
326         QString name_;
327 };
328
329
330 ///
331 typedef std::vector<Menu> MenuList;
332 ///
333 typedef MenuList::const_iterator const_iterator;
334 ///
335 typedef MenuList::iterator iterator;
336
337 /////////////////////////////////////////////////////////////////////
338 // GuiPopupMenu implementation
339 /////////////////////////////////////////////////////////////////////
340
341 void GuiPopupMenu::populate(QMenu * qMenu, Menu * menu)
342 {
343         LYXERR(Debug::GUI, "populating menu " << fromqstr(menu->name()));
344         if (menu->size() == 0) {
345                 LYXERR(Debug::GUI, "\tERROR: empty menu " << fromqstr(menu->name()));
346                 return;
347         }
348         LYXERR(Debug::GUI, " *****  menu entries " << menu->size());
349         Menu::const_iterator m = menu->begin();
350         Menu::const_iterator end = menu->end();
351         for (; m != end; ++m) {
352                 if (m->kind() == MenuItem::Separator)
353                         qMenu->addSeparator();
354                 else if (m->kind() == MenuItem::Submenu) {
355                         QMenu * subMenu = qMenu->addMenu(label(*m));
356                         populate(subMenu, m->submenu());
357                 } else {
358                         // we have a MenuItem::Command
359                         qMenu->addAction(new Action(*view, QIcon(), label(*m), m->func(),
360                                 QString()));
361                 }
362         }
363 }
364
365
366 QString GuiPopupMenu::label(MenuItem const & mi) const
367 {
368         QString label = mi.label();
369         label.replace("&", "&&");
370
371         QString shortcut = mi.shortcut();
372         if (!shortcut.isEmpty()) {
373                 int pos = label.indexOf(shortcut);
374                 if (pos != -1)
375                         //label.insert(pos, 1, char_type('&'));
376                         label.replace(pos, 0, "&");
377         }
378
379         QString const binding = mi.binding();
380         if (!binding.isEmpty())
381                 label += '\t' + binding;
382
383         return label;
384 }
385
386 /////////////////////////////////////////////////////////////////////
387 // Menu implementation
388 /////////////////////////////////////////////////////////////////////
389
390 void Menu::addWithStatusCheck(MenuItem const & i)
391 {
392         switch (i.kind()) {
393
394         case MenuItem::Command: {
395                 FuncStatus status = lyx::getStatus(i.func());
396                 if (status.unknown() || (!status.enabled() && i.optional()))
397                         break;
398                 items_.push_back(i);
399                 items_.back().status(status);
400                 break;
401         }
402
403         case MenuItem::Submenu: {
404                 if (i.submenu()) {
405                         bool enabled = false;
406                         for (const_iterator cit = i.submenu()->begin();
407                              cit != i.submenu()->end(); ++cit) {
408                                 if ((cit->kind() == MenuItem::Command
409                                      || cit->kind() == MenuItem::Submenu)
410                                     && cit->status().enabled()) {
411                                         enabled = true;
412                                         break;
413                                 }
414                         }
415                         if (enabled || !i.optional()) {
416                                 items_.push_back(i);
417                                 items_.back().status().enabled(enabled);
418                         }
419                 }
420                 else
421                         items_.push_back(i);
422                 break;
423         }
424
425         case MenuItem::Separator:
426                 if (!items_.empty() && items_.back().kind() != MenuItem::Separator)
427                         items_.push_back(i);
428                 break;
429
430         default:
431                 items_.push_back(i);
432         }
433 }
434
435
436 void Menu::read(Lexer & lex)
437 {
438         enum Menutags {
439                 md_item = 1,
440                 md_branches,
441                 md_documents,
442                 md_bookmarks,
443                 md_charstyles,
444                 md_custom,
445                 md_elements,
446                 md_endmenu,
447                 md_exportformats,
448                 md_importformats,
449                 md_lastfiles,
450                 md_optitem,
451                 md_optsubmenu,
452                 md_separator,
453                 md_submenu,
454                 md_toc,
455                 md_updateformats,
456                 md_viewformats,
457                 md_floatlistinsert,
458                 md_floatinsert,
459                 md_pasterecent,
460                 md_toolbars,
461                 md_last
462         };
463
464         struct keyword_item menutags[md_last - 1] = {
465                 { "bookmarks", md_bookmarks },
466                 { "branches", md_branches },
467                 { "charstyles", md_charstyles },
468                 { "custom", md_custom },
469                 { "documents", md_documents },
470                 { "elements", md_elements },
471                 { "end", md_endmenu },
472                 { "exportformats", md_exportformats },
473                 { "floatinsert", md_floatinsert },
474                 { "floatlistinsert", md_floatlistinsert },
475                 { "importformats", md_importformats },
476                 { "item", md_item },
477                 { "lastfiles", md_lastfiles },
478                 { "optitem", md_optitem },
479                 { "optsubmenu", md_optsubmenu },
480                 { "pasterecent", md_pasterecent },
481                 { "separator", md_separator },
482                 { "submenu", md_submenu },
483                 { "toc", md_toc },
484                 { "toolbars", md_toolbars },
485                 { "updateformats", md_updateformats },
486                 { "viewformats", md_viewformats }
487         };
488
489         lex.pushTable(menutags, md_last - 1);
490         if (lyxerr.debugging(Debug::PARSER))
491                 lex.printTable(lyxerr);
492
493         bool quit = false;
494         bool optional = false;
495
496         while (lex.isOK() && !quit) {
497                 switch (lex.lex()) {
498                 case md_optitem:
499                         optional = true;
500                         // fallback to md_item
501                 case md_item: {
502                         lex.next(true);
503                         docstring const name = translateIfPossible(lex.getDocString());
504                         lex.next(true);
505                         string const command = lex.getString();
506                         FuncRequest func = lyxaction.lookupFunc(command);
507                         add(MenuItem(MenuItem::Command, toqstr(name), func, optional));
508                         optional = false;
509                         break;
510                 }
511
512                 case md_separator:
513                         add(MenuItem(MenuItem::Separator));
514                         break;
515
516                 case md_lastfiles:
517                         add(MenuItem(MenuItem::Lastfiles));
518                         break;
519
520                 case md_charstyles:
521                         add(MenuItem(MenuItem::CharStyles));
522                         break;
523
524                 case md_custom:
525                         add(MenuItem(MenuItem::Custom));
526                         break;
527
528                 case md_elements:
529                         add(MenuItem(MenuItem::Elements));
530                         break;
531
532                 case md_documents:
533                         add(MenuItem(MenuItem::Documents));
534                         break;
535
536                 case md_bookmarks:
537                         add(MenuItem(MenuItem::Bookmarks));
538                         break;
539
540                 case md_toc:
541                         add(MenuItem(MenuItem::Toc));
542                         break;
543
544                 case md_viewformats:
545                         add(MenuItem(MenuItem::ViewFormats));
546                         break;
547
548                 case md_updateformats:
549                         add(MenuItem(MenuItem::UpdateFormats));
550                         break;
551
552                 case md_exportformats:
553                         add(MenuItem(MenuItem::ExportFormats));
554                         break;
555
556                 case md_importformats:
557                         add(MenuItem(MenuItem::ImportFormats));
558                         break;
559
560                 case md_floatlistinsert:
561                         add(MenuItem(MenuItem::FloatListInsert));
562                         break;
563
564                 case md_floatinsert:
565                         add(MenuItem(MenuItem::FloatInsert));
566                         break;
567
568                 case md_pasterecent:
569                         add(MenuItem(MenuItem::PasteRecent));
570                         break;
571
572                 case md_toolbars:
573                         add(MenuItem(MenuItem::Toolbars));
574                         break;
575
576                 case md_branches:
577                         add(MenuItem(MenuItem::Branches));
578                         break;
579
580                 case md_optsubmenu:
581                         optional = true;
582                         // fallback to md_submenu
583                 case md_submenu: {
584                         lex.next(true);
585                         docstring const mlabel = translateIfPossible(lex.getDocString());
586                         lex.next(true);
587                         docstring const mname = lex.getDocString();
588                         add(MenuItem(MenuItem::Submenu,
589                                 toqstr(mlabel), toqstr(mname), optional));
590                         optional = false;
591                         break;
592                 }
593
594                 case md_endmenu:
595                         quit = true;
596                         break;
597
598                 default:
599                         lex.printError("Menu::read: "
600                                        "Unknown menu tag: `$$Token'");
601                         break;
602                 }
603         }
604         lex.popTable();
605 }
606
607
608 MenuItem const & Menu::operator[](size_type i) const
609 {
610         return items_[i];
611 }
612
613
614 bool Menu::hasFunc(FuncRequest const & func) const
615 {
616         for (const_iterator it = begin(), et = end(); it != et; ++it)
617                 if (it->func() == func)
618                         return true;
619         return false;
620 }
621
622
623 void Menu::checkShortcuts() const
624 {
625         // This is a quadratic algorithm, but we do not care because
626         // menus are short enough
627         for (const_iterator it1 = begin(); it1 != end(); ++it1) {
628                 QString shortcut = it1->shortcut();
629                 if (shortcut.isEmpty())
630                         continue;
631                 if (!it1->label().contains(shortcut))
632                         lyxerr << "Menu warning: menu entry \""
633                                << fromqstr(it1->label())
634                                << "\" does not contain shortcut `"
635                                << fromqstr(shortcut) << "'." << endl;
636                 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
637                         if (!it2->shortcut().compare(shortcut, Qt::CaseInsensitive)) {
638                                 lyxerr << "Menu warning: menu entries "
639                                        << '"' << fromqstr(it1->fulllabel())
640                                        << "\" and \"" << fromqstr(it2->fulllabel())
641                                        << "\" share the same shortcut."
642                                        << endl;
643                         }
644                 }
645         }
646 }
647
648
649 bool Menu::searchMenu(FuncRequest const & func, vector<docstring> & names) const
650 {
651         const_iterator m = begin();
652         const_iterator m_end = end();
653         for (; m != m_end; ++m) {
654                 if (m->kind() == MenuItem::Command && m->func() == func) {
655                         names.push_back(qstring_to_ucs4(m->label()));
656                         return true;
657                 }
658                 if (m->kind() == MenuItem::Submenu) {
659                         names.push_back(qstring_to_ucs4(m->label()));
660                         Menu const & submenu = *m->submenu();
661                         if (submenu.searchMenu(func, names))
662                                 return true;
663                         names.pop_back();
664                 }
665         }
666         return false;
667 }
668
669
670 bool compareFormat(Format const * p1, Format const * p2)
671 {
672         return *p1 < *p2;
673 }
674
675
676 QString limitStringLength(docstring const & str)
677 {
678         size_t const max_item_length = 45;
679
680         if (str.size() > max_item_length)
681                 return toqstr(str.substr(0, max_item_length - 3) + "...");
682
683         return toqstr(str);
684 }
685
686
687 void Menu::expandLastfiles()
688 {
689         LastFilesSection::LastFiles const & lf = LyX::cref().session().lastFiles().lastFiles();
690         LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
691
692         int ii = 1;
693
694         for (; lfit != lf.end() && ii < 10; ++lfit, ++ii) {
695                 string const file = lfit->absFilename();
696                 QString const label = QString("%1. %2|%3").arg(ii)
697                         .arg(toqstr(makeDisplayPath(file, 30))).arg(ii);
698                 add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, file)));
699         }
700 }
701
702
703 void Menu::expandDocuments()
704 {
705         Buffer * first = theBufferList().first();
706         if (first) {
707                 Buffer * b = first;
708                 int ii = 1;
709                 
710                 // We cannot use a for loop as the buffer list cycles.
711                 do {
712                         QString label = toqstr(b->fileName().displayName(20));
713                         if (!b->isClean())
714                                 label += "*";
715                         if (ii < 10)
716                                 label = QString::number(ii) + ". " + label + '|' + QString::number(ii);
717                         add(MenuItem(MenuItem::Command, label,
718                                 FuncRequest(LFUN_BUFFER_SWITCH, b->absFileName())));
719                         
720                         b = theBufferList().next(b);
721                         ++ii;
722                 } while (b != first); 
723         } else {
724                 add(MenuItem(MenuItem::Command, qt_("No Documents Open!"),
725                            FuncRequest(LFUN_NOACTION)));
726         }
727 }
728
729
730 void Menu::expandBookmarks()
731 {
732         lyx::BookmarksSection const & bm = LyX::cref().session().bookmarks();
733
734         for (size_t i = 1; i <= bm.size(); ++i) {
735                 if (bm.isValid(i)) {
736                         string const file = bm.bookmark(i).filename.absFilename();
737                         QString const label = QString("%1. %2|%3").arg(i)
738                                 .arg(toqstr(makeDisplayPath(file, 20))).arg(i);
739                         add(MenuItem(MenuItem::Command, label,
740                                 FuncRequest(LFUN_BOOKMARK_GOTO, convert<docstring>(i))));
741                 }
742         }
743 }
744
745
746 void Menu::expandFormats(MenuItem::Kind kind, Buffer const * buf)
747 {
748         if (!buf && kind != MenuItem::ImportFormats) {
749                 add(MenuItem(MenuItem::Command,
750                                     qt_("No Document Open!"),
751                                     FuncRequest(LFUN_NOACTION)));
752                 return;
753         }
754
755         typedef vector<Format const *> Formats;
756         Formats formats;
757         kb_action action;
758
759         switch (kind) {
760         case MenuItem::ImportFormats:
761                 formats = theConverters().importableFormats();
762                 action = LFUN_BUFFER_IMPORT;
763                 break;
764         case MenuItem::ViewFormats:
765                 formats = buf->exportableFormats(true);
766                 action = LFUN_BUFFER_VIEW;
767                 break;
768         case MenuItem::UpdateFormats:
769                 formats = buf->exportableFormats(true);
770                 action = LFUN_BUFFER_UPDATE;
771                 break;
772         default:
773                 formats = buf->exportableFormats(false);
774                 action = LFUN_BUFFER_EXPORT;
775         }
776         sort(formats.begin(), formats.end(), &compareFormat);
777
778         Formats::const_iterator fit = formats.begin();
779         Formats::const_iterator end = formats.end();
780         for (; fit != end ; ++fit) {
781                 if ((*fit)->dummy())
782                         continue;
783                 QString label = toqstr((*fit)->prettyname());
784                 QString const shortcut = toqstr((*fit)->shortcut());
785
786                 switch (kind) {
787                 case MenuItem::ImportFormats:
788                         // FIXME: This is a hack, we should rather solve
789                         // FIXME: bug 2488 instead.
790                         if ((*fit)->name() == "text")
791                                 label = qt_("Plain Text");
792                         else if ((*fit)->name() == "textparagraph")
793                                 label = qt_("Plain Text, Join Lines");
794                         label += "...";
795                         break;
796                 case MenuItem::ViewFormats:
797                 case MenuItem::ExportFormats:
798                 case MenuItem::UpdateFormats:
799                         if (!(*fit)->documentFormat())
800                                 continue;
801                         break;
802                 default:
803                         BOOST_ASSERT(false);
804                         break;
805                 }
806                 // FIXME: if we had proper support for translating the
807                 // format names defined in configure.py, there would
808                 // not be a need to check whether the shortcut is
809                 // correct. If we add it uncondiitonally, it would
810                 // create useless warnings on bad shortcuts
811                 if (!shortcut.isEmpty() && label.contains(shortcut))
812                         label += '|' + shortcut;
813
814                 if (buf)
815                         addWithStatusCheck(MenuItem(MenuItem::Command, label,
816                                 FuncRequest(action, (*fit)->name())));
817                 else
818                         add(MenuItem(MenuItem::Command, label,
819                                 FuncRequest(action, (*fit)->name())));
820         }
821 }
822
823
824 void Menu::expandFloatListInsert(Buffer const * buf)
825 {
826         if (!buf) {
827                 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
828                                     FuncRequest(LFUN_NOACTION)));
829                 return;
830         }
831
832         FloatList const & floats = buf->params().documentClass().floats();
833         FloatList::const_iterator cit = floats.begin();
834         FloatList::const_iterator end = floats.end();
835         for (; cit != end; ++cit) {
836                 addWithStatusCheck(MenuItem(MenuItem::Command,
837                                     qt_(cit->second.listName()),
838                                     FuncRequest(LFUN_FLOAT_LIST,
839                                                 cit->second.type())));
840         }
841 }
842
843
844 void Menu::expandFloatInsert(Buffer const * buf)
845 {
846         if (!buf) {
847                 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
848                                     FuncRequest(LFUN_NOACTION)));
849                 return;
850         }
851
852         FloatList const & floats = buf->params().documentClass().floats();
853         FloatList::const_iterator cit = floats.begin();
854         FloatList::const_iterator end = floats.end();
855         for (; cit != end; ++cit) {
856                 // normal float
857                 QString const label = qt_(cit->second.name());
858                 addWithStatusCheck(MenuItem(MenuItem::Command, label,
859                                     FuncRequest(LFUN_FLOAT_INSERT,
860                                                 cit->second.type())));
861         }
862 }
863
864
865 void Menu::expandFlexInsert(Buffer const * buf, string s)
866 {
867         if (!buf) {
868                 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
869                                     FuncRequest(LFUN_NOACTION)));
870                 return;
871         }
872         TextClass::InsetLayouts const & insetLayouts =
873                 buf->params().documentClass().insetLayouts();
874         TextClass::InsetLayouts::const_iterator cit = insetLayouts.begin();
875         TextClass::InsetLayouts::const_iterator end = insetLayouts.end();
876         for (; cit != end; ++cit) {
877                 docstring const label = cit->first;
878                 if (cit->second.lyxtype() == s)
879                         addWithStatusCheck(MenuItem(MenuItem::Command, 
880                                 toqstr(label), FuncRequest(LFUN_FLEX_INSERT,
881                                                 label)));
882         }
883 }
884
885
886 size_t const max_number_of_items = 25;
887
888 void Menu::expandToc2(Toc const & toc_list,
889                 size_t from, size_t to, int depth)
890 {
891         int shortcut_count = 0;
892
893         // check whether depth is smaller than the smallest depth in toc.
894         int min_depth = 1000;
895         for (size_t i = from; i < to; ++i)
896                 min_depth = min(min_depth, toc_list[i].depth());
897         if (min_depth > depth)
898                 depth = min_depth;
899
900         if (to - from <= max_number_of_items) {
901                 for (size_t i = from; i < to; ++i) {
902                         QString label(4 * max(0, toc_list[i].depth() - depth), ' ');
903                         label += limitStringLength(toc_list[i].str());
904                         if (toc_list[i].depth() == depth
905                             && shortcut_count < 9) {
906                                 if (label.contains(QString::number(shortcut_count + 1)))
907                                         label += '|' + QString::number(++shortcut_count);
908                         }
909                         add(MenuItem(MenuItem::Command, label,
910                                             FuncRequest(toc_list[i].action())));
911                 }
912         } else {
913                 size_t pos = from;
914                 while (pos < to) {
915                         size_t new_pos = pos + 1;
916                         while (new_pos < to &&
917                                toc_list[new_pos].depth() > depth)
918                                 ++new_pos;
919
920                         QString label(4 * max(0, toc_list[pos].depth() - depth), ' ');
921                         label += limitStringLength(toc_list[pos].str());
922                         if (toc_list[pos].depth() == depth &&
923                             shortcut_count < 9) {
924                                 if (label.contains(QString::number(shortcut_count + 1)))
925                                         label += '|' + QString::number(++shortcut_count);
926                         }
927                         if (new_pos == pos + 1) {
928                                 add(MenuItem(MenuItem::Command,
929                                                     label, FuncRequest(toc_list[pos].action())));
930                         } else {
931                                 MenuItem item(MenuItem::Submenu, label);
932                                 item.setSubmenu(new Menu);
933                                 item.submenu()->expandToc2(toc_list, pos, new_pos, depth + 1);
934                                 add(item);
935                         }
936                         pos = new_pos;
937                 }
938         }
939 }
940
941
942 void Menu::expandToc(Buffer const * buf)
943 {
944         // To make things very cleanly, we would have to pass buf to
945         // all MenuItem constructors and to expandToc2. However, we
946         // know that all the entries in a TOC will be have status_ ==
947         // OK, so we avoid this unnecessary overhead (JMarc)
948
949         if (!buf) {
950                 add(MenuItem(MenuItem::Command, qt_("No Document Open!"),
951                                     FuncRequest(LFUN_NOACTION)));
952                 return;
953         }
954
955         Buffer* cbuf = const_cast<Buffer*>(buf);
956         cbuf->tocBackend().update();
957         cbuf->structureChanged();
958
959         // Add an entry for the master doc if this is a child doc
960         Buffer const * const master = buf->masterBuffer();
961         if (buf != master) {
962                 ParIterator const pit = par_iterator_begin(master->inset());
963                 string const arg = convert<string>(pit->id());
964                 FuncRequest f(LFUN_PARAGRAPH_GOTO, arg);
965                 add(MenuItem(MenuItem::Command, qt_("Master Document"), f));
966         }
967
968         FloatList const & floatlist = buf->params().documentClass().floats();
969         TocList const & toc_list = buf->tocBackend().tocs();
970         TocList::const_iterator cit = toc_list.begin();
971         TocList::const_iterator end = toc_list.end();
972         for (; cit != end; ++cit) {
973                 // Handle this later
974                 if (cit->first == "tableofcontents")
975                         continue;
976
977                 // All the rest is for floats
978                 Menu * submenu = new Menu;
979                 TocIterator ccit = cit->second.begin();
980                 TocIterator eend = cit->second.end();
981                 for (; ccit != eend; ++ccit) {
982                         QString const label = limitStringLength(ccit->str());
983                         submenu->add(MenuItem(MenuItem::Command, label,
984                                            FuncRequest(ccit->action())));
985                 }
986                 string const & floatName = floatlist.getType(cit->first).listName();
987                 QString label;
988                 if (!floatName.empty())
989                         label = qt_(floatName);
990                 // BUG3633: listings is not a proper float so its name
991                 // is not shown in floatlist.
992                 else if (cit->first == "equation")
993                         label = qt_("List of Equations");
994                 else if (cit->first == "index")
995                         label = qt_("List of Indexes");
996                 else if (cit->first == "listing")
997                         label = qt_("List of Listings");
998                 else if (cit->first == "marginalnote")
999                         label = qt_("List of Marginal notes");
1000                 else if (cit->first == "note")
1001                         label = qt_("List of Notes");
1002                 else if (cit->first == "footnote")
1003                         label = qt_("List of Foot notes");
1004                 else if (cit->first == "label")
1005                         label = qt_("Labels and References");
1006                 else if (cit->first == "citation")
1007                         label = qt_("List of Citations");
1008                 // this should not happen now, but if something else like
1009                 // listings is added later, this can avoid an empty menu name.
1010                 else
1011                         label = qt_("Other floats");
1012                 MenuItem item(MenuItem::Submenu, label);
1013                 item.setSubmenu(submenu);
1014                 add(item);
1015         }
1016
1017         // Handle normal TOC
1018         cit = toc_list.find("tableofcontents");
1019         if (cit == end) {
1020                 addWithStatusCheck(MenuItem(MenuItem::Command,
1021                                     qt_("No Table of contents"),
1022                                     FuncRequest()));
1023         } else {
1024                 expandToc2(cit->second, 0, cit->second.size(), 0);
1025         }
1026 }
1027
1028
1029 void Menu::expandPasteRecent()
1030 {
1031         vector<docstring> const sel = cap::availableSelections();
1032
1033         vector<docstring>::const_iterator cit = sel.begin();
1034         vector<docstring>::const_iterator end = sel.end();
1035
1036         for (unsigned int index = 0; cit != end; ++cit, ++index) {
1037                 add(MenuItem(MenuItem::Command, toqstr(*cit),
1038                                     FuncRequest(LFUN_PASTE, convert<string>(index))));
1039         }
1040 }
1041
1042
1043 void Menu::expandToolbars()
1044 {
1045         //
1046         // extracts the toolbars from the backend
1047         ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
1048         ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
1049
1050         for (; cit != end; ++cit) {
1051                 QString label = qt_(cit->gui_name);
1052                 // frontends are not supposed to turn on/off toolbars,
1053                 // if they cannot update ToolbarBackend::flags. That
1054                 // is to say, ToolbarsBackend::flags should reflect
1055                 // the true state of toolbars.
1056                 //
1057                 // menu is displayed as
1058                 //       on/off review
1059                 // and
1060                 //              review (auto)
1061                 // in the case of auto.
1062                 if (cit->flags & ToolbarInfo::AUTO)
1063                         label += qt_(" (auto)");
1064                 add(MenuItem(MenuItem::Command, label,
1065                                     FuncRequest(LFUN_TOOLBAR_TOGGLE, cit->name + " allowauto")));
1066         }
1067 }
1068
1069
1070 void Menu::expandBranches(Buffer const * buf)
1071 {
1072         if (!buf) {
1073                 add(MenuItem(MenuItem::Command,
1074                                     qt_("No Document Open!"),
1075                                     FuncRequest(LFUN_NOACTION)));
1076                 return;
1077         }
1078
1079         BufferParams const & params = buf->masterBuffer()->params();
1080         if (params.branchlist().empty()) {
1081                 add(MenuItem(MenuItem::Command,
1082                                     qt_("No Branch in Document!"),
1083                                     FuncRequest(LFUN_NOACTION)));
1084                 return;
1085         }
1086
1087         BranchList::const_iterator cit = params.branchlist().begin();
1088         BranchList::const_iterator end = params.branchlist().end();
1089
1090         for (int ii = 1; cit != end; ++cit, ++ii) {
1091                 docstring label = cit->getBranch();
1092                 if (ii < 10)
1093                         label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
1094                 addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(label),
1095                                     FuncRequest(LFUN_BRANCH_INSERT,
1096                                                 cit->getBranch())));
1097         }
1098 }
1099
1100 } // namespace anon
1101
1102
1103 struct Menus::Impl {
1104         ///
1105         bool hasMenu(QString const &) const;
1106         ///
1107         Menu & getMenu(QString const &);
1108         ///
1109         Menu const & getMenu(QString const &) const;
1110
1111         /// Expands some special entries of the menu
1112         /** The entries with the following kind are expanded to a
1113             sequence of Command MenuItems: Lastfiles, Documents,
1114             ViewFormats, ExportFormats, UpdateFormats, Branches
1115         */
1116         void expand(Menu const & frommenu, Menu & tomenu,
1117                     Buffer const *) const;
1118
1119         /// Initialize specific MACOS X menubar
1120         void macxMenuBarInit(GuiView * view);
1121
1122         /// Mac special menu.
1123         /** This defines a menu whose entries list the FuncRequests
1124             that will be removed by expand() in other menus. This is
1125             used by the Qt/Mac code
1126         */
1127         Menu specialmenu_;
1128
1129         ///
1130         MenuList menulist_;
1131         ///
1132         Menu menubar_;
1133
1134         typedef QHash<QString, GuiPopupMenu *> NameMap;
1135
1136         /// name to menu for \c menu() method.
1137         NameMap name_map_;
1138 };
1139
1140
1141 /////////////////////////////////////////////////////////////////////
1142 // Menus::Impl implementation
1143 /////////////////////////////////////////////////////////////////////
1144
1145 /*
1146   Here is what the Qt documentation says about how a menubar is chosen:
1147
1148      1) If the window has a QMenuBar then it is used. 2) If the window
1149      is a modal then its menubar is used. If no menubar is specified
1150      then a default menubar is used (as documented below) 3) If the
1151      window has no parent then the default menubar is used (as
1152      documented below).
1153
1154      The above 3 steps are applied all the way up the parent window
1155      chain until one of the above are satisifed. If all else fails a
1156      default menubar will be created, the default menubar on Qt/Mac is
1157      an empty menubar, however you can create a different default
1158      menubar by creating a parentless QMenuBar, the first one created
1159      will thus be designated the default menubar, and will be used
1160      whenever a default menubar is needed.
1161
1162   Thus, for Qt/Mac, we add the menus to a free standing menubar, so
1163   that this menubar will be used also when one of LyX' dialogs has
1164   focus. (JMarc)
1165 */
1166 void Menus::Impl::macxMenuBarInit(GuiView * view)
1167 {
1168         // The Mac menubar initialisation must be done only once!
1169         static bool done = false;
1170         if (done)
1171                 return;
1172         done = true;
1173
1174         /* Since Qt 4.2, the qt/mac menu code has special code for
1175            specifying the role of a menu entry. However, it does not
1176            work very well with our scheme of creating menus on demand,
1177            and therefore we need to put these entries in a special
1178            invisible menu. (JMarc)
1179         */
1180
1181         /* The entries of our special mac menu. If we add support for
1182          * special entries in Menus, we could imagine something
1183          * like
1184          *    SpecialItem About " "About LyX" "dialog-show aboutlyx"
1185          * and therefore avoid hardcoding. I am not sure it is worth
1186          * the hassle, though. (JMarc)
1187          */
1188         struct MacMenuEntry {
1189                 kb_action action;
1190                 char const * arg;
1191                 char const * label;
1192                 QAction::MenuRole role;
1193         };
1194
1195         MacMenuEntry entries[] = {
1196                 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
1197                  QAction::AboutRole},
1198                 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
1199                  QAction::PreferencesRole},
1200                 {LFUN_RECONFIGURE, "", "Reconfigure",
1201                  QAction::ApplicationSpecificRole},
1202                 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
1203         };
1204         const size_t num_entries = sizeof(entries) / sizeof(entries[0]);
1205
1206         // the special menu for Menus.
1207         for (size_t i = 0 ; i < num_entries ; ++i) {
1208                 FuncRequest const func(entries[i].action,
1209                                        from_utf8(entries[i].arg));
1210                 specialmenu_.add(MenuItem(MenuItem::Command, entries[i].label, func));
1211         }
1212
1213         // add the entries to a QMenu that will eventually be empty
1214         // and therefore invisible.
1215         QMenu * qMenu = view->menuBar()->addMenu("special");
1216         Menu::const_iterator cit = specialmenu_.begin();
1217         Menu::const_iterator end = specialmenu_.end();
1218         for (size_t i = 0 ; cit != end ; ++cit, ++i) {
1219                 Action * action = new Action(*view, QIcon(), cit->label(),
1220                                              cit->func(), QString());
1221                 action->setMenuRole(entries[i].role);
1222                 qMenu->addAction(action);
1223         }
1224 }
1225
1226
1227 void Menus::Impl::expand(Menu const & frommenu, Menu & tomenu,
1228                          Buffer const * buf) const
1229 {
1230         if (!tomenu.empty())
1231                 tomenu.clear();
1232
1233         for (Menu::const_iterator cit = frommenu.begin();
1234              cit != frommenu.end() ; ++cit) {
1235                 switch (cit->kind()) {
1236                 case MenuItem::Lastfiles:
1237                         tomenu.expandLastfiles();
1238                         break;
1239
1240                 case MenuItem::Documents:
1241                         tomenu.expandDocuments();
1242                         break;
1243
1244                 case MenuItem::Bookmarks:
1245                         tomenu.expandBookmarks();
1246                         break;
1247
1248                 case MenuItem::ImportFormats:
1249                 case MenuItem::ViewFormats:
1250                 case MenuItem::UpdateFormats:
1251                 case MenuItem::ExportFormats:
1252                         tomenu.expandFormats(cit->kind(), buf);
1253                         break;
1254
1255                 case MenuItem::CharStyles:
1256                         tomenu.expandFlexInsert(buf, "charstyle");
1257                         break;
1258
1259                 case MenuItem::Custom:
1260                         tomenu.expandFlexInsert(buf, "custom");
1261                         break;
1262
1263                 case MenuItem::Elements:
1264                         tomenu.expandFlexInsert(buf, "element");
1265                         break;
1266
1267                 case MenuItem::FloatListInsert:
1268                         tomenu.expandFloatListInsert(buf);
1269                         break;
1270
1271                 case MenuItem::FloatInsert:
1272                         tomenu.expandFloatInsert(buf);
1273                         break;
1274
1275                 case MenuItem::PasteRecent:
1276                         tomenu.expandPasteRecent();
1277                         break;
1278
1279                 case MenuItem::Toolbars:
1280                         tomenu.expandToolbars();
1281                         break;
1282
1283                 case MenuItem::Branches:
1284                         tomenu.expandBranches(buf);
1285                         break;
1286
1287                 case MenuItem::Toc:
1288                         tomenu.expandToc(buf);
1289                         break;
1290
1291                 case MenuItem::Submenu: {
1292                         MenuItem item(*cit);
1293                         item.setSubmenu(new Menu(cit->submenuname()));
1294                         expand(getMenu(cit->submenuname()), *item.submenu(), buf);
1295                         tomenu.addWithStatusCheck(item);
1296                 }
1297                 break;
1298
1299                 case MenuItem::Separator:
1300                         tomenu.addWithStatusCheck(*cit);
1301                         break;
1302
1303                 case MenuItem::Command:
1304                         if (!specialmenu_.hasFunc(cit->func()))
1305                                 tomenu.addWithStatusCheck(*cit);
1306                 }
1307         }
1308
1309         // we do not want the menu to end with a separator
1310         if (!tomenu.empty() && tomenu.items_.back().kind() == MenuItem::Separator)
1311                 tomenu.items_.pop_back();
1312
1313         // Check whether the shortcuts are unique
1314         tomenu.checkShortcuts();
1315 }
1316
1317
1318 bool Menus::Impl::hasMenu(QString const & name) const
1319 {
1320         return find_if(menulist_.begin(), menulist_.end(),
1321                 MenuNamesEqual(name)) != menulist_.end();
1322 }
1323
1324
1325 Menu const & Menus::Impl::getMenu(QString const & name) const
1326 {
1327         const_iterator cit = find_if(menulist_.begin(), menulist_.end(),
1328                 MenuNamesEqual(name));
1329         if (cit == menulist_.end())
1330                 lyxerr << "No submenu named " << fromqstr(name) << endl;
1331         BOOST_ASSERT(cit != menulist_.end());
1332         return (*cit);
1333 }
1334
1335
1336 Menu & Menus::Impl::getMenu(QString const & name)
1337 {
1338         iterator it = find_if(menulist_.begin(), menulist_.end(),
1339                 MenuNamesEqual(name));
1340         if (it == menulist_.end())
1341                 lyxerr << "No submenu named " << fromqstr(name) << endl;
1342         BOOST_ASSERT(it != menulist_.end());
1343         return (*it);
1344 }
1345
1346 /////////////////////////////////////////////////////////////////////
1347 // Menus implementation
1348 /////////////////////////////////////////////////////////////////////
1349
1350 Menus::Menus(): d(new Impl) {}
1351
1352
1353 void Menus::read(Lexer & lex)
1354 {
1355         enum Menutags {
1356                 md_menu = 1,
1357                 md_menubar,
1358                 md_endmenuset,
1359                 md_last
1360         };
1361
1362         struct keyword_item menutags[md_last - 1] = {
1363                 { "end", md_endmenuset },
1364                 { "menu", md_menu },
1365                 { "menubar", md_menubar }
1366         };
1367
1368         //consistency check
1369         if (compare_ascii_no_case(lex.getString(), "menuset")) {
1370                 lyxerr << "Menubackend::read: ERROR wrong token:`"
1371                        << lex.getString() << '\'' << endl;
1372         }
1373
1374         lex.pushTable(menutags, md_last - 1);
1375         if (lyxerr.debugging(Debug::PARSER))
1376                 lex.printTable(lyxerr);
1377
1378         bool quit = false;
1379
1380         while (lex.isOK() && !quit) {
1381                 switch (lex.lex()) {
1382                 case md_menubar:
1383                         d->menubar_.read(lex);
1384                         break;
1385                 case md_menu: {
1386                         lex.next(true);
1387                         QString const name = toqstr(lex.getDocString());
1388                         if (d->hasMenu(name))
1389                                 d->getMenu(name).read(lex);
1390                         else {
1391                                 Menu menu(name);
1392                                 menu.read(lex);
1393                                 d->menulist_.push_back(menu);
1394                         }
1395                         break;
1396                 }
1397                 case md_endmenuset:
1398                         quit = true;
1399                         break;
1400                 default:
1401                         lex.printError("menubackend::read: "
1402                                        "Unknown menu tag: `$$Token'");
1403                         break;
1404                 }
1405         }
1406         lex.popTable();
1407 }
1408
1409
1410 bool Menus::searchMenu(FuncRequest const & func,
1411         vector<docstring> & names) const
1412 {
1413         return d->menubar_.searchMenu(func, names);
1414 }
1415
1416
1417 void Menus::fillMenuBar(GuiView * view)
1418 {
1419         // Clear all menubar contents before filling it.
1420         view->menuBar()->clear();
1421         
1422 #ifdef Q_WS_MACX
1423         // setup special mac specific menu item
1424         d->macxMenuBarInit(view);
1425 #endif
1426
1427         LYXERR(Debug::GUI, "populating menu bar" << fromqstr(d->menubar_.name()));
1428
1429         if (d->menubar_.size() == 0) {
1430                 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
1431                         << fromqstr(d->menubar_.name()));
1432                 return;
1433         }
1434         else {
1435                 LYXERR(Debug::GUI, "menu bar entries "
1436                         << d->menubar_.size());
1437         }
1438
1439         Menu menu;
1440         d->expand(d->menubar_, menu, view->buffer());
1441
1442         Menu::const_iterator m = menu.begin();
1443         Menu::const_iterator end = menu.end();
1444
1445         for (; m != end; ++m) {
1446
1447                 if (m->kind() != MenuItem::Submenu) {
1448                         LYXERR(Debug::GUI, "\tERROR: not a submenu " << fromqstr(m->label()));
1449                         continue;
1450                 }
1451
1452                 LYXERR(Debug::GUI, "menu bar item " << fromqstr(m->label())
1453                         << " is a submenu named " << fromqstr(m->submenuname()));
1454
1455                 QString name = m->submenuname();
1456                 if (!d->hasMenu(name)) {
1457                         LYXERR(Debug::GUI, "\tERROR: " << fromqstr(name)
1458                                 << " submenu has no menu!");
1459                         continue;
1460                 }
1461
1462                 GuiPopupMenu * qmenu = new GuiPopupMenu(view, *m, true);
1463                 view->menuBar()->addMenu(qmenu);
1464
1465                 d->name_map_[name] = qmenu;
1466         }
1467 }
1468
1469
1470 void Menus::updateMenu(QString const & name)
1471 {
1472         GuiPopupMenu * qmenu = d->name_map_[name];
1473         LYXERR(Debug::GUI, "GuiPopupMenu::updateView()"
1474                 << "\tTriggered menu: " << fromqstr(qmenu->name));
1475         qmenu->clear();
1476
1477         if (qmenu->name.isEmpty())
1478                 return;
1479
1480         // Here, We make sure that theLyXFunc points to the correct LyXView.
1481         theLyXFunc().setLyXView(qmenu->view);
1482
1483         Menu const & fromLyxMenu = d->getMenu(qmenu->name);
1484         d->expand(fromLyxMenu, *qmenu->top_level_menu, qmenu->view->buffer());
1485
1486         if (!d->hasMenu(qmenu->top_level_menu->name())) {
1487                 LYXERR(Debug::GUI, "\tWARNING: menu seems empty"
1488                         << fromqstr(qmenu->top_level_menu->name()));
1489         }
1490         qmenu->populate(qmenu, qmenu->top_level_menu);
1491 }
1492
1493
1494 QMenu * Menus::menu(QString const & name)
1495 {
1496         LYXERR(Debug::GUI, "Context menu requested: " << fromqstr(name));
1497         GuiPopupMenu * menu = d->name_map_.value(name, 0);
1498         if (!menu)
1499                 LYXERR0("resquested context menu not found: " << fromqstr(name));
1500         return menu;
1501 }
1502
1503 } // namespace frontend
1504 } // namespace lyx
1505
1506 #include "Menus_moc.cpp"