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