]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathBox.cpp
8aba8d80fcbc6323336d8cbc0756176a62d858ee
[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 << 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::infoize(odocstream & os) const
138 {
139         os << "FBox: ";
140 }
141
142
143 /////////////////////////////////////////////////////////////////////
144 //
145 // InsetMathMakebox
146 //
147 /////////////////////////////////////////////////////////////////////
148
149
150 InsetMathMakebox::InsetMathMakebox(Buffer * buf, bool framebox)
151         : InsetMathNest(buf, 3), framebox_(framebox)
152 {}
153
154
155 void InsetMathMakebox::metrics(MetricsInfo & mi, Dimension & dim) const
156 {
157         FontSetChanger dummy(mi.base, "textnormal");
158         
159         Dimension wdim;
160         static docstring bracket = from_ascii("[");
161         mathed_string_dim(mi.base.font, bracket, wdim);
162         int w = wdim.wid;
163         
164         Dimension dim0;
165         Dimension dim1;
166         Dimension dim2;
167         cell(0).metrics(mi, dim0);
168         cell(1).metrics(mi, dim1);
169         cell(2).metrics(mi, dim2);
170         
171         dim.wid = w + dim0.wid + w + w + dim1.wid + w + 2 + dim2.wid;
172         dim.asc = std::max(std::max(wdim.asc, dim0.asc), std::max(dim1.asc, dim2.asc)); 
173         dim.des = std::max(std::max(wdim.des, dim0.des), std::max(dim1.des, dim2.des));
174         
175         if (framebox_) {
176                 dim.wid += 4;
177                 dim.asc += 3;
178                 dim.des += 2;
179         } else {
180                 dim.asc += 1;
181                 dim.des += 1;
182         }
183         
184         metricsMarkers(dim);
185 }
186
187
188 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
189 {
190         drawMarkers(pi, x, y);
191         
192         FontSetChanger dummy(pi.base, "textnormal");
193         BufferView const & bv = *pi.base.bv;
194         int w = mathed_char_width(pi.base.font, '[');
195         
196         if (framebox_) {
197                 Dimension const dim = dimension(*pi.base.bv);
198                 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
199                                   dim.width() - 2, dim.height() - 2, Color_foreground);
200                 x += 2;
201         }
202         
203         drawStrBlack(pi, x, y, from_ascii("["));
204         x += w;
205         cell(0).draw(pi, x, y);
206         x += cell(0).dimension(bv).wid;
207         drawStrBlack(pi, x, y, from_ascii("]"));
208         x += w;
209
210         drawStrBlack(pi, x, y, from_ascii("["));
211         x += w;
212         cell(1).draw(pi, x, y);
213         x += cell(1).dimension(bv).wid;
214         drawStrBlack(pi, x, y, from_ascii("]"));
215         x += w + 2;
216
217         cell(2).draw(pi, x, y);
218 }
219
220
221 void InsetMathMakebox::write(WriteStream & os) const
222 {
223         ModeSpecifier specifier(os, TEXT_MODE);
224         os << (framebox_ ? "\\framebox" : "\\makebox");
225         if (cell(0).size() || !os.latex()) {
226                 os << '[' << cell(0) << ']';
227                 if (cell(1).size() || !os.latex())
228                         os << '[' << cell(1) << ']';
229         }
230         os << '{' << cell(2) << '}';
231 }
232
233
234 void InsetMathMakebox::normalize(NormalStream & os) const
235 {
236         os << (framebox_ ? "[framebox " : "[makebox ")
237            << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
238 }
239
240
241 void InsetMathMakebox::infoize(odocstream & os) const
242 {
243         os << (framebox_ ? "Framebox" : "Makebox") 
244            << " (width: " << cell(0)
245            << " pos: " << cell(1) << ")";
246 }
247
248
249 /////////////////////////////////////////////////////////////////////
250 //
251 // InsetMathBoxed
252 //
253 /////////////////////////////////////////////////////////////////////
254
255 InsetMathBoxed::InsetMathBoxed(Buffer * buf)
256         : InsetMathNest(buf, 1)
257 {}
258
259
260 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
261 {
262         cell(0).metrics(mi, dim);
263         metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
264 }
265
266
267 void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
268 {
269         Dimension const dim = dimension(*pi.base.bv);
270         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
271                 dim.width() - 2, dim.height() - 2, Color_foreground);
272         cell(0).draw(pi, x + 3, y);
273         setPosCache(pi, x, y);
274 }
275
276
277 void InsetMathBoxed::write(WriteStream & os) const
278 {
279         ModeSpecifier specifier(os, MATH_MODE);
280         os << "\\boxed{" << cell(0) << '}';
281 }
282
283
284 void InsetMathBoxed::normalize(NormalStream & os) const
285 {
286         os << "[boxed " << cell(0) << ']';
287 }
288
289
290 void InsetMathBoxed::infoize(odocstream & os) const
291 {
292         os << "Boxed: ";
293 }
294
295
296 void InsetMathBoxed::validate(LaTeXFeatures & features) const
297 {
298         features.require("amsmath");
299         InsetMathNest::validate(features);
300 }
301
302
303 } // namespace lyx