]> git.lyx.org Git - features.git/blob - src/MenuBackend.C
handle Wide attribute of floats in their dialog (from Juergen S)
[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 }
390
391 } // namespace anon
392
393
394 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
395                          Buffer const * buf) const
396 {
397         for (Menu::const_iterator cit = frommenu.begin();
398              cit != frommenu.end() ; ++cit) {
399                 switch (cit->kind()) {
400                 case MenuItem::Lastfiles: 
401                         expandLastfiles(tomenu);
402                         break;
403
404                 case MenuItem::Documents:
405                         expandDocuments(tomenu);
406                         break;
407
408                 case MenuItem::ImportFormats:
409                 case MenuItem::ViewFormats:
410                 case MenuItem::UpdateFormats:
411                 case MenuItem::ExportFormats:
412                         expandFormats(cit->kind(), tomenu, buf);
413                         break;
414
415                 case MenuItem::FloatListInsert:
416                         expandFloatListInsert(tomenu);
417                         break;
418
419                 case MenuItem::FloatInsert:
420                         expandFloatInsert(tomenu);
421                         break;
422
423                 case MenuItem::Submenu: {
424                         MenuItem item(*cit);
425                         item.submenu_.reset(new Menu(cit->submenuname_));
426                         expand(getMenu(cit->submenuname_),
427                                *item.submenu_, buf);
428                         tomenu.add(item);
429                 }
430                 break;
431                         
432                 default:
433                         tomenu.add(*cit);
434                 }
435         }
436
437         // Check whether the shortcuts are unique
438         if (lyxerr.debugging(Debug::GUI))
439                 tomenu.checkShortcuts();
440 }
441
442
443 bool Menu::hasSubmenu(string const & name) const
444 {
445         return find_if(begin(), end(),
446                        lyx::compare_memfun(&MenuItem::submenuname,
447                                            name)) != end();
448 }
449
450
451 void MenuBackend::read(LyXLex & lex)
452 {
453         enum Menutags {
454                 md_menu = 1,
455                 md_menubar,
456                 md_endmenuset,
457                 md_last
458         };
459
460         struct keyword_item menutags[md_last - 1] = {
461                 { "end", md_endmenuset },
462                 { "menu", md_menu },
463                 { "menubar", md_menubar }
464         };
465
466         //consistency check
467         if (compare_ascii_no_case(lex.getString(), "menuset")) {
468                 lyxerr << "Menubackend::read: ERROR wrong token:`"
469                        << lex.getString() << '\'' << endl;
470         }
471
472         lex.pushTable(menutags, md_last - 1);
473         if (lyxerr.debugging(Debug::PARSER))
474                 lex.printTable(lyxerr);
475
476         bool quit = false;
477
478         while (lex.isOK() && !quit) {
479                 switch (lex.lex()) {
480                 case md_menubar:
481                         menubar_.read(lex);
482                         break;
483                 case md_menu: {
484                         lex.next(true);
485                         string const name = lex.getString();
486                         if (hasMenu(name)) {
487                                 getMenu(name).read(lex);
488                         } else {
489                                 Menu menu(name);
490                                 menu.read(lex);
491                                 add(menu);
492                         }
493                         break;
494                 }
495                 case md_endmenuset:
496                         quit = true;
497                         break;
498                 default:
499                         lex.printError("menubackend::read: "
500                                        "Unknown menu tag: `$$Token'");
501                         break;
502                 }
503         }
504         lex.popTable();
505 }
506
507
508 void MenuBackend::defaults()
509 {
510         menulist_.clear();
511
512         lyxerr[Debug::GUI] << "MenuBackend::defaults: using default values"
513                            << endl;
514
515         Menu file("file");
516         file
517                 .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
518                 .add(MenuItem(MenuItem::Command, _("Open...|O"), "file-open"))
519                 .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
520                 .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
521                 .add(MenuItem(MenuItem::Separator))
522                 .add(MenuItem(MenuItem::Lastfiles));
523         add(file);
524
525         Menu import("import");
526         import
527                 .add(MenuItem(MenuItem::Command,
528                               _("LaTeX...|L"), "buffer-import latex"))
529                 .add(MenuItem(MenuItem::Command,
530                               _("LinuxDoc...|L"), "buffer-import linuxdoc"));
531         add(import);
532
533         Menu edit("edit");
534         edit
535                 .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
536                 .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
537                 .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
538                 .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
539         add(edit);
540
541         Menu documents("documents");
542         documents.add(MenuItem(MenuItem::Documents));
543         add(documents);
544
545         menubar_.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
546                 .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
547                 .add(MenuItem(MenuItem::Submenu,
548                               _("Documents|D"), "documents"));
549
550 }
551
552
553 void MenuBackend::add(Menu const & menu)
554 {
555         menulist_.push_back(menu);
556 }
557
558
559 bool MenuBackend::hasMenu(string const & name) const
560 {
561         return find_if(begin(), end(),
562                        lyx::compare_memfun(&Menu::name, name)) != end();
563 }
564
565
566 Menu const & MenuBackend::getMenu(string const & name) const
567 {
568         const_iterator cit = find_if(begin(), end(),
569                                      lyx::compare_memfun(&Menu::name, name));
570         lyx::Assert(cit != end());
571         return (*cit);
572 }
573
574
575 Menu & MenuBackend::getMenu(string const & name)
576 {
577         MenuList::iterator it =
578                 find_if(menulist_.begin(), menulist_.end(),
579                         lyx::compare_memfun(&Menu::name, name));
580         lyx::Assert(it != menulist_.end());
581         return (*it);
582 }
583
584
585 Menu const & MenuBackend::getMenubar() const
586 {
587         return menubar_;
588 }