cloase a leak; remove a few casts; cosmetics

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2410 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2001-08-03 09:54:48 +00:00
parent fcdb9597e1
commit 18a610d4d6
16 changed files with 310 additions and 209 deletions

View File

@ -32,6 +32,10 @@ General hints for bug reports:
---------------------------------------------------------------------- ----------------------------------------------------------------------
"R. Lahaye" <lahaye@users.sourceforge.net>
- Mathematical \frac with subscripts, for example:
\begin_inset Formula $\frac{\alpha _{1} }{\beta _{2} }$
is wrongly displayed
Dekel: Dekel:
@ -355,3 +359,12 @@ Jean-Marc:
Angus: Angus:
- make math lables editable - make math lables editable
Tuukka:
- An inline equation does not get a number, so putting a label there is not
overly sensible...
Ok. In that case show a message in the bottom similarly as the message
"you cannot type two spaces this way". Eg. "You can insert math labels
only in display mode"

View File

@ -5,6 +5,9 @@
* math_matrixinset.C: fix mis-alignment of eqnarray columns * math_matrixinset.C: fix mis-alignment of eqnarray columns
* formula*.[Ch]: seperation of the "pimpl" MathInset * into
MathMatrixInset * and MathMacroTemplate * to save a few casts
2001-07-25 André Pönitz <poenitz@gmx.net> 2001-07-25 André Pönitz <poenitz@gmx.net>
* formulabase.C: re-enable 'space enlargement' feature * formulabase.C: re-enable 'space enlargement' feature

View File

@ -51,22 +51,28 @@ extern MathCursor * mathcursor;
InsetFormula::InsetFormula() InsetFormula::InsetFormula()
: InsetFormulaBase(new MathMatrixInset) : par_(new MathMatrixInset)
{} {}
InsetFormula::InsetFormula(MathInsetTypes t) InsetFormula::InsetFormula(MathInsetTypes t)
: InsetFormulaBase(new MathMatrixInset(t)) : par_(new MathMatrixInset(t))
{} {}
InsetFormula::InsetFormula(string const & s) InsetFormula::InsetFormula(string const & s)
: InsetFormulaBase(mathed_parse(s)) : par_(mathed_parse_normal(s))
{ {
metrics(); metrics();
} }
InsetFormula::~InsetFormula()
{
delete par_;
}
Inset * InsetFormula::clone(Buffer const &, bool) const Inset * InsetFormula::clone(Buffer const &, bool) const
{ {
return new InsetFormula(*this); return new InsetFormula(*this);
@ -82,14 +88,14 @@ void InsetFormula::write(ostream & os) const
int InsetFormula::latex(ostream & os, bool fragile, bool) const int InsetFormula::latex(ostream & os, bool fragile, bool) const
{ {
par()->write(os, fragile); par_->write(os, fragile);
return 1; return 1;
} }
int InsetFormula::ascii(ostream & os, int) const int InsetFormula::ascii(ostream & os, int) const
{ {
par()->write(os, false); par_->write(os, false);
return 1; return 1;
} }
@ -108,7 +114,7 @@ int InsetFormula::docBook(ostream & os) const
void InsetFormula::read(LyXLex & lex) void InsetFormula::read(LyXLex & lex)
{ {
par(mathed_parse(lex)); par(mathed_parse_normal(lex));
metrics(); metrics();
} }
@ -123,9 +129,9 @@ void InsetFormula::draw(BufferView * bv, LyXFont const &,
Painter & pain = bv->painter(); Painter & pain = bv->painter();
metrics(); metrics();
int w = par()->width(); int w = par_->width();
int h = par()->height(); int h = par_->height();
int a = par()->ascent(); int a = par_->ascent();
pain.fillRectangle(x, y - a, w, h, LColor::mathbg); pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
if (mathcursor && mathcursor->formula() == this) { if (mathcursor && mathcursor->formula() == this) {
@ -133,8 +139,8 @@ void InsetFormula::draw(BufferView * bv, LyXFont const &,
pain.rectangle(x, y - a, w, h, LColor::mathframe); pain.rectangle(x, y - a, w, h, LColor::mathframe);
} }
par()->draw(pain, x, y); par_->draw(pain, x, y);
xx += par()->width(); xx += par_->width();
setCursorVisible(false); setCursorVisible(false);
} }
@ -142,12 +148,13 @@ void InsetFormula::draw(BufferView * bv, LyXFont const &,
void InsetFormula::metrics() const void InsetFormula::metrics() const
{ {
const_cast<MathInset *>(par_)->metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT); const_cast<MathMatrixInset *>(par_)
-> metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
} }
vector<string> const InsetFormula::getLabelList() const vector<string> const InsetFormula::getLabelList() const
{ {
return par()->getLabelList(); return par_->getLabelList();
} }
@ -174,9 +181,9 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
//lyxerr << "toggling all numbers\n"; //lyxerr << "toggling all numbers\n";
if (display()) { if (display()) {
bv->lockedInsetStoreUndo(Undo::INSERT); bv->lockedInsetStoreUndo(Undo::INSERT);
bool old = par()->numberedType(); bool old = par_->numberedType();
for (int row = 0; row < par()->nrows(); ++row) for (int row = 0; row < par_->nrows(); ++row)
par()->numbered(row, !old); par_->numbered(row, !old);
bv->owner()->message(old ? _("No number") : _("Number")); bv->owner()->message(old ? _("No number") : _("Number"));
updateLocal(bv, true); updateLocal(bv, true);
} }
@ -189,9 +196,9 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
if (display()) { if (display()) {
bv->lockedInsetStoreUndo(Undo::INSERT); bv->lockedInsetStoreUndo(Undo::INSERT);
int row = mathcursor->row(); int row = mathcursor->row();
bool old = par()->numbered(row); bool old = par_->numbered(row);
bv->owner()->message(old ? _("No number") : _("Number")); bv->owner()->message(old ? _("No number") : _("Number"));
par()->numbered(row, !old); par_->numbered(row, !old);
updateLocal(bv, true); updateLocal(bv, true);
} }
break; break;
@ -202,7 +209,7 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
bv->lockedInsetStoreUndo(Undo::INSERT); bv->lockedInsetStoreUndo(Undo::INSERT);
int row = mathcursor->row(); int row = mathcursor->row();
string old_label = par()->label(row); string old_label = par_->label(row);
string new_label = arg; string new_label = arg;
if (new_label.empty()) { if (new_label.empty()) {
@ -223,13 +230,13 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
if (!new_label.empty()) { if (!new_label.empty()) {
lyxerr << "setting label to '" << new_label << "'\n"; lyxerr << "setting label to '" << new_label << "'\n";
par()->numbered(row, true); par_->numbered(row, true);
} }
if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label)) if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
bv->redraw(); bv->redraw();
par()->label(row, new_label); par_->label(row, new_label);
updateLocal(bv, true); updateLocal(bv, true);
break; break;
@ -247,7 +254,7 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
int x; int x;
int y; int y;
mathcursor->getPos(x, y); mathcursor->getPos(x, y);
par()->mutate(arg); par_->mutate(arg);
mathcursor->setPos(x, y); mathcursor->setPos(x, y);
mathcursor->normalize(); mathcursor->normalize();
updateLocal(bv, true); updateLocal(bv, true);
@ -259,10 +266,10 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
int x; int x;
int y; int y;
mathcursor->getPos(x, y); mathcursor->getPos(x, y);
if (par()->getType() == LM_OT_SIMPLE) if (par_->getType() == LM_OT_SIMPLE)
par()->mutate(LM_OT_EQUATION); par_->mutate(LM_OT_EQUATION);
else else
par()->mutate(LM_OT_SIMPLE); par_->mutate(LM_OT_SIMPLE);
mathcursor->setPos(x, y); mathcursor->setPos(x, y);
mathcursor->normalize(); mathcursor->normalize();
updateLocal(bv, true); updateLocal(bv, true);
@ -273,7 +280,7 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
{ {
string const clip = bv->getClipboard(); string const clip = bv->getClipboard();
if (!clip.empty()) if (!clip.empty())
par(mathed_parse(clip)); par(mathed_parse_normal(clip));
break; break;
} }
@ -290,33 +297,33 @@ void InsetFormula::handleExtern(const string & arg, BufferView *)
//string outfile = lyx::tempName("maple.out"); //string outfile = lyx::tempName("maple.out");
string outfile = "/tmp/lyx2" + arg + ".out"; string outfile = "/tmp/lyx2" + arg + ".out";
ostringstream os; ostringstream os;
par()->writeNormal(os); par_->writeNormal(os);
string code = os.str().c_str(); string code = os.str().c_str();
string script = "lyx2" + arg + " '" + code + "' " + outfile; string script = "lyx2" + arg + " '" + code + "' " + outfile;
lyxerr << "calling: " << script << endl; lyxerr << "calling: " << script << endl;
Systemcalls cmd(Systemcalls::System, script, 0); Systemcalls cmd(Systemcalls::System, script, 0);
ifstream is(outfile.c_str()); ifstream is(outfile.c_str());
par(mathed_parse(is)); par(mathed_parse_normal(is));
metrics(); metrics();
} }
bool InsetFormula::display() const bool InsetFormula::display() const
{ {
return par()->getType() != LM_OT_SIMPLE; return par_->getType() != LM_OT_SIMPLE;
} }
MathMatrixInset * InsetFormula::par() const MathInset * InsetFormula::par() const
{ {
return static_cast<MathMatrixInset *>(par_); return par_;
} }
void InsetFormula::par(MathInset * p) void InsetFormula::par(MathMatrixInset * p)
{ {
delete par_; delete par_;
par_ = p ? p : new MathMatrixInset; par_ = p ? static_cast<MathMatrixInset *>(p) : new MathMatrixInset;
} }
@ -328,7 +335,7 @@ Inset::Code InsetFormula::lyxCode() const
void InsetFormula::validate(LaTeXFeatures & features) const void InsetFormula::validate(LaTeXFeatures & features) const
{ {
par()->validate(features); par_->validate(features);
} }
bool InsetFormula::insetAllowed(Inset::Code code) const bool InsetFormula::insetAllowed(Inset::Code code) const
@ -339,24 +346,24 @@ bool InsetFormula::insetAllowed(Inset::Code code) const
int InsetFormula::ascent(BufferView *, LyXFont const &) const int InsetFormula::ascent(BufferView *, LyXFont const &) const
{ {
return par()->ascent() + 1; return par_->ascent() + 1;
} }
int InsetFormula::descent(BufferView *, LyXFont const &) const int InsetFormula::descent(BufferView *, LyXFont const &) const
{ {
return par()->descent() + 1; return par_->descent() + 1;
} }
int InsetFormula::width(BufferView *, LyXFont const &) const int InsetFormula::width(BufferView *, LyXFont const &) const
{ {
metrics(); metrics();
return par()->width(); return par_->width();
} }
MathInsetTypes InsetFormula::getType() const MathInsetTypes InsetFormula::getType() const
{ {
return par()->getType();; return par_->getType();;
} }

View File

@ -36,6 +36,8 @@ public:
/// ///
explicit InsetFormula(string const &); explicit InsetFormula(string const &);
/// ///
~InsetFormula();
///
int ascent(BufferView *, LyXFont const &) const; int ascent(BufferView *, LyXFont const &) const;
/// ///
int descent(BufferView *, LyXFont const &) const; int descent(BufferView *, LyXFont const &) const;
@ -75,7 +77,7 @@ public:
/// ///
void handleExtern(string const & arg, BufferView * bv); void handleExtern(string const & arg, BufferView * bv);
/// ///
MathMatrixInset * par() const; MathInset * par() const;
/// ///
bool display() const; bool display() const;
/// ///
@ -84,6 +86,8 @@ public:
MathInsetTypes getType() const; MathInsetTypes getType() const;
private: private:
/// Safe setting of contents /// Safe setting of contents
void par(MathInset *); void par(MathMatrixInset *);
///
MathMatrixInset * par_;
}; };
#endif #endif

View File

@ -50,7 +50,6 @@ using std::vector;
extern char const * latex_special_chars; extern char const * latex_special_chars;
int greek_kb_flag = 0;
extern char const * latex_mathenv[]; extern char const * latex_mathenv[];
MathCursor * mathcursor = 0; MathCursor * mathcursor = 0;
@ -116,8 +115,7 @@ MathArrayInset * matrixpar(int & idx)
InsetFormulaBase::InsetFormulaBase(MathInset * par) InsetFormulaBase::InsetFormulaBase()
: par_(par)
{ {
#ifdef WITH_WARNINGS #ifdef WITH_WARNINGS
#warning This is needed as long the math parser is not re-entrant #warning This is needed as long the math parser is not re-entrant
@ -127,22 +125,12 @@ InsetFormulaBase::InsetFormulaBase(MathInset * par)
} }
InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f)
: UpdatableInset(f), par_(f.par_->clone())
{}
InsetFormulaBase::~InsetFormulaBase()
{
delete par_;
}
void InsetFormulaBase::read(Buffer const *, LyXLex & lex) void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
{ {
read(lex); read(lex);
} }
void InsetFormulaBase::write(Buffer const *, ostream & os) const void InsetFormulaBase::write(Buffer const *, ostream & os) const
{ {
write(os); write(os);
@ -204,6 +192,7 @@ void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
void InsetFormulaBase::edit(BufferView * bv, bool front) void InsetFormulaBase::edit(BufferView * bv, bool front)
{ {
#warning Please have a look if this is right (Jug) #warning Please have a look if this is right (Jug)
#warning Does not look wrong... although I do not know what it is supposed to do (Andre)
edit(bv, front ? 0 : 1, 0, 0); edit(bv, front ? 0 : 1, 0, 0);
} }
@ -225,8 +214,8 @@ void InsetFormulaBase::insetUnlock(BufferView * bv)
void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
{ {
mathcursor->getPos(x, y); mathcursor->getPos(x, y);
x -= par_->xo(); x -= par()->xo();
y -= par_->yo(); y -= par()->yo();
} }
@ -241,8 +230,8 @@ void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
int x; int x;
int y; int y;
mathcursor->getPos(x, y); mathcursor->getPos(x, y);
//x -= par_->xo(); //x -= par()->xo();
y -= par_->yo(); y -= par()->yo();
int asc; int asc;
int desc; int desc;
math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc); math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
@ -260,8 +249,8 @@ void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
int x; int x;
int y; int y;
mathcursor->getPos(x, y); mathcursor->getPos(x, y);
x -= par_->xo(); x -= par()->xo();
y -= par_->yo(); y -= par()->yo();
int asc; int asc;
int desc; int desc;
math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc); math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
@ -299,19 +288,13 @@ void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
} }
void InsetFormulaBase::metrics() const
{
const_cast<MathInset *>(par_)->metrics(LM_ST_TEXT);
}
void InsetFormulaBase::insetButtonRelease(BufferView * bv, void InsetFormulaBase::insetButtonRelease(BufferView * bv,
int x, int y, int /*button*/) int x, int y, int /*button*/)
{ {
if (mathcursor) { if (mathcursor) {
hideInsetCursor(bv); hideInsetCursor(bv);
x += par_->xo(); x += par()->xo();
y += par_->yo(); y += par()->yo();
mathcursor->setPos(x, y); mathcursor->setPos(x, y);
showInsetCursor(bv); showInsetCursor(bv);
if (sel_flag) { if (sel_flag) {
@ -343,14 +326,14 @@ void InsetFormulaBase::insetMotionNotify(BufferView * bv,
if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) { if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
sel_flag = true; sel_flag = true;
hideInsetCursor(bv); hideInsetCursor(bv);
mathcursor->setPos(sel_x + par_->xo(), sel_y + par_->yo()); mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
mathcursor->selStart(); mathcursor->selStart();
showInsetCursor(bv); showInsetCursor(bv);
mathcursor->getPos(sel_x, sel_y); mathcursor->getPos(sel_x, sel_y);
} else if (sel_flag) { } else if (sel_flag) {
hideInsetCursor(bv); hideInsetCursor(bv);
x += par_->xo(); x += par()->xo();
y += par_->yo(); y += par()->yo();
mathcursor->setPos(x, y); mathcursor->setPos(x, y);
showInsetCursor(bv); showInsetCursor(bv);
mathcursor->getPos(x, y); mathcursor->getPos(x, y);
@ -376,6 +359,8 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
//lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
// << " arg: '" << arg << "' cursor: " << mathcursor << "\n"; // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
static int greek_kb_flag = 0;
if (!mathcursor) if (!mathcursor)
return UNDISPATCHED; return UNDISPATCHED;
@ -499,7 +484,7 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
int y1; int y1;
istringstream is(arg.c_str()); istringstream is(arg.c_str());
is >> x >> y; is >> x >> y;
par_->getXY(x1, y1); par()->getXY(x1, y1);
mathcursor->setPos(x1 + x, y1 + y); mathcursor->setPos(x1 + x, y1 + y);
updateLocal(bv, false); updateLocal(bv, false);
} }
@ -926,11 +911,6 @@ LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const
return font; return font;
} }
MathInset * InsetFormulaBase::par() const
{
return par_;
}
void mathDispatchCreation(BufferView * bv, string const & arg, bool display) void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
{ {

View File

@ -32,12 +32,8 @@ class MathInset;
/// ///
class InsetFormulaBase : public UpdatableInset { class InsetFormulaBase : public UpdatableInset {
public: public:
///
InsetFormulaBase(InsetFormulaBase const & p);
/// ///
explicit InsetFormulaBase(MathInset *); InsetFormulaBase();
///
virtual ~InsetFormulaBase();
/// ///
virtual int ascent(BufferView *, LyXFont const &) const = 0; virtual int ascent(BufferView *, LyXFont const &) const = 0;
/// ///
@ -120,14 +116,12 @@ public:
/// ///
virtual std::vector<string> const getLabelList() const; virtual std::vector<string> const getLabelList() const;
/// ///
MathInset * par() const; virtual MathInset * par() const = 0;
/// ///
virtual void metrics() const; virtual void metrics() const = 0;
protected: protected:
/// ///
virtual void updateLocal(BufferView * bv, bool mark_dirty); virtual void updateLocal(BufferView * bv, bool mark_dirty);
///
MathInset * par_;
private: private:
/// unimplemented /// unimplemented
void operator=(const InsetFormulaBase &); void operator=(const InsetFormulaBase &);

View File

@ -43,19 +43,32 @@ using std::ostream;
extern MathCursor * mathcursor; extern MathCursor * mathcursor;
InsetFormulaMacro::InsetFormulaMacro() InsetFormulaMacro::InsetFormulaMacro()
: InsetFormulaBase(new MathMacroTemplate("unknown", 0)) : tmacro_(new MathMacroTemplate("unknown", 0))
{} {}
InsetFormulaMacro::InsetFormulaMacro(string nm, int na) InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
: InsetFormulaBase(new MathMacroTemplate(nm, na)) : tmacro_(new MathMacroTemplate(nm, na))
{ {
MathMacroTable::insertTemplate(tmacro()); MathMacroTable::insertTemplate(tmacro_);
}
InsetFormulaMacro::~InsetFormulaMacro()
{
#ifdef WITH_WARNINGS
#warning Need to unregister from MathMacroTable.
#endif
// Instead of unregister an delete leak this until it gets fixed
//delete tmacro_;
} }
Inset * InsetFormulaMacro::clone(Buffer const &, bool) const Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
{ {
#ifdef WITH_WARNINGS
#warning This should not be needed in reality...
#endif
return new InsetFormulaMacro(*this); return new InsetFormulaMacro(*this);
} }
@ -63,20 +76,20 @@ Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
void InsetFormulaMacro::write(ostream & os) const void InsetFormulaMacro::write(ostream & os) const
{ {
os << "FormulaMacro "; os << "FormulaMacro ";
tmacro()->write(os, false); tmacro().write(os, false);
} }
int InsetFormulaMacro::latex(ostream & os, bool fragile, int InsetFormulaMacro::latex(ostream & os, bool fragile,
bool /*free_spacing*/) const bool /*free_spacing*/) const
{ {
tmacro()->write(os, fragile); tmacro().write(os, fragile);
return 2; return 2;
} }
int InsetFormulaMacro::ascii(ostream & os, int) const int InsetFormulaMacro::ascii(ostream & os, int) const
{ {
tmacro()->write(os, false); tmacro().write(os, false);
return 0; return 0;
} }
@ -96,35 +109,91 @@ int InsetFormulaMacro::docBook(ostream & os) const
void InsetFormulaMacro::read(LyXLex & lex) void InsetFormulaMacro::read(LyXLex & lex)
{ {
// Awful hack... // Awful hack...
delete par_; delete tmacro_;
par_ = mathed_parse(lex); tmacro_ = mathed_parse_macro(lex);
MathMacroTable::insertTemplate(tmacro()); MathMacroTable::insertTemplate(tmacro_);
par_->metrics(LM_ST_TEXT); metrics();
} }
string InsetFormulaMacro::prefix() const string InsetFormulaMacro::prefix() const
{ {
return string(" ") + _("Macro: ") + tmacro()->name() + ": "; return string(" ") + _("Macro: ") + tmacro().name() + ": ";
} }
int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
{ {
return tmacro()->ascent() + 5; return tmacro().ascent() + 5;
} }
int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
{ {
return tmacro()->descent() + 5; return tmacro().descent() + 5;
} }
int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
{ {
tmacro()->metrics(LM_ST_TEXT); metrics();
return 10 + lyxfont::width(prefix(), f) + tmacro()->width(); return 10 + lyxfont::width(prefix(), f) + tmacro().width();
}
UpdatableInset::RESULT
InsetFormulaMacro::localDispatch(BufferView * bv,
kb_action action, string const & arg)
{
RESULT result = DISPATCHED;
switch (action) {
case LFUN_MATH_MACROARG: {
int const i = lyx::atoi(arg);
lyxerr << "inserting macro arg " << i << "\n";
if (i > 0 && i <= tmacro().numargs()) {
mathcursor->insert(new MathMacroArgument(i));
updateLocal(bv, true);
} else {
lyxerr << "not in range 0.." << tmacro().numargs() << "\n";
}
break;
}
default:
result = InsetFormulaBase::localDispatch(bv, action, arg);
}
return result;
}
MathMacroTemplate const & InsetFormulaMacro::tmacro() const
{
return *tmacro_;
}
Inset::Code InsetFormulaMacro::lyxCode() const
{
return Inset::MATHMACRO_CODE;
}
MathInsetTypes InsetFormulaMacro::getType() const
{
return LM_OT_MACRO;
}
MathInset * InsetFormulaMacro::par() const
{
return const_cast<MathMacroTemplate *>(tmacro_);
}
void InsetFormulaMacro::metrics() const
{
par()->metrics(LM_ST_TEXT);
} }
@ -152,51 +221,9 @@ void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
x += width(bv, font); x += width(bv, font);
// formula // formula
float t = tmacro()->width() + 5; float t = tmacro().width() + 5;
x -= t; x -= t;
tmacro()->draw(pain, int(x), baseline); par()->draw(pain, int(x), baseline);
x += t; x += t;
} }
UpdatableInset::RESULT
InsetFormulaMacro::localDispatch(BufferView * bv,
kb_action action, string const & arg)
{
RESULT result = DISPATCHED;
switch (action) {
case LFUN_MATH_MACROARG: {
int const i = lyx::atoi(arg);
lyxerr << "inserting macro arg " << i << "\n";
if (i > 0 && i <= tmacro()->numargs()) {
mathcursor->insert(new MathMacroArgument(i));
updateLocal(bv, true);
} else {
lyxerr << "not in range 0.." << tmacro()->numargs() << "\n";
}
break;
}
default:
result = InsetFormulaBase::localDispatch(bv, action, arg);
}
return result;
}
MathMacroTemplate * InsetFormulaMacro::tmacro() const
{
return static_cast<MathMacroTemplate *>(par_);
}
Inset::Code InsetFormulaMacro::lyxCode() const
{
return Inset::MATHMACRO_CODE;
}
MathInsetTypes InsetFormulaMacro::getType() const
{
return LM_OT_MACRO;
}

View File

@ -37,6 +37,8 @@ public:
/// ///
explicit InsetFormulaMacro(string name, int na); explicit InsetFormulaMacro(string name, int na);
/// ///
~InsetFormulaMacro();
///
int ascent(BufferView *, LyXFont const &) const; int ascent(BufferView *, LyXFont const &) const;
/// ///
int descent(BufferView *, LyXFont const &) const; int descent(BufferView *, LyXFont const &) const;
@ -66,11 +68,17 @@ public:
RESULT localDispatch(BufferView *, kb_action, string const &); RESULT localDispatch(BufferView *, kb_action, string const &);
/// ///
MathInsetTypes getType() const; MathInsetTypes getType() const;
///
MathInset * par() const;
///
void metrics() const;
private: private:
///
MathMacroTemplate const & tmacro() const;
/// prefix in inset /// prefix in inset
string prefix() const; string prefix() const;
/// ///
MathMacroTemplate * tmacro() const; MathMacroTemplate const * tmacro_;
}; };
#endif #endif

View File

@ -588,7 +588,7 @@ bool MathCursor::toggleLimits()
{ {
if (!prevIsInset()) if (!prevIsInset())
return false; return false;
MathInset * p = prevInset(); MathScriptInset * p = prevScriptInset();
int old = p->limits(); int old = p->limits();
p->limits(old < 0 ? 1 : -1); p->limits(old < 0 ? 1 : -1);
return old != p->limits(); return old != p->limits();

View File

@ -28,7 +28,7 @@ int MathInset::workwidth;
MathInset::MathInset(int nargs, string const & name) MathInset::MathInset(int nargs, string const & name)
: name_(name), width_(0), ascent_(0), descent_(0), : name_(name), width_(0), ascent_(0), descent_(0),
size_(LM_ST_DISPLAY), cells_(nargs), xo_(0), yo_(0) size_(LM_ST_DISPLAY), code_(LM_TC_MIN), cells_(nargs), xo_(0), yo_(0)
{} {}
@ -56,16 +56,6 @@ int MathInset::height() const
} }
int MathInset::limits() const
{
return false;
}
void MathInset::limits(int)
{}
string const & MathInset::name() const string const & MathInset::name() const
{ {
return name_; return name_;
@ -89,6 +79,7 @@ void MathInset::size(MathStyles s)
size_ = s; size_ = s;
} }
std::ostream & operator<<(std::ostream & os, MathInset const & inset) std::ostream & operator<<(std::ostream & os, MathInset const & inset)
{ {
inset.write(os, false); inset.write(os, false);
@ -126,24 +117,24 @@ int MathInset::nargs() const
} }
MathXArray & MathInset::xcell(int i) MathXArray & MathInset::xcell(int i)
{ {
return cells_[i]; return cells_[i];
} }
MathXArray const & MathInset::xcell(int i) const MathXArray const & MathInset::xcell(int i) const
{ {
return cells_[i]; return cells_[i];
} }
MathArray & MathInset::cell(int i) MathArray & MathInset::cell(int i)
{ {
return cells_[i].data_; return cells_[i].data_;
} }
MathArray const & MathInset::cell(int i) const MathArray const & MathInset::cell(int i) const
{ {
return cells_[i].data_; return cells_[i].data_;
@ -158,6 +149,7 @@ void MathInset::substitute(MathArray & array, MathMacro const & m) const
array.push_back(p); array.push_back(p);
} }
void MathInset::metrics(MathStyles st) void MathInset::metrics(MathStyles st)
{ {
size_ = st; size_ = st;
@ -165,6 +157,7 @@ void MathInset::metrics(MathStyles st)
xcell(i).metrics(st); xcell(i).metrics(st);
} }
void MathInset::draw(Painter & pain, int x, int y) void MathInset::draw(Painter & pain, int x, int y)
{ {
xo_ = x; xo_ = x;
@ -205,6 +198,7 @@ bool MathInset::idxLeft(int & idx, int & pos) const
return idxPrev(idx, pos); return idxPrev(idx, pos);
} }
bool MathInset::idxUp(int &, int &) const bool MathInset::idxUp(int &, int &) const
{ {
return false; return false;
@ -226,6 +220,7 @@ bool MathInset::idxFirst(int & i, int & pos) const
return true; return true;
} }
bool MathInset::idxLast(int & i, int & pos) const bool MathInset::idxLast(int & i, int & pos) const
{ {
if (nargs() == 0) if (nargs() == 0)
@ -266,12 +261,14 @@ bool MathInset::idxFirstDown(int &, int &) const
return false; return false;
} }
void MathInset::idxDelete(int &, bool & popit, bool & deleteit) void MathInset::idxDelete(int &, bool & popit, bool & deleteit)
{ {
popit = false; popit = false;
deleteit = false; deleteit = false;
} }
void MathInset::idxDeleteRange(int, int) void MathInset::idxDeleteRange(int, int)
{} {}
@ -349,12 +346,14 @@ bool MathInset::covers(int x, int y) const
y <= yo_ + descent_; y <= yo_ + descent_;
} }
void MathInset::validate(LaTeXFeatures & features) const void MathInset::validate(LaTeXFeatures & features) const
{ {
for (int i = 0; i < nargs(); ++i) for (int i = 0; i < nargs(); ++i)
cell(i).validate(features); cell(i).validate(features);
} }
std::vector<int> MathInset::idxBetween(int from, int to) const std::vector<int> MathInset::idxBetween(int from, int to) const
{ {
std::vector<int> res; std::vector<int> res;
@ -362,3 +361,15 @@ std::vector<int> MathInset::idxBetween(int from, int to) const
res.push_back(i); res.push_back(i);
return res; return res;
} }
MathTextCodes MathInset::code() const
{
return code_;
}
void MathInset::code(MathTextCodes t)
{
code_ = t;
}

View File

@ -70,10 +70,6 @@ public:
/// ///
virtual int height() const; virtual int height() const;
/// ///
virtual int limits() const;
///
virtual void limits(int);
///
string const & name() const; string const & name() const;
/// ///
virtual void setName(string const & n); virtual void setName(string const & n);
@ -181,6 +177,7 @@ public:
virtual bool isArray() const { return false; } virtual bool isArray() const { return false; }
/// ///
virtual bool isActive() const { return nargs() > 0; } virtual bool isActive() const { return nargs() > 0; }
/// identifies insets that display scripts directly above and below
/// ///
@ -195,6 +192,12 @@ public:
/// ///
static int workwidth; static int workwidth;
/// the inherited text style
MathTextCodes code() const;
///
void code(MathTextCodes t);
protected: protected:
/// usually the LaTeX name of the thingy /// usually the LaTeX name of the thingy
string name_; string name_;
@ -208,6 +211,8 @@ protected:
void size(MathStyles s); void size(MathStyles s);
/// the used font size /// the used font size
MathStyles size_; MathStyles size_;
/// the inherited text style
MathTextCodes code_;
protected: protected:
/// ///

View File

@ -32,9 +32,9 @@ void MathMacroTable::dump()
} }
void MathMacroTable::insertTemplate(MathMacroTemplate * p) void MathMacroTable::insertTemplate(MathMacroTemplate const * p)
{ {
macro_table[p->name()] = p; macro_table[p->name()] = const_cast<MathMacroTemplate *>(p);
} }

View File

@ -17,9 +17,7 @@ class MathMacroTemplate;
struct MathMacroTable { struct MathMacroTable {
public: public:
/// ///
static void updateTemplate(MathMacroTemplate *); static void insertTemplate(MathMacroTemplate const *);
///
static void insertTemplate(MathMacroTemplate *);
/// ///
static MathMacroTemplate & provideTemplate(string const &); static MathMacroTemplate & provideTemplate(string const &);
/// ///

View File

@ -7,13 +7,13 @@
#include "debug.h" #include "debug.h"
MathMacroTemplate::MathMacroTemplate() : MathMacroTemplate::MathMacroTemplate()
MathInset(1, "undefined"), numargs_(0) : MathInset(1), numargs_(0)
{} {}
MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs) : MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs)
MathInset(1, nm), numargs_(numargs) : MathInset(1, nm), numargs_(numargs)
{} {}

View File

@ -85,7 +85,7 @@ void mathed_parse_into(MathArray & array, unsigned flags);
unsigned char getuchar(std::istream * is) unsigned char getuchar(std::istream * is)
{ {
char c; char c = 0;
is->get(c); is->get(c);
if (!is->good()) { if (!is->good()) {
lyxerr << "The input stream is not well..." << endl; lyxerr << "The input stream is not well..." << endl;
@ -124,7 +124,6 @@ union {
string yytext; string yytext;
int yylineno; int yylineno;
istream * yyis; istream * yyis;
MathTextCodes yyvarcode;
@ -251,10 +250,7 @@ int yylex()
unsigned char c = getuchar(yyis); unsigned char c = getuchar(yyis);
//lyxerr << "reading byte: '" << c << "' code: " << lexcode[c] << endl; //lyxerr << "reading byte: '" << c << "' code: " << lexcode[c] << endl;
if (yyvarcode == LM_TC_TEXTRM && c == ' ') { if (lexcode[c] == LexNewLine) {
yylval.i = ' ';
return LM_TK_ALPHA;
} else if (lexcode[c] == LexNewLine) {
++yylineno; ++yylineno;
continue; continue;
} else if (lexcode[c] == LexComment) { } else if (lexcode[c] == LexComment) {
@ -266,7 +262,7 @@ int yylex()
|| lexcode[c] == LexMathSpace) { || lexcode[c] == LexMathSpace) {
yylval.i = c; yylval.i = c;
return LM_TK_STR; return LM_TK_STR;
} else if (lexcode[c] == LexAlpha) { } else if (lexcode[c] == LexAlpha || lexcode[c] == LexSpace) {
yylval.i = c; yylval.i = c;
return LM_TK_ALPHA; return LM_TK_ALPHA;
} else if (lexcode[c] == LexBOP) { } else if (lexcode[c] == LexBOP) {
@ -328,8 +324,11 @@ int yylex()
yytext += c; yytext += c;
c = getuchar(yyis); c = getuchar(yyis);
} }
if (yyis->good()) while (lexcode[c] == LexSpace && yyis->good())
c = getuchar(yyis);
if (yyis->good() && lexcode[c] != LexSpace)
yyis->putback(c); yyis->putback(c);
//lyxerr[Debug::MATHED] << "reading: text '" << yytext << "'\n"; //lyxerr[Debug::MATHED] << "reading: text '" << yytext << "'\n";
latexkeys const * l = in_word_set(yytext); latexkeys const * l = in_word_set(yytext);
if (!l) if (!l)
@ -425,23 +424,28 @@ void mathed_parse_lines(MathInset * inset, int col,
} }
MathInset * mathed_parse() MathMacroTemplate * mathed_parse_macro()
{ {
MathInset * p = 0; if (yylex() != LM_TK_NEWCOMMAND) {
lyxerr << "\\newcommand expected\n";
return 0;
}
string name = lexArg('{').substr(1);
string arg = lexArg('[');
int narg = arg.empty() ? 0 : atoi(arg.c_str());
MathMacroTemplate * p = new MathMacroTemplate(name, narg);
mathed_parse_into(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
return p;
}
MathMatrixInset * mathed_parse_normal()
{
MathMatrixInset * p = 0;
int t = yylex(); int t = yylex();
switch (t) { switch (t) {
case LM_TK_NEWCOMMAND: {
string const name = lexArg('{').substr(1);
string const arg = lexArg('[');
int narg = arg.empty() ? 0 : atoi(arg.c_str());
p = new MathMacroTemplate(name, narg);
mathed_parse_into(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
//lyxerr[Debug::MATHED] << "LM_TK_NEWCOMMAND: name: "
// << name << " nargs: " << narg << "\n";
break;
}
case LM_TK_MATH: case LM_TK_MATH:
case LM_TK_BEGIN: { case LM_TK_BEGIN: {
int i = yylval.i; int i = yylval.i;
@ -522,7 +526,7 @@ void mathed_parse_into(MathArray & array, unsigned flags)
static int plevel = -1; static int plevel = -1;
++plevel; ++plevel;
yyvarcode = LM_TC_VAR; MathTextCodes yyvarcode = LM_TC_VAR;
int t = yylex(); int t = yylex();
bool panic = false; bool panic = false;
@ -559,12 +563,16 @@ void mathed_parse_into(MathArray & array, unsigned flags)
switch (t) { switch (t) {
case LM_TK_ALPHA: case LM_TK_ALPHA:
array.push_back(yylval.i, yyvarcode); if (!isspace(yylval.i) || yyvarcode == LM_TC_TEXTRM)
array.push_back(yylval.i, yyvarcode);
break; break;
case LM_TK_ARGUMENT: case LM_TK_ARGUMENT: {
array.push_back(new MathMacroArgument(yylval.i)); MathMacroArgument * p = new MathMacroArgument(yylval.i);
p->code(yyvarcode);
array.push_back(p);
break; break;
}
case LM_TK_SPECIAL: case LM_TK_SPECIAL:
array.push_back(yylval.i, LM_TC_SPECIAL); array.push_back(yylval.i, LM_TC_SPECIAL);
@ -846,27 +854,44 @@ MathArray mathed_parse_cell(string const & str)
} }
MathInset * mathed_parse(string const & str) MathMacroTemplate * mathed_parse_macro(string const & str)
{ {
istringstream is(str.c_str()); istringstream is(str.c_str());
return mathed_parse(is); return mathed_parse_macro(is);
} }
MathInset * mathed_parse(istream & is) MathMatrixInset * mathed_parse_normal(string const & str)
{
istringstream is(str.c_str());
return mathed_parse_normal(is);
}
MathMatrixInset * mathed_parse_normal(istream & is)
{ {
yyis = &is; yyis = &is;
yylineno = 0; yylineno = 0;
return mathed_parse(); return mathed_parse_normal();
} }
MathInset * mathed_parse(LyXLex & lex) MathMacroTemplate * mathed_parse_macro(istream & is)
{
yyis = &is;
yylineno = 0;
return mathed_parse_macro();
}
MathMatrixInset * mathed_parse_normal(LyXLex & lex)
{ {
yyis = &lex.getStream(); yyis = &lex.getStream();
yylineno = lex.GetLineNo(); yylineno = lex.GetLineNo();
MathInset * p = mathed_parse(); MathMatrixInset * p = mathed_parse_normal();
// Update line number // Update line number
lex.setLineNo(yylineno); lex.setLineNo(yylineno);
@ -883,4 +908,25 @@ MathInset * mathed_parse(LyXLex & lex)
return p; return p;
} }
MathMacroTemplate * mathed_parse_macro(LyXLex & lex)
{
yyis = &lex.getStream();
yylineno = lex.GetLineNo();
MathMacroTemplate * p = mathed_parse_macro();
// Update line number
lex.setLineNo(yylineno);
// reading of end_inset
while (lex.IsOK()) {
lex.nextToken();
if (lex.GetString() == "\\end_inset")
break;
lyxerr[Debug::MATHED] << "InsetFormula::Read: Garbage before \\end_inset,"
" or missing \\end_inset!" << endl;
}
return p;
}
//]}) //]})

View File

@ -29,7 +29,8 @@
#include "symbol_def.h" #include "symbol_def.h"
class MathArray; class MathArray;
class MathInset; class MathMatrixInset;
class MathMacroTemplate;
class LyXLex; class LyXLex;
/// ///
@ -120,8 +121,12 @@ latexkeys const * in_word_set(string const & str);
latexkeys const * lm_get_key_by_id(unsigned int id, short tc); latexkeys const * lm_get_key_by_id(unsigned int id, short tc);
MathInset * mathed_parse(string const &); MathMatrixInset * mathed_parse_normal(string const &);
MathInset * mathed_parse(std::istream &); MathMatrixInset * mathed_parse_normal(std::istream &);
MathInset * mathed_parse(LyXLex &); MathMatrixInset * mathed_parse_normal(LyXLex &);
MathMacroTemplate * mathed_parse_macro(string const &);
MathMacroTemplate * mathed_parse_macro(std::istream &);
MathMacroTemplate * mathed_parse_macro(LyXLex &);
#endif #endif