]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFrac.C
fix reading UTF8 encoded symbol file
[lyx.git] / src / mathed / InsetMathFrac.C
1 /**
2  * \file InsetMathFrac.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetMathFrac.h"
15 #include "MathData.h"
16 #include "MathStream.h"
17 #include "TextPainter.h"
18 #include "LaTeXFeatures.h"
19 #include "LColor.h"
20 #include "frontends/Painter.h"
21
22
23 namespace lyx {
24
25
26 using std::string;
27 using std::max;
28 using std::auto_ptr;
29
30
31 InsetMathFrac::InsetMathFrac(Kind kind)
32         : kind_(kind)
33 {}
34
35
36 auto_ptr<InsetBase> InsetMathFrac::doClone() const
37 {
38         return auto_ptr<InsetBase>(new InsetMathFrac(*this));
39 }
40
41
42 InsetMathFrac * InsetMathFrac::asFracInset()
43 {
44         return kind_ == ATOP ? 0 : this;
45 }
46
47
48 InsetMathFrac const * InsetMathFrac::asFracInset() const
49 {
50         return kind_ == ATOP ? 0 : this;
51 }
52
53
54 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
55 {
56         FracChanger dummy(mi.base);
57         cell(0).metrics(mi);
58         cell(1).metrics(mi);
59         if (kind_ == NICEFRAC) {
60                 dim.wid =  cell(0).width() + cell(1).width() + 5;
61                 dim.asc = cell(0).height() + 5;
62                 dim.des = cell(1).height() - 5;
63         } else {
64                 dim.wid = max(cell(0).width(), cell(1).width()) + 2;
65                 dim.asc = cell(0).height() + 2 + 5;
66                 dim.des = cell(1).height() + 2 - 5;
67         }
68         metricsMarkers(dim);
69         dim_ = dim;
70 }
71
72
73 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
74 {
75         setPosCache(pi, x, y);
76         int m = x + dim_.wid / 2;
77         FracChanger dummy(pi.base);
78         if (kind_ == NICEFRAC) {
79                 cell(0).draw(pi, x + 2, 
80                                 y - cell(0).descent() - 5);
81                 cell(1).draw(pi, x + cell(0).width() + 5,
82                                 y + cell(1).ascent() / 2);
83                 pi.pain.line(x + cell(0).width(), 
84                                 y + dim_.des - 2, 
85                                 x + cell(0).width() + 5, 
86                                 y - dim_.asc + 2, LColor::math);
87         } else {
88                 cell(0).draw(pi, m - cell(0).width() / 2, 
89                                 y - cell(0).descent() - 2 - 5);
90                 cell(1).draw(pi, m - cell(1).width() / 2,
91                                 y + cell(1).ascent()  + 2 - 5);
92         }
93         if (kind_ == FRAC || kind_ == OVER)
94                 pi.pain.line(x + 1, y - 5, 
95                                 x + dim_.wid - 2, y - 5, LColor::math);
96         drawMarkers(pi, x, y);
97 }
98
99
100 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
101 {
102         cell(0).metricsT(mi, dim);
103         cell(1).metricsT(mi, dim);
104         dim.wid = max(cell(0).width(), cell(1).width());
105         dim.asc = cell(0).height() + 1;
106         dim.des = cell(1).height();
107         //dim = dim_;
108 }
109
110
111 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
112 {
113         int m = x + dim_.width() / 2;
114         cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
115         cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
116         // ASCII art: ignore niceties
117         if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC)
118                 pain.horizontalLine(x, y, dim_.width());
119 }
120
121
122 void InsetMathFrac::write(WriteStream & os) const
123 {
124         switch (kind_) {
125         case ATOP:
126                 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
127                 break;
128         case OVER:
129                 // \\over is only for compatibility, normalize this to \\frac
130                 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
131                 break;
132         case FRAC:
133         case NICEFRAC:
134                 InsetMathNest::write(os);
135                 break;
136         }
137 }
138
139
140 docstring InsetMathFrac::name() const
141 {
142         switch (kind_) {
143         case FRAC:
144                 return from_ascii("frac");
145         case OVER:
146                 return from_ascii("over");
147         case NICEFRAC:
148                 return from_ascii("nicefrac");
149         case ATOP:
150                 return from_ascii("atop");
151         }
152         // shut up stupid compiler
153         return docstring();
154 }
155
156
157 bool InsetMathFrac::extraBraces() const
158 {
159         return kind_ == ATOP || kind_ == OVER;
160 }
161
162
163 void InsetMathFrac::maple(MapleStream & os) const
164 {
165         os << '(' << cell(0) << ")/(" << cell(1) << ')';
166 }
167
168
169 void InsetMathFrac::mathematica(MathematicaStream & os) const
170 {
171         os << '(' << cell(0) << ")/(" << cell(1) << ')';
172 }
173
174
175 void InsetMathFrac::octave(OctaveStream & os) const
176 {
177         os << '(' << cell(0) << ")/(" << cell(1) << ')';
178 }
179
180
181 void InsetMathFrac::mathmlize(MathStream & os) const
182 {
183         os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
184 }
185
186
187 void InsetMathFrac::validate(LaTeXFeatures & features) const
188 {
189         if (kind_ == NICEFRAC)
190                 features.require("nicefrac");
191         InsetMathNest::validate(features);
192 }
193
194
195
196 } // namespace lyx