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