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