2 * \file ToolbarBackend.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
9 * Full author contact details are available in file CREDITS.
14 #include "ToolbarBackend.h"
15 #include "FuncRequest.h"
19 #include "LyXAction.h"
20 #include "support/lstrings.h"
22 #include <boost/bind.hpp>
27 using support::compare_ascii_no_case;
28 using support::getVectorFromString;
39 class ToolbarNamesEqual : public std::unary_function<ToolbarInfo, bool> {
41 ToolbarNamesEqual(string const & name)
43 bool operator()(ToolbarInfo const & tbinfo) const
45 return tbinfo.name == name_;
54 ToolbarBackend toolbarbackend;
57 ToolbarItem::ToolbarItem(Type type, FuncRequest const & func, docstring const & label)
58 : type_(type), func_(func), label_(label)
63 ToolbarItem::ToolbarItem(Type type, string const & name, docstring const & label)
64 : type_(type), label_(label), name_(name)
69 ToolbarItem::~ToolbarItem()
73 void ToolbarInfo::add(ToolbarItem const & item)
75 items.push_back(item);
76 items.back().func_.origin = FuncRequest::TOOLBAR;
80 ToolbarInfo & ToolbarInfo::read(Lexer & lex)
94 struct keyword_item toolTags[TO_LAST - 1] = {
95 { "end", TO_ENDTOOLBAR },
96 { "iconpalette", TO_ICONPALETTE },
97 { "item", TO_COMMAND },
98 { "layouts", TO_LAYOUTS },
99 { "minibuffer", TO_MINIBUFFER },
100 { "popupmenu", TO_POPUPMENU },
101 { "separator", TO_SEPARATOR },
102 { "tableinsert", TO_TABLEINSERT }
106 if (compare_ascii_no_case(lex.getString(), "toolbar")) {
107 lyxerr << "ToolbarInfo::read: ERROR wrong token:`"
108 << lex.getString() << '\'' << endl;
112 name = lex.getString();
115 gui_name = lex.getString();
117 // FIXME what to do here?
119 lyxerr << "ToolbarInfo::read: Malformed toolbar "
120 "description " << lex.getString() << endl;
126 lex.pushTable(toolTags, TO_LAST - 1);
128 if (lyxerr.debugging(Debug::PARSER))
129 lex.printTable(lyxerr);
131 while (lex.isOK() && !quit) {
134 if (lex.next(true)) {
135 docstring const tooltip = translateIfPossible(lex.getDocString());
137 string const func_arg = lex.getString();
138 LYXERR(Debug::PARSER)
139 << "ToolbarInfo::read TO_COMMAND func: `"
140 << func_arg << '\'' << endl;
143 lyxaction.lookupFunc(func_arg);
144 add(ToolbarItem(ToolbarItem::COMMAND, func, tooltip));
149 add(ToolbarItem(ToolbarItem::MINIBUFFER,
150 FuncRequest(kb_action(ToolbarItem::MINIBUFFER))));
154 add(ToolbarItem(ToolbarItem::SEPARATOR,
155 FuncRequest(kb_action(ToolbarItem::SEPARATOR))));
159 if (lex.next(true)) {
160 string const name = lex.getString();
162 docstring const label = lex.getDocString();
163 add(ToolbarItem(ToolbarItem::POPUPMENU, name, label));
168 if (lex.next(true)) {
169 string const name = lex.getString();
171 docstring const label = lex.getDocString();
172 add(ToolbarItem(ToolbarItem::ICONPALETTE, name, label));
177 add(ToolbarItem(ToolbarItem::LAYOUTS,
178 FuncRequest(kb_action(ToolbarItem::LAYOUTS))));
182 if (lex.next(true)) {
183 docstring const tooltip = lex.getDocString();
184 add(ToolbarItem(ToolbarItem::TABLEINSERT,
185 FuncRequest(kb_action(ToolbarItem::TABLEINSERT)), tooltip));
194 lex.printError("ToolbarInfo::read: "
195 "Unknown toolbar tag: `$$Token'");
207 ToolbarBackend::ToolbarBackend()
212 void ToolbarBackend::readToolbars(Lexer & lex)
220 struct keyword_item toolTags[TO_LAST - 1] = {
221 { "end", TO_ENDTOOLBARSET },
222 { "toolbar", TO_TOOLBAR }
226 if (compare_ascii_no_case(lex.getString(), "toolbarset")) {
227 lyxerr << "ToolbarBackend::readToolbars: ERROR wrong token:`"
228 << lex.getString() << '\'' << endl;
231 lex.pushTable(toolTags, TO_LAST - 1);
233 if (lyxerr.debugging(Debug::PARSER))
234 lex.printTable(lyxerr);
237 while (lex.isOK() && !quit) {
242 toolbars.push_back(tbinfo);
245 case TO_ENDTOOLBARSET:
249 lex.printError("ToolbarBackend::readToolbars: "
250 "Unknown toolbar tag: `$$Token'");
259 void ToolbarBackend::readToolbarSettings(Lexer & lex)
262 if (compare_ascii_no_case(lex.getString(), "toolbars")) {
263 lyxerr << "ToolbarBackend::readToolbarSettings: ERROR wrong token:`"
264 << lex.getString() << '\'' << endl;
270 string name = lex.getString();
273 if (!compare_ascii_no_case(name, "end"))
276 Toolbars::iterator tcit = toolbars.begin();
277 Toolbars::iterator tend = toolbars.end();
278 for (; tcit != tend; ++tcit) {
279 if (tcit->name == name)
284 lyxerr << "ToolbarBackend: undefined toolbar "
289 tcit->flags = static_cast<ToolbarInfo::Flags>(0);
290 string flagstr = lex.getString();
292 vector<string> flags = getVectorFromString(flagstr);
294 vector<string>::const_iterator cit = flags.begin();
295 vector<string>::const_iterator end = flags.end();
297 for (; cit != end; ++cit) {
299 if (!compare_ascii_no_case(*cit, "off"))
300 flag = ToolbarInfo::OFF;
301 else if (!compare_ascii_no_case(*cit, "on"))
302 flag = ToolbarInfo::ON;
303 else if (!compare_ascii_no_case(*cit, "math"))
304 flag = ToolbarInfo::MATH;
305 else if (!compare_ascii_no_case(*cit, "table"))
306 flag = ToolbarInfo::TABLE;
307 else if (!compare_ascii_no_case(*cit, "review"))
308 flag = ToolbarInfo::REVIEW;
309 else if (!compare_ascii_no_case(*cit, "top"))
310 flag = ToolbarInfo::TOP;
311 else if (!compare_ascii_no_case(*cit, "bottom"))
312 flag = ToolbarInfo::BOTTOM;
313 else if (!compare_ascii_no_case(*cit, "left"))
314 flag = ToolbarInfo::LEFT;
315 else if (!compare_ascii_no_case(*cit, "right"))
316 flag = ToolbarInfo::RIGHT;
318 lyxerr << "ToolbarBackend::readToolbarSettings: unrecognised token:`"
319 << *cit << '\'' << endl;
321 tcit->flags = static_cast<ToolbarInfo::Flags>(tcit->flags | flag);
324 usedtoolbars.push_back(*tcit);
329 ToolbarInfo const * ToolbarBackend::getDefinedToolbarInfo(string const & name) const
331 Toolbars::const_iterator it = find_if(toolbars.begin(), toolbars.end(), ToolbarNamesEqual(name));
332 if (it == toolbars.end())
338 ToolbarInfo * ToolbarBackend::getUsedToolbarInfo(string const &name)
340 Toolbars::iterator it = find_if(usedtoolbars.begin(), usedtoolbars.end(), ToolbarNamesEqual(name));
341 if (it == usedtoolbars.end())