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 * ====================================================== */
14 #include "MenuBackend.h"
16 #include "LyXAction.h"
20 #include "lastfiles.h"
22 #include "lyx_main.h" // for lastfiles
23 #include "bufferlist.h"
28 #include "FloatList.h"
30 #include "frontends/LyXView.h"
31 #include "support/LAssert.h"
32 #include "support/filetools.h"
33 #include "support/lyxfunctional.h"
34 #include "support/lstrings.h"
35 #include "support/tostr.h"
39 extern BufferList bufferlist;
40 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
49 // This is the global menu definition
50 MenuBackend menubackend;
53 MenuItem::MenuItem(Kind kind, string const & label,
54 string const & command, bool optional)
55 : kind_(kind), label_(label), optional_(optional)
70 action_ = lyxaction.LookupFunc(command);
72 if (action_ == LFUN_UNKNOWN_ACTION) {
73 lyxerr << "MenuItem(): LyX command `"
74 << command << "' does not exist." << endl;
77 lyxerr[Debug::GUI] << "Optional item "
81 submenuname_ = command;
87 MenuItem::MenuItem(Kind kind, string const & label, int action, bool optional)
88 : kind_(kind), label_(label), action_(action), submenuname_(),
97 void MenuItem::submenu(Menu * menu)
103 string const MenuItem::label() const
105 return token(label_, '|', 0);
109 string const MenuItem::shortcut() const
111 return token(label_, '|', 1);
114 string const MenuItem::binding() const
116 if (kind_ != Command)
119 // Get the keys bound to this action, but keep only the
121 string bindings = toplevel_keymap->findbinding(action_);
123 if (!bindings.empty()) {
124 return bindings.substr(1, bindings.find(']') - 1);
130 Menu & Menu::add(MenuItem const & i, LyXView const * view)
138 case MenuItem::Command:
141 view->getLyXFunc().getStatus(i.action());
143 || (status.disabled() && i.optional()))
146 items_.back().status(status);
149 case MenuItem::Submenu:
152 bool disabled = true;
153 for (const_iterator cit = i.submenu()->begin();
154 cit != i.submenu()->end(); ++cit) {
155 if ((cit->kind() == MenuItem::Command
156 || cit->kind() == MenuItem::Submenu)
157 && !cit->status().disabled()) {
162 if (!disabled || !i.optional()) {
164 items_.back().status().disabled(disabled);
171 case MenuItem::Separator:
173 && items_.back().kind() != MenuItem::Separator)
184 Menu & Menu::read(LyXLex & lex)
205 struct keyword_item menutags[md_last - 1] = {
206 { "documents", md_documents },
207 { "end", md_endmenu },
208 { "exportformats", md_exportformats },
209 { "floatinsert", md_floatinsert },
210 { "floatlistinsert", md_floatlistinsert },
211 { "importformats", md_importformats },
213 { "lastfiles", md_lastfiles },
214 { "optitem", md_optitem },
215 { "optsubmenu", md_optsubmenu },
216 { "separator", md_separator },
217 { "submenu", md_submenu },
219 { "updateformats", md_updateformats },
220 { "viewformats", md_viewformats }
223 lex.pushTable(menutags, md_last - 1);
224 if (lyxerr.debugging(Debug::PARSER))
225 lex.printTable(lyxerr);
228 bool optional = false;
230 while (lex.isOK() && !quit) {
234 // fallback to md_item
237 string const name = _(lex.getString());
239 string const command = lex.getString();
240 add(MenuItem(MenuItem::Command, name,
247 add(MenuItem(MenuItem::Separator));
251 add(MenuItem(MenuItem::Lastfiles));
255 add(MenuItem(MenuItem::Documents));
259 add(MenuItem(MenuItem::Toc));
263 add(MenuItem(MenuItem::ViewFormats));
266 case md_updateformats:
267 add(MenuItem(MenuItem::UpdateFormats));
270 case md_exportformats:
271 add(MenuItem(MenuItem::ExportFormats));
274 case md_importformats:
275 add(MenuItem(MenuItem::ImportFormats));
278 case md_floatlistinsert:
279 add(MenuItem(MenuItem::FloatListInsert));
283 add(MenuItem(MenuItem::FloatInsert));
288 // fallback to md_submenu
291 string const mlabel = _(lex.getString());
293 string const mname = lex.getString();
294 add(MenuItem(MenuItem::Submenu, mlabel, mname,
305 lex.printError("Menu::read: "
306 "Unknown menu tag: `$$Token'");
315 void Menu::checkShortcuts() const
317 // This is a quadratic algorithm, but we do not care because
318 // it is used for debugging only.
319 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
320 string shortcut = it1->shortcut();
321 if (shortcut.empty())
323 if (!contains(it1->label(), shortcut))
324 lyxerr << "Menu warning: menu entry \""
326 << "\" does not contain shortcut `"
327 << shortcut << '\'' << endl;
328 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
329 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
330 lyxerr << "Menu warning: menu entries "
331 << '"' << it1->fulllabel()
332 << "\" and \"" << it2->fulllabel()
333 << "\" share the same shortcut."
343 class compare_format {
345 bool operator()(Format const * p1, Format const * p2) {
350 string const limit_string_length(string const & str)
352 string::size_type const max_item_length = 45;
354 if (str.size() > max_item_length)
355 return str.substr(0, max_item_length - 3) + "...";
361 void expandLastfiles(Menu & tomenu, LyXView const * view)
364 LastFiles::const_iterator lfit = lastfiles->begin();
365 LastFiles::const_iterator end = lastfiles->end();
367 for (; lfit != end && ii < 10; ++lfit, ++ii) {
368 string const label = tostr(ii) + ". "
369 + MakeDisplayPath((*lfit), 30)
371 int const action = lyxaction.
372 getPseudoAction(LFUN_FILE_OPEN,
374 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
378 void expandDocuments(Menu & tomenu, LyXView const * view)
380 typedef vector<string> Strings;
381 Strings const names = bufferlist.getFileNames();
384 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
385 LFUN_NOACTION), view);
390 Strings::const_iterator docit = names.begin();
391 Strings::const_iterator end = names.end();
392 for (; docit != end; ++docit, ++ii) {
394 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
395 string label = MakeDisplayPath(*docit, 20);
397 label = tostr(ii) + ". " + label + '|' + tostr(ii);
398 tomenu.add(MenuItem(MenuItem::Command, label, action), view);
403 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
405 if (!view->buffer() && kind != MenuItem::ImportFormats) {
406 tomenu.add(MenuItem(MenuItem::Command,
407 _("No Documents Open!"), LFUN_NOACTION),
412 typedef vector<Format const *> Formats;
417 case MenuItem::ImportFormats:
418 formats = Importer::GetImportableFormats();
419 action = LFUN_IMPORT;
421 case MenuItem::ViewFormats:
422 formats = Exporter::GetExportableFormats(view->buffer(), true);
423 action = LFUN_PREVIEW;
425 case MenuItem::UpdateFormats:
426 formats = Exporter::GetExportableFormats(view->buffer(), true);
427 action = LFUN_UPDATE;
430 formats = Exporter::GetExportableFormats(view->buffer(), false);
431 action = LFUN_EXPORT;
433 sort(formats.begin(), formats.end(), compare_format());
435 Formats::const_iterator fit = formats.begin();
436 Formats::const_iterator end = formats.end();
437 for (; fit != end ; ++fit) {
440 string label = (*fit)->prettyname();
441 // we need to hide the default graphic export formats
442 // from the external menu, because we need them only
443 // for the internal lyx-view and external latex run
444 if (label == "EPS" || label == "XPM" || label == "PNG")
447 if (kind == MenuItem::ImportFormats) {
448 if ((*fit)->name() == "text")
449 label = _("ASCII text as lines");
450 else if ((*fit)->name() == "textparagraph")
451 label = _("ASCII text as paragraphs");
454 if (!(*fit)->shortcut().empty())
455 label += '|' + (*fit)->shortcut();
456 int const action2 = lyxaction.
457 getPseudoAction(action, (*fit)->name());
458 tomenu.add(MenuItem(MenuItem::Command, label, action2),
464 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
466 if (!view->buffer()) {
467 tomenu.add(MenuItem(MenuItem::Command,
468 _("No Documents Open!"), LFUN_NOACTION),
473 FloatList const & floats =
474 view->buffer()->params.getLyXTextClass().floats();
475 FloatList::const_iterator cit = floats.begin();
476 FloatList::const_iterator end = floats.end();
477 for (; cit != end; ++cit) {
478 int const action = lyxaction
479 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
480 tomenu.add(MenuItem(MenuItem::Command,
481 _(cit->second.listName()), action),
487 void expandFloatInsert(Menu & tomenu, LyXView const * view)
489 if (!view->buffer()) {
490 tomenu.add(MenuItem(MenuItem::Command,
491 _("No Documents Open!"), LFUN_NOACTION),
496 FloatList const & floats =
497 view->buffer()->params.getLyXTextClass().floats();
498 FloatList::const_iterator cit = floats.begin();
499 FloatList::const_iterator end = floats.end();
500 for (; cit != end; ++cit) {
503 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
505 string const label = _(cit->second.name());
506 tomenu.add(MenuItem(MenuItem::Command, label, action),
512 Menu::size_type const max_number_of_items = 25;
514 void expandToc2(Menu & tomenu, toc::Toc const & toc_list,
515 toc::Toc::size_type from, toc::Toc::size_type to, int depth)
517 int shortcut_count = 0;
518 if (to - from <= max_number_of_items) {
519 for (toc::Toc::size_type i = from; i < to; ++i) {
520 int const action = toc_list[i].action();
521 string label(4 * max(0, toc_list[i].depth - depth),' ');
522 label += limit_string_length(toc_list[i].str);
523 if (toc_list[i].depth == depth
524 && ++shortcut_count <= 9) {
525 label += '|' + tostr(shortcut_count);
527 tomenu.add(MenuItem(MenuItem::Command, label, action));
530 toc::Toc::size_type pos = from;
532 toc::Toc::size_type new_pos = pos + 1;
533 while (new_pos < to &&
534 toc_list[new_pos].depth > depth)
537 int const action = toc_list[pos].action();
538 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
539 label += limit_string_length(toc_list[pos].str);
540 if (toc_list[pos].depth == depth &&
541 ++shortcut_count <= 9)
542 label += '|' + tostr(shortcut_count);
544 if (new_pos == pos + 1) {
545 tomenu.add(MenuItem(MenuItem::Command,
548 MenuItem item(MenuItem::Submenu, label);
549 item.submenu(new Menu);
550 expandToc2(*item.submenu(),
551 toc_list, pos, new_pos, depth + 1);
560 void expandToc(Menu & tomenu, LyXView const * view)
562 // To make things very cleanly, we would have to pass view to
563 // all MenuItem constructors and to expandToc2. However, we
564 // know that all the entries in a TOC will be have status_ ==
565 // OK, so we avoid this unnecessary overhead (JMarc)
567 if (!view->buffer()) {
568 tomenu.add(MenuItem(MenuItem::Command,
569 _("No Documents Open!"), LFUN_NOACTION),
574 toc::TocList toc_list = toc::getTocList(view->buffer());
575 toc::TocList::const_iterator cit = toc_list.begin();
576 toc::TocList::const_iterator end = toc_list.end();
577 for (; cit != end; ++cit) {
579 if (cit->first == "TOC")
582 // All the rest is for floats
583 Menu * menu = new Menu;
584 toc::Toc::const_iterator ccit = cit->second.begin();
585 toc::Toc::const_iterator eend = cit->second.end();
586 for (; ccit != eend; ++ccit) {
587 string const label = limit_string_length(ccit->str);
588 menu->add(MenuItem(MenuItem::Command,
589 label, ccit->action()));
591 string const & floatName = cit->first;
592 // Is the _(...) really needed here? (Lgb)
593 MenuItem item(MenuItem::Submenu, _(floatName));
599 cit = toc_list.find("TOC");
601 tomenu.add(MenuItem(MenuItem::Command,
602 _("No Table of contents")),
605 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
613 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
614 LyXView const * view) const
616 for (Menu::const_iterator cit = frommenu.begin();
617 cit != frommenu.end() ; ++cit) {
618 switch (cit->kind()) {
619 case MenuItem::Lastfiles:
620 expandLastfiles(tomenu, view);
623 case MenuItem::Documents:
624 expandDocuments(tomenu, view);
627 case MenuItem::ImportFormats:
628 case MenuItem::ViewFormats:
629 case MenuItem::UpdateFormats:
630 case MenuItem::ExportFormats:
631 expandFormats(cit->kind(), tomenu, view);
634 case MenuItem::FloatListInsert:
635 expandFloatListInsert(tomenu, view);
638 case MenuItem::FloatInsert:
639 expandFloatInsert(tomenu, view);
643 expandToc(tomenu, view);
646 case MenuItem::Submenu: {
648 item.submenu(new Menu(cit->submenuname()));
649 expand(getMenu(cit->submenuname()),
650 *item.submenu(), view);
651 tomenu.add(item, view);
656 tomenu.add(*cit, view);
660 // we do not want the menu to end with a separator
662 && tomenu.items_.back().kind() == MenuItem::Separator)
663 tomenu.items_.pop_back();
665 // Check whether the shortcuts are unique
666 if (lyxerr.debugging(Debug::GUI))
667 tomenu.checkShortcuts();
671 bool Menu::hasSubmenu(string const & name) const
673 return find_if(begin(), end(),
674 lyx::compare_memfun(&MenuItem::submenuname,
679 void MenuBackend::read(LyXLex & lex)
688 struct keyword_item menutags[md_last - 1] = {
689 { "end", md_endmenuset },
691 { "menubar", md_menubar }
695 if (compare_ascii_no_case(lex.getString(), "menuset")) {
696 lyxerr << "Menubackend::read: ERROR wrong token:`"
697 << lex.getString() << '\'' << endl;
700 lex.pushTable(menutags, md_last - 1);
701 if (lyxerr.debugging(Debug::PARSER))
702 lex.printTable(lyxerr);
706 while (lex.isOK() && !quit) {
713 string const name = lex.getString();
715 getMenu(name).read(lex);
727 lex.printError("menubackend::read: "
728 "Unknown menu tag: `$$Token'");
736 void MenuBackend::add(Menu const & menu)
738 menulist_.push_back(menu);
742 bool MenuBackend::hasMenu(string const & name) const
744 return find_if(begin(), end(),
745 lyx::compare_memfun(&Menu::name, name)) != end();
749 Menu const & MenuBackend::getMenu(string const & name) const
751 const_iterator cit = find_if(begin(), end(),
752 lyx::compare_memfun(&Menu::name, name));
754 lyxerr << "No submenu named " << name << endl;
755 lyx::Assert(cit != end());
760 Menu & MenuBackend::getMenu(string const & name)
762 MenuList::iterator it =
763 find_if(menulist_.begin(), menulist_.end(),
764 lyx::compare_memfun(&Menu::name, name));
765 lyx::Assert(it != menulist_.end());
770 Menu const & MenuBackend::getMenubar() const