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 "BufferParams.h"
19 #include "BufferView.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
26 #include "MetricsInfo.h"
27 #include "texstream.h"
29 #include "insets/InsetBox.h"
30 #include "insets/InsetBranch.h"
31 #include "insets/InsetCommand.h"
32 #include "insets/InsetERT.h"
33 #include "insets/InsetExternal.h"
34 #include "insets/InsetFloat.h"
35 #include "insets/InsetGraphics.h"
36 #include "insets/InsetIndex.h"
37 #include "insets/InsetLine.h"
38 #include "insets/InsetListings.h"
39 #include "insets/InsetNote.h"
40 #include "insets/InsetPhantom.h"
41 #include "insets/InsetSpace.h"
42 #include "insets/InsetTabular.h"
43 #include "insets/InsetVSpace.h"
44 #include "insets/InsetWrap.h"
46 #include "support/debug.h"
47 #include "support/lstrings.h"
49 #include "frontends/Application.h"
54 using namespace lyx::support;
59 // FIXME Would it now be possible to use the InsetCode in
60 // place of the mailer name and recover that information?
61 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p)
62 : Inset(buf), p_(p), broken_(false)
66 // The sole purpose of this copy constructor is to make sure
67 // that the mouse_hover_ map is not copied and remains empty.
68 InsetCommand::InsetCommand(InsetCommand const & rhs)
69 : Inset(rhs), p_(rhs.p_), broken_(false)
73 InsetCommand & InsetCommand::operator=(InsetCommand const & rhs)
78 Inset::operator=(rhs);
81 button_ = RenderButton();
88 InsetCommand::~InsetCommand()
90 if (p_.code() != NO_CODE)
91 hideDialogs(insetName(p_.code()), this);
93 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
94 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
95 for (; it != end; ++it)
97 it->first->clearLastInset(this);
101 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
103 button_.update(screenLabel(), editable() || clickable(*mi.base.bv, 0, 0),
104 inheritFont(), broken_);
105 button_.metrics(mi, dim);
109 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
112 mouse_hover_[bv] = mouse_hover;
117 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
119 button_.setRenderState(mouse_hover_[pi.base.bv]);
120 button_.draw(pi, x, y);
124 void InsetCommand::setParam(string const & name, docstring const & value)
130 docstring const & InsetCommand::getParam(string const & name) const
136 void InsetCommand::setParams(InsetCommandParams const & p)
143 void InsetCommand::latex(otexstream & os, OutputParams const & runparams_in) const
145 OutputParams runparams = runparams_in;
146 docstring command = getCommand(runparams);
147 if (buffer().params().use_minted
148 && prefixIs(command, from_ascii("\\lstlistoflistings")))
154 int InsetCommand::plaintext(odocstringstream & os,
155 OutputParams const &, size_t) const
157 docstring const str = "[" + buffer().B_("LaTeX Command: ")
158 + from_utf8(getCmdName()) + "]";
164 void InsetCommand::docbook(XMLStream &, OutputParams const &) const
170 void InsetCommand::validate(LaTeXFeatures & features) const
172 if (params().info().hasParam("literal")
173 && params()["literal"] == "true")
176 ParamInfo::const_iterator it = params().info().begin();
177 ParamInfo::const_iterator end = params().info().end();
178 for (; it != end; ++it) {
179 if (it->handling() == ParamInfo::HANDLING_LATEXIFY) {
180 docstring const text = params()[it->name()];
181 // Validate the contents (if we LaTeXify, specific
182 // macros might require packages)
183 for (pos_type i = 0; i < int(text.size()) ; ++i)
184 BufferEncodings::validate(text[i], features);
190 void InsetCommand::changeCmdName(string const & new_name)
192 string const & old_name = getCmdName();
193 if (old_name == new_name)
196 if (buffer().masterParams().track_changes) {
197 // With change tracking, we insert a new inset and
198 // delete the old one
199 InsetCommandParams p(p_.code());
201 p.setCmdName(new_name);
202 string const data = InsetCommand::params2string(p);
203 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
204 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
206 p_.setCmdName(new_name);
210 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
212 switch (cmd.action()) {
213 case LFUN_INSET_MODIFY: {
214 if (cmd.getArg(0) == "changetype") {
216 changeCmdName(cmd.getArg(1));
217 cur.forceBufferUpdate();
221 InsetCommandParams p(p_.code());
222 InsetCommand::string2params(to_utf8(cmd.argument()), p);
223 if (p.getCmdName().empty())
224 cur.noScreenUpdate();
227 if (buffer().masterParams().track_changes) {
228 // With change tracking, we insert a new inset and
229 // delete the old one
230 string const data = InsetCommand::params2string(p);
231 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
232 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
236 // FIXME We might also want to check here if this one is in the TOC.
237 // But I think most of those are labeled.
239 cur.forceBufferUpdate();
243 case LFUN_INSET_DIALOG_UPDATE: {
244 string const name = to_utf8(cmd.argument());
245 cur.bv().updateDialog(name, params2string(params()));
250 Inset::doDispatch(cur, cmd);
257 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
258 FuncStatus & status) const
260 switch (cmd.action()) {
262 case LFUN_ERT_INSERT:
263 status.setEnabled(false);
267 case LFUN_INSET_MODIFY:
268 if (cmd.getArg(0) == "changetype") {
269 string const newtype = cmd.getArg(1);
270 status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
271 status.setOnOff(newtype == p_.getCmdName());
273 status.setEnabled(true);
276 case LFUN_INSET_DIALOG_UPDATE:
277 status.setEnabled(true);
281 return Inset::getStatus(cur, cmd, status);
286 string InsetCommand::contextMenuName() const
288 return "context-" + insetName(p_.code());
292 bool InsetCommand::showInsetDialog(BufferView * bv) const
294 if (p_.code() != NO_CODE)
295 bv->showDialog(insetName(p_.code()), params2string(p_),
296 const_cast<InsetCommand *>(this));
301 bool InsetCommand::string2params(string const & data,
302 InsetCommandParams & params)
307 // This happens when inset-insert is called without argument except for the
309 // "inset-insert toc"
310 string const name = insetName(params.code());
313 istringstream dstream(data);
315 lex.setStream(dstream);
316 lex.setContext("InsetCommand::string2params");
317 lex >> name.c_str(); // check for name
318 lex >> "CommandInset";
324 string InsetCommand::params2string(InsetCommandParams const & params)
327 data << insetName(params.code()) << ' ';
329 data << "\\end_inset\n";
334 bool decodeInsetParam(string const & name, string & data,
335 Buffer const & buffer)
337 InsetCode const code = insetCode(name);
341 case INDEX_PRINT_CODE:
345 case NOMENCL_PRINT_CODE:
350 InsetCommandParams p(code);
351 data = InsetCommand::params2string(p);
355 // data is the include type: one of "include",
356 // "input", "verbatiminput" or "verbatiminput*"
358 // default type is requested
360 InsetCommandParams p(INCLUDE_CODE, data);
361 data = InsetCommand::params2string(p);
365 // \c data == "Boxed" || "Frameless" etc
366 InsetBoxParams p(data);
367 data = InsetBox::params2string(p);
372 data = InsetBranch::params2string(p);
376 InsetCommandParams p(CITE_CODE);
377 data = InsetCommand::params2string(p);
381 data = InsetERT::params2string(InsetCollapsible::Open);
384 case EXTERNAL_CODE: {
385 InsetExternalParams p;
386 data = InsetExternal::params2string(p, buffer);
391 data = InsetFloat::params2string(p);
396 data = InsetIndex::params2string(p);
399 case LISTINGS_CODE: {
400 InsetListingsParams p;
401 data = InsetListings::params2string(p);
404 case GRAPHICS_CODE: {
405 InsetGraphicsParams p;
406 data = InsetGraphics::params2string(p, buffer);
409 case MATH_SPACE_CODE: {
410 InsetSpaceParams p(true);
411 data = InsetSpace::params2string(p);
416 data = InsetNote::params2string(p);
420 InsetPhantomParams p;
421 data = InsetPhantom::params2string(p);
426 data = InsetSpace::params2string(p);
431 data = InsetVSpace::params2string(space);
436 data = InsetWrap::params2string(p);
441 } // end switch(code)