]> git.lyx.org Git - features.git/blob - src/MenuBackend.C
split Menu::expand in chunks
[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                 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("Menu::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_ascii_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 void expandLastfiles(Menu & tomenu)
262 {
263         int ii = 1;
264         LastFiles::const_iterator lfit = lastfiles->begin();
265         LastFiles::const_iterator end = lastfiles->end();
266
267         for (; lfit != end && ii < 10; ++lfit, ++ii) {
268                 string const label = tostr(ii) + ". "
269                         + MakeDisplayPath((*lfit), 30)
270                         + '|' + tostr(ii);
271                 int const action = lyxaction.
272                         getPseudoAction(LFUN_FILE_OPEN,
273                                         (*lfit));
274                 tomenu.add(MenuItem(MenuItem::Command,
275                                     label, action));
276         }
277 }
278
279 void expandDocuments(Menu & tomenu)
280 {
281         typedef vector<string> Strings;
282         Strings const names = bufferlist.getFileNames();
283
284         if (names.empty()) {
285                 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
286                                     LFUN_NOACTION));
287                 return;
288         }
289         
290         int ii = 1;
291         Strings::const_iterator docit = names.begin();
292         Strings::const_iterator end = names.end();
293         for (; docit != end; ++docit, ++ii) {
294                 int const action =
295                         lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
296                 string label = MakeDisplayPath(*docit, 30);
297                 if (ii < 10)
298                         label = tostr(ii) + ". " + label + '|' + tostr(ii);
299                 tomenu.add(MenuItem(MenuItem::Command, label, action));
300         }
301 }
302
303
304 void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
305 {
306         if (!buf && kind != MenuItem::ImportFormats) {
307                 tomenu.add(MenuItem(MenuItem::Command,
308                                     _("No Documents Open!"), LFUN_NOACTION));
309                 return;
310         }                               
311                         
312         typedef vector<Format const *> Formats;
313         Formats formats;
314         kb_action action;
315         
316         switch (kind) {
317         case MenuItem::ImportFormats:
318                 formats = Importer::GetImportableFormats();
319                 action = LFUN_IMPORT;
320                 break;
321         case MenuItem::ViewFormats:
322                 formats = Exporter::GetExportableFormats(buf, true);
323                 action = LFUN_PREVIEW;
324                 break;
325         case MenuItem::UpdateFormats:
326                 formats = Exporter::GetExportableFormats(buf, true);
327                 action = LFUN_UPDATE;
328                 break;
329         default:
330                 formats = Exporter::GetExportableFormats(buf, false);
331                 action = LFUN_EXPORT;
332         }
333         sort(formats.begin(), formats.end(), compare_format());
334
335         Formats::const_iterator fit = formats.begin();
336         Formats::const_iterator end = formats.end();
337         for (; fit != end ; ++fit) {
338                 if ((*fit)->dummy())
339                         continue;
340                 string label = (*fit)->prettyname();
341                 // we need to hide the default graphic export formats
342                 // from the external menu, because we need them only
343                 // for the internal lyx-view and external latex run
344                 if (label == "EPS" || label == "XPM" || label == "PNG")
345                         continue;
346
347                 if (kind == MenuItem::ImportFormats)
348                         if ((*fit)->name() == "text")
349                                 label = _("Ascii text as lines");
350                         else if ((*fit)->name() == "textparagraph")
351                                 label = _("Ascii text as paragraphs");
352                 if (!(*fit)->shortcut().empty())
353                         label += "|" + (*fit)->shortcut();
354                 int const action2 = lyxaction.
355                         getPseudoAction(action, (*fit)->name());
356                 tomenu.add(MenuItem(MenuItem::Command, label, action2));
357         }
358 }
359
360 void expandFloatListInsert(Menu & tomenu)
361 {
362         FloatList::const_iterator cit = floatList.begin();
363         FloatList::const_iterator end = floatList.end();
364         for (; cit != end; ++cit) {
365                 int const action =  lyxaction
366                         .getPseudoAction(LFUN_FLOAT_LIST, cit->second.type());
367                 tomenu.add(MenuItem(MenuItem::Command,
368                                     _(cit->second.listName()),
369                                     action));
370         }
371 }
372
373 void expandFloatInsert(Menu & tomenu)
374 {
375         FloatList::const_iterator cit = floatList.begin();
376         FloatList::const_iterator end = floatList.end();
377         for (; cit != end; ++cit) {
378                 // normal float
379                 int const action =
380                         lyxaction.getPseudoAction(LFUN_INSET_FLOAT,
381                                                   cit->second.type());
382                 string const label = _(cit->second.name());
383                 tomenu.add(MenuItem(MenuItem::Command, label, action));
384                 
385                 // and the wide version
386                 int const action2 =
387                         lyxaction.getPseudoAction(LFUN_INSET_WIDE_FLOAT,
388                                                   cit->second.type());
389                 string const label2 = label + _(" (wide)");
390                 tomenu.add(MenuItem(MenuItem::Command, label2, action2));
391         }
392 }
393
394 } // namespace anon
395
396
397 void Menu::expand(Menu & tomenu, Buffer const * buf) const
398 {
399         for (const_iterator cit = begin();
400              cit != end() ; ++cit) {
401                 switch (cit->kind()) {
402                 case MenuItem::Lastfiles: 
403                         expandLastfiles(tomenu);
404                         break;
405
406                 case MenuItem::Documents:
407                         expandDocuments(tomenu);
408                         break;
409
410                 case MenuItem::ImportFormats:
411                 case MenuItem::ViewFormats:
412                 case MenuItem::UpdateFormats:
413                 case MenuItem::ExportFormats:
414                         expandFormats(cit->kind(), tomenu, buf);
415                         break;
416
417                 case MenuItem::FloatListInsert:
418                         expandFloatListInsert(tomenu);
419                         break;
420
421                 case MenuItem::FloatInsert:
422                         expandFloatInsert(tomenu);
423                         break;
424
425                 default:
426                         tomenu.add(*cit);
427                 }
428         }
429
430         // Check whether the shortcuts are unique
431         if (lyxerr.debugging(Debug::GUI))
432                 checkShortcuts();
433 }
434
435
436 bool Menu::hasSubmenu(string const & name) const
437 {
438         return find_if(begin(), end(),
439                        lyx::compare_memfun(&MenuItem::submenu, name)) != end();
440 }
441
442
443 void MenuBackend::read(LyXLex & lex)
444 {
445         enum Menutags {
446                 md_menu = 1,
447                 md_menubar,
448                 md_endmenuset,
449                 md_last
450         };
451
452         struct keyword_item menutags[md_last - 1] = {
453                 { "end", md_endmenuset },
454                 { "menu", md_menu },
455                 { "menubar", md_menubar }
456         };
457
458         //consistency check
459         if (compare_ascii_no_case(lex.getString(), "menuset")) {
460                 lyxerr << "Menubackend::read: ERROR wrong token:`"
461                        << lex.getString() << '\'' << endl;
462         }
463
464         lex.pushTable(menutags, md_last - 1);
465         if (lyxerr.debugging(Debug::PARSER))
466                 lex.printTable(lyxerr);
467
468         bool quit = false;
469
470         while (lex.isOK() && !quit) {
471                 switch (lex.lex()) {
472                 case md_menubar:
473                         menubar_.read(lex);
474                         break;
475                 case md_menu: {
476                         lex.next(true);
477                         string const name = lex.getString();
478                         if (hasMenu(name)) {
479                                 getMenu(name).read(lex);
480                         } else {
481                                 Menu menu(name);
482                                 menu.read(lex);
483                                 add(menu);
484                         }
485                         break;
486                 }
487                 case md_endmenuset:
488                         quit = true;
489                         break;
490                 default:
491                         lex.printError("menubackend::read: "
492                                        "Unknown menu tag: `$$Token'");
493                         break;
494                 }
495         }
496         lex.popTable();
497 }
498
499
500 void MenuBackend::defaults()
501 {
502         menulist_.clear();
503
504         lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
505                            << endl;
506
507         Menu file("file");
508         file
509                 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
510                 .add(MenuItem(MenuItem::Command, _("Open...|O"), "file-open"))
511                 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
512                 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
513                 .add(MenuItem(MenuItem::Separator))
514                 .add(MenuItem(MenuItem::Lastfiles));
515         add(file);
516
517         Menu import("import");
518         import
519                 .add(MenuItem(MenuItem::Command,
520                               _("LaTeX...|L"), "buffer-import latex"))
521                 .add(MenuItem(MenuItem::Command,
522                               _("LinuxDoc...|L"), "buffer-import linuxdoc"));
523         add(import);
524
525         Menu edit("edit");
526         edit
527                 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
528                 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
529                 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
530                 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
531         add(edit);
532
533         Menu documents("documents");
534         documents.add(MenuItem(MenuItem::Documents));
535         add(documents);
536
537         menubar_.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
538                 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
539                 .add(MenuItem(MenuItem::Submenu,
540                               _("Documents|D"), "documents"));
541
542 }
543
544
545 void MenuBackend::add(Menu const & menu)
546 {
547         menulist_.push_back(menu);
548 }
549
550
551 bool MenuBackend::hasMenu(string const & name) const
552 {
553         return find_if(begin(), end(),
554                        lyx::compare_memfun(&Menu::name, name)) != end();
555 }
556
557
558 Menu const & MenuBackend::getMenu(string const & name) const
559 {
560         const_iterator cit = find_if(begin(), end(),
561                                      lyx::compare_memfun(&Menu::name, name));
562         lyx::Assert(cit != end());
563         return (*cit);
564 }
565
566
567 Menu & MenuBackend::getMenu(string const & name)
568 {
569         MenuList::iterator it =
570                 find_if(menulist_.begin(), menulist_.end(),
571                         lyx::compare_memfun(&Menu::name, name));
572         lyx::Assert(it != menulist_.end());
573         return (*it);
574 }
575
576
577 Menu const & MenuBackend::getMenubar() const
578 {
579         return menubar_;
580 }