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