]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathRef.cpp
6a911afa57b2a53e73dd147f0c1133448dc01c1d
[features.git] / src / mathed / InsetMathRef.cpp
1 /**
2  * \file InsetMathRef.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathRef.h"
14
15 #include "BufferView.h"
16 #include "Buffer.h"
17 #include "Cursor.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
20 #include "LaTeXFeatures.h"
21 #include "LyX.h"
22 #include "MathData.h"
23 #include "MathFactory.h"
24 #include "MathSupport.h"
25 #include "OutputParams.h"
26 #include "ParIterator.h"
27 #include "sgml.h"
28
29 #include "insets/InsetCommand.h"
30
31 #include "support/debug.h"
32 #include "support/gettext.h"
33
34 #include <ostream>
35
36 using namespace std;
37
38 namespace lyx {
39
40 InsetMathRef::InsetMathRef(Buffer * buf)
41         : InsetMathCommand(buf, from_ascii("ref"), false)
42 {}
43
44
45 InsetMathRef::InsetMathRef(Buffer * buf, docstring const & data)
46         : InsetMathCommand(buf, data, false)
47 {}
48
49
50 Inset * InsetMathRef::clone() const
51 {
52         return new InsetMathRef(*this);
53 }
54
55
56 void InsetMathRef::infoize(odocstream & os) const
57 {
58         os << "Ref: " << cell(0);
59 }
60
61
62 void InsetMathRef::doDispatch(Cursor & cur, FuncRequest & cmd)
63 {
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();
74                                 break;
75                         }
76                         MathData ar;
77                         if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
78                                 cur.recordUndo();
79                                 *this = *ar[0].nucleus()->asRefInset();
80                                 break;
81                         }
82                 }
83                 cur.undispatched();
84                 break;
85
86         case LFUN_INSET_DIALOG_UPDATE: {
87                 string const data = createDialogStr();
88                 cur.bv().updateDialog("ref", data);
89                 break;
90         }
91
92         case LFUN_MOUSE_RELEASE:
93                 if (cur.selection()) {
94                         cur.undispatched();
95                         break;
96                 }
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))));
101                         break;
102                 }
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);
107                         break;
108                 }
109                 cur.undispatched();
110                 break;
111
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
116                 // active.
117                 cur.leaveInset(*this);
118                 cur.bv().mouseSetCursor(cur, do_selection);
119                 break;
120         }
121
122         case LFUN_MOUSE_DOUBLE:
123         case LFUN_MOUSE_TRIPLE:
124                 // eat other mouse commands
125                 break;
126
127         default:
128                 InsetMathCommand::doDispatch(cur, cmd);
129                 break;
130         }
131 }
132
133
134 bool InsetMathRef::getStatus(Cursor & cur, FuncRequest const & cmd,
135                          FuncStatus & status) const
136 {
137         switch (cmd.action()) {
138         // we handle these
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);
146                 return true;
147         default:
148                 return InsetMathCommand::getStatus(cur, cmd, status);
149         }
150 }
151
152
153 docstring const InsetMathRef::screenLabel() const
154 {
155         docstring str;
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));
159                         break;
160                 }
161         }
162         str += asString(cell(0));
163
164         //if (/* !isLatex && */ !cell(0).empty()) {
165         //      str += "||";
166         //      str += asString(cell(1));
167         //}
168         return str;
169 }
170
171
172 void InsetMathRef::validate(LaTeXFeatures & features) const
173 {
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");
182 }
183
184
185 int InsetMathRef::docbook(odocstream & os, OutputParams const & runparams) const
186 {
187         if (cell(1).empty()) {
188                 os << "<xref linkend=\""
189                    << sgml::cleanID(buffer(), runparams, asString(cell(0)));
190                 if (runparams.flavor == OutputParams::XML)
191                         os << "\"/>";
192                 else
193                         os << "\">";
194         } else {
195                 os << "<link linkend=\""
196                    << sgml::cleanID(buffer(), runparams, asString(cell(0)))
197                    << "\">"
198                    << asString(cell(1))
199                    << "</link>";
200         }
201
202         return 0;
203 }
204
205
206 void InsetMathRef::updateBuffer(ParIterator const & it, UpdateType /*utype*/, bool const /*deleted*/)
207 {
208         if (!buffer_) {
209                 LYXERR0("InsetMathRef::updateBuffer: no buffer_!");
210                 return;
211         }
212         // register this inset into the buffer reference cache.
213         buffer().addReference(getTarget(), this, it);
214 }
215
216
217 string const InsetMathRef::createDialogStr() const
218 {
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);
224 }
225
226
227 docstring const InsetMathRef::getTarget() const
228 {
229         return asString(cell(0));
230 }
231
232
233 void InsetMathRef::changeTarget(docstring const & target)
234 {
235         InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
236         icp["reference"] = target;
237         if (!cell(1).empty())
238                 icp["name"] = asString(cell(1));
239         MathData ar;
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
245                 setBuffer(buf);
246         }
247 }
248
249
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("") }
259 };
260
261
262 } // namespace lyx