]> git.lyx.org Git - lyx.git/blob - src/MenuBackend.C
the Qt/Mac merge menu patch; this is actually different from macmove8, so I hope...
[lyx.git] / src / MenuBackend.C
1 /**
2  * \file MenuBackend.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author André Pönitz
10  * \author Dekel Tsur
11  * \author Martin Vermeer
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "MenuBackend.h"
19
20 #include "BranchList.h"
21 #include "buffer.h"
22 #include "bufferlist.h"
23 #include "bufferparams.h"
24 #include "CutAndPaste.h"
25 #include "debug.h"
26 #include "exporter.h"
27 #include "Floating.h"
28 #include "FloatList.h"
29 #include "format.h"
30 #include "gettext.h"
31 #include "importer.h"
32 #include "kbmap.h"
33 #include "lastfiles.h"
34 #include "LyXAction.h"
35 #include "lyx_main.h" // for lastfiles
36 #include "lyxfunc.h"
37 #include "lyxlex.h"
38 #include "toc.h"
39
40 #include "frontends/LyXView.h"
41
42 #include "support/filetools.h"
43 #include "support/lstrings.h"
44 #include "support/tostr.h"
45
46 #include <boost/bind.hpp>
47
48 #include <algorithm>
49
50 using lyx::support::compare_ascii_no_case;
51 using lyx::support::contains;
52 using lyx::support::MakeDisplayPath;
53 using lyx::support::token;
54
55 using boost::bind;
56
57 using std::auto_ptr;
58 using std::endl;
59 using std::equal_to;
60 using std::find_if;
61 using std::max;
62 using std::sort;
63 using std::string;
64 using std::vector;
65
66
67 extern BufferList bufferlist;
68 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
69
70 namespace {
71
72 class MenuNamesEqual : public std::unary_function<Menu, bool> {
73 public:
74         MenuNamesEqual(string const & name)
75                 : name_(name) {}
76         bool operator()(Menu const & menu) const
77         {
78                 return menu.name() == name_;
79         }
80 private:
81         string name_;
82 };
83
84 } // namespace anon
85
86
87 // This is the global menu definition
88 MenuBackend menubackend;
89
90
91 MenuItem::MenuItem(Kind kind)
92         : kind_(kind), optional_(false)
93 {}
94
95
96 MenuItem::MenuItem(Kind kind, string const & label,
97                    string const & submenu, bool optional)
98         : kind_(kind), label_(label),
99           submenuname_(submenu), optional_(optional)
100 {
101         BOOST_ASSERT(kind == Submenu);
102 }
103
104
105 MenuItem::MenuItem(Kind kind, string const & label,
106                    FuncRequest const & func, bool optional)
107         : kind_(kind), label_(label), func_(func), optional_(optional)
108 {}
109
110
111 MenuItem::~MenuItem()
112 {}
113
114
115 void MenuItem::submenu(Menu * menu)
116 {
117         submenu_.reset(menu);
118 }
119
120
121 string const MenuItem::label() const
122 {
123         return token(label_, '|', 0);
124 }
125
126
127 string const MenuItem::shortcut() const
128 {
129         return token(label_, '|', 1);
130 }
131
132
133 string const MenuItem::binding() const
134 {
135         if (kind_ != Command)
136                 return string();
137
138         // Get the keys bound to this action, but keep only the
139         // first one later
140         kb_keymap::Bindings bindings = toplevel_keymap->findbindings(func_);
141
142         if (bindings.size()) {
143                 return bindings.begin()->print();
144         } else {
145                 lyxerr[Debug::KBMAP]
146                         << "No binding for "
147                         << lyxaction.getActionName(func_.action)
148                         << '(' << func_.argument << ')' << endl;
149                 return string();
150         }
151
152 }
153
154
155 Menu & Menu::add(MenuItem const & i, LyXView const * view)
156 {
157         if (!view) {
158                 items_.push_back(i);
159                 return *this;
160         }
161
162         switch (i.kind()) {
163
164         case MenuItem::Command: {
165                 FuncStatus status =
166                         view->getLyXFunc().getStatus(i.func());
167                 if (status.unknown()
168                     || (!status.enabled() && i.optional()))
169                         break;
170                 items_.push_back(i);
171                 items_.back().status(status);
172                 break;
173         }
174
175         case MenuItem::Submenu: {
176                 if (i.submenu()) {
177                         bool enabled = false;
178                         for (const_iterator cit = i.submenu()->begin();
179                              cit != i.submenu()->end(); ++cit) {
180                                 if ((cit->kind() == MenuItem::Command
181                                      || cit->kind() == MenuItem::Submenu)
182                                     && cit->status().enabled()) {
183                                         enabled = true;
184                                         break;
185                                 }
186                         }
187                         if (enabled || !i.optional()) {
188                                 items_.push_back(i);
189                                 items_.back().status().enabled(enabled);
190                         }
191                 }
192                 else
193                         items_.push_back(i);
194                 break;
195         }
196
197         case MenuItem::Separator:
198                 if (!items_.empty()
199                     && items_.back().kind() != MenuItem::Separator)
200                         items_.push_back(i);
201                 break;
202
203         default:
204                 items_.push_back(i);
205         }
206
207         return *this;
208 }
209
210
211 Menu & Menu::read(LyXLex & lex)
212 {
213         enum Menutags {
214                 md_item = 1,
215                 md_branches,
216                 md_documents,
217                 md_charstyles,
218                 md_endmenu,
219                 md_exportformats,
220                 md_importformats,
221                 md_lastfiles,
222                 md_optitem,
223                 md_optsubmenu,
224                 md_separator,
225                 md_submenu,
226                 md_toc,
227                 md_updateformats,
228                 md_viewformats,
229                 md_floatlistinsert,
230                 md_floatinsert,
231                 md_pasterecent,
232                 md_last
233         };
234
235         struct keyword_item menutags[md_last - 1] = {
236                 { "branches", md_branches },
237                 { "charstyles", md_charstyles },
238                 { "documents", md_documents },
239                 { "end", md_endmenu },
240                 { "exportformats", md_exportformats },
241                 { "floatinsert", md_floatinsert },
242                 { "floatlistinsert", md_floatlistinsert },
243                 { "importformats", md_importformats },
244                 { "item", md_item },
245                 { "lastfiles", md_lastfiles },
246                 { "optitem", md_optitem },
247                 { "optsubmenu", md_optsubmenu },
248                 { "pasterecent", md_pasterecent },
249                 { "separator", md_separator },
250                 { "submenu", md_submenu },
251                 { "toc", md_toc },
252                 { "updateformats", md_updateformats },
253                 { "viewformats", md_viewformats }
254         };
255
256         lex.pushTable(menutags, md_last - 1);
257         if (lyxerr.debugging(Debug::PARSER))
258                 lex.printTable(lyxerr);
259
260         bool quit = false;
261         bool optional = false;
262
263         while (lex.isOK() && !quit) {
264                 switch (lex.lex()) {
265                 case md_optitem:
266                         optional = true;
267                         // fallback to md_item
268                 case md_item: {
269                         lex.next(true);
270                         string const name = _(lex.getString());
271                         lex.next(true);
272                         string const command = lex.getString();
273                         FuncRequest func = lyxaction.lookupFunc(command);
274                         add(MenuItem(MenuItem::Command, name, func, optional));
275                         optional = false;
276                         break;
277                 }
278
279                 case md_separator:
280                         add(MenuItem(MenuItem::Separator));
281                         break;
282
283                 case md_lastfiles:
284                         add(MenuItem(MenuItem::Lastfiles));
285                         break;
286
287                 case md_charstyles:
288                         add(MenuItem(MenuItem::CharStyles));
289                         break;
290
291                 case md_documents:
292                         add(MenuItem(MenuItem::Documents));
293                         break;
294
295                 case md_toc:
296                         add(MenuItem(MenuItem::Toc));
297                         break;
298
299                 case md_viewformats:
300                         add(MenuItem(MenuItem::ViewFormats));
301                         break;
302
303                 case md_updateformats:
304                         add(MenuItem(MenuItem::UpdateFormats));
305                         break;
306
307                 case md_exportformats:
308                         add(MenuItem(MenuItem::ExportFormats));
309                         break;
310
311                 case md_importformats:
312                         add(MenuItem(MenuItem::ImportFormats));
313                         break;
314
315                 case md_floatlistinsert:
316                         add(MenuItem(MenuItem::FloatListInsert));
317                         break;
318
319                 case md_floatinsert:
320                         add(MenuItem(MenuItem::FloatInsert));
321                         break;
322
323                 case md_pasterecent:
324                         add(MenuItem(MenuItem::PasteRecent));
325                         break;
326
327                 case md_branches:
328                         add(MenuItem(MenuItem::Branches));
329                         break;
330
331                 case md_optsubmenu:
332                         optional = true;
333                         // fallback to md_submenu
334                 case md_submenu: {
335                         lex.next(true);
336                         string const mlabel = _(lex.getString());
337                         lex.next(true);
338                         string const mname = lex.getString();
339                         add(MenuItem(MenuItem::Submenu, mlabel, mname,
340                                      optional));
341                         optional = false;
342                         break;
343                 }
344
345                 case md_endmenu:
346                         quit = true;
347                         break;
348
349                 default:
350                         lex.printError("Menu::read: "
351                                        "Unknown menu tag: `$$Token'");
352                         break;
353                 }
354         }
355         lex.popTable();
356         return *this;
357 }
358
359
360 MenuItem const & Menu::operator[](size_type i) const
361 {
362         return items_[i];
363 }
364
365
366 bool Menu::hasFunc(FuncRequest const & func) const
367 {
368         return find_if(begin(), end(),
369                        bind(std::equal_to<FuncRequest>(),
370                             bind(&MenuItem::func, _1),
371                             func)) != end();
372 }
373
374 void Menu::checkShortcuts() const
375 {
376         // This is a quadratic algorithm, but we do not care because
377         // menus are short enough
378         for (const_iterator it1 = begin(); it1 != end(); ++it1) {
379                 string shortcut = it1->shortcut();
380                 if (shortcut.empty())
381                         continue;
382                 if (!contains(it1->label(), shortcut))
383                         lyxerr << "Menu warning: menu entry \""
384                                << it1->label()
385                                << "\" does not contain shortcut `"
386                                << shortcut << "'." << endl;
387                 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
388                         if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
389                                 lyxerr << "Menu warning: menu entries "
390                                        << '"' << it1->fulllabel()
391                                        << "\" and \"" << it2->fulllabel()
392                                        << "\" share the same shortcut."
393                                        << endl;
394                         }
395                 }
396         }
397 }
398
399
400 void MenuBackend::specialMenu(string const &name)
401 {
402         if (hasMenu(name)) 
403                 specialmenu_ = &getMenu(name);
404 }
405
406
407 namespace {
408
409 class compare_format {
410 public:
411         bool operator()(Format const * p1, Format const * p2) {
412                 return *p1 < *p2;
413         }
414 };
415
416 string const limit_string_length(string const & str)
417 {
418         string::size_type const max_item_length = 45;
419
420         if (str.size() > max_item_length)
421                 return str.substr(0, max_item_length - 3) + "...";
422         else
423                 return str;
424 }
425
426
427 void expandLastfiles(Menu & tomenu, LyXView const * view)
428 {
429         LastFiles const & lastfiles = LyX::cref().lastfiles();
430
431         int ii = 1;
432         LastFiles::const_iterator lfit = lastfiles.begin();
433         LastFiles::const_iterator end = lastfiles.end();
434
435         for (; lfit != end && ii < 10; ++lfit, ++ii) {
436                 string const label = tostr(ii) + ". "
437                         + MakeDisplayPath((*lfit), 30)
438                         + '|' + tostr(ii);
439                 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
440         }
441 }
442
443 void expandDocuments(Menu & tomenu, LyXView const * view)
444 {
445         typedef vector<string> Strings;
446         Strings const names = bufferlist.getFileNames();
447
448         if (names.empty()) {
449                 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
450                                     FuncRequest(LFUN_NOACTION)), view);
451                 return;
452         }
453
454         int ii = 1;
455         Strings::const_iterator docit = names.begin();
456         Strings::const_iterator end = names.end();
457         for (; docit != end; ++docit, ++ii) {
458                 string label = MakeDisplayPath(*docit, 20);
459                 if (ii < 10)
460                         label = tostr(ii) + ". " + label + '|' + tostr(ii);
461                 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_SWITCHBUFFER, *docit)), view);
462         }
463 }
464
465
466 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
467 {
468         if (!view->buffer() && kind != MenuItem::ImportFormats) {
469                 tomenu.add(MenuItem(MenuItem::Command,
470                                     _("No Documents Open!"),
471                                     FuncRequest(LFUN_NOACTION)),
472                                     view);
473                 return;
474         }
475
476         typedef vector<Format const *> Formats;
477         Formats formats;
478         kb_action action;
479
480         switch (kind) {
481         case MenuItem::ImportFormats:
482                 formats = Importer::GetImportableFormats();
483                 action = LFUN_IMPORT;
484                 break;
485         case MenuItem::ViewFormats:
486                 formats = Exporter::GetExportableFormats(*view->buffer(), true);
487                 action = LFUN_PREVIEW;
488                 break;
489         case MenuItem::UpdateFormats:
490                 formats = Exporter::GetExportableFormats(*view->buffer(), true);
491                 action = LFUN_UPDATE;
492                 break;
493         default:
494                 formats = Exporter::GetExportableFormats(*view->buffer(), false);
495                 action = LFUN_EXPORT;
496         }
497         sort(formats.begin(), formats.end(), compare_format());
498
499         Formats::const_iterator fit = formats.begin();
500         Formats::const_iterator end = formats.end();
501         for (; fit != end ; ++fit) {
502                 if ((*fit)->dummy())
503                         continue;
504                 string label = (*fit)->prettyname();
505                 // we need to hide the default graphic export formats
506                 // from the external menu, because we need them only
507                 // for the internal lyx-view and external latex run
508                 if (label == "EPS" || label == "XPM" || label == "PNG")
509                         continue;
510
511                 if (kind == MenuItem::ImportFormats) {
512                         if ((*fit)->name() == "text")
513                                 label = _("ASCII text as lines");
514                         else if ((*fit)->name() == "textparagraph")
515                                 label = _("ASCII text as paragraphs");
516                         label += "...";
517                 }
518                 if (!(*fit)->shortcut().empty())
519                         label += '|' + (*fit)->shortcut();
520
521                 tomenu.add(MenuItem(MenuItem::Command, label,
522                                     FuncRequest(action, (*fit)->name())),
523                            view);
524         }
525 }
526
527
528 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
529 {
530         if (!view->buffer()) {
531                 tomenu.add(MenuItem(MenuItem::Command,
532                                     _("No Documents Open!"),
533                                     FuncRequest(LFUN_NOACTION)),
534                            view);
535                 return;
536         }
537
538         FloatList const & floats =
539                 view->buffer()->params().getLyXTextClass().floats();
540         FloatList::const_iterator cit = floats.begin();
541         FloatList::const_iterator end = floats.end();
542         for (; cit != end; ++cit) {
543                 tomenu.add(MenuItem(MenuItem::Command,
544                                     _(cit->second.listName()),
545                                     FuncRequest(LFUN_FLOAT_LIST,
546                                                 cit->second.type())),
547                            view);
548         }
549 }
550
551
552 void expandFloatInsert(Menu & tomenu, LyXView const * view)
553 {
554         if (!view->buffer()) {
555                 tomenu.add(MenuItem(MenuItem::Command,
556                                     _("No Documents Open!"),
557                                     FuncRequest(LFUN_NOACTION)),
558                            view);
559                 return;
560         }
561
562         FloatList const & floats =
563                 view->buffer()->params().getLyXTextClass().floats();
564         FloatList::const_iterator cit = floats.begin();
565         FloatList::const_iterator end = floats.end();
566         for (; cit != end; ++cit) {
567                 // normal float
568                 string const label = _(cit->second.name());
569                 tomenu.add(MenuItem(MenuItem::Command, label,
570                                     FuncRequest(LFUN_INSET_FLOAT,
571                                                 cit->second.type())),
572                            view);
573         }
574 }
575
576
577 void expandCharStyleInsert(Menu & tomenu, LyXView const * view)
578 {
579         if (!view->buffer()) {
580                 tomenu.add(MenuItem(MenuItem::Command,
581                                     _("No Documents Open!"),
582                                     FuncRequest(LFUN_NOACTION)),
583                            view);
584                 return;
585         }
586         CharStyles & charstyles =
587                 view->buffer()->params().getLyXTextClass().charstyles();
588         CharStyles::iterator cit = charstyles.begin();
589         CharStyles::iterator end = charstyles.end();
590         for (; cit != end; ++cit) {
591                 string const label = cit->name;
592                 tomenu.add(MenuItem(MenuItem::Command, label,
593                                     FuncRequest(LFUN_INSERT_CHARSTYLE,
594                                                 cit->name)), view);
595         }
596 }
597
598
599 Menu::size_type const max_number_of_items = 25;
600
601 void expandToc2(Menu & tomenu,
602                 lyx::toc::Toc const & toc_list,
603                 lyx::toc::Toc::size_type from,
604                 lyx::toc::Toc::size_type to, int depth)
605 {
606         int shortcut_count = 0;
607         if (to - from <= max_number_of_items) {
608                 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
609                         string label(4 * max(0, toc_list[i].depth - depth),' ');
610                         label += limit_string_length(toc_list[i].str);
611                         if (toc_list[i].depth == depth
612                             && shortcut_count < 9) {
613                                 if (label.find(tostr(shortcut_count + 1)) != string::npos)
614                                         label += '|' + tostr(++shortcut_count);
615                         }
616                         tomenu.add(MenuItem(MenuItem::Command, label,
617                                             FuncRequest(toc_list[i].action())));
618                 }
619         } else {
620                 lyx::toc::Toc::size_type pos = from;
621                 while (pos < to) {
622                         lyx::toc::Toc::size_type new_pos = pos + 1;
623                         while (new_pos < to &&
624                                toc_list[new_pos].depth > depth)
625                                 ++new_pos;
626
627                         string label(4 * max(0, toc_list[pos].depth - depth), ' ');
628                         label += limit_string_length(toc_list[pos].str);
629                         if (toc_list[pos].depth == depth &&
630                             shortcut_count < 9) {
631                                 if (label.find(tostr(shortcut_count + 1)) != string::npos)
632                                         label += '|' + tostr(++shortcut_count);
633                                 }
634                         if (new_pos == pos + 1) {
635                                 tomenu.add(MenuItem(MenuItem::Command,
636                                                     label, FuncRequest(toc_list[pos].action())));
637                         } else {
638                                 MenuItem item(MenuItem::Submenu, label);
639                                 item.submenu(new Menu);
640                                 expandToc2(*item.submenu(),
641                                            toc_list, pos, new_pos, depth + 1);
642                                 tomenu.add(item);
643                         }
644                         pos = new_pos;
645                 }
646         }
647 }
648
649
650 void expandToc(Menu & tomenu, LyXView const * view)
651 {
652         // To make things very cleanly, we would have to pass view to
653         // all MenuItem constructors and to expandToc2. However, we
654         // know that all the entries in a TOC will be have status_ ==
655         // OK, so we avoid this unnecessary overhead (JMarc)
656
657         if (!view->buffer()) {
658                 tomenu.add(MenuItem(MenuItem::Command,
659                                     _("No Documents Open!"),
660                                     FuncRequest(LFUN_NOACTION)),
661                            view);
662                 return;
663         }
664
665         lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
666         lyx::toc::TocList::const_iterator cit = toc_list.begin();
667         lyx::toc::TocList::const_iterator end = toc_list.end();
668         for (; cit != end; ++cit) {
669                 // Handle this later
670                 if (cit->first == "TOC")
671                         continue;
672
673                 // All the rest is for floats
674                 auto_ptr<Menu> menu(new Menu);
675                 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
676                 lyx::toc::Toc::const_iterator eend = cit->second.end();
677                 for (; ccit != eend; ++ccit) {
678                         string const label = limit_string_length(ccit->str);
679                         menu->add(MenuItem(MenuItem::Command,
680                                            label,
681                                            FuncRequest(ccit->action())));
682                 }
683                 string const & floatName = cit->first;
684                 // Is the _(...) really needed here? (Lgb)
685                 MenuItem item(MenuItem::Submenu, _(floatName));
686                 item.submenu(menu.release());
687                 tomenu.add(item);
688         }
689
690         // Handle normal TOC
691         cit = toc_list.find("TOC");
692         if (cit == end) {
693                 tomenu.add(MenuItem(MenuItem::Command,
694                                     _("No Table of contents"),
695                                     FuncRequest()),
696                            view);
697         } else {
698                 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
699         }
700 }
701
702
703 void expandPasteRecent(Menu & tomenu, LyXView const * view)
704 {
705         if (!view || !view->buffer())
706                 return;
707
708         vector<string> const sel =
709                 lyx::cap::availableSelections(*view->buffer());
710
711         vector<string>::const_iterator cit = sel.begin();
712         vector<string>::const_iterator end = sel.end();
713
714         for (unsigned int index = 0; cit != end; ++cit, ++index) {
715                 tomenu.add(MenuItem(MenuItem::Command, *cit,
716                                     FuncRequest(LFUN_PASTE, tostr(index))));
717         }
718 }
719
720
721 void expandBranches(Menu & tomenu, LyXView const * view)
722 {
723         if (!view || !view->buffer())
724                 return;
725
726         BufferParams const & params = view->buffer()->params();
727
728         std::list<Branch>::const_iterator cit = params.branchlist().begin();
729         std::list<Branch>::const_iterator end = params.branchlist().end();
730
731         for (int ii = 1; cit != end; ++cit, ++ii) {
732                 string label = cit->getBranch();
733                 if (ii < 10)
734                         label = tostr(ii) + ". " + label + "|" + tostr(ii);
735                 tomenu.add(MenuItem(MenuItem::Command, label,
736                                     FuncRequest(LFUN_INSERT_BRANCH,
737                                                 cit->getBranch())), view);
738         }
739 }
740
741
742 } // namespace anon
743
744
745 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
746                          LyXView const * view) const
747 {
748         for (Menu::const_iterator cit = frommenu.begin();
749              cit != frommenu.end() ; ++cit) {
750                 switch (cit->kind()) {
751                 case MenuItem::Lastfiles:
752                         expandLastfiles(tomenu, view);
753                         break;
754
755                 case MenuItem::Documents:
756                         expandDocuments(tomenu, view);
757                         break;
758
759                 case MenuItem::ImportFormats:
760                 case MenuItem::ViewFormats:
761                 case MenuItem::UpdateFormats:
762                 case MenuItem::ExportFormats:
763                         expandFormats(cit->kind(), tomenu, view);
764                         break;
765
766                 case MenuItem::CharStyles:
767                         expandCharStyleInsert(tomenu, view);
768                         break;
769
770                 case MenuItem::FloatListInsert:
771                         expandFloatListInsert(tomenu, view);
772                         break;
773
774                 case MenuItem::FloatInsert:
775                         expandFloatInsert(tomenu, view);
776                         break;
777
778                 case MenuItem::PasteRecent:
779                         expandPasteRecent(tomenu, view);
780                         break;
781
782                 case MenuItem::Branches:
783                         expandBranches(tomenu, view);
784                         break;
785
786                 case MenuItem::Toc:
787                         expandToc(tomenu, view);
788                         break;
789
790                 case MenuItem::Submenu: {
791                         MenuItem item(*cit);
792                         item.submenu(new Menu(cit->submenuname()));
793                         expand(getMenu(cit->submenuname()),
794                                *item.submenu(), view);
795                         tomenu.add(item, view);
796                 }
797                 break;
798
799                 case MenuItem::Separator:
800                         tomenu.add(*cit, view);
801                         break;
802
803                 case MenuItem::Command:
804                         if (!specialmenu_ 
805                             || !specialmenu_->hasFunc(cit->func()))
806                                 tomenu.add(*cit, view);
807                 }
808         }
809
810         // we do not want the menu to end with a separator
811         if (!tomenu.empty()
812             && tomenu.items_.back().kind() == MenuItem::Separator)
813                 tomenu.items_.pop_back();
814
815         // Check whether the shortcuts are unique
816         tomenu.checkShortcuts();
817 }
818
819
820 void MenuBackend::read(LyXLex & lex)
821 {
822         enum Menutags {
823                 md_menu = 1,
824                 md_menubar,
825                 md_endmenuset,
826                 md_last
827         };
828
829         struct keyword_item menutags[md_last - 1] = {
830                 { "end", md_endmenuset },
831                 { "menu", md_menu },
832                 { "menubar", md_menubar }
833         };
834
835         //consistency check
836         if (compare_ascii_no_case(lex.getString(), "menuset")) {
837                 lyxerr << "Menubackend::read: ERROR wrong token:`"
838                        << lex.getString() << '\'' << endl;
839         }
840
841         lex.pushTable(menutags, md_last - 1);
842         if (lyxerr.debugging(Debug::PARSER))
843                 lex.printTable(lyxerr);
844
845         bool quit = false;
846
847         while (lex.isOK() && !quit) {
848                 switch (lex.lex()) {
849                 case md_menubar:
850                         menubar_.read(lex);
851                         break;
852                 case md_menu: {
853                         lex.next(true);
854                         string const name = lex.getString();
855                         if (hasMenu(name)) {
856                                 getMenu(name).read(lex);
857                         } else {
858                                 Menu menu(name);
859                                 menu.read(lex);
860                                 add(menu);
861                         }
862                         break;
863                 }
864                 case md_endmenuset:
865                         quit = true;
866                         break;
867                 default:
868                         lex.printError("menubackend::read: "
869                                        "Unknown menu tag: `$$Token'");
870                         break;
871                 }
872         }
873         lex.popTable();
874 }
875
876
877 void MenuBackend::add(Menu const & menu)
878 {
879         menulist_.push_back(menu);
880 }
881
882
883 bool MenuBackend::hasMenu(string const & name) const
884 {
885         return find_if(begin(), end(), MenuNamesEqual(name)) != end();
886 }
887
888
889 Menu const & MenuBackend::getMenu(string const & name) const
890 {
891         const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
892         if (cit == end())
893                 lyxerr << "No submenu named " << name << endl;
894         BOOST_ASSERT(cit != end());
895         return (*cit);
896 }
897
898
899 Menu & MenuBackend::getMenu(string const & name)
900 {
901         iterator it = find_if(begin(), end(), MenuNamesEqual(name));
902         if (it == end())
903                 lyxerr << "No submenu named " << name << endl;
904         BOOST_ASSERT(it != end());
905         return (*it);
906 }
907
908
909 Menu const & MenuBackend::getMenubar() const
910 {
911         return menubar_;
912 }