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;
32 using std::ostringstream;
35 InsetCommand::InsetCommand(InsetCommandParams const & p,
36 string const & mailer_name)
38 mailer_name_(mailer_name),
40 updateButtonLabel_(true)
44 InsetCommand::~InsetCommand()
46 if (!mailer_name_.empty())
47 InsetCommandMailer(mailer_name_, *this).hideDialog();
51 bool InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
53 if (updateButtonLabel_) {
54 updateButtonLabel_ = false;
55 button_.update(getScreenLabel(mi.base.bv->buffer()),
56 editable() != NOT_EDITABLE);
58 button_.metrics(mi, dim);
59 bool const changed = dim_ != dim;
65 bool InsetCommand::setMouseHover(bool mouse_hover)
67 mouse_hover_ = mouse_hover;
72 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
74 setPosCache(pi, x, y);
75 button_.setRenderState(mouse_hover_);
76 button_.draw(pi, x, y);
80 void InsetCommand::setParams(InsetCommandParams const & p)
83 updateButtonLabel_ = true;
87 int InsetCommand::latex(Buffer const &, odocstream & os,
88 OutputParams const &) const
95 int InsetCommand::plaintext(Buffer const & buf, odocstream & os,
96 OutputParams const &) const
98 docstring const str = "[" + buf.B_("LaTeX Command: ") + from_utf8(getCmdName()) + "]";
104 int InsetCommand::docbook(Buffer const &, odocstream &,
105 OutputParams const &) const
111 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
113 switch (cmd.action) {
114 case LFUN_INSET_REFRESH:
115 updateButtonLabel_ = true;
118 case LFUN_INSET_MODIFY: {
119 InsetCommandParams p(p_.getCmdName());
120 InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p);
121 if (p.getCmdName().empty())
128 case LFUN_INSET_DIALOG_UPDATE: {
129 string const name = to_utf8(cmd.argument());
130 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
134 case LFUN_MOUSE_RELEASE: {
135 if (!mailer_name_.empty() && !cur.selection())
136 InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
141 Inset::doDispatch(cur, cmd);
148 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
149 FuncStatus & status) const
151 switch (cmd.action) {
153 case LFUN_ERT_INSERT:
154 status.enabled(false);
157 case LFUN_INSET_REFRESH:
158 case LFUN_INSET_MODIFY:
159 case LFUN_INSET_DIALOG_UPDATE:
160 status.enabled(true);
163 return Inset::getStatus(cur, cmd, status);
168 void InsetCommand::replaceContents(std::string const & from, string const & to)
170 if (getContents() == from)
175 InsetCommandMailer::InsetCommandMailer(string const & name,
176 InsetCommand & inset)
177 : name_(name), inset_(inset)
181 string const InsetCommandMailer::inset2string(Buffer const &) const
183 return params2string(name(), inset_.params());
187 void InsetCommandMailer::string2params(string const & name,
189 InsetCommandParams & params)
195 istringstream data(in);
201 if (!lex || n != name)
202 return print_mailer_error("InsetCommandMailer", in, 1, name);
204 // This is part of the inset proper that is usually swallowed
205 // by Text::readInset
208 if (!lex || id != "LatexCommand")
209 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
216 InsetCommandMailer::params2string(string const & name,
217 InsetCommandParams const & params)
222 data << "\\end_inset\n";