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"
23 #include "MetricsInfo.h"
31 using std::istringstream;
33 using std::ostringstream;
36 InsetCommand::InsetCommand(InsetCommandParams const & p,
37 string const & mailer_name)
39 mailer_name_(mailer_name),
41 updateButtonLabel_(true)
45 InsetCommand::~InsetCommand()
47 if (!mailer_name_.empty())
48 InsetCommandMailer(mailer_name_, *this).hideDialog();
52 bool InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
54 if (updateButtonLabel_) {
55 updateButtonLabel_ = false;
56 button_.update(getScreenLabel(mi.base.bv->buffer()),
57 editable() != NOT_EDITABLE);
59 button_.metrics(mi, dim);
60 bool const changed = dim_ != dim;
66 bool InsetCommand::setMouseHover(bool mouse_hover)
68 mouse_hover_ = mouse_hover;
73 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
75 setPosCache(pi, x, y);
76 button_.setRenderState(mouse_hover_);
77 button_.draw(pi, x, y);
81 void InsetCommand::setParams(InsetCommandParams const & p)
84 updateButtonLabel_ = true;
88 int InsetCommand::latex(Buffer const &, odocstream & os,
89 OutputParams const &) const
96 int InsetCommand::plaintext(Buffer const & buf, odocstream & os,
97 OutputParams const &) const
99 docstring const str = "[" + buf.B_("LaTeX Command: ") + from_utf8(getCmdName()) + "]";
105 int InsetCommand::docbook(Buffer const &, odocstream &,
106 OutputParams const &) const
112 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
114 switch (cmd.action) {
115 case LFUN_INSET_REFRESH:
116 updateButtonLabel_ = true;
119 case LFUN_INSET_MODIFY: {
120 InsetCommandParams p(p_.getCmdName());
121 InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p);
122 if (p.getCmdName().empty())
129 case LFUN_INSET_DIALOG_UPDATE: {
130 string const name = to_utf8(cmd.argument());
131 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
135 case LFUN_MOUSE_RELEASE: {
136 if (!mailer_name_.empty() && !cur.selection())
137 InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
142 Inset::doDispatch(cur, cmd);
149 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
150 FuncStatus & status) const
152 switch (cmd.action) {
154 case LFUN_ERT_INSERT:
155 status.enabled(false);
158 case LFUN_INSET_REFRESH:
159 case LFUN_INSET_MODIFY:
160 case LFUN_INSET_DIALOG_UPDATE:
161 status.enabled(true);
164 return Inset::getStatus(cur, cmd, status);
169 void InsetCommand::replaceContents(std::string const & from, string const & to)
171 if (getContents() == from)
176 InsetCommandMailer::InsetCommandMailer(string const & name,
177 InsetCommand & inset)
178 : name_(name), inset_(inset)
182 string const InsetCommandMailer::inset2string(Buffer const &) const
184 return params2string(name(), inset_.params());
188 void InsetCommandMailer::string2params(string const & name,
190 InsetCommandParams & params)
196 istringstream data(in);
202 if (!lex || n != name)
203 return print_mailer_error("InsetCommandMailer", in, 1, name);
205 // This is part of the inset proper that is usually swallowed
206 // by Text::readInset
209 if (!lex || id != "LatexCommand")
210 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
217 InsetCommandMailer::params2string(string const & name,
218 InsetCommandParams const & params)
223 data << "\\end_inset\n";