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')
22 row_.data_ = new MathedRowSt(nc_ + 1);
24 for (int j = 1; j < n; ++j)
27 if (nr_ == 1 && nc_ > 1) {
28 for (int j = 0; j < nc_ - 1; ++j)
29 it.insert('T', LM_TC_TAB);
32 row_.data_ = new MathedRowSt(nc_ + 1);
38 MathedInset * MathMatrixInset::Clone()
40 return new MathMatrixInset(*this);
44 void MathMatrixInset::SetAlign(char vv, string const & hh)
47 h_align_ = hh.substr(0, nc_); // usr just h_align = hh; perhaps
51 // Check the number of tabs and crs
52 void MathMatrixInset::setData(MathedArray const & a)
56 MathedIter it(&array);
72 it.insert(' ', LM_TC_TAB);
82 // Automatically inserts tabs around bops
83 // DISABLED because it's very easy to insert tabs
87 void MathMatrixInset::draw(Painter & pain, int x, int baseline)
89 MathParInset::draw(pain, x, baseline);
94 void MathMatrixInset::Metrics()
97 #warning This leaks row_.data but goes away soon
98 // lyxerr << " MIDA ";
102 it.crow_.st_ = new MathedRowSt(it.ncols + 1); // this leaks
104 MathedRowSt * mrow = it.crow_.st_;
107 if (it.col >= it.ncols)
108 it.ncols = it.col + 1;
109 MathedRowSt * r = new MathedRowSt(it.ncols + 1); // this leaks
110 it.crow_.st_->next_ = r;
119 for (MathedRowContainer::iterator it = row_.begin(); it; ++it)
120 for (int i = 0; i <= nc_; ++i)
124 MathParInset::Metrics();
126 MathedRowContainer::iterator cxrow = row_.begin();
127 if (nc_ <= 1 && cxrow.is_last()) {
128 cxrow->ascent(ascent);
129 cxrow->descent(descent);
132 // Vertical positions of each row
133 MathedRowContainer::iterator cprow = cxrow;
135 for ( ; cxrow; ++cxrow) {
136 for (int i = 0; i < nc_; ++i) {
137 if (cxrow == row_.begin() || ws_[i] < cxrow->getTab(i))
138 ws_[i] = cxrow->getTab(i);
139 if (cxrow.is_last() && ws_[i] == 0)
143 cxrow->setBaseline((cxrow == row_.begin()) ?
145 cxrow->ascent() + cprow->descent()
146 + MATH_ROWSEP + cprow->getBaseline());
147 h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;
151 int const hl = Descent();
154 // Compute vertical align
157 ascent = row_.begin()->getBaseline();
163 ascent = (row_.begin().is_last()) ? h / 2 : h - hl;
166 descent = h - ascent + 2;
168 // Increase ws_[i] for 'R' columns (except the first one)
169 for (int i = 1; i < nc_; ++i)
170 if (h_align_[i] == 'R')
171 ws_[i] += 10 * df_width;
172 // Increase ws_[i] for 'C' column
173 if (h_align_[0] == 'C')
174 if (ws_[0] < 7 * workWidth / 8)
175 ws_[0] = 7 * workWidth / 8;
179 for (cxrow = row_.begin(); cxrow; ++cxrow) {
180 int rg = MATH_COLSEP;
182 for (int i = 0; i < nc_; ++i) {
184 if (cxrow->getTab(i) <= 0) {
185 cxrow->setTab(i, df_width);
188 switch (h_align_[i]) {
193 lf = (ws_[i] - cxrow->getTab(i))/2;
197 lf = ws_[i] - cxrow->getTab(i);
200 if (cxrow == row_.begin())
202 else if (cxrow.is_last())
203 lf = ws_[i] - cxrow->getTab(i);
205 lf = (ws_[i] - cxrow->getTab(i))/2;
208 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
209 cxrow->setTab(i, lf + rg);
210 rg = ws_[i] - ww + MATH_COLSEP;
211 if (cxrow == row_.begin())
212 width += ws_[i] + MATH_COLSEP;
214 cxrow->setBaseline(cxrow->getBaseline() - ascent);
219 void MathMatrixInset::Write(ostream & os, bool fragile)
221 if (GetType() == LM_OT_MATRIX) {
227 if (v_align_ == 't' || v_align_ == 'b') {
235 ++number_of_newlines;
237 MathParInset::Write(os, fragile);
238 if (GetType() == LM_OT_MATRIX){
245 ++number_of_newlines;