Changed the GDI driver interface to pass an opaque PHYSDEV pointer
instead of a DC structure.
Removed some direct accesses to the DC structure from the drivers.
Got rid the bitmap driver.

diff --git a/graphics/x11drv/bitblt.c b/graphics/x11drv/bitblt.c
index 7b7c206..e2ebb74 100644
--- a/graphics/x11drv/bitblt.c
+++ b/graphics/x11drv/bitblt.c
@@ -856,7 +856,7 @@
  * Retrieve an area from the source DC, stretching and mapping all the
  * pixels to Windows colors.
  */
-static int BITBLT_GetSrcAreaStretch( DC *dcSrc, DC *dcDst,
+static int BITBLT_GetSrcAreaStretch( X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
                                       Pixmap pixmap, GC gc,
                                       INT xSrc, INT ySrc,
                                       INT widthSrc, INT heightSrc,
@@ -865,8 +865,7 @@
                                       RECT *visRectSrc, RECT *visRectDst )
 {
     XImage *imageSrc, *imageDst;
-    X11DRV_PDEVICE *physDevSrc = (X11DRV_PDEVICE *)dcSrc->physDev;
-    X11DRV_PDEVICE *physDevDst = (X11DRV_PDEVICE *)dcDst->physDev;
+    DC *dcDst = physDevDst->dc;
 
     RECT rectSrc = *visRectSrc;
     RECT rectDst = *visRectDst;
@@ -912,16 +911,16 @@
  * Retrieve an area from the source DC, mapping all the
  * pixels to Windows colors.
  */
-static int BITBLT_GetSrcArea( DC *dcSrc, DC *dcDst, Pixmap pixmap, GC gc,
-                              INT xSrc, INT ySrc, RECT *visRectSrc )
+static int BITBLT_GetSrcArea( X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
+                              Pixmap pixmap, GC gc, INT xSrc, INT ySrc, RECT *visRectSrc )
 {
     XImage *imageSrc, *imageDst;
     register INT x, y;
     int exposures = 0;
     INT width  = visRectSrc->right - visRectSrc->left;
     INT height = visRectSrc->bottom - visRectSrc->top;
-    X11DRV_PDEVICE *physDevSrc = (X11DRV_PDEVICE *)dcSrc->physDev;
-    X11DRV_PDEVICE *physDevDst = (X11DRV_PDEVICE *)dcDst->physDev;
+    DC *dcSrc = physDevSrc->dc;
+    DC *dcDst = physDevDst->dc;
 
     if (dcSrc->bitsPerPixel == dcDst->bitsPerPixel)
     {
@@ -1018,14 +1017,13 @@
  * Retrieve an area from the destination DC, mapping all the
  * pixels to Windows colors.
  */
-static int BITBLT_GetDstArea(DC *dc, Pixmap pixmap, GC gc, RECT *visRectDst)
+static int BITBLT_GetDstArea(X11DRV_PDEVICE *physDev, Pixmap pixmap, GC gc, RECT *visRectDst)
 {
     int exposures = 0;
     INT width  = visRectDst->right - visRectDst->left;
     INT height = visRectDst->bottom - visRectDst->top;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
 
-    if (!X11DRV_PALETTE_XPixelToPalette || (dc->bitsPerPixel == 1) ||
+    if (!X11DRV_PALETTE_XPixelToPalette || (physDev->dc->bitsPerPixel == 1) ||
 	(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) )
     {
         XCopyArea( gdi_display, physDev->drawable, pixmap, gc,
@@ -1037,7 +1035,7 @@
         register INT x, y;
         XImage *image;
 
-        if (dc->flags & DC_MEMORY)
+        if (physDev->dc->flags & DC_MEMORY)
             image = XGetImage( gdi_display, physDev->drawable,
                                visRectDst->left, visRectDst->top,
                                width, height, AllPlanes, ZPixmap );
@@ -1067,19 +1065,18 @@
  * Put an area back into the destination DC, mapping the pixel
  * colors to X pixels.
  */
-static int BITBLT_PutDstArea(DC *dc, Pixmap pixmap, GC gc, RECT *visRectDst)
+static int BITBLT_PutDstArea(X11DRV_PDEVICE *physDev, Pixmap pixmap, RECT *visRectDst)
 {
     int exposures = 0;
     INT width  = visRectDst->right - visRectDst->left;
     INT height = visRectDst->bottom - visRectDst->top;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
 
     /* !X11DRV_PALETTE_PaletteToXPixel is _NOT_ enough */
 
-    if (!X11DRV_PALETTE_PaletteToXPixel || (dc->bitsPerPixel == 1) || 
+    if (!X11DRV_PALETTE_PaletteToXPixel || (physDev->dc->bitsPerPixel == 1) ||
         (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) )
     {
-        XCopyArea( gdi_display, pixmap, physDev->drawable, gc, 0, 0,
+        XCopyArea( gdi_display, pixmap, physDev->drawable, physDev->gc, 0, 0,
                    width, height, visRectDst->left, visRectDst->top );
         exposures++;
     }
@@ -1094,7 +1091,7 @@
                 XPutPixel( image, x, y,
                            X11DRV_PALETTE_PaletteToXPixel[XGetPixel( image, x, y )]);
             }
-        XPutImage( gdi_display, physDev->drawable, gc, image, 0, 0,
+        XPutImage( gdi_display, physDev->drawable, physDev->gc, image, 0, 0,
                    visRectDst->left, visRectDst->top, width, height );
         XDestroyImage( image );
     }
@@ -1109,10 +1106,10 @@
  * Return FALSE if one of the rectangles is empty.
  */
 static BOOL BITBLT_GetVisRectangles( DC *dcDst, INT xDst, INT yDst,
-                                       INT widthDst, INT heightDst,
-                                       DC *dcSrc, INT xSrc, INT ySrc,
-                                       INT widthSrc, INT heightSrc,
-                                       RECT *visRectSrc, RECT *visRectDst )
+                                     INT widthDst, INT heightDst,
+                                     DC *dcSrc, INT xSrc, INT ySrc,
+                                     INT widthSrc, INT heightSrc,
+                                     RECT *visRectSrc, RECT *visRectDst )
 {
     RECT rect, clipRect;
 
@@ -1198,11 +1195,11 @@
  *
  * Implementation of PatBlt(), BitBlt() and StretchBlt().
  */
-static BOOL BITBLT_InternalStretchBlt( DC *dcDst, INT xDst, INT yDst,
-                                         INT widthDst, INT heightDst,
-                                         DC *dcSrc, INT xSrc, INT ySrc,
-                                         INT widthSrc, INT heightSrc,
-                                         DWORD rop )
+static BOOL BITBLT_InternalStretchBlt( X11DRV_PDEVICE *physDevDst, INT xDst, INT yDst,
+                                       INT widthDst, INT heightDst,
+                                       X11DRV_PDEVICE *physDevSrc, INT xSrc, INT ySrc,
+                                       INT widthSrc, INT heightSrc,
+                                       DWORD rop )
 {
     BOOL usePat, useSrc, useDst, destUsed, fStretch, fNullBrush;
     RECT visRectDst, visRectSrc;
@@ -1210,8 +1207,8 @@
     const BYTE *opcode;
     Pixmap pixmaps[3] = { 0, 0, 0 };  /* pixmaps for DST, SRC, TMP */
     GC tmpGC = 0;
-    X11DRV_PDEVICE *physDevSrc = NULL;
-    X11DRV_PDEVICE *physDevDst = (X11DRV_PDEVICE *)dcDst->physDev;
+    DC *dcSrc = physDevSrc ? physDevSrc->dc : NULL;
+    DC *dcDst = physDevDst->dc;
 
     /* compensate for off-by-one shifting for negative widths and heights */
     if (widthDst < 0)
@@ -1223,7 +1220,6 @@
     if (heightSrc < 0)
         ++ySrc;
 
-    if(dcSrc) physDevSrc = (X11DRV_PDEVICE *)dcSrc->physDev;
     usePat = (((rop >> 4) & 0x0f0000) != (rop & 0x0f0000));
     useSrc = (((rop >> 2) & 0x330000) != (rop & 0x330000));
     useDst = (((rop >> 1) & 0x550000) != (rop & 0x550000));
@@ -1332,7 +1328,7 @@
 
     case PATINVERT:  /* 0x5a */
 	if (perfect_graphics()) break;
-        if (X11DRV_SetupGCForBrush( dcDst ))
+        if (X11DRV_SetupGCForBrush( physDevDst ))
         {
             wine_tsx11_lock();
             XSetFunction( gdi_display, physDevDst->gc, GXxor );
@@ -1344,7 +1340,7 @@
 
     case 0xa50065:
 	if (perfect_graphics()) break;
-	if (X11DRV_SetupGCForBrush( dcDst ))
+	if (X11DRV_SetupGCForBrush( physDevDst ))
 	{
             wine_tsx11_lock();
 	    XSetFunction( gdi_display, physDevDst->gc, GXequiv );
@@ -1384,7 +1380,7 @@
         break;
 
     case PATCOPY:  /* 0xf0 */
-        if (!X11DRV_SetupGCForBrush( dcDst )) return TRUE;
+        if (!X11DRV_SetupGCForBrush( physDevDst )) return TRUE;
         wine_tsx11_lock();
         XSetFunction( gdi_display, physDevDst->gc, GXcopy );
         XFillRectangle( gdi_display, physDevDst->drawable, physDevDst->gc,
@@ -1421,17 +1417,17 @@
         pixmaps[SRC] = XCreatePixmap( gdi_display, root_window, width, height,
                                       dcDst->bitsPerPixel );
         if (fStretch)
-            BITBLT_GetSrcAreaStretch( dcSrc, dcDst, pixmaps[SRC], tmpGC,
+            BITBLT_GetSrcAreaStretch( physDevSrc, physDevDst, pixmaps[SRC], tmpGC,
                                       xSrc, ySrc, widthSrc, heightSrc,
                                       xDst, yDst, widthDst, heightDst,
                                       &visRectSrc, &visRectDst );
         else
-            BITBLT_GetSrcArea( dcSrc, dcDst, pixmaps[SRC], tmpGC,
+            BITBLT_GetSrcArea( physDevSrc, physDevDst, pixmaps[SRC], tmpGC,
                                xSrc, ySrc, &visRectSrc );
     }
 
-    if (useDst) BITBLT_GetDstArea( dcDst, pixmaps[DST], tmpGC, &visRectDst );
-    if (usePat) fNullBrush = !X11DRV_SetupGCForPatBlt( dcDst, tmpGC, TRUE );
+    if (useDst) BITBLT_GetDstArea( physDevDst, pixmaps[DST], tmpGC, &visRectDst );
+    if (usePat) fNullBrush = !X11DRV_SetupGCForPatBlt( physDevDst, tmpGC, TRUE );
     else fNullBrush = FALSE;
     destUsed = FALSE;
 
@@ -1472,8 +1468,8 @@
         }
     }
     XSetFunction( gdi_display, physDevDst->gc, GXcopy );
-    physDevDst->exposures += BITBLT_PutDstArea( dcDst, pixmaps[destUsed ? DST : SRC],
-                                                physDevDst->gc, &visRectDst );
+    physDevDst->exposures += BITBLT_PutDstArea( physDevDst, pixmaps[destUsed ? DST : SRC],
+                                                &visRectDst );
     XFreePixmap( gdi_display, pixmaps[DST] );
     if (pixmaps[SRC]) XFreePixmap( gdi_display, pixmaps[SRC] );
     if (pixmaps[TMP]) XFreePixmap( gdi_display, pixmaps[TMP] );
@@ -1486,14 +1482,13 @@
 /***********************************************************************
  *           X11DRV_PatBlt
  */
-BOOL X11DRV_PatBlt( DC *dc, INT left, INT top,
-                      INT width, INT height, DWORD rop )
+BOOL X11DRV_PatBlt( X11DRV_PDEVICE *physDev, INT left, INT top, INT width, INT height, DWORD rop )
 {
     BOOL result;
 
-    X11DRV_LockDIBSection( dc, DIB_Status_GdiMod, FALSE );
-    result = BITBLT_InternalStretchBlt( dc, left, top, width, height, NULL, 0, 0, 0, 0, rop );
-    X11DRV_UnlockDIBSection( dc, TRUE );
+    X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
+    result = BITBLT_InternalStretchBlt( physDev, left, top, width, height, NULL, 0, 0, 0, 0, rop );
+    X11DRV_UnlockDIBSection( physDev, TRUE );
     return result;
 }
 
@@ -1501,13 +1496,15 @@
 /***********************************************************************
  *           X11DRV_BitBlt
  */
-BOOL X11DRV_BitBlt( DC *dcDst, INT xDst, INT yDst,
-                      INT width, INT height, DC *dcSrc,
-                      INT xSrc, INT ySrc, DWORD rop )
+BOOL X11DRV_BitBlt( X11DRV_PDEVICE *physDevDst, INT xDst, INT yDst,
+                    INT width, INT height, X11DRV_PDEVICE *physDevSrc,
+                    INT xSrc, INT ySrc, DWORD rop )
 {
     BOOL result = FALSE;
     INT sSrc, sDst;
     RECT visRectDst, visRectSrc;
+    DC *dcSrc = physDevSrc->dc;
+    DC *dcDst = physDevDst->dc;
 
     if (((rop >> 16) & 0x55) == ((rop >> 17) & 0x55)) {
       /* FIXME: seems the ROP doesn't include destination;
@@ -1515,8 +1512,8 @@
        * we can pass TRUE instead of FALSE to CoerceDIBSection(dcDst...),
        * which may avoid a copy in some situations */
     }
-    sDst = X11DRV_LockDIBSection( dcDst, DIB_Status_None, FALSE );
-    sSrc = X11DRV_LockDIBSection( dcSrc, DIB_Status_None, FALSE );
+    sDst = X11DRV_LockDIBSection( physDevDst, DIB_Status_None, FALSE );
+    sSrc = X11DRV_LockDIBSection( physDevSrc, DIB_Status_None, FALSE );
 
     if ((sSrc == DIB_Status_AppMod) && (rop == SRCCOPY) &&
         (dcSrc->bitsPerPixel == dcDst->bitsPerPixel))
@@ -1545,22 +1542,22 @@
       if (sDst == DIB_Status_AppMod) {
         FIXME("potential optimization - client-side DIB copy\n");
       }
-      X11DRV_CoerceDIBSection( dcDst, DIB_Status_GdiMod, FALSE );
+      X11DRV_CoerceDIBSection( physDevDst, DIB_Status_GdiMod, FALSE );
 
-      X11DRV_DIB_CopyDIBSection( dcSrc, dcDst, xSrc, ySrc, xDst, yDst, width, height );
+      X11DRV_DIB_CopyDIBSection( physDevSrc, physDevDst, xSrc, ySrc, xDst, yDst, width, height );
       result = TRUE;
       goto END;
     }
 
-    X11DRV_CoerceDIBSection( dcDst, DIB_Status_GdiMod, FALSE );
-    X11DRV_CoerceDIBSection( dcSrc, DIB_Status_GdiMod, FALSE );
+    X11DRV_CoerceDIBSection( physDevDst, DIB_Status_GdiMod, FALSE );
+    X11DRV_CoerceDIBSection( physDevSrc, DIB_Status_GdiMod, FALSE );
 
-    result = BITBLT_InternalStretchBlt( dcDst, xDst, yDst, width, height,
-                                        dcSrc, xSrc, ySrc, width, height, rop );
+    result = BITBLT_InternalStretchBlt( physDevDst, xDst, yDst, width, height,
+                                        physDevSrc, xSrc, ySrc, width, height, rop );
 
 END:
-    X11DRV_UnlockDIBSection( dcSrc, FALSE );
-    X11DRV_UnlockDIBSection( dcDst, TRUE );
+    X11DRV_UnlockDIBSection( physDevSrc, FALSE );
+    X11DRV_UnlockDIBSection( physDevDst, TRUE );
 
     return result;
 }
@@ -1569,21 +1566,21 @@
 /***********************************************************************
  *           X11DRV_StretchBlt
  */
-BOOL X11DRV_StretchBlt( DC *dcDst, INT xDst, INT yDst,
-                          INT widthDst, INT heightDst,
-                          DC *dcSrc, INT xSrc, INT ySrc,
-                          INT widthSrc, INT heightSrc, DWORD rop )
+BOOL X11DRV_StretchBlt( X11DRV_PDEVICE *physDevDst, INT xDst, INT yDst,
+                        INT widthDst, INT heightDst,
+                        X11DRV_PDEVICE *physDevSrc, INT xSrc, INT ySrc,
+                        INT widthSrc, INT heightSrc, DWORD rop )
 {
     BOOL result;
 
-    X11DRV_LockDIBSection( dcDst, DIB_Status_GdiMod, FALSE );
-    X11DRV_LockDIBSection( dcSrc, DIB_Status_GdiMod, FALSE );
+    X11DRV_LockDIBSection( physDevDst, DIB_Status_GdiMod, FALSE );
+    X11DRV_LockDIBSection( physDevSrc, DIB_Status_GdiMod, FALSE );
 
-    result = BITBLT_InternalStretchBlt( dcDst, xDst, yDst, widthDst, heightDst,
-                                        dcSrc, xSrc, ySrc, widthSrc, heightSrc, rop );
+    result = BITBLT_InternalStretchBlt( physDevDst, xDst, yDst, widthDst, heightDst,
+                                        physDevSrc, xSrc, ySrc, widthSrc, heightSrc, rop );
 
-    X11DRV_UnlockDIBSection( dcSrc, FALSE );
-    X11DRV_UnlockDIBSection( dcDst, TRUE );
+    X11DRV_UnlockDIBSection( physDevSrc, FALSE );
+    X11DRV_UnlockDIBSection( physDevDst, TRUE );
     return result;
 }
 
diff --git a/graphics/x11drv/bitmap.c b/graphics/x11drv/bitmap.c
index 3ab5f71..14d4164 100644
--- a/graphics/x11drv/bitmap.c
+++ b/graphics/x11drv/bitmap.c
@@ -74,14 +74,13 @@
 }
 
 /***********************************************************************
- *           X11DRV_BITMAP_SelectObject
+ *           X11DRV_SelectBitmap   (X11DRV.@)
  */
-HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap )
+HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
 {
     BITMAPOBJ *bmp;
     HRGN hrgn;
-    HBITMAP prevHandle = dc->hBitmap;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
 
     if (!(dc->flags & DC_MEMORY)) return 0;
     if (hbitmap == dc->hBitmap) return hbitmap;  /* nothing to do */
@@ -141,7 +140,7 @@
         DC_InitDC( dc );
     }
     GDI_ReleaseObj( hbitmap );
-    return prevHandle;
+    return hbitmap;
 }
 
 
@@ -481,6 +480,7 @@
         TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
         bmp->physBitmap = NULL;
         bmp->funcs = NULL;
+        if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
         GDI_ReleaseObj( hbitmap );
     }
     return TRUE;
diff --git a/graphics/x11drv/brush.c b/graphics/x11drv/brush.c
index 55c6f78..f0a9c5a 100644
--- a/graphics/x11drv/brush.c
+++ b/graphics/x11drv/brush.c
@@ -154,9 +154,9 @@
 /***********************************************************************
  *           BRUSH_SelectSolidBrush
  */
-static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
+static void BRUSH_SelectSolidBrush( X11DRV_PDEVICE *physDev, COLORREF color )
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
 
     if ((dc->bitsPerPixel > 1) && (screen_depth <= 8) && !COLOR_IsSolid( color ))
     {
@@ -168,7 +168,7 @@
     else
     {
 	  /* Solid brush */
-	physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( dc, color );
+	physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
 	physDev->brush.fillStyle = FillSolid;
     }
 }
@@ -177,10 +177,10 @@
 /***********************************************************************
  *           BRUSH_SelectPatternBrush
  */
-static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
+static BOOL BRUSH_SelectPatternBrush( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
 {
     BOOL ret = FALSE;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
     if (!bmp) return FALSE;
 
@@ -229,21 +229,17 @@
 
 
 /***********************************************************************
- *           BRUSH_SelectObject
+ *           X11DRV_SelectBrush   (X11DRV.@)
  */
-HBRUSH X11DRV_BRUSH_SelectObject( DC * dc, HBRUSH hbrush )
+HBRUSH X11DRV_SelectBrush( X11DRV_PDEVICE *physDev, HBRUSH hbrush )
 {
     LOGBRUSH logbrush;
     HBITMAP16 hBitmap;
     BITMAPINFO * bmpInfo;
-    HBRUSH16 prevHandle = dc->hBrush;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
 
     if (!GetObjectA( hbrush, sizeof(logbrush), &logbrush )) return 0;
 
-    TRACE("hdc=%04x hbrush=%04x\n", dc->hSelf,hbrush);
-
-    dc->hBrush = hbrush;
+    TRACE("hdc=%04x hbrush=%04x\n", physDev->hdc,hbrush);
 
     if (physDev->brush.pixmap)
     {
@@ -260,12 +256,12 @@
 
       case BS_SOLID:
         TRACE("BS_SOLID\n" );
-	BRUSH_SelectSolidBrush( dc, logbrush.lbColor );
+	BRUSH_SelectSolidBrush( physDev, logbrush.lbColor );
 	break;
 	
       case BS_HATCHED:
 	TRACE("BS_HATCHED\n" );
-	physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( dc, logbrush.lbColor );
+	physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, logbrush.lbColor );
 	physDev->brush.pixmap = TSXCreateBitmapFromData( gdi_display, root_window,
 				 HatchBrushes[logbrush.lbHatch], 8, 8 );
 	physDev->brush.fillStyle = FillStippled;
@@ -273,7 +269,7 @@
 	
       case BS_PATTERN:
 	TRACE("BS_PATTERN\n");
-	BRUSH_SelectPatternBrush( dc, (HBRUSH16)logbrush.lbHatch );
+	BRUSH_SelectPatternBrush( physDev, (HBRUSH16)logbrush.lbHatch );
 	break;
 
       case BS_DIBPATTERN:
@@ -281,17 +277,16 @@
 	if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch )))
 	{
 	    int size = DIB_BitmapInfoSize( bmpInfo, logbrush.lbColor );
-	    hBitmap = CreateDIBitmap( dc->hSelf, &bmpInfo->bmiHeader,
+	    hBitmap = CreateDIBitmap( physDev->hdc, &bmpInfo->bmiHeader,
                                         CBM_INIT, ((char *)bmpInfo) + size,
                                         bmpInfo,
                                         (WORD)logbrush.lbColor );
-	    BRUSH_SelectPatternBrush( dc, hBitmap );
+	    BRUSH_SelectPatternBrush( physDev, hBitmap );
 	    DeleteObject( hBitmap );
 	    GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );	    
 	}
 	
 	break;
     }
-    
-    return prevHandle;
+    return hbrush;
 }
diff --git a/graphics/x11drv/clipping.c b/graphics/x11drv/clipping.c
index 8fcf56c..17f3af3 100644
--- a/graphics/x11drv/clipping.c
+++ b/graphics/x11drv/clipping.c
@@ -38,11 +38,11 @@
  *
  *           Could write using GetRegionData but this would be slower.
  */
-void X11DRV_SetDeviceClipping( DC * dc )
+void X11DRV_SetDeviceClipping( X11DRV_PDEVICE *physDev )
 {
     XRectangle *pXrect;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
-    
+    DC *dc = physDev->dc;
+
     RGNOBJ *obj = (RGNOBJ *) GDI_GetObjPtr(dc->hGCClipRgn, REGION_MAGIC);
     if (!obj)
     {
@@ -96,7 +96,7 @@
     DC *dc = DC_GetDCPtr( hdc );
     if (dc)
     {
-        X11DRV_PDEVICE *physDev = dc->physDev;
+        X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
         /*
          * This function change the coordinate system (DCOrgX,DCOrgY)
          * values. When it moves the origin, other data like the current clipping
@@ -114,7 +114,7 @@
         physDev->drawable = drawable;
         TSXSetSubwindowMode( gdi_display, physDev->gc, mode );
 	if(physDev->xrender)
-	  X11DRV_XRender_UpdateDrawable(dc);
+	  X11DRV_XRender_UpdateDrawable( physDev );
         GDI_ReleaseObj( hdc );
     }
 }
@@ -130,7 +130,7 @@
     DC *dc = DC_GetDCPtr( hdc );
     if (dc)
     {
-        X11DRV_PDEVICE *physDev = dc->physDev;
+        X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
         TSXSetGraphicsExposures( gdi_display, physDev->gc, True );
         physDev->exposures = 0;
         GDI_ReleaseObj( hdc );
@@ -151,7 +151,7 @@
     if (dc)
     {
         XEvent event;
-        X11DRV_PDEVICE *physDev = dc->physDev;
+        X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
 
         SetRectRgn( hrgn, 0, 0, 0, 0 );
         wine_tsx11_lock();
diff --git a/graphics/x11drv/dib.c b/graphics/x11drv/dib.c
index ab92d2a..7354b03 100644
--- a/graphics/x11drv/dib.c
+++ b/graphics/x11drv/dib.c
@@ -54,7 +54,7 @@
 /* This structure holds the arguments for DIB_SetImageBits() */
 typedef struct
 {
-    struct tagDC   *dc;
+    X11DRV_PDEVICE *physDev;
     LPCVOID         bits;
     XImage         *image;
     PALETTEENTRY   *palentry;
@@ -143,7 +143,7 @@
  * Fills the color map of a bitmap palette. Should not be called
  * for a >8-bit deep bitmap.
  */
-int *X11DRV_DIB_GenColorMap( DC *dc, int *colorMapping,
+int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
                              WORD coloruse, WORD depth, BOOL quads,
                              const void *colorPtr, int start, int end )
 {
@@ -165,7 +165,7 @@
                                        rgb->rgbBlue > 255*3/2);
             else
                 for (i = start; i < end; i++, rgb++)
-                    colorMapping[i] = X11DRV_PALETTE_ToPhysical( dc, RGB(rgb->rgbRed,
+                    colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, RGB(rgb->rgbRed,
                                                                 rgb->rgbGreen,
                                                                 rgb->rgbBlue));
         }
@@ -179,7 +179,7 @@
                                        rgb->rgbtBlue > 255*3/2);
             else
                 for (i = start; i < end; i++, rgb++)
-                    colorMapping[i] = X11DRV_PALETTE_ToPhysical( dc, RGB(rgb->rgbtRed,
+                    colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, RGB(rgb->rgbtRed,
                                                                rgb->rgbtGreen,
                                                                rgb->rgbtBlue));
         }
@@ -190,10 +190,10 @@
             WORD * index = (WORD *)colorPtr;
 
             for (i = start; i < end; i++, index++)
-                colorMapping[i] = X11DRV_PALETTE_ToPhysical( dc, PALETTEINDEX(*index) );
+                colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(*index) );
         } else {
             for (i = start; i < end; i++)
-                colorMapping[i] = X11DRV_PALETTE_ToPhysical( dc, PALETTEINDEX(i) );
+                colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(i) );
         }
     }
 
@@ -206,7 +206,7 @@
  * Build the color map from the bitmap palette. Should not be called
  * for a >8-bit deep bitmap.
  */
-int *X11DRV_DIB_BuildColorMap( DC *dc, WORD coloruse, WORD depth, 
+int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE *physDev, WORD coloruse, WORD depth, 
                                const BITMAPINFO *info, int *nColors )
 {
     int colors;
@@ -240,7 +240,7 @@
 	return NULL;
 
     *nColors = colors;
-    return X11DRV_DIB_GenColorMap( dc, colorMapping, coloruse, depth,
+    return X11DRV_DIB_GenColorMap( physDev, colorMapping, coloruse, depth,
                                    isInfo, colorPtr, 0, colors);
 }
 
@@ -2939,7 +2939,7 @@
  */
 static void X11DRV_DIB_SetImageBits_16( int lines, const BYTE *srcbits,
                                  DWORD srcwidth, DWORD dstwidth, int left,
-                                       DC *dc, DWORD rSrc, DWORD gSrc, DWORD bSrc,
+                                       X11DRV_PDEVICE *physDev, DWORD rSrc, DWORD gSrc, DWORD bSrc,
                                        XImage *bmpImage, DWORD linebytes )
 {
     DWORD x;
@@ -3185,7 +3185,7 @@
                         ((srcval >> bShift2) & 0x07);
                     XPutPixel(bmpImage, x, h,
                               X11DRV_PALETTE_ToPhysical
-                              (dc, RGB(red,green,blue)));
+                              (physDev, RGB(red,green,blue)));
                 }
                 srcbits += linebytes;
             }
@@ -3528,7 +3528,7 @@
  */
 static void X11DRV_DIB_SetImageBits_24( int lines, const BYTE *srcbits,
                                  DWORD srcwidth, DWORD dstwidth, int left,
-                                 DC *dc,
+                                 X11DRV_PDEVICE *physDev,
                                  DWORD rSrc, DWORD gSrc, DWORD bSrc,
                                  XImage *bmpImage, DWORD linebytes )
 {
@@ -3675,7 +3675,7 @@
                 for (x = left; x < dstwidth+left; x++) {
                     XPutPixel(bmpImage, x, h,
                               X11DRV_PALETTE_ToPhysical
-                              (dc, RGB(srcbyte[2], srcbyte[1], srcbyte[0])));
+                              (physDev, RGB(srcbyte[2], srcbyte[1], srcbyte[0])));
                     srcbyte+=3;
                 }
                 srcbits += linebytes;
@@ -3906,7 +3906,7 @@
  */
 static void X11DRV_DIB_SetImageBits_32(int lines, const BYTE *srcbits,
                                        DWORD srcwidth, DWORD dstwidth, int left,
-                                       DC *dc,
+                                       X11DRV_PDEVICE *physDev,
                                        DWORD rSrc, DWORD gSrc, DWORD bSrc,
                                        XImage *bmpImage,
                                        DWORD linebytes)
@@ -4156,7 +4156,7 @@
                     green=(srcvalue >> gShift) & 0xff;
                     blue= (srcvalue >> bShift) & 0xff;
                     XPutPixel(bmpImage, x, h, X11DRV_PALETTE_ToPhysical
-                              (dc, RGB(red,green,blue)));
+                              (physDev, RGB(red,green,blue)));
                 }
                 srcbits += linebytes;
             }
@@ -4554,21 +4554,21 @@
     case 16:
 	X11DRV_DIB_SetImageBits_16( descr->lines, descr->bits,
 				    descr->infoWidth, descr->width,
-                                   descr->xSrc, descr->dc,
+                                   descr->xSrc, descr->physDev,
                                    descr->rMask, descr->gMask, descr->bMask,
                                    bmpImage, descr->dibpitch);
 	break;
     case 24:
 	X11DRV_DIB_SetImageBits_24( descr->lines, descr->bits,
 				    descr->infoWidth, descr->width,
-				    descr->xSrc, descr->dc, 
+				    descr->xSrc, descr->physDev, 
                                     descr->rMask, descr->gMask, descr->bMask,
 				    bmpImage, descr->dibpitch);
 	break;
     case 32:
 	X11DRV_DIB_SetImageBits_32( descr->lines, descr->bits,
 				    descr->infoWidth, descr->width,
-                                   descr->xSrc, descr->dc,
+                                   descr->xSrc, descr->physDev,
                                    descr->rMask, descr->gMask, descr->bMask,
                                    bmpImage, descr->dibpitch);
 	break;
@@ -4732,7 +4732,7 @@
  *		X11DRV_SetDIBitsToDevice
  *
  */
-INT X11DRV_SetDIBitsToDevice( DC *dc, INT xDest, INT yDest, DWORD cx,
+INT X11DRV_SetDIBitsToDevice( X11DRV_PDEVICE *physDev, INT xDest, INT yDest, DWORD cx,
 				DWORD cy, INT xSrc, INT ySrc,
 				UINT startscan, UINT lines, LPCVOID bits,
 				const BITMAPINFO *info, UINT coloruse )
@@ -4741,8 +4741,7 @@
     DWORD width, oldcy = cy;
     INT result;
     int height, tmpheight;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
-
+    DC *dc = physDev->dc;
 
     if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height, 
 			   &descr.infoBpp, &descr.compression ) == -1)
@@ -4758,7 +4757,7 @@
     if (xSrc + cx >= width) cx = width - xSrc;
     if (!cx || !cy) return 0;
 
-    X11DRV_SetupGCForText( dc );  /* To have the correct colors */
+    X11DRV_SetupGCForText( physDev );  /* To have the correct colors */
     TSXSetFunction(gdi_display, physDev->gc, X11DRV_XROPfunction[dc->ROPmode-1]);
 
     switch (descr.infoBpp)
@@ -4767,7 +4766,7 @@
        case 4:
        case 8:
                descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap( 
-                                            coloruse == DIB_PAL_COLORS ? dc : NULL, coloruse,
+                                            coloruse == DIB_PAL_COLORS ? physDev : NULL, coloruse,
                                             dc->bitsPerPixel, info, &descr.nColorMap );
                if (!descr.colorMap) return 0;
                descr.rMask = descr.gMask = descr.bMask = 0;
@@ -4789,7 +4788,7 @@
                break;
     }
 
-    descr.dc        = dc;
+    descr.physDev   = physDev;
     descr.bits      = bits;
     descr.image     = NULL;
     descr.palentry  = NULL;
@@ -4817,18 +4816,17 @@
 }
 
 /***********************************************************************
- *           X11DRV_DIB_SetDIBits
+ *           X11DRV_SetDIBits   (X11DRV.@)
  */
-INT X11DRV_DIB_SetDIBits(
-  BITMAPOBJ *bmp, DC *dc, UINT startscan,
-  UINT lines, LPCVOID bits, const BITMAPINFO *info,
-  UINT coloruse, HBITMAP hbitmap)
+INT X11DRV_SetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan,
+                      UINT lines, LPCVOID bits, const BITMAPINFO *info, UINT coloruse )
 {
   X11DRV_DIB_IMAGEBITS_DESCR descr;
+  BITMAPOBJ *bmp;
   int height, tmpheight;
   INT result;
 
-  descr.dc = dc;
+  descr.physDev = physDev;
 
   if (DIB_GetBitmapInfo( &info->bmiHeader, &descr.infoWidth, &height,
 			 &descr.infoBpp, &descr.compression ) == -1)
@@ -4839,6 +4837,8 @@
   if (!lines || (startscan >= height))
       return 0;
 
+  if (!(bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
+
   if (startscan + lines > height) lines = height - startscan;
 
   switch (descr.infoBpp)
@@ -4847,10 +4847,14 @@
        case 4:
        case 8:
 	       descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
-                        coloruse == DIB_PAL_COLORS ? descr.dc : NULL, coloruse,
+                        coloruse == DIB_PAL_COLORS ? descr.physDev : NULL, coloruse,
                                                           bmp->bitmap.bmBitsPixel,
                                                           info, &descr.nColorMap );
-               if (!descr.colorMap) return 0;
+               if (!descr.colorMap)
+               {
+                   GDI_ReleaseObj( hbitmap );
+                   return 0;
+               }
                descr.rMask = descr.gMask = descr.bMask = 0;
                break;
        case 15:
@@ -4895,30 +4899,37 @@
 
   if (descr.colorMap) HeapFree(GetProcessHeap(), 0, descr.colorMap);
 
+  GDI_ReleaseObj( hbitmap );
   return result;
 }
 
 /***********************************************************************
- *           X11DRV_DIB_GetDIBits
+ *           X11DRV_GetDIBits   (X11DRV.@)
  */
-INT X11DRV_DIB_GetDIBits(
-  BITMAPOBJ *bmp, DC *dc, UINT startscan, 
-  UINT lines, LPVOID bits, BITMAPINFO *info,
-  UINT coloruse, HBITMAP hbitmap)
+INT X11DRV_GetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan, UINT lines,
+                      LPVOID bits, BITMAPINFO *info, UINT coloruse )
 {
-  X11DRV_DIBSECTION *dib = (X11DRV_DIBSECTION *) bmp->dib;
+  X11DRV_DIBSECTION *dib;
   X11DRV_DIB_IMAGEBITS_DESCR descr;
   PALETTEOBJ * palette;
+  BITMAPOBJ *bmp;
   int height;
-  
+  DC *dc = physDev->dc;
+
+  if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC )))
+      return 0;
+  if (!(bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
+  {
+      GDI_ReleaseObj( dc->hPalette );
+      return 0;
+  }
+  dib = (X11DRV_DIBSECTION *) bmp->dib;
+
   TRACE("%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
 	lines, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
 	(int)info->bmiHeader.biWidth, (int)info->bmiHeader.biHeight,
         startscan );
 
-  if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC )))
-      return 0;
-
   if( lines > bmp->bitmap.bmHeight ) lines = bmp->bitmap.bmHeight;
 
   height = info->bmiHeader.biHeight;
@@ -4971,7 +4982,7 @@
     X11DRV_CreateBitmap(hbitmap);
 
 
-  descr.dc        = dc;
+  descr.physDev   = physDev;
   descr.palentry  = palette->logpalette.palPalEntry;
   descr.bits      = bits;
   descr.image     = NULL;
@@ -5020,7 +5031,7 @@
 
 done:
   GDI_ReleaseObj( dc->hPalette );
- 
+  GDI_ReleaseObj( hbitmap );
   return lines;
 }
 
@@ -5061,7 +5072,7 @@
 			 &descr.infoBpp, &descr.compression ) == -1)
     return;
 
-  descr.dc        = NULL;
+  descr.physDev   = NULL;
   descr.palentry  = NULL;
   descr.image     = dib->image;
   descr.colorMap  = colorMap;
@@ -5122,13 +5133,13 @@
 /***********************************************************************
  *           X11DRV_DIB_CopyDIBSection
  */
-void X11DRV_DIB_CopyDIBSection(DC *dcSrc, DC *dcDst,
-			       DWORD xSrc, DWORD ySrc,
-			       DWORD xDest, DWORD yDest,
-			       DWORD width, DWORD height)
+void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
+                               DWORD xSrc, DWORD ySrc, DWORD xDest, DWORD yDest,
+                               DWORD width, DWORD height)
 {
   BITMAPOBJ *bmp;
-  X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dcDst->physDev;
+  DC *dcSrc = physDevSrc->dc;
+  DC *dcDst = physDevDst->dc;
   int nColorMap = 0, *colorMap = NULL, aColorMap = FALSE;
 
   TRACE("(%p,%p,%ld,%ld,%ld,%ld,%ld,%ld)\n", dcSrc, dcDst,
@@ -5167,7 +5178,7 @@
 	colorMap = dib->colorMap;
 	nColorMap = dib->nColorMap;
       } else {
-	colorMap = X11DRV_DIB_BuildColorMap( dcSrc, (WORD)-1,
+	colorMap = X11DRV_DIB_BuildColorMap( physDevSrc, (WORD)-1,
 					     bmp->dib->dsBm.bmBitsPixel,
 					     (BITMAPINFO*)&(bmp->dib->dsBmih),
 					     &nColorMap );
@@ -5176,7 +5187,7 @@
     }
     /* perform the copy */
     X11DRV_DIB_DoCopyDIBSection(bmp, FALSE, colorMap, nColorMap,
-				physDev->drawable, xSrc, ySrc, xDest, yDest,
+				physDevDst->drawable, xSrc, ySrc, xDest, yDest,
 				width, height);
     /* free color mapping */
     if (aColorMap)
@@ -5534,32 +5545,32 @@
 /***********************************************************************
  *           X11DRV_CoerceDIBSection
  */
-INT X11DRV_CoerceDIBSection(DC *dc, INT req, BOOL lossy)
+INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req, BOOL lossy)
 {
-  if (!dc) return DIB_Status_None;
-  return X11DRV_CoerceDIBSection2( dc->hBitmap, req, lossy );
+  if (!physDev) return DIB_Status_None;
+  return X11DRV_CoerceDIBSection2( physDev->dc->hBitmap, req, lossy );
 }
 
 /***********************************************************************
  *           X11DRV_LockDIBSection
  */
-INT X11DRV_LockDIBSection(DC *dc, INT req, BOOL lossy)
+INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req, BOOL lossy)
 {
-  if (!dc) return DIB_Status_None;
-  if (!(dc->flags & DC_MEMORY)) return DIB_Status_None;
+  if (!physDev) return DIB_Status_None;
+  if (!(physDev->dc->flags & DC_MEMORY)) return DIB_Status_None;
 
-  return X11DRV_LockDIBSection2( dc->hBitmap, req, lossy );
+  return X11DRV_LockDIBSection2( physDev->dc->hBitmap, req, lossy );
 }
 
 /***********************************************************************
  *           X11DRV_UnlockDIBSection
  */
-void X11DRV_UnlockDIBSection(DC *dc, BOOL commit)
+void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
 {
-  if (!dc) return;
-  if (!(dc->flags & DC_MEMORY)) return;
+  if (!physDev) return;
+  if (!(physDev->dc->flags & DC_MEMORY)) return;
 
-  X11DRV_UnlockDIBSection2( dc->hBitmap, commit );
+  X11DRV_UnlockDIBSection2( physDev->dc->hBitmap, commit );
 }
 
 
@@ -5633,7 +5644,7 @@
  *           X11DRV_DIB_CreateDIBSection
  */
 HBITMAP X11DRV_DIB_CreateDIBSection(
-  DC *dc, BITMAPINFO *bmi, UINT usage,
+  X11DRV_PDEVICE *physDev, BITMAPINFO *bmi, UINT usage,
   LPVOID *bits, HANDLE section,
   DWORD offset, DWORD ovr_pitch)
 {
@@ -5694,8 +5705,8 @@
   
   /* Create Color Map */
   if (bm.bmBits && bm.bmBitsPixel <= 8)
-      colorMap = X11DRV_DIB_BuildColorMap( usage == DIB_PAL_COLORS? dc : NULL, 
-				usage, bm.bmBitsPixel, bmi, &nColorMap );
+      colorMap = X11DRV_DIB_BuildColorMap( usage == DIB_PAL_COLORS? physDev : NULL,
+                                           usage, bm.bmBitsPixel, bmi, &nColorMap );
 
   /* Allocate Memory for DIB and fill structure */
   if (bm.bmBits)
@@ -5738,7 +5749,7 @@
   /* Create Device Dependent Bitmap and add DIB pointer */
   if (dib) 
     {
-      res = CreateDIBitmap(dc->hSelf, bi, 0, NULL, bmi, usage);
+      res = CreateDIBitmap(physDev->hdc, bi, 0, NULL, bmi, usage);
       if (res)
 	{
 	  bmp = (BITMAPOBJ *) GDI_GetObjPtr(res, BITMAP_MAGIC);
@@ -5843,50 +5854,62 @@
 }
 
 /***********************************************************************
- *           X11DRV_DIB_SetDIBColorTable
+ *           X11DRV_SetDIBColorTable   (X11DRV.@)
  */
-UINT X11DRV_DIB_SetDIBColorTable(BITMAPOBJ *bmp, DC *dc, UINT start, UINT count, const RGBQUAD *colors)
+UINT X11DRV_SetDIBColorTable( X11DRV_PDEVICE *physDev, UINT start, UINT count, const RGBQUAD *colors )
 {
-  X11DRV_DIBSECTION *dib = (X11DRV_DIBSECTION *) bmp->dib;
+    BITMAPOBJ * bmp;
+    X11DRV_DIBSECTION *dib;
+    UINT ret = 0;
 
-  if (dib && dib->colorMap) {
-    UINT end = count + start;
-    if (end > dib->nColorMap) end = dib->nColorMap;
-    /*
-     * Changing color table might change the mapping between
-     * DIB colors and X11 colors and thus alter the visible state
-     * of the bitmap object.
-     */
-    X11DRV_DIB_Lock(bmp, DIB_Status_AppMod, FALSE);
-    X11DRV_DIB_GenColorMap( dc, dib->colorMap, DIB_RGB_COLORS, 
-                           dib->dibSection.dsBm.bmBitsPixel,
-                            TRUE, colors, start, end );
-    X11DRV_DIB_Unlock(bmp, TRUE);
-    return end - start;
-  }
-  return 0;
+    if (!(bmp = (BITMAPOBJ*)GDI_GetObjPtr( physDev->dc->hBitmap, BITMAP_MAGIC ))) return 0;
+    dib = (X11DRV_DIBSECTION *) bmp->dib;
+
+    if (dib && dib->colorMap) {
+        UINT end = count + start;
+        if (end > dib->nColorMap) end = dib->nColorMap;
+        /*
+         * Changing color table might change the mapping between
+         * DIB colors and X11 colors and thus alter the visible state
+         * of the bitmap object.
+         */
+        X11DRV_DIB_Lock(bmp, DIB_Status_AppMod, FALSE);
+        X11DRV_DIB_GenColorMap( physDev, dib->colorMap, DIB_RGB_COLORS, 
+                                dib->dibSection.dsBm.bmBitsPixel,
+                                TRUE, colors, start, end );
+        X11DRV_DIB_Unlock(bmp, TRUE);
+        ret = end - start;
+    }
+    GDI_ReleaseObj( physDev->dc->hBitmap );
+    return ret;
 }
 
 /***********************************************************************
- *           X11DRV_DIB_GetDIBColorTable
+ *           X11DRV_GetDIBColorTable   (X11DRV.@)
  */
-UINT X11DRV_DIB_GetDIBColorTable(BITMAPOBJ *bmp, DC *dc, UINT start, UINT count, RGBQUAD *colors)
+UINT X11DRV_GetDIBColorTable( X11DRV_PDEVICE *physDev, UINT start, UINT count, RGBQUAD *colors )
 {
-  X11DRV_DIBSECTION *dib = (X11DRV_DIBSECTION *) bmp->dib;
+    BITMAPOBJ * bmp;
+    X11DRV_DIBSECTION *dib;
+    UINT ret = 0;
 
-  if (dib && dib->colorMap) {
-    UINT i, end = count + start;
-    if (end > dib->nColorMap) end = dib->nColorMap;
-    for (i = start; i < end; i++,colors++) {
-      COLORREF col = X11DRV_PALETTE_ToLogical( dib->colorMap[i] );
-      colors->rgbBlue  = GetBValue(col);
-      colors->rgbGreen = GetGValue(col);
-      colors->rgbRed   = GetRValue(col);
-      colors->rgbReserved = 0;
+    if (!(bmp = (BITMAPOBJ*)GDI_GetObjPtr( physDev->dc->hBitmap, BITMAP_MAGIC ))) return 0;
+    dib = (X11DRV_DIBSECTION *) bmp->dib;
+
+    if (dib && dib->colorMap) {
+        UINT i, end = count + start;
+        if (end > dib->nColorMap) end = dib->nColorMap;
+        for (i = start; i < end; i++,colors++) {
+            COLORREF col = X11DRV_PALETTE_ToLogical( dib->colorMap[i] );
+            colors->rgbBlue  = GetBValue(col);
+            colors->rgbGreen = GetGValue(col);
+            colors->rgbRed   = GetRValue(col);
+            colors->rgbReserved = 0;
+        }
+        ret = end-start;
     }
-    return end-start;
-  }
-  return 0;
+    GDI_ReleaseObj( physDev->dc->hBitmap );
+    return ret;
 }
 
 
diff --git a/graphics/x11drv/graphics.c b/graphics/x11drv/graphics.c
index 83953ec..efb8f11 100644
--- a/graphics/x11drv/graphics.c
+++ b/graphics/x11drv/graphics.c
@@ -81,12 +81,12 @@
  * If fMapColors is TRUE, X pixels are mapped to Windows colors.
  * Return FALSE if brush is BS_NULL, TRUE otherwise.
  */
-BOOL X11DRV_SetupGCForPatBlt( DC * dc, GC gc, BOOL fMapColors )
+BOOL X11DRV_SetupGCForPatBlt( X11DRV_PDEVICE *physDev, GC gc, BOOL fMapColors )
 {
     XGCValues val;
     unsigned long mask;
     Pixmap pixmap = 0;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
 
     if (physDev->brush.style == BS_NULL) return FALSE;
     if (physDev->brush.pixel == -1)
@@ -126,7 +126,7 @@
     {
     case FillStippled:
     case FillOpaqueStippled:
-	if (dc->backgroundMode==OPAQUE) val.fill_style = FillOpaqueStippled;
+	if (GetBkMode(dc->hSelf)==OPAQUE) val.fill_style = FillOpaqueStippled;
 	val.stipple = physDev->brush.pixmap;
 	mask = GCStipple;
         break;
@@ -159,7 +159,7 @@
     }
     val.ts_x_origin = dc->DCOrgX + dc->brushOrgX;
     val.ts_y_origin = dc->DCOrgY + dc->brushOrgY;
-    val.fill_rule = (dc->polyFillMode==WINDING) ? WindingRule : EvenOddRule;
+    val.fill_rule = (GetPolyFillMode(physDev->hdc) == WINDING) ? WindingRule : EvenOddRule;
     TSXChangeGC( gdi_display, gc,
 	       GCFunction | GCForeground | GCBackground | GCFillStyle |
 	       GCFillRule | GCTileStipXOrigin | GCTileStipYOrigin | mask,
@@ -175,10 +175,9 @@
  * Setup physDev->gc for drawing operations using current brush.
  * Return FALSE if brush is BS_NULL, TRUE otherwise.
  */
-BOOL X11DRV_SetupGCForBrush( DC * dc )
+BOOL X11DRV_SetupGCForBrush( X11DRV_PDEVICE *physDev )
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
-    return X11DRV_SetupGCForPatBlt( dc, physDev->gc, FALSE );
+    return X11DRV_SetupGCForPatBlt( physDev, physDev->gc, FALSE );
 }
 
 
@@ -188,10 +187,10 @@
  * Setup physDev->gc for drawing operations using current pen.
  * Return FALSE if pen is PS_NULL, TRUE otherwise.
  */
-BOOL X11DRV_SetupGCForPen( DC * dc )
+BOOL X11DRV_SetupGCForPen( X11DRV_PDEVICE *physDev )
 {
     XGCValues val;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
 
     if (physDev->pen.style == PS_NULL) return FALSE;
 
@@ -225,8 +224,7 @@
         (physDev->pen.style != PS_INSIDEFRAME))
     {
         TSXSetDashes( gdi_display, physDev->gc, 0, physDev->pen.dashes, physDev->pen.dash_len );
-	val.line_style = (dc->backgroundMode == OPAQUE) ?
-	                      LineDoubleDash : LineOnOffDash;
+        val.line_style = (GetBkMode(dc->hSelf) == OPAQUE) ? LineDoubleDash : LineOnOffDash;
     }
     else val.line_style = LineSolid;
     val.line_width = physDev->pen.width;
@@ -271,9 +269,8 @@
  * Setup physDev->gc for text drawing operations.
  * Return FALSE if the font is null, TRUE otherwise.
  */
-BOOL X11DRV_SetupGCForText( DC * dc )
+BOOL X11DRV_SetupGCForText( X11DRV_PDEVICE *physDev )
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
     XFontStruct* xfs = XFONT_GetFontStruct( physDev->font );
 
     if( xfs )
@@ -299,15 +296,15 @@
  *           X11DRV_LineTo
  */
 BOOL
-X11DRV_LineTo( DC *dc, INT x, INT y )
+X11DRV_LineTo( X11DRV_PDEVICE *physDev, INT x, INT y )
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
     POINT start;
     POINT end;
 
-    if (X11DRV_SetupGCForPen( dc )) {
+    if (X11DRV_SetupGCForPen( physDev )) {
 	/* Update the pixmap from the DIB section */
-	X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+	X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
 
 	start.x = dc->CursPosX;
 	start.y = dc->CursPosY;
@@ -323,7 +320,7 @@
 		  dc->DCOrgY + end.y);
 
 	/* Update the DIBSection from the pixmap */
-	X11DRV_UnlockDIBSection(dc, TRUE); 
+	X11DRV_UnlockDIBSection(physDev, TRUE); 
     }
     return TRUE;
 }
@@ -338,7 +335,7 @@
  *
  */
 static BOOL
-X11DRV_DrawArc( DC *dc, INT left, INT top, INT right,
+X11DRV_DrawArc( X11DRV_PDEVICE *physDev, INT left, INT top, INT right,
                 INT bottom, INT xstart, INT ystart,
                 INT xend, INT yend, INT lines )
 {
@@ -346,7 +343,7 @@
     INT width, oldwidth, oldendcap;
     double start_angle, end_angle;
     XPoint points[4];
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
     BOOL update = FALSE;
 
     left   = XLPTODP( dc, left );
@@ -407,11 +404,11 @@
     if (idiff_angle <= 0) idiff_angle += 360 * 64;
 
     /* Update the pixmap from the DIB section */
-    X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+    X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
 
       /* Fill arc with brush if Chord() or Pie() */
 
-    if ((lines > 0) && X11DRV_SetupGCForBrush( dc )) {
+    if ((lines > 0) && X11DRV_SetupGCForBrush( physDev )) {
         TSXSetArcMode( gdi_display, physDev->gc, (lines==1) ? ArcChord : ArcPieSlice);
         TSXFillArc( gdi_display, physDev->drawable, physDev->gc,
                  dc->DCOrgX + left, dc->DCOrgY + top,
@@ -421,7 +418,7 @@
 
       /* Draw arc and lines */
 
-    if (X11DRV_SetupGCForPen( dc )){
+    if (X11DRV_SetupGCForPen( physDev )){
         TSXDrawArc( gdi_display, physDev->drawable, physDev->gc,
                     dc->DCOrgX + left, dc->DCOrgY + top,
                     right-left-1, bottom-top-1, istart_angle, idiff_angle );
@@ -485,7 +482,7 @@
     }
 
     /* Update the DIBSection of the pixmap */
-    X11DRV_UnlockDIBSection(dc, update);
+    X11DRV_UnlockDIBSection(physDev, update);
 
     physDev->pen.width = oldwidth;
     physDev->pen.endcap = oldendcap;
@@ -497,10 +494,10 @@
  *           X11DRV_Arc
  */
 BOOL
-X11DRV_Arc( DC *dc, INT left, INT top, INT right, INT bottom,
+X11DRV_Arc( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
             INT xstart, INT ystart, INT xend, INT yend )
 {
-    return X11DRV_DrawArc( dc, left, top, right, bottom,
+    return X11DRV_DrawArc( physDev, left, top, right, bottom,
 			   xstart, ystart, xend, yend, 0 );
 }
 
@@ -509,10 +506,10 @@
  *           X11DRV_Pie
  */
 BOOL
-X11DRV_Pie( DC *dc, INT left, INT top, INT right, INT bottom,
+X11DRV_Pie( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
             INT xstart, INT ystart, INT xend, INT yend )
 {
-    return X11DRV_DrawArc( dc, left, top, right, bottom,
+    return X11DRV_DrawArc( physDev, left, top, right, bottom,
 			   xstart, ystart, xend, yend, 2 );
 }
 
@@ -520,10 +517,10 @@
  *           X11DRV_Chord
  */
 BOOL
-X11DRV_Chord( DC *dc, INT left, INT top, INT right, INT bottom,
+X11DRV_Chord( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
               INT xstart, INT ystart, INT xend, INT yend )
 {
-    return X11DRV_DrawArc( dc, left, top, right, bottom,
+    return X11DRV_DrawArc( physDev, left, top, right, bottom,
 		  	   xstart, ystart, xend, yend, 1 );
 }
 
@@ -532,10 +529,10 @@
  *           X11DRV_Ellipse
  */
 BOOL
-X11DRV_Ellipse( DC *dc, INT left, INT top, INT right, INT bottom )
+X11DRV_Ellipse( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
 {
     INT width, oldwidth;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
     BOOL update = FALSE;
 
     left   = XLPTODP( dc, left );
@@ -564,16 +561,16 @@
     physDev->pen.width = width;
 
     /* Update the pixmap from the DIB section */
-    X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+    X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
 
-    if (X11DRV_SetupGCForBrush( dc ))
+    if (X11DRV_SetupGCForBrush( physDev ))
     {
         TSXFillArc( gdi_display, physDev->drawable, physDev->gc,
                     dc->DCOrgX + left, dc->DCOrgY + top,
                     right-left-1, bottom-top-1, 0, 360*64 );
 	update = TRUE;
     }
-    if (X11DRV_SetupGCForPen( dc ))
+    if (X11DRV_SetupGCForPen( physDev ))
     {
         TSXDrawArc( gdi_display, physDev->drawable, physDev->gc,
                     dc->DCOrgX + left, dc->DCOrgY + top,
@@ -582,7 +579,7 @@
     }
 
     /* Update the DIBSection from the pixmap */
-    X11DRV_UnlockDIBSection(dc, update);
+    X11DRV_UnlockDIBSection(physDev, update);
     
     physDev->pen.width = oldwidth;
     return TRUE;
@@ -593,10 +590,10 @@
  *           X11DRV_Rectangle
  */
 BOOL
-X11DRV_Rectangle(DC *dc, INT left, INT top, INT right, INT bottom)
+X11DRV_Rectangle(X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
 {
     INT width, oldwidth, oldjoinstyle;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
     BOOL update = FALSE;
 
     TRACE("(%d %d %d %d)\n", 
@@ -632,10 +629,10 @@
         physDev->pen.linejoin = PS_JOIN_MITER;
 
     /* Update the pixmap from the DIB section */
-    X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+    X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
     
     if ((right > left + width) && (bottom > top + width))
-        if (X11DRV_SetupGCForBrush( dc ))
+        if (X11DRV_SetupGCForBrush( physDev ))
 	{
             TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
                               dc->DCOrgX + left + (width + 1) / 2,
@@ -643,7 +640,7 @@
                               right-left-width-1, bottom-top-width-1);
 	    update = TRUE;
 	}
-    if (X11DRV_SetupGCForPen( dc ))
+    if (X11DRV_SetupGCForPen( physDev ))
     {
         TSXDrawRectangle( gdi_display, physDev->drawable, physDev->gc,
                           dc->DCOrgX + left, dc->DCOrgY + top,
@@ -652,7 +649,7 @@
     }
 
     /* Update the DIBSection from the pixmap */
-    X11DRV_UnlockDIBSection(dc, update);
+    X11DRV_UnlockDIBSection(physDev, update);
    
     physDev->pen.width = oldwidth;
     physDev->pen.linejoin = oldjoinstyle;
@@ -663,11 +660,11 @@
  *           X11DRV_RoundRect
  */
 BOOL
-X11DRV_RoundRect( DC *dc, INT left, INT top, INT right,
+X11DRV_RoundRect( X11DRV_PDEVICE *physDev, INT left, INT top, INT right,
                   INT bottom, INT ell_width, INT ell_height )
 {
     INT width, oldwidth, oldendcap;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
     BOOL update = FALSE;
 
     TRACE("(%d %d %d %d  %d %d\n", 
@@ -710,10 +707,10 @@
     physDev->pen.endcap = PS_ENDCAP_SQUARE;
 
     /* Update the pixmap from the DIB section */
-    X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+    X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
 
     wine_tsx11_lock();
-    if (X11DRV_SetupGCForBrush( dc ))
+    if (X11DRV_SetupGCForBrush( physDev ))
     {
         if (ell_width > (right-left) )
             if (ell_height > (bottom-top) )
@@ -787,7 +784,8 @@
      * BTW this stuff is optimized for an Xfree86 server
      * read the comments inside the X11DRV_DrawArc function
      */
-    if (X11DRV_SetupGCForPen(dc)) {
+    if (X11DRV_SetupGCForPen( physDev ))
+    {
         if (ell_width > (right-left) )
             if (ell_height > (bottom-top) )
                 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
@@ -855,7 +853,7 @@
     }
     wine_tsx11_unlock();
     /* Update the DIBSection from the pixmap */
-    X11DRV_UnlockDIBSection(dc, update);
+    X11DRV_UnlockDIBSection(physDev, update);
 
     physDev->pen.width = oldwidth;
     physDev->pen.endcap = oldendcap;
@@ -867,17 +865,17 @@
  *           X11DRV_SetPixel
  */
 COLORREF
-X11DRV_SetPixel( DC *dc, INT x, INT y, COLORREF color )
+X11DRV_SetPixel( X11DRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
 {
     Pixel pixel;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
     
     x = dc->DCOrgX + INTERNAL_XWPTODP( dc, x, y );
     y = dc->DCOrgY + INTERNAL_YWPTODP( dc, x, y );
-    pixel = X11DRV_PALETTE_ToPhysical( dc, color );
+    pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
 
     /* Update the pixmap from the DIB section */
-    X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+    X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
 
     /* inefficient but simple... */
     wine_tsx11_lock();
@@ -887,7 +885,7 @@
     wine_tsx11_unlock();
 
     /* Update the DIBSection from the pixmap */
-    X11DRV_UnlockDIBSection(dc, TRUE);
+    X11DRV_UnlockDIBSection(physDev, TRUE);
 
     return X11DRV_PALETTE_ToLogical(pixel);
 }
@@ -897,15 +895,15 @@
  *           X11DRV_GetPixel
  */
 COLORREF
-X11DRV_GetPixel( DC *dc, INT x, INT y )
+X11DRV_GetPixel( X11DRV_PDEVICE *physDev, INT x, INT y )
 {
     static Pixmap pixmap = 0;
     XImage * image;
     int pixel;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
 
     /* Update the pixmap from the DIB section */
-    X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+    X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
 
     x = dc->DCOrgX + INTERNAL_XWPTODP( dc, x, y );
     y = dc->DCOrgY + INTERNAL_YWPTODP( dc, x, y );
@@ -930,7 +928,7 @@
     wine_tsx11_unlock();
 
     /* Update the DIBSection from the pixmap */
-    X11DRV_UnlockDIBSection(dc, FALSE);
+    X11DRV_UnlockDIBSection(physDev, FALSE);
 
     return X11DRV_PALETTE_ToLogical(pixel);
 }
@@ -940,12 +938,12 @@
  *           X11DRV_PaintRgn
  */
 BOOL
-X11DRV_PaintRgn( DC *dc, HRGN hrgn )
+X11DRV_PaintRgn( X11DRV_PDEVICE *physDev, HRGN hrgn )
 {
     RECT box;
     HRGN tmpVisRgn, prevVisRgn;
-    HDC  hdc = dc->hSelf; /* FIXME: should not mix dc/hdc this way */
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
+    HDC hdc = physDev->hdc; /* FIXME: should not mix dc/hdc this way */
 
     if (!(tmpVisRgn = CreateRectRgn( 0, 0, 0, 0 ))) return FALSE;
 
@@ -968,17 +966,17 @@
       /* Fill the region */
 
     GetRgnBox( dc->hGCClipRgn, &box );
-    if (X11DRV_SetupGCForBrush( dc ))
+    if (X11DRV_SetupGCForBrush( physDev ))
     {
 	/* Update the pixmap from the DIB section */
-    	X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+    	X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
 
         TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
 		          box.left, box.top,
 		          box.right-box.left, box.bottom-box.top );
     
 	/* Update the DIBSection from the pixmap */
-    	X11DRV_UnlockDIBSection(dc, TRUE);
+    	X11DRV_UnlockDIBSection(physDev, TRUE);
     }
 
       /* Restore the visible region */
@@ -991,12 +989,12 @@
  *          X11DRV_Polyline
  */
 BOOL
-X11DRV_Polyline( DC *dc, const POINT* pt, INT count )
+X11DRV_Polyline( X11DRV_PDEVICE *physDev, const POINT* pt, INT count )
 {
     INT oldwidth;
     register int i;
     XPoint *points;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
 
     if((oldwidth = physDev->pen.width) == 0) physDev->pen.width = 1;
 
@@ -1011,16 +1009,16 @@
 	points[i].y = dc->DCOrgY + INTERNAL_YWPTODP( dc, pt[i].x, pt[i].y );
     }
 
-    if (X11DRV_SetupGCForPen ( dc ))
+    if (X11DRV_SetupGCForPen ( physDev ))
     {
 	/* Update the pixmap from the DIB section */
-	X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+	X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
  
         TSXDrawLines( gdi_display, physDev->drawable, physDev->gc,
                       points, count, CoordModeOrigin );
 
 	/* Update the DIBSection from the pixmap */
-    	X11DRV_UnlockDIBSection(dc, TRUE);
+    	X11DRV_UnlockDIBSection(physDev, TRUE);
     }
 
     HeapFree( GetProcessHeap(), 0, points );
@@ -1033,11 +1031,11 @@
  *          X11DRV_Polygon
  */
 BOOL
-X11DRV_Polygon( DC *dc, const POINT* pt, INT count )
+X11DRV_Polygon( X11DRV_PDEVICE *physDev, const POINT* pt, INT count )
 {
     register int i;
     XPoint *points;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
     BOOL update = FALSE;
 
     if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (count+1) )))
@@ -1053,15 +1051,15 @@
     points[count] = points[0];
 
     /* Update the pixmap from the DIB section */
-    X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+    X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
  
-    if (X11DRV_SetupGCForBrush( dc ))
+    if (X11DRV_SetupGCForBrush( physDev ))
     {
         TSXFillPolygon( gdi_display, physDev->drawable, physDev->gc,
                         points, count+1, Complex, CoordModeOrigin);
 	update = TRUE;
     }
-    if (X11DRV_SetupGCForPen ( dc ))
+    if (X11DRV_SetupGCForPen ( physDev ))
     {
         TSXDrawLines( gdi_display, physDev->drawable, physDev->gc,
                       points, count+1, CoordModeOrigin );
@@ -1069,7 +1067,7 @@
     }
    
     /* Update the DIBSection from the pixmap */
-    X11DRV_UnlockDIBSection(dc, update);
+    X11DRV_UnlockDIBSection(physDev, update);
 
     HeapFree( GetProcessHeap(), 0, points );
     return TRUE;
@@ -1080,27 +1078,27 @@
  *          X11DRV_PolyPolygon
  */
 BOOL 
-X11DRV_PolyPolygon( DC *dc, const POINT* pt, const INT* counts, UINT polygons)
+X11DRV_PolyPolygon( X11DRV_PDEVICE *physDev, const POINT* pt, const INT* counts, UINT polygons)
 {
     HRGN hrgn;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
 
     /* FIXME: The points should be converted to device coords before */
     /* creating the region. */
 
-    hrgn = CreatePolyPolygonRgn( pt, counts, polygons, dc->polyFillMode );
-    X11DRV_PaintRgn( dc, hrgn );
+    hrgn = CreatePolyPolygonRgn( pt, counts, polygons, GetPolyFillMode( physDev->hdc ) );
+    X11DRV_PaintRgn( physDev, hrgn );
     DeleteObject( hrgn );
 
       /* Draw the outline of the polygons */
 
-    if (X11DRV_SetupGCForPen ( dc ))
+    if (X11DRV_SetupGCForPen ( physDev ))
     {
 	int i, j, max = 0;
 	XPoint *points;
 
 	/* Update the pixmap from the DIB section */
-	X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+	X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
  
 	for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
         if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (max+1) )))
@@ -1122,7 +1120,7 @@
 	}
 	
 	/* Update the DIBSection of the dc's bitmap */
-	X11DRV_UnlockDIBSection(dc, TRUE);
+	X11DRV_UnlockDIBSection(physDev, TRUE);
 
 	HeapFree( GetProcessHeap(), 0, points );
     }
@@ -1134,17 +1132,17 @@
  *          X11DRV_PolyPolyline
  */
 BOOL 
-X11DRV_PolyPolyline( DC *dc, const POINT* pt, const DWORD* counts, DWORD polylines )
+X11DRV_PolyPolyline( X11DRV_PDEVICE *physDev, const POINT* pt, const DWORD* counts, DWORD polylines )
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
     
-    if (X11DRV_SetupGCForPen ( dc ))
+    if (X11DRV_SetupGCForPen ( physDev ))
     {
         int i, j, max = 0;
         XPoint *points;
 
 	/* Update the pixmap from the DIB section */
-    	X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+    	X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
  
         for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
         if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * max )))
@@ -1165,7 +1163,7 @@
         }
 	
 	/* Update the DIBSection of the dc's bitmap */
-    	X11DRV_UnlockDIBSection(dc, TRUE);
+    	X11DRV_UnlockDIBSection(physDev, TRUE);
 
 	HeapFree( GetProcessHeap(), 0, points );
     }
@@ -1180,12 +1178,11 @@
  * (xorg,yorg) is the origin of the X image relative to the drawable.
  * (x,y) is relative to the origin of the X image.
  */
-static void X11DRV_InternalFloodFill(XImage *image, DC *dc,
+static void X11DRV_InternalFloodFill(XImage *image, X11DRV_PDEVICE *physDev,
                                      int x, int y,
                                      int xOrg, int yOrg,
                                      Pixel pixel, WORD fillType )
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
     int left, right;
 
 #define TO_FLOOD(x,y)  ((fillType == FLOODFILLBORDER) ? \
@@ -1220,7 +1217,7 @@
             while ((x < right) && !TO_FLOOD(x,y)) x++;
             if (x >= right) break;
             while ((x < right) && TO_FLOOD(x,y)) x++;
-            X11DRV_InternalFloodFill(image, dc, x-1, y,
+            X11DRV_InternalFloodFill(image, physDev, x-1, y,
                                      xOrg, yOrg, pixel, fillType );
         }
     }
@@ -1235,7 +1232,7 @@
             while ((x < right) && !TO_FLOOD(x,y)) x++;
             if (x >= right) break;
             while ((x < right) && TO_FLOOD(x,y)) x++;
-            X11DRV_InternalFloodFill(image, dc, x-1, y,
+            X11DRV_InternalFloodFill(image, physDev, x-1, y,
                                      xOrg, yOrg, pixel, fillType );
         }
     }
@@ -1247,12 +1244,12 @@
  *          X11DRV_ExtFloodFill
  */
 BOOL
-X11DRV_ExtFloodFill( DC *dc, INT x, INT y, COLORREF color,
+X11DRV_ExtFloodFill( X11DRV_PDEVICE *physDev, INT x, INT y, COLORREF color,
                      UINT fillType )
 {
     XImage *image;
     RECT rect;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
 
     TRACE("X11DRV_ExtFloodFill %d,%d %06lx %d\n", x, y, color, fillType );
 
@@ -1266,23 +1263,23 @@
                                rect.bottom - rect.top,
                                AllPlanes, ZPixmap ))) return FALSE;
 
-    if (X11DRV_SetupGCForBrush( dc ))
+    if (X11DRV_SetupGCForBrush( physDev ))
     {
 	/* Update the pixmap from the DIB section */
-	X11DRV_LockDIBSection(dc, DIB_Status_GdiMod, FALSE);
+	X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
 
           /* ROP mode is always GXcopy for flood-fill */
         wine_tsx11_lock();
         XSetFunction( gdi_display, physDev->gc, GXcopy );
-        X11DRV_InternalFloodFill(image, dc,
+        X11DRV_InternalFloodFill(image, physDev,
                                  XLPTODP(dc,x) + dc->DCOrgX - rect.left,
                                  YLPTODP(dc,y) + dc->DCOrgY - rect.top,
                                  rect.left, rect.top,
-                                 X11DRV_PALETTE_ToPhysical( dc, color ),
+                                 X11DRV_PALETTE_ToPhysical( physDev, color ),
                                  fillType );
         wine_tsx11_unlock();
         /* Update the DIBSection of the dc's bitmap */
-        X11DRV_UnlockDIBSection(dc, TRUE);
+        X11DRV_UnlockDIBSection(physDev, TRUE);
     }
 
     TSXDestroyImage( image );
@@ -1293,44 +1290,29 @@
  *          X11DRV_SetBkColor
  */
 COLORREF
-X11DRV_SetBkColor( DC *dc, COLORREF color )
+X11DRV_SetBkColor( X11DRV_PDEVICE *physDev, COLORREF color )
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
-    COLORREF oldColor;
-
-    oldColor = dc->backgroundColor;
-    dc->backgroundColor = color;
-
-    physDev->backgroundPixel = X11DRV_PALETTE_ToPhysical( dc, color );
-
-    return oldColor;
+    physDev->backgroundPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
+    return color;
 }
 
 /**********************************************************************
  *          X11DRV_SetTextColor
  */
 COLORREF
-X11DRV_SetTextColor( DC *dc, COLORREF color )
+X11DRV_SetTextColor( X11DRV_PDEVICE *physDev, COLORREF color )
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
-    COLORREF oldColor;
-
-    oldColor = dc->textColor;
-    dc->textColor = color;
-
-    physDev->textPixel = X11DRV_PALETTE_ToPhysical( dc, color );
-
-    return oldColor;
+    physDev->textPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
+    return color;
 }
 
 /***********************************************************************
  *           X11DRV_GetDCOrgEx
  */
-BOOL X11DRV_GetDCOrgEx( DC *dc, LPPOINT lpp )
+BOOL X11DRV_GetDCOrgEx( X11DRV_PDEVICE *physDev, LPPOINT lpp )
 {
-    if (!(dc->flags & DC_MEMORY))
+    if (!(physDev->dc->flags & DC_MEMORY))
     {
-       X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *) dc->physDev;
        Window root;
        int x, y, w, h, border, depth;
 
diff --git a/graphics/x11drv/init.c b/graphics/x11drv/init.c
index dc7001f..abc07c0 100644
--- a/graphics/x11drv/init.c
+++ b/graphics/x11drv/init.c
@@ -36,17 +36,6 @@
 
 const DC_FUNCTIONS *X11DRV_DC_Funcs = NULL;  /* hack */
 
-BITMAP_DRIVER X11DRV_BITMAP_Driver =
-{
-  X11DRV_DIB_SetDIBits,
-  X11DRV_DIB_GetDIBits,
-  X11DRV_DIB_DeleteDIBSection,
-  X11DRV_DIB_SetDIBColorTable,
-  X11DRV_DIB_GetDIBColorTable,
-  X11DRV_DIB_Lock,
-  X11DRV_DIB_Unlock
-};
-
 PALETTE_DRIVER X11DRV_PALETTE_Driver =
 {
   X11DRV_PALETTE_SetMapping,
@@ -74,7 +63,6 @@
     Screen *screen = DefaultScreenOfDisplay(display);
 
     gdi_display = display;
-    BITMAP_Driver = &X11DRV_BITMAP_Driver;
     PALETTE_Driver = &X11DRV_PALETTE_Driver;
 
     palette_size = X11DRV_PALETTE_Init();
@@ -114,12 +102,14 @@
 
     if (!X11DRV_DC_Funcs) X11DRV_DC_Funcs = dc->funcs;  /* hack */
 
-    dc->physDev = physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
-				       sizeof(*physDev) );
+    physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) );
     if(!physDev) {
         ERR("Can't allocate physDev\n");
 	return FALSE;
     }
+    dc->physDev = (PHYSDEV)physDev;
+    physDev->hdc = dc->hSelf;
+    physDev->dc  = dc;  /* FIXME */
 
     if (dc->flags & DC_MEMORY)
     {
@@ -172,12 +162,12 @@
 /**********************************************************************
  *	     X11DRV_DeleteDC
  */
-BOOL X11DRV_DeleteDC( DC *dc )
+BOOL X11DRV_DeleteDC( X11DRV_PDEVICE *physDev )
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
 
     if(physDev->xrender)
-      X11DRV_XRender_DeleteDC(dc);
+      X11DRV_XRender_DeleteDC( physDev );
     wine_tsx11_lock();
     XFreeGC( gdi_display, physDev->gc );
     while (physDev->used_visuals-- > 0)
@@ -192,7 +182,7 @@
 /***********************************************************************
  *           GetDeviceCaps    (X11DRV.@)
  */
-INT X11DRV_GetDeviceCaps( DC *dc, INT cap )
+INT X11DRV_GetDeviceCaps( X11DRV_PDEVICE *physDev, INT cap )
 {
     switch(cap)
     {
@@ -254,7 +244,7 @@
     case LOGPIXELSY:
         return log_pixels_y;
     case CAPS1:
-        FIXME("(%04x): CAPS1 is unimplemented, will return 0\n", dc->hSelf );
+        FIXME("(%04x): CAPS1 is unimplemented, will return 0\n", physDev->hdc );
         /* please see wingdi.h for the possible bit-flag values that need
            to be returned. also, see 
            http://msdn.microsoft.com/library/ddkdoc/win95ddk/graphcnt_1m0p.htm */
@@ -275,7 +265,7 @@
     case BTLALIGNMENT:
         return 0;
     default:
-        FIXME("(%04x): unsupported capability %d, will return 0\n", dc->hSelf, cap );
+        FIXME("(%04x): unsupported capability %d, will return 0\n", physDev->hdc, cap );
         return 0;
     }
 }
@@ -284,11 +274,9 @@
 /**********************************************************************
  *           ExtEscape  (X11DRV.@)
  */
-INT X11DRV_ExtEscape( DC *dc, INT escape, INT in_count, LPCVOID in_data,
+INT X11DRV_ExtEscape( X11DRV_PDEVICE *physDev, INT escape, INT in_count, LPCVOID in_data,
                       INT out_count, LPVOID out_data )
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
-
     switch(escape)
     {
     case QUERYESCSUPPORT:
diff --git a/graphics/x11drv/objects.c b/graphics/x11drv/objects.c
index ba9c68a..ecc8ff0 100644
--- a/graphics/x11drv/objects.c
+++ b/graphics/x11drv/objects.c
@@ -30,25 +30,6 @@
 
 
 /***********************************************************************
- *           X11DRV_SelectObject
- */
-HGDIOBJ X11DRV_SelectObject( DC *dc, HGDIOBJ handle )
-{
-    TRACE("hdc=%04x %04x\n", dc->hSelf, handle );
-
-    switch(GetObjectType( handle ))
-    {
-    case OBJ_PEN:    return X11DRV_PEN_SelectObject( dc, handle );
-    case OBJ_BRUSH:  return X11DRV_BRUSH_SelectObject( dc, handle );
-    case OBJ_BITMAP: return X11DRV_BITMAP_SelectObject( dc, handle );
-    case OBJ_FONT:   return X11DRV_FONT_SelectObject( dc, handle );
-    case OBJ_REGION: return (HGDIOBJ)SelectClipRgn( dc->hSelf, handle );
-    }
-    return 0;
-}
-
-
-/***********************************************************************
  *           X11DRV_DeleteObject
  */
 BOOL X11DRV_DeleteObject( HGDIOBJ handle )
diff --git a/graphics/x11drv/palette.c b/graphics/x11drv/palette.c
index a711541..0a69f0e 100644
--- a/graphics/x11drv/palette.c
+++ b/graphics/x11drv/palette.c
@@ -765,8 +765,9 @@
  *
  * Return the physical color closest to 'color'.
  */
-int X11DRV_PALETTE_ToPhysical( DC *dc, COLORREF color )
+int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
 {
+    DC *dc = physDev ? physDev->dc : NULL;
     WORD 		 index = 0;
     HPALETTE16		 hPal = (dc)? dc->hPalette: GetStockObject(DEFAULT_PALETTE);
     unsigned char	 spec_type = color >> 24;
diff --git a/graphics/x11drv/pen.c b/graphics/x11drv/pen.c
index 508366c..799a0b9 100644
--- a/graphics/x11drv/pen.c
+++ b/graphics/x11drv/pen.c
@@ -32,17 +32,15 @@
 static const char PEN_alternate[]  = { 1,1 };
 
 /***********************************************************************
- *           PEN_SelectObject
+ *           X11DRV_SelectPen   (X11DRV.@)
  */
-HPEN X11DRV_PEN_SelectObject( DC * dc, HPEN hpen )
+HPEN X11DRV_SelectPen( X11DRV_PDEVICE *physDev, HPEN hpen )
 {
     LOGPEN logpen;
-    HPEN prevHandle = dc->hPen;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
 
     if (!GetObjectA( hpen, sizeof(logpen), &logpen )) return 0;
 
-    dc->hPen = hpen;
     physDev->pen.style = logpen.lopnStyle & PS_STYLE_MASK;
     physDev->pen.type = logpen.lopnStyle & PS_TYPE_MASK;
     physDev->pen.endcap = logpen.lopnStyle & PS_ENDCAP_MASK;
@@ -52,7 +50,7 @@
                                    dc->xformWorld2Vport.eM11 * 0.5);
     if (physDev->pen.width < 0) physDev->pen.width = -physDev->pen.width;
     if (physDev->pen.width == 1) physDev->pen.width = 0;  /* Faster */
-    physDev->pen.pixel = X11DRV_PALETTE_ToPhysical( dc, logpen.lopnColor );
+    physDev->pen.pixel = X11DRV_PALETTE_ToPhysical( physDev, logpen.lopnColor );
     switch(logpen.lopnStyle & PS_STYLE_MASK)
     {
       case PS_DASH:
@@ -79,6 +77,5 @@
         FIXME("PS_USERSTYLE is not supported\n");
 	break;
     }
-    
-    return prevHandle;
+    return hpen;
 }
diff --git a/graphics/x11drv/text.c b/graphics/x11drv/text.c
index 6b91eb1..fbf5d96 100644
--- a/graphics/x11drv/text.c
+++ b/graphics/x11drv/text.c
@@ -44,7 +44,7 @@
  *           X11DRV_ExtTextOut
  */
 BOOL
-X11DRV_ExtTextOut( DC *dc, INT x, INT y, UINT flags,
+X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
                    const RECT *lprect, LPCWSTR wstr, UINT count,
                    const INT *lpDx )
 {
@@ -55,18 +55,17 @@
     RECT 		rect;
     char		dfBreakChar, lfUnderline, lfStrikeOut;
     BOOL		rotated = FALSE;
-    X11DRV_PDEVICE      *physDev = (X11DRV_PDEVICE *)dc->physDev;
     XChar2b		*str2b = NULL;
     BOOL		dibUpdateFlag = FALSE;
     BOOL                result = TRUE; 
-
+    DC *dc = physDev->dc;
 
     if(dc->gdiFont)
-        return X11DRV_XRender_ExtTextOut(dc, x, y, flags, lprect, wstr, count,
+        return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count,
 					 lpDx);
 
 
-    if (!X11DRV_SetupGCForText( dc )) return TRUE;
+    if (!X11DRV_SetupGCForText( physDev )) return TRUE;
 
     pfo = XFONT_GetFontObject( physDev->font );
     font = pfo->fs;
@@ -104,7 +103,7 @@
             SIZE sz;
             if (flags & ETO_CLIPPED)  /* Can't clip with no rectangle */
 	      return FALSE;
-	    if (!X11DRV_GetTextExtentPoint( dc, wstr, count, &sz ))
+	    if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
 	      return FALSE;
 	    rect.left   = INTERNAL_XWPTODP( dc, x, y );
 	    rect.right  = INTERNAL_XWPTODP( dc, x+sz.cx, y+sz.cy );
@@ -132,7 +131,7 @@
 
     if (flags & ETO_OPAQUE)
     {
-        X11DRV_LockDIBSection( dc, DIB_Status_GdiMod, FALSE );
+        X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
         dibUpdateFlag = TRUE;
         TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
         TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
@@ -151,7 +150,7 @@
     else
     {
         SIZE sz;
-        if (!X11DRV_GetTextExtentPoint( dc, wstr, count, &sz ))
+        if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
 	    return FALSE;
 	width = INTERNAL_XWSTODS(dc, sz.cx);
     }
@@ -215,11 +214,11 @@
 
     if (!dibUpdateFlag)
     {
-        X11DRV_LockDIBSection( dc, DIB_Status_GdiMod, FALSE );
+        X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
         dibUpdateFlag = TRUE;
     }
 
-    if (dc->backgroundMode != TRANSPARENT)
+    if (GetBkMode( physDev->hdc ) != TRANSPARENT)
     {
           /* If rectangle is opaque and clipped, do nothing */
         if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
@@ -406,7 +405,7 @@
     result = FALSE;
     
 END:
-    if (dibUpdateFlag) X11DRV_UnlockDIBSection( dc, TRUE );
+    if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
     return result;
 }
 
@@ -414,10 +413,10 @@
 /***********************************************************************
  *           X11DRV_GetTextExtentPoint
  */
-BOOL X11DRV_GetTextExtentPoint( DC *dc, LPCWSTR str, INT count,
+BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
                                   LPSIZE size )
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
     fontObject* pfo = XFONT_GetFontObject( physDev->font );
 
     TRACE("%s %d\n", debugstr_wn(str,count), count);
diff --git a/graphics/x11drv/xfont.c b/graphics/x11drv/xfont.c
index e34e33b..c866343 100644
--- a/graphics/x11drv/xfont.c
+++ b/graphics/x11drv/xfont.c
@@ -3182,13 +3182,13 @@
  *                                                                     *
  ***********************************************************************/
 /***********************************************************************
- *           X11DRV_FONT_SelectObject
+ *           X11DRV_SelectFont   (X11DRV.@)
  */
-HFONT X11DRV_FONT_SelectObject( DC* dc, HFONT hfont )
+HFONT X11DRV_SelectFont( X11DRV_PDEVICE *physDev, HFONT hfont )
 {
     LOGFONTW logfont;
     LOGFONT16 lf;
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
+    DC *dc = physDev->dc;
 
     TRACE("dc=%p, hfont=%04x\n", dc, hfont);
 
@@ -3197,7 +3197,7 @@
     TRACE("dc->gdiFont = %p\n", dc->gdiFont);
 
     if(dc->gdiFont && X11DRV_XRender_Installed) {
-        X11DRV_XRender_SelectFont(dc, hfont);
+        X11DRV_XRender_SelectFont(physDev, hfont);
 	return FALSE;
     }
 
@@ -3326,9 +3326,8 @@
 /***********************************************************************
  *           X11DRV_GetTextMetrics
  */
-BOOL X11DRV_GetTextMetrics(DC *dc, TEXTMETRICW *metrics)
+BOOL X11DRV_GetTextMetrics(X11DRV_PDEVICE *physDev, TEXTMETRICW *metrics)
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
     TEXTMETRICA tmA;
 
     if( CHECK_PFONT(physDev->font) )
@@ -3345,10 +3344,9 @@
 /***********************************************************************
  *           X11DRV_GetCharWidth
  */
-BOOL X11DRV_GetCharWidth( DC *dc, UINT firstChar, UINT lastChar,
+BOOL X11DRV_GetCharWidth( X11DRV_PDEVICE *physDev, UINT firstChar, UINT lastChar,
                             LPINT buffer )
 {
-    X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
     fontObject* pfo = XFONT_GetFontObject( physDev->font );
 
     if( pfo )