2001-02-13 13:28:32 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2001-02-26 12:53:35 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2001-02-13 13:28:32 +00:00
|
|
|
#include "math_matrixinset.h"
|
|
|
|
#include "math_rowst.h"
|
|
|
|
#include "math_xiter.h"
|
2001-02-14 15:00:50 +00:00
|
|
|
#include "support/LOstream.h"
|
|
|
|
|
|
|
|
using std::ostream;
|
2001-02-13 13:28:32 +00:00
|
|
|
|
|
|
|
extern int number_of_newlines;
|
|
|
|
|
|
|
|
MathMatrixInset::MathMatrixInset(int m, int n, short st)
|
2001-02-16 09:25:43 +00:00
|
|
|
: MathParInset(st, "array", LM_OT_MATRIX), nc_(m), nr_(0), ws_(m),
|
2001-03-08 13:38:20 +00:00
|
|
|
v_align_(0), h_align_(nc_, 'c')
|
2001-02-13 13:28:32 +00:00
|
|
|
{
|
2001-02-16 09:25:43 +00:00
|
|
|
flag = 15;
|
|
|
|
if (n > 0) {
|
|
|
|
MathedXIter it(this);
|
2001-03-08 13:38:20 +00:00
|
|
|
for (int j = 1; j < n; ++j)
|
|
|
|
it.addRow();
|
2001-02-16 09:25:43 +00:00
|
|
|
nr_ = n;
|
|
|
|
if (nr_ == 1 && nc_ > 1) {
|
|
|
|
for (int j = 0; j < nc_ - 1; ++j)
|
2001-02-17 18:52:53 +00:00
|
|
|
it.insert('T', LM_TC_TAB);
|
2001-02-16 09:25:43 +00:00
|
|
|
}
|
|
|
|
} else if (n < 0) {
|
2001-03-15 10:10:01 +00:00
|
|
|
MathedXIter it(this);
|
|
|
|
it.addRow();
|
2001-02-16 09:25:43 +00:00
|
|
|
nr_ = 1;
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathedInset * MathMatrixInset::Clone()
|
|
|
|
{
|
2001-02-28 17:21:16 +00:00
|
|
|
return new MathMatrixInset(*this);
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathMatrixInset::SetAlign(char vv, string const & hh)
|
|
|
|
{
|
2001-02-16 09:25:43 +00:00
|
|
|
v_align_ = vv;
|
|
|
|
h_align_ = hh.substr(0, nc_); // usr just h_align = hh; perhaps
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check the number of tabs and crs
|
2001-02-20 16:00:22 +00:00
|
|
|
void MathMatrixInset::setData(MathedArray const & a)
|
2001-02-13 13:28:32 +00:00
|
|
|
{
|
2001-02-20 16:00:22 +00:00
|
|
|
array = a;
|
|
|
|
|
|
|
|
MathedIter it(&array);
|
2001-02-16 09:25:43 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (it.IsCR()) {
|
|
|
|
while (nn > 0) {
|
2001-02-17 18:52:53 +00:00
|
|
|
it.insert(' ', LM_TC_TAB);
|
2001-02-16 09:25:43 +00:00
|
|
|
--nn;
|
|
|
|
}
|
|
|
|
nn = nc_ - 1;
|
|
|
|
++nr_;
|
|
|
|
}
|
|
|
|
it.Next();
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
2001-02-16 09:25:43 +00:00
|
|
|
it.Reset();
|
|
|
|
|
|
|
|
// Automatically inserts tabs around bops
|
|
|
|
// DISABLED because it's very easy to insert tabs
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathMatrixInset::draw(Painter & pain, int x, int baseline)
|
|
|
|
{
|
2001-02-16 09:25:43 +00:00
|
|
|
MathParInset::draw(pain, x, baseline);
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MathMatrixInset::Metrics()
|
|
|
|
{
|
2001-03-15 10:10:01 +00:00
|
|
|
// Adjust row structure
|
|
|
|
MathedXIter it(this);
|
|
|
|
it.GoBegin();
|
|
|
|
int nrows = 1;
|
|
|
|
while (it.OK()) {
|
|
|
|
if (it.IsCR()) {
|
|
|
|
++nrows;
|
|
|
|
if (it.col >= it.ncols)
|
|
|
|
it.ncols = it.col + 1;
|
|
|
|
}
|
|
|
|
it.Next();
|
|
|
|
}
|
|
|
|
row_.data_.resize(nrows);
|
2001-02-16 09:25:43 +00:00
|
|
|
|
|
|
|
// Clean the arrays
|
2001-03-06 17:44:53 +00:00
|
|
|
for (MathedRowContainer::iterator it = row_.begin(); it; ++it)
|
2001-02-16 09:25:43 +00:00
|
|
|
for (int i = 0; i <= nc_; ++i)
|
2001-03-06 17:44:53 +00:00
|
|
|
it->setTab(i, 0);
|
2001-02-13 13:28:32 +00:00
|
|
|
|
2001-02-16 09:25:43 +00:00
|
|
|
// Basic metrics
|
|
|
|
MathParInset::Metrics();
|
2001-03-06 17:44:53 +00:00
|
|
|
|
|
|
|
MathedRowContainer::iterator cxrow = row_.begin();
|
|
|
|
if (nc_ <= 1 && cxrow.is_last()) {
|
|
|
|
cxrow->ascent(ascent);
|
|
|
|
cxrow->descent(descent);
|
2001-02-16 09:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vertical positions of each row
|
2001-03-06 17:44:53 +00:00
|
|
|
MathedRowContainer::iterator cprow = cxrow;
|
2001-02-16 09:25:43 +00:00
|
|
|
int h = 0;
|
2001-03-06 17:44:53 +00:00
|
|
|
for ( ; cxrow; ++cxrow) {
|
2001-02-16 09:25:43 +00:00
|
|
|
for (int i = 0; i < nc_; ++i) {
|
2001-03-06 17:44:53 +00:00
|
|
|
if (cxrow == row_.begin() || ws_[i] < cxrow->getTab(i))
|
2001-02-16 09:25:43 +00:00
|
|
|
ws_[i] = cxrow->getTab(i);
|
2001-03-06 17:44:53 +00:00
|
|
|
if (cxrow.is_last() && ws_[i] == 0)
|
2001-02-16 09:25:43 +00:00
|
|
|
ws_[i] = df_width;
|
|
|
|
}
|
|
|
|
|
2001-03-06 17:44:53 +00:00
|
|
|
cxrow->setBaseline((cxrow == row_.begin()) ?
|
2001-02-16 09:25:43 +00:00
|
|
|
cxrow->ascent() :
|
|
|
|
cxrow->ascent() + cprow->descent()
|
|
|
|
+ MATH_ROWSEP + cprow->getBaseline());
|
|
|
|
h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;
|
|
|
|
cprow = cxrow;
|
|
|
|
}
|
|
|
|
|
2001-02-26 12:53:35 +00:00
|
|
|
int const hl = Descent();
|
2001-02-16 09:25:43 +00:00
|
|
|
h -= MATH_ROWSEP;
|
|
|
|
|
|
|
|
// Compute vertical align
|
|
|
|
switch (v_align_) {
|
|
|
|
case 't':
|
2001-03-06 17:44:53 +00:00
|
|
|
ascent = row_.begin()->getBaseline();
|
2001-02-13 13:28:32 +00:00
|
|
|
break;
|
2001-02-16 09:25:43 +00:00
|
|
|
case 'b':
|
|
|
|
ascent = h - hl;
|
2001-02-13 13:28:32 +00:00
|
|
|
break;
|
2001-02-16 09:25:43 +00:00
|
|
|
default:
|
2001-03-06 18:17:16 +00:00
|
|
|
ascent = (row_.begin().is_last()) ? h / 2 : h - hl;
|
2001-02-13 13:28:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-02-16 09:25:43 +00:00
|
|
|
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
|
|
|
|
width = MATH_COLSEP;
|
2001-03-06 17:44:53 +00:00
|
|
|
for (cxrow = row_.begin(); cxrow; ++cxrow) {
|
2001-02-16 09:25:43 +00:00
|
|
|
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':
|
2001-03-06 17:44:53 +00:00
|
|
|
if (cxrow == row_.begin())
|
2001-02-16 09:25:43 +00:00
|
|
|
lf = 0;
|
2001-03-06 17:44:53 +00:00
|
|
|
else if (cxrow.is_last())
|
2001-02-16 09:25:43 +00:00
|
|
|
lf = ws_[i] - cxrow->getTab(i);
|
|
|
|
else
|
|
|
|
lf = (ws_[i] - cxrow->getTab(i))/2;
|
|
|
|
break;
|
|
|
|
}
|
2001-02-26 12:53:35 +00:00
|
|
|
int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
|
2001-02-16 09:25:43 +00:00
|
|
|
cxrow->setTab(i, lf + rg);
|
|
|
|
rg = ws_[i] - ww + MATH_COLSEP;
|
2001-03-06 17:44:53 +00:00
|
|
|
if (cxrow == row_.begin())
|
2001-02-16 09:25:43 +00:00
|
|
|
width += ws_[i] + MATH_COLSEP;
|
|
|
|
}
|
|
|
|
cxrow->setBaseline(cxrow->getBaseline() - ascent);
|
|
|
|
}
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathMatrixInset::Write(ostream & os, bool fragile)
|
|
|
|
{
|
2001-02-16 09:25:43 +00:00
|
|
|
if (GetType() == LM_OT_MATRIX) {
|
2001-02-13 13:28:32 +00:00
|
|
|
if (fragile)
|
|
|
|
os << "\\protect";
|
|
|
|
os << "\\begin{"
|
|
|
|
<< name
|
|
|
|
<< '}';
|
2001-02-16 09:25:43 +00:00
|
|
|
if (v_align_ == 't' || v_align_ == 'b') {
|
2001-02-13 13:28:32 +00:00
|
|
|
os << '['
|
2001-02-16 09:25:43 +00:00
|
|
|
<< char(v_align_)
|
2001-02-13 13:28:32 +00:00
|
|
|
<< ']';
|
|
|
|
}
|
|
|
|
os << '{'
|
2001-02-16 09:25:43 +00:00
|
|
|
<< h_align_
|
2001-02-13 13:28:32 +00:00
|
|
|
<< "}\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;
|
|
|
|
}
|
|
|
|
}
|