]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathArray.cpp
* the old cursor is stored before dispatch and then used after moving
[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::auto_ptr;
29 using std::istringstream;
30 using std::istream_iterator;
31 using std::vector;
32 using std::string;
33
34
35 InsetMathArray::InsetMathArray(docstring const & name, int m, int n)
36         : InsetMathGrid(m, n), name_(name)
37 {}
38
39
40 InsetMathArray::InsetMathArray(docstring const & name, int m, int n,
41                 char valign, docstring const & halign)
42         : InsetMathGrid(m, n, valign, halign), name_(name)
43 {}
44
45
46 InsetMathArray::InsetMathArray(docstring const & name, char valign,
47                 docstring const & halign)
48         : InsetMathGrid(valign, halign), name_(name)
49 {}
50
51
52 InsetMathArray::InsetMathArray(docstring const & name, docstring const & str)
53         : InsetMathGrid(1, 1), name_(name)
54 {
55         vector< vector<string> > dat;
56         istringstream is(to_utf8(str));
57         string line;
58         while (getline(is, line)) {
59                 istringstream ls(line);
60                 typedef istream_iterator<string> iter;
61                 vector<string> v = vector<string>(iter(ls), iter());
62                 if (v.size())
63                         dat.push_back(v);
64         }
65
66         for (row_type row = 1; row < dat.size(); ++row)
67                 addRow(0);
68         for (col_type col = 1; col < dat[0].size(); ++col)
69                 addCol(0);
70         for (row_type row = 0; row < dat.size(); ++row)
71                 for (col_type col = 0; col < dat[0].size(); ++col)
72                         mathed_parse_cell(cell(index(row, col)), from_utf8(dat[row][col]));
73 }
74
75
76 auto_ptr<Inset> InsetMathArray::doClone() const
77 {
78         return auto_ptr<Inset>(new InsetMathArray(*this));
79 }
80
81
82 bool InsetMathArray::metrics(MetricsInfo & mi, Dimension & dim) const
83 {
84         ArrayChanger dummy(mi.base);
85         InsetMathGrid::metrics(mi, dim);
86         dim.wid += 6;
87         bool const changed = dim_ != dim;
88         dim_ = dim;
89         return changed;
90 }
91
92
93 void InsetMathArray::draw(PainterInfo & pi, int x, int y) const
94 {
95         setPosCache(pi, x, y);
96         ArrayChanger dummy(pi.base);
97         InsetMathGrid::drawWithMargin(pi, x, y, 4, 2);
98 }
99
100
101 void InsetMathArray::write(WriteStream & os) const
102 {
103         if (os.fragile())
104                 os << "\\protect";
105         os << "\\begin{" << name_ << '}';
106
107         if (v_align_ == 't' || v_align_ == 'b')
108                 os << '[' << char(v_align_) << ']';
109         os << '{' << halign() << "}\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