]> git.lyx.org Git - lyx.git/blob - src/MenuBackend.C
Herbert's patch, part 1
[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-2001 The LyX Team.
8  *
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include <algorithm>
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 "converter.h"
27 #include "exporter.h"
28 #include "importer.h"
29 #include "FloatList.h"
30 #include "support/LAssert.h"
31 #include "support/filetools.h"
32 #include "support/lyxfunctional.h"
33 #include "support/lstrings.h"
34
35 extern LyXAction lyxaction;
36 extern LastFiles * lastfiles; 
37 extern BufferList bufferlist;
38
39 using std::endl;
40 using std::vector;
41 using std::pair;
42 using std::find_if;
43 using std::sort;
44
45 // This is the global menu definition
46 MenuBackend menubackend;
47
48
49 MenuItem::MenuItem(Kind kind, string const & label, 
50                    string const & command, bool optional) 
51         : kind_(kind), label_(label), optional_(optional)
52 {
53         switch (kind) {
54         case Separator:
55         case Documents:
56         case Lastfiles:
57         case Toc:
58         case ViewFormats:
59         case UpdateFormats:
60         case ExportFormats:
61         case ImportFormats:
62         case FloatListInsert:
63         case FloatInsert:
64                 break;
65         case Command:
66                 action_ = lyxaction.LookupFunc(command);
67
68                 if (action_ == LFUN_UNKNOWN_ACTION) {
69                         lyxerr << "MenuItem(): LyX command `"
70                                << command << "' does not exist." << endl; 
71                 }
72                 if (optional_)
73                         lyxerr[Debug::GUI] << "Optional item " 
74                                            << command << endl; 
75                 break;
76         case Submenu:
77                 submenu_ = command;
78                 break;
79         }
80 }
81
82 string const MenuItem::label() const 
83
84         return token(label_, '|', 0); 
85 }
86
87 string const MenuItem::shortcut() const
88
89         return token(label_, '|', 1); 
90 }
91
92
93 Menu & Menu::add(MenuItem const & i)
94 {
95         items_.push_back(i);
96         return *this;
97 }
98
99
100 Menu & Menu::read(LyXLex & lex)
101 {
102         enum Menutags {
103                 md_item = 1,
104                 md_documents,
105                 md_endmenu,
106                 md_exportformats,
107                 md_importformats,
108                 md_lastfiles,
109                 md_optitem,
110                 md_separator,
111                 md_submenu,
112                 md_toc,
113                 md_updateformats,
114                 md_viewformats,
115                 md_floatlistinsert,
116                 md_floatinsert,
117                 md_last
118         };
119
120         struct keyword_item menutags[md_last - 1] = {
121                 { "documents", md_documents },
122                 { "end", md_endmenu },
123                 { "exportformats", md_exportformats },
124                 { "floatinsert", md_floatinsert },
125                 { "floatlistinsert", md_floatlistinsert },
126                 { "importformats", md_importformats },
127                 { "item", md_item },
128                 { "lastfiles", md_lastfiles },
129                 { "optitem", md_optitem }, 
130                 { "separator", md_separator },
131                 { "submenu", md_submenu },
132                 { "toc", md_toc },
133                 { "updateformats", md_updateformats },
134                 { "viewformats", md_viewformats }
135         };
136
137         lex.pushTable(menutags, md_last - 1);
138         if (lyxerr.debugging(Debug::PARSER))
139                 lex.printTable(lyxerr);
140
141         bool quit = false;
142         bool optional = false;
143
144         while (lex.isOK() && !quit) {
145                 switch (lex.lex()) {
146                 case md_optitem:
147                         optional = true;
148                         // fallback to md_item
149                 case md_item: {
150                         lex.next(true);
151                         string const name = _(lex.getString());
152                         lex.next(true);
153                         string const command = lex.getString();
154                         add(MenuItem(MenuItem::Command, name, 
155                                      command, optional));
156                         optional = false;
157                         break;
158                 }
159
160                 case md_separator:
161                         add(MenuItem(MenuItem::Separator));
162                         break;
163
164                 case md_lastfiles:
165                         add(MenuItem(MenuItem::Lastfiles));
166                         break;
167
168                 case md_documents:
169                         add(MenuItem(MenuItem::Documents));
170                         break;
171
172                 case md_toc:
173                         add(MenuItem(MenuItem::Toc));
174                         break;
175
176                 case md_viewformats:
177                         add(MenuItem(MenuItem::ViewFormats));
178                         break;
179
180                 case md_updateformats:
181                         add(MenuItem(MenuItem::UpdateFormats));
182                         break;
183
184                 case md_exportformats:
185                         add(MenuItem(MenuItem::ExportFormats));
186                         break;
187
188                 case md_importformats:
189                         add(MenuItem(MenuItem::ImportFormats));
190                         break;
191
192                 case md_floatlistinsert:
193                         add(MenuItem(MenuItem::FloatListInsert));
194                         break;
195
196                 case md_floatinsert:
197                         add(MenuItem(MenuItem::FloatInsert));
198                         break;
199                         
200                 case md_submenu: {
201                         lex.next(true);
202                         string const mlabel = _(lex.getString());
203                         lex.next(true);
204                         string const mname = lex.getString();
205                         add(MenuItem(MenuItem::Submenu, mlabel, mname));
206                         break;
207                 }
208
209                 case md_endmenu:
210                         quit = true;
211                         break;
212
213                 default:
214                         lex.printError("menubar::read: "
215                                        "Unknown menu tag: `$$Token'");
216                         break;
217                 }
218         }
219         lex.popTable();
220         return *this;
221 }
222
223
224 void Menu::checkShortcuts() const
225 {
226         // This is a quadratic algorithm, but we do not care because
227         // it is used for debugging only.
228         for (const_iterator it1 = begin(); it1 != end(); ++it1) {
229                 string shortcut = it1->shortcut();
230                 if (shortcut.empty())
231                         continue;
232                 if (!contains(it1->label(), shortcut))
233                         lyxerr << "Menu warning: menu entry \""
234                                << it1->label()
235                                << "\" does not contain shortcut `"
236                                << shortcut << '\'' << endl;
237                 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
238                         if (!compare_no_case(it2->shortcut(), shortcut)) {
239                                 lyxerr << "Menu warning: menu entries "
240                                        << '"' << it1->fulllabel()
241                                        << "\" and \"" << it2->fulllabel()
242                                        << "\" share the same shortcut."
243                                        << endl;
244                         }
245                 }
246         }
247 }
248
249 namespace {
250
251 class compare_format {
252 public:
253         bool operator()(Format const * p1, Format const * p2) {
254                 return *p1 < *p2;       
255         }
256 };
257
258 } // namespace anon
259
260
261 void Menu::expand(Menu & tomenu, Buffer * buf) const
262 {
263         for (const_iterator cit = begin();
264              cit != end() ; ++cit) {
265                 switch (cit->kind()) {
266                 case MenuItem::Lastfiles: {
267                         int ii = 1;
268                         LastFiles::const_iterator lfit = lastfiles->begin();
269                         LastFiles::const_iterator end = lastfiles->end();
270                         
271                         for (; lfit != end && ii < 10; ++lfit, ++ii) {
272                                 string const label = tostr(ii) + ". "
273                                         + MakeDisplayPath((*lfit), 30)
274                                         + '|' + tostr(ii);
275                                 int const action = lyxaction.
276                                         getPseudoAction(LFUN_FILE_OPEN,
277                                                         (*lfit));
278                                 tomenu.add(MenuItem(MenuItem::Command,
279                                                     label, action));
280                         }
281                 }
282                 break;
283                 
284                 case MenuItem::Documents: {
285                         typedef vector<string> Strings;
286                         
287                         Strings const names = bufferlist.getFileNames();
288                         
289                         if (names.empty()) {
290                                 tomenu.add(MenuItem(MenuItem::Command,
291                                                     _("No Documents Open!"),
292                                                     LFUN_NOACTION));
293                                 break;
294                         }
295
296                         Strings::const_iterator docit = names.begin();
297                         Strings::const_iterator end = names.end();
298                         for (; docit != end ; ++docit) {
299                                 int const action = lyxaction
300                                         .getPseudoAction(LFUN_SWITCHBUFFER,
301                                                          *docit);
302                                 string const label =
303                                         MakeDisplayPath(*docit, 30);
304                                 tomenu.add(MenuItem(MenuItem::Command,
305                                                     label, action));
306                         }
307                 }
308                 break;
309
310                 case MenuItem::ImportFormats:
311                 case MenuItem::ViewFormats:
312                 case MenuItem::UpdateFormats:
313                 case MenuItem::ExportFormats: {
314                         typedef vector<Format const *> Formats;
315                         
316                         Formats formats;
317                         
318                         kb_action action;
319                         switch (cit->kind()) {
320                         case MenuItem::ImportFormats:
321                                 formats = Importer::GetImportableFormats();
322                                 action = LFUN_IMPORT;
323                                 break;
324                         case MenuItem::ViewFormats:
325                                 formats = Exporter::GetExportableFormats(buf, true); 
326                                 action = LFUN_PREVIEW;
327                                 break;
328                         case MenuItem::UpdateFormats:
329                                 formats = Exporter::GetExportableFormats(buf, true); 
330                                 action = LFUN_UPDATE;
331                                 break;
332                         default:
333                                 formats = Exporter::GetExportableFormats(buf, false); 
334                                 action = LFUN_EXPORT;
335                         }
336                         sort(formats.begin(), formats.end(), compare_format());
337
338                         Formats::const_iterator fit = formats.begin();
339                         Formats::const_iterator end = formats.end();
340                         
341                         for (; fit != end ; ++fit) {
342                                 if ((*fit)->dummy())
343                                         continue;
344                                 string label = (*fit)->prettyname();
345                                 if (cit->kind() == MenuItem::ImportFormats)
346                                         if ((*fit)->name() == "text")
347                                                 label = _("Ascii text as lines");
348                                         else if ((*fit)->name() == "textparagraph")
349                                                 label = _("Ascii text as paragraphs");
350                                 if (!(*fit)->shortcut().empty())
351                                         label += "|" + (*fit)->shortcut();
352                                 int const action2 = lyxaction.
353                                         getPseudoAction(action,
354                                                         (*fit)->name());
355                                 tomenu.add(MenuItem(MenuItem::Command,
356                                                     label, action2));
357                         }
358                 }
359                 break;
360
361                 case MenuItem::FloatListInsert:
362                 {
363                         FloatList::const_iterator cit = floatList.begin();
364                         FloatList::const_iterator end = floatList.end();
365                         for (; cit != end; ++cit) {
366                                 int const action =  lyxaction
367                                         .getPseudoAction(LFUN_FLOAT_LIST,
368                                                          cit->second.type());
369                                 string const label = cit->second.name() + _(" List");
370                                 tomenu.add(MenuItem(MenuItem::Command,
371                                                     label, action));
372                         }
373                 }
374                 break;
375
376                 case MenuItem::FloatInsert:
377                 {
378                         FloatList::const_iterator cit = floatList.begin();
379                         FloatList::const_iterator end = floatList.end();
380                         for (; cit != end; ++cit) {
381                                 // normal float
382                                 int const action = lyxaction
383                                         .getPseudoAction(LFUN_INSET_FLOAT,
384                                                          cit->second.type());
385                                 string const label = cit->second.name();
386                                 tomenu.add(MenuItem(MenuItem::Command,
387                                                     label, action));
388                                 
389                                 // and the wide version
390                                 int const action2 = lyxaction
391                                         .getPseudoAction(LFUN_INSET_WIDE_FLOAT,
392                                                          cit->second.type());
393                                 string const label2 = _("Wide ") + label;
394                                 tomenu.add(MenuItem(MenuItem::Command,
395                                                     label2, action2));
396                         }
397                 }
398                 break;
399                 
400                 default:
401                         tomenu.add(*cit);
402                 }
403         }
404
405         // Check whether the shortcuts are unique
406         if (lyxerr.debugging(Debug::GUI))
407                 checkShortcuts();
408 }
409
410
411 bool Menu::hasSubmenu(string const & name) const
412 {
413         return find_if(begin(), end(),
414                        lyx::compare_memfun(&MenuItem::submenu, name)) != end();
415 }
416
417
418 void MenuBackend::read(LyXLex & lex)
419 {
420         enum Menutags {
421                 md_menu = 1,
422                 md_menubar,
423                 md_endmenuset,
424                 md_last
425         };
426
427         struct keyword_item menutags[md_last - 1] = {
428                 { "end", md_endmenuset },
429                 { "menu", md_menu },
430                 { "menubar", md_menubar }
431         };
432
433         //consistency check
434         if (compare_no_case(lex.getString(), "menuset")) {
435                 lyxerr << "Menubackend::read: ERROR wrong token:`"
436                        << lex.getString() << '\'' << endl;
437         }
438
439         lex.pushTable(menutags, md_last - 1);
440         if (lyxerr.debugging(Debug::PARSER))
441                 lex.printTable(lyxerr);
442
443         bool quit = false;
444         bool menubar = false;
445
446         while (lex.isOK() && !quit) {
447                 switch (lex.lex()) {
448                 case md_menubar: 
449                         menubar = true;
450                         // fallback to md_menu
451                 case md_menu: {
452                         lex.next(true);
453                         string const name = lex.getString();
454                         if (hasMenu(name)) {
455                                 if (getMenu(name).menubar() == menubar) {
456                                         getMenu(name).read(lex);
457                                 } else {
458                                         lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
459                                         return;
460                                 }
461                         } else {                                
462                                 Menu menu(name, menubar);
463                                 menu.read(lex);
464                                 add(menu);
465                         }
466                         menubar = false;
467                         break;
468                 }
469                 case md_endmenuset:
470                         quit = true;
471                         break;
472                 default:
473                         lex.printError("menubackend::read: "
474                                        "Unknown menu tag: `$$Token'");
475                         break;
476                 }
477         }
478         lex.popTable();
479 }
480
481
482 void MenuBackend::defaults()
483 {
484         menulist_.clear();
485
486         lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values" 
487                            << endl;
488
489         Menu file("file");
490         file
491                 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
492                 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
493                 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
494                 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
495                 .add(MenuItem(MenuItem::Separator))
496                 .add(MenuItem(MenuItem::Lastfiles));
497         add(file);
498
499         Menu import("import");
500         import
501                 .add(MenuItem(MenuItem::Command,
502                               _("LaTeX...|L"), "buffer-import latex"))
503                 .add(MenuItem(MenuItem::Command,
504                               _("LinuxDoc...|L"), "buffer-import linuxdoc"));
505         add(import);
506  
507         Menu edit("edit");
508         edit
509                 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
510                 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
511                 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
512                 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
513         add(edit);
514
515         Menu documents("documents");
516         documents.add(MenuItem(MenuItem::Documents));
517         add(documents);
518
519         Menu main("main", true);
520         main
521                 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
522                 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
523                 .add(MenuItem(MenuItem::Submenu,
524                               _("Documents|D"), "documents"));
525         add(main);
526
527         Menu main_nobuffer("main_nobuffer", true);
528         main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
529         add(main_nobuffer);
530
531         if (lyxerr.debugging(Debug::GUI)) {
532                 for (const_iterator cit = begin();
533                     cit != end() ; ++cit)
534                         lyxerr << "Menu name: " << cit->name() 
535                                << ", Menubar: " << cit->menubar() 
536                                << endl;
537         }
538 }
539
540
541 void MenuBackend::add(Menu const & menu)
542 {
543         menulist_.push_back(menu);
544 }
545
546
547 bool MenuBackend::hasMenu(string const & name) const
548 {
549         return find_if(begin(), end(),
550                        lyx::compare_memfun(&Menu::name, name)) != end();
551 }
552
553
554 Menu const & MenuBackend::getMenu(string const & name) const
555 {
556         const_iterator cit = find_if(begin(), end(),
557                                      lyx::compare_memfun(&Menu::name, name));
558         lyx::Assert(cit != end());
559         return (*cit);
560 }
561
562
563 Menu & MenuBackend::getMenu(string const & name)
564 {
565         MenuList::iterator it =
566                 find_if(menulist_.begin(), menulist_.end(),
567                         lyx::compare_memfun(&Menu::name, name));
568         lyx::Assert(it != menulist_.end());
569         return (*it);
570 }