gdi32: Reselect objects into the DC only if scaling factors change.
[wine/hacks.git] / dlls / gdi32 / dc.c
blobcb4de19cff7be543a4b034e3500098cdb7e9aae5
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 FLOAT 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 FLOAT scaleX, scaleY;
295 /* Construct a transformation to do the window-to-viewport conversion */
296 scaleX = (FLOAT)dc->vportExtX / (FLOAT)dc->wndExtX;
297 scaleY = (FLOAT)dc->vportExtY / (FLOAT)dc->wndExtY;
298 xformWnd2Vport.eM11 = scaleX;
299 xformWnd2Vport.eM12 = 0.0;
300 xformWnd2Vport.eM21 = 0.0;
301 xformWnd2Vport.eM22 = scaleY;
302 xformWnd2Vport.eDx = (FLOAT)dc->vportOrgX -
303 scaleX * (FLOAT)dc->wndOrgX;
304 xformWnd2Vport.eDy = (FLOAT)dc->vportOrgY -
305 scaleY * (FLOAT)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 (oldworld2vport.eM11 != dc->xformWorld2Vport.eM11 ||
319 oldworld2vport.eM22 != dc->xformWorld2Vport.eM22)
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 if (dc->funcs->pSetWorldTransform)
1243 ret = dc->funcs->pSetWorldTransform(dc->physDev, xform);
1244 if (!ret) goto done;
1247 dc->xformWorld2Wnd = *xform;
1248 DC_UpdateXforms( dc );
1249 ret = TRUE;
1250 done:
1251 release_dc_ptr( dc );
1252 return ret;
1256 /****************************************************************************
1257 * ModifyWorldTransform [GDI32.@]
1258 * Modifies the world transformation for a device context.
1260 * PARAMS
1261 * hdc [I] Handle to device context
1262 * xform [I] XFORM structure that will be used to modify the world
1263 * transformation
1264 * iMode [I] Specifies in what way to modify the world transformation
1265 * Possible values:
1266 * MWT_IDENTITY
1267 * Resets the world transformation to the identity matrix.
1268 * The parameter xform is ignored.
1269 * MWT_LEFTMULTIPLY
1270 * Multiplies xform into the world transformation matrix from
1271 * the left.
1272 * MWT_RIGHTMULTIPLY
1273 * Multiplies xform into the world transformation matrix from
1274 * the right.
1276 * RETURNS
1277 * Success: TRUE.
1278 * Failure: FALSE. Use GetLastError() to determine the cause.
1280 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1281 DWORD iMode )
1283 BOOL ret = FALSE;
1284 DC *dc = get_dc_ptr( hdc );
1286 /* Check for illegal parameters */
1287 if (!dc) return FALSE;
1288 if (!xform && iMode != MWT_IDENTITY) goto done;
1290 /* Check that graphics mode is GM_ADVANCED */
1291 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1293 if (dc->funcs->pModifyWorldTransform)
1295 ret = dc->funcs->pModifyWorldTransform(dc->physDev, xform, iMode);
1296 if (!ret) goto done;
1299 switch (iMode)
1301 case MWT_IDENTITY:
1302 dc->xformWorld2Wnd.eM11 = 1.0f;
1303 dc->xformWorld2Wnd.eM12 = 0.0f;
1304 dc->xformWorld2Wnd.eM21 = 0.0f;
1305 dc->xformWorld2Wnd.eM22 = 1.0f;
1306 dc->xformWorld2Wnd.eDx = 0.0f;
1307 dc->xformWorld2Wnd.eDy = 0.0f;
1308 break;
1309 case MWT_LEFTMULTIPLY:
1310 CombineTransform( &dc->xformWorld2Wnd, xform,
1311 &dc->xformWorld2Wnd );
1312 break;
1313 case MWT_RIGHTMULTIPLY:
1314 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1315 xform );
1316 break;
1317 default:
1318 goto done;
1321 DC_UpdateXforms( dc );
1322 ret = TRUE;
1323 done:
1324 release_dc_ptr( dc );
1325 return ret;
1329 /****************************************************************************
1330 * CombineTransform [GDI32.@]
1331 * Combines two transformation matrices.
1333 * PARAMS
1334 * xformResult [O] Stores the result of combining the two matrices
1335 * xform1 [I] Specifies the first matrix to apply
1336 * xform2 [I] Specifies the second matrix to apply
1338 * REMARKS
1339 * The same matrix can be passed in for more than one of the parameters.
1341 * RETURNS
1342 * Success: TRUE.
1343 * Failure: FALSE. Use GetLastError() to determine the cause.
1345 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1346 const XFORM *xform2 )
1348 XFORM xformTemp;
1350 /* Check for illegal parameters */
1351 if (!xformResult || !xform1 || !xform2)
1352 return FALSE;
1354 /* Create the result in a temporary XFORM, since xformResult may be
1355 * equal to xform1 or xform2 */
1356 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1357 xform1->eM12 * xform2->eM21;
1358 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1359 xform1->eM12 * xform2->eM22;
1360 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1361 xform1->eM22 * xform2->eM21;
1362 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1363 xform1->eM22 * xform2->eM22;
1364 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1365 xform1->eDy * xform2->eM21 +
1366 xform2->eDx;
1367 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1368 xform1->eDy * xform2->eM22 +
1369 xform2->eDy;
1371 /* Copy the result to xformResult */
1372 *xformResult = xformTemp;
1374 return TRUE;
1378 /***********************************************************************
1379 * SetDCHook (GDI32.@)
1381 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1383 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1385 DC *dc = get_dc_ptr( hdc );
1387 if (!dc) return FALSE;
1389 if (!(dc->flags & DC_SAVED))
1391 dc->dwHookData = dwHookData;
1392 dc->hookThunk = hookProc;
1394 release_dc_ptr( dc );
1395 return TRUE;
1399 /***********************************************************************
1400 * GetDCHook (GDI32.@)
1402 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1404 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1406 DC *dc = get_dc_ptr( hdc );
1407 DWORD_PTR ret;
1409 if (!dc) return 0;
1410 if (proc) *proc = dc->hookThunk;
1411 ret = dc->dwHookData;
1412 release_dc_ptr( dc );
1413 return ret;
1417 /* relay function to call the 16-bit DC hook proc */
1418 static BOOL WINAPI call_dc_hook16( HDC hdc, WORD code, DWORD_PTR data, LPARAM lParam )
1420 WORD args[6];
1421 DWORD ret = 0;
1422 DC *dc = get_dc_ptr( hdc );
1424 if (!dc) return FALSE;
1425 if (dc->hookProc)
1427 args[5] = HDC_16(hdc);
1428 args[4] = code;
1429 args[3] = HIWORD(data);
1430 args[2] = LOWORD(data);
1431 args[1] = HIWORD(lParam);
1432 args[0] = LOWORD(lParam);
1433 WOWCallback16Ex( (DWORD)dc->hookProc, WCB16_PASCAL, sizeof(args), args, &ret );
1435 release_dc_ptr( dc );
1436 return LOWORD(ret);
1439 /***********************************************************************
1440 * SetDCHook (GDI.190)
1442 BOOL16 WINAPI SetDCHook16( HDC16 hdc16, FARPROC16 hookProc, DWORD dwHookData )
1444 DC *dc = get_dc_ptr( HDC_32(hdc16) );
1446 if (!dc) return FALSE;
1447 if (!(dc->flags & DC_SAVED))
1449 dc->dwHookData = dwHookData;
1450 dc->hookThunk = call_dc_hook16;
1451 dc->hookProc = hookProc;
1453 release_dc_ptr( dc );
1454 return TRUE;
1458 /***********************************************************************
1459 * GetDCHook (GDI.191)
1461 DWORD WINAPI GetDCHook16( HDC16 hdc16, FARPROC16 *phookProc )
1463 HDC hdc = HDC_32( hdc16 );
1464 DC *dc = get_dc_ptr( hdc );
1465 DWORD ret;
1467 if (!dc) return 0;
1468 *phookProc = dc->hookProc;
1469 ret = dc->dwHookData;
1470 release_dc_ptr( dc );
1471 return ret;
1475 /***********************************************************************
1476 * SetHookFlags (GDI32.@)
1478 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1480 WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
1482 DC *dc = get_dc_obj( hdc ); /* not get_dc_ptr, this needs to work from any thread */
1483 LONG ret = 0;
1485 if (!dc) return 0;
1487 /* "Undocumented Windows" info is slightly confusing. */
1489 TRACE("hDC %p, flags %04x\n",hdc,flags);
1491 if (flags & DCHF_INVALIDATEVISRGN)
1492 ret = InterlockedExchange( &dc->dirty, 1 );
1493 else if (flags & DCHF_VALIDATEVISRGN || !flags)
1494 ret = InterlockedExchange( &dc->dirty, 0 );
1496 GDI_ReleaseObj( dc );
1497 return ret;
1500 /***********************************************************************
1501 * SetICMMode (GDI32.@)
1503 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1505 /*FIXME: Assume that ICM is always off, and cannot be turned on */
1506 if (iEnableICM == ICM_OFF) return ICM_OFF;
1507 if (iEnableICM == ICM_ON) return 0;
1508 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1509 return 0;
1512 /***********************************************************************
1513 * GetDeviceGammaRamp (GDI32.@)
1515 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1517 BOOL ret = FALSE;
1518 DC *dc = get_dc_ptr( hDC );
1520 if( dc )
1522 if (dc->funcs->pGetDeviceGammaRamp)
1523 ret = dc->funcs->pGetDeviceGammaRamp(dc->physDev, ptr);
1524 release_dc_ptr( dc );
1526 return ret;
1529 /***********************************************************************
1530 * SetDeviceGammaRamp (GDI32.@)
1532 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1534 BOOL ret = FALSE;
1535 DC *dc = get_dc_ptr( hDC );
1537 if( dc )
1539 if (dc->funcs->pSetDeviceGammaRamp)
1540 ret = dc->funcs->pSetDeviceGammaRamp(dc->physDev, ptr);
1541 release_dc_ptr( dc );
1543 return ret;
1546 /***********************************************************************
1547 * GetColorSpace (GDI32.@)
1549 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1551 /*FIXME Need to to whatever GetColorSpace actually does */
1552 return 0;
1555 /***********************************************************************
1556 * CreateColorSpaceA (GDI32.@)
1558 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1560 FIXME( "stub\n" );
1561 return 0;
1564 /***********************************************************************
1565 * CreateColorSpaceW (GDI32.@)
1567 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1569 FIXME( "stub\n" );
1570 return 0;
1573 /***********************************************************************
1574 * DeleteColorSpace (GDI32.@)
1576 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1578 FIXME( "stub\n" );
1580 return TRUE;
1583 /***********************************************************************
1584 * SetColorSpace (GDI32.@)
1586 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1588 FIXME( "stub\n" );
1590 return hColorSpace;
1593 /***********************************************************************
1594 * GetBoundsRect (GDI32.@)
1596 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1598 UINT ret;
1599 DC *dc = get_dc_ptr( hdc );
1601 if ( !dc ) return 0;
1603 if (rect) *rect = dc->BoundsRect;
1605 ret = ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1607 if (flags & DCB_RESET)
1609 dc->BoundsRect.left = 0;
1610 dc->BoundsRect.top = 0;
1611 dc->BoundsRect.right = 0;
1612 dc->BoundsRect.bottom = 0;
1613 dc->flags &= ~DC_BOUNDS_SET;
1615 release_dc_ptr( dc );
1616 return ret;
1620 /***********************************************************************
1621 * SetBoundsRect (GDI32.@)
1623 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1625 UINT ret;
1626 DC *dc;
1628 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1629 if (!(dc = get_dc_ptr( hdc ))) return 0;
1631 ret = ((dc->flags & DC_BOUNDS_ENABLE) ? DCB_ENABLE : DCB_DISABLE) |
1632 ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1634 if (flags & DCB_RESET)
1636 dc->BoundsRect.left = 0;
1637 dc->BoundsRect.top = 0;
1638 dc->BoundsRect.right = 0;
1639 dc->BoundsRect.bottom = 0;
1640 dc->flags &= ~DC_BOUNDS_SET;
1643 if ((flags & DCB_ACCUMULATE) && rect && rect->left < rect->right && rect->top < rect->bottom)
1645 if (dc->flags & DC_BOUNDS_SET)
1647 dc->BoundsRect.left = min( dc->BoundsRect.left, rect->left );
1648 dc->BoundsRect.top = min( dc->BoundsRect.top, rect->top );
1649 dc->BoundsRect.right = max( dc->BoundsRect.right, rect->right );
1650 dc->BoundsRect.bottom = max( dc->BoundsRect.bottom, rect->bottom );
1652 else
1654 dc->BoundsRect = *rect;
1655 dc->flags |= DC_BOUNDS_SET;
1659 if (flags & DCB_ENABLE) dc->flags |= DC_BOUNDS_ENABLE;
1660 if (flags & DCB_DISABLE) dc->flags &= ~DC_BOUNDS_ENABLE;
1662 release_dc_ptr( dc );
1663 return ret;
1667 /***********************************************************************
1668 * GetRelAbs (GDI32.@)
1670 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1672 INT ret = 0;
1673 DC *dc = get_dc_ptr( hdc );
1674 if (dc)
1676 ret = dc->relAbsMode;
1677 release_dc_ptr( dc );
1679 return ret;
1685 /***********************************************************************
1686 * GetBkMode (GDI32.@)
1688 INT WINAPI GetBkMode( HDC hdc )
1690 INT ret = 0;
1691 DC * dc = get_dc_ptr( hdc );
1692 if (dc)
1694 ret = dc->backgroundMode;
1695 release_dc_ptr( dc );
1697 return ret;
1701 /***********************************************************************
1702 * SetBkMode (GDI32.@)
1704 INT WINAPI SetBkMode( HDC hdc, INT mode )
1706 INT ret;
1707 DC *dc;
1708 if ((mode <= 0) || (mode > BKMODE_LAST))
1710 SetLastError(ERROR_INVALID_PARAMETER);
1711 return 0;
1713 if (!(dc = get_dc_ptr( hdc ))) return 0;
1715 ret = dc->backgroundMode;
1716 if (dc->funcs->pSetBkMode)
1717 if (!dc->funcs->pSetBkMode( dc->physDev, mode ))
1718 ret = 0;
1719 if (ret)
1720 dc->backgroundMode = mode;
1721 release_dc_ptr( dc );
1722 return ret;
1726 /***********************************************************************
1727 * GetROP2 (GDI32.@)
1729 INT WINAPI GetROP2( HDC hdc )
1731 INT ret = 0;
1732 DC * dc = get_dc_ptr( hdc );
1733 if (dc)
1735 ret = dc->ROPmode;
1736 release_dc_ptr( dc );
1738 return ret;
1742 /***********************************************************************
1743 * SetROP2 (GDI32.@)
1745 INT WINAPI SetROP2( HDC hdc, INT mode )
1747 INT ret;
1748 DC *dc;
1749 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1751 SetLastError(ERROR_INVALID_PARAMETER);
1752 return 0;
1754 if (!(dc = get_dc_ptr( hdc ))) return 0;
1755 ret = dc->ROPmode;
1756 if (dc->funcs->pSetROP2)
1757 if (!dc->funcs->pSetROP2( dc->physDev, mode ))
1758 ret = 0;
1759 if (ret)
1760 dc->ROPmode = mode;
1761 release_dc_ptr( dc );
1762 return ret;
1766 /***********************************************************************
1767 * SetRelAbs (GDI32.@)
1769 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1771 INT ret;
1772 DC *dc;
1773 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1775 SetLastError(ERROR_INVALID_PARAMETER);
1776 return 0;
1778 if (!(dc = get_dc_ptr( hdc ))) return 0;
1779 if (dc->funcs->pSetRelAbs)
1780 ret = dc->funcs->pSetRelAbs( dc->physDev, mode );
1781 else
1783 ret = dc->relAbsMode;
1784 dc->relAbsMode = mode;
1786 release_dc_ptr( dc );
1787 return ret;
1791 /***********************************************************************
1792 * GetPolyFillMode (GDI32.@)
1794 INT WINAPI GetPolyFillMode( HDC hdc )
1796 INT ret = 0;
1797 DC * dc = get_dc_ptr( hdc );
1798 if (dc)
1800 ret = dc->polyFillMode;
1801 release_dc_ptr( dc );
1803 return ret;
1807 /***********************************************************************
1808 * SetPolyFillMode (GDI32.@)
1810 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1812 INT ret;
1813 DC *dc;
1814 if ((mode <= 0) || (mode > POLYFILL_LAST))
1816 SetLastError(ERROR_INVALID_PARAMETER);
1817 return 0;
1819 if (!(dc = get_dc_ptr( hdc ))) return 0;
1820 ret = dc->polyFillMode;
1821 if (dc->funcs->pSetPolyFillMode)
1822 if (!dc->funcs->pSetPolyFillMode( dc->physDev, mode ))
1823 ret = 0;
1824 if (ret)
1825 dc->polyFillMode = mode;
1826 release_dc_ptr( dc );
1827 return ret;
1831 /***********************************************************************
1832 * GetStretchBltMode (GDI32.@)
1834 INT WINAPI GetStretchBltMode( HDC hdc )
1836 INT ret = 0;
1837 DC * dc = get_dc_ptr( hdc );
1838 if (dc)
1840 ret = dc->stretchBltMode;
1841 release_dc_ptr( dc );
1843 return ret;
1847 /***********************************************************************
1848 * SetStretchBltMode (GDI32.@)
1850 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1852 INT ret;
1853 DC *dc;
1854 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1856 SetLastError(ERROR_INVALID_PARAMETER);
1857 return 0;
1859 if (!(dc = get_dc_ptr( hdc ))) return 0;
1860 ret = dc->stretchBltMode;
1861 if (dc->funcs->pSetStretchBltMode)
1862 if (!dc->funcs->pSetStretchBltMode( dc->physDev, mode ))
1863 ret = 0;
1864 if (ret)
1865 dc->stretchBltMode = mode;
1866 release_dc_ptr( dc );
1867 return ret;
1871 /***********************************************************************
1872 * GetMapMode (GDI32.@)
1874 INT WINAPI GetMapMode( HDC hdc )
1876 INT ret = 0;
1877 DC * dc = get_dc_ptr( hdc );
1878 if (dc)
1880 ret = dc->MapMode;
1881 release_dc_ptr( dc );
1883 return ret;
1887 /***********************************************************************
1888 * GetBrushOrgEx (GDI32.@)
1890 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1892 DC * dc = get_dc_ptr( hdc );
1893 if (!dc) return FALSE;
1894 pt->x = dc->brushOrgX;
1895 pt->y = dc->brushOrgY;
1896 release_dc_ptr( dc );
1897 return TRUE;
1901 /***********************************************************************
1902 * GetCurrentPositionEx (GDI32.@)
1904 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1906 DC * dc = get_dc_ptr( hdc );
1907 if (!dc) return FALSE;
1908 pt->x = dc->CursPosX;
1909 pt->y = dc->CursPosY;
1910 release_dc_ptr( dc );
1911 return TRUE;
1915 /***********************************************************************
1916 * GetViewportExtEx (GDI32.@)
1918 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1920 DC * dc = get_dc_ptr( hdc );
1921 if (!dc) return FALSE;
1922 size->cx = dc->vportExtX;
1923 size->cy = dc->vportExtY;
1924 release_dc_ptr( dc );
1925 return TRUE;
1929 /***********************************************************************
1930 * GetViewportOrgEx (GDI32.@)
1932 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1934 DC * dc = get_dc_ptr( hdc );
1935 if (!dc) return FALSE;
1936 pt->x = dc->vportOrgX;
1937 pt->y = dc->vportOrgY;
1938 release_dc_ptr( dc );
1939 return TRUE;
1943 /***********************************************************************
1944 * GetWindowExtEx (GDI32.@)
1946 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1948 DC * dc = get_dc_ptr( hdc );
1949 if (!dc) return FALSE;
1950 size->cx = dc->wndExtX;
1951 size->cy = dc->wndExtY;
1952 release_dc_ptr( dc );
1953 return TRUE;
1957 /***********************************************************************
1958 * GetWindowOrgEx (GDI32.@)
1960 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1962 DC * dc = get_dc_ptr( hdc );
1963 if (!dc) return FALSE;
1964 pt->x = dc->wndOrgX;
1965 pt->y = dc->wndOrgY;
1966 release_dc_ptr( dc );
1967 return TRUE;
1971 /***********************************************************************
1972 * InquireVisRgn (GDI.131)
1974 HRGN16 WINAPI InquireVisRgn16( HDC16 hdc )
1976 HRGN16 ret = 0;
1977 DC * dc = get_dc_ptr( HDC_32(hdc) );
1978 if (dc)
1980 ret = HRGN_16(dc->hVisRgn);
1981 release_dc_ptr( dc );
1983 return ret;
1987 /***********************************************************************
1988 * GetClipRgn (GDI.173)
1990 HRGN16 WINAPI GetClipRgn16( HDC16 hdc )
1992 HRGN16 ret = 0;
1993 DC * dc = get_dc_ptr( HDC_32(hdc) );
1994 if (dc)
1996 ret = HRGN_16(dc->hClipRgn);
1997 release_dc_ptr( dc );
1999 return ret;
2003 /***********************************************************************
2004 * GetLayout (GDI32.@)
2006 * Gets left->right or right->left text layout flags of a dc.
2009 DWORD WINAPI GetLayout(HDC hdc)
2011 DWORD layout = GDI_ERROR;
2013 DC * dc = get_dc_ptr( hdc );
2014 if (dc)
2016 layout = dc->layout;
2017 release_dc_ptr( dc );
2020 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
2022 return layout;
2025 /***********************************************************************
2026 * SetLayout (GDI32.@)
2028 * Sets left->right or right->left text layout flags of a dc.
2031 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
2033 DWORD oldlayout = GDI_ERROR;
2035 DC * dc = get_dc_ptr( hdc );
2036 if (dc)
2038 oldlayout = dc->layout;
2039 dc->layout = layout;
2040 release_dc_ptr( dc );
2043 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
2045 return oldlayout;
2048 /***********************************************************************
2049 * GetDCBrushColor (GDI32.@)
2051 * Retrieves the current brush color for the specified device
2052 * context (DC).
2055 COLORREF WINAPI GetDCBrushColor(HDC hdc)
2057 DC *dc;
2058 COLORREF dcBrushColor = CLR_INVALID;
2060 TRACE("hdc(%p)\n", hdc);
2062 dc = get_dc_ptr( hdc );
2063 if (dc)
2065 dcBrushColor = dc->dcBrushColor;
2066 release_dc_ptr( dc );
2069 return dcBrushColor;
2072 /***********************************************************************
2073 * SetDCBrushColor (GDI32.@)
2075 * Sets the current device context (DC) brush color to the specified
2076 * color value. If the device cannot represent the specified color
2077 * value, the color is set to the nearest physical color.
2080 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
2082 DC *dc;
2083 COLORREF oldClr = CLR_INVALID;
2085 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2087 dc = get_dc_ptr( hdc );
2088 if (dc)
2090 if (dc->funcs->pSetDCBrushColor)
2091 crColor = dc->funcs->pSetDCBrushColor( dc->physDev, crColor );
2092 else if (dc->hBrush == GetStockObject( DC_BRUSH ))
2094 /* If DC_BRUSH is selected, update driver pen color */
2095 HBRUSH hBrush = CreateSolidBrush( crColor );
2096 dc->funcs->pSelectBrush( dc->physDev, hBrush );
2097 DeleteObject( hBrush );
2100 if (crColor != CLR_INVALID)
2102 oldClr = dc->dcBrushColor;
2103 dc->dcBrushColor = crColor;
2106 release_dc_ptr( dc );
2109 return oldClr;
2112 /***********************************************************************
2113 * GetDCPenColor (GDI32.@)
2115 * Retrieves the current pen color for the specified device
2116 * context (DC).
2119 COLORREF WINAPI GetDCPenColor(HDC hdc)
2121 DC *dc;
2122 COLORREF dcPenColor = CLR_INVALID;
2124 TRACE("hdc(%p)\n", hdc);
2126 dc = get_dc_ptr( hdc );
2127 if (dc)
2129 dcPenColor = dc->dcPenColor;
2130 release_dc_ptr( dc );
2133 return dcPenColor;
2136 /***********************************************************************
2137 * SetDCPenColor (GDI32.@)
2139 * Sets the current device context (DC) pen color to the specified
2140 * color value. If the device cannot represent the specified color
2141 * value, the color is set to the nearest physical color.
2144 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
2146 DC *dc;
2147 COLORREF oldClr = CLR_INVALID;
2149 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2151 dc = get_dc_ptr( hdc );
2152 if (dc)
2154 if (dc->funcs->pSetDCPenColor)
2155 crColor = dc->funcs->pSetDCPenColor( dc->physDev, crColor );
2156 else if (dc->hPen == GetStockObject( DC_PEN ))
2158 /* If DC_PEN is selected, update the driver pen color */
2159 LOGPEN logpen = { PS_SOLID, { 0, 0 }, crColor };
2160 HPEN hPen = CreatePenIndirect( &logpen );
2161 dc->funcs->pSelectPen( dc->physDev, hPen );
2162 DeleteObject( hPen );
2165 if (crColor != CLR_INVALID)
2167 oldClr = dc->dcPenColor;
2168 dc->dcPenColor = crColor;
2171 release_dc_ptr( dc );
2174 return oldClr;
2177 /***********************************************************************
2178 * CancelDC (GDI32.@)
2180 BOOL WINAPI CancelDC(HDC hdc)
2182 FIXME("stub\n");
2183 return TRUE;
2186 /***********************************************************************
2187 * SetVirtualResolution (GDI32.@)
2189 * Undocumented on msdn. Called when PowerPoint XP saves a file.
2191 DWORD WINAPI SetVirtualResolution(HDC hdc, DWORD dw2, DWORD dw3, DWORD dw4, DWORD dw5)
2193 FIXME("(%p %08x %08x %08x %08x): stub!\n", hdc, dw2, dw3, dw4, dw5);
2194 return FALSE;
2197 /*******************************************************************
2198 * GetMiterLimit [GDI32.@]
2202 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2204 BOOL bRet = FALSE;
2205 DC *dc;
2207 TRACE("(%p,%p)\n", hdc, peLimit);
2209 dc = get_dc_ptr( hdc );
2210 if (dc)
2212 if (peLimit)
2213 *peLimit = dc->miterLimit;
2215 release_dc_ptr( dc );
2216 bRet = TRUE;
2218 return bRet;
2221 /*******************************************************************
2222 * SetMiterLimit [GDI32.@]
2226 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2228 BOOL bRet = FALSE;
2229 DC *dc;
2231 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2233 dc = get_dc_ptr( hdc );
2234 if (dc)
2236 if (peOldLimit)
2237 *peOldLimit = dc->miterLimit;
2238 dc->miterLimit = eNewLimit;
2239 release_dc_ptr( dc );
2240 bRet = TRUE;
2242 return bRet;
2245 /*******************************************************************
2246 * GdiIsMetaPrintDC [GDI32.@]
2248 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2250 FIXME("%p\n", hdc);
2251 return FALSE;
2254 /*******************************************************************
2255 * GdiIsMetaFileDC [GDI32.@]
2257 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2259 TRACE("%p\n", hdc);
2261 switch( GetObjectType( hdc ) )
2263 case OBJ_METADC:
2264 case OBJ_ENHMETADC:
2265 return TRUE;
2267 return FALSE;
2270 /*******************************************************************
2271 * GdiIsPlayMetafileDC [GDI32.@]
2273 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2275 FIXME("%p\n", hdc);
2276 return FALSE;