kernel32/tests: Fix a failing test in Vista.
[wine/multimedia.git] / dlls / gdi32 / dc.c
blob8e4601ddf7f3fb93eee1123051e73e596e625839
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, void *obj );
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, MAGIC_DONTCARE );
57 if (!dc) return NULL;
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))
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 HDC hdc;
78 DC *dc;
80 if (!(dc = GDI_AllocObject( sizeof(*dc), magic, (HGDIOBJ*)&hdc, &dc_funcs ))) return NULL;
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;
157 /***********************************************************************
158 * free_dc_ptr
160 BOOL free_dc_ptr( DC *dc )
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 );
169 /***********************************************************************
170 * get_dc_ptr
172 * Retrieve a DC pointer but release the GDI lock.
174 DC *get_dc_ptr( HDC hdc )
176 DC *dc = get_dc_obj( hdc );
177 if (!dc) return NULL;
179 if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
181 dc->thread = GetCurrentThreadId();
183 else if (dc->thread != GetCurrentThreadId())
185 WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
186 GDI_ReleaseObj( hdc );
187 return NULL;
189 else InterlockedIncrement( &dc->refcount );
191 GDI_ReleaseObj( hdc );
192 return dc;
196 /***********************************************************************
197 * release_dc_ptr
199 void release_dc_ptr( DC *dc )
201 LONG ref;
203 dc->thread = 0;
204 ref = InterlockedDecrement( &dc->refcount );
205 assert( ref >= 0 );
206 if (ref) dc->thread = GetCurrentThreadId(); /* we still own it */
210 /***********************************************************************
211 * update_dc
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.
217 void update_dc( DC *dc )
219 if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookThunk)
220 dc->hookThunk( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
224 /***********************************************************************
225 * DC_DeleteObject
227 static BOOL DC_DeleteObject( HGDIOBJ handle, void *obj )
229 GDI_ReleaseObj( handle );
230 return DeleteDC( handle );
234 /***********************************************************************
235 * DC_InitDC
237 * Setup device-specific DC values for a newly created DC.
239 void DC_InitDC( DC* dc )
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 );
251 /***********************************************************************
252 * DC_InvertXform
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.
258 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
260 double determinant;
262 determinant = xformSrc->eM11*xformSrc->eM22 -
263 xformSrc->eM12*xformSrc->eM21;
264 if (determinant > -1e-12 && determinant < 1e-12)
265 return FALSE;
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;
276 return TRUE;
280 /***********************************************************************
281 * DC_UpdateXforms
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.
290 void DC_UpdateXforms( DC *dc )
292 XFORM xformWnd2Vport, oldworld2vport;
293 double scaleX, scaleY;
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;
307 oldworld2vport = dc->xformWorld2Vport;
308 /* Combine with the world transformation */
309 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
310 &xformWnd2Vport );
312 /* Create inverse of world-to-viewport transformation */
313 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
314 &dc->xformVport2World );
316 /* Reselect the font and pen back into the dc so that the size
317 gets updated. */
318 if (memcmp(&oldworld2vport, &dc->xformWorld2Vport, sizeof(oldworld2vport)) &&
319 !GdiIsMetaFileDC(dc->hSelf))
321 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
322 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_PEN));
327 /***********************************************************************
328 * GetDCState (Not a Windows API)
330 HDC WINAPI GetDCState( HDC hdc )
332 DC * newdc, * dc;
333 HGDIOBJ handle;
335 if (!(dc = get_dc_ptr( hdc ))) return 0;
336 if (!(newdc = GDI_AllocObject( sizeof(DC), GDIMAGIC(dc->header.wMagic), &handle, &dc_funcs )))
338 release_dc_ptr( dc );
339 return 0;
341 TRACE("(%p): returning %p\n", hdc, handle );
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;
385 newdc->hSelf = (HDC)handle;
386 newdc->thread = GetCurrentThreadId();
387 newdc->refcount = 1;
388 newdc->saveLevel = 0;
389 newdc->saved_dc = 0;
390 GDI_ReleaseObj( handle );
392 PATH_InitGdiPath( &newdc->path );
394 newdc->pAbortProc = NULL;
395 newdc->hookThunk = NULL;
396 newdc->hookProc = 0;
397 newdc->saved_visrgn = NULL;
399 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
401 newdc->hVisRgn = 0;
402 newdc->hClipRgn = 0;
403 newdc->hMetaRgn = 0;
404 newdc->hMetaClipRgn = 0;
405 if (dc->hClipRgn)
407 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
408 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
410 if (dc->hMetaRgn)
412 newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
413 CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
415 /* don't bother recomputing hMetaClipRgn, we'll do that in SetDCState */
417 if(dc->gdiFont) {
418 newdc->gdiFont = dc->gdiFont;
419 } else
420 newdc->gdiFont = 0;
422 release_dc_ptr( newdc );
423 release_dc_ptr( dc );
424 return handle;
428 /***********************************************************************
429 * SetDCState (Not a Windows API)
431 void WINAPI SetDCState( HDC hdc, HDC hdcs )
433 DC *dc, *dcs;
435 if (!(dc = get_dc_ptr( hdc ))) return;
436 if (!(dcs = get_dc_ptr( hdcs )))
438 release_dc_ptr( dc );
439 return;
441 if (!(dcs->flags & DC_SAVED))
443 release_dc_ptr( dc );
444 release_dc_ptr( dcs );
445 return;
447 TRACE("%p %p\n", hdc, hdcs );
449 update_dc( dc );
450 dc->flags = dcs->flags & ~DC_SAVED;
451 dc->layout = dcs->layout;
452 dc->hDevice = dcs->hDevice;
453 dc->ROPmode = dcs->ROPmode;
454 dc->polyFillMode = dcs->polyFillMode;
455 dc->stretchBltMode = dcs->stretchBltMode;
456 dc->relAbsMode = dcs->relAbsMode;
457 dc->backgroundMode = dcs->backgroundMode;
458 dc->backgroundColor = dcs->backgroundColor;
459 dc->textColor = dcs->textColor;
460 dc->dcBrushColor = dcs->dcBrushColor;
461 dc->dcPenColor = dcs->dcPenColor;
462 dc->brushOrgX = dcs->brushOrgX;
463 dc->brushOrgY = dcs->brushOrgY;
464 dc->textAlign = dcs->textAlign;
465 dc->charExtra = dcs->charExtra;
466 dc->breakExtra = dcs->breakExtra;
467 dc->breakRem = dcs->breakRem;
468 dc->MapMode = dcs->MapMode;
469 dc->GraphicsMode = dcs->GraphicsMode;
470 dc->CursPosX = dcs->CursPosX;
471 dc->CursPosY = dcs->CursPosY;
472 dc->ArcDirection = dcs->ArcDirection;
473 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
474 dc->xformWorld2Vport = dcs->xformWorld2Vport;
475 dc->xformVport2World = dcs->xformVport2World;
476 dc->vport2WorldValid = dcs->vport2WorldValid;
477 dc->BoundsRect = dcs->BoundsRect;
479 dc->wndOrgX = dcs->wndOrgX;
480 dc->wndOrgY = dcs->wndOrgY;
481 dc->wndExtX = dcs->wndExtX;
482 dc->wndExtY = dcs->wndExtY;
483 dc->vportOrgX = dcs->vportOrgX;
484 dc->vportOrgY = dcs->vportOrgY;
485 dc->vportExtX = dcs->vportExtX;
486 dc->vportExtY = dcs->vportExtY;
488 if (dcs->hClipRgn)
490 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
491 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
493 else
495 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
496 dc->hClipRgn = 0;
498 if (dcs->hMetaRgn)
500 if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
501 CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
503 else
505 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
506 dc->hMetaRgn = 0;
508 CLIPPING_UpdateGCRegion( dc );
510 SelectObject( hdc, dcs->hBitmap );
511 SelectObject( hdc, dcs->hBrush );
512 SelectObject( hdc, dcs->hFont );
513 SelectObject( hdc, dcs->hPen );
514 SetBkColor( hdc, dcs->backgroundColor);
515 SetTextColor( hdc, dcs->textColor);
516 GDISelectPalette( hdc, dcs->hPalette, FALSE );
517 release_dc_ptr( dc );
518 release_dc_ptr( dcs );
522 /***********************************************************************
523 * GetDCState (GDI.179)
525 HDC16 WINAPI GetDCState16( HDC16 hdc )
527 return HDC_16( GetDCState( HDC_32(hdc) ));
531 /***********************************************************************
532 * SetDCState (GDI.180)
534 void WINAPI SetDCState16( HDC16 hdc, HDC16 hdcs )
536 SetDCState( HDC_32(hdc), HDC_32(hdcs) );
540 /***********************************************************************
541 * SaveDC (GDI32.@)
543 INT WINAPI SaveDC( HDC hdc )
545 HDC hdcs;
546 DC * dc, * dcs;
547 INT ret;
549 dc = get_dc_ptr( hdc );
550 if (!dc) return 0;
552 if(dc->funcs->pSaveDC)
554 ret = dc->funcs->pSaveDC( dc->physDev );
555 if(ret)
556 ret = ++dc->saveLevel;
557 release_dc_ptr( dc );
558 return ret;
561 if (!(hdcs = GetDCState( hdc )))
563 release_dc_ptr( dc );
564 return 0;
566 dcs = get_dc_ptr( hdcs );
568 /* Copy path. The reason why path saving / restoring is in SaveDC/
569 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
570 * functions are only in Win16 (which doesn't have paths) and that
571 * SetDCState doesn't allow us to signal an error (which can happen
572 * when copying paths).
574 if (!PATH_AssignGdiPath( &dcs->path, &dc->path ))
576 release_dc_ptr( dc );
577 release_dc_ptr( dcs );
578 DeleteDC( hdcs );
579 return 0;
582 dcs->saved_dc = dc->saved_dc;
583 dc->saved_dc = hdcs;
584 TRACE("(%p): returning %d\n", hdc, dc->saveLevel+1 );
585 ret = ++dc->saveLevel;
586 release_dc_ptr( dcs );
587 release_dc_ptr( dc );
588 return ret;
592 /***********************************************************************
593 * RestoreDC (GDI32.@)
595 BOOL WINAPI RestoreDC( HDC hdc, INT level )
597 DC * dc, * dcs;
598 BOOL success;
600 TRACE("%p %d\n", hdc, level );
601 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
603 if(abs(level) > dc->saveLevel || level == 0)
605 release_dc_ptr( dc );
606 return FALSE;
609 update_dc( dc );
611 if(dc->funcs->pRestoreDC)
613 success = dc->funcs->pRestoreDC( dc->physDev, level );
614 if(level < 0) level = dc->saveLevel + level + 1;
615 if(success)
616 dc->saveLevel = level - 1;
617 release_dc_ptr( dc );
618 return success;
621 if (level < 0) level = dc->saveLevel + level + 1;
622 success=TRUE;
623 while (dc->saveLevel >= level)
625 HDC hdcs = dc->saved_dc;
626 if (!(dcs = get_dc_ptr( hdcs )))
628 success = FALSE;
629 break;
631 dc->saved_dc = dcs->saved_dc;
632 dcs->saved_dc = 0;
633 if (--dc->saveLevel < level)
635 SetDCState( hdc, hdcs );
636 if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
637 /* FIXME: This might not be quite right, since we're
638 * returning FALSE but still destroying the saved DC state */
639 success=FALSE;
641 release_dc_ptr( dcs );
642 DeleteDC( hdcs );
644 release_dc_ptr( dc );
645 return success;
649 /***********************************************************************
650 * CreateDCW (GDI32.@)
652 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
653 const DEVMODEW *initData )
655 HDC hdc;
656 DC * dc;
657 const DC_FUNCTIONS *funcs;
658 WCHAR buf[300];
660 GDI_CheckNotLock();
662 if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
664 if (!driver)
666 ERR( "no device found for %s\n", debugstr_w(device) );
667 return 0;
669 strcpyW(buf, driver);
672 if (!(funcs = DRIVER_load_driver( buf )))
674 ERR( "no driver found for %s\n", debugstr_w(buf) );
675 return 0;
677 if (!(dc = alloc_dc_ptr( funcs, DC_MAGIC ))) goto error;
678 hdc = dc->hSelf;
680 dc->hBitmap = GetStockObject( DEFAULT_BITMAP );
681 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error;
683 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
684 debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
686 if (dc->funcs->pCreateDC &&
687 !dc->funcs->pCreateDC( hdc, &dc->physDev, buf, device, output, initData ))
689 WARN("creation aborted by device\n" );
690 goto error;
693 SetRectRgn( dc->hVisRgn, 0, 0,
694 GetDeviceCaps( hdc, DESKTOPHORZRES ), GetDeviceCaps( hdc, DESKTOPVERTRES ) );
696 DC_InitDC( dc );
697 release_dc_ptr( dc );
698 return hdc;
700 error:
701 if (dc && dc->hVisRgn) DeleteObject( dc->hVisRgn );
702 if (dc) free_dc_ptr( dc );
703 DRIVER_release_driver( funcs );
704 return 0;
708 /***********************************************************************
709 * CreateDCA (GDI32.@)
711 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
712 const DEVMODEA *initData )
714 UNICODE_STRING driverW, deviceW, outputW;
715 DEVMODEW *initDataW;
716 HDC ret;
718 if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
719 else driverW.Buffer = NULL;
721 if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
722 else deviceW.Buffer = NULL;
724 if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
725 else outputW.Buffer = NULL;
727 initDataW = NULL;
728 if (initData)
730 /* don't convert initData for DISPLAY driver, it's not used */
731 if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
732 initDataW = GdiConvertToDevmodeW(initData);
735 ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
737 RtlFreeUnicodeString(&driverW);
738 RtlFreeUnicodeString(&deviceW);
739 RtlFreeUnicodeString(&outputW);
740 HeapFree(GetProcessHeap(), 0, initDataW);
741 return ret;
745 /***********************************************************************
746 * CreateICA (GDI32.@)
748 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
749 const DEVMODEA* initData )
751 /* Nothing special yet for ICs */
752 return CreateDCA( driver, device, output, initData );
756 /***********************************************************************
757 * CreateICW (GDI32.@)
759 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
760 const DEVMODEW* initData )
762 /* Nothing special yet for ICs */
763 return CreateDCW( driver, device, output, initData );
767 /***********************************************************************
768 * CreateCompatibleDC (GDI32.@)
770 HDC WINAPI CreateCompatibleDC( HDC hdc )
772 DC *dc, *origDC;
773 HDC ret;
774 const DC_FUNCTIONS *funcs = NULL;
775 PHYSDEV physDev = NULL;
777 GDI_CheckNotLock();
779 if ((origDC = get_dc_ptr( hdc )))
781 if (GetObjectType( hdc ) == OBJ_DC)
783 funcs = origDC->funcs;
784 physDev = origDC->physDev;
786 release_dc_ptr( origDC );
787 if (funcs) funcs = DRIVER_get_driver( funcs );
789 else if (hdc) return 0;
791 if (!funcs && !(funcs = DRIVER_load_driver( displayW ))) return 0;
793 if (!(dc = alloc_dc_ptr( funcs, MEMORY_DC_MAGIC ))) goto error;
795 TRACE("(%p): returning %p\n", hdc, dc->hSelf );
797 dc->hBitmap = GetStockObject( DEFAULT_BITMAP );
798 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error; /* default bitmap is 1x1 */
800 /* Copy the driver-specific physical device info into
801 * the new DC. The driver may use this read-only info
802 * while creating the compatible DC below. */
803 dc->physDev = physDev;
804 ret = dc->hSelf;
806 if (dc->funcs->pCreateDC &&
807 !dc->funcs->pCreateDC( dc->hSelf, &dc->physDev, NULL, NULL, NULL, NULL ))
809 WARN("creation aborted by device\n");
810 goto error;
813 DC_InitDC( dc );
814 release_dc_ptr( dc );
815 return ret;
817 error:
818 if (dc && dc->hVisRgn) DeleteObject( dc->hVisRgn );
819 if (dc) free_dc_ptr( dc );
820 DRIVER_release_driver( funcs );
821 return 0;
825 /***********************************************************************
826 * DeleteDC (GDI32.@)
828 BOOL WINAPI DeleteDC( HDC hdc )
830 const DC_FUNCTIONS *funcs = NULL;
831 DC * dc;
833 TRACE("%p\n", hdc );
835 GDI_CheckNotLock();
837 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
838 if (dc->refcount != 1)
840 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
841 release_dc_ptr( dc );
842 return FALSE;
845 /* Call hook procedure to check whether is it OK to delete this DC */
846 if (dc->hookThunk && !dc->hookThunk( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ))
848 release_dc_ptr( dc );
849 return FALSE;
852 while (dc->saveLevel)
854 DC * dcs;
855 HDC hdcs = dc->saved_dc;
856 if (!(dcs = get_dc_ptr( hdcs ))) break;
857 dc->saved_dc = dcs->saved_dc;
858 dc->saveLevel--;
859 if (dcs->hClipRgn) DeleteObject( dcs->hClipRgn );
860 if (dcs->hMetaRgn) DeleteObject( dcs->hMetaRgn );
861 if (dcs->hMetaClipRgn) DeleteObject( dcs->hMetaClipRgn );
862 if (dcs->hVisRgn) DeleteObject( dcs->hVisRgn );
863 PATH_DestroyGdiPath(&dcs->path);
864 free_dc_ptr( dcs );
867 if (!(dc->flags & DC_SAVED))
869 SelectObject( hdc, GetStockObject(BLACK_PEN) );
870 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
871 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
872 SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
873 funcs = dc->funcs;
874 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc->physDev);
875 dc->physDev = NULL;
878 while (dc->saved_visrgn)
880 struct saved_visrgn *next = dc->saved_visrgn->next;
881 DeleteObject( dc->saved_visrgn->hrgn );
882 HeapFree( GetProcessHeap(), 0, dc->saved_visrgn );
883 dc->saved_visrgn = next;
885 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
886 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
887 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
888 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
889 PATH_DestroyGdiPath(&dc->path);
891 free_dc_ptr( dc );
892 if (funcs) DRIVER_release_driver( funcs ); /* do that after releasing the GDI lock */
893 return TRUE;
897 /***********************************************************************
898 * ResetDCW (GDI32.@)
900 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
902 DC *dc;
903 HDC ret = hdc;
905 if ((dc = get_dc_ptr( hdc )))
907 if (dc->funcs->pResetDC) ret = dc->funcs->pResetDC( dc->physDev, devmode );
908 release_dc_ptr( dc );
910 return ret;
914 /***********************************************************************
915 * ResetDCA (GDI32.@)
917 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
919 DEVMODEW *devmodeW;
920 HDC ret;
922 if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
923 else devmodeW = NULL;
925 ret = ResetDCW(hdc, devmodeW);
927 HeapFree(GetProcessHeap(), 0, devmodeW);
928 return ret;
932 /***********************************************************************
933 * GetDeviceCaps (GDI32.@)
935 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
937 DC *dc;
938 INT ret = 0;
940 if ((dc = get_dc_ptr( hdc )))
942 if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc->physDev, cap );
943 else switch(cap) /* return meaningful values for some entries */
945 case HORZRES: ret = 640; break;
946 case VERTRES: ret = 480; break;
947 case BITSPIXEL: ret = 1; break;
948 case PLANES: ret = 1; break;
949 case NUMCOLORS: ret = 2; break;
950 case ASPECTX: ret = 36; break;
951 case ASPECTY: ret = 36; break;
952 case ASPECTXY: ret = 51; break;
953 case LOGPIXELSX: ret = 72; break;
954 case LOGPIXELSY: ret = 72; break;
955 case SIZEPALETTE: ret = 2; break;
957 release_dc_ptr( dc );
959 return ret;
963 /***********************************************************************
964 * GetBkColor (GDI32.@)
966 COLORREF WINAPI GetBkColor( HDC hdc )
968 COLORREF ret = 0;
969 DC * dc = get_dc_ptr( hdc );
970 if (dc)
972 ret = dc->backgroundColor;
973 release_dc_ptr( dc );
975 return ret;
979 /***********************************************************************
980 * SetBkColor (GDI32.@)
982 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
984 COLORREF oldColor;
985 DC * dc = get_dc_ptr( hdc );
987 TRACE("hdc=%p color=0x%08x\n", hdc, color);
989 if (!dc) return CLR_INVALID;
990 oldColor = dc->backgroundColor;
991 if (dc->funcs->pSetBkColor)
993 color = dc->funcs->pSetBkColor(dc->physDev, color);
994 if (color == CLR_INVALID) /* don't change it */
996 color = oldColor;
997 oldColor = CLR_INVALID;
1000 dc->backgroundColor = color;
1001 release_dc_ptr( dc );
1002 return oldColor;
1006 /***********************************************************************
1007 * GetTextColor (GDI32.@)
1009 COLORREF WINAPI GetTextColor( HDC hdc )
1011 COLORREF ret = 0;
1012 DC * dc = get_dc_ptr( hdc );
1013 if (dc)
1015 ret = dc->textColor;
1016 release_dc_ptr( dc );
1018 return ret;
1022 /***********************************************************************
1023 * SetTextColor (GDI32.@)
1025 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
1027 COLORREF oldColor;
1028 DC * dc = get_dc_ptr( hdc );
1030 TRACE(" hdc=%p color=0x%08x\n", hdc, color);
1032 if (!dc) return CLR_INVALID;
1033 oldColor = dc->textColor;
1034 if (dc->funcs->pSetTextColor)
1036 color = dc->funcs->pSetTextColor(dc->physDev, color);
1037 if (color == CLR_INVALID) /* don't change it */
1039 color = oldColor;
1040 oldColor = CLR_INVALID;
1043 dc->textColor = color;
1044 release_dc_ptr( dc );
1045 return oldColor;
1049 /***********************************************************************
1050 * GetTextAlign (GDI32.@)
1052 UINT WINAPI GetTextAlign( HDC hdc )
1054 UINT ret = 0;
1055 DC * dc = get_dc_ptr( hdc );
1056 if (dc)
1058 ret = dc->textAlign;
1059 release_dc_ptr( dc );
1061 return ret;
1065 /***********************************************************************
1066 * SetTextAlign (GDI32.@)
1068 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
1070 UINT ret;
1071 DC *dc = get_dc_ptr( hdc );
1073 TRACE("hdc=%p align=%d\n", hdc, align);
1075 if (!dc) return 0x0;
1076 ret = dc->textAlign;
1077 if (dc->funcs->pSetTextAlign)
1078 if (!dc->funcs->pSetTextAlign(dc->physDev, align))
1079 ret = GDI_ERROR;
1080 if (ret != GDI_ERROR)
1081 dc->textAlign = align;
1082 release_dc_ptr( dc );
1083 return ret;
1086 /***********************************************************************
1087 * GetDCOrgEx (GDI32.@)
1089 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1091 DC * dc;
1093 if (!lpp) return FALSE;
1094 if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1096 lpp->x = lpp->y = 0;
1097 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc->physDev, lpp );
1098 release_dc_ptr( dc );
1099 return TRUE;
1103 /***********************************************************************
1104 * SetDCOrg (GDI.117)
1106 DWORD WINAPI SetDCOrg16( HDC16 hdc16, INT16 x, INT16 y )
1108 DWORD prevOrg = 0;
1109 HDC hdc = HDC_32( hdc16 );
1110 DC *dc = get_dc_ptr( hdc );
1111 if (!dc) return 0;
1112 if (dc->funcs->pSetDCOrg) prevOrg = dc->funcs->pSetDCOrg( dc->physDev, x, y );
1113 release_dc_ptr( dc );
1114 return prevOrg;
1118 /***********************************************************************
1119 * GetGraphicsMode (GDI32.@)
1121 INT WINAPI GetGraphicsMode( HDC hdc )
1123 INT ret = 0;
1124 DC * dc = get_dc_ptr( hdc );
1125 if (dc)
1127 ret = dc->GraphicsMode;
1128 release_dc_ptr( dc );
1130 return ret;
1134 /***********************************************************************
1135 * SetGraphicsMode (GDI32.@)
1137 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1139 INT ret = 0;
1140 DC *dc = get_dc_ptr( hdc );
1142 /* One would think that setting the graphics mode to GM_COMPATIBLE
1143 * would also reset the world transformation matrix to the unity
1144 * matrix. However, in Windows, this is not the case. This doesn't
1145 * make a lot of sense to me, but that's the way it is.
1147 if (!dc) return 0;
1148 if ((mode > 0) && (mode <= GM_LAST))
1150 ret = dc->GraphicsMode;
1151 dc->GraphicsMode = mode;
1153 release_dc_ptr( dc );
1154 return ret;
1158 /***********************************************************************
1159 * GetArcDirection (GDI32.@)
1161 INT WINAPI GetArcDirection( HDC hdc )
1163 INT ret = 0;
1164 DC * dc = get_dc_ptr( hdc );
1165 if (dc)
1167 ret = dc->ArcDirection;
1168 release_dc_ptr( dc );
1170 return ret;
1174 /***********************************************************************
1175 * SetArcDirection (GDI32.@)
1177 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1179 DC * dc;
1180 INT nOldDirection = 0;
1182 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1184 SetLastError(ERROR_INVALID_PARAMETER);
1185 return 0;
1188 if ((dc = get_dc_ptr( hdc )))
1190 if (dc->funcs->pSetArcDirection)
1192 dc->funcs->pSetArcDirection(dc->physDev, nDirection);
1194 nOldDirection = dc->ArcDirection;
1195 dc->ArcDirection = nDirection;
1196 release_dc_ptr( dc );
1198 return nOldDirection;
1202 /***********************************************************************
1203 * GetWorldTransform (GDI32.@)
1205 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1207 DC * dc;
1208 if (!xform) return FALSE;
1209 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1210 *xform = dc->xformWorld2Wnd;
1211 release_dc_ptr( dc );
1212 return TRUE;
1216 /***********************************************************************
1217 * GetTransform (GDI32.@)
1219 BOOL WINAPI GetTransform( HDC hdc, DWORD unknown, LPXFORM xform )
1221 if (unknown == 0x0203) return GetWorldTransform( hdc, xform );
1222 FIXME("stub: don't know what to do for code %x\n", unknown );
1223 return FALSE;
1227 /***********************************************************************
1228 * SetWorldTransform (GDI32.@)
1230 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1232 BOOL ret = FALSE;
1233 DC *dc = get_dc_ptr( hdc );
1235 if (!dc) return FALSE;
1236 if (!xform) goto done;
1238 /* Check that graphics mode is GM_ADVANCED */
1239 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1241 TRACE("eM11 %f eM12 %f eM21 %f eM22 %f eDx %f eDy %f\n",
1242 xform->eM11, xform->eM12, xform->eM21, xform->eM22, xform->eDx, xform->eDy);
1244 if (dc->funcs->pSetWorldTransform)
1246 ret = dc->funcs->pSetWorldTransform(dc->physDev, xform);
1247 if (!ret) goto done;
1250 dc->xformWorld2Wnd = *xform;
1251 DC_UpdateXforms( dc );
1252 ret = TRUE;
1253 done:
1254 release_dc_ptr( dc );
1255 return ret;
1259 /****************************************************************************
1260 * ModifyWorldTransform [GDI32.@]
1261 * Modifies the world transformation for a device context.
1263 * PARAMS
1264 * hdc [I] Handle to device context
1265 * xform [I] XFORM structure that will be used to modify the world
1266 * transformation
1267 * iMode [I] Specifies in what way to modify the world transformation
1268 * Possible values:
1269 * MWT_IDENTITY
1270 * Resets the world transformation to the identity matrix.
1271 * The parameter xform is ignored.
1272 * MWT_LEFTMULTIPLY
1273 * Multiplies xform into the world transformation matrix from
1274 * the left.
1275 * MWT_RIGHTMULTIPLY
1276 * Multiplies xform into the world transformation matrix from
1277 * the right.
1279 * RETURNS
1280 * Success: TRUE.
1281 * Failure: FALSE. Use GetLastError() to determine the cause.
1283 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1284 DWORD iMode )
1286 BOOL ret = FALSE;
1287 DC *dc = get_dc_ptr( hdc );
1289 /* Check for illegal parameters */
1290 if (!dc) return FALSE;
1291 if (!xform && iMode != MWT_IDENTITY) goto done;
1293 /* Check that graphics mode is GM_ADVANCED */
1294 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1296 if (dc->funcs->pModifyWorldTransform)
1298 ret = dc->funcs->pModifyWorldTransform(dc->physDev, xform, iMode);
1299 if (!ret) goto done;
1302 switch (iMode)
1304 case MWT_IDENTITY:
1305 dc->xformWorld2Wnd.eM11 = 1.0f;
1306 dc->xformWorld2Wnd.eM12 = 0.0f;
1307 dc->xformWorld2Wnd.eM21 = 0.0f;
1308 dc->xformWorld2Wnd.eM22 = 1.0f;
1309 dc->xformWorld2Wnd.eDx = 0.0f;
1310 dc->xformWorld2Wnd.eDy = 0.0f;
1311 break;
1312 case MWT_LEFTMULTIPLY:
1313 CombineTransform( &dc->xformWorld2Wnd, xform,
1314 &dc->xformWorld2Wnd );
1315 break;
1316 case MWT_RIGHTMULTIPLY:
1317 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1318 xform );
1319 break;
1320 default:
1321 goto done;
1324 DC_UpdateXforms( dc );
1325 ret = TRUE;
1326 done:
1327 release_dc_ptr( dc );
1328 return ret;
1332 /****************************************************************************
1333 * CombineTransform [GDI32.@]
1334 * Combines two transformation matrices.
1336 * PARAMS
1337 * xformResult [O] Stores the result of combining the two matrices
1338 * xform1 [I] Specifies the first matrix to apply
1339 * xform2 [I] Specifies the second matrix to apply
1341 * REMARKS
1342 * The same matrix can be passed in for more than one of the parameters.
1344 * RETURNS
1345 * Success: TRUE.
1346 * Failure: FALSE. Use GetLastError() to determine the cause.
1348 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1349 const XFORM *xform2 )
1351 XFORM xformTemp;
1353 /* Check for illegal parameters */
1354 if (!xformResult || !xform1 || !xform2)
1355 return FALSE;
1357 /* Create the result in a temporary XFORM, since xformResult may be
1358 * equal to xform1 or xform2 */
1359 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1360 xform1->eM12 * xform2->eM21;
1361 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1362 xform1->eM12 * xform2->eM22;
1363 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1364 xform1->eM22 * xform2->eM21;
1365 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1366 xform1->eM22 * xform2->eM22;
1367 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1368 xform1->eDy * xform2->eM21 +
1369 xform2->eDx;
1370 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1371 xform1->eDy * xform2->eM22 +
1372 xform2->eDy;
1374 /* Copy the result to xformResult */
1375 *xformResult = xformTemp;
1377 return TRUE;
1381 /***********************************************************************
1382 * SetDCHook (GDI32.@)
1384 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1386 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1388 DC *dc = get_dc_ptr( hdc );
1390 if (!dc) return FALSE;
1392 if (!(dc->flags & DC_SAVED))
1394 dc->dwHookData = dwHookData;
1395 dc->hookThunk = hookProc;
1397 release_dc_ptr( dc );
1398 return TRUE;
1402 /***********************************************************************
1403 * GetDCHook (GDI32.@)
1405 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1407 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1409 DC *dc = get_dc_ptr( hdc );
1410 DWORD_PTR ret;
1412 if (!dc) return 0;
1413 if (proc) *proc = dc->hookThunk;
1414 ret = dc->dwHookData;
1415 release_dc_ptr( dc );
1416 return ret;
1420 /* relay function to call the 16-bit DC hook proc */
1421 static BOOL WINAPI call_dc_hook16( HDC hdc, WORD code, DWORD_PTR data, LPARAM lParam )
1423 WORD args[6];
1424 DWORD ret = 0;
1425 DC *dc = get_dc_ptr( hdc );
1427 if (!dc) return FALSE;
1428 if (dc->hookProc)
1430 args[5] = HDC_16(hdc);
1431 args[4] = code;
1432 args[3] = HIWORD(data);
1433 args[2] = LOWORD(data);
1434 args[1] = HIWORD(lParam);
1435 args[0] = LOWORD(lParam);
1436 WOWCallback16Ex( (DWORD)dc->hookProc, WCB16_PASCAL, sizeof(args), args, &ret );
1438 release_dc_ptr( dc );
1439 return LOWORD(ret);
1442 /***********************************************************************
1443 * SetDCHook (GDI.190)
1445 BOOL16 WINAPI SetDCHook16( HDC16 hdc16, FARPROC16 hookProc, DWORD dwHookData )
1447 DC *dc = get_dc_ptr( HDC_32(hdc16) );
1449 if (!dc) return FALSE;
1450 if (!(dc->flags & DC_SAVED))
1452 dc->dwHookData = dwHookData;
1453 dc->hookThunk = call_dc_hook16;
1454 dc->hookProc = hookProc;
1456 release_dc_ptr( dc );
1457 return TRUE;
1461 /***********************************************************************
1462 * GetDCHook (GDI.191)
1464 DWORD WINAPI GetDCHook16( HDC16 hdc16, FARPROC16 *phookProc )
1466 HDC hdc = HDC_32( hdc16 );
1467 DC *dc = get_dc_ptr( hdc );
1468 DWORD ret;
1470 if (!dc) return 0;
1471 *phookProc = dc->hookProc;
1472 ret = dc->dwHookData;
1473 release_dc_ptr( dc );
1474 return ret;
1478 /***********************************************************************
1479 * SetHookFlags (GDI32.@)
1481 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1483 WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
1485 DC *dc = get_dc_obj( hdc ); /* not get_dc_ptr, this needs to work from any thread */
1486 LONG ret = 0;
1488 if (!dc) return 0;
1490 /* "Undocumented Windows" info is slightly confusing. */
1492 TRACE("hDC %p, flags %04x\n",hdc,flags);
1494 if (flags & DCHF_INVALIDATEVISRGN)
1495 ret = InterlockedExchange( &dc->dirty, 1 );
1496 else if (flags & DCHF_VALIDATEVISRGN || !flags)
1497 ret = InterlockedExchange( &dc->dirty, 0 );
1499 GDI_ReleaseObj( dc );
1500 return ret;
1503 /***********************************************************************
1504 * SetICMMode (GDI32.@)
1506 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1508 /*FIXME: Assume that ICM is always off, and cannot be turned on */
1509 if (iEnableICM == ICM_OFF) return ICM_OFF;
1510 if (iEnableICM == ICM_ON) return 0;
1511 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1512 return 0;
1515 /***********************************************************************
1516 * GetDeviceGammaRamp (GDI32.@)
1518 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1520 BOOL ret = FALSE;
1521 DC *dc = get_dc_ptr( hDC );
1523 if( dc )
1525 if (dc->funcs->pGetDeviceGammaRamp)
1526 ret = dc->funcs->pGetDeviceGammaRamp(dc->physDev, ptr);
1527 release_dc_ptr( dc );
1529 return ret;
1532 /***********************************************************************
1533 * SetDeviceGammaRamp (GDI32.@)
1535 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1537 BOOL ret = FALSE;
1538 DC *dc = get_dc_ptr( hDC );
1540 if( dc )
1542 if (dc->funcs->pSetDeviceGammaRamp)
1543 ret = dc->funcs->pSetDeviceGammaRamp(dc->physDev, ptr);
1544 release_dc_ptr( dc );
1546 return ret;
1549 /***********************************************************************
1550 * GetColorSpace (GDI32.@)
1552 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1554 /*FIXME Need to to whatever GetColorSpace actually does */
1555 return 0;
1558 /***********************************************************************
1559 * CreateColorSpaceA (GDI32.@)
1561 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1563 FIXME( "stub\n" );
1564 return 0;
1567 /***********************************************************************
1568 * CreateColorSpaceW (GDI32.@)
1570 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1572 FIXME( "stub\n" );
1573 return 0;
1576 /***********************************************************************
1577 * DeleteColorSpace (GDI32.@)
1579 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1581 FIXME( "stub\n" );
1583 return TRUE;
1586 /***********************************************************************
1587 * SetColorSpace (GDI32.@)
1589 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1591 FIXME( "stub\n" );
1593 return hColorSpace;
1596 /***********************************************************************
1597 * GetBoundsRect (GDI32.@)
1599 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1601 UINT ret;
1602 DC *dc = get_dc_ptr( hdc );
1604 if ( !dc ) return 0;
1606 if (rect) *rect = dc->BoundsRect;
1608 ret = ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1610 if (flags & DCB_RESET)
1612 dc->BoundsRect.left = 0;
1613 dc->BoundsRect.top = 0;
1614 dc->BoundsRect.right = 0;
1615 dc->BoundsRect.bottom = 0;
1616 dc->flags &= ~DC_BOUNDS_SET;
1618 release_dc_ptr( dc );
1619 return ret;
1623 /***********************************************************************
1624 * SetBoundsRect (GDI32.@)
1626 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1628 UINT ret;
1629 DC *dc;
1631 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1632 if (!(dc = get_dc_ptr( hdc ))) return 0;
1634 ret = ((dc->flags & DC_BOUNDS_ENABLE) ? DCB_ENABLE : DCB_DISABLE) |
1635 ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1637 if (flags & DCB_RESET)
1639 dc->BoundsRect.left = 0;
1640 dc->BoundsRect.top = 0;
1641 dc->BoundsRect.right = 0;
1642 dc->BoundsRect.bottom = 0;
1643 dc->flags &= ~DC_BOUNDS_SET;
1646 if ((flags & DCB_ACCUMULATE) && rect && rect->left < rect->right && rect->top < rect->bottom)
1648 if (dc->flags & DC_BOUNDS_SET)
1650 dc->BoundsRect.left = min( dc->BoundsRect.left, rect->left );
1651 dc->BoundsRect.top = min( dc->BoundsRect.top, rect->top );
1652 dc->BoundsRect.right = max( dc->BoundsRect.right, rect->right );
1653 dc->BoundsRect.bottom = max( dc->BoundsRect.bottom, rect->bottom );
1655 else
1657 dc->BoundsRect = *rect;
1658 dc->flags |= DC_BOUNDS_SET;
1662 if (flags & DCB_ENABLE) dc->flags |= DC_BOUNDS_ENABLE;
1663 if (flags & DCB_DISABLE) dc->flags &= ~DC_BOUNDS_ENABLE;
1665 release_dc_ptr( dc );
1666 return ret;
1670 /***********************************************************************
1671 * GetRelAbs (GDI32.@)
1673 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1675 INT ret = 0;
1676 DC *dc = get_dc_ptr( hdc );
1677 if (dc)
1679 ret = dc->relAbsMode;
1680 release_dc_ptr( dc );
1682 return ret;
1688 /***********************************************************************
1689 * GetBkMode (GDI32.@)
1691 INT WINAPI GetBkMode( HDC hdc )
1693 INT ret = 0;
1694 DC * dc = get_dc_ptr( hdc );
1695 if (dc)
1697 ret = dc->backgroundMode;
1698 release_dc_ptr( dc );
1700 return ret;
1704 /***********************************************************************
1705 * SetBkMode (GDI32.@)
1707 INT WINAPI SetBkMode( HDC hdc, INT mode )
1709 INT ret;
1710 DC *dc;
1711 if ((mode <= 0) || (mode > BKMODE_LAST))
1713 SetLastError(ERROR_INVALID_PARAMETER);
1714 return 0;
1716 if (!(dc = get_dc_ptr( hdc ))) return 0;
1718 ret = dc->backgroundMode;
1719 if (dc->funcs->pSetBkMode)
1720 if (!dc->funcs->pSetBkMode( dc->physDev, mode ))
1721 ret = 0;
1722 if (ret)
1723 dc->backgroundMode = mode;
1724 release_dc_ptr( dc );
1725 return ret;
1729 /***********************************************************************
1730 * GetROP2 (GDI32.@)
1732 INT WINAPI GetROP2( HDC hdc )
1734 INT ret = 0;
1735 DC * dc = get_dc_ptr( hdc );
1736 if (dc)
1738 ret = dc->ROPmode;
1739 release_dc_ptr( dc );
1741 return ret;
1745 /***********************************************************************
1746 * SetROP2 (GDI32.@)
1748 INT WINAPI SetROP2( HDC hdc, INT mode )
1750 INT ret;
1751 DC *dc;
1752 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1754 SetLastError(ERROR_INVALID_PARAMETER);
1755 return 0;
1757 if (!(dc = get_dc_ptr( hdc ))) return 0;
1758 ret = dc->ROPmode;
1759 if (dc->funcs->pSetROP2)
1760 if (!dc->funcs->pSetROP2( dc->physDev, mode ))
1761 ret = 0;
1762 if (ret)
1763 dc->ROPmode = mode;
1764 release_dc_ptr( dc );
1765 return ret;
1769 /***********************************************************************
1770 * SetRelAbs (GDI32.@)
1772 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1774 INT ret;
1775 DC *dc;
1776 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1778 SetLastError(ERROR_INVALID_PARAMETER);
1779 return 0;
1781 if (!(dc = get_dc_ptr( hdc ))) return 0;
1782 if (dc->funcs->pSetRelAbs)
1783 ret = dc->funcs->pSetRelAbs( dc->physDev, mode );
1784 else
1786 ret = dc->relAbsMode;
1787 dc->relAbsMode = mode;
1789 release_dc_ptr( dc );
1790 return ret;
1794 /***********************************************************************
1795 * GetPolyFillMode (GDI32.@)
1797 INT WINAPI GetPolyFillMode( HDC hdc )
1799 INT ret = 0;
1800 DC * dc = get_dc_ptr( hdc );
1801 if (dc)
1803 ret = dc->polyFillMode;
1804 release_dc_ptr( dc );
1806 return ret;
1810 /***********************************************************************
1811 * SetPolyFillMode (GDI32.@)
1813 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1815 INT ret;
1816 DC *dc;
1817 if ((mode <= 0) || (mode > POLYFILL_LAST))
1819 SetLastError(ERROR_INVALID_PARAMETER);
1820 return 0;
1822 if (!(dc = get_dc_ptr( hdc ))) return 0;
1823 ret = dc->polyFillMode;
1824 if (dc->funcs->pSetPolyFillMode)
1825 if (!dc->funcs->pSetPolyFillMode( dc->physDev, mode ))
1826 ret = 0;
1827 if (ret)
1828 dc->polyFillMode = mode;
1829 release_dc_ptr( dc );
1830 return ret;
1834 /***********************************************************************
1835 * GetStretchBltMode (GDI32.@)
1837 INT WINAPI GetStretchBltMode( HDC hdc )
1839 INT ret = 0;
1840 DC * dc = get_dc_ptr( hdc );
1841 if (dc)
1843 ret = dc->stretchBltMode;
1844 release_dc_ptr( dc );
1846 return ret;
1850 /***********************************************************************
1851 * SetStretchBltMode (GDI32.@)
1853 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1855 INT ret;
1856 DC *dc;
1857 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1859 SetLastError(ERROR_INVALID_PARAMETER);
1860 return 0;
1862 if (!(dc = get_dc_ptr( hdc ))) return 0;
1863 ret = dc->stretchBltMode;
1864 if (dc->funcs->pSetStretchBltMode)
1865 if (!dc->funcs->pSetStretchBltMode( dc->physDev, mode ))
1866 ret = 0;
1867 if (ret)
1868 dc->stretchBltMode = mode;
1869 release_dc_ptr( dc );
1870 return ret;
1874 /***********************************************************************
1875 * GetMapMode (GDI32.@)
1877 INT WINAPI GetMapMode( HDC hdc )
1879 INT ret = 0;
1880 DC * dc = get_dc_ptr( hdc );
1881 if (dc)
1883 ret = dc->MapMode;
1884 release_dc_ptr( dc );
1886 return ret;
1890 /***********************************************************************
1891 * GetBrushOrgEx (GDI32.@)
1893 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1895 DC * dc = get_dc_ptr( hdc );
1896 if (!dc) return FALSE;
1897 pt->x = dc->brushOrgX;
1898 pt->y = dc->brushOrgY;
1899 release_dc_ptr( dc );
1900 return TRUE;
1904 /***********************************************************************
1905 * GetCurrentPositionEx (GDI32.@)
1907 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1909 DC * dc = get_dc_ptr( hdc );
1910 if (!dc) return FALSE;
1911 pt->x = dc->CursPosX;
1912 pt->y = dc->CursPosY;
1913 release_dc_ptr( dc );
1914 return TRUE;
1918 /***********************************************************************
1919 * GetViewportExtEx (GDI32.@)
1921 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1923 DC * dc = get_dc_ptr( hdc );
1924 if (!dc) return FALSE;
1925 size->cx = dc->vportExtX;
1926 size->cy = dc->vportExtY;
1927 release_dc_ptr( dc );
1928 return TRUE;
1932 /***********************************************************************
1933 * GetViewportOrgEx (GDI32.@)
1935 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1937 DC * dc = get_dc_ptr( hdc );
1938 if (!dc) return FALSE;
1939 pt->x = dc->vportOrgX;
1940 pt->y = dc->vportOrgY;
1941 release_dc_ptr( dc );
1942 return TRUE;
1946 /***********************************************************************
1947 * GetWindowExtEx (GDI32.@)
1949 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1951 DC * dc = get_dc_ptr( hdc );
1952 if (!dc) return FALSE;
1953 size->cx = dc->wndExtX;
1954 size->cy = dc->wndExtY;
1955 release_dc_ptr( dc );
1956 return TRUE;
1960 /***********************************************************************
1961 * GetWindowOrgEx (GDI32.@)
1963 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1965 DC * dc = get_dc_ptr( hdc );
1966 if (!dc) return FALSE;
1967 pt->x = dc->wndOrgX;
1968 pt->y = dc->wndOrgY;
1969 release_dc_ptr( dc );
1970 return TRUE;
1974 /***********************************************************************
1975 * InquireVisRgn (GDI.131)
1977 HRGN16 WINAPI InquireVisRgn16( HDC16 hdc )
1979 HRGN16 ret = 0;
1980 DC * dc = get_dc_ptr( HDC_32(hdc) );
1981 if (dc)
1983 ret = HRGN_16(dc->hVisRgn);
1984 release_dc_ptr( dc );
1986 return ret;
1990 /***********************************************************************
1991 * GetClipRgn (GDI.173)
1993 HRGN16 WINAPI GetClipRgn16( HDC16 hdc )
1995 HRGN16 ret = 0;
1996 DC * dc = get_dc_ptr( HDC_32(hdc) );
1997 if (dc)
1999 ret = HRGN_16(dc->hClipRgn);
2000 release_dc_ptr( dc );
2002 return ret;
2006 /***********************************************************************
2007 * GetLayout (GDI32.@)
2009 * Gets left->right or right->left text layout flags of a dc.
2012 DWORD WINAPI GetLayout(HDC hdc)
2014 DWORD layout = GDI_ERROR;
2016 DC * dc = get_dc_ptr( hdc );
2017 if (dc)
2019 layout = dc->layout;
2020 release_dc_ptr( dc );
2023 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
2025 return layout;
2028 /***********************************************************************
2029 * SetLayout (GDI32.@)
2031 * Sets left->right or right->left text layout flags of a dc.
2034 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
2036 DWORD oldlayout = GDI_ERROR;
2038 DC * dc = get_dc_ptr( hdc );
2039 if (dc)
2041 oldlayout = dc->layout;
2042 dc->layout = layout;
2043 release_dc_ptr( dc );
2046 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
2048 return oldlayout;
2051 /***********************************************************************
2052 * GetDCBrushColor (GDI32.@)
2054 * Retrieves the current brush color for the specified device
2055 * context (DC).
2058 COLORREF WINAPI GetDCBrushColor(HDC hdc)
2060 DC *dc;
2061 COLORREF dcBrushColor = CLR_INVALID;
2063 TRACE("hdc(%p)\n", hdc);
2065 dc = get_dc_ptr( hdc );
2066 if (dc)
2068 dcBrushColor = dc->dcBrushColor;
2069 release_dc_ptr( dc );
2072 return dcBrushColor;
2075 /***********************************************************************
2076 * SetDCBrushColor (GDI32.@)
2078 * Sets the current device context (DC) brush color to the specified
2079 * color value. If the device cannot represent the specified color
2080 * value, the color is set to the nearest physical color.
2083 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
2085 DC *dc;
2086 COLORREF oldClr = CLR_INVALID;
2088 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2090 dc = get_dc_ptr( hdc );
2091 if (dc)
2093 if (dc->funcs->pSetDCBrushColor)
2094 crColor = dc->funcs->pSetDCBrushColor( dc->physDev, crColor );
2095 else if (dc->hBrush == GetStockObject( DC_BRUSH ))
2097 /* If DC_BRUSH is selected, update driver pen color */
2098 HBRUSH hBrush = CreateSolidBrush( crColor );
2099 dc->funcs->pSelectBrush( dc->physDev, hBrush );
2100 DeleteObject( hBrush );
2103 if (crColor != CLR_INVALID)
2105 oldClr = dc->dcBrushColor;
2106 dc->dcBrushColor = crColor;
2109 release_dc_ptr( dc );
2112 return oldClr;
2115 /***********************************************************************
2116 * GetDCPenColor (GDI32.@)
2118 * Retrieves the current pen color for the specified device
2119 * context (DC).
2122 COLORREF WINAPI GetDCPenColor(HDC hdc)
2124 DC *dc;
2125 COLORREF dcPenColor = CLR_INVALID;
2127 TRACE("hdc(%p)\n", hdc);
2129 dc = get_dc_ptr( hdc );
2130 if (dc)
2132 dcPenColor = dc->dcPenColor;
2133 release_dc_ptr( dc );
2136 return dcPenColor;
2139 /***********************************************************************
2140 * SetDCPenColor (GDI32.@)
2142 * Sets the current device context (DC) pen color to the specified
2143 * color value. If the device cannot represent the specified color
2144 * value, the color is set to the nearest physical color.
2147 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
2149 DC *dc;
2150 COLORREF oldClr = CLR_INVALID;
2152 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2154 dc = get_dc_ptr( hdc );
2155 if (dc)
2157 if (dc->funcs->pSetDCPenColor)
2158 crColor = dc->funcs->pSetDCPenColor( dc->physDev, crColor );
2159 else if (dc->hPen == GetStockObject( DC_PEN ))
2161 /* If DC_PEN is selected, update the driver pen color */
2162 LOGPEN logpen = { PS_SOLID, { 0, 0 }, crColor };
2163 HPEN hPen = CreatePenIndirect( &logpen );
2164 dc->funcs->pSelectPen( dc->physDev, hPen );
2165 DeleteObject( hPen );
2168 if (crColor != CLR_INVALID)
2170 oldClr = dc->dcPenColor;
2171 dc->dcPenColor = crColor;
2174 release_dc_ptr( dc );
2177 return oldClr;
2180 /***********************************************************************
2181 * CancelDC (GDI32.@)
2183 BOOL WINAPI CancelDC(HDC hdc)
2185 FIXME("stub\n");
2186 return TRUE;
2189 /***********************************************************************
2190 * SetVirtualResolution (GDI32.@)
2192 * Undocumented on msdn. Called when PowerPoint XP saves a file.
2194 DWORD WINAPI SetVirtualResolution(HDC hdc, DWORD dw2, DWORD dw3, DWORD dw4, DWORD dw5)
2196 FIXME("(%p %08x %08x %08x %08x): stub!\n", hdc, dw2, dw3, dw4, dw5);
2197 return FALSE;
2200 /*******************************************************************
2201 * GetMiterLimit [GDI32.@]
2205 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2207 BOOL bRet = FALSE;
2208 DC *dc;
2210 TRACE("(%p,%p)\n", hdc, peLimit);
2212 dc = get_dc_ptr( hdc );
2213 if (dc)
2215 if (peLimit)
2216 *peLimit = dc->miterLimit;
2218 release_dc_ptr( dc );
2219 bRet = TRUE;
2221 return bRet;
2224 /*******************************************************************
2225 * SetMiterLimit [GDI32.@]
2229 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2231 BOOL bRet = FALSE;
2232 DC *dc;
2234 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2236 dc = get_dc_ptr( hdc );
2237 if (dc)
2239 if (peOldLimit)
2240 *peOldLimit = dc->miterLimit;
2241 dc->miterLimit = eNewLimit;
2242 release_dc_ptr( dc );
2243 bRet = TRUE;
2245 return bRet;
2248 /*******************************************************************
2249 * GdiIsMetaPrintDC [GDI32.@]
2251 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2253 FIXME("%p\n", hdc);
2254 return FALSE;
2257 /*******************************************************************
2258 * GdiIsMetaFileDC [GDI32.@]
2260 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2262 TRACE("%p\n", hdc);
2264 switch( GetObjectType( hdc ) )
2266 case OBJ_METADC:
2267 case OBJ_ENHMETADC:
2268 return TRUE;
2270 return FALSE;
2273 /*******************************************************************
2274 * GdiIsPlayMetafileDC [GDI32.@]
2276 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2278 FIXME("%p\n", hdc);
2279 return FALSE;