1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
10 * ====================================================== */
15 #include "MenuBackend.h"
17 #include "LyXAction.h"
21 #include "lastfiles.h"
23 #include "lyx_main.h" // for lastfiles
24 #include "bufferlist.h"
29 #include "FloatList.h"
31 #include "frontends/LyXView.h"
32 #include "support/LAssert.h"
33 #include "support/filetools.h"
34 #include "support/lyxfunctional.h"
35 #include "support/lstrings.h"
37 extern BufferList bufferlist;
38 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
47 // This is the global menu definition
48 MenuBackend menubackend;
51 MenuItem::MenuItem(Kind kind, string const & label,
52 string const & command, bool optional)
53 : kind_(kind), label_(label), optional_(optional)
68 action_ = lyxaction.LookupFunc(command);
70 if (action_ == LFUN_UNKNOWN_ACTION) {
71 lyxerr << "MenuItem(): LyX command `"
72 << command << "' does not exist." << endl;
75 lyxerr[Debug::GUI] << "Optional item "
79 submenuname_ = command;
85 MenuItem::MenuItem(Kind kind, string const & label, int action, bool optional)
86 : kind_(kind), label_(label), action_(action), submenuname_(),
95 void MenuItem::submenu(Menu * menu)
101 string const MenuItem::label() const
103 return token(label_, '|', 0);
107 string const MenuItem::shortcut() const
109 return token(label_, '|', 1);
112 string const MenuItem::binding() const
114 if (kind_ != Command)
117 // Get the keys bound to this action, but keep only the
119 string bindings = toplevel_keymap->findbinding(action_);
121 if (!bindings.empty()) {
122 return bindings.substr(1, bindings.find(']') - 1);
128 Menu & Menu::add(MenuItem const & i, LyXView const * view)
136 case MenuItem::Command:
139 view->getLyXFunc().getStatus(i.action());
141 || (status.disabled() && i.optional()))
144 items_.back().status(status);
147 case MenuItem::Submenu:
150 bool disabled = true;
151 for (const_iterator cit = i.submenu()->begin();
152 cit != i.submenu()->end(); ++cit) {
153 if ((cit->kind() == MenuItem::Command
154 || cit->kind() == MenuItem::Submenu)
155 && !cit->status().disabled()) {
160 if (!disabled || !i.optional()) {
162 items_.back().status().disabled(disabled);
169 case MenuItem::Separator:
171 && items_.back().kind() != MenuItem::Separator)
182 Menu & Menu::read(LyXLex & lex)
203 struct keyword_item menutags[md_last - 1] = {
204 { "documents", md_documents },
205 { "end", md_endmenu },
206 { "exportformats", md_exportformats },
207 { "floatinsert", md_floatinsert },
208 { "floatlistinsert", md_floatlistinsert },
209 { "importformats", md_importformats },
211 { "lastfiles", md_lastfiles },
212 { "optitem", md_optitem },
213 { "optsubmenu", md_optsubmenu },
214 { "separator", md_separator },
215 { "submenu", md_submenu },
217 { "updateformats", md_updateformats },
218 { "viewformats", md_viewformats }
221 lex.pushTable(menutags, md_last - 1);
222 if (lyxerr.debugging(Debug::PARSER))
223 lex.printTable(lyxerr);
226 bool optional = false;
228 while (lex.isOK() && !quit) {
232 // fallback to md_item
235 string const name = _(lex.getString());
237 string const command = lex.getString();
238 add(MenuItem(MenuItem::Command, name,
245 add(MenuItem(MenuItem::Separator));
249 add(MenuItem(MenuItem::Lastfiles));
253 add(MenuItem(MenuItem::Documents));
257 add(MenuItem(MenuItem::Toc));
261 add(MenuItem(MenuItem::ViewFormats));
264 case md_updateformats:
265 add(MenuItem(MenuItem::UpdateFormats));
268 case md_exportformats:
269 add(MenuItem(MenuItem::ExportFormats));
272 case md_importformats:
273 add(MenuItem(MenuItem::ImportFormats));
276 case md_floatlistinsert:
277 add(MenuItem(MenuItem::FloatListInsert));
281 add(MenuItem(MenuItem::FloatInsert));
286 // fallback to md_submenu
289 string const mlabel = _(lex.getString());
291 string const mname = lex.getString();
292 add(MenuItem(MenuItem::Submenu, mlabel, mname,
303 lex.printError("Menu::read: "
304 "Unknown menu tag: `$$Token'");
313 void Menu::checkShortcuts() const
315 // This is a quadratic algorithm, but we do not care because
316 // it is used for debugging only.
317 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
318 string shortcut = it1->shortcut();
319 if (shortcut.empty())
321 if (!contains(it1->label(), shortcut))
322 lyxerr << "Menu warning: menu entry \""
324 << "\" does not contain shortcut `"
325 << shortcut << '\'' << endl;
326 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
327 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
328 lyxerr << "Menu warning: menu entries "
329 << '"' << it1->fulllabel()
330 << "\" and \"" << it2->fulllabel()
331 << "\" share the same shortcut."
341 class compare_format {
343 bool operator()(Format const * p1, Format const * p2) {
348 string const limit_string_length(string const & str)
350 string::size_type const max_item_length = 45;
352 if (str.size() > max_item_length)
353 return str.substr(0, max_item_length - 3) + "...";
359 void expandLastfiles(Menu & tomenu, LyXView const * view)
362 LastFiles::const_iterator lfit = lastfiles->begin();
363 LastFiles::const_iterator end = lastfiles->end();
365 for (; lfit != end && ii < 10; ++lfit, ++ii) {
366 string const label = tostr(ii) + ". "
367 + MakeDisplayPath((*lfit), 30)
369 int const action = lyxaction.
370 getPseudoAction(LFUN_FILE_OPEN,
372 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
376 void expandDocuments(Menu & tomenu, LyXView const * view)
378 typedef vector<string> Strings;
379 Strings const names = bufferlist.getFileNames();
382 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
383 LFUN_NOACTION), view);
388 Strings::const_iterator docit = names.begin();
389 Strings::const_iterator end = names.end();
390 for (; docit != end; ++docit, ++ii) {
392 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
393 string label = MakeDisplayPath(*docit, 20);
395 label = tostr(ii) + ". " + label + '|' + tostr(ii);
396 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
401 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
403 if (!view->buffer() && kind != MenuItem::ImportFormats) {
404 tomenu.add(MenuItem(MenuItem::Command,
405 _("No Documents Open!"), LFUN_NOACTION),
410 typedef vector<Format const *> Formats;
415 case MenuItem::ImportFormats:
416 formats = Importer::GetImportableFormats();
417 action = LFUN_IMPORT;
419 case MenuItem::ViewFormats:
420 formats = Exporter::GetExportableFormats(view->buffer(), true);
421 action = LFUN_PREVIEW;
423 case MenuItem::UpdateFormats:
424 formats = Exporter::GetExportableFormats(view->buffer(), true);
425 action = LFUN_UPDATE;
428 formats = Exporter::GetExportableFormats(view->buffer(), false);
429 action = LFUN_EXPORT;
431 sort(formats.begin(), formats.end(), compare_format());
433 Formats::const_iterator fit = formats.begin();
434 Formats::const_iterator end = formats.end();
435 for (; fit != end ; ++fit) {
438 string label = (*fit)->prettyname();
439 // we need to hide the default graphic export formats
440 // from the external menu, because we need them only
441 // for the internal lyx-view and external latex run
442 if (label == "EPS" || label == "XPM" || label == "PNG")
445 if (kind == MenuItem::ImportFormats) {
446 if ((*fit)->name() == "text")
447 label = _("ASCII text as lines");
448 else if ((*fit)->name() == "textparagraph")
449 label = _("ASCII text as paragraphs");
452 if (!(*fit)->shortcut().empty())
453 label += '|' + (*fit)->shortcut();
454 int const action2 = lyxaction.
455 getPseudoAction(action, (*fit)->name());
456 tomenu.add(MenuItem(MenuItem::Command, label, action2),
462 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
464 if (!view->buffer()) {
465 tomenu.add(MenuItem(MenuItem::Command,
466 _("No Documents Open!"), LFUN_NOACTION),
471 FloatList const & floats =
472 view->buffer()->params.getLyXTextClass().floats();
473 FloatList::const_iterator cit = floats.begin();
474 FloatList::const_iterator end = floats.end();
475 for (; cit != end; ++cit) {
476 int const action = lyxaction
477 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
478 tomenu.add(MenuItem(MenuItem::Command,
479 _(cit->second.listName()), action),
485 void expandFloatInsert(Menu & tomenu, LyXView const * view)
487 if (!view->buffer()) {
488 tomenu.add(MenuItem(MenuItem::Command,
489 _("No Documents Open!"), LFUN_NOACTION),
494 FloatList const & floats =
495 view->buffer()->params.getLyXTextClass().floats();
496 FloatList::const_iterator cit = floats.begin();
497 FloatList::const_iterator end = floats.end();
498 for (; cit != end; ++cit) {
501 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
503 string const label = _(cit->second.name());
504 tomenu.add(MenuItem(MenuItem::Command, label, action),
510 Menu::size_type const max_number_of_items = 25;
512 void expandToc2(Menu & tomenu, toc::Toc const & toc_list,
513 toc::Toc::size_type from, toc::Toc::size_type to, int depth)
515 int shortcut_count = 0;
516 if (to - from <= max_number_of_items) {
517 for (toc::Toc::size_type i = from; i < to; ++i) {
518 int const action = toc_list[i].action();
519 string label(4 * max(0, toc_list[i].depth - depth),' ');
520 label += limit_string_length(toc_list[i].str);
521 if (toc_list[i].depth == depth
522 && ++shortcut_count <= 9) {
523 label += '|' + tostr(shortcut_count);
525 tomenu.add(MenuItem(MenuItem::Command, label, action));
528 toc::Toc::size_type pos = from;
530 toc::Toc::size_type new_pos = pos + 1;
531 while (new_pos < to &&
532 toc_list[new_pos].depth > depth)
535 int const action = toc_list[pos].action();
536 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
537 label += limit_string_length(toc_list[pos].str);
538 if (toc_list[pos].depth == depth &&
539 ++shortcut_count <= 9)
540 label += '|' + tostr(shortcut_count);
542 if (new_pos == pos + 1) {
543 tomenu.add(MenuItem(MenuItem::Command,
546 MenuItem item(MenuItem::Submenu, label);
547 item.submenu(new Menu);
548 expandToc2(*item.submenu(),
549 toc_list, pos, new_pos, depth + 1);
558 void expandToc(Menu & tomenu, LyXView const * view)
560 // To make things very cleanly, we would have to pass view to
561 // all MenuItem constructors and to expandToc2. However, we
562 // know that all the entries in a TOC will be have status_ ==
563 // OK, so we avoid this unnecessary overhead (JMarc)
565 if (!view->buffer()) {
566 tomenu.add(MenuItem(MenuItem::Command,
567 _("No Documents Open!"), LFUN_NOACTION),
572 toc::TocList toc_list = toc::getTocList(view->buffer());
573 toc::TocList::const_iterator cit = toc_list.begin();
574 toc::TocList::const_iterator end = toc_list.end();
575 for (; cit != end; ++cit) {
577 if (cit->first == "TOC")
580 // All the rest is for floats
581 Menu * menu = new Menu;
582 toc::Toc::const_iterator ccit = cit->second.begin();
583 toc::Toc::const_iterator eend = cit->second.end();
584 for (; ccit != eend; ++ccit) {
585 string const label = limit_string_length(ccit->str);
586 menu->add(MenuItem(MenuItem::Command,
587 label, ccit->action()));
589 string const & floatName = cit->first;
590 // Is the _(...) really needed here? (Lgb)
591 MenuItem item(MenuItem::Submenu, _(floatName));
597 cit = toc_list.find("TOC");
599 tomenu.add(MenuItem(MenuItem::Command,
600 _("No Table of contents")),
603 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
611 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
612 LyXView const * view) const
614 for (Menu::const_iterator cit = frommenu.begin();
615 cit != frommenu.end() ; ++cit) {
616 switch (cit->kind()) {
617 case MenuItem::Lastfiles:
618 expandLastfiles(tomenu, view);
621 case MenuItem::Documents:
622 expandDocuments(tomenu, view);
625 case MenuItem::ImportFormats:
626 case MenuItem::ViewFormats:
627 case MenuItem::UpdateFormats:
628 case MenuItem::ExportFormats:
629 expandFormats(cit->kind(), tomenu, view);
632 case MenuItem::FloatListInsert:
633 expandFloatListInsert(tomenu, view);
636 case MenuItem::FloatInsert:
637 expandFloatInsert(tomenu, view);
641 expandToc(tomenu, view);
644 case MenuItem::Submenu: {
646 item.submenu(new Menu(cit->submenuname()));
647 expand(getMenu(cit->submenuname()),
648 *item.submenu(), view);
649 tomenu.add(item, view);
654 tomenu.add(*cit, view);
658 // we do not want the menu to end with a separator
660 && tomenu.items_.back().kind() == MenuItem::Separator)
661 tomenu.items_.pop_back();
663 // Check whether the shortcuts are unique
664 if (lyxerr.debugging(Debug::GUI))
665 tomenu.checkShortcuts();
669 bool Menu::hasSubmenu(string const & name) const
671 return find_if(begin(), end(),
672 lyx::compare_memfun(&MenuItem::submenuname,
677 void MenuBackend::read(LyXLex & lex)
686 struct keyword_item menutags[md_last - 1] = {
687 { "end", md_endmenuset },
689 { "menubar", md_menubar }
693 if (compare_ascii_no_case(lex.getString(), "menuset")) {
694 lyxerr << "Menubackend::read: ERROR wrong token:`"
695 << lex.getString() << '\'' << endl;
698 lex.pushTable(menutags, md_last - 1);
699 if (lyxerr.debugging(Debug::PARSER))
700 lex.printTable(lyxerr);
704 while (lex.isOK() && !quit) {
711 string const name = lex.getString();
713 getMenu(name).read(lex);
725 lex.printError("menubackend::read: "
726 "Unknown menu tag: `$$Token'");
734 void MenuBackend::add(Menu const & menu)
736 menulist_.push_back(menu);
740 bool MenuBackend::hasMenu(string const & name) const
742 return find_if(begin(), end(),
743 lyx::compare_memfun(&Menu::name, name)) != end();
747 Menu const & MenuBackend::getMenu(string const & name) const
749 const_iterator cit = find_if(begin(), end(),
750 lyx::compare_memfun(&Menu::name, name));
752 lyxerr << "No submenu named " << name << endl;
753 lyx::Assert(cit != end());
758 Menu & MenuBackend::getMenu(string const & name)
760 MenuList::iterator it =
761 find_if(menulist_.begin(), menulist_.end(),
762 lyx::compare_memfun(&Menu::name, name));
763 lyx::Assert(it != menulist_.end());
768 Menu const & MenuBackend::getMenubar() const