3 #include "math_matrixinset.h"
4 #include "math_rowst.h"
5 #include "math_xiter.h"
6 #include "support/LOstream.h"
10 extern int number_of_newlines;
12 MathMatrixInset::MathMatrixInset(int m, int n, short st)
13 : MathParInset(st, "array", LM_OT_MATRIX), nc_(m), nr_(0), ws_(m),
14 v_align_(0), h_align_(nc_, 'c'), row_(0)
18 row_ = new MathedRowSt(nc_ + 1);
20 for (int j = 1; j < n; ++j) it.addRow();
22 if (nr_ == 1 && nc_ > 1) {
23 for (int j = 0; j < nc_ - 1; ++j)
24 it.insert('T', LM_TC_TAB);
27 row_ = new MathedRowSt(nc_ + 1);
33 MathMatrixInset::MathMatrixInset(MathMatrixInset * mt)
34 : MathParInset(mt->GetStyle(), mt->GetName(), mt->GetType()),
35 nc_(mt->nc_), nr_(0), ws_(mt->nc_),
36 v_align_(mt->v_align_), h_align_(mt->h_align_)
39 it.SetData(mt->GetData());
43 MathedRowSt * mrow = mt->row_;
46 MathedRowSt * r = new MathedRowSt(nc_ + 1);
47 r->setNumbered(mrow->isNumbered());
49 r->setLabel(mrow->getLabel());
54 mrow = mrow->getNext();
64 MathMatrixInset::~MathMatrixInset()
66 MathedRowSt * r = row_;
68 MathedRowSt * q = r->getNext();
75 MathedInset * MathMatrixInset::Clone()
77 return new MathMatrixInset(this);
81 void MathMatrixInset::SetAlign(char vv, string const & hh)
84 h_align_ = hh.substr(0, nc_); // usr just h_align = hh; perhaps
88 // Check the number of tabs and crs
89 void MathMatrixInset::setData(MathedArray * a)
108 it.insert(' ', LM_TC_TAB);
118 // Automatically inserts tabs around bops
119 // DISABLED because it's very easy to insert tabs
124 void MathMatrixInset::draw(Painter & pain, int x, int baseline)
126 MathParInset::draw(pain, x, baseline);
131 void MathMatrixInset::Metrics()
134 // lyxerr << " MIDA ";
135 MathedXIter it(this);
136 row_ = it.adjustVerticalSt();
140 MathedRowSt * cxrow = row_;
142 for (int i = 0; i <= nc_; ++i)
144 cxrow = cxrow->getNext();
148 MathParInset::Metrics();
150 if (nc_ <= 1 && !row_->getNext()) {
151 row_->ascent(ascent);
152 row_->descent(descent);
155 // Vertical positions of each row
157 MathedRowSt * cprow = 0;
160 for (int i = 0; i < nc_; ++i) {
161 if (cxrow == row_ || ws_[i] < cxrow->getTab(i))
162 ws_[i] = cxrow->getTab(i);
163 if (cxrow->getNext() == 0 && ws_[i] == 0)
167 cxrow->setBaseline((cxrow == row_) ?
169 cxrow->ascent() + cprow->descent()
170 + MATH_ROWSEP + cprow->getBaseline());
171 h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;
173 cxrow = cxrow->getNext();
179 // Compute vertical align
182 ascent = row_->getBaseline();
188 ascent = (row_->getNext()) ? h / 2 : h - hl;
191 descent = h - ascent + 2;
193 // Increase ws_[i] for 'R' columns (except the first one)
194 for (int i = 1; i < nc_; ++i)
195 if (h_align_[i] == 'R')
196 ws_[i] += 10 * df_width;
197 // Increase ws_[i] for 'C' column
198 if (h_align_[0] == 'C')
199 if (ws_[0] < 7 * workWidth / 8)
200 ws_[0] = 7 * workWidth / 8;
206 int rg = MATH_COLSEP;
208 for (int i = 0; i < nc_; ++i) {
210 if (cxrow->getTab(i) <= 0) {
211 cxrow->setTab(i, df_width);
214 switch (h_align_[i]) {
219 lf = (ws_[i] - cxrow->getTab(i))/2;
223 lf = ws_[i] - cxrow->getTab(i);
228 else if (!cxrow->getNext())
229 lf = ws_[i] - cxrow->getTab(i);
231 lf = (ws_[i] - cxrow->getTab(i))/2;
234 int ww = (isvoid) ? lf : lf + cxrow->getTab(i);
235 cxrow->setTab(i, lf + rg);
236 rg = ws_[i] - ww + MATH_COLSEP;
238 width += ws_[i] + MATH_COLSEP;
240 cxrow->setBaseline(cxrow->getBaseline() - ascent);
241 cxrow = cxrow->getNext();
246 void MathMatrixInset::Write(ostream & os, bool fragile)
248 if (GetType() == LM_OT_MATRIX) {
254 if (v_align_ == 't' || v_align_ == 'b') {
262 ++number_of_newlines;
264 MathParInset::Write(os, fragile);
265 if (GetType() == LM_OT_MATRIX){
272 ++number_of_newlines;