binom is binom is binom and choose is choose is choose ...

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4528 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2002-07-04 11:00:51 +00:00
parent 3fac52583e
commit cbafd3fc82
11 changed files with 115 additions and 123 deletions

View File

@ -63,14 +63,23 @@ InsetFormula::InsetFormula()
: par_(MathAtom(new MathHullInset)), loader_(0) : par_(MathAtom(new MathHullInset)), loader_(0)
{} {}
/*
InsetFormula::InsetFormula(string const & type) InsetFormula::InsetFormula(BufferView * bv)
: par_(MathAtom(new MathHullInset(type))), loader_(0) : par_(MathAtom(new MathHullInset)), loader_(0)
{} {
view_ = bv;
}
*/ InsetFormula::InsetFormula(string const & data)
: par_(MathAtom(new MathHullInset)), loader_(0)
{
if (!data.size())
return;
if (!mathed_parse_normal(par_, data))
lyxerr << "cannot interpret '" << data << "' as math\n";
}
Inset * InsetFormula::clone(Buffer const &, bool) const Inset * InsetFormula::clone(Buffer const &, bool) const
@ -95,23 +104,6 @@ int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
} }
void InsetFormula::read(string const & s)
{
if (s.size()) {
bool res = mathed_parse_normal(par_, s);
if (!res)
res = mathed_parse_normal(par_, "$" + s + "$");
if (!res) {
lyxerr << "cannot interpret '" << s << "' as math\n";
par_ = MathAtom(new MathHullInset("simple"));
}
}
metrics();
updatePreview();
}
int InsetFormula::ascii(Buffer const *, ostream & os, int) const int InsetFormula::ascii(Buffer const *, ostream & os, int) const
{ {
@ -184,7 +176,8 @@ void InsetFormula::draw(BufferView * bv, LyXFont const & font,
if (canPreview()) { if (canPreview()) {
pi.pain.image(x + 1, y - a + 1, w - 2, h - 2, *(loader_->image())); pi.pain.image(x + 1, y - a + 1, w - 2, h - 2, *(loader_->image()));
} else { } else {
pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT; //pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
pi.base.style = LM_ST_TEXT;
pi.base.font = font; pi.base.font = font;
pi.base.font.setColor(LColor::math); pi.base.font.setColor(LColor::math);
if (lcolor.getX11Name(LColor::mathbg) if (lcolor.getX11Name(LColor::mathbg)
@ -302,7 +295,7 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
int x; int x;
int y; int y;
mathcursor->getPos(x, y); mathcursor->getPos(x, y);
hull()->mutate(arg); mutate(arg);
mathcursor->setPos(x, y); mathcursor->setPos(x, y);
mathcursor->normalize(); mathcursor->normalize();
updateLocal(bv, true); updateLocal(bv, true);
@ -326,9 +319,9 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
int y = 0; int y = 0;
mathcursor->getPos(x, y); mathcursor->getPos(x, y);
if (hullType() == "simple") if (hullType() == "simple")
hull()->mutate("equation"); mutate("equation");
else else
hull()->mutate("simple"); mutate("simple");
mathcursor->setPos(x, y); mathcursor->setPos(x, y);
mathcursor->normalize(); mathcursor->normalize();
updateLocal(bv, true); updateLocal(bv, true);
@ -419,15 +412,16 @@ int InsetFormula::width(BufferView * bv, LyXFont const & font) const
} }
string const & InsetFormula::hullType() const string InsetFormula::hullType() const
{ {
return hull()->getType(); return hull() ? hull()->getType() : "none";
} }
void InsetFormula::mutate(string const & type ) void InsetFormula::mutate(string const & type )
{ {
hull()->mutate(type); if (hull())
hull()->mutate(type);
} }

View File

@ -34,7 +34,9 @@ public:
/// ///
InsetFormula(); InsetFormula();
/// ///
//explicit InsetFormula(string const & type); explicit InsetFormula(BufferView *);
///
explicit InsetFormula(const string & data);
/// ///
int ascent(BufferView *, LyXFont const &) const; int ascent(BufferView *, LyXFont const &) const;
/// ///
@ -49,8 +51,6 @@ public:
/// ///
void read(Buffer const *, LyXLex & lex); void read(Buffer const *, LyXLex & lex);
/// ///
void read(const string & data);
///
int latex(Buffer const *, std::ostream &, bool fragile, bool free_spc) const; int latex(Buffer const *, std::ostream &, bool fragile, bool free_spc) const;
/// ///
int ascii(Buffer const *, std::ostream &, int linelen) const; int ascii(Buffer const *, std::ostream &, int linelen) const;
@ -72,7 +72,7 @@ public:
/// ///
std::vector<string> const getLabelList() const; std::vector<string> const getLabelList() const;
/// ///
string const & hullType() const; string hullType() const;
/// ///
MathAtom const & par() const { return par_; } MathAtom const & par() const { return par_; }
/// ///

View File

@ -79,7 +79,7 @@ bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
delete new_inset; delete new_inset;
return false; return false;
} }
new_inset->edit(bv, 0, 0, mouse_button::none); new_inset->edit(bv, true);
return true; return true;
} }
@ -154,7 +154,8 @@ void InsetFormulaBase::metrics(BufferView * bv) const
view_ = bv; view_ = bv;
MathMetricsInfo mi; MathMetricsInfo mi;
mi.view = view_; mi.view = view_;
mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT; //mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
mi.base.style = LM_ST_TEXT;
mi.base.font = font_; mi.base.font = font_;
mi.base.font.setColor(LColor::math); mi.base.font.setColor(LColor::math);
par()->metrics(mi); par()->metrics(mi);
@ -227,8 +228,8 @@ void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
bv->hideLockedInsetCursor(); bv->hideLockedInsetCursor();
else { else {
metrics(bv); metrics(bv);
int x; int x = 0;
int y; int y = 0;
mathcursor->getPos(x, y); mathcursor->getPos(x, y);
y -= yo_; y -= yo_;
int asc = 0; int asc = 0;
@ -935,45 +936,44 @@ bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
void mathDispatchCreation(BufferView * bv, string const & arg, bool display) void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
{ {
if (bv->available()) { if (!bv->available())
// use selection if available.. return;
//string sel;
//if (action == LFUN_MATH_IMPORT_SELECTION)
// sel = "";
//else
string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false); // use selection if available..
//string sel;
//if (action == LFUN_MATH_IMPORT_SELECTION)
// sel = "";
//else
if (sel.empty()) { string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
InsetFormula * f = new InsetFormula;
if (sel.empty()) {
InsetFormula * f = new InsetFormula(bv);
if (openNewInset(bv, f)) {
f->mutate("simple"); f->mutate("simple");
if (openNewInset(bv, f)) { // don't do that also for LFUN_MATH_MODE unless you want end up with
// don't do that also for LFUN_MATH_MODE unless you want end up with // always changing to mathrm when opening an inlined inset
// always changing to mathrm when opening an inlined inset // -- I really hate "LyXfunc overloading"...
// -- I really hate "LyXfunc overloading"... if (display)
if (display) f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
f->localDispatch(bv, LFUN_MATH_DISPLAY, string()); f->localDispatch(bv, LFUN_INSERT_MATH, arg);
f->localDispatch(bv, LFUN_INSERT_MATH, arg); }
} } else {
// create a macro if we see "\\newcommand" somewhere, and an ordinary
// formula otherwise
if (sel.find("\\newcommand") == string::npos &&
sel.find("\\def") == string::npos)
{
InsetFormula * f = new InsetFormula(sel);
bv->getLyXText()->cutSelection(bv);
openNewInset(bv, f);
} else { } else {
// create a macro if we see "\\newcommand" somewhere, and an ordinary string name;
// formula otherwise if (!mathed_parse_macro(name, sel))
if (sel.find("\\newcommand") == string::npos && return;
sel.find("\\def") == string::npos) InsetFormulaMacro * f = new InsetFormulaMacro(sel);
{ bv->getLyXText()->cutSelection(bv);
InsetFormula * f = new InsetFormula; openNewInset(bv, f);
f->mutate("simple");
f->read(sel);
bv->getLyXText()->cutSelection(bv);
openNewInset(bv, f);
} else {
string name;
if (!mathed_parse_macro(name, sel))
return;
InsetFormulaMacro * f = new InsetFormulaMacro(sel);
bv->getLyXText()->cutSelection(bv);
openNewInset(bv, f);
}
} }
} }
bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode")); bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
@ -1000,67 +1000,64 @@ void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
void mathDispatchMathMacro(BufferView * bv, string const & arg) void mathDispatchMathMacro(BufferView * bv, string const & arg)
{ {
if (bv->available()) { if (!bv->available())
if (arg.empty()) return;
bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument")); if (arg.empty())
else { bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
string s = arg; else {
string const s1 = token(s, ' ', 1); string s = arg;
int const na = s1.empty() ? 0 : lyx::atoi(s1); string const s1 = token(s, ' ', 1);
openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na)); int const na = s1.empty() ? 0 : lyx::atoi(s1);
} openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
} }
} }
void mathDispatchMathDelim(BufferView * bv, string const & arg) void mathDispatchMathDelim(BufferView * bv, string const & arg)
{ {
if (bv->available()) { if (!bv->available())
InsetFormula * f = new InsetFormula; return;
InsetFormula * f = new InsetFormula(bv);
if (openNewInset(bv, f)) {
f->mutate("simple"); f->mutate("simple");
if (openNewInset(bv, new InsetFormula)) bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
} }
} }
void mathDispatchInsertMatrix(BufferView * bv, string const & arg) void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
{ {
if (bv->available()) { if (!bv->available())
InsetFormula * f = new InsetFormula; return;
InsetFormula * f = new InsetFormula(bv);
if (openNewInset(bv, f)) {
f->mutate("simple"); f->mutate("simple");
if (openNewInset(bv, new InsetFormula)) bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
} }
} }
void mathDispatchInsertMath(BufferView * bv, string const & arg) void mathDispatchInsertMath(BufferView * bv, string const & arg)
{ {
if (bv->available()) { if (!bv->available())
if (arg.size() && arg[0] == '\\') { return;
InsetFormula * f = new InsetFormula; InsetFormula * f = new InsetFormula(bv);
f->read(arg); if (openNewInset(bv, f)) {
if (!bv->insertInset(f)) f->mutate("simple");
delete f; bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATH, arg);
else if (!mathcursor) // hotfix
bv->getLyXText()->cursorRight(bv);
} else {
mathDispatchMathMode(bv, arg);
}
} }
} }
void mathDispatchGreek(BufferView * bv, string const & arg) void mathDispatchGreek(BufferView * bv, string const & arg)
{ {
if (bv->available()) { if (!bv->available())
InsetFormula * f = new InsetFormula; return;
InsetFormula * f = new InsetFormula(bv);
if (openNewInset(bv, f)) {
f->mutate("simple"); f->mutate("simple");
if (openNewInset(bv, f)) { bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg); bv->unlockInset(f);
bv->unlockInset(f);
}
} }
} }

View File

@ -43,7 +43,7 @@ public:
/// ///
virtual void draw(BufferView *,LyXFont const &, int, float &, bool) const = 0; virtual void draw(BufferView *,LyXFont const &, int, float &, bool) const = 0;
/// ///
virtual string const & hullType() const = 0; virtual string hullType() const { return "none"; }
/// lowest x coordinate /// lowest x coordinate
virtual int xlow() const; virtual int xlow() const;
/// highest x coordinate /// highest x coordinate
@ -125,6 +125,7 @@ private:
/// common base for handling accents /// common base for handling accents
void handleAccent(BufferView * bv, string const & arg, string const & name); void handleAccent(BufferView * bv, string const & arg, string const & name);
protected:
/// ///
mutable BufferView * view_; mutable BufferView * view_;
/// ///

View File

@ -158,13 +158,6 @@ Inset::Code InsetFormulaMacro::lyxCode() const
} }
string const & InsetFormulaMacro::hullType() const
{
static string macro("macro");
return macro;
}
void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f, void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
int y, float & xx, bool /*cleared*/) const int y, float & xx, bool /*cleared*/) const
{ {

View File

@ -64,8 +64,6 @@ public:
/// ///
Inset::Code lyxCode() const; Inset::Code lyxCode() const;
/// ///
string const & hullType() const;
///
MathAtom const & par() const; MathAtom const & par() const;
/// ///
MathAtom & par(); MathAtom & par();

View File

@ -13,7 +13,8 @@
using std::max; using std::max;
MathBinomInset::MathBinomInset() MathBinomInset::MathBinomInset(bool choose)
: choose_(choose)
{} {}
@ -58,7 +59,10 @@ void MathBinomInset::draw(MathPainterInfo & pi, int x, int y) const
void MathBinomInset::write(WriteStream & os) const void MathBinomInset::write(WriteStream & os) const
{ {
os << '{' << cell(0) << " \\choose " << cell(1) << '}'; if (choose_)
os << '{' << cell(0) << " \\choose " << cell(1) << '}';
else
os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
} }

View File

@ -14,7 +14,7 @@
class MathBinomInset : public MathFracbaseInset { class MathBinomInset : public MathFracbaseInset {
public: public:
/// ///
MathBinomInset(); explicit MathBinomInset(bool choose = false);
/// ///
MathInset * clone() const; MathInset * clone() const;
/// ///
@ -28,6 +28,8 @@ public:
private: private:
/// ///
int dw() const; int dw() const;
///
bool choose_;
}; };
#endif #endif

View File

@ -297,7 +297,7 @@ MathAtom createMathInset(string const & s)
if (s == "stack") if (s == "stack")
return MathAtom(new MathStackrelInset); return MathAtom(new MathStackrelInset);
if (s == "binom" || s == "choose") if (s == "binom" || s == "choose")
return MathAtom(new MathBinomInset); return MathAtom(new MathBinomInset(s == "choose"));
if (s == "over" || s == "frac") if (s == "over" || s == "frac")
return MathAtom(new MathFracInset); return MathAtom(new MathFracInset);
if (s == "atop") if (s == "atop")

View File

@ -107,7 +107,7 @@ private:
/// consistency check /// consistency check
void check() const; void check() const;
/// "simple", "display", "eqnarray",... /// "none", "simple", "display", "eqnarray",...
string type_; string type_;
/// ///
std::vector<int> nonum_; std::vector<int> nonum_;

View File

@ -582,7 +582,10 @@ bool Parser::parse_normal(MathAtom & at)
if (ar.size() != 1) { if (ar.size() != 1) {
lyxerr << "Unusual contents found: " << ar << endl; lyxerr << "Unusual contents found: " << ar << endl;
at.reset(new MathParInset); at.reset(new MathParInset);
at->cell(0) = ar; if (at->nargs() > 0)
at->cell(0) = ar;
else
lyxerr << "Unusual contents found: " << ar << endl;
return true; return true;
} }
at = ar[0]; at = ar[0];
@ -690,7 +693,7 @@ void Parser::parse_into1(MathGridInset & grid, unsigned flags,
MathArray ar; MathArray ar;
parse_into(ar, FLAG_BRACE_LAST, mathmode); parse_into(ar, FLAG_BRACE_LAST, mathmode);
// reduce multiple nesting levels to a single one // reduce multiple nesting levels to a single one
// this helps to keep the annoyance of a choose b to a minimum // this helps to keep the annoyance of "a choose b" to a minimum
if (ar.size() && ar.front()->asBraceInset()) if (ar.size() && ar.front()->asBraceInset())
ar = ar.front()->asBraceInset()->cell(0); ar = ar.front()->asBraceInset()->cell(0);
cell->push_back(MathAtom(new MathBraceInset)); cell->push_back(MathAtom(new MathBraceInset));