]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathRef.cpp
Revert "Give a 2 pixels space for markers around math objects"
[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 "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         : CommandInset(buf, from_ascii("ref"), false)
42 {}
43
44
45 InsetMathRef::InsetMathRef(Buffer * buf, docstring const & data)
46         : CommandInset(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                         MathData ar;
68                         if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
69                                 cur.recordUndo();
70                                 *this = *ar[0].nucleus()->asRefInset();
71                                 break;
72                         }
73                 }
74                 cur.undispatched();
75                 break;
76
77         case LFUN_INSET_DIALOG_UPDATE: {
78                 string const data = createDialogStr();
79                 cur.bv().updateDialog("ref", data);
80                 break;
81         }
82
83         case LFUN_MOUSE_RELEASE:
84                 if (cur.selection()) {
85                         cur.undispatched();
86                         break;
87                 }
88                 if (cmd.button() == mouse_button::button3) {
89                         LYXERR0("trying to goto ref '" << to_utf8(asString(cell(0))) << "'");
90                         //FIXME: use DispatchResult argument
91                         lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO, asString(cell(0))));
92                         break;
93                 }
94                 if (cmd.button() == mouse_button::button1) {
95                         // Eventually trigger dialog with button 3, not 1
96                         string const data = createDialogStr();
97                         cur.bv().showDialog("ref", data, this);
98                         break;
99                 }
100                 cur.undispatched();
101                 break;
102
103         case LFUN_MOUSE_PRESS: {
104                 bool do_selection = cmd.button() == mouse_button::button1
105                         && cmd.modifier() == ShiftModifier;
106                 // For some reason the cursor points inside the first cell, which is not
107                 // active.
108                 cur.leaveInset(*this);
109                 cur.bv().mouseSetCursor(cur, do_selection);
110                 break;
111         }
112
113         case LFUN_MOUSE_DOUBLE:
114         case LFUN_MOUSE_TRIPLE:
115                 // eat other mouse commands
116                 break;
117
118         default:
119                 CommandInset::doDispatch(cur, cmd);
120                 break;
121         }
122 }
123
124
125 bool InsetMathRef::getStatus(Cursor & cur, FuncRequest const & cmd,
126                          FuncStatus & status) const
127 {
128         switch (cmd.action()) {
129         // we handle these
130         case LFUN_INSET_MODIFY:
131         case LFUN_INSET_DIALOG_UPDATE:
132         case LFUN_MOUSE_RELEASE:
133         case LFUN_MOUSE_PRESS:
134         case LFUN_MOUSE_DOUBLE:
135         case LFUN_MOUSE_TRIPLE:
136                 status.setEnabled(true);
137                 return true;
138         default:
139                 return CommandInset::getStatus(cur, cmd, status);
140         }
141 }
142
143
144 docstring const InsetMathRef::screenLabel() const
145 {
146         docstring str;
147         for (int i = 0; !types[i].latex_name.empty(); ++i) {
148                 if (commandname() == types[i].latex_name) {
149                         str = _(to_utf8(types[i].short_gui_name));
150                         break;
151                 }
152         }
153         str += asString(cell(0));
154
155         //if (/* !isLatex && */ !cell(0).empty()) {
156         //      str += "||";
157         //      str += asString(cell(1));
158         //}
159         return str;
160 }
161
162
163 void InsetMathRef::validate(LaTeXFeatures & features) const
164 {
165         if (commandname() == "vref" || commandname() == "vpageref")
166                 features.require("varioref");
167         else if (commandname() == "prettyref")
168                 features.require("prettyref");
169         else if (commandname() == "eqref")
170                 features.require("amsmath");
171 }
172
173
174 int InsetMathRef::docbook(odocstream & os, OutputParams const & runparams) const
175 {
176         if (cell(1).empty()) {
177                 os << "<xref linkend=\""
178                    << sgml::cleanID(buffer(), runparams, asString(cell(0)));
179                 if (runparams.flavor == OutputParams::XML)
180                         os << "\"/>";
181                 else
182                         os << "\">";
183         } else {
184                 os << "<link linkend=\""
185                    << sgml::cleanID(buffer(), runparams, asString(cell(0)))
186                    << "\">"
187                    << asString(cell(1))
188                    << "</link>";
189         }
190
191         return 0;
192 }
193
194
195 void InsetMathRef::updateBuffer(ParIterator const & it, UpdateType /*utype*/)
196 {
197         if (!buffer_) {
198                 LYXERR0("InsetMathRef::updateBuffer: no buffer_!");
199                 return;
200         }
201         // register this inset into the buffer reference cache.
202         buffer().addReference(getTarget(), this, it);
203 }
204
205
206 string const InsetMathRef::createDialogStr() const
207 {
208         InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
209         icp["reference"] = asString(cell(0));
210         if (!cell(1).empty())
211                 icp["name"] = asString(cell(1));
212         return InsetCommand::params2string(icp);
213 }
214
215
216 docstring const InsetMathRef::getTarget() const
217 {
218         return asString(cell(0));
219 }
220
221
222 void InsetMathRef::changeTarget(docstring const & target)
223 {
224         InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
225         icp["reference"] = target;
226         if (!cell(1).empty())
227                 icp["name"] = asString(cell(1));
228         MathData ar;
229         Buffer & buf = buffer();
230         if (createInsetMath_fromDialogStr(
231             from_utf8(InsetCommand::params2string(icp)), ar)) {
232                 *this = *ar[0].nucleus()->asRefInset();
233                 // FIXME audit setBuffer calls
234                 setBuffer(buf);
235         }
236 }
237
238
239 InsetMathRef::ref_type_info InsetMathRef::types[] = {
240         { from_ascii("ref"),       from_ascii(N_("Standard[[mathref]]")),   from_ascii(N_("Ref: "))},
241         { from_ascii("eqref"),     from_ascii(N_("Equation")),              from_ascii(N_("EqRef: "))},
242         { from_ascii("pageref"),   from_ascii(N_("Page Number")),           from_ascii(N_("Page: "))},
243         { from_ascii("vpageref"),  from_ascii(N_("Textual Page Number")),   from_ascii(N_("TextPage: "))},
244         { from_ascii("vref"),      from_ascii(N_("Standard+Textual Page")), from_ascii(N_("Ref+Text: "))},
245         { from_ascii("prettyref"), from_ascii(N_("PrettyRef")),             from_ascii(N_("FormatRef: "))},
246         { from_ascii(""), from_ascii(""), from_ascii("") }
247 };
248
249
250 } // namespace lyx