+/**
+ * \file math_matrixinset.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
#include <config.h>
#include "math_matrixinset.h"
-#include "math_rowst.h"
-#include "math_xiter.h"
-#include "support/LOstream.h"
+#include "math_data.h"
+#include "math_mathmlstream.h"
-using std::ostream;
+using std::auto_ptr;
-extern int number_of_newlines;
-MathMatrixInset::MathMatrixInset(int m, int n, short st)
- : MathParInset(st, "array", LM_OT_MATRIX), nc_(m), nr_(0), ws_(m),
- v_align_(0), h_align_(nc_, 'c'), row_(0)
-{
- flag = 15;
- if (n > 0) {
- row_ = new MathedRowSt(nc_ + 1);
- MathedXIter it(this);
- for (int j = 1; j < n; ++j) it.addRow();
- nr_ = n;
- if (nr_ == 1 && nc_ > 1) {
- for (int j = 0; j < nc_ - 1; ++j)
- it.insert('T', LM_TC_TAB);
- }
- } else if (n < 0) {
- row_ = new MathedRowSt(nc_ + 1);
- nr_ = 1;
- }
-}
+MathMatrixInset::MathMatrixInset(MathGridInset const & p)
+ : MathGridInset(p)
+{}
-MathMatrixInset::MathMatrixInset(MathMatrixInset * mt)
- : MathParInset(mt->GetStyle(), mt->GetName(), mt->GetType()),
- nc_(mt->nc_), nr_(0), ws_(mt->nc_),
- v_align_(mt->v_align_), h_align_(mt->h_align_)
+auto_ptr<InsetBase> MathMatrixInset::doClone() const
{
- array = *(mt->GetData());
- if (mt->row_ != 0) {
- MathedRowSt * ro = 0;
- MathedRowSt * mrow = mt->row_;
-
- while (mrow) {
- MathedRowSt * r = new MathedRowSt(nc_ + 1);
- r->setNumbered(mrow->isNumbered());
- //if (mrow->label)
- r->setLabel(mrow->getLabel());
- if (!ro)
- row_ = r;
- else
- ro->setNext(r);
- mrow = mrow->getNext();
- ro = r;
- ++nr_;
- }
- } else
- row_ = 0;
- flag = mt->flag;
+ return auto_ptr<InsetBase>(new MathMatrixInset(*this));
}
-MathMatrixInset::~MathMatrixInset()
+void MathMatrixInset::write(WriteStream & os) const
{
- MathedRowSt * r = row_;
- while (r) {
- MathedRowSt * q = r->getNext();
- delete r;
- r = q;
- }
+ MathGridInset::write(os);
}
-MathedInset * MathMatrixInset::Clone()
+void MathMatrixInset::normalize(NormalStream & os) const
{
- return new MathMatrixInset(this);
+ MathGridInset::normalize(os);
}
-void MathMatrixInset::SetAlign(char vv, string const & hh)
+void MathMatrixInset::maple(MapleStream & os) const
{
- v_align_ = vv;
- h_align_ = hh.substr(0, nc_); // usr just h_align = hh; perhaps
+ os << "matrix(" << int(nrows()) << ',' << int(ncols()) << ",[";
+ for (idx_type idx = 0; idx < nargs(); ++idx) {
+ if (idx)
+ os << ',';
+ os << cell(idx);
+ }
+ os << "])";
}
-// Check the number of tabs and crs
-void MathMatrixInset::setData(MathedArray * a)
+void MathMatrixInset::maxima(MaximaStream & os) const
{
- if (!a) return;
- MathedIter it(a);
- int nn = nc_ - 1;
- nr_ = 1;
- // count tabs per row
- while (it.OK()) {
- if (it.IsTab()) {
- if (nn < 0) {
- it.Delete();
- continue;
- } else {
- // it.Next();
- --nn;
- }
+ os << "matrix(";
+ for (row_type row = 0; row < nrows(); ++row) {
+ if (row)
+ os << ',';
+ os << '[';
+ for (col_type col = 0; col < ncols(); ++col) {
+ if (col)
+ os << ',';
+ os << cell(index(row, col));
}
- if (it.IsCR()) {
- while (nn > 0) {
- it.insert(' ', LM_TC_TAB);
- --nn;
- }
- nn = nc_ - 1;
- ++nr_;
- }
- it.Next();
+ os << ']';
}
- it.Reset();
-
- // Automatically inserts tabs around bops
- // DISABLED because it's very easy to insert tabs
- array = *a;
-}
-
-
-void MathMatrixInset::draw(Painter & pain, int x, int baseline)
-{
- MathParInset::draw(pain, x, baseline);
+ os << ')';
}
-
-void MathMatrixInset::Metrics()
+void MathMatrixInset::mathmlize(MathMLStream & os) const
{
- if (!row_) {
- // lyxerr << " MIDA ";
- MathedXIter it(this);
- row_ = it.adjustVerticalSt();
- }
-
- // Clean the arrays
- MathedRowSt * cxrow = row_;
- while (cxrow) {
- for (int i = 0; i <= nc_; ++i)
- cxrow->setTab(i, 0);
- cxrow = cxrow->getNext();
- }
-
- // Basic metrics
- MathParInset::Metrics();
-
- if (nc_ <= 1 && !row_->getNext()) {
- row_->ascent(ascent);
- row_->descent(descent);
- }
-
- // Vertical positions of each row
- cxrow = row_;
- MathedRowSt * cprow = 0;
- int h = 0;
- while (cxrow) {
- for (int i = 0; i < nc_; ++i) {
- if (cxrow == row_ || ws_[i] < cxrow->getTab(i))
- ws_[i] = cxrow->getTab(i);
- if (cxrow->getNext() == 0 && ws_[i] == 0)
- ws_[i] = df_width;
- }
-
- cxrow->setBaseline((cxrow == row_) ?
- cxrow->ascent() :
- cxrow->ascent() + cprow->descent()
- + MATH_ROWSEP + cprow->getBaseline());
- h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;
- cprow = cxrow;
- cxrow = cxrow->getNext();
- }
-
- int hl = Descent();
- h -= MATH_ROWSEP;
-
- // Compute vertical align
- switch (v_align_) {
- case 't':
- ascent = row_->getBaseline();
- break;
- case 'b':
- ascent = h - hl;
- break;
- default:
- ascent = (row_->getNext()) ? h / 2 : h - hl;
- break;
- }
- descent = h - ascent + 2;
-
- // Increase ws_[i] for 'R' columns (except the first one)
- for (int i = 1; i < nc_; ++i)
- if (h_align_[i] == 'R')
- ws_[i] += 10 * df_width;
- // Increase ws_[i] for 'C' column
- if (h_align_[0] == 'C')
- if (ws_[0] < 7 * workWidth / 8)
- ws_[0] = 7 * workWidth / 8;
-
- // Adjust local tabs
- cxrow = row_;
- width = MATH_COLSEP;
- while (cxrow) {
- int rg = MATH_COLSEP;
- int lf = 0;
- for (int i = 0; i < nc_; ++i) {
- bool isvoid = false;
- if (cxrow->getTab(i) <= 0) {
- cxrow->setTab(i, df_width);
- isvoid = true;
- }
- switch (h_align_[i]) {
- case 'l':
- lf = 0;
- break;
- case 'c':
- lf = (ws_[i] - cxrow->getTab(i))/2;
- break;
- case 'r':
- case 'R':
- lf = ws_[i] - cxrow->getTab(i);
- break;
- case 'C':
- if (cxrow == row_)
- lf = 0;
- else if (!cxrow->getNext())
- lf = ws_[i] - cxrow->getTab(i);
- else
- lf = (ws_[i] - cxrow->getTab(i))/2;
- break;
- }
- int ww = (isvoid) ? lf : lf + cxrow->getTab(i);
- cxrow->setTab(i, lf + rg);
- rg = ws_[i] - ww + MATH_COLSEP;
- if (cxrow == row_)
- width += ws_[i] + MATH_COLSEP;
- }
- cxrow->setBaseline(cxrow->getBaseline() - ascent);
- cxrow = cxrow->getNext();
- }
+ MathGridInset::mathmlize(os);
}
-void MathMatrixInset::Write(ostream & os, bool fragile)
+void MathMatrixInset::octave(OctaveStream & os) const
{
- if (GetType() == LM_OT_MATRIX) {
- if (fragile)
- os << "\\protect";
- os << "\\begin{"
- << name
- << '}';
- if (v_align_ == 't' || v_align_ == 'b') {
- os << '['
- << char(v_align_)
- << ']';
- }
- os << '{'
- << h_align_
- << "}\n";
- ++number_of_newlines;
- }
- MathParInset::Write(os, fragile);
- if (GetType() == LM_OT_MATRIX){
- os << "\n";
- if (fragile)
- os << "\\protect";
- os << "\\end{"
- << name
- << '}';
- ++number_of_newlines;
+ os << '[';
+ for (row_type row = 0; row < nrows(); ++row) {
+ if (row)
+ os << ';';
+ os << '[';
+ for (col_type col = 0; col < ncols(); ++col)
+ os << cell(index(row, col)) << ' ';
+ os << ']';
}
+ os << ']';
}