Get rid of a few pointless A/W conversions.
[wine] / windows / painting.c
index 9ddd9f1..8b631b6 100644 (file)
  * Window painting functions
  *
  * Copyright 1993, 1994, 1995 Alexandre Julliard
+ *                      1999 Alex Korobka
  *
- * FIXME: Do not repaint full nonclient area all the time. Instead, compute 
- *       intersection with hrgnUpdate (which should be moved from client to 
- *       window coords as well, lookup 'the pain' comment in the winpos.c).
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include "config.h"
+#include "wine/port.h"
+
+#include <stdarg.h>
+#include <string.h>
+#include "windef.h"
+#include "winbase.h"
+#include "wingdi.h"
+#include "wine/winuser16.h"
+#include "wownt32.h"
+#include "wine/unicode.h"
+#include "wine/server.h"
+#include "user.h"
 #include "win.h"
-#include "queue.h"
-#include "gdi.h"
+#include "message.h"
 #include "dce.h"
-#include "heap.h"
-#include "debug.h"
+#include "wine/debug.h"
 
-  /* Last CTLCOLOR id */
-#define CTLCOLOR_MAX   CTLCOLOR_STATIC
+WINE_DEFAULT_DEBUG_CHANNEL(win);
+WINE_DECLARE_DEBUG_CHANNEL(nonclient);
 
-/***********************************************************************
- *           WIN_UpdateNCArea
- *
- */
-void WIN_UpdateNCArea(WND* wnd, BOOL32 bUpdate)
-{
-    POINT16 pt = {0, 0}; 
-    HRGN32 hClip = 1;
+/* client rect in window coordinates */
 
-    TRACE(nonclient,"hwnd %04x, hrgnUpdate %04x\n", 
-                      wnd->hwndSelf, wnd->hrgnUpdate );
+#define GETCLIENTRECTW( wnd, r )       (r).left = (wnd)->rectClient.left - (wnd)->rectWindow.left; \
+                                       (r).top = (wnd)->rectClient.top - (wnd)->rectWindow.top; \
+                                       (r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
+                                       (r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
 
-    /* desktop window doesn't have nonclient area */
-    if(wnd == WIN_GetDesktop()) 
-    {
-        wnd->flags &= ~WIN_NEEDS_NCPAINT;
-        return;
-    }
+  /* PAINT_RedrawWindow() control flags */
+#define RDW_EX_DELAY_NCPAINT    0x0020
 
-    if( wnd->hrgnUpdate > 1 )
-    {
-       ClientToScreen16(wnd->hwndSelf, &pt);
+  /* WIN_UpdateNCRgn() flags */
+#define UNC_CHECK              0x0001
+#define UNC_ENTIRE             0x0002
+#define UNC_REGION             0x0004
+#define UNC_UPDATE             0x0008
+#define UNC_DELAY_NCPAINT       0x0010
+#define UNC_IN_BEGINPAINT       0x0020
 
-        hClip = CreateRectRgn32( 0, 0, 0, 0 );
-        if (!CombineRgn32( hClip, wnd->hrgnUpdate, 0, RGN_COPY ))
-        {
-            DeleteObject32(hClip);
-            hClip = 1;
-        }
-       else
-           OffsetRgn32( hClip, pt.x, pt.y );
+  /* Last COLOR id */
+#define COLOR_MAX   COLOR_GRADIENTINACTIVECAPTION
 
-        if (bUpdate)
-        {
-           /* exclude non-client area from update region */
-            HRGN32 hrgn = CreateRectRgn32( 0, 0,
-                                 wnd->rectClient.right - wnd->rectClient.left,
-                                 wnd->rectClient.bottom - wnd->rectClient.top);
+HPALETTE (WINAPI *pfnGDISelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = NULL;
+UINT (WINAPI *pfnGDIRealizePalette)(HDC hdc) = NULL;
 
-            if (hrgn && (CombineRgn32( wnd->hrgnUpdate, wnd->hrgnUpdate,
-                                       hrgn, RGN_AND) == NULLREGION))
-            {
-                DeleteObject32( wnd->hrgnUpdate );
-                wnd->hrgnUpdate = 1;
-            }
-
-            DeleteObject32( hrgn );
-        }
-    }
 
-    wnd->flags &= ~WIN_NEEDS_NCPAINT;
-
-    if ((wnd->hwndSelf == GetActiveWindow32()) &&
-        !(wnd->flags & WIN_NCACTIVATED))
+/***********************************************************************
+ *           add_paint_count
+ *
+ * Add an increment (normally 1 or -1) to the current paint count of a window.
+ */
+static void add_paint_count( HWND hwnd, int incr )
+{
+    SERVER_START_REQ( inc_window_paint_count )
     {
-        wnd->flags |= WIN_NCACTIVATED;
-        if( hClip > 1) DeleteObject32( hClip );
-        hClip = 1;
+        req->handle = hwnd;
+        req->incr   = incr;
+        wine_server_call( req );
     }
-
-    if (hClip) SendMessage16( wnd->hwndSelf, WM_NCPAINT, hClip, 0L );
-
-    if (hClip > 1) DeleteObject32( hClip );
+    SERVER_END_REQ;
 }
 
 
 /***********************************************************************
- *           BeginPaint16    (USER.39)
+ *           crop_rgn
+ *
+ * hSrc:       Region to crop.
+ * lpRect:     Clipping rectangle.
+ *
+ * hDst: Region to hold the result (a new region is created if it's 0).
+ *       Allowed to be the same region as hSrc in which case everything
+ *      will be done in place, with no memory reallocations.
+ *
+ * Returns: hDst if success, 0 otherwise.
  */
-HDC16 WINAPI BeginPaint16( HWND16 hwnd, LPPAINTSTRUCT16 lps ) 
+static HRGN crop_rgn( HRGN hDst, HRGN hSrc, const RECT *rect )
 {
-    BOOL32 bIcon;
-    HRGN32 hrgnUpdate;
-    WND *wndPtr = WIN_FindWndPtr( hwnd );
-    if (!wndPtr) return 0;
-
-    bIcon = (wndPtr->dwStyle & WS_MINIMIZE && wndPtr->class->hIcon);
-
-    wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
-
-    if (wndPtr->flags & WIN_NEEDS_NCPAINT) WIN_UpdateNCArea( wndPtr, TRUE );
+    HRGN h = CreateRectRgnIndirect( rect );
+    if (hDst == 0) hDst = h;
+    CombineRgn( hDst, hSrc, h, RGN_AND );
+    if (hDst != h) DeleteObject( h );
+    return hDst;
+}
 
-    if (((hrgnUpdate = wndPtr->hrgnUpdate) != 0) ||
-        (wndPtr->flags & WIN_INTERNAL_PAINT))
-        QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
 
-    wndPtr->hrgnUpdate = 0;
-    wndPtr->flags &= ~WIN_INTERNAL_PAINT;
+/***********************************************************************
+ *           WIN_HaveToDelayNCPAINT
+ *
+ * Currently, in the Wine painting mechanism, the WM_NCPAINT message
+ * is generated as soon as a region intersecting the non-client area
+ * of a window is invalidated.
+ *
+ * This technique will work fine for all windows whose parents
+ * have the WS_CLIPCHILDREN style. When the parents have that style,
+ * they are not going to override the contents of their children.
+ * However, when the parent doesn't have that style, Windows relies
+ * on a "painter's algorithm" to display the contents of the windows.
+ * That is, windows are painted from back to front. This includes the
+ * non-client area.
+ *
+ * This method looks at the current state of a window to determine
+ * if the sending of the WM_NCPAINT message should be delayed until
+ * the BeginPaint call.
+ *
+ * PARAMS:
+ *   wndPtr   - A Locked window pointer to the window we're
+ *              examining.
+ *   uncFlags - This is the flag passed to the WIN_UpdateNCRgn
+ *              function. This is a shortcut for the cases when
+ *              we already know when to avoid scanning all the
+ *              parents of a window. If you already know that this
+ *              window's NCPAINT should be delayed, set the
+ *              UNC_DELAY_NCPAINT flag for this parameter.
+ *
+ *              This shortcut behavior is implemented in the
+ *              RDW_Paint() method.
+ *
+ */
+static BOOL WIN_HaveToDelayNCPAINT( HWND hwnd, UINT uncFlags)
+{
+  /*
+   * Test the shortcut first. (duh)
+   */
+  if (uncFlags & UNC_DELAY_NCPAINT)
+    return TRUE;
 
-    HideCaret32( hwnd );
+  /*
+   * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
+   * method only. This is another shortcut to avoid going
+   * up the parent's chain of the window to finally
+   * figure-out that none of them has an invalid region.
+   */
+  if (uncFlags & UNC_IN_BEGINPAINT)
+    return FALSE;
+
+  /*
+   * Scan all the parents of this window to find a window
+   * that doesn't have the WS_CLIPCHILDREN style and that
+   * has an invalid region.
+   */
+  while ((hwnd = GetAncestor( hwnd, GA_PARENT )))
+  {
+      WND* parentWnd = WIN_FindWndPtr( hwnd );
+      if (parentWnd && !(parentWnd->dwStyle & WS_CLIPCHILDREN) && parentWnd->hrgnUpdate)
+      {
+          WIN_ReleaseWndPtr( parentWnd );
+          return TRUE;
+      }
+      WIN_ReleaseWndPtr( parentWnd );
+  }
+  return FALSE;
+}
 
-    TRACE(win,"hrgnUpdate = %04x, \n", hrgnUpdate);
+/***********************************************************************
+ *           WIN_UpdateNCRgn
+ *
+ *  Things to do:
+ *     Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
+ *     Crop hrgnUpdate to a client rect, especially if it 1.
+ *     If UNC_REGION is set return update region for the client rect.
+ *
+ *  NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
+ *       The trick is that when the returned region handle may be different from hRgn.
+ *       In this case the old hRgn must be considered gone. BUT, if the returned value
+ *       is 1 then the hRgn is preserved and RDW_Paint() will have to get
+ *       a DC without extra clipping region.
+ */
+static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
+{
+    RECT  r;
+    HRGN  hClip = 0;
+    HRGN  hrgnRet = 0;
 
-    /* When bIcon is TRUE hrgnUpdate is automatically in window coordinates
-     * (because rectClient == rectWindow for WS_MINIMIZE windows).
-     */
+    TRACE_(nonclient)("hwnd %p [%p] hrgn %p, unc %04x, ncf %i\n",
+                      wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
 
-    if (wndPtr->class->style & CS_PARENTDC)
-    {
-        /* Don't clip the output to the update region for CS_PARENTDC window */
-       if(hrgnUpdate > 1)
-           DeleteObject32(hrgnUpdate);
-        lps->hdc = GetDCEx16( hwnd, 0, DCX_WINDOWPAINT | DCX_USESTYLE |
-                              (bIcon ? DCX_WINDOW : 0) );
-    }
-    else
+    /* desktop window doesn't have a nonclient area */
+    if(wnd->hwndSelf == GetDesktopWindow())
     {
-        lps->hdc = GetDCEx16(hwnd, hrgnUpdate, DCX_INTERSECTRGN |
-                             DCX_WINDOWPAINT | DCX_USESTYLE |
-                             (bIcon ? DCX_WINDOW : 0) );
+        wnd->flags &= ~WIN_NEEDS_NCPAINT;
+       if( wnd->hrgnUpdate > (HRGN)1 )
+        {
+            if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
+            CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
+            hrgnRet = hRgn;
+        }
+       else
+       {
+           hrgnRet = wnd->hrgnUpdate;
+       }
+        return hrgnRet;
     }
 
-    TRACE(win,"hdc = %04x\n", lps->hdc);
-
-    if (!lps->hdc)
+    if ((wnd->hwndSelf == GetForegroundWindow()) &&
+        !(wnd->flags & WIN_NCACTIVATED) )
     {
-        WARN(win, "GetDCEx() failed in BeginPaint(), hwnd=%04x\n", hwnd);
-        return 0;
+       wnd->flags |= WIN_NCACTIVATED;
+       uncFlags |= UNC_ENTIRE;
     }
 
-    GetClipBox16( lps->hdc, &lps->rcPaint );
-
-TRACE(win,"box = (%i,%i - %i,%i)\n", lps->rcPaint.left, lps->rcPaint.top,
-                   lps->rcPaint.right, lps->rcPaint.bottom );
-
-    if (wndPtr->flags & WIN_NEEDS_ERASEBKGND)
+    /*
+     * If the window's non-client area needs to be painted,
+     */
+    if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
+        !WIN_HaveToDelayNCPAINT(wnd->hwndSelf, uncFlags) )
     {
-        wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
-        lps->fErase = !SendMessage16(hwnd, (bIcon) ? WM_ICONERASEBKGND
-                                                   : WM_ERASEBKGND,
-                                     (WPARAM16)lps->hdc, 0 );
-    }
-    else lps->fErase = TRUE;
-
-    return lps->hdc;
-}
+           RECT r2, r3;
 
+           wnd->flags &= ~WIN_NEEDS_NCPAINT;
+           GETCLIENTRECTW( wnd, r );
 
-/***********************************************************************
- *           BeginPaint32    (USER32.10)
- */
-HDC32 WINAPI BeginPaint32( HWND32 hwnd, PAINTSTRUCT32 *lps )
-{
-    PAINTSTRUCT16 ps;
+           TRACE_(nonclient)( "\tclient box (%ld,%ld-%ld,%ld), hrgnUpdate %p\n",
+                               r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
+           if( wnd->hrgnUpdate > (HRGN)1 )
+           {
+               /* Check if update rgn overlaps with nonclient area */
 
-    BeginPaint16( hwnd, &ps );
-    lps->hdc            = (HDC32)ps.hdc;
-    lps->fErase         = ps.fErase;
-    lps->rcPaint.top    = ps.rcPaint.top;
-    lps->rcPaint.left   = ps.rcPaint.left;
-    lps->rcPaint.right  = ps.rcPaint.right;
-    lps->rcPaint.bottom = ps.rcPaint.bottom;
-    lps->fRestore       = ps.fRestore;
-    lps->fIncUpdate     = ps.fIncUpdate;
-    return lps->hdc;
-}
+               GetRgnBox( wnd->hrgnUpdate, &r2 );
+               UnionRect( &r3, &r2, &r );
+               if( r3.left != r.left || r3.top != r.top ||
+                   r3.right != r.right || r3.bottom != r.bottom ) /* it does */
+               {
+                   /* crop hrgnUpdate, save old one in hClip - the only
+                    * case that places a valid region handle in hClip */
 
+                   hClip = wnd->hrgnUpdate;
+                   wnd->hrgnUpdate = crop_rgn( hRgn, hClip, &r );
+                   if( uncFlags & UNC_REGION ) hrgnRet = hClip;
+               }
 
-/***********************************************************************
- *           EndPaint16    (USER.40)
- */
-BOOL16 WINAPI EndPaint16( HWND16 hwnd, const PAINTSTRUCT16* lps )
-{
-    ReleaseDC16( hwnd, lps->hdc );
-    ShowCaret32( hwnd );
-    return TRUE;
-}
+               if( uncFlags & UNC_CHECK )
+               {
+                   GetRgnBox( wnd->hrgnUpdate, &r3 );
+                   if( IsRectEmpty( &r3 ) )
+                   {
+                       /* delete the update region since all invalid
+                        * parts were in the nonclient area */
 
+                       DeleteObject( wnd->hrgnUpdate );
+                       wnd->hrgnUpdate = 0;
+                       if(!(wnd->flags & WIN_INTERNAL_PAINT))
+                           add_paint_count( wnd->hwndSelf, -1 );
 
-/***********************************************************************
- *           EndPaint32    (USER32.176)
- */
-BOOL32 WINAPI EndPaint32( HWND32 hwnd, const PAINTSTRUCT32 *lps )
-{
-    ReleaseDC32( hwnd, lps->hdc );
-    ShowCaret32( hwnd );
-    return TRUE;
-}
+                       wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
+                   }
+               }
 
+               if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
+           }
+           else
+           if( wnd->hrgnUpdate == (HRGN)1 )/* entire window */
+           {
+               if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
+               if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
+               uncFlags |= UNC_ENTIRE;
+           }
+    }
+    else /* no WM_NCPAINT unless forced */
+    {
+       if( wnd->hrgnUpdate >  (HRGN)1 )
+       {
+copyrgn:
+           if( uncFlags & UNC_REGION )
+            {
+                if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
+                CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
+                hrgnRet = hRgn;
+            }
+       }
+       else
+       if( wnd->hrgnUpdate == (HRGN)1 && (uncFlags & UNC_UPDATE) )
+       {
+           GETCLIENTRECTW( wnd, r );
+           wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
+           if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
+       }
+    }
 
-/***********************************************************************
- *           FillWindow    (USER.324)
- */
-void WINAPI FillWindow( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc, HBRUSH16 hbrush )
-{
-    RECT16 rect;
-    GetClientRect16( hwnd, &rect );
-    DPtoLP16( hdc, (LPPOINT16)&rect, 2 );
-    PaintRect( hwndParent, hwnd, hdc, hbrush, &rect );
-}
+    if(!hClip && (uncFlags & UNC_ENTIRE) )
+    {
+       /* still don't do anything if there is no nonclient area */
+       hClip = (HRGN)(memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
+    }
 
+    if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
+    {
+        if ( hClip == hrgnRet && hrgnRet > (HRGN)1 ) {
+           hClip = CreateRectRgn( 0, 0, 0, 0 );
+           CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
+       }
 
-/***********************************************************************
- *          PAINT_GetControlBrush
- */
-static HBRUSH16 PAINT_GetControlBrush( HWND32 hParent, HWND32 hWnd, HDC16 hDC, UINT16 ctlType )
-{
-    HBRUSH16 bkgBrush = (HBRUSH16)SendMessage32A( hParent, WM_CTLCOLORMSGBOX + ctlType, 
-                                                            (WPARAM32)hDC, (LPARAM)hWnd );
-    if( !IsGDIObject(bkgBrush) )
-       bkgBrush = DEFWND_ControlColor( hDC, ctlType );
-    return bkgBrush;
-}
+       SendMessageA( wnd->hwndSelf, WM_NCPAINT, (WPARAM)hClip, 0L );
+       if( (hClip > (HRGN)1) && (hClip != hRgn) && (hClip != hrgnRet) )
+           DeleteObject( hClip );
+       /*
+         * Since all Window locks are suspended while processing the WM_NCPAINT
+         * we want to make sure the window still exists before continuing.
+        */
+        if (!IsWindow(wnd->hwndSelf))
+        {
+         DeleteObject(hrgnRet);
+         hrgnRet=0;
+        }
+    }
 
+    TRACE_(nonclient)("returning %p (hClip = %p, hrgnUpdate = %p)\n", hrgnRet, hClip, wnd->hrgnUpdate );
 
-/***********************************************************************
- *           PaintRect    (USER.325)
- */
-void WINAPI PaintRect( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc,
-                       HBRUSH16 hbrush, const RECT16 *rect)
-{
-    if( hbrush <= CTLCOLOR_MAX ) 
-       if( hwndParent )
-           hbrush = PAINT_GetControlBrush( hwndParent, hwnd, hdc, (UINT16)hbrush );
-       else 
-           return;
-    if( hbrush ) 
-       FillRect16( hdc, rect, hbrush );
+    return hrgnRet;
 }
 
 
 /***********************************************************************
- *           GetControlBrush    (USER.326)
+ *             RDW_ValidateParent [RDW_UpdateRgns() helper]
+ *
+ *  Validate the portions of parents that are covered by a validated child
+ *  wndPtr = child
  */
-HBRUSH16 WINAPI GetControlBrush( HWND16 hwnd, HDC16 hdc, UINT16 ctlType )
+static void RDW_ValidateParent(WND *wndChild)
 {
-    WND* wndPtr = WIN_FindWndPtr( hwnd );
-
-    if((ctlType <= CTLCOLOR_MAX) && wndPtr )
+    HWND parent;
+    HRGN hrg;
+
+    if (wndChild->hrgnUpdate == (HRGN)1 ) {
+        RECT r;
+        r.left = 0;
+        r.top = 0;
+        r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
+        r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
+        hrg = CreateRectRgnIndirect( &r );
+    } else
+        hrg = wndChild->hrgnUpdate;
+
+    parent = GetAncestor( wndChild->hwndSelf, GA_PARENT );
+    while (parent && parent != GetDesktopWindow())
     {
-       WND* parent;
-       if( wndPtr->dwStyle & WS_POPUP ) parent = wndPtr->owner;
-       else parent = wndPtr->parent;
-       if( !parent ) parent = wndPtr;
-       return (HBRUSH16)PAINT_GetControlBrush( parent->hwndSelf, hwnd, hdc, ctlType );
+        WND *wndParent = WIN_FindWndPtr( parent );
+        if (wndParent && !(wndParent->dwStyle & WS_CLIPCHILDREN))
+        {
+            if (wndParent->hrgnUpdate != 0)
+            {
+                POINT ptOffset;
+                RECT rect, rectParent;
+                if( wndParent->hrgnUpdate == (HRGN)1 )
+                {
+                   RECT r;
+
+                   r.left = 0;
+                   r.top = 0;
+                   r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
+                   r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
+
+                   wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
+                }
+                /* we must offset the child region by the offset of the child rect in the parent */
+                GetWindowRect(wndParent->hwndSelf, &rectParent);
+                GetWindowRect(wndChild->hwndSelf, &rect);
+                ptOffset.x = rect.left - rectParent.left;
+                ptOffset.y = rect.top - rectParent.top;
+                OffsetRgn( hrg, ptOffset.x, ptOffset.y );
+                if (CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF ) == NULLREGION)
+                {
+                    /* the update region has become empty */
+                    DeleteObject( wndParent->hrgnUpdate );
+                    wndParent->hrgnUpdate = 0;
+                    wndParent->flags &= ~WIN_NEEDS_ERASEBKGND;
+                    if( !(wndParent->flags & WIN_INTERNAL_PAINT) )
+                        add_paint_count( wndParent->hwndSelf, -1 );
+                }
+                OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
+            }
+        }
+        WIN_ReleaseWndPtr( wndParent );
+        parent = GetAncestor( parent, GA_PARENT );
     }
-    return (HBRUSH16)0;
+    if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
 }
 
-
 /***********************************************************************
- *           PAINT_RedrawWindow
+ *             RDW_UpdateRgns [RedrawWindow() helper]
  *
- * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
- * SendMessage() calls. This is a comment inside DefWindowProc() source 
- * from 16-bit SDK:
- *
- *   This message avoids lots of inter-app message traffic
- *   by switching to the other task and continuing the
- *   recursion there.
- * 
- * wParam         = flags
- * LOWORD(lParam) = hrgnClip
- * HIWORD(lParam) = hwndSkip  (not used; always NULL)
+ *  Walks the window tree and adds/removes parts of the hRgn to/from update
+ *  regions of windows that overlap it. Also, manages internal paint flags.
  *
- * All in all, a prime candidate for a rewrite.
+ *  NOTE: Walks the window tree so the caller must lock it.
+ *       MUST preserve hRgn (can modify but then has to restore).
  */
-BOOL32 PAINT_RedrawWindow( HWND32 hwnd, const RECT32 *rectUpdate,
-                           HRGN32 hrgnUpdate, UINT32 flags, UINT32 control )
+static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
 {
-    BOOL32 bIcon;
-    HRGN32 hrgn;
-    RECT32 rectClient;
-    WND* wndPtr;
-    WND **list, **ppWnd;
+    /*
+     * Called only when one of the following is set:
+     * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
+     */
 
-    if (!hwnd) hwnd = GetDesktopWindow32();
-    if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
-    if (!WIN_IsWindowDrawable( wndPtr, !(flags & RDW_FRAME) ) )
-        return TRUE;  /* No redraw needed */
+    BOOL bHadOne =  wndPtr->hrgnUpdate && hRgn;
+    BOOL bChildren =  (!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
+                       ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
+    RECT r;
+
+    r.left = 0;
+    r.top = 0;
+    r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
+    r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
+
+    TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
 
-    bIcon = (wndPtr->dwStyle & WS_MINIMIZE && wndPtr->class->hIcon);
-    if (rectUpdate)
+    if( flags & RDW_INVALIDATE )
     {
-        TRACE(win, "%04x %d,%d-%d,%d %04x flags=%04x\n",
-                    hwnd, rectUpdate->left, rectUpdate->top,
-                    rectUpdate->right, rectUpdate->bottom, hrgnUpdate, flags );
+       if( hRgn > (HRGN)1 )
+       {
+           switch ((UINT)wndPtr->hrgnUpdate)
+           {
+               default:
+                       CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
+                       /* fall through */
+               case 0:
+                       wndPtr->hrgnUpdate = crop_rgn( wndPtr->hrgnUpdate,
+                                                       wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
+                                                       &r );
+                       if( !bHadOne )
+                       {
+                           GetRgnBox( wndPtr->hrgnUpdate, &r );
+                           if( IsRectEmpty( &r ) )
+                           {
+                               DeleteObject( wndPtr->hrgnUpdate );
+                               wndPtr->hrgnUpdate = 0;
+                               goto end;
+                           }
+                       }
+                       break;
+               case 1: /* already an entire window */
+                       break;
+           }
+       }
+       else if( hRgn == (HRGN)1 )
+       {
+           if( wndPtr->hrgnUpdate > (HRGN)1 )
+               DeleteObject( wndPtr->hrgnUpdate );
+           wndPtr->hrgnUpdate = (HRGN)1;
+       }
+       else
+        {
+            /* hRgn is zero */
+            if( wndPtr->hrgnUpdate > (HRGN)1)
+            {
+                GetRgnBox( wndPtr->hrgnUpdate, &r );
+                if( IsRectEmpty( &r ) )
+                {
+                    DeleteObject( wndPtr->hrgnUpdate );
+                    wndPtr->hrgnUpdate = 0;
+                    goto end;
+                }
+            }
+            hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends
+                                         * on code in RDW_Paint() */
+        }
+
+       if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
+            add_paint_count( wndPtr->hwndSelf, 1 );
+
+       if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
+       if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
+       flags    |= RDW_FRAME;
     }
-    else
+    else if( flags & RDW_VALIDATE )
     {
-        TRACE(win, "%04x NULL %04x flags=%04x\n", hwnd, hrgnUpdate, flags);
+       if( wndPtr->hrgnUpdate )
+       {
+           if( hRgn > (HRGN)1 )
+           {
+               if( wndPtr->hrgnUpdate == (HRGN)1 )
+                   wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
+
+               if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
+                   == NULLREGION )
+                {
+                    DeleteObject( wndPtr->hrgnUpdate );
+                    wndPtr->hrgnUpdate = 0;
+                }
+           }
+           else /* validate everything */
+           {
+               if( wndPtr->hrgnUpdate > (HRGN)1 ) DeleteObject( wndPtr->hrgnUpdate );
+               wndPtr->hrgnUpdate = 0;
+           }
+
+           if( !wndPtr->hrgnUpdate )
+           {
+               wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
+               if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
+                    add_paint_count( wndPtr->hwndSelf, -1 );
+           }
+       }
+
+       if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
+       if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
+
     }
 
-    GetClientRect32( hwnd, &rectClient );
+    if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
+        RDW_ValidateParent(wndPtr); /* validate parent covered by region */
 
-    if (flags & RDW_INVALIDATE)  /* Invalidate */
+    /* in/validate child windows that intersect with the region if it
+     * is a valid handle. */
+
+    if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
     {
-        int rgnNotEmpty = COMPLEXREGION;
+        HWND *list;
+       if( hRgn > (HRGN)1 && bChildren && (list = WIN_ListChildren( wndPtr->hwndSelf )))
+       {
+           POINT ptTotal, prevOrigin = {0,0};
+            POINT ptClient;
+            INT i;
 
-        if (wndPtr->hrgnUpdate > 1)  /* Is there already an update region? */
-        {
-            if ((hrgn = hrgnUpdate) == 0)
-                hrgn = CreateRectRgnIndirect32( rectUpdate ? rectUpdate :
-                                                &rectClient );
-            rgnNotEmpty = CombineRgn32( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate,
-                                        hrgn, RGN_OR );
-            if (!hrgnUpdate) DeleteObject32( hrgn );
-        }
-        else  /* No update region yet */
-        {
-            if (!(wndPtr->flags & WIN_INTERNAL_PAINT))
-                QUEUE_IncPaintCount( wndPtr->hmemTaskQ );
-            if (hrgnUpdate)
+            ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
+            ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
+
+            for(i = ptTotal.x = ptTotal.y = 0; list[i]; i++)
             {
-                wndPtr->hrgnUpdate = CreateRectRgn32( 0, 0, 0, 0 );
-                rgnNotEmpty = CombineRgn32( wndPtr->hrgnUpdate, hrgnUpdate,
-                                            0, RGN_COPY );
+                WND *wnd = WIN_FindWndPtr( list[i] );
+                if (!wnd) continue;
+                if( wnd->dwStyle & WS_VISIBLE )
+                {
+                    POINT ptOffset;
+
+                    r.left = wnd->rectWindow.left + ptClient.x;
+                    r.right = wnd->rectWindow.right + ptClient.x;
+                    r.top = wnd->rectWindow.top + ptClient.y;
+                    r.bottom = wnd->rectWindow.bottom + ptClient.y;
+
+                    ptOffset.x = r.left - prevOrigin.x;
+                    ptOffset.y = r.top - prevOrigin.y;
+                    OffsetRect( &r, -ptTotal.x, -ptTotal.y );
+
+                    if( RectInRegion( hRgn, &r ) )
+                    {
+                        OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
+                        RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
+                        prevOrigin.x = r.left + ptTotal.x;
+                        prevOrigin.y = r.top + ptTotal.y;
+                        ptTotal.x += ptOffset.x;
+                        ptTotal.y += ptOffset.y;
+                    }
+                }
+                WIN_ReleaseWndPtr( wnd );
             }
-            else wndPtr->hrgnUpdate = CreateRectRgnIndirect32( rectUpdate ?
-                                                    rectUpdate : &rectClient );
-        }
-       
-        if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
+            HeapFree( GetProcessHeap(), 0, list );
+            OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
+            bChildren = 0;
+       }
+    }
 
-        /* restrict update region to client area (FIXME: correct?) */
-        if (wndPtr->hrgnUpdate)
-        {
-            HRGN32 clientRgn = CreateRectRgnIndirect32( &rectClient );
-            rgnNotEmpty = CombineRgn32( wndPtr->hrgnUpdate, clientRgn, 
-                                        wndPtr->hrgnUpdate, RGN_AND );
-            DeleteObject32( clientRgn );
-        }
+    /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
 
-       /* check for bogus update region */ 
-       if ( rgnNotEmpty == NULLREGION )
-          {
-            wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
-            DeleteObject32( wndPtr->hrgnUpdate );
-            wndPtr->hrgnUpdate=0;
-             if (!(wndPtr->flags & WIN_INTERNAL_PAINT))
-                   QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
-          }
-       else
-             if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
-       flags |= RDW_FRAME;  /* Force children frame invalidation */
-    }
-    else if (flags & RDW_VALIDATE)  /* Validate */
+    if( bChildren )
     {
-          /* We need an update region in order to validate anything */
-        if (wndPtr->hrgnUpdate > 1)
+        HWND *list;
+        if ((list = WIN_ListChildren( wndPtr->hwndSelf )))
         {
-            if (!hrgnUpdate && !rectUpdate)
-            {
-                  /* Special case: validate everything */
-                DeleteObject32( wndPtr->hrgnUpdate );
-                wndPtr->hrgnUpdate = 0;
-            }
-            else
+            INT i;
+            for (i = 0; list[i]; i++)
             {
-                if ((hrgn = hrgnUpdate) == 0)
-                    hrgn = CreateRectRgnIndirect32( rectUpdate );
-                if (CombineRgn32( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate,
-                                  hrgn, RGN_DIFF ) == NULLREGION)
-                {
-                    DeleteObject32( wndPtr->hrgnUpdate );
-                    wndPtr->hrgnUpdate = 0;
-                }
-                if (!hrgnUpdate) DeleteObject32( hrgn );
+                WND *wnd = WIN_FindWndPtr( list[i] );
+                if (!wnd) continue;
+                if( wnd->dwStyle & WS_VISIBLE )
+                    RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
+                WIN_ReleaseWndPtr( wnd );
             }
-            if (!wndPtr->hrgnUpdate)  /* No more update region */
-               if (!(wndPtr->flags & WIN_INTERNAL_PAINT))
-                   QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
+            HeapFree( GetProcessHeap(), 0, list );
         }
-        if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
-       if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
     }
 
-      /* Set/clear internal paint flag */
+end:
+
+    /* Set/clear internal paint flag */
 
     if (flags & RDW_INTERNALPAINT)
     {
-       if ( wndPtr->hrgnUpdate <= 1 && !(wndPtr->flags & WIN_INTERNAL_PAINT))
-           QUEUE_IncPaintCount( wndPtr->hmemTaskQ );
-       wndPtr->flags |= WIN_INTERNAL_PAINT;        
+        if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
+            add_paint_count( wndPtr->hwndSelf, 1 );
+        wndPtr->flags |= WIN_INTERNAL_PAINT;
     }
     else if (flags & RDW_NOINTERNALPAINT)
     {
-       if ( wndPtr->hrgnUpdate <= 1 && (wndPtr->flags & WIN_INTERNAL_PAINT))
-           QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
-       wndPtr->flags &= ~WIN_INTERNAL_PAINT;
+        if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
+            add_paint_count( wndPtr->hwndSelf, -1 );
+        wndPtr->flags &= ~WIN_INTERNAL_PAINT;
     }
+}
+
+/***********************************************************************
+ *           RDW_Paint [RedrawWindow() helper]
+ *
+ * Walks the window tree and paints/erases windows that have
+ * nonzero update regions according to redraw flags. hrgn is a scratch
+ * region passed down during recursion. Must not be 1.
+ *
+ */
+static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
+{
+/* NOTE: wndPtr is locked by caller.
+ *
+ * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
+ * SendMessage() calls. This is a comment inside DefWindowProc() source
+ * from 16-bit SDK:
+ *
+ *   This message avoids lots of inter-app message traffic
+ *   by switching to the other task and continuing the
+ *   recursion there.
+ *
+ * wParam         = flags
+ * LOWORD(lParam) = hrgnClip
+ * HIWORD(lParam) = hwndSkip  (not used; always NULL)
+ *
+ */
+    HDC  hDC;
+    HWND hWnd = wndPtr->hwndSelf;
+    BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
+
+      /* Erase/update the window itself ... */
 
-      /* Erase/update window */
+    TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
+
+    /*
+     * Check if this window should delay it's processing of WM_NCPAINT.
+     * See WIN_HaveToDelayNCPAINT for a description of the mechanism
+     */
+    if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr->hwndSelf, 0) )
+       ex |= RDW_EX_DELAY_NCPAINT;
 
     if (flags & RDW_UPDATENOW)
     {
         if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
-            SendMessage16( hwnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
+            SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
     }
     else if (flags & RDW_ERASENOW)
     {
-        if (wndPtr->flags & WIN_NEEDS_NCPAINT)
-           WIN_UpdateNCArea( wndPtr, FALSE);
+       UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
+       HRGN hrgnRet;
 
-        if (wndPtr->flags & WIN_NEEDS_ERASEBKGND)
-        {
-            HDC32 hdc = GetDCEx32( hwnd, wndPtr->hrgnUpdate,
-                                   DCX_INTERSECTRGN | DCX_USESTYLE |
-                                   DCX_KEEPCLIPRGN | DCX_WINDOWPAINT |
-                                   (bIcon ? DCX_WINDOW : 0) );
-            if (hdc)
-            {
-               if (SendMessage16( hwnd, (bIcon) ? WM_ICONERASEBKGND
-                                               : WM_ERASEBKGND,
-                                  (WPARAM16)hdc, 0 ))
-                  wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
-               ReleaseDC32( hwnd, hdc );
+       hrgnRet = WIN_UpdateNCRgn(wndPtr,
+                                 hrgn,
+                                 UNC_REGION | UNC_CHECK |
+                                 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
+
+        if( hrgnRet )
+       {
+           if( hrgnRet > (HRGN)1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
+           if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
+           {
+               if( bIcon ) dcx |= DCX_WINDOW;
+               else
+               if( hrgnRet )
+                   OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
+                                       wndPtr->rectWindow.top  - wndPtr->rectClient.top);
+               else
+                   dcx &= ~DCX_INTERSECTRGN;
+               if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
+               {
+                   if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
+                                      (WPARAM)hDC, 0 ))
+                        wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
+                   ReleaseDC( hWnd, hDC );
+               }
             }
         }
     }
 
-      /* Recursively process children */
+    if( !IsWindow(hWnd) ) return hrgn;
 
-    if (!(flags & RDW_NOCHILDREN) &&
-        ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) &&
-       !(wndPtr->dwStyle & WS_MINIMIZE) )
+      /* ... and its child windows */
+
+    if(!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
+       ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
     {
-        if ( hrgnUpdate || rectUpdate )
+        HWND *list, *phwnd;
+
+       if( (list = WIN_ListChildren( wndPtr->hwndSelf )) )
        {
-          if (!(hrgn = CreateRectRgn32( 0, 0, 0, 0 ))) return TRUE;
-          if( !hrgnUpdate )
-           {
-               control |= (RDW_C_DELETEHRGN | RDW_C_USEHRGN);
-               if( !(hrgnUpdate = CreateRectRgnIndirect32( rectUpdate )) )
-                {
-                    DeleteObject32( hrgn );
-                    return TRUE;
-                }
-           }
-           if( (list = WIN_BuildWinArray( wndPtr, 0, NULL )) )
-          {
-               for (ppWnd = list; *ppWnd; ppWnd++)
-               {
-                   wndPtr = *ppWnd;
-                   if (!IsWindow32(wndPtr->hwndSelf)) continue;
-                   if (wndPtr->dwStyle & WS_VISIBLE)
-                   {
-                       SetRectRgn32( hrgn, 
-                               wndPtr->rectWindow.left, wndPtr->rectWindow.top, 
-                               wndPtr->rectWindow.right, wndPtr->rectWindow.bottom );
-                       if (CombineRgn32( hrgn, hrgn, hrgnUpdate, RGN_AND ))
-                       {
-                           OffsetRgn32( hrgn, -wndPtr->rectClient.left,
-                                        -wndPtr->rectClient.top );
-                           PAINT_RedrawWindow( wndPtr->hwndSelf, NULL, hrgn, flags,
-                                         RDW_C_USEHRGN );
-                       }
-                   }
-               }
-               HeapFree( SystemHeap, 0, list );
-          }
-          DeleteObject32( hrgn );
-          if (control & RDW_C_DELETEHRGN) DeleteObject32( hrgnUpdate );
-       }
-        else
-        {
-           if( (list = WIN_BuildWinArray( wndPtr, 0, NULL )) )
+           for (phwnd = list; *phwnd; phwnd++)
            {
-               for (ppWnd = list; *ppWnd; ppWnd++)
-               {
-                   wndPtr = *ppWnd;
-                   if (IsWindow32( wndPtr->hwndSelf ))
-                       PAINT_RedrawWindow( wndPtr->hwndSelf, NULL, 0, flags, 0 );
-               }
-               HeapFree( SystemHeap, 0, list );
+                if (!(wndPtr = WIN_FindWndPtr( *phwnd ))) continue;
+                if ( (wndPtr->dwStyle & WS_VISIBLE) &&
+                     (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
+                    hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
+                WIN_ReleaseWndPtr(wndPtr);
            }
+            HeapFree( GetProcessHeap(), 0, list );
        }
-
     }
-    return TRUE;
+
+    return hrgn;
 }
 
 
 /***********************************************************************
- *           RedrawWindow32    (USER32.426)
+ *           dump_rdw_flags
  */
-BOOL32 WINAPI RedrawWindow32( HWND32 hwnd, const RECT32 *rectUpdate,
-                              HRGN32 hrgnUpdate, UINT32 flags )
+static void dump_rdw_flags(UINT flags)
 {
-    return PAINT_RedrawWindow( hwnd, rectUpdate, hrgnUpdate, flags, 0 );
+    TRACE("flags:");
+    if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
+    if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
+    if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
+    if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
+    if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
+    if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
+    if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
+    if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
+    if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
+    if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
+    if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
+    if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
+
+#define RDW_FLAGS \
+    (RDW_INVALIDATE | \
+    RDW_INTERNALPAINT | \
+    RDW_ERASE | \
+    RDW_VALIDATE | \
+    RDW_NOINTERNALPAINT | \
+    RDW_NOERASE | \
+    RDW_NOCHILDREN | \
+    RDW_ALLCHILDREN | \
+    RDW_UPDATENOW | \
+    RDW_ERASENOW | \
+    RDW_FRAME | \
+    RDW_NOFRAME)
+
+    if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
+    TRACE("\n");
+#undef RDW_FLAGS
 }
 
 
 /***********************************************************************
- *           RedrawWindow16    (USER.290)
+ *             RedrawWindow (USER32.@)
  */
-BOOL16 WINAPI RedrawWindow16( HWND16 hwnd, const RECT16 *rectUpdate,
-                              HRGN16 hrgnUpdate, UINT16 flags )
+BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
+                              HRGN hrgnUpdate, UINT flags )
 {
-    if (rectUpdate)
+    HRGN hRgn = 0;
+    RECT r, r2;
+    POINT pt;
+    WND* wndPtr;
+
+    if (!hwnd) hwnd = GetDesktopWindow();
+
+    /* check if the window or its parents are visible/not minimized */
+
+    if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
+
+    /* process pending events and messages before painting */
+    if (flags & RDW_UPDATENOW)
+        MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
+
+    if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
+    if (TRACE_ON(win))
+    {
+       if( hrgnUpdate )
+       {
+           GetRgnBox( hrgnUpdate, &r );
+            TRACE( "%p (%p) NULL %p box (%ld,%ld-%ld,%ld) flags=%04x\n",
+                 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
+       }
+       else
+       {
+           if( rectUpdate )
+               r = *rectUpdate;
+           else
+               SetRectEmpty( &r );
+           TRACE( "%p (%p) %s %ld,%ld-%ld,%ld %p flags=%04x\n",
+                   hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
+                   r.top, r.right, r.bottom, hrgnUpdate, flags );
+       }
+        dump_rdw_flags(flags);
+    }
+
+    /* prepare an update region in window coordinates */
+
+    if (((flags & (RDW_INVALIDATE|RDW_FRAME)) == (RDW_INVALIDATE|RDW_FRAME)) ||
+        ((flags & (RDW_VALIDATE|RDW_NOFRAME)) == (RDW_VALIDATE|RDW_NOFRAME)))
+       r = wndPtr->rectWindow;
+    else
+       r = wndPtr->rectClient;
+
+    pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
+    pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
+    OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
+
+    if (flags & RDW_INVALIDATE)  /* ------------------------- Invalidate */
     {
-        RECT32 r;
-        CONV_RECT16TO32( rectUpdate, &r );
-        return (BOOL16)RedrawWindow32( (HWND32)hwnd, &r, hrgnUpdate, flags );
+       /* If the window doesn't have hrgnUpdate we leave hRgn zero
+        * and put a new region straight into wndPtr->hrgnUpdate
+        * so that RDW_UpdateRgns() won't have to do any extra work.
+        */
+
+       if( hrgnUpdate )
+       {
+           if( wndPtr->hrgnUpdate )
+            {
+                hRgn = CreateRectRgn( 0, 0, 0, 0 );
+                CombineRgn( hRgn, hrgnUpdate, 0, RGN_COPY );
+                OffsetRgn( hRgn, pt.x, pt.y );
+            }
+           else
+            {
+               wndPtr->hrgnUpdate = crop_rgn( 0, hrgnUpdate, &r );
+                OffsetRgn( wndPtr->hrgnUpdate, pt.x, pt.y );
+            }
+       }
+       else if( rectUpdate )
+       {
+           if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
+           OffsetRect( &r2, pt.x, pt.y );
+           if( wndPtr->hrgnUpdate == 0 )
+               wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
+           else
+               hRgn = CreateRectRgnIndirect( &r2 );
+       }
+       else /* entire window or client depending on RDW_FRAME */
+       {
+           if( flags & RDW_FRAME )
+           {
+                if (wndPtr->hrgnUpdate) hRgn = (HRGN)1;
+                else wndPtr->hrgnUpdate = (HRGN)1;
+           }
+           else
+           {
+               GETCLIENTRECTW( wndPtr, r2 );
+                if( wndPtr->hrgnUpdate == 0 )
+                    wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
+                else
+                    hRgn = CreateRectRgnIndirect( &r2 );
+           }
+       }
     }
-    return (BOOL16)PAINT_RedrawWindow( (HWND32)hwnd, NULL, 
-                                      (HRGN32)hrgnUpdate, flags, 0 );
+    else if (flags & RDW_VALIDATE)  /* ------------------------- Validate */
+    {
+       /* In this we cannot leave with zero hRgn */
+       if( hrgnUpdate )
+       {
+           hRgn = crop_rgn( hRgn, hrgnUpdate,  &r );
+            OffsetRgn( hRgn, pt.x, pt.y );
+           GetRgnBox( hRgn, &r2 );
+           if( IsRectEmpty( &r2 ) ) goto END;
+       }
+       else if( rectUpdate )
+       {
+           if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
+               OffsetRect( &r2, pt.x, pt.y );
+           hRgn = CreateRectRgnIndirect( &r2 );
+       }
+       else /* entire window or client depending on RDW_NOFRAME */
+        {
+           if( flags & RDW_NOFRAME )
+               hRgn = (HRGN)1;
+           else
+           {
+               GETCLIENTRECTW( wndPtr, r2 );
+                hRgn = CreateRectRgnIndirect( &r2 );
+            }
+        }
+    }
+
+    /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
+
+    RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
+
+    /* Erase/update windows, from now on hRgn is a scratch region */
+
+    hRgn = RDW_Paint( wndPtr, (hRgn == (HRGN)1) ? 0 : hRgn, flags, 0 );
+
+END:
+    if( hRgn > (HRGN)1 && (hRgn != hrgnUpdate) )
+       DeleteObject(hRgn );
+    WIN_ReleaseWndPtr(wndPtr);
+    return TRUE;
 }
 
 
 /***********************************************************************
- *           UpdateWindow16   (USER.124)
+ *             UpdateWindow (USER32.@)
  */
-void WINAPI UpdateWindow16( HWND16 hwnd )
+BOOL WINAPI UpdateWindow( HWND hwnd )
 {
-    PAINT_RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_NOCHILDREN, 0 );
+    return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
 }
 
+
 /***********************************************************************
- *           UpdateWindow32   (USER32.567)
+ *             InvalidateRgn (USER32.@)
  */
-void WINAPI UpdateWindow32( HWND32 hwnd )
+BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
 {
-    PAINT_RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_NOCHILDREN, 0 );
+    return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
 }
 
+
 /***********************************************************************
- *           InvalidateRgn16   (USER.126)
+ *             InvalidateRect (USER32.@)
  */
-void WINAPI InvalidateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
+BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
 {
-    PAINT_RedrawWindow((HWND32)hwnd, NULL, (HRGN32)hrgn, 
-                      RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
+    return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
 }
 
 
 /***********************************************************************
- *           InvalidateRgn32   (USER32.329)
+ *             ValidateRgn (USER32.@)
  */
-void WINAPI InvalidateRgn32( HWND32 hwnd, HRGN32 hrgn, BOOL32 erase )
+BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
 {
-    PAINT_RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
+    return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
 }
 
 
 /***********************************************************************
- *           InvalidateRect16   (USER.125)
+ *             ValidateRect (USER32.@)
  */
-void WINAPI InvalidateRect16( HWND16 hwnd, const RECT16 *rect, BOOL16 erase )
+BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
 {
-    RedrawWindow16( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
+    return RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
 }
 
 
 /***********************************************************************
- *           InvalidateRect32   (USER32.328)
+ *             GetUpdateRect (USER32.@)
  */
-void WINAPI InvalidateRect32( HWND32 hwnd, const RECT32 *rect, BOOL32 erase )
+BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
 {
-    PAINT_RedrawWindow( hwnd, rect, 0, 
-                       RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
+    BOOL retvalue;
+    WND * wndPtr = WIN_FindWndPtr( hwnd );
+    if (!wndPtr) return FALSE;
+
+    if (rect)
+    {
+       if (wndPtr->hrgnUpdate > (HRGN)1)
+       {
+           HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
+            if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
+            {
+                retvalue = FALSE;
+                goto END;
+            }
+           GetRgnBox( hrgn, rect );
+           DeleteObject( hrgn );
+           if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
+           {
+               if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
+               {
+                   DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect,  2);
+               }
+           }
+       }
+       else
+       if( wndPtr->hrgnUpdate == (HRGN)1 )
+       {
+           GetClientRect( hwnd, rect );
+           if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
+       }
+       else
+           SetRectEmpty( rect );
+    }
+    retvalue = (wndPtr->hrgnUpdate >= (HRGN)1);
+END:
+    WIN_ReleaseWndPtr(wndPtr);
+    return retvalue;
 }
 
 
 /***********************************************************************
- *           ValidateRgn16   (USER.128)
+ *             GetUpdateRgn (USER32.@)
  */
-void WINAPI ValidateRgn16( HWND16 hwnd, HRGN16 hrgn )
+INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
 {
-    PAINT_RedrawWindow( (HWND32)hwnd, NULL, (HRGN32)hrgn, 
-                       RDW_VALIDATE | RDW_NOCHILDREN, 0 );
+    INT retval;
+    WND * wndPtr = WIN_FindWndPtr( hwnd );
+    if (!wndPtr) return ERROR;
+
+    if (wndPtr->hrgnUpdate == 0)
+    {
+        SetRectRgn( hrgn, 0, 0, 0, 0 );
+        retval = NULLREGION;
+        goto END;
+    }
+    else
+    if (wndPtr->hrgnUpdate == (HRGN)1)
+    {
+       SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
+                               wndPtr->rectClient.bottom - wndPtr->rectClient.top );
+       retval = SIMPLEREGION;
+    }
+    else
+    {
+       retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
+       OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
+                        wndPtr->rectWindow.top - wndPtr->rectClient.top );
+    }
+    if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
+END:
+    WIN_ReleaseWndPtr(wndPtr);
+    return retval;
 }
 
 
 /***********************************************************************
- *           ValidateRgn32   (USER32.572)
+ *             ExcludeUpdateRgn (USER32.@)
  */
-void WINAPI ValidateRgn32( HWND32 hwnd, HRGN32 hrgn )
+INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
 {
-    PAINT_RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN, 0 );
+    RECT rect;
+    WND * wndPtr;
+
+    if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
+
+    if (wndPtr->hrgnUpdate)
+    {
+       INT ret;
+       HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
+                                     wndPtr->rectWindow.top - wndPtr->rectClient.top,
+                                     wndPtr->rectWindow.right - wndPtr->rectClient.left,
+                                     wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
+       if( wndPtr->hrgnUpdate > (HRGN)1 )
+       {
+           CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
+           OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
+                           wndPtr->rectWindow.top - wndPtr->rectClient.top );
+       }
+
+       /* do ugly coordinate translations in dce.c */
+
+       ret = DCE_ExcludeRgn( hdc, hwnd, hrgn );
+       DeleteObject( hrgn );
+        WIN_ReleaseWndPtr(wndPtr);
+       return ret;
+    }
+    WIN_ReleaseWndPtr(wndPtr);
+    return GetClipBox( hdc, &rect );
 }
 
 
+
 /***********************************************************************
- *           ValidateRect16   (USER.127)
+ *             FillRect (USER.81)
+ * NOTE
+ *   The Win16 variant doesn't support special color brushes like
+ *   the Win32 one, despite the fact that Win16, as well as Win32,
+ *   supports special background brushes for a window class.
  */
-void WINAPI ValidateRect16( HWND16 hwnd, const RECT16 *rect )
+INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
 {
-    RedrawWindow16( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
+    HBRUSH prevBrush;
+
+    /* coordinates are logical so we cannot fast-check 'rect',
+     * it will be done later in the PatBlt().
+     */
+
+    if (!(prevBrush = SelectObject( HDC_32(hdc), HBRUSH_32(hbrush) ))) return 0;
+    PatBlt( HDC_32(hdc), rect->left, rect->top,
+              rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
+    SelectObject( HDC_32(hdc), prevBrush );
+    return 1;
 }
 
 
 /***********************************************************************
- *           ValidateRect32   (USER32.571)
+ *             FillRect (USER32.@)
  */
-void WINAPI ValidateRect32( HWND32 hwnd, const RECT32 *rect )
+INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
 {
-    PAINT_RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN, 0 );
+    HBRUSH prevBrush;
+
+    if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
+       hbrush = GetSysColorBrush( (INT) hbrush - 1 );
+    }
+
+    if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
+    PatBlt( hdc, rect->left, rect->top,
+              rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
+    SelectObject( hdc, prevBrush );
+    return 1;
 }
 
 
 /***********************************************************************
- *           GetUpdateRect16   (USER.190)
+ *             InvertRect (USER.82)
  */
-BOOL16 WINAPI GetUpdateRect16( HWND16 hwnd, LPRECT16 rect, BOOL16 erase )
+void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
 {
-    RECT32 r;
-    BOOL16 ret;
+    PatBlt( HDC_32(hdc), rect->left, rect->top,
+              rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
+}
 
-    if (!rect) return GetUpdateRect32( hwnd, NULL, erase );
-    ret = GetUpdateRect32( hwnd, &r, erase );
-    CONV_RECT32TO16( &r, rect );
-    return ret;
+
+/***********************************************************************
+ *             InvertRect (USER32.@)
+ */
+BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
+{
+    return PatBlt( hdc, rect->left, rect->top,
+                    rect->right - rect->left, rect->bottom - rect->top,
+                    DSTINVERT );
 }
 
 
 /***********************************************************************
- *           GetUpdateRect32   (USER32.297)
+ *             FrameRect (USER32.@)
  */
-BOOL32 WINAPI GetUpdateRect32( HWND32 hwnd, LPRECT32 rect, BOOL32 erase )
+INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
 {
-    WND * wndPtr = WIN_FindWndPtr( hwnd );
-    if (!wndPtr) return FALSE;
+    HBRUSH prevBrush;
+    RECT r = *rect;
+
+    if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
+    if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
+
+    PatBlt( hdc, r.left, r.top, 1,
+             r.bottom - r.top, PATCOPY );
+    PatBlt( hdc, r.right - 1, r.top, 1,
+             r.bottom - r.top, PATCOPY );
+    PatBlt( hdc, r.left, r.top,
+             r.right - r.left, 1, PATCOPY );
+    PatBlt( hdc, r.left, r.bottom - 1,
+             r.right - r.left, 1, PATCOPY );
+
+    SelectObject( hdc, prevBrush );
+    return TRUE;
+}
 
-    if (rect)
-    {
-       if (wndPtr->hrgnUpdate > 1)
-       {
-           HRGN32 hrgn = CreateRectRgn32( 0, 0, 0, 0 );
-           if (GetUpdateRgn32( hwnd, hrgn, erase ) == ERROR) return FALSE;
-           GetRgnBox32( hrgn, rect );
-           DeleteObject32( hrgn );
-       }
-       else SetRectEmpty32( rect );
-    }
-    return (wndPtr->hrgnUpdate > 1);
+
+/***********************************************************************
+ *             FrameRect (USER.83)
+ */
+INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
+{
+    RECT rect;
+    CONV_RECT16TO32( rect16, &rect );
+    return FrameRect( HDC_32(hdc), &rect, HBRUSH_32(hbrush) );
 }
 
 
 /***********************************************************************
- *           GetUpdateRgn16   (USER.237)
+ *             DrawFocusRect (USER.466)
  */
-INT16 WINAPI GetUpdateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
+void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
 {
-    return GetUpdateRgn32( hwnd, hrgn, erase );
+    RECT rect32;
+    CONV_RECT16TO32( rc, &rect32 );
+    DrawFocusRect( HDC_32(hdc), &rect32 );
 }
 
 
 /***********************************************************************
- *           GetUpdateRgn32   (USER32.298)
+ *             DrawFocusRect (USER32.@)
+ *
+ * FIXME: PatBlt(PATINVERT) with background brush.
  */
-INT32 WINAPI GetUpdateRgn32( HWND32 hwnd, HRGN32 hrgn, BOOL32 erase )
+BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
 {
-    INT32 retval;
-    WND * wndPtr = WIN_FindWndPtr( hwnd );
-    if (!wndPtr) return ERROR;
+    HBRUSH hOldBrush;
+    HPEN hOldPen, hNewPen;
+    INT oldDrawMode, oldBkMode;
+
+    hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
+    hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
+    hOldPen = SelectObject(hdc, hNewPen);
+    oldDrawMode = SetROP2(hdc, R2_XORPEN);
+    oldBkMode = SetBkMode(hdc, TRANSPARENT);
+
+    Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
+
+    SetBkMode(hdc, oldBkMode);
+    SetROP2(hdc, oldDrawMode);
+    SelectObject(hdc, hOldPen);
+    DeleteObject(hNewPen);
+    SelectObject(hdc, hOldBrush);
+
+    return TRUE;
+}
+
 
-    if (wndPtr->hrgnUpdate <= 1)
+/**********************************************************************
+ *             DrawAnimatedRects (USER32.@)
+ */
+BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
+                                   const RECT* lprcFrom,
+                                   const RECT* lprcTo )
+{
+    FIXME("(%p,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
+    return TRUE;
+}
+
+
+/**********************************************************************
+ *          PAINTING_DrawStateJam
+ *
+ * Jams in the requested type in the dc
+ */
+static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
+                                  DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
+                                  LPRECT rc, UINT dtflags, BOOL unicode )
+{
+    HDC memdc;
+    HBITMAP hbmsave;
+    BOOL retval;
+    INT cx = rc->right - rc->left;
+    INT cy = rc->bottom - rc->top;
+
+    switch(opcode)
     {
-        SetRectRgn32( hrgn, 0, 0, 0, 0 );
-        return NULLREGION;
+    case DST_TEXT:
+    case DST_PREFIXTEXT:
+        if(unicode)
+            return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
+        else
+            return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
+
+    case DST_ICON:
+        return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
+
+    case DST_BITMAP:
+        memdc = CreateCompatibleDC(hdc);
+        if(!memdc) return FALSE;
+        hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
+        if(!hbmsave)
+        {
+            DeleteDC(memdc);
+            return FALSE;
+        }
+        retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
+        SelectObject(memdc, hbmsave);
+        DeleteDC(memdc);
+        return retval;
+
+    case DST_COMPLEX:
+        if(func) {
+           BOOL bRet;
+           /* DRAWSTATEPROC assumes that it draws at the center of coordinates  */
+
+           OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
+            bRet = func(hdc, lp, wp, cx, cy);
+           /* Restore origin */
+           OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
+           return bRet;
+       } else
+            return FALSE;
     }
-    retval = CombineRgn32( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
-    if (erase) RedrawWindow32( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
+    return FALSE;
+}
+
+/**********************************************************************
+ *      PAINTING_DrawState()
+ */
+static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
+                               INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
+{
+    HBITMAP hbm, hbmsave;
+    HFONT hfsave;
+    HBRUSH hbsave, hbrtmp = 0;
+    HDC memdc;
+    RECT rc;
+    UINT dtflags = DT_NOCLIP;
+    COLORREF fg, bg;
+    UINT opcode = flags & 0xf;
+    INT len = wp;
+    BOOL retval, tmp;
+
+    if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len)    /* The string is '\0' terminated */
+    {
+        if(unicode)
+            len = strlenW((LPWSTR)lp);
+        else
+            len = strlen((LPSTR)lp);
+    }
+
+    /* Find out what size the image has if not given by caller */
+    if(!cx || !cy)
+    {
+        SIZE s;
+        CURSORICONINFO *ici;
+       BITMAP bm;
+
+        switch(opcode)
+        {
+        case DST_TEXT:
+        case DST_PREFIXTEXT:
+            if(unicode)
+                retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
+            else
+                retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
+            if(!retval) return FALSE;
+            break;
+
+        case DST_ICON:
+            ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
+            if(!ici) return FALSE;
+            s.cx = ici->nWidth;
+            s.cy = ici->nHeight;
+            GlobalUnlock16((HGLOBAL16)lp);
+            break;
+
+        case DST_BITMAP:
+           if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
+               return FALSE;
+            s.cx = bm.bmWidth;
+            s.cy = bm.bmHeight;
+            break;
+
+        case DST_COMPLEX: /* cx and cy must be set in this mode */
+            return FALSE;
+       }
+
+        if(!cx) cx = s.cx;
+        if(!cy) cy = s.cy;
+    }
+
+    rc.left   = x;
+    rc.top    = y;
+    rc.right  = x + cx;
+    rc.bottom = y + cy;
+
+    if(flags & DSS_RIGHT)    /* This one is not documented in the win32.hlp file */
+        dtflags |= DT_RIGHT;
+    if(opcode == DST_TEXT)
+        dtflags |= DT_NOPREFIX;
+
+    /* For DSS_NORMAL we just jam in the image and return */
+    if((flags & 0x7ff0) == DSS_NORMAL)
+    {
+        return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
+    }
+
+    /* For all other states we need to convert the image to B/W in a local bitmap */
+    /* before it is displayed */
+    fg = SetTextColor(hdc, RGB(0, 0, 0));
+    bg = SetBkColor(hdc, RGB(255, 255, 255));
+    hbm = NULL; hbmsave = NULL;
+    memdc = NULL; hbsave = NULL;
+    retval = FALSE; /* assume failure */
+
+    /* From here on we must use "goto cleanup" when something goes wrong */
+    hbm     = CreateBitmap(cx, cy, 1, 1, NULL);
+    if(!hbm) goto cleanup;
+    memdc   = CreateCompatibleDC(hdc);
+    if(!memdc) goto cleanup;
+    hbmsave = (HBITMAP)SelectObject(memdc, hbm);
+    if(!hbmsave) goto cleanup;
+    rc.left = rc.top = 0;
+    rc.right = cx;
+    rc.bottom = cy;
+    if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
+    SetBkColor(memdc, RGB(255, 255, 255));
+    SetTextColor(memdc, RGB(0, 0, 0));
+    hfsave  = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
+
+    /* DST_COMPLEX may draw text as well,
+     * so we must be sure that correct font is selected
+     */
+    if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
+    tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
+    if(hfsave) SelectObject(memdc, hfsave);
+    if(!tmp) goto cleanup;
+
+    /* This state cause the image to be dithered */
+    if(flags & DSS_UNION)
+    {
+        hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
+        if(!hbsave) goto cleanup;
+        tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
+        SelectObject(memdc, hbsave);
+        if(!tmp) goto cleanup;
+    }
+
+    if (flags & DSS_DISABLED)
+       hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
+    else if (flags & DSS_DEFAULT)
+       hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
+
+    /* Draw light or dark shadow */
+    if (flags & (DSS_DISABLED|DSS_DEFAULT))
+    {
+       if(!hbrtmp) goto cleanup;
+       hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
+       if(!hbsave) goto cleanup;
+       if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
+       SelectObject(hdc, hbsave);
+       DeleteObject(hbrtmp);
+       hbrtmp = 0;
+    }
+
+    if (flags & DSS_DISABLED)
+    {
+       hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
+       if(!hbrtmp) goto cleanup;
+    }
+    else if (!hbr)
+    {
+       hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
+    }
+
+    hbsave = (HBRUSH)SelectObject(hdc, hbr);
+
+    if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
+
+    retval = TRUE; /* We succeeded */
+
+cleanup:
+    SetTextColor(hdc, fg);
+    SetBkColor(hdc, bg);
+
+    if(hbsave)  SelectObject(hdc, hbsave);
+    if(hbmsave) SelectObject(memdc, hbmsave);
+    if(hbrtmp)  DeleteObject(hbrtmp);
+    if(hbm)     DeleteObject(hbm);
+    if(memdc)   DeleteDC(memdc);
+
     return retval;
 }
 
+/**********************************************************************
+ *             DrawStateA (USER32.@)
+ */
+BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
+                   DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
+                   INT x, INT y, INT cx, INT cy, UINT flags)
+{
+    return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
+}
 
-/***********************************************************************
- *           ExcludeUpdateRgn16   (USER.238)
+/**********************************************************************
+ *             DrawStateW (USER32.@)
  */
-INT16 WINAPI ExcludeUpdateRgn16( HDC16 hdc, HWND16 hwnd )
+BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
+                   DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
+                   INT x, INT y, INT cx, INT cy, UINT flags)
 {
-    return ExcludeUpdateRgn32( hdc, hwnd );
+    return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
 }
 
 
 /***********************************************************************
- *           ExcludeUpdateRgn32   (USER32.195)
+ *             SelectPalette (Not a Windows API)
  */
-INT32 WINAPI ExcludeUpdateRgn32( HDC32 hdc, HWND32 hwnd )
+HPALETTE WINAPI SelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
 {
-    RECT32 rect;
-    WND * wndPtr;
+    WORD wBkgPalette = 1;
 
-    if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
-
-    if (wndPtr->hrgnUpdate)
+    if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
     {
-       INT32 ret;
-       HRGN32 hrgn = CreateRectRgn32(wndPtr->rectWindow.left - wndPtr->rectClient.left,
-                                     wndPtr->rectWindow.top - wndPtr->rectClient.top,
-                                     wndPtr->rectClient.right - wndPtr->rectClient.left,
-                                     wndPtr->rectClient.bottom - wndPtr->rectClient.top);
-       if( wndPtr->hrgnUpdate > 1 )
-           CombineRgn32(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
-
-       /* do ugly coordinate translations in dce.c */
-
-       ret = DCE_ExcludeRgn( hdc, wndPtr, hrgn );
-       DeleteObject32( hrgn );
-       return ret;
-    } 
-    return GetClipBox32( hdc, &rect );
+        HWND hwnd = WindowFromDC( hDC );
+        if (hwnd)
+        {
+            HWND hForeground = GetForegroundWindow();
+            /* set primary palette if it's related to current active */
+            if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
+        }
+    }
+    return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
 }
 
 
+/***********************************************************************
+ *             UserRealizePalette (USER32.@)
+ */
+UINT WINAPI UserRealizePalette( HDC hDC )
+{
+    UINT realized = pfnGDIRealizePalette( hDC );
+
+    /* do not send anything if no colors were changed */
+    if (realized && IsDCCurrentPalette16( HDC_16(hDC) ))
+    {
+        /* send palette change notification */
+        HWND hWnd = WindowFromDC( hDC );
+        if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
+                                       SMTO_ABORTIFHUNG, 2000, NULL );
+    }
+    return realized;
+}