]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathMatrix.cpp
54a68bd558050f7d45598d45f5e5c1c6badaa7c9
[lyx.git] / src / mathed / InsetMathMatrix.cpp
1 /**
2  * \file InsetMathMatrix.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 "InsetMathMatrix.h"
14 #include "MathData.h"
15 #include "MathStream.h"
16
17 #include "support/convert.h"
18
19 using namespace std;
20
21 namespace lyx {
22
23 InsetMathMatrix::InsetMathMatrix(InsetMathGrid const & p,
24                         docstring const & left, docstring const & right)
25         : InsetMathGrid(p), left_(left), right_(right)
26 {}
27
28
29 Inset * InsetMathMatrix::clone() const
30 {
31         return new InsetMathMatrix(*this);
32 }
33
34
35 void InsetMathMatrix::write(TeXMathStream & os) const
36 {
37         InsetMathGrid::write(os);
38 }
39
40
41 void InsetMathMatrix::normalize(NormalStream & os) const
42 {
43         InsetMathGrid::normalize(os);
44 }
45
46
47 void InsetMathMatrix::maple(MapleStream & os) const
48 {
49         os << "matrix(" << int(nrows()) << ',' << int(ncols()) << ",[";
50         for (idx_type idx = 0; idx < nargs(); ++idx) {
51                 if (idx)
52                         os << ',';
53                 os << cell(idx);
54         }
55         os << "])";
56 }
57
58
59 void InsetMathMatrix::maxima(MaximaStream & os) const
60 {
61         os << "matrix(";
62         for (row_type row = 0; row < nrows(); ++row) {
63                 if (row)
64                         os << ',';
65                 os << '[';
66                 for (col_type col = 0; col < ncols(); ++col) {
67                         if (col)
68                                 os << ',';
69                         os << cell(index(row, col));
70                 }
71                 os << ']';
72         }
73         os << ')';
74 }
75
76
77 void InsetMathMatrix::mathematica(MathematicaStream & os) const
78 {
79         os << '{';
80         for (row_type row = 0; row < nrows(); ++row) {
81                 if (row)
82                         os << ',';
83                 os << '{';
84                 for (col_type col = 0; col < ncols(); ++col) {
85                         if (col)
86                                 os << ',';
87                         os << cell(index(row, col));
88                 }
89                 os << '}';
90         }
91         os << '}';
92 }
93
94
95 void InsetMathMatrix::mathmlize(MathMLStream & ms) const
96 {
97         // lspace='3/18em', but fractions are not allowed.
98         ms << MTagInline("mo", "form='prefix' fence='true' stretchy='true' symmetric='true' lspace='0.1666em'")
99            << convertDelimToXMLEscape(left_)
100            << ETagInline("mo")
101            << MTag("mtable");
102         for (row_type row = 0; row < nrows(); ++row) {
103                 ms << MTag("mtr");
104                 for (col_type col = 0; col < ncols(); ++col) {
105                         idx_type const i = index(row, col);
106                         if (cellinfo(i).multi != CELL_PART_OF_MULTICOLUMN) {
107                                 col_type const cellcols = ncellcols(i);
108                                 ostringstream attr;
109                                 if (cellcols > 1)
110                                         attr << "columnspan='" << cellcols << '\'';
111                                 ms << MTag("mtd", attr.str()) << cell(i) << ETag("mtd");
112                         }
113                 }
114                 ms << ETag("mtr");
115         }
116         ms << ETag("mtable")
117            << MTagInline("mo", "form='postfix' fence='true' stretchy='true' symmetric='true' lspace='0.1666em'")
118            << convertDelimToXMLEscape(right_)
119            << ETagInline("mo");
120 }
121
122
123 void InsetMathMatrix::htmlize(HtmlStream & os) const
124 {
125         os << MTag("table", "class='matrix'") << '\n';
126
127         // we do not print the delimiters but instead try to hack them
128         string const rows = convert<string>(nrows());
129         string const lattrib =
130                         "class='ldelim' rowspan='" + rows + "'";
131         string const rattrib =
132                         "class='rdelim' rowspan='" + rows + "'";
133
134         for (row_type row = 0; row < nrows(); ++row) {
135                 os << MTag("tr") << '\n';
136                 if (row == 0)
137                         os << MTag("td", lattrib) << ETag("td") << '\n';
138                 for (col_type col = 0; col < ncols(); ++col) {
139                         idx_type const i = index(row, col);
140                         if (cellinfo(i).multi != CELL_PART_OF_MULTICOLUMN) {
141                                 col_type const cellcols = ncellcols(i);
142                                 ostringstream attr;
143                                 if (cellcols > 1)
144                                         attr << "colspan='" << cellcols
145                                              << '\'';
146                                 os << MTag("td", attr.str()) << cell(i)
147                                    << ETag("td") << '\n';
148                         }
149                 }
150                 if (row == 0)
151                         os << MTag("td", rattrib) << ETag("td") << '\n';
152                 os << ETag("tr") << '\n';
153         }
154         os << ETag("table") << '\n';
155 }
156
157
158 void InsetMathMatrix::octave(OctaveStream & os) const
159 {
160         os << '[';
161         for (row_type row = 0; row < nrows(); ++row) {
162                 if (row)
163                         os << ';';
164                 os << '[';
165                 for (col_type col = 0; col < ncols(); ++col)
166                         os << cell(index(row, col)) << ' ';
167                 os << ']';
168         }
169         os << ']';
170 }
171
172
173 } // namespace lyx