]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathBox.cpp
Introduce a return value for mathmlize(). We will need this to be able
[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(Buffer * buf, docstring const & name)
36         : InsetMathNest(buf, 1), name_(name)
37 {}
38
39
40 void InsetMathBox::write(WriteStream & os) const
41 {
42         ModeSpecifier specifier(os, TEXT_MODE);
43         os << '\\' << name_ << '{' << cell(0) << '}';
44 }
45
46
47 void InsetMathBox::normalize(NormalStream & os) const
48 {
49         os << '[' << name_ << ' ';
50         //text_->write(buffer(), os);
51         os << "] ";
52 }
53
54
55 docstring InsetMathBox::mathmlize(MathStream & ms) const
56 {       
57         // FIXME This doesn't actually work yet. We need to be able to signal 
58         // that we are in text mode and then just call ms << cell(0). So we
59         // need something like ModeSpecifier for MathStream.
60         ms << MTag("mtext");
61         ms.os() << cell(0);
62         ms << ETag("mtext");
63         return docstring();
64 }
65
66
67 void InsetMathBox::metrics(MetricsInfo & mi, Dimension & dim) const
68 {
69         FontSetChanger dummy(mi.base, "textnormal");
70         cell(0).metrics(mi, dim);
71         metricsMarkers(dim);
72 }
73
74
75 void InsetMathBox::draw(PainterInfo & pi, int x, int y) const
76 {
77         FontSetChanger dummy(pi.base, "textnormal");
78         cell(0).draw(pi, x, y);
79         drawMarkers(pi, x, y);
80 }
81
82
83 void InsetMathBox::infoize(odocstream & os) const
84 {       
85         os << "Box: " << name_;
86 }
87
88
89 void InsetMathBox::validate(LaTeXFeatures & features) const
90 {
91         if (name_ == "tag" || name_ == "tag*")
92                 features.require("amsmath");
93         cell(0).validate(features);
94 }
95
96
97
98 /////////////////////////////////////////////////////////////////////
99 //
100 // InsetMathFBox
101 //
102 /////////////////////////////////////////////////////////////////////
103
104
105 InsetMathFBox::InsetMathFBox(Buffer * buf)
106         : InsetMathNest(buf, 1)
107 {}
108
109
110 void InsetMathFBox::metrics(MetricsInfo & mi, Dimension & dim) const
111 {
112         FontSetChanger dummy(mi.base, "textnormal");
113         cell(0).metrics(mi, dim);
114         metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
115 }
116
117
118 void InsetMathFBox::draw(PainterInfo & pi, int x, int y) const
119 {
120         Dimension const dim = dimension(*pi.base.bv);
121         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
122                 dim.width() - 2, dim.height() - 2, Color_foreground);
123         FontSetChanger dummy(pi.base, "textnormal");
124         cell(0).draw(pi, x + 3, y);
125         setPosCache(pi, x, y);
126 }
127
128
129 void InsetMathFBox::write(WriteStream & os) const
130 {
131         ModeSpecifier specifier(os, TEXT_MODE);
132         os << "\\fbox{" << cell(0) << '}';
133 }
134
135
136 void InsetMathFBox::normalize(NormalStream & os) const
137 {
138         os << "[fbox " << cell(0) << ']';
139 }
140
141
142 void InsetMathFBox::infoize(odocstream & os) const
143 {
144         os << "FBox: ";
145 }
146
147
148 /////////////////////////////////////////////////////////////////////
149 //
150 // InsetMathMakebox
151 //
152 /////////////////////////////////////////////////////////////////////
153
154
155 InsetMathMakebox::InsetMathMakebox(Buffer * buf, bool framebox)
156         : InsetMathNest(buf, 3), framebox_(framebox)
157 {}
158
159
160 void InsetMathMakebox::metrics(MetricsInfo & mi, Dimension & dim) const
161 {
162         FontSetChanger dummy(mi.base, "textnormal");
163         
164         Dimension wdim;
165         static docstring bracket = from_ascii("[");
166         mathed_string_dim(mi.base.font, bracket, wdim);
167         int w = wdim.wid;
168         
169         Dimension dim0;
170         Dimension dim1;
171         Dimension dim2;
172         cell(0).metrics(mi, dim0);
173         cell(1).metrics(mi, dim1);
174         cell(2).metrics(mi, dim2);
175         
176         dim.wid = w + dim0.wid + w + w + dim1.wid + w + 2 + dim2.wid;
177         dim.asc = std::max(std::max(wdim.asc, dim0.asc), std::max(dim1.asc, dim2.asc)); 
178         dim.des = std::max(std::max(wdim.des, dim0.des), std::max(dim1.des, dim2.des));
179         
180         if (framebox_) {
181                 dim.wid += 4;
182                 dim.asc += 3;
183                 dim.des += 2;
184         } else {
185                 dim.asc += 1;
186                 dim.des += 1;
187         }
188         
189         metricsMarkers(dim);
190 }
191
192
193 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
194 {
195         drawMarkers(pi, x, y);
196         
197         FontSetChanger dummy(pi.base, "textnormal");
198         BufferView const & bv = *pi.base.bv;
199         int w = mathed_char_width(pi.base.font, '[');
200         
201         if (framebox_) {
202                 Dimension const dim = dimension(*pi.base.bv);
203                 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
204                                   dim.width() - 2, dim.height() - 2, Color_foreground);
205                 x += 2;
206         }
207         
208         drawStrBlack(pi, x, y, from_ascii("["));
209         x += w;
210         cell(0).draw(pi, x, y);
211         x += cell(0).dimension(bv).wid;
212         drawStrBlack(pi, x, y, from_ascii("]"));
213         x += w;
214
215         drawStrBlack(pi, x, y, from_ascii("["));
216         x += w;
217         cell(1).draw(pi, x, y);
218         x += cell(1).dimension(bv).wid;
219         drawStrBlack(pi, x, y, from_ascii("]"));
220         x += w + 2;
221
222         cell(2).draw(pi, x, y);
223 }
224
225
226 void InsetMathMakebox::write(WriteStream & os) const
227 {
228         ModeSpecifier specifier(os, TEXT_MODE);
229         os << (framebox_ ? "\\framebox" : "\\makebox");
230         if (cell(0).size() || !os.latex()) {
231                 os << '[' << cell(0) << ']';
232                 if (cell(1).size() || !os.latex())
233                         os << '[' << cell(1) << ']';
234         }
235         os << '{' << cell(2) << '}';
236 }
237
238
239 void InsetMathMakebox::normalize(NormalStream & os) const
240 {
241         os << (framebox_ ? "[framebox " : "[makebox ")
242            << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
243 }
244
245
246 void InsetMathMakebox::infoize(odocstream & os) const
247 {
248         os << (framebox_ ? "Framebox" : "Makebox") 
249            << " (width: " << cell(0)
250            << " pos: " << cell(1) << ")";
251 }
252
253
254 /////////////////////////////////////////////////////////////////////
255 //
256 // InsetMathBoxed
257 //
258 /////////////////////////////////////////////////////////////////////
259
260 InsetMathBoxed::InsetMathBoxed(Buffer * buf)
261         : InsetMathNest(buf, 1)
262 {}
263
264
265 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
266 {
267         cell(0).metrics(mi, dim);
268         metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
269 }
270
271
272 void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
273 {
274         Dimension const dim = dimension(*pi.base.bv);
275         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
276                 dim.width() - 2, dim.height() - 2, Color_foreground);
277         cell(0).draw(pi, x + 3, y);
278         setPosCache(pi, x, y);
279 }
280
281
282 void InsetMathBoxed::write(WriteStream & os) const
283 {
284         ModeSpecifier specifier(os, MATH_MODE);
285         os << "\\boxed{" << cell(0) << '}';
286 }
287
288
289 void InsetMathBoxed::normalize(NormalStream & os) const
290 {
291         os << "[boxed " << cell(0) << ']';
292 }
293
294
295 void InsetMathBoxed::infoize(odocstream & os) const
296 {
297         os << "Boxed: ";
298 }
299
300
301 void InsetMathBoxed::validate(LaTeXFeatures & features) const
302 {
303         features.require("amsmath");
304         InsetMathNest::validate(features);
305 }
306
307
308 } // namespace lyx