gdi32: Add a couple of SetGraphicsMode/SetWorldTransform tests, make them pass under...
[wine/multimedia.git] / dlls / gdi32 / dc.c
blobaa8fbc52ca7e17bd06d68fdfcc4bc7cdc5f588b4
1 /*
2 * GDI Device Context functions
4 * Copyright 1993 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winreg.h"
31 #include "winternl.h"
32 #include "winerror.h"
33 #include "wownt32.h"
34 #include "gdi_private.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(dc);
40 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
42 static BOOL DC_DeleteObject( HGDIOBJ handle );
44 static const struct gdi_obj_funcs dc_funcs =
46 NULL, /* pSelectObject */
47 NULL, /* pGetObjectA */
48 NULL, /* pGetObjectW */
49 NULL, /* pUnrealizeObject */
50 DC_DeleteObject /* pDeleteObject */
54 static inline DC *get_dc_obj( HDC hdc )
56 DC *dc = GDI_GetObjPtr( hdc, 0 );
57 if (!dc) return NULL;
59 if ((dc->header.type != OBJ_DC) &&
60 (dc->header.type != OBJ_MEMDC) &&
61 (dc->header.type != OBJ_METADC) &&
62 (dc->header.type != OBJ_ENHMETADC))
64 GDI_ReleaseObj( hdc );
65 SetLastError( ERROR_INVALID_HANDLE );
66 dc = NULL;
68 return dc;
72 /***********************************************************************
73 * alloc_dc_ptr
75 DC *alloc_dc_ptr( const DC_FUNCTIONS *funcs, WORD magic )
77 DC *dc;
79 if (!(dc = HeapAlloc( GetProcessHeap(), 0, sizeof(*dc) ))) return NULL;
81 dc->funcs = funcs;
82 dc->physDev = NULL;
83 dc->thread = GetCurrentThreadId();
84 dc->refcount = 1;
85 dc->dirty = 0;
86 dc->saveLevel = 0;
87 dc->saved_dc = 0;
88 dc->dwHookData = 0;
89 dc->hookProc = NULL;
90 dc->hookThunk = NULL;
91 dc->wndOrgX = 0;
92 dc->wndOrgY = 0;
93 dc->wndExtX = 1;
94 dc->wndExtY = 1;
95 dc->vportOrgX = 0;
96 dc->vportOrgY = 0;
97 dc->vportExtX = 1;
98 dc->vportExtY = 1;
99 dc->miterLimit = 10.0f; /* 10.0 is the default, from MSDN */
100 dc->flags = 0;
101 dc->layout = 0;
102 dc->hClipRgn = 0;
103 dc->hMetaRgn = 0;
104 dc->hMetaClipRgn = 0;
105 dc->hVisRgn = 0;
106 dc->hPen = GDI_inc_ref_count( GetStockObject( BLACK_PEN ));
107 dc->hBrush = GDI_inc_ref_count( GetStockObject( WHITE_BRUSH ));
108 dc->hFont = GDI_inc_ref_count( GetStockObject( SYSTEM_FONT ));
109 dc->hBitmap = 0;
110 dc->hDevice = 0;
111 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
112 dc->gdiFont = 0;
113 dc->ROPmode = R2_COPYPEN;
114 dc->polyFillMode = ALTERNATE;
115 dc->stretchBltMode = BLACKONWHITE;
116 dc->relAbsMode = ABSOLUTE;
117 dc->backgroundMode = OPAQUE;
118 dc->backgroundColor = RGB( 255, 255, 255 );
119 dc->dcBrushColor = RGB( 255, 255, 255 );
120 dc->dcPenColor = RGB( 0, 0, 0 );
121 dc->textColor = RGB( 0, 0, 0 );
122 dc->brushOrgX = 0;
123 dc->brushOrgY = 0;
124 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
125 dc->charExtra = 0;
126 dc->breakExtra = 0;
127 dc->breakRem = 0;
128 dc->MapMode = MM_TEXT;
129 dc->GraphicsMode = GM_COMPATIBLE;
130 dc->pAbortProc = NULL;
131 dc->CursPosX = 0;
132 dc->CursPosY = 0;
133 dc->ArcDirection = AD_COUNTERCLOCKWISE;
134 dc->xformWorld2Wnd.eM11 = 1.0f;
135 dc->xformWorld2Wnd.eM12 = 0.0f;
136 dc->xformWorld2Wnd.eM21 = 0.0f;
137 dc->xformWorld2Wnd.eM22 = 1.0f;
138 dc->xformWorld2Wnd.eDx = 0.0f;
139 dc->xformWorld2Wnd.eDy = 0.0f;
140 dc->xformWorld2Vport = dc->xformWorld2Wnd;
141 dc->xformVport2World = dc->xformWorld2Wnd;
142 dc->vport2WorldValid = TRUE;
143 dc->BoundsRect.left = 0;
144 dc->BoundsRect.top = 0;
145 dc->BoundsRect.right = 0;
146 dc->BoundsRect.bottom = 0;
147 dc->saved_visrgn = NULL;
148 PATH_InitGdiPath(&dc->path);
150 if (!(dc->hSelf = alloc_gdi_handle( &dc->header, magic, &dc_funcs )))
152 HeapFree( GetProcessHeap(), 0, dc );
153 dc = NULL;
155 return dc;
160 /***********************************************************************
161 * free_dc_ptr
163 BOOL free_dc_ptr( DC *dc )
165 assert( dc->refcount == 1 );
166 if (free_gdi_handle( dc->hSelf ) != dc) return FALSE; /* shouldn't happen */
167 return HeapFree( GetProcessHeap(), 0, dc );
171 /***********************************************************************
172 * get_dc_ptr
174 * Retrieve a DC pointer but release the GDI lock.
176 DC *get_dc_ptr( HDC hdc )
178 DC *dc = get_dc_obj( hdc );
179 if (!dc) return NULL;
181 if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
183 dc->thread = GetCurrentThreadId();
185 else if (dc->thread != GetCurrentThreadId())
187 WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
188 GDI_ReleaseObj( hdc );
189 return NULL;
191 else InterlockedIncrement( &dc->refcount );
193 GDI_ReleaseObj( hdc );
194 return dc;
198 /***********************************************************************
199 * release_dc_ptr
201 void release_dc_ptr( DC *dc )
203 LONG ref;
205 dc->thread = 0;
206 ref = InterlockedDecrement( &dc->refcount );
207 assert( ref >= 0 );
208 if (ref) dc->thread = GetCurrentThreadId(); /* we still own it */
212 /***********************************************************************
213 * update_dc
215 * Make sure the DC vis region is up to date.
216 * This function may call up to USER so the GDI lock should _not_
217 * be held when calling it.
219 void update_dc( DC *dc )
221 if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookThunk)
222 dc->hookThunk( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
226 /***********************************************************************
227 * DC_DeleteObject
229 static BOOL DC_DeleteObject( HGDIOBJ handle )
231 return DeleteDC( handle );
235 /***********************************************************************
236 * DC_InitDC
238 * Setup device-specific DC values for a newly created DC.
240 void DC_InitDC( DC* dc )
242 if (dc->funcs->pRealizeDefaultPalette) dc->funcs->pRealizeDefaultPalette( dc->physDev );
243 SetTextColor( dc->hSelf, dc->textColor );
244 SetBkColor( dc->hSelf, dc->backgroundColor );
245 SelectObject( dc->hSelf, dc->hPen );
246 SelectObject( dc->hSelf, dc->hBrush );
247 SelectObject( dc->hSelf, dc->hFont );
248 CLIPPING_UpdateGCRegion( dc );
252 /***********************************************************************
253 * DC_InvertXform
255 * Computes the inverse of the transformation xformSrc and stores it to
256 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
257 * is singular.
259 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
261 double determinant;
263 determinant = xformSrc->eM11*xformSrc->eM22 -
264 xformSrc->eM12*xformSrc->eM21;
265 if (determinant > -1e-12 && determinant < 1e-12)
266 return FALSE;
268 xformDest->eM11 = xformSrc->eM22 / determinant;
269 xformDest->eM12 = -xformSrc->eM12 / determinant;
270 xformDest->eM21 = -xformSrc->eM21 / determinant;
271 xformDest->eM22 = xformSrc->eM11 / determinant;
272 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
273 xformSrc->eDy * xformDest->eM21;
274 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
275 xformSrc->eDy * xformDest->eM22;
277 return TRUE;
281 /***********************************************************************
282 * DC_UpdateXforms
284 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
285 * fields of the specified DC by creating a transformation that
286 * represents the current mapping mode and combining it with the DC's
287 * world transform. This function should be called whenever the
288 * parameters associated with the mapping mode (window and viewport
289 * extents and origins) or the world transform change.
291 void DC_UpdateXforms( DC *dc )
293 XFORM xformWnd2Vport, oldworld2vport;
294 double scaleX, scaleY;
296 /* Construct a transformation to do the window-to-viewport conversion */
297 scaleX = (double)dc->vportExtX / (double)dc->wndExtX;
298 scaleY = (double)dc->vportExtY / (double)dc->wndExtY;
299 xformWnd2Vport.eM11 = scaleX;
300 xformWnd2Vport.eM12 = 0.0;
301 xformWnd2Vport.eM21 = 0.0;
302 xformWnd2Vport.eM22 = scaleY;
303 xformWnd2Vport.eDx = (double)dc->vportOrgX -
304 scaleX * (double)dc->wndOrgX;
305 xformWnd2Vport.eDy = (double)dc->vportOrgY -
306 scaleY * (double)dc->wndOrgY;
308 oldworld2vport = dc->xformWorld2Vport;
309 /* Combine with the world transformation */
310 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
311 &xformWnd2Vport );
313 /* Create inverse of world-to-viewport transformation */
314 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
315 &dc->xformVport2World );
317 /* Reselect the font and pen back into the dc so that the size
318 gets updated. */
319 if (memcmp(&oldworld2vport, &dc->xformWorld2Vport, sizeof(oldworld2vport)) &&
320 !GdiIsMetaFileDC(dc->hSelf))
322 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
323 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_PEN));
328 /***********************************************************************
329 * get_dc_state (Not a Windows API)
331 HDC get_dc_state( HDC hdc )
333 DC * newdc, * dc;
334 HGDIOBJ handle;
336 if (!(dc = get_dc_ptr( hdc ))) return 0;
337 if (!(newdc = HeapAlloc( GetProcessHeap(), 0, sizeof(*newdc ))))
339 release_dc_ptr( dc );
340 return 0;
343 newdc->flags = dc->flags | DC_SAVED;
344 newdc->layout = dc->layout;
345 newdc->hPen = dc->hPen;
346 newdc->hBrush = dc->hBrush;
347 newdc->hFont = dc->hFont;
348 newdc->hBitmap = dc->hBitmap;
349 newdc->hDevice = dc->hDevice;
350 newdc->hPalette = dc->hPalette;
351 newdc->ROPmode = dc->ROPmode;
352 newdc->polyFillMode = dc->polyFillMode;
353 newdc->stretchBltMode = dc->stretchBltMode;
354 newdc->relAbsMode = dc->relAbsMode;
355 newdc->backgroundMode = dc->backgroundMode;
356 newdc->backgroundColor = dc->backgroundColor;
357 newdc->textColor = dc->textColor;
358 newdc->dcBrushColor = dc->dcBrushColor;
359 newdc->dcPenColor = dc->dcPenColor;
360 newdc->brushOrgX = dc->brushOrgX;
361 newdc->brushOrgY = dc->brushOrgY;
362 newdc->textAlign = dc->textAlign;
363 newdc->charExtra = dc->charExtra;
364 newdc->breakExtra = dc->breakExtra;
365 newdc->breakRem = dc->breakRem;
366 newdc->MapMode = dc->MapMode;
367 newdc->GraphicsMode = dc->GraphicsMode;
368 newdc->CursPosX = dc->CursPosX;
369 newdc->CursPosY = dc->CursPosY;
370 newdc->ArcDirection = dc->ArcDirection;
371 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
372 newdc->xformWorld2Vport = dc->xformWorld2Vport;
373 newdc->xformVport2World = dc->xformVport2World;
374 newdc->vport2WorldValid = dc->vport2WorldValid;
375 newdc->wndOrgX = dc->wndOrgX;
376 newdc->wndOrgY = dc->wndOrgY;
377 newdc->wndExtX = dc->wndExtX;
378 newdc->wndExtY = dc->wndExtY;
379 newdc->vportOrgX = dc->vportOrgX;
380 newdc->vportOrgY = dc->vportOrgY;
381 newdc->vportExtX = dc->vportExtX;
382 newdc->vportExtY = dc->vportExtY;
383 newdc->BoundsRect = dc->BoundsRect;
384 newdc->gdiFont = dc->gdiFont;
386 newdc->thread = GetCurrentThreadId();
387 newdc->refcount = 1;
388 newdc->saveLevel = 0;
389 newdc->saved_dc = 0;
391 PATH_InitGdiPath( &newdc->path );
393 newdc->pAbortProc = NULL;
394 newdc->hookThunk = NULL;
395 newdc->hookProc = 0;
396 newdc->saved_visrgn = NULL;
398 if (!(newdc->hSelf = alloc_gdi_handle( &newdc->header, dc->header.type, &dc_funcs )))
400 HeapFree( GetProcessHeap(), 0, newdc );
401 release_dc_ptr( dc );
402 return 0;
404 handle = newdc->hSelf;
405 TRACE("(%p): returning %p\n", hdc, handle );
407 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
409 newdc->hVisRgn = 0;
410 newdc->hClipRgn = 0;
411 newdc->hMetaRgn = 0;
412 newdc->hMetaClipRgn = 0;
413 if (dc->hClipRgn)
415 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
416 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
418 if (dc->hMetaRgn)
420 newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
421 CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
423 /* don't bother recomputing hMetaClipRgn, we'll do that in SetDCState */
425 release_dc_ptr( newdc );
426 release_dc_ptr( dc );
427 return handle;
431 /***********************************************************************
432 * set_dc_state (Not a Windows API)
434 void set_dc_state( HDC hdc, HDC hdcs )
436 DC *dc, *dcs;
438 if (!(dc = get_dc_ptr( hdc ))) return;
439 if (!(dcs = get_dc_ptr( hdcs )))
441 release_dc_ptr( dc );
442 return;
444 if (!(dcs->flags & DC_SAVED))
446 release_dc_ptr( dc );
447 release_dc_ptr( dcs );
448 return;
450 TRACE("%p %p\n", hdc, hdcs );
452 update_dc( dc );
453 dc->flags = dcs->flags & ~DC_SAVED;
454 dc->layout = dcs->layout;
455 dc->hDevice = dcs->hDevice;
456 dc->ROPmode = dcs->ROPmode;
457 dc->polyFillMode = dcs->polyFillMode;
458 dc->stretchBltMode = dcs->stretchBltMode;
459 dc->relAbsMode = dcs->relAbsMode;
460 dc->backgroundMode = dcs->backgroundMode;
461 dc->backgroundColor = dcs->backgroundColor;
462 dc->textColor = dcs->textColor;
463 dc->dcBrushColor = dcs->dcBrushColor;
464 dc->dcPenColor = dcs->dcPenColor;
465 dc->brushOrgX = dcs->brushOrgX;
466 dc->brushOrgY = dcs->brushOrgY;
467 dc->textAlign = dcs->textAlign;
468 dc->charExtra = dcs->charExtra;
469 dc->breakExtra = dcs->breakExtra;
470 dc->breakRem = dcs->breakRem;
471 dc->MapMode = dcs->MapMode;
472 dc->GraphicsMode = dcs->GraphicsMode;
473 dc->CursPosX = dcs->CursPosX;
474 dc->CursPosY = dcs->CursPosY;
475 dc->ArcDirection = dcs->ArcDirection;
476 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
477 dc->xformWorld2Vport = dcs->xformWorld2Vport;
478 dc->xformVport2World = dcs->xformVport2World;
479 dc->vport2WorldValid = dcs->vport2WorldValid;
480 dc->BoundsRect = dcs->BoundsRect;
482 dc->wndOrgX = dcs->wndOrgX;
483 dc->wndOrgY = dcs->wndOrgY;
484 dc->wndExtX = dcs->wndExtX;
485 dc->wndExtY = dcs->wndExtY;
486 dc->vportOrgX = dcs->vportOrgX;
487 dc->vportOrgY = dcs->vportOrgY;
488 dc->vportExtX = dcs->vportExtX;
489 dc->vportExtY = dcs->vportExtY;
491 if (dcs->hClipRgn)
493 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
494 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
496 else
498 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
499 dc->hClipRgn = 0;
501 if (dcs->hMetaRgn)
503 if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
504 CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
506 else
508 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
509 dc->hMetaRgn = 0;
511 CLIPPING_UpdateGCRegion( dc );
513 SelectObject( hdc, dcs->hBitmap );
514 SelectObject( hdc, dcs->hBrush );
515 SelectObject( hdc, dcs->hFont );
516 SelectObject( hdc, dcs->hPen );
517 SetBkColor( hdc, dcs->backgroundColor);
518 SetTextColor( hdc, dcs->textColor);
519 GDISelectPalette( hdc, dcs->hPalette, FALSE );
520 release_dc_ptr( dc );
521 release_dc_ptr( dcs );
525 /***********************************************************************
526 * SaveDC (GDI32.@)
528 INT WINAPI SaveDC( HDC hdc )
530 HDC hdcs;
531 DC * dc, * dcs;
532 INT ret;
534 dc = get_dc_ptr( hdc );
535 if (!dc) return 0;
537 if(dc->funcs->pSaveDC)
539 ret = dc->funcs->pSaveDC( dc->physDev );
540 if(ret)
541 ret = ++dc->saveLevel;
542 release_dc_ptr( dc );
543 return ret;
546 if (!(hdcs = get_dc_state( hdc )))
548 release_dc_ptr( dc );
549 return 0;
551 dcs = get_dc_ptr( hdcs );
553 /* Copy path. The reason why path saving / restoring is in SaveDC/
554 * RestoreDC and not in GetDCState/SetDCState is that the ...DCState
555 * functions are only in Win16 (which doesn't have paths) and that
556 * SetDCState doesn't allow us to signal an error (which can happen
557 * when copying paths).
559 if (!PATH_AssignGdiPath( &dcs->path, &dc->path ))
561 release_dc_ptr( dc );
562 release_dc_ptr( dcs );
563 DeleteDC( hdcs );
564 return 0;
567 dcs->saved_dc = dc->saved_dc;
568 dc->saved_dc = hdcs;
569 TRACE("(%p): returning %d\n", hdc, dc->saveLevel+1 );
570 ret = ++dc->saveLevel;
571 release_dc_ptr( dcs );
572 release_dc_ptr( dc );
573 return ret;
577 /***********************************************************************
578 * RestoreDC (GDI32.@)
580 BOOL WINAPI RestoreDC( HDC hdc, INT level )
582 DC * dc, * dcs;
583 BOOL success;
585 TRACE("%p %d\n", hdc, level );
586 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
588 if(abs(level) > dc->saveLevel || level == 0)
590 release_dc_ptr( dc );
591 return FALSE;
594 update_dc( dc );
596 if(dc->funcs->pRestoreDC)
598 success = dc->funcs->pRestoreDC( dc->physDev, level );
599 if(level < 0) level = dc->saveLevel + level + 1;
600 if(success)
601 dc->saveLevel = level - 1;
602 release_dc_ptr( dc );
603 return success;
606 if (level < 0) level = dc->saveLevel + level + 1;
607 success=TRUE;
608 while (dc->saveLevel >= level)
610 HDC hdcs = dc->saved_dc;
611 if (!(dcs = get_dc_ptr( hdcs )))
613 success = FALSE;
614 break;
616 dc->saved_dc = dcs->saved_dc;
617 dcs->saved_dc = 0;
618 if (--dc->saveLevel < level)
620 set_dc_state( hdc, hdcs );
621 if (!PATH_AssignGdiPath( &dc->path, &dcs->path ))
622 /* FIXME: This might not be quite right, since we're
623 * returning FALSE but still destroying the saved DC state */
624 success=FALSE;
626 release_dc_ptr( dcs );
627 DeleteDC( hdcs );
629 release_dc_ptr( dc );
630 return success;
634 /***********************************************************************
635 * CreateDCW (GDI32.@)
637 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
638 const DEVMODEW *initData )
640 HDC hdc;
641 DC * dc;
642 const DC_FUNCTIONS *funcs;
643 WCHAR buf[300];
645 GDI_CheckNotLock();
647 if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
649 if (!driver)
651 ERR( "no device found for %s\n", debugstr_w(device) );
652 return 0;
654 strcpyW(buf, driver);
657 if (!(funcs = DRIVER_load_driver( buf )))
659 ERR( "no driver found for %s\n", debugstr_w(buf) );
660 return 0;
662 if (!(dc = alloc_dc_ptr( funcs, OBJ_DC ))) goto error;
663 hdc = dc->hSelf;
665 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
666 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error;
668 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
669 debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
671 if (dc->funcs->pCreateDC &&
672 !dc->funcs->pCreateDC( hdc, &dc->physDev, buf, device, output, initData ))
674 WARN("creation aborted by device\n" );
675 goto error;
678 SetRectRgn( dc->hVisRgn, 0, 0,
679 GetDeviceCaps( hdc, DESKTOPHORZRES ), GetDeviceCaps( hdc, DESKTOPVERTRES ) );
681 DC_InitDC( dc );
682 release_dc_ptr( dc );
683 return hdc;
685 error:
686 if (dc && dc->hVisRgn) DeleteObject( dc->hVisRgn );
687 if (dc) free_dc_ptr( dc );
688 DRIVER_release_driver( funcs );
689 return 0;
693 /***********************************************************************
694 * CreateDCA (GDI32.@)
696 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
697 const DEVMODEA *initData )
699 UNICODE_STRING driverW, deviceW, outputW;
700 DEVMODEW *initDataW;
701 HDC ret;
703 if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
704 else driverW.Buffer = NULL;
706 if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
707 else deviceW.Buffer = NULL;
709 if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
710 else outputW.Buffer = NULL;
712 initDataW = NULL;
713 if (initData)
715 /* don't convert initData for DISPLAY driver, it's not used */
716 if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
717 initDataW = GdiConvertToDevmodeW(initData);
720 ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
722 RtlFreeUnicodeString(&driverW);
723 RtlFreeUnicodeString(&deviceW);
724 RtlFreeUnicodeString(&outputW);
725 HeapFree(GetProcessHeap(), 0, initDataW);
726 return ret;
730 /***********************************************************************
731 * CreateICA (GDI32.@)
733 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
734 const DEVMODEA* initData )
736 /* Nothing special yet for ICs */
737 return CreateDCA( driver, device, output, initData );
741 /***********************************************************************
742 * CreateICW (GDI32.@)
744 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
745 const DEVMODEW* initData )
747 /* Nothing special yet for ICs */
748 return CreateDCW( driver, device, output, initData );
752 /***********************************************************************
753 * CreateCompatibleDC (GDI32.@)
755 HDC WINAPI CreateCompatibleDC( HDC hdc )
757 DC *dc, *origDC;
758 HDC ret;
759 const DC_FUNCTIONS *funcs = NULL;
760 PHYSDEV physDev = NULL;
762 GDI_CheckNotLock();
764 if ((origDC = get_dc_ptr( hdc )))
766 if (GetObjectType( hdc ) == OBJ_DC)
768 funcs = origDC->funcs;
769 physDev = origDC->physDev;
771 release_dc_ptr( origDC );
772 if (funcs) funcs = DRIVER_get_driver( funcs );
774 else if (hdc) return 0;
776 if (!funcs && !(funcs = DRIVER_load_driver( displayW ))) return 0;
778 if (!(dc = alloc_dc_ptr( funcs, OBJ_MEMDC ))) goto error;
780 TRACE("(%p): returning %p\n", hdc, dc->hSelf );
782 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
783 if (!(dc->hVisRgn = CreateRectRgn( 0, 0, 1, 1 ))) goto error; /* default bitmap is 1x1 */
785 /* Copy the driver-specific physical device info into
786 * the new DC. The driver may use this read-only info
787 * while creating the compatible DC below. */
788 dc->physDev = physDev;
789 ret = dc->hSelf;
791 if (dc->funcs->pCreateDC &&
792 !dc->funcs->pCreateDC( dc->hSelf, &dc->physDev, NULL, NULL, NULL, NULL ))
794 WARN("creation aborted by device\n");
795 goto error;
798 DC_InitDC( dc );
799 release_dc_ptr( dc );
800 return ret;
802 error:
803 if (dc && dc->hVisRgn) DeleteObject( dc->hVisRgn );
804 if (dc) free_dc_ptr( dc );
805 DRIVER_release_driver( funcs );
806 return 0;
810 /***********************************************************************
811 * DeleteDC (GDI32.@)
813 BOOL WINAPI DeleteDC( HDC hdc )
815 const DC_FUNCTIONS *funcs = NULL;
816 DC * dc;
818 TRACE("%p\n", hdc );
820 GDI_CheckNotLock();
822 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
823 if (dc->refcount != 1)
825 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
826 release_dc_ptr( dc );
827 return FALSE;
830 /* Call hook procedure to check whether is it OK to delete this DC */
831 if (dc->hookThunk && !dc->hookThunk( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ))
833 release_dc_ptr( dc );
834 return FALSE;
837 while (dc->saveLevel)
839 DC * dcs;
840 HDC hdcs = dc->saved_dc;
841 if (!(dcs = get_dc_ptr( hdcs ))) break;
842 dc->saved_dc = dcs->saved_dc;
843 dc->saveLevel--;
844 if (dcs->hClipRgn) DeleteObject( dcs->hClipRgn );
845 if (dcs->hMetaRgn) DeleteObject( dcs->hMetaRgn );
846 if (dcs->hMetaClipRgn) DeleteObject( dcs->hMetaClipRgn );
847 if (dcs->hVisRgn) DeleteObject( dcs->hVisRgn );
848 PATH_DestroyGdiPath(&dcs->path);
849 free_dc_ptr( dcs );
852 if (!(dc->flags & DC_SAVED))
854 SelectObject( hdc, GetStockObject(BLACK_PEN) );
855 SelectObject( hdc, GetStockObject(WHITE_BRUSH) );
856 SelectObject( hdc, GetStockObject(SYSTEM_FONT) );
857 SelectObject( hdc, GetStockObject(DEFAULT_BITMAP) );
858 funcs = dc->funcs;
859 if (dc->funcs->pDeleteDC) dc->funcs->pDeleteDC(dc->physDev);
860 dc->physDev = NULL;
863 while (dc->saved_visrgn)
865 struct saved_visrgn *next = dc->saved_visrgn->next;
866 DeleteObject( dc->saved_visrgn->hrgn );
867 HeapFree( GetProcessHeap(), 0, dc->saved_visrgn );
868 dc->saved_visrgn = next;
870 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
871 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
872 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
873 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
874 PATH_DestroyGdiPath(&dc->path);
876 free_dc_ptr( dc );
877 if (funcs) DRIVER_release_driver( funcs ); /* do that after releasing the GDI lock */
878 return TRUE;
882 /***********************************************************************
883 * ResetDCW (GDI32.@)
885 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
887 DC *dc;
888 HDC ret = hdc;
890 if ((dc = get_dc_ptr( hdc )))
892 if (dc->funcs->pResetDC) ret = dc->funcs->pResetDC( dc->physDev, devmode );
893 release_dc_ptr( dc );
895 return ret;
899 /***********************************************************************
900 * ResetDCA (GDI32.@)
902 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
904 DEVMODEW *devmodeW;
905 HDC ret;
907 if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
908 else devmodeW = NULL;
910 ret = ResetDCW(hdc, devmodeW);
912 HeapFree(GetProcessHeap(), 0, devmodeW);
913 return ret;
917 /***********************************************************************
918 * GetDeviceCaps (GDI32.@)
920 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
922 DC *dc;
923 INT ret = 0;
925 if ((dc = get_dc_ptr( hdc )))
927 if (dc->funcs->pGetDeviceCaps) ret = dc->funcs->pGetDeviceCaps( dc->physDev, cap );
928 else switch(cap) /* return meaningful values for some entries */
930 case HORZRES: ret = 640; break;
931 case VERTRES: ret = 480; break;
932 case BITSPIXEL: ret = 1; break;
933 case PLANES: ret = 1; break;
934 case NUMCOLORS: ret = 2; break;
935 case ASPECTX: ret = 36; break;
936 case ASPECTY: ret = 36; break;
937 case ASPECTXY: ret = 51; break;
938 case LOGPIXELSX: ret = 72; break;
939 case LOGPIXELSY: ret = 72; break;
940 case SIZEPALETTE: ret = 2; break;
942 release_dc_ptr( dc );
944 return ret;
948 /***********************************************************************
949 * GetBkColor (GDI32.@)
951 COLORREF WINAPI GetBkColor( HDC hdc )
953 COLORREF ret = 0;
954 DC * dc = get_dc_ptr( hdc );
955 if (dc)
957 ret = dc->backgroundColor;
958 release_dc_ptr( dc );
960 return ret;
964 /***********************************************************************
965 * SetBkColor (GDI32.@)
967 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
969 COLORREF oldColor;
970 DC * dc = get_dc_ptr( hdc );
972 TRACE("hdc=%p color=0x%08x\n", hdc, color);
974 if (!dc) return CLR_INVALID;
975 oldColor = dc->backgroundColor;
976 if (dc->funcs->pSetBkColor)
978 color = dc->funcs->pSetBkColor(dc->physDev, color);
979 if (color == CLR_INVALID) /* don't change it */
981 color = oldColor;
982 oldColor = CLR_INVALID;
985 dc->backgroundColor = color;
986 release_dc_ptr( dc );
987 return oldColor;
991 /***********************************************************************
992 * GetTextColor (GDI32.@)
994 COLORREF WINAPI GetTextColor( HDC hdc )
996 COLORREF ret = 0;
997 DC * dc = get_dc_ptr( hdc );
998 if (dc)
1000 ret = dc->textColor;
1001 release_dc_ptr( dc );
1003 return ret;
1007 /***********************************************************************
1008 * SetTextColor (GDI32.@)
1010 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
1012 COLORREF oldColor;
1013 DC * dc = get_dc_ptr( hdc );
1015 TRACE(" hdc=%p color=0x%08x\n", hdc, color);
1017 if (!dc) return CLR_INVALID;
1018 oldColor = dc->textColor;
1019 if (dc->funcs->pSetTextColor)
1021 color = dc->funcs->pSetTextColor(dc->physDev, color);
1022 if (color == CLR_INVALID) /* don't change it */
1024 color = oldColor;
1025 oldColor = CLR_INVALID;
1028 dc->textColor = color;
1029 release_dc_ptr( dc );
1030 return oldColor;
1034 /***********************************************************************
1035 * GetTextAlign (GDI32.@)
1037 UINT WINAPI GetTextAlign( HDC hdc )
1039 UINT ret = 0;
1040 DC * dc = get_dc_ptr( hdc );
1041 if (dc)
1043 ret = dc->textAlign;
1044 release_dc_ptr( dc );
1046 return ret;
1050 /***********************************************************************
1051 * SetTextAlign (GDI32.@)
1053 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
1055 UINT ret;
1056 DC *dc = get_dc_ptr( hdc );
1058 TRACE("hdc=%p align=%d\n", hdc, align);
1060 if (!dc) return 0x0;
1061 ret = dc->textAlign;
1062 if (dc->funcs->pSetTextAlign)
1063 if (!dc->funcs->pSetTextAlign(dc->physDev, align))
1064 ret = GDI_ERROR;
1065 if (ret != GDI_ERROR)
1066 dc->textAlign = align;
1067 release_dc_ptr( dc );
1068 return ret;
1071 /***********************************************************************
1072 * GetDCOrgEx (GDI32.@)
1074 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1076 DC * dc;
1078 if (!lpp) return FALSE;
1079 if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1081 lpp->x = lpp->y = 0;
1082 if (dc->funcs->pGetDCOrgEx) dc->funcs->pGetDCOrgEx( dc->physDev, lpp );
1083 release_dc_ptr( dc );
1084 return TRUE;
1088 /***********************************************************************
1089 * GetGraphicsMode (GDI32.@)
1091 INT WINAPI GetGraphicsMode( HDC hdc )
1093 INT ret = 0;
1094 DC * dc = get_dc_ptr( hdc );
1095 if (dc)
1097 ret = dc->GraphicsMode;
1098 release_dc_ptr( dc );
1100 return ret;
1104 /***********************************************************************
1105 * SetGraphicsMode (GDI32.@)
1107 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1109 INT ret = 0;
1110 DC *dc = get_dc_ptr( hdc );
1112 /* One would think that setting the graphics mode to GM_COMPATIBLE
1113 * would also reset the world transformation matrix to the unity
1114 * matrix. However, in Windows, this is not the case. This doesn't
1115 * make a lot of sense to me, but that's the way it is.
1117 if (!dc) return 0;
1118 if ((mode > 0) && (mode <= GM_LAST))
1120 ret = dc->GraphicsMode;
1121 dc->GraphicsMode = mode;
1123 release_dc_ptr( dc );
1124 return ret;
1128 /***********************************************************************
1129 * GetArcDirection (GDI32.@)
1131 INT WINAPI GetArcDirection( HDC hdc )
1133 INT ret = 0;
1134 DC * dc = get_dc_ptr( hdc );
1135 if (dc)
1137 ret = dc->ArcDirection;
1138 release_dc_ptr( dc );
1140 return ret;
1144 /***********************************************************************
1145 * SetArcDirection (GDI32.@)
1147 INT WINAPI SetArcDirection( HDC hdc, INT nDirection )
1149 DC * dc;
1150 INT nOldDirection = 0;
1152 if (nDirection!=AD_COUNTERCLOCKWISE && nDirection!=AD_CLOCKWISE)
1154 SetLastError(ERROR_INVALID_PARAMETER);
1155 return 0;
1158 if ((dc = get_dc_ptr( hdc )))
1160 if (dc->funcs->pSetArcDirection)
1162 dc->funcs->pSetArcDirection(dc->physDev, nDirection);
1164 nOldDirection = dc->ArcDirection;
1165 dc->ArcDirection = nDirection;
1166 release_dc_ptr( dc );
1168 return nOldDirection;
1172 /***********************************************************************
1173 * GetWorldTransform (GDI32.@)
1175 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1177 DC * dc;
1178 if (!xform) return FALSE;
1179 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1180 *xform = dc->xformWorld2Wnd;
1181 release_dc_ptr( dc );
1182 return TRUE;
1186 /***********************************************************************
1187 * GetTransform (GDI32.@)
1189 BOOL WINAPI GetTransform( HDC hdc, DWORD unknown, LPXFORM xform )
1191 if (unknown == 0x0203) return GetWorldTransform( hdc, xform );
1192 FIXME("stub: don't know what to do for code %x\n", unknown );
1193 return FALSE;
1197 /***********************************************************************
1198 * SetWorldTransform (GDI32.@)
1200 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
1202 BOOL ret = FALSE;
1203 DC *dc;
1205 if (!xform) return FALSE;
1207 dc = get_dc_ptr( hdc );
1208 if (!dc) return FALSE;
1210 /* Check that graphics mode is GM_ADVANCED */
1211 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1213 TRACE("eM11 %f eM12 %f eM21 %f eM22 %f eDx %f eDy %f\n",
1214 xform->eM11, xform->eM12, xform->eM21, xform->eM22, xform->eDx, xform->eDy);
1216 /* The transform must conform to (eM11 * eM22 != eM12 * eM21) requirement */
1217 if (xform->eM11 * xform->eM22 == xform->eM12 * xform->eM21) goto done;
1219 if (dc->funcs->pSetWorldTransform)
1221 ret = dc->funcs->pSetWorldTransform(dc->physDev, xform);
1222 if (!ret) goto done;
1225 dc->xformWorld2Wnd = *xform;
1226 DC_UpdateXforms( dc );
1227 ret = TRUE;
1228 done:
1229 release_dc_ptr( dc );
1230 return ret;
1234 /****************************************************************************
1235 * ModifyWorldTransform [GDI32.@]
1236 * Modifies the world transformation for a device context.
1238 * PARAMS
1239 * hdc [I] Handle to device context
1240 * xform [I] XFORM structure that will be used to modify the world
1241 * transformation
1242 * iMode [I] Specifies in what way to modify the world transformation
1243 * Possible values:
1244 * MWT_IDENTITY
1245 * Resets the world transformation to the identity matrix.
1246 * The parameter xform is ignored.
1247 * MWT_LEFTMULTIPLY
1248 * Multiplies xform into the world transformation matrix from
1249 * the left.
1250 * MWT_RIGHTMULTIPLY
1251 * Multiplies xform into the world transformation matrix from
1252 * the right.
1254 * RETURNS
1255 * Success: TRUE.
1256 * Failure: FALSE. Use GetLastError() to determine the cause.
1258 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform,
1259 DWORD iMode )
1261 BOOL ret = FALSE;
1262 DC *dc = get_dc_ptr( hdc );
1264 /* Check for illegal parameters */
1265 if (!dc) return FALSE;
1266 if (!xform && iMode != MWT_IDENTITY) goto done;
1268 /* Check that graphics mode is GM_ADVANCED */
1269 if (dc->GraphicsMode!=GM_ADVANCED) goto done;
1271 if (dc->funcs->pModifyWorldTransform)
1273 ret = dc->funcs->pModifyWorldTransform(dc->physDev, xform, iMode);
1274 if (!ret) goto done;
1277 switch (iMode)
1279 case MWT_IDENTITY:
1280 dc->xformWorld2Wnd.eM11 = 1.0f;
1281 dc->xformWorld2Wnd.eM12 = 0.0f;
1282 dc->xformWorld2Wnd.eM21 = 0.0f;
1283 dc->xformWorld2Wnd.eM22 = 1.0f;
1284 dc->xformWorld2Wnd.eDx = 0.0f;
1285 dc->xformWorld2Wnd.eDy = 0.0f;
1286 break;
1287 case MWT_LEFTMULTIPLY:
1288 CombineTransform( &dc->xformWorld2Wnd, xform,
1289 &dc->xformWorld2Wnd );
1290 break;
1291 case MWT_RIGHTMULTIPLY:
1292 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd,
1293 xform );
1294 break;
1295 default:
1296 goto done;
1299 DC_UpdateXforms( dc );
1300 ret = TRUE;
1301 done:
1302 release_dc_ptr( dc );
1303 return ret;
1307 /****************************************************************************
1308 * CombineTransform [GDI32.@]
1309 * Combines two transformation matrices.
1311 * PARAMS
1312 * xformResult [O] Stores the result of combining the two matrices
1313 * xform1 [I] Specifies the first matrix to apply
1314 * xform2 [I] Specifies the second matrix to apply
1316 * REMARKS
1317 * The same matrix can be passed in for more than one of the parameters.
1319 * RETURNS
1320 * Success: TRUE.
1321 * Failure: FALSE. Use GetLastError() to determine the cause.
1323 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1324 const XFORM *xform2 )
1326 XFORM xformTemp;
1328 /* Check for illegal parameters */
1329 if (!xformResult || !xform1 || !xform2)
1330 return FALSE;
1332 /* Create the result in a temporary XFORM, since xformResult may be
1333 * equal to xform1 or xform2 */
1334 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1335 xform1->eM12 * xform2->eM21;
1336 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1337 xform1->eM12 * xform2->eM22;
1338 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1339 xform1->eM22 * xform2->eM21;
1340 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1341 xform1->eM22 * xform2->eM22;
1342 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1343 xform1->eDy * xform2->eM21 +
1344 xform2->eDx;
1345 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1346 xform1->eDy * xform2->eM22 +
1347 xform2->eDy;
1349 /* Copy the result to xformResult */
1350 *xformResult = xformTemp;
1352 return TRUE;
1356 /***********************************************************************
1357 * SetDCHook (GDI32.@)
1359 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1361 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1363 DC *dc = get_dc_ptr( hdc );
1365 if (!dc) return FALSE;
1367 if (!(dc->flags & DC_SAVED))
1369 dc->dwHookData = dwHookData;
1370 dc->hookThunk = hookProc;
1372 release_dc_ptr( dc );
1373 return TRUE;
1377 /***********************************************************************
1378 * GetDCHook (GDI32.@)
1380 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1382 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1384 DC *dc = get_dc_ptr( hdc );
1385 DWORD_PTR ret;
1387 if (!dc) return 0;
1388 if (proc) *proc = dc->hookThunk;
1389 ret = dc->dwHookData;
1390 release_dc_ptr( dc );
1391 return ret;
1395 /* relay function to call the 16-bit DC hook proc */
1396 static BOOL WINAPI call_dc_hook16( HDC hdc, WORD code, DWORD_PTR data, LPARAM lParam )
1398 WORD args[6];
1399 DWORD ret = 0;
1400 DC *dc = get_dc_ptr( hdc );
1402 if (!dc) return FALSE;
1403 if (dc->hookProc)
1405 args[5] = HDC_16(hdc);
1406 args[4] = code;
1407 args[3] = HIWORD(data);
1408 args[2] = LOWORD(data);
1409 args[1] = HIWORD(lParam);
1410 args[0] = LOWORD(lParam);
1411 WOWCallback16Ex( (DWORD)dc->hookProc, WCB16_PASCAL, sizeof(args), args, &ret );
1413 release_dc_ptr( dc );
1414 return LOWORD(ret);
1417 /***********************************************************************
1418 * SetDCHook (GDI.190)
1420 BOOL16 WINAPI SetDCHook16( HDC16 hdc16, FARPROC16 hookProc, DWORD dwHookData )
1422 DC *dc = get_dc_ptr( HDC_32(hdc16) );
1424 if (!dc) return FALSE;
1425 if (!(dc->flags & DC_SAVED))
1427 dc->dwHookData = dwHookData;
1428 dc->hookThunk = call_dc_hook16;
1429 dc->hookProc = hookProc;
1431 release_dc_ptr( dc );
1432 return TRUE;
1436 /***********************************************************************
1437 * GetDCHook (GDI.191)
1439 DWORD WINAPI GetDCHook16( HDC16 hdc16, FARPROC16 *phookProc )
1441 HDC hdc = HDC_32( hdc16 );
1442 DC *dc = get_dc_ptr( hdc );
1443 DWORD ret;
1445 if (!dc) return 0;
1446 *phookProc = dc->hookProc;
1447 ret = dc->dwHookData;
1448 release_dc_ptr( dc );
1449 return ret;
1453 /***********************************************************************
1454 * SetHookFlags (GDI32.@)
1456 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1458 WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
1460 DC *dc = get_dc_obj( hdc ); /* not get_dc_ptr, this needs to work from any thread */
1461 LONG ret = 0;
1463 if (!dc) return 0;
1465 /* "Undocumented Windows" info is slightly confusing. */
1467 TRACE("hDC %p, flags %04x\n",hdc,flags);
1469 if (flags & DCHF_INVALIDATEVISRGN)
1470 ret = InterlockedExchange( &dc->dirty, 1 );
1471 else if (flags & DCHF_VALIDATEVISRGN || !flags)
1472 ret = InterlockedExchange( &dc->dirty, 0 );
1474 GDI_ReleaseObj( dc );
1475 return ret;
1478 /***********************************************************************
1479 * SetICMMode (GDI32.@)
1481 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1483 /*FIXME: Assume that ICM is always off, and cannot be turned on */
1484 if (iEnableICM == ICM_OFF) return ICM_OFF;
1485 if (iEnableICM == ICM_ON) return 0;
1486 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1487 return 0;
1490 /***********************************************************************
1491 * GetDeviceGammaRamp (GDI32.@)
1493 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1495 BOOL ret = FALSE;
1496 DC *dc = get_dc_ptr( hDC );
1498 if( dc )
1500 if (dc->funcs->pGetDeviceGammaRamp)
1501 ret = dc->funcs->pGetDeviceGammaRamp(dc->physDev, ptr);
1502 release_dc_ptr( dc );
1504 return ret;
1507 /***********************************************************************
1508 * SetDeviceGammaRamp (GDI32.@)
1510 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1512 BOOL ret = FALSE;
1513 DC *dc = get_dc_ptr( hDC );
1515 if( dc )
1517 if (dc->funcs->pSetDeviceGammaRamp)
1518 ret = dc->funcs->pSetDeviceGammaRamp(dc->physDev, ptr);
1519 release_dc_ptr( dc );
1521 return ret;
1524 /***********************************************************************
1525 * GetColorSpace (GDI32.@)
1527 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1529 /*FIXME Need to to whatever GetColorSpace actually does */
1530 return 0;
1533 /***********************************************************************
1534 * CreateColorSpaceA (GDI32.@)
1536 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1538 FIXME( "stub\n" );
1539 return 0;
1542 /***********************************************************************
1543 * CreateColorSpaceW (GDI32.@)
1545 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1547 FIXME( "stub\n" );
1548 return 0;
1551 /***********************************************************************
1552 * DeleteColorSpace (GDI32.@)
1554 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1556 FIXME( "stub\n" );
1558 return TRUE;
1561 /***********************************************************************
1562 * SetColorSpace (GDI32.@)
1564 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1566 FIXME( "stub\n" );
1568 return hColorSpace;
1571 /***********************************************************************
1572 * GetBoundsRect (GDI32.@)
1574 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1576 UINT ret;
1577 DC *dc = get_dc_ptr( hdc );
1579 if ( !dc ) return 0;
1581 if (rect) *rect = dc->BoundsRect;
1583 ret = ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1585 if (flags & DCB_RESET)
1587 dc->BoundsRect.left = 0;
1588 dc->BoundsRect.top = 0;
1589 dc->BoundsRect.right = 0;
1590 dc->BoundsRect.bottom = 0;
1591 dc->flags &= ~DC_BOUNDS_SET;
1593 release_dc_ptr( dc );
1594 return ret;
1598 /***********************************************************************
1599 * SetBoundsRect (GDI32.@)
1601 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1603 UINT ret;
1604 DC *dc;
1606 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1607 if (!(dc = get_dc_ptr( hdc ))) return 0;
1609 ret = ((dc->flags & DC_BOUNDS_ENABLE) ? DCB_ENABLE : DCB_DISABLE) |
1610 ((dc->flags & DC_BOUNDS_SET) ? DCB_SET : DCB_RESET);
1612 if (flags & DCB_RESET)
1614 dc->BoundsRect.left = 0;
1615 dc->BoundsRect.top = 0;
1616 dc->BoundsRect.right = 0;
1617 dc->BoundsRect.bottom = 0;
1618 dc->flags &= ~DC_BOUNDS_SET;
1621 if ((flags & DCB_ACCUMULATE) && rect && rect->left < rect->right && rect->top < rect->bottom)
1623 if (dc->flags & DC_BOUNDS_SET)
1625 dc->BoundsRect.left = min( dc->BoundsRect.left, rect->left );
1626 dc->BoundsRect.top = min( dc->BoundsRect.top, rect->top );
1627 dc->BoundsRect.right = max( dc->BoundsRect.right, rect->right );
1628 dc->BoundsRect.bottom = max( dc->BoundsRect.bottom, rect->bottom );
1630 else
1632 dc->BoundsRect = *rect;
1633 dc->flags |= DC_BOUNDS_SET;
1637 if (flags & DCB_ENABLE) dc->flags |= DC_BOUNDS_ENABLE;
1638 if (flags & DCB_DISABLE) dc->flags &= ~DC_BOUNDS_ENABLE;
1640 release_dc_ptr( dc );
1641 return ret;
1645 /***********************************************************************
1646 * GetRelAbs (GDI32.@)
1648 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1650 INT ret = 0;
1651 DC *dc = get_dc_ptr( hdc );
1652 if (dc)
1654 ret = dc->relAbsMode;
1655 release_dc_ptr( dc );
1657 return ret;
1663 /***********************************************************************
1664 * GetBkMode (GDI32.@)
1666 INT WINAPI GetBkMode( HDC hdc )
1668 INT ret = 0;
1669 DC * dc = get_dc_ptr( hdc );
1670 if (dc)
1672 ret = dc->backgroundMode;
1673 release_dc_ptr( dc );
1675 return ret;
1679 /***********************************************************************
1680 * SetBkMode (GDI32.@)
1682 INT WINAPI SetBkMode( HDC hdc, INT mode )
1684 INT ret;
1685 DC *dc;
1686 if ((mode <= 0) || (mode > BKMODE_LAST))
1688 SetLastError(ERROR_INVALID_PARAMETER);
1689 return 0;
1691 if (!(dc = get_dc_ptr( hdc ))) return 0;
1693 ret = dc->backgroundMode;
1694 if (dc->funcs->pSetBkMode)
1695 if (!dc->funcs->pSetBkMode( dc->physDev, mode ))
1696 ret = 0;
1697 if (ret)
1698 dc->backgroundMode = mode;
1699 release_dc_ptr( dc );
1700 return ret;
1704 /***********************************************************************
1705 * GetROP2 (GDI32.@)
1707 INT WINAPI GetROP2( HDC hdc )
1709 INT ret = 0;
1710 DC * dc = get_dc_ptr( hdc );
1711 if (dc)
1713 ret = dc->ROPmode;
1714 release_dc_ptr( dc );
1716 return ret;
1720 /***********************************************************************
1721 * SetROP2 (GDI32.@)
1723 INT WINAPI SetROP2( HDC hdc, INT mode )
1725 INT ret;
1726 DC *dc;
1727 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1729 SetLastError(ERROR_INVALID_PARAMETER);
1730 return 0;
1732 if (!(dc = get_dc_ptr( hdc ))) return 0;
1733 ret = dc->ROPmode;
1734 if (dc->funcs->pSetROP2)
1735 if (!dc->funcs->pSetROP2( dc->physDev, mode ))
1736 ret = 0;
1737 if (ret)
1738 dc->ROPmode = mode;
1739 release_dc_ptr( dc );
1740 return ret;
1744 /***********************************************************************
1745 * SetRelAbs (GDI32.@)
1747 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1749 INT ret;
1750 DC *dc;
1751 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1753 SetLastError(ERROR_INVALID_PARAMETER);
1754 return 0;
1756 if (!(dc = get_dc_ptr( hdc ))) return 0;
1757 if (dc->funcs->pSetRelAbs)
1758 ret = dc->funcs->pSetRelAbs( dc->physDev, mode );
1759 else
1761 ret = dc->relAbsMode;
1762 dc->relAbsMode = mode;
1764 release_dc_ptr( dc );
1765 return ret;
1769 /***********************************************************************
1770 * GetPolyFillMode (GDI32.@)
1772 INT WINAPI GetPolyFillMode( HDC hdc )
1774 INT ret = 0;
1775 DC * dc = get_dc_ptr( hdc );
1776 if (dc)
1778 ret = dc->polyFillMode;
1779 release_dc_ptr( dc );
1781 return ret;
1785 /***********************************************************************
1786 * SetPolyFillMode (GDI32.@)
1788 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1790 INT ret;
1791 DC *dc;
1792 if ((mode <= 0) || (mode > POLYFILL_LAST))
1794 SetLastError(ERROR_INVALID_PARAMETER);
1795 return 0;
1797 if (!(dc = get_dc_ptr( hdc ))) return 0;
1798 ret = dc->polyFillMode;
1799 if (dc->funcs->pSetPolyFillMode)
1800 if (!dc->funcs->pSetPolyFillMode( dc->physDev, mode ))
1801 ret = 0;
1802 if (ret)
1803 dc->polyFillMode = mode;
1804 release_dc_ptr( dc );
1805 return ret;
1809 /***********************************************************************
1810 * GetStretchBltMode (GDI32.@)
1812 INT WINAPI GetStretchBltMode( HDC hdc )
1814 INT ret = 0;
1815 DC * dc = get_dc_ptr( hdc );
1816 if (dc)
1818 ret = dc->stretchBltMode;
1819 release_dc_ptr( dc );
1821 return ret;
1825 /***********************************************************************
1826 * SetStretchBltMode (GDI32.@)
1828 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1830 INT ret;
1831 DC *dc;
1832 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1834 SetLastError(ERROR_INVALID_PARAMETER);
1835 return 0;
1837 if (!(dc = get_dc_ptr( hdc ))) return 0;
1838 ret = dc->stretchBltMode;
1839 if (dc->funcs->pSetStretchBltMode)
1840 if (!dc->funcs->pSetStretchBltMode( dc->physDev, mode ))
1841 ret = 0;
1842 if (ret)
1843 dc->stretchBltMode = mode;
1844 release_dc_ptr( dc );
1845 return ret;
1849 /***********************************************************************
1850 * GetMapMode (GDI32.@)
1852 INT WINAPI GetMapMode( HDC hdc )
1854 INT ret = 0;
1855 DC * dc = get_dc_ptr( hdc );
1856 if (dc)
1858 ret = dc->MapMode;
1859 release_dc_ptr( dc );
1861 return ret;
1865 /***********************************************************************
1866 * GetBrushOrgEx (GDI32.@)
1868 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1870 DC * dc = get_dc_ptr( hdc );
1871 if (!dc) return FALSE;
1872 pt->x = dc->brushOrgX;
1873 pt->y = dc->brushOrgY;
1874 release_dc_ptr( dc );
1875 return TRUE;
1879 /***********************************************************************
1880 * GetCurrentPositionEx (GDI32.@)
1882 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1884 DC * dc = get_dc_ptr( hdc );
1885 if (!dc) return FALSE;
1886 pt->x = dc->CursPosX;
1887 pt->y = dc->CursPosY;
1888 release_dc_ptr( dc );
1889 return TRUE;
1893 /***********************************************************************
1894 * GetViewportExtEx (GDI32.@)
1896 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1898 DC * dc = get_dc_ptr( hdc );
1899 if (!dc) return FALSE;
1900 size->cx = dc->vportExtX;
1901 size->cy = dc->vportExtY;
1902 release_dc_ptr( dc );
1903 return TRUE;
1907 /***********************************************************************
1908 * GetViewportOrgEx (GDI32.@)
1910 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1912 DC * dc = get_dc_ptr( hdc );
1913 if (!dc) return FALSE;
1914 pt->x = dc->vportOrgX;
1915 pt->y = dc->vportOrgY;
1916 release_dc_ptr( dc );
1917 return TRUE;
1921 /***********************************************************************
1922 * GetWindowExtEx (GDI32.@)
1924 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1926 DC * dc = get_dc_ptr( hdc );
1927 if (!dc) return FALSE;
1928 size->cx = dc->wndExtX;
1929 size->cy = dc->wndExtY;
1930 release_dc_ptr( dc );
1931 return TRUE;
1935 /***********************************************************************
1936 * GetWindowOrgEx (GDI32.@)
1938 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1940 DC * dc = get_dc_ptr( hdc );
1941 if (!dc) return FALSE;
1942 pt->x = dc->wndOrgX;
1943 pt->y = dc->wndOrgY;
1944 release_dc_ptr( dc );
1945 return TRUE;
1949 /***********************************************************************
1950 * GetLayout (GDI32.@)
1952 * Gets left->right or right->left text layout flags of a dc.
1955 DWORD WINAPI GetLayout(HDC hdc)
1957 DWORD layout = GDI_ERROR;
1959 DC * dc = get_dc_ptr( hdc );
1960 if (dc)
1962 layout = dc->layout;
1963 release_dc_ptr( dc );
1966 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1968 return layout;
1971 /***********************************************************************
1972 * SetLayout (GDI32.@)
1974 * Sets left->right or right->left text layout flags of a dc.
1977 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1979 DWORD oldlayout = GDI_ERROR;
1981 DC * dc = get_dc_ptr( hdc );
1982 if (dc)
1984 oldlayout = dc->layout;
1985 dc->layout = layout;
1986 release_dc_ptr( dc );
1989 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
1991 return oldlayout;
1994 /***********************************************************************
1995 * GetDCBrushColor (GDI32.@)
1997 * Retrieves the current brush color for the specified device
1998 * context (DC).
2001 COLORREF WINAPI GetDCBrushColor(HDC hdc)
2003 DC *dc;
2004 COLORREF dcBrushColor = CLR_INVALID;
2006 TRACE("hdc(%p)\n", hdc);
2008 dc = get_dc_ptr( hdc );
2009 if (dc)
2011 dcBrushColor = dc->dcBrushColor;
2012 release_dc_ptr( dc );
2015 return dcBrushColor;
2018 /***********************************************************************
2019 * SetDCBrushColor (GDI32.@)
2021 * Sets the current device context (DC) brush color to the specified
2022 * color value. If the device cannot represent the specified color
2023 * value, the color is set to the nearest physical color.
2026 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
2028 DC *dc;
2029 COLORREF oldClr = CLR_INVALID;
2031 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2033 dc = get_dc_ptr( hdc );
2034 if (dc)
2036 if (dc->funcs->pSetDCBrushColor)
2037 crColor = dc->funcs->pSetDCBrushColor( dc->physDev, crColor );
2038 else if (dc->hBrush == GetStockObject( DC_BRUSH ))
2040 /* If DC_BRUSH is selected, update driver pen color */
2041 HBRUSH hBrush = CreateSolidBrush( crColor );
2042 dc->funcs->pSelectBrush( dc->physDev, hBrush );
2043 DeleteObject( hBrush );
2046 if (crColor != CLR_INVALID)
2048 oldClr = dc->dcBrushColor;
2049 dc->dcBrushColor = crColor;
2052 release_dc_ptr( dc );
2055 return oldClr;
2058 /***********************************************************************
2059 * GetDCPenColor (GDI32.@)
2061 * Retrieves the current pen color for the specified device
2062 * context (DC).
2065 COLORREF WINAPI GetDCPenColor(HDC hdc)
2067 DC *dc;
2068 COLORREF dcPenColor = CLR_INVALID;
2070 TRACE("hdc(%p)\n", hdc);
2072 dc = get_dc_ptr( hdc );
2073 if (dc)
2075 dcPenColor = dc->dcPenColor;
2076 release_dc_ptr( dc );
2079 return dcPenColor;
2082 /***********************************************************************
2083 * SetDCPenColor (GDI32.@)
2085 * Sets the current device context (DC) pen color to the specified
2086 * color value. If the device cannot represent the specified color
2087 * value, the color is set to the nearest physical color.
2090 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
2092 DC *dc;
2093 COLORREF oldClr = CLR_INVALID;
2095 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2097 dc = get_dc_ptr( hdc );
2098 if (dc)
2100 if (dc->funcs->pSetDCPenColor)
2101 crColor = dc->funcs->pSetDCPenColor( dc->physDev, crColor );
2102 else if (dc->hPen == GetStockObject( DC_PEN ))
2104 /* If DC_PEN is selected, update the driver pen color */
2105 LOGPEN logpen = { PS_SOLID, { 0, 0 }, crColor };
2106 HPEN hPen = CreatePenIndirect( &logpen );
2107 dc->funcs->pSelectPen( dc->physDev, hPen );
2108 DeleteObject( hPen );
2111 if (crColor != CLR_INVALID)
2113 oldClr = dc->dcPenColor;
2114 dc->dcPenColor = crColor;
2117 release_dc_ptr( dc );
2120 return oldClr;
2123 /***********************************************************************
2124 * CancelDC (GDI32.@)
2126 BOOL WINAPI CancelDC(HDC hdc)
2128 FIXME("stub\n");
2129 return TRUE;
2132 /***********************************************************************
2133 * SetVirtualResolution (GDI32.@)
2135 * Undocumented on msdn. Called when PowerPoint XP saves a file.
2137 DWORD WINAPI SetVirtualResolution(HDC hdc, DWORD dw2, DWORD dw3, DWORD dw4, DWORD dw5)
2139 FIXME("(%p %08x %08x %08x %08x): stub!\n", hdc, dw2, dw3, dw4, dw5);
2140 return FALSE;
2143 /*******************************************************************
2144 * GetMiterLimit [GDI32.@]
2148 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2150 BOOL bRet = FALSE;
2151 DC *dc;
2153 TRACE("(%p,%p)\n", hdc, peLimit);
2155 dc = get_dc_ptr( hdc );
2156 if (dc)
2158 if (peLimit)
2159 *peLimit = dc->miterLimit;
2161 release_dc_ptr( dc );
2162 bRet = TRUE;
2164 return bRet;
2167 /*******************************************************************
2168 * SetMiterLimit [GDI32.@]
2172 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2174 BOOL bRet = FALSE;
2175 DC *dc;
2177 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2179 dc = get_dc_ptr( hdc );
2180 if (dc)
2182 if (peOldLimit)
2183 *peOldLimit = dc->miterLimit;
2184 dc->miterLimit = eNewLimit;
2185 release_dc_ptr( dc );
2186 bRet = TRUE;
2188 return bRet;
2191 /*******************************************************************
2192 * GdiIsMetaPrintDC [GDI32.@]
2194 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2196 FIXME("%p\n", hdc);
2197 return FALSE;
2200 /*******************************************************************
2201 * GdiIsMetaFileDC [GDI32.@]
2203 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2205 TRACE("%p\n", hdc);
2207 switch( GetObjectType( hdc ) )
2209 case OBJ_METADC:
2210 case OBJ_ENHMETADC:
2211 return TRUE;
2213 return FALSE;
2216 /*******************************************************************
2217 * GdiIsPlayMetafileDC [GDI32.@]
2219 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2221 FIXME("%p\n", hdc);
2222 return FALSE;