]> git.lyx.org Git - lyx.git/blob - src/mathed/math_boxinset.C
most of the so far unapplied stuff from porto including proper support for
[lyx.git] / src / mathed / math_boxinset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_boxinset.h"
8 #include "math_support.h"
9 #include "math_mathmlstream.h"
10 #include "math_streamstr.h"
11
12 #include "math_cursor.h"
13 #include "commandtags.h"
14 #include "formulabase.h"
15 #include "BufferView.h"
16 #include "frontends/LyXView.h"
17 #include "frontends/Painter.h"
18 #include "frontends/Dialogs.h"
19 #include "lyxfunc.h"
20 #include "gettext.h"
21 #include "LaTeXFeatures.h"
22
23
24 ButtonInset::ButtonInset()
25         : MathNestInset(2)
26 {}
27
28
29 void ButtonInset::metrics(MathMetricsInfo & mi) const
30 {
31         MathFontSetChanger dummy(mi.base, "textnormal");
32         if (editing()) {
33                 MathNestInset::metrics(mi);
34                 width_   = xcell(0).width() + xcell(1).width() + 4;
35                 ascent_  = max(xcell(0).ascent(), xcell(1).ascent());
36                 descent_ = max(xcell(0).descent(), xcell(1).descent());
37         } else {
38                 string s = screenLabel();
39                 mathed_string_dim(mi.base.font,
40                                  s, ascent_, descent_, width_);
41                 width_ += 10;
42         }
43 }
44
45
46 void ButtonInset::draw(MathPainterInfo & pi, int x, int y) const
47 {
48         MathFontSetChanger dummy(pi.base, "textnormal");
49         if (editing()) {
50                 xcell(0).draw(pi, x, y);
51                 xcell(1).draw(pi, x + xcell(0).width() + 2, y);
52                 mathed_draw_framebox(pi, x, y, this);
53         } else {
54                 pi.pain.buttonText(x + 2, y, screenLabel(),
55                         pi.base.font);
56         }
57 }
58
59
60 ////////////////////////////////
61
62 CommandInset::CommandInset(string const & data)
63 {
64         lock_ = true;
65
66         string::size_type idx0 = data.find("|++|");
67         name_ = data.substr(0, idx0);
68         if (idx0 == string::npos)
69                 return;
70         idx0 += 4;
71         string::size_type idx1 = data.find("|++|", idx0);
72         cell(0) = asArray(data.substr(idx0, idx1 - idx0));
73         if (idx1 == string::npos)
74                 return;
75         cell(1) = asArray(data.substr(idx1 + 4));
76 }
77
78
79 MathInset * CommandInset::clone() const
80 {
81         return new CommandInset(*this);
82 }
83
84
85 void CommandInset::write(WriteStream & os) const
86 {
87         os << "\\" << name_;
88         if (cell(1).size())
89                 os << "[" << cell(1) << "]";
90         os << "{" << cell(0) << "}";
91 }
92
93
94 string CommandInset::screenLabel() const
95 {
96         return name_;
97 }
98
99 ////////////////////////////////
100
101 RefInset::RefInset()
102         : CommandInset("ref")
103 {}
104
105
106 RefInset::RefInset(string const & data)
107         : CommandInset(data)
108 {}
109
110
111 MathInset * RefInset::clone() const
112 {
113         return new RefInset(*this);
114 }
115
116
117 void RefInset::infoize(std::ostream & os) const
118 {
119         os << "Ref: " << cell(0);
120 }
121
122
123 int RefInset::dispatch(string const & cmd, idx_type, pos_type) 
124 {
125         if (cmd == "mouse 3") {
126                 cerr << "trying to goto ref" << cell(0) << "\n";
127                 mathcursor->formula()->view()->owner()->getLyXFunc()->
128                         dispatch(LFUN_REF_GOTO, asString(cell(0)));
129                 return 1; // dispatched
130         }
131         
132         if (cmd == "mouse 1") {
133                 cerr << "trying to open ref" << cell(0) << "\n";
134                 // Eventually trigger dialog with button 3 not 1
135 //              mathcursor->formula()->view()->owner()->getDialogs()
136 //                      ->showRef(this);
137                 return 1; // dispatched
138         }
139
140         return 0; // undispatched
141 }
142
143
144 string RefInset::screenLabel() const
145 {
146         string str;
147         for (int i = 0; !types[i].latex_name.empty(); ++i)
148                 if (name_ == types[i].latex_name) {
149                         str = _(types[i].short_gui_name);
150                         break;
151                 }
152         str += asString(cell(0));
153
154         //if (/* !isLatex && */ !cell(0).empty()) {
155         //      str += "||";
156         //      str += asString(cell(1));
157         //}
158         return str;
159 }
160
161
162 void RefInset::validate(LaTeXFeatures & features) const
163 {
164         if (name_ == "vref" || name_ == "vpageref")
165                 features.require("varioref");
166         else if (name_ == "prettyref")
167                 features.require("prettyref");
168 }
169
170
171 int RefInset::ascii(std::ostream & os, int) const
172 {
173         os << "[" << asString(cell(0)) << "]";
174         return 0;
175 }
176
177
178 int RefInset::linuxdoc(std::ostream & os) const
179 {
180         os << "<ref id=\"" << asString(cell(0))
181            << "\" name=\"" << asString(cell(1)) << "\" >";
182         return 0;
183 }
184
185
186 int RefInset::docbook(std::ostream & os, bool) const
187 {
188         if (cell(1).empty()) {
189                 os << "<xref linkend=\"" << asString(cell(0)) << "\">";
190         } else {
191                 os << "<link linkend=\"" << asString(cell(0))
192                    << "\">" << asString(cell(1)) << "</link>";
193         }
194
195         return 0;
196 }
197
198 RefInset::type_info RefInset::types[] = {
199         { "ref",        N_("Standard"),                 N_("Ref: ")},
200         { "pageref",    N_("Page Number"),              N_("Page: ")},
201         { "vpageref",   N_("Textual Page Number"),      N_("TextPage: ")},
202         { "vref",       N_("Standard+Textual Page"),    N_("Ref+Text: ")},
203         { "prettyref",  N_("PrettyRef"),                N_("PrettyRef: ")},
204         { "", "", "" }
205 };
206
207 ///////////////////////////////////
208
209
210 MathBoxInset::MathBoxInset(string const & name)
211         : MathGridInset(1, 1), name_(name)
212 {}
213
214
215 MathInset * MathBoxInset::clone() const
216 {
217         return new MathBoxInset(*this);
218 }
219
220
221 void MathBoxInset::write(WriteStream & os) const
222 {
223         os << "\\" << name_ << "{" << cell(0) << "}";
224 }
225
226
227 void MathBoxInset::normalize(NormalStream & os) const
228 {
229         os << '[' << name_ << ' ';
230         //text_->write(buffer(), os);
231         os << "] ";
232 }
233
234
235 void MathBoxInset::rebreak()
236 {
237         //lyxerr << "trying to rebreak...\n";
238 }
239
240
241 void MathBoxInset::metrics(MathMetricsInfo & mi) const
242 {
243         MathFontSetChanger dummy(mi.base, "textnormal");
244         MathGridInset::metrics(mi);
245 }
246
247
248 void MathBoxInset::draw(MathPainterInfo & pi, int x, int y) const
249 {
250         MathFontSetChanger dummy(pi.base, "textnormal");
251         MathGridInset::draw(pi, x, y);
252         mathed_draw_framebox(pi, x, y, this);
253 }