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