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