]> git.lyx.org Git - lyx.git/blob - src/MenuBackend.C
c2e107f22455c1924a9e824cb3c71fd930d8366b
[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_no_case;
54 using support::compare_ascii_no_case;
55 using support::contains;
56 using support::makeDisplayPath;
57 using support::token;
58 using support::uppercase;
59
60 using boost::bind;
61
62 using std::auto_ptr;
63 using std::endl;
64 using std::equal_to;
65 using std::find_if;
66 using std::max;
67 using std::sort;
68 using std::string;
69 using std::vector;
70
71
72 namespace {
73
74 class MenuNamesEqual : public std::unary_function<Menu, bool> {
75 public:
76         MenuNamesEqual(docstring const & name)
77                 : name_(name) {}
78         bool operator()(Menu const & menu) const
79         {
80                 return menu.name() == name_;
81         }
82 private:
83         docstring name_;
84 };
85
86 } // namespace anon
87
88
89 // This is the global menu definition
90 MenuBackend menubackend;
91
92
93 MenuItem::MenuItem(Kind kind)
94         : kind_(kind), optional_(false)
95 {}
96
97
98 MenuItem::MenuItem(Kind kind, docstring const & label,
99                    docstring const & submenu, bool optional)
100         : kind_(kind), label_(label),
101           submenuname_(submenu), optional_(optional)
102 {
103         BOOST_ASSERT(kind == Submenu);
104 }
105
106
107 MenuItem::MenuItem(Kind kind, docstring const & label,
108                    FuncRequest const & func, bool optional)
109         : kind_(kind), label_(label), func_(func), optional_(optional)
110 {
111         func_.origin = FuncRequest::MENU;
112 }
113
114
115 MenuItem::~MenuItem()
116 {}
117
118
119 void MenuItem::submenu(Menu * menu)
120 {
121         submenu_.reset(menu);
122 }
123
124
125 docstring const MenuItem::label() const
126 {
127         return token(label_, char_type('|'), 0);
128 }
129
130
131 docstring const MenuItem::shortcut() const
132 {
133         return token(label_, char_type('|'), 1);
134 }
135
136
137 docstring const MenuItem::binding() const
138 {
139         if (kind_ != Command)
140                 return docstring();
141
142         // Get the keys bound to this action, but keep only the
143         // first one later
144         kb_keymap::Bindings bindings = theTopLevelKeymap().findbindings(func_);
145
146         if (bindings.size()) {
147                 return from_utf8(bindings.begin()->print());
148         } else {
149                 lyxerr[Debug::KBMAP]
150                         << "No binding for "
151                         << lyxaction.getActionName(func_.action)
152                         << '(' << to_utf8(func_.argument()) << ')' << endl;
153                 return docstring();
154         }
155
156 }
157
158
159 Menu & Menu::add(MenuItem const & i)
160 {
161         items_.push_back(i);
162         return *this;
163 }
164
165
166 Menu & Menu::addWithStatusCheck(MenuItem const & i)
167 {
168         switch (i.kind()) {
169
170         case MenuItem::Command: {
171                 FuncStatus status = lyx::getStatus(i.func());
172                 if (status.unknown() || (!status.enabled() && i.optional()))
173                         break;
174                 items_.push_back(i);
175                 items_.back().status(status);
176                 break;
177         }
178
179         case MenuItem::Submenu: {
180                 if (i.submenu()) {
181                         bool enabled = false;
182                         for (const_iterator cit = i.submenu()->begin();
183                              cit != i.submenu()->end(); ++cit) {
184                                 if ((cit->kind() == MenuItem::Command
185                                      || cit->kind() == MenuItem::Submenu)
186                                     && cit->status().enabled()) {
187                                         enabled = true;
188                                         break;
189                                 }
190                         }
191                         if (enabled || !i.optional()) {
192                                 items_.push_back(i);
193                                 items_.back().status().enabled(enabled);
194                         }
195                 }
196                 else
197                         items_.push_back(i);
198                 break;
199         }
200
201         case MenuItem::Separator:
202                 if (!items_.empty()
203                     && items_.back().kind() != MenuItem::Separator)
204                         items_.push_back(i);
205                 break;
206
207         default:
208                 items_.push_back(i);
209         }
210
211         return *this;
212 }
213
214
215 Menu & Menu::read(LyXLex & lex)
216 {
217         enum Menutags {
218                 md_item = 1,
219                 md_branches,
220                 md_documents,
221                 md_bookmarks,
222                 md_charstyles,
223                 md_endmenu,
224                 md_exportformats,
225                 md_importformats,
226                 md_lastfiles,
227                 md_optitem,
228                 md_optsubmenu,
229                 md_separator,
230                 md_submenu,
231                 md_toc,
232                 md_updateformats,
233                 md_viewformats,
234                 md_floatlistinsert,
235                 md_floatinsert,
236                 md_pasterecent,
237                 md_toolbars,
238                 md_last
239         };
240
241         struct keyword_item menutags[md_last - 1] = {
242                 { "bookmarks", md_bookmarks },
243                 { "branches", md_branches },
244                 { "charstyles", md_charstyles },
245                 { "documents", md_documents },
246                 { "end", md_endmenu },
247                 { "exportformats", md_exportformats },
248                 { "floatinsert", md_floatinsert },
249                 { "floatlistinsert", md_floatlistinsert },
250                 { "importformats", md_importformats },
251                 { "item", md_item },
252                 { "lastfiles", md_lastfiles },
253                 { "optitem", md_optitem },
254                 { "optsubmenu", md_optsubmenu },
255                 { "pasterecent", md_pasterecent },
256                 { "separator", md_separator },
257                 { "submenu", md_submenu },
258                 { "toc", md_toc },
259                 { "toolbars", md_toolbars },
260                 { "updateformats", md_updateformats },
261                 { "viewformats", md_viewformats }
262         };
263
264         lex.pushTable(menutags, md_last - 1);
265         if (lyxerr.debugging(Debug::PARSER))
266                 lex.printTable(lyxerr);
267
268         bool quit = false;
269         bool optional = false;
270
271         while (lex.isOK() && !quit) {
272                 switch (lex.lex()) {
273                 case md_optitem:
274                         optional = true;
275                         // fallback to md_item
276                 case md_item: {
277                         lex.next(true);
278                         docstring const name = translateIfPossible(lex.getDocString());
279                         lex.next(true);
280                         string const command = lex.getString();
281                         FuncRequest func = lyxaction.lookupFunc(command);
282                         add(MenuItem(MenuItem::Command, name, func, optional));
283                         optional = false;
284                         break;
285                 }
286
287                 case md_separator:
288                         add(MenuItem(MenuItem::Separator));
289                         break;
290
291                 case md_lastfiles:
292                         add(MenuItem(MenuItem::Lastfiles));
293                         break;
294
295                 case md_charstyles:
296                         add(MenuItem(MenuItem::CharStyles));
297                         break;
298
299                 case md_documents:
300                         add(MenuItem(MenuItem::Documents));
301                         break;
302
303                 case md_bookmarks:
304                         add(MenuItem(MenuItem::Bookmarks));
305                         break;
306
307                 case md_toc:
308                         add(MenuItem(MenuItem::Toc));
309                         break;
310
311                 case md_viewformats:
312                         add(MenuItem(MenuItem::ViewFormats));
313                         break;
314
315                 case md_updateformats:
316                         add(MenuItem(MenuItem::UpdateFormats));
317                         break;
318
319                 case md_exportformats:
320                         add(MenuItem(MenuItem::ExportFormats));
321                         break;
322
323                 case md_importformats:
324                         add(MenuItem(MenuItem::ImportFormats));
325                         break;
326
327                 case md_floatlistinsert:
328                         add(MenuItem(MenuItem::FloatListInsert));
329                         break;
330
331                 case md_floatinsert:
332                         add(MenuItem(MenuItem::FloatInsert));
333                         break;
334
335                 case md_pasterecent:
336                         add(MenuItem(MenuItem::PasteRecent));
337                         break;
338
339                 case md_toolbars:
340                         add(MenuItem(MenuItem::Toolbars));
341                         break;
342
343                 case md_branches:
344                         add(MenuItem(MenuItem::Branches));
345                         break;
346
347                 case md_optsubmenu:
348                         optional = true;
349                         // fallback to md_submenu
350                 case md_submenu: {
351                         lex.next(true);
352                         docstring const mlabel = translateIfPossible(lex.getDocString());
353                         lex.next(true);
354                         docstring const mname = lex.getDocString();
355                         add(MenuItem(MenuItem::Submenu, mlabel, mname,
356                                      optional));
357                         optional = false;
358                         break;
359                 }
360
361                 case md_endmenu:
362                         quit = true;
363                         break;
364
365                 default:
366                         lex.printError("Menu::read: "
367                                        "Unknown menu tag: `$$Token'");
368                         break;
369                 }
370         }
371         lex.popTable();
372         return *this;
373 }
374
375
376 MenuItem const & Menu::operator[](size_type i) const
377 {
378         return items_[i];
379 }
380
381
382 bool Menu::hasFunc(FuncRequest const & func) const
383 {
384         return find_if(begin(), end(),
385                        bind(std::equal_to<FuncRequest>(),
386                             bind(&MenuItem::func, _1),
387                             func)) != end();
388 }
389
390 void Menu::checkShortcuts() const
391 {
392         // This is a quadratic algorithm, but we do not care because
393         // menus are short enough
394         for (const_iterator it1 = begin(); it1 != end(); ++it1) {
395                 docstring shortcut = it1->shortcut();
396                 if (shortcut.empty())
397                         continue;
398                 if (!contains(it1->label(), shortcut))
399                         lyxerr << "Menu warning: menu entry \""
400                                << to_utf8(it1->label())
401                                << "\" does not contain shortcut `"
402                                << to_utf8(shortcut) << "'." << endl;
403                 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
404                         if (!compare_no_case(it2->shortcut(), shortcut)) {
405                                 lyxerr << "Menu warning: menu entries "
406                                        << '"' << to_utf8(it1->fulllabel())
407                                        << "\" and \"" << to_utf8(it2->fulllabel())
408                                        << "\" share the same shortcut."
409                                        << endl;
410                         }
411                 }
412         }
413 }
414
415
416 void MenuBackend::specialMenu(docstring const &name)
417 {
418         if (hasMenu(name))
419                 specialmenu_ = &getMenu(name);
420 }
421
422
423 namespace {
424
425 class compare_format {
426 public:
427         bool operator()(Format const * p1, Format const * p2) {
428                 return *p1 < *p2;
429         }
430 };
431
432 docstring const limit_string_length(docstring const & str)
433 {
434         docstring::size_type const max_item_length = 45;
435
436         if (str.size() > max_item_length)
437                 return str.substr(0, max_item_length - 3) + "...";
438         else
439                 return str;
440 }
441
442
443 void expandLastfiles(Menu & tomenu)
444 {
445         lyx::LastFilesSection::LastFiles const & lf = LyX::cref().session().lastFiles().lastFiles();
446         lyx::LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
447
448         int ii = 1;
449
450         for (; lfit != lf.end() && ii < 10; ++lfit, ++ii) {
451                 string const file = lfit->absFilename();
452                 docstring const label = convert<docstring>(ii) + ". "
453                         + makeDisplayPath(file, 30)
454                         + char_type('|') + convert<docstring>(ii);
455                 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, file)));
456         }
457 }
458
459
460 void expandDocuments(Menu & tomenu)
461 {
462         typedef vector<string> Strings;
463         Strings const names = theBufferList().getFileNames();
464
465         if (names.empty()) {
466                 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
467                                     FuncRequest(LFUN_NOACTION)));
468                 return;
469         }
470
471         int ii = 1;
472         Strings::const_iterator docit = names.begin();
473         Strings::const_iterator end = names.end();
474         for (; docit != end; ++docit, ++ii) {
475                 docstring label = makeDisplayPath(*docit, 20);
476                 if (ii < 10)
477                         label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
478                 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BUFFER_SWITCH, *docit)));
479         }
480 }
481
482
483 void expandBookmarks(Menu & tomenu)
484 {
485         lyx::BookmarksSection const & bm = LyX::cref().session().bookmarks();
486
487         for (size_t i = 1; i <= bm.size(); ++i) {
488                 if (bm.isValid(i)) {
489                         docstring const label = convert<docstring>(i) + ". "
490                                 + makeDisplayPath(bm.bookmark(i).filename.absFilename(), 20)
491                                 + char_type('|') + convert<docstring>(i);
492                         tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BOOKMARK_GOTO, 
493                                 convert<docstring>(i))));
494                 }
495         }
496 }
497
498
499 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
500 {
501         if (!buf && kind != MenuItem::ImportFormats) {
502                 tomenu.add(MenuItem(MenuItem::Command,
503                                     _("No Documents Open!"),
504                                     FuncRequest(LFUN_NOACTION)));
505                 return;
506         }
507
508         typedef vector<Format const *> Formats;
509         Formats formats;
510         kb_action action;
511
512         switch (kind) {
513         case MenuItem::ImportFormats:
514                 formats = Importer::GetImportableFormats();
515                 action = LFUN_BUFFER_IMPORT;
516                 break;
517         case MenuItem::ViewFormats:
518                 formats = Exporter::getExportableFormats(*buf, true);
519                 action = LFUN_BUFFER_VIEW;
520                 break;
521         case MenuItem::UpdateFormats:
522                 formats = Exporter::getExportableFormats(*buf, true);
523                 action = LFUN_BUFFER_UPDATE;
524                 break;
525         default:
526                 formats = Exporter::getExportableFormats(*buf, false);
527                 action = LFUN_BUFFER_EXPORT;
528         }
529         sort(formats.begin(), formats.end(), compare_format());
530
531         Formats::const_iterator fit = formats.begin();
532         Formats::const_iterator end = formats.end();
533         for (; fit != end ; ++fit) {
534                 if ((*fit)->dummy())
535                         continue;
536                 docstring label = from_utf8((*fit)->prettyname());
537
538                 switch (kind) {
539                 case MenuItem::ImportFormats:
540                         if ((*fit)->name() == "text")
541                                 label = _("Plain Text as Lines");
542                         else if ((*fit)->name() == "textparagraph")
543                                 label = _("Plain Text as Paragraphs");
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                                                 cit->name)));
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_
894                             || !specialmenu_->hasFunc(cit->func()))
895                                 tomenu.addWithStatusCheck(*cit);
896                 }
897         }
898
899         // we do not want the menu to end with a separator
900         if (!tomenu.empty()
901             && tomenu.items_.back().kind() == MenuItem::Separator)
902                 tomenu.items_.pop_back();
903
904         // Check whether the shortcuts are unique
905         tomenu.checkShortcuts();
906 }
907
908
909 void MenuBackend::read(LyXLex & lex)
910 {
911         enum Menutags {
912                 md_menu = 1,
913                 md_menubar,
914                 md_endmenuset,
915                 md_last
916         };
917
918         struct keyword_item menutags[md_last - 1] = {
919                 { "end", md_endmenuset },
920                 { "menu", md_menu },
921                 { "menubar", md_menubar }
922         };
923
924         //consistency check
925         if (compare_ascii_no_case(lex.getString(), "menuset")) {
926                 lyxerr << "Menubackend::read: ERROR wrong token:`"
927                        << lex.getString() << '\'' << endl;
928         }
929
930         lex.pushTable(menutags, md_last - 1);
931         if (lyxerr.debugging(Debug::PARSER))
932                 lex.printTable(lyxerr);
933
934         bool quit = false;
935
936         while (lex.isOK() && !quit) {
937                 switch (lex.lex()) {
938                 case md_menubar:
939                         menubar_.read(lex);
940                         break;
941                 case md_menu: {
942                         lex.next(true);
943                         docstring const name = lex.getDocString();
944                         if (hasMenu(name)) {
945                                 getMenu(name).read(lex);
946                         } else {
947                                 Menu menu(name);
948                                 menu.read(lex);
949                                 add(menu);
950                         }
951                         break;
952                 }
953                 case md_endmenuset:
954                         quit = true;
955                         break;
956                 default:
957                         lex.printError("menubackend::read: "
958                                        "Unknown menu tag: `$$Token'");
959                         break;
960                 }
961         }
962         lex.popTable();
963 }
964
965
966 void MenuBackend::add(Menu const & menu)
967 {
968         menulist_.push_back(menu);
969 }
970
971
972 bool MenuBackend::hasMenu(docstring const & name) const
973 {
974         return find_if(begin(), end(), MenuNamesEqual(name)) != end();
975 }
976
977
978 Menu const & MenuBackend::getMenu(docstring const & name) const
979 {
980         const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
981         if (cit == end())
982                 lyxerr << "No submenu named " << to_utf8(name) << endl;
983         BOOST_ASSERT(cit != end());
984         return (*cit);
985 }
986
987
988 Menu & MenuBackend::getMenu(docstring const & name)
989 {
990         iterator it = find_if(begin(), end(), MenuNamesEqual(name));
991         if (it == end())
992                 lyxerr << "No submenu named " << to_utf8(name) << endl;
993         BOOST_ASSERT(it != end());
994         return (*it);
995 }
996
997
998 Menu const & MenuBackend::getMenubar() const
999 {
1000         return menubar_;
1001 }
1002
1003
1004 } // namespace lyx