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 void 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);
62 bool InsetCommand::setMouseHover(bool mouse_hover)
64 mouse_hover_ = mouse_hover;
69 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
71 button_.setRenderState(mouse_hover_);
72 button_.draw(pi, x, y);
76 void InsetCommand::setParams(InsetCommandParams const & p)
79 updateButtonLabel_ = true;
83 int InsetCommand::latex(Buffer const &, odocstream & os,
84 OutputParams const &) const
91 int InsetCommand::plaintext(Buffer const & buf, odocstream & os,
92 OutputParams const &) const
94 docstring const str = "[" + buf.B_("LaTeX Command: ") + from_utf8(getCmdName()) + "]";
100 int InsetCommand::docbook(Buffer const &, odocstream &,
101 OutputParams const &) const
107 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
109 switch (cmd.action) {
110 case LFUN_INSET_REFRESH:
111 updateButtonLabel_ = true;
114 case LFUN_INSET_MODIFY: {
115 InsetCommandParams p(p_.insetType());
116 InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p);
117 if (p.getCmdName().empty())
124 case LFUN_INSET_DIALOG_UPDATE: {
125 string const name = to_utf8(cmd.argument());
126 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
130 case LFUN_MOUSE_RELEASE: {
131 if (!cur.selection())
137 Inset::doDispatch(cur, cmd);
144 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
145 FuncStatus & status) const
147 switch (cmd.action) {
149 case LFUN_ERT_INSERT:
150 status.enabled(false);
153 case LFUN_INSET_REFRESH:
154 case LFUN_INSET_MODIFY:
155 case LFUN_INSET_DIALOG_UPDATE:
156 status.enabled(true);
159 return Inset::getStatus(cur, cmd, status);
164 void InsetCommand::edit(Cursor & cur, bool)
166 if (!mailer_name_.empty())
167 InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
171 void InsetCommand::replaceContents(std::string const & from, string const & to)
173 if (getContents() == from)
178 InsetCommandMailer::InsetCommandMailer(string const & name,
179 InsetCommand & inset)
180 : name_(name), inset_(inset)
184 string const InsetCommandMailer::inset2string(Buffer const &) const
186 return params2string(name(), inset_.params());
190 void InsetCommandMailer::string2params(string const & name,
192 InsetCommandParams & params)
198 istringstream data(in);
204 if (!lex || n != name)
205 return print_mailer_error("InsetCommandMailer", in, 1, name);
207 // This is part of the inset proper that is usually swallowed
208 // by Text::readInset
211 if (!lex || id != "CommandInset")
212 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
219 InsetCommandMailer::params2string(string const & name,
220 InsetCommandParams const & params)
225 data << "\\end_inset\n";