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 #include "MenuBackend.h"
17 #include "LyXAction.h"
20 #include "lastfiles.h"
21 #include "lyx_main.h" // for lastfiles
22 #include "bufferlist.h"
24 #include "converter.h"
27 #include "FloatList.h"
29 #include "support/LAssert.h"
30 #include "support/filetools.h"
31 #include "support/lyxfunctional.h"
32 #include "support/lstrings.h"
34 extern BufferList bufferlist;
43 // This is the global menu definition
44 MenuBackend menubackend;
47 MenuItem::MenuItem(Kind kind, string const & label,
48 string const & command, bool optional)
49 : kind_(kind), label_(label), optional_(optional)
64 action_ = lyxaction.LookupFunc(command);
66 if (action_ == LFUN_UNKNOWN_ACTION) {
67 lyxerr << "MenuItem(): LyX command `"
68 << command << "' does not exist." << endl;
71 lyxerr[Debug::GUI] << "Optional item "
75 submenuname_ = command;
81 MenuItem::MenuItem(Kind kind, string const & label, int action, bool optional)
82 : kind_(kind), label_(label), action_(action), submenuname_(),
91 void MenuItem::submenu(Menu * menu)
97 string const MenuItem::label() const
99 return token(label_, '|', 0);
103 string const MenuItem::shortcut() const
105 return token(label_, '|', 1);
109 Menu & Menu::add(MenuItem const & i)
116 Menu & Menu::read(LyXLex & lex)
136 struct keyword_item menutags[md_last - 1] = {
137 { "documents", md_documents },
138 { "end", md_endmenu },
139 { "exportformats", md_exportformats },
140 { "floatinsert", md_floatinsert },
141 { "floatlistinsert", md_floatlistinsert },
142 { "importformats", md_importformats },
144 { "lastfiles", md_lastfiles },
145 { "optitem", md_optitem },
146 { "separator", md_separator },
147 { "submenu", md_submenu },
149 { "updateformats", md_updateformats },
150 { "viewformats", md_viewformats }
153 lex.pushTable(menutags, md_last - 1);
154 if (lyxerr.debugging(Debug::PARSER))
155 lex.printTable(lyxerr);
158 bool optional = false;
160 while (lex.isOK() && !quit) {
164 // fallback to md_item
167 string const name = _(lex.getString());
169 string const command = lex.getString();
170 add(MenuItem(MenuItem::Command, name,
177 add(MenuItem(MenuItem::Separator));
181 add(MenuItem(MenuItem::Lastfiles));
185 add(MenuItem(MenuItem::Documents));
189 add(MenuItem(MenuItem::Toc));
193 add(MenuItem(MenuItem::ViewFormats));
196 case md_updateformats:
197 add(MenuItem(MenuItem::UpdateFormats));
200 case md_exportformats:
201 add(MenuItem(MenuItem::ExportFormats));
204 case md_importformats:
205 add(MenuItem(MenuItem::ImportFormats));
208 case md_floatlistinsert:
209 add(MenuItem(MenuItem::FloatListInsert));
213 add(MenuItem(MenuItem::FloatInsert));
218 string const mlabel = _(lex.getString());
220 string const mname = lex.getString();
221 add(MenuItem(MenuItem::Submenu, mlabel, mname));
230 lex.printError("Menu::read: "
231 "Unknown menu tag: `$$Token'");
240 void Menu::checkShortcuts() const
242 // This is a quadratic algorithm, but we do not care because
243 // it is used for debugging only.
244 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
245 string shortcut = it1->shortcut();
246 if (shortcut.empty())
248 if (!contains(it1->label(), shortcut))
249 lyxerr << "Menu warning: menu entry \""
251 << "\" does not contain shortcut `"
252 << shortcut << '\'' << endl;
253 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
254 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
255 lyxerr << "Menu warning: menu entries "
256 << '"' << it1->fulllabel()
257 << "\" and \"" << it2->fulllabel()
258 << "\" share the same shortcut."
268 class compare_format {
270 bool operator()(Format const * p1, Format const * p2) {
275 string const limit_string_length(string const & str)
277 string::size_type const max_item_length = 45;
279 if (str.size() > max_item_length)
280 return str.substr(0, max_item_length - 3) + "...";
286 void expandLastfiles(Menu & tomenu)
289 LastFiles::const_iterator lfit = lastfiles->begin();
290 LastFiles::const_iterator end = lastfiles->end();
292 for (; lfit != end && ii < 10; ++lfit, ++ii) {
293 string const label = tostr(ii) + ". "
294 + MakeDisplayPath((*lfit), 30)
296 int const action = lyxaction.
297 getPseudoAction(LFUN_FILE_OPEN,
299 tomenu.add(MenuItem(MenuItem::Command,
304 void expandDocuments(Menu & tomenu)
306 typedef vector<string> Strings;
307 Strings const names = bufferlist.getFileNames();
310 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
316 Strings::const_iterator docit = names.begin();
317 Strings::const_iterator end = names.end();
318 for (; docit != end; ++docit, ++ii) {
320 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
321 string label = MakeDisplayPath(*docit, 30);
323 label = tostr(ii) + ". " + label + '|' + tostr(ii);
324 tomenu.add(MenuItem(MenuItem::Command, label, action));
329 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
331 if (!buf && kind != MenuItem::ImportFormats) {
332 tomenu.add(MenuItem(MenuItem::Command,
333 _("No Documents Open!"), LFUN_NOACTION));
337 typedef vector<Format const *> Formats;
342 case MenuItem::ImportFormats:
343 formats = Importer::GetImportableFormats();
344 action = LFUN_IMPORT;
346 case MenuItem::ViewFormats:
347 formats = Exporter::GetExportableFormats(buf, true);
348 action = LFUN_PREVIEW;
350 case MenuItem::UpdateFormats:
351 formats = Exporter::GetExportableFormats(buf, true);
352 action = LFUN_UPDATE;
355 formats = Exporter::GetExportableFormats(buf, false);
356 action = LFUN_EXPORT;
358 sort(formats.begin(), formats.end(), compare_format());
360 Formats::const_iterator fit = formats.begin();
361 Formats::const_iterator end = formats.end();
362 for (; fit != end ; ++fit) {
365 string label = (*fit)->prettyname();
366 // we need to hide the default graphic export formats
367 // from the external menu, because we need them only
368 // for the internal lyx-view and external latex run
369 if (label == "EPS" || label == "XPM" || label == "PNG")
372 if (kind == MenuItem::ImportFormats) {
373 if ((*fit)->name() == "text")
374 label = _("ASCII text as lines");
375 else if ((*fit)->name() == "textparagraph")
376 label = _("ASCII text as paragraphs");
379 if (!(*fit)->shortcut().empty())
380 label += '|' + (*fit)->shortcut();
381 int const action2 = lyxaction.
382 getPseudoAction(action, (*fit)->name());
383 tomenu.add(MenuItem(MenuItem::Command, label, action2));
388 void expandFloatListInsert(Menu & tomenu, Buffer const * buf)
391 tomenu.add(MenuItem(MenuItem::Command,
392 _("No Documents Open!"), LFUN_NOACTION));
396 FloatList const & floats = buf->params.getLyXTextClass().floats();
397 FloatList::const_iterator cit = floats.begin();
398 FloatList::const_iterator end = floats.end();
399 for (; cit != end; ++cit) {
400 int const action = lyxaction
401 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
402 tomenu.add(MenuItem(MenuItem::Command,
403 _(cit->second.listName()),
409 void expandFloatInsert(Menu & tomenu, Buffer const * buf)
412 tomenu.add(MenuItem(MenuItem::Command,
413 _("No Documents Open!"), LFUN_NOACTION));
417 FloatList const & floats = buf->params.getLyXTextClass().floats();
418 FloatList::const_iterator cit = floats.begin();
419 FloatList::const_iterator end = floats.end();
420 for (; cit != end; ++cit) {
423 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
425 string const label = _(cit->second.name());
426 tomenu.add(MenuItem(MenuItem::Command, label, action));
431 Menu::size_type const max_number_of_items = 25;
433 void expandToc2(Menu & tomenu, toc::Toc const & toc_list,
434 toc::Toc::size_type from, toc::Toc::size_type to, int depth)
436 int shortcut_count = 0;
437 if (to - from <= max_number_of_items) {
438 for (toc::Toc::size_type i = from; i < to; ++i) {
439 int const action = toc_list[i].action();
440 string label(4 * max(0, toc_list[i].depth - depth),' ');
441 label += limit_string_length(toc_list[i].str);
442 if (toc_list[i].depth == depth
443 && ++shortcut_count <= 9) {
444 label += '|' + tostr(shortcut_count);
446 tomenu.add(MenuItem(MenuItem::Command, label, action));
449 toc::Toc::size_type pos = from;
451 toc::Toc::size_type new_pos = pos + 1;
452 while (new_pos < to &&
453 toc_list[new_pos].depth > depth)
456 int const action = toc_list[pos].action();
457 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
458 label += limit_string_length(toc_list[pos].str);
459 if (toc_list[pos].depth == depth &&
460 ++shortcut_count <= 9)
461 label += '|' + tostr(shortcut_count);
463 if (new_pos == pos + 1) {
464 tomenu.add(MenuItem(MenuItem::Command,
467 MenuItem item(MenuItem::Submenu, label);
468 item.submenu(new Menu);
469 expandToc2(*item.submenu(),
470 toc_list, pos, new_pos, depth + 1);
479 void expandToc(Menu & tomenu, Buffer const * buf)
482 tomenu.add(MenuItem(MenuItem::Command,
483 _("No Documents Open!"), LFUN_NOACTION));
487 toc::TocList toc_list = toc::getTocList(buf);
488 toc::TocList::const_iterator cit = toc_list.begin();
489 toc::TocList::const_iterator end = toc_list.end();
490 for (; cit != end; ++cit) {
492 if (cit->first == "TOC")
495 // All the rest is for floats
496 Menu * menu = new Menu;
497 toc::Toc::const_iterator ccit = cit->second.begin();
498 toc::Toc::const_iterator eend = cit->second.end();
499 for (; ccit != eend; ++ccit) {
500 string const label = limit_string_length(ccit->str);
501 menu->add(MenuItem(MenuItem::Command,
502 label, ccit->action()));
504 string const & floatName = cit->first;
505 // Is the _(...) really needed here? (Lgb)
506 MenuItem item(MenuItem::Submenu, _(floatName));
512 cit = toc_list.find("TOC");
514 tomenu.add(MenuItem(MenuItem::Command,
515 _("No Table of contents")));
517 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
525 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
526 Buffer const * buf) const
528 for (Menu::const_iterator cit = frommenu.begin();
529 cit != frommenu.end() ; ++cit) {
530 switch (cit->kind()) {
531 case MenuItem::Lastfiles:
532 expandLastfiles(tomenu);
535 case MenuItem::Documents:
536 expandDocuments(tomenu);
539 case MenuItem::ImportFormats:
540 case MenuItem::ViewFormats:
541 case MenuItem::UpdateFormats:
542 case MenuItem::ExportFormats:
543 expandFormats(cit->kind(), tomenu, buf);
546 case MenuItem::FloatListInsert:
547 expandFloatListInsert(tomenu, buf);
550 case MenuItem::FloatInsert:
551 expandFloatInsert(tomenu, buf);
555 expandToc(tomenu, buf);
558 case MenuItem::Submenu: {
560 item.submenu(new Menu(cit->submenuname()));
561 expand(getMenu(cit->submenuname()),
562 *item.submenu(), buf);
572 // Check whether the shortcuts are unique
573 if (lyxerr.debugging(Debug::GUI))
574 tomenu.checkShortcuts();
578 bool Menu::hasSubmenu(string const & name) const
580 return find_if(begin(), end(),
581 lyx::compare_memfun(&MenuItem::submenuname,
586 void MenuBackend::read(LyXLex & lex)
595 struct keyword_item menutags[md_last - 1] = {
596 { "end", md_endmenuset },
598 { "menubar", md_menubar }
602 if (compare_ascii_no_case(lex.getString(), "menuset")) {
603 lyxerr << "Menubackend::read: ERROR wrong token:`"
604 << lex.getString() << '\'' << endl;
607 lex.pushTable(menutags, md_last - 1);
608 if (lyxerr.debugging(Debug::PARSER))
609 lex.printTable(lyxerr);
613 while (lex.isOK() && !quit) {
620 string const name = lex.getString();
622 getMenu(name).read(lex);
634 lex.printError("menubackend::read: "
635 "Unknown menu tag: `$$Token'");
643 void MenuBackend::defaults()
647 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
652 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
653 .add(MenuItem(MenuItem::Command, _("Open...|O"), "file-open"))
654 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
655 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
656 .add(MenuItem(MenuItem::Separator))
657 .add(MenuItem(MenuItem::Lastfiles));
660 Menu import("import");
662 .add(MenuItem(MenuItem::Command,
663 _("LaTeX...|L"), "buffer-import latex"))
664 .add(MenuItem(MenuItem::Command,
665 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
670 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
671 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
672 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
673 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
676 Menu documents("documents");
677 documents.add(MenuItem(MenuItem::Documents));
680 menubar_.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
681 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
682 .add(MenuItem(MenuItem::Submenu,
683 _("Documents|D"), "documents"));
688 void MenuBackend::add(Menu const & menu)
690 menulist_.push_back(menu);
694 bool MenuBackend::hasMenu(string const & name) const
696 return find_if(begin(), end(),
697 lyx::compare_memfun(&Menu::name, name)) != end();
701 Menu const & MenuBackend::getMenu(string const & name) const
703 const_iterator cit = find_if(begin(), end(),
704 lyx::compare_memfun(&Menu::name, name));
706 lyxerr << "No submenu named " << name << endl;
707 lyx::Assert(cit != end());
712 Menu & MenuBackend::getMenu(string const & name)
714 MenuList::iterator it =
715 find_if(menulist_.begin(), menulist_.end(),
716 lyx::compare_memfun(&Menu::name, name));
717 lyx::Assert(it != menulist_.end());
722 Menu const & MenuBackend::getMenubar() const