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