]> git.lyx.org Git - lyx.git/blob - src/MenuBackend.C
Fix small bug in reading \set_color in lyxrc
[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 "support/LAssert.h"
18 #include "MenuBackend.h"
19 #include "lyxlex.h"
20 #include "LyXAction.h"
21 #include "debug.h"
22
23 extern LyXAction lyxaction;
24
25 using std::endl;
26
27 // This is the global menu definition
28 MenuBackend menubackend;
29
30
31 MenuItem::MenuItem(Kind kind, string const & label, 
32                    string const & command, bool optional) 
33         : kind_(kind), label_(label), optional_(optional)
34 {
35         switch(kind) {
36         case Separator:
37         case Documents:
38         case Lastfiles:
39                 break;
40         case Command:
41                 action_ = lyxaction.LookupFunc(command);
42
43                 if (action_ == LFUN_UNKNOWN_ACTION) {
44                         lyxerr << "MenuItem(): LyX command `"
45                                << command << "' does not exist." << endl; 
46                 }
47                 if (optional_)
48                         lyxerr[Debug::GUI] << "Optional item " 
49                                            << command << endl; 
50                 break;
51         case Submenu:
52                 submenu_ = command;
53                 break;
54         }
55 }
56
57
58 Menu & Menu::add(MenuItem const & i)
59 {
60         items_.push_back(i);
61         return *this;
62 }
63
64
65 void Menu::read(LyXLex & lex)
66 {
67         enum Menutags {
68                 md_item = 1,
69                 md_documents,
70                 md_endmenu,
71                 md_lastfiles,
72                 md_optitem,
73                 md_submenu,
74                 md_separator,
75                 md_last
76         };
77
78         struct keyword_item menutags[md_last-1] = {
79                 { "documents", md_documents },
80                 { "end", md_endmenu },
81                 { "item", md_item },
82                 { "lastfiles", md_lastfiles },
83                 { "optitem", md_optitem }, 
84                 { "separator", md_separator },
85                 { "submenu", md_submenu }
86         };
87
88         lex.pushTable(menutags, md_last - 1);
89         if (lyxerr.debugging(Debug::PARSER))
90                 lex.printTable(lyxerr);
91
92         bool quit = false;
93         bool optional = false;
94
95         while (lex.IsOK() && !quit) {
96                 switch(lex.lex()) {
97                 case md_optitem:
98                         optional = true;
99                         // fallback to md_item
100                 case md_item: {
101                         lex.next();
102                         string name = lex.GetString();
103                         lex.next();
104                         string command = lex.GetString();
105                         add(MenuItem(MenuItem::Command, name, 
106                                      command, optional));
107                         optional = false;
108                         break;
109                 }
110                 case md_separator:
111                         add(MenuItem(MenuItem::Separator));
112                         break;
113                 case md_lastfiles:
114                         add(MenuItem(MenuItem::Lastfiles));
115                         break;
116                 case md_documents:
117                         add(MenuItem(MenuItem::Documents));
118                         break;
119                 case md_submenu: {
120                         lex.next();
121                         string mlabel = lex.GetString();
122                         lex.next();
123                         string mname = lex.GetString();
124                         add(MenuItem(MenuItem::Submenu, mlabel, mname));
125                         break;
126                 }
127                 case md_endmenu:
128                         quit = true;
129                         break;
130                 default:
131                         lex.printError("menubar::read: "
132                                        "Unknown menu tag: `$$Token'");
133                         break;
134                 }
135         }
136         lex.popTable();
137 }
138
139
140 void MenuBackend::read(LyXLex & lex)
141 {
142         enum Menutags {
143                 md_menu = 1,
144                 md_menubar,
145                 md_endmenuset,
146                 md_last
147         };
148
149         struct keyword_item menutags[md_last - 1] = {
150                 { "end", md_endmenuset },
151                 { "menu", md_menu },
152                 { "menubar", md_menubar }
153         };
154
155         //consistency check
156         if (compare_no_case(lex.GetString(), "menuset"))
157                 lyxerr << "Menubackend::read: ERROR wrong token:`"
158                        << lex.GetString() << '\'' << endl;
159
160         lex.pushTable(menutags, md_last - 1);
161         if (lyxerr.debugging(Debug::PARSER))
162                 lex.printTable(lyxerr);
163
164         bool quit = false;
165
166         while (lex.IsOK() && !quit) {
167                 switch(lex.lex()) {
168                 case md_menu: {
169                         lex.next();
170                         string name = lex.GetString();
171                         Menu menu(name, false);
172                         menu.read(lex);
173                         add(menu);
174                         break;
175                 }
176                 case md_menubar: {
177                         lex.next();
178                         string name = lex.GetString();
179                         Menu menubar(name, true);
180                         menubar.read(lex);
181                         add(menubar);
182                         break;
183                 }
184                 case md_endmenuset:
185                         quit = true;
186                         break;
187                 default:
188                         lex.printError("menubackend::read: "
189                                        "Unknown menu tag: `$$Token'");
190                         break;
191                 }
192         }
193         lex.popTable();
194 }
195
196
197 void MenuBackend::defaults()
198 {
199         menulist_.clear();
200
201         lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values" 
202                            << endl;
203
204         Menu file("file");
205         file
206                 .add(MenuItem(MenuItem::Command, "New...|N", "buffer-new"))
207                 .add(MenuItem(MenuItem::Command, "Open...|O", "buffer-open"))
208                 .add(MenuItem(MenuItem::Submenu, "Import|I", "import"))
209                 .add(MenuItem(MenuItem::Command, "Quit|Q", "lyx-quit"))
210                 .add(MenuItem(MenuItem::Separator))
211                 .add(MenuItem(MenuItem::Lastfiles));
212         add(file);
213
214         Menu import("import");
215         import
216                 .add(MenuItem(MenuItem::Command,
217                               "LaTeX...|L", "buffer-import latex"))
218                 .add(MenuItem(MenuItem::Command,
219                               "LinuxDoc...|L", "buffer-import linuxdoc"));
220         add(import);
221  
222         Menu edit("edit");
223         edit
224                 .add(MenuItem(MenuItem::Command, "Cut", "cut"))
225                 .add(MenuItem(MenuItem::Command, "Copy", "copy"))
226                 .add(MenuItem(MenuItem::Command, "Paste", "paste"))
227                 .add(MenuItem(MenuItem::Command, "Emphasize", "font-emph"));
228         add(edit);
229
230         Menu documents("documents");
231         documents.add(MenuItem(MenuItem::Documents));
232         add(documents);
233
234         Menu main("main", true);
235         main
236                 .add(MenuItem(MenuItem::Submenu, "File|F", "file"))
237                 .add(MenuItem(MenuItem::Submenu, "Edit|E", "edit"))
238                 .add(MenuItem(MenuItem::Submenu, 
239                               "Documents|D", "documents"));
240         add(main);
241
242         Menu main_nobuffer("main_nobuffer", true);
243         main_nobuffer.add(MenuItem(MenuItem::Submenu, "File|F", "file"));
244         add(main_nobuffer);
245
246         if (lyxerr.debugging(Debug::GUI)) {
247                 for(const_iterator cit = begin();
248                     cit != end() ; ++cit)
249                         lyxerr << "Menu name: " << cit->name() 
250                                << ", Menubar: " << cit->menubar() 
251                                << endl;
252         }
253 }
254
255
256 void MenuBackend::add(Menu const & menu)
257 {
258         menulist_.push_back(menu);
259 }
260
261
262 bool MenuBackend::hasMenu(string const & name) const
263 {
264         for (const_iterator cit = begin(); cit != end(); ++cit) {
265                 if ((*cit).name() == name)
266                         return true;
267         }
268         return false;
269 }
270
271
272 Menu const & MenuBackend::getMenu(string const & name) const
273 {
274         for (const_iterator cit = begin(); cit != end(); ++cit) {
275                 if ((*cit).name() == name)
276                         return (*cit);
277         }
278         Assert(false); // we actually require the name to exist.
279         return menulist_.front();
280 }