]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Toolbars.cpp
GuiApplication::resetGui(): reset as many things as possible, including menus and...
[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 "FuncRequest.h"
16 #include "Lexer.h"
17 #include "LyXAction.h"
18 #include "support/lstrings.h"
19
20 #include "support/debug.h"
21 #include "support/gettext.h"
22
23 #include <boost/bind.hpp>
24
25 #include <algorithm>
26
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31 namespace frontend {
32
33 namespace {
34
35 } // namespace anon
36
37
38 /////////////////////////////////////////////////////////////////////////
39 //
40 // ToolbarItem
41 //
42 /////////////////////////////////////////////////////////////////////////
43
44 ToolbarItem::ToolbarItem(Type type, FuncRequest const & func, docstring const & label)
45         : type_(type), func_(func), label_(label)
46 {
47 }
48
49
50 ToolbarItem::ToolbarItem(Type type, string const & name, docstring const & label)
51         : type_(type), label_(label), name_(name)
52 {
53 }
54
55
56 void ToolbarInfo::add(ToolbarItem const & item)
57 {
58         items.push_back(item);
59         items.back().func_.origin = FuncRequest::TOOLBAR;
60 }
61
62
63 ToolbarInfo & ToolbarInfo::read(Lexer & lex)
64 {
65         enum {
66                 TO_COMMAND = 1,
67                 TO_ENDTOOLBAR,
68                 TO_SEPARATOR,
69                 TO_LAYOUTS,
70                 TO_MINIBUFFER,
71                 TO_TABLEINSERT,
72                 TO_POPUPMENU,
73                 TO_ICONPALETTE,
74         };
75
76         struct LexerKeyword toolTags[] = {
77                 { "end", TO_ENDTOOLBAR },
78                 { "iconpalette", TO_ICONPALETTE },
79                 { "item", TO_COMMAND },
80                 { "layouts", TO_LAYOUTS },
81                 { "minibuffer", TO_MINIBUFFER },
82                 { "popupmenu", TO_POPUPMENU },
83                 { "separator", TO_SEPARATOR },
84                 { "tableinsert", TO_TABLEINSERT }
85         };
86
87         //consistency check
88         if (compare_ascii_no_case(lex.getString(), "toolbar")) {
89                 LYXERR0("ToolbarInfo::read: ERROR wrong token:`"
90                        << lex.getString() << '\'');
91         }
92
93         lex.next(true);
94         name = lex.getString();
95
96         lex.next(true);
97         gui_name = lex.getString();
98
99         // FIXME what to do here?
100         if (!lex) {
101                 LYXERR0("ToolbarInfo::read: Malformed toolbar "
102                         "description " <<  lex.getString());
103                 return *this;
104         }
105
106         bool quit = false;
107
108         lex.pushTable(toolTags);
109
110         if (lyxerr.debugging(Debug::PARSER))
111                 lex.printTable(lyxerr);
112
113         while (lex.isOK() && !quit) {
114                 switch (lex.lex()) {
115                 case TO_COMMAND:
116                         if (lex.next(true)) {
117                                 docstring const tooltip = translateIfPossible(lex.getDocString());
118                                 lex.next(true);
119                                 string const func_arg = lex.getString();
120                                 LYXERR(Debug::PARSER, "ToolbarInfo::read TO_COMMAND func: `"
121                                         << func_arg << '\'');
122
123                                 FuncRequest func =
124                                         lyxaction.lookupFunc(func_arg);
125                                 add(ToolbarItem(ToolbarItem::COMMAND, func, tooltip));
126                         }
127                         break;
128
129                 case TO_MINIBUFFER:
130                         add(ToolbarItem(ToolbarItem::MINIBUFFER,
131                                 FuncRequest(FuncCode(ToolbarItem::MINIBUFFER))));
132                         break;
133
134                 case TO_SEPARATOR:
135                         add(ToolbarItem(ToolbarItem::SEPARATOR,
136                                 FuncRequest(FuncCode(ToolbarItem::SEPARATOR))));
137                         break;
138
139                 case TO_POPUPMENU:
140                         if (lex.next(true)) {
141                                 string const name = lex.getString();
142                                 lex.next(true);
143                                 docstring const label = lex.getDocString();
144                                 add(ToolbarItem(ToolbarItem::POPUPMENU, name, label));
145                         }
146                         break;
147
148                 case TO_ICONPALETTE:
149                         if (lex.next(true)) {
150                                 string const name = lex.getString();
151                                 lex.next(true);
152                                 docstring const label = lex.getDocString();
153                                 add(ToolbarItem(ToolbarItem::ICONPALETTE, name, label));
154                         }
155                         break;
156
157                 case TO_LAYOUTS:
158                         add(ToolbarItem(ToolbarItem::LAYOUTS,
159                                 FuncRequest(FuncCode(ToolbarItem::LAYOUTS))));
160                         break;
161
162                 case TO_TABLEINSERT:
163                         if (lex.next(true)) {
164                                 docstring const tooltip = lex.getDocString();
165                                 add(ToolbarItem(ToolbarItem::TABLEINSERT,
166                                         FuncRequest(FuncCode(ToolbarItem::TABLEINSERT)), tooltip));
167                         }
168                         break;
169
170                 case TO_ENDTOOLBAR:
171                         quit = true;
172                         break;
173
174                 default:
175                         lex.printError("ToolbarInfo::read: "
176                                        "Unknown toolbar tag: `$$Token'");
177                         break;
178                 }
179         }
180
181         lex.popTable();
182         return *this;
183 }
184
185
186
187 /////////////////////////////////////////////////////////////////////////
188 //
189 // Toolbars
190 //
191 /////////////////////////////////////////////////////////////////////////
192
193 void Toolbars::reset()
194 {
195         toolbar_info_.clear();
196         toolbar_visibility_.clear();
197 }
198
199
200 void Toolbars::readToolbars(Lexer & lex)
201 {
202         enum {
203                 TO_TOOLBAR = 1,
204                 TO_ENDTOOLBARSET,
205         };
206
207         struct LexerKeyword toolTags[] = {
208                 { "end", TO_ENDTOOLBARSET },
209                 { "toolbar", TO_TOOLBAR }
210         };
211
212         //consistency check
213         if (compare_ascii_no_case(lex.getString(), "toolbarset")) {
214                 LYXERR0("Toolbars::readToolbars: ERROR wrong token:`"
215                        << lex.getString() << '\'');
216         }
217
218         lex.pushTable(toolTags);
219
220         if (lyxerr.debugging(Debug::PARSER))
221                 lex.printTable(lyxerr);
222
223         bool quit = false;
224         while (lex.isOK() && !quit) {
225                 switch (lex.lex()) {
226                 case TO_TOOLBAR: {
227                         ToolbarInfo tbinfo;
228                         tbinfo.read(lex);
229                         toolbar_info_.push_back(tbinfo);
230                         break;
231                         }
232                 case TO_ENDTOOLBARSET:
233                         quit = true;
234                         break;
235                 default:
236                         lex.printError("Toolbars::readToolbars: "
237                                        "Unknown toolbar tag: `$$Token'");
238                         break;
239                 }
240         }
241
242         lex.popTable();
243 }
244
245
246 void Toolbars::readToolbarSettings(Lexer & lex)
247 {
248         //consistency check
249         if (compare_ascii_no_case(lex.getString(), "toolbars")) {
250                 LYXERR0("Toolbars::readToolbarSettings: ERROR wrong token:`"
251                        << lex.getString() << '\'');
252         }
253
254         lex.next(true);
255
256         while (lex.isOK()) {
257                 string name = lex.getString();
258                 lex.next(true);
259
260                 if (!compare_ascii_no_case(name, "end"))
261                         return;
262
263                 int visibility = 0;
264                 string flagstr = lex.getString();
265                 lex.next(true);
266                 vector<string> flags = getVectorFromString(flagstr);
267
268                 vector<string>::const_iterator cit = flags.begin();
269                 vector<string>::const_iterator end = flags.end();
270
271                 for (; cit != end; ++cit) {
272                         Visibility flag = ON;
273                         if (!compare_ascii_no_case(*cit, "off"))
274                                 flag = OFF;
275                         else if (!compare_ascii_no_case(*cit, "on"))
276                                 flag = ON;
277                         else if (!compare_ascii_no_case(*cit, "math"))
278                                 flag = MATH;
279                         else if (!compare_ascii_no_case(*cit, "table"))
280                                 flag = TABLE;
281                         else if (!compare_ascii_no_case(*cit, "mathmacrotemplate"))
282                                 flag = MATHMACROTEMPLATE;
283                         else if (!compare_ascii_no_case(*cit, "review"))
284                                 flag = REVIEW;
285                         else if (!compare_ascii_no_case(*cit, "top"))
286                                 flag = TOP;
287                         else if (!compare_ascii_no_case(*cit, "bottom"))
288                                 flag = BOTTOM;
289                         else if (!compare_ascii_no_case(*cit, "left"))
290                                 flag = LEFT;
291                         else if (!compare_ascii_no_case(*cit, "right"))
292                                 flag = RIGHT;
293                         else if (!compare_ascii_no_case(*cit, "auto"))
294                                 flag = AUTO;
295                         else {
296                                 LYXERR(Debug::ANY,
297                                         "Toolbars::readToolbarSettings: unrecognised token:`"
298                                         << *cit << '\'');
299                                 continue;
300                         }
301                         visibility |= flag;
302                 }
303                 toolbar_visibility_[name] = visibility;
304
305                 if (visibility >= MATH) {
306                         if (ToolbarInfo const * ti = info(name))
307                                 const_cast<ToolbarInfo *>(ti)->gui_name += " (auto)";
308                 }
309         }
310 }
311
312
313 ToolbarInfo const * Toolbars::info(std::string const & name) const
314 {
315         Infos::const_iterator end = toolbar_info_.end();
316         for (Infos::const_iterator it = toolbar_info_.begin(); it != end; ++it)
317                 if (it->name == name)
318                         return &(*it);
319         return 0;
320 }
321
322
323 int Toolbars::defaultVisibility(std::string const & name) const
324 {
325         map<string, int>::const_iterator it = toolbar_visibility_.find(name);
326         if (it != toolbar_visibility_.end())
327                 return it->second;
328         return OFF | BOTTOM;
329 }
330
331
332 bool Toolbars::isMainToolbar(std::string const & name) const
333 {
334         return toolbar_visibility_.find(name) != toolbar_visibility_.end();
335 }
336
337
338 } // namespace frontend
339 } // namespace lyx