]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathStackrel.cpp
de.po
[lyx.git] / src / mathed / InsetMathStackrel.cpp
1 /**
2  * \file InsetMathStackrel.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathStackrel.h"
14
15 #include "MathData.h"
16 #include "MathStream.h"
17
18 #include "Cursor.h"
19 #include "LaTeXFeatures.h"
20 #include "MetricsInfo.h"
21
22 #include "support/lassert.h"
23
24 using namespace std;
25
26 namespace lyx {
27
28 InsetMathStackrel::InsetMathStackrel(Buffer * buf, bool sub)
29         : InsetMathFracBase(buf, sub ? 3 : 2)
30 {}
31
32
33 Inset * InsetMathStackrel::clone() const
34 {
35         return new InsetMathStackrel(*this);
36 }
37
38
39 bool InsetMathStackrel::idxUpDown(Cursor & cur, bool up) const
40 {
41         idx_type const npos = 1234; // impossible number
42         idx_type target = npos;
43         if (up) {
44                 idx_type const targets[] = { 1, npos, 0 };
45                 target = targets[cur.idx()];
46         } else {
47                 idx_type const targets[] = { 2, 0, npos };
48                 target = targets[cur.idx()];
49         }
50
51         if (target == npos || target == nargs())
52                 return false;
53         cur.idx() = target;
54         cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
55         return true;
56 }
57
58
59 bool InsetMathStackrel::idxFirst(Cursor & cur) const
60 {
61         LASSERT(&cur.inset() == this, return false);
62         cur.idx() = 0;
63         cur.pos() = 0;
64         return true;
65 }
66
67
68 bool InsetMathStackrel::idxLast(Cursor & cur) const
69 {
70         LASSERT(&cur.inset() == this, return false);
71         cur.idx() = 0;
72         cur.pos() = cur.lastpos();
73         return true;
74 }
75
76
77 MathClass InsetMathStackrel::mathClass() const
78 {
79         // FIXME: update this when/if \stackbin is supported
80         return MC_REL;
81 }
82
83
84 void InsetMathStackrel::metrics(MetricsInfo & mi, Dimension & dim) const
85 {
86         Changer dummy2 = mi.base.changeEnsureMath();
87         Dimension dim0;
88         cell(0).metrics(mi, dim0);
89         Changer dummy = mi.base.changeScript();
90         Dimension dim1;
91         cell(1).metrics(mi, dim1);
92         if (nargs() > 2) {
93                 Dimension dim2;
94                 cell(2).metrics(mi, dim2);
95                 dim.wid = max(max(dim1.width(), dim0.width()), dim2.width()) + 4;
96                 dim.asc = dim0.ascent() + dim1.height() + 4;
97                 dim.des = dim0.descent() + dim2.height() + dim2.descent() + 1;
98         } else {
99                 dim.wid = max(dim1.width(), dim0.width()) + 4;
100                 dim.asc = dim0.ascent() + dim1.height() + 4;
101                 dim.des = dim0.descent();
102         }
103 }
104
105
106 void InsetMathStackrel::draw(PainterInfo & pi, int x, int y) const
107 {
108         Changer dummy2 = pi.base.changeEnsureMath();
109         Dimension const dim = dimension(*pi.base.bv);
110         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
111         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
112         int m  = x + dim.width() / 2;
113         int yo = y - dim0.ascent() - dim1.descent() - 1;
114         cell(0).draw(pi, m - dim0.width() / 2, y);
115         Changer dummy = pi.base.changeScript();
116         cell(1).draw(pi, m - dim1.width() / 2, yo);
117         if (nargs() > 2) {
118                 Dimension const & dim2 = cell(2).dimension(*pi.base.bv);
119                 int y2 = y + dim0.descent() + dim2.ascent() + 1;
120                 cell(2).draw(pi, m - dim2.width() / 2, y2);
121         }
122 }
123
124
125 void InsetMathStackrel::write(WriteStream & os) const
126 {
127         MathEnsurer ensurer(os);
128         os << "\\stackrel";
129         if (nargs() > 2)
130                 os << '[' << cell(2) << ']';
131         os << '{' << cell(1) << "}{" << cell(0) << '}';
132 }
133
134
135 void InsetMathStackrel::normalize(NormalStream & os) const
136 {
137         os << "[stackrel " << cell(1) << ' ' << cell(0);
138         if (nargs() > 2)
139                 os << ' ' << cell(2);
140         os << ']';
141 }
142
143
144 void InsetMathStackrel::mathmlize(MathStream & ms) const
145 {
146         if (nargs() > 2)
147                 ms << "<munderover>" << cell(0) << cell(2) << cell(1) << "</munderover>";
148         else
149                 ms << "<mover accent='false'>" << cell(0) << cell(1) << "</mover>";
150 }
151
152
153 void InsetMathStackrel::htmlize(HtmlStream & os) const
154 {
155         if (nargs() > 2) {
156                 os << MTag("span", "class='underoverset'")
157                    << MTag("span", "class='top'") << cell(1) << ETag("span")
158                    << MTag("span") << cell(0) << ETag("span")
159                    << MTag("span", "class='bottom'") << cell(2) << ETag("span");
160         } else {
161                 // at the moment, this is exactly the same as overset
162                 os << MTag("span", "class='overset'")
163                    << MTag("span", "class='top'") << cell(1) << ETag("span")
164                    << MTag("span") << cell(0) << ETag("span");
165         }
166         os << ETag("span");
167 }
168
169
170 void InsetMathStackrel::validate(LaTeXFeatures & features) const
171 {
172         if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
173                 if (nargs() > 2) {
174                         // FIXME: "vertical-align: middle" works only if the
175                         // height of sub and super script is approximately equal.
176                         features.addCSSSnippet(
177                                 "span.underoverset{display: inline-block; vertical-align: middle; text-align:center;}\n"
178                                 "span.underoverset span {display: block;}\n"
179                                 "span.bottom{font-size: 66%;}\n"
180                                 "span.top{font-size: 66%;}");
181                 } else {
182                         // from overset
183                         features.addCSSSnippet(
184                                 "span.overset{display: inline-block; vertical-align: bottom; text-align:center;}\n"
185                                 "span.overset span {display: block;}\n"
186                                 "span.top{font-size: 66%;}");
187                 }
188         }
189         if (nargs() > 2)
190                 features.require("stackrel");
191
192         InsetMathNest::validate(features);
193 }
194
195 } // namespace lyx