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 : InsetMathCommand(buf, from_ascii("ref"), false)
45 InsetMathRef::InsetMathRef(Buffer * buf, docstring const & data)
46 : InsetMathCommand(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 (cur.selection()) {
88 if (cmd.button() == mouse_button::button3) {
89 LYXERR0("trying to goto ref '" << to_utf8(asString(cell(0))) << "'");
90 //FIXME: use DispatchResult argument
91 lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO, asString(cell(0))));
94 if (cmd.button() == mouse_button::button1) {
95 // Eventually trigger dialog with button 3, not 1
96 string const data = createDialogStr();
97 cur.bv().showDialog("ref", data, this);
103 case LFUN_MOUSE_PRESS: {
104 bool do_selection = cmd.button() == mouse_button::button1
105 && cmd.modifier() == ShiftModifier;
106 // For some reason the cursor points inside the first cell, which is not
108 cur.leaveInset(*this);
109 cur.bv().mouseSetCursor(cur, do_selection);
113 case LFUN_MOUSE_DOUBLE:
114 case LFUN_MOUSE_TRIPLE:
115 // eat other mouse commands
119 InsetMathCommand::doDispatch(cur, cmd);
125 bool InsetMathRef::getStatus(Cursor & cur, FuncRequest const & cmd,
126 FuncStatus & status) const
128 switch (cmd.action()) {
130 case LFUN_INSET_MODIFY:
131 case LFUN_INSET_DIALOG_UPDATE:
132 case LFUN_MOUSE_RELEASE:
133 case LFUN_MOUSE_PRESS:
134 case LFUN_MOUSE_DOUBLE:
135 case LFUN_MOUSE_TRIPLE:
136 status.setEnabled(true);
139 return InsetMathCommand::getStatus(cur, cmd, status);
144 docstring const InsetMathRef::screenLabel() const
147 for (int i = 0; !types[i].latex_name.empty(); ++i) {
148 if (commandname() == types[i].latex_name) {
149 str = _(to_utf8(types[i].short_gui_name));
153 str += asString(cell(0));
155 //if (/* !isLatex && */ !cell(0).empty()) {
157 // str += asString(cell(1));
163 void InsetMathRef::validate(LaTeXFeatures & features) const
165 if (commandname() == "vref" || commandname() == "vpageref")
166 features.require("varioref");
167 else if (commandname() == "prettyref")
168 features.require("prettyref");
169 else if (commandname() == "eqref")
170 features.require("amsmath");
171 else if (commandname() == "nameref")
172 features.require("nameref");
176 int InsetMathRef::docbook(odocstream & os, OutputParams const & runparams) const
178 if (cell(1).empty()) {
179 os << "<xref linkend=\""
180 << sgml::cleanID(buffer(), runparams, asString(cell(0)));
181 if (runparams.flavor == OutputParams::XML)
186 os << "<link linkend=\""
187 << sgml::cleanID(buffer(), runparams, asString(cell(0)))
197 void InsetMathRef::updateBuffer(ParIterator const & it, UpdateType /*utype*/)
200 LYXERR0("InsetMathRef::updateBuffer: no buffer_!");
203 // register this inset into the buffer reference cache.
204 buffer().addReference(getTarget(), this, it);
208 string const InsetMathRef::createDialogStr() const
210 InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
211 icp["reference"] = asString(cell(0));
212 if (!cell(1).empty())
213 icp["name"] = asString(cell(1));
214 return InsetCommand::params2string(icp);
218 docstring const InsetMathRef::getTarget() const
220 return asString(cell(0));
224 void InsetMathRef::changeTarget(docstring const & target)
226 InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
227 icp["reference"] = target;
228 if (!cell(1).empty())
229 icp["name"] = asString(cell(1));
231 Buffer & buf = buffer();
232 if (createInsetMath_fromDialogStr(
233 from_utf8(InsetCommand::params2string(icp)), ar)) {
234 *this = *ar[0].nucleus()->asRefInset();
235 // FIXME audit setBuffer calls
241 InsetMathRef::ref_type_info InsetMathRef::types[] = {
242 { from_ascii("ref"), from_ascii(N_("Standard[[mathref]]")), from_ascii(N_("Ref: "))},
243 { from_ascii("eqref"), from_ascii(N_("Equation")), from_ascii(N_("EqRef: "))},
244 { from_ascii("pageref"), from_ascii(N_("Page Number")), from_ascii(N_("Page: "))},
245 { from_ascii("vpageref"), from_ascii(N_("Textual Page Number")), from_ascii(N_("TextPage: "))},
246 { from_ascii("vref"), from_ascii(N_("Standard+Textual Page")), from_ascii(N_("Ref+Text: "))},
247 { from_ascii("prettyref"), from_ascii(N_("PrettyRef")), from_ascii(N_("FormatRef: "))},
248 { from_ascii("nameref"), from_ascii(N_("Reference to Name")), from_ascii(N_("NameRef: "))},
249 { from_ascii(""), from_ascii(""), from_ascii("") }