well, it basically needed re-writing. please apply.

thanks
john

-- 
"...life's not a paragraph
and death i think is no
parenthesis"
        - e. e. cummings
Index: src/BufferView.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView.C,v
retrieving revision 1.81
diff -u -r1.81 BufferView.C
--- src/BufferView.C    2001/08/02 14:54:59     1.81
+++ src/BufferView.C    2001/12/16 02:39:47
@@ -113,10 +113,9 @@
 }
 
 
-Inset * BufferView::checkInsetHit(LyXText * text, int & x, int & y,
-                                 unsigned int button)
+Inset * BufferView::checkInsetHit(LyXText * text, int & x, int & y)
 {
-       return pimpl_->checkInsetHit(text, x, y, button);
+       return pimpl_->checkInsetHit(text, x, y);
 }
 
 
Index: src/BufferView.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView.h,v
retrieving revision 1.78
diff -u -r1.78 BufferView.h
--- src/BufferView.h    2001/08/08 14:36:55     1.78
+++ src/BufferView.h    2001/12/16 02:39:47
@@ -73,8 +73,7 @@
        ///
        void updateScrollbar();
        ///
-       Inset * checkInsetHit(LyXText *, int & x, int & y,
-                             unsigned int button);
+       Inset * checkInsetHit(LyXText *, int & x, int & y);
        /// 
        void redoCurrentBuffer();
        ///
Index: src/BufferView_pimpl.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView_pimpl.C,v
retrieving revision 1.183
diff -u -r1.183 BufferView_pimpl.C
--- src/BufferView_pimpl.C      2001/12/10 20:06:54     1.183
+++ src/BufferView_pimpl.C      2001/12/16 02:39:55
@@ -579,7 +579,7 @@
 {
        if (!buffer_ || !screen_.get()) return;
 
-       Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
+       Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos);
 
        // ok ok, this is a hack.
        if (button == 4 || button == 5) {
@@ -755,7 +755,7 @@
        // If we hit an inset, we have the inset coordinates in these
        // and inset_hit points to the inset.  If we do not hit an
        // inset, inset_hit is 0, and inset_x == x, inset_y == y.
-       Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
+       Inset * inset_hit = checkInsetHit(bv_->text, x, y);
 
        if (bv_->theLockingInset()) {
                // We are in inset locking mode.
@@ -862,63 +862,91 @@
 }
 
 
-/* 
- * Returns an inset if inset was hit. 0 otherwise.
- * If hit, the coordinates are changed relative to the inset. 
- * Otherwise coordinates are not changed, and false is returned.
- */
-Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
-                                        unsigned int /* button */)
+Box BufferView::Pimpl::insetDimensions(LyXText const & text, LyXCursor const & 
+cursor) const
 {
+       Paragraph /*const*/ & par = *cursor.par();
+       pos_type const pos = cursor.pos();
+
+       lyx::Assert(par.getInset(pos));
+ 
+       Inset const & inset(*par.getInset(pos));
+
+       LyXFont const & font = text.getFont(buffer_, &par, pos);
+ 
+       int const width = inset.width(bv_, font);
+       int const inset_x = font.isVisibleRightToLeft()
+               ? (cursor.x() - width) : cursor.x();
+ 
+       return Box(
+               inset_x + inset.scroll(),
+               inset_x + width,
+               cursor.y() - inset.ascent(bv_, font),
+               cursor.y() + inset.descent(bv_, font));
+}
+ 
+ 
+Inset * BufferView::Pimpl::checkInset(LyXText const & text, LyXCursor const & cursor, 
+int & x, int & y) const
+{
+       pos_type const pos(cursor.pos());
+       Paragraph /*const*/ & par(*cursor.par());
+
+       if (pos >= par.size() || !par.isInset(pos)) {
+               return 0;
+       }
+
+       Inset /*const*/ * inset = par.getInset(pos);
+ 
+       if (!isEditableInset(inset)) {
+               return 0;
+       }
+
+       Box b(insetDimensions(text, cursor));
+
+       if (!b.contained(x, y)) {
+               return 0;
+       }
+ 
+       text.setCursor(bv_, &par, pos, true);
+ 
+       x -= b.x1;
+       // The origin of an inset is on the baseline
+       y -= (text.cursor.y());
+  
+       return inset;
+}
+
+ 
+Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y)
+{
        if (!screen_.get())
                return 0;
   
        int y_tmp = y + text->first;
-  
+ 
        LyXCursor cursor;
        text->setCursorFromCoordinates(bv_, cursor, x, y_tmp);
-       text->setCursor(bv_, cursor, cursor.par(),cursor.pos(),true);
+ 
+       Inset * inset(checkInset(*text, cursor, x, y_tmp));
 
-       lyx::pos_type pos;
+       if (inset) {
+               y = y_tmp;
+               return inset;
+       }
+ 
+       // look at previous position
  
-       if (cursor.pos() < cursor.par()->size()
-           && cursor.par()->isInset(cursor.pos())
-           && isEditableInset(cursor.par()->getInset(cursor.pos())))
-       {
-               pos = cursor.pos();
-       } else if ((cursor.pos() - 1 >= 0)
-               && cursor.par()->isInset(cursor.pos() - 1)
-               && isEditableInset(cursor.par()->getInset(cursor.pos() - 1)))
-       {
-               pos = cursor.pos() - 1;
-               // if the inset takes a full row, then the cursor.y()
-               // at the end of the inset will be wrong. So step the cursor
-               // back one
-               text->setCursor(bv_, cursor, cursor.par(), cursor.pos() - 1, true);
-       } else {
+       if (cursor.pos() == 0) {
                return 0;
        }
 
-       // Check whether the inset really was hit
-       Inset * inset = cursor.par()->getInset(pos);
-       LyXFont const & font = text->getFont(buffer_, cursor.par(), pos);
-       int const width = inset->width(bv_, font);
-       int const inset_x = font.isVisibleRightToLeft()
-               ? cursor.x() - width : cursor.x();
-       int const start_x = inset_x + inset->scroll();
-       int const end_x = inset_x + width;
-       int const start_y = cursor.y() - inset->ascent(bv_, font);
-       int const end_y = cursor.y() + inset->descent(bv_, font);
- 
-       if (x > start_x && x < end_x && y_tmp > start_y && y < end_y) {
-               text->setCursor(bv_, cursor.par(), pos, true);
-               x = x - start_x;
-               // The origin of an inset is on the baseline
-               y = y_tmp - (text->cursor.y()); 
-               return inset;
-       }
+       // move back one
+       text->setCursor(bv_, cursor, cursor.par(), cursor.pos() - 1, true);
 
-       return 0;
+       inset = checkInset(*text, cursor, x, y_tmp);
+       if (inset) {
+               y = y_tmp;
+       }
+       return inset;
 }
 
 
Index: src/BufferView_pimpl.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView_pimpl.h,v
retrieving revision 1.41
diff -u -r1.41 BufferView_pimpl.h
--- src/BufferView_pimpl.h      2001/11/27 10:34:11     1.41
+++ src/BufferView_pimpl.h      2001/12/16 02:39:55
@@ -10,6 +10,7 @@
 #include "commandtags.h"
 #include "frontends/Timeout.h"
 #include "WorkArea.h"
+#include "box.h"
 #include "insets/insetspecialchar.h"
 #include "support/types.h"
 
@@ -54,9 +55,12 @@
        void updateScrollbar();
        ///
        void scrollCB(double value);
-       ///
-       Inset * checkInsetHit(LyXText *, int & x, int & y,
-                             unsigned int button);
+       /**
+        * Returns an inset if inset was hit, or 0 if not.
+        *
+        * If hit, the coordinates are changed relative to the inset.
+        */
+       Inset * checkInsetHit(LyXText *, int & x, int & y);
        /// 
        int scrollUp(long time);
        ///
@@ -126,6 +130,17 @@
        ///
        bool Dispatch(kb_action action, string const & argument);
 private:
+       /**
+        * Return the on-screen dimensions of the inset at the cursor.
+        * Pre-condition: the cursor must be at an inset.
+        */
+       Box insetDimensions(LyXText const & text, LyXCursor const & cursor) const;
+       /**
+        * check if the given co-ordinates are inside an inset at the given cursor,
+        * if one exists. If so, the inset is returned, and the co-ordinates are
+        * made relative. Otherwise, 0 is returned.
+        */
+       Inset * checkInset(LyXText const & text, LyXCursor const & cursor, int & x, 
+int & y) const; 
        ///
        friend class BufferView;
        /// open and lock an updatable inset
Index: src/Makefile.am
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/Makefile.am,v
retrieving revision 1.99
diff -u -r1.99 Makefile.am
--- src/Makefile.am     2001/12/05 23:11:06     1.99
+++ src/Makefile.am     2001/12/16 02:40:10
@@ -89,6 +89,7 @@
        WorkArea.h \
        XFormsView.C \
        XFormsView.h \
+       box.h \
        broken_headers.h \
        buffer.C \
        buffer.h \
Index: src/box.h
===================================================================
RCS file: box.h
diff -N box.h
--- /dev/null   Tue May  5 22:32:27 1998
+++ box.h       Sun Dec 16 03:40:10 2001
@@ -0,0 +1,43 @@
+/**
+ * \file box.h
+ * Copyright 2001 the LyX Team
+ * Read the file COPYING
+ *
+ * \author John Levon <[EMAIL PROTECTED]>
+ */
+
+#ifndef BOX_H
+#define BOX_H
+
+#include <config.h>
+ 
+#include "debug.h" 
+ 
+/**
+ * A simple class representing rectangular regions.
+ * It is expected that the box be constructed in
+ * normalised form, that is to say : x1,y1 is top-left,
+ * x2,y2 is bottom-right.
+ */
+struct Box {
+       unsigned int x1;
+       unsigned int x2;
+       unsigned int y1;
+       unsigned int y2;
+
+       Box(unsigned int x1_, unsigned int x2_,
+               unsigned int y1_, unsigned int y2_) :
+               x1(x1_), x2(x2_), y1(y1_), y2(y2_) {}
+
+       /**
+        * Returns true if the given co-ordinates are within
+        * the box. Check is exclusive (point on a border
+        * returns false).
+        */
+       bool contained(unsigned int x, unsigned int y) {
+               return (x1 < x && x2 > x &&
+                       y1 < y && y2 > y);
+       }
+};
+ 
+#endif // BOX_H
Index: src/insets/insettext.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insettext.C,v
retrieving revision 1.233
diff -u -r1.233 insettext.C
--- src/insets/insettext.C      2001/12/14 11:55:58     1.233
+++ src/insets/insettext.C      2001/12/16 02:40:48
@@ -914,7 +914,7 @@
 
        int tmp_x = x - drawTextXOffset;
        int tmp_y = y + insetAscent - getLyXText(bv)->first;
-       Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
+       Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
 
        hideInsetCursor(bv);
        if (the_locking_inset) {
@@ -1960,7 +1960,7 @@
        x -= drawTextXOffset;
        int dummyx = x;
        int dummyy = y + insetAscent;
-       Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
+       Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
 
        if (inset) {
                if (x < 0)
Index: src/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/ChangeLog,v
retrieving revision 1.464
diff -u -r1.464 ChangeLog
--- src/ChangeLog       2001/12/15 16:24:40     1.464
+++ src/ChangeLog       2001/12/16 02:44:57
@@ -1,3 +1,13 @@
+2001-12-15  John Levon  <[EMAIL PROTECTED]>
+
+       * BufferView.h: 
+       * BufferView.C:
+       * BufferView_pimpl.h:
+       * BufferView_pimpl.C: cleanup and fix of checkInsetHit().
+
+       * Makefile.am:
+       * box.h: new start of class for above
+
 2001-12-15  Lars Gullik Bj�nnes  <[EMAIL PROTECTED]>
 
        * lyxlength.C (LyXLength): Initialize private variables.

Reply via email to