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/gettext.h"
48 #include "support/lstrings.h"
50 #include "frontends/Application.h"
55 using namespace lyx::support;
60 // FIXME Would it now be possible to use the InsetCode in
61 // place of the mailer name and recover that information?
62 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p)
63 : Inset(buf), p_(p), broken_(false)
67 // The sole purpose of this copy constructor is to make sure
68 // that the mouse_hover_ map is not copied and remains empty.
69 InsetCommand::InsetCommand(InsetCommand const & rhs)
70 : Inset(rhs), p_(rhs.p_), broken_(false)
74 InsetCommand & InsetCommand::operator=(InsetCommand const & rhs)
79 Inset::operator=(rhs);
82 button_ = RenderButton();
89 InsetCommand::~InsetCommand()
91 if (p_.code() != NO_CODE)
92 hideDialogs(insetName(p_.code()), this);
94 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
95 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
96 for (; it != end; ++it)
98 it->first->clearLastInset(this);
102 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
104 button_.update(screenLabel(), editable() || clickable(*mi.base.bv, 0, 0),
105 inheritFont(), broken_);
106 button_.metrics(mi, dim);
110 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
113 mouse_hover_[bv] = mouse_hover;
118 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
120 button_.setRenderState(mouse_hover_[pi.base.bv]);
121 button_.draw(pi, x, y);
125 void InsetCommand::setParam(string const & name, docstring const & value)
131 docstring const & InsetCommand::getParam(string const & name) const
137 void InsetCommand::setParams(InsetCommandParams const & p)
144 void InsetCommand::latex(otexstream & os, OutputParams const & runparams_in) const
146 OutputParams runparams = runparams_in;
147 docstring command = getCommand(runparams);
148 if (buffer().params().use_minted
149 && prefixIs(command, from_ascii("\\lstlistoflistings")))
155 int InsetCommand::plaintext(odocstringstream & os,
156 OutputParams const &, size_t) const
158 docstring const str = "[" + buffer().B_("LaTeX Command: ")
159 + from_utf8(getCmdName()) + "]";
165 int InsetCommand::docbook(odocstream &, OutputParams const &) const
171 void InsetCommand::validate(LaTeXFeatures & features) const
173 if (params().info().hasParam("literal")
174 && params()["literal"] == "true")
177 ParamInfo::const_iterator it = params().info().begin();
178 ParamInfo::const_iterator end = params().info().end();
179 for (; it != end; ++it) {
180 if (it->handling() == ParamInfo::HANDLING_LATEXIFY) {
181 docstring const text = params()[it->name()];
182 // Validate the contents (if we LaTeXify, specific
183 // macros might require packages)
184 for (pos_type i = 0; i < int(text.size()) ; ++i)
185 BufferEncodings::validate(text[i], features);
191 void InsetCommand::changeCmdName(string const & new_name)
193 string const old_name = getCmdName();
194 if (old_name == new_name)
197 if (buffer().masterParams().track_changes) {
198 // With change tracking, we insert a new inset and
199 // delete the old one
200 InsetCommandParams p(p_.code());
202 p.setCmdName(new_name);
203 string const data = InsetCommand::params2string(p);
204 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
205 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
207 p_.setCmdName(new_name);
211 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
213 switch (cmd.action()) {
214 case LFUN_INSET_MODIFY: {
215 if (cmd.getArg(0) == "changetype") {
217 changeCmdName(cmd.getArg(1));
218 cur.forceBufferUpdate();
222 InsetCommandParams p(p_.code());
223 InsetCommand::string2params(to_utf8(cmd.argument()), p);
224 if (p.getCmdName().empty())
225 cur.noScreenUpdate();
228 if (buffer().masterParams().track_changes) {
229 // With change tracking, we insert a new inset and
230 // delete the old one
231 string const data = InsetCommand::params2string(p);
232 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
233 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
237 // FIXME We might also want to check here if this one is in the TOC.
238 // But I think most of those are labeled.
240 cur.forceBufferUpdate();
244 case LFUN_INSET_DIALOG_UPDATE: {
245 string const name = to_utf8(cmd.argument());
246 cur.bv().updateDialog(name, params2string(params()));
251 Inset::doDispatch(cur, cmd);
258 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
259 FuncStatus & status) const
261 switch (cmd.action()) {
263 case LFUN_ERT_INSERT:
264 status.setEnabled(false);
268 case LFUN_INSET_MODIFY:
269 if (cmd.getArg(0) == "changetype") {
270 string const newtype = cmd.getArg(1);
271 status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
272 status.setOnOff(newtype == p_.getCmdName());
274 status.setEnabled(true);
277 case LFUN_INSET_DIALOG_UPDATE:
278 status.setEnabled(true);
282 return Inset::getStatus(cur, cmd, status);
287 string InsetCommand::contextMenuName() const
289 return "context-" + insetName(p_.code());
293 bool InsetCommand::showInsetDialog(BufferView * bv) const
295 if (p_.code() != NO_CODE)
296 bv->showDialog(insetName(p_.code()), params2string(p_),
297 const_cast<InsetCommand *>(this));
302 bool InsetCommand::string2params(string const & data,
303 InsetCommandParams & params)
308 // This happens when inset-insert is called without argument except for the
310 // "inset-insert toc"
311 string const name = insetName(params.code());
314 istringstream dstream(data);
316 lex.setStream(dstream);
317 lex.setContext("InsetCommand::string2params");
318 lex >> name.c_str(); // check for name
319 lex >> "CommandInset";
325 string InsetCommand::params2string(InsetCommandParams const & params)
328 data << insetName(params.code()) << ' ';
330 data << "\\end_inset\n";
335 bool decodeInsetParam(string const & name, string & data,
336 Buffer const & buffer)
338 InsetCode const code = insetCode(name);
342 case INDEX_PRINT_CODE:
346 case NOMENCL_PRINT_CODE:
351 InsetCommandParams p(code);
352 data = InsetCommand::params2string(p);
356 // data is the include type: one of "include",
357 // "input", "verbatiminput" or "verbatiminput*"
359 // default type is requested
361 InsetCommandParams p(INCLUDE_CODE, data);
362 data = InsetCommand::params2string(p);
366 // \c data == "Boxed" || "Frameless" etc
367 InsetBoxParams p(data);
368 data = InsetBox::params2string(p);
373 data = InsetBranch::params2string(p);
377 InsetCommandParams p(CITE_CODE);
378 data = InsetCommand::params2string(p);
382 data = InsetERT::params2string(InsetCollapsible::Open);
385 case EXTERNAL_CODE: {
386 InsetExternalParams p;
387 data = InsetExternal::params2string(p, buffer);
392 data = InsetFloat::params2string(p);
397 data = InsetIndex::params2string(p);
400 case LISTINGS_CODE: {
401 InsetListingsParams p;
402 data = InsetListings::params2string(p);
405 case GRAPHICS_CODE: {
406 InsetGraphicsParams p;
407 data = InsetGraphics::params2string(p, buffer);
410 case MATH_SPACE_CODE: {
411 InsetSpaceParams p(true);
412 data = InsetSpace::params2string(p);
417 data = InsetNote::params2string(p);
421 InsetPhantomParams p;
422 data = InsetPhantom::params2string(p);
427 data = InsetSpace::params2string(p);
432 data = InsetVSpace::params2string(space);
437 data = InsetWrap::params2string(p);
442 } // end switch(code)