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 InsetCommand::~InsetCommand()
64 if (!mailer_name_.empty())
65 hideDialogs(mailer_name_, this);
66 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
67 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
68 for (; it != end; ++it)
70 it->first->clearLastInset(this);
74 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
76 button_.update(screenLabel(), editable() || hasSettings());
77 button_.metrics(mi, dim);
81 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
83 mouse_hover_[bv] = mouse_hover;
88 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
90 button_.setRenderState(mouse_hover_[pi.base.bv]);
91 button_.draw(pi, x, y);
95 void InsetCommand::setParam(std::string const & name, docstring const & value)
101 docstring const & InsetCommand::getParam(std::string const & name) const
107 void InsetCommand::setParams(InsetCommandParams const & p)
114 int InsetCommand::latex(odocstream & os, OutputParams const & runparams_in) const
116 OutputParams runparams = runparams_in;
117 os << getCommand(runparams);
122 int InsetCommand::plaintext(odocstream & os, OutputParams const &) const
124 docstring const str = "[" + buffer().B_("LaTeX Command: ")
125 + from_utf8(getCmdName()) + "]";
131 int InsetCommand::docbook(odocstream &, OutputParams const &) const
137 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
139 switch (cmd.action()) {
140 case LFUN_INSET_MODIFY: {
141 if (cmd.getArg(0) == "changetype") {
142 p_.setCmdName(cmd.getArg(1));
146 InsetCommandParams p(p_.code());
147 InsetCommand::string2params(mailer_name_, to_utf8(cmd.argument()), p);
148 if (p.getCmdName().empty())
155 case LFUN_INSET_DIALOG_UPDATE: {
156 string const name = to_utf8(cmd.argument());
157 cur.bv().updateDialog(name, params2string(name, params()));
162 Inset::doDispatch(cur, cmd);
169 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
170 FuncStatus & status) const
172 switch (cmd.action()) {
174 case LFUN_ERT_INSERT:
175 status.setEnabled(false);
179 case LFUN_INSET_MODIFY:
180 if (cmd.getArg(0) == "changetype") {
181 string const newtype = cmd.getArg(1);
182 status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
183 status.setOnOff(newtype == p_.getCmdName());
185 status.setEnabled(true);
188 case LFUN_INSET_DIALOG_UPDATE:
189 status.setEnabled(true);
193 return Inset::getStatus(cur, cmd, status);
198 docstring InsetCommand::contextMenu(BufferView const &, int, int) const
200 return from_ascii("context-") + from_ascii(mailer_name_);
204 bool InsetCommand::showInsetDialog(BufferView * bv) const
206 if (!mailer_name_.empty())
207 bv->showDialog(mailer_name_, params2string(mailer_name_, p_),
208 const_cast<InsetCommand *>(this));
213 // FIXME This could take an InsetCode instead of a string
214 bool InsetCommand::string2params(string const & name, string const & in,
215 InsetCommandParams & params)
220 istringstream data(in);
223 lex.setContext("InsetCommand::string2params");
224 lex >> name.c_str(); // check for name
225 lex >> "CommandInset";
231 // FIXME This could take an InsetCode instead of a string
232 string InsetCommand::params2string(string const & name,
233 InsetCommandParams const & params)
238 data << "\\end_inset\n";
243 bool decodeInsetParam(string const & name, string & data,
244 Buffer const & buffer)
246 InsetCode const code = insetCode(name);
253 case NOMENCL_PRINT_CODE:
256 case HYPERLINK_CODE: {
257 InsetCommandParams p(code);
258 data = InsetCommand::params2string(name, p);
262 // data is the include type: one of "include",
263 // "input", "verbatiminput" or "verbatiminput*"
265 // default type is requested
267 InsetCommandParams p(INCLUDE_CODE, data);
268 data = InsetCommand::params2string("include", p);
272 // \c data == "Boxed" || "Frameless" etc
273 InsetBoxParams p(data);
274 data = InsetBox::params2string(p);
279 data = InsetBranch::params2string(p);
283 InsetCommandParams p(CITE_CODE);
284 data = InsetCommand::params2string(name, p);
288 data = InsetERT::params2string(InsetCollapsable::Open);
291 case EXTERNAL_CODE: {
292 InsetExternalParams p;
293 data = InsetExternal::params2string(p, buffer);
298 data = InsetFloat::params2string(p);
301 case LISTINGS_CODE: {
302 InsetListingsParams p;
303 data = InsetListings::params2string(p);
306 case GRAPHICS_CODE: {
307 InsetGraphicsParams p;
308 data = InsetGraphics::params2string(p, buffer);
313 data = InsetNote::params2string(p);
317 InsetPhantomParams p;
318 data = InsetPhantom::params2string(p);
323 data = InsetSpace::params2string(p);
328 data = InsetVSpace::params2string(space);
333 data = InsetWrap::params2string(p);
338 } // end switch(code)