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"
27 using std::istringstream;
29 using std::ostringstream;
32 InsetCommand::InsetCommand(InsetCommandParams const & p,
33 string const & mailer_name)
34 : p_(p.getCmdName(), p.getContents(), p.getOptions(), p.getSecOptions()),
35 mailer_name_(mailer_name),
36 updateButtonLabel_(true)
40 InsetCommand::~InsetCommand()
42 if (!mailer_name_.empty())
43 InsetCommandMailer(mailer_name_, *this).hideDialog();
47 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
49 if (updateButtonLabel_) {
50 updateButtonLabel_ = false;
51 button_.update(getScreenLabel(*mi.base.bv->buffer()),
52 editable() != NOT_EDITABLE);
54 button_.metrics(mi, dim);
59 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
61 setPosCache(pi, x, y);
62 button_.draw(pi, x, y);
66 void InsetCommand::setParams(InsetCommandParams const & p)
69 updateButtonLabel_ = true;
73 int InsetCommand::latex(Buffer const &, ostream & os,
74 OutputParams const &) const
81 int InsetCommand::plaintext(Buffer const &, ostream &,
82 OutputParams const &) const
88 int InsetCommand::docbook(Buffer const &, ostream &,
89 OutputParams const &) const
95 void InsetCommand::doDispatch(LCursor & cur, FuncRequest & cmd)
98 case LFUN_INSET_REFRESH:
99 updateButtonLabel_ = true;
102 case LFUN_INSET_MODIFY: {
103 InsetCommandParams p;
104 InsetCommandMailer::string2params(mailer_name_, lyx::to_utf8(cmd.argument()), p);
105 if (p.getCmdName().empty())
112 case LFUN_INSET_DIALOG_UPDATE: {
113 string const name = lyx::to_utf8(cmd.argument());
114 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
118 case LFUN_MOUSE_RELEASE: {
119 if (!mailer_name_.empty())
120 InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
125 InsetBase::doDispatch(cur, cmd);
132 bool InsetCommand::getStatus(LCursor & cur, FuncRequest const & cmd,
133 FuncStatus & status) const
135 switch (cmd.action) {
137 case LFUN_ERT_INSERT:
138 status.enabled(false);
141 case LFUN_INSET_REFRESH:
142 case LFUN_INSET_MODIFY:
143 case LFUN_INSET_DIALOG_UPDATE:
144 status.enabled(true);
147 return InsetBase::getStatus(cur, cmd, status);
152 InsetCommandMailer::InsetCommandMailer(string const & name,
153 InsetCommand & inset)
154 : name_(name), inset_(inset)
158 string const InsetCommandMailer::inset2string(Buffer const &) const
160 return params2string(name(), inset_.params());
164 void InsetCommandMailer::string2params(string const & name,
166 InsetCommandParams & params)
168 params = InsetCommandParams();
172 istringstream data(in);
178 if (!lex || n != name)
179 return print_mailer_error("InsetCommandMailer", in, 1, name);
181 // This is part of the inset proper that is usually swallowed
182 // by LyXText::readInset
185 if (!lex || id != "LatexCommand")
186 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
193 InsetCommandMailer::params2string(string const & name,
194 InsetCommandParams const & params)
199 data << "\\end_inset\n";