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 LyXAction lyxaction;
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;
89 string const MenuItem::label() const
91 return token(label_, '|', 0);
95 string const MenuItem::shortcut() const
97 return token(label_, '|', 1);
101 Menu & Menu::add(MenuItem const & i)
108 Menu & Menu::read(LyXLex & lex)
128 struct keyword_item menutags[md_last - 1] = {
129 { "documents", md_documents },
130 { "end", md_endmenu },
131 { "exportformats", md_exportformats },
132 { "floatinsert", md_floatinsert },
133 { "floatlistinsert", md_floatlistinsert },
134 { "importformats", md_importformats },
136 { "lastfiles", md_lastfiles },
137 { "optitem", md_optitem },
138 { "separator", md_separator },
139 { "submenu", md_submenu },
141 { "updateformats", md_updateformats },
142 { "viewformats", md_viewformats }
145 lex.pushTable(menutags, md_last - 1);
146 if (lyxerr.debugging(Debug::PARSER))
147 lex.printTable(lyxerr);
150 bool optional = false;
152 while (lex.isOK() && !quit) {
156 // fallback to md_item
159 string const name = _(lex.getString());
161 string const command = lex.getString();
162 add(MenuItem(MenuItem::Command, name,
169 add(MenuItem(MenuItem::Separator));
173 add(MenuItem(MenuItem::Lastfiles));
177 add(MenuItem(MenuItem::Documents));
181 add(MenuItem(MenuItem::Toc));
185 add(MenuItem(MenuItem::ViewFormats));
188 case md_updateformats:
189 add(MenuItem(MenuItem::UpdateFormats));
192 case md_exportformats:
193 add(MenuItem(MenuItem::ExportFormats));
196 case md_importformats:
197 add(MenuItem(MenuItem::ImportFormats));
200 case md_floatlistinsert:
201 add(MenuItem(MenuItem::FloatListInsert));
205 add(MenuItem(MenuItem::FloatInsert));
210 string const mlabel = _(lex.getString());
212 string const mname = lex.getString();
213 add(MenuItem(MenuItem::Submenu, mlabel, mname));
222 lex.printError("Menu::read: "
223 "Unknown menu tag: `$$Token'");
232 void Menu::checkShortcuts() const
234 // This is a quadratic algorithm, but we do not care because
235 // it is used for debugging only.
236 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
237 string shortcut = it1->shortcut();
238 if (shortcut.empty())
240 if (!contains(it1->label(), shortcut))
241 lyxerr << "Menu warning: menu entry \""
243 << "\" does not contain shortcut `"
244 << shortcut << '\'' << endl;
245 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
246 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
247 lyxerr << "Menu warning: menu entries "
248 << '"' << it1->fulllabel()
249 << "\" and \"" << it2->fulllabel()
250 << "\" share the same shortcut."
260 class compare_format {
262 bool operator()(Format const * p1, Format const * p2) {
267 string const limit_string_length(string const & str)
269 string::size_type const max_item_length = 45;
271 if (str.size() > max_item_length)
272 return str.substr(0, max_item_length - 3) + "...";
278 void expandLastfiles(Menu & tomenu)
281 LastFiles::const_iterator lfit = lastfiles->begin();
282 LastFiles::const_iterator end = lastfiles->end();
284 for (; lfit != end && ii < 10; ++lfit, ++ii) {
285 string const label = tostr(ii) + ". "
286 + MakeDisplayPath((*lfit), 30)
288 int const action = lyxaction.
289 getPseudoAction(LFUN_FILE_OPEN,
291 tomenu.add(MenuItem(MenuItem::Command,
296 void expandDocuments(Menu & tomenu)
298 typedef vector<string> Strings;
299 Strings const names = bufferlist.getFileNames();
302 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
308 Strings::const_iterator docit = names.begin();
309 Strings::const_iterator end = names.end();
310 for (; docit != end; ++docit, ++ii) {
312 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
313 string label = MakeDisplayPath(*docit, 30);
315 label = tostr(ii) + ". " + label + '|' + tostr(ii);
316 tomenu.add(MenuItem(MenuItem::Command, label, action));
321 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
323 if (!buf && kind != MenuItem::ImportFormats) {
324 tomenu.add(MenuItem(MenuItem::Command,
325 _("No Documents Open!"), LFUN_NOACTION));
329 typedef vector<Format const *> Formats;
334 case MenuItem::ImportFormats:
335 formats = Importer::GetImportableFormats();
336 action = LFUN_IMPORT;
338 case MenuItem::ViewFormats:
339 formats = Exporter::GetExportableFormats(buf, true);
340 action = LFUN_PREVIEW;
342 case MenuItem::UpdateFormats:
343 formats = Exporter::GetExportableFormats(buf, true);
344 action = LFUN_UPDATE;
347 formats = Exporter::GetExportableFormats(buf, false);
348 action = LFUN_EXPORT;
350 sort(formats.begin(), formats.end(), compare_format());
352 Formats::const_iterator fit = formats.begin();
353 Formats::const_iterator end = formats.end();
354 for (; fit != end ; ++fit) {
357 string label = (*fit)->prettyname();
358 // we need to hide the default graphic export formats
359 // from the external menu, because we need them only
360 // for the internal lyx-view and external latex run
361 if (label == "EPS" || label == "XPM" || label == "PNG")
364 if (kind == MenuItem::ImportFormats)
365 if ((*fit)->name() == "text")
366 label = _("Ascii text as lines");
367 else if ((*fit)->name() == "textparagraph")
368 label = _("Ascii text as paragraphs");
369 if (!(*fit)->shortcut().empty())
370 label += "|" + (*fit)->shortcut();
371 int const action2 = lyxaction.
372 getPseudoAction(action, (*fit)->name());
373 tomenu.add(MenuItem(MenuItem::Command, label, action2));
378 void expandFloatListInsert(Menu & tomenu)
380 FloatList::const_iterator cit = floatList.begin();
381 FloatList::const_iterator end = floatList.end();
382 for (; cit != end; ++cit) {
383 int const action = lyxaction
384 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
385 tomenu.add(MenuItem(MenuItem::Command,
386 _(cit->second.listName()),
392 void expandFloatInsert(Menu & tomenu)
394 FloatList::const_iterator cit = floatList.begin();
395 FloatList::const_iterator end = floatList.end();
396 for (; cit != end; ++cit) {
399 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
401 string const label = _(cit->second.name());
402 tomenu.add(MenuItem(MenuItem::Command, label, action));
407 Menu::size_type const max_number_of_items = 25;
409 void expandToc2(Menu & tomenu, toc::Toc const & toc_list,
410 toc::Toc::size_type from, toc::Toc::size_type to, int depth)
412 int shortcut_count = 0;
413 if (to - from <= max_number_of_items) {
414 for (toc::Toc::size_type i = from; i < to; ++i) {
415 int const action = toc_list[i].action();
416 string label(4 * max(0, toc_list[i].depth - depth),' ');
417 label += limit_string_length(toc_list[i].str);
418 if (toc_list[i].depth == depth
419 && ++shortcut_count <= 9) {
420 label += "|" + tostr(shortcut_count);
422 tomenu.add(MenuItem(MenuItem::Command, label, action));
425 toc::Toc::size_type pos = from;
427 toc::Toc::size_type new_pos = pos + 1;
428 while (new_pos < to &&
429 toc_list[new_pos].depth > depth)
432 int const action = toc_list[pos].action();
433 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
434 label += limit_string_length(toc_list[pos].str);
435 if (toc_list[pos].depth == depth &&
436 ++shortcut_count <= 9)
437 label += '|' + tostr(shortcut_count);
439 if (new_pos == pos + 1) {
440 tomenu.add(MenuItem(MenuItem::Command,
443 MenuItem item(MenuItem::Submenu, label);
444 item.submenu(new Menu);
445 expandToc2(*item.submenu(),
446 toc_list, pos, new_pos, depth + 1);
455 void expandToc(Menu & tomenu, Buffer const * buf)
457 toc::TocList toc_list = toc::getTocList(buf);
458 toc::TocList::const_iterator cit = toc_list.begin();
459 toc::TocList::const_iterator end = toc_list.end();
460 for (; cit != end; ++cit) {
462 if (cit->first == "TOC") continue;
464 // All the rest is for floats
465 Menu * menu = new Menu;
466 toc::Toc::const_iterator ccit = cit->second.begin();
467 toc::Toc::const_iterator eend = cit->second.end();
468 for (; ccit != eend; ++ccit) {
469 string const label = limit_string_length(ccit->str);
470 menu->add(MenuItem(MenuItem::Command,
471 label, ccit->action()));
473 MenuItem item(MenuItem::Submenu,
474 floatList[cit->first]->second.name());
480 cit = toc_list.find("TOC");
482 tomenu.add(MenuItem(MenuItem::Command,
483 _("No Table of contents")));
485 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
493 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
494 Buffer const * buf) const
496 for (Menu::const_iterator cit = frommenu.begin();
497 cit != frommenu.end() ; ++cit) {
498 switch (cit->kind()) {
499 case MenuItem::Lastfiles:
500 expandLastfiles(tomenu);
503 case MenuItem::Documents:
504 expandDocuments(tomenu);
507 case MenuItem::ImportFormats:
508 case MenuItem::ViewFormats:
509 case MenuItem::UpdateFormats:
510 case MenuItem::ExportFormats:
511 expandFormats(cit->kind(), tomenu, buf);
514 case MenuItem::FloatListInsert:
515 expandFloatListInsert(tomenu);
518 case MenuItem::FloatInsert:
519 expandFloatInsert(tomenu);
523 expandToc(tomenu, buf);
526 case MenuItem::Submenu: {
528 item.submenu(new Menu(cit->submenuname()));
529 expand(getMenu(cit->submenuname()),
530 *item.submenu(), buf);
540 // Check whether the shortcuts are unique
541 if (lyxerr.debugging(Debug::GUI))
542 tomenu.checkShortcuts();
546 bool Menu::hasSubmenu(string const & name) const
548 return find_if(begin(), end(),
549 lyx::compare_memfun(&MenuItem::submenuname,
554 void MenuBackend::read(LyXLex & lex)
563 struct keyword_item menutags[md_last - 1] = {
564 { "end", md_endmenuset },
566 { "menubar", md_menubar }
570 if (compare_ascii_no_case(lex.getString(), "menuset")) {
571 lyxerr << "Menubackend::read: ERROR wrong token:`"
572 << lex.getString() << '\'' << endl;
575 lex.pushTable(menutags, md_last - 1);
576 if (lyxerr.debugging(Debug::PARSER))
577 lex.printTable(lyxerr);
581 while (lex.isOK() && !quit) {
588 string const name = lex.getString();
590 getMenu(name).read(lex);
602 lex.printError("menubackend::read: "
603 "Unknown menu tag: `$$Token'");
611 void MenuBackend::defaults()
615 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
620 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
621 .add(MenuItem(MenuItem::Command, _("Open...|O"), "file-open"))
622 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
623 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
624 .add(MenuItem(MenuItem::Separator))
625 .add(MenuItem(MenuItem::Lastfiles));
628 Menu import("import");
630 .add(MenuItem(MenuItem::Command,
631 _("LaTeX...|L"), "buffer-import latex"))
632 .add(MenuItem(MenuItem::Command,
633 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
638 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
639 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
640 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
641 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
644 Menu documents("documents");
645 documents.add(MenuItem(MenuItem::Documents));
648 menubar_.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
649 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
650 .add(MenuItem(MenuItem::Submenu,
651 _("Documents|D"), "documents"));
656 void MenuBackend::add(Menu const & menu)
658 menulist_.push_back(menu);
662 bool MenuBackend::hasMenu(string const & name) const
664 return find_if(begin(), end(),
665 lyx::compare_memfun(&Menu::name, name)) != end();
669 Menu const & MenuBackend::getMenu(string const & name) const
671 const_iterator cit = find_if(begin(), end(),
672 lyx::compare_memfun(&Menu::name, name));
673 lyx::Assert(cit != end());
678 Menu & MenuBackend::getMenu(string const & name)
680 MenuList::iterator it =
681 find_if(menulist_.begin(), menulist_.end(),
682 lyx::compare_memfun(&Menu::name, name));
683 lyx::Assert(it != menulist_.end());
688 Menu const & MenuBackend::getMenubar() const