]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathArray.cpp
... and RELEASE-NOTES
[lyx.git] / src / mathed / InsetMathArray.cpp
1 /**
2  * \file InsetMathArray.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathArray.h"
14
15 #include "LaTeXFeatures.h"
16 #include "MathData.h"
17 #include "MathParser.h"
18 #include "MathStream.h"
19 #include "MetricsInfo.h"
20
21 #include "support/lstrings.h"
22
23 #include <iterator>
24 #include <sstream>
25
26 using namespace std;
27
28 namespace lyx {
29
30
31 InsetMathArray::InsetMathArray(Buffer * buf, docstring const & name, int m,
32                 int n)
33         : InsetMathGrid(buf, m, n), name_(name)
34 {}
35
36
37 InsetMathArray::InsetMathArray(Buffer * buf, docstring const & name, int m,
38                 int n, char valign, docstring const & halign)
39         : InsetMathGrid(buf, m, n, valign, halign), name_(name)
40 {}
41
42
43 InsetMathArray::InsetMathArray(Buffer * buf, docstring const & name,
44                 docstring const & str)
45         : InsetMathGrid(buf, 1, 1), name_(name)
46 {
47         vector< vector<string> > dat;
48         istringstream is(to_utf8(str));
49         string line;
50         while (getline(is, line)) {
51                 istringstream ls(line);
52                 typedef istream_iterator<string> iter;
53                 vector<string> v = vector<string>(iter(ls), iter());
54                 if (!v.empty())
55                         dat.push_back(v);
56         }
57
58         for (row_type row = 1; row < dat.size(); ++row)
59                 addRow(0);
60         for (col_type col = 1; col < dat[0].size(); ++col)
61                 addCol(0);
62         for (row_type row = 0; row < dat.size(); ++row)
63                 for (col_type col = 0; col < dat[0].size(); ++col)
64                         mathed_parse_cell(cell(index(row, col)),
65                                           from_utf8(dat[row][col]), Parse::NORMAL);
66 }
67
68
69 Inset * InsetMathArray::clone() const
70 {
71         return new InsetMathArray(*this);
72 }
73
74
75 void InsetMathArray::metrics(MetricsInfo & mi, Dimension & dim) const
76 {
77         Changer dummy =
78                 mi.base.changeStyle(LM_ST_TEXT, mi.base.style == LM_ST_DISPLAY);
79         InsetMathGrid::metrics(mi, dim);
80         dim.wid += 6;
81 }
82
83
84 Dimension const InsetMathArray::dimension(BufferView const & bv) const
85 {
86         Dimension dim = InsetMathGrid::dimension(bv);
87         dim.wid += 6;
88         return dim;
89 }
90
91
92 void InsetMathArray::draw(PainterInfo & pi, int x, int y) const
93 {
94         setPosCache(pi, x, y);
95         Changer dummy =
96                 pi.base.changeStyle(LM_ST_TEXT, pi.base.style == LM_ST_DISPLAY);
97         InsetMathGrid::drawWithMargin(pi, x, y, 4, 2);
98 }
99
100
101 void InsetMathArray::write(WriteStream & os) const
102 {
103         MathEnsurer ensurer(os);
104
105         if (os.fragile())
106                 os << "\\protect";
107         os << "\\begin{" << name_ << '}';
108         bool open = os.startOuterRow();
109
110         char const v = verticalAlignment();
111         if (v == 't' || v == 'b')
112                 os << '[' << v << ']';
113         os << '{' << horizontalAlignments() << "}\n";
114
115         InsetMathGrid::write(os);
116
117         if (os.fragile())
118                 os << "\\protect";
119         os << "\\end{" << name_ << '}';
120         if (open)
121                 os.startOuterRow();
122         // adding a \n here is bad if the array is the last item
123         // in an \eqnarray...
124 }
125
126
127 void InsetMathArray::infoize(odocstream & os) const
128 {
129         docstring name = name_;
130         name[0] = support::uppercase(name[0]);
131         os << name << ' ';
132 }
133
134
135 void InsetMathArray::normalize(NormalStream & os) const
136 {
137         os << '[' << name_ << ' ';
138         InsetMathGrid::normalize(os);
139         os << ']';
140 }
141
142
143 void InsetMathArray::maple(MapleStream & os) const
144 {
145         os << "array(";
146         InsetMathGrid::maple(os);
147         os << ')';
148 }
149
150
151 void InsetMathArray::validate(LaTeXFeatures & features) const
152 {
153         if (name_ == "subarray")
154                 features.require("amsmath");
155         InsetMathGrid::validate(features);
156 }
157
158
159 } // namespace lyx