2 * \file InsetCommand.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
14 #include "InsetCommand.h"
17 #include "BufferEncodings.h"
18 #include "BufferView.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
24 #include "MetricsInfo.h"
25 #include "texstream.h"
27 #include "insets/InsetBox.h"
28 #include "insets/InsetBranch.h"
29 #include "insets/InsetCommand.h"
30 #include "insets/InsetERT.h"
31 #include "insets/InsetExternal.h"
32 #include "insets/InsetFloat.h"
33 #include "insets/InsetGraphics.h"
34 #include "insets/InsetIndex.h"
35 #include "insets/InsetLine.h"
36 #include "insets/InsetListings.h"
37 #include "insets/InsetNote.h"
38 #include "insets/InsetPhantom.h"
39 #include "insets/InsetSpace.h"
40 #include "insets/InsetTabular.h"
41 #include "insets/InsetVSpace.h"
42 #include "insets/InsetWrap.h"
44 #include "support/debug.h"
45 #include "support/gettext.h"
47 #include "frontends/Application.h"
56 // FIXME Would it now be possible to use the InsetCode in
57 // place of the mailer name and recover that information?
58 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p)
63 // The sole purpose of this copy constructor is to make sure
64 // that the mouse_hover_ map is not copied and remains empty.
65 InsetCommand::InsetCommand(InsetCommand const & rhs)
66 : Inset(rhs), p_(rhs.p_)
70 InsetCommand & InsetCommand::operator=(InsetCommand const & rhs)
75 Inset::operator=(rhs);
78 button_ = RenderButton();
84 InsetCommand::~InsetCommand()
86 if (p_.code() != NO_CODE)
87 hideDialogs(insetName(p_.code()), this);
89 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
90 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
91 for (; it != end; ++it)
93 it->first->clearLastInset(this);
97 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
99 button_.update(screenLabel(), editable() || clickable(*mi.base.bv, 0, 0),
101 button_.metrics(mi, dim);
105 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
108 mouse_hover_[bv] = mouse_hover;
113 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
115 button_.setRenderState(mouse_hover_[pi.base.bv]);
116 button_.draw(pi, x, y);
120 void InsetCommand::setParam(string const & name, docstring const & value)
126 docstring const & InsetCommand::getParam(string const & name) const
132 void InsetCommand::setParams(InsetCommandParams const & p)
139 void InsetCommand::latex(otexstream & os, OutputParams const & runparams_in) const
141 OutputParams runparams = runparams_in;
142 os << getCommand(runparams);
146 int InsetCommand::plaintext(odocstringstream & os,
147 OutputParams const &, size_t) const
149 docstring const str = "[" + buffer().B_("LaTeX Command: ")
150 + from_utf8(getCmdName()) + "]";
156 int InsetCommand::docbook(odocstream &, OutputParams const &) const
162 void InsetCommand::validate(LaTeXFeatures & features) const
164 if (params().info().hasParam("literal")
165 && params()["literal"] == "true")
168 ParamInfo::const_iterator it = params().info().begin();
169 ParamInfo::const_iterator end = params().info().end();
170 for (; it != end; ++it) {
171 if (it->handling() == ParamInfo::HANDLING_LATEXIFY) {
172 docstring const text = params()[it->name()];
173 // Validate the contents (if we LaTeXify, specific
174 // macros might require packages)
175 for (pos_type i = 0; i < int(text.size()) ; ++i)
176 BufferEncodings::validate(text[i], features);
182 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
184 switch (cmd.action()) {
185 case LFUN_INSET_MODIFY: {
186 if (cmd.getArg(0) == "changetype") {
188 p_.setCmdName(cmd.getArg(1));
189 cur.forceBufferUpdate();
193 InsetCommandParams p(p_.code());
194 InsetCommand::string2params(to_utf8(cmd.argument()), p);
195 if (p.getCmdName().empty())
196 cur.noScreenUpdate();
201 // FIXME We might also want to check here if this one is in the TOC.
202 // But I think most of those are labeled.
204 cur.forceBufferUpdate();
208 case LFUN_INSET_DIALOG_UPDATE: {
209 string const name = to_utf8(cmd.argument());
210 cur.bv().updateDialog(name, params2string(params()));
215 Inset::doDispatch(cur, cmd);
222 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
223 FuncStatus & status) const
225 switch (cmd.action()) {
227 case LFUN_ERT_INSERT:
228 status.setEnabled(false);
232 case LFUN_INSET_MODIFY:
233 if (cmd.getArg(0) == "changetype") {
234 string const newtype = cmd.getArg(1);
235 status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
236 status.setOnOff(newtype == p_.getCmdName());
238 status.setEnabled(true);
241 case LFUN_INSET_DIALOG_UPDATE:
242 status.setEnabled(true);
246 return Inset::getStatus(cur, cmd, status);
251 string InsetCommand::contextMenuName() const
253 return "context-" + insetName(p_.code());
257 bool InsetCommand::showInsetDialog(BufferView * bv) const
259 if (p_.code() != NO_CODE)
260 bv->showDialog(insetName(p_.code()), params2string(p_),
261 const_cast<InsetCommand *>(this));
266 bool InsetCommand::string2params(string const & data,
267 InsetCommandParams & params)
272 // This happens when inset-insert is called without argument except for the
274 // "inset-insert toc"
275 string const name = insetName(params.code());
278 istringstream dstream(data);
280 lex.setStream(dstream);
281 lex.setContext("InsetCommand::string2params");
282 lex >> name.c_str(); // check for name
283 lex >> "CommandInset";
289 string InsetCommand::params2string(InsetCommandParams const & params)
292 data << insetName(params.code()) << ' ';
294 data << "\\end_inset\n";
299 bool decodeInsetParam(string const & name, string & data,
300 Buffer const & buffer)
302 InsetCode const code = insetCode(name);
306 case INDEX_PRINT_CODE:
310 case NOMENCL_PRINT_CODE:
313 case HYPERLINK_CODE: {
314 InsetCommandParams p(code);
315 data = InsetCommand::params2string(p);
319 // data is the include type: one of "include",
320 // "input", "verbatiminput" or "verbatiminput*"
322 // default type is requested
324 InsetCommandParams p(INCLUDE_CODE, data);
325 data = InsetCommand::params2string(p);
329 // \c data == "Boxed" || "Frameless" etc
330 InsetBoxParams p(data);
331 data = InsetBox::params2string(p);
336 data = InsetBranch::params2string(p);
340 InsetCommandParams p(CITE_CODE);
341 data = InsetCommand::params2string(p);
345 data = InsetERT::params2string(InsetCollapsable::Open);
348 case EXTERNAL_CODE: {
349 InsetExternalParams p;
350 data = InsetExternal::params2string(p, buffer);
355 data = InsetFloat::params2string(p);
360 data = InsetIndex::params2string(p);
363 case LISTINGS_CODE: {
364 InsetListingsParams p;
365 data = InsetListings::params2string(p);
368 case GRAPHICS_CODE: {
369 InsetGraphicsParams p;
370 data = InsetGraphics::params2string(p, buffer);
373 case MATH_SPACE_CODE: {
374 InsetSpaceParams p(true);
375 data = InsetSpace::params2string(p);
380 data = InsetNote::params2string(p);
384 InsetPhantomParams p;
385 data = InsetPhantom::params2string(p);
390 data = InsetSpace::params2string(p);
395 data = InsetVSpace::params2string(space);
400 data = InsetWrap::params2string(p);
405 } // end switch(code)