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;
88 string const MenuItem::label() const
90 return token(label_, '|', 0);
94 string const MenuItem::shortcut() const
96 return token(label_, '|', 1);
100 Menu & Menu::add(MenuItem const & i)
107 Menu & Menu::read(LyXLex & lex)
127 struct keyword_item menutags[md_last - 1] = {
128 { "documents", md_documents },
129 { "end", md_endmenu },
130 { "exportformats", md_exportformats },
131 { "floatinsert", md_floatinsert },
132 { "floatlistinsert", md_floatlistinsert },
133 { "importformats", md_importformats },
135 { "lastfiles", md_lastfiles },
136 { "optitem", md_optitem },
137 { "separator", md_separator },
138 { "submenu", md_submenu },
140 { "updateformats", md_updateformats },
141 { "viewformats", md_viewformats }
144 lex.pushTable(menutags, md_last - 1);
145 if (lyxerr.debugging(Debug::PARSER))
146 lex.printTable(lyxerr);
149 bool optional = false;
151 while (lex.isOK() && !quit) {
155 // fallback to md_item
158 string const name = _(lex.getString());
160 string const command = lex.getString();
161 add(MenuItem(MenuItem::Command, name,
168 add(MenuItem(MenuItem::Separator));
172 add(MenuItem(MenuItem::Lastfiles));
176 add(MenuItem(MenuItem::Documents));
180 add(MenuItem(MenuItem::Toc));
184 add(MenuItem(MenuItem::ViewFormats));
187 case md_updateformats:
188 add(MenuItem(MenuItem::UpdateFormats));
191 case md_exportformats:
192 add(MenuItem(MenuItem::ExportFormats));
195 case md_importformats:
196 add(MenuItem(MenuItem::ImportFormats));
199 case md_floatlistinsert:
200 add(MenuItem(MenuItem::FloatListInsert));
204 add(MenuItem(MenuItem::FloatInsert));
209 string const mlabel = _(lex.getString());
211 string const mname = lex.getString();
212 add(MenuItem(MenuItem::Submenu, mlabel, mname));
221 lex.printError("Menu::read: "
222 "Unknown menu tag: `$$Token'");
231 void Menu::checkShortcuts() const
233 // This is a quadratic algorithm, but we do not care because
234 // it is used for debugging only.
235 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
236 string shortcut = it1->shortcut();
237 if (shortcut.empty())
239 if (!contains(it1->label(), shortcut))
240 lyxerr << "Menu warning: menu entry \""
242 << "\" does not contain shortcut `"
243 << shortcut << '\'' << endl;
244 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
245 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
246 lyxerr << "Menu warning: menu entries "
247 << '"' << it1->fulllabel()
248 << "\" and \"" << it2->fulllabel()
249 << "\" share the same shortcut."
259 class compare_format {
261 bool operator()(Format const * p1, Format const * p2) {
266 string const limit_string_length(string const & str)
268 string::size_type const max_item_length = 45;
270 if (str.size() > max_item_length)
271 return str.substr(0, max_item_length - 3) + "...";
277 void expandLastfiles(Menu & tomenu)
280 LastFiles::const_iterator lfit = lastfiles->begin();
281 LastFiles::const_iterator end = lastfiles->end();
283 for (; lfit != end && ii < 10; ++lfit, ++ii) {
284 string const label = tostr(ii) + ". "
285 + MakeDisplayPath((*lfit), 30)
287 int const action = lyxaction.
288 getPseudoAction(LFUN_FILE_OPEN,
290 tomenu.add(MenuItem(MenuItem::Command,
295 void expandDocuments(Menu & tomenu)
297 typedef vector<string> Strings;
298 Strings const names = bufferlist.getFileNames();
301 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
307 Strings::const_iterator docit = names.begin();
308 Strings::const_iterator end = names.end();
309 for (; docit != end; ++docit, ++ii) {
311 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
312 string label = MakeDisplayPath(*docit, 30);
314 label = tostr(ii) + ". " + label + '|' + tostr(ii);
315 tomenu.add(MenuItem(MenuItem::Command, label, action));
320 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
322 if (!buf && kind != MenuItem::ImportFormats) {
323 tomenu.add(MenuItem(MenuItem::Command,
324 _("No Documents Open!"), LFUN_NOACTION));
328 typedef vector<Format const *> Formats;
333 case MenuItem::ImportFormats:
334 formats = Importer::GetImportableFormats();
335 action = LFUN_IMPORT;
337 case MenuItem::ViewFormats:
338 formats = Exporter::GetExportableFormats(buf, true);
339 action = LFUN_PREVIEW;
341 case MenuItem::UpdateFormats:
342 formats = Exporter::GetExportableFormats(buf, true);
343 action = LFUN_UPDATE;
346 formats = Exporter::GetExportableFormats(buf, false);
347 action = LFUN_EXPORT;
349 sort(formats.begin(), formats.end(), compare_format());
351 Formats::const_iterator fit = formats.begin();
352 Formats::const_iterator end = formats.end();
353 for (; fit != end ; ++fit) {
356 string label = (*fit)->prettyname();
357 // we need to hide the default graphic export formats
358 // from the external menu, because we need them only
359 // for the internal lyx-view and external latex run
360 if (label == "EPS" || label == "XPM" || label == "PNG")
363 if (kind == MenuItem::ImportFormats)
364 if ((*fit)->name() == "text")
365 label = _("Ascii text as lines");
366 else if ((*fit)->name() == "textparagraph")
367 label = _("Ascii text as paragraphs");
368 if (!(*fit)->shortcut().empty())
369 label += "|" + (*fit)->shortcut();
370 int const action2 = lyxaction.
371 getPseudoAction(action, (*fit)->name());
372 tomenu.add(MenuItem(MenuItem::Command, label, action2));
377 void expandFloatListInsert(Menu & tomenu)
379 FloatList::const_iterator cit = floatList.begin();
380 FloatList::const_iterator end = floatList.end();
381 for (; cit != end; ++cit) {
382 int const action = lyxaction
383 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
384 tomenu.add(MenuItem(MenuItem::Command,
385 _(cit->second.listName()),
391 void expandFloatInsert(Menu & tomenu)
393 FloatList::const_iterator cit = floatList.begin();
394 FloatList::const_iterator end = floatList.end();
395 for (; cit != end; ++cit) {
398 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
400 string const label = _(cit->second.name());
401 tomenu.add(MenuItem(MenuItem::Command, label, action));
406 Menu::size_type const max_number_of_items = 25;
408 void expandToc2(Menu & tomenu, toc::Toc const & toc_list,
409 toc::Toc::size_type from, toc::Toc::size_type to, int depth)
411 int shortcut_count = 0;
412 if (to - from <= max_number_of_items) {
413 for (toc::Toc::size_type i = from; i < to; ++i) {
414 int const action = toc_list[i].action();
415 string label(4 * max(0, toc_list[i].depth - depth),' ');
416 label += limit_string_length(toc_list[i].str);
417 if (toc_list[i].depth == depth
418 && ++shortcut_count <= 9) {
419 label += "|" + tostr(shortcut_count);
421 tomenu.add(MenuItem(MenuItem::Command, label, action));
424 toc::Toc::size_type pos = from;
426 toc::Toc::size_type new_pos = pos + 1;
427 while (new_pos < to &&
428 toc_list[new_pos].depth > depth)
431 int const action = toc_list[pos].action();
432 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
433 label += limit_string_length(toc_list[pos].str);
434 if (toc_list[pos].depth == depth &&
435 ++shortcut_count <= 9)
436 label += '|' + tostr(shortcut_count);
438 if (new_pos == pos + 1) {
439 tomenu.add(MenuItem(MenuItem::Command,
442 MenuItem item(MenuItem::Submenu, label);
443 item.submenu(new Menu);
444 expandToc2(*item.submenu(),
445 toc_list, pos, new_pos, depth + 1);
454 void expandToc(Menu & tomenu, Buffer const * buf)
456 toc::TocList toc_list = toc::getTocList(buf);
457 toc::TocList::const_iterator cit = toc_list.begin();
458 toc::TocList::const_iterator end = toc_list.end();
459 for (; cit != end; ++cit) {
461 if (cit->first == "TOC") continue;
463 // All the rest is for floats
464 Menu * menu = new Menu;
465 toc::Toc::const_iterator ccit = cit->second.begin();
466 toc::Toc::const_iterator eend = cit->second.end();
467 for (; ccit != eend; ++ccit) {
468 string const label = limit_string_length(ccit->str);
469 menu->add(MenuItem(MenuItem::Command,
470 label, ccit->action()));
472 MenuItem item(MenuItem::Submenu,
473 floatList[cit->first]->second.name());
479 cit = toc_list.find("TOC");
481 tomenu.add(MenuItem(MenuItem::Command,
482 _("No Table of contents")));
484 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
492 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
493 Buffer const * buf) const
495 for (Menu::const_iterator cit = frommenu.begin();
496 cit != frommenu.end() ; ++cit) {
497 switch (cit->kind()) {
498 case MenuItem::Lastfiles:
499 expandLastfiles(tomenu);
502 case MenuItem::Documents:
503 expandDocuments(tomenu);
506 case MenuItem::ImportFormats:
507 case MenuItem::ViewFormats:
508 case MenuItem::UpdateFormats:
509 case MenuItem::ExportFormats:
510 expandFormats(cit->kind(), tomenu, buf);
513 case MenuItem::FloatListInsert:
514 expandFloatListInsert(tomenu);
517 case MenuItem::FloatInsert:
518 expandFloatInsert(tomenu);
522 expandToc(tomenu, buf);
525 case MenuItem::Submenu: {
527 item.submenu(new Menu(cit->submenuname()));
528 expand(getMenu(cit->submenuname()),
529 *item.submenu(), buf);
539 // Check whether the shortcuts are unique
540 if (lyxerr.debugging(Debug::GUI))
541 tomenu.checkShortcuts();
545 bool Menu::hasSubmenu(string const & name) const
547 return find_if(begin(), end(),
548 lyx::compare_memfun(&MenuItem::submenuname,
553 void MenuBackend::read(LyXLex & lex)
562 struct keyword_item menutags[md_last - 1] = {
563 { "end", md_endmenuset },
565 { "menubar", md_menubar }
569 if (compare_ascii_no_case(lex.getString(), "menuset")) {
570 lyxerr << "Menubackend::read: ERROR wrong token:`"
571 << lex.getString() << '\'' << endl;
574 lex.pushTable(menutags, md_last - 1);
575 if (lyxerr.debugging(Debug::PARSER))
576 lex.printTable(lyxerr);
580 while (lex.isOK() && !quit) {
587 string const name = lex.getString();
589 getMenu(name).read(lex);
601 lex.printError("menubackend::read: "
602 "Unknown menu tag: `$$Token'");
610 void MenuBackend::defaults()
614 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
619 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
620 .add(MenuItem(MenuItem::Command, _("Open...|O"), "file-open"))
621 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
622 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
623 .add(MenuItem(MenuItem::Separator))
624 .add(MenuItem(MenuItem::Lastfiles));
627 Menu import("import");
629 .add(MenuItem(MenuItem::Command,
630 _("LaTeX...|L"), "buffer-import latex"))
631 .add(MenuItem(MenuItem::Command,
632 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
637 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
638 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
639 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
640 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
643 Menu documents("documents");
644 documents.add(MenuItem(MenuItem::Documents));
647 menubar_.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
648 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
649 .add(MenuItem(MenuItem::Submenu,
650 _("Documents|D"), "documents"));
655 void MenuBackend::add(Menu const & menu)
657 menulist_.push_back(menu);
661 bool MenuBackend::hasMenu(string const & name) const
663 return find_if(begin(), end(),
664 lyx::compare_memfun(&Menu::name, name)) != end();
668 Menu const & MenuBackend::getMenu(string const & name) const
670 const_iterator cit = find_if(begin(), end(),
671 lyx::compare_memfun(&Menu::name, name));
672 lyx::Assert(cit != end());
677 Menu & MenuBackend::getMenu(string const & name)
679 MenuList::iterator it =
680 find_if(menulist_.begin(), menulist_.end(),
681 lyx::compare_memfun(&Menu::name, name));
682 lyx::Assert(it != menulist_.end());
687 Menu const & MenuBackend::getMenubar() const