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