]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathArray.cpp
f8cc87e7726f92e6e64a24fbd1b75cf9cd0a8e95
[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(docstring const & name, int m, int n)
32         : InsetMathGrid(m, n), name_(name)
33 {}
34
35
36 InsetMathArray::InsetMathArray(docstring const & name, int m, int n,
37                 char valign, docstring const & halign)
38         : InsetMathGrid(m, n, valign, halign), name_(name)
39 {}
40
41
42 InsetMathArray::InsetMathArray(docstring const & name, docstring const & str)
43         : InsetMathGrid(1, 1), name_(name)
44 {
45         vector< vector<string> > dat;
46         istringstream is(to_utf8(str));
47         string line;
48         while (getline(is, line)) {
49                 istringstream ls(line);
50                 typedef istream_iterator<string> iter;
51                 vector<string> v = vector<string>(iter(ls), iter());
52                 if (v.size())
53                         dat.push_back(v);
54         }
55
56         for (row_type row = 1; row < dat.size(); ++row)
57                 addRow(0);
58         for (col_type col = 1; col < dat[0].size(); ++col)
59                 addCol(0);
60         for (row_type row = 0; row < dat.size(); ++row)
61                 for (col_type col = 0; col < dat[0].size(); ++col)
62                         mathed_parse_cell(cell(index(row, col)),
63                                           from_utf8(dat[row][col]),
64                                           Parse::NORMAL, &buffer());
65 }
66
67
68 Inset * InsetMathArray::clone() const
69 {
70         return new InsetMathArray(*this);
71 }
72
73
74 void InsetMathArray::metrics(MetricsInfo & mi, Dimension & dim) const
75 {
76         ArrayChanger dummy(mi.base);
77         InsetMathGrid::metrics(mi, dim);
78         dim.wid += 6;
79 }
80
81
82 Dimension const InsetMathArray::dimension(BufferView const & bv) const
83 {
84         Dimension dim = InsetMathGrid::dimension(bv);
85         dim.wid += 6;
86         return dim;
87 }
88
89
90 void InsetMathArray::draw(PainterInfo & pi, int x, int y) const
91 {
92         setPosCache(pi, x, y);
93         ArrayChanger dummy(pi.base);
94         InsetMathGrid::drawWithMargin(pi, x, y, 4, 2);
95 }
96
97
98 void InsetMathArray::write(WriteStream & os) const
99 {
100         MathEnsurer ensurer(os);
101
102         if (os.fragile())
103                 os << "\\protect";
104         os << "\\begin{" << name_ << '}';
105
106         char const v = verticalAlignment();
107         if (v == 't' || v == 'b')
108                 os << '[' << v << ']';
109         os << '{' << horizontalAlignments() << "}\n";
110
111         InsetMathGrid::write(os);
112
113         if (os.fragile())
114                 os << "\\protect";
115         os << "\\end{" << name_ << '}';
116         // adding a \n here is bad if the array is the last item
117         // in an \eqnarray...
118 }
119
120
121 void InsetMathArray::infoize(odocstream & os) const
122 {
123         docstring name = name_;
124         name[0] = support::uppercase(name[0]);
125         os << name << ' ';
126 }
127
128
129 void InsetMathArray::normalize(NormalStream & os) const
130 {
131         os << '[' << name_ << ' ';
132         InsetMathGrid::normalize(os);
133         os << ']';
134 }
135
136
137 void InsetMathArray::maple(MapleStream & os) const
138 {
139         os << "array(";
140         InsetMathGrid::maple(os);
141         os << ')';
142 }
143
144
145 void InsetMathArray::validate(LaTeXFeatures & features) const
146 {
147         if (name_ == "subarray")
148                 features.require("amsmath");
149         InsetMathGrid::validate(features);
150 }
151
152
153 } // namespace lyx