]> git.lyx.org Git - lyx.git/blob - src/mathed/math_arrayinset.C
Make Helge happy: no more crash on arrow up/down in math macro
[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 "support/lstrings.h"
21
22 #include <iterator>
23 #include <sstream>
24
25 using std::getline;
26
27 using std::string;
28 using std::auto_ptr;
29 using std::istringstream;
30 using std::istream_iterator;
31 using std::vector;
32
33
34 MathArrayInset::MathArrayInset(string const & name, int m, int n)
35         : MathGridInset(m, n), name_(name)
36 {}
37
38
39 MathArrayInset::MathArrayInset(string const & name, int m, int n,
40                 char valign, string const & halign)
41         : MathGridInset(m, n, valign, halign), name_(name)
42 {}
43
44
45 MathArrayInset::MathArrayInset(string const & name, char valign,
46                 string const & halign)
47         : MathGridInset(valign, halign), name_(name)
48 {}
49
50
51 MathArrayInset::MathArrayInset(string const & name, string const & str)
52         : MathGridInset(1, 1), name_(name)
53 {
54         vector< vector<string> > dat;
55         istringstream is(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)), dat[row][col]);
72 }
73
74
75 auto_ptr<InsetBase> MathArrayInset::doClone() const
76 {
77         return auto_ptr<InsetBase>(new MathArrayInset(*this));
78 }
79
80
81 void MathArrayInset::metrics(MetricsInfo & mi, Dimension & dim) const
82 {
83         ArrayChanger dummy(mi.base);
84         MathGridInset::metrics(mi, dim);
85         dim.wid += 6;   
86         dim_ = dim;
87 }
88
89
90 void MathArrayInset::draw(PainterInfo & pi, int x, int y) const
91 {
92         setPosCache(pi, x, y);
93         ArrayChanger dummy(pi.base);
94         MathGridInset::drawWithMargin(pi, x, y, 4, 2);
95 }
96
97
98 void MathArrayInset::write(WriteStream & os) const
99 {
100         if (os.fragile())
101                 os << "\\protect";
102         os << "\\begin{" << name_ << '}';
103
104         if (v_align_ == 't' || v_align_ == 'b')
105                 os << '[' << char(v_align_) << ']';
106         os << '{' << halign() << "}\n";
107
108         MathGridInset::write(os);
109
110         if (os.fragile())
111                 os << "\\protect";
112         os << "\\end{" << name_ << '}';
113         // adding a \n here is bad if the array is the last item
114         // in an \eqnarray...
115 }
116
117
118 void MathArrayInset::infoize(std::ostream & os) const
119 {
120         string name = name_;
121         name[0] = lyx::support::uppercase(name[0]);
122         os << name << ' ';
123 }
124
125
126 void MathArrayInset::normalize(NormalStream & os) const
127 {
128         os << '[' << name_ << ' ';
129         MathGridInset::normalize(os);
130         os << ']';
131 }
132
133
134 void MathArrayInset::maple(MapleStream & os) const
135 {
136         os << "array(";
137         MathGridInset::maple(os);
138         os << ')';
139 }
140
141
142 void MathArrayInset::validate(LaTeXFeatures & features) const
143 {
144         if (name_ == "subarray")
145                 features.require("amsmath");
146         MathGridInset::validate(features);
147 }