]> git.lyx.org Git - lyx.git/blob - src/mathed/math_deliminset.C
Ensure all #warning statements are wrapped by #ifdef WITH_WARNINGS.
[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::clone() 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 }
115
116
117 bool MathDelimInset::isParanthesis() const
118 {
119         return left_ == "(" && right_ == ")";
120 }
121
122
123 bool MathDelimInset::isBrackets() const
124 {
125         return left_ == "[" && right_ == "]";
126 }
127
128
129 bool MathDelimInset::isAbs() const
130 {
131         return left_ == "|" && right_ == "|";
132 }
133
134
135 void MathDelimInset::maple(MapleStream & os) const
136 {
137         if (isAbs()) {
138                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
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::maxima(MaximaStream & os) const
148 {
149         if (isAbs()) {
150                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
151                         os << "determinant(" << cell(0) << ')';
152                 else
153                         os << "abs(" << cell(0) << ')';
154         }
155         else
156                 os << left_ << cell(0) << right_;
157 }
158
159
160 void MathDelimInset::mathematica(MathematicaStream & os) const
161 {
162         if (isAbs()) {
163                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
164                         os << "Det" << cell(0) << ']';
165                 else
166                         os << "Abs[" << cell(0) << ']';
167         }
168         else
169                 os << left_ << cell(0) << right_;
170 }
171
172
173 void MathDelimInset::mathmlize(MathMLStream & os) const
174 {
175         os << "<fenced open=\"" << left_ << "\" close=\""
176                 << right_ << "\">" << cell(0) << "</fenced>";
177 }
178
179
180 void MathDelimInset::octave(OctaveStream & os) const
181 {
182         if (isAbs())
183                 os << "det(" << cell(0) << ')';
184         else
185                 os << left_ << cell(0) << right_;
186 }