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/InsetListings.h"
33 #include "insets/InsetNote.h"
34 #include "insets/InsetPhantom.h"
35 #include "insets/InsetSpace.h"
36 #include "insets/InsetTabular.h"
37 #include "insets/InsetVSpace.h"
38 #include "insets/InsetWrap.h"
40 #include "support/debug.h"
41 #include "support/gettext.h"
43 #include "frontends/Application.h"
52 // FIXME Would it now be possible to use the InsetCode in
53 // place of the mailer name and recover that information?
54 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p,
55 string const & mailer_name)
57 mailer_name_(mailer_name),
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_), mailer_name_(rhs.mailer_name_)
69 InsetCommand::~InsetCommand()
71 if (!mailer_name_.empty())
72 hideDialogs(mailer_name_, this);
73 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
74 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
75 for (; it != end; ++it)
77 it->first->clearLastInset(this);
81 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
83 button_.update(screenLabel(), editable() || hasSettings());
84 button_.metrics(mi, dim);
88 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(std::string const & name, docstring const & value)
108 docstring const & InsetCommand::getParam(std::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") {
149 p_.setCmdName(cmd.getArg(1));
153 InsetCommandParams p(p_.code());
154 InsetCommand::string2params(mailer_name_, to_utf8(cmd.argument()), p);
155 if (p.getCmdName().empty())
156 cur.noScreenUpdate();
159 // FIXME We might also want to check here if this one is in the TOC.
160 // But I think most of those are labeled.
162 cur.forceBufferUpdate();
166 case LFUN_INSET_DIALOG_UPDATE: {
167 string const name = to_utf8(cmd.argument());
168 cur.bv().updateDialog(name, params2string(name, params()));
173 Inset::doDispatch(cur, cmd);
180 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
181 FuncStatus & status) const
183 switch (cmd.action()) {
185 case LFUN_ERT_INSERT:
186 status.setEnabled(false);
190 case LFUN_INSET_MODIFY:
191 if (cmd.getArg(0) == "changetype") {
192 string const newtype = cmd.getArg(1);
193 status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
194 status.setOnOff(newtype == p_.getCmdName());
196 status.setEnabled(true);
199 case LFUN_INSET_DIALOG_UPDATE:
200 status.setEnabled(true);
204 return Inset::getStatus(cur, cmd, status);
209 docstring InsetCommand::contextMenu(BufferView const &, int, int) const
211 return from_ascii("context-") + from_ascii(mailer_name_);
215 bool InsetCommand::showInsetDialog(BufferView * bv) const
217 if (!mailer_name_.empty())
218 bv->showDialog(mailer_name_, params2string(mailer_name_, p_),
219 const_cast<InsetCommand *>(this));
224 // FIXME This could take an InsetCode instead of a string
225 bool InsetCommand::string2params(string const & name, string const & in,
226 InsetCommandParams & params)
231 istringstream data(in);
234 lex.setContext("InsetCommand::string2params");
235 lex >> name.c_str(); // check for name
236 lex >> "CommandInset";
242 // FIXME This could take an InsetCode instead of a string
243 string InsetCommand::params2string(string const & name,
244 InsetCommandParams const & params)
249 data << "\\end_inset\n";
254 bool decodeInsetParam(string const & name, string & data,
255 Buffer const & buffer)
257 InsetCode const code = insetCode(name);
264 case NOMENCL_PRINT_CODE:
267 case HYPERLINK_CODE: {
268 InsetCommandParams p(code);
269 data = InsetCommand::params2string(name, p);
273 // data is the include type: one of "include",
274 // "input", "verbatiminput" or "verbatiminput*"
276 // default type is requested
278 InsetCommandParams p(INCLUDE_CODE, data);
279 data = InsetCommand::params2string("include", p);
283 // \c data == "Boxed" || "Frameless" etc
284 InsetBoxParams p(data);
285 data = InsetBox::params2string(p);
290 data = InsetBranch::params2string(p);
294 InsetCommandParams p(CITE_CODE);
295 data = InsetCommand::params2string(name, p);
299 data = InsetERT::params2string(InsetCollapsable::Open);
302 case EXTERNAL_CODE: {
303 InsetExternalParams p;
304 data = InsetExternal::params2string(p, buffer);
309 data = InsetFloat::params2string(p);
312 case LISTINGS_CODE: {
313 InsetListingsParams p;
314 data = InsetListings::params2string(p);
317 case GRAPHICS_CODE: {
318 InsetGraphicsParams p;
319 data = InsetGraphics::params2string(p, buffer);
324 data = InsetNote::params2string(p);
328 InsetPhantomParams p;
329 data = InsetPhantom::params2string(p);
334 data = InsetSpace::params2string(p);
339 data = InsetVSpace::params2string(space);
344 data = InsetWrap::params2string(p);
349 } // end switch(code)