2 * \file ToolbarBackend.C
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>
24 #include "frontends/controllers/ControlMath.h"
28 using support::compare_ascii_no_case;
29 using support::getVectorFromString;
40 class ToolbarNamesEqual : public std::unary_function<ToolbarInfo, bool> {
42 ToolbarNamesEqual(string const & name)
44 bool operator()(ToolbarInfo const & tb) const
46 return tb.name == name_;
55 ToolbarBackend toolbarbackend;
58 ToolbarItem::ToolbarItem(Type type, FuncRequest const & func, docstring const & label)
59 : type_(type), func_(func), label_(label)
64 ToolbarItem::ToolbarItem(Type type, string const & name, docstring const & label)
65 : type_(type), name_(name), label_(label)
70 ToolbarItem::~ToolbarItem()
74 void ToolbarInfo::add(ToolbarItem const & item)
76 items.push_back(item);
77 items.back().func_.origin = FuncRequest::TOOLBAR;
81 ToolbarInfo & ToolbarInfo::read(LyXLex & lex)
95 struct keyword_item toolTags[TO_LAST - 1] = {
96 { "end", TO_ENDTOOLBAR },
97 { "iconpalette", TO_ICONPALETTE },
98 { "item", TO_COMMAND },
99 { "layouts", TO_LAYOUTS },
100 { "minibuffer", TO_MINIBUFFER },
101 { "popupmenu", TO_POPUPMENU },
102 { "separator", TO_SEPARATOR },
103 { "tableinsert", TO_TABLEINSERT }
107 if (compare_ascii_no_case(lex.getString(), "toolbar")) {
108 lyxerr << "ToolbarInfo::read: ERROR wrong token:`"
109 << lex.getString() << '\'' << endl;
113 name = lex.getString();
116 gui_name = lex.getString();
118 // FIXME what to do here?
120 lyxerr << "ToolbarInfo::read: Malformed toolbar "
121 "description " << lex.getString() << endl;
127 lex.pushTable(toolTags, TO_LAST - 1);
129 if (lyxerr.debugging(Debug::PARSER))
130 lex.printTable(lyxerr);
132 while (lex.isOK() && !quit) {
135 if (lex.next(true)) {
136 docstring const tooltip = translateIfPossible(lex.getDocString());
138 string const func_arg = lex.getString();
139 LYXERR(Debug::PARSER)
140 << "ToolbarInfo::read TO_COMMAND func: `"
141 << func_arg << '\'' << endl;
144 lyxaction.lookupFunc(func_arg);
145 add(ToolbarItem(ToolbarItem::COMMAND, func, tooltip));
150 add(ToolbarItem(ToolbarItem::MINIBUFFER,
151 FuncRequest(kb_action(ToolbarItem::MINIBUFFER))));
155 add(ToolbarItem(ToolbarItem::SEPARATOR,
156 FuncRequest(kb_action(ToolbarItem::SEPARATOR))));
160 if (lex.next(true)) {
161 string const name = lex.getString();
163 docstring const label = lex.getDocString();
164 add(ToolbarItem(ToolbarItem::POPUPMENU, name, label));
169 if (lex.next(true)) {
170 string const name = lex.getString();
172 docstring const label = lex.getDocString();
173 add(ToolbarItem(ToolbarItem::ICONPALETTE, name, label));
178 add(ToolbarItem(ToolbarItem::LAYOUTS,
179 FuncRequest(kb_action(ToolbarItem::LAYOUTS))));
183 add(ToolbarItem(ToolbarItem::TABLEINSERT,
184 FuncRequest(kb_action(ToolbarItem::TABLEINSERT))));
192 lex.printError("ToolbarInfo::read: "
193 "Unknown toolbar tag: `$$Token'");
205 ToolbarBackend::ToolbarBackend()
210 void ToolbarBackend::readToolbars(LyXLex & lex)
218 struct keyword_item toolTags[TO_LAST - 1] = {
219 { "end", TO_ENDTOOLBARSET },
220 { "toolbar", TO_TOOLBAR }
224 if (compare_ascii_no_case(lex.getString(), "toolbarset")) {
225 lyxerr << "ToolbarBackend::readToolbars: ERROR wrong token:`"
226 << lex.getString() << '\'' << endl;
229 lex.pushTable(toolTags, TO_LAST - 1);
231 if (lyxerr.debugging(Debug::PARSER))
232 lex.printTable(lyxerr);
235 while (lex.isOK() && !quit) {
240 toolbars.push_back(tb);
243 case TO_ENDTOOLBARSET:
247 lex.printError("ToolbarBackend::readToolbars: "
248 "Unknown toolbar tag: `$$Token'");
257 void ToolbarBackend::readToolbarSettings(LyXLex & lex)
260 if (compare_ascii_no_case(lex.getString(), "toolbars")) {
261 lyxerr << "ToolbarBackend::readToolbarSettings: ERROR wrong token:`"
262 << lex.getString() << '\'' << endl;
268 string name = lex.getString();
271 if (!compare_ascii_no_case(name, "end"))
274 Toolbars::iterator tcit = toolbars.begin();
275 Toolbars::iterator tend = toolbars.end();
276 for (; tcit != tend; ++tcit) {
277 if (tcit->name == name)
282 lyxerr << "ToolbarBackend: undefined toolbar "
287 tcit->flags = static_cast<ToolbarInfo::Flags>(0);
288 string flagstr = lex.getString();
290 vector<string> flags = getVectorFromString(flagstr);
292 vector<string>::const_iterator cit = flags.begin();
293 vector<string>::const_iterator end = flags.end();
295 for (; cit != end; ++cit) {
297 if (!compare_ascii_no_case(*cit, "off"))
298 flag = ToolbarInfo::OFF;
299 else if (!compare_ascii_no_case(*cit, "on"))
300 flag = ToolbarInfo::ON;
301 else if (!compare_ascii_no_case(*cit, "math"))
302 flag = ToolbarInfo::MATH;
303 else if (!compare_ascii_no_case(*cit, "table"))
304 flag = ToolbarInfo::TABLE;
305 else if (!compare_ascii_no_case(*cit, "review"))
306 flag = ToolbarInfo::REVIEW;
307 else if (!compare_ascii_no_case(*cit, "top"))
308 flag = ToolbarInfo::TOP;
309 else if (!compare_ascii_no_case(*cit, "bottom"))
310 flag = ToolbarInfo::BOTTOM;
311 else if (!compare_ascii_no_case(*cit, "left"))
312 flag = ToolbarInfo::LEFT;
313 else if (!compare_ascii_no_case(*cit, "right"))
314 flag = ToolbarInfo::RIGHT;
316 lyxerr << "ToolbarBackend::readToolbarSettings: unrecognised token:`"
317 << *cit << '\'' << endl;
319 tcit->flags = static_cast<ToolbarInfo::Flags>(tcit->flags | flag);
322 usedtoolbars.push_back(*tcit);
327 ToolbarInfo const & ToolbarBackend::getToolbar(string const & name) const
329 Toolbars::const_iterator cit = find_if(toolbars.begin(), toolbars.end(), ToolbarNamesEqual(name));
330 if (cit == toolbars.end())
331 lyxerr << "No toolbar named " << name << endl;
332 BOOST_ASSERT(cit != toolbars.end());
337 ToolbarInfo & ToolbarBackend::getToolbar(string const & name)
339 Toolbars::iterator it = find_if(toolbars.begin(), toolbars.end(), ToolbarNamesEqual(name));
340 if (it == toolbars.end())
341 lyxerr << "No toolbar named " << name << endl;
342 BOOST_ASSERT(it != toolbars.end());