]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDelim.cpp
Put MathData on a diet: transfer dimension cache to BufferView' CoordCache along...
[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         Dimension dim0;
74         cell(0).metrics(mi, dim0);
75         Dimension t = theFontMetrics(mi.base.font).dimension('I');
76         int h0 = (t.asc + t.des) / 2;
77         int a0 = max(dim0.asc, t.asc)   - h0;
78         int d0 = max(dim0.des, t.des)  + h0;
79         dw_ = dim0.height() / 5;
80         if (dw_ > 8)
81                 dw_ = 8;
82         if (dw_ < 4)
83                 dw_ = 4;
84         dim.wid = dim0.width() + 2 * dw_ + 8;
85         dim.asc = max(a0, d0) + h0;
86         dim.des = max(a0, d0) - h0;
87         // Cache the inset dimension. 
88         setDimCache(mi, dim);
89 }
90
91
92 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
93 {
94         Dimension const dim = dimension(*pi.base.bv);
95         int const b = y - dim.asc;
96         cell(0).draw(pi, x + dw_ + 4, y);
97         mathed_draw_deco(pi, x + 4, b, dw_, dim.height(), left_);
98         mathed_draw_deco(pi, x + dim.width() - dw_ - 4,
99                 b, dw_, dim.height(), right_);
100         setPosCache(pi, x, y);
101 }
102
103
104 bool InsetMathDelim::isParenthesis() const
105 {
106         return left_ == "(" && right_ == ")";
107 }
108
109
110 bool InsetMathDelim::isBrackets() const
111 {
112         return left_ == "[" && right_ == "]";
113 }
114
115
116 bool InsetMathDelim::isAbs() const
117 {
118         return left_ == "|" && right_ == "|";
119 }
120
121
122 void InsetMathDelim::maple(MapleStream & os) const
123 {
124         if (isAbs()) {
125                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
126                         os << "linalg[det](" << cell(0) << ')';
127                 else
128                         os << "abs(" << cell(0) << ')';
129         }
130         else
131                 os << left_ << cell(0) << right_;
132 }
133
134
135 void InsetMathDelim::maxima(MaximaStream & os) const
136 {
137         if (isAbs()) {
138                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
139                         os << "determinant(" << cell(0) << ')';
140                 else
141                         os << "abs(" << cell(0) << ')';
142         }
143         else
144                 os << left_ << cell(0) << right_;
145 }
146
147
148 void InsetMathDelim::mathematica(MathematicaStream & os) const
149 {
150         if (isAbs()) {
151                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
152                         os << "Det" << cell(0) << ']';
153                 else
154                         os << "Abs[" << cell(0) << ']';
155         }
156         else
157                 os << left_ << cell(0) << right_;
158 }
159
160
161 void InsetMathDelim::mathmlize(MathStream & os) const
162 {
163         os << "<fenced open=\"" << left_ << "\" close=\""
164                 << right_ << "\">" << cell(0) << "</fenced>";
165 }
166
167
168 void InsetMathDelim::octave(OctaveStream & os) const
169 {
170         if (isAbs())
171                 os << "det(" << cell(0) << ')';
172         else
173                 os << left_ << cell(0) << right_;
174 }
175
176
177 } // namespace lyx