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"
28 #include "support/filetools.h"
29 #include "support/lyxfunctional.h"
31 extern LyXAction lyxaction;
32 extern LastFiles * lastfiles;
33 extern BufferList bufferlist;
41 // This is the global menu definition
42 MenuBackend menubackend;
45 MenuItem::MenuItem(Kind kind, string const & label,
46 string const & command, bool optional)
47 : 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)
104 struct keyword_item menutags[md_last - 1] = {
105 { "documents", md_documents },
106 { "end", md_endmenu },
107 { "exportformats", md_exportformats },
108 { "importformats", md_importformats },
110 { "lastfiles", md_lastfiles },
111 { "optitem", md_optitem },
112 { "references", md_references },
113 { "separator", md_separator },
114 { "submenu", md_submenu },
116 { "updateformats", md_updateformats },
117 { "viewformats", md_viewformats }
120 lex.pushTable(menutags, md_last - 1);
121 if (lyxerr.debugging(Debug::PARSER))
122 lex.printTable(lyxerr);
125 bool optional = false;
127 while (lex.IsOK() && !quit) {
131 // fallback to md_item
134 string name = _(lex.GetString());
136 string const command = lex.GetString();
137 add(MenuItem(MenuItem::Command, name,
144 add(MenuItem(MenuItem::Separator));
148 add(MenuItem(MenuItem::Lastfiles));
152 add(MenuItem(MenuItem::Documents));
156 add(MenuItem(MenuItem::Toc));
160 add(MenuItem(MenuItem::References));
164 add(MenuItem(MenuItem::ViewFormats));
167 case md_updateformats:
168 add(MenuItem(MenuItem::UpdateFormats));
171 case md_exportformats:
172 add(MenuItem(MenuItem::ExportFormats));
175 case md_importformats:
176 add(MenuItem(MenuItem::ImportFormats));
181 string mlabel = _(lex.GetString());
183 string mname = lex.GetString();
184 add(MenuItem(MenuItem::Submenu, mlabel, mname));
193 lex.printError("menubar::read: "
194 "Unknown menu tag: `$$Token'");
202 struct compare_formatpair {
203 bool operator()(FormatPair const & a, FormatPair const & b) {
204 return a.format->prettyname < b.format->prettyname;
208 void Menu::checkShortcuts() const
210 // This is a quadratic algorithm, but we do not care because
211 // it is used for debugging only.
212 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
213 string shortcut = it1->shortcut();
214 if (shortcut.empty())
216 if (!contains(it1->label(), shortcut))
217 lyxerr << "Menu warning: menu entry \""
219 << "\" does not contain shortcut `"
220 << shortcut << '\'' << endl;
221 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
222 if (!compare_no_case(it2->shortcut(), shortcut)) {
223 lyxerr << "Menu warning: menu entries "
224 << '"' << it1->fulllabel()
225 << "\" and \"" << it2->fulllabel()
226 << "\" share the same shortcut."
233 void Menu::expand(Menu & tomenu, Buffer * buf) const
235 for (const_iterator cit = begin();
236 cit != end() ; ++cit) {
237 switch ((*cit).kind()) {
238 case MenuItem::Lastfiles: {
240 for (LastFiles::const_iterator lfit = lastfiles->begin();
241 lfit != lastfiles->end() && ii < 10;
243 string label = tostr(ii) + ". "
244 + MakeDisplayPath((*lfit), 30)
246 int action = lyxaction.
247 getPseudoAction(LFUN_FILE_OPEN,
249 tomenu.add(MenuItem(MenuItem::Command,
255 case MenuItem::Documents: {
256 vector<string> names = bufferlist.getFileNames();
259 tomenu.add(MenuItem(MenuItem::Command,
260 _("No Documents Open!"),
265 for (vector<string>::const_iterator docit = names.begin();
266 docit != names.end() ; ++docit) {
268 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
269 string label = MakeDisplayPath(*docit, 30);
270 tomenu.add(MenuItem(MenuItem::Command,
276 case MenuItem::ViewFormats:
277 case MenuItem::UpdateFormats:
278 case MenuItem::ExportFormats: {
279 vector<FormatPair> names;
281 if ((*cit).kind() == MenuItem::ViewFormats) {
282 names = Exporter::GetExportableFormats(buf, true);
283 action = LFUN_PREVIEW;
284 } else if ((*cit).kind() == MenuItem::UpdateFormats) {
285 names = Exporter::GetExportableFormats(buf, true);
286 action = LFUN_UPDATE;
288 names = Exporter::GetExportableFormats(buf, false);
289 action = LFUN_EXPORT;
291 sort(names.begin(), names.end(), compare_formatpair());
293 for (vector<FormatPair>::const_iterator fit = names.begin();
294 fit != names.end() ; ++fit) {
295 if ((*fit).format->dummy())
297 string fmt = (*fit).format->name;
298 string label = (*fit).format->prettyname;
300 fit != names.begin() &&
301 (*fit).format == (*(fit-1)).format;
303 fit+1 != names.end() &&
304 (*fit).format == (*(fit+1)).format;
306 (same_before || same_after)) {
307 fmt += ":" + (*fit).from->name;
309 split((*fit).command, head, ' ');
310 label += _(" (using ") + head + ")";
311 if (!(*fit).format->shortcut.empty() &&
313 label += "|" + (*fit).format->shortcut;
314 } else if (!(*fit).format->shortcut.empty())
315 label += "|" + (*fit).format->shortcut;
316 int action2 = lyxaction.getPseudoAction(action, fmt);
317 tomenu.add(MenuItem(MenuItem::Command,
323 case MenuItem::ImportFormats: {
324 vector<FormatPair> names = Converter::GetReachableTo("lyx");
325 sort(names.begin(), names.end(), compare_formatpair());
327 for (vector<FormatPair>::const_iterator fit = names.begin();
328 fit != names.end() ; ++fit) {
329 if ((*fit).format->dummy())
331 string fmt = (*fit).format->name;
332 string label = (*fit).format->prettyname;
333 if (!(*fit).format->shortcut.empty())
334 label += "|" + (*fit).format->shortcut;
335 int action2 = lyxaction.getPseudoAction(LFUN_IMPORT, fmt);
336 tomenu.add(MenuItem(MenuItem::Command,
347 // Check whether the shortcuts are unique
348 if (lyxerr.debugging(Debug::GUI))
353 void MenuBackend::read(LyXLex & lex)
362 struct keyword_item menutags[md_last - 1] = {
363 { "end", md_endmenuset },
365 { "menubar", md_menubar }
369 if (compare_no_case(lex.GetString(), "menuset"))
370 lyxerr << "Menubackend::read: ERROR wrong token:`"
371 << lex.GetString() << '\'' << endl;
373 lex.pushTable(menutags, md_last - 1);
374 if (lyxerr.debugging(Debug::PARSER))
375 lex.printTable(lyxerr);
378 bool menubar = false;
380 while (lex.IsOK() && !quit) {
384 // fallback to md_menu
387 string name = lex.GetString();
389 if (getMenu(name).menubar() == menubar) {
390 getMenu(name).read(lex);
392 lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
396 Menu menu(name, menubar);
407 lex.printError("menubackend::read: "
408 "Unknown menu tag: `$$Token'");
416 void MenuBackend::defaults()
420 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
425 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
426 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
427 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
428 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
429 .add(MenuItem(MenuItem::Separator))
430 .add(MenuItem(MenuItem::Lastfiles));
433 Menu import("import");
435 .add(MenuItem(MenuItem::Command,
436 _("LaTeX...|L"), "buffer-import latex"))
437 .add(MenuItem(MenuItem::Command,
438 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
443 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
444 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
445 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
446 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
449 Menu documents("documents");
450 documents.add(MenuItem(MenuItem::Documents));
453 Menu main("main", true);
455 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
456 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
457 .add(MenuItem(MenuItem::Submenu,
458 _("Documents|D"), "documents"));
461 Menu main_nobuffer("main_nobuffer", true);
462 main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
465 if (lyxerr.debugging(Debug::GUI)) {
466 for (const_iterator cit = begin();
467 cit != end() ; ++cit)
468 lyxerr << "Menu name: " << cit->name()
469 << ", Menubar: " << cit->menubar()
475 void MenuBackend::add(Menu const & menu)
477 menulist_.push_back(menu);
481 bool MenuBackend::hasMenu(string const & name) const
483 return find_if(begin(), end(),
484 compare_memfun(&Menu::name, name)) != end();
488 Menu const & MenuBackend::getMenu(string const & name) const
490 const_iterator cit = find_if(begin(), end(),
491 compare_memfun(&Menu::name, name));
492 Assert(cit != end());
497 Menu & MenuBackend::getMenu(string const & name)
499 MenuList::iterator it = find_if(menulist_.begin(), menulist_.end(),
500 compare_memfun(&Menu::name, name));
501 Assert(it != menulist_.end());