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