]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathRoot.cpp
Remove hardcoded values
[lyx.git] / src / mathed / InsetMathRoot.cpp
1 /**
2  * \file InsetMathRoot.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 "InsetMathRoot.h"
15
16 #include "MathData.h"
17 #include "MathStream.h"
18
19 #include "Cursor.h"
20 #include "LaTeXFeatures.h"
21 #include "MetricsInfo.h"
22
23 #include "frontends/Painter.h"
24
25
26 using namespace std;
27
28 namespace lyx {
29
30
31 InsetMathRoot::InsetMathRoot(Buffer * buf)
32         : InsetMathNest(buf, 2)
33 {}
34
35
36 Inset * InsetMathRoot::clone() const
37 {
38         return new InsetMathRoot(*this);
39 }
40
41
42 void InsetMathRoot::metrics(MetricsInfo & mi, Dimension & dim) const
43 {
44         Changer dummy = mi.base.changeEnsureMath();
45         InsetMathNest::metrics(mi);
46         Dimension const & dim0 = cell(0).dimension(*mi.base.bv);
47         Dimension const & dim1 = cell(1).dimension(*mi.base.bv);
48         dim.asc = max(dim0.ascent()  + 5, dim1.ascent())  + 2;
49         dim.des = max(dim0.descent() - 5, dim1.descent()) + 2;
50         dim.wid = dim0.width() + dim1.width() + 10;
51         metricsMarkers(mi, dim);
52 }
53
54
55 void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
56 {
57         Changer dummy = pi.base.changeEnsureMath();
58         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
59         int const w = dim0.width();
60         // the "exponent"
61         cell(0).draw(pi, x, y - 5 - dim0.descent());
62         // the "base"
63         cell(1).draw(pi, x + w + 8, y);
64         Dimension const dim = dimension(*pi.base.bv);
65         int const a = dim.ascent();
66         int const d = dim.descent();
67         int xp[4];
68         int yp[4];
69         pi.pain.line(x + dim.width(), y - a + 1,
70                                 x + w + 4, y - a + 1, pi.base.font.color());
71         xp[0] = x + w + 4;         yp[0] = y - a + 1;
72         xp[1] = x + w;             yp[1] = y + d;
73         xp[2] = x + w - 2;         yp[2] = y + (d - a)/2 + 2;
74         xp[3] = x + w - 5;         yp[3] = y + (d - a)/2 + 4;
75         pi.pain.lines(xp, yp, 4, pi.base.font.color());
76         drawMarkers(pi, x, y);
77 }
78
79
80 void InsetMathRoot::write(WriteStream & os) const
81 {
82         MathEnsurer ensurer(os);
83         os << "\\sqrt[" << cell(0) << "]{" << cell(1) << '}';
84 }
85
86
87 void InsetMathRoot::normalize(NormalStream & os) const
88 {
89         os << "[root " << cell(0) << ' ' << cell(1) << ']';
90 }
91
92
93 bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
94 {
95         Cursor::idx_type const target = up ? 0 : 1;
96         if (cur.idx() == target)
97                 return false;
98         cur.idx() = target;
99         cur.pos() = up ? cur.lastpos() : 0;
100         return true;
101 }
102
103
104 void InsetMathRoot::maple(MapleStream & os) const
105 {
106         os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
107 }
108
109
110 void InsetMathRoot::mathematica(MathematicaStream & os) const
111 {
112         os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
113 }
114
115
116 void InsetMathRoot::octave(OctaveStream & os) const
117 {
118         os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
119 }
120
121
122 void InsetMathRoot::mathmlize(MathStream & os) const
123 {
124         os << MTag("mroot") << cell(1) << cell(0) << ETag("mroot");
125 }
126
127
128 void InsetMathRoot::htmlize(HtmlStream & os) const
129 {
130         os << MTag("span", "class='root'")
131            << MTag("sup") << cell(0) << ETag("sup")
132            << from_ascii("&radic;") 
133            << MTag("span", "class='rootof'")    << cell(1) << ETag("span") 
134                  << ETag("span");
135 }
136
137
138 void InsetMathRoot::validate(LaTeXFeatures & features) const
139 {
140         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
141                 features.addCSSSnippet(
142                         "span.rootof{border-top: thin solid black;}\n"
143                         "span.root sup{font-size: 75%;}");
144         InsetMathNest::validate(features);
145 }
146
147 } // namespace lyx