]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathRef.cpp
Revert "XHTML: remove DOCTYPE, as the document is then understood as HTML4/XHTML1...
[lyx.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 "xml.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 void InsetMathRef::docbook(XMLStream & xs, OutputParams const &) const
186 {
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));
190         } else {
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))
194                    << asString(cell(1))
195                    << xml::EndTag("link");
196         }
197 }
198
199
200 void InsetMathRef::updateBuffer(ParIterator const & it, UpdateType /*utype*/, bool const /*deleted*/)
201 {
202         if (!buffer_) {
203                 LYXERR0("InsetMathRef::updateBuffer: no buffer_!");
204                 return;
205         }
206         // register this inset into the buffer reference cache.
207         buffer().addReference(getTarget(), this, it);
208 }
209
210
211 string const InsetMathRef::createDialogStr() const
212 {
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);
218 }
219
220
221 docstring const InsetMathRef::getTarget() const
222 {
223         return asString(cell(0));
224 }
225
226
227 void InsetMathRef::changeTarget(docstring const & target)
228 {
229         InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
230         icp["reference"] = target;
231         if (!cell(1).empty())
232                 icp["name"] = asString(cell(1));
233         MathData ar;
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
239                 setBuffer(buf);
240         }
241 }
242
243
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("") }
253 };
254
255
256 } // namespace lyx