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