]> git.lyx.org Git - lyx.git/blob - src/MenuBackend.C
Real fix from Bernhard Roider
[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(bool forgui) 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 bindings.begin()->print(forgui);
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(Menu const & menu)
417 {
418         specialmenu_ = menu;
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                 string const file = lfit->absFilename();
451                 docstring const label = convert<docstring>(ii) + ". "
452                         + makeDisplayPath(file, 30)
453                         + char_type('|') + convert<docstring>(ii);
454                 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, file)));
455         }
456 }
457
458
459 void expandDocuments(Menu & tomenu)
460 {
461         typedef vector<string> Strings;
462         Strings const names = theBufferList().getFileNames();
463
464         if (names.empty()) {
465                 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
466                                     FuncRequest(LFUN_NOACTION)));
467                 return;
468         }
469
470         int ii = 1;
471         Strings::const_iterator docit = names.begin();
472         Strings::const_iterator end = names.end();
473         for (; docit != end; ++docit, ++ii) {
474                 docstring label = makeDisplayPath(*docit, 20);
475                 if (ii < 10)
476                         label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
477                 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BUFFER_SWITCH, *docit)));
478         }
479 }
480
481
482 void expandBookmarks(Menu & tomenu)
483 {
484         lyx::BookmarksSection const & bm = LyX::cref().session().bookmarks();
485
486         for (size_t i = 1; i <= bm.size(); ++i) {
487                 if (bm.isValid(i)) {
488                         docstring const label = convert<docstring>(i) + ". "
489                                 + makeDisplayPath(bm.bookmark(i).filename.absFilename(), 20)
490                                 + char_type('|') + convert<docstring>(i);
491                         tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BOOKMARK_GOTO, 
492                                 convert<docstring>(i))));
493                 }
494         }
495 }
496
497
498 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
499 {
500         if (!buf && kind != MenuItem::ImportFormats) {
501                 tomenu.add(MenuItem(MenuItem::Command,
502                                     _("No Documents Open!"),
503                                     FuncRequest(LFUN_NOACTION)));
504                 return;
505         }
506
507         typedef vector<Format const *> Formats;
508         Formats formats;
509         kb_action action;
510
511         switch (kind) {
512         case MenuItem::ImportFormats:
513                 formats = Importer::GetImportableFormats();
514                 action = LFUN_BUFFER_IMPORT;
515                 break;
516         case MenuItem::ViewFormats:
517                 formats = Exporter::getExportableFormats(*buf, true);
518                 action = LFUN_BUFFER_VIEW;
519                 break;
520         case MenuItem::UpdateFormats:
521                 formats = Exporter::getExportableFormats(*buf, true);
522                 action = LFUN_BUFFER_UPDATE;
523                 break;
524         default:
525                 formats = Exporter::getExportableFormats(*buf, false);
526                 action = LFUN_BUFFER_EXPORT;
527         }
528         sort(formats.begin(), formats.end(), compare_format());
529
530         Formats::const_iterator fit = formats.begin();
531         Formats::const_iterator end = formats.end();
532         for (; fit != end ; ++fit) {
533                 if ((*fit)->dummy())
534                         continue;
535                 docstring label = from_utf8((*fit)->prettyname());
536
537                 switch (kind) {
538                 case MenuItem::ImportFormats:
539                         if ((*fit)->name() == "text")
540                                 label = _("Plain Text");
541                         else if ((*fit)->name() == "textparagraph")
542                                 label = _("Plain Text, Join Lines");
543                         label += "...";
544                         break;
545                 case MenuItem::ViewFormats:
546                 case MenuItem::ExportFormats:
547                 case MenuItem::UpdateFormats:
548                         if (!(*fit)->documentFormat())
549                                 continue;
550                         break;
551                 default:
552                         BOOST_ASSERT(false);
553                         break;
554                 }
555                 if (!(*fit)->shortcut().empty())
556                         label += char_type('|') + from_utf8((*fit)->shortcut());
557
558                 if (buf)
559                         tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
560                                 FuncRequest(action, (*fit)->name())));
561                 else
562                         tomenu.add(MenuItem(MenuItem::Command, label,
563                                 FuncRequest(action, (*fit)->name())));
564         }
565 }
566
567
568 void expandFloatListInsert(Menu & tomenu, Buffer const * buf)
569 {
570         if (!buf) {
571                 tomenu.add(MenuItem(MenuItem::Command,
572                                     _("No Documents Open!"),
573                                     FuncRequest(LFUN_NOACTION)));
574                 return;
575         }
576
577         FloatList const & floats =
578                 buf->params().getLyXTextClass().floats();
579         FloatList::const_iterator cit = floats.begin();
580         FloatList::const_iterator end = floats.end();
581         for (; cit != end; ++cit) {
582                 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
583                                     _(cit->second.listName()),
584                                     FuncRequest(LFUN_FLOAT_LIST,
585                                                 cit->second.type())));
586         }
587 }
588
589
590 void expandFloatInsert(Menu & tomenu, Buffer const * buf)
591 {
592         if (!buf) {
593                 tomenu.add(MenuItem(MenuItem::Command,
594                                     _("No Documents Open!"),
595                                     FuncRequest(LFUN_NOACTION)));
596                 return;
597         }
598
599         FloatList const & floats =
600                 buf->params().getLyXTextClass().floats();
601         FloatList::const_iterator cit = floats.begin();
602         FloatList::const_iterator end = floats.end();
603         for (; cit != end; ++cit) {
604                 // normal float
605                 docstring const label = _(cit->second.name());
606                 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
607                                     FuncRequest(LFUN_FLOAT_INSERT,
608                                                 cit->second.type())));
609         }
610 }
611
612
613 void expandCharStyleInsert(Menu & tomenu, Buffer const * buf)
614 {
615         if (!buf) {
616                 tomenu.add(MenuItem(MenuItem::Command,
617                                     _("No Documents Open!"),
618                                     FuncRequest(LFUN_NOACTION)));
619                 return;
620         }
621         CharStyles & charstyles =
622                 buf->params().getLyXTextClass().charstyles();
623         CharStyles::iterator cit = charstyles.begin();
624         CharStyles::iterator end = charstyles.end();
625         for (; cit != end; ++cit) {
626                 docstring const label = from_utf8(cit->name);
627                 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
628                                     FuncRequest(LFUN_CHARSTYLE_INSERT,
629                                                 label)));
630         }
631 }
632
633
634 Menu::size_type const max_number_of_items = 25;
635
636 void expandToc2(Menu & tomenu,
637                 Toc const & toc_list,
638                 Toc::size_type from,
639                 Toc::size_type to, int depth)
640 {
641         int shortcut_count = 0;
642
643         // check whether depth is smaller than the smallest depth in toc.
644         int min_depth = 1000;
645         for (Toc::size_type i = from; i < to; ++i)
646                 min_depth = std::min(min_depth, toc_list[i].depth());
647         if (min_depth > depth)
648                 depth = min_depth;
649
650
651         if (to - from <= max_number_of_items) {
652                 for (Toc::size_type i = from; i < to; ++i) {
653                         docstring label(4 * max(0, toc_list[i].depth() - depth), char_type(' '));
654                         label += limit_string_length(toc_list[i].str());
655                         if (toc_list[i].depth() == depth
656                             && shortcut_count < 9) {
657                                 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
658                                         label += char_type('|') + convert<docstring>(++shortcut_count);
659                         }
660                         tomenu.add(MenuItem(MenuItem::Command, label,
661                                             FuncRequest(toc_list[i].action())));
662                 }
663         } else {
664                 Toc::size_type pos = from;
665                 while (pos < to) {
666                         Toc::size_type new_pos = pos + 1;
667                         while (new_pos < to &&
668                                toc_list[new_pos].depth() > depth)
669                                 ++new_pos;
670
671                         docstring label(4 * max(0, toc_list[pos].depth() - depth), ' ');
672                         label += limit_string_length(toc_list[pos].str());
673                         if (toc_list[pos].depth() == depth &&
674                             shortcut_count < 9) {
675                                 if (label.find(convert<docstring>(shortcut_count + 1)) != docstring::npos)
676                                         label += char_type('|') + convert<docstring>(++shortcut_count);
677                         }
678                         if (new_pos == pos + 1) {
679                                 tomenu.add(MenuItem(MenuItem::Command,
680                                                     label, FuncRequest(toc_list[pos].action())));
681                         } else {
682                                 MenuItem item(MenuItem::Submenu, label);
683                                 item.submenu(new Menu);
684                                 expandToc2(*item.submenu(),
685                                            toc_list, pos, new_pos, depth + 1);
686                                 tomenu.add(item);
687                         }
688                         pos = new_pos;
689                 }
690         }
691 }
692
693
694 void expandToc(Menu & tomenu, Buffer const * buf)
695 {
696         // To make things very cleanly, we would have to pass buf to
697         // all MenuItem constructors and to expandToc2. However, we
698         // know that all the entries in a TOC will be have status_ ==
699         // OK, so we avoid this unnecessary overhead (JMarc)
700
701         if (!buf) {
702                 tomenu.add(MenuItem(MenuItem::Command,
703                                     _("No Documents Open!"),
704                                     FuncRequest(LFUN_NOACTION)));
705                 return;
706         }
707
708         // Add an entry for the master doc if this is a child doc
709         Buffer const * const master = buf->getMasterBuffer();
710         if (buf != master) {
711                 ParIterator const pit = par_iterator_begin(master->inset());
712                 string const arg = convert<string>(pit->id());
713                 FuncRequest f(LFUN_PARAGRAPH_GOTO, arg);
714                 tomenu.add(MenuItem(MenuItem::Command, _("Master Document"), f));
715         }
716
717         FloatList const & floatlist = buf->params().getLyXTextClass().floats();
718         TocList const & toc_list = buf->tocBackend().tocs();
719         TocList::const_iterator cit = toc_list.begin();
720         TocList::const_iterator end = toc_list.end();
721         for (; cit != end; ++cit) {
722                 // Handle this later
723                 if (cit->first == "tableofcontents")
724                         continue;
725
726                 // All the rest is for floats
727                 auto_ptr<Menu> menu(new Menu);
728                 TocIterator ccit = cit->second.begin();
729                 TocIterator eend = cit->second.end();
730                 for (; ccit != eend; ++ccit) {
731                         docstring const label = limit_string_length(ccit->str());
732                         menu->add(MenuItem(MenuItem::Command,
733                                            label,
734                                            FuncRequest(ccit->action())));
735                 }
736                 string const & floatName = floatlist.getType(cit->first).listName();
737                 MenuItem item(MenuItem::Submenu, _(floatName));
738                 item.submenu(menu.release());
739                 tomenu.add(item);
740         }
741
742         // Handle normal TOC
743         cit = toc_list.find("tableofcontents");
744         if (cit == end) {
745                 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command,
746                                     _("No Table of contents"),
747                                     FuncRequest()));
748         } else {
749                 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
750         }
751 }
752
753
754 void expandPasteRecent(Menu & tomenu, Buffer const * buf)
755 {
756         if (!buf)
757                 return;
758
759         vector<docstring> const sel =
760                 cap::availableSelections(*buf);
761
762         vector<docstring>::const_iterator cit = sel.begin();
763         vector<docstring>::const_iterator end = sel.end();
764
765         for (unsigned int index = 0; cit != end; ++cit, ++index) {
766                 tomenu.add(MenuItem(MenuItem::Command, *cit,
767                                     FuncRequest(LFUN_PASTE, convert<string>(index))));
768         }
769 }
770
771
772 void expandToolbars(Menu & tomenu)
773 {
774         //
775         // extracts the toolbars from the backend
776         ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
777         ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
778
779         for (; cit != end; ++cit) {
780                 docstring label = _(cit->gui_name);
781                 // frontends are not supposed to turn on/off toolbars, if they cannot
782                 // update ToolbarBackend::flags. That is to say, ToolbarsBackend::flags
783                 // should reflect the true state of toolbars.
784                 // 
785                 // menu is displayed as 
786                 //       on/off review
787                 // and 
788                 //              review (auto)
789                 // in the case of auto.
790                 if (cit->flags & ToolbarBackend::AUTO)
791                         label += _(" (auto)");
792                 tomenu.add(MenuItem(MenuItem::Command, label,
793                                     FuncRequest(LFUN_TOOLBAR_TOGGLE_STATE, cit->name)));
794         }
795 }
796
797
798 void expandBranches(Menu & tomenu, Buffer const * buf)
799 {
800         if (!buf)
801                 return;
802
803         BufferParams const & params = buf->getMasterBuffer()->params();
804
805         BranchList::const_iterator cit = params.branchlist().begin();
806         BranchList::const_iterator end = params.branchlist().end();
807
808         for (int ii = 1; cit != end; ++cit, ++ii) {
809                 docstring label = cit->getBranch();
810                 if (ii < 10)
811                         label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
812                 tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
813                                     FuncRequest(LFUN_BRANCH_INSERT,
814                                                 cit->getBranch())));
815         }
816 }
817
818
819 } // namespace anon
820
821
822 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
823                          Buffer const * buf) const
824 {
825         if (!tomenu.empty())
826                 tomenu.clear();
827
828         for (Menu::const_iterator cit = frommenu.begin();
829              cit != frommenu.end() ; ++cit) {
830                 switch (cit->kind()) {
831                 case MenuItem::Lastfiles:
832                         expandLastfiles(tomenu);
833                         break;
834
835                 case MenuItem::Documents:
836                         expandDocuments(tomenu);
837                         break;
838
839                 case MenuItem::Bookmarks:
840                         expandBookmarks(tomenu);
841                         break;
842
843                 case MenuItem::ImportFormats:
844                 case MenuItem::ViewFormats:
845                 case MenuItem::UpdateFormats:
846                 case MenuItem::ExportFormats:
847                         expandFormats(cit->kind(), tomenu, buf);
848                         break;
849
850                 case MenuItem::CharStyles:
851                         expandCharStyleInsert(tomenu, buf);
852                         break;
853
854                 case MenuItem::FloatListInsert:
855                         expandFloatListInsert(tomenu, buf);
856                         break;
857
858                 case MenuItem::FloatInsert:
859                         expandFloatInsert(tomenu, buf);
860                         break;
861
862                 case MenuItem::PasteRecent:
863                         expandPasteRecent(tomenu, buf);
864                         break;
865
866                 case MenuItem::Toolbars:
867                         expandToolbars(tomenu);
868                         break;
869
870                 case MenuItem::Branches:
871                         expandBranches(tomenu, buf);
872                         break;
873
874                 case MenuItem::Toc:
875                         expandToc(tomenu, buf);
876                         break;
877
878                 case MenuItem::Submenu: {
879                         MenuItem item(*cit);
880                         item.submenu(new Menu(cit->submenuname()));
881                         expand(getMenu(cit->submenuname()),
882                                *item.submenu(), buf);
883                         tomenu.addWithStatusCheck(item);
884                 }
885                 break;
886
887                 case MenuItem::Separator:
888                         tomenu.addWithStatusCheck(*cit);
889                         break;
890
891                 case MenuItem::Command:
892                         if (!specialmenu_.hasFunc(cit->func()))
893                                 tomenu.addWithStatusCheck(*cit);
894                 }
895         }
896
897         // we do not want the menu to end with a separator
898         if (!tomenu.empty()
899             && tomenu.items_.back().kind() == MenuItem::Separator)
900                 tomenu.items_.pop_back();
901
902         // Check whether the shortcuts are unique
903         tomenu.checkShortcuts();
904 }
905
906
907 void MenuBackend::read(LyXLex & lex)
908 {
909         enum Menutags {
910                 md_menu = 1,
911                 md_menubar,
912                 md_endmenuset,
913                 md_last
914         };
915
916         struct keyword_item menutags[md_last - 1] = {
917                 { "end", md_endmenuset },
918                 { "menu", md_menu },
919                 { "menubar", md_menubar }
920         };
921
922         //consistency check
923         if (compare_ascii_no_case(lex.getString(), "menuset")) {
924                 lyxerr << "Menubackend::read: ERROR wrong token:`"
925                        << lex.getString() << '\'' << endl;
926         }
927
928         lex.pushTable(menutags, md_last - 1);
929         if (lyxerr.debugging(Debug::PARSER))
930                 lex.printTable(lyxerr);
931
932         bool quit = false;
933
934         while (lex.isOK() && !quit) {
935                 switch (lex.lex()) {
936                 case md_menubar:
937                         menubar_.read(lex);
938                         break;
939                 case md_menu: {
940                         lex.next(true);
941                         docstring const name = lex.getDocString();
942                         if (hasMenu(name)) {
943                                 getMenu(name).read(lex);
944                         } else {
945                                 Menu menu(name);
946                                 menu.read(lex);
947                                 add(menu);
948                         }
949                         break;
950                 }
951                 case md_endmenuset:
952                         quit = true;
953                         break;
954                 default:
955                         lex.printError("menubackend::read: "
956                                        "Unknown menu tag: `$$Token'");
957                         break;
958                 }
959         }
960         lex.popTable();
961 }
962
963
964 void MenuBackend::add(Menu const & menu)
965 {
966         menulist_.push_back(menu);
967 }
968
969
970 bool MenuBackend::hasMenu(docstring const & name) const
971 {
972         return find_if(begin(), end(), MenuNamesEqual(name)) != end();
973 }
974
975
976 Menu const & MenuBackend::getMenu(docstring const & name) const
977 {
978         const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
979         if (cit == end())
980                 lyxerr << "No submenu named " << to_utf8(name) << endl;
981         BOOST_ASSERT(cit != end());
982         return (*cit);
983 }
984
985
986 Menu & MenuBackend::getMenu(docstring const & name)
987 {
988         iterator it = find_if(begin(), end(), MenuNamesEqual(name));
989         if (it == end())
990                 lyxerr << "No submenu named " << to_utf8(name) << endl;
991         BOOST_ASSERT(it != end());
992         return (*it);
993 }
994
995
996 Menu const & MenuBackend::getMenubar() const
997 {
998         return menubar_;
999 }
1000
1001
1002 } // namespace lyx