2 * \file InsetMathRef.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathRef.h"
15 #include "BufferView.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
20 #include "LaTeXFeatures.h"
23 #include "MathFactory.h"
24 #include "MathSupport.h"
25 #include "OutputParams.h"
26 #include "ParIterator.h"
29 #include "insets/InsetCommand.h"
31 #include "support/debug.h"
32 #include "support/gettext.h"
40 InsetMathRef::InsetMathRef(Buffer * buf)
41 : CommandInset(buf, from_ascii("ref"), false)
45 InsetMathRef::InsetMathRef(Buffer * buf, docstring const & data)
46 : CommandInset(buf, data, false)
50 Inset * InsetMathRef::clone() const
52 return new InsetMathRef(*this);
56 void InsetMathRef::infoize(odocstream & os) const
58 os << "Ref: " << cell(0);
62 void InsetMathRef::doDispatch(Cursor & cur, FuncRequest & cmd)
64 switch (cmd.action()) {
65 case LFUN_INSET_MODIFY:
66 if (cmd.getArg(0) == "ref") {
68 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
70 *this = *ar[0].nucleus()->asRefInset();
77 case LFUN_INSET_DIALOG_UPDATE: {
78 string const data = createDialogStr();
79 cur.bv().updateDialog("ref", data);
83 case LFUN_MOUSE_RELEASE:
84 if (cmd.button() == mouse_button::button3) {
85 LYXERR0("trying to goto ref '" << to_utf8(asString(cell(0))) << "'");
86 //FIXME: use DispatchResult argument
87 lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO, asString(cell(0))));
90 if (cmd.button() == mouse_button::button1) {
91 // Eventually trigger dialog with button 3, not 1
92 string const data = createDialogStr();
93 cur.bv().showDialog("ref", data, this);
99 case LFUN_MOUSE_PRESS:
100 case LFUN_MOUSE_MOTION:
101 // eat other mouse commands
105 CommandInset::doDispatch(cur, cmd);
111 bool InsetMathRef::getStatus(Cursor & cur, FuncRequest const & cmd,
112 FuncStatus & status) const
114 switch (cmd.action()) {
116 case LFUN_INSET_MODIFY:
117 case LFUN_INSET_DIALOG_UPDATE:
118 case LFUN_MOUSE_RELEASE:
119 case LFUN_MOUSE_PRESS:
120 case LFUN_MOUSE_MOTION:
121 status.setEnabled(true);
124 return CommandInset::getStatus(cur, cmd, status);
129 docstring const InsetMathRef::screenLabel() const
132 for (int i = 0; !types[i].latex_name.empty(); ++i) {
133 if (commandname() == types[i].latex_name) {
134 str = _(to_utf8(types[i].short_gui_name));
138 str += asString(cell(0));
140 //if (/* !isLatex && */ !cell(0).empty()) {
142 // str += asString(cell(1));
148 void InsetMathRef::validate(LaTeXFeatures & features) const
150 if (commandname() == "vref" || commandname() == "vpageref")
151 features.require("varioref");
152 else if (commandname() == "prettyref")
153 features.require("prettyref");
154 else if (commandname() == "eqref")
155 features.require("amsmath");
159 int InsetMathRef::docbook(odocstream & os, OutputParams const & runparams) const
161 if (cell(1).empty()) {
162 os << "<xref linkend=\""
163 << sgml::cleanID(buffer(), runparams, asString(cell(0)));
164 if (runparams.flavor == OutputParams::XML)
169 os << "<link linkend=\""
170 << sgml::cleanID(buffer(), runparams, asString(cell(0)))
180 void InsetMathRef::updateBuffer(ParIterator const & it, UpdateType /*utype*/)
183 LYXERR0("InsetMathRef::updateBuffer: no buffer_!");
186 // register this inset into the buffer reference cache.
187 buffer().references(getTarget()).push_back(make_pair(this, it));
191 string const InsetMathRef::createDialogStr() const
193 InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
194 icp["reference"] = asString(cell(0));
195 if (!cell(1).empty())
196 icp["name"] = asString(cell(1));
197 return InsetCommand::params2string(icp);
201 docstring const InsetMathRef::getTarget() const
203 return asString(cell(0));
207 void InsetMathRef::changeTarget(docstring const & target)
209 InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
210 icp["reference"] = target;
211 if (!cell(1).empty())
212 icp["name"] = asString(cell(1));
214 Buffer & buf = buffer();
215 if (createInsetMath_fromDialogStr(
216 from_utf8(InsetCommand::params2string(icp)), ar)) {
217 *this = *ar[0].nucleus()->asRefInset();
218 // FIXME audit setBuffer calls
224 InsetMathRef::ref_type_info InsetMathRef::types[] = {
225 { from_ascii("ref"), from_ascii(N_("Standard[[mathref]]")), from_ascii(N_("Ref: "))},
226 { from_ascii("eqref"), from_ascii(N_("Equation")), from_ascii(N_("EqRef: "))},
227 { from_ascii("pageref"), from_ascii(N_("Page Number")), from_ascii(N_("Page: "))},
228 { from_ascii("vpageref"), from_ascii(N_("Textual Page Number")), from_ascii(N_("TextPage: "))},
229 { from_ascii("vref"), from_ascii(N_("Standard+Textual Page")), from_ascii(N_("Ref+Text: "))},
230 { from_ascii("prettyref"), from_ascii(N_("PrettyRef")), from_ascii(N_("FormatRef: "))},
231 { from_ascii(""), from_ascii(""), from_ascii("") }