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