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