]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDelim.cpp
209e13bd7ccfaba94d9cf3431ea28b17d4d31dbe
[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 "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         dim_ = dim;
87 }
88
89
90 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
91 {
92         int const b = y - dim_.asc;
93         cell(0).draw(pi, x + dw_ + 4, y);
94         mathed_draw_deco(pi, x + 4, b, dw_, dim_.height(), left_);
95         mathed_draw_deco(pi, x + dim_.width() - dw_ - 4,
96                 b, dw_, dim_.height(), right_);
97         setPosCache(pi, x, y);
98 }
99
100
101 bool InsetMathDelim::isParenthesis() const
102 {
103         return left_ == "(" && right_ == ")";
104 }
105
106
107 bool InsetMathDelim::isBrackets() const
108 {
109         return left_ == "[" && right_ == "]";
110 }
111
112
113 bool InsetMathDelim::isAbs() const
114 {
115         return left_ == "|" && right_ == "|";
116 }
117
118
119 void InsetMathDelim::maple(MapleStream & os) const
120 {
121         if (isAbs()) {
122                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
123                         os << "linalg[det](" << cell(0) << ')';
124                 else
125                         os << "abs(" << cell(0) << ')';
126         }
127         else
128                 os << left_ << cell(0) << right_;
129 }
130
131
132 void InsetMathDelim::maxima(MaximaStream & os) const
133 {
134         if (isAbs()) {
135                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
136                         os << "determinant(" << cell(0) << ')';
137                 else
138                         os << "abs(" << cell(0) << ')';
139         }
140         else
141                 os << left_ << cell(0) << right_;
142 }
143
144
145 void InsetMathDelim::mathematica(MathematicaStream & os) const
146 {
147         if (isAbs()) {
148                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
149                         os << "Det" << cell(0) << ']';
150                 else
151                         os << "Abs[" << cell(0) << ']';
152         }
153         else
154                 os << left_ << cell(0) << right_;
155 }
156
157
158 void InsetMathDelim::mathmlize(MathStream & os) const
159 {
160         os << "<fenced open=\"" << left_ << "\" close=\""
161                 << right_ << "\">" << cell(0) << "</fenced>";
162 }
163
164
165 void InsetMathDelim::octave(OctaveStream & os) const
166 {
167         if (isAbs())
168                 os << "det(" << cell(0) << ')';
169         else
170                 os << left_ << cell(0) << right_;
171 }
172
173
174 } // namespace lyx