1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
10 * ====================================================== */
13 #pragma implementation
18 #include "support/LAssert.h"
19 #include "MenuBackend.h"
21 #include "LyXAction.h"
24 #include "lastfiles.h"
25 #include "bufferlist.h"
26 #include "converter.h"
29 #include "support/filetools.h"
30 #include "support/lyxfunctional.h"
32 extern LyXAction lyxaction;
33 extern LastFiles * lastfiles;
34 extern BufferList bufferlist;
42 // This is the global menu definition
43 MenuBackend menubackend;
46 MenuItem::MenuItem(Kind kind, string const & label,
47 string const & command, bool optional)
48 : kind_(kind), label_(label), optional_(optional)
62 action_ = lyxaction.LookupFunc(command);
64 if (action_ == LFUN_UNKNOWN_ACTION) {
65 lyxerr << "MenuItem(): LyX command `"
66 << command << "' does not exist." << endl;
69 lyxerr[Debug::GUI] << "Optional item "
79 Menu & Menu::add(MenuItem const & i)
86 Menu & Menu::read(LyXLex & lex)
105 struct keyword_item menutags[md_last - 1] = {
106 { "documents", md_documents },
107 { "end", md_endmenu },
108 { "exportformats", md_exportformats },
109 { "importformats", md_importformats },
111 { "lastfiles", md_lastfiles },
112 { "optitem", md_optitem },
113 { "references", md_references },
114 { "separator", md_separator },
115 { "submenu", md_submenu },
117 { "updateformats", md_updateformats },
118 { "viewformats", md_viewformats }
121 lex.pushTable(menutags, md_last - 1);
122 if (lyxerr.debugging(Debug::PARSER))
123 lex.printTable(lyxerr);
126 bool optional = false;
128 while (lex.IsOK() && !quit) {
132 // fallback to md_item
135 string name = _(lex.GetString());
137 string const command = lex.GetString();
138 add(MenuItem(MenuItem::Command, name,
145 add(MenuItem(MenuItem::Separator));
149 add(MenuItem(MenuItem::Lastfiles));
153 add(MenuItem(MenuItem::Documents));
157 add(MenuItem(MenuItem::Toc));
161 add(MenuItem(MenuItem::References));
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));
182 string mlabel = _(lex.GetString());
184 string mname = lex.GetString();
185 add(MenuItem(MenuItem::Submenu, mlabel, mname));
194 lex.printError("menubar::read: "
195 "Unknown menu tag: `$$Token'");
204 void Menu::checkShortcuts() const
206 // This is a quadratic algorithm, but we do not care because
207 // it is used for debugging only.
208 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
209 string shortcut = it1->shortcut();
210 if (shortcut.empty())
212 if (!contains(it1->label(), shortcut))
213 lyxerr << "Menu warning: menu entry \""
215 << "\" does not contain shortcut `"
216 << shortcut << '\'' << endl;
217 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
218 if (!compare_no_case(it2->shortcut(), shortcut)) {
219 lyxerr << "Menu warning: menu entries "
220 << '"' << it1->fulllabel()
221 << "\" and \"" << it2->fulllabel()
222 << "\" share the same shortcut."
229 void Menu::expand(Menu & tomenu, Buffer * buf) const
231 for (const_iterator cit = begin();
232 cit != end() ; ++cit) {
233 switch ((*cit).kind()) {
234 case MenuItem::Lastfiles: {
236 for (LastFiles::const_iterator lfit = lastfiles->begin();
237 lfit != lastfiles->end() && ii < 10;
239 string label = tostr(ii) + ". "
240 + MakeDisplayPath((*lfit), 30)
242 int action = lyxaction.
243 getPseudoAction(LFUN_FILE_OPEN,
245 tomenu.add(MenuItem(MenuItem::Command,
251 case MenuItem::Documents: {
252 vector<string> names = bufferlist.getFileNames();
255 tomenu.add(MenuItem(MenuItem::Command,
256 _("No Documents Open!"),
261 for (vector<string>::const_iterator docit = names.begin();
262 docit != names.end() ; ++docit) {
264 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
265 string label = MakeDisplayPath(*docit, 30);
266 tomenu.add(MenuItem(MenuItem::Command,
272 case MenuItem::ImportFormats:
273 case MenuItem::ViewFormats:
274 case MenuItem::UpdateFormats:
275 case MenuItem::ExportFormats: {
276 vector<Format const *> formats;
278 switch ((*cit).kind()) {
279 case MenuItem::ImportFormats:
280 formats = Importer::GetImportableFormats();
281 action = LFUN_IMPORT;
283 case MenuItem::ViewFormats:
284 formats = Exporter::GetExportableFormats(buf, true);
285 action = LFUN_PREVIEW;
287 case MenuItem::UpdateFormats:
288 formats = Exporter::GetExportableFormats(buf, true);
289 action = LFUN_UPDATE;
292 formats = Exporter::GetExportableFormats(buf, false);
293 action = LFUN_EXPORT;
295 sort(formats.begin(), formats.end());
297 for (vector<Format const *>::const_iterator fit = formats.begin();
298 fit != formats.end() ; ++fit) {
301 string label = (*fit)->prettyname();
302 if ((*cit).kind() == MenuItem::ImportFormats)
303 if ((*fit)->name() == "text")
304 label = _("Ascii text as lines");
305 else if ((*fit)->name() == "textparagraph")
306 label = _("Ascii text as paragraphs");
307 if (!(*fit)->shortcut().empty())
308 label += "|" + (*fit)->shortcut();
309 int action2 = lyxaction.
310 getPseudoAction(action, (*fit)->name());
311 tomenu.add(MenuItem(MenuItem::Command,
322 // Check whether the shortcuts are unique
323 if (lyxerr.debugging(Debug::GUI))
328 void MenuBackend::read(LyXLex & lex)
337 struct keyword_item menutags[md_last - 1] = {
338 { "end", md_endmenuset },
340 { "menubar", md_menubar }
344 if (compare_no_case(lex.GetString(), "menuset"))
345 lyxerr << "Menubackend::read: ERROR wrong token:`"
346 << lex.GetString() << '\'' << endl;
348 lex.pushTable(menutags, md_last - 1);
349 if (lyxerr.debugging(Debug::PARSER))
350 lex.printTable(lyxerr);
353 bool menubar = false;
355 while (lex.IsOK() && !quit) {
359 // fallback to md_menu
362 string name = lex.GetString();
364 if (getMenu(name).menubar() == menubar) {
365 getMenu(name).read(lex);
367 lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
371 Menu menu(name, menubar);
382 lex.printError("menubackend::read: "
383 "Unknown menu tag: `$$Token'");
391 void MenuBackend::defaults()
395 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
400 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
401 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
402 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
403 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
404 .add(MenuItem(MenuItem::Separator))
405 .add(MenuItem(MenuItem::Lastfiles));
408 Menu import("import");
410 .add(MenuItem(MenuItem::Command,
411 _("LaTeX...|L"), "buffer-import latex"))
412 .add(MenuItem(MenuItem::Command,
413 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
418 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
419 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
420 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
421 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
424 Menu documents("documents");
425 documents.add(MenuItem(MenuItem::Documents));
428 Menu main("main", true);
430 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
431 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
432 .add(MenuItem(MenuItem::Submenu,
433 _("Documents|D"), "documents"));
436 Menu main_nobuffer("main_nobuffer", true);
437 main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
440 if (lyxerr.debugging(Debug::GUI)) {
441 for (const_iterator cit = begin();
442 cit != end() ; ++cit)
443 lyxerr << "Menu name: " << cit->name()
444 << ", Menubar: " << cit->menubar()
450 void MenuBackend::add(Menu const & menu)
452 menulist_.push_back(menu);
456 bool MenuBackend::hasMenu(string const & name) const
458 return find_if(begin(), end(),
459 compare_memfun(&Menu::name, name)) != end();
463 Menu const & MenuBackend::getMenu(string const & name) const
465 const_iterator cit = find_if(begin(), end(),
466 compare_memfun(&Menu::name, name));
467 Assert(cit != end());
472 Menu & MenuBackend::getMenu(string const & name)
474 MenuList::iterator it = find_if(menulist_.begin(), menulist_.end(),
475 compare_memfun(&Menu::name, name));
476 Assert(it != menulist_.end());