]> git.lyx.org Git - lyx.git/blob - src/mathed/math_arrayinset.C
Fix math cursor positioning bug
[lyx.git] / src / mathed / math_arrayinset.C
1 /**
2  * \file math_arrayinset.C
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 "math_arrayinset.h"
15 #include "math_data.h"
16 #include "math_parser.h"
17 #include "math_mathmlstream.h"
18 #include "math_streamstr.h"
19
20 #include <iterator>
21 #include <sstream>
22
23 using std::getline;
24
25 using std::string;
26 using std::auto_ptr;
27 using std::istringstream;
28 using std::istream_iterator;
29 using std::vector;
30
31
32 MathArrayInset::MathArrayInset(string const & name, int m, int n)
33         : MathGridInset(m, n), name_(name)
34 {}
35
36
37 MathArrayInset::MathArrayInset(string const & name, int m, int n,
38                 char valign, string const & halign)
39         : MathGridInset(m, n, valign, halign), name_(name)
40 {}
41
42
43 MathArrayInset::MathArrayInset(string const & name, char valign,
44                 string const & halign)
45         : MathGridInset(valign, halign), name_(name)
46 {}
47
48
49 MathArrayInset::MathArrayInset(string const & name, string const & str)
50         : MathGridInset(1, 1), name_(name)
51 {
52         vector< vector<string> > dat;
53         istringstream is(str);
54         string line;
55         while (getline(is, line)) {
56                 istringstream ls(line);
57                 typedef istream_iterator<string> iter;
58                 vector<string> v = vector<string>(iter(ls), iter());
59                 if (v.size())
60                         dat.push_back(v);
61         }
62
63         for (row_type row = 1; row < dat.size(); ++row)
64                 addRow(0);
65         for (col_type col = 1; col < dat[0].size(); ++col)
66                 addCol(0);
67         for (row_type row = 0; row < dat.size(); ++row)
68                 for (col_type col = 0; col < dat[0].size(); ++col)
69                         mathed_parse_cell(cell(index(row, col)), dat[row][col]);
70 }
71
72
73 auto_ptr<InsetBase> MathArrayInset::doClone() const
74 {
75         return auto_ptr<InsetBase>(new MathArrayInset(*this));
76 }
77
78
79 void MathArrayInset::metrics(MetricsInfo & mi, Dimension & dim) const
80 {
81         ArrayChanger dummy(mi.base);
82         MathGridInset::metrics(mi, dim);
83 }
84
85
86 void MathArrayInset::draw(PainterInfo & pi, int x, int y) const
87 {
88         setPosCache(pi, x, y);
89         ArrayChanger dummy(pi.base);
90         MathGridInset::draw(pi, x + 1, y);
91 }
92
93
94 void MathArrayInset::write(WriteStream & os) const
95 {
96         if (os.fragile())
97                 os << "\\protect";
98         os << "\\begin{" << name_ << '}';
99
100         if (v_align_ == 't' || v_align_ == 'b')
101                 os << '[' << char(v_align_) << ']';
102         os << '{' << halign() << "}\n";
103
104         MathGridInset::write(os);
105
106         if (os.fragile())
107                 os << "\\protect";
108         os << "\\end{" << name_ << '}';
109         // adding a \n here is bad if the array is the last item
110         // in an \eqnarray...
111 }
112
113
114 void MathArrayInset::infoize(std::ostream & os) const
115 {
116         os << "Array";
117 }
118
119
120 void MathArrayInset::normalize(NormalStream & os) const
121 {
122         os << '[' << name_ << ' ';
123         MathGridInset::normalize(os);
124         os << ']';
125 }
126
127
128 void MathArrayInset::maple(MapleStream & os) const
129 {
130         os << "array(";
131         MathGridInset::maple(os);
132         os << ')';
133 }
134
135
136 void MathArrayInset::validate(LaTeXFeatures & features) const
137 {
138         if (name_ == "subarray")
139                 features.require("amsmath");
140         MathGridInset::validate(features);
141 }