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"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
23 #include "MetricsInfo.h"
25 #include "insets/InsetBox.h"
26 #include "insets/InsetBranch.h"
27 #include "insets/InsetCommand.h"
28 #include "insets/InsetERT.h"
29 #include "insets/InsetExternal.h"
30 #include "insets/InsetFloat.h"
31 #include "insets/InsetGraphics.h"
32 #include "insets/InsetIndex.h"
33 #include "insets/InsetLine.h"
34 #include "insets/InsetListings.h"
35 #include "insets/InsetNote.h"
36 #include "insets/InsetPhantom.h"
37 #include "insets/InsetSpace.h"
38 #include "insets/InsetTabular.h"
39 #include "insets/InsetVSpace.h"
40 #include "insets/InsetWrap.h"
42 #include "support/debug.h"
43 #include "support/gettext.h"
45 #include "frontends/Application.h"
54 // FIXME Would it now be possible to use the InsetCode in
55 // place of the mailer name and recover that information?
56 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p)
61 // The sole purpose of this copy constructor is to make sure
62 // that the mouse_hover_ map is not copied and remains empty.
63 InsetCommand::InsetCommand(InsetCommand const & rhs)
64 : Inset(rhs), p_(rhs.p_)
68 InsetCommand & InsetCommand::operator=(InsetCommand const & rhs)
73 Inset::operator=(rhs);
76 button_ = RenderButton();
82 InsetCommand::~InsetCommand()
84 if (p_.code() != NO_CODE)
85 hideDialogs(insetName(p_.code()), this);
87 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
88 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
89 for (; it != end; ++it)
91 it->first->clearLastInset(this);
95 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
97 button_.update(screenLabel(), editable() || clickable(*mi.base.bv, 0, 0));
98 button_.metrics(mi, dim);
102 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
105 mouse_hover_[bv] = mouse_hover;
110 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
112 button_.setRenderState(mouse_hover_[pi.base.bv]);
113 button_.draw(pi, x, y);
117 void InsetCommand::setParam(string const & name, docstring const & value)
123 docstring const & InsetCommand::getParam(string const & name) const
129 void InsetCommand::setParams(InsetCommandParams const & p)
136 void InsetCommand::latex(otexstream & os, OutputParams const & runparams_in) const
138 OutputParams runparams = runparams_in;
139 os << getCommand(runparams);
143 int InsetCommand::plaintext(odocstringstream & os,
144 OutputParams const &, size_t) const
146 docstring const str = "[" + buffer().B_("LaTeX Command: ")
147 + from_utf8(getCmdName()) + "]";
153 int InsetCommand::docbook(odocstream &, OutputParams const &) const
159 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
161 switch (cmd.action()) {
162 case LFUN_INSET_MODIFY: {
163 if (cmd.getArg(0) == "changetype") {
165 p_.setCmdName(cmd.getArg(1));
166 cur.forceBufferUpdate();
170 InsetCommandParams p(p_.code());
171 InsetCommand::string2params(to_utf8(cmd.argument()), p);
172 if (p.getCmdName().empty())
173 cur.noScreenUpdate();
178 // FIXME We might also want to check here if this one is in the TOC.
179 // But I think most of those are labeled.
181 cur.forceBufferUpdate();
185 case LFUN_INSET_DIALOG_UPDATE: {
186 string const name = to_utf8(cmd.argument());
187 cur.bv().updateDialog(name, params2string(params()));
192 Inset::doDispatch(cur, cmd);
199 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
200 FuncStatus & status) const
202 switch (cmd.action()) {
204 case LFUN_ERT_INSERT:
205 status.setEnabled(false);
209 case LFUN_INSET_MODIFY:
210 if (cmd.getArg(0) == "changetype") {
211 string const newtype = cmd.getArg(1);
212 status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
213 status.setOnOff(newtype == p_.getCmdName());
215 status.setEnabled(true);
218 case LFUN_INSET_DIALOG_UPDATE:
219 status.setEnabled(true);
223 return Inset::getStatus(cur, cmd, status);
228 string InsetCommand::contextMenuName() const
230 return "context-" + insetName(p_.code());
234 bool InsetCommand::showInsetDialog(BufferView * bv) const
236 if (p_.code() != NO_CODE)
237 bv->showDialog(insetName(p_.code()), params2string(p_),
238 const_cast<InsetCommand *>(this));
243 bool InsetCommand::string2params(string const & data,
244 InsetCommandParams & params)
249 // This happens when inset-insert is called without argument except for the
251 // "inset-insert toc"
252 string const name = insetName(params.code());
255 istringstream dstream(data);
257 lex.setStream(dstream);
258 lex.setContext("InsetCommand::string2params");
259 lex >> name.c_str(); // check for name
260 lex >> "CommandInset";
266 string InsetCommand::params2string(InsetCommandParams const & params)
269 data << insetName(params.code()) << ' ';
271 data << "\\end_inset\n";
276 bool decodeInsetParam(string const & name, string & data,
277 Buffer const & buffer)
279 InsetCode const code = insetCode(name);
283 case INDEX_PRINT_CODE:
287 case NOMENCL_PRINT_CODE:
290 case HYPERLINK_CODE: {
291 InsetCommandParams p(code);
292 data = InsetCommand::params2string(p);
296 // data is the include type: one of "include",
297 // "input", "verbatiminput" or "verbatiminput*"
299 // default type is requested
301 InsetCommandParams p(INCLUDE_CODE, data);
302 data = InsetCommand::params2string(p);
306 // \c data == "Boxed" || "Frameless" etc
307 InsetBoxParams p(data);
308 data = InsetBox::params2string(p);
313 data = InsetBranch::params2string(p);
317 InsetCommandParams p(CITE_CODE);
318 data = InsetCommand::params2string(p);
322 data = InsetERT::params2string(InsetCollapsable::Open);
325 case EXTERNAL_CODE: {
326 InsetExternalParams p;
327 data = InsetExternal::params2string(p, buffer);
332 data = InsetFloat::params2string(p);
337 data = InsetIndex::params2string(p);
340 case LISTINGS_CODE: {
341 InsetListingsParams p;
342 data = InsetListings::params2string(p);
345 case GRAPHICS_CODE: {
346 InsetGraphicsParams p;
347 data = InsetGraphics::params2string(p, buffer);
350 case MATH_SPACE_CODE: {
351 InsetSpaceParams p(true);
352 data = InsetSpace::params2string(p);
357 data = InsetNote::params2string(p);
361 InsetPhantomParams p;
362 data = InsetPhantom::params2string(p);
367 data = InsetSpace::params2string(p);
372 data = InsetVSpace::params2string(space);
377 data = InsetWrap::params2string(p);
382 } // end switch(code)