]> git.lyx.org Git - lyx.git/blob - src/mathed/ref_inset.C
Jean-Marc's fix for wrong descent
[lyx.git] / src / mathed / ref_inset.C
1
2 #include <config.h>
3
4 #include "ref_inset.h"
5 #include "funcrequest.h"
6 #include "formulabase.h"
7 #include "BufferView.h"
8 #include "frontends/LyXView.h"
9 #include "frontends/Painter.h"
10 #include "frontends/Dialogs.h"
11 #include "lyxfunc.h"
12 #include "gettext.h"
13 #include "LaTeXFeatures.h"
14 #include "debug.h"
15 #include "math_mathmlstream.h"
16 #include "Lsstream.h"
17 #include "math_parser.h"
18 #include "support/lstrings.h"
19
20
21 RefInset::RefInset()
22         : CommandInset("ref")
23 {}
24
25
26 RefInset::RefInset(string const & data)
27         : CommandInset(data)
28 {}
29
30
31 MathInset * RefInset::clone() const
32 {
33         return new RefInset(*this);
34 }
35
36
37 void RefInset::infoize(std::ostream & os) const
38 {
39         os << "Ref: " << cell(0);
40 }
41
42
43 dispatch_result
44 RefInset::dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos)
45 {
46         switch (cmd.action) {
47                 case LFUN_MOUSE_RELEASE:
48                         if (cmd.button() == mouse_button::button3) {
49                                 lyxerr << "trying to goto ref" << cell(0) << "\n";
50                                 cmd.view()->dispatch(FuncRequest(LFUN_REF_GOTO, asString(cell(0))));
51                                 return DISPATCHED;
52                         }
53                         if (cmd.button() == mouse_button::button1) {
54                                 // Eventually trigger dialog with button 3
55                                 // not 1
56                                 ostringstream data;
57                                 data << "ref LatexCommand ";
58                                 WriteStream wsdata(data);
59                                 write(wsdata);
60                                 wsdata << "\n\\end_inset\n\n";
61
62                                 cmd.view()->owner()->getDialogs().
63                                         show("ref", data.str(), this);
64                                 return DISPATCHED;
65                         }
66                         break;
67                 case LFUN_MOUSE_PRESS:
68                 case LFUN_MOUSE_MOTION:
69                         // eat other mouse commands
70                         return DISPATCHED;
71                 default:
72                         return CommandInset::dispatch(cmd, idx, pos);
73         }
74         // not our business
75         return UNDISPATCHED;
76 }
77
78
79 string RefInset::screenLabel() const
80 {
81         string str;
82         for (int i = 0; !types[i].latex_name.empty(); ++i)
83                 if (name_ == types[i].latex_name) {
84                         str = _(types[i].short_gui_name);
85                         break;
86                 }
87         str += asString(cell(0));
88
89         //if (/* !isLatex && */ !cell(0).empty()) {
90         //      str += "||";
91         //      str += asString(cell(1));
92         //}
93         return str;
94 }
95
96
97 void RefInset::validate(LaTeXFeatures & features) const
98 {
99         if (name_ == "vref" || name_ == "vpageref")
100                 features.require("varioref");
101         else if (name_ == "prettyref")
102                 features.require("prettyref");
103 }
104
105
106 int RefInset::ascii(std::ostream & os, int) const
107 {
108         os << '[' << asString(cell(0)) << ']';
109         return 0;
110 }
111
112
113 int RefInset::linuxdoc(std::ostream & os) const
114 {
115         os << "<ref id=\"" << asString(cell(0))
116            << "\" name=\"" << asString(cell(1)) << "\" >";
117         return 0;
118 }
119
120
121 int RefInset::docbook(std::ostream & os, bool) const
122 {
123         if (cell(1).empty()) {
124                 os << "<xref linkend=\"" << asString(cell(0)) << "\">";
125         } else {
126                 os << "<link linkend=\"" << asString(cell(0))
127                    << "\">" << asString(cell(1)) << "</link>";
128         }
129
130         return 0;
131 }
132
133
134 dispatch_result RefInset::localDispatch(FuncRequest const & cmd)
135 {
136         if (cmd.action != LFUN_INSET_MODIFY || cmd.getArg(0) != "ref")
137                 return UNDISPATCHED;
138
139         MathArray ar;
140         if (!string2RefInset(cmd.argument, ar))
141                 return UNDISPATCHED;
142
143         *this = *ar[0].nucleus()->asRefInset();
144 //      if (cmd.view())
145 //                 // This does not compile because updateInset expects
146 //                 // an Inset* and 'this' isn't.
147 //              cmd.view()->updateInset(this, true);
148         return DISPATCHED;
149 }
150
151
152 bool string2RefInset(string const & str, MathArray & ar)
153 {
154         string name;
155         string body = split(str, name, ' ');
156
157         if (name != "ref")
158                 return false;
159
160         // body comes with a head "LatexCommand " and a
161         // tail "\nend_inset\n\n". Strip them off.
162         string trimmed;
163         body = split(body, trimmed, ' ');
164         split(body, trimmed, '\n');
165
166         mathed_parse_cell(ar, trimmed);
167         if (ar.size() != 1)
168                 return false;
169
170         return ar[0].nucleus()->asRefInset();
171 }
172
173
174 RefInset::ref_type_info RefInset::types[] = {
175         { "ref",        N_("Standard"),                 N_("Ref: ")},
176         { "pageref",    N_("Page Number"),              N_("Page: ")},
177         { "vpageref",   N_("Textual Page Number"),      N_("TextPage: ")},
178         { "vref",       N_("Standard+Textual Page"),    N_("Ref+Text: ")},
179         { "prettyref",  N_("PrettyRef"),                N_("PrettyRef: ")},
180         { "", "", "" }
181 };