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