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