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 "BufferView.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
23 #include "MetricsInfo.h"
24 #include "texstream.h"
26 #include "insets/InsetBox.h"
27 #include "insets/InsetBranch.h"
28 #include "insets/InsetCommand.h"
29 #include "insets/InsetERT.h"
30 #include "insets/InsetExternal.h"
31 #include "insets/InsetFloat.h"
32 #include "insets/InsetGraphics.h"
33 #include "insets/InsetIndex.h"
34 #include "insets/InsetLine.h"
35 #include "insets/InsetListings.h"
36 #include "insets/InsetNote.h"
37 #include "insets/InsetPhantom.h"
38 #include "insets/InsetSpace.h"
39 #include "insets/InsetTabular.h"
40 #include "insets/InsetVSpace.h"
41 #include "insets/InsetWrap.h"
43 #include "support/debug.h"
44 #include "support/gettext.h"
46 #include "frontends/Application.h"
55 // FIXME Would it now be possible to use the InsetCode in
56 // place of the mailer name and recover that information?
57 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p)
62 // The sole purpose of this copy constructor is to make sure
63 // that the mouse_hover_ map is not copied and remains empty.
64 InsetCommand::InsetCommand(InsetCommand const & rhs)
65 : Inset(rhs), p_(rhs.p_)
69 InsetCommand & InsetCommand::operator=(InsetCommand const & rhs)
74 Inset::operator=(rhs);
77 button_ = RenderButton();
83 InsetCommand::~InsetCommand()
85 if (p_.code() != NO_CODE)
86 hideDialogs(insetName(p_.code()), this);
88 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
89 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
90 for (; it != end; ++it)
92 it->first->clearLastInset(this);
96 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
98 button_.update(screenLabel(), editable() || clickable(*mi.base.bv, 0, 0));
99 button_.metrics(mi, dim);
103 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
106 mouse_hover_[bv] = mouse_hover;
111 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
113 button_.setRenderState(mouse_hover_[pi.base.bv]);
114 button_.draw(pi, x, y);
118 void InsetCommand::setParam(string const & name, docstring const & value)
124 docstring const & InsetCommand::getParam(string const & name) const
130 void InsetCommand::setParams(InsetCommandParams const & p)
137 void InsetCommand::latex(otexstream & os, OutputParams const & runparams_in) const
139 OutputParams runparams = runparams_in;
140 os << getCommand(runparams);
144 int InsetCommand::plaintext(odocstringstream & os,
145 OutputParams const &, size_t) const
147 docstring const str = "[" + buffer().B_("LaTeX Command: ")
148 + from_utf8(getCmdName()) + "]";
154 int InsetCommand::docbook(odocstream &, OutputParams const &) const
160 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
162 switch (cmd.action()) {
163 case LFUN_INSET_MODIFY: {
164 if (cmd.getArg(0) == "changetype") {
166 p_.setCmdName(cmd.getArg(1));
167 cur.forceBufferUpdate();
171 InsetCommandParams p(p_.code());
172 InsetCommand::string2params(to_utf8(cmd.argument()), p);
173 if (p.getCmdName().empty())
174 cur.noScreenUpdate();
179 // FIXME We might also want to check here if this one is in the TOC.
180 // But I think most of those are labeled.
182 cur.forceBufferUpdate();
186 case LFUN_INSET_DIALOG_UPDATE: {
187 string const name = to_utf8(cmd.argument());
188 cur.bv().updateDialog(name, params2string(params()));
193 Inset::doDispatch(cur, cmd);
200 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
201 FuncStatus & status) const
203 switch (cmd.action()) {
205 case LFUN_ERT_INSERT:
206 status.setEnabled(false);
210 case LFUN_INSET_MODIFY:
211 if (cmd.getArg(0) == "changetype") {
212 string const newtype = cmd.getArg(1);
213 status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
214 status.setOnOff(newtype == p_.getCmdName());
216 status.setEnabled(true);
219 case LFUN_INSET_DIALOG_UPDATE:
220 status.setEnabled(true);
224 return Inset::getStatus(cur, cmd, status);
229 string InsetCommand::contextMenuName() const
231 return "context-" + insetName(p_.code());
235 bool InsetCommand::showInsetDialog(BufferView * bv) const
237 if (p_.code() != NO_CODE)
238 bv->showDialog(insetName(p_.code()), params2string(p_),
239 const_cast<InsetCommand *>(this));
244 bool InsetCommand::string2params(string const & data,
245 InsetCommandParams & params)
250 // This happens when inset-insert is called without argument except for the
252 // "inset-insert toc"
253 string const name = insetName(params.code());
256 istringstream dstream(data);
258 lex.setStream(dstream);
259 lex.setContext("InsetCommand::string2params");
260 lex >> name.c_str(); // check for name
261 lex >> "CommandInset";
267 string InsetCommand::params2string(InsetCommandParams const & params)
270 data << insetName(params.code()) << ' ';
272 data << "\\end_inset\n";
277 bool decodeInsetParam(string const & name, string & data,
278 Buffer const & buffer)
280 InsetCode const code = insetCode(name);
284 case INDEX_PRINT_CODE:
288 case NOMENCL_PRINT_CODE:
291 case HYPERLINK_CODE: {
292 InsetCommandParams p(code);
293 data = InsetCommand::params2string(p);
297 // data is the include type: one of "include",
298 // "input", "verbatiminput" or "verbatiminput*"
300 // default type is requested
302 InsetCommandParams p(INCLUDE_CODE, data);
303 data = InsetCommand::params2string(p);
307 // \c data == "Boxed" || "Frameless" etc
308 InsetBoxParams p(data);
309 data = InsetBox::params2string(p);
314 data = InsetBranch::params2string(p);
318 InsetCommandParams p(CITE_CODE);
319 data = InsetCommand::params2string(p);
323 data = InsetERT::params2string(InsetCollapsable::Open);
326 case EXTERNAL_CODE: {
327 InsetExternalParams p;
328 data = InsetExternal::params2string(p, buffer);
333 data = InsetFloat::params2string(p);
338 data = InsetIndex::params2string(p);
341 case LISTINGS_CODE: {
342 InsetListingsParams p;
343 data = InsetListings::params2string(p);
346 case GRAPHICS_CODE: {
347 InsetGraphicsParams p;
348 data = InsetGraphics::params2string(p, buffer);
351 case MATH_SPACE_CODE: {
352 InsetSpaceParams p(true);
353 data = InsetSpace::params2string(p);
358 data = InsetNote::params2string(p);
362 InsetPhantomParams p;
363 data = InsetPhantom::params2string(p);
368 data = InsetSpace::params2string(p);
373 data = InsetVSpace::params2string(space);
378 data = InsetWrap::params2string(p);
383 } // end switch(code)