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."
222 class compare_format {
224 bool operator()(Format const * p1, Format const * p2) {
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(), compare_format());
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))
327 bool Menu::hasSubmenu(string const & name) const
329 return find_if(begin(), end(),
330 compare_memfun(&MenuItem::submenu, name)) != end();
334 void MenuBackend::read(LyXLex & lex)
343 struct keyword_item menutags[md_last - 1] = {
344 { "end", md_endmenuset },
346 { "menubar", md_menubar }
350 if (compare_no_case(lex.GetString(), "menuset"))
351 lyxerr << "Menubackend::read: ERROR wrong token:`"
352 << lex.GetString() << '\'' << endl;
354 lex.pushTable(menutags, md_last - 1);
355 if (lyxerr.debugging(Debug::PARSER))
356 lex.printTable(lyxerr);
359 bool menubar = false;
361 while (lex.IsOK() && !quit) {
365 // fallback to md_menu
368 string name = lex.GetString();
370 if (getMenu(name).menubar() == menubar) {
371 getMenu(name).read(lex);
373 lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
377 Menu menu(name, menubar);
388 lex.printError("menubackend::read: "
389 "Unknown menu tag: `$$Token'");
397 void MenuBackend::defaults()
401 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
406 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
407 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
408 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
409 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
410 .add(MenuItem(MenuItem::Separator))
411 .add(MenuItem(MenuItem::Lastfiles));
414 Menu import("import");
416 .add(MenuItem(MenuItem::Command,
417 _("LaTeX...|L"), "buffer-import latex"))
418 .add(MenuItem(MenuItem::Command,
419 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
424 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
425 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
426 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
427 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
430 Menu documents("documents");
431 documents.add(MenuItem(MenuItem::Documents));
434 Menu main("main", true);
436 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
437 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
438 .add(MenuItem(MenuItem::Submenu,
439 _("Documents|D"), "documents"));
442 Menu main_nobuffer("main_nobuffer", true);
443 main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
446 if (lyxerr.debugging(Debug::GUI)) {
447 for (const_iterator cit = begin();
448 cit != end() ; ++cit)
449 lyxerr << "Menu name: " << cit->name()
450 << ", Menubar: " << cit->menubar()
456 void MenuBackend::add(Menu const & menu)
458 menulist_.push_back(menu);
462 bool MenuBackend::hasMenu(string const & name) const
464 return find_if(begin(), end(),
465 compare_memfun(&Menu::name, name)) != end();
469 Menu const & MenuBackend::getMenu(string const & name) const
471 const_iterator cit = find_if(begin(), end(),
472 compare_memfun(&Menu::name, name));
473 Assert(cit != end());
478 Menu & MenuBackend::getMenu(string const & name)
480 MenuList::iterator it = find_if(menulist_.begin(), menulist_.end(),
481 compare_memfun(&Menu::name, name));
482 Assert(it != menulist_.end());