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 int InsetMathRef::docbook(odocstream & os, OutputParams const & runparams) const
187 if (cell(1).empty()) {
188 os << "<xref linkend=\""
189 << sgml::cleanID(buffer(), runparams, asString(cell(0)));
190 if (runparams.flavor == OutputParams::XML)
195 os << "<link linkend=\""
196 << sgml::cleanID(buffer(), runparams, asString(cell(0)))
206 void InsetMathRef::updateBuffer(ParIterator const & it, UpdateType /*utype*/, bool const /*deleted*/)
209 LYXERR0("InsetMathRef::updateBuffer: no buffer_!");
212 // register this inset into the buffer reference cache.
213 buffer().addReference(getTarget(), this, it);
217 string const InsetMathRef::createDialogStr() const
219 InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
220 icp["reference"] = asString(cell(0));
221 if (!cell(1).empty())
222 icp["name"] = asString(cell(1));
223 return InsetCommand::params2string(icp);
227 docstring const InsetMathRef::getTarget() const
229 return asString(cell(0));
233 void InsetMathRef::changeTarget(docstring const & target)
235 InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
236 icp["reference"] = target;
237 if (!cell(1).empty())
238 icp["name"] = asString(cell(1));
240 Buffer & buf = buffer();
241 if (createInsetMath_fromDialogStr(
242 from_utf8(InsetCommand::params2string(icp)), ar)) {
243 *this = *ar[0].nucleus()->asRefInset();
244 // FIXME audit setBuffer calls
250 InsetMathRef::ref_type_info InsetMathRef::types[] = {
251 { from_ascii("ref"), from_ascii(N_("Standard[[mathref]]")), from_ascii(N_("Ref: "))},
252 { from_ascii("eqref"), from_ascii(N_("Equation")), from_ascii(N_("EqRef: "))},
253 { from_ascii("pageref"), from_ascii(N_("Page Number")), from_ascii(N_("Page: "))},
254 { from_ascii("vpageref"), from_ascii(N_("Textual Page Number")), from_ascii(N_("TextPage: "))},
255 { from_ascii("vref"), from_ascii(N_("Standard+Textual Page")), from_ascii(N_("Ref+Text: "))},
256 { from_ascii("prettyref"), from_ascii(N_("PrettyRef")), from_ascii(N_("FormatRef: "))},
257 { from_ascii("nameref"), from_ascii(N_("Reference to Name")), from_ascii(N_("NameRef: "))},
258 { from_ascii(""), from_ascii(""), from_ascii("") }