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"
25 extern LyXAction lyxaction;
29 // This is the global menu definition
30 MenuBackend menubackend;
33 MenuItem::MenuItem(Kind kind, string const & label,
34 string const & command, bool optional)
35 : kind_(kind), label_(label), optional_(optional)
43 action_ = lyxaction.LookupFunc(command);
45 if (action_ == LFUN_UNKNOWN_ACTION) {
46 lyxerr << "MenuItem(): LyX command `"
47 << command << "' does not exist." << endl;
50 lyxerr[Debug::GUI] << "Optional item "
60 Menu & Menu::add(MenuItem const & i)
67 Menu & Menu::read(LyXLex & lex)
80 struct keyword_item menutags[md_last-1] = {
81 { "documents", md_documents },
82 { "end", md_endmenu },
84 { "lastfiles", md_lastfiles },
85 { "optitem", md_optitem },
86 { "separator", md_separator },
87 { "submenu", md_submenu }
90 lex.pushTable(menutags, md_last - 1);
91 if (lyxerr.debugging(Debug::PARSER))
92 lex.printTable(lyxerr);
95 bool optional = false;
97 while (lex.IsOK() && !quit) {
101 // fallback to md_item
104 char * tmp = strdup(lex.GetString().c_str());
105 string name = _(tmp);
108 string command = lex.GetString();
109 add(MenuItem(MenuItem::Command, name,
115 add(MenuItem(MenuItem::Separator));
118 add(MenuItem(MenuItem::Lastfiles));
121 add(MenuItem(MenuItem::Documents));
125 char * tmp = strdup(lex.GetString().c_str());
126 string mlabel = _(tmp);
129 string mname = lex.GetString();
130 add(MenuItem(MenuItem::Submenu, mlabel, mname));
137 lex.printError("menubar::read: "
138 "Unknown menu tag: `$$Token'");
147 void MenuBackend::read(LyXLex & lex)
156 struct keyword_item menutags[md_last - 1] = {
157 { "end", md_endmenuset },
159 { "menubar", md_menubar }
163 if (compare_no_case(lex.GetString(), "menuset"))
164 lyxerr << "Menubackend::read: ERROR wrong token:`"
165 << lex.GetString() << '\'' << endl;
167 lex.pushTable(menutags, md_last - 1);
168 if (lyxerr.debugging(Debug::PARSER))
169 lex.printTable(lyxerr);
172 bool menubar = false;
174 while (lex.IsOK() && !quit) {
178 // fallback to md_menu
181 string name = lex.GetString();
183 if (getMenu(name).menubar() == menubar) {
184 getMenu(name).read(lex);
186 lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
190 Menu menu(name, menubar);
201 lex.printError("menubackend::read: "
202 "Unknown menu tag: `$$Token'");
210 void MenuBackend::defaults()
214 lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
219 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
220 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
221 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
222 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
223 .add(MenuItem(MenuItem::Separator))
224 .add(MenuItem(MenuItem::Lastfiles));
227 Menu import("import");
229 .add(MenuItem(MenuItem::Command,
230 _("LaTeX...|L"), "buffer-import latex"))
231 .add(MenuItem(MenuItem::Command,
232 _("LinuxDoc...|L"), "buffer-import linuxdoc"));
237 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
238 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
239 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
240 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
243 Menu documents("documents");
244 documents.add(MenuItem(MenuItem::Documents));
247 Menu main("main", true);
249 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
250 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
251 .add(MenuItem(MenuItem::Submenu,
252 _("Documents|D"), "documents"));
255 Menu main_nobuffer("main_nobuffer", true);
256 main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
259 if (lyxerr.debugging(Debug::GUI)) {
260 for(const_iterator cit = begin();
261 cit != end() ; ++cit)
262 lyxerr << "Menu name: " << cit->name()
263 << ", Menubar: " << cit->menubar()
269 void MenuBackend::add(Menu const & menu)
271 menulist_.push_back(menu);
275 bool MenuBackend::hasMenu(string const & name) const
277 const_iterator mend = end();
278 for (const_iterator cit = begin(); cit != mend; ++cit) {
279 if ((*cit).name() == name)
286 Menu const & MenuBackend::getMenu(string const & name) const
288 const_iterator mend = end();
289 for (const_iterator cit = begin(); cit != mend; ++cit) {
290 if ((*cit).name() == name)
293 Assert(false); // we actually require the name to exist.
294 return menulist_.front();
298 Menu & MenuBackend::getMenu(string const & name)
300 MenuList::iterator end = menulist_.end();
301 for (MenuList::iterator cit = menulist_.begin();
303 if ((*cit).name() == name)
306 Assert(false); // we actually require the name to exist.
307 return menulist_.front();