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