3 #include "math_matrixinset.h"
4 #include "math_rowst.h"
5 #include "math_xiter.h"
7 extern int number_of_newlines;
9 MathMatrixInset::MathMatrixInset(int m, int n, short st)
10 : MathParInset(st, "array", LM_OT_MATRIX), nc(m), nr(0), ws_(m),
11 v_align(0), h_align(nc, 'c')
16 row = new MathedRowSt(nc+1);
18 for (int j = 1; j < n; ++j) it.addRow();
20 if (nr == 1 && nc > 1) {
21 for (int j = 0; j < nc - 1; ++j)
22 it.Insert('T', LM_TC_TAB);
25 row = new MathedRowSt(nc + 1);
31 MathMatrixInset::MathMatrixInset(MathMatrixInset * mt)
32 : MathParInset(mt->GetStyle(), mt->GetName(), mt->GetType()),
33 nc(mt->nc), nr(0), ws_(mt->nc), v_align(mt->v_align), h_align(mt->h_align)
36 it.SetData(mt->GetData());
39 MathedRowSt * r, * ro= 0, * mrow = mt->row;
40 //mrow = mt->row; // This must be redundant...
42 r = new MathedRowSt(nc + 1);
43 r->setNumbered(mrow->isNumbered());
45 r->setLabel(mrow->getLabel());
50 mrow = mrow->getNext();
60 MathMatrixInset::~MathMatrixInset()
62 MathedRowSt * r = row;
64 MathedRowSt * q = r->getNext();
71 MathedInset * MathMatrixInset::Clone()
73 return new MathMatrixInset(this);
77 void MathMatrixInset::SetAlign(char vv, string const & hh)
80 h_align = hh.substr(0, nc); // usr just h_align = hh; perhaps
84 // Check the number of tabs and crs
85 void MathMatrixInset::SetData(MathedArray * a)
104 it.Insert(' ', LM_TC_TAB);
114 // Automatically inserts tabs around bops
115 // DISABLED because it's very easy to insert tabs
120 void MathMatrixInset::draw(Painter & pain, int x, int baseline)
122 MathParInset::draw(pain, x, baseline);
127 void MathMatrixInset::Metrics()
130 MathedRowSt * cprow= 0;
133 // lyxerr << " MIDA ";
134 MathedXIter it(this);
135 row = it.adjustVerticalSt();
139 MathedRowSt * cxrow = row;
141 for (i = 0; i <= nc; ++i) cxrow->setTab(i, 0);
142 cxrow = cxrow->getNext();
146 MathParInset::Metrics();
148 if (nc <= 1 && !row->getNext()) {
150 row->descent(descent);
153 // Vertical positions of each row
156 for (i = 0; i < nc; ++i) {
157 if (cxrow == row || ws_[i] < cxrow->getTab(i))
158 ws_[i] = cxrow->getTab(i);
159 if (cxrow->getNext() == 0 && ws_[i] == 0) ws_[i] = df_width;
162 cxrow->setBaseline((cxrow == row) ?
164 cxrow->ascent() + cprow->descent()
165 + MATH_ROWSEP + cprow->getBaseline());
166 h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;
168 cxrow = cxrow->getNext();
174 // Compute vertical align
176 case 't': ascent = row->getBaseline(); break;
177 case 'b': ascent = h - hl; break;
178 default: ascent = (row->getNext()) ? h / 2: h - hl; break;
180 descent = h - ascent + 2;
182 // Increase ws_[i] for 'R' columns (except the first one)
183 for (i = 1; i < nc; ++i)
184 if (h_align[i] == 'R')
185 ws_[i] += 10*df_width;
186 // Increase ws_[i] for 'C' column
187 if (h_align[0] == 'C')
188 if (ws_[0] < 7*workWidth/8)
189 ws_[0] = 7*workWidth/8;
195 int rg = MATH_COLSEP, ww, lf = 0; //, * w = cxrow->w;
196 for (i = 0; i < nc; ++i) {
198 if (cxrow->getTab(i) <= 0) {
199 cxrow->setTab(i, df_width);
202 switch (h_align[i]) {
207 lf = (ws_[i] - cxrow->getTab(i))/2;
211 lf = ws_[i] - cxrow->getTab(i);
216 else if (!cxrow->getNext())
217 lf = ws_[i] - cxrow->getTab(i);
219 lf = (ws_[i] - cxrow->getTab(i))/2;
222 ww = (isvoid) ? lf : lf + cxrow->getTab(i);
223 cxrow->setTab(i, lf + rg);
224 rg = ws_[i] - ww + MATH_COLSEP;
225 if (cxrow == row) width += ws_[i] + MATH_COLSEP;
227 cxrow->setBaseline(cxrow->getBaseline() - ascent);
228 cxrow = cxrow->getNext();
233 void MathMatrixInset::Write(ostream & os, bool fragile)
235 if (GetType() == LM_OT_MATRIX){
241 if (v_align == 't' || v_align == 'b') {
249 ++number_of_newlines;
251 MathParInset::Write(os, fragile);
252 if (GetType() == LM_OT_MATRIX){
259 ++number_of_newlines;