Create an X window for every window, including children.
Fixed non-client rectangle calculations in managed mode.
Added support for icon window in managed mode.

diff --git a/windows/dce.c b/windows/dce.c
index 400231a..3a143ae 100644
--- a/windows/dce.c
+++ b/windows/dce.c
@@ -211,9 +211,9 @@
 
     dce->hClipRgn = 0;
 
-    TRACE("\trestoring VisRgn\n");
-
-    RestoreVisRgn16(dce->hDC);
+    /* make it dirty so that the vis rgn gets recomputed next time */
+    dce->DCXflags |= DCX_DCEDIRTY;
+    SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
 }
 
 
@@ -412,8 +412,9 @@
     BOOL	bUpdateClipOrigin = FALSE;
 
     TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n", 
-				hwnd, hrgnClip, (unsigned)flags);
-    
+          hwnd, hrgnClip, (unsigned)flags);
+
+    if (!hwnd) hwnd = GetDesktopWindow();
     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
 
     /* fixup flags */
@@ -437,17 +438,11 @@
 	else flags |= DCX_CACHE;
     }
 
-    if( flags & DCX_NOCLIPCHILDREN )
-    {
-        flags |= DCX_CACHE;
-        flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
-    }
-
     if (flags & DCX_WINDOW) 
 	flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
 
-    if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent ) 
-	flags &= ~DCX_PARENTCLIP;
+    if (!wndPtr->parent || (wndPtr->parent->hwndSelf == GetDesktopWindow()))
+        flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
     else if( flags & DCX_PARENTCLIP )
     {
 	flags |= DCX_CACHE;
@@ -514,25 +509,6 @@
 	{
 	    TRACE("\tskipping hVisRgn update\n");
 	    bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
-
-            /* Abey - 16Jul99. to take care of the nested GetDC. first one
-               with DCX_EXCLUDERGN or DCX_INTERSECTRGN flags and the next
-               one with or without these flags. */
-
-	    if(dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
-	    {
-		/* This is likely to be a nested BeginPaint().
-                   or a BeginPaint() followed by a GetDC()*/
-
-		if( dce->hClipRgn != hrgnClip )
-		{
-		    FIXME("new hrgnClip[%04x] smashes the previous[%04x]\n",
-			  hrgnClip, dce->hClipRgn );
-		    DCE_DeleteClipRgn( dce );
-		}
-		else 
-		    RestoreVisRgn16(dce->hDC);
-	    }
 	}
     }
     if (!dce)
@@ -542,6 +518,14 @@
     }
 
     if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
+
+    if (((flags ^ dce->DCXflags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
+        (dce->hClipRgn != hrgnClip))
+    {
+        /* if the extra clip region has changed, get rid of the old one */
+        DCE_DeleteClipRgn( dce );
+    }
+
     dce->hwndCurrent = hwnd;
     dce->hClipRgn = hrgnClip;
     dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
@@ -581,7 +565,7 @@
 	     HWND hwnd /* [in] handle of window */
 ) {
     if (!hwnd)
-        return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
+        return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
     return GetDCEx( hwnd, 0, DCX_USESTYLE );
 }
 
@@ -591,7 +575,6 @@
  */
 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
 {
-    if (!hwnd) hwnd = GetDesktopWindow16();
     return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
 }
 
@@ -601,7 +584,6 @@
  */
 HDC WINAPI GetWindowDC( HWND hwnd )
 {
-    if (!hwnd) hwnd = GetDesktopWindow();
     return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
 }
 
diff --git a/windows/defwnd.c b/windows/defwnd.c
index 7a2b647..3c5e537 100644
--- a/windows/defwnd.c
+++ b/windows/defwnd.c
@@ -415,11 +415,7 @@
 	}
 
     case WM_SYNCPAINT:
-	if (wndPtr->hrgnUpdate) 
-	{
-	   RedrawWindow ( wndPtr->hwndSelf, 0, wndPtr->hrgnUpdate,
-                    RDW_ERASENOW | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
-        }
+        RedrawWindow ( wndPtr->hwndSelf, NULL, 0, RDW_ERASENOW | RDW_ERASE | RDW_ALLCHILDREN );
         return 0;
 
     case WM_SETREDRAW:
diff --git a/windows/nonclient.c b/windows/nonclient.c
index cc47c45..b25049c 100644
--- a/windows/nonclient.c
+++ b/windows/nonclient.c
@@ -75,24 +75,6 @@
 
 #define HAS_MENU(w)  (!((w)->dwStyle & WS_CHILD) && ((w)->wIDmenu != 0))
 
-/***********************************************************************
- *           WIN_WindowNeedsWMBorder
- *
- * This method defines the rules for a window to have a WM border,
- * caption...  It is used for consistency purposes.
- */
-BOOL WIN_WindowNeedsWMBorder( DWORD style, DWORD exStyle )
-{
-    if (!(style & WS_CHILD) && 
-	Options.managed  &&
-	!(exStyle & WS_EX_TOOLWINDOW) &&
-        ( ((style & WS_CAPTION) == WS_CAPTION) ||
-	  (style & WS_THICKFRAME)))
-        return TRUE;
-    if (exStyle & WS_EX_TRAYWINDOW)
-	return TRUE;
-    return FALSE;
-}
 
 /***********************************************************************
  *           NC_AdjustRect
@@ -106,21 +88,17 @@
 	ERR("Called in Win95 mode. Aiee! Please report this.\n" );
 
     if(style & WS_ICONIC) return;
-    /* Decide if the window will be managed (see CreateWindowEx) */
-    if (!WIN_WindowNeedsWMBorder(style, exStyle))
-    {
-        if (HAS_THICKFRAME( style, exStyle ))
-            InflateRect( rect, GetSystemMetrics(SM_CXFRAME), GetSystemMetrics(SM_CYFRAME) );
-        else
-        if (HAS_DLGFRAME( style, exStyle ))
-            InflateRect( rect, GetSystemMetrics(SM_CXDLGFRAME), GetSystemMetrics(SM_CYDLGFRAME) );
-        else
-        if (HAS_THINFRAME( style ))
-            InflateRect( rect, GetSystemMetrics(SM_CXBORDER), GetSystemMetrics(SM_CYBORDER));
 
-        if ((style & WS_CAPTION) == WS_CAPTION)
-            rect->top -= GetSystemMetrics(SM_CYCAPTION) - GetSystemMetrics(SM_CYBORDER);
-    }
+    if (HAS_THICKFRAME( style, exStyle ))
+        InflateRect( rect, GetSystemMetrics(SM_CXFRAME), GetSystemMetrics(SM_CYFRAME) );
+    else if (HAS_DLGFRAME( style, exStyle ))
+        InflateRect( rect, GetSystemMetrics(SM_CXDLGFRAME), GetSystemMetrics(SM_CYDLGFRAME) );
+    else if (HAS_THINFRAME( style ))
+        InflateRect( rect, GetSystemMetrics(SM_CXBORDER), GetSystemMetrics(SM_CYBORDER));
+
+    if ((style & WS_CAPTION) == WS_CAPTION)
+        rect->top -= GetSystemMetrics(SM_CYCAPTION) - GetSystemMetrics(SM_CYBORDER);
+
     if (menu) rect->top -= GetSystemMetrics(SM_CYMENU) + GetSystemMetrics(SM_CYBORDER);
 
     if (style & WS_VSCROLL) {
@@ -172,29 +150,21 @@
 {
     if(style & WS_ICONIC) return;
 
-    /* Decide if the window will be managed (see CreateWindowEx) */
-    if (!WIN_WindowNeedsWMBorder(style, exStyle))
+    if (HAS_THICKFRAME( style, exStyle ))
+        InflateRect( rect, GetSystemMetrics(SM_CXFRAME), GetSystemMetrics(SM_CYFRAME) );
+    else if (HAS_DLGFRAME( style, exStyle ))
+        InflateRect(rect, GetSystemMetrics(SM_CXDLGFRAME), GetSystemMetrics(SM_CYDLGFRAME) );
+    else if (HAS_THINFRAME( style ))
+        InflateRect( rect, GetSystemMetrics(SM_CXBORDER), GetSystemMetrics(SM_CYBORDER));
+
+    if ((style & WS_CAPTION) == WS_CAPTION)
     {
-        if (HAS_THICKFRAME( style, exStyle ))
-            InflateRect( rect, GetSystemMetrics(SM_CXFRAME), GetSystemMetrics(SM_CYFRAME) );
+        if (exStyle & WS_EX_TOOLWINDOW)
+            rect->top -= GetSystemMetrics(SM_CYSMCAPTION);
         else
-        if (HAS_DLGFRAME( style, exStyle ))
-            InflateRect(rect, GetSystemMetrics(SM_CXDLGFRAME), GetSystemMetrics(SM_CYDLGFRAME) );
-        else
-        if (HAS_THINFRAME( style ))
-            InflateRect( rect, GetSystemMetrics(SM_CXBORDER), GetSystemMetrics(SM_CYBORDER));
-
-        if ((style & WS_CAPTION) == WS_CAPTION)
-        {
-	    if (exStyle & WS_EX_TOOLWINDOW)
-		rect->top -= GetSystemMetrics(SM_CYSMCAPTION);
-	    else
-		rect->top -= GetSystemMetrics(SM_CYCAPTION);
-        }
+            rect->top -= GetSystemMetrics(SM_CYCAPTION);
     }
-
-    if (menu)
-	rect->top -= GetSystemMetrics(SM_CYMENU);
+    if (menu) rect->top -= GetSystemMetrics(SM_CYMENU);
 }
 
 
@@ -240,6 +210,20 @@
 }
 
 
+
+static HICON NC_IconForWindow( HWND hwnd )
+{
+    HICON hIcon = (HICON) GetClassLongA( hwnd, GCL_HICONSM );
+    if (!hIcon) hIcon = (HICON) GetClassLongA( hwnd, GCL_HICON );
+
+    /* If there is no hIcon specified and this is a modal dialog,
+     * get the default one.
+     */
+    if (!hIcon && (GetWindowLongA( hwnd, GWL_STYLE ) & DS_MODALFRAME))
+        hIcon = LoadImageA(0, IDI_WINLOGOA, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
+    return hIcon;
+}
+
 /***********************************************************************
  *		DrawCaption (USER.660) Draws a caption bar
  *
@@ -355,18 +339,9 @@
 	pt.x = rc.left + 2;
 	pt.y = (rc.bottom + rc.top - GetSystemMetrics(SM_CYSMICON)) / 2;
 
-	if (hIcon) {
-	    DrawIconEx (hdc, pt.x, pt.y, hIcon, GetSystemMetrics(SM_CXSMICON),
-			  GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
-	}
-	else {
-	    WND* wndPtr = WIN_FindWndPtr(hwnd);
-	    HICON hAppIcon = (HICON) NC_IconForWindow(wndPtr);
-	    DrawIconEx (hdc, pt.x, pt.y, hAppIcon, GetSystemMetrics(SM_CXSMICON),
-			  GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
-            WIN_ReleaseWndPtr(wndPtr);
-	}
-
+        if (!hIcon) hIcon = NC_IconForWindow(hwnd);
+        DrawIconEx (hdc, pt.x, pt.y, hIcon, GetSystemMetrics(SM_CXSMICON),
+                    GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
 	rc.left += (rc.bottom - rc.top);
     }
 
@@ -574,7 +549,7 @@
     rect->right  = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
     rect->bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
 
-    if ((wndPtr->dwStyle & WS_ICONIC) || (wndPtr->dwExStyle & WS_EX_MANAGED)) goto END;
+    if (wndPtr->dwStyle & WS_ICONIC) goto END;
 
     /* Remove frame from rectangle */
     if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
@@ -630,76 +605,73 @@
 
     if (wndPtr->dwStyle & WS_MINIMIZE) return HTCAPTION;
 
-    if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
+    /* Check borders */
+    if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
     {
-        /* Check borders */
-        if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
+        InflateRect( &rect, -GetSystemMetrics(SM_CXFRAME), -GetSystemMetrics(SM_CYFRAME) );
+        if (!PtInRect( &rect, pt ))
         {
-            InflateRect( &rect, -GetSystemMetrics(SM_CXFRAME), -GetSystemMetrics(SM_CYFRAME) );
-            if (!PtInRect( &rect, pt ))
+            /* Check top sizing border */
+            if (pt.y < rect.top)
             {
-                /* Check top sizing border */
-                if (pt.y < rect.top)
-                {
-                    if (pt.x < rect.left+GetSystemMetrics(SM_CXSIZE)) return HTTOPLEFT;
-                    if (pt.x >= rect.right-GetSystemMetrics(SM_CXSIZE)) return HTTOPRIGHT;
-                    return HTTOP;
-                }
-                /* Check bottom sizing border */
-                if (pt.y >= rect.bottom)
-                {
-                    if (pt.x < rect.left+GetSystemMetrics(SM_CXSIZE)) return HTBOTTOMLEFT;
-                    if (pt.x >= rect.right-GetSystemMetrics(SM_CXSIZE)) return HTBOTTOMRIGHT;
-                    return HTBOTTOM;
-                }
-                /* Check left sizing border */
-                if (pt.x < rect.left)
-                {
-                    if (pt.y < rect.top+GetSystemMetrics(SM_CYSIZE)) return HTTOPLEFT;
-                    if (pt.y >= rect.bottom-GetSystemMetrics(SM_CYSIZE)) return HTBOTTOMLEFT;
-                    return HTLEFT;
-                }
-                /* Check right sizing border */
-                if (pt.x >= rect.right)
-                {
-                    if (pt.y < rect.top+GetSystemMetrics(SM_CYSIZE)) return HTTOPRIGHT;
-                    if (pt.y >= rect.bottom-GetSystemMetrics(SM_CYSIZE)) return HTBOTTOMRIGHT;
-                    return HTRIGHT;
-                }
+                if (pt.x < rect.left+GetSystemMetrics(SM_CXSIZE)) return HTTOPLEFT;
+                if (pt.x >= rect.right-GetSystemMetrics(SM_CXSIZE)) return HTTOPRIGHT;
+                return HTTOP;
+            }
+            /* Check bottom sizing border */
+            if (pt.y >= rect.bottom)
+            {
+                if (pt.x < rect.left+GetSystemMetrics(SM_CXSIZE)) return HTBOTTOMLEFT;
+                if (pt.x >= rect.right-GetSystemMetrics(SM_CXSIZE)) return HTBOTTOMRIGHT;
+                return HTBOTTOM;
+            }
+            /* Check left sizing border */
+            if (pt.x < rect.left)
+            {
+                if (pt.y < rect.top+GetSystemMetrics(SM_CYSIZE)) return HTTOPLEFT;
+                if (pt.y >= rect.bottom-GetSystemMetrics(SM_CYSIZE)) return HTBOTTOMLEFT;
+                return HTLEFT;
+            }
+            /* Check right sizing border */
+            if (pt.x >= rect.right)
+            {
+                if (pt.y < rect.top+GetSystemMetrics(SM_CYSIZE)) return HTTOPRIGHT;
+                if (pt.y >= rect.bottom-GetSystemMetrics(SM_CYSIZE)) return HTBOTTOMRIGHT;
+                return HTRIGHT;
             }
         }
-        else  /* No thick frame */
+    }
+    else  /* No thick frame */
+    {
+        if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
+            InflateRect(&rect, -GetSystemMetrics(SM_CXDLGFRAME), -GetSystemMetrics(SM_CYDLGFRAME));
+        else if (HAS_THINFRAME( wndPtr->dwStyle ))
+            InflateRect(&rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER));
+        if (!PtInRect( &rect, pt )) return HTBORDER;
+    }
+
+    /* Check caption */
+
+    if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
+    {
+        rect.top += GetSystemMetrics(SM_CYCAPTION) - GetSystemMetrics(SM_CYBORDER);
+        if (!PtInRect( &rect, pt ))
         {
-            if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
-                InflateRect(&rect, -GetSystemMetrics(SM_CXDLGFRAME), -GetSystemMetrics(SM_CYDLGFRAME));
-            else if (HAS_THINFRAME( wndPtr->dwStyle ))
-                InflateRect(&rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER));
-            if (!PtInRect( &rect, pt )) return HTBORDER;
-        }
+            /* Check system menu */
+            if ((wndPtr->dwStyle & WS_SYSMENU) && !(wndPtr->dwExStyle & WS_EX_TOOLWINDOW))
+                rect.left += GetSystemMetrics(SM_CXSIZE);
+            if (pt.x <= rect.left) return HTSYSMENU;
 
-        /* Check caption */
+            /* Check maximize box */
+            if (wndPtr->dwStyle & WS_MAXIMIZEBOX)
+                rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
 
-        if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
-        {
-            rect.top += GetSystemMetrics(SM_CYCAPTION) - GetSystemMetrics(SM_CYBORDER);
-            if (!PtInRect( &rect, pt ))
-            {
-                /* Check system menu */
-                if ((wndPtr->dwStyle & WS_SYSMENU) && !(wndPtr->dwExStyle & WS_EX_TOOLWINDOW))
-                    rect.left += GetSystemMetrics(SM_CXSIZE);
-                if (pt.x <= rect.left) return HTSYSMENU;
-
-                /* Check maximize box */
-                if (wndPtr->dwStyle & WS_MAXIMIZEBOX)
-                    rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
-
-                if (pt.x >= rect.right) return HTMAXBUTTON;
-                /* Check minimize box */
-                if (wndPtr->dwStyle & WS_MINIMIZEBOX)
-                    rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
-                if (pt.x >= rect.right) return HTMINBUTTON;
-                return HTCAPTION;
-            }
+            if (pt.x >= rect.right) return HTMAXBUTTON;
+            /* Check minimize box */
+            if (wndPtr->dwStyle & WS_MINIMIZEBOX)
+                rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
+            if (pt.x >= rect.right) return HTMINBUTTON;
+            return HTCAPTION;
         }
     }
 
@@ -765,90 +737,87 @@
 
     if (wndPtr->dwStyle & WS_MINIMIZE) return HTCAPTION;
 
-    if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
+    /* Check borders */
+    if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
     {
-        /* Check borders */
-        if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
+        InflateRect( &rect, -GetSystemMetrics(SM_CXFRAME), -GetSystemMetrics(SM_CYFRAME) );
+        if (!PtInRect( &rect, pt ))
         {
-            InflateRect( &rect, -GetSystemMetrics(SM_CXFRAME), -GetSystemMetrics(SM_CYFRAME) );
-            if (!PtInRect( &rect, pt ))
+            /* Check top sizing border */
+            if (pt.y < rect.top)
             {
-                /* Check top sizing border */
-                if (pt.y < rect.top)
-                {
-                    if (pt.x < rect.left+GetSystemMetrics(SM_CXSIZE)) return HTTOPLEFT;
-                    if (pt.x >= rect.right-GetSystemMetrics(SM_CXSIZE)) return HTTOPRIGHT;
-                    return HTTOP;
-                }
-                /* Check bottom sizing border */
-                if (pt.y >= rect.bottom)
-                {
-                    if (pt.x < rect.left+GetSystemMetrics(SM_CXSIZE)) return HTBOTTOMLEFT;
-                    if (pt.x >= rect.right-GetSystemMetrics(SM_CXSIZE)) return HTBOTTOMRIGHT;
-                    return HTBOTTOM;
-                }
-                /* Check left sizing border */
-                if (pt.x < rect.left)
-                {
-                    if (pt.y < rect.top+GetSystemMetrics(SM_CYSIZE)) return HTTOPLEFT;
-                    if (pt.y >= rect.bottom-GetSystemMetrics(SM_CYSIZE)) return HTBOTTOMLEFT;
-                    return HTLEFT;
-                }
-                /* Check right sizing border */
-                if (pt.x >= rect.right)
-                {
-                    if (pt.y < rect.top+GetSystemMetrics(SM_CYSIZE)) return HTTOPRIGHT;
-                    if (pt.y >= rect.bottom-GetSystemMetrics(SM_CYSIZE)) return HTBOTTOMRIGHT;
-                    return HTRIGHT;
-                }
+                if (pt.x < rect.left+GetSystemMetrics(SM_CXSIZE)) return HTTOPLEFT;
+                if (pt.x >= rect.right-GetSystemMetrics(SM_CXSIZE)) return HTTOPRIGHT;
+                return HTTOP;
+            }
+            /* Check bottom sizing border */
+            if (pt.y >= rect.bottom)
+            {
+                if (pt.x < rect.left+GetSystemMetrics(SM_CXSIZE)) return HTBOTTOMLEFT;
+                if (pt.x >= rect.right-GetSystemMetrics(SM_CXSIZE)) return HTBOTTOMRIGHT;
+                return HTBOTTOM;
+            }
+            /* Check left sizing border */
+            if (pt.x < rect.left)
+            {
+                if (pt.y < rect.top+GetSystemMetrics(SM_CYSIZE)) return HTTOPLEFT;
+                if (pt.y >= rect.bottom-GetSystemMetrics(SM_CYSIZE)) return HTBOTTOMLEFT;
+                return HTLEFT;
+            }
+            /* Check right sizing border */
+            if (pt.x >= rect.right)
+            {
+                if (pt.y < rect.top+GetSystemMetrics(SM_CYSIZE)) return HTTOPRIGHT;
+                if (pt.y >= rect.bottom-GetSystemMetrics(SM_CYSIZE)) return HTBOTTOMRIGHT;
+                return HTRIGHT;
             }
         }
-        else  /* No thick frame */
-        {
-            if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
-                InflateRect(&rect, -GetSystemMetrics(SM_CXDLGFRAME), -GetSystemMetrics(SM_CYDLGFRAME));
-            else if (HAS_THINFRAME( wndPtr->dwStyle ))
-                InflateRect(&rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER));
-            if (!PtInRect( &rect, pt )) return HTBORDER;
-        }
+    }
+    else  /* No thick frame */
+    {
+        if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
+            InflateRect(&rect, -GetSystemMetrics(SM_CXDLGFRAME), -GetSystemMetrics(SM_CYDLGFRAME));
+        else if (HAS_THINFRAME( wndPtr->dwStyle ))
+            InflateRect(&rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER));
+        if (!PtInRect( &rect, pt )) return HTBORDER;
+    }
 
-        /* Check caption */
+    /* Check caption */
 
-        if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
+    if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
+    {
+        if (wndPtr->dwExStyle & WS_EX_TOOLWINDOW)
+            rect.top += GetSystemMetrics(SM_CYSMCAPTION) - 1;
+        else
+            rect.top += GetSystemMetrics(SM_CYCAPTION) - 1;
+        if (!PtInRect( &rect, pt ))
         {
-	    if (wndPtr->dwExStyle & WS_EX_TOOLWINDOW)
-	        rect.top += GetSystemMetrics(SM_CYSMCAPTION) - 1;
-	    else
-	        rect.top += GetSystemMetrics(SM_CYCAPTION) - 1;
-            if (!PtInRect( &rect, pt ))
+            /* Check system menu */
+            if ((wndPtr->dwStyle & WS_SYSMENU) && !(wndPtr->dwExStyle & WS_EX_TOOLWINDOW))
             {
-                /* Check system menu */
-                if ((wndPtr->dwStyle & WS_SYSMENU) && !(wndPtr->dwExStyle & WS_EX_TOOLWINDOW))
-		{
-		    if (NC_IconForWindow(wndPtr))
-			rect.left += GetSystemMetrics(SM_CYCAPTION) - 1;
-		}
-                if (pt.x < rect.left) return HTSYSMENU;
-
-                /* Check close button */
-                if (wndPtr->dwStyle & WS_SYSMENU)
-                    rect.right -= GetSystemMetrics(SM_CYCAPTION) - 1;
-                if (pt.x > rect.right) return HTCLOSE;
-
-                /* Check maximize box */
-		/* In win95 there is automatically a Maximize button when there is a minimize one*/
-                if ((wndPtr->dwStyle & WS_MAXIMIZEBOX)|| (wndPtr->dwStyle & WS_MINIMIZEBOX))
-                    rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
-                if (pt.x > rect.right) return HTMAXBUTTON;
-
-                /* Check minimize box */
-		/* In win95 there is automatically a Maximize button when there is a Maximize one*/
-                if ((wndPtr->dwStyle & WS_MINIMIZEBOX)||(wndPtr->dwStyle & WS_MAXIMIZEBOX))
-                    rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
-
-                if (pt.x > rect.right) return HTMINBUTTON;
-                return HTCAPTION;
+                if (NC_IconForWindow(wndPtr->hwndSelf))
+                    rect.left += GetSystemMetrics(SM_CYCAPTION) - 1;
             }
+            if (pt.x < rect.left) return HTSYSMENU;
+
+            /* Check close button */
+            if (wndPtr->dwStyle & WS_SYSMENU)
+                rect.right -= GetSystemMetrics(SM_CYCAPTION) - 1;
+            if (pt.x > rect.right) return HTCLOSE;
+
+            /* Check maximize box */
+            /* In win95 there is automatically a Maximize button when there is a minimize one*/
+            if ((wndPtr->dwStyle & WS_MAXIMIZEBOX)|| (wndPtr->dwStyle & WS_MINIMIZEBOX))
+                rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
+            if (pt.x > rect.right) return HTMAXBUTTON;
+
+            /* Check minimize box */
+            /* In win95 there is automatically a Maximize button when there is a Maximize one*/
+            if ((wndPtr->dwStyle & WS_MINIMIZEBOX)||(wndPtr->dwStyle & WS_MAXIMIZEBOX))
+                rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
+
+            if (pt.x > rect.right) return HTMINBUTTON;
+            return HTCAPTION;
         }
     }
 
@@ -925,20 +894,15 @@
     RECT rect;
     HDC hdcMem;
     HBITMAP hbitmap;
-    WND *wndPtr = WIN_FindWndPtr( hwnd );
 
-    if( !(wndPtr->dwExStyle & WS_EX_MANAGED) )
-    {
-      NC_GetInsideRect( hwnd, &rect );
-      hdcMem = CreateCompatibleDC( hdc );
-      hbitmap = SelectObject( hdcMem, hbitmapClose );
-      BitBlt(hdc, rect.left, rect.top, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
-               hdcMem, (wndPtr->dwStyle & WS_CHILD) ? GetSystemMetrics(SM_CXSIZE) : 0, 0,
-               down ? NOTSRCCOPY : SRCCOPY );
-      SelectObject( hdcMem, hbitmap );
-      DeleteDC( hdcMem );
-    }
-    WIN_ReleaseWndPtr(wndPtr);
+    NC_GetInsideRect( hwnd, &rect );
+    hdcMem = CreateCompatibleDC( hdc );
+    hbitmap = SelectObject( hdcMem, hbitmapClose );
+    BitBlt(hdc, rect.left, rect.top, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
+           hdcMem, (GetWindowLongA(hwnd,GWL_STYLE) & WS_CHILD) ? GetSystemMetrics(SM_CXSIZE) : 0, 0,
+           down ? NOTSRCCOPY : SRCCOPY );
+    SelectObject( hdcMem, hbitmap );
+    DeleteDC( hdcMem );
 }
 
 
@@ -948,22 +912,17 @@
 static void NC_DrawMaxButton( HWND hwnd, HDC16 hdc, BOOL down )
 {
     RECT rect;
-    WND *wndPtr = WIN_FindWndPtr( hwnd );
     HDC hdcMem;
 
-    if( !(wndPtr->dwExStyle & WS_EX_MANAGED) )
-    {
-      NC_GetInsideRect( hwnd, &rect );
-      hdcMem = CreateCompatibleDC( hdc );
-      SelectObject( hdcMem,  (IsZoomed(hwnd) 
-			     ? (down ? hbitmapRestoreD : hbitmapRestore)
-			     : (down ? hbitmapMaximizeD : hbitmapMaximize)) );
-      BitBlt( hdc, rect.right - GetSystemMetrics(SM_CXSIZE) - 1, rect.top,
-		GetSystemMetrics(SM_CXSIZE) + 1, GetSystemMetrics(SM_CYSIZE), hdcMem, 0, 0,
-		SRCCOPY );
-      DeleteDC( hdcMem );
-    }
-    WIN_ReleaseWndPtr(wndPtr);
+    NC_GetInsideRect( hwnd, &rect );
+    hdcMem = CreateCompatibleDC( hdc );
+    SelectObject( hdcMem,  (IsZoomed(hwnd)
+                            ? (down ? hbitmapRestoreD : hbitmapRestore)
+                            : (down ? hbitmapMaximizeD : hbitmapMaximize)) );
+    BitBlt( hdc, rect.right - GetSystemMetrics(SM_CXSIZE) - 1, rect.top,
+            GetSystemMetrics(SM_CXSIZE) + 1, GetSystemMetrics(SM_CYSIZE), hdcMem, 0, 0,
+            SRCCOPY );
+    DeleteDC( hdcMem );
 
 }
 
@@ -974,21 +933,17 @@
 static void NC_DrawMinButton( HWND hwnd, HDC16 hdc, BOOL down )
 {
     RECT rect;
-    WND *wndPtr = WIN_FindWndPtr( hwnd );
     HDC hdcMem;
 
-    if( !(wndPtr->dwExStyle & WS_EX_MANAGED) )
-    {
-      NC_GetInsideRect( hwnd, &rect );
-      hdcMem = CreateCompatibleDC( hdc );
-      SelectObject( hdcMem, (down ? hbitmapMinimizeD : hbitmapMinimize) );
-      if (wndPtr->dwStyle & WS_MAXIMIZEBOX) rect.right -= GetSystemMetrics(SM_CXSIZE)+1;
-      BitBlt( hdc, rect.right - GetSystemMetrics(SM_CXSIZE) - 1, rect.top,
-		GetSystemMetrics(SM_CXSIZE) + 1, GetSystemMetrics(SM_CYSIZE), hdcMem, 0, 0,
-		SRCCOPY );
-      DeleteDC( hdcMem );
-    }
-    WIN_ReleaseWndPtr(wndPtr);
+    NC_GetInsideRect( hwnd, &rect );
+    hdcMem = CreateCompatibleDC( hdc );
+    SelectObject( hdcMem, (down ? hbitmapMinimizeD : hbitmapMinimize) );
+    if (GetWindowLongA(hwnd,GWL_STYLE) & WS_MAXIMIZEBOX)
+        rect.right -= GetSystemMetrics(SM_CXSIZE)+1;
+    BitBlt( hdc, rect.right - GetSystemMetrics(SM_CXSIZE) - 1, rect.top,
+            GetSystemMetrics(SM_CXSIZE) + 1, GetSystemMetrics(SM_CYSIZE), hdcMem, 0, 0,
+            SRCCOPY );
+    DeleteDC( hdcMem );
 }
 
 
@@ -1012,28 +967,17 @@
 BOOL
 NC_DrawSysButton95 (HWND hwnd, HDC hdc, BOOL down)
 {
-    WND *wndPtr = WIN_FindWndPtr( hwnd );
+    HICON hIcon = NC_IconForWindow( hwnd );
 
-    if( !(wndPtr->dwExStyle & WS_EX_MANAGED) )
+    if (hIcon)
     {
-	HICON  hIcon;
-	RECT rect;
-
-	NC_GetInsideRect( hwnd, &rect );
-
-	hIcon = NC_IconForWindow( wndPtr );
-
-	if (hIcon)
-	    DrawIconEx (hdc, rect.left + 2, rect.top + 2, hIcon,
-			  GetSystemMetrics(SM_CXSMICON),
-			  GetSystemMetrics(SM_CYSMICON),
-			  0, 0, DI_NORMAL);
-
-        WIN_ReleaseWndPtr(wndPtr);
-	return (hIcon != 0);
+        RECT rect;
+        NC_GetInsideRect( hwnd, &rect );
+        DrawIconEx (hdc, rect.left + 2, rect.top + 2, hIcon,
+                    GetSystemMetrics(SM_CXSMICON),
+                    GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
     }
-    WIN_ReleaseWndPtr(wndPtr);
-    return FALSE;
+    return (hIcon != 0);
 }
 
 
@@ -1058,37 +1002,32 @@
 static void NC_DrawCloseButton95 (HWND hwnd, HDC hdc, BOOL down, BOOL bGrayed)
 {
     RECT rect;
-    WND *wndPtr = WIN_FindWndPtr( hwnd );
 
-    if( !(wndPtr->dwExStyle & WS_EX_MANAGED) )
+    NC_GetInsideRect( hwnd, &rect );
+
+    /* A tool window has a smaller Close button */
+    if (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_TOOLWINDOW)
     {
-        NC_GetInsideRect( hwnd, &rect );
+        INT iBmpHeight = 11; /* Windows does not use SM_CXSMSIZE and SM_CYSMSIZE   */
+        INT iBmpWidth = 11;  /* it uses 11x11 for  the close button in tool window */
+        INT iCaptionHeight = GetSystemMetrics(SM_CYSMCAPTION);
 
-	/* A tool window has a smaller Close button */
-	if(wndPtr->dwExStyle & WS_EX_TOOLWINDOW)
-	{
-            INT iBmpHeight = 11; /* Windows does not use SM_CXSMSIZE and SM_CYSMSIZE   */
-            INT iBmpWidth = 11;  /* it uses 11x11 for  the close button in tool window */
-            INT iCaptionHeight = GetSystemMetrics(SM_CYSMCAPTION);
-
-            rect.top = rect.top + (iCaptionHeight - 1 - iBmpHeight) / 2;
-            rect.left = rect.right - (iCaptionHeight + 1 + iBmpWidth) / 2;
-            rect.bottom = rect.top + iBmpHeight;
-            rect.right = rect.left + iBmpWidth;
-        }
-        else
-        {
-            rect.left = rect.right - GetSystemMetrics(SM_CXSIZE) - 1;
-            rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 1;
-            rect.top += 2;
-            rect.right -= 2;
-        }
-        DrawFrameControl( hdc, &rect, DFC_CAPTION,
-                          (DFCS_CAPTIONCLOSE |
-                           (down ? DFCS_PUSHED : 0) |
-                           (bGrayed ? DFCS_INACTIVE : 0)) );
+        rect.top = rect.top + (iCaptionHeight - 1 - iBmpHeight) / 2;
+        rect.left = rect.right - (iCaptionHeight + 1 + iBmpWidth) / 2;
+        rect.bottom = rect.top + iBmpHeight;
+        rect.right = rect.left + iBmpWidth;
     }
-    WIN_ReleaseWndPtr(wndPtr);
+    else
+    {
+        rect.left = rect.right - GetSystemMetrics(SM_CXSIZE) - 1;
+        rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 1;
+        rect.top += 2;
+        rect.right -= 2;
+    }
+    DrawFrameControl( hdc, &rect, DFC_CAPTION,
+                      (DFCS_CAPTIONCLOSE |
+                       (down ? DFCS_PUSHED : 0) |
+                       (bGrayed ? DFCS_INACTIVE : 0)) );
 }
 
 /******************************************************************************
@@ -1100,23 +1039,18 @@
 static void NC_DrawMaxButton95(HWND hwnd,HDC16 hdc,BOOL down, BOOL bGrayed)
 {
     RECT rect;
-    WND *wndPtr = WIN_FindWndPtr( hwnd );
+    UINT flags = IsZoomed(hwnd) ? DFCS_CAPTIONRESTORE : DFCS_CAPTIONMAX;
 
-    if( !(wndPtr->dwExStyle & WS_EX_MANAGED))
-    {
-        UINT flags = IsZoomed(hwnd) ? DFCS_CAPTIONRESTORE : DFCS_CAPTIONMAX;
-        NC_GetInsideRect( hwnd, &rect );
-        if (wndPtr->dwStyle & WS_SYSMENU)
-            rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
-        rect.left = rect.right - GetSystemMetrics(SM_CXSIZE);
-        rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 1;
-        rect.top += 2;
-        rect.right -= 2;
-        if (down) flags |= DFCS_PUSHED;
-        if (bGrayed) flags |= DFCS_INACTIVE;
-        DrawFrameControl( hdc, &rect, DFC_CAPTION, flags );
-    }
-    WIN_ReleaseWndPtr(wndPtr);
+    NC_GetInsideRect( hwnd, &rect );
+    if (GetWindowLongA( hwnd, GWL_STYLE) & WS_SYSMENU)
+        rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
+    rect.left = rect.right - GetSystemMetrics(SM_CXSIZE);
+    rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 1;
+    rect.top += 2;
+    rect.right -= 2;
+    if (down) flags |= DFCS_PUSHED;
+    if (bGrayed) flags |= DFCS_INACTIVE;
+    DrawFrameControl( hdc, &rect, DFC_CAPTION, flags );
 }
 
 /******************************************************************************
@@ -1128,25 +1062,21 @@
 static void  NC_DrawMinButton95(HWND hwnd,HDC16 hdc,BOOL down, BOOL bGrayed)
 {
     RECT rect;
-    WND *wndPtr = WIN_FindWndPtr( hwnd );
+    UINT flags = DFCS_CAPTIONMIN;
+    DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
 
-    if( !(wndPtr->dwExStyle & WS_EX_MANAGED))
-    {
-        UINT flags = DFCS_CAPTIONMIN;
-        NC_GetInsideRect( hwnd, &rect );
-        if (wndPtr->dwStyle & WS_SYSMENU)
-            rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
-        if (wndPtr->dwStyle & (WS_MAXIMIZEBOX|WS_MINIMIZEBOX))
-            rect.right -= GetSystemMetrics(SM_CXSIZE) - 2;
-        rect.left = rect.right - GetSystemMetrics(SM_CXSIZE);
-        rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 1;
-        rect.top += 2;
-        rect.right -= 2;
-        if (down) flags |= DFCS_PUSHED;
-        if (bGrayed) flags |= DFCS_INACTIVE;
-        DrawFrameControl( hdc, &rect, DFC_CAPTION, flags );
-    }
-    WIN_ReleaseWndPtr(wndPtr);
+    NC_GetInsideRect( hwnd, &rect );
+    if (style & WS_SYSMENU)
+        rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
+    if (style & (WS_MAXIMIZEBOX|WS_MINIMIZEBOX))
+        rect.right -= GetSystemMetrics(SM_CXSIZE) - 2;
+    rect.left = rect.right - GetSystemMetrics(SM_CXSIZE);
+    rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 1;
+    rect.top += 2;
+    rect.right -= 2;
+    if (down) flags |= DFCS_PUSHED;
+    if (bGrayed) flags |= DFCS_INACTIVE;
+    DrawFrameControl( hdc, &rect, DFC_CAPTION, flags );
 }
 
 /***********************************************************************
@@ -1306,22 +1236,11 @@
 			    DWORD style, BOOL active )
 {
     RECT r = *rect;
-    WND * wndPtr = WIN_FindWndPtr( hwnd );
     char buffer[256];
 
-    if (wndPtr->dwExStyle & WS_EX_MANAGED)
-    {
-        WIN_ReleaseWndPtr(wndPtr);
-        return;
-    }
-
     if (!hbitmapClose)
     {
-	if (!(hbitmapClose = LoadBitmapA( 0, MAKEINTRESOURCEA(OBM_CLOSE) )))
-        {
-            WIN_ReleaseWndPtr(wndPtr);
-	    return;
-        }
+	if (!(hbitmapClose = LoadBitmapA( 0, MAKEINTRESOURCEA(OBM_CLOSE) ))) return;
 	hbitmapMinimize  = LoadBitmapA( 0, MAKEINTRESOURCEA(OBM_REDUCE) );
 	hbitmapMinimizeD = LoadBitmapA( 0, MAKEINTRESOURCEA(OBM_REDUCED) );
 	hbitmapMaximize  = LoadBitmapA( 0, MAKEINTRESOURCEA(OBM_ZOOM) );
@@ -1330,7 +1249,7 @@
 	hbitmapRestoreD  = LoadBitmapA( 0, MAKEINTRESOURCEA(OBM_RESTORED) );
     }
     
-    if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME)
+    if (GetWindowLongA( hwnd, GWL_EXSTYLE) & WS_EX_DLGMODALFRAME)
     {
         HBRUSH hbrushOld = SelectObject(hdc, GetSysColorBrush(COLOR_WINDOW) );
 	PatBlt( hdc, r.left, r.top, 1, r.bottom-r.top+1,PATCOPY );
@@ -1340,7 +1259,6 @@
 	r.right--;
 	SelectObject( hdc, hbrushOld );
     }
-    WIN_ReleaseWndPtr(wndPtr);
     MoveToEx( hdc, r.left, r.bottom, NULL );
     LineTo( hdc, r.right, r.bottom );
 
@@ -1409,18 +1327,10 @@
     BOOL active )
 {
     RECT  r = *rect;
-    WND     *wndPtr = WIN_FindWndPtr( hwnd );
     char    buffer[256];
     HPEN  hPrevPen;
     HMENU hSysMenu;
 
-    if (wndPtr->dwExStyle & WS_EX_MANAGED)
-    {
-        WIN_ReleaseWndPtr(wndPtr);
-        return;
-    }
-    WIN_ReleaseWndPtr(wndPtr);
-
     hPrevPen = SelectObject( hdc, GetSysColorPen(COLOR_3DFACE) );
     MoveToEx( hdc, r.left, r.bottom - 1, NULL );
     LineTo( hdc, r.right, r.bottom - 1 );
@@ -1521,27 +1431,24 @@
 
     SelectObject( hdc, GetSysColorPen(COLOR_WINDOWFRAME) );
 
-    if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
+    if (HAS_ANYFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
     {
-        if (HAS_ANYFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
-	{
-	    SelectObject( hdc, GetStockObject(NULL_BRUSH) );
-            Rectangle( hdc, 0, 0, rect.right, rect.bottom );
-            InflateRect( &rect, -1, -1 );
-        }
+        SelectObject( hdc, GetStockObject(NULL_BRUSH) );
+        Rectangle( hdc, 0, 0, rect.right, rect.bottom );
+        InflateRect( &rect, -1, -1 );
+    }
 
-	if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
-            NC_DrawFrame(hdc, &rect, FALSE, active );
-	else if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle )) 
-            NC_DrawFrame( hdc, &rect, TRUE, active );
+    if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
+        NC_DrawFrame(hdc, &rect, FALSE, active );
+    else if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
+        NC_DrawFrame( hdc, &rect, TRUE, active );
 
-        if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
-        {
-            RECT r = rect;
-            r.bottom = rect.top + GetSystemMetrics(SM_CYSIZE);
-            rect.top += GetSystemMetrics(SM_CYSIZE) + GetSystemMetrics(SM_CYBORDER);
-            NC_DrawCaption( hdc, &r, hwnd, wndPtr->dwStyle, active );
-        }
+    if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
+    {
+        RECT r = rect;
+        r.bottom = rect.top + GetSystemMetrics(SM_CYSIZE);
+        rect.top += GetSystemMetrics(SM_CYSIZE) + GetSystemMetrics(SM_CYBORDER);
+        NC_DrawCaption( hdc, &r, hwnd, wndPtr->dwStyle, active );
     }
 
     if (HAS_MENU(wndPtr))
@@ -1652,34 +1559,32 @@
 
     SelectObject( hdc, GetSysColorPen(COLOR_WINDOWFRAME) );
 
-    if(!(wndPtr->dwExStyle & WS_EX_MANAGED)) {
-        if (HAS_BIGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle)) {
-            DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT | BF_ADJUST);
-        }
-	if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
-            NC_DrawFrame95(hdc, &rect, FALSE, active );
-        else if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle )) 
-            NC_DrawFrame95( hdc, &rect, TRUE, active );
-	else if (HAS_THINFRAME( wndPtr->dwStyle )) {
-            SelectObject( hdc, GetStockObject(NULL_BRUSH) );
-            Rectangle( hdc, 0, 0, rect.right, rect.bottom );
-	}
+    if (HAS_BIGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle)) {
+        DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT | BF_ADJUST);
+    }
+    if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
+        NC_DrawFrame95(hdc, &rect, FALSE, active );
+    else if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
+        NC_DrawFrame95( hdc, &rect, TRUE, active );
+    else if (HAS_THINFRAME( wndPtr->dwStyle )) {
+        SelectObject( hdc, GetStockObject(NULL_BRUSH) );
+        Rectangle( hdc, 0, 0, rect.right, rect.bottom );
+    }
 
-        if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
-        {
-            RECT  r = rect;
-	    if (wndPtr->dwExStyle & WS_EX_TOOLWINDOW) {
-		r.bottom = rect.top + GetSystemMetrics(SM_CYSMCAPTION);
-		rect.top += GetSystemMetrics(SM_CYSMCAPTION);
-	    }
-	    else {
-		r.bottom = rect.top + GetSystemMetrics(SM_CYCAPTION);
-		rect.top += GetSystemMetrics(SM_CYCAPTION);
-	    }
-	    if( !clip || IntersectRect( &rfuzz, &r, &rectClip ) )
-                NC_DrawCaption95 (hdc, &r, hwnd, wndPtr->dwStyle,
-                                  wndPtr->dwExStyle, active);
+    if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
+    {
+        RECT  r = rect;
+        if (wndPtr->dwExStyle & WS_EX_TOOLWINDOW) {
+            r.bottom = rect.top + GetSystemMetrics(SM_CYSMCAPTION);
+            rect.top += GetSystemMetrics(SM_CYSMCAPTION);
         }
+        else {
+            r.bottom = rect.top + GetSystemMetrics(SM_CYCAPTION);
+            rect.top += GetSystemMetrics(SM_CYCAPTION);
+        }
+        if( !clip || IntersectRect( &rfuzz, &r, &rectClip ) )
+            NC_DrawCaption95 (hdc, &r, hwnd, wndPtr->dwStyle,
+                              wndPtr->dwExStyle, active);
     }
 
     if (HAS_MENU(wndPtr))
@@ -2351,16 +2256,3 @@
     
     return TRUE;
 }
-
-HICON16 NC_IconForWindow(WND *wndPtr)
-{
-	HICON16 hIcon = (HICON) GetClassLongA(wndPtr->hwndSelf, GCL_HICONSM);
-	if(!hIcon) hIcon = (HICON) GetClassLongA(wndPtr->hwndSelf, GCL_HICON);
-
-	/* If there is no hIcon specified and this is a modal dialog, */ 
-        /* get the default one.                                       */
-	if (!hIcon && (wndPtr->dwStyle & DS_MODALFRAME))
-		hIcon = LoadImageA(0, IDI_WINLOGOA, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
-
-	return hIcon;
-}
diff --git a/windows/painting.c b/windows/painting.c
index 86d7b64..898d9d8 100644
--- a/windows/painting.c
+++ b/windows/painting.c
@@ -28,6 +28,17 @@
 					(r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
 					(r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
 
+  /* PAINT_RedrawWindow() control flags */
+#define RDW_EX_DELAY_NCPAINT    0x0020
+
+  /* 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
+
   /* Last COLOR id */
 #define COLOR_MAX   COLOR_GRADIENTINACTIVECAPTION
 
@@ -127,7 +138,7 @@
  *	  is 1 then the hRgn is preserved and RDW_Paint() will have to get 
  *	  a DC without extra clipping region.
  */
-HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
+static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
 {
     RECT  r;
     HRGN  hClip = 0;
@@ -323,7 +334,7 @@
 
     TRACE("hrgnUpdate = %04x, \n", hrgnUpdate);
 
-    if (GetClassWord16(wndPtr->hwndSelf, GCW_STYLE) & CS_PARENTDC)
+    if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_PARENTDC)
     {
         /* Don't clip the output to the update region for CS_PARENTDC window */
 	if( hrgnUpdate ) 
@@ -336,7 +347,7 @@
 	if( hrgnUpdate ) /* convert to client coordinates */
 	    OffsetRgn( hrgnUpdate, wndPtr->rectWindow.left - wndPtr->rectClient.left,
 			           wndPtr->rectWindow.top - wndPtr->rectClient.top );
-        lps->hdc = GetDCEx(hwnd, hrgnUpdate, DCX_INTERSECTRGN |
+        lps->hdc = GetDCEx(hwnd, hrgnUpdate, DCX_INTERSECTRGN | 
                              DCX_WINDOWPAINT | DCX_USESTYLE | (bIcon ? DCX_WINDOW : 0) );
 	/* ReleaseDC() in EndPaint() will delete the region */
     }
@@ -477,7 +488,7 @@
  *  Validate the portions of parents that are covered by a validated child
  *  wndPtr = child
  */
-void  RDW_ValidateParent(WND *wndChild)  
+static void RDW_ValidateParent(WND *wndChild)
 {
     WND *wndParent = WIN_LockWndPtr(wndChild->parent);
     WND *wndDesktop = WIN_GetDesktop();
@@ -611,15 +622,14 @@
 
 		if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
 		    == NULLREGION )
-		    goto EMPTY;
+                {
+                    DeleteObject( wndPtr->hrgnUpdate );
+                    wndPtr->hrgnUpdate = 0;
+                }
 	    }
 	    else /* validate everything */
 	    {
-		if( wndPtr->hrgnUpdate > 1 )
-		{
-EMPTY:
-		    DeleteObject( wndPtr->hrgnUpdate );
-		}
+		if( wndPtr->hrgnUpdate > 1 ) DeleteObject( wndPtr->hrgnUpdate );
 		wndPtr->hrgnUpdate = 0;
 	    }
 
@@ -757,7 +767,7 @@
         if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
             SendMessage16( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
     }
-    else if ((flags & RDW_ERASENOW) || (ex & RDW_EX_TOPFRAME))
+    else if (flags & RDW_ERASENOW)
     {
 	UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
 	HRGN hrgnRet;
@@ -765,7 +775,6 @@
 	hrgnRet = WIN_UpdateNCRgn(wndPtr, 
 				  hrgn, 
 				  UNC_REGION | UNC_CHECK | 
-				  ((ex & RDW_EX_TOPFRAME) ? UNC_ENTIRE : 0) |
 				  ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) ); 
 
         if( hrgnRet )
@@ -792,7 +801,6 @@
     }
 
     if( !IsWindow(hWnd) ) return hrgn;
-    ex &= ~RDW_EX_TOPFRAME;
 
       /* ... and its child windows */
 
@@ -820,12 +828,12 @@
     return hrgn;
 }
 
+
 /***********************************************************************
- *           PAINT_RedrawWindow
- *
+ *		RedrawWindow (USER32.@)
  */
-BOOL PAINT_RedrawWindow( HWND hwnd, const RECT *rectUpdate,
-                           HRGN hrgnUpdate, UINT flags, UINT ex )
+BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
+                              HRGN hrgnUpdate, UINT flags )
 {
     HRGN hRgn = 0;
     RECT r, r2;
@@ -848,8 +856,8 @@
 	if( hrgnUpdate )
 	{
 	    GetRgnBox( hrgnUpdate, &r );
-            TRACE( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x, exflags=%04x\n", 
-	          hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags, ex);
+            TRACE( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x\n",
+	          hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
 	}
 	else
 	{
@@ -857,9 +865,9 @@
 		r = *rectUpdate;
 	    else
 		SetRectEmpty( &r );
-	    TRACE( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x, exflags=%04x\n",
+	    TRACE( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x\n",
 			hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left, 
-			r.top, r.right, r.bottom, hrgnUpdate, flags, ex );
+			r.top, r.right, r.bottom, hrgnUpdate, flags );
 	}
     }
 
@@ -870,17 +878,9 @@
     else
 	r = wndPtr->rectClient;
 
-    if( ex & RDW_EX_XYWINDOW )
-    {
-	pt.x = pt.y = 0;
-        OffsetRect( &r, -wndPtr->rectWindow.left, -wndPtr->rectWindow.top );
-    }
-    else
-    {
-	pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
-	pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
-	OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
-    }
+    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 */
     {
@@ -935,7 +935,6 @@
 	{
 	    if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
 		OffsetRect( &r2, pt.x, pt.y );
-rect2v:
 	    hRgn = CreateRectRgnIndirect( &r2 );
 	}
 	else /* entire window or client depending on RDW_FRAME */
@@ -945,7 +944,7 @@
 	    else
 	    {
 		GETCLIENTRECTW( wndPtr, r2 );
-		goto rect2v;
+                hRgn = CreateRectRgnIndirect( &r2 );
             }
         }
     }
@@ -956,7 +955,7 @@
 
     /* Erase/update windows, from now on hRgn is a scratch region */
 
-    hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, ex );
+    hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, 0 );
 
 END:
     if( hRgn > 1 && (hRgn != hrgnUpdate) )
@@ -967,16 +966,6 @@
 
 
 /***********************************************************************
- *		RedrawWindow (USER32.@)
- */
-BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
-                              HRGN hrgnUpdate, UINT flags )
-{
-    return PAINT_RedrawWindow( hwnd, rectUpdate, hrgnUpdate, flags, 0 );
-}
-
-
-/***********************************************************************
  *		RedrawWindow (USER.290)
  */
 BOOL16 WINAPI RedrawWindow16( HWND16 hwnd, const RECT16 *rectUpdate,
diff --git a/windows/scroll.c b/windows/scroll.c
index dba6111..bf094eb 100644
--- a/windows/scroll.c
+++ b/windows/scroll.c
@@ -13,10 +13,6 @@
 #include "wingdi.h"
 #include "wine/winuser16.h"
 #include "winuser.h"
-#include "win.h"
-#include "gdi.h"
-#include "dce.h"
-#include "region.h"
 #include "user.h"
 #include "debugtools.h"
 
@@ -79,109 +75,9 @@
                           const RECT *prLClip, HRGN hrgnUpdate,
                           LPRECT rcUpdate )
 {
-    RECT rect, rClip, rSrc;
-    POINT src, dest;
-    DC *dc = DC_GetDCUpdate( hdc );
-
-    TRACE("%04x %d,%d hrgnUpdate=%04x rcUpdate = %p cliprc = (%d,%d-%d,%d), rc=(%d,%d-%d,%d)\n",
-                   (HDC16)hdc, dx, dy, hrgnUpdate, rcUpdate, 
-		   prLClip ? prLClip->left : 0, prLClip ? prLClip->top : 0, prLClip ? prLClip->right : 0, prLClip ? prLClip->bottom : 0,
-		   rc ? rc->left : 0, rc ? rc->top : 0, rc ? rc->right : 0, rc ? rc->bottom : 0 );
-
-    if ( !dc || !hdc ) return FALSE;
-
-/*
-    TRACE(scroll,"\t[wndOrgX=%i, wndExtX=%i, vportOrgX=%i, vportExtX=%i]\n",
-          dc->wndOrgX, dc->wndExtX, dc->vportOrgX, dc->vportExtX );
-    TRACE(scroll,"\t[wndOrgY=%i, wndExtY=%i, vportOrgY=%i, vportExtY=%i]\n",
-	  dc->wndOrgY, dc->wndExtY, dc->vportOrgY, dc->vportExtY );
-*/
-
-    /* compute device clipping region (in device coordinates) */
-
-    if ( rc )
-	rect = *rc;
-    else /* maybe we should just return FALSE? */
-	GetClipBox( hdc, &rect );
-
-    LPtoDP( hdc, (LPPOINT)&rect, 2 );
-
-    if (prLClip)
-    {
-        rClip = *prLClip;
-        LPtoDP( hdc, (LPPOINT)&rClip, 2 );
-	IntersectRect( &rClip, &rect, &rClip );
-    }
-    else
-        rClip = rect;
-
-    dx = XLPTODP ( dc, rect.left + dx ) - XLPTODP ( dc, rect.left );
-    dy = YLPTODP ( dc, rect.top + dy ) - YLPTODP ( dc, rect.top );
-
-    rSrc = rClip;
-    OffsetRect( &rSrc, -dx, -dy );
-    IntersectRect( &rSrc, &rSrc, &rect );
-
-    if(dc->hVisRgn)
-    {
-        if (!IsRectEmpty(&rSrc))
-        {
-            dest.x = (src.x = rSrc.left) + dx;
-            dest.y = (src.y = rSrc.top) + dy;
-
-            /* copy bits */
-    
-            DPtoLP( hdc, (LPPOINT)&rSrc, 2 );
-            DPtoLP( hdc, &src, 1 );
-            DPtoLP( hdc, &dest, 1 );
-
-            if (!BitBlt( hdc, dest.x, dest.y,
-                           rSrc.right - rSrc.left, rSrc.bottom - rSrc.top,
-                           hdc, src.x, src.y, SRCCOPY))
-            {
-                GDI_ReleaseObj( hdc );
-                return FALSE;
-            }
-        }
-
-        /* compute update areas */
-
-        if (hrgnUpdate || rcUpdate)
-        {
-            HRGN hrgn =
-              (hrgnUpdate) ? hrgnUpdate : CreateRectRgn( 0,0,0,0 );
-            HRGN hrgn2;
-
-            hrgn2 = CreateRectRgnIndirect( &rect );
-            OffsetRgn( hrgn2, dc->DCOrgX, dc->DCOrgY );
-            CombineRgn( hrgn2, hrgn2, dc->hVisRgn, RGN_AND );
-            OffsetRgn( hrgn2, -dc->DCOrgX, -dc->DCOrgY );
-            SetRectRgn( hrgn, rClip.left, rClip.top,
-                          rClip.right, rClip.bottom );
-            CombineRgn( hrgn, hrgn, hrgn2, RGN_AND );
-            OffsetRgn( hrgn2, dx, dy );
-            CombineRgn( hrgn, hrgn, hrgn2, RGN_DIFF );
-
-            if( rcUpdate )
-	    {
-		GetRgnBox( hrgn, rcUpdate );
-
-		/* Put the rcUpdate in logical coordinate */
-		DPtoLP( hdc, (LPPOINT)rcUpdate, 2 );
-	    }
-            if (!hrgnUpdate) DeleteObject( hrgn );
-            DeleteObject( hrgn2 );
-
-        }
-    }
-    else
-    {
-       if (hrgnUpdate) SetRectRgn(hrgnUpdate, 0, 0, 0, 0);
-       if (rcUpdate) SetRectEmpty(rcUpdate);
-    }
-
-    GDI_ReleaseObj( hdc );
-    return TRUE;
+    if (USER_Driver.pScrollDC)
+        return USER_Driver.pScrollDC( hdc, dx, dy, rc, prLClip, hrgnUpdate, rcUpdate );
+    return FALSE;
 }
 
 
diff --git a/windows/win.c b/windows/win.c
index 77a73c6..0ad24dc 100644
--- a/windows/win.c
+++ b/windows/win.c
@@ -553,6 +553,7 @@
     DWORD clsStyle;
     WNDPROC winproc;
     DCE *dce;
+    CREATESTRUCTA cs;
 
     TRACE("Creating desktop window\n");
 
@@ -601,9 +602,21 @@
     pWndDesktop->cbWndExtra        = wndExtra;
     pWndDesktop->irefCount         = 0;
 
-    if (!USER_Driver.pCreateWindow( hwndDesktop )) return FALSE;
+    cs.lpCreateParams = NULL;
+    cs.hInstance      = 0;
+    cs.hMenu          = 0;
+    cs.hwndParent     = 0;
+    cs.x              = 0;
+    cs.y              = 0;
+    cs.cx             = pWndDesktop->rectWindow.right;
+    cs.cy             = pWndDesktop->rectWindow.bottom;
+    cs.style          = pWndDesktop->dwStyle;
+    cs.dwExStyle      = pWndDesktop->dwExStyle;
+    cs.lpszName       = NULL;
+    cs.lpszClass      = DESKTOP_CLASS_ATOM;
 
-    SendMessageW( hwndDesktop, WM_NCCREATE, 0, 0 );
+    if (!USER_Driver.pCreateWindow( hwndDesktop, &cs )) return FALSE;
+
     pWndDesktop->flags |= WIN_NEEDS_ERASEBKGND;
     return TRUE;
 }
@@ -685,14 +698,12 @@
     INT sw = SW_SHOW; 
     struct tagCLASS *classPtr;
     WND *wndPtr;
-    HWND retvalue;
-    HWND16 hwnd, hwndLinkAfter;
+    HWND hwnd, hwndLinkAfter;
     POINT maxSize, maxPos, minTrack, maxTrack;
     INT wndExtra;
     DWORD clsStyle;
     WNDPROC winproc;
     DCE *dce;
-    LRESULT CALLBACK (*localSend32)(HWND, UINT, WPARAM, LPARAM);
 
     TRACE("%s %s %08lx %08lx %d,%d %dx%d %04x %04x %08x %p\n",
           (type == WIN_PROC_32W) ? debugres_w((LPWSTR)cs->lpszName) : debugres_a(cs->lpszName), 
@@ -811,7 +822,7 @@
 	    TRACE("CBT-hook returned 0\n");
 	    USER_HEAP_FREE( hwnd );
             CLASS_RemoveWindow( classPtr );
-            retvalue =  0;
+            hwnd =  0;
             goto end;
 	}
     }
@@ -862,12 +873,6 @@
     wndPtr->rectWindow.bottom = cs->y + cs->cy;
     wndPtr->rectClient        = wndPtr->rectWindow;
 
-    if (!USER_Driver.pCreateWindow(wndPtr->hwndSelf))
-    {
-        retvalue = FALSE;
-        goto end;
-    }
-
     /* Set the window menu */
 
     if ((wndPtr->dwStyle & (WS_CAPTION | WS_CHILD)) == WS_CAPTION )
@@ -889,96 +894,44 @@
     }
     else wndPtr->wIDmenu = (UINT)cs->hMenu;
 
-    /* Send the WM_CREATE message 
-     * Perhaps we shouldn't allow width/height changes as well. 
-     * See p327 in "Internals". 
-     */
-
-    maxPos.x = wndPtr->rectWindow.left; maxPos.y = wndPtr->rectWindow.top;
-
-    localSend32 = (type == WIN_PROC_32W) ? SendMessageW : SendMessageA;
-    if( (*localSend32)( hwnd, WM_NCCREATE, 0, (LPARAM)cs) )
+    if (!USER_Driver.pCreateWindow( wndPtr->hwndSelf, cs ))
     {
-        /* Insert the window in the linked list */
-
-        WIN_LinkWindow( hwnd, hwndLinkAfter );
-
-        WINPOS_SendNCCalcSize( hwnd, FALSE, &wndPtr->rectWindow,
-                               NULL, NULL, 0, &wndPtr->rectClient );
-        OffsetRect(&wndPtr->rectWindow, maxPos.x - wndPtr->rectWindow.left,
-                                          maxPos.y - wndPtr->rectWindow.top);
-        if( ((*localSend32)( hwnd, WM_CREATE, 0, (LPARAM)cs )) != -1 )
-        {
-            /* Send the size messages */
-
-            if (!(wndPtr->flags & WIN_NEED_SIZE))
-            {
-                /* send it anyway */
-	        if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
-		    ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
-		  WARN("sending bogus WM_SIZE message 0x%08lx\n",
-			MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
-				 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
-                (*localSend32)( hwnd, WM_SIZE, SIZE_RESTORED,
-                                MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
-                                         wndPtr->rectClient.bottom-wndPtr->rectClient.top));
-                (*localSend32)( hwnd, WM_MOVE, 0,
-                                MAKELONG( wndPtr->rectClient.left,
-                                          wndPtr->rectClient.top ) );
-            }
-
-            /* Show the window, maximizing or minimizing if needed */
-
-            if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
-            {
-		RECT16 newPos;
-		UINT16 swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
-                wndPtr->dwStyle &= ~(WS_MAXIMIZE | WS_MINIMIZE);
-		WINPOS_MinMaximize( wndPtr, swFlag, &newPos );
-                swFlag = ((wndPtr->dwStyle & WS_CHILD) || GetActiveWindow())
-                    ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
-                    : SWP_NOZORDER | SWP_FRAMECHANGED;
-                SetWindowPos( hwnd, 0, newPos.left, newPos.top,
-                                newPos.right, newPos.bottom, swFlag );
-            }
-
-	    if( (wndPtr->dwStyle & WS_CHILD) && !(wndPtr->dwExStyle & WS_EX_NOPARENTNOTIFY) )
-	    {
-		/* Notify the parent window only */
-
-		SendMessageA( wndPtr->parent->hwndSelf, WM_PARENTNOTIFY,
-				MAKEWPARAM(WM_CREATE, wndPtr->wIDmenu), (LPARAM)hwnd );
-                if( !IsWindow(hwnd) )
-                {
-                    retvalue = 0;
-                    goto end;
-                }
-	    }
-
-            if (cs->style & WS_VISIBLE) ShowWindow( hwnd, sw );
-
-            /* Call WH_SHELL hook */
-
-            if (!(wndPtr->dwStyle & WS_CHILD) && !wndPtr->owner)
-                HOOK_CallHooksA( WH_SHELL, HSHELL_WINDOWCREATED, hwnd, 0 );
-
-            TRACE("created window %04x\n", hwnd);
-            retvalue = hwnd;
-            goto end;
-        }
-        WIN_UnlinkWindow( hwnd );
+        WARN("aborted by WM_xxCREATE!\n");
+        WIN_ReleaseWndPtr(WIN_DestroyWindow( wndPtr ));
+        CLASS_RemoveWindow( classPtr );
+        WIN_ReleaseWndPtr(wndPtr);
+        return 0;
     }
 
-    /* Abort window creation */
+    if( (wndPtr->dwStyle & WS_CHILD) && !(wndPtr->dwExStyle & WS_EX_NOPARENTNOTIFY) )
+    {
+        /* Notify the parent window only */
 
-    WARN("aborted by WM_xxCREATE!\n");
-    WIN_ReleaseWndPtr(WIN_DestroyWindow( wndPtr ));
-    CLASS_RemoveWindow( classPtr );
-    retvalue = 0;
-end:
+        SendMessageA( wndPtr->parent->hwndSelf, WM_PARENTNOTIFY,
+                      MAKEWPARAM(WM_CREATE, wndPtr->wIDmenu), (LPARAM)hwnd );
+        if( !IsWindow(hwnd) )
+        {
+            hwnd = 0;
+            goto end;
+        }
+    }
+
+    if (cs->style & WS_VISIBLE)
+    {
+        /* in case WS_VISIBLE got set in the meantime */
+        wndPtr->dwStyle &= ~WS_VISIBLE;
+        ShowWindow( hwnd, sw );
+    }
+
+    /* Call WH_SHELL hook */
+
+    if (!(wndPtr->dwStyle & WS_CHILD) && !wndPtr->owner)
+        HOOK_CallHooksA( WH_SHELL, HSHELL_WINDOWCREATED, hwnd, 0 );
+
+    TRACE("created window %04x\n", hwnd);
+ end:
     WIN_ReleaseWndPtr(wndPtr);
-
-    return retvalue;
+    return hwnd;
 }
 
 
@@ -2176,8 +2129,12 @@
  */
 INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
 {
-    FIXME("(0x%08x,%p,0x%x),stub!\n",hwnd,lpString,nMaxCount);
-    return GetWindowTextW(hwnd,lpString,nMaxCount);
+    WND *win = WIN_FindWndPtr( hwnd );
+    if (!win) return 0;
+    if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
+    else lpString[0] = 0;
+    WIN_ReleaseWndPtr( win );
+    return strlenW(lpString);
 }
 
 
diff --git a/windows/winpos.c b/windows/winpos.c
index ae3e946..42e87c3 100644
--- a/windows/winpos.c
+++ b/windows/winpos.c
@@ -42,8 +42,6 @@
 #define PLACE_MAX		0x0002
 #define PLACE_RECT		0x0004
 
-#define MINMAX_NOSWP		0x00010000
-
 
 #define DWP_MAGIC  ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
 
@@ -119,58 +117,6 @@
 }
 
 /***********************************************************************
- *           WINPOS_FindIconPos
- *
- * Find a suitable place for an iconic window.
- */
-static POINT16 WINPOS_FindIconPos( WND* wndPtr, POINT16 pt )
-{
-    RECT16 rectParent;
-    short x, y, xspacing, yspacing;
-
-    GetClientRect16( wndPtr->parent->hwndSelf, &rectParent );
-    if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
-        (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
-        return pt;  /* The icon already has a suitable position */
-
-    xspacing = GetSystemMetrics(SM_CXICONSPACING);
-    yspacing = GetSystemMetrics(SM_CYICONSPACING);
-
-    y = rectParent.bottom;
-    for (;;)
-    {
-        x = rectParent.left;
-        do 
-        {
-              /* Check if another icon already occupies this spot */
-            WND *childPtr = WIN_LockWndPtr(wndPtr->parent->child);
-            while (childPtr)
-            {
-                if ((childPtr->dwStyle & WS_MINIMIZE) && (childPtr != wndPtr))
-                {
-                    if ((childPtr->rectWindow.left < x + xspacing) &&
-                        (childPtr->rectWindow.right >= x) &&
-                        (childPtr->rectWindow.top <= y) &&
-                        (childPtr->rectWindow.bottom > y - yspacing))
-                        break;  /* There's a window in there */
-                }
-                WIN_UpdateWndPtr(&childPtr,childPtr->next);
-            }
-            WIN_ReleaseWndPtr(childPtr);
-            if (!childPtr) /* No window was found, so it's OK for us */
-            {
-		pt.x = x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
-		pt.y = y - (yspacing + GetSystemMetrics(SM_CYICON)) / 2;
-		return pt;
-            }
-	    x += xspacing;
-        } while(x <= rectParent.right-xspacing);
-        y -= yspacing;
-    }
-}
-
-
-/***********************************************************************
  *		ArrangeIconicWindows (USER.170)
  */
 UINT16 WINAPI ArrangeIconicWindows16( HWND16 parent) 
@@ -440,7 +386,8 @@
         MapWindowPoints( GetDesktopWindow(), wndScope->parent->hwndSelf, &xy, 1 );
 
     if (xy.x < wndScope->rectClient.left || pt.x >= wndScope->rectClient.right ||
-        xy.y < wndScope->rectClient.top || pt.y >= wndScope->rectClient.bottom)
+        xy.y < wndScope->rectClient.top || pt.y >= wndScope->rectClient.bottom ||
+        wndScope->dwStyle & WS_MINIMIZE)
         goto hittest;
 
     xy.x -= wndScope->rectClient.left;
@@ -1154,8 +1101,7 @@
     MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXSCREEN);
     MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYSCREEN);
 
-    if (wndPtr->dwExStyle & WS_EX_MANAGED) xinc = yinc = 0;
-    else if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
+    if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
     {
         xinc = GetSystemMetrics(SM_CXDLGFRAME);
         yinc = GetSystemMetrics(SM_CYDLGFRAME);
@@ -1207,107 +1153,6 @@
 }
 
 /***********************************************************************
- *           WINPOS_MinMaximize
- *
- * Fill in lpRect and return additional flags to be used with SetWindowPos().
- * This function assumes that 'cmd' is different from the current window
- * state.
- */
-UINT WINPOS_MinMaximize( WND* wndPtr, UINT16 cmd, LPRECT16 lpRect )
-{
-    UINT swpFlags = 0;
-    POINT pt, size;
-    LPINTERNALPOS lpPos;
-
-    TRACE("0x%04x %u\n", wndPtr->hwndSelf, cmd );
-
-    size.x = wndPtr->rectWindow.left; size.y = wndPtr->rectWindow.top;
-    lpPos = WINPOS_InitInternalPos( wndPtr, size, &wndPtr->rectWindow );
-
-    if (lpPos && !HOOK_CallHooksA(WH_CBT, HCBT_MINMAX, wndPtr->hwndSelf, cmd))
-    {
-	if( wndPtr->dwStyle & WS_MINIMIZE )
-	{
-	    if( !SendMessageA( wndPtr->hwndSelf, WM_QUERYOPEN, 0, 0L ) )
-		return (SWP_NOSIZE | SWP_NOMOVE);
-	    swpFlags |= SWP_NOCOPYBITS;
-	}
-	switch( cmd )
-	{
-	    case SW_MINIMIZE:
-		 if( wndPtr->dwStyle & WS_MAXIMIZE)
-		 {
-		     wndPtr->flags |= WIN_RESTORE_MAX;
-		     wndPtr->dwStyle &= ~WS_MAXIMIZE;
-                 }
-                 else
-		     wndPtr->flags &= ~WIN_RESTORE_MAX;
-		 wndPtr->dwStyle |= WS_MINIMIZE;
-
-                 if( wndPtr->pDriver->pSetHostAttr( wndPtr, HAK_ICONICSTATE, TRUE ) )
-                     swpFlags |= MINMAX_NOSWP;
-
-		 lpPos->ptIconPos = WINPOS_FindIconPos( wndPtr, lpPos->ptIconPos );
-
-		 SetRect16( lpRect, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
-				    GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
-		 swpFlags |= SWP_NOCOPYBITS;
-		 break;
-
-	    case SW_MAXIMIZE:
-                CONV_POINT16TO32( &lpPos->ptMaxPos, &pt );
-                WINPOS_GetMinMaxInfo( wndPtr, &size, &pt, NULL, NULL );
-                CONV_POINT32TO16( &pt, &lpPos->ptMaxPos );
-
-		 if( wndPtr->dwStyle & WS_MINIMIZE )
-		 {
-                     wndPtr->pDriver->pSetHostAttr( wndPtr, HAK_ICONICSTATE, FALSE );
-
-		     WINPOS_ShowIconTitle( wndPtr, FALSE );
-		     wndPtr->dwStyle &= ~WS_MINIMIZE;
-		 }
-                 wndPtr->dwStyle |= WS_MAXIMIZE;
-
-		 SetRect16( lpRect, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
-				    size.x, size.y );
-		 break;
-
-	    case SW_RESTORE:
-		 if( wndPtr->dwStyle & WS_MINIMIZE )
-		 {
-                     wndPtr->pDriver->pSetHostAttr( wndPtr, HAK_ICONICSTATE, FALSE );
-
-		     wndPtr->dwStyle &= ~WS_MINIMIZE;
-		     WINPOS_ShowIconTitle( wndPtr, FALSE );
-
-		     if( wndPtr->flags & WIN_RESTORE_MAX)
-		     {
-			 /* Restore to maximized position */
-                         CONV_POINT16TO32( &lpPos->ptMaxPos, &pt );
-                         WINPOS_GetMinMaxInfo( wndPtr, &size, &pt, NULL, NULL);
-                         CONV_POINT32TO16( &pt, &lpPos->ptMaxPos );
-			 wndPtr->dwStyle |= WS_MAXIMIZE;
-			 SetRect16( lpRect, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y, size.x, size.y );
-			 break;
-		     }
-		 } 
-		 else 
-		     if( !(wndPtr->dwStyle & WS_MAXIMIZE) ) return (UINT16)(-1);
- 		     else wndPtr->dwStyle &= ~WS_MAXIMIZE;
-
-		 /* Restore to normal position */
-
-		*lpRect = lpPos->rectNormal; 
-		 lpRect->right -= lpRect->left; 
-		 lpRect->bottom -= lpRect->top;
-
-		 break;
-	}
-    } else swpFlags |= SWP_NOSIZE | SWP_NOMOVE;
-    return swpFlags;
-}
-
-/***********************************************************************
  *		ShowWindowAsync (USER32.@)
  *
  * doesn't wait; returns immediately.
@@ -1332,140 +1177,9 @@
 /***********************************************************************
  *		ShowWindow (USER32.@)
  */
-BOOL WINAPI ShowWindow( HWND hwnd, INT cmd ) 
-{    
-    WND* 	wndPtr = WIN_FindWndPtr( hwnd );
-    BOOL 	wasVisible, showFlag;
-    RECT16 	newPos = {0, 0, 0, 0};
-    UINT 	swp = 0;
-
-    if (!wndPtr) return FALSE;
-
-    TRACE("hwnd=%04x, cmd=%d\n", hwnd, cmd);
-
-    wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
-
-    switch(cmd)
-    {
-        case SW_HIDE:
-            if (!wasVisible) goto END;;
-	    swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | 
-		        SWP_NOACTIVATE | SWP_NOZORDER;
-	    break;
-
-	case SW_SHOWMINNOACTIVE:
-            swp |= SWP_NOACTIVATE | SWP_NOZORDER;
-            /* fall through */
-	case SW_SHOWMINIMIZED:
-            swp |= SWP_SHOWWINDOW;
-            /* fall through */
-	case SW_MINIMIZE:
-            swp |= SWP_FRAMECHANGED;
-            if( !(wndPtr->dwStyle & WS_MINIMIZE) )
-		 swp |= WINPOS_MinMaximize( wndPtr, SW_MINIMIZE, &newPos );
-            else swp |= SWP_NOSIZE | SWP_NOMOVE;
-	    break;
-
-	case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
-            swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
-            if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
-		 swp |= WINPOS_MinMaximize( wndPtr, SW_MAXIMIZE, &newPos );
-            else swp |= SWP_NOSIZE | SWP_NOMOVE;
-            break;
-
-	case SW_SHOWNA:
-            swp |= SWP_NOACTIVATE | SWP_NOZORDER;
-            /* fall through */
-	case SW_SHOW:
-	    swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
-
-	    /*
-	     * ShowWindow has a little peculiar behavior that if the
-	     * window is already the topmost window, it will not
-	     * activate it.
-	     */
-	    if (GetTopWindow((HWND)0)==hwnd && (wasVisible || GetActiveWindow() == hwnd))
-	      swp |= SWP_NOACTIVATE;
-
-	    break;
-
-	case SW_SHOWNOACTIVATE:
-            swp |= SWP_NOZORDER;
-            if (GetActiveWindow()) swp |= SWP_NOACTIVATE;
-            /* fall through */
-	case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
-	case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
-	case SW_RESTORE:
-	    swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
-
-            if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
-		 swp |= WINPOS_MinMaximize( wndPtr, SW_RESTORE, &newPos );
-            else swp |= SWP_NOSIZE | SWP_NOMOVE;
-	    break;
-    }
-
-    showFlag = (cmd != SW_HIDE);
-    if (showFlag != wasVisible)
-    {
-        SendMessageA( hwnd, WM_SHOWWINDOW, showFlag, 0 );
-        if (!IsWindow( hwnd )) goto END;
-    }
-
-    if ((wndPtr->dwStyle & WS_CHILD) &&
-        !IsWindowVisible( wndPtr->parent->hwndSelf ) &&
-        (swp & (SWP_NOSIZE | SWP_NOMOVE)) == (SWP_NOSIZE | SWP_NOMOVE) )
-    {
-        /* Don't call SetWindowPos() on invisible child windows */
-        if (cmd == SW_HIDE) wndPtr->dwStyle &= ~WS_VISIBLE;
-        else wndPtr->dwStyle |= WS_VISIBLE;
-    }
-    else
-    {
-        /* We can't activate a child window */
-        if ((wndPtr->dwStyle & WS_CHILD) &&
-            !(wndPtr->dwExStyle & WS_EX_MDICHILD))
-            swp |= SWP_NOACTIVATE | SWP_NOZORDER;
-	if (!(swp & MINMAX_NOSWP))
-        {
-	    SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top, 
-					  newPos.right, newPos.bottom, LOWORD(swp) );
-            if (cmd == SW_HIDE)
-            {
-               /* FIXME: This will cause the window to be activated irrespective
-                * of whether it is owned by the same thread. Has to be done
-                * asynchronously.
-                */
-
-               if (hwnd == GetActiveWindow())
-                  WINPOS_ActivateOtherWindow(wndPtr);
-
-               /* Revert focus to parent */
-               if (hwnd == GetFocus() || IsChild(hwnd, GetFocus()))
-                  SetFocus( GetParent(hwnd) );
-            }
-        }
-        if (!IsWindow( hwnd )) goto END;
-	else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( wndPtr, TRUE );
-    }
-
-    if (wndPtr->flags & WIN_NEED_SIZE)
-    {
-        /* should happen only in CreateWindowEx() */
-	int wParam = SIZE_RESTORED;
-
-	wndPtr->flags &= ~WIN_NEED_SIZE;
-	if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
-	else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
-	SendMessageA( hwnd, WM_SIZE, wParam,
-		     MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
-			    wndPtr->rectClient.bottom-wndPtr->rectClient.top));
-	SendMessageA( hwnd, WM_MOVE, 0,
-		   MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
-    }
-
-END:
-    WIN_ReleaseWndPtr(wndPtr);
-    return wasVisible;
+BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
+{
+    return USER_Driver.pShowWindow( hwnd, cmd );
 }
 
 
diff --git a/windows/x11drv/clipboard.c b/windows/x11drv/clipboard.c
index 9de7f5f..b115853 100644
--- a/windows/x11drv/clipboard.c
+++ b/windows/x11drv/clipboard.c
@@ -315,7 +315,6 @@
     Display *display = thread_display();
     HWND           hWnd = 0;
     HWND           hWndClipWindow = GetOpenClipboardWindow();
-    WND*           wnd = NULL;
     XEvent         xe;
     Atom           aTargets;
     Atom           atype=AnyPropertyType;
@@ -343,10 +342,7 @@
     /*
      * Query the selection owner for the TARGETS property
      */
-    wnd = WIN_FindWndPtr(hWnd);
-    w = X11DRV_WND_FindXWindow(wnd);
-    WIN_ReleaseWndPtr(wnd);
-    wnd = NULL;
+    w = X11DRV_get_top_window(hWnd);
 
     aTargets = TSXInternAtom(display, "TARGETS", False);
 
@@ -863,9 +859,7 @@
     if ( !(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)) )
     {
         Atom xaClipboard = TSXInternAtom(display, _CLIPBOARD, False);
-        WND *tmpWnd = WIN_FindWndPtr( hWndClipWindow ? hWndClipWindow : AnyPopup() );
-        owner = X11DRV_WND_FindXWindow(tmpWnd );
-        WIN_ReleaseWndPtr(tmpWnd);
+        owner = X11DRV_get_top_window( hWndClipWindow ? hWndClipWindow : AnyPopup() );
 
         /* Grab PRIMARY selection if not owned */
         if ( !(selectionAcquired & S_PRIMARY) )
@@ -1012,18 +1006,15 @@
     BOOL bRet = selectionAcquired;
     HWND hWndClipWindow = GetOpenClipboardWindow();
     HWND hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow();
-    WND* wnd = NULL;
     LPWINE_CLIPFORMAT lpFormat;
 
     TRACE("%d\n", wFormat);
 
-    if( !selectionAcquired && (wnd = WIN_FindWndPtr(hWnd)) )
+    if (!selectionAcquired)
     {
 	XEvent xe;
         Atom propRequest;
-	Window w = X11DRV_WND_FindXWindow(wnd);
-        WIN_ReleaseWndPtr(wnd);
-        wnd = NULL;
+	Window w = X11DRV_get_top_window(hWnd);
 
         /* Map the format ID requested to an X selection property.
          * If the format is in the cache, use the atom associated
@@ -1089,7 +1080,7 @@
 {
     Display *display = thread_display();
     HWND hWndClipOwner = 0;
-    Window XWnd = X11DRV_WND_GetXWindow(pWnd);
+    Window XWnd = get_whole_window(pWnd);
     Atom xaClipboard;
     BOOL bLostSelection = FALSE;
 
@@ -1118,10 +1109,10 @@
     selectionWindow = None;
 
     if( pWnd->next ) 
-        selectionWindow = X11DRV_WND_GetXWindow(pWnd->next);
+        selectionWindow = get_whole_window(pWnd->next);
     else if( pWnd->parent )
          if( pWnd->parent->child != pWnd ) 
-             selectionWindow = X11DRV_WND_GetXWindow(pWnd->parent->child);
+             selectionWindow = get_whole_window(pWnd->parent->child);
 
     if( selectionWindow != None )
     {
diff --git a/windows/x11drv/event.c b/windows/x11drv/event.c
index 9757619..5ac098a 100644
--- a/windows/x11drv/event.c
+++ b/windows/x11drv/event.c
@@ -51,7 +51,7 @@
 extern Atom dndSelection;
 
 extern void X11DRV_KEYBOARD_UpdateState(void);
-extern void X11DRV_KEYBOARD_HandleEvent(WND *pWnd, XKeyEvent *event);
+extern void X11DRV_KEYBOARD_HandleEvent( XKeyEvent *event, int x, int y );
 
 #define NB_BUTTONS      5     /* Windows can handle 3 buttons and the wheel too */
 
@@ -97,16 +97,16 @@
 static void EVENT_FocusIn( HWND hWnd, XFocusChangeEvent *event );
 static void EVENT_FocusOut( HWND hWnd, XFocusChangeEvent *event );
 static void EVENT_Expose( HWND hWnd, XExposeEvent *event );
-static void EVENT_GraphicsExpose( HWND hWnd, XGraphicsExposeEvent *event );
-static void EVENT_ConfigureNotify( HWND hWnd, XConfigureEvent *event );
 static void EVENT_SelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple );
 static void EVENT_SelectionClear( HWND hWnd, XSelectionClearEvent *event);
 static void EVENT_PropertyNotify( XPropertyEvent *event );
 static void EVENT_ClientMessage( HWND hWnd, XClientMessageEvent *event );
-static void EVENT_MapNotify( HWND pWnd, XMapEvent *event );
-static void EVENT_UnmapNotify( HWND pWnd, XUnmapEvent *event );
 static void EVENT_MappingNotify( XMappingEvent *event );
 
+extern void X11DRV_MapNotify( HWND hwnd, XMapEvent *event );
+extern void X11DRV_UnmapNotify( HWND hwnd, XUnmapEvent *event );
+extern void X11DRV_ConfigureNotify( HWND hwnd, XConfigureEvent *event );
+
 #ifdef HAVE_LIBXXF86DGA2
 static int DGAMotionEventType;
 static int DGAButtonPressEventType;
@@ -122,14 +122,6 @@
 static void EVENT_DGAButtonReleaseEvent( XDGAButtonEvent *event );
 #endif
 
-/* Usable only with OLVWM - compile option perhaps?
-static void EVENT_EnterNotify( HWND hWnd, XCrossingEvent *event );
-*/
-
-static void EVENT_GetGeometry( Display *display, Window win, int *px, int *py,
-                               unsigned int *pwidth, unsigned int *pheight );
-
-
 /* Static used for the current input method */
 static INPUT_TYPE current_input_type = X11DRV_INPUT_ABSOLUTE;
 static BOOL in_transition = FALSE; /* This is not used as for today */
@@ -138,31 +130,22 @@
 /***********************************************************************
  *           process_events
  */
-static void process_events( Display *display )
+static int process_events( struct x11drv_thread_data *data )
 {
     XEvent event;
+    int count = 0;
 
     wine_tsx11_lock();
-    while ( XPending( display ) )
+    while ( XPending( data->display ) )
     {
-        XNextEvent( display, &event );
+        XNextEvent( data->display, &event );
         wine_tsx11_unlock();
         EVENT_ProcessEvent( &event );
+        count++;
         wine_tsx11_lock();
     }
     wine_tsx11_unlock();
-}
-
-/***********************************************************************
- *		X11DRV_Synchronize
- *
- * Synchronize with the X server. Should not be used too often.
- */
-void X11DRV_Synchronize( void )
-{
-    Display *display = thread_display();
-    TSXSync( display, False );
-    process_events( display );
+    return count;
 }
 
 
@@ -183,14 +166,19 @@
     for (i = 0; i < count; i++) new_handles[i] = handles[i];
     new_handles[count] = data->display_fd;
 
-    data->process_event_count++;
     wine_tsx11_lock();
     XFlush( gdi_display );
     XFlush( data->display );
     wine_tsx11_unlock();
-    ret = WaitForMultipleObjectsEx( count+1, new_handles, flags & MWMO_WAITALL,
-                                    timeout, flags & MWMO_ALERTABLE );
-    if (ret == count) process_events( data->display );
+
+    data->process_event_count++;
+    if (process_events( data )) ret = count;
+    else
+    {
+        ret = WaitForMultipleObjectsEx( count+1, new_handles, flags & MWMO_WAITALL,
+                                        timeout, flags & MWMO_ALERTABLE );
+        if (ret == count) process_events( data );
+    }
     data->process_event_count--;
     return ret;
 }
@@ -272,33 +260,20 @@
       ke.keycode = evt->keycode;
       ke.same_screen = TRUE;
       
-      X11DRV_KEYBOARD_HandleEvent(NULL, &ke);
+      X11DRV_KEYBOARD_HandleEvent(&ke,pt.x,pt.y);
       return;
     }
   }
 #endif
-  
-  if ( TSXFindContext( display, event->xany.window, winContext,
-		       (char **)&hWnd ) != 0) {
-    if ( event->type == ClientMessage) {
-      /* query window (drag&drop event contains only drag window) */
-      Window   	root, child;
-      int      	root_x, root_y, child_x, child_y;
-      unsigned	u;
-      TSXQueryPointer( display, root_window, &root, &child,
-		       &root_x, &root_y, &child_x, &child_y, &u);
-      if (TSXFindContext( display, child, winContext, (char **)&hWnd ) != 0)
-	return;
-    } else {
+
+  if (TSXFindContext( display, event->xany.window, winContext, (char **)&hWnd ) != 0)
       hWnd = 0;  /* Not for a registered window */
-    }
-  }
 
   if ( !hWnd && event->xany.window != root_window
              && event->type != PropertyNotify 
              && event->type != MappingNotify)
-      ERR("Got event %s for unknown Window %08lx\n",
-          event_names[event->type], event->xany.window );
+      WARN( "Got event %s for unknown Window %08lx\n",
+            event_names[event->type], event->xany.window );
   else
       TRACE("Got event %s for hwnd %04x\n",
             event_names[event->type], hWnd );
@@ -334,65 +309,22 @@
 					MotionNotify, event));    
       EVENT_MotionNotify( hWnd, (XMotionEvent*)event );
       break;
-      
+
     case FocusIn:
-    {
-      WND *pWndLastFocus = 0;
-      XWindowAttributes win_attr;
-      BOOL bIsDisabled;
-      XFocusChangeEvent *xfocChange = (XFocusChangeEvent*)event;
-
-      if (!hWnd) return;
-
-      bIsDisabled = GetWindowLongA( hWnd, GWL_STYLE ) & WS_DISABLED;
-
-      /* If the window has been disabled and we are in managed mode,
-       * revert the X focus back to the last focus window. This is to disallow
-       * the window manager from switching focus away while the app is
-       * in a modal state.
-       */
-      if ( Options.managed && bIsDisabled && glastXFocusWin)
-      {
-        /* Change focus only if saved focus window is registered and viewable */
-        if ( TSXFindContext( xfocChange->display, glastXFocusWin, winContext,
-                             (char **)&pWndLastFocus ) == 0 )
-        {
-          if ( TSXGetWindowAttributes( display, glastXFocusWin, &win_attr ) &&
-                 (win_attr.map_state == IsViewable) )
-          {
-            TSXSetInputFocus( xfocChange->display, glastXFocusWin, RevertToParent, CurrentTime );
-            X11DRV_Synchronize();
-            break;
-          }
-        }
-      }
-       
-      EVENT_FocusIn( hWnd, xfocChange );
+      EVENT_FocusIn( hWnd, (XFocusChangeEvent*)event );
       break;
-    }
-      
+
     case FocusOut:
-    {
-      /* Save the last window which had the focus */
-      XFocusChangeEvent *xfocChange = (XFocusChangeEvent*)event;
-      glastXFocusWin = xfocChange->window;
-      if (!hWnd) return;
-      if (GetWindowLongA( hWnd, GWL_STYLE ) & WS_DISABLED) glastXFocusWin = 0;
       EVENT_FocusOut( hWnd, (XFocusChangeEvent*)event );
       break;
-    }
-      
+
     case Expose:
-      EVENT_Expose( hWnd, (XExposeEvent *)event );
+      EVENT_Expose( hWnd, &event->xexpose );
       break;
-      
-    case GraphicsExpose:
-      EVENT_GraphicsExpose( hWnd, (XGraphicsExposeEvent *)event );
-      break;
-      
+
     case ConfigureNotify:
       if (!hWnd) return;
-      EVENT_ConfigureNotify( hWnd, (XConfigureEvent*)event );
+      X11DRV_ConfigureNotify( hWnd, &event->xconfigure );
       break;
 
     case SelectionRequest:
@@ -414,23 +346,15 @@
       EVENT_ClientMessage( hWnd, (XClientMessageEvent *) event );
       break;
 
-#if 0
-    case EnterNotify:
-      EVENT_EnterNotify( hWnd, (XCrossingEvent *) event );
-      break;
-#endif
-
     case NoExpose:
       break;
-      
+
     case MapNotify:
-      if (!hWnd) return;
-      EVENT_MapNotify( hWnd, (XMapEvent *)event );
+      X11DRV_MapNotify( hWnd, (XMapEvent *)event );
       break;
 
     case UnmapNotify:
-      if (!hWnd) return;
-      EVENT_UnmapNotify( hWnd, (XUnmapEvent *)event );
+      X11DRV_UnmapNotify( hWnd, (XUnmapEvent *)event );
       break;
 
     case MappingNotify:
@@ -446,136 +370,6 @@
 }
 
 /***********************************************************************
- *           EVENT_QueryZOrder
- *
- * Synchronize internal z-order with the window manager's.
- */
-static BOOL __check_query_condition( WND** pWndA, WND** pWndB )
-{
-  /* return TRUE if we have at least two managed windows */
-  
-  for( *pWndB = NULL; *pWndA; *pWndA = (*pWndA)->next )
-    if( ((*pWndA)->dwExStyle & WS_EX_MANAGED) &&
-	((*pWndA)->dwStyle & WS_VISIBLE )) break;
-  if( *pWndA )
-    for( *pWndB = (*pWndA)->next; *pWndB; *pWndB = (*pWndB)->next )
-      if( ((*pWndB)->dwExStyle & WS_EX_MANAGED) &&
-	  ((*pWndB)->dwStyle & WS_VISIBLE )) break;
-  return ((*pWndB) != NULL);
-}
-
-static Window __get_common_ancestor( Display *display, Window A, Window B,
-                                     Window** children, unsigned* total )
-{
-    /* find the real root window */
-  
-    Window      root, *childrenB;
-    unsigned    totalB;
-
-    while( A != B && A && B )
-    {
-      TSXQueryTree( display, A, &root, &A, children, total );
-      TSXQueryTree( display, B, &root, &B, &childrenB, &totalB );
-      if( childrenB ) TSXFree( childrenB );
-      if( *children ) TSXFree( *children ), *children = NULL;
-    }
-
-    if( A && B )
-    {
-	TSXQueryTree( display, A, &root, &B, children, total );
-	return A;
-    }
-    return 0 ;
-}
-
-static Window __get_top_decoration( Display *display, Window w, Window ancestor )
-{
-  Window*     children, root, prev = w, parent = w;
-  unsigned    total;
-  
-  do
-    {
-      w = parent;
-      TSXQueryTree( display, w, &root, &parent, &children, &total );
-      if( children ) TSXFree( children );
-    } while( parent && parent != ancestor );
-  TRACE("\t%08x -> %08x\n", (unsigned)prev, (unsigned)w );
-  return ( parent ) ? w : 0 ;
-}
-
-static unsigned __td_lookup( Window w, Window* list, unsigned max )
-{
-  unsigned    i;
-  for( i = max - 1; i >= 0; i-- ) if( list[i] == w ) break;
-  return i;
-}
-
-static HWND EVENT_QueryZOrder( Display *display, HWND hWndCheck)
-{
-  HWND      hwndInsertAfter = HWND_TOP;
-  WND      *pWndCheck = WIN_FindWndPtr(hWndCheck);
-  WND      *pDesktop = WIN_GetDesktop();
-  WND      *pWnd, *pWndZ = WIN_LockWndPtr(pDesktop->child);
-  Window      w, parent, *children = NULL;
-  unsigned    total, check, pos, best;
-  
-  if( !__check_query_condition(&pWndZ, &pWnd) )
-  {
-      WIN_ReleaseWndPtr(pWndCheck);
-      WIN_ReleaseWndPtr(pDesktop->child);
-      WIN_ReleaseDesktop();
-      return hwndInsertAfter;
-  }
-  WIN_LockWndPtr(pWndZ);
-  WIN_LockWndPtr(pWnd);
-  WIN_ReleaseWndPtr(pDesktop->child);
-  WIN_ReleaseDesktop();
-  
-  parent = __get_common_ancestor( display, X11DRV_WND_GetXWindow(pWndZ),
-				  X11DRV_WND_GetXWindow(pWnd),
-				  &children, &total );
-  if( parent && children )
-  {
-      /* w is the ancestor if pWndCheck that is a direct descendant of 'parent' */
-
-      w = __get_top_decoration( display, X11DRV_WND_GetXWindow(pWndCheck), parent );
-
-      if( w != children[total-1] ) /* check if at the top */
-      {
-	  /* X child at index 0 is at the bottom, at index total-1 is at the top */
-	  check = __td_lookup( w, children, total );
-	  best = total;
-
-          for( WIN_UpdateWndPtr(&pWnd,pWndZ); pWnd;WIN_UpdateWndPtr(&pWnd,pWnd->next))
-          {
-	      /* go through all windows in Wine z-order... */
-
-	      if( pWnd != pWndCheck )
-              {
-		  if( !(pWnd->dwExStyle & WS_EX_MANAGED) ||
-		      !(w = __get_top_decoration( display, X11DRV_WND_GetXWindow(pWnd), parent )) )
-		    continue;
-		  pos = __td_lookup( w, children, total );
-		  if( pos < best && pos > check )
-                  {
-		      /* find a nearest Wine window precedes 
-		       * pWndCheck in the real z-order... */
-		      best = pos;
-		      hwndInsertAfter = pWnd->hwndSelf;
-                  }
-		  if( best - check == 1 ) break;
-              }
-          }
-      }
-  }
-  if( children ) TSXFree( children );
-  WIN_ReleaseWndPtr(pWnd);
-  WIN_ReleaseWndPtr(pWndZ);
-  WIN_ReleaseWndPtr(pWndCheck);
-  return hwndInsertAfter;
-}
-
-/***********************************************************************
  *           X11DRV_EVENT_XStateToKeyState
  *
  * Translate a X event state (Button1Mask, ShiftMask, etc...) to
@@ -593,62 +387,74 @@
   return kstate;
 }
 
+
 /***********************************************************************
  *           EVENT_Expose
  */
-static void EVENT_Expose( HWND hWnd, XExposeEvent *event )
+static void EVENT_Expose( HWND hwnd, XExposeEvent *event )
 {
-  RECT rect;
-  int  offx = 0,offy = 0;
+    RECT rect;
+    struct x11drv_win_data *data;
+    int flags = RDW_INVALIDATE | RDW_ERASE;
+    WND *win;
 
-  WND *pWnd = WIN_FindWndPtr(hWnd);
-  /* Make position relative to client area instead of window */
-  offx =  (pWnd? (pWnd->rectClient.left - pWnd->rectWindow.left) : 0);
-  offy =  (pWnd? (pWnd->rectClient.top - pWnd->rectWindow.top) : 0);
+    TRACE( "win %x (%lx) %d,%d %dx%d\n",
+           hwnd, event->window, event->x, event->y, event->width, event->height );
 
-  rect.left   = event->x - offx;
-  rect.top    = event->y - offy;
+    rect.left   = event->x;
+    rect.top    = event->y;
+    rect.right  = rect.left + event->width;
+    rect.bottom = rect.top + event->height;
 
-  rect.right  = rect.left + event->width;
-  rect.bottom = rect.top + event->height;
+    if (!(win = WIN_FindWndPtr(hwnd))) return;
+    data = win->pDriverData;
 
-  WIN_ReleaseWndPtr(pWnd);
+    if (event->window != data->client_window)  /* whole window or icon window */
+    {
+        flags |= RDW_FRAME;
+        /* make position relative to client area instead of window */
+        OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
+    }
 
-  RedrawWindow( hWnd, &rect, 0, RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
-
-  if (event->count == 0)
-    SendNotifyMessageA(hWnd,WM_SYNCPAINT, 0, 0);
+    /* find the top level parent that doesn't clip children and invalidate the area */
+    /* on the parent (which will invalidate all the children too) */
+    while (win->parent && win->parent->hwndSelf != GetDesktopWindow() &&
+           !(win->parent->dwStyle & WS_CLIPCHILDREN))
+    {
+        OffsetRect( &rect, win->rectClient.left, win->rectClient.top );
+        WIN_UpdateWndPtr( &win, win->parent );
+        flags &= ~RDW_FRAME;  /* parent will invalidate children frame anyway */
+    }
+    hwnd = win->hwndSelf;
+    WIN_ReleaseWndPtr(win);
+    RedrawWindow( hwnd, &rect, 0, flags );
 }
 
 
-/***********************************************************************
- *           EVENT_GraphicsExpose
- *
- * This is needed when scrolling area is partially obscured
- * by non-Wine X window.
- */
-static void EVENT_GraphicsExpose( HWND hWnd, XGraphicsExposeEvent *event )
+/* get the coordinates of a mouse event */
+static void get_coords( HWND *hwnd, Window window, int x, int y, POINT *pt )
 {
-  RECT rect;
-  int  offx = 0,offy = 0;
+    struct x11drv_win_data *data;
+    WND *win;
 
-  WND *pWnd = WIN_FindWndPtr(hWnd);
-  /* Make position relative to client area instead of window */
-  offx =  (pWnd? (pWnd->rectClient.left - pWnd->rectWindow.left) : 0);
-  offy =  (pWnd? (pWnd->rectClient.top - pWnd->rectWindow.top) : 0);
+    if (!(win = WIN_FindWndPtr( *hwnd ))) return;
+    data = win->pDriverData;
 
-  rect.left   = event->x - offx;
-  rect.top    = event->y - offy;
-
-  rect.right  = rect.left + event->width;
-  rect.bottom = rect.top + event->height;
-
-  WIN_ReleaseWndPtr(pWnd);
-
-  RedrawWindow( hWnd, &rect, 0, RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_ERASE );
-
-  if (event->count == 0)
-    SendNotifyMessageA(hWnd,WM_SYNCPAINT, 0, 0);
+    if (window == data->whole_window)
+    {
+        x -= data->client_rect.left;
+        y -= data->client_rect.top;
+    }
+    while (win->parent && win->parent->hwndSelf != GetDesktopWindow())
+    {
+        x += win->rectClient.left;
+        y += win->rectClient.top;
+        WIN_UpdateWndPtr( &win, win->parent );
+    }
+    pt->x = x + win->rectClient.left;
+    pt->y = y + win->rectClient.top;
+    *hwnd = win->hwndSelf;
+    WIN_ReleaseWndPtr( win );
 }
 
 
@@ -659,10 +465,9 @@
  */
 static void EVENT_Key( HWND hWnd, XKeyEvent *event )
 {
-    WND *pWnd = WIN_FindWndPtr(hWnd);
-  X11DRV_KEYBOARD_HandleEvent( pWnd, event );
-    WIN_ReleaseWndPtr(pWnd);
-
+    POINT pt;
+    get_coords( &hWnd, event->window, event->x, event->y, &pt );
+    X11DRV_KEYBOARD_HandleEvent( event, pt.x, pt.y );
 }
 
 
@@ -671,22 +476,22 @@
  */
 static void EVENT_MotionNotify( HWND hWnd, XMotionEvent *event )
 {
-  if (current_input_type == X11DRV_INPUT_ABSOLUTE) {
-    WND *pWnd = WIN_FindWndPtr(hWnd);
-    int xOffset = pWnd? pWnd->rectWindow.left : 0;
-    int yOffset = pWnd? pWnd->rectWindow.top  : 0;
-    WIN_ReleaseWndPtr(pWnd);
-    
-    X11DRV_SendEvent( MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, 
-                            xOffset + event->x, yOffset + event->y,
-                            X11DRV_EVENT_XStateToKeyState( event->state ), 
-                            event->time - X11DRV_server_startticks, hWnd);
-  } else {
-    X11DRV_SendEvent( MOUSEEVENTF_MOVE,
-                            event->x_root, event->y_root,
-                            X11DRV_EVENT_XStateToKeyState( event->state ), 
-                            event->time - X11DRV_server_startticks, hWnd);
-  }
+    POINT pt;
+
+    if (current_input_type == X11DRV_INPUT_ABSOLUTE)
+    {
+        get_coords( &hWnd, event->window, event->x, event->y, &pt );
+        X11DRV_SendEvent( MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, pt.x, pt.y,
+                          X11DRV_EVENT_XStateToKeyState( event->state ),
+                          event->time - X11DRV_server_startticks, hWnd);
+    }
+    else
+    {
+        X11DRV_SendEvent( MOUSEEVENTF_MOVE,
+                          event->x_root, event->y_root,
+                          X11DRV_EVENT_XStateToKeyState( event->state ),
+                          event->time - X11DRV_server_startticks, hWnd);
+    }
 }
 
 
@@ -695,51 +500,46 @@
  */
 static void EVENT_ButtonPress( HWND hWnd, XButtonEvent *event )
 {
-  static WORD statusCodes[NB_BUTTONS] = 
-  { MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_MIDDLEDOWN, MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_WHEEL, MOUSEEVENTF_WHEEL};
-  int buttonNum = event->button - 1;
-  
-  WND *pWnd = WIN_FindWndPtr(hWnd);
-  int xOffset = pWnd? pWnd->rectWindow.left : 0;
-  int yOffset = pWnd? pWnd->rectWindow.top  : 0;
-  WORD keystate,wData = 0;
-  
-  WIN_ReleaseWndPtr(pWnd);
+    static const WORD statusCodes[NB_BUTTONS] = { MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_MIDDLEDOWN,
+                                                  MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_WHEEL,
+                                                  MOUSEEVENTF_WHEEL};
+    int buttonNum = event->button - 1;
+    WORD keystate, wData = 0;
+    POINT pt;
 
-  if (buttonNum >= NB_BUTTONS) return;
+    if (buttonNum >= NB_BUTTONS) return;
 
-  /*
-   * Get the compatible keystate
-   */
-  keystate = X11DRV_EVENT_XStateToKeyState( event->state );
-  
-  /*
-   * Make sure that the state of the button that was just 
-   * pressed is "down".
-   */
-  switch (buttonNum)
-  {
+    get_coords( &hWnd, event->window, event->x, event->y, &pt );
+
+    /* Get the compatible keystate */
+    keystate = X11DRV_EVENT_XStateToKeyState( event->state );
+
+    /*
+     * Make sure that the state of the button that was just 
+     * pressed is "down".
+     */
+    switch (buttonNum)
+    {
     case 0:
-      keystate |= MK_LBUTTON;
-      break;
+        keystate |= MK_LBUTTON;
+        break;
     case 1:
-      keystate |= MK_MBUTTON;
-      break;
+        keystate |= MK_MBUTTON;
+        break;
     case 2:
-      keystate |= MK_RBUTTON;
-      break;
+        keystate |= MK_RBUTTON;
+        break;
     case 3:
         wData = WHEEL_DELTA;
         break;
     case 4:
         wData = -WHEEL_DELTA;
         break;
-  }
-  
-  X11DRV_SendEvent( statusCodes[buttonNum], 
-                          xOffset + event->x, yOffset + event->y,
-                          MAKEWPARAM(keystate,wData),
-                          event->time - X11DRV_server_startticks, hWnd);
+    }
+
+    X11DRV_SendEvent( statusCodes[buttonNum], pt.x, pt.y,
+                      MAKEWPARAM(keystate,wData),
+                      event->time - X11DRV_server_startticks, hWnd);
 }
 
 
@@ -748,45 +548,39 @@
  */
 static void EVENT_ButtonRelease( HWND hWnd, XButtonEvent *event )
 {
-  static WORD statusCodes[NB_BUTTONS] = 
-  { MOUSEEVENTF_LEFTUP, MOUSEEVENTF_MIDDLEUP, MOUSEEVENTF_RIGHTUP };
-  int buttonNum = event->button - 1;
-  WND *pWnd = WIN_FindWndPtr(hWnd);
-  int xOffset = pWnd? pWnd->rectWindow.left : 0;
-  int yOffset = pWnd? pWnd->rectWindow.top  : 0;
-  WORD keystate;
-  
-  WIN_ReleaseWndPtr(pWnd);
-  
-  if (buttonNum >= NB_BUTTONS) return;    
-  
-  /*
-   * Get the compatible keystate
-   */
-  keystate = X11DRV_EVENT_XStateToKeyState( event->state );
+    static const WORD statusCodes[NB_BUTTONS] = { MOUSEEVENTF_LEFTUP, MOUSEEVENTF_MIDDLEUP,
+                                                  MOUSEEVENTF_RIGHTUP, 0, 0 };
+    int buttonNum = event->button - 1;
+    WORD keystate;
+    POINT pt;
 
-  /*
-   * Make sure that the state of the button that was just 
-   * released is "up".
-   */
-  switch (buttonNum)
-  {
+    if (buttonNum >= NB_BUTTONS) return;
+
+    get_coords( &hWnd, event->window, event->x, event->y, &pt );
+
+    /* Get the compatible keystate */
+    keystate = X11DRV_EVENT_XStateToKeyState( event->state );
+
+    /*
+     * Make sure that the state of the button that was just 
+     * released is "up".
+     */
+    switch (buttonNum)
+    {
     case 0:
-      keystate &= ~MK_LBUTTON;
-      break;
+        keystate &= ~MK_LBUTTON;
+        break;
     case 1:
-      keystate &= ~MK_MBUTTON;
-      break;
+        keystate &= ~MK_MBUTTON;
+        break;
     case 2:
-      keystate &= ~MK_RBUTTON;
-      break;
-  default:
-      return;
-  }
-
-  X11DRV_SendEvent( statusCodes[buttonNum], 
-                          xOffset + event->x, yOffset + event->y,
-                          keystate, event->time - X11DRV_server_startticks, hWnd);
+        keystate &= ~MK_RBUTTON;
+        break;
+    default:
+        return;
+    }
+    X11DRV_SendEvent( statusCodes[buttonNum], pt.x, pt.y,
+                      keystate, event->time - X11DRV_server_startticks, hWnd);
 }
 
 
@@ -795,12 +589,42 @@
  */
 static void EVENT_FocusIn( HWND hWnd, XFocusChangeEvent *event )
 {
-    if (event->detail != NotifyPointer)
-        if (hWnd != GetForegroundWindow())
+    WND *pWndLastFocus;
+    XWindowAttributes win_attr;
+    BOOL bIsDisabled;
+
+    if (!hWnd) return;
+
+    bIsDisabled = GetWindowLongA( hWnd, GWL_STYLE ) & WS_DISABLED;
+
+    /* If the window has been disabled and we are in managed mode,
+       * revert the X focus back to the last focus window. This is to disallow
+       * the window manager from switching focus away while the app is
+       * in a modal state.
+       */
+    if ( Options.managed && bIsDisabled && glastXFocusWin)
+    {
+        /* Change focus only if saved focus window is registered and viewable */
+        wine_tsx11_lock();
+        if (XFindContext( event->display, glastXFocusWin, winContext,
+                           (char **)&pWndLastFocus ) == 0 )
         {
-            SetForegroundWindow( hWnd );
-	    X11DRV_KEYBOARD_UpdateState();
+            if (XGetWindowAttributes( event->display, glastXFocusWin, &win_attr ) &&
+                (win_attr.map_state == IsViewable) )
+            {
+                XSetInputFocus( event->display, glastXFocusWin, RevertToParent, CurrentTime );
+                wine_tsx11_unlock();
+                return;
+            }
         }
+        wine_tsx11_unlock();
+    }
+
+    if (event->detail != NotifyPointer && hWnd != GetForegroundWindow())
+    {
+        SetForegroundWindow( hWnd );
+        X11DRV_KEYBOARD_UpdateState();
+    }
 }
 
 
@@ -811,25 +635,27 @@
  */
 static void EVENT_FocusOut( HWND hWnd, XFocusChangeEvent *event )
 {
-    if (event->detail != NotifyPointer)
-        if (hWnd == GetForegroundWindow())
-	{
-	    SendMessageA( hWnd, WM_CANCELMODE, 0, 0 );
+    /* Save the last window which had the focus */
+    glastXFocusWin = event->window;
+    if (!hWnd) return;
+    if (GetWindowLongA( hWnd, GWL_STYLE ) & WS_DISABLED) glastXFocusWin = 0;
 
-
-            /* don't reset the foreground window, if the window who's 
+    if (event->detail != NotifyPointer && hWnd == GetForegroundWindow())
+    {
+        /* don't reset the foreground window, if the window which is
                getting the focus is a Wine window */
-            if (!X11DRV_CheckFocus())
-            {
-                /* Abey : 6-Oct-99. Check again if the focus out window is the
-                   Foreground window, because in most cases the messages sent
-                   above must have already changed the foreground window, in which
-                   case we don't have to change the foreground window to 0 */
+        if (!X11DRV_CheckFocus())
+        {
+            SendMessageA( hWnd, WM_CANCELMODE, 0, 0 );
+            /* Abey : 6-Oct-99. Check again if the focus out window is the
+               Foreground window, because in most cases the messages sent
+               above must have already changed the foreground window, in which
+               case we don't have to change the foreground window to 0 */
 
-                if (hWnd == GetForegroundWindow())
-                    SetForegroundWindow( 0 );
-            }
-	}
+            if (hWnd == GetForegroundWindow())
+                SetForegroundWindow( 0 );
+        }
+    }
 }
 
 /**********************************************************************
@@ -849,100 +675,6 @@
     return TRUE;
 }
 
-/**********************************************************************
- *              EVENT_GetGeometry
- *
- * Helper function for ConfigureNotify handling.
- * Get the new geometry of a window relative to the root window.
- */
-static void EVENT_GetGeometry( Display *display, Window win, int *px, int *py,
-                               unsigned int *pwidth, unsigned int *pheight )
-{
-    Window root, top;
-    int x, y, width, height, border, depth;
-
-    wine_tsx11_lock();
-
-    /* Get the geometry of the window */
-    XGetGeometry( display, win, &root, &x, &y, &width, &height,
-                  &border, &depth );
-
-    /* Translate the window origin to root coordinates */
-    XTranslateCoordinates( display, win, root, 0, 0, &x, &y, &top );
-
-    wine_tsx11_unlock();
-
-    *px = x;
-    *py = y;
-    *pwidth = width;
-    *pheight = height;
-}
-
-/**********************************************************************
- *              EVENT_ConfigureNotify
- *
- * The ConfigureNotify event is only selected on top-level windows
- * when the -managed flag is used.
- */
-static void EVENT_ConfigureNotify( HWND hWnd, XConfigureEvent *event )
-{
-    RECT rectWindow;
-    int x, y, flags = 0;
-    unsigned int width, height;
-    HWND newInsertAfter, oldInsertAfter;
-  
-    /* Get geometry and Z-order according to X */
-
-    EVENT_GetGeometry( event->display, event->window, &x, &y, &width, &height );
-    newInsertAfter = EVENT_QueryZOrder( event->display, hWnd );
-
-    /* Get geometry and Z-order according to Wine */
-
-    /*
-     *  Needs to find the first Visible Window above the current one
-     */
-    oldInsertAfter = hWnd;
-    for (;;)
-    {
-        oldInsertAfter = GetWindow( oldInsertAfter, GW_HWNDPREV );
-        if (!oldInsertAfter)
-        {
-            oldInsertAfter = HWND_TOP;
-            break;
-        }
-        if (GetWindowLongA( oldInsertAfter, GWL_STYLE ) & WS_VISIBLE) break;
-    }
-
-    /* Compare what has changed */
-
-    GetWindowRect( hWnd, &rectWindow );
-    if ( rectWindow.left == x && rectWindow.top == y )
-        flags |= SWP_NOMOVE;
-    else
-        TRACE_(win)( "%04x moving from (%d,%d) to (%d,%d)\n", hWnd, 
-	             rectWindow.left, rectWindow.top, x, y );
-
-    if (    rectWindow.right - rectWindow.left == width
-         && rectWindow.bottom - rectWindow.top == height )
-        flags |= SWP_NOSIZE;
-    else
-        TRACE_(win)( "%04x resizing from (%d,%d) to (%d,%d)\n", hWnd, 
-                     rectWindow.right - rectWindow.left, 
-                     rectWindow.bottom - rectWindow.top, width, height );
-
-    if ( newInsertAfter == oldInsertAfter )
-        flags |= SWP_NOZORDER;
-    else
-        TRACE_(win)( "%04x restacking from after %04x to after %04x\n", hWnd, 
-                     oldInsertAfter, newInsertAfter );
-
-    /* If anything changed, call SetWindowPos */
-
-    if ( flags != (SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER) )
-        SetWindowPos( hWnd, newInsertAfter, x, y, width, height, 
-                            flags | SWP_NOACTIVATE | SWP_WINE_NOHOSTMOVE );
-}
-
 
 /***********************************************************************
  *           EVENT_SelectionRequest_TARGETS
@@ -1524,7 +1256,7 @@
   
   pWnd = WIN_FindWndPtr(hWnd);
   
-  TSXQueryPointer( event->display, X11DRV_WND_GetXWindow(pWnd), &w_aux_root, &w_aux_child, 
+  TSXQueryPointer( event->display, get_whole_window(pWnd), &w_aux_root, &w_aux_child,
                    &x, &y, (int *) &u.pt_aux.x, (int *) &u.pt_aux.y,
                    (unsigned int*)&aux_long);
   
@@ -1743,22 +1475,24 @@
     if ((event->message_type == wmProtocols) && 
 	(((Atom) event->data.l[0]) == wmDeleteWindow))
     {
-      /* Ignore the delete window request if the window has been disabled
-       * and we are in managed mode. This is to disallow applications from
-       * being closed by the window manager while in a modal state.
-       */
-      BOOL bIsDisabled;
-      bIsDisabled = GetWindowLongA( hWnd, GWL_STYLE ) & WS_DISABLED;
-
-      if ( !Options.managed || !bIsDisabled )
-      PostMessage16( hWnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
+        /* Ignore the delete window request if the window has been disabled */
+        if (!(GetWindowLongA( hWnd, GWL_STYLE ) & WS_DISABLED))
+            PostMessageA( hWnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
     }
-    else if ( event->message_type == dndProtocol &&
-	      (event->data.l[0] == DndFile || event->data.l[0] == DndFiles) )
-      EVENT_DropFromOffiX(hWnd, event);
-    else if ( event->message_type == dndProtocol &&
-	      event->data.l[0] == DndURL )
-      EVENT_DropURLs(hWnd, event);
+    else if (event->message_type == dndProtocol)
+    {
+        /* query window (drag&drop event contains only drag window) */
+        Window root, child;
+        int root_x, root_y, child_x, child_y;
+        unsigned int u;
+        TSXQueryPointer( event->display, root_window, &root, &child,
+                         &root_x, &root_y, &child_x, &child_y, &u);
+        if (TSXFindContext( event->display, child, winContext, (char **)&hWnd ) != 0) return;
+        if (event->data.l[0] == DndFile || event->data.l[0] == DndFiles)
+            EVENT_DropFromOffiX(hWnd, event);
+        else if (event->data.l[0] == DndURL)
+            EVENT_DropURLs(hWnd, event);
+    }
     else {
 #if 0
       /* enable this if you want to see the message */
@@ -1782,65 +1516,6 @@
   }
 }
 
-/**********************************************************************
- *           EVENT_EnterNotify
- *
- * Install colormap when Wine window is focused in
- * self-managed mode with private colormap
- */
-#if 0
-void EVENT_EnterNotify( HWND hWnd, XCrossingEvent *event )
-{
-  if( !Options.managed && root_window == DefaultRootWindow(event->display) &&
-      (COLOR_GetSystemPaletteFlags() & COLOR_PRIVATE) && GetFocus() )
-    TSXInstallColormap( event->display, X11DRV_PALETTE_GetColormap() );
-}
-#endif
-
-/**********************************************************************
- *		EVENT_MapNotify
- */
-void EVENT_MapNotify( HWND hWnd, XMapEvent *event )
-{
-  HWND hwndFocus = GetFocus();
-  WND *wndFocus = WIN_FindWndPtr(hwndFocus);
-  WND *pWnd = WIN_FindWndPtr(hWnd);
-  if (pWnd && (pWnd->dwExStyle & WS_EX_MANAGED))
-  {
-      DCE_InvalidateDCE( pWnd, &pWnd->rectWindow );
-      pWnd->dwStyle &= ~WS_MINIMIZE;
-      pWnd->dwStyle |=  WS_VISIBLE;
-      WIN_InternalShowOwnedPopups(hWnd,TRUE,TRUE);
-  }
-  WIN_ReleaseWndPtr(pWnd);
-
-  if (hwndFocus && IsChild( hWnd, hwndFocus ))
-      X11DRV_SetFocus(hwndFocus);
-
-  WIN_ReleaseWndPtr(wndFocus);
-  
-  return;
-}
-
-
-/**********************************************************************
- *              EVENT_UnmapNotify
- */
-void EVENT_UnmapNotify( HWND hWnd, XUnmapEvent *event )
-{
-  WND *pWnd = WIN_FindWndPtr(hWnd);
-  if (pWnd && (pWnd->dwExStyle & WS_EX_MANAGED))
-  {
-      EndMenu();
-      if( pWnd->dwStyle & WS_VISIBLE )
-      {
-	  pWnd->dwStyle |=  WS_MINIMIZE;
-	  pWnd->dwStyle &= ~WS_VISIBLE;
-            WIN_InternalShowOwnedPopups(hWnd,FALSE,TRUE);
-      }
-  }
-  WIN_ReleaseWndPtr(pWnd);
-}
 
 /***********************************************************************
  *           EVENT_MappingNotify
diff --git a/windows/x11drv/keyboard.c b/windows/x11drv/keyboard.c
index 3fc1e1f..78ff6aa 100644
--- a/windows/x11drv/keyboard.c
+++ b/windows/x11drv/keyboard.c
@@ -708,7 +708,7 @@
  *
  * Handle a X key event
  */
-void X11DRV_KEYBOARD_HandleEvent( WND *pWnd, XKeyEvent *event )
+void X11DRV_KEYBOARD_HandleEvent( XKeyEvent *event, int x, int y )
 {
     char Str[24]; 
     KeySym keysym;
@@ -717,8 +717,6 @@
     static BOOL force_extended = FALSE; /* hack for AltGr translation */
     int ascii_chars;
 
-    INT event_x = (pWnd? pWnd->rectWindow.left : 0) + event->x;
-    INT event_y = (pWnd? pWnd->rectWindow.top  : 0) + event->y;
     DWORD event_time = event->time - X11DRV_server_startticks;
 
     /* this allows support for dead keys */
@@ -745,11 +743,11 @@
 	{
 	TRACE_(key)("Alt Gr key event received\n");
 	event->keycode = kcControl; /* Simulate Control */
-	X11DRV_KEYBOARD_HandleEvent( pWnd, event );
+	X11DRV_KEYBOARD_HandleEvent( event, x, y );
 
 	event->keycode = kcAlt; /* Simulate Alt */
 	force_extended = TRUE;
-	X11DRV_KEYBOARD_HandleEvent( pWnd, event );
+	X11DRV_KEYBOARD_HandleEvent( event, x, y );
 	force_extended = FALSE;
     
     /* Here we save the pressed/released state of the AltGr key, to be able to 
@@ -783,13 +781,11 @@
     switch (vkey & 0xff)
     {
     case VK_NUMLOCK:    
-      KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, event->type, event_x, event_y,
-                            event_time );
+      KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, event->type, x, y, event_time );
       break;
     case VK_CAPITAL:
       TRACE("Caps Lock event. (type %d). State before : %#.2x\n",event->type,pKeyStateTable[vkey]);
-      KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, event->type, event_x, event_y,
-                            event_time ); 
+      KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, event->type, x, y, event_time );
       TRACE("State after : %#.2x\n",pKeyStateTable[vkey]);
       break;
     default:
@@ -797,19 +793,15 @@
 	if (!(pKeyStateTable[VK_NUMLOCK] & 0x01) != !(event->state & NumLockMask))
 	  { 
 	    TRACE("Adjusting NumLock state. \n");
-	    KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, KeyPress, event_x, event_y,
-                                  event_time );
-	    KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, KeyRelease, event_x, event_y,
-                                  event_time );
+	    KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, KeyPress, x, y, event_time );
+	    KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, KeyRelease, x, y, event_time );
 	  }
         /* Adjust the CAPSLOCK state if it has been changed outside wine */
 	if (!(pKeyStateTable[VK_CAPITAL] & 0x01) != !(event->state & LockMask))
 	  {
               TRACE("Adjusting Caps Lock state.\n");
-	    KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, KeyPress, event_x, event_y,
-                                  event_time );
-	    KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, KeyRelease, event_x, event_y,
-                                  event_time );
+	    KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, KeyPress, x, y, event_time );
+	    KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, KeyRelease, x, y, event_time );
 	  }
 	/* Not Num nor Caps : end of intermediary states for both. */
 	NumState = FALSE;
@@ -823,8 +815,7 @@
 	if ( vkey & 0x100 )              dwFlags |= KEYEVENTF_EXTENDEDKEY;
 	if ( force_extended )            dwFlags |= KEYEVENTF_WINE_FORCEEXTENDED;
 
-	KEYBOARD_SendEvent( vkey & 0xff, bScan, dwFlags, 
-                            event_x, event_y, event_time );
+        KEYBOARD_SendEvent( vkey & 0xff, bScan, dwFlags, x, y, event_time );
     }
    }
 }
diff --git a/windows/x11drv/mouse.c b/windows/x11drv/mouse.c
index 255972a..03f68a1 100644
--- a/windows/x11drv/mouse.c
+++ b/windows/x11drv/mouse.c
@@ -154,13 +154,8 @@
 /* set the cursor of a window; helper for X11DRV_SetCursor */
 static BOOL CALLBACK set_win_cursor( HWND hwnd, LPARAM cursor )
 {
-    WND *wndPtr = WIN_FindWndPtr(hwnd);
-    if (wndPtr)
-    {
-        Window win = X11DRV_WND_GetXWindow(wndPtr);
-        if (win) TSXDefineCursor( thread_display(), win, (Cursor)cursor );
-    }
-    WIN_ReleaseWndPtr( wndPtr );
+    Window win = X11DRV_get_whole_window( hwnd );
+    if (win) TSXDefineCursor( thread_display(), win, (Cursor)cursor );
     return TRUE;
 }
 
diff --git a/windows/x11drv/wnd.c b/windows/x11drv/wnd.c
index 901a13b..601219e 100644
--- a/windows/x11drv/wnd.c
+++ b/windows/x11drv/wnd.c
@@ -40,37 +40,11 @@
 
 WND_DRIVER X11DRV_WND_Driver =
 {
-  X11DRV_WND_ForceWindowRaise,
-  X11DRV_WND_SetHostAttr
+  X11DRV_WND_ForceWindowRaise
 };
 
 
 /***********************************************************************
- *		X11DRV_WND_GetXWindow
- *
- * Return the X window associated to a window.
- */
-Window X11DRV_WND_GetXWindow(WND *wndPtr)
-{
-    return wndPtr && wndPtr->pDriverData ? 
-      ((X11DRV_WND_DATA *) wndPtr->pDriverData)->window : 0;
-}
-
-/***********************************************************************
- *		X11DRV_WND_FindXWindow
- *
- * Return the the first X window associated to a window chain.
- */
-Window X11DRV_WND_FindXWindow(WND *wndPtr)
-{
-    while (wndPtr && 
-	   !((X11DRV_WND_DATA *) wndPtr->pDriverData)->window) 
-      wndPtr = wndPtr->parent;
-    return wndPtr ?
-      ((X11DRV_WND_DATA *) wndPtr->pDriverData)->window : 0;
-}
-
-/***********************************************************************
  *		X11DRV_WND_IsZeroSizeWnd
  *
  * Return TRUE if the window has a height or widht less or equal to 0
@@ -101,7 +75,7 @@
     return;
   }
   
-  if( !wndPtr || !X11DRV_WND_GetXWindow(wndPtr) || (wndPtr->dwExStyle & WS_EX_MANAGED) )
+  if( !wndPtr || !get_whole_window(wndPtr) || (wndPtr->dwExStyle & WS_EX_MANAGED) )
   {
       WIN_ReleaseDesktop();
     return;
@@ -114,9 +88,9 @@
   winChanges.stack_mode = Above;
   while (wndPtr)
     {
-      if ( !X11DRV_WND_IsZeroSizeWnd(wndPtr) && X11DRV_WND_GetXWindow(wndPtr) )         
-         TSXReconfigureWMWindow( thread_display(), X11DRV_WND_GetXWindow(wndPtr), 0,
-				CWStackMode, &winChanges );
+      if ( !X11DRV_WND_IsZeroSizeWnd(wndPtr) && get_whole_window(wndPtr) )
+         TSXReconfigureWMWindow( thread_display(), get_whole_window(wndPtr), 0,
+                                 CWStackMode, &winChanges );
 
       wndPrev = pDesktop->child;
       if (wndPrev == wndPtr) break;
@@ -126,345 +100,3 @@
     }
   WIN_ReleaseDesktop();
 }
-
-/***********************************************************************
- *		X11DRV_WND_FindDesktopXWindow	[Internal]
- *
- * Find the actual X window which needs be restacked.
- * Used by X11DRV_WND_SetWindowPos().
- */
-static Window X11DRV_WND_FindDesktopXWindow( WND *wndPtr )
-{
-  if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
-    return X11DRV_WND_GetXWindow(wndPtr);
-  else
-    {
-      Window window, root, parent, *children;
-      int nchildren;
-      window = X11DRV_WND_GetXWindow(wndPtr);
-      for (;;)
-        {
-	  TSXQueryTree( thread_display(), window, &root, &parent,
-                        &children, &nchildren );
-	  TSXFree( children );
-	  if (parent == root)
-	    return window;
-	  window = parent;
-        }
-    }
-}
-
-/***********************************************************************
- *           WINPOS_SetXWindowPos
- *
- * SetWindowPos() for an X window. Used by the real SetWindowPos().
- */
-void X11DRV_WND_SetWindowPos(WND *wndPtr, const WINDOWPOS *winpos, BOOL bChangePos)
-{
-    XWindowChanges winChanges;
-    Display *display = thread_display();
-    int changeMask = 0;
-    BOOL isZeroSizeWnd = FALSE;
-    BOOL forceMapWindow = FALSE;
-    WND *winposPtr = WIN_FindWndPtr( winpos->hwnd );
-    if ( !winposPtr ) return;
-
-    /* find out if after this function we will end out with a zero-size window */
-    if (X11DRV_WND_IsZeroSizeWnd(winposPtr))
-    {
-        /* if current size is 0, and no resizing */
-        if (winpos->flags & SWP_NOSIZE)
-            isZeroSizeWnd = TRUE;
-        else if ((winpos->cx > 0) && (winpos->cy > 0)) 
-        {
-            /* if this function is setting a new size > 0 for the window, we
-               should map the window if WS_VISIBLE is set */
-            if ((winposPtr->dwStyle & WS_VISIBLE) && !(winpos->flags & SWP_HIDEWINDOW))
-                forceMapWindow = TRUE;
-        }
-    }
-    /* if resizing to 0 */
-    if ( !(winpos->flags & SWP_NOSIZE) && ((winpos->cx <= 0) || (winpos->cy <= 0)) )
-            isZeroSizeWnd = TRUE;
-
-    if(!wndPtr->hwndSelf) wndPtr = NULL; /* FIXME: WND destroyed, shouldn't happen!!! */
-  
-    if (!(winpos->flags & SWP_SHOWWINDOW) && (winpos->flags & SWP_HIDEWINDOW))
-    {
-      if(X11DRV_WND_GetXWindow(wndPtr)) 
-	TSXUnmapWindow( display, X11DRV_WND_GetXWindow(wndPtr) );
-    }
-
-    if(bChangePos)
-    {
-	if ( !(winpos->flags & SWP_NOSIZE))
-	{
-	  winChanges.width     = (winpos->cx > 0 ) ? winpos->cx : 1;
-	  winChanges.height    = (winpos->cy > 0 ) ? winpos->cy : 1;
-	  changeMask |= CWWidth | CWHeight;
-	  
-	  /* Tweak dialog window size hints */
-	  
-	  if ((winposPtr->dwExStyle & WS_EX_MANAGED) &&
-               HAS_DLGFRAME(winposPtr->dwStyle,winposPtr->dwExStyle))
-	    {
-	      XSizeHints *size_hints = TSXAllocSizeHints();
-	      
-	      if (size_hints)
-		{
-		  long supplied_return;
-		  
-		  TSXGetWMSizeHints( display, X11DRV_WND_GetXWindow(winposPtr), size_hints,
-				     &supplied_return, XA_WM_NORMAL_HINTS);
-		  size_hints->min_width = size_hints->max_width = winpos->cx;
-		  size_hints->min_height = size_hints->max_height = winpos->cy;
-		  TSXSetWMSizeHints( display, X11DRV_WND_GetXWindow(winposPtr), size_hints,
-				     XA_WM_NORMAL_HINTS );
-		  TSXFree(size_hints);
-		}
-	    }
-	}
-	if (!(winpos->flags & SWP_NOMOVE))
-	{
-	  winChanges.x = winpos->x;
-	  winChanges.y = winpos->y;
-	  changeMask |= CWX | CWY;
-	}
-	if (!(winpos->flags & SWP_NOZORDER) && !isZeroSizeWnd)
-	{
-	  winChanges.stack_mode = Below;
-	  changeMask |= CWStackMode;
-	  
-	  if (winpos->hwndInsertAfter == HWND_TOP) winChanges.stack_mode = Above;
-	  else if (winpos->hwndInsertAfter != HWND_BOTTOM)
-	    {
-	      WND*   insertPtr = WIN_FindWndPtr( winpos->hwndInsertAfter );
-	      Window stack[2];
-
-          /* If the window where we should do the insert is zero-sized (not mapped)
-             don't used this window since it will possibly crash the X server,
-             use the "non zero-sized" window above */ 
-	      if (X11DRV_WND_IsZeroSizeWnd(insertPtr))
-          {
-              /* find the window on top of the zero sized window */ 
-              WND *pDesktop = WIN_GetDesktop();
-              WND *wndPrev = pDesktop->child;
-              WND *wndZeroSized = insertPtr;
-
-              while (1)
-              {
-                  if (wndPrev == wndZeroSized)
-                      break;  /* zero-sized window is on top */
-
-                  while (wndPrev && (wndPrev->next != wndZeroSized))
-                      wndPrev = wndPrev->next;
-
-                  /* check if the window found is not zero-sized */
-                  if (X11DRV_WND_IsZeroSizeWnd(wndPrev))
-                  {
-                      wndZeroSized = wndPrev; /* restart the search */
-                      wndPrev = pDesktop->child;
-                  }
-                  else
-                      break;   /* "above" window is found */
-              }
-              WIN_ReleaseDesktop();
-              
-              if (wndPrev == wndZeroSized)
-              {
-                  /* the zero-sized window is on top */
-                  /* so set the window on top */
-                  winChanges.stack_mode = Above;
-              }
-              else
-              {
-                  stack[0] = X11DRV_WND_FindDesktopXWindow( wndPrev );
-                  stack[1] = X11DRV_WND_FindDesktopXWindow( winposPtr );
-
-                  TSXRestackWindows(display, stack, 2);
-                  changeMask &= ~CWStackMode;
-              }
-          }
-          else  /* Normal behavior, windows are not zero-sized */
-          {
-              stack[0] = X11DRV_WND_FindDesktopXWindow( insertPtr );
-              stack[1] = X11DRV_WND_FindDesktopXWindow( winposPtr );
-	      
-	          TSXRestackWindows(display, stack, 2);
-	          changeMask &= ~CWStackMode;
-          }
-
-          WIN_ReleaseWndPtr(insertPtr);
-	    }
-	}
-	if (changeMask && X11DRV_WND_GetXWindow(winposPtr))
-	{
-	    TSXReconfigureWMWindow( display, X11DRV_WND_GetXWindow(winposPtr), 0, changeMask, &winChanges );
-	    if( winposPtr->clsStyle & (CS_VREDRAW | CS_HREDRAW) )
-		X11DRV_WND_SetGravity( winposPtr, ForgetGravity );
-	}
-    }
-    
-    /* don't map the window if it's a zero size window */
-    if ( ((winpos->flags & SWP_SHOWWINDOW) && !isZeroSizeWnd) || forceMapWindow )
-    {
-	if(X11DRV_WND_GetXWindow(wndPtr)) 
-	   TSXMapWindow( display, X11DRV_WND_GetXWindow(wndPtr) );
-    }
-    WIN_ReleaseWndPtr(winposPtr);
-}
-
-/*****************************************************************
- *		 X11DRV_WND_SurfaceCopy
- *
- * Copies rect to (rect.left + dx, rect.top + dy). 
- */
-void X11DRV_WND_SurfaceCopy(WND* wndPtr, HDC hdc, INT dx, INT dy, 
-			    const RECT *rect, BOOL bUpdate)
-{
-    X11DRV_PDEVICE *physDev;
-    POINT dst, src;
-    DC *dcPtr = DC_GetDCPtr( hdc );
-
-    if (!dcPtr) return;
-    physDev = (X11DRV_PDEVICE *)dcPtr->physDev;
-    dst.x = (src.x = dcPtr->DCOrgX + rect->left) + dx;
-    dst.y = (src.y = dcPtr->DCOrgY + rect->top) + dy;
-
-    wine_tsx11_lock();
-    if (bUpdate) /* handles non-Wine windows hanging over the copied area */
-        XSetGraphicsExposures( gdi_display, physDev->gc, True );
-    XSetFunction( gdi_display, physDev->gc, GXcopy );
-    XCopyArea( gdi_display, physDev->drawable, physDev->drawable, physDev->gc,
-               src.x, src.y, rect->right - rect->left, rect->bottom - rect->top,
-               dst.x, dst.y );
-    if (bUpdate)
-        XSetGraphicsExposures( gdi_display, physDev->gc, False );
-    wine_tsx11_unlock();
-    GDI_ReleaseObj( hdc );
-
-    if (bUpdate) /* Make sure exposure events have been processed */
-        X11DRV_Synchronize();
-}
-
-/***********************************************************************
- *              X11DRV_SetWMHint
- */
-static BOOL X11DRV_SetWMHint(Display* display, WND* wndPtr, int hint, int val)
-{
-    XWMHints* wm_hints = TSXGetWMHints( display, X11DRV_WND_GetXWindow(wndPtr) );
-    if (!wm_hints) wm_hints = TSXAllocWMHints();
-    if (wm_hints)
-    {
-        wm_hints->flags = hint;
-	switch( hint )
-	{
-	    case InputHint:
-		 wm_hints->input = val;
-		 break;
-
-	    case StateHint:
-		 wm_hints->initial_state = val;
-		 break;
-
-	    case IconPixmapHint:
-		 wm_hints->icon_pixmap = (Pixmap)val;
-		 break;
-
-	    case IconWindowHint:
-		 wm_hints->icon_window = (Window)val;
-		 break;
-	}
-
-        TSXSetWMHints( display, X11DRV_WND_GetXWindow(wndPtr), wm_hints );
-        TSXFree(wm_hints);
-        return TRUE;
-    }
-    return FALSE;
-}
-
-
-void X11DRV_WND_SetGravity( WND* wnd, int value )
-{
-    X11DRV_WND_DATA *data = wnd->pDriverData;
-
-    if (data && data->window && data->bit_gravity != value )
-    {
-        XSetWindowAttributes win_attr;
-        win_attr.bit_gravity = value;
-        data->bit_gravity = value;
-        TSXChangeWindowAttributes( thread_display(), data->window, CWBitGravity, &win_attr );
-    }
-}
-
-
-/***********************************************************************
- *              X11DRV_WND_SetHostAttr
- *
- * This function returns TRUE if the attribute is supported and the
- * action was successful. Otherwise it should return FALSE and Wine will try 
- * to get by without the functionality provided by the host window system.
- */
-BOOL X11DRV_WND_SetHostAttr(WND* wnd, INT ha, INT value)
-{
-    Window w;
-
-    if( (w = X11DRV_WND_GetXWindow(wnd)) )
-    {
-        Display *display = thread_display();
-	switch( ha )
-	{
-	case HAK_ICONICSTATE: /* called when a window is minimized/restored */
-
-            /* don't do anything if it'a zero size window */ 
-            if (X11DRV_WND_IsZeroSizeWnd(wnd))
-                return TRUE;
-
-		    if( (wnd->dwExStyle & WS_EX_MANAGED) )
-		    {
-			if( value )
-			{
-			    if( wnd->dwStyle & WS_VISIBLE )
-			    {
-				XClientMessageEvent ev;
-
-				/* FIXME: set proper icon */
-
-				ev.type = ClientMessage;
-				ev.display = display;
-				ev.message_type = wmChangeState;
-				ev.format = 32;
-				ev.data.l[0] = IconicState;
-				ev.window = w;
-
-				if( TSXSendEvent (display, DefaultRootWindow(display),
-		True, (SubstructureRedirectMask | SubstructureNotifyMask), (XEvent*)&ev))
-				{
-				    XEvent xe;
-				    TSXFlush (display);
-				    while( !TSXCheckTypedWindowEvent( display, w, UnmapNotify, &xe) );
-				}
-				else 
-				    break;
-			    }
-			    else
-				X11DRV_SetWMHint( display, wnd, StateHint, IconicState );
-			}
-			else
-			{
-			    if( !(wnd->flags & WS_VISIBLE) )
-				X11DRV_SetWMHint( display, wnd, StateHint, NormalState );
-			    else
-			    {
-				XEvent xe;
-				TSXMapWindow(display, w );
-				while( !TSXCheckTypedWindowEvent( display, w, MapNotify, &xe) );
-			    }
-			}
-			return TRUE;
-		    }
-		    break;
-	}
-    }
-    return FALSE;
-}