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 // FIXME Would it now be possible to use the InsetCode in
36 // place of the mailer name and recover that information?
37 InsetCommand::InsetCommand(InsetCommandParams const & p,
38 string const & mailer_name)
40 mailer_name_(mailer_name),
42 updateButtonLabel_(true)
46 InsetCommand::~InsetCommand()
48 if (!mailer_name_.empty())
49 InsetCommandMailer(mailer_name_, *this).hideDialog();
53 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
55 if (updateButtonLabel_) {
56 updateButtonLabel_ = false;
57 button_.update(getScreenLabel(mi.base.bv->buffer()),
58 editable() != NOT_EDITABLE);
60 button_.metrics(mi, 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 button_.setRenderState(mouse_hover_);
74 button_.draw(pi, x, y);
78 void InsetCommand::setParams(InsetCommandParams const & p)
81 updateButtonLabel_ = true;
85 int InsetCommand::latex(Buffer const &, odocstream & os,
86 OutputParams const &) const
93 int InsetCommand::plaintext(Buffer const & buf, odocstream & os,
94 OutputParams const &) const
96 docstring const str = "[" + buf.B_("LaTeX Command: ") + from_utf8(getCmdName()) + "]";
102 int InsetCommand::docbook(Buffer const &, odocstream &,
103 OutputParams const &) const
109 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
111 switch (cmd.action) {
112 case LFUN_INSET_REFRESH:
113 updateButtonLabel_ = true;
116 case LFUN_INSET_MODIFY: {
117 InsetCommandParams p(p_.code());
118 InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p);
119 if (p.getCmdName().empty())
126 case LFUN_INSET_DIALOG_UPDATE: {
127 string const name = to_utf8(cmd.argument());
128 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
132 case LFUN_MOUSE_RELEASE: {
133 if (!cur.selection())
139 Inset::doDispatch(cur, cmd);
146 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
147 FuncStatus & status) const
149 switch (cmd.action) {
151 case LFUN_ERT_INSERT:
152 status.enabled(false);
155 case LFUN_INSET_REFRESH:
156 case LFUN_INSET_MODIFY:
157 case LFUN_INSET_DIALOG_UPDATE:
158 status.enabled(true);
161 return Inset::getStatus(cur, cmd, status);
166 void InsetCommand::edit(Cursor & cur, bool)
168 if (!mailer_name_.empty())
169 InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
173 InsetCommandMailer::InsetCommandMailer(
174 string const & name, InsetCommand & inset)
175 : name_(name), inset_(inset)
179 string const InsetCommandMailer::inset2string(Buffer const &) const
181 return params2string(name(), inset_.params());
185 //FIXME This could take an InsetCode instead of a string
186 bool InsetCommandMailer::string2params(
187 string const & name, string const & in, InsetCommandParams & params)
193 istringstream data(in);
199 if (!lex || n != name) {
200 print_mailer_error("InsetCommandMailer", in, 1, name);
204 // This is part of the inset proper that is usually swallowed
205 // by Text::readInset
208 if (!lex || id != "CommandInset") {
209 print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
218 //FIXME This could take an InsetCode instead of a string
220 InsetCommandMailer::params2string(string const & name,
221 InsetCommandParams const & params)
226 data << "\\end_inset\n";