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::expand(Menu & tomenu, Buffer * buf) const
203 for (const_iterator cit = begin();
204 cit != end() ; ++cit) {
205 switch ((*cit).kind()) {
206 case MenuItem::Lastfiles: {
208 for (LastFiles::const_iterator lfit = lastfiles->begin();
209 lfit != lastfiles->end() && ii < 10;
211 string label = tostr(ii) + ". "
212 + MakeDisplayPath((*lfit), 30)
214 int action = lyxaction.
215 getPseudoAction(LFUN_FILE_OPEN,
217 tomenu.add(MenuItem(MenuItem::Command,
223 case MenuItem::Documents: {
224 vector<string> names = bufferlist.getFileNames();
227 tomenu.add(MenuItem(MenuItem::Command,
228 _("No Documents Open!"),
233 for (vector<string>::const_iterator docit = names.begin();
234 docit != names.end() ; ++docit) {
236 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
237 string label = MakeDisplayPath(*docit, 30);
238 tomenu.add(MenuItem(MenuItem::Command,
244 case MenuItem::ViewFormats:
245 case MenuItem::UpdateFormats:
246 case MenuItem::ExportFormats: {
247 vector<FormatPair> names;
249 if ((*cit).kind() == MenuItem::ViewFormats) {
250 names = Exporter::GetViewableFormats(buf);
251 action = LFUN_PREVIEW;
252 } else if ((*cit).kind() == MenuItem::UpdateFormats) {
253 names = Exporter::GetViewableFormats(buf);
254 action = LFUN_UPDATE;
256 names = Exporter::GetExportableFormats(buf);
257 action = LFUN_EXPORT;
259 sort(names.begin(), names.end(), compare_formatpair());
261 for (vector<FormatPair>::const_iterator fit = names.begin();
262 fit != names.end() ; ++fit) {
263 string fmt = (*fit).format->name;
264 string label = (*fit).format->prettyname;
266 ( (fit != names.begin() &&
267 (*fit).format == (*(fit-1)).format) ||
268 (fit+1 != names.end() &&
269 (*fit).format == (*(fit+1)).format) )) {
270 fmt += ":" + (*fit).from->name;
272 split((*fit).command, head, ' ');
273 label += _(" (using ") + head + ")";
275 int action2 = lyxaction.getPseudoAction(action, fmt);
276 tomenu.add(MenuItem(MenuItem::Command,
290 void MenuBackend::read(LyXLex & lex)
299 struct keyword_item menutags[md_last - 1] = {
300 { "end", md_endmenuset },
302 { "menubar", md_menubar }
306 if (compare_no_case(lex.GetString(), "menuset"))
307 lyxerr << "Menubackend::read: ERROR wrong token:`"
308 << lex.GetString() << '\'' << endl;
310 lex.pushTable(menutags, md_last - 1);
311 if (lyxerr.debugging(Debug::PARSER))
312 lex.printTable(lyxerr);
315 bool menubar = false;
317 while (lex.IsOK() && !quit) {
321 // fallback to md_menu
324 string name = lex.GetString();
326 if (getMenu(name).menubar() == menubar) {
327 getMenu(name).read(lex);
329 lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
333 Menu menu(name, menubar);
344 lex.printError("menubackend::read: "
345 "Unknown menu tag: `$$Token'");
353 void MenuBackend::defaults()
357 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
362 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
363 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
364 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
365 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
366 .add(MenuItem(MenuItem::Separator))
367 .add(MenuItem(MenuItem::Lastfiles));
370 Menu import("import");
372 .add(MenuItem(MenuItem::Command,
373 _("LaTeX...|L"), "buffer-import latex"))
374 .add(MenuItem(MenuItem::Command,
375 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
380 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
381 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
382 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
383 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
386 Menu documents("documents");
387 documents.add(MenuItem(MenuItem::Documents));
390 Menu main("main", true);
392 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
393 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
394 .add(MenuItem(MenuItem::Submenu,
395 _("Documents|D"), "documents"));
398 Menu main_nobuffer("main_nobuffer", true);
399 main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
402 if (lyxerr.debugging(Debug::GUI)) {
403 for(const_iterator cit = begin();
404 cit != end() ; ++cit)
405 lyxerr << "Menu name: " << cit->name()
406 << ", Menubar: " << cit->menubar()
412 void MenuBackend::add(Menu const & menu)
414 menulist_.push_back(menu);
418 bool MenuBackend::hasMenu(string const & name) const
420 return find_if(begin(), end(),
421 compare_memfun(&Menu::name, name)) != end();
425 Menu const & MenuBackend::getMenu(string const & name) const
427 const_iterator cit = find_if(begin(), end(),
428 compare_memfun(&Menu::name, name));
429 Assert(cit != end());
434 Menu & MenuBackend::getMenu(string const & name)
436 MenuList::iterator it = find_if(menulist_.begin(), menulist_.end(),
437 compare_memfun(&Menu::name, name));
438 Assert(it != menulist_.end());