~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/gdi32/dc.c

Version: ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~ [ wine-1.0-rc5 ] ~ [ wine-1.0-rc4 ] ~ [ wine-1.0-rc3 ] ~ [ wine-1.0-rc2 ] ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

  1 /*
  2  * GDI Device Context functions
  3  *
  4  * Copyright 1993 Alexandre Julliard
  5  *
  6  * This library is free software; you can redistribute it and/or
  7  * modify it under the terms of the GNU Lesser General Public
  8  * License as published by the Free Software Foundation; either
  9  * version 2.1 of the License, or (at your option) any later version.
 10  *
 11  * This library is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  * Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU Lesser General Public
 17  * License along with this library; if not, write to the Free Software
 18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 19  */
 20 
 21 #include "config.h"
 22 
 23 #include <assert.h>
 24 #include <stdarg.h>
 25 #include <stdlib.h>
 26 #include <string.h>
 27 #include "windef.h"
 28 #include "winbase.h"
 29 #include "wingdi.h"
 30 #include "winreg.h"
 31 #include "winternl.h"
 32 #include "winerror.h"
 33 #include "wownt32.h"
 34 #include "gdi_private.h"
 35 #include "wine/unicode.h"
 36 #include "wine/debug.h"
 37 
 38 WINE_DEFAULT_DEBUG_CHANNEL(dc);
 39 
 40 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
 41 
 42 static BOOL DC_DeleteObject( HGDIOBJ handle, void *obj );
 43 
 44 static const struct gdi_obj_funcs dc_funcs =
 45 {
 46     NULL,             /* pSelectObject */
 47     NULL,             /* pGetObjectA */
 48     NULL,             /* pGetObjectW */
 49     NULL,             /* pUnrealizeObject */
 50     DC_DeleteObject   /* pDeleteObject */
 51 };
 52 
 53 
 54 static inline DC *get_dc_obj( HDC hdc )
 55 {
 56     DC *dc = GDI_GetObjPtr( hdc, MAGIC_DONTCARE );
 57     if (!dc) return NULL;
 58 
 59     if ((GDIMAGIC(dc->header.wMagic) != DC_MAGIC) &&
 60         (GDIMAGIC(dc->header.wMagic) != MEMORY_DC_MAGIC) &&
 61         (GDIMAGIC(dc->header.wMagic) != METAFILE_DC_MAGIC) &&
 62         (GDIMAGIC(dc->header.wMagic) != ENHMETAFILE_DC_MAGIC))
 63     {
 64         GDI_ReleaseObj( hdc );
 65         SetLastError( ERROR_INVALID_HANDLE );
 66         dc = NULL;
 67     }
 68     return dc;
 69 }
 70 
 71 
 72 /***********************************************************************
 73  *           alloc_dc_ptr
 74  */
 75 DC *alloc_dc_ptr( const DC_FUNCTIONS *funcs, WORD magic )
 76 {
 77     HDC hdc;
 78     DC *dc;
 79 
 80     if (!(dc = GDI_AllocObject( sizeof(*dc), magic, (HGDIOBJ*)&hdc, &dc_funcs ))) return NULL;
 81 
 82     dc->hSelf               = hdc;
 83     dc->funcs               = funcs;
 84     dc->physDev             = NULL;
 85     dc->thread              = GetCurrentThreadId();
 86     dc->refcount            = 1;
 87     dc->dirty               = 0;
 88     dc->saveLevel           = 0;
 89     dc->saved_dc            = 0;
 90     dc->dwHookData          = 0;
 91     dc->hookProc            = NULL;
 92     dc->hookThunk           = NULL;
 93     dc->wndOrgX             = 0;
 94     dc->wndOrgY             = 0;
 95     dc->wndExtX             = 1;
 96     dc->wndExtY             = 1;
 97     dc->vportOrgX           = 0;
 98     dc->vportOrgY           = 0;
 99     dc->vportExtX           = 1;
100     dc->vportExtY           = 1;
101     dc->miterLimit          = 10.0f; /* 10.0 is the default, from MSDN */
102     dc->flags               = 0;
103     dc->layout              = 0;
104     dc->hClipRgn            = 0;
105     dc->hMetaRgn            = 0;
106     dc->hMetaClipRgn        = 0;
107     dc->hVisRgn             = 0;
108     dc->hPen                = GetStockObject( BLACK_PEN );
109     dc->hBrush              = GetStockObject( WHITE_BRUSH );
110     dc->hFont               = GetStockObject( SYSTEM_FONT );
111     dc->hBitmap             = 0;
112     dc->hDevice             = 0;
113     dc->hPalette            = GetStockObject( DEFAULT_PALETTE );
114     dc->gdiFont             = 0;
115     dc->ROPmode             = R2_COPYPEN;
116     dc->polyFillMode        = ALTERNATE;
117     dc->stretchBltMode      = BLACKONWHITE;
118     dc->relAbsMode          = ABSOLUTE;
119     dc->backgroundMode      = OPAQUE;
120     dc->backgroundColor     = RGB( 255, 255, 255 );
121     dc->dcBrushColor        = RGB( 255, 255, 255 );
122     dc->dcPenColor          = RGB( 0, 0, 0 );
123     dc->textColor           = RGB( 0, 0, 0 );
124     dc->brushOrgX           = 0;
125     dc->brushOrgY           = 0;
126     dc->textAlign           = TA_LEFT | TA_TOP | TA_NOUPDATECP;
127     dc->charExtra           = 0;
128     dc->breakExtra          = 0;
129     dc->breakRem            = 0;
130     dc->MapMode             = MM_TEXT;
131     dc->GraphicsMode        = GM_COMPATIBLE;
132     dc->pAbortProc          = NULL;
133     dc->CursPosX            = 0;
134     dc->CursPosY            = 0;
135     dc->ArcDirection        = AD_COUNTERCLOCKWISE;
136     dc->xformWorld2Wnd.eM11 = 1.0f;
137     dc->xformWorld2Wnd.eM12 = 0.0f;
138     dc->xformWorld2Wnd.eM21 = 0.0f;
139     dc->xformWorld2Wnd.eM22 = 1.0f;
140     dc->xformWorld2Wnd.eDx  = 0.0f;
141     dc->xformWorld2Wnd.eDy  = 0.0f;
142     dc->xformWorld2Vport    = dc->xformWorld2Wnd;
143     dc->xformVport2World    = dc->xformWorld2Wnd;
144     dc->vport2WorldValid    = TRUE;
145     dc->BoundsRect.left     = 0;
146     dc->BoundsRect.top      = 0;
147     dc->BoundsRect.right    = 0;
148     dc->BoundsRect.bottom   = 0;
149     dc->saved_visrgn        = NULL;
150     PATH_InitGdiPath(&dc->path);
151     GDI_ReleaseObj( dc->hSelf );
152     return dc;
153 }
154 
155 
156 
157 /***********************************************************************
158  *           free_dc_ptr
159  */
160 BOOL free_dc_ptr( DC *dc )
161 {
162     assert( dc->refcount == 1 );
163     /* grab the gdi lock again */
164     if (!GDI_GetObjPtr( dc->hSelf, MAGIC_DONTCARE )) return FALSE;  /* shouldn't happen */
165     return GDI_FreeObject( dc->hSelf, dc );
166 }
167 
168 
169 /***********************************************************************
170  *           get_dc_ptr
171  *
172  * Retrieve a DC pointer but release the GDI lock.
173  */
174 DC *get_dc_ptr( HDC hdc )
175 {
176     DC *dc = get_dc_obj( hdc );
177     if (!dc) return NULL;
178 
179     if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
180     {
181         dc->thread = GetCurrentThreadId();
182     }
183     else if (dc->thread != GetCurrentThreadId())
184     {
185         WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
186         GDI_ReleaseObj( hdc );
187         return NULL;
188     }
189     else InterlockedIncrement( &dc->refcount );
190 
191     GDI_ReleaseObj( hdc );
192     return dc;
193 }
194 
195 
196 /***********************************************************************
197  *           release_dc_ptr
198  */
199 void release_dc_ptr( DC *dc )
200 {
201     LONG ref;
202 
203     dc->thread = 0;
204     ref = InterlockedDecrement( &dc->refcount );
205     assert( ref >= 0 );
206     if (ref) dc->thread = GetCurrentThreadId();  /* we still own it */
207 }
208 
209 
210 /***********************************************************************
211  *           update_dc
212  *
213  * Make sure the DC vis region is up to date.
214  * This function may call up to USER so the GDI lock should _not_
215  * be held when calling it.
216  */
217 void update_dc( DC *dc )
218 {
219     if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookThunk)
220         dc->hookThunk( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
221 }
222 
223 
224 /***********************************************************************
225  *           DC_DeleteObject
226  */
227 static BOOL DC_DeleteObject( HGDIOBJ handle, void *obj )
228 {
229     GDI_ReleaseObj( handle );
230     return DeleteDC( handle );
231 }
232 
233 
234 /***********************************************************************
235  *           DC_InitDC
236  *
237  * Setup device-specific DC values for a newly created DC.
238  */
239 void DC_InitDC( DC* dc )
240 {
241     if (dc->funcs->pRealizeDefaultPalette) dc->funcs->pRealizeDefaultPalette( dc->physDev );
242     SetTextColor( dc->hSelf, dc->textColor );
243     SetBkColor( dc->hSelf, dc->backgroundColor );
244     SelectObject( dc->hSelf, dc->hPen );
245     SelectObject( dc->hSelf, dc->hBrush );
246     SelectObject( dc->hSelf, dc->hFont );
247     CLIPPING_UpdateGCRegion( dc );
248 }
249 
250 
251 /***********************************************************************
252  *           DC_InvertXform
253  *
254  * Computes the inverse of the transformation xformSrc and stores it to
255  * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
256  * is singular.
257  */
258 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
259 {
260     double determinant;
261 
262     determinant = xformSrc->eM11*xformSrc->eM22 -
263         xformSrc->eM12*xformSrc->eM21;
264     if (determinant > -1e-12 && determinant < 1e-12)
265         return FALSE;
266 
267     xformDest->eM11 =  xformSrc->eM22 / determinant;
268     xformDest->eM12 = -xformSrc->eM12 / determinant;
269     xformDest->eM21 = -xformSrc->eM21 / determinant;
270     xformDest->eM22 =  xformSrc->eM11 / determinant;
271     xformDest->eDx  = -xformSrc->eDx * xformDest->eM11 -
272                        xformSrc->eDy * xformDest->eM21;
273     xformDest->eDy  = -xformSrc->eDx * xformDest->eM12 -
274                        xformSrc->eDy * xformDest->eM22;
275 
276     return TRUE;
277 }
278 
279 
280 /***********************************************************************
281  *           DC_UpdateXforms
282  *
283  * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
284  * fields of the specified DC by creating a transformation that
285  * represents the current mapping mode and combining it with the DC's
286  * world transform. This function should be called whenever the
287  * parameters associated with the mapping mode (window and viewport
288  * extents and origins) or the world transform change.
289  */
290 void DC_UpdateXforms( DC *dc )
291 {
292     XFORM xformWnd2Vport, oldworld2vport;
293     double scaleX, scaleY;
294 
295     /* Construct a transformation to do the window-to-viewport conversion */
296     scaleX = (double)dc->vportExtX / (double)dc->wndExtX;
297     scaleY = (double)dc->vportExtY / (double)dc->wndExtY;
298     xformWnd2Vport.eM11 = scaleX;
299     xformWnd2Vport.eM12 = 0.0;
300     xformWnd2Vport.eM21 = 0.0;
301     xformWnd2Vport.eM22 = scaleY;
302     xformWnd2Vport.eDx  = (double)dc->vportOrgX -
303         scaleX * (double)dc->wndOrgX;
304     xformWnd2Vport.eDy  = (double)dc->vportOrgY -
305         scaleY * (double)dc->wndOrgY;
306 
307     oldworld2vport = dc->xformWorld2Vport;
308     /* Combine with the world transformation */
309     CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
310         &xformWnd2Vport );
311 
312     /* Create inverse of world-to-viewport transformation */
313     dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
314         &dc->xformVport2World );
315 
316     /* Reselect the font and pen back into the dc so that the size
317        gets updated. */
318     if ((oldworld2vport.eM11 != dc->xformWorld2Vport.eM11 ||
319          oldworld2vport.eM22 != dc->xformWorld2Vport.eM22) &&
320          !GdiIsMetaFileDC(dc->hSelf))
321     {
322         SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
323         SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_PEN));
324     }
325 }
326 
327 
328 /***********************************************************************
329  *           GetDCState   (Not a Windows API)
330  */
331 HDC WINAPI GetDCState( HDC hdc )
332 {
333     DC * newdc, * dc;
334     HGDIOBJ handle;
335 
336     if (!(dc = get_dc_ptr( hdc ))) return 0;
337     if (!(newdc = GDI_AllocObject( sizeof(DC), GDIMAGIC(dc->header.wMagic), &handle, &dc_funcs )))
338     {
339       release_dc_ptr( dc );
340       return 0;
341     }
342     TRACE("(%p): returning %p\n", hdc, handle );
343 
344     newdc->flags            = dc->flags | DC_SAVED;
345     newdc->layout           = dc->layout;
346     newdc->hPen             = dc->hPen;
347     newdc->hBrush           = dc->hBrush;
348     newdc->hFont            = dc->hFont;
349     newdc->hBitmap          = dc->hBitmap;
350     newdc->hDevice          = dc->hDevice;
351     newdc->hPalette         = dc->hPalette;
352     newdc->ROPmode          = dc->ROPmode;
353     newdc->polyFillMode     = dc->polyFillMode;
354     newdc->stretchBltMode   = dc->stretchBltMode;
355     newdc->relAbsMode       = dc->relAbsMode;
356     newdc->backgroundMode   = dc->backgroundMode;
357     newdc->backgroundColor  = dc->backgroundColor;
358     newdc->textColor        = dc->textColor;
359     newdc->dcBrushColor     = dc->dcBrushColor;
360     newdc->dcPenColor       = dc->dcPenColor;
361     newdc->brushOrgX        = dc->brushOrgX;
362     newdc->brushOrgY        = dc->brushOrgY;
363     newdc->textAlign        = dc->textAlign;
364     newdc->charExtra        = dc->charExtra;
365     newdc->breakExtra       = dc->breakExtra;
366     newdc->breakRem         = dc->breakRem;
367     newdc->MapMode          = dc->MapMode;
368     newdc->GraphicsMode     = dc->GraphicsMode;
369     newdc->CursPosX         = dc->CursPosX;
370     newdc->CursPosY         = dc->CursPosY;
371     newdc->ArcDirection     = dc->ArcDirection;
372     newdc->xformWorld2Wnd   = dc->xformWorld2Wnd;
373     newdc->xformWorld2Vport = dc->xformWorld2Vport;
374     newdc->xformVport2World = dc->xformVport2World;
375     newdc->vport2WorldValid = dc->vport2WorldValid;
376     newdc->wndOrgX          = dc->wndOrgX;
377     newdc->wndOrgY          = dc->wndOrgY;
378     newdc->wndExtX          = dc->wndExtX;
379     newdc->wndExtY          = dc->wndExtY;
380     newdc->vportOrgX        = dc->vportOrgX;
381     newdc->vportOrgY        = dc->vportOrgY;
382     newdc->vportExtX        = dc->vportExtX;
383     newdc->vportExtY        = dc->vportExtY;
384     newdc->BoundsRect       = dc->BoundsRect;
385 
386     newdc->hSelf = (HDC)handle;
387     newdc->thread    = GetCurrentThreadId();
388     newdc->refcount  = 1;
389     newdc->saveLevel = 0;
390     newdc->saved_dc  = 0;
391     GDI_ReleaseObj( handle );
392 
393     PATH_InitGdiPath( &newdc->path );
394 
395     newdc->pAbortProc = NULL;
396     newdc->hookThunk  = NULL;
397     newdc->hookProc   = 0;
398     newdc->saved_visrgn = NULL;
399 
400     /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
401 
402     newdc->hVisRgn      = 0;
403     newdc->hClipRgn     = 0;
404     newdc->hMetaRgn     = 0;
405     newdc->hMetaClipRgn = 0;
406     if (dc->hClipRgn)
407     {
408         newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
409         CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
410     }
411     if (dc->hMetaRgn)
412     {
413         newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
414         CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
415     }
416     /* don't bother recomputing hMetaClipRgn, we'll do that in SetDCState */
417 
418     if(dc->gdiFont) {
419         newdc->gdiFont = dc->gdiFont;
420     } else
421         newdc->gdiFont = 0;
422 
423     release_dc_ptr( newdc );
424     release_dc_ptr( dc );
425     return handle;
426 }
427 
428 
429 /***********************************************************************
430  *           SetDCState   (Not a Windows API)
431  */
432 void WINAPI SetDCState( HDC hdc, HDC hdcs )
433 {
434     DC *dc, *dcs;
435 
436     if (!(dc = get_dc_ptr( hdc ))) return;
437     if (!(dcs = get_dc_ptr( hdcs )))
438     {
439         release_dc_ptr( dc );
440         return;
441     }
442     if (!(dcs->flags & DC_SAVED))
443     {
444         release_dc_ptr( dc );
445         release_dc_ptr( dcs );
446         return;
447     }
448     TRACE("%p %p\n", hdc, hdcs );
449 
450     update_dc( dc );
451     dc->flags            = dcs->flags & ~DC_SAVED;
452     dc->layout           = dcs->layout;
453     dc->hDevice          = dcs->hDevice;
454     dc->ROPmode          = dcs->ROPmode;
455     dc->polyFillMode     = dcs->polyFillMode;
456     dc->stretchBltMode   = dcs->stretchBltMode;
457     dc->relAbsMode       = dcs->relAbsMode;
458     dc->backgroundMode   = dcs->backgroundMode;
459     dc->backgroundColor  = dcs->backgroundColor;
460     dc->textColor        = dcs->textColor;
461     dc->dcBrushColor     = dcs->dcBrushColor;
462     dc->dcPenColor       = dcs->dcPenColor;
463     dc->brushOrgX        = dcs->brushOrgX;
464     dc->brushOrgY        = dcs->brushOrgY;
465     dc->textAlign        = dcs->textAlign;
466     dc->charExtra        = dcs->charExtra;
467     dc->breakExtra       = dcs->breakExtra;
468     dc->breakRem         = dcs->breakRem;
469     dc->MapMode          = dcs->MapMode;
470     dc->GraphicsMode     = dcs->GraphicsMode;
471     dc->CursPosX         = dcs->CursPosX;
472     dc->CursPosY         = dcs->CursPosY;
473     dc->ArcDirection     = dcs->ArcDirection;
474     dc->xformWorld2Wnd   = dcs->xformWorld2Wnd;
475     dc->xformWorld2Vport = dcs->xformWorld2Vport;
476     dc->xformVport2World = dcs->xformVport2World;
477     dc->vport2WorldValid = dcs->vport2WorldValid;
478     dc->BoundsRect       = dcs->BoundsRect;
479 
480     dc->wndOrgX          = dcs->wndOrgX;
481     dc->wndOrgY          = dcs->wndOrgY;
482     dc->wndExtX          = dcs->wndExtX;
483     dc->wndExtY          = dcs->wndExtY;
484     dc->vportOrgX        = dcs->vportOrgX;
485     dc->vportOrgY        = dcs->vportOrgY;
486     dc->vportExtX        = dcs->vportExtX;
487     dc->vportExtY        = dcs->vportExtY;
488 
489     if (dcs->hClipRgn)
490     {
491         if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
492         CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
493     }
494     else
495     {
496         if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
497         dc->hClipRgn = 0;
498     }
499     if (dcs->hMetaRgn)
500     {
501         if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
502         CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
503     }
504     else
505     {
506         if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
507         dc->hMetaRgn = 0;
508     }
509     CLIPPING_UpdateGCRegion( dc );
510 
511     SelectObject( hdc, dcs->hBitmap );
512     SelectObject( hdc, dcs->hBrush );
513     SelectObject( hdc, dcs->hFont );
514     SelectObject( hdc, dcs->hPen );
515     SetBkColor( hdc, dcs->backgroundColor);
516     SetTextColor( hdc, dcs->textColor);
517     GDISelectPalette( hdc, dcs->hPalette, FALSE );
518     release_dc_ptr( dc );
519     release_dc_ptr( dcs );
520 }
521 
522 
523 /***********************************************************************
524  *           GetDCState   (GDI.179)
525  */
526 HDC16 WINAPI GetDCState16( HDC16 hdc )
527 {
528     return HDC_16( GetDCState( HDC_32(hdc) ));
529 }
530 
531 
532 /***********************************************************************
533  *           SetDCState   (GDI.180)
534  */
535 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
536 {
537     SetDCState( HDC_32(hdc), HDC_32(hdcs) );
538 }
539 
540 
541 /***********************************************************************
542  *           SaveDC    (GDI32.@)
543  */
544 INT WINAPI SaveDC( HDC hdc )
545 {
546     HDC hdcs;
547     DC * dc, * dcs;
548     INT ret;
549 
550     dc = get_dc_ptr( hdc );
551     if (!dc) return 0;
552 
553     if(dc->funcs->pSaveDC)
554     {
555         ret = dc->funcs->pSaveDC( dc->physDev );
556         if(ret)
557             ret = ++dc->saveLevel;
558         release_dc_ptr( dc );
559         return ret;
560     }
561 
562     if (!(hdcs = GetDCState( hdc )))
563     {
564         release_dc_ptr( dc );
565         return 0;
566     }
567     dcs = get_dc_ptr( hdcs );
568 
569     /* Copy path. The reason why path saving / restoring is in SaveDC/
570      * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
571      * functions are only in Win16 (which doesn't have paths) and that
572      * SetDCState doesn't allow us to signal an error (which can happen
573      * when copying paths).
574      */
575     if (!PATH_AssignGdiPath( &dcs->path, &dc->path ))
576     {
577         release_dc_ptr( dc );
578         release_dc_ptr( dcs );
579         DeleteDC( hdcs );
580         return 0;
581     }
582 
583     dcs->saved_dc = dc->saved_dc;
584     dc->saved_dc = hdcs;
585     TRACE("(%p): returning %d\n", hdc, dc->saveLevel+1 );
586     ret = ++dc->saveLevel;
587     release_dc_ptr( dcs );
588     release_dc_ptr( dc );
589     return ret;
590 }
591 
592 
593 /***********************************************************************
594  *           RestoreDC    (GDI32.@)
595  */
596 BOOL WINAPI RestoreDC( HDC hdc, INT level )
597 {
598     DC * dc, * dcs;
599     BOOL success;
600 
601     TRACE("%p %d\n", hdc, level );
602     if (!(dc = get_dc_ptr( hdc ))) return FALSE;
603 
604     if(abs(level) > dc->saveLevel || level == 0)
605     {
606         release_dc_ptr( dc );
607         return FALSE;
608     }
609 
610     update_dc( dc );
611 
612     if(dc->funcs->pRestoreDC)
613     {
614         success = dc->funcs->pRestoreDC( dc->physDev, level );
615         if(level < 0) level = dc->saveLevel + level + 1;
616         if(success)
617             dc->saveLevel = level - 1;
618         release_dc_ptr( dc );
619         return success;
620     }
621 
622     if (level < 0) level = dc->saveLevel + level + 1;
623     success=TRUE;
624     while (dc->saveLevel >= level)
625     {
626         HDC hdcs = dc->saved_dc;
627         if (!(dcs = get_dc_ptr( hdcs )))
628         {
629             success = FALSE;
630             break;
631         }
632         dc->saved_dc = dcs->saved_dc;
633         dcs->saved_dc = 0;
634         if (--dc->saveLevel < level)
635         {
636             SetDCState( hdc, hdcs );
637             if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
638                 /* FIXME: This might not be quite right, since we're
639                  * returning FALSE but still destroying the saved DC state */
640                 success=FALSE;
641         }
642         release_dc_ptr( dcs );
643         DeleteDC( hdcs );
644     }
645     release_dc_ptr( dc );
646     return success;
647 }
648 
649 
650 /***********************************************************************
651  *           CreateDCW    (GDI32.@)
652  */
653 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
654                       const DEVMODEW *initData )
655 {
656     HDC hdc;
657     DC * dc;
658     const DC_FUNCTIONS *funcs;
659     WCHAR buf[300];
660 
661     GDI_CheckNotLock();
662 
663     if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
664     {
665         if (!driver)
666         {
667             ERR( "no device found for %s\n", debugstr_w(device) );
668             return 0;
669         }
670         strcpyW(buf, driver);
671     }
672 
673     if (!(funcs = DRIVER_load_driver( buf )))
674     {
675         ERR( "no driver found for %s\n", debugstr_w(buf) );
676         return 0;
677     }
678     if (!(dc = alloc_dc_ptr( funcs, DC_MAGIC ))) goto error;
679     hdc = dc->hSelf;
680 
681     dc->hBitmap = GetStockObject( DEFAULT_BITMAP );
682     if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error;
683 
684     TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
685           debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
686 
687     if (dc->funcs->pCreateDC &&
688         !dc->funcs->pCreateDC( hdc, &dc->physDev, buf, device, output, initData ))
689     {
690         WARN("creation aborted by device\n" );
691         goto error;
692     }
693 
694     SetRectRgn( dc->hVisRgn, 0, 0,
695                 GetDeviceCaps( hdc, DESKTOPHORZRES ), GetDeviceCaps( hdc, DESKTOPVERTRES ) );
696 
697     DC_InitDC( dc );
698     release_dc_ptr( dc );
699     return hdc;
700 
701 error:
702     if (dc && dc->hVisRgn) DeleteObject( dc->hVisRgn );
703     if (dc) free_dc_ptr( dc );
704     DRIVER_release_driver( funcs );
705     return 0;
706 }
707 
708 
709 /***********************************************************************
710  *           CreateDCA    (GDI32.@)
711  */
712 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
713                       const DEVMODEA *initData )
714 {
715     UNICODE_STRING driverW, deviceW, outputW;
716     DEVMODEW *initDataW;
717     HDC ret;
718 
719     if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
720     else driverW.Buffer = NULL;
721 
722     if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
723     else deviceW.Buffer = NULL;
724 
725     if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
726     else outputW.Buffer = NULL;
727 
728     initDataW = NULL;
729     if (initData)
730     {
731         /* don't convert initData for DISPLAY driver, it's not used */
732         if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
733             initDataW = GdiConvertToDevmodeW(initData);
734     }
735 
736     ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
737 
738     RtlFreeUnicodeString(&driverW);
739     RtlFreeUnicodeString(&deviceW);
740     RtlFreeUnicodeString(&outputW);
741     HeapFree(GetProcessHeap(), 0, initDataW);
742     return ret;
743 }
744 
745 
746 /***********************************************************************
747  *           CreateICA    (GDI32.@)
748  */
749 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
750                           const DEVMODEA* initData )
751 {
752       /* Nothing special yet for ICs */
753     return CreateDCA( driver, device, output, initData );
754 }
755 
756 
757 /***********************************************************************
758  *           CreateICW    (GDI32.@)
759  */
760 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
761                           const DEVMODEW* initData )
762 {
763       /* Nothing special yet for ICs */
764     return CreateDCW( driver, device, output, initData );
765 }
766 
767 
768 /***********************************************************************
769  *           CreateCompatibleDC   (GDI32.@)
770  */
771 HDC WINAPI CreateCompatibleDC( HDC hdc )
772 {
773     DC *dc, *origDC;
774     HDC ret;
775     const DC_FUNCTIONS *funcs = NULL;
776     PHYSDEV physDev = NULL;
777 
778     GDI_CheckNotLock();
779 
780     if ((origDC = get_dc_ptr( hdc )))
781     {
782         if (GetObjectType( hdc ) == OBJ_DC)
783         {
784             funcs = origDC->funcs;
785             physDev = origDC->physDev;
786         }
787         release_dc_ptr( origDC );
788         if (funcs) funcs = DRIVER_get_driver( funcs );
789     }
790     else if (hdc) return 0;
791 
792     if (!funcs && !(funcs = DRIVER_load_driver( displayW ))) return 0;
793 
794     if (!(dc = alloc_dc_ptr( funcs, MEMORY_DC_MAGIC ))) goto error;
795 
796     TRACE("(%p): returning %p\n", hdc, dc->hSelf );
797 
798     dc->hBitmap = GetStockObject( DEFAULT_BITMAP );
799     if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error;   /* default bitmap is 1x1 */
800 
801     /* Copy the driver-specific physical device info into
802      * the new DC. The driver may use this read-only info
803      * while creating the compatible DC below. */
804     dc->physDev = physDev;
805     ret = dc->hSelf;
806 
807     if (dc->funcs->pCreateDC &&
808         !dc->funcs->pCreateDC( dc->hSelf, &dc->physDev, NULL, NULL, NULL, NULL ))
809     {
810         WARN("creation aborted by device\n");
811         goto error;
812     }
813 
814     DC_InitDC( dc );
815     release_dc_ptr( dc );
816     return ret;
817 
818 error:
819     if (dc && dc->hVisRgn) DeleteObject( dc->hVisRgn );
820     if (dc) free_dc_ptr( dc );
821     DRIVER_release_driver( funcs );
822     return 0;
823 }
824 
825 
826 /***********************************************************************
827  *           DeleteDC    (GDI32.@)
828  */
829 BOOL WINAPI DeleteDC( HDC hdc )
830 {
831     const DC_FUNCTIONS *funcs = NULL;
832     DC * dc;
833 
834     TRACE("%p\n", hdc );
835 
836     GDI_CheckNotLock();
837 
838     if (!(dc = get_dc_ptr( hdc ))) return FALSE;
839     if (dc->refcount != 1)
840     {
841         FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
842         release_dc_ptr( dc );
843         return FALSE;
844     }
845 
846     /* Call hook procedure to check whether is it OK to delete this DC */
847     if (dc->hookThunk && !dc->hookThunk( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ))
848     {
849         release_dc_ptr( dc );
850         return FALSE;
851     }
852 
853     while (dc->saveLevel)
854     {
855         DC * dcs;
856         HDC hdcs = dc->saved_dc;
857         if (!(dcs = get_dc_ptr( hdcs ))) break;
858         dc->saved_dc = dcs->saved_dc;
859         dc->saveLevel--;
860         if (dcs->hClipRgn) DeleteObject( dcs->hClipRgn );
861         if (dcs->hMetaRgn) DeleteObject( dcs->hMetaRgn );
862         if (dcs->hMetaClipRgn) DeleteObject( dcs->hMetaClipRgn );
863         if (dcs->hVisRgn) DeleteObject( dcs->hVisRgn );
864         PATH_DestroyGdiPath(&dcs->path);
865         free_dc_ptr( dcs );
866     }
867 
868     if (!(dc->flags & DC_SAVED))
869     {
870         SelectObject( hdc, GetStockObject(BLACK_PEN) );
871         SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
872         SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
873         SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
874         funcs = dc->funcs;
875         if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc->physDev);
876         dc->physDev = NULL;
877     }
878 
879     while (dc->saved_visrgn)
880     {
881         struct saved_visrgn *next = dc->saved_visrgn->next;
882         DeleteObject( dc->saved_visrgn->hrgn );
883         HeapFree( GetProcessHeap(), 0, dc->saved_visrgn );
884         dc->saved_visrgn = next;
885     }
886     if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
887     if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
888     if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
889     if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
890     PATH_DestroyGdiPath(&dc->path);
891 
892     free_dc_ptr( dc );
893     if (funcs) DRIVER_release_driver( funcs );  /* do that after releasing the GDI lock */
894     return TRUE;
895 }
896 
897 
898 /***********************************************************************
899  *           ResetDCW    (GDI32.@)
900  */
901 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
902 {
903     DC *dc;
904     HDC ret = hdc;
905 
906     if ((dc = get_dc_ptr( hdc )))
907     {
908         if (dc->funcs->pResetDC) ret = dc->funcs->pResetDC( dc->physDev, devmode );
909         release_dc_ptr( dc );
910     }
911     return ret;
912 }
913 
914 
915 /***********************************************************************
916  *           ResetDCA    (GDI32.@)
917  */
918 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
919 {
920     DEVMODEW *devmodeW;
921     HDC ret;
922 
923     if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
924     else devmodeW = NULL;
925 
926     ret = ResetDCW(hdc, devmodeW);
927 
928     HeapFree(GetProcessHeap(), 0, devmodeW);
929     return ret;
930 }
931 
932 
933 /***********************************************************************
934  *           GetDeviceCaps    (GDI32.@)
935  */
936 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
937 {
938     DC *dc;
939     INT ret = 0;
940 
941     if ((dc = get_dc_ptr( hdc )))
942     {
943         if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc->physDev, cap );
944         else switch(cap)  /* return meaningful values for some entries */
945         {
946         case HORZRES:     ret = 640; break;
947         case VERTRES:     ret = 480; break;
948         case BITSPIXEL:   ret = 1; break;
949         case PLANES:      ret = 1; break;
950         case NUMCOLORS:   ret = 2; break;
951         case ASPECTX:     ret = 36; break;
952         case ASPECTY:     ret = 36; break;
953         case ASPECTXY:    ret = 51; break;
954         case LOGPIXELSX:  ret = 72; break;
955         case LOGPIXELSY:  ret = 72; break;
956         case SIZEPALETTE: ret = 2; break;
957         }
958         release_dc_ptr( dc );
959     }
960     return ret;
961 }
962 
963 
964 /***********************************************************************
965  *              GetBkColor (GDI32.@)
966  */
967 COLORREF WINAPI GetBkColor( HDC hdc )
968 {
969     COLORREF ret = 0;
970     DC * dc = get_dc_ptr( hdc );
971     if (dc)
972     {
973         ret = dc->backgroundColor;
974         release_dc_ptr( dc );
975     }
976     return ret;
977 }
978 
979 
980 /***********************************************************************
981  *           SetBkColor    (GDI32.@)
982  */
983 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
984 {
985     COLORREF oldColor;
986     DC * dc = get_dc_ptr( hdc );
987 
988     TRACE("hdc=%p color=0x%08x\n", hdc, color);
989 
990     if (!dc) return CLR_INVALID;
991     oldColor = dc->backgroundColor;
992     if (dc->funcs->pSetBkColor)
993     {
994         color = dc->funcs->pSetBkColor(dc->physDev, color);
995         if (color == CLR_INVALID)  /* don't change it */
996         {
997             color = oldColor;
998             oldColor = CLR_INVALID;
999         }
1000     }
1001     dc->backgroundColor = color;
1002     release_dc_ptr( dc );
1003     return oldColor;
1004 }
1005 
1006 
1007 /***********************************************************************
1008  *              GetTextColor (GDI32.@)
1009  */
1010 COLORREF WINAPI GetTextColor( HDC hdc )
1011 {
1012     COLORREF ret = 0;
1013     DC * dc = get_dc_ptr( hdc );
1014     if (dc)
1015     {
1016         ret = dc->textColor;
1017         release_dc_ptr( dc );
1018     }
1019     return ret;
1020 }
1021 
1022 
1023 /***********************************************************************
1024  *           SetTextColor    (GDI32.@)
1025  */
1026 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
1027 {
1028     COLORREF oldColor;
1029     DC * dc = get_dc_ptr( hdc );
1030 
1031     TRACE(" hdc=%p color=0x%08x\n", hdc, color);
1032 
1033     if (!dc) return CLR_INVALID;
1034     oldColor = dc->textColor;
1035     if (dc->funcs->pSetTextColor)
1036     {
1037         color = dc->funcs->pSetTextColor(dc->physDev, color);
1038         if (color == CLR_INVALID)  /* don't change it */
1039         {
1040             color = oldColor;
1041             oldColor = CLR_INVALID;
1042         }
1043     }
1044     dc->textColor = color;
1045     release_dc_ptr( dc );
1046     return oldColor;
1047 }
1048 
1049 
1050 /***********************************************************************
1051  *              GetTextAlign (GDI32.@)
1052  */
1053 UINT WINAPI GetTextAlign( HDC hdc )
1054 {
1055     UINT ret = 0;
1056     DC * dc = get_dc_ptr( hdc );
1057     if (dc)
1058     {
1059         ret = dc->textAlign;
1060         release_dc_ptr( dc );
1061     }
1062     return ret;
1063 }
1064 
1065 
1066 /***********************************************************************
1067  *           SetTextAlign    (GDI32.@)
<