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)
61 action_ = lyxaction.LookupFunc(command);
63 if (action_ == LFUN_UNKNOWN_ACTION) {
64 lyxerr << "MenuItem(): LyX command `"
65 << command << "' does not exist." << endl;
68 lyxerr[Debug::GUI] << "Optional item "
78 Menu & Menu::add(MenuItem const & i)
85 Menu & Menu::read(LyXLex & lex)
103 struct keyword_item menutags[md_last - 1] = {
104 { "documents", md_documents },
105 { "end", md_endmenu },
106 { "exportformats", md_exportformats },
107 { "importformats", md_importformats },
109 { "lastfiles", md_lastfiles },
110 { "optitem", md_optitem },
111 { "separator", md_separator },
112 { "submenu", md_submenu },
114 { "updateformats", md_updateformats },
115 { "viewformats", md_viewformats }
118 lex.pushTable(menutags, md_last - 1);
119 if (lyxerr.debugging(Debug::PARSER))
120 lex.printTable(lyxerr);
123 bool optional = false;
125 while (lex.IsOK() && !quit) {
129 // fallback to md_item
132 string name = _(lex.GetString());
134 string const command = lex.GetString();
135 add(MenuItem(MenuItem::Command, name,
142 add(MenuItem(MenuItem::Separator));
146 add(MenuItem(MenuItem::Lastfiles));
150 add(MenuItem(MenuItem::Documents));
154 add(MenuItem(MenuItem::Toc));
158 add(MenuItem(MenuItem::ViewFormats));
161 case md_updateformats:
162 add(MenuItem(MenuItem::UpdateFormats));
165 case md_exportformats:
166 add(MenuItem(MenuItem::ExportFormats));
169 case md_importformats:
170 add(MenuItem(MenuItem::ImportFormats));
175 string mlabel = _(lex.GetString());
177 string mname = lex.GetString();
178 add(MenuItem(MenuItem::Submenu, mlabel, mname));
187 lex.printError("menubar::read: "
188 "Unknown menu tag: `$$Token'");
197 void Menu::checkShortcuts() const
199 // This is a quadratic algorithm, but we do not care because
200 // it is used for debugging only.
201 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
202 string shortcut = it1->shortcut();
203 if (shortcut.empty())
205 if (!contains(it1->label(), shortcut))
206 lyxerr << "Menu warning: menu entry \""
208 << "\" does not contain shortcut `"
209 << shortcut << '\'' << endl;
210 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
211 if (!compare_no_case(it2->shortcut(), shortcut)) {
212 lyxerr << "Menu warning: menu entries "
213 << '"' << it1->fulllabel()
214 << "\" and \"" << it2->fulllabel()
215 << "\" share the same shortcut."
225 class compare_format {
227 bool operator()(Format const * p1, Format const * p2) {
235 void Menu::expand(Menu & tomenu, Buffer * buf) const
237 for (const_iterator cit = begin();
238 cit != end() ; ++cit) {
239 switch ((*cit).kind()) {
240 case MenuItem::Lastfiles: {
242 for (LastFiles::const_iterator lfit = lastfiles->begin();
243 lfit != lastfiles->end() && ii < 10;
245 string const label = tostr(ii) + ". "
246 + MakeDisplayPath((*lfit), 30)
248 int const action = lyxaction.
249 getPseudoAction(LFUN_FILE_OPEN,
251 tomenu.add(MenuItem(MenuItem::Command,
257 case MenuItem::Documents: {
258 vector<string> const names = bufferlist.getFileNames();
261 tomenu.add(MenuItem(MenuItem::Command,
262 _("No Documents Open!"),
267 for (vector<string>::const_iterator docit = names.begin();
268 docit != names.end() ; ++docit) {
270 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
272 MakeDisplayPath(*docit, 30);
273 tomenu.add(MenuItem(MenuItem::Command,
279 case MenuItem::ImportFormats:
280 case MenuItem::ViewFormats:
281 case MenuItem::UpdateFormats:
282 case MenuItem::ExportFormats: {
283 vector<Format const *> formats;
285 switch ((*cit).kind()) {
286 case MenuItem::ImportFormats:
287 formats = Importer::GetImportableFormats();
288 action = LFUN_IMPORT;
290 case MenuItem::ViewFormats:
291 formats = Exporter::GetExportableFormats(buf, true);
292 action = LFUN_PREVIEW;
294 case MenuItem::UpdateFormats:
295 formats = Exporter::GetExportableFormats(buf, true);
296 action = LFUN_UPDATE;
299 formats = Exporter::GetExportableFormats(buf, false);
300 action = LFUN_EXPORT;
302 sort(formats.begin(), formats.end(), compare_format());
304 for (vector<Format const *>::const_iterator fit = formats.begin();
305 fit != formats.end() ; ++fit) {
308 string label = (*fit)->prettyname();
309 if ((*cit).kind() == MenuItem::ImportFormats)
310 if ((*fit)->name() == "text")
311 label = _("Ascii text as lines");
312 else if ((*fit)->name() == "textparagraph")
313 label = _("Ascii text as paragraphs");
314 if (!(*fit)->shortcut().empty())
315 label += "|" + (*fit)->shortcut();
316 int const action2 = lyxaction.
317 getPseudoAction(action, (*fit)->name());
318 tomenu.add(MenuItem(MenuItem::Command,
329 // Check whether the shortcuts are unique
330 if (lyxerr.debugging(Debug::GUI))
334 bool Menu::hasSubmenu(string const & name) const
336 return find_if(begin(), end(),
337 lyx::compare_memfun(&MenuItem::submenu, name)) != end();
341 void MenuBackend::read(LyXLex & lex)
350 struct keyword_item menutags[md_last - 1] = {
351 { "end", md_endmenuset },
353 { "menubar", md_menubar }
357 if (compare_no_case(lex.GetString(), "menuset"))
358 lyxerr << "Menubackend::read: ERROR wrong token:`"
359 << lex.GetString() << '\'' << endl;
361 lex.pushTable(menutags, md_last - 1);
362 if (lyxerr.debugging(Debug::PARSER))
363 lex.printTable(lyxerr);
366 bool menubar = false;
368 while (lex.IsOK() && !quit) {
372 // fallback to md_menu
375 string name = lex.GetString();
377 if (getMenu(name).menubar() == menubar) {
378 getMenu(name).read(lex);
380 lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
384 Menu menu(name, menubar);
395 lex.printError("menubackend::read: "
396 "Unknown menu tag: `$$Token'");
404 void MenuBackend::defaults()
408 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
413 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
414 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
415 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
416 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
417 .add(MenuItem(MenuItem::Separator))
418 .add(MenuItem(MenuItem::Lastfiles));
421 Menu import("import");
423 .add(MenuItem(MenuItem::Command,
424 _("LaTeX...|L"), "buffer-import latex"))
425 .add(MenuItem(MenuItem::Command,
426 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
431 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
432 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
433 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
434 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
437 Menu documents("documents");
438 documents.add(MenuItem(MenuItem::Documents));
441 Menu main("main", true);
443 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
444 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
445 .add(MenuItem(MenuItem::Submenu,
446 _("Documents|D"), "documents"));
449 Menu main_nobuffer("main_nobuffer", true);
450 main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
453 if (lyxerr.debugging(Debug::GUI)) {
454 for (const_iterator cit = begin();
455 cit != end() ; ++cit)
456 lyxerr << "Menu name: " << cit->name()
457 << ", Menubar: " << cit->menubar()
463 void MenuBackend::add(Menu const & menu)
465 menulist_.push_back(menu);
469 bool MenuBackend::hasMenu(string const & name) const
471 return find_if(begin(), end(),
472 lyx::compare_memfun(&Menu::name, name)) != end();
476 Menu const & MenuBackend::getMenu(string const & name) const
478 const_iterator cit = find_if(begin(), end(),
479 lyx::compare_memfun(&Menu::name, name));
480 lyx::Assert(cit != end());
485 Menu & MenuBackend::getMenu(string const & name)
487 MenuList::iterator it =
488 find_if(menulist_.begin(), menulist_.end(),
489 lyx::compare_memfun(&Menu::name, name));
490 lyx::Assert(it != menulist_.end());