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