]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathRef.C
move everything into namespace lyx
[lyx.git] / src / mathed / InsetMathRef.C
1 /**
2  * \file InsetMathRef.C
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
30 namespace lyx {
31
32 using std::string;
33 using std::auto_ptr;
34 using std::endl;
35
36
37 RefInset::RefInset()
38         : CommandInset("ref")
39 {}
40
41
42 RefInset::RefInset(string const & data)
43         : CommandInset(data)
44 {}
45
46
47 auto_ptr<InsetBase> RefInset::doClone() const
48 {
49         return auto_ptr<InsetBase>(new RefInset(*this));
50 }
51
52
53 void RefInset::infoize(std::ostream & os) const
54 {
55         os << "Ref: " << cell(0);
56 }
57
58
59 void RefInset::doDispatch(LCursor & cur, FuncRequest & cmd)
60 {
61         switch (cmd.action) {
62         case LFUN_INSET_MODIFY:
63                 if (cmd.getArg(0) == "ref") {
64                         MathArray ar;
65                         if (createInsetMath_fromDialogStr(to_utf8(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                         lyxerr << "trying to goto ref '" << asString(cell(0)) << "'" << endl;
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().showInsetDialog("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 RefInset::getStatus(LCursor & 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 RefInset::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 = _(types[i].short_gui_name);
130                         break;
131                 }
132         // FIXME UNICODE
133         str += from_utf8(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 RefInset::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 RefInset::plaintext(odocstream & os, OutputParams const &) const
153 {
154         // FIXME UNICODE
155         os << '[' << from_utf8(asString(cell(0))) << ']';
156         return 0;
157 }
158
159
160 int RefInset::docbook(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
161 {
162         if (cell(1).empty()) {
163                 // FIXME UNICODE
164                 os << "<xref linkend=\""
165                    << from_ascii(sgml::cleanID(buf, runparams, asString(cell(0))));
166                 if (runparams.flavor == OutputParams::XML)
167                         os << "\"/>";
168                 else
169                         os << "\">";
170         } else {
171                 // FIXME UNICODE
172                 os << "<link linkend=\""
173                    << from_ascii(sgml::cleanID(buf, runparams, asString(cell(0))))
174                    << "\">"
175                    << from_ascii(asString(cell(1)))
176                    << "</link>";
177         }
178
179         return 0;
180 }
181
182
183 RefInset::ref_type_info RefInset::types[] = {
184         { "ref",       N_("Standard"),              N_("Ref: ")},
185         { "eqref",     N_("Equation"),              N_("EqRef: ")},
186         { "pageref",   N_("Page Number"),           N_("Page: ")},
187         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
188         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
189         { "prettyref", N_("PrettyRef"),             N_("PrettyRef: ")},
190         { "", "", "" }
191 };
192
193
194 } // namespace lyx