]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFrac.cpp
remove unneeded header.
[lyx.git] / src / mathed / InsetMathFrac.cpp
1 /**
2  * \file InsetMathFrac.cpp
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 "Color.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<Inset> InsetMathFrac::doClone() const
37 {
38         return auto_ptr<Inset>(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 bool 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         if (dim_ == dim)
70                 return false;
71         dim_ = dim;
72         return true;
73 }
74
75
76 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
77 {
78         setPosCache(pi, x, y);
79         int m = x + dim_.wid / 2;
80         FracChanger dummy(pi.base);
81         if (kind_ == NICEFRAC) {
82                 cell(0).draw(pi, x + 2, 
83                                 y - cell(0).descent() - 5);
84                 cell(1).draw(pi, x + cell(0).width() + 5,
85                                 y + cell(1).ascent() / 2);
86                 pi.pain.line(x + cell(0).width(), 
87                                 y + dim_.des - 2, 
88                                 x + cell(0).width() + 5, 
89                                 y - dim_.asc + 2, Color::math);
90         } else {
91                 cell(0).draw(pi, m - cell(0).width() / 2, 
92                                 y - cell(0).descent() - 2 - 5);
93                 cell(1).draw(pi, m - cell(1).width() / 2,
94                                 y + cell(1).ascent()  + 2 - 5);
95         }
96         if (kind_ == FRAC || kind_ == OVER)
97                 pi.pain.line(x + 1, y - 5, 
98                                 x + dim_.wid - 2, y - 5, Color::math);
99         drawMarkers(pi, x, y);
100 }
101
102
103 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
104 {
105         cell(0).metricsT(mi, dim);
106         cell(1).metricsT(mi, dim);
107         dim.wid = max(cell(0).width(), cell(1).width());
108         dim.asc = cell(0).height() + 1;
109         dim.des = cell(1).height();
110         //dim = dim_;
111 }
112
113
114 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
115 {
116         int m = x + dim_.width() / 2;
117         cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
118         cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
119         // ASCII art: ignore niceties
120         if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC)
121                 pain.horizontalLine(x, y, dim_.width());
122 }
123
124
125 void InsetMathFrac::write(WriteStream & os) const
126 {
127         switch (kind_) {
128         case ATOP:
129                 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
130                 break;
131         case OVER:
132                 // \\over is only for compatibility, normalize this to \\frac
133                 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
134                 break;
135         case FRAC:
136         case NICEFRAC:
137                 InsetMathNest::write(os);
138                 break;
139         }
140 }
141
142
143 docstring InsetMathFrac::name() const
144 {
145         switch (kind_) {
146         case FRAC:
147                 return from_ascii("frac");
148         case OVER:
149                 return from_ascii("over");
150         case NICEFRAC:
151                 return from_ascii("nicefrac");
152         case ATOP:
153                 return from_ascii("atop");
154         }
155         // shut up stupid compiler
156         return docstring();
157 }
158
159
160 bool InsetMathFrac::extraBraces() const
161 {
162         return kind_ == ATOP || kind_ == OVER;
163 }
164
165
166 void InsetMathFrac::maple(MapleStream & os) const
167 {
168         os << '(' << cell(0) << ")/(" << cell(1) << ')';
169 }
170
171
172 void InsetMathFrac::mathematica(MathematicaStream & os) const
173 {
174         os << '(' << cell(0) << ")/(" << cell(1) << ')';
175 }
176
177
178 void InsetMathFrac::octave(OctaveStream & os) const
179 {
180         os << '(' << cell(0) << ")/(" << cell(1) << ')';
181 }
182
183
184 void InsetMathFrac::mathmlize(MathStream & os) const
185 {
186         os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
187 }
188
189
190 void InsetMathFrac::validate(LaTeXFeatures & features) const
191 {
192         if (kind_ == NICEFRAC)
193                 features.require("nicefrac");
194         InsetMathNest::validate(features);
195 }
196
197
198
199 } // namespace lyx