]> git.lyx.org Git - lyx.git/blob - src/mathed/math_deliminset.C
Make Helge happy: no more crash on arrow up/down in math macro
[lyx.git] / src / mathed / math_deliminset.C
1 /**
2  * \file math_deliminset.C
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 "math_deliminset.h"
15 #include "math_data.h"
16 #include "math_mathmlstream.h"
17 #include "math_streamstr.h"
18 #include "math_support.h"
19
20
21 using std::string;
22 using std::max;
23 using std::auto_ptr;
24
25 namespace {
26
27 string convertDelimToLatexName(string const & name)
28 {
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         if (name == ")")
40                 return name;
41         if (name == "]")
42                 return name;
43         if (name == "/")
44                 return name;
45         if (name == "|")
46                 return name;
47         return '\\' + name + ' ';
48 }
49
50 }
51
52
53
54 MathDelimInset::MathDelimInset(string const & l, string const & r)
55         : MathNestInset(1), left_(l), right_(r)
56 {}
57
58
59 MathDelimInset::MathDelimInset
60                 (string const & l, string const & r, MathArray const & ar)
61         : MathNestInset(1), left_(l), right_(r)
62 {
63         cell(0) = ar;
64 }
65
66
67 auto_ptr<InsetBase> MathDelimInset::doClone() const
68 {
69         return auto_ptr<InsetBase>(new MathDelimInset(*this));
70 }
71
72
73 void MathDelimInset::write(WriteStream & os) const
74 {
75         os << "\\left" << convertDelimToLatexName(left_) << cell(0)
76            << "\\right" << convertDelimToLatexName(right_);
77 }
78
79
80 void MathDelimInset::normalize(NormalStream & os) const
81 {
82         os << "[delim " << convertDelimToLatexName(left_) << ' '
83            << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
84 }
85
86
87 void MathDelimInset::metrics(MetricsInfo & mi, Dimension & dim) const
88 {
89         cell(0).metrics(mi);
90         Dimension t;
91         mathed_char_dim(mi.base.font, 'I', t);
92         int h0 = (t.asc + t.des) / 2;
93         int a0 = max(cell(0).ascent(), t.asc)   - h0;
94         int d0 = max(cell(0).descent(), t.des)  + h0;
95         dw_ = cell(0).height() / 5;
96         if (dw_ > 8)
97                 dw_ = 8;
98         if (dw_ < 4)
99                 dw_ = 4;
100         dim_.wid = cell(0).width() + 2 * dw_ + 8;
101         dim_.asc = max(a0, d0) + h0;
102         dim_.des = max(a0, d0) - h0;
103         dim = dim_;
104 }
105
106
107 void MathDelimInset::draw(PainterInfo & pi, int x, int y) const
108 {
109         int const b = y - dim_.asc;
110         cell(0).draw(pi, x + dw_ + 4, y);
111         mathed_draw_deco(pi, x + 4, b, dw_, dim_.height(), left_);
112         mathed_draw_deco(pi, x + dim_.width() - dw_ - 4,
113                 b, dw_, dim_.height(), right_);
114         setPosCache(pi, x, y);
115 }
116
117
118 bool MathDelimInset::isParanthesis() const
119 {
120         return left_ == "(" && right_ == ")";
121 }
122
123
124 bool MathDelimInset::isBrackets() const
125 {
126         return left_ == "[" && right_ == "]";
127 }
128
129
130 bool MathDelimInset::isAbs() const
131 {
132         return left_ == "|" && right_ == "|";
133 }
134
135
136 void MathDelimInset::maple(MapleStream & os) const
137 {
138         if (isAbs()) {
139                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
140                         os << "linalg[det](" << cell(0) << ')';
141                 else
142                         os << "abs(" << cell(0) << ')';
143         }
144         else
145                 os << left_ << cell(0) << right_;
146 }
147
148 void MathDelimInset::maxima(MaximaStream & os) const
149 {
150         if (isAbs()) {
151                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
152                         os << "determinant(" << cell(0) << ')';
153                 else
154                         os << "abs(" << cell(0) << ')';
155         }
156         else
157                 os << left_ << cell(0) << right_;
158 }
159
160
161 void MathDelimInset::mathematica(MathematicaStream & os) const
162 {
163         if (isAbs()) {
164                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
165                         os << "Det" << cell(0) << ']';
166                 else
167                         os << "Abs[" << cell(0) << ']';
168         }
169         else
170                 os << left_ << cell(0) << right_;
171 }
172
173
174 void MathDelimInset::mathmlize(MathMLStream & os) const
175 {
176         os << "<fenced open=\"" << left_ << "\" close=\""
177                 << right_ << "\">" << cell(0) << "</fenced>";
178 }
179
180
181 void MathDelimInset::octave(OctaveStream & os) const
182 {
183         if (isAbs())
184                 os << "det(" << cell(0) << ')';
185         else
186                 os << left_ << cell(0) << right_;
187 }