]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathArray.cpp
Change the interface to a paragraph's layout. We still store a LayoutPtr, but now...
[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, char valign,
43                 docstring const & halign)
44         : InsetMathGrid(valign, halign), name_(name)
45 {}
46
47
48 InsetMathArray::InsetMathArray(docstring const & name, docstring const & str)
49         : InsetMathGrid(1, 1), name_(name)
50 {
51         vector< vector<string> > dat;
52         istringstream is(to_utf8(str));
53         string line;
54         while (getline(is, line)) {
55                 istringstream ls(line);
56                 typedef istream_iterator<string> iter;
57                 vector<string> v = vector<string>(iter(ls), iter());
58                 if (v.size())
59                         dat.push_back(v);
60         }
61
62         for (row_type row = 1; row < dat.size(); ++row)
63                 addRow(0);
64         for (col_type col = 1; col < dat[0].size(); ++col)
65                 addCol(0);
66         for (row_type row = 0; row < dat.size(); ++row)
67                 for (col_type col = 0; col < dat[0].size(); ++col)
68                         mathed_parse_cell(cell(index(row, col)), from_utf8(dat[row][col]));
69 }
70
71
72 Inset * InsetMathArray::clone() const
73 {
74         return new InsetMathArray(*this);
75 }
76
77
78 void InsetMathArray::metrics(MetricsInfo & mi, Dimension & dim) const
79 {
80         ArrayChanger dummy(mi.base);
81         InsetMathGrid::metrics(mi, dim);
82         dim.wid += 6;
83 }
84
85
86 Dimension const InsetMathArray::dimension(BufferView const & bv) const
87 {
88         Dimension dim = InsetMathGrid::dimension(bv);
89         dim.wid += 6;
90         return dim;
91 }
92
93
94 void InsetMathArray::draw(PainterInfo & pi, int x, int y) const
95 {
96         setPosCache(pi, x, y);
97         ArrayChanger dummy(pi.base);
98         InsetMathGrid::drawWithMargin(pi, x, y, 4, 2);
99 }
100
101
102 void InsetMathArray::write(WriteStream & os) const
103 {
104         if (os.fragile())
105                 os << "\\protect";
106         os << "\\begin{" << name_ << '}';
107
108         if (v_align_ == 't' || v_align_ == 'b')
109                 os << '[' << char(v_align_) << ']';
110         os << '{' << halign() << "}\n";
111
112         InsetMathGrid::write(os);
113
114         if (os.fragile())
115                 os << "\\protect";
116         os << "\\end{" << name_ << '}';
117         // adding a \n here is bad if the array is the last item
118         // in an \eqnarray...
119 }
120
121
122 void InsetMathArray::infoize(odocstream & os) const
123 {
124         docstring name = name_;
125         name[0] = support::uppercase(name[0]);
126         os << name << ' ';
127 }
128
129
130 void InsetMathArray::normalize(NormalStream & os) const
131 {
132         os << '[' << name_ << ' ';
133         InsetMathGrid::normalize(os);
134         os << ']';
135 }
136
137
138 void InsetMathArray::maple(MapleStream & os) const
139 {
140         os << "array(";
141         InsetMathGrid::maple(os);
142         os << ')';
143 }
144
145
146 void InsetMathArray::validate(LaTeXFeatures & features) const
147 {
148         if (name_ == "subarray")
149                 features.require("amsmath");
150         InsetMathGrid::validate(features);
151 }
152
153
154 } // namespace lyx