ScrollWindowEx: Don't move the caret of child windows

Michael Kaufmann hallo at michael-kaufmann.ch
Sun Oct 30 09:14:59 CST 2005


This fixes a bug that's visible in the demo program of bug 2397: The 
caret moves outside the edit control when the window content gets scrolled.

Changelog:
ScrollWindowEx: Don't move the caret of child windows and hide it only 
if it's in the source or in the destination rectangle

-------------- next part --------------
Index: dlls/user/painting.c
===================================================================
RCS file: /home/wine/wine/dlls/user/painting.c,v
retrieving revision 1.33
diff -u -r1.33 painting.c
--- dlls/user/painting.c	3 Aug 2005 19:16:01 -0000	1.33
+++ dlls/user/painting.c	30 Oct 2005 14:03:07 -0000
@@ -346,30 +346,75 @@
 /*************************************************************************
  *             fix_caret
  *
- * Helper for ScrollWindowEx.
+ * Helper for ScrollWindowEx:
+ * If the return value is 0, no special caret handling is necessary.
+ * Otherwise the return value is the handle of the window that owns the
+ * caret. Its caret needs to be hidden during the scroll operation and
+ * moved to new_caret_pos if move_caret is TRUE.
  */
-static HWND fix_caret(HWND hWnd, LPRECT lprc, UINT flags)
+static HWND fix_caret(HWND hWnd, const LPRECT scroll_rect, INT dx, INT dy,
+                     UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
 {
     GUITHREADINFO info;
+    RECT rect, mapped_rcCaret;
+    BOOL hide_caret = FALSE;
 
     if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
     if (!info.hwndCaret) return 0;
-    if (info.hwndCaret == hWnd ||
-        ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret)))
+    
+    if (info.hwndCaret == hWnd)
     {
-        POINT pt;
-        pt.x = info.rcCaret.left;
-        pt.y = info.rcCaret.top;
-        MapWindowPoints( info.hwndCaret, hWnd, (LPPOINT)&info.rcCaret, 2 );
-        if( IntersectRect(lprc, lprc, &info.rcCaret) )
+        /* Move the caret if it's (partially) in the source rectangle */
+        if (IntersectRect(&rect, scroll_rect, &info.rcCaret))
+        {
+            *move_caret = TRUE;
+            hide_caret = TRUE;
+            new_caret_pos->x = info.rcCaret.left + dx;
+            new_caret_pos->y = info.rcCaret.top + dy;
+        }
+        else
+        {
+            *move_caret = FALSE;
+            
+            /* Hide the caret if it's in the destination rectangle */
+            rect = *scroll_rect;
+            OffsetRect(&rect, dx, dy);
+            hide_caret = IntersectRect(&rect, &rect, &info.rcCaret);
+        }
+    }
+    else
+    {
+        if ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret))
         {
-            HideCaret(0);
-            lprc->left = pt.x;
-            lprc->top = pt.y;
-            return info.hwndCaret;
+            *move_caret = FALSE;
+            
+            /* Hide the caret if it's in the source or in the destination
+               rectangle */
+            mapped_rcCaret = info.rcCaret;
+            MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
+            
+            if (IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
+            {
+                hide_caret = TRUE;
+            }
+            else
+            {
+                rect = *scroll_rect;
+                OffsetRect(&rect, dx, dy);
+                hide_caret = IntersectRect(&rect, &rect, &mapped_rcCaret);
+            }
         }
+        else
+            return 0;
+    }
+
+    if (hide_caret)
+    {    
+        HideCaret(info.hwndCaret);
+        return info.hwndCaret;
     }
-    return 0;
+    else
+        return 0;
 }
 
 
@@ -722,8 +767,9 @@
     HRGN  hrgnWinupd = 0;
     HDC   hDC;
     RECT  rc, cliprc;
-    RECT caretrc;
     HWND hwndCaret = NULL;
+    BOOL moveCaret = FALSE;
+    POINT newCaretPos;
 
     TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
            hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
@@ -749,8 +795,9 @@
     if( !IsRectEmpty(&cliprc) && (dx || dy)) {
         DWORD dcxflags = DCX_CACHE;
         DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
-        caretrc = rc;
-        hwndCaret = fix_caret(hwnd, &caretrc, flags);
+        
+        hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret,
+                              &newCaretPos);
 
         if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
         if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
@@ -825,7 +872,7 @@
     }
 
     if( hwndCaret ) {
-        SetCaretPos( caretrc.left + dx, caretrc.top + dy );
+        if ( moveCaret ) SetCaretPos( newCaretPos.x, newCaretPos.y );
         ShowCaret(hwndCaret);
     }
 


More information about the wine-patches mailing list