]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathBox.cpp
* fixing the framebox part of http://bugzilla.lyx.org/show_bug.cgi?id=2461:
[lyx.git] / src / mathed / InsetMathBox.cpp
1 /**
2  * \file InsetMathBox.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  * \author Ling Li (InsetMathMakebox)
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetMathBox.h"
15
16 #include "LaTeXFeatures.h"
17 #include "MathData.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "MetricsInfo.h"
21
22 #include "frontends/Painter.h"
23
24 #include <ostream>
25
26
27 namespace lyx {
28
29 /////////////////////////////////////////////////////////////////////
30 //
31 // InsetMathBox
32 //
33 /////////////////////////////////////////////////////////////////////
34
35 InsetMathBox::InsetMathBox(docstring const & name)
36         : InsetMathNest(1), name_(name)
37 {}
38
39
40 void InsetMathBox::write(WriteStream & os) const
41 {
42         os << '\\' << name_ << '{' << cell(0) << '}';
43 }
44
45
46 void InsetMathBox::normalize(NormalStream & os) const
47 {
48         os << '[' << name_ << ' ';
49         //text_->write(buffer(), os);
50         os << "] ";
51 }
52
53
54 void InsetMathBox::metrics(MetricsInfo & mi, Dimension & dim) const
55 {
56         FontSetChanger dummy(mi.base, "textnormal");
57         cell(0).metrics(mi, dim);
58         metricsMarkers(dim);
59 }
60
61
62 void InsetMathBox::draw(PainterInfo & pi, int x, int y) const
63 {
64         FontSetChanger dummy(pi.base, "textnormal");
65         cell(0).draw(pi, x, y);
66         drawMarkers(pi, x, y);
67 }
68
69
70 void InsetMathBox::infoize(odocstream & os) const
71 {
72         os << "Box: " << name_;
73 }
74
75
76
77 /////////////////////////////////////////////////////////////////////
78 //
79 // InsetMathFBox
80 //
81 /////////////////////////////////////////////////////////////////////
82
83
84 InsetMathFBox::InsetMathFBox()
85         : InsetMathNest(1)
86 {}
87
88
89 void InsetMathFBox::metrics(MetricsInfo & mi, Dimension & dim) const
90 {
91         FontSetChanger dummy(mi.base, "textnormal");
92         cell(0).metrics(mi, dim);
93         metricsMarkers(dim, 3); // 1 pixel space, 1 frame, 1 space
94 }
95
96
97 void InsetMathFBox::draw(PainterInfo & pi, int x, int y) const
98 {
99         Dimension const dim = dimension(*pi.base.bv);
100         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
101                 dim.width() - 2, dim.height() - 2, Color_foreground);
102         FontSetChanger dummy(pi.base, "textnormal");
103         cell(0).draw(pi, x + 3, y);
104         setPosCache(pi, x, y);
105 }
106
107
108 void InsetMathFBox::write(WriteStream & os) const
109 {
110         os << "\\fbox{" << cell(0) << '}';
111 }
112
113
114 void InsetMathFBox::normalize(NormalStream & os) const
115 {
116         os << "[fbox " << cell(0) << ']';
117 }
118
119
120 void InsetMathFBox::infoize(odocstream & os) const
121 {
122         os << "FBox: ";
123 }
124
125
126 /////////////////////////////////////////////////////////////////////
127 //
128 // InsetMathFrameBox
129 //
130 /////////////////////////////////////////////////////////////////////
131
132
133 InsetMathFrameBox::InsetMathFrameBox()
134         : InsetMathNest(3)
135 {}
136
137
138 void InsetMathFrameBox::metrics(MetricsInfo & mi, Dimension & dim) const
139 {
140         FontSetChanger dummy(mi.base, "textnormal");
141         
142         Dimension wdim;
143         static docstring bracket = from_ascii("[");
144         mathed_string_dim(mi.base.font, bracket, wdim);
145         int w = wdim.wid;
146         
147         Dimension dim0;
148         Dimension dim1;
149         Dimension dim2;
150         cell(0).metrics(mi, dim0);
151         cell(1).metrics(mi, dim1);
152         cell(2).metrics(mi, dim2);
153         
154         dim.wid = 5 + w + dim0.wid + w + 4 + w + dim1.wid + w + 4 + dim2.wid + 5;
155         dim.asc = std::max(std::max(wdim.asc, dim0.asc), std::max(dim1.asc, dim2.asc)); 
156         dim.des = std::max(std::max(wdim.des, dim0.des), std::max(dim1.des, dim2.des));
157         dim.asc += 3;
158         dim.des += 3;
159         
160         metricsMarkers(dim);
161 }
162
163
164 void InsetMathFrameBox::draw(PainterInfo & pi, int x, int y) const
165 {
166         drawMarkers(pi, x, y);
167         
168         FontSetChanger dummy(pi.base, "textnormal");
169         Dimension const dim = dimension(*pi.base.bv);
170         int w = mathed_char_width(pi.base.font, '[');
171         
172         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
173                 dim.width() - 2, dim.height() - 2, Color_foreground);
174         
175         x += 5;
176         BufferView const & bv = *pi.base.bv;
177
178         drawStrBlack(pi, x, y, from_ascii("["));
179         x += w;
180         cell(0).draw(pi, x, y);
181         x += cell(0).dimension(bv).wid;
182         drawStrBlack(pi, x, y, from_ascii("]"));
183         x += w + 4;
184
185         drawStrBlack(pi, x, y, from_ascii("["));
186         x += w;
187         cell(1).draw(pi, x, y);
188         x += cell(1).dimension(bv).wid;
189         drawStrBlack(pi, x, y, from_ascii("]"));
190         x += w + 4;
191
192         cell(2).draw(pi, x, y);
193 }
194
195
196 void InsetMathFrameBox::write(WriteStream & os) const
197 {
198         os << "\\framebox";
199         os << '[' << cell(0) << ']';
200         if (cell(1).size())
201                 os << '[' << cell(1) << ']';
202         os << '{' << cell(2) << '}';
203 }
204
205
206 void InsetMathFrameBox::normalize(NormalStream & os) const
207 {
208         os << "[framebox " << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
209 }
210
211
212
213 /////////////////////////////////////////////////////////////////////
214 //
215 // InsetMathBoxed
216 //
217 /////////////////////////////////////////////////////////////////////
218
219 InsetMathBoxed::InsetMathBoxed()
220         : InsetMathNest(1)
221 {}
222
223
224 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
225 {
226         cell(0).metrics(mi, dim);
227         metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
228 }
229
230
231 void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
232 {
233         Dimension const dim = dimension(*pi.base.bv);
234         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
235                 dim.width() - 2, dim.height() - 2, Color_foreground);
236         cell(0).draw(pi, x + 3, y);
237         setPosCache(pi, x, y);
238 }
239
240
241 void InsetMathBoxed::write(WriteStream & os) const
242 {
243         os << "\\boxed{" << cell(0) << '}';
244 }
245
246
247 void InsetMathBoxed::normalize(NormalStream & os) const
248 {
249         os << "[boxed " << cell(0) << ']';
250 }
251
252
253 void InsetMathBoxed::infoize(odocstream & os) const
254 {
255         os << "Boxed: ";
256 }
257
258
259 void InsetMathBoxed::validate(LaTeXFeatures & features) const
260 {
261         features.require("amsmath");
262 }
263
264
265 /////////////////////////////////////////////////////////////////////
266 //
267 // InsetMathMakebox
268 //
269 /////////////////////////////////////////////////////////////////////
270
271
272 InsetMathMakebox::InsetMathMakebox()
273         : InsetMathNest(3)
274 {}
275
276
277 void InsetMathMakebox::metrics(MetricsInfo & mi, Dimension & dim) const
278 {
279         FontSetChanger dummy(mi.base, from_ascii("textnormal"));
280         w_ = mathed_char_width(mi.base.font, '[');
281         InsetMathNest::metrics(mi);
282         dim   = cell(0).dimension(*mi.base.bv);
283         dim  += cell(1).dimension(*mi.base.bv);
284         dim  += cell(2).dimension(*mi.base.bv);
285         dim.wid += 4 * w_ + 4;
286         metricsMarkers(dim);
287 }
288
289
290 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
291 {
292         FontSetChanger dummy(pi.base, from_ascii("textnormal"));
293         drawMarkers(pi, x, y);
294
295         drawStrBlack(pi, x, y, from_ascii("["));
296         x += w_;
297         cell(0).draw(pi, x, y);
298         x += cell(0).dimension(*pi.base.bv).width();
299         drawStrBlack(pi, x, y, from_ascii("]"));
300         x += w_ + 2;
301
302         drawStrBlack(pi, x, y, from_ascii("["));
303         x += w_;
304         cell(1).draw(pi, x, y);
305         x += cell(1).dimension(*pi.base.bv).wid;
306         drawStrBlack(pi, x, y, from_ascii("]"));
307         x += w_ + 2;
308
309         cell(2).draw(pi, x, y);
310         setPosCache(pi, x, y);
311 }
312
313
314 void InsetMathMakebox::write(WriteStream & os) const
315 {
316         os << "\\makebox";
317         os << '[' << cell(0) << ']';
318         if (cell(1).size())
319                 os << '[' << cell(1) << ']';
320         os << '{' << cell(2) << '}';
321 }
322
323
324 void InsetMathMakebox::normalize(NormalStream & os) const
325 {
326         os << "[makebox " << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
327 }
328
329
330 void InsetMathMakebox::infoize(odocstream & os) const
331 {
332         os << "Makebox (width: " << cell(0)
333             << " pos: " << cell(1) << ")";
334 }
335
336
337 } // namespace lyx