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