]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathDelim.cpp
19bcc6ccd8f254ad12bec287986575e18a5f97c2
[features.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 "MathStream.h"
18 #include "MathSupport.h"
19
20 #include "frontends/FontMetrics.h"
21
22
23 namespace lyx {
24
25 static docstring convertDelimToLatexName(docstring const & name)
26 {
27         if (name.size() == 1) {
28                 char_type const c = name[0];
29                 if (c == '<' || c == '(' || c == '[' || c == '.'
30                     || c == '>' || c == ')' || c == ']' || c == '/' || c == '|')
31                         return name;
32         }
33         return '\\' + name + ' ';
34 }
35
36
37 InsetMathDelim::InsetMathDelim(docstring const & l, docstring const & r)
38         : InsetMathNest(1), left_(l), right_(r)
39 {}
40
41
42 InsetMathDelim::InsetMathDelim
43                 (docstring const & l, docstring const & r, MathData const & ar)
44         : InsetMathNest(1), left_(l), right_(r)
45 {
46         cell(0) = ar;
47 }
48
49
50 Inset * InsetMathDelim::clone() const
51 {
52         return new InsetMathDelim(*this);
53 }
54
55
56 void InsetMathDelim::write(WriteStream & os) const
57 {
58         os << "\\left" << convertDelimToLatexName(left_) << cell(0)
59            << "\\right" << convertDelimToLatexName(right_);
60 }
61
62
63 void InsetMathDelim::normalize(NormalStream & os) const
64 {
65         os << "[delim " << convertDelimToLatexName(left_) << ' '
66            << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
67 }
68
69
70 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
71 {
72         using std::max;
73         cell(0).metrics(mi);
74         Dimension t = theFontMetrics(mi.base.font).dimension('I');
75         int h0 = (t.asc + t.des) / 2;
76         int a0 = max(cell(0).ascent(), t.asc)   - h0;
77         int d0 = max(cell(0).descent(), t.des)  + h0;
78         dw_ = cell(0).height() / 5;
79         if (dw_ > 8)
80                 dw_ = 8;
81         if (dw_ < 4)
82                 dw_ = 4;
83         dim.wid = cell(0).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