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 array = mt->GetData();
41 MathedRowSt * mrow = mt->row_;
44 MathedRowSt * r = new MathedRowSt(nc_ + 1);
45 r->setNumbered(mrow->isNumbered());
47 r->setLabel(mrow->getLabel());
52 mrow = mrow->getNext();
62 MathMatrixInset::~MathMatrixInset()
64 MathedRowSt * r = row_;
66 MathedRowSt * q = r->getNext();
73 MathedInset * MathMatrixInset::Clone()
75 return new MathMatrixInset(this);
79 void MathMatrixInset::SetAlign(char vv, string const & hh)
82 h_align_ = hh.substr(0, nc_); // usr just h_align = hh; perhaps
86 // Check the number of tabs and crs
87 void MathMatrixInset::setData(MathedArray const & a)
91 MathedIter it(&array);
107 it.insert(' ', LM_TC_TAB);
117 // Automatically inserts tabs around bops
118 // DISABLED because it's very easy to insert tabs
122 void MathMatrixInset::draw(Painter & pain, int x, int baseline)
124 MathParInset::draw(pain, x, baseline);
129 void MathMatrixInset::Metrics()
132 // lyxerr << " MIDA ";
133 MathedXIter it(this);
134 row_ = it.adjustVerticalSt();
138 MathedRowSt * cxrow = row_;
140 for (int i = 0; i <= nc_; ++i)
142 cxrow = cxrow->getNext();
146 MathParInset::Metrics();
148 if (nc_ <= 1 && !row_->getNext()) {
149 row_->ascent(ascent);
150 row_->descent(descent);
153 // Vertical positions of each row
155 MathedRowSt * cprow = 0;
158 for (int i = 0; i < nc_; ++i) {
159 if (cxrow == row_ || ws_[i] < cxrow->getTab(i))
160 ws_[i] = cxrow->getTab(i);
161 if (cxrow->getNext() == 0 && ws_[i] == 0)
165 cxrow->setBaseline((cxrow == row_) ?
167 cxrow->ascent() + cprow->descent()
168 + MATH_ROWSEP + cprow->getBaseline());
169 h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;
171 cxrow = cxrow->getNext();
177 // Compute vertical align
180 ascent = row_->getBaseline();
186 ascent = (row_->getNext()) ? h / 2 : h - hl;
189 descent = h - ascent + 2;
191 // Increase ws_[i] for 'R' columns (except the first one)
192 for (int i = 1; i < nc_; ++i)
193 if (h_align_[i] == 'R')
194 ws_[i] += 10 * df_width;
195 // Increase ws_[i] for 'C' column
196 if (h_align_[0] == 'C')
197 if (ws_[0] < 7 * workWidth / 8)
198 ws_[0] = 7 * workWidth / 8;
204 int rg = MATH_COLSEP;
206 for (int i = 0; i < nc_; ++i) {
208 if (cxrow->getTab(i) <= 0) {
209 cxrow->setTab(i, df_width);
212 switch (h_align_[i]) {
217 lf = (ws_[i] - cxrow->getTab(i))/2;
221 lf = ws_[i] - cxrow->getTab(i);
226 else if (!cxrow->getNext())
227 lf = ws_[i] - cxrow->getTab(i);
229 lf = (ws_[i] - cxrow->getTab(i))/2;
232 int ww = (isvoid) ? lf : lf + cxrow->getTab(i);
233 cxrow->setTab(i, lf + rg);
234 rg = ws_[i] - ww + MATH_COLSEP;
236 width += ws_[i] + MATH_COLSEP;
238 cxrow->setBaseline(cxrow->getBaseline() - ascent);
239 cxrow = cxrow->getNext();
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;