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 MathMatrixInset::MathMatrixInset(MathMatrixInset const & mt)
39 : MathParInset(mt.GetStyle(), mt.GetName(), mt.GetType()),
40 nc_(mt.nc_), nr_(0), ws_(mt.nc_),
41 v_align_(mt.v_align_), h_align_(mt.h_align_)
44 if (!mt.row_.empty()) {
46 MathedRowSt * mrow = mt.row_.data_;
49 MathedRowSt * r = new MathedRowSt(nc_ + 1);
50 r->setNumbered(mrow->isNumbered());
52 r->setLabel(mrow->getLabel());
67 MathMatrixInset::~MathMatrixInset()
69 MathedRowSt * r = row_.data_;
71 MathedRowSt * q = r->next_;
79 MathedInset * MathMatrixInset::Clone()
81 return new MathMatrixInset(*this);
85 void MathMatrixInset::SetAlign(char vv, string const & hh)
88 h_align_ = hh.substr(0, nc_); // usr just h_align = hh; perhaps
92 // Check the number of tabs and crs
93 void MathMatrixInset::setData(MathedArray const & a)
97 MathedIter it(&array);
100 // count tabs per row
113 it.insert(' ', LM_TC_TAB);
123 // Automatically inserts tabs around bops
124 // DISABLED because it's very easy to insert tabs
128 void MathMatrixInset::draw(Painter & pain, int x, int baseline)
130 MathParInset::draw(pain, x, baseline);
135 void MathMatrixInset::Metrics()
138 // lyxerr << " MIDA ";
139 MathedXIter it(this);
140 row_.data_ = it.adjustVerticalSt();
144 for (MathedRowContainer::iterator it = row_.begin(); it; ++it)
145 for (int i = 0; i <= nc_; ++i)
149 MathParInset::Metrics();
151 MathedRowContainer::iterator cxrow = row_.begin();
152 if (nc_ <= 1 && cxrow.is_last()) {
153 cxrow->ascent(ascent);
154 cxrow->descent(descent);
157 // Vertical positions of each row
158 MathedRowContainer::iterator cprow = cxrow;
160 for ( ; cxrow; ++cxrow) {
161 for (int i = 0; i < nc_; ++i) {
162 if (cxrow == row_.begin() || ws_[i] < cxrow->getTab(i))
163 ws_[i] = cxrow->getTab(i);
164 if (cxrow.is_last() && ws_[i] == 0)
168 cxrow->setBaseline((cxrow == row_.begin()) ?
170 cxrow->ascent() + cprow->descent()
171 + MATH_ROWSEP + cprow->getBaseline());
172 h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;
176 int const hl = Descent();
179 // Compute vertical align
182 ascent = row_.begin()->getBaseline();
188 ascent = (row_.begin().is_last()) ? 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;
204 for (cxrow = row_.begin(); cxrow; ++cxrow) {
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);
225 if (cxrow == row_.begin())
227 else if (cxrow.is_last())
228 lf = ws_[i] - cxrow->getTab(i);
230 lf = (ws_[i] - cxrow->getTab(i))/2;
233 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
234 cxrow->setTab(i, lf + rg);
235 rg = ws_[i] - ww + MATH_COLSEP;
236 if (cxrow == row_.begin())
237 width += ws_[i] + MATH_COLSEP;
239 cxrow->setBaseline(cxrow->getBaseline() - ascent);
244 void MathMatrixInset::Write(ostream & os, bool fragile)
246 if (GetType() == LM_OT_MATRIX) {
252 if (v_align_ == 't' || v_align_ == 'b') {
260 ++number_of_newlines;
262 MathParInset::Write(os, fragile);
263 if (GetType() == LM_OT_MATRIX){
270 ++number_of_newlines;