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"
22 #include "MetricsInfo.h"
24 #include "support/debug.h"
25 #include "support/gettext.h"
27 #include "frontends/Application.h"
36 // FIXME Would it now be possible to use the InsetCode in
37 // place of the mailer name and recover that information?
38 InsetCommand::InsetCommand(InsetCommandParams const & p,
39 string const & mailer_name)
41 mailer_name_(mailer_name),
46 InsetCommand::~InsetCommand()
48 if (!mailer_name_.empty())
49 hideDialogs(mailer_name_, this);
53 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
55 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::setParam(std::string const & name, docstring const & value)
80 docstring const & InsetCommand::getParam(std::string const & name) const
86 void InsetCommand::setParams(InsetCommandParams const & p)
93 int InsetCommand::latex(odocstream & os, OutputParams const &) const
100 int InsetCommand::plaintext(odocstream & os, OutputParams const &) const
102 docstring const str = "[" + buffer().B_("LaTeX Command: ")
103 + from_utf8(getCmdName()) + "]";
109 int InsetCommand::docbook(odocstream &, OutputParams const &) const
115 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
117 switch (cmd.action) {
118 case LFUN_INSET_MODIFY: {
119 if (cmd.getArg(0) == "changetype") {
120 p_.setCmdName(cmd.getArg(1));
124 InsetCommandParams p(p_.code());
125 InsetCommand::string2params(mailer_name_, to_utf8(cmd.argument()), p);
126 if (p.getCmdName().empty())
133 case LFUN_INSET_DIALOG_UPDATE: {
134 string const name = to_utf8(cmd.argument());
135 cur.bv().updateDialog(name, params2string(name, params()));
139 case LFUN_MOUSE_RELEASE: {
140 if (!cur.selection() && cmd.button() != mouse_button::button3)
146 Inset::doDispatch(cur, cmd);
153 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
154 FuncStatus & status) const
156 switch (cmd.action) {
158 case LFUN_ERT_INSERT:
159 status.enabled(false);
162 case LFUN_INSET_MODIFY:
163 if (cmd.getArg(0) == "changetype") {
164 string const newtype = cmd.getArg(1);
165 status.enabled(p_.isCompatibleCommand(p_.code(), newtype));
166 status.setOnOff(newtype == p_.getCmdName());
168 status.enabled(true);
170 case LFUN_INSET_DIALOG_UPDATE:
171 status.enabled(true);
174 return Inset::getStatus(cur, cmd, status);
179 docstring InsetCommand::contextMenu(BufferView const &, int, int) const
181 return from_ascii("context-") + from_ascii(mailer_name_);
185 void InsetCommand::edit(Cursor & cur, bool, EntryDirection)
187 if (!mailer_name_.empty())
188 cur.bv().showDialog(mailer_name_, params2string(mailer_name_, p_), this);
192 // FIXME This could take an InsetCode instead of a string
193 bool InsetCommand::string2params(string const & name, string const & in,
194 InsetCommandParams & params)
200 istringstream data(in);
206 if (!lex || n != name) {
207 LYXERR0("InsetCommand::string2params(" << in << ")\n"
208 "Expected arg 1 to be \"" << name << "\"\n");
212 // This is part of the inset proper that is usually swallowed
213 // by Text::readInset
216 if (!lex || id != "CommandInset") {
217 LYXERR0("InsetCommand::string2params(" << in << ")\n"
218 "Expected arg 2 to be \"CommandInset\"\n");
227 // FIXME This could take an InsetCode instead of a string
228 string InsetCommand::params2string(string const & name,
229 InsetCommandParams const & params)
234 data << "\\end_inset\n";