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