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