]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Toolbars.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / Toolbars.cpp
1 /**
2  * \file Toolbars.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Toolbars.h"
15 #include "Converter.h"
16 #include "Format.h"
17 #include "FuncRequest.h"
18 #include "Lexer.h"
19 #include "LyXAction.h"
20 #include "qt_helpers.h"
21
22 #include "support/debug.h"
23 #include "support/gettext.h"
24 #include "support/lstrings.h"
25
26 #include <boost/bind.hpp>
27
28 #include <algorithm>
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34 namespace frontend {
35
36
37 /////////////////////////////////////////////////////////////////////////
38 //
39 // ToolbarItem
40 //
41 /////////////////////////////////////////////////////////////////////////
42
43 ToolbarItem::ToolbarItem(Type type, FuncRequest const & func, docstring const & label)
44         : type_(type), func_(func), label_(label)
45 {
46 }
47
48
49 ToolbarItem::ToolbarItem(Type type, string const & name, docstring const & label)
50         : type_(type), label_(label), name_(name)
51 {
52 }
53
54
55 void ToolbarInfo::add(ToolbarItem const & item)
56 {
57         items.push_back(item);
58         items.back().func_.origin = FuncRequest::TOOLBAR;
59 }
60
61
62 ToolbarInfo & ToolbarInfo::read(Lexer & lex)
63 {
64         enum {
65                 TO_COMMAND = 1,
66                 TO_ENDTOOLBAR,
67                 TO_SEPARATOR,
68                 TO_LAYOUTS,
69                 TO_MINIBUFFER,
70                 TO_TABLEINSERT,
71                 TO_POPUPMENU,
72                 TO_STICKYPOPUPMENU,
73                 TO_ICONPALETTE,
74                 TO_EXPORTFORMATS,
75                 TO_IMPORTFORMATS,
76                 TO_UPDATEFORMATS,
77                 TO_VIEWFORMATS,
78         };
79
80         struct LexerKeyword toolTags[] = {
81                 { "end", TO_ENDTOOLBAR },
82                 { "exportformats", TO_EXPORTFORMATS },
83                 { "iconpalette", TO_ICONPALETTE },
84                 { "importformats", TO_IMPORTFORMATS },
85                 { "item", TO_COMMAND },
86                 { "layouts", TO_LAYOUTS },
87                 { "minibuffer", TO_MINIBUFFER },
88                 { "popupmenu", TO_POPUPMENU },
89                 { "separator", TO_SEPARATOR },
90                 { "stickypopupmenu", TO_STICKYPOPUPMENU },
91                 { "tableinsert", TO_TABLEINSERT },
92                 { "updateformats", TO_UPDATEFORMATS },
93                 { "viewformats", TO_VIEWFORMATS },
94         };
95
96         //consistency check
97         if (compare_ascii_no_case(lex.getString(), "toolbar")) {
98                 LYXERR0("ToolbarInfo::read: ERROR wrong token:`"
99                        << lex.getString() << '\'');
100         }
101
102         lex.next(true);
103         name = lex.getString();
104
105         lex.next(true);
106         gui_name = _(lex.getString());
107
108         // FIXME what to do here?
109         if (!lex) {
110                 LYXERR0("ToolbarInfo::read: Malformed toolbar "
111                         "description " <<  lex.getString());
112                 return *this;
113         }
114
115         bool quit = false;
116
117         lex.pushTable(toolTags);
118
119         if (lyxerr.debugging(Debug::PARSER))
120                 lex.printTable(lyxerr);
121
122         while (lex.isOK() && !quit) {
123                 int const code = lex.lex();
124                 switch (code) {
125                 case TO_COMMAND:
126                         if (lex.next(true)) {
127                                 docstring const tooltip = translateIfPossible(lex.getDocString());
128                                 lex.next(true);
129                                 string const func_arg = lex.getString();
130                                 LYXERR(Debug::PARSER, "ToolbarInfo::read TO_COMMAND func: `"
131                                         << func_arg << '\'');
132
133                                 FuncRequest func =
134                                         lyxaction.lookupFunc(func_arg);
135                                 add(ToolbarItem(ToolbarItem::COMMAND, func, tooltip));
136                         }
137                         break;
138
139                 case TO_MINIBUFFER:
140                         add(ToolbarItem(ToolbarItem::MINIBUFFER,
141                                 FuncRequest(FuncCode(ToolbarItem::MINIBUFFER))));
142                         break;
143
144                 case TO_SEPARATOR:
145                         add(ToolbarItem(ToolbarItem::SEPARATOR,
146                                 FuncRequest(FuncCode(ToolbarItem::SEPARATOR))));
147                         break;
148
149                 case TO_POPUPMENU:
150                         if (lex.next(true)) {
151                                 string const name = lex.getString();
152                                 lex.next(true);
153                                 docstring const label = lex.getDocString();
154                                 add(ToolbarItem(ToolbarItem::POPUPMENU, name, label));
155                         }
156                         break;
157
158                 case TO_STICKYPOPUPMENU:
159                         if (lex.next(true)) {
160                                 string const name = lex.getString();
161                                 lex.next(true);
162                                 docstring const label = lex.getDocString();
163                                 add(ToolbarItem(ToolbarItem::STICKYPOPUPMENU, name, label));
164                         }
165                         break;
166
167                 case TO_ICONPALETTE:
168                         if (lex.next(true)) {
169                                 string const name = lex.getString();
170                                 lex.next(true);
171                                 docstring const label = lex.getDocString();
172                                 add(ToolbarItem(ToolbarItem::ICONPALETTE, name, label));
173                         }
174                         break;
175
176                 case TO_LAYOUTS:
177                         add(ToolbarItem(ToolbarItem::LAYOUTS,
178                                 FuncRequest(FuncCode(ToolbarItem::LAYOUTS))));
179                         break;
180
181                 case TO_TABLEINSERT:
182                         if (lex.next(true)) {
183                                 docstring const tooltip = lex.getDocString();
184                                 add(ToolbarItem(ToolbarItem::TABLEINSERT,
185                                         FuncRequest(FuncCode(ToolbarItem::TABLEINSERT)), tooltip));
186                         }
187                         break;
188
189                 case TO_ENDTOOLBAR:
190                         quit = true;
191                         break;
192
193                 case TO_EXPORTFORMATS:
194                 case TO_IMPORTFORMATS:
195                 case TO_UPDATEFORMATS:
196                 case TO_VIEWFORMATS: {
197                         vector<Format const *> formats = (code == TO_IMPORTFORMATS) ?
198                                 theConverters().importableFormats() :
199                                 theConverters().exportableFormats(code != TO_EXPORTFORMATS);
200                         sort(formats.begin(), formats.end());
201                         vector<Format const *>::const_iterator fit = formats.begin();
202                         vector<Format const *>::const_iterator end = formats.end();
203                         for (; fit != end ; ++fit) {
204                                 if ((*fit)->dummy())
205                                         continue;
206                                 if (code != TO_IMPORTFORMATS &&
207                                     !(*fit)->documentFormat())
208                                         continue;
209
210                                 docstring const prettyname =
211                                         from_utf8((*fit)->prettyname());
212                                 docstring tooltip;
213                                 FuncCode lfun = LFUN_NOACTION;
214                                 switch (code) {
215                                 case TO_EXPORTFORMATS:
216                                         lfun = LFUN_BUFFER_EXPORT;
217                                         tooltip = _("Export %1$s");
218                                         break;
219                                 case TO_IMPORTFORMATS:
220                                         lfun = LFUN_BUFFER_IMPORT;
221                                         tooltip = _("Import %1$s");
222                                         break;
223                                 case TO_UPDATEFORMATS:
224                                         lfun = LFUN_BUFFER_UPDATE;
225                                         tooltip = _("Update %1$s");
226                                         break;
227                                 case TO_VIEWFORMATS:
228                                         lfun = LFUN_BUFFER_VIEW;
229                                         tooltip = _("View %1$s");
230                                         break;
231                                 }
232                                 FuncRequest func(lfun, (*fit)->name(),
233                                                 FuncRequest::TOOLBAR);
234                                 add(ToolbarItem(ToolbarItem::COMMAND, func,
235                                                 bformat(tooltip, prettyname)));
236                         }
237                         break;
238                 }
239
240                 default:
241                         lex.printError("ToolbarInfo::read: "
242                                        "Unknown toolbar tag: `$$Token'");
243                         break;
244                 }
245         }
246
247         lex.popTable();
248         return *this;
249 }
250
251
252
253 /////////////////////////////////////////////////////////////////////////
254 //
255 // Toolbars
256 //
257 /////////////////////////////////////////////////////////////////////////
258
259 void Toolbars::reset()
260 {
261         toolbar_info_.clear();
262         toolbar_visibility_.clear();
263 }
264
265
266 void Toolbars::readToolbars(Lexer & lex)
267 {
268         enum {
269                 TO_TOOLBAR = 1,
270                 TO_ENDTOOLBARSET,
271         };
272
273         struct LexerKeyword toolTags[] = {
274                 { "end", TO_ENDTOOLBARSET },
275                 { "toolbar", TO_TOOLBAR }
276         };
277
278         //consistency check
279         if (compare_ascii_no_case(lex.getString(), "toolbarset")) {
280                 LYXERR0("Toolbars::readToolbars: ERROR wrong token:`"
281                        << lex.getString() << '\'');
282         }
283
284         lex.pushTable(toolTags);
285
286         if (lyxerr.debugging(Debug::PARSER))
287                 lex.printTable(lyxerr);
288
289         bool quit = false;
290         while (lex.isOK() && !quit) {
291                 switch (lex.lex()) {
292                 case TO_TOOLBAR: {
293                         ToolbarInfo tbinfo;
294                         tbinfo.read(lex);
295                         toolbar_info_.push_back(tbinfo);
296                         break;
297                         }
298                 case TO_ENDTOOLBARSET:
299                         quit = true;
300                         break;
301                 default:
302                         lex.printError("Toolbars::readToolbars: "
303                                        "Unknown toolbar tag: `$$Token'");
304                         break;
305                 }
306         }
307
308         lex.popTable();
309 }
310
311
312 void Toolbars::readToolbarSettings(Lexer & lex)
313 {
314         //consistency check
315         if (compare_ascii_no_case(lex.getString(), "toolbars")) {
316                 LYXERR0("Toolbars::readToolbarSettings: ERROR wrong token:`"
317                        << lex.getString() << '\'');
318         }
319
320         lex.next(true);
321
322         while (lex.isOK()) {
323                 string name = lex.getString();
324                 lex.next(true);
325
326                 if (!compare_ascii_no_case(name, "end"))
327                         return;
328
329                 int visibility = 0;
330                 string flagstr = lex.getString();
331                 lex.next(true);
332                 vector<string> flags = getVectorFromString(flagstr);
333
334                 vector<string>::const_iterator cit = flags.begin();
335                 vector<string>::const_iterator end = flags.end();
336
337                 for (; cit != end; ++cit) {
338                         Visibility flag = ON;
339                         if (!compare_ascii_no_case(*cit, "off"))
340                                 flag = OFF;
341                         else if (!compare_ascii_no_case(*cit, "on"))
342                                 flag = ON;
343                         else if (!compare_ascii_no_case(*cit, "math"))
344                                 flag = MATH;
345                         else if (!compare_ascii_no_case(*cit, "table"))
346                                 flag = TABLE;
347                         else if (!compare_ascii_no_case(*cit, "mathmacrotemplate"))
348                                 flag = MATHMACROTEMPLATE;
349                         else if (!compare_ascii_no_case(*cit, "review"))
350                                 flag = REVIEW;
351                         else if (!compare_ascii_no_case(*cit, "top"))
352                                 flag = TOP;
353                         else if (!compare_ascii_no_case(*cit, "bottom"))
354                                 flag = BOTTOM;
355                         else if (!compare_ascii_no_case(*cit, "left"))
356                                 flag = LEFT;
357                         else if (!compare_ascii_no_case(*cit, "right"))
358                                 flag = RIGHT;
359                         else if (!compare_ascii_no_case(*cit, "auto"))
360                                 flag = AUTO;
361                         else if (!compare_ascii_no_case(*cit, "samerow"))
362                                 flag = SAMEROW;
363                         else {
364                                 LYXERR(Debug::ANY,
365                                         "Toolbars::readToolbarSettings: unrecognised token:`"
366                                         << *cit << '\'');
367                                 continue;
368                         }
369                         visibility |= flag;
370                 }
371                 toolbar_visibility_[name] = visibility;
372
373                 if (visibility >= MATH) {
374                         if (ToolbarInfo const * ti = info(name))
375                                 const_cast<ToolbarInfo *>(ti)->gui_name +=
376                                         " (" + _("auto") + ")";
377                 }
378         }
379 }
380
381
382 ToolbarInfo const * Toolbars::info(std::string const & name) const
383 {
384         Infos::const_iterator end = toolbar_info_.end();
385         for (Infos::const_iterator it = toolbar_info_.begin(); it != end; ++it)
386                 if (it->name == name)
387                         return &(*it);
388         return 0;
389 }
390
391
392 int Toolbars::defaultVisibility(std::string const & name) const
393 {
394         map<string, int>::const_iterator it = toolbar_visibility_.find(name);
395         if (it != toolbar_visibility_.end())
396                 return it->second;
397         return OFF | BOTTOM;
398 }
399
400
401 bool Toolbars::isMainToolbar(std::string const & name) const
402 {
403         return toolbar_visibility_.find(name) != toolbar_visibility_.end();
404 }
405
406
407 } // namespace frontend
408 } // namespace lyx