]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFrac.C
This commit saves the need to check for lyx::use_gui in a number of places.
[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 "MathMLStream.h"
17 #include "TextPainter.h"
18 #include "LaTeXFeatures.h"
19 #include "LColor.h"
20 #include "frontends/Painter.h"
21
22
23 using std::string;
24 using std::max;
25 using std::auto_ptr;
26
27
28 InsetMathFrac::InsetMathFrac(Kind kind)
29         : kind_(kind)
30 {}
31
32
33 auto_ptr<InsetBase> InsetMathFrac::doClone() const
34 {
35         return auto_ptr<InsetBase>(new InsetMathFrac(*this));
36 }
37
38
39 InsetMathFrac * InsetMathFrac::asFracInset()
40 {
41         return kind_ == ATOP ? 0 : this;
42 }
43
44
45 InsetMathFrac const * InsetMathFrac::asFracInset() const
46 {
47         return kind_ == ATOP ? 0 : this;
48 }
49
50
51 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
52 {
53         FracChanger dummy(mi.base);
54         cell(0).metrics(mi);
55         cell(1).metrics(mi);
56         if (kind_ == NICEFRAC) {
57                 dim.wid =  cell(0).width() + cell(1).width() + 5;
58                 dim.asc = cell(0).height() + 5;
59                 dim.des = cell(1).height() - 5;
60         } else {
61                 dim.wid = max(cell(0).width(), cell(1).width()) + 2;
62                 dim.asc = cell(0).height() + 2 + 5;
63                 dim.des = cell(1).height() + 2 - 5;
64         }
65         metricsMarkers(dim);
66         dim_ = dim;
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, LColor::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, LColor::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 = 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 string InsetMathFrac::name() const
138 {
139         switch (kind_) {
140         case FRAC:
141                 return "frac";
142         case OVER:
143                 return "over";
144         case NICEFRAC:
145                 return "nicefrac";
146         case ATOP:
147                 return "atop";
148         }
149         // shut up stupid compiler
150         return string();
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(MathMLStream & 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