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