]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathRef.cpp
Refactor InsetQuotes.h enums
[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 "ParIterator.h"
26 #include "xml.h"
27
28 #include "insets/InsetCommand.h"
29
30 #include "support/debug.h"
31 #include "support/gettext.h"
32
33 #include <ostream>
34
35 using namespace std;
36
37 namespace lyx {
38
39 InsetMathRef::InsetMathRef(Buffer * buf)
40         : InsetMathCommand(buf, from_ascii("ref"), false)
41 {}
42
43
44 InsetMathRef::InsetMathRef(Buffer * buf, docstring const & data)
45         : InsetMathCommand(buf, data, false)
46 {}
47
48
49 Inset * InsetMathRef::clone() const
50 {
51         return new InsetMathRef(*this);
52 }
53
54
55 void InsetMathRef::infoize(odocstream & os) const
56 {
57         os << "Ref: " << cell(0);
58 }
59
60
61 void InsetMathRef::doDispatch(Cursor & cur, FuncRequest & cmd)
62 {
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();
73                                 break;
74                         }
75                         MathData ar;
76                         if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
77                                 cur.recordUndo();
78                                 *this = *ar[0].nucleus()->asRefInset();
79                                 break;
80                         }
81                 }
82                 cur.undispatched();
83                 break;
84
85         case LFUN_INSET_DIALOG_UPDATE: {
86                 string const data = createDialogStr();
87                 cur.bv().updateDialog("ref", data);
88                 break;
89         }
90
91         case LFUN_MOUSE_RELEASE:
92                 if (cur.selection()) {
93                         cur.undispatched();
94                         break;
95                 }
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))));
100                         break;
101                 }
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);
106                         break;
107                 }
108                 cur.undispatched();
109                 break;
110
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
115                 // active.
116                 cur.leaveInset(*this);
117                 cur.bv().mouseSetCursor(cur, do_selection);
118                 break;
119         }
120
121         case LFUN_MOUSE_DOUBLE:
122         case LFUN_MOUSE_TRIPLE:
123                 // eat other mouse commands
124                 break;
125
126         default:
127                 InsetMathCommand::doDispatch(cur, cmd);
128                 break;
129         }
130 }
131
132
133 bool InsetMathRef::getStatus(Cursor & cur, FuncRequest const & cmd,
134                          FuncStatus & status) const
135 {
136         switch (cmd.action()) {
137         // we handle these
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);
145                 return true;
146         default:
147                 return InsetMathCommand::getStatus(cur, cmd, status);
148         }
149 }
150
151
152 docstring const InsetMathRef::screenLabel() const
153 {
154         docstring str;
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));
158                         break;
159                 }
160         }
161         str += asString(cell(0));
162
163         //if (/* !isLatex && */ !cell(0).empty()) {
164         //      str += "||";
165         //      str += asString(cell(1));
166         //}
167         return str;
168 }
169
170
171 void InsetMathRef::validate(LaTeXFeatures & features) const
172 {
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");
181 }
182
183
184 void InsetMathRef::docbook(XMLStream & xs, OutputParams const &) const
185 {
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));
189         } else {
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))
193                    << asString(cell(1))
194                    << xml::EndTag("link");
195         }
196 }
197
198
199 void InsetMathRef::updateBuffer(ParIterator const & it, UpdateType /*utype*/, bool const /*deleted*/)
200 {
201         if (!buffer_) {
202                 LYXERR0("InsetMathRef::updateBuffer: no buffer_!");
203                 return;
204         }
205         // register this inset into the buffer reference cache.
206         buffer().addReference(getTarget(), this, it);
207 }
208
209
210 string const InsetMathRef::createDialogStr() const
211 {
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);
217 }
218
219
220 docstring const InsetMathRef::getTarget() const
221 {
222         return asString(cell(0));
223 }
224
225
226 void InsetMathRef::changeTarget(docstring const & target)
227 {
228         InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
229         icp["reference"] = target;
230         if (!cell(1).empty())
231                 icp["name"] = asString(cell(1));
232         MathData ar;
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
238                 setBuffer(buf);
239         }
240 }
241
242
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("") }
252 };
253
254
255 } // namespace lyx