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 "bufferlist.h"
26 #include "converter.h"
29 #include "FloatList.h"
30 #include "support/LAssert.h"
31 #include "support/filetools.h"
32 #include "support/lyxfunctional.h"
33 #include "support/lstrings.h"
35 extern LyXAction lyxaction;
36 extern LastFiles * lastfiles;
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 "
82 string const MenuItem::label() const
84 return token(label_, '|', 0);
87 string const MenuItem::shortcut() const
89 return token(label_, '|', 1);
93 Menu & Menu::add(MenuItem const & i)
100 Menu & Menu::read(LyXLex & lex)
120 struct keyword_item menutags[md_last - 1] = {
121 { "documents", md_documents },
122 { "end", md_endmenu },
123 { "exportformats", md_exportformats },
124 { "floatinsert", md_floatinsert },
125 { "floatlistinsert", md_floatlistinsert },
126 { "importformats", md_importformats },
128 { "lastfiles", md_lastfiles },
129 { "optitem", md_optitem },
130 { "separator", md_separator },
131 { "submenu", md_submenu },
133 { "updateformats", md_updateformats },
134 { "viewformats", md_viewformats }
137 lex.pushTable(menutags, md_last - 1);
138 if (lyxerr.debugging(Debug::PARSER))
139 lex.printTable(lyxerr);
142 bool optional = false;
144 while (lex.isOK() && !quit) {
148 // fallback to md_item
151 string const name = _(lex.getString());
153 string const command = lex.getString();
154 add(MenuItem(MenuItem::Command, name,
161 add(MenuItem(MenuItem::Separator));
165 add(MenuItem(MenuItem::Lastfiles));
169 add(MenuItem(MenuItem::Documents));
173 add(MenuItem(MenuItem::Toc));
177 add(MenuItem(MenuItem::ViewFormats));
180 case md_updateformats:
181 add(MenuItem(MenuItem::UpdateFormats));
184 case md_exportformats:
185 add(MenuItem(MenuItem::ExportFormats));
188 case md_importformats:
189 add(MenuItem(MenuItem::ImportFormats));
192 case md_floatlistinsert:
193 add(MenuItem(MenuItem::FloatListInsert));
197 add(MenuItem(MenuItem::FloatInsert));
202 string const mlabel = _(lex.getString());
204 string const mname = lex.getString();
205 add(MenuItem(MenuItem::Submenu, mlabel, mname));
214 lex.printError("menubar::read: "
215 "Unknown menu tag: `$$Token'");
224 void Menu::checkShortcuts() const
226 // This is a quadratic algorithm, but we do not care because
227 // it is used for debugging only.
228 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
229 string shortcut = it1->shortcut();
230 if (shortcut.empty())
232 if (!contains(it1->label(), shortcut))
233 lyxerr << "Menu warning: menu entry \""
235 << "\" does not contain shortcut `"
236 << shortcut << '\'' << endl;
237 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
238 if (!compare_no_case(it2->shortcut(), shortcut)) {
239 lyxerr << "Menu warning: menu entries "
240 << '"' << it1->fulllabel()
241 << "\" and \"" << it2->fulllabel()
242 << "\" share the same shortcut."
251 class compare_format {
253 bool operator()(Format const * p1, Format const * p2) {
261 void Menu::expand(Menu & tomenu, Buffer * buf) const
263 for (const_iterator cit = begin();
264 cit != end() ; ++cit) {
265 switch (cit->kind()) {
266 case MenuItem::Lastfiles: {
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,
284 case MenuItem::Documents: {
285 typedef vector<string> Strings;
287 Strings const names = bufferlist.getFileNames();
290 tomenu.add(MenuItem(MenuItem::Command,
291 _("No Documents Open!"),
296 Strings::const_iterator docit = names.begin();
297 Strings::const_iterator end = names.end();
298 for (; docit != end ; ++docit) {
299 int const action = lyxaction
300 .getPseudoAction(LFUN_SWITCHBUFFER,
303 MakeDisplayPath(*docit, 30);
304 tomenu.add(MenuItem(MenuItem::Command,
310 case MenuItem::ImportFormats:
311 case MenuItem::ViewFormats:
312 case MenuItem::UpdateFormats:
313 case MenuItem::ExportFormats: {
314 typedef vector<Format const *> Formats;
319 switch (cit->kind()) {
320 case MenuItem::ImportFormats:
321 formats = Importer::GetImportableFormats();
322 action = LFUN_IMPORT;
324 case MenuItem::ViewFormats:
325 formats = Exporter::GetExportableFormats(buf, true);
326 action = LFUN_PREVIEW;
328 case MenuItem::UpdateFormats:
329 formats = Exporter::GetExportableFormats(buf, true);
330 action = LFUN_UPDATE;
333 formats = Exporter::GetExportableFormats(buf, false);
334 action = LFUN_EXPORT;
336 sort(formats.begin(), formats.end(), compare_format());
338 Formats::const_iterator fit = formats.begin();
339 Formats::const_iterator end = formats.end();
341 for (; fit != end ; ++fit) {
344 string label = (*fit)->prettyname();
345 if (cit->kind() == MenuItem::ImportFormats)
346 if ((*fit)->name() == "text")
347 label = _("Ascii text as lines");
348 else if ((*fit)->name() == "textparagraph")
349 label = _("Ascii text as paragraphs");
350 if (!(*fit)->shortcut().empty())
351 label += "|" + (*fit)->shortcut();
352 int const action2 = lyxaction.
353 getPseudoAction(action,
355 tomenu.add(MenuItem(MenuItem::Command,
361 case MenuItem::FloatListInsert:
363 FloatList::const_iterator cit = floatList.begin();
364 FloatList::const_iterator end = floatList.end();
365 for (; cit != end; ++cit) {
366 int const action = lyxaction
367 .getPseudoAction(LFUN_FLOAT_LIST,
369 string const label = cit->second.name() + _(" List");
370 tomenu.add(MenuItem(MenuItem::Command,
376 case MenuItem::FloatInsert:
378 FloatList::const_iterator cit = floatList.begin();
379 FloatList::const_iterator end = floatList.end();
380 for (; cit != end; ++cit) {
382 int const action = lyxaction
383 .getPseudoAction(LFUN_INSET_FLOAT,
385 string const label = cit->second.name();
386 tomenu.add(MenuItem(MenuItem::Command,
389 // and the wide version
390 int const action2 = lyxaction
391 .getPseudoAction(LFUN_INSET_WIDE_FLOAT,
393 string const label2 = _("Wide ") + label;
394 tomenu.add(MenuItem(MenuItem::Command,
405 // Check whether the shortcuts are unique
406 if (lyxerr.debugging(Debug::GUI))
411 bool Menu::hasSubmenu(string const & name) const
413 return find_if(begin(), end(),
414 lyx::compare_memfun(&MenuItem::submenu, name)) != end();
418 void MenuBackend::read(LyXLex & lex)
427 struct keyword_item menutags[md_last - 1] = {
428 { "end", md_endmenuset },
430 { "menubar", md_menubar }
434 if (compare_no_case(lex.getString(), "menuset")) {
435 lyxerr << "Menubackend::read: ERROR wrong token:`"
436 << lex.getString() << '\'' << endl;
439 lex.pushTable(menutags, md_last - 1);
440 if (lyxerr.debugging(Debug::PARSER))
441 lex.printTable(lyxerr);
444 bool menubar = false;
446 while (lex.isOK() && !quit) {
450 // fallback to md_menu
453 string const name = lex.getString();
455 if (getMenu(name).menubar() == menubar) {
456 getMenu(name).read(lex);
458 lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
462 Menu menu(name, menubar);
473 lex.printError("menubackend::read: "
474 "Unknown menu tag: `$$Token'");
482 void MenuBackend::defaults()
486 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
491 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
492 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
493 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
494 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
495 .add(MenuItem(MenuItem::Separator))
496 .add(MenuItem(MenuItem::Lastfiles));
499 Menu import("import");
501 .add(MenuItem(MenuItem::Command,
502 _("LaTeX...|L"), "buffer-import latex"))
503 .add(MenuItem(MenuItem::Command,
504 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
509 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
510 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
511 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
512 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
515 Menu documents("documents");
516 documents.add(MenuItem(MenuItem::Documents));
519 Menu main("main", true);
521 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
522 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
523 .add(MenuItem(MenuItem::Submenu,
524 _("Documents|D"), "documents"));
527 Menu main_nobuffer("main_nobuffer", true);
528 main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
531 if (lyxerr.debugging(Debug::GUI)) {
532 for (const_iterator cit = begin();
533 cit != end() ; ++cit)
534 lyxerr << "Menu name: " << cit->name()
535 << ", Menubar: " << cit->menubar()
541 void MenuBackend::add(Menu const & menu)
543 menulist_.push_back(menu);
547 bool MenuBackend::hasMenu(string const & name) const
549 return find_if(begin(), end(),
550 lyx::compare_memfun(&Menu::name, name)) != end();
554 Menu const & MenuBackend::getMenu(string const & name) const
556 const_iterator cit = find_if(begin(), end(),
557 lyx::compare_memfun(&Menu::name, name));
558 lyx::Assert(cit != end());
563 Menu & MenuBackend::getMenu(string const & name)
565 MenuList::iterator it =
566 find_if(menulist_.begin(), menulist_.end(),
567 lyx::compare_memfun(&Menu::name, name));
568 lyx::Assert(it != menulist_.end());