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"
18 #include "DispatchResult.h"
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
22 #include "MetricsInfo.h"
24 #include "insets/InsetBox.h"
25 #include "insets/InsetBranch.h"
26 #include "insets/InsetCommand.h"
27 #include "insets/InsetERT.h"
28 #include "insets/InsetExternal.h"
29 #include "insets/InsetFloat.h"
30 #include "insets/InsetGraphics.h"
31 #include "insets/InsetInclude.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,
56 string const & mailer_name)
58 mailer_name_(mailer_name),
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_), mailer_name_(rhs.mailer_name_)
70 InsetCommand::~InsetCommand()
72 if (!mailer_name_.empty())
73 hideDialogs(mailer_name_, this);
74 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
75 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
76 for (; it != end; ++it)
78 it->first->clearLastInset(this);
82 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
84 button_.update(screenLabel(), editable() || hasSettings());
85 button_.metrics(mi, dim);
89 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
91 mouse_hover_[bv] = mouse_hover;
96 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
98 button_.setRenderState(mouse_hover_[pi.base.bv]);
99 button_.draw(pi, x, y);
103 void InsetCommand::setParam(std::string const & name, docstring const & value)
109 docstring const & InsetCommand::getParam(std::string const & name) const
115 void InsetCommand::setParams(InsetCommandParams const & p)
122 int InsetCommand::latex(odocstream & os, OutputParams const & runparams_in) const
124 OutputParams runparams = runparams_in;
125 os << getCommand(runparams);
130 int InsetCommand::plaintext(odocstream & os, OutputParams const &) const
132 docstring const str = "[" + buffer().B_("LaTeX Command: ")
133 + from_utf8(getCmdName()) + "]";
139 int InsetCommand::docbook(odocstream &, OutputParams const &) const
145 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
147 switch (cmd.action()) {
148 case LFUN_INSET_MODIFY: {
149 if (cmd.getArg(0) == "changetype") {
150 p_.setCmdName(cmd.getArg(1));
154 InsetCommandParams p(p_.code());
155 InsetCommand::string2params(mailer_name_, to_utf8(cmd.argument()), p);
156 if (p.getCmdName().empty())
157 cur.noScreenUpdate();
160 // FIXME We might also want to check here if this one is in the TOC.
161 // But I think most of those are labeled.
163 cur.forceBufferUpdate();
167 case LFUN_INSET_DIALOG_UPDATE: {
168 string const name = to_utf8(cmd.argument());
169 cur.bv().updateDialog(name, params2string(name, params()));
174 Inset::doDispatch(cur, cmd);
181 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
182 FuncStatus & status) const
184 switch (cmd.action()) {
186 case LFUN_ERT_INSERT:
187 status.setEnabled(false);
191 case LFUN_INSET_MODIFY:
192 if (cmd.getArg(0) == "changetype") {
193 string const newtype = cmd.getArg(1);
194 status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
195 status.setOnOff(newtype == p_.getCmdName());
197 status.setEnabled(true);
200 case LFUN_INSET_DIALOG_UPDATE:
201 status.setEnabled(true);
205 return Inset::getStatus(cur, cmd, status);
210 docstring InsetCommand::contextMenu(BufferView const &, int, int) const
212 return from_ascii("context-") + from_ascii(mailer_name_);
216 bool InsetCommand::showInsetDialog(BufferView * bv) const
218 if (!mailer_name_.empty())
219 bv->showDialog(mailer_name_, params2string(mailer_name_, p_),
220 const_cast<InsetCommand *>(this));
225 // FIXME This could take an InsetCode instead of a string
226 bool InsetCommand::string2params(string const & name, string const & in,
227 InsetCommandParams & params)
232 // This happens when inset-insert is called without argument except for the
234 // "inset-insert toc"
237 istringstream data(in);
240 lex.setContext("InsetCommand::string2params");
241 lex >> name.c_str(); // check for name
242 lex >> "CommandInset";
248 // FIXME This could take an InsetCode instead of a string
249 string InsetCommand::params2string(string const & name,
250 InsetCommandParams const & params)
255 data << "\\end_inset\n";
260 bool decodeInsetParam(string const & name, string & data,
261 Buffer const & buffer)
263 InsetCode const code = insetCode(name);
271 case NOMENCL_PRINT_CODE:
274 case HYPERLINK_CODE: {
275 InsetCommandParams p(code);
276 data = InsetCommand::params2string(name, p);
280 // data is the include type: one of "include",
281 // "input", "verbatiminput" or "verbatiminput*"
283 // default type is requested
285 InsetCommandParams p(INCLUDE_CODE, data);
286 data = InsetCommand::params2string("include", p);
290 // \c data == "Boxed" || "Frameless" etc
291 InsetBoxParams p(data);
292 data = InsetBox::params2string(p);
297 data = InsetBranch::params2string(p);
301 InsetCommandParams p(CITE_CODE);
302 data = InsetCommand::params2string(name, p);
306 data = InsetERT::params2string(InsetCollapsable::Open);
309 case EXTERNAL_CODE: {
310 InsetExternalParams p;
311 data = InsetExternal::params2string(p, buffer);
316 data = InsetFloat::params2string(p);
319 case LISTINGS_CODE: {
320 InsetListingsParams p;
321 data = InsetListings::params2string(p);
324 case GRAPHICS_CODE: {
325 InsetGraphicsParams p;
326 data = InsetGraphics::params2string(p, buffer);
331 data = InsetNote::params2string(p);
335 InsetPhantomParams p;
336 data = InsetPhantom::params2string(p);
341 data = InsetSpace::params2string(p);
346 data = InsetVSpace::params2string(space);
351 data = InsetWrap::params2string(p);
356 } // end switch(code)