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