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