new hierarchy for frac-like things

'native' \stackrel support
remove MathInset::name_ member (sizeof(MathInset) == 20 on IA32 now);


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2457 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2001-08-08 17:26:30 +00:00
parent 252b1e560c
commit cbf29c8fdc
30 changed files with 310 additions and 197 deletions

View File

@ -1,4 +1,13 @@
2001-08-08 André Pönitz <poenitz@gmx.net>
* math_fracbase.[Ch]:
* math_fracinset.[Ch]: new hierarchy
* math_stackrelbase.[Ch]: native \stackrel support
* math_inset.[Ch]: removal of the name_ member
2001-08-06 André Pönitz <poenitz@gmx.net>
* formulamacro.C: fix nasty bug due to missing copy constructor

View File

@ -39,6 +39,8 @@ libmathed_la_SOURCES = \
math_dotsinset.h \
math_fracinset.C \
math_fracinset.h \
math_fracbase.C \
math_fracbase.h \
math_funcinset.C \
math_funcinset.h \
math_funcliminset.C \
@ -72,6 +74,8 @@ libmathed_la_SOURCES = \
math_spaceinset.h \
math_sqrtinset.C \
math_sqrtinset.h \
math_stackrelinset.C \
math_stackrelinset.h \
math_symbolinset.C \
math_symbolinset.h \
support.C \

View File

@ -87,24 +87,6 @@ unsigned char MathArray::getChar(int pos) const
}
/*
string MathArray::getString(int & pos) const
{
string s;
if (isInset(pos))
return s;
MathTextCodes const fcode = getCode(pos);
do {
s += getChar(pos);
++pos;
} while (pos < size() && !isInset(pos) && getCode(pos) == fcode);
return s;
}
*/
MathTextCodes MathArray::getCode(int pos) const
{
return pos < size() ? (bf_[pos]->code()) : LM_TC_MIN;

View File

@ -21,7 +21,6 @@
#include "mathed/support.h"
#include "math_defs.h"
#include "LString.h"
class MathInset;
class MathMacro;
@ -104,9 +103,6 @@ public:
MathInset const * nextInset(int pos) const;
///
unsigned char getChar(int pos) const;
/// read subsequent chars of the same kind.
// pos is afterwards one behind the last char belonging to the string
string getString(int & pos) const;
///
MathTextCodes getCode(int pos) const;
///

View File

@ -7,7 +7,7 @@
MathArrayInset::MathArrayInset(int m, int n)
: MathGridInset(m, n, "array")
: MathGridInset(m, n)
{}

View File

@ -47,6 +47,7 @@
#include "math_rootinset.h"
#include "math_spaceinset.h"
#include "math_sqrtinset.h"
#include "math_stackrelinset.h"
#include "support/lstrings.h"
#include "math_scriptinset.h"
#include "math_parser.h"
@ -686,11 +687,11 @@ void MathCursor::interpret(string const & s)
break;
case LM_TK_STACK:
p = new MathFracInset("stackrel");
p = new MathStackrelInset;
break;
case LM_TK_FRAC:
p = new MathFracInset("frac");
p = new MathFracInset;
break;
case LM_TK_SQRT:

View File

@ -0,0 +1,72 @@
#ifdef __GNUG__
#pragma implementation
#endif
#include "math_fracbase.h"
MathFracbaseInset::MathFracbaseInset()
: MathNestInset(2)
{}
bool MathFracbaseInset::idxRight(int &, int &) const
{
return false;
}
bool MathFracbaseInset::idxLeft(int &, int &) const
{
return false;
}
bool MathFracbaseInset::idxUp(int & idx, int &) const
{
if (idx == 0)
return false;
idx = 0;
return true;
}
bool MathFracbaseInset::idxDown(int & idx, int &) const
{
if (idx == 1)
return false;
idx = 1;
return true;
}
bool MathFracbaseInset::idxFirstUp(int & idx, int & pos) const
{
idx = 0;
pos = 0;
return true;
}
bool MathFracbaseInset::idxFirstDown(int & idx, int & pos) const
{
idx = 1;
pos = 0;
return true;
}
bool MathFracbaseInset::idxLastUp(int & idx, int & pos) const
{
idx = 0;
pos = cell(idx).size();
return true;
}
bool MathFracbaseInset::idxLastDown(int & idx, int & pos) const
{
idx = 1;
pos = cell(idx).size();
return true;
}

View File

@ -0,0 +1,33 @@
// -*- C++ -*-
#ifndef MATH_FRACBASE_H
#define MATH_FRACBASE_H
#include "math_nestinset.h"
#ifdef __GNUG__
#pragma interface
#endif
class MathFracbaseInset : public MathNestInset {
public:
///
MathFracbaseInset();
///
bool idxUp(int &, int &) const;
///
bool idxDown(int &, int &) const;
///
bool idxLeft(int &, int &) const;
///
bool idxRight(int &, int &) const;
///
bool idxFirstUp(int & idx, int & pos) const;
///
bool idxFirstDown(int & idx, int & pos) const;
///
bool idxLastUp(int & idx, int & pos) const;
///
bool idxLastDown(int & idx, int & pos) const;
};
#endif

View File

@ -8,8 +8,7 @@
#include "support/LOstream.h"
MathFracInset::MathFracInset(string const & name)
: MathNestInset(2, name)
MathFracInset::MathFracInset()
{}
@ -37,15 +36,13 @@ void MathFracInset::draw(Painter & pain, int x, int y) const
int m = x + width() / 2;
xcell(0).draw(pain, m - xcell(0).width() / 2, y - xcell(0).descent() - 3 - 5);
xcell(1).draw(pain, m - xcell(1).width() / 2, y + xcell(1).ascent() + 3 - 5);
if (name() == "frac")
pain.line(x + 2, y - 5, x + width() - 4, y - 5, LColor::mathline);
pain.line(x + 2, y - 5, x + width() - 4, y - 5, LColor::mathline);
}
void MathFracInset::write(std::ostream & os, bool fragile) const
{
os << '\\' << name() << '{';
os << "\\frac{";
cell(0).write(os, fragile);
os << "}{";
cell(1).write(os, fragile);
@ -55,66 +52,9 @@ void MathFracInset::write(std::ostream & os, bool fragile) const
void MathFracInset::writeNormal(std::ostream & os) const
{
os << '[' << name() << ' ';
os << "[frac ";
cell(0).writeNormal(os);
os << " ";
cell(1).writeNormal(os);
os << "] ";
}
bool MathFracInset::idxRight(int &, int &) const
{
return false;
}
bool MathFracInset::idxLeft(int &, int &) const
{
return false;
}
bool MathFracInset::idxUp(int & idx, int &) const
{
if (idx == 0)
return false;
idx = 0;
return true;
}
bool MathFracInset::idxDown(int & idx, int &) const
{
if (idx == 1)
return false;
idx = 1;
return true;
}
bool MathFracInset::idxFirstUp(int & idx, int & pos) const
{
idx = 0;
pos = 0;
return true;
}
bool MathFracInset::idxFirstDown(int & idx, int & pos) const
{
idx = 1;
pos = 0;
return true;
}
bool MathFracInset::idxLastUp(int & idx, int & pos) const
{
idx = 0;
pos = cell(idx).size();
return true;
}
bool MathFracInset::idxLastDown(int & idx, int & pos) const
{
idx = 1;
pos = cell(idx).size();
return true;
}

View File

@ -2,19 +2,19 @@
#ifndef MATH_FRACINSET_H
#define MATH_FRACINSET_H
#include "math_nestinset.h"
#include "math_fracbase.h"
#ifdef __GNUG__
#pragma interface
#endif
/** Fraction like objects (frac, stackrel, binom)
/** Fraction like objects (frac, binom)
\author Alejandro Aguilar Sierra
*/
class MathFracInset : public MathNestInset {
class MathFracInset : public MathFracbaseInset {
public:
///
explicit MathFracInset(const string & name);
MathFracInset();
///
MathInset * clone() const;
///
@ -25,22 +25,6 @@ public:
void metrics(MathStyles st) const;
///
void draw(Painter &, int x, int y) const;
///
bool idxUp(int &, int &) const;
///
bool idxDown(int &, int &) const;
///
bool idxLeft(int &, int &) const;
///
bool idxRight(int &, int &) const;
///
bool idxFirstUp(int & idx, int & pos) const;
///
bool idxFirstDown(int & idx, int & pos) const;
///
bool idxLastUp(int & idx, int & pos) const;
///
bool idxLastDown(int & idx, int & pos) const;
};
#endif

View File

@ -15,9 +15,8 @@ extern LyXFont WhichFont(short type, int size);
MathFuncInset::MathFuncInset(string const & nm)
{
name_ = nm;
}
: name_(nm)
{}
MathInset * MathFuncInset::clone() const
@ -26,6 +25,18 @@ MathInset * MathFuncInset::clone() const
}
string const & MathFuncInset::name() const
{
return name_;
}
void MathFuncInset::setName(string const & n)
{
name_ = n;
}
void MathFuncInset::write(std::ostream & os, bool /* fragile */) const
{
os << "\\" << name_ << ' ';

View File

@ -26,5 +26,12 @@ public:
void write(std::ostream &, bool fragile) const;
///
void writeNormal(std::ostream &) const;
///
string const & name() const;
///
void setName(string const & n);
private:
///
string name_;
};
#endif

View File

@ -29,8 +29,8 @@ MathGridInset::ColInfo::ColInfo()
{}
MathGridInset::MathGridInset(int m, int n, string const & nm)
: MathNestInset(m * n, nm), rowinfo_(n), colinfo_(m), v_align_('c')
MathGridInset::MathGridInset(int m, int n)
: MathNestInset(m * n), rowinfo_(n), colinfo_(m), v_align_('c')
{
if (m <= 0)
lyxerr << "positve number of columns expected\n";

View File

@ -52,7 +52,7 @@ class MathGridInset : public MathNestInset {
public:
///
MathGridInset(int m, int n, string const & nm);
MathGridInset(int m, int n);
///
void write(std::ostream &, bool fragile) const;
///

View File

@ -26,8 +26,8 @@
int MathInset::workwidth;
MathInset::MathInset(string const & name)
: name_(name), size_(LM_ST_DISPLAY), code_(LM_TC_MIN), xo_(0), yo_(0)
MathInset::MathInset()
: size_(LM_ST_DISPLAY), code_(LM_TC_MIN), xo_(0), yo_(0)
{}
@ -37,18 +37,6 @@ int MathInset::height() const
}
string const & MathInset::name() const
{
return name_;
}
void MathInset::setName(string const & n)
{
name_ = n;
}
MathStyles MathInset::size() const
{
return size_;
@ -248,7 +236,7 @@ void MathInset::userSetSize(MathStyles sz)
void MathInset::writeNormal(std::ostream & os) const
{
os << "[" << name_ << "] ";
os << "[unknown] ";
}

View File

@ -28,7 +28,6 @@
#pragma interface
#endif
#include "LString.h"
#include "symbol_def.h"
#include "xarray.h"
@ -44,7 +43,7 @@ class LaTeXFeatures;
class MathInset {
public:
///
explicit MathInset(string const & nm = string());
MathInset();
/// the virtual base destructor
virtual ~MathInset() {}
@ -70,10 +69,6 @@ public:
///
virtual int height() const;
///
virtual string const & name() const;
///
virtual void setName(string const & n);
///
virtual MathStyles size() const;
/// Where should we go when we press the up cursor key?
@ -205,8 +200,6 @@ public:
virtual void code(MathTextCodes t);
protected:
/// usually the LaTeX name of the thingy
string name_;
/// _sets_ style
void size(MathStyles s) const;
/// the used font size

View File

@ -33,7 +33,7 @@
using std::endl;
MathMacro::MathMacro(MathMacroTemplate const & t)
: MathNestInset(t.numargs(), t.name()), tmplate_(&t)
: MathNestInset(t.numargs()), tmplate_(&t)
{}
@ -48,6 +48,10 @@ MathInset * MathMacro::clone() const
return new MathMacro(*this);
}
string const & MathMacro::name() const
{
return tmplate_->name();
}
void MathMacro::metrics(MathStyles st) const
{
@ -59,7 +63,7 @@ void MathMacro::metrics(MathStyles st) const
ascent_ = expanded_.ascent() + 2;
descent_ = expanded_.descent() + 2;
width_ += mathed_string_width(LM_TC_TEXTRM, size_, name_) + 10;
width_ += mathed_string_width(LM_TC_TEXTRM, size_, name()) + 10;
int lasc;
int ldes;
@ -97,9 +101,9 @@ void MathMacro::draw(Painter & pain, int x, int y) const
if (mathcursor && mathcursor->isInside(this)) {
int h = y - ascent() + 2 + expanded_.ascent();
drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name_);
drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name());
int const w = mathed_string_width(LM_TC_TEXTRM, size(), name_);
int const w = mathed_string_width(LM_TC_TEXTRM, size(), name());
expanded_.draw(pain, x + w + 12, h);
h += expanded_.descent();
@ -132,7 +136,7 @@ void MathMacro::dump(std::ostream & os) const
{
MathMacroTable::dump();
os << "\n macro: '" << this << "'\n";
os << " name: '" << name_ << "'\n";
os << " name: '" << name() << "'\n";
os << " template: '" << tmplate_ << "'\n";
os << " template: '" << *tmplate_ << "'\n";
os << endl;
@ -140,7 +144,7 @@ void MathMacro::dump(std::ostream & os) const
void MathMacro::write(std::ostream & os, bool fragile) const
{
os << '\\' << name_;
os << '\\' << name();
for (int i = 0; i < nargs(); ++i) {
os << '{';
cell(i).write(os, fragile);
@ -153,7 +157,7 @@ void MathMacro::write(std::ostream & os, bool fragile) const
void MathMacro::writeNormal(std::ostream & os) const
{
os << "[macro " << name_ << " ";
os << "[macro " << name() << " ";
for (int i = 0; i < nargs(); ++i) {
cell(i).writeNormal(os);
os << ' ';
@ -188,7 +192,7 @@ bool MathMacro::idxRight(int &, int &) const
void MathMacro::validate(LaTeXFeatures & features) const
{
if (name_ == "binom")
if (name() == "binom")
features.binom = true;
//MathInset::validate(features);
}

View File

@ -66,12 +66,15 @@ public:
void validate(LaTeXFeatures &) const;
private:
///
void operator=(MathMacro const &);
///
string const & name() const;
///
MathMacroTemplate const * const tmplate_;
///
mutable MathXArray expanded_;
///
void operator=(MathMacro const &);
};

View File

@ -102,7 +102,7 @@ void MathMacroTable::builtinMacros()
// binom has two arguments
{
MathFracInset * frac = new MathFracInset("atop");
MathFracInset * frac = new MathFracInset;
frac->cell(0).push_back(new MathMacroArgument(1));
frac->cell(1).push_back(new MathMacroArgument(2));

View File

@ -8,12 +8,12 @@
MathMacroTemplate::MathMacroTemplate()
: MathNestInset(1), numargs_(0)
: MathNestInset(1), numargs_(0), name_()
{}
MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs)
: MathNestInset(1, nm), numargs_(numargs)
: MathNestInset(1), numargs_(numargs), name_(nm)
{}
@ -36,6 +36,12 @@ void MathMacroTemplate::numargs(int numargs)
}
string const & MathMacroTemplate::name() const
{
return name_;
}
void MathMacroTemplate::write(std::ostream & os, bool fragile) const
{
os << "\n\\newcommand{\\" << name_ << "}";

View File

@ -30,12 +30,16 @@ public:
///
void numargs(int);
///
string const & name() const;
///
void draw(Painter &, int x, int y) const;
///
void metrics(MathStyles st) const;
private:
///
int numargs_;
///
string name_;
};
#endif

View File

@ -77,14 +77,14 @@ int firstRelOp(MathArray const & array)
}
MathMatrixInset::MathMatrixInset(MathInsetTypes t)
: MathGridInset(getCols(t), 1, "formula"), objtype_(t), nonum_(1), label_(1)
: MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
{
halign(getAlign(t, ncols()));
}
MathMatrixInset::MathMatrixInset()
: MathGridInset(1, 1, "formula"), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
: MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
{}
MathInset * MathMatrixInset::clone() const

View File

@ -6,11 +6,9 @@
#include "debug.h"
MathNestInset::MathNestInset(int nargs, string const & name)
MathNestInset::MathNestInset(int nargs)
: MathDimInset(), cells_(nargs)
{
name_ = name;
}
{}
int MathNestInset::nargs() const

View File

@ -16,7 +16,7 @@ class LaTeXFeatures;
class MathNestInset : public MathDimInset {
public:
///
explicit MathNestInset(int na = 0, string const & nm = string());
explicit MathNestInset(int ncells);
///
void metrics(MathStyles st) const;

View File

@ -45,6 +45,7 @@
#include "math_sizeinset.h"
#include "math_spaceinset.h"
#include "math_sqrtinset.h"
#include "math_stackrelinset.h"
#include "math_symbolinset.h"
#include "debug.h"
#include "mathed/support.h"
@ -386,14 +387,12 @@ MathInset * lastScriptInset(MathArray & array, bool up, bool down, int limits)
static bool curr_num;
static string curr_label;
void mathed_parse_lines(MathInset * inset, int col,
bool numbered, bool outmost)
void mathed_parse_lines(MathGridInset * p, int col, bool numbered, bool outmost)
{
// save global variables
bool const saved_num = curr_num;
string const saved_label = curr_label;
MathGridInset * p = static_cast<MathGridInset *>(inset);
for (int row = 0; true; ++row) {
// reset global variables
curr_num = numbered;
@ -456,41 +455,41 @@ MathMatrixInset * mathed_parse_normal()
MathInsetTypes typ = latex_mathenv[i].typ;
p = new MathMatrixInset(typ);
MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
switch (typ) {
case LM_OT_SIMPLE: {
curr_num = latex_mathenv[i].numbered;
curr_label.erase();
mathed_parse_into(m->cell(0), 0);
m->numbered(0, curr_num);
m->label(0, curr_label);
mathed_parse_into(p->cell(0), 0);
p->numbered(0, curr_num);
p->label(0, curr_label);
break;
}
case LM_OT_EQUATION: {
curr_num = latex_mathenv[i].numbered;
curr_label.erase();
mathed_parse_into(m->cell(0), FLAG_END);
m->numbered(0, curr_num);
m->label(0, curr_label);
mathed_parse_into(p->cell(0), FLAG_END);
p->numbered(0, curr_num);
p->label(0, curr_label);
break;
}
case LM_OT_EQNARRAY: {
mathed_parse_lines(m, 3, latex_mathenv[i].numbered, true);
mathed_parse_lines(p, 3, latex_mathenv[i].numbered, true);
break;
}
case LM_OT_ALIGN: {
m->halign(lexArg('{'));
mathed_parse_lines(m, 2, latex_mathenv[i].numbered, true);
p->halign(lexArg('{'));
mathed_parse_lines(p, 2, latex_mathenv[i].numbered, true);
break;
}
case LM_OT_ALIGNAT: {
m->halign(lexArg('{'));
mathed_parse_lines(m, 2, latex_mathenv[i].numbered, true);
p->halign(lexArg('{'));
mathed_parse_lines(p, 2, latex_mathenv[i].numbered, true);
break;
}
@ -499,8 +498,6 @@ MathMatrixInset * mathed_parse_normal()
<< "1: unknown math environment: " << typ << "\n";
}
p->setName(latex_mathenv[i].basename);
break;
}
@ -513,15 +510,6 @@ MathMatrixInset * mathed_parse_normal()
}
void handle_frac(MathArray & array, string const & name)
{
MathFracInset * p = new MathFracInset(name);
mathed_parse_into(p->cell(0), FLAG_ITEM);
mathed_parse_into(p->cell(1), FLAG_ITEM);
array.push_back(p);
}
void mathed_parse_into(MathArray & array, unsigned flags)
{
static int plevel = -1;
@ -686,17 +674,23 @@ void mathed_parse_into(MathArray & array, unsigned flags)
array.push_back(new MathDotsInset(yylval.l));
break;
case LM_TK_CHOOSE:
handle_frac(array, "atop");
break;
case LM_TK_STACK:
handle_frac(array, "stackrel");
{
MathStackrelInset * p = new MathStackrelInset;
mathed_parse_into(p->cell(0), FLAG_ITEM);
mathed_parse_into(p->cell(1), FLAG_ITEM);
array.push_back(p);
break;
}
case LM_TK_FRAC:
handle_frac(array, "frac");
{
MathFracInset * p = new MathFracInset;
mathed_parse_into(p->cell(0), FLAG_ITEM);
mathed_parse_into(p->cell(1), FLAG_ITEM);
array.push_back(p);
break;
}
case LM_TK_SQRT:
{

View File

@ -47,8 +47,6 @@ enum MathTokenEnum
///
LM_TK_FRAC,
///
LM_TK_CHOOSE,
///
LM_TK_SQRT,
///
LM_TK_BEGIN,

View File

@ -8,12 +8,10 @@
MathSizeInset::MathSizeInset(MathStyles st)
: MathNestInset(1), style_(st)
{
name_ = verbose();
}
{}
char const * MathSizeInset::verbose() const
char const * MathSizeInset::name() const
{
switch (style_) {
case LM_ST_DISPLAY:

View File

@ -30,7 +30,7 @@ public:
private:
///
char const * verbose() const;
char const * name() const;
///
MathStyles style_;
};

View File

@ -0,0 +1,58 @@
#ifdef __GNUG__
#pragma implementation
#endif
#include "math_stackrelinset.h"
#include "mathed/support.h"
#include "support/LOstream.h"
MathStackrelInset::MathStackrelInset()
{}
MathInset * MathStackrelInset::clone() const
{
return new MathStackrelInset(*this);
}
void MathStackrelInset::metrics(MathStyles st) const
{
size_ = smallerStyleFrac(st);
xcell(0).metrics(size_);
xcell(1).metrics(st);
width_ = std::max(xcell(0).width(), xcell(1).width()) + 4;
ascent_ = xcell(1).ascent() + xcell(0).height() + 4;
descent_ = xcell(1).descent();
}
void MathStackrelInset::draw(Painter & pain, int x, int y) const
{
xo(x);
yo(y);
int m = x + width() / 2;
xcell(0).draw(pain, m - xcell(0).width() / 2, y - xcell(0).height() - 4);
xcell(1).draw(pain, m - xcell(1).width() / 2, y);
}
void MathStackrelInset::write(std::ostream & os, bool fragile) const
{
os << "\\stackrel{";
cell(0).write(os, fragile);
os << "}{";
cell(1).write(os, fragile);
os << '}';
}
void MathStackrelInset::writeNormal(std::ostream & os) const
{
os << "[stackrel ";
cell(0).writeNormal(os);
os << " ";
cell(1).writeNormal(os);
os << "] ";
}

View File

@ -0,0 +1,30 @@
// -*- C++ -*-
#ifndef MATH_STACKRELINSET_H
#define MATH_STACKRELINSET_H
#include "math_fracbase.h"
#ifdef __GNUG__
#pragma interface
#endif
/** Stackrel objects
\author André Pönitz
*/
class MathStackrelInset : public MathFracbaseInset {
public:
///
MathStackrelInset();
///
MathInset * clone() const;
///
void write(std::ostream &, bool fragile) const;
///
void writeNormal(std::ostream &) const;
///
void metrics(MathStyles st) const;
///
void draw(Painter &, int x, int y) const;
};
#endif