]> git.lyx.org Git - features.git/commitdiff
changed cursor movement/deletion behaviour in super/subscripts
authorAndré Pönitz <poenitz@gmx.net>
Mon, 24 Sep 2001 16:25:06 +0000 (16:25 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Mon, 24 Sep 2001 16:25:06 +0000 (16:25 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2796 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/math_atom.C
src/mathed/math_atom.h
src/mathed/math_cursor.C
src/mathed/math_inset.h
src/mathed/math_scriptinset.C
src/mathed/math_scriptinset.h

index d9d3d6be2b79bfd40592f9f4c0fe5948bc8bd3a9..0d90ab8d1d985f39108a154b5516b9cfeb67138e 100644 (file)
@@ -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())
index b6137142ce9636c0fe213bf29e231ebe57d30b2a..c5ef2c5b860e8ff53c3c3a51f91ade945d4a9eee 100644 (file)
@@ -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_; }
        ///
index 6e62c43f5e16ae2d7b52018c965a6edcce583d44..58aa713375580102c28805cbb2d0e91e5d07b3d1 100644 (file)
@@ -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;
index 6b6875ef15a0384a7b619e3671a9d6ca291c5567..f95a3b8716677bff5e72871eef89dc83c1fa7358 100644 (file)
@@ -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; }
        ///
index 9e8732797e8f11824554d5ff0a1281198b5b0d76..5fc4110ca4fbcc011318258a45e6f8fd90e7db54 100644 (file)
@@ -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);
index 3dc848f9892945546e0a5cec99b60f064ccea12a..491d5a0e90cbad0c3a2dea8da5d2c2b31673814a 100644 (file)
@@ -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_;