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(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 &) const
116 int InsetCommand::plaintext(odocstream & os, OutputParams const &) const
118 docstring const str = "[" + buffer().B_("LaTeX Command: ")
119 + from_utf8(getCmdName()) + "]";
125 int InsetCommand::docbook(odocstream &, OutputParams const &) const
131 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
133 switch (cmd.action) {
134 case LFUN_INSET_MODIFY: {
135 if (cmd.getArg(0) == "changetype") {
136 p_.setCmdName(cmd.getArg(1));
140 InsetCommandParams p(p_.code());
141 InsetCommand::string2params(mailer_name_, to_utf8(cmd.argument()), p);
142 if (p.getCmdName().empty())
149 case LFUN_INSET_DIALOG_UPDATE: {
150 string const name = to_utf8(cmd.argument());
151 cur.bv().updateDialog(name, params2string(name, params()));
156 Inset::doDispatch(cur, cmd);
163 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
164 FuncStatus & status) const
166 switch (cmd.action) {
168 case LFUN_ERT_INSERT:
169 status.setEnabled(false);
173 case LFUN_INSET_MODIFY:
174 if (cmd.getArg(0) == "changetype") {
175 string const newtype = cmd.getArg(1);
176 status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
177 status.setOnOff(newtype == p_.getCmdName());
179 status.setEnabled(true);
182 case LFUN_INSET_DIALOG_UPDATE:
183 status.setEnabled(true);
187 return Inset::getStatus(cur, cmd, status);
192 docstring InsetCommand::contextMenu(BufferView const &, int, int) const
194 return from_ascii("context-") + from_ascii(mailer_name_);
198 bool InsetCommand::showInsetDialog(BufferView * bv) const
200 if (!mailer_name_.empty())
201 bv->showDialog(mailer_name_, params2string(mailer_name_, p_),
202 const_cast<InsetCommand *>(this));
207 // FIXME This could take an InsetCode instead of a string
208 bool InsetCommand::string2params(string const & name, string const & in,
209 InsetCommandParams & params)
214 istringstream data(in);
217 lex.setContext("InsetCommand::string2params");
218 lex >> name.c_str(); // check for name
219 lex >> "CommandInset";
225 // FIXME This could take an InsetCode instead of a string
226 string InsetCommand::params2string(string const & name,
227 InsetCommandParams const & params)
232 data << "\\end_inset\n";
237 bool decodeInsetParam(string const & name, string & data,
238 Buffer const & buffer)
240 InsetCode const code = insetCode(name);
247 case NOMENCL_PRINT_CODE:
250 case HYPERLINK_CODE: {
251 InsetCommandParams p(code);
252 data = InsetCommand::params2string(name, p);
256 // data is the include type: one of "include",
257 // "input", "verbatiminput" or "verbatiminput*"
259 // default type is requested
261 InsetCommandParams p(INCLUDE_CODE, data);
262 data = InsetCommand::params2string("include", p);
266 // \c data == "Boxed" || "Frameless" etc
267 InsetBoxParams p(data);
268 data = InsetBox::params2string(p);
273 data = InsetBranch::params2string(p);
277 InsetCommandParams p(CITE_CODE);
278 data = InsetCommand::params2string(name, p);
282 data = InsetERT::params2string(InsetCollapsable::Open);
285 case EXTERNAL_CODE: {
286 InsetExternalParams p;
287 data = InsetExternal::params2string(p, buffer);
292 data = InsetFloat::params2string(p);
295 case LISTINGS_CODE: {
296 InsetListingsParams p;
297 data = InsetListings::params2string(p);
300 case GRAPHICS_CODE: {
301 InsetGraphicsParams p;
302 data = InsetGraphics::params2string(p, buffer);
307 data = InsetNote::params2string(p);
311 InsetPhantomParams p;
312 data = InsetPhantom::params2string(p);
317 data = InsetSpace::params2string(p);
322 data = InsetVSpace::params2string(space);
327 data = InsetWrap::params2string(p);
332 } // end switch(code)