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"
31 #include "support/LAssert.h"
32 #include "support/filetools.h"
33 #include "support/lyxfunctional.h"
34 #include "support/lstrings.h"
36 extern LyXAction lyxaction;
37 extern BufferList bufferlist;
45 // This is the global menu definition
46 MenuBackend menubackend;
49 MenuItem::MenuItem(Kind kind, string const & label,
50 string const & command, bool optional)
51 : kind_(kind), label_(label), optional_(optional)
66 action_ = lyxaction.LookupFunc(command);
68 if (action_ == LFUN_UNKNOWN_ACTION) {
69 lyxerr << "MenuItem(): LyX command `"
70 << command << "' does not exist." << endl;
73 lyxerr[Debug::GUI] << "Optional item "
77 submenuname_ = command;
87 string const MenuItem::label() const
89 return token(label_, '|', 0);
93 string const MenuItem::shortcut() const
95 return token(label_, '|', 1);
99 Menu & Menu::add(MenuItem const & i)
106 Menu & Menu::read(LyXLex & lex)
126 struct keyword_item menutags[md_last - 1] = {
127 { "documents", md_documents },
128 { "end", md_endmenu },
129 { "exportformats", md_exportformats },
130 { "floatinsert", md_floatinsert },
131 { "floatlistinsert", md_floatlistinsert },
132 { "importformats", md_importformats },
134 { "lastfiles", md_lastfiles },
135 { "optitem", md_optitem },
136 { "separator", md_separator },
137 { "submenu", md_submenu },
139 { "updateformats", md_updateformats },
140 { "viewformats", md_viewformats }
143 lex.pushTable(menutags, md_last - 1);
144 if (lyxerr.debugging(Debug::PARSER))
145 lex.printTable(lyxerr);
148 bool optional = false;
150 while (lex.isOK() && !quit) {
154 // fallback to md_item
157 string const name = _(lex.getString());
159 string const command = lex.getString();
160 add(MenuItem(MenuItem::Command, name,
167 add(MenuItem(MenuItem::Separator));
171 add(MenuItem(MenuItem::Lastfiles));
175 add(MenuItem(MenuItem::Documents));
179 add(MenuItem(MenuItem::Toc));
183 add(MenuItem(MenuItem::ViewFormats));
186 case md_updateformats:
187 add(MenuItem(MenuItem::UpdateFormats));
190 case md_exportformats:
191 add(MenuItem(MenuItem::ExportFormats));
194 case md_importformats:
195 add(MenuItem(MenuItem::ImportFormats));
198 case md_floatlistinsert:
199 add(MenuItem(MenuItem::FloatListInsert));
203 add(MenuItem(MenuItem::FloatInsert));
208 string const mlabel = _(lex.getString());
210 string const mname = lex.getString();
211 add(MenuItem(MenuItem::Submenu, mlabel, mname));
220 lex.printError("Menu::read: "
221 "Unknown menu tag: `$$Token'");
230 void Menu::checkShortcuts() const
232 // This is a quadratic algorithm, but we do not care because
233 // it is used for debugging only.
234 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
235 string shortcut = it1->shortcut();
236 if (shortcut.empty())
238 if (!contains(it1->label(), shortcut))
239 lyxerr << "Menu warning: menu entry \""
241 << "\" does not contain shortcut `"
242 << shortcut << '\'' << endl;
243 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
244 if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
245 lyxerr << "Menu warning: menu entries "
246 << '"' << it1->fulllabel()
247 << "\" and \"" << it2->fulllabel()
248 << "\" share the same shortcut."
258 class compare_format {
260 bool operator()(Format const * p1, Format const * p2) {
265 void expandLastfiles(Menu & tomenu)
268 LastFiles::const_iterator lfit = lastfiles->begin();
269 LastFiles::const_iterator end = lastfiles->end();
271 for (; lfit != end && ii < 10; ++lfit, ++ii) {
272 string const label = tostr(ii) + ". "
273 + MakeDisplayPath((*lfit), 30)
275 int const action = lyxaction.
276 getPseudoAction(LFUN_FILE_OPEN,
278 tomenu.add(MenuItem(MenuItem::Command,
283 void expandDocuments(Menu & tomenu)
285 typedef vector<string> Strings;
286 Strings const names = bufferlist.getFileNames();
289 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
295 Strings::const_iterator docit = names.begin();
296 Strings::const_iterator end = names.end();
297 for (; docit != end; ++docit, ++ii) {
299 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
300 string label = MakeDisplayPath(*docit, 30);
302 label = tostr(ii) + ". " + label + '|' + tostr(ii);
303 tomenu.add(MenuItem(MenuItem::Command, label, action));
308 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
310 if (!buf && kind != MenuItem::ImportFormats) {
311 tomenu.add(MenuItem(MenuItem::Command,
312 _("No Documents Open!"), LFUN_NOACTION));
316 typedef vector<Format const *> Formats;
321 case MenuItem::ImportFormats:
322 formats = Importer::GetImportableFormats();
323 action = LFUN_IMPORT;
325 case MenuItem::ViewFormats:
326 formats = Exporter::GetExportableFormats(buf, true);
327 action = LFUN_PREVIEW;
329 case MenuItem::UpdateFormats:
330 formats = Exporter::GetExportableFormats(buf, true);
331 action = LFUN_UPDATE;
334 formats = Exporter::GetExportableFormats(buf, false);
335 action = LFUN_EXPORT;
337 sort(formats.begin(), formats.end(), compare_format());
339 Formats::const_iterator fit = formats.begin();
340 Formats::const_iterator end = formats.end();
341 for (; fit != end ; ++fit) {
344 string label = (*fit)->prettyname();
345 // we need to hide the default graphic export formats
346 // from the external menu, because we need them only
347 // for the internal lyx-view and external latex run
348 if (label == "EPS" || label == "XPM" || label == "PNG")
351 if (kind == MenuItem::ImportFormats)
352 if ((*fit)->name() == "text")
353 label = _("Ascii text as lines");
354 else if ((*fit)->name() == "textparagraph")
355 label = _("Ascii text as paragraphs");
356 if (!(*fit)->shortcut().empty())
357 label += "|" + (*fit)->shortcut();
358 int const action2 = lyxaction.
359 getPseudoAction(action, (*fit)->name());
360 tomenu.add(MenuItem(MenuItem::Command, label, action2));
364 void expandFloatListInsert(Menu & tomenu)
366 FloatList::const_iterator cit = floatList.begin();
367 FloatList::const_iterator end = floatList.end();
368 for (; cit != end; ++cit) {
369 int const action = lyxaction
370 .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
371 tomenu.add(MenuItem(MenuItem::Command,
372 _(cit->second.listName()),
377 void expandFloatInsert(Menu & tomenu)
379 FloatList::const_iterator cit = floatList.begin();
380 FloatList::const_iterator end = floatList.end();
381 for (; cit != end; ++cit) {
384 lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
386 string const label = _(cit->second.name());
387 tomenu.add(MenuItem(MenuItem::Command, label, action));
389 // and the wide version
391 lyxaction.getPseudoAction(LFUN_INSET_WIDE_FLOAT,
393 string const label2 = label + _(" (wide)");
394 tomenu.add(MenuItem(MenuItem::Command, label2, action2));
401 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
402 Buffer const * buf) const
404 for (Menu::const_iterator cit = frommenu.begin();
405 cit != frommenu.end() ; ++cit) {
406 switch (cit->kind()) {
407 case MenuItem::Lastfiles:
408 expandLastfiles(tomenu);
411 case MenuItem::Documents:
412 expandDocuments(tomenu);
415 case MenuItem::ImportFormats:
416 case MenuItem::ViewFormats:
417 case MenuItem::UpdateFormats:
418 case MenuItem::ExportFormats:
419 expandFormats(cit->kind(), tomenu, buf);
422 case MenuItem::FloatListInsert:
423 expandFloatListInsert(tomenu);
426 case MenuItem::FloatInsert:
427 expandFloatInsert(tomenu);
430 case MenuItem::Submenu: {
432 item.submenu_.reset(new Menu(cit->submenuname_));
433 expand(getMenu(cit->submenuname_),
434 *item.submenu_, buf);
444 // Check whether the shortcuts are unique
445 if (lyxerr.debugging(Debug::GUI))
446 tomenu.checkShortcuts();
450 bool Menu::hasSubmenu(string const & name) const
452 return find_if(begin(), end(),
453 lyx::compare_memfun(&MenuItem::submenuname,
458 void MenuBackend::read(LyXLex & lex)
467 struct keyword_item menutags[md_last - 1] = {
468 { "end", md_endmenuset },
470 { "menubar", md_menubar }
474 if (compare_ascii_no_case(lex.getString(), "menuset")) {
475 lyxerr << "Menubackend::read: ERROR wrong token:`"
476 << lex.getString() << '\'' << endl;
479 lex.pushTable(menutags, md_last - 1);
480 if (lyxerr.debugging(Debug::PARSER))
481 lex.printTable(lyxerr);
485 while (lex.isOK() && !quit) {
492 string const name = lex.getString();
494 getMenu(name).read(lex);
506 lex.printError("menubackend::read: "
507 "Unknown menu tag: `$$Token'");
515 void MenuBackend::defaults()
519 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
524 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
525 .add(MenuItem(MenuItem::Command, _("Open...|O"), "file-open"))
526 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
527 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
528 .add(MenuItem(MenuItem::Separator))
529 .add(MenuItem(MenuItem::Lastfiles));
532 Menu import("import");
534 .add(MenuItem(MenuItem::Command,
535 _("LaTeX...|L"), "buffer-import latex"))
536 .add(MenuItem(MenuItem::Command,
537 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
542 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
543 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
544 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
545 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
548 Menu documents("documents");
549 documents.add(MenuItem(MenuItem::Documents));
552 menubar_.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
553 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
554 .add(MenuItem(MenuItem::Submenu,
555 _("Documents|D"), "documents"));
560 void MenuBackend::add(Menu const & menu)
562 menulist_.push_back(menu);
566 bool MenuBackend::hasMenu(string const & name) const
568 return find_if(begin(), end(),
569 lyx::compare_memfun(&Menu::name, name)) != end();
573 Menu const & MenuBackend::getMenu(string const & name) const
575 const_iterator cit = find_if(begin(), end(),
576 lyx::compare_memfun(&Menu::name, name));
577 lyx::Assert(cit != end());
582 Menu & MenuBackend::getMenu(string const & name)
584 MenuList::iterator it =
585 find_if(menulist_.begin(), menulist_.end(),
586 lyx::compare_memfun(&Menu::name, name));
587 lyx::Assert(it != menulist_.end());
592 Menu const & MenuBackend::getMenubar() const