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_)
45 MathedRowSt * mrow = mt.row_;
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_;
70 MathedRowSt * q = r->getNext();
77 MathedInset * MathMatrixInset::Clone()
79 return new MathMatrixInset(*this);
83 void MathMatrixInset::SetAlign(char vv, string const & hh)
86 h_align_ = hh.substr(0, nc_); // usr just h_align = hh; perhaps
90 // Check the number of tabs and crs
91 void MathMatrixInset::setData(MathedArray const & a)
95 MathedIter it(&array);
111 it.insert(' ', LM_TC_TAB);
121 // Automatically inserts tabs around bops
122 // DISABLED because it's very easy to insert tabs
126 void MathMatrixInset::draw(Painter & pain, int x, int baseline)
128 MathParInset::draw(pain, x, baseline);
133 void MathMatrixInset::Metrics()
136 // lyxerr << " MIDA ";
137 MathedXIter it(this);
138 row_ = it.adjustVerticalSt();
142 MathedRowSt * cxrow = row_;
144 for (int i = 0; i <= nc_; ++i)
146 cxrow = cxrow->getNext();
150 MathParInset::Metrics();
152 if (nc_ <= 1 && !row_->getNext()) {
153 row_->ascent(ascent);
154 row_->descent(descent);
157 // Vertical positions of each row
159 MathedRowSt * cprow = 0;
162 for (int i = 0; i < nc_; ++i) {
163 if (cxrow == row_ || ws_[i] < cxrow->getTab(i))
164 ws_[i] = cxrow->getTab(i);
165 if (cxrow->getNext() == 0 && ws_[i] == 0)
169 cxrow->setBaseline((cxrow == row_) ?
171 cxrow->ascent() + cprow->descent()
172 + MATH_ROWSEP + cprow->getBaseline());
173 h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;
175 cxrow = cxrow->getNext();
178 int const hl = Descent();
181 // Compute vertical align
184 ascent = row_->getBaseline();
190 ascent = (row_->getNext()) ? h / 2 : h - hl;
193 descent = h - ascent + 2;
195 // Increase ws_[i] for 'R' columns (except the first one)
196 for (int i = 1; i < nc_; ++i)
197 if (h_align_[i] == 'R')
198 ws_[i] += 10 * df_width;
199 // Increase ws_[i] for 'C' column
200 if (h_align_[0] == 'C')
201 if (ws_[0] < 7 * workWidth / 8)
202 ws_[0] = 7 * workWidth / 8;
208 int rg = MATH_COLSEP;
210 for (int i = 0; i < nc_; ++i) {
212 if (cxrow->getTab(i) <= 0) {
213 cxrow->setTab(i, df_width);
216 switch (h_align_[i]) {
221 lf = (ws_[i] - cxrow->getTab(i))/2;
225 lf = ws_[i] - cxrow->getTab(i);
230 else if (!cxrow->getNext())
231 lf = ws_[i] - cxrow->getTab(i);
233 lf = (ws_[i] - cxrow->getTab(i))/2;
236 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
237 cxrow->setTab(i, lf + rg);
238 rg = ws_[i] - ww + MATH_COLSEP;
240 width += ws_[i] + MATH_COLSEP;
242 cxrow->setBaseline(cxrow->getBaseline() - ascent);
243 cxrow = cxrow->getNext();
248 void MathMatrixInset::Write(ostream & os, bool fragile)
250 if (GetType() == LM_OT_MATRIX) {
256 if (v_align_ == 't' || v_align_ == 'b') {
264 ++number_of_newlines;
266 MathParInset::Write(os, fragile);
267 if (GetType() == LM_OT_MATRIX){
274 ++number_of_newlines;