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"
21 #include "support/gettext.h"
23 #include "MetricsInfo.h"
32 // FIXME Would it now be possible to use the InsetCode in
33 // place of the mailer name and recover that information?
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 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
52 if (updateButtonLabel_) {
53 updateButtonLabel_ = false;
54 button_.update(screenLabel(), editable() != NOT_EDITABLE);
56 button_.metrics(mi, dim);
60 bool InsetCommand::setMouseHover(bool mouse_hover)
62 mouse_hover_ = mouse_hover;
67 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
69 button_.setRenderState(mouse_hover_);
70 button_.draw(pi, x, y);
74 void InsetCommand::setParams(InsetCommandParams const & p)
77 updateButtonLabel_ = true;
81 int InsetCommand::latex(odocstream & os, OutputParams const &) const
88 int InsetCommand::plaintext(odocstream & os, OutputParams const &) const
90 docstring const str = "[" + buffer().B_("LaTeX Command: ")
91 + from_utf8(getCmdName()) + "]";
97 int InsetCommand::docbook(odocstream &, OutputParams const &) const
103 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
105 switch (cmd.action) {
106 case LFUN_INSET_REFRESH:
107 updateButtonLabel_ = true;
110 case LFUN_INSET_MODIFY: {
111 InsetCommandParams p(p_.code());
112 InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p);
113 if (p.getCmdName().empty())
120 case LFUN_INSET_DIALOG_UPDATE: {
121 string const name = to_utf8(cmd.argument());
122 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
126 case LFUN_MOUSE_RELEASE: {
127 if (!cur.selection())
133 Inset::doDispatch(cur, cmd);
140 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
141 FuncStatus & status) const
143 switch (cmd.action) {
145 case LFUN_ERT_INSERT:
146 status.enabled(false);
149 case LFUN_INSET_REFRESH:
150 case LFUN_INSET_MODIFY:
151 case LFUN_INSET_DIALOG_UPDATE:
152 status.enabled(true);
155 return Inset::getStatus(cur, cmd, status);
160 void InsetCommand::edit(Cursor & cur, bool, EntryDirection)
162 if (!mailer_name_.empty())
163 InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
167 InsetCommandMailer::InsetCommandMailer(
168 string const & name, InsetCommand & inset)
169 : name_(name), inset_(inset)
173 string const InsetCommandMailer::inset2string(Buffer const &) const
175 return params2string(name(), inset_.params());
179 //FIXME This could take an InsetCode instead of a string
180 bool InsetCommandMailer::string2params(
181 string const & name, string const & in, InsetCommandParams & params)
187 istringstream data(in);
193 if (!lex || n != name) {
194 print_mailer_error("InsetCommandMailer", in, 1, name);
198 // This is part of the inset proper that is usually swallowed
199 // by Text::readInset
202 if (!lex || id != "CommandInset") {
203 print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
212 //FIXME This could take an InsetCode instead of a string
214 InsetCommandMailer::params2string(string const & name,
215 InsetCommandParams const & params)
220 data << "\\end_inset\n";