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