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 #pragma implementation
19 #include "MenuBackend.h"
21 #include "LyXAction.h"
24 #include "lastfiles.h"
25 #include "lyx_main.h" // for lastfiles
26 #include "bufferlist.h"
28 #include "converter.h"
31 #include "FloatList.h"
33 #include "support/LAssert.h"
34 #include "support/filetools.h"
35 #include "support/lyxfunctional.h"
36 #include "support/lstrings.h"
38 extern BufferList bufferlist;
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);
113 Menu & Menu::add(MenuItem const & i)
120 Menu & Menu::read(LyXLex & lex)
140 struct keyword_item menutags[md_last - 1] = {
141 { "documents", md_documents },
142 { "end", md_endmenu },
143 { "exportformats", md_exportformats },
144 { "floatinsert", md_floatinsert },
145 { "floatlistinsert", md_floatlistinsert },
146 { "importformats", md_importformats },
148 { "lastfiles", md_lastfiles },
149 { "optitem", md_optitem },
150 { "separator", md_separator },
151 { "submenu", md_submenu },
153 { "updateformats", md_updateformats },
154 { "viewformats", md_viewformats }
157 lex.pushTable(menutags, md_last - 1);
158 if (lyxerr.debugging(Debug::PARSER))
159 lex.printTable(lyxerr);
162 bool optional = false;
164 while (lex.isOK() && !quit) {
168 // fallback to md_item
171 string const name = _(lex.getString());
173 string const command = lex.getString();
174 add(MenuItem(MenuItem::Command, name,
181 add(MenuItem(MenuItem::Separator));
185 add(MenuItem(MenuItem::Lastfiles));
189 add(MenuItem(MenuItem::Documents));
193 add(MenuItem(MenuItem::Toc));
197 add(MenuItem(MenuItem::ViewFormats));
200 case md_updateformats:
201 add(MenuItem(MenuItem::UpdateFormats));
204 case md_exportformats:
205 add(MenuItem(MenuItem::ExportFormats));
208 case md_importformats:
209 add(MenuItem(MenuItem::ImportFormats));
212 case md_floatlistinsert:
213 add(MenuItem(MenuItem::FloatListInsert));
217 add(MenuItem(MenuItem::FloatInsert));
222 string const mlabel = _(lex.getString());
224 string const mname = lex.getString();
225 add(MenuItem(MenuItem::Submenu, mlabel, mname));
234 lex.printError("Menu::read: "
235 "Unknown menu tag: `$$Token'");
244 void Menu::checkShortcuts() const
246 // This is a quadratic algorithm, but we do not care because
247 // it is used for debugging only.
248 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
249 string shortcut = it1->shortcut();
250 if (shortcut.empty())
252 if (!contains(it1->label(), shortcut))
253 lyxerr << "Menu warning: menu entry \""
255 << "\" does not contain shortcut `"
256 << shortcut << '\'' << endl;
257 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
258 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
259 lyxerr << "Menu warning: menu entries "
260 << '"' << it1->fulllabel()
261 << "\" and \"" << it2->fulllabel()
262 << "\" share the same shortcut."
272 class compare_format {
274 bool operator()(Format const * p1, Format const * p2) {
279 string const limit_string_length(string const & str)
281 string::size_type const max_item_length = 45;
283 if (str.size() > max_item_length)
284 return str.substr(0, max_item_length - 3) + "...";
290 void expandLastfiles(Menu & tomenu)
293 LastFiles::const_iterator lfit = lastfiles->begin();
294 LastFiles::const_iterator end = lastfiles->end();
296 for (; lfit != end && ii < 10; ++lfit, ++ii) {
297 string const label = tostr(ii) + ". "
298 + MakeDisplayPath((*lfit), 30)
300 int const action = lyxaction.
301 getPseudoAction(LFUN_FILE_OPEN,
303 tomenu.add(MenuItem(MenuItem::Command,
308 void expandDocuments(Menu & tomenu)
310 typedef vector<string> Strings;
311 Strings const names = bufferlist.getFileNames();
314 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
320 Strings::const_iterator docit = names.begin();
321 Strings::const_iterator end = names.end();
322 for (; docit != end; ++docit, ++ii) {
324 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
325 string label = MakeDisplayPath(*docit, 30);
327 label = tostr(ii) + ". " + label + '|' + tostr(ii);
328 tomenu.add(MenuItem(MenuItem::Command, label, action));
333 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
335 if (!buf && kind != MenuItem::ImportFormats) {
336 tomenu.add(MenuItem(MenuItem::Command,
337 _("No Documents Open!"), LFUN_NOACTION));
341 typedef vector<Format const *> Formats;
346 case MenuItem::ImportFormats:
347 formats = Importer::GetImportableFormats();
348 action = LFUN_IMPORT;
350 case MenuItem::ViewFormats:
351 formats = Exporter::GetExportableFormats(buf, true);
352 action = LFUN_PREVIEW;
354 case MenuItem::UpdateFormats:
355 formats = Exporter::GetExportableFormats(buf, false);
356 action = LFUN_UPDATE;
359 formats = Exporter::GetExportableFormats(buf, false);
360 action = LFUN_EXPORT;
362 sort(formats.begin(), formats.end(), compare_format());
364 Formats::const_iterator fit = formats.begin();
365 Formats::const_iterator end = formats.end();
366 for (; fit != end ; ++fit) {
369 string label = (*fit)->prettyname();
370 // we need to hide the default graphic export formats
371 // from the external menu, because we need them only
372 // for the internal lyx-view and external latex run
373 if (label == "EPS" || label == "XPM" || label == "PNG")
376 if (kind == MenuItem::ImportFormats) {
377 if ((*fit)->name() == "text")
378 label = _("ASCII text as lines");
379 else if ((*fit)->name() == "textparagraph")
380 label = _("ASCII text as paragraphs");
383 if (!(*fit)->shortcut().empty())
384 label += '|' + (*fit)->shortcut();
385 int const action2 = lyxaction.
386 getPseudoAction(action, (*fit)->name());
387 tomenu.add(MenuItem(MenuItem::Command, label, action2));
392 void expandFloatListInsert(Menu & tomenu, Buffer const * buf)
395 tomenu.add(MenuItem(MenuItem::Command,
396 _("No Documents Open!"), LFUN_NOACTION));
400 FloatList const & floats = buf->params.getLyXTextClass().floats();
401 FloatList::const_iterator cit = floats.begin();
402 FloatList::const_iterator end = floats.end();
403 for (; cit != end; ++cit) {
404 int const action = lyxaction
405 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
406 tomenu.add(MenuItem(MenuItem::Command,
407 _(cit->second.listName()),
413 void expandFloatInsert(Menu & tomenu, Buffer const * buf)
416 tomenu.add(MenuItem(MenuItem::Command,
417 _("No Documents Open!"), LFUN_NOACTION));
421 FloatList const & floats = buf->params.getLyXTextClass().floats();
422 FloatList::const_iterator cit = floats.begin();
423 FloatList::const_iterator end = floats.end();
424 for (; cit != end; ++cit) {
427 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
429 string const label = _(cit->second.name());
430 tomenu.add(MenuItem(MenuItem::Command, label, action));
435 Menu::size_type const max_number_of_items = 25;
437 void expandToc2(Menu & tomenu, toc::Toc const & toc_list,
438 toc::Toc::size_type from, toc::Toc::size_type to, int depth)
440 int shortcut_count = 0;
441 if (to - from <= max_number_of_items) {
442 for (toc::Toc::size_type i = from; i < to; ++i) {
443 int const action = toc_list[i].action();
444 string label(4 * max(0, toc_list[i].depth - depth),' ');
445 label += limit_string_length(toc_list[i].str);
446 if (toc_list[i].depth == depth
447 && ++shortcut_count <= 9) {
448 label += '|' + tostr(shortcut_count);
450 tomenu.add(MenuItem(MenuItem::Command, label, action));
453 toc::Toc::size_type pos = from;
455 toc::Toc::size_type new_pos = pos + 1;
456 while (new_pos < to &&
457 toc_list[new_pos].depth > depth)
460 int const action = toc_list[pos].action();
461 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
462 label += limit_string_length(toc_list[pos].str);
463 if (toc_list[pos].depth == depth &&
464 ++shortcut_count <= 9)
465 label += '|' + tostr(shortcut_count);
467 if (new_pos == pos + 1) {
468 tomenu.add(MenuItem(MenuItem::Command,
471 MenuItem item(MenuItem::Submenu, label);
472 item.submenu(new Menu);
473 expandToc2(*item.submenu(),
474 toc_list, pos, new_pos, depth + 1);
483 void expandToc(Menu & tomenu, Buffer const * buf)
486 tomenu.add(MenuItem(MenuItem::Command,
487 _("No Documents Open!"), LFUN_NOACTION));
491 toc::TocList toc_list = toc::getTocList(buf);
492 toc::TocList::const_iterator cit = toc_list.begin();
493 toc::TocList::const_iterator end = toc_list.end();
494 for (; cit != end; ++cit) {
496 if (cit->first == "TOC")
499 // All the rest is for floats
500 Menu * menu = new Menu;
501 toc::Toc::const_iterator ccit = cit->second.begin();
502 toc::Toc::const_iterator eend = cit->second.end();
503 for (; ccit != eend; ++ccit) {
504 string const label = limit_string_length(ccit->str);
505 menu->add(MenuItem(MenuItem::Command,
506 label, ccit->action()));
508 string const & floatName = cit->first;
509 // Is the _(...) really needed here? (Lgb)
510 MenuItem item(MenuItem::Submenu, _(floatName));
516 cit = toc_list.find("TOC");
518 tomenu.add(MenuItem(MenuItem::Command,
519 _("No Table of contents")));
521 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
529 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
530 Buffer const * buf) const
532 for (Menu::const_iterator cit = frommenu.begin();
533 cit != frommenu.end() ; ++cit) {
534 switch (cit->kind()) {
535 case MenuItem::Lastfiles:
536 expandLastfiles(tomenu);
539 case MenuItem::Documents:
540 expandDocuments(tomenu);
543 case MenuItem::ImportFormats:
544 case MenuItem::ViewFormats:
545 case MenuItem::UpdateFormats:
546 case MenuItem::ExportFormats:
547 expandFormats(cit->kind(), tomenu, buf);
550 case MenuItem::FloatListInsert:
551 expandFloatListInsert(tomenu, buf);
554 case MenuItem::FloatInsert:
555 expandFloatInsert(tomenu, buf);
559 expandToc(tomenu, buf);
562 case MenuItem::Submenu: {
564 item.submenu(new Menu(cit->submenuname()));
565 expand(getMenu(cit->submenuname()),
566 *item.submenu(), buf);
576 // Check whether the shortcuts are unique
577 if (lyxerr.debugging(Debug::GUI))
578 tomenu.checkShortcuts();
582 bool Menu::hasSubmenu(string const & name) const
584 return find_if(begin(), end(),
585 lyx::compare_memfun(&MenuItem::submenuname,
590 void MenuBackend::read(LyXLex & lex)
599 struct keyword_item menutags[md_last - 1] = {
600 { "end", md_endmenuset },
602 { "menubar", md_menubar }
606 if (compare_ascii_no_case(lex.getString(), "menuset")) {
607 lyxerr << "Menubackend::read: ERROR wrong token:`"
608 << lex.getString() << '\'' << endl;
611 lex.pushTable(menutags, md_last - 1);
612 if (lyxerr.debugging(Debug::PARSER))
613 lex.printTable(lyxerr);
617 while (lex.isOK() && !quit) {
624 string const name = lex.getString();
626 getMenu(name).read(lex);
638 lex.printError("menubackend::read: "
639 "Unknown menu tag: `$$Token'");
647 void MenuBackend::defaults()
651 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
656 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
657 .add(MenuItem(MenuItem::Command, _("Open...|O"), "file-open"))
658 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
659 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
660 .add(MenuItem(MenuItem::Separator))
661 .add(MenuItem(MenuItem::Lastfiles));
664 Menu import("import");
666 .add(MenuItem(MenuItem::Command,
667 _("LaTeX...|L"), "buffer-import latex"))
668 .add(MenuItem(MenuItem::Command,
669 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
674 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
675 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
676 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
677 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
680 Menu documents("documents");
681 documents.add(MenuItem(MenuItem::Documents));
684 menubar_.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
685 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
686 .add(MenuItem(MenuItem::Submenu,
687 _("Documents|D"), "documents"));
692 void MenuBackend::add(Menu const & menu)
694 menulist_.push_back(menu);
698 bool MenuBackend::hasMenu(string const & name) const
700 return find_if(begin(), end(),
701 lyx::compare_memfun(&Menu::name, name)) != end();
705 Menu const & MenuBackend::getMenu(string const & name) const
707 const_iterator cit = find_if(begin(), end(),
708 lyx::compare_memfun(&Menu::name, name));
710 lyxerr << "No submenu named " << name << endl;
711 lyx::Assert(cit != end());
716 Menu & MenuBackend::getMenu(string const & name)
718 MenuList::iterator it =
719 find_if(menulist_.begin(), menulist_.end(),
720 lyx::compare_memfun(&Menu::name, name));
721 lyx::Assert(it != menulist_.end());
726 Menu const & MenuBackend::getMenubar() const