A bit more re-organization.
[lyx.git] / src / mathed / InsetMathDelim.cpp
blob4c873f15e8ea1f1fd9b923ee13d9e84a975efea3
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.
6 * \author Alejandro Aguilar Sierra
7 * \author André Pönitz
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
14 #include "InsetMathDelim.h"
16 #include "MathData.h"
17 #include "MathStream.h"
18 #include "MathSupport.h"
19 #include "MetricsInfo.h"
21 #include "support/docstring.h"
23 #include "frontends/FontMetrics.h"
25 using namespace std;
27 namespace lyx {
29 static docstring convertDelimToLatexName(docstring const & name)
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;
37 return '\\' + name + ' ';
41 InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l,
42 docstring const & r)
43 : InsetMathNest(buf, 1), left_(l), right_(r)
47 InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l, docstring const & r,
48 MathData const & ar)
49 : InsetMathNest(buf, 1), left_(l), right_(r)
51 cell(0) = ar;
55 Inset * InsetMathDelim::clone() const
57 return new InsetMathDelim(*this);
61 void InsetMathDelim::write(WriteStream & os) const
63 MathEnsurer ensurer(os);
64 os << "\\left" << convertDelimToLatexName(left_) << cell(0)
65 << "\\right" << convertDelimToLatexName(right_);
69 void InsetMathDelim::normalize(NormalStream & os) const
71 os << "[delim " << convertDelimToLatexName(left_) << ' '
72 << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
76 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
78 Dimension dim0;
79 cell(0).metrics(mi, dim0);
80 Dimension t = theFontMetrics(mi.base.font).dimension('I');
81 int h0 = (t.asc + t.des) / 2;
82 int a0 = max(dim0.asc, t.asc) - h0;
83 int d0 = max(dim0.des, t.des) + h0;
84 dw_ = dim0.height() / 5;
85 if (dw_ > 8)
86 dw_ = 8;
87 if (dw_ < 4)
88 dw_ = 4;
89 dim.wid = dim0.width() + 2 * dw_ + 8;
90 dim.asc = max(a0, d0) + h0;
91 dim.des = max(a0, d0) - h0;
95 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
97 Dimension const dim = dimension(*pi.base.bv);
98 int const b = y - dim.asc;
99 cell(0).draw(pi, x + dw_ + 4, y);
100 mathed_draw_deco(pi, x + 4, b, dw_, dim.height(), left_);
101 mathed_draw_deco(pi, x + dim.width() - dw_ - 4,
102 b, dw_, dim.height(), right_);
103 setPosCache(pi, x, y);
107 bool InsetMathDelim::isParenthesis() const
109 return left_ == "(" && right_ == ")";
113 bool InsetMathDelim::isBrackets() const
115 return left_ == "[" && right_ == "]";
119 bool InsetMathDelim::isAbs() const
121 return left_ == "|" && right_ == "|";
125 void InsetMathDelim::maple(MapleStream & os) const
127 if (isAbs()) {
128 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
129 os << "linalg[det](" << cell(0) << ')';
130 else
131 os << "abs(" << cell(0) << ')';
133 else
134 os << left_ << cell(0) << right_;
138 void InsetMathDelim::maxima(MaximaStream & os) const
140 if (isAbs()) {
141 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
142 os << "determinant(" << cell(0) << ')';
143 else
144 os << "abs(" << cell(0) << ')';
146 else
147 os << left_ << cell(0) << right_;
151 void InsetMathDelim::mathematica(MathematicaStream & os) const
153 if (isAbs()) {
154 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
155 os << "Det" << cell(0) << ']';
156 else
157 os << "Abs[" << cell(0) << ']';
159 else
160 os << left_ << cell(0) << right_;
164 void InsetMathDelim::mathmlize(MathStream & os) const
166 os << "<fenced open=\"" << left_ << "\" close=\""
167 << right_ << "\">" << cell(0) << "</fenced>";
171 void InsetMathDelim::octave(OctaveStream & os) const
173 if (isAbs())
174 os << "det(" << cell(0) << ')';
175 else
176 os << left_ << cell(0) << right_;
180 } // namespace lyx