]> git.lyx.org Git - features.git/blob - src/MenuBackend.C
Gnome Error&Ref popups, change LyXParagraph::size_type, many tweaks to help cxx compile
[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 <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 #include "lastfiles.h"
25 #include "bufferlist.h"
26 #include "exporter.h"
27 #include "support/filetools.h"
28 #include "support/lyxfunctional.h"
29
30 extern LyXAction lyxaction;
31 extern LastFiles * lastfiles; 
32 extern BufferList bufferlist;
33
34 using std::endl;
35 using std::vector;
36 using std::pair;
37 using std::find_if;
38
39 // This is the global menu definition
40 MenuBackend menubackend;
41
42
43 MenuItem::MenuItem(Kind kind, string const & label, 
44                    string const & command, bool optional) 
45         : kind_(kind), label_(label), optional_(optional)
46 {
47         switch(kind) {
48         case Separator:
49         case Documents:
50         case Lastfiles:
51         case Toc:
52         case References:
53         case ViewFormats:
54         case UpdateFormats:
55         case ExportFormats:
56                 break;
57         case Command:
58                 action_ = lyxaction.LookupFunc(command);
59
60                 if (action_ == LFUN_UNKNOWN_ACTION) {
61                         lyxerr << "MenuItem(): LyX command `"
62                                << command << "' does not exist." << endl; 
63                 }
64                 if (optional_)
65                         lyxerr[Debug::GUI] << "Optional item " 
66                                            << command << endl; 
67                 break;
68         case Submenu:
69                 submenu_ = command;
70                 break;
71         }
72 }
73
74
75 Menu & Menu::add(MenuItem const & i)
76 {
77         items_.push_back(i);
78         return *this;
79 }
80
81
82 Menu & Menu::read(LyXLex & lex)
83 {
84         enum Menutags {
85                 md_item = 1,
86                 md_documents,
87                 md_endmenu,
88                 md_exportformats,
89                 md_lastfiles,
90                 md_optitem,
91                 md_references,
92                 md_separator,
93                 md_submenu,
94                 md_toc,
95                 md_updateformats,
96                 md_viewformats,
97                 md_last
98         };
99
100         struct keyword_item menutags[md_last - 1] = {
101                 { "documents", md_documents },
102                 { "end", md_endmenu },
103                 { "exportformats", md_exportformats },
104                 { "item", md_item },
105                 { "lastfiles", md_lastfiles },
106                 { "optitem", md_optitem }, 
107                 { "references", md_references },
108                 { "separator", md_separator },
109                 { "submenu", md_submenu },
110                 { "toc", md_toc },
111                 { "updateformats", md_updateformats },
112                 { "viewformats", md_viewformats }
113         };
114
115         lex.pushTable(menutags, md_last - 1);
116         if (lyxerr.debugging(Debug::PARSER))
117                 lex.printTable(lyxerr);
118
119         bool quit = false;
120         bool optional = false;
121
122         while (lex.IsOK() && !quit) {
123                 switch(lex.lex()) {
124                 case md_optitem:
125                         optional = true;
126                         // fallback to md_item
127                 case md_item: {
128                         lex.next();
129                         string name = _(lex.GetString());
130                         lex.next();
131                         string const command = lex.GetString();
132                         add(MenuItem(MenuItem::Command, name, 
133                                      command, optional));
134                         optional = false;
135                         break;
136                 }
137
138                 case md_separator:
139                         add(MenuItem(MenuItem::Separator));
140                         break;
141
142                 case md_lastfiles:
143                         add(MenuItem(MenuItem::Lastfiles));
144                         break;
145
146                 case md_documents:
147                         add(MenuItem(MenuItem::Documents));
148                         break;
149
150                 case md_toc:
151                         add(MenuItem(MenuItem::Toc));
152                         break;
153
154                 case md_references:
155                         add(MenuItem(MenuItem::References));
156                         break;
157
158                 case md_viewformats:
159                         add(MenuItem(MenuItem::ViewFormats));
160                         break;
161
162                 case md_updateformats:
163                         add(MenuItem(MenuItem::UpdateFormats));
164                         break;
165
166                 case md_exportformats:
167                         add(MenuItem(MenuItem::ExportFormats));
168                         break;
169
170                 case md_submenu: {
171                         lex.next();
172                         string mlabel = _(lex.GetString());
173                         lex.next();
174                         string mname = lex.GetString();
175                         add(MenuItem(MenuItem::Submenu, mlabel, mname));
176                         break;
177                 }
178
179                 case md_endmenu:
180                         quit = true;
181                         break;
182
183                 default:
184                         lex.printError("menubar::read: "
185                                        "Unknown menu tag: `$$Token'");
186                         break;
187                 }
188         }
189         lex.popTable();
190         return *this;
191 }
192
193
194 void Menu::expand(Menu & tomenu, Buffer * buf) const
195 {
196         for (const_iterator cit = begin();
197              cit != end() ; ++cit) {
198                 switch ((*cit).kind()) {
199                 case MenuItem::Lastfiles: {
200                         int ii = 1;
201                         for (LastFiles::const_iterator lfit = lastfiles->begin();
202                              lfit != lastfiles->end() && ii < 10;
203                              ++lfit, ++ii) {
204                                 string label = tostr(ii) + ". "
205                                         + MakeDisplayPath((*lfit), 30)
206                                         + '|' + tostr(ii);
207                                 int action = lyxaction.
208                                         getPseudoAction(LFUN_FILE_OPEN,
209                                                         (*lfit));
210                                 tomenu.add(MenuItem(MenuItem::Command,
211                                                     label, action));
212                         }
213                 }
214                 break;
215                 
216                 case MenuItem::Documents: {
217                         vector<string> names = bufferlist.getFileNames();
218                         
219                         if (names.empty()) {
220                                 tomenu.add(MenuItem(MenuItem::Command,
221                                                     _("No Documents Open!"),
222                                                     LFUN_NOACTION));
223                                 break;
224                         }
225
226                         for (vector<string>::const_iterator docit = names.begin();
227                              docit != names.end() ; ++docit) {
228                                 int action =
229                                         lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
230                                 string label = MakeDisplayPath(*docit, 30);
231                                 tomenu.add(MenuItem(MenuItem::Command,
232                                                     label, action));
233                         }
234                 }
235                 break;
236
237                 case MenuItem::ViewFormats:
238                 case MenuItem::UpdateFormats:
239                 case MenuItem::ExportFormats: {
240                         vector<pair<string,string> > names;
241                         kb_action action;
242                         if ((*cit).kind() == MenuItem::ViewFormats) {
243                                 names = Exporter::GetViewableFormats(buf);
244                                 action = LFUN_PREVIEW;
245                         } else if ((*cit).kind() == MenuItem::UpdateFormats) {
246                                 names = Exporter::GetViewableFormats(buf);
247                                 action = LFUN_UPDATE;
248                         } else {
249                                 names = Exporter::GetExportableFormats(buf);
250                                 action = LFUN_EXPORT;
251                         }
252
253                         for (vector<pair<string,string> >::const_iterator fit = names.begin();
254                              fit != names.end() ; ++fit) {
255                                 int action2 =
256                                         lyxaction.getPseudoAction(action,
257                                                                   (*fit).first);
258                                 string label = (*fit).second;
259                                 tomenu.add(MenuItem(MenuItem::Command,
260                                                     label, action2));
261                         }
262                 }
263                 break;
264
265                         
266                 default:
267                         tomenu.add(*cit);
268                 }
269         }
270 }
271
272
273 void MenuBackend::read(LyXLex & lex)
274 {
275         enum Menutags {
276                 md_menu = 1,
277                 md_menubar,
278                 md_endmenuset,
279                 md_last
280         };
281
282         struct keyword_item menutags[md_last - 1] = {
283                 { "end", md_endmenuset },
284                 { "menu", md_menu },
285                 { "menubar", md_menubar }
286         };
287
288         //consistency check
289         if (compare_no_case(lex.GetString(), "menuset"))
290                 lyxerr << "Menubackend::read: ERROR wrong token:`"
291                        << lex.GetString() << '\'' << endl;
292
293         lex.pushTable(menutags, md_last - 1);
294         if (lyxerr.debugging(Debug::PARSER))
295                 lex.printTable(lyxerr);
296
297         bool quit = false;
298         bool menubar = false;
299
300         while (lex.IsOK() && !quit) {
301                 switch(lex.lex()) {
302                 case md_menubar: 
303                         menubar = true;
304                         // fallback to md_menu
305                 case md_menu: {
306                         lex.next();
307                         string name = lex.GetString();
308                         if (hasMenu(name)) {
309                                 if (getMenu(name).menubar() == menubar) {
310                                         getMenu(name).read(lex);
311                                 } else {
312                                         lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
313                                         return;
314                                 }
315                         } else {                                
316                                 Menu menu(name, menubar);
317                                 menu.read(lex);
318                                 add(menu);
319                         }
320                         menubar = false;
321                         break;
322                 }
323                 case md_endmenuset:
324                         quit = true;
325                         break;
326                 default:
327                         lex.printError("menubackend::read: "
328                                        "Unknown menu tag: `$$Token'");
329                         break;
330                 }
331         }
332         lex.popTable();
333 }
334
335
336 void MenuBackend::defaults()
337 {
338         menulist_.clear();
339
340         lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values" 
341                            << endl;
342
343         Menu file("file");
344         file
345                 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
346                 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
347                 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
348                 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
349                 .add(MenuItem(MenuItem::Separator))
350                 .add(MenuItem(MenuItem::Lastfiles));
351         add(file);
352
353         Menu import("import");
354         import
355                 .add(MenuItem(MenuItem::Command,
356                               _("LaTeX...|L"), "buffer-import latex"))
357                 .add(MenuItem(MenuItem::Command,
358                               _("LinuxDoc...|L"), "buffer-import linuxdoc"));
359         add(import);
360  
361         Menu edit("edit");
362         edit
363                 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
364                 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
365                 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
366                 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
367         add(edit);
368
369         Menu documents("documents");
370         documents.add(MenuItem(MenuItem::Documents));
371         add(documents);
372
373         Menu main("main", true);
374         main
375                 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
376                 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
377                 .add(MenuItem(MenuItem::Submenu,
378                               _("Documents|D"), "documents"));
379         add(main);
380
381         Menu main_nobuffer("main_nobuffer", true);
382         main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
383         add(main_nobuffer);
384
385         if (lyxerr.debugging(Debug::GUI)) {
386                 for(const_iterator cit = begin();
387                     cit != end() ; ++cit)
388                         lyxerr << "Menu name: " << cit->name() 
389                                << ", Menubar: " << cit->menubar() 
390                                << endl;
391         }
392 }
393
394
395 void MenuBackend::add(Menu const & menu)
396 {
397         menulist_.push_back(menu);
398 }
399
400
401 bool MenuBackend::hasMenu(string const & name) const
402 {
403         return find_if(begin(), end(),
404                        compare_memfun(&Menu::name, name)) != end();
405 }
406
407
408 Menu const & MenuBackend::getMenu(string const & name) const
409 {
410         const_iterator cit = find_if(begin(), end(),
411                                      compare_memfun(&Menu::name, name));
412         Assert(cit != end());
413         return (*cit);
414 }
415
416
417 Menu & MenuBackend::getMenu(string const & name)
418 {
419         MenuList::iterator it = find_if(menulist_.begin(), menulist_.end(),
420                                         compare_memfun(&Menu::name, name));
421         Assert(it != menulist_.end());
422         return (*it);
423 }