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