gdi32: Release GDI handles before freeing the object.
[wine/hacks.git] / dlls / gdi32 / dc.c
blob3648f64b8cc7a438194c6cbeb01bdb58ad7fb2ab
1 /*
2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
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.
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.
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
21 #include "config.h"
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"
38 WINE_DEFAULT_DEBUG_CHANNEL(dc);
40 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
42 static BOOL DC_DeleteObject( HGDIOBJ handle );
44 static const struct gdi_obj_funcs dc_funcs =
46 NULL, /* pSelectObject */
47 NULL, /* pGetObjectA */
48 NULL, /* pGetObjectW */
49 NULL, /* pUnrealizeObject */
50 DC_DeleteObject /* pDeleteObject */
54 static inline DC *get_dc_obj( HDC hdc )
56 DC *dc = GDI_GetObjPtr( hdc, 0 );
57 if (!dc) return NULL;
59 if ((dc->header.type != OBJ_DC) &&
60 (dc->header.type != OBJ_MEMDC) &&
61 (dc->header.type != OBJ_METADC) &&
62 (dc->header.type != OBJ_ENHMETADC))
64 GDI_ReleaseObj( hdc );
65 SetLastError( ERROR_INVALID_HANDLE );
66 dc = NULL;
68 return dc;
72 /***********************************************************************
73 * alloc_dc_ptr
75 DC *alloc_dc_ptr( const DC_FUNCTIONS *funcs, WORD magic )
77 DC *dc;
79 if (!(dc = HeapAlloc( GetProcessHeap(), 0, sizeof(*dc) ))) return NULL;
81 dc->funcs = funcs;
82 dc->physDev = NULL;
83 dc->thread = GetCurrentThreadId();
84 dc->refcount = 1;
85 dc->dirty = 0;
86 dc->saveLevel = 0;
87 dc->saved_dc = 0;
88 dc->dwHookData = 0;
89 dc->hookProc = NULL;
90 dc->hookThunk = NULL;
91 dc->wndOrgX = 0;
92 dc->wndOrgY = 0;
93 dc->wndExtX = 1;
94 dc->wndExtY = 1;
95 dc->vportOrgX = 0;
96 dc->vportOrgY = 0;
97 dc->vportExtX = 1;
98 dc->vportExtY = 1;
99 dc->miterLimit = 10.0f; /* 10.0 is the default, from MSDN */
100 dc->flags = 0;
101 dc->layout = 0;
102 dc->hClipRgn = 0;
103 dc->hMetaRgn = 0;
104 dc->hMetaClipRgn = 0;
105 dc->hVisRgn = 0;
106 dc->hPen = GetStockObject( BLACK_PEN );
107 dc->hBrush = GetStockObject( WHITE_BRUSH );
108 dc->hFont = GetStockObject( SYSTEM_FONT );
109 dc->hBitmap = 0;
110 dc->hDevice = 0;
111 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
112 dc->gdiFont = 0;
113 dc->ROPmode = R2_COPYPEN;
114 dc->polyFillMode = ALTERNATE;
115 dc->stretchBltMode = BLACKONWHITE;
116 dc->relAbsMode = ABSOLUTE;
117 dc->backgroundMode = OPAQUE;
118 dc->backgroundColor = RGB( 255, 255, 255 );
119 dc->dcBrushColor = RGB( 255, 255, 255 );
120 dc->dcPenColor = RGB( 0, 0, 0 );
121 dc->textColor = RGB( 0, 0, 0 );
122 dc->brushOrgX = 0;
123 dc->brushOrgY = 0;
124 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
125 dc->charExtra = 0;
126 dc->breakExtra = 0;
127 dc->breakRem = 0;
128 dc->MapMode = MM_TEXT;
129 dc->GraphicsMode = GM_COMPATIBLE;
130 dc->pAbortProc = NULL;
131 dc->CursPosX = 0;
132 dc->CursPosY = 0;
133 dc->ArcDirection = AD_COUNTERCLOCKWISE;
134 dc->xformWorld2Wnd.eM11 = 1.0f;
135 dc->xformWorld2Wnd.eM12 = 0.0f;
136 dc->xformWorld2Wnd.eM21 = 0.0f;
137 dc->xformWorld2Wnd.eM22 = 1.0f;
138 dc->xformWorld2Wnd.eDx = 0.0f;
139 dc->xformWorld2Wnd.eDy = 0.0f;
140 dc->xformWorld2Vport = dc->xformWorld2Wnd;
141 dc->xformVport2World = dc->xformWorld2Wnd;
142 dc->vport2WorldValid = TRUE;
143 dc->BoundsRect.left = 0;
144 dc->BoundsRect.top = 0;
145 dc->BoundsRect.right = 0;
146 dc->BoundsRect.bottom = 0;
147 dc->saved_visrgn = NULL;
148 PATH_InitGdiPath(&dc->path);
150 if (!(dc->hSelf = alloc_gdi_handle( &dc->header, magic, &dc_funcs )))
152 HeapFree( GetProcessHeap(), 0, dc );
153 dc = NULL;
155 return dc;
160 /***********************************************************************
161 * free_dc_ptr
163 BOOL free_dc_ptr( DC *dc )
165 assert( dc->refcount == 1 );
166 if (free_gdi_handle( dc->hSelf ) != dc) return FALSE; /* shouldn't happen */
167 return HeapFree( GetProcessHeap(), 0, dc );
171 /***********************************************************************
172 * get_dc_ptr
174 * Retrieve a DC pointer but release the GDI lock.
176 DC *get_dc_ptr( HDC hdc )
178 DC *dc = get_dc_obj( hdc );
179 if (!dc) return NULL;
181 if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
183 dc->thread = GetCurrentThreadId();
185 else if (dc->thread != GetCurrentThreadId())
187 WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
188 GDI_ReleaseObj( hdc );
189 return NULL;
191 else InterlockedIncrement( &dc->refcount );
193 GDI_ReleaseObj( hdc );
194 return dc;
198 /***********************************************************************
199 * release_dc_ptr
201 void release_dc_ptr( DC *dc )
203 LONG ref;
205 dc->thread = 0;
206 ref = InterlockedDecrement( &dc->refcount );
207 assert( ref >= 0 );
208 if (ref) dc->thread = GetCurrentThreadId(); /* we still own it */
212 /***********************************************************************
213 * update_dc
215 * Make sure the DC vis region is up to date.
216 * This function may call up to USER so the GDI lock should _not_
217 * be held when calling it.
219 void update_dc( DC *dc )
221 if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookThunk)
222 dc->hookThunk( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
226 /***********************************************************************
227 * DC_DeleteObject
229 static BOOL DC_DeleteObject( HGDIOBJ handle )
231 return DeleteDC( handle );
235 /***********************************************************************
236 * DC_InitDC
238 * Setup device-specific DC values for a newly created DC.
240 void DC_InitDC( DC* dc )
242 if (dc->funcs->pRealizeDefaultPalette) dc->funcs->pRealizeDefaultPalette( dc->physDev );
243 SetTextColor( dc->hSelf, dc->textColor );
244 SetBkColor( dc->hSelf, dc->backgroundColor );
245 SelectObject( dc->hSelf, dc->hPen );
246 SelectObject( dc->hSelf, dc->hBrush );
247 SelectObject( dc->hSelf, dc->hFont );
248 CLIPPING_UpdateGCRegion( dc );
252 /***********************************************************************
253 * DC_InvertXform
255 * Computes the inverse of the transformation xformSrc and stores it to
256 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
257 * is singular.
259 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
261 double determinant;
263 determinant = xformSrc->eM11*xformSrc->eM22 -
264 xformSrc->eM12*xformSrc->eM21;
265 if (determinant > -1e-12 && determinant < 1e-12)
266 return FALSE;
268 xformDest->eM11 = xformSrc->eM22 / determinant;
269 xformDest->eM12 = -xformSrc->eM12 / determinant;
270 xformDest->eM21 = -xformSrc->eM21 / determinant;
271 xformDest->eM22 = xformSrc->eM11 / determinant;
272 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
273 xformSrc->eDy * xformDest->eM21;
274 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
275 xformSrc->eDy * xformDest->eM22;
277 return TRUE;
281 /***********************************************************************
282 * DC_UpdateXforms
284 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
285 * fields of the specified DC by creating a transformation that
286 * represents the current mapping mode and combining it with the DC's
287 * world transform. This function should be called whenever the
288 * parameters associated with the mapping mode (window and viewport
289 * extents and origins) or the world transform change.
291 void DC_UpdateXforms( DC *dc )
293 XFORM xformWnd2Vport, oldworld2vport;
294 double scaleX, scaleY;
296 /* Construct a transformation to do the window-to-viewport conversion */
297 scaleX = (double)dc->vportExtX / (double)dc->wndExtX;
298 scaleY = (double)dc->vportExtY / (double)dc->wndExtY;
299 xformWnd2Vport.eM11 = scaleX;
300 xformWnd2Vport.eM12 = 0.0;
301 xformWnd2Vport.eM21 = 0.0;
302 xformWnd2Vport.eM22 = scaleY;
303 xformWnd2Vport.eDx = (double)dc->vportOrgX -
304 scaleX * (double)dc->wndOrgX;
305 xformWnd2Vport.eDy = (double)dc->vportOrgY -
306 scaleY * (double)dc->wndOrgY;
308 oldworld2vport = dc->xformWorld2Vport;
309 /* Combine with the world transformation */
310 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
311 &xformWnd2Vport );
313 /* Create inverse of world-to-viewport transformation */
314 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
315 &dc->xformVport2World );
317 /* Reselect the font and pen back into the dc so that the size
318 gets updated. */
319 if (memcmp(&oldworld2vport, &dc->xformWorld2Vport, sizeof(oldworld2vport)) &&
320 !GdiIsMetaFileDC(dc->hSelf))
322 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
323 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_PEN));
328 /***********************************************************************
329 * GetDCState (Not a Windows API)
331 static HDC GetDCState( HDC hdc )
333 DC * newdc, * dc;
334 HGDIOBJ handle;
336 if (!(dc = get_dc_ptr( hdc ))) return 0;
337 if (!(newdc = HeapAlloc( GetProcessHeap(), 0, sizeof(*newdc ))))
339 release_dc_ptr( dc );
340 return 0;
343 newdc->flags = dc->flags | DC_SAVED;
344 newdc->layout = dc->layout;
345 newdc->hPen = dc->hPen;
346 newdc->hBrush = dc->hBrush;
347 newdc->hFont = dc->hFont;
348 newdc->hBitmap = dc->hBitmap;
349 newdc->hDevice = dc->hDevice;
350 newdc->hPalette = dc->hPalette;
351 newdc->ROPmode = dc->ROPmode;
352 newdc->polyFillMode = dc->polyFillMode;
353 newdc->stretchBltMode = dc->stretchBltMode;
354 newdc->relAbsMode = dc->relAbsMode;
355 newdc->backgroundMode = dc->backgroundMode;
356 newdc->backgroundColor = dc->backgroundColor;
357 newdc->textColor = dc->textColor;
358 newdc->dcBrushColor = dc->dcBrushColor;
359 newdc->dcPenColor = dc->dcPenColor;
360 newdc->brushOrgX = dc->brushOrgX;
361 newdc->brushOrgY = dc->brushOrgY;
362 newdc->textAlign = dc->textAlign;
363 newdc->charExtra = dc->charExtra;
364 newdc->breakExtra = dc->breakExtra;
365 newdc->breakRem = dc->breakRem;
366 newdc->MapMode = dc->MapMode;
367 newdc->GraphicsMode = dc->GraphicsMode;
368 newdc->CursPosX = dc->CursPosX;
369 newdc->CursPosY = dc->CursPosY;
370 newdc->ArcDirection = dc->ArcDirection;
371 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
372 newdc->xformWorld2Vport = dc->xformWorld2Vport;
373 newdc->xformVport2World = dc->xformVport2World;
374 newdc->vport2WorldValid = dc->vport2WorldValid;
375 newdc->wndOrgX = dc->wndOrgX;
376 newdc->wndOrgY = dc->wndOrgY;
377 newdc->wndExtX = dc->wndExtX;
378 newdc->wndExtY = dc->wndExtY;
379 newdc->vportOrgX = dc->vportOrgX;
380 newdc->vportOrgY = dc->vportOrgY;
381 newdc->vportExtX = dc->vportExtX;
382 newdc->vportExtY = dc->vportExtY;
383 newdc->BoundsRect = dc->BoundsRect;
384 newdc->gdiFont = dc->gdiFont;
386 newdc->thread = GetCurrentThreadId();
387 newdc->refcount = 1;
388 newdc->saveLevel = 0;
389 newdc->saved_dc = 0;
391 PATH_InitGdiPath( &newdc->path );
393 newdc->pAbortProc = NULL;
394 newdc->hookThunk = NULL;
395 newdc->hookProc = 0;
396 newdc->saved_visrgn = NULL;
398 if (!(newdc->hSelf = alloc_gdi_handle( &newdc->header, dc->header.type, &dc_funcs )))
400 HeapFree( GetProcessHeap(), 0, newdc );
401 release_dc_ptr( dc );
402 return 0;
404 handle = newdc->hSelf;
405 TRACE("(%p): returning %p\n", hdc, handle );
407 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
409 newdc->hVisRgn = 0;
410 newdc->hClipRgn = 0;
411 newdc->hMetaRgn = 0;
412 newdc->hMetaClipRgn = 0;
413 if (dc->hClipRgn)
415 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
416 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
418 if (dc->hMetaRgn)
420 newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
421 CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
423 /* don't bother recomputing hMetaClipRgn, we'll do that in SetDCState */
425 release_dc_ptr( newdc );
426 release_dc_ptr( dc );
427 return handle;
431 /***********************************************************************
432 * SetDCState (Not a Windows API)
434 static void SetDCState( HDC hdc, HDC hdcs )
436 DC *dc, *dcs;
438 if (!(dc = get_dc_ptr( hdc ))) return;
439 if (!(dcs = get_dc_ptr( hdcs )))
441 release_dc_ptr( dc );
442 return;
444 if (!(dcs->flags & DC_SAVED))
446 release_dc_ptr( dc );
447 release_dc_ptr( dcs );
448 return;
450 TRACE("%p %p\n", hdc, hdcs );
452 update_dc( dc );
453 dc->flags = dcs->flags & ~DC_SAVED;
454 dc->layout = dcs->layout;
455 dc->hDevice = dcs->hDevice;
456 dc->ROPmode = dcs->ROPmode;
457 dc->polyFillMode = dcs->polyFillMode;
458 dc->stretchBltMode = dcs->stretchBltMode;
459 dc->relAbsMode = dcs->relAbsMode;
460 dc->backgroundMode = dcs->backgroundMode;
461 dc->backgroundColor = dcs->backgroundColor;
462 dc->textColor = dcs->textColor;
463 dc->dcBrushColor = dcs->dcBrushColor;
464 dc->dcPenColor = dcs->dcPenColor;
465 dc->brushOrgX = dcs->brushOrgX;
466 dc->brushOrgY = dcs->brushOrgY;
467 dc->textAlign = dcs->textAlign;
468 dc->charExtra = dcs->charExtra;
469 dc->breakExtra = dcs->breakExtra;
470 dc->breakRem = dcs->breakRem;
471 dc->MapMode = dcs->MapMode;
472 dc->GraphicsMode = dcs->GraphicsMode;
473 dc->CursPosX = dcs->CursPosX;
474 dc->CursPosY = dcs->CursPosY;
475 dc->ArcDirection = dcs->ArcDirection;
476 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
477 dc->xformWorld2Vport = dcs->xformWorld2Vport;
478 dc->xformVport2World = dcs->xformVport2World;
479 dc->vport2WorldValid = dcs->vport2WorldValid;
480 dc->BoundsRect = dcs->BoundsRect;
482 dc->wndOrgX = dcs->wndOrgX;
483 dc->wndOrgY = dcs->wndOrgY;
484 dc->wndExtX = dcs->wndExtX;
485 dc->wndExtY = dcs->wndExtY;
486 dc->vportOrgX = dcs->vportOrgX;
487 dc->vportOrgY = dcs->vportOrgY;
488 dc->vportExtX = dcs->vportExtX;
489 dc->vportExtY = dcs->vportExtY;
491 if (dcs->hClipRgn)
493 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
494 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
496 else
498 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
499 dc->hClipRgn = 0;
501 if (dcs->hMetaRgn)
503 if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
504 CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
506 else
508 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
509 dc->hMetaRgn = 0;
511 CLIPPING_UpdateGCRegion( dc );
513 SelectObject( hdc, dcs->hBitmap );
514 SelectObject( hdc, dcs->hBrush );
515 SelectObject( hdc, dcs->hFont );
516 SelectObject( hdc, dcs->hPen );
517 SetBkColor( hdc, dcs->backgroundColor);
518 SetTextColor( hdc, dcs->textColor);
519 GDISelectPalette( hdc, dcs->hPalette, FALSE );
520 release_dc_ptr( dc );
521 release_dc_ptr( dcs );
525 /***********************************************************************
526 * GetDCState (GDI.179)
528 HDC16 WINAPI GetDCState16( HDC16 hdc )
530 return HDC_16( GetDCState( HDC_32(hdc) ));
534 /***********************************************************************
535 * SetDCState (GDI.180)
537 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
539 SetDCState( HDC_32(hdc), HDC_32(hdcs) );
543 /***********************************************************************
544 * SaveDC (GDI32.@)
546 INT WINAPI SaveDC( HDC hdc )
548 HDC hdcs;
549 DC * dc, * dcs;
550 INT ret;
552 dc = get_dc_ptr( hdc );
553 if (!dc) return 0;
555 if(dc->funcs->pSaveDC)
557 ret = dc->funcs->pSaveDC( dc->physDev );
558 if(ret)
559 ret = ++dc->saveLevel;
560 release_dc_ptr( dc );
561 return ret;
564 if (!(hdcs = GetDCState( hdc )))
566 release_dc_ptr( dc );
567 return 0;
569 dcs = get_dc_ptr( hdcs );
571 /* Copy path. The reason why path saving / restoring is in SaveDC/
572 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
573 * functions are only in Win16 (which doesn't have paths) and that
574 * SetDCState doesn't allow us to signal an error (which can happen
575 * when copying paths).
577 if (!PATH_AssignGdiPath( &dcs->path, &dc->path ))
579 release_dc_ptr( dc );
580 release_dc_ptr( dcs );
581 DeleteDC( hdcs );
582 return 0;
585 dcs->saved_dc = dc->saved_dc;
586 dc->saved_dc = hdcs;
587 TRACE("(%p): returning %d\n", hdc, dc->saveLevel+1 );
588 ret = ++dc->saveLevel;
589 release_dc_ptr( dcs );
590 release_dc_ptr( dc );
591 return ret;
595 /***********************************************************************
596 * RestoreDC (GDI32.@)
598 BOOL WINAPI RestoreDC( HDC hdc, INT level )
600 DC * dc, * dcs;
601 BOOL success;
603 TRACE("%p %d\n", hdc, level );
604 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
606 if(abs(level) > dc->saveLevel || level == 0)
608 release_dc_ptr( dc );
609 return FALSE;
612 update_dc( dc );
614 if(dc->funcs->pRestoreDC)
616 success = dc->funcs->pRestoreDC( dc->physDev, level );
617 if(level < 0) level = dc->saveLevel + level + 1;
618 if(success)
619 dc->saveLevel = level - 1;
620 release_dc_ptr( dc );
621 return success;
624 if (level < 0) level = dc->saveLevel + level + 1;
625 success=TRUE;
626 while (dc->saveLevel >= level)
628 HDC hdcs = dc->saved_dc;
629 if (!(dcs = get_dc_ptr( hdcs )))
631 success = FALSE;
632 break;
634 dc->saved_dc = dcs->saved_dc;
635 dcs->saved_dc = 0;
636 if (--dc->saveLevel < level)
638 SetDCState( hdc, hdcs );
639 if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
640 /* FIXME: This might not be quite right, since we're
641 * returning FALSE but still destroying the saved DC state */
642 success=FALSE;
644 release_dc_ptr( dcs );
645 DeleteDC( hdcs );
647 release_dc_ptr( dc );
648 return success;
652 /***********************************************************************
653 * CreateDCW (GDI32.@)
655 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
656 const DEVMODEW *initData )
658 HDC hdc;
659 DC * dc;
660 const DC_FUNCTIONS *funcs;
661 WCHAR buf[300];
663 GDI_CheckNotLock();
665 if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
667 if (!driver)
669 ERR( "no device found for %s\n", debugstr_w(device) );
670 return 0;
672 strcpyW(buf, driver);
675 if (!(funcs = DRIVER_load_driver( buf )))
677 ERR( "no driver found for %s\n", debugstr_w(buf) );
678 return 0;
680 if (!(dc = alloc_dc_ptr( funcs, OBJ_DC ))) goto error;
681 hdc = dc->hSelf;
683 dc->hBitmap = GetStockObject( DEFAULT_BITMAP );
684 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error;
686 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
687 debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
689 if (dc->funcs->pCreateDC &&
690 !dc->funcs->pCreateDC( hdc, &dc->physDev, buf, device, output, initData ))
692 WARN("creation aborted by device\n" );
693 goto error;
696 SetRectRgn( dc->hVisRgn, 0, 0,
697 GetDeviceCaps( hdc, DESKTOPHORZRES ), GetDeviceCaps( hdc, DESKTOPVERTRES ) );
699 DC_InitDC( dc );
700 release_dc_ptr( dc );
701 return hdc;
703 error:
704 if (dc && dc->hVisRgn) DeleteObject( dc->hVisRgn );
705 if (dc) free_dc_ptr( dc );
706 DRIVER_release_driver( funcs );
707 return 0;
711 /***********************************************************************
712 * CreateDCA (GDI32.@)
714 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
715 const DEVMODEA *initData )
717 UNICODE_STRING driverW, deviceW, outputW;
718 DEVMODEW *initDataW;
719 HDC ret;
721 if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
722 else driverW.Buffer = NULL;
724 if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
725 else deviceW.Buffer = NULL;
727 if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
728 else outputW.Buffer = NULL;
730 initDataW = NULL;
731 if (initData)
733 /* don't convert initData for DISPLAY driver, it's not used */
734 if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
735 initDataW = GdiConvertToDevmodeW(initData);
738 ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
740 RtlFreeUnicodeString(&driverW);
741 RtlFreeUnicodeString(&deviceW);
742 RtlFreeUnicodeString(&outputW);
743 HeapFree(GetProcessHeap(), 0, initDataW);
744 return ret;
748 /***********************************************************************
749 * CreateICA (GDI32.@)
751 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
752 const DEVMODEA* initData )
754 /* Nothing special yet for ICs */
755 return CreateDCA( driver, device, output, initData );
759 /***********************************************************************
760 * CreateICW (GDI32.@)
762 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
763 const DEVMODEW* initData )
765 /* Nothing special yet for ICs */
766 return CreateDCW( driver, device, output, initData );
770 /***********************************************************************
771 * CreateCompatibleDC (GDI32.@)
773 HDC WINAPI CreateCompatibleDC( HDC hdc )
775 DC *dc, *origDC;
776 HDC ret;
777 const DC_FUNCTIONS *funcs = NULL;
778 PHYSDEV physDev = NULL;
780 GDI_CheckNotLock();
782 if ((origDC = get_dc_ptr( hdc )))
784 if (GetObjectType( hdc ) == OBJ_DC)
786 funcs = origDC->funcs;
787 physDev = origDC->physDev;
789 release_dc_ptr( origDC );
790 if (funcs) funcs = DRIVER_get_driver( funcs );
792 else if (hdc) return 0;
794 if (!funcs && !(funcs = DRIVER_load_driver( displayW ))) return 0;
796 if (!(dc = alloc_dc_ptr( funcs, OBJ_MEMDC ))) goto error;
798 TRACE("(%p): returning %p\n", hdc, dc->hSelf );
800 dc->hBitmap = GetStockObject( DEFAULT_BITMAP );
801 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error; /* default bitmap is 1x1 */
803 /* Copy the driver-specific physical device info into
804 * the new DC. The driver may use this read-only info
805 * while creating the compatible DC below. */
806 dc->physDev = physDev;
807 ret = dc->hSelf;
809 if (dc->funcs->pCreateDC &&
810 !dc->funcs->pCreateDC( dc->hSelf, &dc->physDev, NULL, NULL, NULL, NULL ))
812 WARN("creation aborted by device\n");
813 goto error;
816 DC_InitDC( dc );
817 release_dc_ptr( dc );
818 return ret;
820 error:
821 if (dc && dc->hVisRgn) DeleteObject( dc->hVisRgn );
822 if (dc) free_dc_ptr( dc );
823 DRIVER_release_driver( funcs );
824 return 0;
828 /***********************************************************************
829 * DeleteDC (GDI32.@)
831 BOOL WINAPI DeleteDC( HDC hdc )
833 const DC_FUNCTIONS *funcs = NULL;
834 DC * dc;
836 TRACE("%p\n", hdc );
838 GDI_CheckNotLock();
840 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
841 if (dc->refcount != 1)
843 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
844 release_dc_ptr( dc );
845 return FALSE;
848 /* Call hook procedure to check whether is it OK to delete this DC */
849 if (dc->hookThunk && !dc->hookThunk( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ))
851 release_dc_ptr( dc );
852 return FALSE;
855 while (dc->saveLevel)
857 DC * dcs;
858 HDC hdcs = dc->saved_dc;
859 if (!(dcs = get_dc_ptr( hdcs ))) break;
860 dc->saved_dc = dcs->saved_dc;
861 dc->saveLevel--;
862 if (dcs->hClipRgn) DeleteObject( dcs->hClipRgn );
863 if (dcs->hMetaRgn) DeleteObject( dcs->hMetaRgn );
864 if (dcs->hMetaClipRgn) DeleteObject( dcs->hMetaClipRgn );
865 if (dcs->hVisRgn) DeleteObject( dcs->hVisRgn );
866 PATH_DestroyGdiPath(&dcs->path);
867 free_dc_ptr( dcs );
870 if (!(dc->flags & DC_SAVED))
872 SelectObject( hdc, GetStockObject(BLACK_PEN) );
873 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
874 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
875 SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
876 funcs = dc->funcs;
877 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc->physDev);
878 dc->physDev = NULL;
881 while (dc->saved_visrgn)
883 struct saved_visrgn *next = dc->saved_visrgn->next;
884 DeleteObject( dc->saved_visrgn->hrgn );
885 HeapFree( GetProcessHeap(), 0, dc->saved_visrgn );
886 dc->saved_visrgn = next;
888 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
889 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
890 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
891 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
892 PATH_DestroyGdiPath(&dc->path);
894 free_dc_ptr( dc );
895 if (funcs) DRIVER_release_driver( funcs ); /* do that after releasing the GDI lock */
896 return TRUE;
900 /***********************************************************************
901 * ResetDCW (GDI32.@)
903 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
905 DC *dc;
906 HDC ret = hdc;
908 if ((dc = get_dc_ptr( hdc )))
910 if (dc->funcs->pResetDC) ret = dc->funcs->pResetDC( dc->physDev, devmode );
911 release_dc_ptr( dc );
913 return ret;
917 /***********************************************************************
918 * ResetDCA (GDI32.@)
920 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
922 DEVMODEW *devmodeW;
923 HDC ret;
925 if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
926 else devmodeW = NULL;
928 ret = ResetDCW(hdc, devmodeW);
930 HeapFree(GetProcessHeap(), 0, devmodeW);
931 return ret;
935 /***********************************************************************
936 * GetDeviceCaps (GDI32.@)
938 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
940 DC *dc;
941 INT ret = 0;
943 if ((dc = get_dc_ptr( hdc )))
945 if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc->physDev, cap );
946 else switch(cap) /* return meaningful values for some entries */
948 case HORZRES: ret = 640; break;
949 case VERTRES: ret = 480; break;
950 case BITSPIXEL: ret = 1; break;
951 case PLANES: ret = 1; break;
952 case NUMCOLORS: ret = 2; break;
953 case ASPECTX: ret = 36; break;
954 case ASPECTY: ret = 36; break;
955 case ASPECTXY: ret = 51; break;
956 case LOGPIXELSX: ret = 72; break;
957 case LOGPIXELSY: ret = 72; break;
958 case SIZEPALETTE: ret = 2; break;
960 release_dc_ptr( dc );
962 return ret;
966 /***********************************************************************
967 * GetBkColor (GDI32.@)
969 COLORREF WINAPI GetBkColor( HDC hdc )
971 COLORREF ret = 0;
972 DC * dc = get_dc_ptr( hdc );
973 if (dc)
975 ret = dc->backgroundColor;
976 release_dc_ptr( dc );
978 return ret;
982 /***********************************************************************
983 * SetBkColor (GDI32.@)
985 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
987 COLORREF oldColor;
988 DC * dc = get_dc_ptr( hdc );
990 TRACE("hdc=%p color=0x%08x\n", hdc, color);
992 if (!dc) return CLR_INVALID;
993 oldColor = dc->backgroundColor;
994 if (dc->funcs->pSetBkColor)
996 color = dc->funcs->pSetBkColor(dc->physDev, color);
997 if (color == CLR_INVALID) /* don't change it */
999 color = oldColor;
1000 oldColor = CLR_INVALID;
1003 dc->backgroundColor = color;
1004 release_dc_ptr( dc );
1005 return oldColor;
1009 /***********************************************************************
1010 * GetTextColor (GDI32.@)
1012 COLORREF WINAPI GetTextColor( HDC hdc )
1014 COLORREF ret = 0;
1015 DC * dc = get_dc_ptr( hdc );
1016 if (dc)
1018 ret = dc->textColor;
1019 release_dc_ptr( dc );
1021 return ret;
1025 /***********************************************************************
1026 * SetTextColor (GDI32.@)
1028 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
1030 COLORREF oldColor;
1031 DC * dc = get_dc_ptr( hdc );
1033 TRACE(" hdc=%p color=0x%08x\n", hdc, color);
1035 if (!dc) return CLR_INVALID;
1036 oldColor = dc->textColor;
1037 if (dc->funcs->pSetTextColor)
1039 color = dc->funcs->pSetTextColor(dc->physDev, color);
1040 if (color == CLR_INVALID) /* don't change it */
1042 color = oldColor;
1043 oldColor = CLR_INVALID;
1046 dc->textColor = color;
1047 release_dc_ptr( dc );
1048 return oldColor;
1052 /***********************************************************************
1053 * GetTextAlign (GDI32.@)
1055 UINT WINAPI GetTextAlign( HDC hdc )
1057 UINT ret = 0;
1058 DC * dc = get_dc_ptr( hdc );
1059 if (dc)
1061 ret = dc->textAlign;
1062 release_dc_ptr( dc );
1064 return ret;
1068 /***********************************************************************
1069 * SetTextAlign (GDI32.@)
1071 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
1073 UINT ret;
1074 DC *dc = get_dc_ptr( hdc );
1076 TRACE("hdc=%p align=%d\n", hdc, align);
1078 if (!dc) return 0x0;
1079 ret = dc->textAlign;
1080 if (dc->funcs->pSetTextAlign)
1081 if (!dc->funcs->pSetTextAlign(dc->physDev, align))
1082 ret = GDI_ERROR;
1083 if (ret != GDI_ERROR)
1084 dc->textAlign = align;
1085 release_dc_ptr( dc );
1086 return ret;
1089 /***********************************************************************
1090 * GetDCOrgEx (GDI32.@)
1092 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1094 DC * dc;
1096 if (!lpp) return FALSE;
1097 if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1099 lpp->x = lpp->y = 0;
1100 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc->physDev, lpp );
1101 release_dc_ptr( dc );
1102 return TRUE;
1106 /***********************************************************************
1107 * SetDCOrg (GDI.117)
1109 DWORD WINAPI SetDCOrg16( HDC16 hdc16, INT16 x, INT16 y )
1111 DWORD prevOrg = 0;
1112 HDC hdc = HDC_32( hdc16 );
1113 DC *dc = get_dc_ptr( hdc );
1114 if (!dc) return 0;
1115 if (dc->funcs->pSetDCOrg) prevOrg = dc->funcs->pSetDCOrg( dc->physDev, x, y );
1116 release_dc_ptr( dc );
1117 return prevOrg;
1121 /***********************************************************************
1122 * GetGraphicsMode (GDI32.@)
1124 INT WINAPI GetGraphicsMode( HDC hdc )
1126 INT ret = 0;
1127 DC * dc = get_dc_ptr( hdc );
1128 if (dc)
1130 ret = dc->GraphicsMode;
1131 release_dc_ptr( dc );
1133 return ret;
1137 /***********************************************************************
1138 * SetGraphicsMode (GDI32.@)
1140 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1142 INT ret = 0;
1143 DC *dc = get_dc_ptr( hdc );
1145 /* One would think that setting the graphics mode to GM_COMPATIBLE
1146 * would also reset the world transformation matrix to the unity
1147 * matrix. However, in Windows, this is not the case. This doesn't
1148 * make a lot of sense to me, but that's the way it is.
1150 if (!dc) return 0;
1151 if ((mode > 0) && (mode <= GM_LAST))
1153 ret = dc->GraphicsMode;
1154 dc->GraphicsMode = mode;
1156 release_dc_ptr( dc );
1157 return ret;
1161 /***********************************************************************
1162 * GetArcDirection (GDI32.@)
1164 INT WINAPI GetArcDirection( HDC hdc )
1166 INT ret = 0;
1167 DC * dc = get_dc_ptr( hdc );
1168 if (dc)
1170 ret = dc->ArcDirection;
1171 release_dc_ptr( dc );
1173 return ret;
1177 /***********************************************************************
1178 * SetArcDirection (GDI32.@)
1180 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1182 DC * dc;
1183 INT nOldDirection = 0;
1185 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1187 SetLastError(ERROR_INVALID_PARAMETER);
1188 return 0;
1191 if ((dc = get_dc_ptr( hdc )))
1193 if (dc->funcs->pSetArcDirection)
1195 dc->funcs->pSetArcDirection(dc->physDev, nDirection);
1197 nOldDirection = dc->ArcDirection;
1198 dc->ArcDirection = nDirection;
1199 release_dc_ptr( dc );
1201 return nOldDirection;
1205 /***********************************************************************
1206 * GetWorldTransform (GDI32.@)
1208 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1210 DC * dc;
1211 if (!xform) return FALSE;
1212 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1213 *xform = dc->xformWorld2Wnd;
1214 release_dc_ptr( dc );
1215 return TRUE;
1219 /***********************************************************************
1220 * GetTransform (GDI32.@)
1222 BOOL WINAPI GetTransform( HDC hdc, DWORD unknown, LPXFORM xform )
1224 if (unknown == 0x0203) return GetWorldTransform( hdc, xform );
1225 FIXME("stub: don't know what to do for code %x\n", unknown );
1226 return FALSE;
1230 /***********************************************************************
1231 * SetWorldTransform (GDI32.@)
1233 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1235 BOOL ret = FALSE;
1236 DC *dc = get_dc_ptr( hdc );
1238 if (!dc) return FALSE;
1239 if (!xform) goto done;
1241 /* Check that graphics mode is GM_ADVANCED */
1242 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1244 TRACE("eM11 %f eM12 %f eM21 %f eM22 %f eDx %f eDy %f\n",
1245 xform->eM11, xform->eM12, xform->eM21, xform->eM22, xform->eDx, xform->eDy);
1247 if (dc->funcs->pSetWorldTransform)
1249 ret = dc->funcs->pSetWorldTransform(dc->physDev, xform);
1250 if (!ret) goto done;
1253 dc->xformWorld2Wnd = *xform;
1254 DC_UpdateXforms( dc );
1255 ret = TRUE;
1256 done:
1257 release_dc_ptr( dc );
1258 return ret;
1262 /****************************************************************************
1263 * ModifyWorldTransform [GDI32.@]
1264 * Modifies the world transformation for a device context.
1266 * PARAMS
1267 * hdc [I] Handle to device context
1268 * xform [I] XFORM structure that will be used to modify the world
1269 * transformation
1270 * iMode [I] Specifies in what way to modify the world transformation
1271 * Possible values:
1272 * MWT_IDENTITY
1273 * Resets the world transformation to the identity matrix.
1274 * The parameter xform is ignored.
1275 * MWT_LEFTMULTIPLY
1276 * Multiplies xform into the world transformation matrix from
1277 * the left.
1278 * MWT_RIGHTMULTIPLY
1279 * Multiplies xform into the world transformation matrix from
1280 * the right.
1282 * RETURNS
1283 * Success: TRUE.
1284 * Failure: FALSE. Use GetLastError() to determine the cause.
1286 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1287 DWORD iMode )
1289 BOOL ret = FALSE;
1290 DC *dc = get_dc_ptr( hdc );
1292 /* Check for illegal parameters */
1293 if (!dc) return FALSE;
1294 if (!xform && iMode != MWT_IDENTITY) goto done;
1296 /* Check that graphics mode is GM_ADVANCED */
1297 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1299 if (dc->funcs->pModifyWorldTransform)
1301 ret = dc->funcs->pModifyWorldTransform(dc->physDev, xform, iMode);
1302 if (!ret) goto done;
1305 switch (iMode)
1307 case MWT_IDENTITY:
1308 dc->xformWorld2Wnd.eM11 = 1.0f;
1309 dc->xformWorld2Wnd.eM12 = 0.0f;
1310 dc->xformWorld2Wnd.eM21 = 0.0f;
1311 dc->xformWorld2Wnd.eM22 = 1.0f;
1312 dc->xformWorld2Wnd.eDx = 0.0f;
1313 dc->xformWorld2Wnd.eDy = 0.0f;
1314 break;
1315 case MWT_LEFTMULTIPLY:
1316 CombineTransform( &dc->xformWorld2Wnd, xform,
1317 &dc->xformWorld2Wnd );
1318 break;
1319 case MWT_RIGHTMULTIPLY:
1320 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1321 xform );
1322 break;
1323 default:
1324 goto done;
1327 DC_UpdateXforms( dc );
1328 ret = TRUE;
1329 done:
1330 release_dc_ptr( dc );
1331 return ret;
1335 /****************************************************************************
1336 * CombineTransform [GDI32.@]
1337 * Combines two transformation matrices.
1339 * PARAMS
1340 * xformResult [O] Stores the result of combining the two matrices
1341 * xform1 [I] Specifies the first matrix to apply
1342 * xform2 [I] Specifies the second matrix to apply
1344 * REMARKS
1345 * The same matrix can be passed in for more than one of the parameters.
1347 * RETURNS
1348 * Success: TRUE.
1349 * Failure: FALSE. Use GetLastError() to determine the cause.
1351 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1352 const XFORM *xform2 )
1354 XFORM xformTemp;
1356 /* Check for illegal parameters */
1357 if (!xformResult || !xform1 || !xform2)
1358 return FALSE;
1360 /* Create the result in a temporary XFORM, since xformResult may be
1361 * equal to xform1 or xform2 */
1362 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1363 xform1->eM12 * xform2->eM21;
1364 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1365 xform1->eM12 * xform2->eM22;
1366 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1367 xform1->eM22 * xform2->eM21;
1368 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1369 xform1->eM22 * xform2->eM22;
1370 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1371 xform1->eDy * xform2->eM21 +
1372 xform2->eDx;
1373 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1374 xform1->eDy * xform2->eM22 +
1375 xform2->eDy;
1377 /* Copy the result to xformResult */
1378 *xformResult = xformTemp;
1380 return TRUE;
1384 /***********************************************************************
1385 * SetDCHook (GDI32.@)
1387 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1389 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1391 DC *dc = get_dc_ptr( hdc );
1393 if (!dc) return FALSE;
1395 if (!(dc->flags & DC_SAVED))
1397 dc->dwHookData = dwHookData;
1398 dc->hookThunk = hookProc;
1400 release_dc_ptr( dc );
1401 return TRUE;
1405 /***********************************************************************
1406 * GetDCHook (GDI32.@)
1408 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1410 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1412 DC *dc = get_dc_ptr( hdc );
1413 DWORD_PTR ret;
1415 if (!dc) return 0;
1416 if (proc) *proc = dc->hookThunk;
1417 ret = dc->dwHookData;
1418 release_dc_ptr( dc );
1419 return ret;
1423 /* relay function to call the 16-bit DC hook proc */
1424 static BOOL WINAPI call_dc_hook16( HDC hdc, WORD code, DWORD_PTR data, LPARAM lParam )
1426 WORD args[6];
1427 DWORD ret = 0;
1428 DC *dc = get_dc_ptr( hdc );
1430 if (!dc) return FALSE;
1431 if (dc->hookProc)
1433 args[5] = HDC_16(hdc);
1434 args[4] = code;
1435 args[3] = HIWORD(data);
1436 args[2] = LOWORD(data);
1437 args[1] = HIWORD(lParam);
1438 args[0] = LOWORD(lParam);
1439 WOWCallback16Ex( (DWORD)dc->hookProc, WCB16_PASCAL, sizeof(args), args, &ret );
1441 release_dc_ptr( dc );
1442 return LOWORD(ret);
1445 /***********************************************************************
1446 * SetDCHook (GDI.190)
1448 BOOL16 WINAPI SetDCHook16( HDC16 hdc16, FARPROC16 hookProc, DWORD dwHookData )
1450 DC *dc = get_dc_ptr( HDC_32(hdc16) );
1452 if (!dc) return FALSE;
1453 if (!(dc->flags & DC_SAVED))
1455 dc->dwHookData = dwHookData;
1456 dc->hookThunk = call_dc_hook16;
1457 dc->hookProc = hookProc;
1459 release_dc_ptr( dc );
1460 return TRUE;
1464 /***********************************************************************
1465 * GetDCHook (GDI.191)
1467 DWORD WINAPI GetDCHook16( HDC16 hdc16, FARPROC16 *phookProc )
1469 HDC hdc = HDC_32( hdc16 );
1470 DC *dc = get_dc_ptr( hdc );
1471 DWORD ret;
1473 if (!dc) return 0;
1474 *phookProc = dc->hookProc;
1475 ret = dc->dwHookData;
1476 release_dc_ptr( dc );
1477 return ret;
1481 /***********************************************************************
1482 * SetHookFlags (GDI32.@)
1484 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1486 WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
1488 DC *dc = get_dc_obj( hdc ); /* not get_dc_ptr, this needs to work from any thread */
1489 LONG ret = 0;
1491 if (!dc) return 0;
1493 /* "Undocumented Windows" info is slightly confusing. */
1495 TRACE("hDC %p, flags %04x\n",hdc,flags);
1497 if (flags & DCHF_INVALIDATEVISRGN)
1498 ret = InterlockedExchange( &dc->dirty, 1 );
1499 else if (flags & DCHF_VALIDATEVISRGN || !flags)
1500 ret = InterlockedExchange( &dc->dirty, 0 );
1502 GDI_ReleaseObj( dc );
1503 return ret;
1506 /***********************************************************************
1507 * SetICMMode (GDI32.@)
1509 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1511 /*FIXME: Assume that ICM is always off, and cannot be turned on */
1512 if (iEnableICM == ICM_OFF) return ICM_OFF;
1513 if (iEnableICM == ICM_ON) return 0;
1514 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1515 return 0;
1518 /***********************************************************************
1519 * GetDeviceGammaRamp (GDI32.@)
1521 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1523 BOOL ret = FALSE;
1524 DC *dc = get_dc_ptr( hDC );
1526 if( dc )
1528 if (dc->funcs->pGetDeviceGammaRamp)
1529 ret = dc->funcs->pGetDeviceGammaRamp(dc->physDev, ptr);
1530 release_dc_ptr( dc );
1532 return ret;
1535 /***********************************************************************
1536 * SetDeviceGammaRamp (GDI32.@)
1538 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1540 BOOL ret = FALSE;
1541 DC *dc = get_dc_ptr( hDC );
1543 if( dc )
1545 if (dc->funcs->pSetDeviceGammaRamp)
1546 ret = dc->funcs->pSetDeviceGammaRamp(dc->physDev, ptr);
1547 release_dc_ptr( dc );
1549 return ret;
1552 /***********************************************************************
1553 * GetColorSpace (GDI32.@)
1555 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1557 /*FIXME Need to to whatever GetColorSpace actually does */
1558 return 0;
1561 /***********************************************************************
1562 * CreateColorSpaceA (GDI32.@)
1564 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1566 FIXME( "stub\n" );
1567 return 0;
1570 /***********************************************************************
1571 * CreateColorSpaceW (GDI32.@)
1573 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1575 FIXME( "stub\n" );
1576 return 0;
1579 /***********************************************************************
1580 * DeleteColorSpace (GDI32.@)
1582 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1584 FIXME( "stub\n" );
1586 return TRUE;
1589 /***********************************************************************
1590 * SetColorSpace (GDI32.@)
1592 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1594 FIXME( "stub\n" );
1596 return hColorSpace;
1599 /***********************************************************************
1600 * GetBoundsRect (GDI32.@)
1602 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1604 UINT ret;
1605 DC *dc = get_dc_ptr( hdc );
1607 if ( !dc ) return 0;
1609 if (rect) *rect = dc->BoundsRect;
1611 ret = ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1613 if (flags & DCB_RESET)
1615 dc->BoundsRect.left = 0;
1616 dc->BoundsRect.top = 0;
1617 dc->BoundsRect.right = 0;
1618 dc->BoundsRect.bottom = 0;
1619 dc->flags &= ~DC_BOUNDS_SET;
1621 release_dc_ptr( dc );
1622 return ret;
1626 /***********************************************************************
1627 * SetBoundsRect (GDI32.@)
1629 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1631 UINT ret;
1632 DC *dc;
1634 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1635 if (!(dc = get_dc_ptr( hdc ))) return 0;
1637 ret = ((dc->flags & DC_BOUNDS_ENABLE) ? DCB_ENABLE : DCB_DISABLE) |
1638 ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1640 if (flags & DCB_RESET)
1642 dc->BoundsRect.left = 0;
1643 dc->BoundsRect.top = 0;
1644 dc->BoundsRect.right = 0;
1645 dc->BoundsRect.bottom = 0;
1646 dc->flags &= ~DC_BOUNDS_SET;
1649 if ((flags & DCB_ACCUMULATE) && rect && rect->left < rect->right && rect->top < rect->bottom)
1651 if (dc->flags & DC_BOUNDS_SET)
1653 dc->BoundsRect.left = min( dc->BoundsRect.left, rect->left );
1654 dc->BoundsRect.top = min( dc->BoundsRect.top, rect->top );
1655 dc->BoundsRect.right = max( dc->BoundsRect.right, rect->right );
1656 dc->BoundsRect.bottom = max( dc->BoundsRect.bottom, rect->bottom );
1658 else
1660 dc->BoundsRect = *rect;
1661 dc->flags |= DC_BOUNDS_SET;
1665 if (flags & DCB_ENABLE) dc->flags |= DC_BOUNDS_ENABLE;
1666 if (flags & DCB_DISABLE) dc->flags &= ~DC_BOUNDS_ENABLE;
1668 release_dc_ptr( dc );
1669 return ret;
1673 /***********************************************************************
1674 * GetRelAbs (GDI32.@)
1676 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1678 INT ret = 0;
1679 DC *dc = get_dc_ptr( hdc );
1680 if (dc)
1682 ret = dc->relAbsMode;
1683 release_dc_ptr( dc );
1685 return ret;
1691 /***********************************************************************
1692 * GetBkMode (GDI32.@)
1694 INT WINAPI GetBkMode( HDC hdc )
1696 INT ret = 0;
1697 DC * dc = get_dc_ptr( hdc );
1698 if (dc)
1700 ret = dc->backgroundMode;
1701 release_dc_ptr( dc );
1703 return ret;
1707 /***********************************************************************
1708 * SetBkMode (GDI32.@)
1710 INT WINAPI SetBkMode( HDC hdc, INT mode )
1712 INT ret;
1713 DC *dc;
1714 if ((mode <= 0) || (mode > BKMODE_LAST))
1716 SetLastError(ERROR_INVALID_PARAMETER);
1717 return 0;
1719 if (!(dc = get_dc_ptr( hdc ))) return 0;
1721 ret = dc->backgroundMode;
1722 if (dc->funcs->pSetBkMode)
1723 if (!dc->funcs->pSetBkMode( dc->physDev, mode ))
1724 ret = 0;
1725 if (ret)
1726 dc->backgroundMode = mode;
1727 release_dc_ptr( dc );
1728 return ret;
1732 /***********************************************************************
1733 * GetROP2 (GDI32.@)
1735 INT WINAPI GetROP2( HDC hdc )
1737 INT ret = 0;
1738 DC * dc = get_dc_ptr( hdc );
1739 if (dc)
1741 ret = dc->ROPmode;
1742 release_dc_ptr( dc );
1744 return ret;
1748 /***********************************************************************
1749 * SetROP2 (GDI32.@)
1751 INT WINAPI SetROP2( HDC hdc, INT mode )
1753 INT ret;
1754 DC *dc;
1755 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1757 SetLastError(ERROR_INVALID_PARAMETER);
1758 return 0;
1760 if (!(dc = get_dc_ptr( hdc ))) return 0;
1761 ret = dc->ROPmode;
1762 if (dc->funcs->pSetROP2)
1763 if (!dc->funcs->pSetROP2( dc->physDev, mode ))
1764 ret = 0;
1765 if (ret)
1766 dc->ROPmode = mode;
1767 release_dc_ptr( dc );
1768 return ret;
1772 /***********************************************************************
1773 * SetRelAbs (GDI32.@)
1775 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1777 INT ret;
1778 DC *dc;
1779 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1781 SetLastError(ERROR_INVALID_PARAMETER);
1782 return 0;
1784 if (!(dc = get_dc_ptr( hdc ))) return 0;
1785 if (dc->funcs->pSetRelAbs)
1786 ret = dc->funcs->pSetRelAbs( dc->physDev, mode );
1787 else
1789 ret = dc->relAbsMode;
1790 dc->relAbsMode = mode;
1792 release_dc_ptr( dc );
1793 return ret;
1797 /***********************************************************************
1798 * GetPolyFillMode (GDI32.@)
1800 INT WINAPI GetPolyFillMode( HDC hdc )
1802 INT ret = 0;
1803 DC * dc = get_dc_ptr( hdc );
1804 if (dc)
1806 ret = dc->polyFillMode;
1807 release_dc_ptr( dc );
1809 return ret;
1813 /***********************************************************************
1814 * SetPolyFillMode (GDI32.@)
1816 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1818 INT ret;
1819 DC *dc;
1820 if ((mode <= 0) || (mode > POLYFILL_LAST))
1822 SetLastError(ERROR_INVALID_PARAMETER);
1823 return 0;
1825 if (!(dc = get_dc_ptr( hdc ))) return 0;
1826 ret = dc->polyFillMode;
1827 if (dc->funcs->pSetPolyFillMode)
1828 if (!dc->funcs->pSetPolyFillMode( dc->physDev, mode ))
1829 ret = 0;
1830 if (ret)
1831 dc->polyFillMode = mode;
1832 release_dc_ptr( dc );
1833 return ret;
1837 /***********************************************************************
1838 * GetStretchBltMode (GDI32.@)
1840 INT WINAPI GetStretchBltMode( HDC hdc )
1842 INT ret = 0;
1843 DC * dc = get_dc_ptr( hdc );
1844 if (dc)
1846 ret = dc->stretchBltMode;
1847 release_dc_ptr( dc );
1849 return ret;
1853 /***********************************************************************
1854 * SetStretchBltMode (GDI32.@)
1856 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1858 INT ret;
1859 DC *dc;
1860 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1862 SetLastError(ERROR_INVALID_PARAMETER);
1863 return 0;
1865 if (!(dc = get_dc_ptr( hdc ))) return 0;
1866 ret = dc->stretchBltMode;
1867 if (dc->funcs->pSetStretchBltMode)
1868 if (!dc->funcs->pSetStretchBltMode( dc->physDev, mode ))
1869 ret = 0;
1870 if (ret)
1871 dc->stretchBltMode = mode;
1872 release_dc_ptr( dc );
1873 return ret;
1877 /***********************************************************************
1878 * GetMapMode (GDI32.@)
1880 INT WINAPI GetMapMode( HDC hdc )
1882 INT ret = 0;
1883 DC * dc = get_dc_ptr( hdc );
1884 if (dc)
1886 ret = dc->MapMode;
1887 release_dc_ptr( dc );
1889 return ret;
1893 /***********************************************************************
1894 * GetBrushOrgEx (GDI32.@)
1896 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1898 DC * dc = get_dc_ptr( hdc );
1899 if (!dc) return FALSE;
1900 pt->x = dc->brushOrgX;
1901 pt->y = dc->brushOrgY;
1902 release_dc_ptr( dc );
1903 return TRUE;
1907 /***********************************************************************
1908 * GetCurrentPositionEx (GDI32.@)
1910 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1912 DC * dc = get_dc_ptr( hdc );
1913 if (!dc) return FALSE;
1914 pt->x = dc->CursPosX;
1915 pt->y = dc->CursPosY;
1916 release_dc_ptr( dc );
1917 return TRUE;
1921 /***********************************************************************
1922 * GetViewportExtEx (GDI32.@)
1924 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1926 DC * dc = get_dc_ptr( hdc );
1927 if (!dc) return FALSE;
1928 size->cx = dc->vportExtX;
1929 size->cy = dc->vportExtY;
1930 release_dc_ptr( dc );
1931 return TRUE;
1935 /***********************************************************************
1936 * GetViewportOrgEx (GDI32.@)
1938 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1940 DC * dc = get_dc_ptr( hdc );
1941 if (!dc) return FALSE;
1942 pt->x = dc->vportOrgX;
1943 pt->y = dc->vportOrgY;
1944 release_dc_ptr( dc );
1945 return TRUE;
1949 /***********************************************************************
1950 * GetWindowExtEx (GDI32.@)
1952 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1954 DC * dc = get_dc_ptr( hdc );
1955 if (!dc) return FALSE;
1956 size->cx = dc->wndExtX;
1957 size->cy = dc->wndExtY;
1958 release_dc_ptr( dc );
1959 return TRUE;
1963 /***********************************************************************
1964 * GetWindowOrgEx (GDI32.@)
1966 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1968 DC * dc = get_dc_ptr( hdc );
1969 if (!dc) return FALSE;
1970 pt->x = dc->wndOrgX;
1971 pt->y = dc->wndOrgY;
1972 release_dc_ptr( dc );
1973 return TRUE;
1977 /***********************************************************************
1978 * InquireVisRgn (GDI.131)
1980 HRGN16 WINAPI InquireVisRgn16( HDC16 hdc )
1982 HRGN16 ret = 0;
1983 DC * dc = get_dc_ptr( HDC_32(hdc) );
1984 if (dc)
1986 ret = HRGN_16(dc->hVisRgn);
1987 release_dc_ptr( dc );
1989 return ret;
1993 /***********************************************************************
1994 * GetClipRgn (GDI.173)
1996 HRGN16 WINAPI GetClipRgn16( HDC16 hdc )
1998 HRGN16 ret = 0;
1999 DC * dc = get_dc_ptr( HDC_32(hdc) );
2000 if (dc)
2002 ret = HRGN_16(dc->hClipRgn);
2003 release_dc_ptr( dc );
2005 return ret;
2009 /***********************************************************************
2010 * GetLayout (GDI32.@)
2012 * Gets left->right or right->left text layout flags of a dc.
2015 DWORD WINAPI GetLayout(HDC hdc)
2017 DWORD layout = GDI_ERROR;
2019 DC * dc = get_dc_ptr( hdc );
2020 if (dc)
2022 layout = dc->layout;
2023 release_dc_ptr( dc );
2026 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
2028 return layout;
2031 /***********************************************************************
2032 * SetLayout (GDI32.@)
2034 * Sets left->right or right->left text layout flags of a dc.
2037 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
2039 DWORD oldlayout = GDI_ERROR;
2041 DC * dc = get_dc_ptr( hdc );
2042 if (dc)
2044 oldlayout = dc->layout;
2045 dc->layout = layout;
2046 release_dc_ptr( dc );
2049 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
2051 return oldlayout;
2054 /***********************************************************************
2055 * GetDCBrushColor (GDI32.@)
2057 * Retrieves the current brush color for the specified device
2058 * context (DC).
2061 COLORREF WINAPI GetDCBrushColor(HDC hdc)
2063 DC *dc;
2064 COLORREF dcBrushColor = CLR_INVALID;
2066 TRACE("hdc(%p)\n", hdc);
2068 dc = get_dc_ptr( hdc );
2069 if (dc)
2071 dcBrushColor = dc->dcBrushColor;
2072 release_dc_ptr( dc );
2075 return dcBrushColor;
2078 /***********************************************************************
2079 * SetDCBrushColor (GDI32.@)
2081 * Sets the current device context (DC) brush color to the specified
2082 * color value. If the device cannot represent the specified color
2083 * value, the color is set to the nearest physical color.
2086 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
2088 DC *dc;
2089 COLORREF oldClr = CLR_INVALID;
2091 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2093 dc = get_dc_ptr( hdc );
2094 if (dc)
2096 if (dc->funcs->pSetDCBrushColor)
2097 crColor = dc->funcs->pSetDCBrushColor( dc->physDev, crColor );
2098 else if (dc->hBrush == GetStockObject( DC_BRUSH ))
2100 /* If DC_BRUSH is selected, update driver pen color */
2101 HBRUSH hBrush = CreateSolidBrush( crColor );
2102 dc->funcs->pSelectBrush( dc->physDev, hBrush );
2103 DeleteObject( hBrush );
2106 if (crColor != CLR_INVALID)
2108 oldClr = dc->dcBrushColor;
2109 dc->dcBrushColor = crColor;
2112 release_dc_ptr( dc );
2115 return oldClr;
2118 /***********************************************************************
2119 * GetDCPenColor (GDI32.@)
2121 * Retrieves the current pen color for the specified device
2122 * context (DC).
2125 COLORREF WINAPI GetDCPenColor(HDC hdc)
2127 DC *dc;
2128 COLORREF dcPenColor = CLR_INVALID;
2130 TRACE("hdc(%p)\n", hdc);
2132 dc = get_dc_ptr( hdc );
2133 if (dc)
2135 dcPenColor = dc->dcPenColor;
2136 release_dc_ptr( dc );
2139 return dcPenColor;
2142 /***********************************************************************
2143 * SetDCPenColor (GDI32.@)
2145 * Sets the current device context (DC) pen color to the specified
2146 * color value. If the device cannot represent the specified color
2147 * value, the color is set to the nearest physical color.
2150 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
2152 DC *dc;
2153 COLORREF oldClr = CLR_INVALID;
2155 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2157 dc = get_dc_ptr( hdc );
2158 if (dc)
2160 if (dc->funcs->pSetDCPenColor)
2161 crColor = dc->funcs->pSetDCPenColor( dc->physDev, crColor );
2162 else if (dc->hPen == GetStockObject( DC_PEN ))
2164 /* If DC_PEN is selected, update the driver pen color */
2165 LOGPEN logpen = { PS_SOLID, { 0, 0 }, crColor };
2166 HPEN hPen = CreatePenIndirect( &logpen );
2167 dc->funcs->pSelectPen( dc->physDev, hPen );
2168 DeleteObject( hPen );
2171 if (crColor != CLR_INVALID)
2173 oldClr = dc->dcPenColor;
2174 dc->dcPenColor = crColor;
2177 release_dc_ptr( dc );
2180 return oldClr;
2183 /***********************************************************************
2184 * CancelDC (GDI32.@)
2186 BOOL WINAPI CancelDC(HDC hdc)
2188 FIXME("stub\n");
2189 return TRUE;
2192 /***********************************************************************
2193 * SetVirtualResolution (GDI32.@)
2195 * Undocumented on msdn. Called when PowerPoint XP saves a file.
2197 DWORD WINAPI SetVirtualResolution(HDC hdc, DWORD dw2, DWORD dw3, DWORD dw4, DWORD dw5)
2199 FIXME("(%p %08x %08x %08x %08x): stub!\n", hdc, dw2, dw3, dw4, dw5);
2200 return FALSE;
2203 /*******************************************************************
2204 * GetMiterLimit [GDI32.@]
2208 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2210 BOOL bRet = FALSE;
2211 DC *dc;
2213 TRACE("(%p,%p)\n", hdc, peLimit);
2215 dc = get_dc_ptr( hdc );
2216 if (dc)
2218 if (peLimit)
2219 *peLimit = dc->miterLimit;
2221 release_dc_ptr( dc );
2222 bRet = TRUE;
2224 return bRet;
2227 /*******************************************************************
2228 * SetMiterLimit [GDI32.@]
2232 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2234 BOOL bRet = FALSE;
2235 DC *dc;
2237 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2239 dc = get_dc_ptr( hdc );
2240 if (dc)
2242 if (peOldLimit)
2243 *peOldLimit = dc->miterLimit;
2244 dc->miterLimit = eNewLimit;
2245 release_dc_ptr( dc );
2246 bRet = TRUE;
2248 return bRet;
2251 /*******************************************************************
2252 * GdiIsMetaPrintDC [GDI32.@]
2254 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2256 FIXME("%p\n", hdc);
2257 return FALSE;
2260 /*******************************************************************
2261 * GdiIsMetaFileDC [GDI32.@]
2263 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2265 TRACE("%p\n", hdc);
2267 switch( GetObjectType( hdc ) )
2269 case OBJ_METADC:
2270 case OBJ_ENHMETADC:
2271 return TRUE;
2273 return FALSE;
2276 /*******************************************************************
2277 * GdiIsPlayMetafileDC [GDI32.@]
2279 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2281 FIXME("%p\n", hdc);
2282 return FALSE;