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