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