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