]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathBox.cpp
c6f98ca98718dbb7077e496529544eeab3f0dc9e
[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         SetMode textmode(ms, true);
58         ms << MTag("mstyle", "class='mathbox'")
59            << cell(0)
60            << ETag("mstyle");
61 }
62
63
64 void InsetMathBox::htmlize(HtmlStream & ms) const
65 {
66         SetHTMLMode textmode(ms, true);
67         ms << MTag("span", "class='mathbox'")
68            << cell(0)
69            << ETag("span");
70 }
71
72
73 void InsetMathBox::metrics(MetricsInfo & mi, Dimension & dim) const
74 {
75         FontSetChanger dummy(mi.base, "textnormal");
76         cell(0).metrics(mi, dim);
77         metricsMarkers(dim);
78 }
79
80
81 void InsetMathBox::draw(PainterInfo & pi, int x, int y) const
82 {
83         FontSetChanger dummy(pi.base, "textnormal");
84         cell(0).draw(pi, x, y);
85         drawMarkers(pi, x, y);
86 }
87
88
89 void InsetMathBox::infoize(odocstream & os) const
90 {       
91         os << "Box: " << name_;
92 }
93
94
95 void InsetMathBox::validate(LaTeXFeatures & features) const
96 {
97         // FIXME XHTML
98         // It'd be better to be able to get this from an InsetLayout, but at present
99         // InsetLayouts do not seem really to work for things that aren't InsetTexts.
100         if (features.runparams().math_flavor == OutputParams::MathAsMathML)
101                 features.addPreambleSnippet("<style type=\"text/css\">\n"
102                         "mstyle.mathbox { font-style: normal; }\n"
103                         "</style>");
104         else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
105                 features.addPreambleSnippet("<style type=\"text/css\">\n"
106                         "span.mathbox { font-style: normal; }\n"
107                         "</style>");
108
109         if (name_ == "tag" || name_ == "tag*")
110                 features.require("amsmath");
111
112         InsetMathNest::validate(features);
113 }
114
115
116
117 /////////////////////////////////////////////////////////////////////
118 //
119 // InsetMathFBox
120 //
121 /////////////////////////////////////////////////////////////////////
122
123
124 InsetMathFBox::InsetMathFBox(Buffer * buf)
125         : InsetMathNest(buf, 1)
126 {}
127
128
129 void InsetMathFBox::metrics(MetricsInfo & mi, Dimension & dim) const
130 {
131         FontSetChanger dummy(mi.base, "textnormal");
132         cell(0).metrics(mi, dim);
133         metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
134 }
135
136
137 void InsetMathFBox::draw(PainterInfo & pi, int x, int y) const
138 {
139         Dimension const dim = dimension(*pi.base.bv);
140         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
141                 dim.width() - 2, dim.height() - 2, Color_foreground);
142         FontSetChanger dummy(pi.base, "textnormal");
143         cell(0).draw(pi, x + 3, y);
144         setPosCache(pi, x, y);
145 }
146
147
148 void InsetMathFBox::write(WriteStream & os) const
149 {
150         ModeSpecifier specifier(os, TEXT_MODE);
151         os << "\\fbox{" << cell(0) << '}';
152 }
153
154
155 void InsetMathFBox::normalize(NormalStream & os) const
156 {
157         os << "[fbox " << cell(0) << ']';
158 }
159
160
161 void InsetMathFBox::mathmlize(MathStream & ms) const
162 {       
163         SetMode textmode(ms, true);
164         ms << MTag("mstyle", "class='fbox'")
165            << cell(0)
166            << ETag("mstyle");
167 }
168
169
170 void InsetMathFBox::htmlize(HtmlStream & ms) const
171 {
172         SetHTMLMode textmode(ms, true);
173         ms << MTag("span", "class='fbox'")
174            << cell(0)
175            << ETag("span");
176 }
177
178
179 void InsetMathFBox::infoize(odocstream & os) const
180 {
181         os << "FBox: ";
182 }
183
184
185 void InsetMathFBox::validate(LaTeXFeatures & features) const
186 {
187         // FIXME XHTML
188         // It'd be better to be able to get this from an InsetLayout, but at present
189         // InsetLayouts do not seem really to work for things that aren't InsetTexts.
190         if (features.runparams().math_flavor == OutputParams::MathAsMathML)
191                 features.addPreambleSnippet("<style type=\"text/css\">\n"
192                         "mstyle.fbox { border: 1px solid black; font-style: normal; padding: 0.5ex; }\n"
193                         "</style>");
194         else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
195                 features.addPreambleSnippet("<style type=\"text/css\">\n"
196                         "span.fbox { border: 1px solid black; font-style: normal; padding: 0.5ex; }\n"
197                         "</style>");
198
199         cell(0).validate(features);
200         InsetMathNest::validate(features);
201 }
202
203
204
205 /////////////////////////////////////////////////////////////////////
206 //
207 // InsetMathMakebox
208 //
209 /////////////////////////////////////////////////////////////////////
210
211
212 InsetMathMakebox::InsetMathMakebox(Buffer * buf, bool framebox)
213         : InsetMathNest(buf, 3), framebox_(framebox)
214 {}
215
216
217 void InsetMathMakebox::metrics(MetricsInfo & mi, Dimension & dim) const
218 {
219         FontSetChanger dummy(mi.base, "textnormal");
220         
221         Dimension wdim;
222         static docstring bracket = from_ascii("[");
223         mathed_string_dim(mi.base.font, bracket, wdim);
224         int w = wdim.wid;
225         
226         Dimension dim0;
227         Dimension dim1;
228         Dimension dim2;
229         cell(0).metrics(mi, dim0);
230         cell(1).metrics(mi, dim1);
231         cell(2).metrics(mi, dim2);
232         
233         dim.wid = w + dim0.wid + w + w + dim1.wid + w + 2 + dim2.wid;
234         dim.asc = std::max(std::max(wdim.asc, dim0.asc), std::max(dim1.asc, dim2.asc)); 
235         dim.des = std::max(std::max(wdim.des, dim0.des), std::max(dim1.des, dim2.des));
236         
237         if (framebox_) {
238                 dim.wid += 4;
239                 dim.asc += 3;
240                 dim.des += 2;
241         } else {
242                 dim.asc += 1;
243                 dim.des += 1;
244         }
245         
246         metricsMarkers(dim);
247 }
248
249
250 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
251 {
252         drawMarkers(pi, x, y);
253         
254         FontSetChanger dummy(pi.base, "textnormal");
255         BufferView const & bv = *pi.base.bv;
256         int w = mathed_char_width(pi.base.font, '[');
257         
258         if (framebox_) {
259                 Dimension const dim = dimension(*pi.base.bv);
260                 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
261                                   dim.width() - 2, dim.height() - 2, Color_foreground);
262                 x += 2;
263         }
264         
265         drawStrBlack(pi, x, y, from_ascii("["));
266         x += w;
267         cell(0).draw(pi, x, y);
268         x += cell(0).dimension(bv).wid;
269         drawStrBlack(pi, x, y, from_ascii("]"));
270         x += w;
271
272         drawStrBlack(pi, x, y, from_ascii("["));
273         x += w;
274         cell(1).draw(pi, x, y);
275         x += cell(1).dimension(bv).wid;
276         drawStrBlack(pi, x, y, from_ascii("]"));
277         x += w + 2;
278
279         cell(2).draw(pi, x, y);
280 }
281
282
283 void InsetMathMakebox::write(WriteStream & os) const
284 {
285         ModeSpecifier specifier(os, TEXT_MODE);
286         os << (framebox_ ? "\\framebox" : "\\makebox");
287         if (cell(0).size() || !os.latex()) {
288                 os << '[' << cell(0) << ']';
289                 if (cell(1).size() || !os.latex())
290                         os << '[' << cell(1) << ']';
291         }
292         os << '{' << cell(2) << '}';
293 }
294
295
296 void InsetMathMakebox::normalize(NormalStream & os) const
297 {
298         os << (framebox_ ? "[framebox " : "[makebox ")
299            << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
300 }
301
302
303 void InsetMathMakebox::infoize(odocstream & os) const
304 {
305         os << (framebox_ ? "Framebox" : "Makebox") 
306            << " (width: " << cell(0)
307            << " pos: " << cell(1) << ")";
308 }
309
310
311 void InsetMathMakebox::mathmlize(MathStream & ms) const
312 {
313         // FIXME We could do something with the other arguments.
314         std::string const cssclass = framebox_ ? "framebox" : "makebox";
315         SetMode textmode(ms, true);
316         ms << MTag("mstyle", "class='" + cssclass + "'")
317            << cell(2)
318            << ETag("mstyle");
319 }
320
321
322 void InsetMathMakebox::htmlize(HtmlStream & ms) const
323 {
324         // FIXME We could do something with the other arguments.
325         SetHTMLMode textmode(ms, true);
326         std::string const cssclass = framebox_ ? "framebox" : "makebox";
327         ms << MTag("span", "class='" + cssclass + "'")
328            << cell(2)
329            << ETag("span");
330 }
331
332
333 void InsetMathMakebox::validate(LaTeXFeatures & features) const
334 {
335         // FIXME XHTML
336         // It'd be better to be able to get this from an InsetLayout, but at present
337         // InsetLayouts do not seem really to work for things that aren't InsetTexts.
338         if (features.runparams().math_flavor == OutputParams::MathAsMathML)
339                 features.addPreambleSnippet("<style type=\"text/css\">\n"
340                         "mstyle.framebox { border: 1px solid black; }\n"
341                         "</style>");
342         else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
343                 features.addPreambleSnippet("<style type=\"text/css\">\n"
344                         "span.framebox { border: 1px solid black; }\n"
345                         "</style>");
346         InsetMathNest::validate(features);
347 }
348
349
350 /////////////////////////////////////////////////////////////////////
351 //
352 // InsetMathBoxed
353 //
354 /////////////////////////////////////////////////////////////////////
355
356 InsetMathBoxed::InsetMathBoxed(Buffer * buf)
357         : InsetMathNest(buf, 1)
358 {}
359
360
361 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
362 {
363         cell(0).metrics(mi, dim);
364         metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
365 }
366
367
368 void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
369 {
370         Dimension const dim = dimension(*pi.base.bv);
371         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
372                 dim.width() - 2, dim.height() - 2, Color_foreground);
373         cell(0).draw(pi, x + 3, y);
374         setPosCache(pi, x, y);
375 }
376
377
378 void InsetMathBoxed::write(WriteStream & os) const
379 {
380         ModeSpecifier specifier(os, MATH_MODE);
381         os << "\\boxed{" << cell(0) << '}';
382 }
383
384
385 void InsetMathBoxed::normalize(NormalStream & os) const
386 {
387         os << "[boxed " << cell(0) << ']';
388 }
389
390
391 void InsetMathBoxed::infoize(odocstream & os) const
392 {
393         os << "Boxed: ";
394 }
395
396
397 void InsetMathBoxed::mathmlize(MathStream & ms) const
398 {
399         ms << MTag("mstyle", "class='boxed'")
400            << cell(0)
401            << ETag("mstyle");
402 }
403
404
405 void InsetMathBoxed::htmlize(HtmlStream & ms) const
406 {
407         ms << MTag("span", "class='boxed'")
408            << cell(0)
409                  << ETag("span");
410 }
411
412
413 void InsetMathBoxed::validate(LaTeXFeatures & features) const
414 {
415         features.require("amsmath");
416
417         // FIXME XHTML
418         // It'd be better to be able to get this from an InsetLayout, but at present
419         // InsetLayouts do not seem really to work for things that aren't InsetTexts.
420         if (features.runparams().math_flavor == OutputParams::MathAsMathML)
421                 features.addPreambleSnippet("<style type=\"text/css\">\n"
422                         "mstyle.boxed { border: 1px solid black; }\n"
423                         "</style>");
424         else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
425                 features.addPreambleSnippet("<style type=\"text/css\">\n"
426                         "span.boxed { border: 1px solid black; }\n"
427                         "</style>");
428         
429         InsetMathNest::validate(features);
430 }
431
432
433 } // namespace lyx