From 1129104282827a42acc529c8f142f64885afce01 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Mon, 24 Sep 2001 16:25:06 +0000 Subject: [PATCH] changed cursor movement/deletion behaviour in super/subscripts git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2796 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/math_atom.C | 19 +++++++++++ src/mathed/math_atom.h | 6 ++++ src/mathed/math_cursor.C | 64 ++++++++++++++++++++++++++++++++--- src/mathed/math_inset.h | 4 ++- src/mathed/math_scriptinset.C | 5 +++ src/mathed/math_scriptinset.h | 8 ++++- 6 files changed, 100 insertions(+), 6 deletions(-) diff --git a/src/mathed/math_atom.C b/src/mathed/math_atom.C index d9d3d6be2b..0d90ab8d1d 100644 --- a/src/mathed/math_atom.C +++ b/src/mathed/math_atom.C @@ -42,6 +42,13 @@ MathAtom::MathAtom(MathInset * p) } +MathAtom::MathAtom(MathInset * p, MathScriptInset * up, MathScriptInset * down) + : nucleus_(p), limits_(0), xo_(0), yo_(0) +{ + script_[0] = down; + script_[1] = up; +} + MathAtom::MathAtom(MathAtom const & p) { @@ -204,6 +211,18 @@ MathScriptInset * MathAtom::down() const } +MathScriptInset * & MathAtom::up() +{ + return script_[1]; +} + + +MathScriptInset * & MathAtom::down() +{ + return script_[0]; +} + + int MathAtom::dy0() const { if (!down()) diff --git a/src/mathed/math_atom.h b/src/mathed/math_atom.h index b6137142ce..c5ef2c5b86 100644 --- a/src/mathed/math_atom.h +++ b/src/mathed/math_atom.h @@ -38,6 +38,8 @@ public: MathAtom(MathAtom const &); /// explicit MathAtom(MathInset * p); + /// + MathAtom(MathInset * p, MathScriptInset * up, MathScriptInset * down); /// virtual ~MathAtom(); /// @@ -98,6 +100,10 @@ public: MathScriptInset * up() const; /// returns subscript MathScriptInset * down() const; + /// returns superscript + MathScriptInset * & up(); + /// returns subscript + MathScriptInset * & down(); /// MathInset * nucleus() const { return nucleus_; } /// diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 6e62c43f5e..58aa713375 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -146,8 +146,11 @@ bool MathCursor::popLeft() { if (Cursor_.size() <= 1) return false; + if (nextAtom()) + nextAtom()->removeEmptyScripts(); Cursor_.pop_back(); - //array().at(pos())->removeEmptyScripts(); + if (nextAtom()) + nextAtom()->removeEmptyScripts(); return true; } @@ -156,8 +159,11 @@ bool MathCursor::popRight() { if (Cursor_.size() <= 1) return false; + if (nextAtom()) + nextAtom()->removeEmptyScripts(); Cursor_.pop_back(); - //array().at(pos())->removeEmptyScripts(); + if (nextAtom()) + nextAtom()->removeEmptyScripts(); posRight(); return true; } @@ -469,7 +475,7 @@ void MathCursor::insert(MathArray const & ar) void MathCursor::backspace() { if (posLeft()) { - plainErase(); + erase(); return; } @@ -503,9 +509,41 @@ void MathCursor::erase() return; } - if (pos() < size()) + if (pos() == size()) + return; + + // delete nucleus, keep scripts if possible + MathAtom * p = prevAtom(); + MathAtom * n = nextAtom(); + if (pos() > 0) { + bool need_parans = (p->up() && n->up()) || (p->down() && n->down()); + if (need_parans) { + // need empty block + insert('{', LM_TC_TEX); + insert('}', LM_TC_TEX); + p = prevAtom(); + n = nextAtom(); + } + // move indices to the left + if (n->up()) + swap(p->up(), n->up()); + if (n->down()) + swap(p->down(), n->down()); plainErase(); + return; + } + // pos == 0 now + if (n->up() || n->down()) { + insert('{', LM_TC_TEX); + insert('}', LM_TC_TEX); + p = prevAtom(); + n = nextAtom(); + swap(p->up(), n->up()); + swap(p->down(), n->down()); + } + + plainErase(); dump("erase 2"); } @@ -1119,6 +1157,15 @@ bool MathCursor::goUp() if (par()->idxUp(idx(), pos())) return true; + // leave subscript to the nearest side + if (par()->asScriptInset() && par()->asScriptInset()->down()) { + if (pos() <= size() / 2) + popLeft(); + else + popRight(); + return true; + } + // if not, apply brute force. int x0; int y0; @@ -1141,6 +1188,15 @@ bool MathCursor::goDown() if (par()->idxDown(idx(), pos())) return true; + // leave superscript to the nearest side + if (par()->asScriptInset() && par()->asScriptInset()->up()) { + if (pos() <= size() / 2) + popLeft(); + else + popRight(); + return true; + } + // if not, apply brute force. int x0; int y0; diff --git a/src/mathed/math_inset.h b/src/mathed/math_inset.h index 6b6875ef15..f95a3b8716 100644 --- a/src/mathed/math_inset.h +++ b/src/mathed/math_inset.h @@ -168,8 +168,10 @@ public: virtual bool isGrid() const { return false; } /// identifies ArrayInsets virtual bool isArray() const { return false; } - /// identifies Charinsets + /// identifies CharInsets virtual MathCharInset const * asCharInset() const { return 0; } + /// identifies ScriptInsets + virtual MathScriptInset const * asScriptInset() const { return 0; } /// virtual bool isActive() const { return nargs() > 0; } /// diff --git a/src/mathed/math_scriptinset.C b/src/mathed/math_scriptinset.C index 9e8732797e..5fc4110ca4 100644 --- a/src/mathed/math_scriptinset.C +++ b/src/mathed/math_scriptinset.C @@ -20,6 +20,11 @@ MathInset * MathScriptInset::clone() const } +MathScriptInset const * MathScriptInset::asScriptInset() const +{ + return this; +} + void MathScriptInset::write(std::ostream & os, bool fragile) const { cell(0).write(os, fragile); diff --git a/src/mathed/math_scriptinset.h b/src/mathed/math_scriptinset.h index 3dc848f989..491d5a0e90 100644 --- a/src/mathed/math_scriptinset.h +++ b/src/mathed/math_scriptinset.h @@ -15,7 +15,7 @@ class MathScriptInset : public MathNestInset { public: /// - MathScriptInset(bool up); + explicit MathScriptInset(bool up); /// MathInset * clone() const; /// @@ -24,6 +24,12 @@ public: void metrics(MathStyles st) const; /// void draw(Painter &, int x, int y) const; + /// + MathScriptInset const * asScriptInset() const; + /// + bool up() const { return up_; } + /// + bool down() const { return !up_; } private: /// bool up_; -- 2.39.2