]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathBox.cpp
* InsetMathHull:
[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(docstring const & name)
36         : InsetMathNest(1), name_(name)
37 {}
38
39
40 void InsetMathBox::write(WriteStream & os) const
41 {
42         os << '\\' << name_ << '{' << cell(0) << '}';
43 }
44
45
46 void InsetMathBox::normalize(NormalStream & os) const
47 {
48         os << '[' << name_ << ' ';
49         //text_->write(buffer(), os);
50         os << "] ";
51 }
52
53
54 void InsetMathBox::metrics(MetricsInfo & mi, Dimension & dim) const
55 {
56         FontSetChanger dummy(mi.base, "textnormal");
57         cell(0).metrics(mi, dim);
58         metricsMarkers(dim);
59 }
60
61
62 void InsetMathBox::draw(PainterInfo & pi, int x, int y) const
63 {
64         FontSetChanger dummy(pi.base, "textnormal");
65         cell(0).draw(pi, x, y);
66         drawMarkers(pi, x, y);
67 }
68
69
70 void InsetMathBox::infoize(odocstream & os) const
71 {
72         os << "Box: " << name_;
73 }
74
75
76 void InsetMathBox::validate(LaTeXFeatures & features) const
77 {
78         if (name_ == "tag" || name_ == "tag*")
79                 features.require("amsmath");
80         cell(0).validate(features);
81 }
82
83
84
85 /////////////////////////////////////////////////////////////////////
86 //
87 // InsetMathFBox
88 //
89 /////////////////////////////////////////////////////////////////////
90
91
92 InsetMathFBox::InsetMathFBox()
93         : InsetMathNest(1)
94 {}
95
96
97 void InsetMathFBox::metrics(MetricsInfo & mi, Dimension & dim) const
98 {
99         FontSetChanger dummy(mi.base, "textnormal");
100         cell(0).metrics(mi, dim);
101         metricsMarkers(dim, 3); // 1 pixel space, 1 frame, 1 space
102 }
103
104
105 void InsetMathFBox::draw(PainterInfo & pi, int x, int y) const
106 {
107         Dimension const dim = dimension(*pi.base.bv);
108         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
109                 dim.width() - 2, dim.height() - 2, Color_foreground);
110         FontSetChanger dummy(pi.base, "textnormal");
111         cell(0).draw(pi, x + 3, y);
112         setPosCache(pi, x, y);
113 }
114
115
116 void InsetMathFBox::write(WriteStream & os) const
117 {
118         os << "\\fbox{" << cell(0) << '}';
119 }
120
121
122 void InsetMathFBox::normalize(NormalStream & os) const
123 {
124         os << "[fbox " << cell(0) << ']';
125 }
126
127
128 void InsetMathFBox::infoize(odocstream & os) const
129 {
130         os << "FBox: ";
131 }
132
133
134 /////////////////////////////////////////////////////////////////////
135 //
136 // InsetMathMakebox
137 //
138 /////////////////////////////////////////////////////////////////////
139
140
141 InsetMathMakebox::InsetMathMakebox(bool framebox)
142         : InsetMathNest(3), framebox_(framebox)
143 {}
144
145
146 void InsetMathMakebox::metrics(MetricsInfo & mi, Dimension & dim) const
147 {
148         FontSetChanger dummy(mi.base, "textnormal");
149         
150         Dimension wdim;
151         static docstring bracket = from_ascii("[");
152         mathed_string_dim(mi.base.font, bracket, wdim);
153         int w = wdim.wid;
154         
155         Dimension dim0;
156         Dimension dim1;
157         Dimension dim2;
158         cell(0).metrics(mi, dim0);
159         cell(1).metrics(mi, dim1);
160         cell(2).metrics(mi, dim2);
161         
162         dim.wid = w + dim0.wid + w + w + dim1.wid + w + 2 + dim2.wid;
163         dim.asc = std::max(std::max(wdim.asc, dim0.asc), std::max(dim1.asc, dim2.asc)); 
164         dim.des = std::max(std::max(wdim.des, dim0.des), std::max(dim1.des, dim2.des));
165         
166         if (framebox_) {
167                 dim.wid += 4;
168                 dim.asc += 3;
169                 dim.des += 2;
170         } else {
171                 dim.asc += 1;
172                 dim.des += 1;
173         }
174         
175         metricsMarkers(dim);
176 }
177
178
179 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
180 {
181         drawMarkers(pi, x, y);
182         
183         FontSetChanger dummy(pi.base, "textnormal");
184         BufferView const & bv = *pi.base.bv;
185         int w = mathed_char_width(pi.base.font, '[');
186         
187         if (framebox_) {
188                 Dimension const dim = dimension(*pi.base.bv);
189                 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
190                                   dim.width() - 2, dim.height() - 2, Color_foreground);
191                 x += 2;
192         }
193         
194         drawStrBlack(pi, x, y, from_ascii("["));
195         x += w;
196         cell(0).draw(pi, x, y);
197         x += cell(0).dimension(bv).wid;
198         drawStrBlack(pi, x, y, from_ascii("]"));
199         x += w;
200
201         drawStrBlack(pi, x, y, from_ascii("["));
202         x += w;
203         cell(1).draw(pi, x, y);
204         x += cell(1).dimension(bv).wid;
205         drawStrBlack(pi, x, y, from_ascii("]"));
206         x += w + 2;
207
208         cell(2).draw(pi, x, y);
209 }
210
211
212 void InsetMathMakebox::write(WriteStream & os) const
213 {
214         os << (framebox_ ? "\\framebox" : "\\makebox");
215         if (cell(0).size() || !os.latex()) {
216                 os << '[' << cell(0) << ']';
217                 if (cell(1).size() || !os.latex())
218                         os << '[' << cell(1) << ']';
219         }
220         os << '{' << cell(2) << '}';
221 }
222
223
224 void InsetMathMakebox::normalize(NormalStream & os) const
225 {
226         os << (framebox_ ? "[framebox " : "[makebox ")
227            << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
228 }
229
230
231 void InsetMathMakebox::infoize(odocstream & os) const
232 {
233         os << (framebox_ ? "Framebox" : "Makebox") 
234            << " (width: " << cell(0)
235            << " pos: " << cell(1) << ")";
236 }
237
238
239 /////////////////////////////////////////////////////////////////////
240 //
241 // InsetMathBoxed
242 //
243 /////////////////////////////////////////////////////////////////////
244
245 InsetMathBoxed::InsetMathBoxed()
246         : InsetMathNest(1)
247 {}
248
249
250 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
251 {
252         cell(0).metrics(mi, dim);
253         metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
254 }
255
256
257 void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
258 {
259         Dimension const dim = dimension(*pi.base.bv);
260         pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
261                 dim.width() - 2, dim.height() - 2, Color_foreground);
262         cell(0).draw(pi, x + 3, y);
263         setPosCache(pi, x, y);
264 }
265
266
267 void InsetMathBoxed::write(WriteStream & os) const
268 {
269         os << "\\boxed{" << cell(0) << '}';
270 }
271
272
273 void InsetMathBoxed::normalize(NormalStream & os) const
274 {
275         os << "[boxed " << cell(0) << ']';
276 }
277
278
279 void InsetMathBoxed::infoize(odocstream & os) const
280 {
281         os << "Boxed: ";
282 }
283
284
285 void InsetMathBoxed::validate(LaTeXFeatures & features) const
286 {
287         features.require("amsmath");
288 }
289
290
291 } // namespace lyx