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)
60 action_ = lyxaction.LookupFunc(command);
62 if (action_ == LFUN_UNKNOWN_ACTION) {
63 lyxerr << "MenuItem(): LyX command `"
64 << command << "' does not exist." << endl;
67 lyxerr[Debug::GUI] << "Optional item "
77 Menu & Menu::add(MenuItem const & i)
84 Menu & Menu::read(LyXLex & lex)
102 struct keyword_item menutags[md_last - 1] = {
103 { "documents", md_documents },
104 { "end", md_endmenu },
105 { "exportformats", md_exportformats },
107 { "lastfiles", md_lastfiles },
108 { "optitem", md_optitem },
109 { "references", md_references },
110 { "separator", md_separator },
111 { "submenu", md_submenu },
113 { "updateformats", md_updateformats },
114 { "viewformats", md_viewformats }
117 lex.pushTable(menutags, md_last - 1);
118 if (lyxerr.debugging(Debug::PARSER))
119 lex.printTable(lyxerr);
122 bool optional = false;
124 while (lex.IsOK() && !quit) {
128 // fallback to md_item
131 string name = _(lex.GetString());
133 string const command = lex.GetString();
134 add(MenuItem(MenuItem::Command, name,
141 add(MenuItem(MenuItem::Separator));
145 add(MenuItem(MenuItem::Lastfiles));
149 add(MenuItem(MenuItem::Documents));
153 add(MenuItem(MenuItem::Toc));
157 add(MenuItem(MenuItem::References));
161 add(MenuItem(MenuItem::ViewFormats));
164 case md_updateformats:
165 add(MenuItem(MenuItem::UpdateFormats));
168 case md_exportformats:
169 add(MenuItem(MenuItem::ExportFormats));
174 string mlabel = _(lex.GetString());
176 string mname = lex.GetString();
177 add(MenuItem(MenuItem::Submenu, mlabel, mname));
186 lex.printError("menubar::read: "
187 "Unknown menu tag: `$$Token'");
195 struct compare_formatpair {
196 bool operator()(FormatPair const & a, FormatPair const & b) {
197 return a.format->prettyname < b.format->prettyname;
201 void Menu::checkShortcuts() const
203 // This is a quadratic algorithm, but we do not care because
204 // it is used for debugging only.
205 for (const_iterator it1 = begin(); it1 != end(); ++it1) {
206 string shortcut = it1->shortcut();
207 if (shortcut.empty())
209 if (!contains(it1->label(), shortcut))
210 lyxerr << "Menu warning: menu entry \""
212 << "\" does not contain shortcut `"
213 << shortcut << '\'' << endl;
214 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
215 if (!compare_no_case(it2->shortcut(), shortcut)) {
216 lyxerr << "Menu warning: menu entries "
217 << '"' << it1->fulllabel()
218 << "\" and \"" << it2->fulllabel()
219 << "\" share the same shortcut."
226 void Menu::expand(Menu & tomenu, Buffer * buf) const
228 for (const_iterator cit = begin();
229 cit != end() ; ++cit) {
230 switch ((*cit).kind()) {
231 case MenuItem::Lastfiles: {
233 for (LastFiles::const_iterator lfit = lastfiles->begin();
234 lfit != lastfiles->end() && ii < 10;
236 string label = tostr(ii) + ". "
237 + MakeDisplayPath((*lfit), 30)
239 int action = lyxaction.
240 getPseudoAction(LFUN_FILE_OPEN,
242 tomenu.add(MenuItem(MenuItem::Command,
248 case MenuItem::Documents: {
249 vector<string> names = bufferlist.getFileNames();
252 tomenu.add(MenuItem(MenuItem::Command,
253 _("No Documents Open!"),
258 for (vector<string>::const_iterator docit = names.begin();
259 docit != names.end() ; ++docit) {
261 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
262 string label = MakeDisplayPath(*docit, 30);
263 tomenu.add(MenuItem(MenuItem::Command,
269 case MenuItem::ViewFormats:
270 case MenuItem::UpdateFormats:
271 case MenuItem::ExportFormats: {
272 vector<FormatPair> names;
274 if ((*cit).kind() == MenuItem::ViewFormats) {
275 names = Exporter::GetViewableFormats(buf);
276 action = LFUN_PREVIEW;
277 } else if ((*cit).kind() == MenuItem::UpdateFormats) {
278 names = Exporter::GetViewableFormats(buf);
279 action = LFUN_UPDATE;
281 names = Exporter::GetExportableFormats(buf);
282 action = LFUN_EXPORT;
284 sort(names.begin(), names.end(), compare_formatpair());
286 for (vector<FormatPair>::const_iterator fit = names.begin();
287 fit != names.end() ; ++fit) {
288 string fmt = (*fit).format->name;
289 string label = (*fit).format->prettyname;
291 fit != names.begin() &&
292 (*fit).format == (*(fit-1)).format;
294 fit+1 != names.end() &&
295 (*fit).format == (*(fit+1)).format;
297 (same_before || same_after)) {
298 fmt += ":" + (*fit).from->name;
300 split((*fit).command, head, ' ');
301 label += _(" (using ") + head + ")";
302 if (!(*fit).format->shortcut.empty() &&
304 label += "|" + (*fit).format->shortcut;
305 } else if (!(*fit).format->shortcut.empty())
306 label += "|" + (*fit).format->shortcut;
307 int action2 = lyxaction.getPseudoAction(action, fmt);
308 tomenu.add(MenuItem(MenuItem::Command,
320 // Check whether the shortcuts are unique
321 if (lyxerr.debugging(Debug::GUI))
326 void MenuBackend::read(LyXLex & lex)
335 struct keyword_item menutags[md_last - 1] = {
336 { "end", md_endmenuset },
338 { "menubar", md_menubar }
342 if (compare_no_case(lex.GetString(), "menuset"))
343 lyxerr << "Menubackend::read: ERROR wrong token:`"
344 << lex.GetString() << '\'' << endl;
346 lex.pushTable(menutags, md_last - 1);
347 if (lyxerr.debugging(Debug::PARSER))
348 lex.printTable(lyxerr);
351 bool menubar = false;
353 while (lex.IsOK() && !quit) {
357 // fallback to md_menu
360 string name = lex.GetString();
362 if (getMenu(name).menubar() == menubar) {
363 getMenu(name).read(lex);
365 lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
369 Menu menu(name, menubar);
380 lex.printError("menubackend::read: "
381 "Unknown menu tag: `$$Token'");
389 void MenuBackend::defaults()
393 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
398 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
399 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
400 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
401 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
402 .add(MenuItem(MenuItem::Separator))
403 .add(MenuItem(MenuItem::Lastfiles));
406 Menu import("import");
408 .add(MenuItem(MenuItem::Command,
409 _("LaTeX...|L"), "buffer-import latex"))
410 .add(MenuItem(MenuItem::Command,
411 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
416 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
417 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
418 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
419 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
422 Menu documents("documents");
423 documents.add(MenuItem(MenuItem::Documents));
426 Menu main("main", true);
428 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
429 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
430 .add(MenuItem(MenuItem::Submenu,
431 _("Documents|D"), "documents"));
434 Menu main_nobuffer("main_nobuffer", true);
435 main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
438 if (lyxerr.debugging(Debug::GUI)) {
439 for(const_iterator cit = begin();
440 cit != end() ; ++cit)
441 lyxerr << "Menu name: " << cit->name()
442 << ", Menubar: " << cit->menubar()
448 void MenuBackend::add(Menu const & menu)
450 menulist_.push_back(menu);
454 bool MenuBackend::hasMenu(string const & name) const
456 return find_if(begin(), end(),
457 compare_memfun(&Menu::name, name)) != end();
461 Menu const & MenuBackend::getMenu(string const & name) const
463 const_iterator cit = find_if(begin(), end(),
464 compare_memfun(&Menu::name, name));
465 Assert(cit != end());
470 Menu & MenuBackend::getMenu(string const & name)
472 MenuList::iterator it = find_if(menulist_.begin(), menulist_.end(),
473 compare_memfun(&Menu::name, name));
474 Assert(it != menulist_.end());