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