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
18 #include "MenuBackend.h"
20 #include "LyXAction.h"
23 #include "lastfiles.h"
24 #include "bufferlist.h"
25 #include "converter.h"
28 #include "FloatList.h"
29 #include "support/LAssert.h"
30 #include "support/filetools.h"
31 #include "support/lyxfunctional.h"
33 extern LyXAction lyxaction;
34 extern LastFiles * lastfiles;
35 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 "
81 Menu & Menu::add(MenuItem const & i)
88 Menu & Menu::read(LyXLex & lex)
108 struct keyword_item menutags[md_last - 1] = {
109 { "documents", md_documents },
110 { "end", md_endmenu },
111 { "exportformats", md_exportformats },
112 { "floatinsert", md_floatinsert },
113 { "floatlistinsert", md_floatlistinsert },
114 { "importformats", md_importformats },
116 { "lastfiles", md_lastfiles },
117 { "optitem", md_optitem },
118 { "separator", md_separator },
119 { "submenu", md_submenu },
121 { "updateformats", md_updateformats },
122 { "viewformats", md_viewformats }
125 lex.pushTable(menutags, md_last - 1);
126 if (lyxerr.debugging(Debug::PARSER))
127 lex.printTable(lyxerr);
130 bool optional = false;
132 while (lex.IsOK() && !quit) {
136 // fallback to md_item
139 string const name = _(lex.GetString());
141 string const command = lex.GetString();
142 add(MenuItem(MenuItem::Command, name,
149 add(MenuItem(MenuItem::Separator));
153 add(MenuItem(MenuItem::Lastfiles));
157 add(MenuItem(MenuItem::Documents));
161 add(MenuItem(MenuItem::Toc));
165 add(MenuItem(MenuItem::ViewFormats));
168 case md_updateformats:
169 add(MenuItem(MenuItem::UpdateFormats));
172 case md_exportformats:
173 add(MenuItem(MenuItem::ExportFormats));
176 case md_importformats:
177 add(MenuItem(MenuItem::ImportFormats));
180 case md_floatlistinsert:
181 add(MenuItem(MenuItem::FloatListInsert));
185 add(MenuItem(MenuItem::FloatInsert));
190 string mlabel = _(lex.GetString());
192 string mname = lex.GetString();
193 add(MenuItem(MenuItem::Submenu, mlabel, mname));
202 lex.printError("menubar::read: "
203 "Unknown menu tag: `$$Token'");
212 void Menu::checkShortcuts() const
214 // This is a quadratic algorithm, but we do not care because
215 // it is used for debugging only.
216 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
217 string shortcut = it1->shortcut();
218 if (shortcut.empty())
220 if (!contains(it1->label(), shortcut))
221 lyxerr << "Menu warning: menu entry \""
223 << "\" does not contain shortcut `"
224 << shortcut << '\'' << endl;
225 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
226 if (!compare_no_case(it2->shortcut(), shortcut)) {
227 lyxerr << "Menu warning: menu entries "
228 << '"' << it1->fulllabel()
229 << "\" and \"" << it2->fulllabel()
230 << "\" share the same shortcut."
240 class compare_format {
242 bool operator()(Format const * p1, Format const * p2) {
250 void Menu::expand(Menu & tomenu, Buffer * buf) const
252 for (const_iterator cit = begin();
253 cit != end() ; ++cit) {
254 switch ((*cit).kind()) {
255 case MenuItem::Lastfiles: {
257 LastFiles::const_iterator lfit = lastfiles->begin();
258 LastFiles::const_iterator end = lastfiles->end();
260 for (; lfit != end && ii < 10; ++lfit, ++ii) {
261 string const label = tostr(ii) + ". "
262 + MakeDisplayPath((*lfit), 30)
264 int const action = lyxaction.
265 getPseudoAction(LFUN_FILE_OPEN,
267 tomenu.add(MenuItem(MenuItem::Command,
273 case MenuItem::Documents: {
274 typedef vector<string> Strings;
276 Strings const names = bufferlist.getFileNames();
279 tomenu.add(MenuItem(MenuItem::Command,
280 _("No Documents Open!"),
285 Strings::const_iterator docit = names.begin();
286 Strings::const_iterator end = names.end();
287 for (; docit != end ; ++docit) {
288 int const action = lyxaction
289 .getPseudoAction(LFUN_SWITCHBUFFER,
292 MakeDisplayPath(*docit, 30);
293 tomenu.add(MenuItem(MenuItem::Command,
299 case MenuItem::ImportFormats:
300 case MenuItem::ViewFormats:
301 case MenuItem::UpdateFormats:
302 case MenuItem::ExportFormats: {
303 typedef vector<Format const *> Formats;
308 switch ((*cit).kind()) {
309 case MenuItem::ImportFormats:
310 formats = Importer::GetImportableFormats();
311 action = LFUN_IMPORT;
313 case MenuItem::ViewFormats:
314 formats = Exporter::GetExportableFormats(buf, true);
315 action = LFUN_PREVIEW;
317 case MenuItem::UpdateFormats:
318 formats = Exporter::GetExportableFormats(buf, true);
319 action = LFUN_UPDATE;
322 formats = Exporter::GetExportableFormats(buf, false);
323 action = LFUN_EXPORT;
325 sort(formats.begin(), formats.end(), compare_format());
327 Formats::const_iterator fit = formats.begin();
328 Formats::const_iterator end = formats.end();
330 for (; fit != end ; ++fit) {
333 string label = (*fit)->prettyname();
334 if ((*cit).kind() == MenuItem::ImportFormats)
335 if ((*fit)->name() == "text")
336 label = _("Ascii text as lines");
337 else if ((*fit)->name() == "textparagraph")
338 label = _("Ascii text as paragraphs");
339 if (!(*fit)->shortcut().empty())
340 label += "|" + (*fit)->shortcut();
341 int const action2 = lyxaction.
342 getPseudoAction(action,
344 tomenu.add(MenuItem(MenuItem::Command,
350 case MenuItem::FloatListInsert:
352 FloatList::const_iterator cit = floatList.begin();
353 FloatList::const_iterator end = floatList.end();
354 for (; cit != end; ++cit) {
355 int const action = lyxaction
356 .getPseudoAction(LFUN_FLOAT_LIST,
358 string label = _("List of ");
359 label += cit->second.name();
360 tomenu.add(MenuItem(MenuItem::Command,
366 case MenuItem::FloatInsert:
368 FloatList::const_iterator cit = floatList.begin();
369 FloatList::const_iterator end = floatList.end();
370 for (; cit != end; ++cit) {
372 int const action = lyxaction
373 .getPseudoAction(LFUN_INSET_FLOAT,
375 string const label = cit->second.name();
376 tomenu.add(MenuItem(MenuItem::Command,
379 // and the wide version
380 int const action2 = lyxaction
381 .getPseudoAction(LFUN_INSET_WIDE_FLOAT,
383 string const label2 = _("Wide ") + label;
384 tomenu.add(MenuItem(MenuItem::Command,
395 // Check whether the shortcuts are unique
396 if (lyxerr.debugging(Debug::GUI))
401 bool Menu::hasSubmenu(string const & name) const
403 return find_if(begin(), end(),
404 lyx::compare_memfun(&MenuItem::submenu, name)) != end();
408 void MenuBackend::read(LyXLex & lex)
417 struct keyword_item menutags[md_last - 1] = {
418 { "end", md_endmenuset },
420 { "menubar", md_menubar }
424 if (compare_no_case(lex.GetString(), "menuset"))
425 lyxerr << "Menubackend::read: ERROR wrong token:`"
426 << lex.GetString() << '\'' << endl;
428 lex.pushTable(menutags, md_last - 1);
429 if (lyxerr.debugging(Debug::PARSER))
430 lex.printTable(lyxerr);
433 bool menubar = false;
435 while (lex.IsOK() && !quit) {
439 // fallback to md_menu
442 string name = lex.GetString();
444 if (getMenu(name).menubar() == menubar) {
445 getMenu(name).read(lex);
447 lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
451 Menu menu(name, menubar);
462 lex.printError("menubackend::read: "
463 "Unknown menu tag: `$$Token'");
471 void MenuBackend::defaults()
475 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
480 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
481 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
482 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
483 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
484 .add(MenuItem(MenuItem::Separator))
485 .add(MenuItem(MenuItem::Lastfiles));
488 Menu import("import");
490 .add(MenuItem(MenuItem::Command,
491 _("LaTeX...|L"), "buffer-import latex"))
492 .add(MenuItem(MenuItem::Command,
493 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
498 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
499 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
500 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
501 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
504 Menu documents("documents");
505 documents.add(MenuItem(MenuItem::Documents));
508 Menu main("main", true);
510 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
511 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
512 .add(MenuItem(MenuItem::Submenu,
513 _("Documents|D"), "documents"));
516 Menu main_nobuffer("main_nobuffer", true);
517 main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
520 if (lyxerr.debugging(Debug::GUI)) {
521 for (const_iterator cit = begin();
522 cit != end() ; ++cit)
523 lyxerr << "Menu name: " << cit->name()
524 << ", Menubar: " << cit->menubar()
530 void MenuBackend::add(Menu const & menu)
532 menulist_.push_back(menu);
536 bool MenuBackend::hasMenu(string const & name) const
538 return find_if(begin(), end(),
539 lyx::compare_memfun(&Menu::name, name)) != end();
543 Menu const & MenuBackend::getMenu(string const & name) const
545 const_iterator cit = find_if(begin(), end(),
546 lyx::compare_memfun(&Menu::name, name));
547 lyx::Assert(cit != end());
552 Menu & MenuBackend::getMenu(string const & name)
554 MenuList::iterator it =
555 find_if(menulist_.begin(), menulist_.end(),
556 lyx::compare_memfun(&Menu::name, name));
557 lyx::Assert(it != menulist_.end());