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