]> git.lyx.org Git - lyx.git/blob - src/MenuBackend.C
5e078de9dba82b7695dbd10208463bbd22c5a865
[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 #include "ToolbarBackend.h"
40
41 #include "support/filetools.h"
42 #include "support/lstrings.h"
43 #include "support/convert.h"
44
45 #include <boost/bind.hpp>
46
47 #include <algorithm>
48
49
50 namespace lyx {
51
52 using support::compare_no_case;
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() 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 from_utf8(bindings.begin()->print());
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 = _(lex.getString());
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 = _(lex.getString());
352                         lex.next(true);
353                         docstring const mname = from_utf8(lex.getString());
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_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(docstring const &name)
416 {
417         if (hasMenu(name))
418                 specialmenu_ = &getMenu(name);
419 }
420
421
422 namespace {
423
424 class compare_format {
425 public:
426         bool operator()(Format const * p1, Format const * p2) {
427                 return *p1 < *p2;
428         }
429 };
430
431 docstring const limit_string_length(docstring const & str)
432 {
433         docstring::size_type const max_item_length = 45;
434
435         if (str.size() > max_item_length)
436                 return str.substr(0, max_item_length - 3) + "...";
437         else
438                 return str;
439 }
440
441
442 void expandLastfiles(Menu & tomenu)
443 {
444         lyx::LastFilesSection::LastFiles const & lf = LyX::cref().session().lastFiles().lastFiles();
445         lyx::LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
446
447         int ii = 1;
448
449         for (; lfit != lf.end() && ii < 10; ++lfit, ++ii) {
450                 docstring const label = convert<docstring>(ii) + ". "
451                         + makeDisplayPath((*lfit), 30)
452                         + char_type('|') + convert<docstring>(ii);
453                 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))));
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, 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                         if ((*fit)->name() == "text")
539                                 label = _("Plain Text as Lines");
540                         else if ((*fit)->name() == "textparagraph")
541                                 label = _("Plain Text as Paragraphs");
542                         label += "...";
543                         break;
544                 case MenuItem::ViewFormats:
545                 case MenuItem::ExportFormats:
546                 case MenuItem::UpdateFormats:
547                         if (!(*fit)->documentFormat())
548                                 continue;
549                         break;
550                 default:
551                         BOOST_ASSERT(false);
552                         break;
553                 }
554                 if (!(*fit)->shortcut().empty())
555                         label += char_type('|') + from_utf8((*fit)->shortcut());
556
557                 if (buf)
558                         tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
559                                 FuncRequest(action, (*fit)->name())));
560                 else
561                         tomenu.add(MenuItem(MenuItem::Command, label,
562                                 FuncRequest(action, (*fit)->name())));
563         }
564 }
565
566
567 void expandFloatListInsert(Menu & tomenu, Buffer const * buf)
568 {
569         if (!buf) {
570                 tomenu.add(MenuItem(MenuItem::Command,
571                                     _("No Documents Open!"),
572                                     FuncRequest(LFUN_NOACTION)));
573                 return;
574         }
575
576         FloatList const & floats =
577                 buf->params().getLyXTextClass().floats();
578         FloatList::const_iterator cit = floats.begin();
579         FloatList::const_iterator end = floats.end();
580         for (; cit != end; ++cit) {
581                 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
582                                     _(cit->second.listName()),
583                                     FuncRequest(LFUN_FLOAT_LIST,
584                                                 cit->second.type())));
585         }
586 }
587
588
589 void expandFloatInsert(Menu & tomenu, Buffer const * buf)
590 {
591         if (!buf) {
592                 tomenu.add(MenuItem(MenuItem::Command,
593                                     _("No Documents Open!"),
594                                     FuncRequest(LFUN_NOACTION)));
595                 return;
596         }
597
598         FloatList const & floats =
599                 buf->params().getLyXTextClass().floats();
600         FloatList::const_iterator cit = floats.begin();
601         FloatList::const_iterator end = floats.end();
602         for (; cit != end; ++cit) {
603                 // normal float
604                 docstring const label = _(cit->second.name());
605                 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
606                                     FuncRequest(LFUN_FLOAT_INSERT,
607                                                 cit->second.type())));
608         }
609 }
610
611
612 void expandCharStyleInsert(Menu & tomenu, Buffer const * buf)
613 {
614         if (!buf) {
615                 tomenu.add(MenuItem(MenuItem::Command,
616                                     _("No Documents Open!"),
617                                     FuncRequest(LFUN_NOACTION)));
618                 return;
619         }
620         CharStyles & charstyles =
621                 buf->params().getLyXTextClass().charstyles();
622         CharStyles::iterator cit = charstyles.begin();
623         CharStyles::iterator end = charstyles.end();
624         for (; cit != end; ++cit) {
625                 docstring const label = from_utf8(cit->name);
626                 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
627                                     FuncRequest(LFUN_CHARSTYLE_INSERT,
628                                                 cit->name)));
629         }
630 }
631
632
633 Menu::size_type const max_number_of_items = 25;
634
635 void expandToc2(Menu & tomenu,
636                 lyx::toc::Toc const & toc_list,
637                 lyx::toc::Toc::size_type from,
638                 lyx::toc::Toc::size_type to, int depth)
639 {
640         int shortcut_count = 0;
641
642         // check whether depth is smaller than the smallest depth in toc.
643         int min_depth = 1000;
644         for (lyx::toc::Toc::size_type i = from; i < to; ++i)
645                 min_depth = std::min(min_depth, toc_list[i].depth());
646         if (min_depth > depth)
647                 depth = min_depth;
648
649
650         if (to - from <= max_number_of_items) {
651                 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
652                         docstring label(4 * max(0, toc_list[i].depth() - depth), char_type(' '));
653                         label += limit_string_length(toc_list[i].str());
654                         if (toc_list[i].depth() == depth
655                             && shortcut_count < 9) {
656                                 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
657                                         label += char_type('|') + convert<docstring>(++shortcut_count);
658                         }
659                         tomenu.add(MenuItem(MenuItem::Command, label,
660                                             FuncRequest(toc_list[i].action())));
661                 }
662         } else {
663                 lyx::toc::Toc::size_type pos = from;
664                 while (pos < to) {
665                         lyx::toc::Toc::size_type new_pos = pos + 1;
666                         while (new_pos < to &&
667                                toc_list[new_pos].depth() > depth)
668                                 ++new_pos;
669
670                         docstring label(4 * max(0, toc_list[pos].depth() - depth), ' ');
671                         label += limit_string_length(toc_list[pos].str());
672                         if (toc_list[pos].depth() == depth &&
673                             shortcut_count < 9) {
674                                 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
675                                         label += char_type('|') + convert<docstring>(++shortcut_count);
676                         }
677                         if (new_pos == pos + 1) {
678                                 tomenu.add(MenuItem(MenuItem::Command,
679                                                     label, FuncRequest(toc_list[pos].action())));
680                         } else {
681                                 MenuItem item(MenuItem::Submenu, label);
682                                 item.submenu(new Menu);
683                                 expandToc2(*item.submenu(),
684                                            toc_list, pos, new_pos, depth + 1);
685                                 tomenu.add(item);
686                         }
687                         pos = new_pos;
688                 }
689         }
690 }
691
692
693 void expandToc(Menu & tomenu, Buffer const * buf)
694 {
695         // To make things very cleanly, we would have to pass buf to
696         // all MenuItem constructors and to expandToc2. However, we
697         // know that all the entries in a TOC will be have status_ ==
698         // OK, so we avoid this unnecessary overhead (JMarc)
699
700         if (!buf) {
701                 tomenu.add(MenuItem(MenuItem::Command,
702                                     _("No Documents Open!"),
703                                     FuncRequest(LFUN_NOACTION)));
704                 return;
705         }
706
707         FloatList const & floatlist = buf->params().getLyXTextClass().floats();
708         lyx::toc::TocList const & toc_list = lyx::toc::getTocList(*buf);
709         lyx::toc::TocList::const_iterator cit = toc_list.begin();
710         lyx::toc::TocList::const_iterator end = toc_list.end();
711         for (; cit != end; ++cit) {
712                 // Handle this later
713                 if (cit->first == "TOC")
714                         continue;
715
716                 // All the rest is for floats
717                 auto_ptr<Menu> menu(new Menu);
718                 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
719                 lyx::toc::Toc::const_iterator eend = cit->second.end();
720                 for (; ccit != eend; ++ccit) {
721                         docstring const label = limit_string_length(ccit->str());
722                         menu->add(MenuItem(MenuItem::Command,
723                                            label,
724                                            FuncRequest(ccit->action())));
725                 }
726                 string const & floatName = floatlist.getType(cit->first).listName();
727                 MenuItem item(MenuItem::Submenu, _(floatName));
728                 item.submenu(menu.release());
729                 tomenu.add(item);
730         }
731
732         // Handle normal TOC
733         cit = toc_list.find("TOC");
734         if (cit == end) {
735                 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
736                                     _("No Table of contents"),
737                                     FuncRequest()));
738         } else {
739                 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
740         }
741 }
742
743
744 void expandPasteRecent(Menu & tomenu, Buffer const * buf)
745 {
746         if (!buf)
747                 return;
748
749         vector<docstring> const sel =
750                 cap::availableSelections(*buf);
751
752         vector<docstring>::const_iterator cit = sel.begin();
753         vector<docstring>::const_iterator end = sel.end();
754
755         for (unsigned int index = 0; cit != end; ++cit, ++index) {
756                 tomenu.add(MenuItem(MenuItem::Command, *cit,
757                                     FuncRequest(LFUN_PASTE, convert<string>(index))));
758         }
759 }
760
761
762 void expandToolbars(Menu & tomenu, Buffer const * buf)
763 {
764         //
765         // extracts the toolbars from the backend
766         ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
767         ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
768
769         int i = 1;
770         for (; cit != end; ++cit, ++i) {
771                 docstring label = _(cit->name);
772                 label = char_type(uppercase(label[0])) + label.substr(1);
773                 // frontends are not supposed to turn on/off toolbars, if they can not
774                 // update ToolbarBackend::flags. That is to say, ToolbarsBackend::flags
775                 // should reflect the true state of toolbars.
776                 // 
777                 // menu is displayed as 
778                 //       on/off review
779                 // and 
780                 //              review (auto)
781                 // in the case of auto.
782                 if (cit->flags & ToolbarBackend::AUTO)
783                         label += _(" (auto)");
784                 tomenu.add(MenuItem(MenuItem::Command, label,
785                                     FuncRequest(LFUN_TOOLBAR_TOGGLE_STATE, cit->name)));
786         }
787 }
788
789
790 void expandBranches(Menu & tomenu, Buffer const * buf)
791 {
792         if (!buf)
793                 return;
794
795         BufferParams const & params = buf->getMasterBuffer()->params();
796
797         BranchList::const_iterator cit = params.branchlist().begin();
798         BranchList::const_iterator end = params.branchlist().end();
799
800         for (int ii = 1; cit != end; ++cit, ++ii) {
801                 docstring label = cit->getBranch();
802                 if (ii < 10)
803                         label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
804                 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
805                                     FuncRequest(LFUN_BRANCH_INSERT,
806                                                 cit->getBranch())));
807         }
808 }
809
810
811 } // namespace anon
812
813
814 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
815                          Buffer const * buf) const
816 {
817         if (!tomenu.empty())
818                 tomenu.clear();
819
820         for (Menu::const_iterator cit = frommenu.begin();
821              cit != frommenu.end() ; ++cit) {
822                 switch (cit->kind()) {
823                 case MenuItem::Lastfiles:
824                         expandLastfiles(tomenu);
825                         break;
826
827                 case MenuItem::Documents:
828                         expandDocuments(tomenu);
829                         break;
830
831                 case MenuItem::Bookmarks:
832                         expandBookmarks(tomenu);
833                         break;
834
835                 case MenuItem::ImportFormats:
836                 case MenuItem::ViewFormats:
837                 case MenuItem::UpdateFormats:
838                 case MenuItem::ExportFormats:
839                         expandFormats(cit->kind(), tomenu, buf);
840                         break;
841
842                 case MenuItem::CharStyles:
843                         expandCharStyleInsert(tomenu, buf);
844                         break;
845
846                 case MenuItem::FloatListInsert:
847                         expandFloatListInsert(tomenu, buf);
848                         break;
849
850                 case MenuItem::FloatInsert:
851                         expandFloatInsert(tomenu, buf);
852                         break;
853
854                 case MenuItem::PasteRecent:
855                         expandPasteRecent(tomenu, buf);
856                         break;
857
858                 case MenuItem::Toolbars:
859                         expandToolbars(tomenu, buf);
860                         break;
861
862                 case MenuItem::Branches:
863                         expandBranches(tomenu, buf);
864                         break;
865
866                 case MenuItem::Toc:
867                         expandToc(tomenu, buf);
868                         break;
869
870                 case MenuItem::Submenu: {
871                         MenuItem item(*cit);
872                         item.submenu(new Menu(cit->submenuname()));
873                         expand(getMenu(cit->submenuname()),
874                                *item.submenu(), buf);
875                         tomenu.addWithStatusCheck(item);
876                 }
877                 break;
878
879                 case MenuItem::Separator:
880                         tomenu.addWithStatusCheck(*cit);
881                         break;
882
883                 case MenuItem::Command:
884                         if (!specialmenu_
885                             || !specialmenu_->hasFunc(cit->func()))
886                                 tomenu.addWithStatusCheck(*cit);
887                 }
888         }
889
890         // we do not want the menu to end with a separator
891         if (!tomenu.empty()
892             && tomenu.items_.back().kind() == MenuItem::Separator)
893                 tomenu.items_.pop_back();
894
895         // Check whether the shortcuts are unique
896         tomenu.checkShortcuts();
897 }
898
899
900 void MenuBackend::read(LyXLex & lex)
901 {
902         enum Menutags {
903                 md_menu = 1,
904                 md_menubar,
905                 md_endmenuset,
906                 md_last
907         };
908
909         struct keyword_item menutags[md_last - 1] = {
910                 { "end", md_endmenuset },
911                 { "menu", md_menu },
912                 { "menubar", md_menubar }
913         };
914
915         //consistency check
916         if (compare_ascii_no_case(lex.getString(), "menuset")) {
917                 lyxerr << "Menubackend::read: ERROR wrong token:`"
918                        << lex.getString() << '\'' << endl;
919         }
920
921         lex.pushTable(menutags, md_last - 1);
922         if (lyxerr.debugging(Debug::PARSER))
923                 lex.printTable(lyxerr);
924
925         bool quit = false;
926
927         while (lex.isOK() && !quit) {
928                 switch (lex.lex()) {
929                 case md_menubar:
930                         menubar_.read(lex);
931                         break;
932                 case md_menu: {
933                         lex.next(true);
934                         docstring const name = from_utf8(lex.getString());
935                         if (hasMenu(name)) {
936                                 getMenu(name).read(lex);
937                         } else {
938                                 Menu menu(name);
939                                 menu.read(lex);
940                                 add(menu);
941                         }
942                         break;
943                 }
944                 case md_endmenuset:
945                         quit = true;
946                         break;
947                 default:
948                         lex.printError("menubackend::read: "
949                                        "Unknown menu tag: `$$Token'");
950                         break;
951                 }
952         }
953         lex.popTable();
954 }
955
956
957 void MenuBackend::add(Menu const & menu)
958 {
959         menulist_.push_back(menu);
960 }
961
962
963 bool MenuBackend::hasMenu(docstring const & name) const
964 {
965         return find_if(begin(), end(), MenuNamesEqual(name)) != end();
966 }
967
968
969 Menu const & MenuBackend::getMenu(docstring const & name) const
970 {
971         const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
972         if (cit == end())
973                 lyxerr << "No submenu named " << to_utf8(name) << endl;
974         BOOST_ASSERT(cit != end());
975         return (*cit);
976 }
977
978
979 Menu & MenuBackend::getMenu(docstring const & name)
980 {
981         iterator it = find_if(begin(), end(), MenuNamesEqual(name));
982         if (it == end())
983                 lyxerr << "No submenu named " << to_utf8(name) << endl;
984         BOOST_ASSERT(it != end());
985         return (*it);
986 }
987
988
989 Menu const & MenuBackend::getMenubar() const
990 {
991         return menubar_;
992 }
993
994
995 } // namespace lyx