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')
19 row = new MathedRowSt(nc+1);
21 for (int j = 1; j < n; ++j) it.addRow();
23 if (nr == 1 && nc > 1) {
24 for (int j = 0; j < nc - 1; ++j)
25 it.Insert('T', LM_TC_TAB);
28 row = new MathedRowSt(nc + 1);
34 MathMatrixInset::MathMatrixInset(MathMatrixInset * mt)
35 : MathParInset(mt->GetStyle(), mt->GetName(), mt->GetType()),
36 nc(mt->nc), nr(0), ws_(mt->nc), v_align(mt->v_align), h_align(mt->h_align)
39 it.SetData(mt->GetData());
42 MathedRowSt * r, * ro= 0, * mrow = mt->row;
43 //mrow = mt->row; // This must be redundant...
45 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 MathedRowSt * cprow= 0;
136 // lyxerr << " MIDA ";
137 MathedXIter it(this);
138 row = it.adjustVerticalSt();
142 MathedRowSt * cxrow = row;
144 for (i = 0; i <= nc; ++i) cxrow->setTab(i, 0);
145 cxrow = cxrow->getNext();
149 MathParInset::Metrics();
151 if (nc <= 1 && !row->getNext()) {
153 row->descent(descent);
156 // Vertical positions of each row
159 for (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) ws_[i] = df_width;
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
179 case 't': ascent = row->getBaseline(); break;
180 case 'b': ascent = h - hl; break;
181 default: ascent = (row->getNext()) ? h / 2: h - hl; break;
183 descent = h - ascent + 2;
185 // Increase ws_[i] for 'R' columns (except the first one)
186 for (i = 1; i < nc; ++i)
187 if (h_align[i] == 'R')
188 ws_[i] += 10*df_width;
189 // Increase ws_[i] for 'C' column
190 if (h_align[0] == 'C')
191 if (ws_[0] < 7*workWidth/8)
192 ws_[0] = 7*workWidth/8;
198 int rg = MATH_COLSEP, ww, lf = 0; //, * w = cxrow->w;
199 for (i = 0; i < nc; ++i) {
201 if (cxrow->getTab(i) <= 0) {
202 cxrow->setTab(i, df_width);
205 switch (h_align[i]) {
210 lf = (ws_[i] - cxrow->getTab(i))/2;
214 lf = ws_[i] - cxrow->getTab(i);
219 else if (!cxrow->getNext())
220 lf = ws_[i] - cxrow->getTab(i);
222 lf = (ws_[i] - cxrow->getTab(i))/2;
225 ww = (isvoid) ? lf : lf + cxrow->getTab(i);
226 cxrow->setTab(i, lf + rg);
227 rg = ws_[i] - ww + MATH_COLSEP;
228 if (cxrow == row) width += ws_[i] + MATH_COLSEP;
230 cxrow->setBaseline(cxrow->getBaseline() - ascent);
231 cxrow = cxrow->getNext();
236 void MathMatrixInset::Write(ostream & os, bool fragile)
238 if (GetType() == LM_OT_MATRIX){
244 if (v_align == 't' || v_align == 'b') {
252 ++number_of_newlines;
254 MathParInset::Write(os, fragile);
255 if (GetType() == LM_OT_MATRIX){
262 ++number_of_newlines;