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"
17 #include "support/debug.h"
18 #include "support/gettext.h"
19 #include "LyXAction.h"
20 #include "support/lstrings.h"
22 #include <boost/bind.hpp>
26 using namespace lyx::support;
32 class ToolbarNamesEqual
35 ToolbarNamesEqual(string const & name) : name_(name) {}
36 bool operator()(ToolbarInfo const & tbinfo) const
38 return tbinfo.name == name_;
47 ToolbarBackend toolbarbackend;
50 ToolbarItem::ToolbarItem(Type type, FuncRequest const & func, docstring const & label)
51 : type_(type), func_(func), label_(label)
56 ToolbarItem::ToolbarItem(Type type, string const & name, docstring const & label)
57 : type_(type), label_(label), name_(name)
62 ToolbarItem::~ToolbarItem()
66 void ToolbarInfo::add(ToolbarItem const & item)
68 items.push_back(item);
69 items.back().func_.origin = FuncRequest::TOOLBAR;
73 ToolbarInfo & ToolbarInfo::read(Lexer & lex)
87 struct keyword_item toolTags[TO_LAST - 1] = {
88 { "end", TO_ENDTOOLBAR },
89 { "iconpalette", TO_ICONPALETTE },
90 { "item", TO_COMMAND },
91 { "layouts", TO_LAYOUTS },
92 { "minibuffer", TO_MINIBUFFER },
93 { "popupmenu", TO_POPUPMENU },
94 { "separator", TO_SEPARATOR },
95 { "tableinsert", TO_TABLEINSERT }
99 if (compare_ascii_no_case(lex.getString(), "toolbar")) {
100 LYXERR0("ToolbarInfo::read: ERROR wrong token:`"
101 << lex.getString() << '\'');
105 name = lex.getString();
108 gui_name = lex.getString();
110 // FIXME what to do here?
112 LYXERR0("ToolbarInfo::read: Malformed toolbar "
113 "description " << lex.getString());
119 lex.pushTable(toolTags, TO_LAST - 1);
121 if (lyxerr.debugging(Debug::PARSER))
122 lex.printTable(lyxerr);
124 while (lex.isOK() && !quit) {
127 if (lex.next(true)) {
128 docstring const tooltip = translateIfPossible(lex.getDocString());
130 string const func_arg = lex.getString();
131 LYXERR(Debug::PARSER, "ToolbarInfo::read TO_COMMAND func: `"
132 << func_arg << '\'');
135 lyxaction.lookupFunc(func_arg);
136 add(ToolbarItem(ToolbarItem::COMMAND, func, tooltip));
141 add(ToolbarItem(ToolbarItem::MINIBUFFER,
142 FuncRequest(kb_action(ToolbarItem::MINIBUFFER))));
146 add(ToolbarItem(ToolbarItem::SEPARATOR,
147 FuncRequest(kb_action(ToolbarItem::SEPARATOR))));
151 if (lex.next(true)) {
152 string const name = lex.getString();
154 docstring const label = lex.getDocString();
155 add(ToolbarItem(ToolbarItem::POPUPMENU, name, label));
160 if (lex.next(true)) {
161 string const name = lex.getString();
163 docstring const label = lex.getDocString();
164 add(ToolbarItem(ToolbarItem::ICONPALETTE, name, label));
169 add(ToolbarItem(ToolbarItem::LAYOUTS,
170 FuncRequest(kb_action(ToolbarItem::LAYOUTS))));
174 if (lex.next(true)) {
175 docstring const tooltip = lex.getDocString();
176 add(ToolbarItem(ToolbarItem::TABLEINSERT,
177 FuncRequest(kb_action(ToolbarItem::TABLEINSERT)), tooltip));
186 lex.printError("ToolbarInfo::read: "
187 "Unknown toolbar tag: `$$Token'");
199 ToolbarBackend::ToolbarBackend()
204 void ToolbarBackend::readToolbars(Lexer & lex)
212 struct keyword_item toolTags[TO_LAST - 1] = {
213 { "end", TO_ENDTOOLBARSET },
214 { "toolbar", TO_TOOLBAR }
218 if (compare_ascii_no_case(lex.getString(), "toolbarset")) {
219 LYXERR0("ToolbarBackend::readToolbars: ERROR wrong token:`"
220 << lex.getString() << '\'');
223 lex.pushTable(toolTags, TO_LAST - 1);
225 if (lyxerr.debugging(Debug::PARSER))
226 lex.printTable(lyxerr);
229 while (lex.isOK() && !quit) {
234 toolbars.push_back(tbinfo);
237 case TO_ENDTOOLBARSET:
241 lex.printError("ToolbarBackend::readToolbars: "
242 "Unknown toolbar tag: `$$Token'");
251 void ToolbarBackend::readToolbarSettings(Lexer & lex)
254 if (compare_ascii_no_case(lex.getString(), "toolbars")) {
255 LYXERR0("ToolbarBackend::readToolbarSettings: ERROR wrong token:`"
256 << lex.getString() << '\'');
262 string name = lex.getString();
265 if (!compare_ascii_no_case(name, "end"))
268 Toolbars::iterator tcit = toolbars.begin();
269 Toolbars::iterator tend = toolbars.end();
270 for (; tcit != tend; ++tcit) {
271 if (tcit->name == name)
276 LYXERR0("ToolbarBackend: undefined toolbar " << name);
280 tcit->flags = static_cast<ToolbarInfo::Flags>(0);
281 string flagstr = lex.getString();
283 vector<string> flags = getVectorFromString(flagstr);
285 vector<string>::const_iterator cit = flags.begin();
286 vector<string>::const_iterator end = flags.end();
288 for (; cit != end; ++cit) {
290 if (!compare_ascii_no_case(*cit, "off"))
291 flag = ToolbarInfo::OFF;
292 else if (!compare_ascii_no_case(*cit, "on"))
293 flag = ToolbarInfo::ON;
294 else if (!compare_ascii_no_case(*cit, "math"))
295 flag = ToolbarInfo::MATH;
296 else if (!compare_ascii_no_case(*cit, "table"))
297 flag = ToolbarInfo::TABLE;
298 else if (!compare_ascii_no_case(*cit, "mathmacrotemplate"))
299 flag = ToolbarInfo::MATHMACROTEMPLATE;
300 else if (!compare_ascii_no_case(*cit, "review"))
301 flag = ToolbarInfo::REVIEW;
302 else if (!compare_ascii_no_case(*cit, "top"))
303 flag = ToolbarInfo::TOP;
304 else if (!compare_ascii_no_case(*cit, "bottom"))
305 flag = ToolbarInfo::BOTTOM;
306 else if (!compare_ascii_no_case(*cit, "left"))
307 flag = ToolbarInfo::LEFT;
308 else if (!compare_ascii_no_case(*cit, "right"))
309 flag = ToolbarInfo::RIGHT;
312 "ToolbarBackend::readToolbarSettings: unrecognised token:`"
315 tcit->flags = static_cast<ToolbarInfo::Flags>(tcit->flags | flag);
318 usedtoolbars.push_back(*tcit);
323 ToolbarInfo const * ToolbarBackend::getDefinedToolbarInfo(string const & name) const
325 Toolbars::const_iterator it = find_if(toolbars.begin(), toolbars.end(), ToolbarNamesEqual(name));
326 if (it == toolbars.end())
332 ToolbarInfo * ToolbarBackend::getUsedToolbarInfo(string const &name)
334 Toolbars::iterator it = find_if(usedtoolbars.begin(), usedtoolbars.end(), ToolbarNamesEqual(name));
335 if (it == usedtoolbars.end())