]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathBox.cpp
c5272c20e00ba2cba6b3bbfb35cdb36a33ed29b6
[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 // InsetMathMakebox
129 //
130 /////////////////////////////////////////////////////////////////////
131
132
133 InsetMathMakebox::InsetMathMakebox(bool framebox)
134         : InsetMathNest(3), framebox_(framebox)
135 {}
136
137
138 void InsetMathMakebox::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 = w + dim0.wid + w + w + dim1.wid + w + 2 + dim2.wid;
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         
158         if (framebox_) {
159                 dim.wid += 4;
160                 dim.asc += 3;
161                 dim.des += 2;
162         } else {
163                 dim.asc += 1;
164                 dim.des += 1;
165         }
166         
167         metricsMarkers(dim);
168 }
169
170
171 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
172 {
173         drawMarkers(pi, x, y);
174         
175         FontSetChanger dummy(pi.base, "textnormal");
176         BufferView const & bv = *pi.base.bv;
177         int w = mathed_char_width(pi.base.font, '[');
178         
179         if (framebox_) {
180                 Dimension const dim = dimension(*pi.base.bv);
181                 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
182                                   dim.width() - 2, dim.height() - 2, Color_foreground);
183                 x += 2;
184         }
185         
186         drawStrBlack(pi, x, y, from_ascii("["));
187         x += w;
188         cell(0).draw(pi, x, y);
189         x += cell(0).dimension(bv).wid;
190         drawStrBlack(pi, x, y, from_ascii("]"));
191         x += w;
192
193         drawStrBlack(pi, x, y, from_ascii("["));
194         x += w;
195         cell(1).draw(pi, x, y);
196         x += cell(1).dimension(bv).wid;
197         drawStrBlack(pi, x, y, from_ascii("]"));
198         x += w + 2;
199
200         cell(2).draw(pi, x, y);
201 }
202
203
204 void InsetMathMakebox::write(WriteStream & os) const
205 {
206         os << (framebox_ ? "\\framebox" : "\\makebox");
207         if (cell(0).size() || !os.latex()) {
208                 os << '[' << cell(0) << ']';
209                 if (cell(1).size() || !os.latex())
210                         os << '[' << cell(1) << ']';
211         }
212         os << '{' << cell(2) << '}';
213 }
214
215
216 void InsetMathMakebox::normalize(NormalStream & os) const
217 {
218         os << (framebox_ ? "[framebox " : "[makebox ")
219            << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
220 }
221
222
223 void InsetMathMakebox::infoize(odocstream & os) const
224 {
225         os << (framebox_ ? "Framebox" : "Makebox") 
226            << " (width: " << cell(0)
227            << " pos: " << cell(1) << ")";
228 }
229
230
231 /////////////////////////////////////////////////////////////////////
232 //
233 // InsetMathBoxed
234 //
235 /////////////////////////////////////////////////////////////////////
236
237 InsetMathBoxed::InsetMathBoxed()
238         : InsetMathNest(1)
239 {}
240
241
242 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
243 {
244         cell(0).metrics(mi, dim);
245         metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
246 }
247
248
249 void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
250 {
251         Dimension const dim = dimension(*pi.base.bv);
252         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
253                 dim.width() - 2, dim.height() - 2, Color_foreground);
254         cell(0).draw(pi, x + 3, y);
255         setPosCache(pi, x, y);
256 }
257
258
259 void InsetMathBoxed::write(WriteStream & os) const
260 {
261         os << "\\boxed{" << cell(0) << '}';
262 }
263
264
265 void InsetMathBoxed::normalize(NormalStream & os) const
266 {
267         os << "[boxed " << cell(0) << ']';
268 }
269
270
271 void InsetMathBoxed::infoize(odocstream & os) const
272 {
273         os << "Boxed: ";
274 }
275
276
277 void InsetMathBoxed::validate(LaTeXFeatures & features) const
278 {
279         features.require("amsmath");
280 }
281
282
283 } // namespace lyx