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 "
83 string const MenuItem::label() const
85 return token(label_, '|', 0);
89 string const MenuItem::shortcut() const
91 return token(label_, '|', 1);
95 Menu & Menu::add(MenuItem const & i)
102 Menu & Menu::read(LyXLex & lex)
122 struct keyword_item menutags[md_last - 1] = {
123 { "documents", md_documents },
124 { "end", md_endmenu },
125 { "exportformats", md_exportformats },
126 { "floatinsert", md_floatinsert },
127 { "floatlistinsert", md_floatlistinsert },
128 { "importformats", md_importformats },
130 { "lastfiles", md_lastfiles },
131 { "optitem", md_optitem },
132 { "separator", md_separator },
133 { "submenu", md_submenu },
135 { "updateformats", md_updateformats },
136 { "viewformats", md_viewformats }
139 lex.pushTable(menutags, md_last - 1);
140 if (lyxerr.debugging(Debug::PARSER))
141 lex.printTable(lyxerr);
144 bool optional = false;
146 while (lex.isOK() && !quit) {
150 // fallback to md_item
153 string const name = _(lex.getString());
155 string const command = lex.getString();
156 add(MenuItem(MenuItem::Command, name,
163 add(MenuItem(MenuItem::Separator));
167 add(MenuItem(MenuItem::Lastfiles));
171 add(MenuItem(MenuItem::Documents));
175 add(MenuItem(MenuItem::Toc));
179 add(MenuItem(MenuItem::ViewFormats));
182 case md_updateformats:
183 add(MenuItem(MenuItem::UpdateFormats));
186 case md_exportformats:
187 add(MenuItem(MenuItem::ExportFormats));
190 case md_importformats:
191 add(MenuItem(MenuItem::ImportFormats));
194 case md_floatlistinsert:
195 add(MenuItem(MenuItem::FloatListInsert));
199 add(MenuItem(MenuItem::FloatInsert));
204 string const mlabel = _(lex.getString());
206 string const mname = lex.getString();
207 add(MenuItem(MenuItem::Submenu, mlabel, mname));
216 lex.printError("menubar::read: "
217 "Unknown menu tag: `$$Token'");
226 void Menu::checkShortcuts() const
228 // This is a quadratic algorithm, but we do not care because
229 // it is used for debugging only.
230 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
231 string shortcut = it1->shortcut();
232 if (shortcut.empty())
234 if (!contains(it1->label(), shortcut))
235 lyxerr << "Menu warning: menu entry \""
237 << "\" does not contain shortcut `"
238 << shortcut << '\'' << endl;
239 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
240 if (!compare_no_case(it2->shortcut(), shortcut)) {
241 lyxerr << "Menu warning: menu entries "
242 << '"' << it1->fulllabel()
243 << "\" and \"" << it2->fulllabel()
244 << "\" share the same shortcut."
254 class compare_format {
256 bool operator()(Format const * p1, Format const * p2) {
264 void Menu::expand(Menu & tomenu, Buffer * buf) const
266 for (const_iterator cit = begin();
267 cit != end() ; ++cit) {
268 switch (cit->kind()) {
269 case MenuItem::Lastfiles: {
271 LastFiles::const_iterator lfit = lastfiles->begin();
272 LastFiles::const_iterator end = lastfiles->end();
274 for (; lfit != end && ii < 10; ++lfit, ++ii) {
275 string const label = tostr(ii) + ". "
276 + MakeDisplayPath((*lfit), 30)
278 int const action = lyxaction.
279 getPseudoAction(LFUN_FILE_OPEN,
281 tomenu.add(MenuItem(MenuItem::Command,
287 case MenuItem::Documents: {
288 typedef vector<string> Strings;
290 Strings const names = bufferlist.getFileNames();
293 tomenu.add(MenuItem(MenuItem::Command,
294 _("No Documents Open!"),
299 Strings::const_iterator docit = names.begin();
300 Strings::const_iterator end = names.end();
301 for (; docit != end ; ++docit) {
302 int const action = lyxaction
303 .getPseudoAction(LFUN_SWITCHBUFFER,
306 MakeDisplayPath(*docit, 30);
307 tomenu.add(MenuItem(MenuItem::Command,
313 case MenuItem::ImportFormats:
314 case MenuItem::ViewFormats:
315 case MenuItem::UpdateFormats:
316 case MenuItem::ExportFormats: {
318 if (!buf && cit->kind() != MenuItem::ImportFormats) {
319 tomenu.add(MenuItem(MenuItem::Command,
320 _("No Documents Open!"),
325 typedef vector<Format const *> Formats;
330 switch (cit->kind()) {
331 case MenuItem::ImportFormats:
332 formats = Importer::GetImportableFormats();
333 action = LFUN_IMPORT;
335 case MenuItem::ViewFormats:
336 formats = Exporter::GetExportableFormats(buf, true);
337 action = LFUN_PREVIEW;
339 case MenuItem::UpdateFormats:
340 formats = Exporter::GetExportableFormats(buf, true);
341 action = LFUN_UPDATE;
344 formats = Exporter::GetExportableFormats(buf, false);
345 action = LFUN_EXPORT;
347 sort(formats.begin(), formats.end(), compare_format());
349 Formats::const_iterator fit = formats.begin();
350 Formats::const_iterator end = formats.end();
352 for (; fit != end ; ++fit) {
355 string label = (*fit)->prettyname();
356 if (cit->kind() == MenuItem::ImportFormats)
357 if ((*fit)->name() == "text")
358 label = _("Ascii text as lines");
359 else if ((*fit)->name() == "textparagraph")
360 label = _("Ascii text as paragraphs");
361 if (!(*fit)->shortcut().empty())
362 label += "|" + (*fit)->shortcut();
363 int const action2 = lyxaction.
364 getPseudoAction(action,
366 tomenu.add(MenuItem(MenuItem::Command,
372 case MenuItem::FloatListInsert:
374 FloatList::const_iterator cit = floatList.begin();
375 FloatList::const_iterator end = floatList.end();
376 for (; cit != end; ++cit) {
377 int const action = lyxaction
378 .getPseudoAction(LFUN_FLOAT_LIST,
380 tomenu.add(MenuItem(MenuItem::Command,
381 _(cit->second.listName()),
387 case MenuItem::FloatInsert:
389 FloatList::const_iterator cit = floatList.begin();
390 FloatList::const_iterator end = floatList.end();
391 for (; cit != end; ++cit) {
393 int const action = lyxaction
394 .getPseudoAction(LFUN_INSET_FLOAT,
396 string const label = _(cit->second.name());
397 tomenu.add(MenuItem(MenuItem::Command,
400 // and the wide version
401 int const action2 = lyxaction
402 .getPseudoAction(LFUN_INSET_WIDE_FLOAT,
404 string const label2 = label + _(" (wide)");
405 tomenu.add(MenuItem(MenuItem::Command,
416 // Check whether the shortcuts are unique
417 if (lyxerr.debugging(Debug::GUI))
422 bool Menu::hasSubmenu(string const & name) const
424 return find_if(begin(), end(),
425 lyx::compare_memfun(&MenuItem::submenu, name)) != end();
429 void MenuBackend::read(LyXLex & lex)
438 struct keyword_item menutags[md_last - 1] = {
439 { "end", md_endmenuset },
441 { "menubar", md_menubar }
445 if (compare_no_case(lex.getString(), "menuset")) {
446 lyxerr << "Menubackend::read: ERROR wrong token:`"
447 << lex.getString() << '\'' << endl;
450 lex.pushTable(menutags, md_last - 1);
451 if (lyxerr.debugging(Debug::PARSER))
452 lex.printTable(lyxerr);
455 bool menubar = false;
457 while (lex.isOK() && !quit) {
461 // fallback to md_menu
464 string const name = lex.getString();
466 if (getMenu(name).menubar() == menubar) {
467 getMenu(name).read(lex);
469 lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
473 Menu menu(name, menubar);
484 lex.printError("menubackend::read: "
485 "Unknown menu tag: `$$Token'");
493 void MenuBackend::defaults()
497 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
502 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
503 .add(MenuItem(MenuItem::Command, _("Open...|O"), "file-open"))
504 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
505 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
506 .add(MenuItem(MenuItem::Separator))
507 .add(MenuItem(MenuItem::Lastfiles));
510 Menu import("import");
512 .add(MenuItem(MenuItem::Command,
513 _("LaTeX...|L"), "buffer-import latex"))
514 .add(MenuItem(MenuItem::Command,
515 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
520 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
521 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
522 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
523 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
526 Menu documents("documents");
527 documents.add(MenuItem(MenuItem::Documents));
530 Menu main("main", true);
532 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
533 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
534 .add(MenuItem(MenuItem::Submenu,
535 _("Documents|D"), "documents"));
538 Menu main_nobuffer("main_nobuffer", true);
539 main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
542 if (lyxerr.debugging(Debug::GUI)) {
543 for (const_iterator cit = begin();
544 cit != end() ; ++cit)
545 lyxerr << "Menu name: " << cit->name()
546 << ", Menubar: " << cit->menubar()
552 void MenuBackend::add(Menu const & menu)
554 menulist_.push_back(menu);
558 bool MenuBackend::hasMenu(string const & name) const
560 return find_if(begin(), end(),
561 lyx::compare_memfun(&Menu::name, name)) != end();
565 Menu const & MenuBackend::getMenu(string const & name) const
567 const_iterator cit = find_if(begin(), end(),
568 lyx::compare_memfun(&Menu::name, name));
569 lyx::Assert(cit != end());
574 Menu & MenuBackend::getMenu(string const & name)
576 MenuList::iterator it =
577 find_if(menulist_.begin(), menulist_.end(),
578 lyx::compare_memfun(&Menu::name, name));
579 lyx::Assert(it != menulist_.end());