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"
27 #include "converter.h"
30 #include "FloatList.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;
46 // This is the global menu definition
47 MenuBackend menubackend;
50 MenuItem::MenuItem(Kind kind, string const & label,
51 string const & command, bool optional)
52 : kind_(kind), label_(label), optional_(optional)
67 action_ = lyxaction.LookupFunc(command);
69 if (action_ == LFUN_UNKNOWN_ACTION) {
70 lyxerr << "MenuItem(): LyX command `"
71 << command << "' does not exist." << endl;
74 lyxerr[Debug::GUI] << "Optional item "
78 submenuname_ = command;
84 MenuItem::MenuItem(Kind kind, string const & label, int action, bool optional)
85 : kind_(kind), label_(label), action_(action), submenuname_(),
94 void MenuItem::submenu(Menu * menu)
100 string const MenuItem::label() const
102 return token(label_, '|', 0);
106 string const MenuItem::shortcut() const
108 return token(label_, '|', 1);
112 Menu & Menu::add(MenuItem const & i)
119 Menu & Menu::read(LyXLex & lex)
139 struct keyword_item menutags[md_last - 1] = {
140 { "documents", md_documents },
141 { "end", md_endmenu },
142 { "exportformats", md_exportformats },
143 { "floatinsert", md_floatinsert },
144 { "floatlistinsert", md_floatlistinsert },
145 { "importformats", md_importformats },
147 { "lastfiles", md_lastfiles },
148 { "optitem", md_optitem },
149 { "separator", md_separator },
150 { "submenu", md_submenu },
152 { "updateformats", md_updateformats },
153 { "viewformats", md_viewformats }
156 lex.pushTable(menutags, md_last - 1);
157 if (lyxerr.debugging(Debug::PARSER))
158 lex.printTable(lyxerr);
161 bool optional = false;
163 while (lex.isOK() && !quit) {
167 // fallback to md_item
170 string const name = _(lex.getString());
172 string const command = lex.getString();
173 add(MenuItem(MenuItem::Command, name,
180 add(MenuItem(MenuItem::Separator));
184 add(MenuItem(MenuItem::Lastfiles));
188 add(MenuItem(MenuItem::Documents));
192 add(MenuItem(MenuItem::Toc));
196 add(MenuItem(MenuItem::ViewFormats));
199 case md_updateformats:
200 add(MenuItem(MenuItem::UpdateFormats));
203 case md_exportformats:
204 add(MenuItem(MenuItem::ExportFormats));
207 case md_importformats:
208 add(MenuItem(MenuItem::ImportFormats));
211 case md_floatlistinsert:
212 add(MenuItem(MenuItem::FloatListInsert));
216 add(MenuItem(MenuItem::FloatInsert));
221 string const mlabel = _(lex.getString());
223 string const mname = lex.getString();
224 add(MenuItem(MenuItem::Submenu, mlabel, mname));
233 lex.printError("Menu::read: "
234 "Unknown menu tag: `$$Token'");
243 void Menu::checkShortcuts() const
245 // This is a quadratic algorithm, but we do not care because
246 // it is used for debugging only.
247 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
248 string shortcut = it1->shortcut();
249 if (shortcut.empty())
251 if (!contains(it1->label(), shortcut))
252 lyxerr << "Menu warning: menu entry \""
254 << "\" does not contain shortcut `"
255 << shortcut << '\'' << endl;
256 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
257 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
258 lyxerr << "Menu warning: menu entries "
259 << '"' << it1->fulllabel()
260 << "\" and \"" << it2->fulllabel()
261 << "\" share the same shortcut."
271 class compare_format {
273 bool operator()(Format const * p1, Format const * p2) {
278 string const limit_string_length(string const & str)
280 string::size_type const max_item_length = 45;
282 if (str.size() > max_item_length)
283 return str.substr(0, max_item_length - 3) + "...";
289 void expandLastfiles(Menu & tomenu)
292 LastFiles::const_iterator lfit = lastfiles->begin();
293 LastFiles::const_iterator end = lastfiles->end();
295 for (; lfit != end && ii < 10; ++lfit, ++ii) {
296 string const label = tostr(ii) + ". "
297 + MakeDisplayPath((*lfit), 30)
299 int const action = lyxaction.
300 getPseudoAction(LFUN_FILE_OPEN,
302 tomenu.add(MenuItem(MenuItem::Command,
307 void expandDocuments(Menu & tomenu)
309 typedef vector<string> Strings;
310 Strings const names = bufferlist.getFileNames();
313 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
319 Strings::const_iterator docit = names.begin();
320 Strings::const_iterator end = names.end();
321 for (; docit != end; ++docit, ++ii) {
323 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
324 string label = MakeDisplayPath(*docit, 30);
326 label = tostr(ii) + ". " + label + '|' + tostr(ii);
327 tomenu.add(MenuItem(MenuItem::Command, label, action));
332 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
334 if (!buf && kind != MenuItem::ImportFormats) {
335 tomenu.add(MenuItem(MenuItem::Command,
336 _("No Documents Open!"), LFUN_NOACTION));
340 typedef vector<Format const *> Formats;
345 case MenuItem::ImportFormats:
346 formats = Importer::GetImportableFormats();
347 action = LFUN_IMPORT;
349 case MenuItem::ViewFormats:
350 formats = Exporter::GetExportableFormats(buf, true);
351 action = LFUN_PREVIEW;
353 case MenuItem::UpdateFormats:
354 formats = Exporter::GetExportableFormats(buf, true);
355 action = LFUN_UPDATE;
358 formats = Exporter::GetExportableFormats(buf, false);
359 action = LFUN_EXPORT;
361 sort(formats.begin(), formats.end(), compare_format());
363 Formats::const_iterator fit = formats.begin();
364 Formats::const_iterator end = formats.end();
365 for (; fit != end ; ++fit) {
368 string label = (*fit)->prettyname();
369 // we need to hide the default graphic export formats
370 // from the external menu, because we need them only
371 // for the internal lyx-view and external latex run
372 if (label == "EPS" || label == "XPM" || label == "PNG")
375 if (kind == MenuItem::ImportFormats)
376 if ((*fit)->name() == "text")
377 label = _("Ascii text as lines");
378 else if ((*fit)->name() == "textparagraph")
379 label = _("Ascii text as paragraphs");
380 if (!(*fit)->shortcut().empty())
381 label += "|" + (*fit)->shortcut();
382 int const action2 = lyxaction.
383 getPseudoAction(action, (*fit)->name());
384 tomenu.add(MenuItem(MenuItem::Command, label, action2));
389 void expandFloatListInsert(Menu & tomenu)
391 FloatList::const_iterator cit = floatList.begin();
392 FloatList::const_iterator end = floatList.end();
393 for (; cit != end; ++cit) {
394 int const action = lyxaction
395 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
396 tomenu.add(MenuItem(MenuItem::Command,
397 _(cit->second.listName()),
403 void expandFloatInsert(Menu & tomenu)
405 FloatList::const_iterator cit = floatList.begin();
406 FloatList::const_iterator end = floatList.end();
407 for (; cit != end; ++cit) {
410 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
412 string const label = _(cit->second.name());
413 tomenu.add(MenuItem(MenuItem::Command, label, action));
418 Menu::size_type const max_number_of_items = 25;
420 void expandToc2(Menu & tomenu, toc::Toc const & toc_list,
421 toc::Toc::size_type from, toc::Toc::size_type to, int depth)
423 int shortcut_count = 0;
424 if (to - from <= max_number_of_items) {
425 for (toc::Toc::size_type i = from; i < to; ++i) {
426 int const action = toc_list[i].action();
427 string label(4 * max(0, toc_list[i].depth - depth),' ');
428 label += limit_string_length(toc_list[i].str);
429 if (toc_list[i].depth == depth
430 && ++shortcut_count <= 9) {
431 label += "|" + tostr(shortcut_count);
433 tomenu.add(MenuItem(MenuItem::Command, label, action));
436 toc::Toc::size_type pos = from;
438 toc::Toc::size_type new_pos = pos + 1;
439 while (new_pos < to &&
440 toc_list[new_pos].depth > depth)
443 int const action = toc_list[pos].action();
444 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
445 label += limit_string_length(toc_list[pos].str);
446 if (toc_list[pos].depth == depth &&
447 ++shortcut_count <= 9)
448 label += '|' + tostr(shortcut_count);
450 if (new_pos == pos + 1) {
451 tomenu.add(MenuItem(MenuItem::Command,
454 MenuItem item(MenuItem::Submenu, label);
455 item.submenu(new Menu);
456 expandToc2(*item.submenu(),
457 toc_list, pos, new_pos, depth + 1);
466 void expandToc(Menu & tomenu, Buffer const * buf)
468 toc::TocList toc_list = toc::getTocList(buf);
469 toc::TocList::const_iterator cit = toc_list.begin();
470 toc::TocList::const_iterator end = toc_list.end();
471 for (; cit != end; ++cit) {
473 if (cit->first == "TOC") continue;
475 // All the rest is for floats
476 Menu * menu = new Menu;
477 toc::Toc::const_iterator ccit = cit->second.begin();
478 toc::Toc::const_iterator eend = cit->second.end();
479 for (; ccit != eend; ++ccit) {
480 string const label = limit_string_length(ccit->str);
481 menu->add(MenuItem(MenuItem::Command,
482 label, ccit->action()));
484 MenuItem item(MenuItem::Submenu,
485 floatList[cit->first]->second.name());
491 cit = toc_list.find("TOC");
493 tomenu.add(MenuItem(MenuItem::Command,
494 _("No Table of contents")));
496 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
504 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
505 Buffer const * buf) const
507 for (Menu::const_iterator cit = frommenu.begin();
508 cit != frommenu.end() ; ++cit) {
509 switch (cit->kind()) {
510 case MenuItem::Lastfiles:
511 expandLastfiles(tomenu);
514 case MenuItem::Documents:
515 expandDocuments(tomenu);
518 case MenuItem::ImportFormats:
519 case MenuItem::ViewFormats:
520 case MenuItem::UpdateFormats:
521 case MenuItem::ExportFormats:
522 expandFormats(cit->kind(), tomenu, buf);
525 case MenuItem::FloatListInsert:
526 expandFloatListInsert(tomenu);
529 case MenuItem::FloatInsert:
530 expandFloatInsert(tomenu);
534 expandToc(tomenu, buf);
537 case MenuItem::Submenu: {
539 item.submenu(new Menu(cit->submenuname()));
540 expand(getMenu(cit->submenuname()),
541 *item.submenu(), buf);
551 // Check whether the shortcuts are unique
552 if (lyxerr.debugging(Debug::GUI))
553 tomenu.checkShortcuts();
557 bool Menu::hasSubmenu(string const & name) const
559 return find_if(begin(), end(),
560 lyx::compare_memfun(&MenuItem::submenuname,
565 void MenuBackend::read(LyXLex & lex)
574 struct keyword_item menutags[md_last - 1] = {
575 { "end", md_endmenuset },
577 { "menubar", md_menubar }
581 if (compare_ascii_no_case(lex.getString(), "menuset")) {
582 lyxerr << "Menubackend::read: ERROR wrong token:`"
583 << lex.getString() << '\'' << endl;
586 lex.pushTable(menutags, md_last - 1);
587 if (lyxerr.debugging(Debug::PARSER))
588 lex.printTable(lyxerr);
592 while (lex.isOK() && !quit) {
599 string const name = lex.getString();
601 getMenu(name).read(lex);
613 lex.printError("menubackend::read: "
614 "Unknown menu tag: `$$Token'");
622 void MenuBackend::defaults()
626 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
631 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
632 .add(MenuItem(MenuItem::Command, _("Open...|O"), "file-open"))
633 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
634 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
635 .add(MenuItem(MenuItem::Separator))
636 .add(MenuItem(MenuItem::Lastfiles));
639 Menu import("import");
641 .add(MenuItem(MenuItem::Command,
642 _("LaTeX...|L"), "buffer-import latex"))
643 .add(MenuItem(MenuItem::Command,
644 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
649 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
650 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
651 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
652 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
655 Menu documents("documents");
656 documents.add(MenuItem(MenuItem::Documents));
659 menubar_.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
660 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
661 .add(MenuItem(MenuItem::Submenu,
662 _("Documents|D"), "documents"));
667 void MenuBackend::add(Menu const & menu)
669 menulist_.push_back(menu);
673 bool MenuBackend::hasMenu(string const & name) const
675 return find_if(begin(), end(),
676 lyx::compare_memfun(&Menu::name, name)) != end();
680 Menu const & MenuBackend::getMenu(string const & name) const
682 const_iterator cit = find_if(begin(), end(),
683 lyx::compare_memfun(&Menu::name, name));
684 lyx::Assert(cit != end());
689 Menu & MenuBackend::getMenu(string const & name)
691 MenuList::iterator it =
692 find_if(menulist_.begin(), menulist_.end(),
693 lyx::compare_memfun(&Menu::name, name));
694 lyx::Assert(it != menulist_.end());
699 Menu const & MenuBackend::getMenubar() const