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