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"
25 #include "insets/InsetBox.h"
26 #include "insets/InsetBranch.h"
27 #include "insets/InsetCommand.h"
28 #include "insets/InsetERT.h"
29 #include "insets/InsetExternal.h"
30 #include "insets/InsetFloat.h"
31 #include "insets/InsetGraphics.h"
32 #include "insets/InsetLine.h"
33 #include "insets/InsetListings.h"
34 #include "insets/InsetNote.h"
35 #include "insets/InsetPhantom.h"
36 #include "insets/InsetSpace.h"
37 #include "insets/InsetTabular.h"
38 #include "insets/InsetVSpace.h"
39 #include "insets/InsetWrap.h"
41 #include "support/debug.h"
42 #include "support/gettext.h"
44 #include "frontends/Application.h"
53 // FIXME Would it now be possible to use the InsetCode in
54 // place of the mailer name and recover that information?
55 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p)
60 // The sole purpose of this copy constructor is to make sure
61 // that the mouse_hover_ map is not copied and remains empty.
62 InsetCommand::InsetCommand(InsetCommand const & rhs)
63 : Inset(rhs), p_(rhs.p_)
67 InsetCommand::~InsetCommand()
69 if (p_.code() != NO_CODE)
70 hideDialogs(insetName(p_.code()), this);
72 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
73 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
74 for (; it != end; ++it)
76 it->first->clearLastInset(this);
80 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
82 button_.update(screenLabel(), editable() || hasSettings());
83 button_.metrics(mi, dim);
87 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
90 mouse_hover_[bv] = mouse_hover;
95 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
97 button_.setRenderState(mouse_hover_[pi.base.bv]);
98 button_.draw(pi, x, y);
102 void InsetCommand::setParam(string const & name, docstring const & value)
108 docstring const & InsetCommand::getParam(string const & name) const
114 void InsetCommand::setParams(InsetCommandParams const & p)
121 int InsetCommand::latex(odocstream & os, OutputParams const & runparams_in) const
123 OutputParams runparams = runparams_in;
124 os << getCommand(runparams);
129 int InsetCommand::plaintext(odocstream & os, OutputParams const &) const
131 docstring const str = "[" + buffer().B_("LaTeX Command: ")
132 + from_utf8(getCmdName()) + "]";
138 int InsetCommand::docbook(odocstream &, OutputParams const &) const
144 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
146 switch (cmd.action()) {
147 case LFUN_INSET_MODIFY: {
148 if (cmd.getArg(0) == "changetype") {
150 p_.setCmdName(cmd.getArg(1));
151 cur.forceBufferUpdate();
155 InsetCommandParams p(p_.code());
156 InsetCommand::string2params(to_utf8(cmd.argument()), p);
157 if (p.getCmdName().empty())
158 cur.noScreenUpdate();
163 // FIXME We might also want to check here if this one is in the TOC.
164 // But I think most of those are labeled.
166 cur.forceBufferUpdate();
170 case LFUN_INSET_DIALOG_UPDATE: {
171 string const name = to_utf8(cmd.argument());
172 cur.bv().updateDialog(name, params2string(params()));
177 Inset::doDispatch(cur, cmd);
184 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
185 FuncStatus & status) const
187 switch (cmd.action()) {
189 case LFUN_ERT_INSERT:
190 status.setEnabled(false);
194 case LFUN_INSET_MODIFY:
195 if (cmd.getArg(0) == "changetype") {
196 string const newtype = cmd.getArg(1);
197 status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
198 status.setOnOff(newtype == p_.getCmdName());
200 status.setEnabled(true);
203 case LFUN_INSET_DIALOG_UPDATE:
204 status.setEnabled(true);
208 return Inset::getStatus(cur, cmd, status);
213 docstring InsetCommand::contextMenuName() const
215 return from_ascii("context-") + from_ascii(insetName(p_.code()));
219 bool InsetCommand::showInsetDialog(BufferView * bv) const
221 if (p_.code() != NO_CODE)
222 bv->showDialog(insetName(p_.code()), params2string(p_),
223 const_cast<InsetCommand *>(this));
228 bool InsetCommand::string2params(string const & data,
229 InsetCommandParams & params)
234 // This happens when inset-insert is called without argument except for the
236 // "inset-insert toc"
237 string const name = insetName(params.code());
240 istringstream dstream(data);
242 lex.setStream(dstream);
243 lex.setContext("InsetCommand::string2params");
244 lex >> name.c_str(); // check for name
245 lex >> "CommandInset";
251 string InsetCommand::params2string(InsetCommandParams const & params)
254 data << insetName(params.code()) << ' ';
256 data << "\\end_inset\n";
261 bool decodeInsetParam(string const & name, string & data,
262 Buffer const & buffer)
264 InsetCode const code = insetCode(name);
272 case NOMENCL_PRINT_CODE:
275 case HYPERLINK_CODE: {
276 InsetCommandParams p(code);
277 data = InsetCommand::params2string(p);
281 // data is the include type: one of "include",
282 // "input", "verbatiminput" or "verbatiminput*"
284 // default type is requested
286 InsetCommandParams p(INCLUDE_CODE, data);
287 data = InsetCommand::params2string(p);
291 // \c data == "Boxed" || "Frameless" etc
292 InsetBoxParams p(data);
293 data = InsetBox::params2string(p);
298 data = InsetBranch::params2string(p);
302 InsetCommandParams p(CITE_CODE);
303 data = InsetCommand::params2string(p);
307 data = InsetERT::params2string(InsetCollapsable::Open);
310 case EXTERNAL_CODE: {
311 InsetExternalParams p;
312 data = InsetExternal::params2string(p, buffer);
317 data = InsetFloat::params2string(p);
320 case LISTINGS_CODE: {
321 InsetListingsParams p;
322 data = InsetListings::params2string(p);
325 case GRAPHICS_CODE: {
326 InsetGraphicsParams p;
327 data = InsetGraphics::params2string(p, buffer);
332 data = InsetNote::params2string(p);
336 InsetPhantomParams p;
337 data = InsetPhantom::params2string(p);
342 data = InsetSpace::params2string(p);
347 data = InsetVSpace::params2string(space);
352 data = InsetWrap::params2string(p);
357 } // end switch(code)