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"
16 #include "BufferView.h"
17 #include "dispatchresult.h"
18 #include "funcrequest.h"
19 #include "FuncStatus.h"
21 #include "metricsinfo.h"
29 using std::istringstream;
31 using std::ostringstream;
34 InsetCommand::InsetCommand(InsetCommandParams const & p,
35 string const & mailer_name)
37 mailer_name_(mailer_name),
39 updateButtonLabel_(true)
43 InsetCommand::~InsetCommand()
45 if (!mailer_name_.empty())
46 InsetCommandMailer(mailer_name_, *this).hideDialog();
50 bool InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
52 if (updateButtonLabel_) {
53 updateButtonLabel_ = false;
54 button_.update(getScreenLabel(*mi.base.bv->buffer()),
55 editable() != NOT_EDITABLE);
57 button_.metrics(mi, dim);
58 bool const changed = dim_ != dim;
64 bool InsetCommand::setMouseHover(bool mouse_hover)
66 mouse_hover_ = mouse_hover;
71 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
73 setPosCache(pi, x, y);
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 &, odocstream &,
95 OutputParams const &) const
101 int InsetCommand::docbook(Buffer const &, odocstream &,
102 OutputParams const &) const
108 void InsetCommand::doDispatch(LCursor & cur, FuncRequest & cmd)
110 switch (cmd.action) {
111 case LFUN_INSET_REFRESH:
112 updateButtonLabel_ = true;
115 case LFUN_INSET_MODIFY: {
116 InsetCommandParams p(p_.getCmdName());
117 InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p);
118 if (p.getCmdName().empty())
125 case LFUN_INSET_DIALOG_UPDATE: {
126 string const name = to_utf8(cmd.argument());
127 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
131 case LFUN_MOUSE_RELEASE: {
132 if (!mailer_name_.empty())
133 InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
138 InsetBase::doDispatch(cur, cmd);
145 bool InsetCommand::getStatus(LCursor & cur, FuncRequest const & cmd,
146 FuncStatus & status) const
148 switch (cmd.action) {
150 case LFUN_ERT_INSERT:
151 status.enabled(false);
154 case LFUN_INSET_REFRESH:
155 case LFUN_INSET_MODIFY:
156 case LFUN_INSET_DIALOG_UPDATE:
157 status.enabled(true);
160 return InsetBase::getStatus(cur, cmd, status);
165 void InsetCommand::replaceContents(std::string const & from, string const & to)
167 if (getContents() == from)
172 InsetCommandMailer::InsetCommandMailer(string const & name,
173 InsetCommand & inset)
174 : name_(name), inset_(inset)
178 string const InsetCommandMailer::inset2string(Buffer const &) const
180 return params2string(name(), inset_.params());
184 void InsetCommandMailer::string2params(string const & name,
186 InsetCommandParams & params)
192 istringstream data(in);
198 if (!lex || n != name)
199 return print_mailer_error("InsetCommandMailer", in, 1, name);
201 // This is part of the inset proper that is usually swallowed
202 // by LyXText::readInset
205 if (!lex || id != "LatexCommand")
206 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
213 InsetCommandMailer::params2string(string const & name,
214 InsetCommandParams const & params)
219 data << "\\end_inset\n";