Attached patch fixes the following problem:

       \mathrm{some l|ong text}   \\
       1\mathrm{some lpong text}2

cursor at '|', usere presses 'down'. Previously the cursor ended up in 1 or
2, now it goes to 'p'.

Ok to apply?

Andre'

[The whole positioning logic probably needs some polish, but as it happens
to work in most cases right now...]

-- 
Those who desire to give up Freedom in order to gain Security,
will not have, nor do they deserve, either one. (T. Jefferson)
Index: math_cursor.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_cursor.C,v
retrieving revision 1.320
diff -u -p -r1.320 math_cursor.C
--- math_cursor.C       15 Oct 2002 06:21:58 -0000      1.320
+++ math_cursor.C       15 Oct 2002 14:20:52 -0000
@@ -262,7 +262,8 @@ void MathCursor::last()
 }
 
 
-bool positionable(MathIterator const & cursor, MathIterator const & anchor)
+bool positionable
+       (MathIterator const & cursor, MathIterator const & anchor)
 {
        // avoid deeper nested insets when selecting
        if (cursor.size() > anchor.size())
@@ -1003,8 +1004,12 @@ bool MathCursor::goUpDown(bool up)
        while (1) {
                ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << 
'\n';
                // ask inset first
-               if (par()->idxUpDown(idx(), pos(), up, targetx_))
+               if (par()->idxUpDown(idx(), pos(), up, targetx_)) {
+                       // try to find best position within this inset
+                       if (!selection())
+                               bruteFind2(xo, yo);
                        return true;
+               }
 
                // no such inset found, just take something "above"
                ///lyxerr << "updown: handled by strange case\n";
@@ -1058,6 +1063,31 @@ bool MathCursor::bruteFind
        if (best_dist < 1e10)
                Cursor_ = best_cursor;
        return best_dist < 1e10;
+}
+
+
+void MathCursor::bruteFind2(int x, int y)
+{
+       double best_dist = 1e10;
+
+       MathIterator it = Cursor_;
+       it.back().setPos(0);
+       MathIterator et = Cursor_;
+       et.back().setPos(it.cell().size());
+       while (1) {
+               int xo, yo;
+               it.back().getPos(xo, yo);
+               double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
+               // '<=' in order to take the last possible position
+               // this is important for clicking behind \sum in e.g. '\sum_i a'
+               if (d <= best_dist) {
+                       best_dist = d;
+                       Cursor_   = it;
+               }
+               if (it == et)
+                       break;
+               ++it;
+       }
 }
 
 
Index: math_cursor.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_cursor.h,v
retrieving revision 1.131
diff -u -p -r1.131 math_cursor.h
--- math_cursor.h       11 Sep 2002 08:26:02 -0000      1.131
+++ math_cursor.h       15 Oct 2002 14:20:52 -0000
@@ -260,12 +260,13 @@ private:
        bool posRight();
        /// moves position somehow up or down
        bool goUpDown(bool up);
-       /// moves position into box
-       bool bruteFind(int xo, int yo, int xlow, int xhigh, int ylow, int yhigh);
+       /// moves position closest to (x, y) in given box
+       bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
+       /// moves position closest to (x, y) in current cell
+       void bruteFind2(int x, int y);
        /// are we in a nucleus of a script inset?
        bool inNucleus() const;
 
-
        /// grab grid marked by anchor and current cursor 
        MathGridInset grabSelection() const;
        /// erase the selected part and re-sets the cursor
@@ -273,9 +274,9 @@ private:
        /// guess what
        MathGridInset grabAndEraseSelection();
 
-       ///
+       /// the name of the macro we are currently inputting
        string macroName() const;
-       ///
+       /// where in the curent cell does the macro name start?
        MathInset::difference_type macroNamePos() const;
        /// can we enter the inset?
        bool openable(MathAtom const &, bool selection) const;
Index: math_iterator.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_iterator.C,v
retrieving revision 1.14
diff -u -p -r1.14 math_iterator.C
--- math_iterator.C     11 Sep 2002 09:14:56 -0000      1.14
+++ math_iterator.C     15 Oct 2002 14:20:52 -0000
@@ -77,8 +77,8 @@ void MathIterator::goEnd()
 
 void MathIterator::operator++()
 {
-       MathCursorPos   & top = back();
-       MathArray & ar  = top.par_->cell(top.idx_);
+       MathCursorPos & top = back();
+       MathArray     & ar  = top.par_->cell(top.idx_);
 
        // move into the current inset if possible
        // it is impossible for pos() == size()!
Index: math_pos.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_pos.C,v
retrieving revision 1.8
diff -u -p -r1.8 math_pos.C
--- math_pos.C  11 Sep 2002 09:14:56 -0000      1.8
+++ math_pos.C  15 Oct 2002 14:20:52 -0000
@@ -44,6 +44,12 @@ void MathCursorPos::getPos(int & x, int 
 }
 
 
+void MathCursorPos::setPos(MathArray::pos_type pos)
+{
+       pos_ = pos;
+}
+
+
 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
 {
        os << "(par: " << p.par_ << " idx: " << p.idx_ << " pos: " << p.pos_ << ")";
Index: math_pos.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_pos.h,v
retrieving revision 1.6
diff -u -p -r1.6 math_pos.h
--- math_pos.h  11 Sep 2002 09:14:56 -0000      1.6
+++ math_pos.h  15 Oct 2002 14:20:52 -0000
@@ -23,6 +23,8 @@ public:
        MathArray & cell(MathArray::idx_type idx) const;
        /// gets screen position of the thing
        void getPos(int & x, int & y) const;
+       /// set position
+       void setPos(MathArray::pos_type pos);
 
 public:
        /// pointer to an inset

Reply via email to