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"
25 using lyx::odocstream;
27 using lyx::odocstream;
30 using std::istringstream;
32 using std::ostringstream;
35 InsetCommand::InsetCommand(InsetCommandParams const & p,
36 string const & mailer_name)
38 mailer_name_(mailer_name),
39 updateButtonLabel_(true)
43 InsetCommand::~InsetCommand()
45 if (!mailer_name_.empty())
46 InsetCommandMailer(mailer_name_, *this).hideDialog();
50 void 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);
62 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
64 setPosCache(pi, x, y);
65 button_.draw(pi, x, y);
69 void InsetCommand::setParams(InsetCommandParams const & p)
72 updateButtonLabel_ = true;
76 int InsetCommand::latex(Buffer const &, odocstream & os,
77 OutputParams const &) const
84 int InsetCommand::plaintext(Buffer const &, odocstream &,
85 OutputParams const &) const
91 int InsetCommand::docbook(Buffer const &, odocstream &,
92 OutputParams const &) const
98 void InsetCommand::doDispatch(LCursor & cur, FuncRequest & cmd)
100 switch (cmd.action) {
101 case LFUN_INSET_REFRESH:
102 updateButtonLabel_ = true;
105 case LFUN_INSET_MODIFY: {
106 InsetCommandParams p(p_.getCmdName());
107 InsetCommandMailer::string2params(mailer_name_, lyx::to_utf8(cmd.argument()), p);
108 if (p.getCmdName().empty())
115 case LFUN_INSET_DIALOG_UPDATE: {
116 string const name = lyx::to_utf8(cmd.argument());
117 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
121 case LFUN_MOUSE_RELEASE: {
122 if (!mailer_name_.empty())
123 InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
128 InsetBase::doDispatch(cur, cmd);
135 bool InsetCommand::getStatus(LCursor & cur, FuncRequest const & cmd,
136 FuncStatus & status) const
138 switch (cmd.action) {
140 case LFUN_ERT_INSERT:
141 status.enabled(false);
144 case LFUN_INSET_REFRESH:
145 case LFUN_INSET_MODIFY:
146 case LFUN_INSET_DIALOG_UPDATE:
147 status.enabled(true);
150 return InsetBase::getStatus(cur, cmd, status);
155 void InsetCommand::replaceContents(std::string const & from, string const & to)
157 if (getContents() == from)
162 InsetCommandMailer::InsetCommandMailer(string const & name,
163 InsetCommand & inset)
164 : name_(name), inset_(inset)
168 string const InsetCommandMailer::inset2string(Buffer const &) const
170 return params2string(name(), inset_.params());
174 void InsetCommandMailer::string2params(string const & name,
176 InsetCommandParams & params)
182 istringstream data(in);
188 if (!lex || n != name)
189 return print_mailer_error("InsetCommandMailer", in, 1, name);
191 // This is part of the inset proper that is usually swallowed
192 // by LyXText::readInset
195 if (!lex || id != "LatexCommand")
196 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
203 InsetCommandMailer::params2string(string const & name,
204 InsetCommandParams const & params)
209 data << "\\end_inset\n";