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