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