]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathStackrel.cpp
Do some caching of window title and related UI
[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 "Cursor.h"
16 #include "LaTeXFeatures.h"
17 #include "MathData.h"
18 #include "MathStream.h"
19
20 using namespace std;
21
22 namespace lyx {
23
24 InsetMathStackrel::InsetMathStackrel(Buffer * buf, bool sub)
25         : InsetMathFracBase(buf, sub ? 3 : 2)
26 {}
27
28
29 Inset * InsetMathStackrel::clone() const
30 {
31         return new InsetMathStackrel(*this);
32 }
33
34
35 bool InsetMathStackrel::idxUpDown(Cursor & cur, bool up) const
36 {
37         if (up) {
38                 if (cur.idx() == 0)
39                         return false;
40         } else {
41                 if (cur.idx() + 1 ==  nargs())
42                         return false;
43         }
44         InsetMath::idx_type target = up ? cur.idx() - 1 : cur.idx() + 1;
45         if (cur.idx() == target)
46                 return false;
47         cur.idx() = target;
48         cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
49         return true;
50 }
51
52
53 void InsetMathStackrel::metrics(MetricsInfo & mi, Dimension & dim) const
54 {
55         Dimension dim1;
56         cell(1).metrics(mi, dim1);
57         FracChanger dummy(mi.base);
58         Dimension dim0;
59         cell(0).metrics(mi, dim0);
60         if (nargs() > 2) {
61                 Dimension dim2;
62                 cell(2).metrics(mi, dim2);
63                 dim.wid = max(max(dim0.width(), dim1.width()), dim2.width()) + 4;
64                 dim.asc = dim1.ascent() + dim0.height() + 4;
65                 dim.des = dim1.descent() + dim2.height() + dim2.descent() + 1;
66         } else {
67                 dim.wid = max(dim0.width(), dim1.width()) + 4;
68                 dim.asc = dim1.ascent() + dim0.height() + 4;
69                 dim.des = dim1.descent();
70         }
71         metricsMarkers(dim);
72 }
73
74
75 void InsetMathStackrel::draw(PainterInfo & pi, int x, int y) const
76 {
77         Dimension const dim = dimension(*pi.base.bv);
78         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
79         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
80         int m  = x + dim.width() / 2;
81         int yo = y - dim1.ascent() - dim0.descent() - 1;
82         cell(1).draw(pi, m - dim1.width() / 2, y);
83         FracChanger dummy(pi.base);
84         cell(0).draw(pi, m - dim0.width() / 2, yo);
85         if (nargs() > 2) {
86                 Dimension const & dim2 = cell(2).dimension(*pi.base.bv);
87                 int y2 = y + dim1.descent() + dim2.ascent() + 1;
88                 cell(2).draw(pi, m - dim2.width() / 2, y2);
89         }
90         drawMarkers(pi, x, y);
91 }
92
93
94 void InsetMathStackrel::write(WriteStream & os) const
95 {
96         MathEnsurer ensurer(os);
97         os << "\\stackrel";
98         if (nargs() > 2)
99                 os << '[' << cell(2) << ']';
100         os << '{' << cell(0) << "}{" << cell(1) << '}';
101 }
102
103
104 void InsetMathStackrel::normalize(NormalStream & os) const
105 {
106         os << "[stackrel " << cell(0) << ' ' << cell(1);
107         if (nargs() > 2)
108                 os << ' ' << cell(2);
109         os << ']';
110 }
111
112
113 void InsetMathStackrel::mathmlize(MathStream & ms) const
114 {
115         if (nargs() > 2)
116                 ms << "<munderover>" << cell(1) << cell(2) << cell(0) << "</munderover>";
117         else
118                 ms << "<mover accent='false'>" << cell(1) << cell(0) << "</mover>";
119 }
120
121
122 void InsetMathStackrel::htmlize(HtmlStream & os) const
123 {
124         if (nargs() > 2) {
125                 os << MTag("span", "class='underoverset'")
126                    << MTag("span", "class='top'") << cell(0) << ETag("span")
127                    << MTag("span") << cell(1) << ETag("span")
128                    << MTag("span", "class='bottom'") << cell(2) << ETag("span");
129         } else {
130                 // at the moment, this is exactly the same as overset
131                 os << MTag("span", "class='overset'")
132                    << MTag("span", "class='top'") << cell(0) << ETag("span")
133                    << MTag("span") << cell(1) << ETag("span");
134         }
135         os << ETag("span");
136 }
137
138
139 void InsetMathStackrel::validate(LaTeXFeatures & features) const
140 {
141         if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
142                 if (nargs() > 2) {
143                         // FIXME: "vertical-align: middle" works only if the
144                         // height of sub and super script is approximately equal.
145                         features.addCSSSnippet(
146                                 "span.underoverset{display: inline-block; vertical-align: middle; text-align:center;}\n"
147                                 "span.underoverset span {display: block;}\n"
148                                 "span.bottom{font-size: 66%;}\n"
149                                 "span.top{font-size: 66%;}");
150                 } else {
151                         // from overset
152                         features.addCSSSnippet(
153                                 "span.overset{display: inline-block; vertical-align: bottom; text-align:center;}\n"
154                                 "span.overset span {display: block;}\n"
155                                 "span.top{font-size: 66%;}");
156                 }
157         }
158         if (nargs() > 2)
159                 features.require("stackrel");
160
161         InsetMathNest::validate(features);
162 }
163
164 } // namespace lyx