]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathBox.cpp
013e35bd93f2ce1098e4fc5305e1aec0f874a610
[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         // Cache the inset dimension. 
60         setDimCache(mi, dim);
61 }
62
63
64 void InsetMathBox::draw(PainterInfo & pi, int x, int y) const
65 {
66         FontSetChanger dummy(pi.base, "textnormal");
67         cell(0).draw(pi, x, y);
68         drawMarkers(pi, x, y);
69 }
70
71
72 void InsetMathBox::infoize(odocstream & os) const
73 {
74         os << "Box: " << name_;
75 }
76
77
78
79 /////////////////////////////////////////////////////////////////////
80 //
81 // InsetMathFBox
82 //
83 /////////////////////////////////////////////////////////////////////
84
85
86 InsetMathFBox::InsetMathFBox()
87         : InsetMathNest(1)
88 {}
89
90
91 void InsetMathFBox::metrics(MetricsInfo & mi, Dimension & dim) const
92 {
93         FontSetChanger dummy(mi.base, "textnormal");
94         cell(0).metrics(mi, dim);
95         metricsMarkers(dim, 3); // 1 pixel space, 1 frame, 1 space
96         // Cache the inset dimension. 
97         setDimCache(mi, dim);
98 }
99
100
101 void InsetMathFBox::draw(PainterInfo & pi, int x, int y) const
102 {
103         Dimension const dim = dimension(*pi.base.bv);
104         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
105                 dim.width() - 2, dim.height() - 2, Color_foreground);
106         FontSetChanger dummy(pi.base, "textnormal");
107         cell(0).draw(pi, x + 3, y);
108         setPosCache(pi, x, y);
109 }
110
111
112 void InsetMathFBox::write(WriteStream & os) const
113 {
114         os << "\\fbox{" << cell(0) << '}';
115 }
116
117
118 void InsetMathFBox::normalize(NormalStream & os) const
119 {
120         os << "[fbox " << cell(0) << ']';
121 }
122
123
124 void InsetMathFBox::infoize(odocstream & os) const
125 {
126         os << "FBox: ";
127 }
128
129
130 /////////////////////////////////////////////////////////////////////
131 //
132 // InsetMathFrameBox
133 //
134 /////////////////////////////////////////////////////////////////////
135
136
137 InsetMathFrameBox::InsetMathFrameBox()
138         : InsetMathNest(3)
139 {}
140
141
142 void InsetMathFrameBox::metrics(MetricsInfo & mi, Dimension & dim) const
143 {
144         FontSetChanger dummy(mi.base, "textnormal");
145         w_ = mathed_char_width(mi.base.font, '[');
146         InsetMathNest::metrics(mi);
147         dim  = cell(0).dimension(*mi.base.bv);
148         dim += cell(1).dimension(*mi.base.bv);
149         dim += cell(2).dimension(*mi.base.bv);
150         metricsMarkers(dim);
151         // Cache the inset dimension. 
152         setDimCache(mi, dim);
153 }
154
155
156 void InsetMathFrameBox::draw(PainterInfo & pi, int x, int y) const
157 {
158         FontSetChanger dummy(pi.base, "textnormal");
159         Dimension const dim = dimension(*pi.base.bv);
160         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
161                 dim.width() - 2, dim.height() - 2, Color_foreground);
162         x += 5;
163         BufferView const & bv = *pi.base.bv;
164
165         drawStrBlack(pi, x, y, from_ascii("["));
166         x += w_;
167         cell(0).draw(pi, x, y);
168         x += cell(0).dimension(bv).wid;
169         drawStrBlack(pi, x, y, from_ascii("]"));
170         x += w_ + 4;
171
172         drawStrBlack(pi, x, y, from_ascii("["));
173         x += w_;
174         cell(1).draw(pi, x, y);
175         x += cell(1).dimension(bv).wid;
176         drawStrBlack(pi, x, y, from_ascii("]"));
177         x += w_ + 4;
178
179         cell(2).draw(pi, x, y);
180         drawMarkers(pi, x, y);
181 }
182
183
184 void InsetMathFrameBox::write(WriteStream & os) const
185 {
186         os << "\\framebox";
187         os << '[' << cell(0) << ']';
188         if (cell(1).size())
189                 os << '[' << cell(1) << ']';
190         os << '{' << cell(2) << '}';
191 }
192
193
194 void InsetMathFrameBox::normalize(NormalStream & os) const
195 {
196         os << "[framebox " << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
197 }
198
199
200
201 /////////////////////////////////////////////////////////////////////
202 //
203 // InsetMathBoxed
204 //
205 /////////////////////////////////////////////////////////////////////
206
207 InsetMathBoxed::InsetMathBoxed()
208         : InsetMathNest(1)
209 {}
210
211
212 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
213 {
214         cell(0).metrics(mi, dim);
215         metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
216         // Cache the inset dimension. 
217         setDimCache(mi, dim);
218 }
219
220
221 void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
222 {
223         Dimension const dim = dimension(*pi.base.bv);
224         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
225                 dim.width() - 2, dim.height() - 2, Color_foreground);
226         cell(0).draw(pi, x + 3, y);
227         setPosCache(pi, x, y);
228 }
229
230
231 void InsetMathBoxed::write(WriteStream & os) const
232 {
233         os << "\\boxed{" << cell(0) << '}';
234 }
235
236
237 void InsetMathBoxed::normalize(NormalStream & os) const
238 {
239         os << "[boxed " << cell(0) << ']';
240 }
241
242
243 void InsetMathBoxed::infoize(odocstream & os) const
244 {
245         os << "Boxed: ";
246 }
247
248
249 void InsetMathBoxed::validate(LaTeXFeatures & features) const
250 {
251         features.require("amsmath");
252 }
253
254
255 /////////////////////////////////////////////////////////////////////
256 //
257 // InsetMathMakebox
258 //
259 /////////////////////////////////////////////////////////////////////
260
261
262 InsetMathMakebox::InsetMathMakebox()
263         : InsetMathNest(3)
264 {}
265
266
267 void InsetMathMakebox::metrics(MetricsInfo & mi, Dimension & dim) const
268 {
269         FontSetChanger dummy(mi.base, from_ascii("textnormal"));
270         w_ = mathed_char_width(mi.base.font, '[');
271         InsetMathNest::metrics(mi);
272         dim   = cell(0).dimension(*mi.base.bv);
273         dim  += cell(1).dimension(*mi.base.bv);
274         dim  += cell(2).dimension(*mi.base.bv);
275         dim.wid += 4 * w_ + 4;
276         metricsMarkers(dim);
277         // Cache the inset dimension. 
278         setDimCache(mi, dim);
279 }
280
281
282 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
283 {
284         FontSetChanger dummy(pi.base, from_ascii("textnormal"));
285         drawMarkers(pi, x, y);
286
287         drawStrBlack(pi, x, y, from_ascii("["));
288         x += w_;
289         cell(0).draw(pi, x, y);
290         x += cell(0).dimension(*pi.base.bv).width();
291         drawStrBlack(pi, x, y, from_ascii("]"));
292         x += w_ + 2;
293
294         drawStrBlack(pi, x, y, from_ascii("["));
295         x += w_;
296         cell(1).draw(pi, x, y);
297         x += cell(1).dimension(*pi.base.bv).wid;
298         drawStrBlack(pi, x, y, from_ascii("]"));
299         x += w_ + 2;
300
301         cell(2).draw(pi, x, y);
302         setPosCache(pi, x, y);
303 }
304
305
306 void InsetMathMakebox::write(WriteStream & os) const
307 {
308         os << "\\makebox";
309         os << '[' << cell(0) << ']';
310         if (cell(1).size())
311                 os << '[' << cell(1) << ']';
312         os << '{' << cell(2) << '}';
313 }
314
315
316 void InsetMathMakebox::normalize(NormalStream & os) const
317 {
318         os << "[makebox " << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
319 }
320
321
322 void InsetMathMakebox::infoize(odocstream & os) const
323 {
324         os << "Makebox (width: " << cell(0)
325             << " pos: " << cell(1) << ")";
326 }
327
328
329 } // namespace lyx