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;
39 // This is the global menu definition
40 MenuBackend menubackend;
43 MenuItem::MenuItem(Kind kind, string const & label,
44 string const & command, bool optional)
45 : kind_(kind), label_(label), optional_(optional)
58 action_ = lyxaction.LookupFunc(command);
60 if (action_ == LFUN_UNKNOWN_ACTION) {
61 lyxerr << "MenuItem(): LyX command `"
62 << command << "' does not exist." << endl;
65 lyxerr[Debug::GUI] << "Optional item "
75 Menu & Menu::add(MenuItem const & i)
82 Menu & Menu::read(LyXLex & lex)
100 struct keyword_item menutags[md_last - 1] = {
101 { "documents", md_documents },
102 { "end", md_endmenu },
103 { "exportformats", md_exportformats },
105 { "lastfiles", md_lastfiles },
106 { "optitem", md_optitem },
107 { "references", md_references },
108 { "separator", md_separator },
109 { "submenu", md_submenu },
111 { "updateformats", md_updateformats },
112 { "viewformats", md_viewformats }
115 lex.pushTable(menutags, md_last - 1);
116 if (lyxerr.debugging(Debug::PARSER))
117 lex.printTable(lyxerr);
120 bool optional = false;
122 while (lex.IsOK() && !quit) {
126 // fallback to md_item
129 string name = _(lex.GetString());
131 string const command = lex.GetString();
132 add(MenuItem(MenuItem::Command, name,
139 add(MenuItem(MenuItem::Separator));
143 add(MenuItem(MenuItem::Lastfiles));
147 add(MenuItem(MenuItem::Documents));
151 add(MenuItem(MenuItem::Toc));
155 add(MenuItem(MenuItem::References));
159 add(MenuItem(MenuItem::ViewFormats));
162 case md_updateformats:
163 add(MenuItem(MenuItem::UpdateFormats));
166 case md_exportformats:
167 add(MenuItem(MenuItem::ExportFormats));
172 string mlabel = _(lex.GetString());
174 string mname = lex.GetString();
175 add(MenuItem(MenuItem::Submenu, mlabel, mname));
184 lex.printError("menubar::read: "
185 "Unknown menu tag: `$$Token'");
194 void Menu::expand(Menu & tomenu, Buffer * buf) const
196 for (const_iterator cit = begin();
197 cit != end() ; ++cit) {
198 switch ((*cit).kind()) {
199 case MenuItem::Lastfiles: {
201 for (LastFiles::const_iterator lfit = lastfiles->begin();
202 lfit != lastfiles->end() && ii < 10;
204 string label = tostr(ii) + ". "
205 + MakeDisplayPath((*lfit), 30)
207 int action = lyxaction.
208 getPseudoAction(LFUN_FILE_OPEN,
210 tomenu.add(MenuItem(MenuItem::Command,
216 case MenuItem::Documents: {
217 vector<string> names = bufferlist.getFileNames();
220 tomenu.add(MenuItem(MenuItem::Command,
221 _("No Documents Open!"),
226 for (vector<string>::const_iterator docit = names.begin();
227 docit != names.end() ; ++docit) {
229 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
230 string label = MakeDisplayPath(*docit, 30);
231 tomenu.add(MenuItem(MenuItem::Command,
237 case MenuItem::ViewFormats:
238 case MenuItem::UpdateFormats:
239 case MenuItem::ExportFormats: {
240 vector<pair<string,string> > names;
242 if ((*cit).kind() == MenuItem::ViewFormats) {
243 names = Exporter::GetViewableFormats(buf);
244 action = LFUN_PREVIEW;
245 } else if ((*cit).kind() == MenuItem::UpdateFormats) {
246 names = Exporter::GetViewableFormats(buf);
247 action = LFUN_UPDATE;
249 names = Exporter::GetExportableFormats(buf);
250 action = LFUN_EXPORT;
253 for (vector<pair<string,string> >::const_iterator fit = names.begin();
254 fit != names.end() ; ++fit) {
256 lyxaction.getPseudoAction(action,
258 string label = (*fit).second;
259 tomenu.add(MenuItem(MenuItem::Command,
273 void MenuBackend::read(LyXLex & lex)
282 struct keyword_item menutags[md_last - 1] = {
283 { "end", md_endmenuset },
285 { "menubar", md_menubar }
289 if (compare_no_case(lex.GetString(), "menuset"))
290 lyxerr << "Menubackend::read: ERROR wrong token:`"
291 << lex.GetString() << '\'' << endl;
293 lex.pushTable(menutags, md_last - 1);
294 if (lyxerr.debugging(Debug::PARSER))
295 lex.printTable(lyxerr);
298 bool menubar = false;
300 while (lex.IsOK() && !quit) {
304 // fallback to md_menu
307 string name = lex.GetString();
309 if (getMenu(name).menubar() == menubar) {
310 getMenu(name).read(lex);
312 lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
316 Menu menu(name, menubar);
327 lex.printError("menubackend::read: "
328 "Unknown menu tag: `$$Token'");
336 void MenuBackend::defaults()
340 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
345 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
346 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
347 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
348 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
349 .add(MenuItem(MenuItem::Separator))
350 .add(MenuItem(MenuItem::Lastfiles));
353 Menu import("import");
355 .add(MenuItem(MenuItem::Command,
356 _("LaTeX...|L"), "buffer-import latex"))
357 .add(MenuItem(MenuItem::Command,
358 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
363 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
364 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
365 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
366 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
369 Menu documents("documents");
370 documents.add(MenuItem(MenuItem::Documents));
373 Menu main("main", true);
375 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
376 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
377 .add(MenuItem(MenuItem::Submenu,
378 _("Documents|D"), "documents"));
381 Menu main_nobuffer("main_nobuffer", true);
382 main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
385 if (lyxerr.debugging(Debug::GUI)) {
386 for(const_iterator cit = begin();
387 cit != end() ; ++cit)
388 lyxerr << "Menu name: " << cit->name()
389 << ", Menubar: " << cit->menubar()
395 void MenuBackend::add(Menu const & menu)
397 menulist_.push_back(menu);
401 bool MenuBackend::hasMenu(string const & name) const
403 return find_if(begin(), end(),
404 compare_memfun(&Menu::name, name)) != end();
408 Menu const & MenuBackend::getMenu(string const & name) const
410 const_iterator cit = find_if(begin(), end(),
411 compare_memfun(&Menu::name, name));
412 Assert(cit != end());
417 Menu & MenuBackend::getMenu(string const & name)
419 MenuList::iterator it = find_if(menulist_.begin(), menulist_.end(),
420 compare_memfun(&Menu::name, name));
421 Assert(it != menulist_.end());