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