]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathArray.cpp
Fix bug 5802 (http://bugzilla.lyx.org/show_bug.cgi?id=5802)
[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)), from_utf8(dat[row][col]));
63 }
64
65
66 Inset * InsetMathArray::clone() const
67 {
68         return new InsetMathArray(*this);
69 }
70
71
72 void InsetMathArray::metrics(MetricsInfo & mi, Dimension & dim) const
73 {
74         ArrayChanger dummy(mi.base);
75         InsetMathGrid::metrics(mi, dim);
76         dim.wid += 6;
77 }
78
79
80 Dimension const InsetMathArray::dimension(BufferView const & bv) const
81 {
82         Dimension dim = InsetMathGrid::dimension(bv);
83         dim.wid += 6;
84         return dim;
85 }
86
87
88 void InsetMathArray::draw(PainterInfo & pi, int x, int y) const
89 {
90         setPosCache(pi, x, y);
91         ArrayChanger dummy(pi.base);
92         InsetMathGrid::drawWithMargin(pi, x, y, 4, 2);
93 }
94
95
96 void InsetMathArray::write(WriteStream & os) const
97 {
98         MathEnsurer ensurer(os);
99
100         if (os.fragile())
101                 os << "\\protect";
102         os << "\\begin{" << name_ << '}';
103
104         char const v = verticalAlignment();
105         if (v == 't' || v == 'b')
106                 os << '[' << v << ']';
107         os << '{' << horizontalAlignments() << "}\n";
108
109         InsetMathGrid::write(os);
110
111         if (os.fragile())
112                 os << "\\protect";
113         os << "\\end{" << name_ << '}';
114         // adding a \n here is bad if the array is the last item
115         // in an \eqnarray...
116 }
117
118
119 void InsetMathArray::infoize(odocstream & os) const
120 {
121         docstring name = name_;
122         name[0] = support::uppercase(name[0]);
123         os << name << ' ';
124 }
125
126
127 void InsetMathArray::normalize(NormalStream & os) const
128 {
129         os << '[' << name_ << ' ';
130         InsetMathGrid::normalize(os);
131         os << ']';
132 }
133
134
135 void InsetMathArray::maple(MapleStream & os) const
136 {
137         os << "array(";
138         InsetMathGrid::maple(os);
139         os << ')';
140 }
141
142
143 void InsetMathArray::validate(LaTeXFeatures & features) const
144 {
145         if (name_ == "subarray")
146                 features.require("amsmath");
147         InsetMathGrid::validate(features);
148 }
149
150
151 } // namespace lyx