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_)
38 MathedIter it(mt->GetData());
42 MathedRowSt * mrow = mt->row_;
45 MathedRowSt * r = new MathedRowSt(nc_ + 1);
46 r->setNumbered(mrow->isNumbered());
48 r->setLabel(mrow->getLabel());
53 mrow = mrow->getNext();
63 MathMatrixInset::~MathMatrixInset()
65 MathedRowSt * r = row_;
67 MathedRowSt * q = r->getNext();
74 MathedInset * MathMatrixInset::Clone()
76 return new MathMatrixInset(this);
80 void MathMatrixInset::SetAlign(char vv, string const & hh)
83 h_align_ = hh.substr(0, nc_); // usr just h_align = hh; perhaps
87 // Check the number of tabs and crs
88 void MathMatrixInset::setData(MathedArray * a)
107 it.insert(' ', LM_TC_TAB);
117 // Automatically inserts tabs around bops
118 // DISABLED because it's very easy to insert tabs
123 void MathMatrixInset::draw(Painter & pain, int x, int baseline)
125 MathParInset::draw(pain, x, baseline);
130 void MathMatrixInset::Metrics()
133 // lyxerr << " MIDA ";
134 MathedXIter it(this);
135 row_ = it.adjustVerticalSt();
139 MathedRowSt * cxrow = row_;
141 for (int i = 0; i <= nc_; ++i)
143 cxrow = cxrow->getNext();
147 MathParInset::Metrics();
149 if (nc_ <= 1 && !row_->getNext()) {
150 row_->ascent(ascent);
151 row_->descent(descent);
154 // Vertical positions of each row
156 MathedRowSt * cprow = 0;
159 for (int i = 0; i < nc_; ++i) {
160 if (cxrow == row_ || ws_[i] < cxrow->getTab(i))
161 ws_[i] = cxrow->getTab(i);
162 if (cxrow->getNext() == 0 && ws_[i] == 0)
166 cxrow->setBaseline((cxrow == row_) ?
168 cxrow->ascent() + cprow->descent()
169 + MATH_ROWSEP + cprow->getBaseline());
170 h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;
172 cxrow = cxrow->getNext();
178 // Compute vertical align
181 ascent = row_->getBaseline();
187 ascent = (row_->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;
205 int rg = MATH_COLSEP;
207 for (int i = 0; i < nc_; ++i) {
209 if (cxrow->getTab(i) <= 0) {
210 cxrow->setTab(i, df_width);
213 switch (h_align_[i]) {
218 lf = (ws_[i] - cxrow->getTab(i))/2;
222 lf = ws_[i] - cxrow->getTab(i);
227 else if (!cxrow->getNext())
228 lf = ws_[i] - cxrow->getTab(i);
230 lf = (ws_[i] - cxrow->getTab(i))/2;
233 int ww = (isvoid) ? lf : lf + cxrow->getTab(i);
234 cxrow->setTab(i, lf + rg);
235 rg = ws_[i] - ww + MATH_COLSEP;
237 width += ws_[i] + MATH_COLSEP;
239 cxrow->setBaseline(cxrow->getBaseline() - ascent);
240 cxrow = cxrow->getNext();
245 void MathMatrixInset::Write(ostream & os, bool fragile)
247 if (GetType() == LM_OT_MATRIX) {
253 if (v_align_ == 't' || v_align_ == 'b') {
261 ++number_of_newlines;
263 MathParInset::Write(os, fragile);
264 if (GetType() == LM_OT_MATRIX){
271 ++number_of_newlines;