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"
27 #include "support/filetools.h"
28 #include "support/lyxfunctional.h"
30 extern LyXAction lyxaction;
31 extern LastFiles * lastfiles;
32 extern BufferList bufferlist;
38 // This is the global menu definition
39 MenuBackend menubackend;
42 MenuItem::MenuItem(Kind kind, string const & label,
43 string const & command, bool optional)
44 : kind_(kind), label_(label), optional_(optional)
57 action_ = lyxaction.LookupFunc(command);
59 if (action_ == LFUN_UNKNOWN_ACTION) {
60 lyxerr << "MenuItem(): LyX command `"
61 << command << "' does not exist." << endl;
64 lyxerr[Debug::GUI] << "Optional item "
74 Menu & Menu::add(MenuItem const & i)
81 Menu & Menu::read(LyXLex & lex)
99 struct keyword_item menutags[md_last - 1] = {
100 { "documents", md_documents },
101 { "end", md_endmenu },
102 { "exportformats", md_exportformats },
104 { "lastfiles", md_lastfiles },
105 { "optitem", md_optitem },
106 { "references", md_references },
107 { "separator", md_separator },
108 { "submenu", md_submenu },
110 { "updateformats", md_updateformats },
111 { "viewformats", md_viewformats }
114 lex.pushTable(menutags, md_last - 1);
115 if (lyxerr.debugging(Debug::PARSER))
116 lex.printTable(lyxerr);
119 bool optional = false;
121 while (lex.IsOK() && !quit) {
125 // fallback to md_item
128 string name = _(lex.GetString());
130 string const command = lex.GetString();
131 add(MenuItem(MenuItem::Command, name,
138 add(MenuItem(MenuItem::Separator));
142 add(MenuItem(MenuItem::Lastfiles));
146 add(MenuItem(MenuItem::Documents));
150 add(MenuItem(MenuItem::Toc));
154 add(MenuItem(MenuItem::References));
158 add(MenuItem(MenuItem::ViewFormats));
161 case md_updateformats:
162 add(MenuItem(MenuItem::UpdateFormats));
165 case md_exportformats:
166 add(MenuItem(MenuItem::ExportFormats));
171 string mlabel = _(lex.GetString());
173 string mname = lex.GetString();
174 add(MenuItem(MenuItem::Submenu, mlabel, mname));
183 lex.printError("menubar::read: "
184 "Unknown menu tag: `$$Token'");
193 void Menu::expand(Menu & tomenu, Buffer * buf) const
195 for (const_iterator cit = begin();
196 cit != end() ; ++cit) {
197 switch ((*cit).kind()) {
198 case MenuItem::Lastfiles: {
200 for (LastFiles::const_iterator lfit = lastfiles->begin();
201 lfit != lastfiles->end() && ii < 10;
203 string label = tostr(ii) + ". "
204 + MakeDisplayPath((*lfit), 30)
206 int action = lyxaction.
207 getPseudoAction(LFUN_FILE_OPEN,
209 tomenu.add(MenuItem(MenuItem::Command,
215 case MenuItem::Documents: {
216 vector<string> names = bufferlist.getFileNames();
219 tomenu.add(MenuItem(MenuItem::Command,
220 _("No Documents Open!"),
225 for (vector<string>::const_iterator docit = names.begin();
226 docit != names.end() ; ++docit) {
228 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
229 string label = MakeDisplayPath(*docit, 30);
230 tomenu.add(MenuItem(MenuItem::Command,
236 case MenuItem::ViewFormats:
237 case MenuItem::UpdateFormats:
238 case MenuItem::ExportFormats: {
239 vector<pair<string,string> > names;
241 if ((*cit).kind() == MenuItem::ViewFormats) {
242 names = Exporter::GetViewableFormats(buf);
243 action = LFUN_PREVIEW;
244 } else if ((*cit).kind() == MenuItem::UpdateFormats) {
245 names = Exporter::GetViewableFormats(buf);
246 action = LFUN_UPDATE;
248 names = Exporter::GetExportableFormats(buf);
249 action = LFUN_EXPORT;
252 for (vector<pair<string,string> >::const_iterator fit = names.begin();
253 fit != names.end() ; ++fit) {
255 lyxaction.getPseudoAction(action,
257 string label = (*fit).second;
258 tomenu.add(MenuItem(MenuItem::Command,
272 void MenuBackend::read(LyXLex & lex)
281 struct keyword_item menutags[md_last - 1] = {
282 { "end", md_endmenuset },
284 { "menubar", md_menubar }
288 if (compare_no_case(lex.GetString(), "menuset"))
289 lyxerr << "Menubackend::read: ERROR wrong token:`"
290 << lex.GetString() << '\'' << endl;
292 lex.pushTable(menutags, md_last - 1);
293 if (lyxerr.debugging(Debug::PARSER))
294 lex.printTable(lyxerr);
297 bool menubar = false;
299 while (lex.IsOK() && !quit) {
303 // fallback to md_menu
306 string name = lex.GetString();
308 if (getMenu(name).menubar() == menubar) {
309 getMenu(name).read(lex);
311 lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
315 Menu menu(name, menubar);
326 lex.printError("menubackend::read: "
327 "Unknown menu tag: `$$Token'");
335 void MenuBackend::defaults()
339 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
344 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
345 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
346 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
347 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
348 .add(MenuItem(MenuItem::Separator))
349 .add(MenuItem(MenuItem::Lastfiles));
352 Menu import("import");
354 .add(MenuItem(MenuItem::Command,
355 _("LaTeX...|L"), "buffer-import latex"))
356 .add(MenuItem(MenuItem::Command,
357 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
362 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
363 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
364 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
365 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
368 Menu documents("documents");
369 documents.add(MenuItem(MenuItem::Documents));
372 Menu main("main", true);
374 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
375 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
376 .add(MenuItem(MenuItem::Submenu,
377 _("Documents|D"), "documents"));
380 Menu main_nobuffer("main_nobuffer", true);
381 main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
384 if (lyxerr.debugging(Debug::GUI)) {
385 for(const_iterator cit = begin();
386 cit != end() ; ++cit)
387 lyxerr << "Menu name: " << cit->name()
388 << ", Menubar: " << cit->menubar()
394 void MenuBackend::add(Menu const & menu)
396 menulist_.push_back(menu);
400 bool MenuBackend::hasMenu(string const & name) const
402 return find_if(begin(), end(),
403 compare_memfun(&Menu::name, name)) != end();
407 Menu const & MenuBackend::getMenu(string const & name) const
409 const_iterator cit = find_if(begin(), end(),
410 compare_memfun(&Menu::name, name));
411 Assert(cit != end());
416 Menu & MenuBackend::getMenu(string const & name)
418 MenuList::iterator it = find_if(menulist_.begin(), menulist_.end(),
419 compare_memfun(&Menu::name, name));
420 Assert(it != menulist_.end());