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