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 "ParIterator.h"
28 #include "insets/InsetCommand.h"
30 #include "support/debug.h"
31 #include "support/gettext.h"
39 InsetMathRef::InsetMathRef(Buffer * buf)
40 : InsetMathCommand(buf, from_ascii("ref"), false)
44 InsetMathRef::InsetMathRef(Buffer * buf, docstring const & data)
45 : InsetMathCommand(buf, data, false)
49 Inset * InsetMathRef::clone() const
51 return new InsetMathRef(*this);
55 void InsetMathRef::infoize(odocstream & os) const
57 os << "Ref: " << cell(0);
61 void InsetMathRef::doDispatch(Cursor & cur, FuncRequest & cmd)
63 switch (cmd.action()) {
64 case LFUN_INSET_MODIFY:
65 if (cmd.getArg(0) == "ref") {
66 if (cmd.getArg(1) == "changetarget") {
67 string const oldtarget = cmd.getArg(2);
68 string const newtarget = cmd.getArg(3);
69 if (!oldtarget.empty() && !newtarget.empty()
70 && asString(cell(0)) == from_utf8(oldtarget))
71 changeTarget(from_utf8(newtarget));
72 cur.forceBufferUpdate();
76 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
78 *this = *ar[0].nucleus()->asRefInset();
85 case LFUN_INSET_DIALOG_UPDATE: {
86 string const data = createDialogStr();
87 cur.bv().updateDialog("ref", data);
91 case LFUN_MOUSE_RELEASE:
92 if (cur.selection()) {
96 if (cmd.button() == mouse_button::button3) {
97 LYXERR0("trying to goto ref '" << to_utf8(asString(cell(0))) << "'");
98 //FIXME: use DispatchResult argument
99 lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO, asString(cell(0))));
102 if (cmd.button() == mouse_button::button1) {
103 // Eventually trigger dialog with button 3, not 1
104 string const data = createDialogStr();
105 cur.bv().showDialog("ref", data, this);
111 case LFUN_MOUSE_PRESS: {
112 bool do_selection = cmd.button() == mouse_button::button1
113 && cmd.modifier() == ShiftModifier;
114 // For some reason the cursor points inside the first cell, which is not
116 cur.leaveInset(*this);
117 cur.bv().mouseSetCursor(cur, do_selection);
121 case LFUN_MOUSE_DOUBLE:
122 case LFUN_MOUSE_TRIPLE:
123 // eat other mouse commands
127 InsetMathCommand::doDispatch(cur, cmd);
133 bool InsetMathRef::getStatus(Cursor & cur, FuncRequest const & cmd,
134 FuncStatus & status) const
136 switch (cmd.action()) {
138 case LFUN_INSET_MODIFY:
139 case LFUN_INSET_DIALOG_UPDATE:
140 case LFUN_MOUSE_RELEASE:
141 case LFUN_MOUSE_PRESS:
142 case LFUN_MOUSE_DOUBLE:
143 case LFUN_MOUSE_TRIPLE:
144 status.setEnabled(true);
147 return InsetMathCommand::getStatus(cur, cmd, status);
152 docstring const InsetMathRef::screenLabel() const
155 for (int i = 0; !types[i].latex_name.empty(); ++i) {
156 if (commandname() == types[i].latex_name) {
157 str = _(to_utf8(types[i].short_gui_name));
161 str += asString(cell(0));
163 //if (/* !isLatex && */ !cell(0).empty()) {
165 // str += asString(cell(1));
171 void InsetMathRef::validate(LaTeXFeatures & features) const
173 if (commandname() == "vref" || commandname() == "vpageref")
174 features.require("varioref");
175 else if (commandname() == "prettyref")
176 features.require("prettyref");
177 else if (commandname() == "eqref")
178 features.require("amsmath");
179 else if (commandname() == "nameref")
180 features.require("nameref");
184 void InsetMathRef::docbook(XMLStream & xs, OutputParams const &) const
186 if (cell(1).empty()) {
187 docstring attr = from_utf8("linkend=\"") + xml::cleanID(asString(cell(0))) + from_utf8("\"");
188 xs << xml::CompTag("xref", to_utf8(attr));
190 // Link with linkend, as is it within the document (not outside, in which case xlink:href is better suited).
191 docstring attr = from_utf8("linkend=\"") + xml::cleanID(asString(cell(0))) + from_utf8("\"");
192 xs << xml::StartTag("link", to_utf8(attr))
194 << xml::EndTag("link");
199 void InsetMathRef::updateBuffer(ParIterator const & it, UpdateType /*utype*/, bool const /*deleted*/)
202 LYXERR0("InsetMathRef::updateBuffer: no buffer_!");
205 // register this inset into the buffer reference cache.
206 buffer().addReference(getTarget(), this, it);
210 string const InsetMathRef::createDialogStr() const
212 InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
213 icp["reference"] = asString(cell(0));
214 if (!cell(1).empty())
215 icp["name"] = asString(cell(1));
216 return InsetCommand::params2string(icp);
220 docstring const InsetMathRef::getTarget() const
222 return asString(cell(0));
226 void InsetMathRef::changeTarget(docstring const & target)
228 InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
229 icp["reference"] = target;
230 if (!cell(1).empty())
231 icp["name"] = asString(cell(1));
233 Buffer & buf = buffer();
234 if (createInsetMath_fromDialogStr(
235 from_utf8(InsetCommand::params2string(icp)), ar)) {
236 *this = *ar[0].nucleus()->asRefInset();
237 // FIXME audit setBuffer calls
243 InsetMathRef::ref_type_info InsetMathRef::types[] = {
244 { from_ascii("ref"), from_ascii(N_("Standard[[mathref]]")), from_ascii(N_("Ref: "))},
245 { from_ascii("eqref"), from_ascii(N_("Equation")), from_ascii(N_("EqRef: "))},
246 { from_ascii("pageref"), from_ascii(N_("Page Number")), from_ascii(N_("Page: "))},
247 { from_ascii("vpageref"), from_ascii(N_("Textual Page Number")), from_ascii(N_("TextPage: "))},
248 { from_ascii("vref"), from_ascii(N_("Standard+Textual Page")), from_ascii(N_("Ref+Text: "))},
249 { from_ascii("prettyref"), from_ascii(N_("PrettyRef")), from_ascii(N_("FormatRef: "))},
250 { from_ascii("nameref"), from_ascii(N_("Reference to Name")), from_ascii(N_("NameRef: "))},
251 { from_ascii(""), from_ascii(""), from_ascii("") }