]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFrac.cpp
b77dc0f5084ccdd03669cb4508cc91fc442dbe6f
[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 InsetMathFrac::InsetMathFrac(Kind kind)
26         : kind_(kind)
27 {}
28
29
30 Inset * InsetMathFrac::clone() const
31 {
32         return new InsetMathFrac(*this);
33 }
34
35
36 InsetMathFrac * InsetMathFrac::asFracInset()
37 {
38         return kind_ == ATOP ? 0 : this;
39 }
40
41
42 InsetMathFrac const * InsetMathFrac::asFracInset() const
43 {
44         return kind_ == ATOP ? 0 : this;
45 }
46
47
48 bool InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
49 {
50         FracChanger dummy(mi.base);
51         cell(0).metrics(mi);
52         cell(1).metrics(mi);
53         if (kind_ == NICEFRAC) {
54                 dim.wid = cell(0).width() + cell(1).width() + 5;
55                 dim.asc = cell(0).height() + 5;
56                 dim.des = cell(1).height() - 5;
57         } else {
58                 dim.wid = std::max(cell(0).width(), cell(1).width()) + 2;
59                 dim.asc = cell(0).height() + 2 + 5;
60                 dim.des = cell(1).height() + 2 - 5;
61         }
62         metricsMarkers(dim);
63         if (dim_ == dim)
64                 return false;
65         dim_ = dim;
66         return true;
67 }
68
69
70 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
71 {
72         setPosCache(pi, x, y);
73         int m = x + dim_.wid / 2;
74         FracChanger dummy(pi.base);
75         if (kind_ == NICEFRAC) {
76                 cell(0).draw(pi, x + 2,
77                                 y - cell(0).descent() - 5);
78                 cell(1).draw(pi, x + cell(0).width() + 5,
79                                 y + cell(1).ascent() / 2);
80                 pi.pain.line(x + cell(0).width(),
81                                 y + dim_.des - 2,
82                                 x + cell(0).width() + 5,
83                                 y - dim_.asc + 2, Color::math);
84         } else {
85                 cell(0).draw(pi, m - cell(0).width() / 2,
86                                 y - cell(0).descent() - 2 - 5);
87                 cell(1).draw(pi, m - cell(1).width() / 2,
88                                 y + cell(1).ascent()  + 2 - 5);
89         }
90         if (kind_ == FRAC || kind_ == OVER)
91                 pi.pain.line(x + 1, y - 5,
92                                 x + dim_.wid - 2, y - 5, Color::math);
93         drawMarkers(pi, x, y);
94 }
95
96
97 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
98 {
99         cell(0).metricsT(mi, dim);
100         cell(1).metricsT(mi, dim);
101         dim.wid = std::max(cell(0).width(), cell(1).width());
102         dim.asc = cell(0).height() + 1;
103         dim.des = cell(1).height();
104         //dim = dim_;
105 }
106
107
108 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
109 {
110         int m = x + dim_.width() / 2;
111         cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
112         cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
113         // ASCII art: ignore niceties
114         if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC)
115                 pain.horizontalLine(x, y, dim_.width());
116 }
117
118
119 void InsetMathFrac::write(WriteStream & os) const
120 {
121         switch (kind_) {
122         case ATOP:
123                 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
124                 break;
125         case OVER:
126                 // \\over is only for compatibility, normalize this to \\frac
127                 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
128                 break;
129         case FRAC:
130         case NICEFRAC:
131                 InsetMathNest::write(os);
132                 break;
133         }
134 }
135
136
137 docstring InsetMathFrac::name() const
138 {
139         switch (kind_) {
140         case FRAC:
141                 return from_ascii("frac");
142         case OVER:
143                 return from_ascii("over");
144         case NICEFRAC:
145                 return from_ascii("nicefrac");
146         case ATOP:
147                 return from_ascii("atop");
148         }
149         // shut up stupid compiler
150         return docstring();
151 }
152
153
154 bool InsetMathFrac::extraBraces() const
155 {
156         return kind_ == ATOP || kind_ == OVER;
157 }
158
159
160 void InsetMathFrac::maple(MapleStream & os) const
161 {
162         os << '(' << cell(0) << ")/(" << cell(1) << ')';
163 }
164
165
166 void InsetMathFrac::mathematica(MathematicaStream & os) const
167 {
168         os << '(' << cell(0) << ")/(" << cell(1) << ')';
169 }
170
171
172 void InsetMathFrac::octave(OctaveStream & os) const
173 {
174         os << '(' << cell(0) << ")/(" << cell(1) << ')';
175 }
176
177
178 void InsetMathFrac::mathmlize(MathStream & os) const
179 {
180         os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
181 }
182
183
184 void InsetMathFrac::validate(LaTeXFeatures & features) const
185 {
186         if (kind_ == NICEFRAC)
187                 features.require("nicefrac");
188         InsetMathNest::validate(features);
189 }
190
191
192
193 } // namespace lyx