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