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