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