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);
69 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
71 button_.update(screenLabel(), editable() || hasSettings());
72 button_.metrics(mi, dim);
76 bool InsetCommand::setMouseHover(bool mouse_hover)
78 mouse_hover_ = mouse_hover;
83 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
85 button_.setRenderState(mouse_hover_);
86 button_.draw(pi, x, y);
90 void InsetCommand::setParam(std::string const & name, docstring const & value)
96 docstring const & InsetCommand::getParam(std::string const & name) const
102 void InsetCommand::setParams(InsetCommandParams const & p)
109 int InsetCommand::latex(odocstream & os, OutputParams const & runparams_in) const
111 OutputParams runparams = runparams_in;
112 os << getCommand(runparams);
117 int InsetCommand::plaintext(odocstream & os, OutputParams const &) const
119 docstring const str = "[" + buffer().B_("LaTeX Command: ")
120 + from_utf8(getCmdName()) + "]";
126 int InsetCommand::docbook(odocstream &, OutputParams const &) const
132 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
134 switch (cmd.action) {
135 case LFUN_INSET_MODIFY: {
136 if (cmd.getArg(0) == "changetype") {
137 p_.setCmdName(cmd.getArg(1));
141 InsetCommandParams p(p_.code());
142 InsetCommand::string2params(mailer_name_, to_utf8(cmd.argument()), p);
143 if (p.getCmdName().empty())
150 case LFUN_INSET_DIALOG_UPDATE: {
151 string const name = to_utf8(cmd.argument());
152 cur.bv().updateDialog(name, params2string(name, params()));
157 Inset::doDispatch(cur, cmd);
164 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
165 FuncStatus & status) const
167 switch (cmd.action) {
169 case LFUN_ERT_INSERT:
170 status.setEnabled(false);
174 case LFUN_INSET_MODIFY:
175 if (cmd.getArg(0) == "changetype") {
176 string const newtype = cmd.getArg(1);
177 status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
178 status.setOnOff(newtype == p_.getCmdName());
180 status.setEnabled(true);
183 case LFUN_INSET_DIALOG_UPDATE:
184 status.setEnabled(true);
188 return Inset::getStatus(cur, cmd, status);
193 docstring InsetCommand::contextMenu(BufferView const &, int, int) const
195 return from_ascii("context-") + from_ascii(mailer_name_);
199 bool InsetCommand::showInsetDialog(BufferView * bv) const
201 if (!mailer_name_.empty())
202 bv->showDialog(mailer_name_, params2string(mailer_name_, p_),
203 const_cast<InsetCommand *>(this));
208 // FIXME This could take an InsetCode instead of a string
209 bool InsetCommand::string2params(string const & name, string const & in,
210 InsetCommandParams & params)
215 istringstream data(in);
218 lex.setContext("InsetCommand::string2params");
219 lex >> name.c_str(); // check for name
220 lex >> "CommandInset";
226 // FIXME This could take an InsetCode instead of a string
227 string InsetCommand::params2string(string const & name,
228 InsetCommandParams const & params)
233 data << "\\end_inset\n";
238 bool decodeInsetParam(string const & name, string & data,
239 Buffer const & buffer)
241 InsetCode const code = insetCode(name);
248 case NOMENCL_PRINT_CODE:
251 case HYPERLINK_CODE: {
252 InsetCommandParams p(code);
253 data = InsetCommand::params2string(name, p);
257 // data is the include type: one of "include",
258 // "input", "verbatiminput" or "verbatiminput*"
260 // default type is requested
262 InsetCommandParams p(INCLUDE_CODE, data);
263 data = InsetCommand::params2string("include", p);
267 // \c data == "Boxed" || "Frameless" etc
268 InsetBoxParams p(data);
269 data = InsetBox::params2string(p);
274 data = InsetBranch::params2string(p);
278 InsetCommandParams p(CITE_CODE);
279 data = InsetCommand::params2string(name, p);
283 data = InsetERT::params2string(InsetCollapsable::Open);
286 case EXTERNAL_CODE: {
287 InsetExternalParams p;
288 data = InsetExternal::params2string(p, buffer);
293 data = InsetFloat::params2string(p);
296 case LISTINGS_CODE: {
297 InsetListingsParams p;
298 data = InsetListings::params2string(p);
301 case GRAPHICS_CODE: {
302 InsetGraphicsParams p;
303 data = InsetGraphics::params2string(p, buffer);
308 data = InsetNote::params2string(p);
312 InsetPhantomParams p;
313 data = InsetPhantom::params2string(p);
318 data = InsetSpace::params2string(p);
323 data = InsetVSpace::params2string(space);
328 data = InsetWrap::params2string(p);
333 } // end switch(code)