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") {
67 if (cmd.getArg(1) == "changetarget") {
68 string const oldtarget = cmd.getArg(2);
69 string const newtarget = cmd.getArg(3);
70 if (!oldtarget.empty() && !newtarget.empty()
71 && asString(cell(0)) == from_utf8(oldtarget))
72 changeTarget(from_utf8(newtarget));
73 cur.forceBufferUpdate();
77 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
79 *this = *ar[0].nucleus()->asRefInset();
86 case LFUN_INSET_DIALOG_UPDATE: {
87 string const data = createDialogStr();
88 cur.bv().updateDialog("ref", data);
92 case LFUN_MOUSE_RELEASE:
93 if (cur.selection()) {
97 if (cmd.button() == mouse_button::button3) {
98 LYXERR0("trying to goto ref '" << to_utf8(asString(cell(0))) << "'");
99 //FIXME: use DispatchResult argument
100 lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO, asString(cell(0))));
103 if (cmd.button() == mouse_button::button1) {
104 // Eventually trigger dialog with button 3, not 1
105 string const data = createDialogStr();
106 cur.bv().showDialog("ref", data, this);
112 case LFUN_MOUSE_PRESS: {
113 bool do_selection = cmd.button() == mouse_button::button1
114 && cmd.modifier() == ShiftModifier;
115 // For some reason the cursor points inside the first cell, which is not
117 cur.leaveInset(*this);
118 cur.bv().mouseSetCursor(cur, do_selection);
122 case LFUN_MOUSE_DOUBLE:
123 case LFUN_MOUSE_TRIPLE:
124 // eat other mouse commands
128 InsetMathCommand::doDispatch(cur, cmd);
134 bool InsetMathRef::getStatus(Cursor & cur, FuncRequest const & cmd,
135 FuncStatus & status) const
137 switch (cmd.action()) {
139 case LFUN_INSET_MODIFY:
140 case LFUN_INSET_DIALOG_UPDATE:
141 case LFUN_MOUSE_RELEASE:
142 case LFUN_MOUSE_PRESS:
143 case LFUN_MOUSE_DOUBLE:
144 case LFUN_MOUSE_TRIPLE:
145 status.setEnabled(true);
148 return InsetMathCommand::getStatus(cur, cmd, status);
153 docstring const InsetMathRef::screenLabel() const
156 for (int i = 0; !types[i].latex_name.empty(); ++i) {
157 if (commandname() == types[i].latex_name) {
158 str = _(to_utf8(types[i].short_gui_name));
162 str += asString(cell(0));
164 //if (/* !isLatex && */ !cell(0).empty()) {
166 // str += asString(cell(1));
172 void InsetMathRef::validate(LaTeXFeatures & features) const
174 if (commandname() == "vref" || commandname() == "vpageref")
175 features.require("varioref");
176 else if (commandname() == "prettyref")
177 features.require("prettyref");
178 else if (commandname() == "eqref")
179 features.require("amsmath");
180 else if (commandname() == "nameref")
181 features.require("nameref");
185 void InsetMathRef::docbook(XMLStream & xs, OutputParams const &) const
187 if (cell(1).empty()) {
188 docstring attr = from_utf8("linkend=\"") + xml::cleanID(asString(cell(0))) + from_utf8("\"");
189 xs << xml::CompTag("xref", to_utf8(attr));
191 // Link with linkend, as is it within the document (not outside, in which case xlink:href is better suited).
192 docstring attr = from_utf8("linkend=\"") + xml::cleanID(asString(cell(0))) + from_utf8("\"");
193 xs << xml::StartTag("link", to_utf8(attr))
195 << xml::EndTag("link");
200 void InsetMathRef::updateBuffer(ParIterator const & it, UpdateType /*utype*/, bool const /*deleted*/)
203 LYXERR0("InsetMathRef::updateBuffer: no buffer_!");
206 // register this inset into the buffer reference cache.
207 buffer().addReference(getTarget(), this, it);
211 string const InsetMathRef::createDialogStr() const
213 InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
214 icp["reference"] = asString(cell(0));
215 if (!cell(1).empty())
216 icp["name"] = asString(cell(1));
217 return InsetCommand::params2string(icp);
221 docstring const InsetMathRef::getTarget() const
223 return asString(cell(0));
227 void InsetMathRef::changeTarget(docstring const & target)
229 InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
230 icp["reference"] = target;
231 if (!cell(1).empty())
232 icp["name"] = asString(cell(1));
234 Buffer & buf = buffer();
235 if (createInsetMath_fromDialogStr(
236 from_utf8(InsetCommand::params2string(icp)), ar)) {
237 *this = *ar[0].nucleus()->asRefInset();
238 // FIXME audit setBuffer calls
244 InsetMathRef::ref_type_info InsetMathRef::types[] = {
245 { from_ascii("ref"), from_ascii(N_("Standard[[mathref]]")), from_ascii(N_("Ref: "))},
246 { from_ascii("eqref"), from_ascii(N_("Equation")), from_ascii(N_("EqRef: "))},
247 { from_ascii("pageref"), from_ascii(N_("Page Number")), from_ascii(N_("Page: "))},
248 { from_ascii("vpageref"), from_ascii(N_("Textual Page Number")), from_ascii(N_("TextPage: "))},
249 { from_ascii("vref"), from_ascii(N_("Standard+Textual Page")), from_ascii(N_("Ref+Text: "))},
250 { from_ascii("prettyref"), from_ascii(N_("PrettyRef")), from_ascii(N_("FormatRef: "))},
251 { from_ascii("nameref"), from_ascii(N_("Reference to Name")), from_ascii(N_("NameRef: "))},
252 { from_ascii(""), from_ascii(""), from_ascii("") }