[haiku-development] Re: TextView/TextControl scrolling

  • From: "Rene Gollent" <anevilyak@xxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Thu, 27 Sep 2007 19:52:04 -0500

Hi Stefano,

Give this one a try please.

Regards,

Rene

On 9/27/07, Rene Gollent <anevilyak@xxxxxxxxx> wrote:
> Will look when I get home from work, thanks for the heads up :)
>
> On 9/27/07, Stefano Ceccherini <stefano.ceccherini@xxxxxxxxx> wrote:
> > 2007/9/27, Rene Gollent <anevilyak@xxxxxxxxx>:
> > > Hi there,
> > >
> > > I've recently been investigating the incorrect scrolling problems with
> > > BTextControl, and have come up with a patch which fixes it without (as
> > > far as I can tell) breaking any other behavior. If someone could
> > > please have a look at it and let me know if they see any issues, I'd
> > > appreciate it.
> >
> > So, I've applied the patch to my local tree, and tested it.
> > I see one problem when you rename a file/folder in the tracker's desktop
> > window.
> > The text widget is scrolled too much to the right and downward.
> >
> >
>
Index: src/kits/interface/TextInput.h
===================================================================
--- src/kits/interface/TextInput.h      (revision 22335)
+++ src/kits/interface/TextInput.h      (working copy)
@@ -62,6 +62,7 @@
                void                    AlignTextRect();
                void                    SetInitialText();
 
+virtual void                   ScrollToOffset(int32 nOffset);
 virtual        void                    Paste(BClipboard *clipboard);
 
 protected:
Index: src/kits/interface/TextView.cpp
===================================================================
--- src/kits/interface/TextView.cpp     (revision 22335)
+++ src/kits/interface/TextView.cpp     (working copy)
@@ -1929,24 +1929,36 @@
 void
 BTextView::ScrollToOffset(int32 inOffset)
 {
+       if (ScrollBar(B_HORIZONTAL) || ScrollBar(B_VERTICAL))
+               _ScrollToOffset(inOffset);
+}
+
+void
+BTextView::_ScrollToOffset(int32 inOffset)
+{
        BRect bounds = Bounds();
        float lineHeight = 0.0;
+       float xdiff = 0.0;
+       float ydiff = 0.0;
         BPoint point = PointAt(inOffset, &lineHeight);
 
-       // TODO: We should do the following, since otherwise the textview
-       // won't scroll unless it's attached to a scrollview.
-       /*if (!bounds.Contains(point))
-               ScrollTo(point); */
+       if (point.x < bounds.left) {
+               xdiff = -1 * (bounds.IntegerWidth() / 2);
+               // normalize scroll value to prevent scrolling past left 
boundary of view
+               if (bounds.left < fabs(xdiff))
+                       xdiff = -1 * bounds.left;
+       } else if (point.x >= bounds.right)
+               xdiff = bounds.IntegerWidth() / 2;
 
-       if (ScrollBar(B_HORIZONTAL) != NULL) {
-               if (point.x < bounds.left || point.x >= bounds.right)
-                       ScrollBar(B_HORIZONTAL)->SetValue(point.x - 
(bounds.IntegerWidth() / 2));
-       }
+       if (point.y < bounds.top) {
+               ydiff = -1 * (bounds.IntegerHeight() / 2);
+               // normalize scroll value to prevent scrolling past top of view
+               if (bounds.top < fabs(ydiff))
+                       ydiff = -1 * bounds.top;
+       } else if (point.y >= bounds.bottom)
+               ydiff = bounds.IntegerHeight() / 2;
 
-       if (ScrollBar(B_VERTICAL) != NULL) {
-               if (point.y < bounds.top || (point.y + lineHeight) >= 
bounds.bottom)
-                       ScrollBar(B_VERTICAL)->SetValue(point.y - 
(bounds.IntegerHeight() / 2));
-       }
+       ScrollBy(xdiff, ydiff);
 }
 
 
Index: src/kits/interface/TextInput.cpp
===================================================================
--- src/kits/interface/TextInput.cpp    (revision 22335)
+++ src/kits/interface/TextInput.cpp    (working copy)
@@ -100,6 +100,11 @@
        }
 }
 
+void
+_BTextInput_::ScrollToOffset(int32 nOffset)
+{
+       _ScrollToOffset(nOffset);
+}
 
 void
 _BTextInput_::MakeFocus(bool state)
Index: headers/os/interface/TextView.h
===================================================================
--- headers/os/interface/TextView.h     (revision 22335)
+++ headers/os/interface/TextView.h     (working copy)
@@ -255,6 +255,7 @@
                undo_state              UndoState(bool *isRedo) const;
 
 protected:
+void                   _ScrollToOffset(int32 nOffset);
 virtual        void                    GetDragParameters(BMessage      *drag, 
                                                                                
  BBitmap       **bitmap,
                                                                                
  BPoint        *point,

Other related posts: