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