]> git.lyx.org Git - lyx.git/blob - src/mathed/math_deliminset.C
make \newcommand{\bb}[1]{\mathbf{#1}} work for read/write/display.
[lyx.git] / src / mathed / math_deliminset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_deliminset.h"
8 #include "math_parser.h"
9 #include "math_support.h"
10 #include "math_mathmlstream.h"
11 #include "math_streamstr.h"
12 #include "math_extern.h"
13
14
15 MathDelimInset::MathDelimInset(string const & l, string const & r)
16         : MathNestInset(1), left_(l), right_(r)
17 {}
18
19
20 MathInset * MathDelimInset::clone() const
21 {   
22         return new MathDelimInset(*this);
23 }
24
25
26 string MathDelimInset::latexName(string const & name)
27 {
28         if (name == "(")
29                 return name;
30         if (name == "[")
31                 return name;
32         if (name == ".")
33                 return name;
34         if (name == ")")
35                 return name;
36         if (name == "]")
37                 return name;
38         if (name == "/")
39                 return name;
40         if (name == "|")
41                 return name;
42         return "\\" + name + " ";
43 }
44
45
46 void MathDelimInset::write(WriteStream & os) const
47 {
48         os << "\\left" << latexName(left_) << cell(0)
49            << "\\right" << latexName(right_);
50 }
51
52
53 void MathDelimInset::normalize(NormalStream & os) const
54 {
55         os << "[delim " << latexName(left_) << ' '
56            << latexName(right_) << ' ' << cell(0) << ']';
57 }
58
59
60 int MathDelimInset::dw() const
61 {
62         int w = height() / 5;
63         if (w > 12)
64                 w = 12;
65         if (w < 4)
66                 w = 4;
67         return w;
68 }
69
70
71 void MathDelimInset::metrics(MathMetricsInfo const & mi) const
72 {
73         xcell(0).metrics(mi);
74         int a, d, w;
75         mathed_char_dim(LM_TC_VAR, mi, 'I', a, d, w);
76         int h0   = (a + d) / 2;
77         int a0   = std::max(xcell(0).ascent(), a)   - h0;
78         int d0   = std::max(xcell(0).descent(), d)  + h0;
79         ascent_  = std::max(a0, d0) + h0;
80         descent_ = std::max(a0, d0) - h0;
81         width_   = xcell(0).width() + 2 * dw() + 4;
82 }
83
84
85 void MathDelimInset::draw(Painter & pain, int x, int y) const
86
87         int const w = dw();
88         int const b = y - ascent_;
89         xcell(0).draw(pain, x + w + 2, y);
90         mathed_draw_deco(pain, x + 1, b, w, height(), left_);
91         mathed_draw_deco(pain, x + width() - w - 1, b, w, height(), right_);
92 }
93
94
95 bool MathDelimInset::isParanthesis() const
96 {
97         return left_ == "(" && right_ == ")";
98 }
99
100
101 bool MathDelimInset::isBrackets() const
102 {
103         return left_ == "[" && right_ == "]";
104 }
105
106
107 bool MathDelimInset::isAbs() const
108 {
109         return left_ == "|" && right_ == "|";
110 }
111
112
113 void MathDelimInset::maplize(MapleStream & os) const
114 {
115         if (isAbs()) {
116                 bool mat =
117                         cell(0).size() == 1 && cell(0).begin()->nucleus()
118                                         && cell(0).begin()->nucleus()->asMatrixInset();
119                 if (mat)        
120                         os << "linalg[det](" << cell(0) << ")";
121                 else
122                         os << "abs(" << cell(0) << ")";
123         }
124         else
125                 os << left_ << cell(0) << right_;
126 }
127
128
129 void MathDelimInset::mathmlize(MathMLStream & os) const
130 {
131         os << "<fenced open=\"" << left_ << "\" close=\""
132                 << right_ << "\">" << cell(0) << "</fenced>";
133 }
134
135
136 void MathDelimInset::octavize(OctaveStream & os) const
137 {
138         if (isAbs())
139                 os << "det(" << cell(0) << ")";
140         else
141                 os << left_ << cell(0) << right_;
142 }