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