7 #include "math_matrixinset.h"
8 #include "math_rowst.h"
9 #include "math_xiter.h"
10 #include "support/LOstream.h"
14 extern int number_of_newlines;
16 MathMatrixInset::MathMatrixInset(int m, int n, short st)
17 : MathParInset(st, "array", LM_OT_MATRIX), nc_(m), nr_(0), ws_(m),
18 v_align_(0), h_align_(nc_, 'c'), row_(0)
22 row_ = new MathedRowSt(nc_ + 1);
24 for (int j = 1; j < n; ++j) it.addRow();
26 if (nr_ == 1 && nc_ > 1) {
27 for (int j = 0; j < nc_ - 1; ++j)
28 it.insert('T', LM_TC_TAB);
31 row_ = new MathedRowSt(nc_ + 1);
37 MathMatrixInset::MathMatrixInset(MathMatrixInset const & mt)
38 : MathParInset(mt.GetStyle(), mt.GetName(), mt.GetType()),
39 nc_(mt.nc_), nr_(0), ws_(mt.nc_),
40 v_align_(mt.v_align_), h_align_(mt.h_align_)
43 if (!mt.row_.empty()) {
45 MathedRowSt * mrow = mt.row_.data_;
48 MathedRowSt * r = new MathedRowSt(nc_ + 1);
49 r->setNumbered(mrow->isNumbered());
51 r->setLabel(mrow->getLabel());
56 mrow = mrow->getNext();
66 MathMatrixInset::~MathMatrixInset()
68 MathedRowSt * r = row_.data_;
70 MathedRowSt * q = r->getNext();
78 MathedInset * MathMatrixInset::Clone()
80 return new MathMatrixInset(*this);
84 void MathMatrixInset::SetAlign(char vv, string const & hh)
87 h_align_ = hh.substr(0, nc_); // usr just h_align = hh; perhaps
91 // Check the number of tabs and crs
92 void MathMatrixInset::setData(MathedArray const & a)
96 MathedIter it(&array);
112 it.insert(' ', LM_TC_TAB);
122 // Automatically inserts tabs around bops
123 // DISABLED because it's very easy to insert tabs
127 void MathMatrixInset::draw(Painter & pain, int x, int baseline)
129 MathParInset::draw(pain, x, baseline);
134 void MathMatrixInset::Metrics()
137 // lyxerr << " MIDA ";
138 MathedXIter it(this);
139 row_ = it.adjustVerticalSt();
143 for (MathedRowContainer::iterator it = row_.begin(); it; ++it)
144 for (int i = 0; i <= nc_; ++i)
148 MathParInset::Metrics();
150 MathedRowContainer::iterator cxrow = row_.begin();
151 if (nc_ <= 1 && cxrow.is_last()) {
152 cxrow->ascent(ascent);
153 cxrow->descent(descent);
156 // Vertical positions of each row
157 MathedRowContainer::iterator cprow = cxrow;
159 for ( ; cxrow; ++cxrow) {
160 for (int i = 0; i < nc_; ++i) {
161 if (cxrow == row_.begin() || ws_[i] < cxrow->getTab(i))
162 ws_[i] = cxrow->getTab(i);
163 if (cxrow.is_last() && ws_[i] == 0)
167 cxrow->setBaseline((cxrow == row_.begin()) ?
169 cxrow->ascent() + cprow->descent()
170 + MATH_ROWSEP + cprow->getBaseline());
171 h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;
175 int const hl = Descent();
178 // Compute vertical align
181 ascent = row_.begin()->getBaseline();
187 ascent = (row_.data_->getNext()) ? h / 2 : h - hl;
190 descent = h - ascent + 2;
192 // Increase ws_[i] for 'R' columns (except the first one)
193 for (int i = 1; i < nc_; ++i)
194 if (h_align_[i] == 'R')
195 ws_[i] += 10 * df_width;
196 // Increase ws_[i] for 'C' column
197 if (h_align_[0] == 'C')
198 if (ws_[0] < 7 * workWidth / 8)
199 ws_[0] = 7 * workWidth / 8;
203 for (cxrow = row_.begin(); cxrow; ++cxrow) {
204 int rg = MATH_COLSEP;
206 for (int i = 0; i < nc_; ++i) {
208 if (cxrow->getTab(i) <= 0) {
209 cxrow->setTab(i, df_width);
212 switch (h_align_[i]) {
217 lf = (ws_[i] - cxrow->getTab(i))/2;
221 lf = ws_[i] - cxrow->getTab(i);
224 if (cxrow == row_.begin())
226 else if (cxrow.is_last())
227 lf = ws_[i] - cxrow->getTab(i);
229 lf = (ws_[i] - cxrow->getTab(i))/2;
232 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
233 cxrow->setTab(i, lf + rg);
234 rg = ws_[i] - ww + MATH_COLSEP;
235 if (cxrow == row_.begin())
236 width += ws_[i] + MATH_COLSEP;
238 cxrow->setBaseline(cxrow->getBaseline() - ascent);
243 void MathMatrixInset::Write(ostream & os, bool fragile)
245 if (GetType() == LM_OT_MATRIX) {
251 if (v_align_ == 't' || v_align_ == 'b') {
259 ++number_of_newlines;
261 MathParInset::Write(os, fragile);
262 if (GetType() == LM_OT_MATRIX){
269 ++number_of_newlines;