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");
381 if (!(*fit)->shortcut().empty())
382 label += '|' + (*fit)->shortcut();
383 int const action2 = lyxaction.
384 getPseudoAction(action, (*fit)->name());
385 tomenu.add(MenuItem(MenuItem::Command, label, action2));
390 void expandFloatListInsert(Menu & tomenu, Buffer const * buf)
393 tomenu.add(MenuItem(MenuItem::Command,
394 _("No Documents Open!"), LFUN_NOACTION));
398 FloatList const & floats = buf->params.getLyXTextClass().floats();
399 FloatList::const_iterator cit = floats.begin();
400 FloatList::const_iterator end = floats.end();
401 for (; cit != end; ++cit) {
402 int const action = lyxaction
403 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
404 tomenu.add(MenuItem(MenuItem::Command,
405 _(cit->second.listName()),
411 void expandFloatInsert(Menu & tomenu, Buffer const * buf)
414 tomenu.add(MenuItem(MenuItem::Command,
415 _("No Documents Open!"), LFUN_NOACTION));
419 FloatList const & floats = buf->params.getLyXTextClass().floats();
420 FloatList::const_iterator cit = floats.begin();
421 FloatList::const_iterator end = floats.end();
422 for (; cit != end; ++cit) {
425 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
427 string const label = _(cit->second.name());
428 tomenu.add(MenuItem(MenuItem::Command, label, action));
433 Menu::size_type const max_number_of_items = 25;
435 void expandToc2(Menu & tomenu, toc::Toc const & toc_list,
436 toc::Toc::size_type from, toc::Toc::size_type to, int depth)
438 int shortcut_count = 0;
439 if (to - from <= max_number_of_items) {
440 for (toc::Toc::size_type i = from; i < to; ++i) {
441 int const action = toc_list[i].action();
442 string label(4 * max(0, toc_list[i].depth - depth),' ');
443 label += limit_string_length(toc_list[i].str);
444 if (toc_list[i].depth == depth
445 && ++shortcut_count <= 9) {
446 label += '|' + tostr(shortcut_count);
448 tomenu.add(MenuItem(MenuItem::Command, label, action));
451 toc::Toc::size_type pos = from;
453 toc::Toc::size_type new_pos = pos + 1;
454 while (new_pos < to &&
455 toc_list[new_pos].depth > depth)
458 int const action = toc_list[pos].action();
459 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
460 label += limit_string_length(toc_list[pos].str);
461 if (toc_list[pos].depth == depth &&
462 ++shortcut_count <= 9)
463 label += '|' + tostr(shortcut_count);
465 if (new_pos == pos + 1) {
466 tomenu.add(MenuItem(MenuItem::Command,
469 MenuItem item(MenuItem::Submenu, label);
470 item.submenu(new Menu);
471 expandToc2(*item.submenu(),
472 toc_list, pos, new_pos, depth + 1);
481 void expandToc(Menu & tomenu, Buffer const * buf)
484 tomenu.add(MenuItem(MenuItem::Command,
485 _("No Documents Open!"), LFUN_NOACTION));
489 toc::TocList toc_list = toc::getTocList(buf);
490 toc::TocList::const_iterator cit = toc_list.begin();
491 toc::TocList::const_iterator end = toc_list.end();
492 for (; cit != end; ++cit) {
494 if (cit->first == "TOC")
497 // All the rest is for floats
498 Menu * menu = new Menu;
499 toc::Toc::const_iterator ccit = cit->second.begin();
500 toc::Toc::const_iterator eend = cit->second.end();
501 for (; ccit != eend; ++ccit) {
502 string const label = limit_string_length(ccit->str);
503 menu->add(MenuItem(MenuItem::Command,
504 label, ccit->action()));
506 string const & floatName = cit->first;
507 // Is the _(...) really needed here? (Lgb)
508 MenuItem item(MenuItem::Submenu, _(floatName));
514 cit = toc_list.find("TOC");
516 tomenu.add(MenuItem(MenuItem::Command,
517 _("No Table of contents")));
519 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
527 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
528 Buffer const * buf) const
530 for (Menu::const_iterator cit = frommenu.begin();
531 cit != frommenu.end() ; ++cit) {
532 switch (cit->kind()) {
533 case MenuItem::Lastfiles:
534 expandLastfiles(tomenu);
537 case MenuItem::Documents:
538 expandDocuments(tomenu);
541 case MenuItem::ImportFormats:
542 case MenuItem::ViewFormats:
543 case MenuItem::UpdateFormats:
544 case MenuItem::ExportFormats:
545 expandFormats(cit->kind(), tomenu, buf);
548 case MenuItem::FloatListInsert:
549 expandFloatListInsert(tomenu, buf);
552 case MenuItem::FloatInsert:
553 expandFloatInsert(tomenu, buf);
557 expandToc(tomenu, buf);
560 case MenuItem::Submenu: {
562 item.submenu(new Menu(cit->submenuname()));
563 expand(getMenu(cit->submenuname()),
564 *item.submenu(), buf);
574 // Check whether the shortcuts are unique
575 if (lyxerr.debugging(Debug::GUI))
576 tomenu.checkShortcuts();
580 bool Menu::hasSubmenu(string const & name) const
582 return find_if(begin(), end(),
583 lyx::compare_memfun(&MenuItem::submenuname,
588 void MenuBackend::read(LyXLex & lex)
597 struct keyword_item menutags[md_last - 1] = {
598 { "end", md_endmenuset },
600 { "menubar", md_menubar }
604 if (compare_ascii_no_case(lex.getString(), "menuset")) {
605 lyxerr << "Menubackend::read: ERROR wrong token:`"
606 << lex.getString() << '\'' << endl;
609 lex.pushTable(menutags, md_last - 1);
610 if (lyxerr.debugging(Debug::PARSER))
611 lex.printTable(lyxerr);
615 while (lex.isOK() && !quit) {
622 string const name = lex.getString();
624 getMenu(name).read(lex);
636 lex.printError("menubackend::read: "
637 "Unknown menu tag: `$$Token'");
645 void MenuBackend::defaults()
649 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
654 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
655 .add(MenuItem(MenuItem::Command, _("Open...|O"), "file-open"))
656 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
657 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
658 .add(MenuItem(MenuItem::Separator))
659 .add(MenuItem(MenuItem::Lastfiles));
662 Menu import("import");
664 .add(MenuItem(MenuItem::Command,
665 _("LaTeX...|L"), "buffer-import latex"))
666 .add(MenuItem(MenuItem::Command,
667 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
672 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
673 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
674 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
675 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
678 Menu documents("documents");
679 documents.add(MenuItem(MenuItem::Documents));
682 menubar_.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
683 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
684 .add(MenuItem(MenuItem::Submenu,
685 _("Documents|D"), "documents"));
690 void MenuBackend::add(Menu const & menu)
692 menulist_.push_back(menu);
696 bool MenuBackend::hasMenu(string const & name) const
698 return find_if(begin(), end(),
699 lyx::compare_memfun(&Menu::name, name)) != end();
703 Menu const & MenuBackend::getMenu(string const & name) const
705 const_iterator cit = find_if(begin(), end(),
706 lyx::compare_memfun(&Menu::name, name));
708 lyxerr << "No submenu named " << name << endl;
709 lyx::Assert(cit != end());
714 Menu & MenuBackend::getMenu(string const & name)
716 MenuList::iterator it =
717 find_if(menulist_.begin(), menulist_.end(),
718 lyx::compare_memfun(&Menu::name, name));
719 lyx::Assert(it != menulist_.end());
724 Menu const & MenuBackend::getMenubar() const