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 button_.setRenderState(mouse_hover_);
75 button_.draw(pi, x, y);
79 void InsetCommand::setParams(InsetCommandParams const & p)
82 updateButtonLabel_ = true;
86 int InsetCommand::latex(Buffer const &, odocstream & os,
87 OutputParams const &) const
94 int InsetCommand::plaintext(Buffer const & buf, odocstream & os,
95 OutputParams const &) const
97 docstring const str = "[" + buf.B_("LaTeX Command: ") + from_utf8(getCmdName()) + "]";
103 int InsetCommand::docbook(Buffer const &, odocstream &,
104 OutputParams const &) const
110 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
112 switch (cmd.action) {
113 case LFUN_INSET_REFRESH:
114 updateButtonLabel_ = true;
117 case LFUN_INSET_MODIFY: {
118 InsetCommandParams p(p_.getCmdName());
119 InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p);
120 if (p.getCmdName().empty())
127 case LFUN_INSET_DIALOG_UPDATE: {
128 string const name = to_utf8(cmd.argument());
129 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
133 case LFUN_MOUSE_RELEASE: {
134 if (!cur.selection())
140 Inset::doDispatch(cur, cmd);
147 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
148 FuncStatus & status) const
150 switch (cmd.action) {
152 case LFUN_ERT_INSERT:
153 status.enabled(false);
156 case LFUN_INSET_REFRESH:
157 case LFUN_INSET_MODIFY:
158 case LFUN_INSET_DIALOG_UPDATE:
159 status.enabled(true);
162 return Inset::getStatus(cur, cmd, status);
167 void InsetCommand::edit(Cursor & cur, bool)
169 if (!mailer_name_.empty())
170 InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
174 void InsetCommand::replaceContents(std::string const & from, string const & to)
176 if (getContents() == from)
181 InsetCommandMailer::InsetCommandMailer(string const & name,
182 InsetCommand & inset)
183 : name_(name), inset_(inset)
187 string const InsetCommandMailer::inset2string(Buffer const &) const
189 return params2string(name(), inset_.params());
193 void InsetCommandMailer::string2params(string const & name,
195 InsetCommandParams & params)
201 istringstream data(in);
207 if (!lex || n != name)
208 return print_mailer_error("InsetCommandMailer", in, 1, name);
210 // This is part of the inset proper that is usually swallowed
211 // by Text::readInset
214 if (!lex || id != "LatexCommand")
215 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
222 InsetCommandMailer::params2string(string const & name,
223 InsetCommandParams const & params)
228 data << "\\end_inset\n";