]> git.lyx.org Git - lyx.git/blob - src/MenuBackend.C
Forgot to add this files.
[lyx.git] / src / MenuBackend.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2000 The LyX Team.
8  *
9  *
10  * ====================================================== */
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include <config.h>
17 #include <memory>
18 #include "support/LAssert.h"
19 #include "MenuBackend.h"
20 #include "lyxlex.h"
21 #include "LyXAction.h"
22 #include "debug.h"
23 #include "gettext.h"
24
25 extern LyXAction lyxaction;
26
27 using std::endl;
28
29 // This is the global menu definition
30 MenuBackend menubackend;
31
32
33 MenuItem::MenuItem(Kind kind, string const & label, 
34                    string const & command, bool optional) 
35         : kind_(kind), label_(label), optional_(optional)
36 {
37         switch(kind) {
38         case Separator:
39         case Documents:
40         case Lastfiles:
41                 break;
42         case Command:
43                 action_ = lyxaction.LookupFunc(command);
44
45                 if (action_ == LFUN_UNKNOWN_ACTION) {
46                         lyxerr << "MenuItem(): LyX command `"
47                                << command << "' does not exist." << endl; 
48                 }
49                 if (optional_)
50                         lyxerr[Debug::GUI] << "Optional item " 
51                                            << command << endl; 
52                 break;
53         case Submenu:
54                 submenu_ = command;
55                 break;
56         }
57 }
58
59
60 Menu & Menu::add(MenuItem const & i)
61 {
62         items_.push_back(i);
63         return *this;
64 }
65
66
67 Menu & Menu::read(LyXLex & lex)
68 {
69         enum Menutags {
70                 md_item = 1,
71                 md_documents,
72                 md_endmenu,
73                 md_lastfiles,
74                 md_optitem,
75                 md_submenu,
76                 md_separator,
77                 md_last
78         };
79
80         struct keyword_item menutags[md_last-1] = {
81                 { "documents", md_documents },
82                 { "end", md_endmenu },
83                 { "item", md_item },
84                 { "lastfiles", md_lastfiles },
85                 { "optitem", md_optitem }, 
86                 { "separator", md_separator },
87                 { "submenu", md_submenu }
88         };
89
90         lex.pushTable(menutags, md_last - 1);
91         if (lyxerr.debugging(Debug::PARSER))
92                 lex.printTable(lyxerr);
93
94         bool quit = false;
95         bool optional = false;
96
97         while (lex.IsOK() && !quit) {
98                 switch(lex.lex()) {
99                 case md_optitem:
100                         optional = true;
101                         // fallback to md_item
102                 case md_item: {
103                         lex.next();
104                         char * tmp = strdup(lex.GetString().c_str());
105                         string name = _(tmp);
106                         free(tmp);
107                         lex.next();
108                         string command = lex.GetString();
109                         add(MenuItem(MenuItem::Command, name, 
110                                      command, optional));
111                         optional = false;
112                         break;
113                 }
114                 case md_separator:
115                         add(MenuItem(MenuItem::Separator));
116                         break;
117                 case md_lastfiles:
118                         add(MenuItem(MenuItem::Lastfiles));
119                         break;
120                 case md_documents:
121                         add(MenuItem(MenuItem::Documents));
122                         break;
123                 case md_submenu: {
124                         lex.next();
125                         char * tmp = strdup(lex.GetString().c_str());
126                         string mlabel = _(tmp);
127                         free(tmp);
128                         lex.next();
129                         string mname = lex.GetString();
130                         add(MenuItem(MenuItem::Submenu, mlabel, mname));
131                         break;
132                 }
133                 case md_endmenu:
134                         quit = true;
135                         break;
136                 default:
137                         lex.printError("menubar::read: "
138                                        "Unknown menu tag: `$$Token'");
139                         break;
140                 }
141         }
142         lex.popTable();
143         return *this;
144 }
145
146
147 void MenuBackend::read(LyXLex & lex)
148 {
149         enum Menutags {
150                 md_menu = 1,
151                 md_menubar,
152                 md_endmenuset,
153                 md_last
154         };
155
156         struct keyword_item menutags[md_last - 1] = {
157                 { "end", md_endmenuset },
158                 { "menu", md_menu },
159                 { "menubar", md_menubar }
160         };
161
162         //consistency check
163         if (compare_no_case(lex.GetString(), "menuset"))
164                 lyxerr << "Menubackend::read: ERROR wrong token:`"
165                        << lex.GetString() << '\'' << endl;
166
167         lex.pushTable(menutags, md_last - 1);
168         if (lyxerr.debugging(Debug::PARSER))
169                 lex.printTable(lyxerr);
170
171         bool quit = false;
172         bool menubar = false;
173
174         while (lex.IsOK() && !quit) {
175                 switch(lex.lex()) {
176                 case md_menubar: 
177                         menubar = true;
178                         // fallback to md_menu
179                 case md_menu: {
180                         lex.next();
181                         string name = lex.GetString();
182                         if (hasMenu(name)) {
183                                 if (getMenu(name).menubar() == menubar) {
184                                         getMenu(name).read(lex);
185                                 } else {
186                                         lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
187                                         return;
188                                 }
189                         } else {                                
190                                 Menu menu(name, menubar);
191                                 menu.read(lex);
192                                 add(menu);
193                         }
194                         menubar = false;
195                         break;
196                 }
197                 case md_endmenuset:
198                         quit = true;
199                         break;
200                 default:
201                         lex.printError("menubackend::read: "
202                                        "Unknown menu tag: `$$Token'");
203                         break;
204                 }
205         }
206         lex.popTable();
207 }
208
209
210 void MenuBackend::defaults()
211 {
212         menulist_.clear();
213
214         lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values" 
215                            << endl;
216
217         Menu file("file");
218         file
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));
225         add(file);
226
227         Menu import("import");
228         import
229                 .add(MenuItem(MenuItem::Command,
230                               _("LaTeX...|L"), "buffer-import latex"))
231                 .add(MenuItem(MenuItem::Command,
232                               _("LinuxDoc...|L"), "buffer-import linuxdoc"));
233         add(import);
234  
235         Menu edit("edit");
236         edit
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"));
241         add(edit);
242
243         Menu documents("documents");
244         documents.add(MenuItem(MenuItem::Documents));
245         add(documents);
246
247         Menu main("main", true);
248         main
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"));
253         add(main);
254
255         Menu main_nobuffer("main_nobuffer", true);
256         main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
257         add(main_nobuffer);
258
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() 
264                                << endl;
265         }
266 }
267
268
269 void MenuBackend::add(Menu const & menu)
270 {
271         menulist_.push_back(menu);
272 }
273
274
275 bool MenuBackend::hasMenu(string const & name) const
276 {
277         const_iterator mend = end();
278         for (const_iterator cit = begin(); cit != mend; ++cit) {
279                 if ((*cit).name() == name)
280                         return true;
281         }
282         return false;
283 }
284
285
286 Menu const & MenuBackend::getMenu(string const & name) const
287 {
288         const_iterator mend = end();
289         for (const_iterator cit = begin(); cit != mend; ++cit) {
290                 if ((*cit).name() == name)
291                         return (*cit);
292         }
293         Assert(false); // we actually require the name to exist.
294         return menulist_.front();
295 }
296
297
298 Menu & MenuBackend::getMenu(string const & name)
299 {
300         MenuList::iterator end = menulist_.end();
301         for (MenuList::iterator cit = menulist_.begin(); 
302              cit != end; ++cit) {
303                 if ((*cit).name() == name)
304                         return (*cit);
305         }
306         Assert(false); // we actually require the name to exist.
307         return menulist_.front();
308 }
309