]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathRef.cpp
f17acde308ce9a8003ef40c2ca0cb17660d9e2a0
[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 "LaTeXFeatures.h"
17 #include "Buffer.h"
18 #include "Cursor.h"
19 #include "support/debug.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "support/gettext.h"
23 #include "MathData.h"
24 #include "MathFactory.h"
25 #include "MathSupport.h"
26 #include "OutputParams.h"
27 #include "sgml.h"
28
29 #include "insets/InsetCommand.h"
30
31 #include <ostream>
32
33 using namespace std;
34
35 namespace lyx {
36
37 InsetMathRef::InsetMathRef()
38         : CommandInset(from_ascii("ref"))
39 {}
40
41
42 InsetMathRef::InsetMathRef(docstring const & data)
43         : CommandInset(data)
44 {}
45
46
47 Inset * InsetMathRef::clone() const
48 {
49         return new InsetMathRef(*this);
50 }
51
52
53 void InsetMathRef::infoize(odocstream & os) const
54 {
55         os << "Ref: " << cell(0);
56 }
57
58
59 void InsetMathRef::doDispatch(Cursor & cur, FuncRequest & cmd)
60 {
61         switch (cmd.action) {
62         case LFUN_INSET_MODIFY:
63                 if (cmd.getArg(0) == "ref") {
64                         MathData ar;
65                         if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
66                                 *this = *ar[0].nucleus()->asRefInset();
67                                 break;
68                         }
69                 }
70                 cur.undispatched();
71                 break;
72
73         case LFUN_INSET_DIALOG_UPDATE: {
74                 string const data = createDialogStr("ref");
75                 cur.bv().updateDialog("ref", data);
76                 break;
77         }
78
79         case LFUN_MOUSE_RELEASE:
80                 if (cmd.button() == mouse_button::button3) {
81                         LYXERR0("trying to goto ref '" << to_utf8(asString(cell(0))) << "'");
82                         cur.bv().dispatch(FuncRequest(LFUN_LABEL_GOTO, asString(cell(0))));
83                         break;
84                 }
85                 if (cmd.button() == mouse_button::button1) {
86                         // Eventually trigger dialog with button 3, not 1
87                         string const data = createDialogStr("ref");
88                         cur.bv().showDialog("ref", data, this);
89                         break;
90                 }
91                 cur.undispatched();
92                 break;
93
94         case LFUN_MOUSE_PRESS:
95         case LFUN_MOUSE_MOTION:
96                 // eat other mouse commands
97                 break;
98
99         default:
100                 CommandInset::doDispatch(cur, cmd);
101                 break;
102         }
103 }
104
105
106 bool InsetMathRef::getStatus(Cursor & cur, FuncRequest const & cmd,
107                          FuncStatus & status) const
108 {
109         switch (cmd.action) {
110         // we handle these
111         case LFUN_INSET_MODIFY:
112         case LFUN_INSET_DIALOG_UPDATE:
113         case LFUN_MOUSE_RELEASE:
114         case LFUN_MOUSE_PRESS:
115         case LFUN_MOUSE_MOTION:
116                 status.enabled(true);
117                 return true;
118         default:
119                 return CommandInset::getStatus(cur, cmd, status);
120         }
121 }
122
123
124 docstring const InsetMathRef::screenLabel() const
125 {
126         docstring str;
127         for (int i = 0; !types[i].latex_name.empty(); ++i) {
128                 if (commandname() == types[i].latex_name) {
129                         str = _(to_utf8(types[i].short_gui_name));
130                         break;
131                 }
132         }
133         str += asString(cell(0));
134
135         //if (/* !isLatex && */ !cell(0).empty()) {
136         //      str += "||";
137         //      str += asString(cell(1));
138         //}
139         return str;
140 }
141
142
143 void InsetMathRef::validate(LaTeXFeatures & features) const
144 {
145         if (commandname() == "vref" || commandname() == "vpageref")
146                 features.require("varioref");
147         else if (commandname() == "prettyref")
148                 features.require("prettyref");
149 }
150
151
152 int InsetMathRef::docbook(Buffer const & buf, odocstream & os,
153                       OutputParams const & runparams) const
154 {
155         if (cell(1).empty()) {
156                 os << "<xref linkend=\""
157                    << sgml::cleanID(buf, runparams, asString(cell(0)));
158                 if (runparams.flavor == OutputParams::XML)
159                         os << "\"/>";
160                 else
161                         os << "\">";
162         } else {
163                 os << "<link linkend=\""
164                    << sgml::cleanID(buf, runparams, asString(cell(0)))
165                    << "\">"
166                    << asString(cell(1))
167                    << "</link>";
168         }
169
170         return 0;
171 }
172
173
174 string const InsetMathRef::createDialogStr(string const & name) const
175 {
176         InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
177         icp["reference"] = asString(cell(0));
178         if (!cell(1).empty())
179                 icp["name"] = asString(cell(1));
180         return InsetCommandMailer::params2string(name, icp);
181 }
182
183
184 InsetMathRef::ref_type_info InsetMathRef::types[] = {
185         { from_ascii("ref"),       from_ascii(N_("Standard[[mathref]]")),              from_ascii(N_("Ref: "))},
186         { from_ascii("eqref"),     from_ascii(N_("Equation")),              from_ascii(N_("EqRef: "))},
187         { from_ascii("pageref"),   from_ascii(N_("Page Number")),           from_ascii(N_("Page: "))},
188         { from_ascii("vpageref"),  from_ascii(N_("Textual Page Number")),   from_ascii(N_("TextPage: "))},
189         { from_ascii("vref"),      from_ascii(N_("Standard+Textual Page")), from_ascii(N_("Ref+Text: "))},
190         { from_ascii("prettyref"), from_ascii(N_("PrettyRef")),             from_ascii(N_("FormatRef: "))},
191         { from_ascii(""), from_ascii(""), from_ascii("") }
192 };
193
194
195 } // namespace lyx