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