]> git.lyx.org Git - lyx.git/blob - src/mathed/math_deliminset.C
merge MathArray and MathXArray classes.
[lyx.git] / src / mathed / math_deliminset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_deliminset.h"
8 #include "math_parser.h"
9 #include "math_support.h"
10 #include "math_mathmlstream.h"
11 #include "math_streamstr.h"
12 #include "math_extern.h"
13
14
15 using std::max;
16
17 namespace {
18
19 string convertDelimToLatexName(string const & name)
20 {
21         if (name == "<")
22                 return name;
23         if (name == "(")
24                 return name;
25         if (name == "[")
26                 return name;
27         if (name == ".")
28                 return name;
29         if (name == ">")
30                 return name;
31         if (name == ")")
32                 return name;
33         if (name == "]")
34                 return name;
35         if (name == "/")
36                 return name;
37         if (name == "|")
38                 return name;
39         return "\\" + name + " ";
40 }
41
42 }
43
44
45
46 MathDelimInset::MathDelimInset(string const & l, string const & r)
47         : MathNestInset(1), left_(l), right_(r)
48 {}
49
50
51 MathDelimInset::MathDelimInset
52                 (string const & l, string const & r, MathArray const & ar)
53         : MathNestInset(1), left_(l), right_(r)
54 {
55         cell(0) = ar;
56 }
57
58
59 MathInset * MathDelimInset::clone() const
60 {
61         return new MathDelimInset(*this);
62 }
63
64
65 void MathDelimInset::write(WriteStream & os) const
66 {
67         os << "\\left" << convertDelimToLatexName(left_) << cell(0)
68            << "\\right" << convertDelimToLatexName(right_);
69 }
70
71
72 void MathDelimInset::normalize(NormalStream & os) const
73 {
74         os << "[delim " << convertDelimToLatexName(left_) << ' '
75            << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
76 }
77
78
79 int MathDelimInset::dw() const
80 {
81         int w = height() / 5;
82         if (w > 8)
83                 w = 8;
84         if (w < 4)
85                 w = 4;
86         return w;
87 }
88
89
90 void MathDelimInset::metrics(MathMetricsInfo & mi) const
91 {
92         cell(0).metrics(mi);
93         Dimension t;
94         mathed_char_dim(mi.base.font, 'I', t);
95         int h0 = (t.a + t.d) / 2;
96         int a0 = max(cell(0).ascent(), t.a)   - h0;
97         int d0 = max(cell(0).descent(), t.d)  + h0;
98         dim_.a = max(a0, d0) + h0;
99         dim_.d = max(a0, d0) - h0;
100         dim_.w = cell(0).width() + 2 * dw() + 8;
101 }
102
103
104 void MathDelimInset::draw(MathPainterInfo & pi, int x, int y) const
105 {
106         int const w = dw();
107         int const b = y - ascent();
108         cell(0).draw(pi, x + w + 4, y);
109         mathed_draw_deco(pi, x + 4, b, w, height(), left_);
110         mathed_draw_deco(pi, x + width() - w - 4, b, w, height(), right_);
111 }
112
113
114 bool MathDelimInset::isParanthesis() const
115 {
116         return left_ == "(" && right_ == ")";
117 }
118
119
120 bool MathDelimInset::isBrackets() const
121 {
122         return left_ == "[" && right_ == "]";
123 }
124
125
126 bool MathDelimInset::isAbs() const
127 {
128         return left_ == "|" && right_ == "|";
129 }
130
131
132 void MathDelimInset::maplize(MapleStream & os) const
133 {
134         if (isAbs()) {
135                 bool mat =
136                         cell(0).size() == 1 && cell(0).begin()->nucleus()
137                                         && cell(0).begin()->nucleus()->asMatrixInset();
138                 if (mat)
139                         os << "linalg[det](" << cell(0) << ")";
140                 else
141                         os << "abs(" << cell(0) << ")";
142         }
143         else
144                 os << left_ << cell(0) << right_;
145 }
146
147 void MathDelimInset::mathematicize(MathematicaStream & os) const
148 {
149         if (isAbs()) {
150                 bool mat =
151                         cell(0).size() == 1 && cell(0).begin()->nucleus()
152                                         && cell(0).begin()->nucleus()->asMatrixInset();
153                 if (mat)
154                         os << "Det" << cell(0) << ']';
155                 else
156                         os << "Abs[" << cell(0) << ']';
157         }
158         else
159                 os << left_ << cell(0) << right_;
160 }
161
162
163
164 void MathDelimInset::mathmlize(MathMLStream & os) const
165 {
166         os << "<fenced open=\"" << left_ << "\" close=\""
167                 << right_ << "\">" << cell(0) << "</fenced>";
168 }
169
170
171 void MathDelimInset::octavize(OctaveStream & os) const
172 {
173         if (isAbs())
174                 os << "det(" << cell(0) << ")";
175         else
176                 os << left_ << cell(0) << right_;
177 }