]> git.lyx.org Git - lyx.git/blob - src/MenuBackend.C
Added new FINISED states FINISHED_RIGHT, FINISHED_UP, FINISHED_DOWN.
[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 label = _("List of ");
370                                 label += cit->second.name();
371                                 tomenu.add(MenuItem(MenuItem::Command,
372                                                     label, action));
373                         }
374                 }
375                 break;
376
377                 case MenuItem::FloatInsert:
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 = lyxaction
384                                         .getPseudoAction(LFUN_INSET_FLOAT,
385                                                          cit->second.type());
386                                 string const label = cit->second.name();
387                                 tomenu.add(MenuItem(MenuItem::Command,
388                                                     label, action));
389                                 
390                                 // and the wide version
391                                 int const action2 = lyxaction
392                                         .getPseudoAction(LFUN_INSET_WIDE_FLOAT,
393                                                          cit->second.type());
394                                 string const label2 = _("Wide ") + label;
395                                 tomenu.add(MenuItem(MenuItem::Command,
396                                                     label2, action2));
397                         }
398                 }
399                 break;
400                 
401                 default:
402                         tomenu.add(*cit);
403                 }
404         }
405
406         // Check whether the shortcuts are unique
407         if (lyxerr.debugging(Debug::GUI))
408                 checkShortcuts();
409 }
410
411
412 bool Menu::hasSubmenu(string const & name) const
413 {
414         return find_if(begin(), end(),
415                        lyx::compare_memfun(&MenuItem::submenu, name)) != end();
416 }
417
418
419 void MenuBackend::read(LyXLex & lex)
420 {
421         enum Menutags {
422                 md_menu = 1,
423                 md_menubar,
424                 md_endmenuset,
425                 md_last
426         };
427
428         struct keyword_item menutags[md_last - 1] = {
429                 { "end", md_endmenuset },
430                 { "menu", md_menu },
431                 { "menubar", md_menubar }
432         };
433
434         //consistency check
435         if (compare_no_case(lex.getString(), "menuset")) {
436                 lyxerr << "Menubackend::read: ERROR wrong token:`"
437                        << lex.getString() << '\'' << endl;
438         }
439
440         lex.pushTable(menutags, md_last - 1);
441         if (lyxerr.debugging(Debug::PARSER))
442                 lex.printTable(lyxerr);
443
444         bool quit = false;
445         bool menubar = false;
446
447         while (lex.isOK() && !quit) {
448                 switch (lex.lex()) {
449                 case md_menubar: 
450                         menubar = true;
451                         // fallback to md_menu
452                 case md_menu: {
453                         lex.next(true);
454                         string const name = lex.getString();
455                         if (hasMenu(name)) {
456                                 if (getMenu(name).menubar() == menubar) {
457                                         getMenu(name).read(lex);
458                                 } else {
459                                         lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
460                                         return;
461                                 }
462                         } else {                                
463                                 Menu menu(name, menubar);
464                                 menu.read(lex);
465                                 add(menu);
466                         }
467                         menubar = false;
468                         break;
469                 }
470                 case md_endmenuset:
471                         quit = true;
472                         break;
473                 default:
474                         lex.printError("menubackend::read: "
475                                        "Unknown menu tag: `$$Token'");
476                         break;
477                 }
478         }
479         lex.popTable();
480 }
481
482
483 void MenuBackend::defaults()
484 {
485         menulist_.clear();
486
487         lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values" 
488                            << endl;
489
490         Menu file("file");
491         file
492                 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
493                 .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
494                 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
495                 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
496                 .add(MenuItem(MenuItem::Separator))
497                 .add(MenuItem(MenuItem::Lastfiles));
498         add(file);
499
500         Menu import("import");
501         import
502                 .add(MenuItem(MenuItem::Command,
503                               _("LaTeX...|L"), "buffer-import latex"))
504                 .add(MenuItem(MenuItem::Command,
505                               _("LinuxDoc...|L"), "buffer-import linuxdoc"));
506         add(import);
507  
508         Menu edit("edit");
509         edit
510                 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
511                 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
512                 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
513                 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
514         add(edit);
515
516         Menu documents("documents");
517         documents.add(MenuItem(MenuItem::Documents));
518         add(documents);
519
520         Menu main("main", true);
521         main
522                 .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
523                 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
524                 .add(MenuItem(MenuItem::Submenu,
525                               _("Documents|D"), "documents"));
526         add(main);
527
528         Menu main_nobuffer("main_nobuffer", true);
529         main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
530         add(main_nobuffer);
531
532         if (lyxerr.debugging(Debug::GUI)) {
533                 for (const_iterator cit = begin();
534                     cit != end() ; ++cit)
535                         lyxerr << "Menu name: " << cit->name() 
536                                << ", Menubar: " << cit->menubar() 
537                                << endl;
538         }
539 }
540
541
542 void MenuBackend::add(Menu const & menu)
543 {
544         menulist_.push_back(menu);
545 }
546
547
548 bool MenuBackend::hasMenu(string const & name) const
549 {
550         return find_if(begin(), end(),
551                        lyx::compare_memfun(&Menu::name, name)) != end();
552 }
553
554
555 Menu const & MenuBackend::getMenu(string const & name) const
556 {
557         const_iterator cit = find_if(begin(), end(),
558                                      lyx::compare_memfun(&Menu::name, name));
559         lyx::Assert(cit != end());
560         return (*cit);
561 }
562
563
564 Menu & MenuBackend::getMenu(string const & name)
565 {
566         MenuList::iterator it =
567                 find_if(menulist_.begin(), menulist_.end(),
568                         lyx::compare_memfun(&Menu::name, name));
569         lyx::Assert(it != menulist_.end());
570         return (*it);
571 }