d2d1/tests: Fix stroke style object leak (Valgrind).
[wine.git] / dlls / gdi32 / dc.c
blob892d3e9744b88c94fa838ea0d79718b5ec111b84
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 "gdi_private.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dc);
39 static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };
41 static BOOL DC_DeleteObject( HGDIOBJ handle );
43 static const struct gdi_obj_funcs dc_funcs =
45 NULL, /* pSelectObject */
46 NULL, /* pGetObjectA */
47 NULL, /* pGetObjectW */
48 NULL, /* pUnrealizeObject */
49 DC_DeleteObject /* pDeleteObject */
53 static inline DC *get_dc_obj( HDC hdc )
55 WORD type;
56 DC *dc = get_any_obj_ptr( hdc, &type );
57 if (!dc) return NULL;
59 switch (type)
61 case OBJ_DC:
62 case OBJ_MEMDC:
63 case OBJ_METADC:
64 case OBJ_ENHMETADC:
65 return dc;
66 default:
67 GDI_ReleaseObj( hdc );
68 SetLastError( ERROR_INVALID_HANDLE );
69 return NULL;
74 /***********************************************************************
75 * set_initial_dc_state
77 static void set_initial_dc_state( DC *dc )
79 dc->wnd_org.x = 0;
80 dc->wnd_org.y = 0;
81 dc->wnd_ext.cx = 1;
82 dc->wnd_ext.cy = 1;
83 dc->vport_org.x = 0;
84 dc->vport_org.y = 0;
85 dc->vport_ext.cx = 1;
86 dc->vport_ext.cy = 1;
87 dc->miterLimit = 10.0f; /* 10.0 is the default, from MSDN */
88 dc->layout = 0;
89 dc->font_code_page = CP_ACP;
90 dc->ROPmode = R2_COPYPEN;
91 dc->polyFillMode = ALTERNATE;
92 dc->stretchBltMode = BLACKONWHITE;
93 dc->relAbsMode = ABSOLUTE;
94 dc->backgroundMode = OPAQUE;
95 dc->backgroundColor = RGB( 255, 255, 255 );
96 dc->dcBrushColor = RGB( 255, 255, 255 );
97 dc->dcPenColor = RGB( 0, 0, 0 );
98 dc->textColor = RGB( 0, 0, 0 );
99 dc->brush_org.x = 0;
100 dc->brush_org.y = 0;
101 dc->mapperFlags = 0;
102 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
103 dc->charExtra = 0;
104 dc->breakExtra = 0;
105 dc->breakRem = 0;
106 dc->MapMode = MM_TEXT;
107 dc->GraphicsMode = GM_COMPATIBLE;
108 dc->cur_pos.x = 0;
109 dc->cur_pos.y = 0;
110 dc->ArcDirection = AD_COUNTERCLOCKWISE;
111 dc->xformWorld2Wnd.eM11 = 1.0f;
112 dc->xformWorld2Wnd.eM12 = 0.0f;
113 dc->xformWorld2Wnd.eM21 = 0.0f;
114 dc->xformWorld2Wnd.eM22 = 1.0f;
115 dc->xformWorld2Wnd.eDx = 0.0f;
116 dc->xformWorld2Wnd.eDy = 0.0f;
117 dc->xformWorld2Vport = dc->xformWorld2Wnd;
118 dc->xformVport2World = dc->xformWorld2Wnd;
119 dc->vport2WorldValid = TRUE;
121 reset_bounds( &dc->bounds );
124 /***********************************************************************
125 * alloc_dc_ptr
127 DC *alloc_dc_ptr( WORD magic )
129 DC *dc;
131 if (!(dc = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dc) ))) return NULL;
133 dc->nulldrv.funcs = &null_driver;
134 dc->physDev = &dc->nulldrv;
135 dc->thread = GetCurrentThreadId();
136 dc->refcount = 1;
137 dc->hPen = GDI_inc_ref_count( GetStockObject( BLACK_PEN ));
138 dc->hBrush = GDI_inc_ref_count( GetStockObject( WHITE_BRUSH ));
139 dc->hFont = GDI_inc_ref_count( GetStockObject( SYSTEM_FONT ));
140 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
142 set_initial_dc_state( dc );
144 if (!(dc->hSelf = alloc_gdi_handle( dc, magic, &dc_funcs )))
146 HeapFree( GetProcessHeap(), 0, dc );
147 return NULL;
149 dc->nulldrv.hdc = dc->hSelf;
151 if (font_driver && !font_driver->pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL ))
153 free_dc_ptr( dc );
154 return NULL;
156 return dc;
161 /***********************************************************************
162 * free_dc_state
164 static void free_dc_state( DC *dc )
166 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
167 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
168 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
169 if (dc->region) DeleteObject( dc->region );
170 if (dc->path) free_gdi_path( dc->path );
171 HeapFree( GetProcessHeap(), 0, dc );
175 /***********************************************************************
176 * free_dc_ptr
178 void free_dc_ptr( DC *dc )
180 assert( dc->refcount == 1 );
182 while (dc->physDev != &dc->nulldrv)
184 PHYSDEV physdev = dc->physDev;
185 dc->physDev = physdev->next;
186 physdev->funcs->pDeleteDC( physdev );
188 GDI_dec_ref_count( dc->hPen );
189 GDI_dec_ref_count( dc->hBrush );
190 GDI_dec_ref_count( dc->hFont );
191 if (dc->hBitmap) GDI_dec_ref_count( dc->hBitmap );
192 free_gdi_handle( dc->hSelf );
193 free_dc_state( dc );
197 /***********************************************************************
198 * get_dc_ptr
200 * Retrieve a DC pointer but release the GDI lock.
202 DC *get_dc_ptr( HDC hdc )
204 DC *dc = get_dc_obj( hdc );
205 if (!dc) return NULL;
206 if (dc->disabled)
208 GDI_ReleaseObj( hdc );
209 return NULL;
212 if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
214 dc->thread = GetCurrentThreadId();
216 else if (dc->thread != GetCurrentThreadId())
218 WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
219 GDI_ReleaseObj( hdc );
220 return NULL;
222 else InterlockedIncrement( &dc->refcount );
224 GDI_ReleaseObj( hdc );
225 return dc;
229 /***********************************************************************
230 * release_dc_ptr
232 void release_dc_ptr( DC *dc )
234 LONG ref;
236 dc->thread = 0;
237 ref = InterlockedDecrement( &dc->refcount );
238 assert( ref >= 0 );
239 if (ref) dc->thread = GetCurrentThreadId(); /* we still own it */
243 /***********************************************************************
244 * update_dc
246 * Make sure the DC vis region is up to date.
247 * This function may call up to USER so the GDI lock should _not_
248 * be held when calling it.
250 void update_dc( DC *dc )
252 if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookProc)
253 dc->hookProc( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
257 /***********************************************************************
258 * DC_DeleteObject
260 static BOOL DC_DeleteObject( HGDIOBJ handle )
262 return DeleteDC( handle );
266 /***********************************************************************
267 * DC_InitDC
269 * Setup device-specific DC values for a newly created DC.
271 void DC_InitDC( DC* dc )
273 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRealizeDefaultPalette );
274 physdev->funcs->pRealizeDefaultPalette( physdev );
275 SetTextColor( dc->hSelf, dc->textColor );
276 SetBkColor( dc->hSelf, dc->backgroundColor );
277 SelectObject( dc->hSelf, dc->hPen );
278 SelectObject( dc->hSelf, dc->hBrush );
279 SelectObject( dc->hSelf, dc->hFont );
280 update_dc_clipping( dc );
281 SetVirtualResolution( dc->hSelf, 0, 0, 0, 0 );
282 physdev = GET_DC_PHYSDEV( dc, pSetBoundsRect );
283 physdev->funcs->pSetBoundsRect( physdev, &dc->bounds, dc->bounds_enabled ? DCB_ENABLE : DCB_DISABLE );
287 /***********************************************************************
288 * DC_InvertXform
290 * Computes the inverse of the transformation xformSrc and stores it to
291 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
292 * is singular.
294 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
296 double determinant;
298 determinant = xformSrc->eM11*xformSrc->eM22 -
299 xformSrc->eM12*xformSrc->eM21;
300 if (determinant > -1e-12 && determinant < 1e-12)
301 return FALSE;
303 xformDest->eM11 = xformSrc->eM22 / determinant;
304 xformDest->eM12 = -xformSrc->eM12 / determinant;
305 xformDest->eM21 = -xformSrc->eM21 / determinant;
306 xformDest->eM22 = xformSrc->eM11 / determinant;
307 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
308 xformSrc->eDy * xformDest->eM21;
309 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
310 xformSrc->eDy * xformDest->eM22;
312 return TRUE;
315 /* Construct a transformation to do the window-to-viewport conversion */
316 static void construct_window_to_viewport(DC *dc, XFORM *xform)
318 double scaleX, scaleY;
319 scaleX = (double)dc->vport_ext.cx / (double)dc->wnd_ext.cx;
320 scaleY = (double)dc->vport_ext.cy / (double)dc->wnd_ext.cy;
322 if (dc->layout & LAYOUT_RTL) scaleX = -scaleX;
323 xform->eM11 = scaleX;
324 xform->eM12 = 0.0;
325 xform->eM21 = 0.0;
326 xform->eM22 = scaleY;
327 xform->eDx = (double)dc->vport_org.x - scaleX * (double)dc->wnd_org.x;
328 xform->eDy = (double)dc->vport_org.y - scaleY * (double)dc->wnd_org.y;
329 if (dc->layout & LAYOUT_RTL) xform->eDx = dc->vis_rect.right - dc->vis_rect.left - 1 - xform->eDx;
332 /***********************************************************************
333 * linear_xform_cmp
335 * Compares the linear transform portion of two XFORMs (i.e. the 2x2 submatrix).
336 * Returns 0 if they match.
338 static inline int linear_xform_cmp( const XFORM *a, const XFORM *b )
340 return memcmp( a, b, FIELD_OFFSET( XFORM, eDx ) );
343 /***********************************************************************
344 * DC_UpdateXforms
346 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
347 * fields of the specified DC by creating a transformation that
348 * represents the current mapping mode and combining it with the DC's
349 * world transform. This function should be called whenever the
350 * parameters associated with the mapping mode (window and viewport
351 * extents and origins) or the world transform change.
353 void DC_UpdateXforms( DC *dc )
355 XFORM xformWnd2Vport, oldworld2vport;
357 construct_window_to_viewport(dc, &xformWnd2Vport);
359 oldworld2vport = dc->xformWorld2Vport;
360 /* Combine with the world transformation */
361 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
362 &xformWnd2Vport );
364 /* Create inverse of world-to-viewport transformation */
365 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
366 &dc->xformVport2World );
368 /* Reselect the font and pen back into the dc so that the size
369 gets updated. */
370 if (linear_xform_cmp( &oldworld2vport, &dc->xformWorld2Vport ) &&
371 !GdiIsMetaFileDC(dc->hSelf))
373 SelectObject(dc->hSelf, dc->hFont);
374 SelectObject(dc->hSelf, dc->hPen);
379 /***********************************************************************
380 * nulldrv_SaveDC
382 INT nulldrv_SaveDC( PHYSDEV dev )
384 DC *newdc, *dc = get_nulldrv_dc( dev );
386 if (!(newdc = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*newdc )))) return 0;
387 newdc->layout = dc->layout;
388 newdc->hPen = dc->hPen;
389 newdc->hBrush = dc->hBrush;
390 newdc->hFont = dc->hFont;
391 newdc->hBitmap = dc->hBitmap;
392 newdc->hPalette = dc->hPalette;
393 newdc->ROPmode = dc->ROPmode;
394 newdc->polyFillMode = dc->polyFillMode;
395 newdc->stretchBltMode = dc->stretchBltMode;
396 newdc->relAbsMode = dc->relAbsMode;
397 newdc->backgroundMode = dc->backgroundMode;
398 newdc->backgroundColor = dc->backgroundColor;
399 newdc->textColor = dc->textColor;
400 newdc->dcBrushColor = dc->dcBrushColor;
401 newdc->dcPenColor = dc->dcPenColor;
402 newdc->brush_org = dc->brush_org;
403 newdc->mapperFlags = dc->mapperFlags;
404 newdc->textAlign = dc->textAlign;
405 newdc->charExtra = dc->charExtra;
406 newdc->breakExtra = dc->breakExtra;
407 newdc->breakRem = dc->breakRem;
408 newdc->MapMode = dc->MapMode;
409 newdc->GraphicsMode = dc->GraphicsMode;
410 newdc->cur_pos = dc->cur_pos;
411 newdc->ArcDirection = dc->ArcDirection;
412 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
413 newdc->xformWorld2Vport = dc->xformWorld2Vport;
414 newdc->xformVport2World = dc->xformVport2World;
415 newdc->vport2WorldValid = dc->vport2WorldValid;
416 newdc->wnd_org = dc->wnd_org;
417 newdc->wnd_ext = dc->wnd_ext;
418 newdc->vport_org = dc->vport_org;
419 newdc->vport_ext = dc->vport_ext;
420 newdc->virtual_res = dc->virtual_res;
421 newdc->virtual_size = dc->virtual_size;
423 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
425 if (dc->hClipRgn)
427 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
428 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
430 if (dc->hMetaRgn)
432 newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
433 CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
436 if (!PATH_SavePath( newdc, dc ))
438 release_dc_ptr( dc );
439 free_dc_state( newdc );
440 return 0;
443 newdc->saved_dc = dc->saved_dc;
444 dc->saved_dc = newdc;
445 return ++dc->saveLevel;
449 /***********************************************************************
450 * nulldrv_RestoreDC
452 BOOL nulldrv_RestoreDC( PHYSDEV dev, INT level )
454 DC *dcs, *first_dcs, *dc = get_nulldrv_dc( dev );
455 INT save_level;
457 /* find the state level to restore */
459 if (abs(level) > dc->saveLevel || level == 0) return FALSE;
460 if (level < 0) level = dc->saveLevel + level + 1;
461 first_dcs = dc->saved_dc;
462 for (dcs = first_dcs, save_level = dc->saveLevel; save_level > level; save_level--)
463 dcs = dcs->saved_dc;
465 /* restore the state */
467 if (!PATH_RestorePath( dc, dcs )) return FALSE;
469 dc->layout = dcs->layout;
470 dc->ROPmode = dcs->ROPmode;
471 dc->polyFillMode = dcs->polyFillMode;
472 dc->stretchBltMode = dcs->stretchBltMode;
473 dc->relAbsMode = dcs->relAbsMode;
474 dc->backgroundMode = dcs->backgroundMode;
475 dc->backgroundColor = dcs->backgroundColor;
476 dc->textColor = dcs->textColor;
477 dc->dcBrushColor = dcs->dcBrushColor;
478 dc->dcPenColor = dcs->dcPenColor;
479 dc->brush_org = dcs->brush_org;
480 dc->mapperFlags = dcs->mapperFlags;
481 dc->textAlign = dcs->textAlign;
482 dc->charExtra = dcs->charExtra;
483 dc->breakExtra = dcs->breakExtra;
484 dc->breakRem = dcs->breakRem;
485 dc->MapMode = dcs->MapMode;
486 dc->GraphicsMode = dcs->GraphicsMode;
487 dc->cur_pos = dcs->cur_pos;
488 dc->ArcDirection = dcs->ArcDirection;
489 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
490 dc->xformWorld2Vport = dcs->xformWorld2Vport;
491 dc->xformVport2World = dcs->xformVport2World;
492 dc->vport2WorldValid = dcs->vport2WorldValid;
493 dc->wnd_org = dcs->wnd_org;
494 dc->wnd_ext = dcs->wnd_ext;
495 dc->vport_org = dcs->vport_org;
496 dc->vport_ext = dcs->vport_ext;
497 dc->virtual_res = dcs->virtual_res;
498 dc->virtual_size = dcs->virtual_size;
500 if (dcs->hClipRgn)
502 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
503 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
505 else
507 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
508 dc->hClipRgn = 0;
510 if (dcs->hMetaRgn)
512 if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
513 CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
515 else
517 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
518 dc->hMetaRgn = 0;
520 DC_UpdateXforms( dc );
521 update_dc_clipping( dc );
523 SelectObject( dev->hdc, dcs->hBitmap );
524 SelectObject( dev->hdc, dcs->hBrush );
525 SelectObject( dev->hdc, dcs->hFont );
526 SelectObject( dev->hdc, dcs->hPen );
527 SetBkColor( dev->hdc, dcs->backgroundColor);
528 SetTextColor( dev->hdc, dcs->textColor);
529 GDISelectPalette( dev->hdc, dcs->hPalette, FALSE );
531 dc->saved_dc = dcs->saved_dc;
532 dcs->saved_dc = 0;
533 dc->saveLevel = save_level - 1;
535 /* now destroy all the saved DCs */
537 while (first_dcs)
539 DC *next = first_dcs->saved_dc;
540 free_dc_state( first_dcs );
541 first_dcs = next;
543 return TRUE;
547 /***********************************************************************
548 * reset_dc_state
550 static BOOL reset_dc_state( HDC hdc )
552 DC *dc, *dcs, *next;
554 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
556 set_initial_dc_state( dc );
557 SetBkColor( hdc, RGB( 255, 255, 255 ));
558 SetTextColor( hdc, RGB( 0, 0, 0 ));
559 SelectObject( hdc, GetStockObject( WHITE_BRUSH ));
560 SelectObject( hdc, GetStockObject( SYSTEM_FONT ));
561 SelectObject( hdc, GetStockObject( BLACK_PEN ));
562 SetVirtualResolution( hdc, 0, 0, 0, 0 );
563 GDISelectPalette( hdc, GetStockObject( DEFAULT_PALETTE ), FALSE );
564 SetBoundsRect( hdc, NULL, DCB_DISABLE );
565 AbortPath( hdc );
567 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
568 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
569 dc->hClipRgn = 0;
570 dc->hMetaRgn = 0;
571 update_dc_clipping( dc );
573 for (dcs = dc->saved_dc; dcs; dcs = next)
575 next = dcs->saved_dc;
576 free_dc_state( dcs );
578 dc->saved_dc = NULL;
579 dc->saveLevel = 0;
580 release_dc_ptr( dc );
581 return TRUE;
585 /***********************************************************************
586 * SaveDC (GDI32.@)
588 INT WINAPI SaveDC( HDC hdc )
590 DC * dc;
591 INT ret = 0;
593 if ((dc = get_dc_ptr( hdc )))
595 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSaveDC );
596 ret = physdev->funcs->pSaveDC( physdev );
597 release_dc_ptr( dc );
599 return ret;
603 /***********************************************************************
604 * RestoreDC (GDI32.@)
606 BOOL WINAPI RestoreDC( HDC hdc, INT level )
608 PHYSDEV physdev;
609 DC *dc;
610 BOOL success = FALSE;
612 TRACE("%p %d\n", hdc, level );
613 if ((dc = get_dc_ptr( hdc )))
615 update_dc( dc );
616 physdev = GET_DC_PHYSDEV( dc, pRestoreDC );
617 success = physdev->funcs->pRestoreDC( physdev, level );
618 release_dc_ptr( dc );
620 return success;
624 /***********************************************************************
625 * CreateDCW (GDI32.@)
627 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
628 const DEVMODEW *initData )
630 HDC hdc;
631 DC * dc;
632 const struct gdi_dc_funcs *funcs;
633 WCHAR buf[300];
635 GDI_CheckNotLock();
637 if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
639 if (!driver)
641 ERR( "no device found for %s\n", debugstr_w(device) );
642 return 0;
644 strcpyW(buf, driver);
647 if (!(funcs = DRIVER_load_driver( buf )))
649 ERR( "no driver found for %s\n", debugstr_w(buf) );
650 return 0;
652 if (!(dc = alloc_dc_ptr( OBJ_DC ))) return 0;
653 hdc = dc->hSelf;
655 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
657 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
658 debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
660 if (funcs->pCreateDC)
662 if (!funcs->pCreateDC( &dc->physDev, buf, device, output, initData ))
664 WARN("creation aborted by device\n" );
665 free_dc_ptr( dc );
666 return 0;
670 dc->vis_rect.left = 0;
671 dc->vis_rect.top = 0;
672 dc->vis_rect.right = GetDeviceCaps( hdc, DESKTOPHORZRES );
673 dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
675 DC_InitDC( dc );
676 release_dc_ptr( dc );
677 return hdc;
681 /***********************************************************************
682 * CreateDCA (GDI32.@)
684 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
685 const DEVMODEA *initData )
687 UNICODE_STRING driverW, deviceW, outputW;
688 DEVMODEW *initDataW;
689 HDC ret;
691 if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
692 else driverW.Buffer = NULL;
694 if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
695 else deviceW.Buffer = NULL;
697 if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
698 else outputW.Buffer = NULL;
700 initDataW = NULL;
701 if (initData)
703 /* don't convert initData for DISPLAY driver, it's not used */
704 if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
705 initDataW = GdiConvertToDevmodeW(initData);
708 ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
710 RtlFreeUnicodeString(&driverW);
711 RtlFreeUnicodeString(&deviceW);
712 RtlFreeUnicodeString(&outputW);
713 HeapFree(GetProcessHeap(), 0, initDataW);
714 return ret;
718 /***********************************************************************
719 * CreateICA (GDI32.@)
721 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
722 const DEVMODEA* initData )
724 /* Nothing special yet for ICs */
725 return CreateDCA( driver, device, output, initData );
729 /***********************************************************************
730 * CreateICW (GDI32.@)
732 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
733 const DEVMODEW* initData )
735 /* Nothing special yet for ICs */
736 return CreateDCW( driver, device, output, initData );
740 /***********************************************************************
741 * CreateCompatibleDC (GDI32.@)
743 HDC WINAPI CreateCompatibleDC( HDC hdc )
745 DC *dc, *origDC;
746 HDC ret;
747 const struct gdi_dc_funcs *funcs;
748 PHYSDEV physDev = NULL;
750 GDI_CheckNotLock();
752 if (hdc)
754 if (!(origDC = get_dc_ptr( hdc ))) return 0;
755 physDev = GET_DC_PHYSDEV( origDC, pCreateCompatibleDC );
756 funcs = physDev->funcs;
757 release_dc_ptr( origDC );
759 else funcs = DRIVER_load_driver( displayW );
761 if (!(dc = alloc_dc_ptr( OBJ_MEMDC ))) return 0;
763 TRACE("(%p): returning %p\n", hdc, dc->hSelf );
765 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
766 dc->vis_rect.left = 0;
767 dc->vis_rect.top = 0;
768 dc->vis_rect.right = 1;
769 dc->vis_rect.bottom = 1;
770 dc->device_rect = dc->vis_rect;
772 ret = dc->hSelf;
774 if (funcs->pCreateCompatibleDC && !funcs->pCreateCompatibleDC( physDev, &dc->physDev ))
776 WARN("creation aborted by device\n");
777 free_dc_ptr( dc );
778 return 0;
781 if (!dib_driver.pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL ))
783 free_dc_ptr( dc );
784 return 0;
786 physDev = GET_DC_PHYSDEV( dc, pSelectBitmap );
787 physDev->funcs->pSelectBitmap( physDev, dc->hBitmap );
789 DC_InitDC( dc );
790 release_dc_ptr( dc );
791 return ret;
795 /***********************************************************************
796 * DeleteDC (GDI32.@)
798 BOOL WINAPI DeleteDC( HDC hdc )
800 DC * dc;
802 TRACE("%p\n", hdc );
804 GDI_CheckNotLock();
806 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
807 if (dc->refcount != 1)
809 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
810 release_dc_ptr( dc );
811 return FALSE;
814 /* Call hook procedure to check whether is it OK to delete this DC */
815 if (dc->hookProc && !dc->hookProc( dc->hSelf, DCHC_DELETEDC, dc->dwHookData, 0 ))
817 release_dc_ptr( dc );
818 return TRUE;
820 reset_dc_state( hdc );
821 free_dc_ptr( dc );
822 return TRUE;
826 /***********************************************************************
827 * ResetDCW (GDI32.@)
829 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
831 DC *dc;
832 HDC ret = 0;
834 if ((dc = get_dc_ptr( hdc )))
836 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pResetDC );
837 ret = physdev->funcs->pResetDC( physdev, devmode );
838 if (ret) /* reset the visible region */
840 dc->dirty = 0;
841 dc->vis_rect.left = 0;
842 dc->vis_rect.top = 0;
843 dc->vis_rect.right = GetDeviceCaps( hdc, DESKTOPHORZRES );
844 dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
845 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
846 dc->hVisRgn = 0;
847 update_dc_clipping( dc );
849 release_dc_ptr( dc );
851 return ret;
855 /***********************************************************************
856 * ResetDCA (GDI32.@)
858 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
860 DEVMODEW *devmodeW;
861 HDC ret;
863 if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
864 else devmodeW = NULL;
866 ret = ResetDCW(hdc, devmodeW);
868 HeapFree(GetProcessHeap(), 0, devmodeW);
869 return ret;
873 /***********************************************************************
874 * GetDeviceCaps (GDI32.@)
876 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
878 DC *dc;
879 INT ret = 0;
881 if ((dc = get_dc_ptr( hdc )))
883 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetDeviceCaps );
884 ret = physdev->funcs->pGetDeviceCaps( physdev, cap );
885 release_dc_ptr( dc );
887 return ret;
891 /***********************************************************************
892 * GetBkColor (GDI32.@)
894 COLORREF WINAPI GetBkColor( HDC hdc )
896 COLORREF ret = 0;
897 DC * dc = get_dc_ptr( hdc );
898 if (dc)
900 ret = dc->backgroundColor;
901 release_dc_ptr( dc );
903 return ret;
907 /***********************************************************************
908 * SetBkColor (GDI32.@)
910 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
912 COLORREF ret = CLR_INVALID;
913 DC * dc = get_dc_ptr( hdc );
915 TRACE("hdc=%p color=0x%08x\n", hdc, color);
917 if (dc)
919 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetBkColor );
920 ret = dc->backgroundColor;
921 dc->backgroundColor = physdev->funcs->pSetBkColor( physdev, color );
922 release_dc_ptr( dc );
924 return ret;
928 /***********************************************************************
929 * GetTextColor (GDI32.@)
931 COLORREF WINAPI GetTextColor( HDC hdc )
933 COLORREF ret = 0;
934 DC * dc = get_dc_ptr( hdc );
935 if (dc)
937 ret = dc->textColor;
938 release_dc_ptr( dc );
940 return ret;
944 /***********************************************************************
945 * SetTextColor (GDI32.@)
947 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
949 COLORREF ret = CLR_INVALID;
950 DC * dc = get_dc_ptr( hdc );
952 TRACE(" hdc=%p color=0x%08x\n", hdc, color);
954 if (dc)
956 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetTextColor );
957 ret = dc->textColor;
958 dc->textColor = physdev->funcs->pSetTextColor( physdev, color );
959 release_dc_ptr( dc );
961 return ret;
965 /***********************************************************************
966 * GetTextAlign (GDI32.@)
968 UINT WINAPI GetTextAlign( HDC hdc )
970 UINT ret = 0;
971 DC * dc = get_dc_ptr( hdc );
972 if (dc)
974 ret = dc->textAlign;
975 release_dc_ptr( dc );
977 return ret;
981 /***********************************************************************
982 * SetTextAlign (GDI32.@)
984 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
986 UINT ret = GDI_ERROR;
987 DC *dc = get_dc_ptr( hdc );
989 TRACE("hdc=%p align=%d\n", hdc, align);
991 if (dc)
993 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetTextAlign );
994 align = physdev->funcs->pSetTextAlign( physdev, align );
995 if (align != GDI_ERROR)
997 ret = dc->textAlign;
998 dc->textAlign = align;
1000 release_dc_ptr( dc );
1002 return ret;
1005 /***********************************************************************
1006 * GetDCOrgEx (GDI32.@)
1008 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1010 DC * dc;
1012 if (!lpp) return FALSE;
1013 if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1014 lpp->x = dc->vis_rect.left;
1015 lpp->y = dc->vis_rect.top;
1016 release_dc_ptr( dc );
1017 return TRUE;
1021 /***********************************************************************
1022 * GetGraphicsMode (GDI32.@)
1024 INT WINAPI GetGraphicsMode( HDC hdc )
1026 INT ret = 0;
1027 DC * dc = get_dc_ptr( hdc );
1028 if (dc)
1030 ret = dc->GraphicsMode;
1031 release_dc_ptr( dc );
1033 return ret;
1037 /***********************************************************************
1038 * SetGraphicsMode (GDI32.@)
1040 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1042 INT ret = 0;
1043 DC *dc = get_dc_ptr( hdc );
1045 /* One would think that setting the graphics mode to GM_COMPATIBLE
1046 * would also reset the world transformation matrix to the unity
1047 * matrix. However, in Windows, this is not the case. This doesn't
1048 * make a lot of sense to me, but that's the way it is.
1050 if (!dc) return 0;
1051 if ((mode > 0) && (mode <= GM_LAST))
1053 ret = dc->GraphicsMode;
1054 dc->GraphicsMode = mode;
1056 /* font metrics depend on the graphics mode */
1057 if (ret != mode) SelectObject(dc->hSelf, dc->hFont);
1058 release_dc_ptr( dc );
1059 return ret;
1063 /***********************************************************************
1064 * GetArcDirection (GDI32.@)
1066 INT WINAPI GetArcDirection( HDC hdc )
1068 INT ret = 0;
1069 DC * dc = get_dc_ptr( hdc );
1070 if (dc)
1072 ret = dc->ArcDirection;
1073 release_dc_ptr( dc );
1075 return ret;
1079 /***********************************************************************
1080 * SetArcDirection (GDI32.@)
1082 INT WINAPI SetArcDirection( HDC hdc, INT dir )
1084 DC * dc;
1085 INT ret = 0;
1087 if (dir != AD_COUNTERCLOCKWISE && dir != AD_CLOCKWISE)
1089 SetLastError(ERROR_INVALID_PARAMETER);
1090 return 0;
1093 if ((dc = get_dc_ptr( hdc )))
1095 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetArcDirection );
1096 dir = physdev->funcs->pSetArcDirection( physdev, dir );
1097 if (dir)
1099 ret = dc->ArcDirection;
1100 dc->ArcDirection = dir;
1102 release_dc_ptr( dc );
1104 return ret;
1108 /***********************************************************************
1109 * GetWorldTransform (GDI32.@)
1111 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1113 DC * dc;
1114 if (!xform) return FALSE;
1115 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1116 *xform = dc->xformWorld2Wnd;
1117 release_dc_ptr( dc );
1118 return TRUE;
1122 /***********************************************************************
1123 * GetTransform (GDI32.@)
1125 * Undocumented
1127 * Returns one of the co-ordinate space transforms
1129 * PARAMS
1130 * hdc [I] Device context.
1131 * which [I] Which xform to return:
1132 * 0x203 World -> Page transform (that set by SetWorldTransform).
1133 * 0x304 Page -> Device transform (the mapping mode transform).
1134 * 0x204 World -> Device transform (the combination of the above two).
1135 * 0x402 Device -> World transform (the inversion of the above).
1136 * xform [O] The xform.
1139 BOOL WINAPI GetTransform( HDC hdc, DWORD which, XFORM *xform )
1141 BOOL ret = TRUE;
1142 DC *dc = get_dc_ptr( hdc );
1143 if (!dc) return FALSE;
1145 switch(which)
1147 case 0x203:
1148 *xform = dc->xformWorld2Wnd;
1149 break;
1151 case 0x304:
1152 construct_window_to_viewport(dc, xform);
1153 break;
1155 case 0x204:
1156 *xform = dc->xformWorld2Vport;
1157 break;
1159 case 0x402:
1160 *xform = dc->xformVport2World;
1161 break;
1163 default:
1164 FIXME("Unknown code %x\n", which);
1165 ret = FALSE;
1168 release_dc_ptr( dc );
1169 return ret;
1173 /****************************************************************************
1174 * CombineTransform [GDI32.@]
1175 * Combines two transformation matrices.
1177 * PARAMS
1178 * xformResult [O] Stores the result of combining the two matrices
1179 * xform1 [I] Specifies the first matrix to apply
1180 * xform2 [I] Specifies the second matrix to apply
1182 * REMARKS
1183 * The same matrix can be passed in for more than one of the parameters.
1185 * RETURNS
1186 * Success: TRUE.
1187 * Failure: FALSE. Use GetLastError() to determine the cause.
1189 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1190 const XFORM *xform2 )
1192 XFORM xformTemp;
1194 /* Check for illegal parameters */
1195 if (!xformResult || !xform1 || !xform2)
1196 return FALSE;
1198 /* Create the result in a temporary XFORM, since xformResult may be
1199 * equal to xform1 or xform2 */
1200 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1201 xform1->eM12 * xform2->eM21;
1202 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1203 xform1->eM12 * xform2->eM22;
1204 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1205 xform1->eM22 * xform2->eM21;
1206 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1207 xform1->eM22 * xform2->eM22;
1208 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1209 xform1->eDy * xform2->eM21 +
1210 xform2->eDx;
1211 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1212 xform1->eDy * xform2->eM22 +
1213 xform2->eDy;
1215 /* Copy the result to xformResult */
1216 *xformResult = xformTemp;
1218 return TRUE;
1222 /***********************************************************************
1223 * SetDCHook (GDI32.@)
1225 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1227 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1229 DC *dc = get_dc_ptr( hdc );
1231 if (!dc) return FALSE;
1233 dc->dwHookData = dwHookData;
1234 dc->hookProc = hookProc;
1235 release_dc_ptr( dc );
1236 return TRUE;
1240 /***********************************************************************
1241 * GetDCHook (GDI32.@)
1243 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1245 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1247 DC *dc = get_dc_ptr( hdc );
1248 DWORD_PTR ret;
1250 if (!dc) return 0;
1251 if (proc) *proc = dc->hookProc;
1252 ret = dc->dwHookData;
1253 release_dc_ptr( dc );
1254 return ret;
1258 /***********************************************************************
1259 * SetHookFlags (GDI32.@)
1261 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1263 WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
1265 DC *dc = get_dc_obj( hdc ); /* not get_dc_ptr, this needs to work from any thread */
1266 LONG ret = 0;
1268 if (!dc) return 0;
1270 TRACE("hDC %p, flags %04x\n",hdc,flags);
1272 if (flags & DCHF_INVALIDATEVISRGN)
1273 ret = InterlockedExchange( &dc->dirty, 1 );
1274 else if (flags & DCHF_VALIDATEVISRGN || !flags)
1275 ret = InterlockedExchange( &dc->dirty, 0 );
1277 if (flags & DCHF_DISABLEDC)
1278 ret = InterlockedExchange( &dc->disabled, 1 );
1279 else if (flags & DCHF_ENABLEDC)
1280 ret = InterlockedExchange( &dc->disabled, 0 );
1282 GDI_ReleaseObj( hdc );
1284 if (flags & DCHF_RESETDC) ret = reset_dc_state( hdc );
1285 return ret;
1288 /***********************************************************************
1289 * SetICMMode (GDI32.@)
1291 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1293 /*FIXME: Assume that ICM is always off, and cannot be turned on */
1294 if (iEnableICM == ICM_OFF) return ICM_OFF;
1295 if (iEnableICM == ICM_ON) return 0;
1296 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1297 return 0;
1300 /***********************************************************************
1301 * GetDeviceGammaRamp (GDI32.@)
1303 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1305 BOOL ret = FALSE;
1306 DC *dc = get_dc_ptr( hDC );
1308 TRACE("%p, %p\n", hDC, ptr);
1309 if( dc )
1311 if (GetObjectType( hDC ) != OBJ_MEMDC)
1313 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetDeviceGammaRamp );
1314 ret = physdev->funcs->pGetDeviceGammaRamp( physdev, ptr );
1316 else SetLastError( ERROR_INVALID_PARAMETER );
1317 release_dc_ptr( dc );
1319 return ret;
1322 static BOOL check_gamma_ramps(void *ptr)
1324 WORD *ramp = ptr;
1326 while (ramp < (WORD*)ptr + 3 * 256)
1328 float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
1329 unsigned i, f, l, g_n, c;
1331 f = ramp[0];
1332 l = ramp[255];
1333 if (f >= l)
1335 TRACE("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
1336 return FALSE;
1338 r_d = l - f;
1339 g_min = g_max = g_avg = 0.0;
1341 /* check gamma ramp entries to estimate the gamma */
1342 TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
1343 for (i=1, g_n=0; i<255; i++)
1345 if (ramp[i] < f || ramp[i] > l)
1347 TRACE("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
1348 return FALSE;
1350 c = ramp[i] - f;
1351 if (!c) continue; /* avoid log(0) */
1353 /* normalize entry values into 0..1 range */
1354 r_x = i/255.0; r_y = c / r_d;
1355 /* compute logarithms of values */
1356 r_lx = log(r_x); r_ly = log(r_y);
1357 /* compute gamma for this entry */
1358 r_v = r_ly / r_lx;
1359 /* compute differential (error estimate) for this entry */
1360 /* some games use table-based logarithms that magnifies the error by 128 */
1361 r_e = -r_lx * 128 / (c * r_lx * r_lx);
1363 /* compute min & max while compensating for estimated error */
1364 if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
1365 if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
1367 /* add to average */
1368 g_avg += r_v;
1369 g_n++;
1372 if (!g_n)
1374 TRACE("no gamma data, shouldn't happen\n");
1375 return FALSE;
1377 g_avg /= g_n;
1378 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
1380 /* check that the gamma is reasonably uniform across the ramp */
1381 if (g_max - g_min > 12.8)
1383 TRACE("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
1384 return FALSE;
1387 /* check that the gamma is not too bright */
1388 if (g_avg < 0.2)
1390 TRACE("too bright gamma ( %5.3f), rejected\n", g_avg);
1391 return FALSE;
1394 ramp += 256;
1397 return TRUE;
1400 /***********************************************************************
1401 * SetDeviceGammaRamp (GDI32.@)
1403 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1405 BOOL ret = FALSE;
1406 DC *dc = get_dc_ptr( hDC );
1408 TRACE("%p, %p\n", hDC, ptr);
1409 if( dc )
1411 if (GetObjectType( hDC ) != OBJ_MEMDC)
1413 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDeviceGammaRamp );
1415 if (check_gamma_ramps(ptr))
1416 ret = physdev->funcs->pSetDeviceGammaRamp( physdev, ptr );
1418 else SetLastError( ERROR_INVALID_PARAMETER );
1419 release_dc_ptr( dc );
1421 return ret;
1424 /***********************************************************************
1425 * GetColorSpace (GDI32.@)
1427 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1429 /*FIXME Need to do whatever GetColorSpace actually does */
1430 return 0;
1433 /***********************************************************************
1434 * CreateColorSpaceA (GDI32.@)
1436 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1438 FIXME( "stub\n" );
1439 return 0;
1442 /***********************************************************************
1443 * CreateColorSpaceW (GDI32.@)
1445 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1447 FIXME( "stub\n" );
1448 return 0;
1451 /***********************************************************************
1452 * DeleteColorSpace (GDI32.@)
1454 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1456 FIXME( "stub\n" );
1458 return TRUE;
1461 /***********************************************************************
1462 * SetColorSpace (GDI32.@)
1464 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1466 FIXME( "stub\n" );
1468 return hColorSpace;
1471 /***********************************************************************
1472 * GetBoundsRect (GDI32.@)
1474 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1476 PHYSDEV physdev;
1477 RECT device_rect;
1478 UINT ret;
1479 DC *dc = get_dc_ptr( hdc );
1481 if ( !dc ) return 0;
1483 physdev = GET_DC_PHYSDEV( dc, pGetBoundsRect );
1484 ret = physdev->funcs->pGetBoundsRect( physdev, &device_rect, DCB_RESET );
1485 if (!ret)
1487 release_dc_ptr( dc );
1488 return 0;
1490 if (dc->bounds_enabled && ret == DCB_SET) add_bounds_rect( &dc->bounds, &device_rect );
1492 if (rect)
1494 if (is_rect_empty( &dc->bounds ))
1496 rect->left = rect->top = rect->right = rect->bottom = 0;
1497 ret = DCB_RESET;
1499 else
1501 *rect = dc->bounds;
1502 rect->left = max( rect->left, 0 );
1503 rect->top = max( rect->top, 0 );
1504 rect->right = min( rect->right, dc->vis_rect.right - dc->vis_rect.left );
1505 rect->bottom = min( rect->bottom, dc->vis_rect.bottom - dc->vis_rect.top );
1506 ret = DCB_SET;
1508 dp_to_lp( dc, (POINT *)rect, 2 );
1510 else ret = 0;
1512 if (flags & DCB_RESET) reset_bounds( &dc->bounds );
1513 release_dc_ptr( dc );
1514 return ret;
1518 /***********************************************************************
1519 * SetBoundsRect (GDI32.@)
1521 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1523 PHYSDEV physdev;
1524 UINT ret;
1525 DC *dc;
1527 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1528 if (!(dc = get_dc_ptr( hdc ))) return 0;
1530 physdev = GET_DC_PHYSDEV( dc, pSetBoundsRect );
1531 ret = physdev->funcs->pSetBoundsRect( physdev, &dc->bounds, flags );
1532 if (!ret)
1534 release_dc_ptr( dc );
1535 return 0;
1538 ret = (dc->bounds_enabled ? DCB_ENABLE : DCB_DISABLE) |
1539 (is_rect_empty( &dc->bounds ) ? ret & DCB_SET : DCB_SET);
1541 if (flags & DCB_RESET) reset_bounds( &dc->bounds );
1543 if ((flags & DCB_ACCUMULATE) && rect)
1545 RECT rc = *rect;
1547 lp_to_dp( dc, (POINT *)&rc, 2 );
1548 add_bounds_rect( &dc->bounds, &rc );
1551 if (flags & DCB_ENABLE) dc->bounds_enabled = TRUE;
1552 if (flags & DCB_DISABLE) dc->bounds_enabled = FALSE;
1554 release_dc_ptr( dc );
1555 return ret;
1559 /***********************************************************************
1560 * GetRelAbs (GDI32.@)
1562 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1564 INT ret = 0;
1565 DC *dc = get_dc_ptr( hdc );
1566 if (dc)
1568 ret = dc->relAbsMode;
1569 release_dc_ptr( dc );
1571 return ret;
1577 /***********************************************************************
1578 * GetBkMode (GDI32.@)
1580 INT WINAPI GetBkMode( HDC hdc )
1582 INT ret = 0;
1583 DC * dc = get_dc_ptr( hdc );
1584 if (dc)
1586 ret = dc->backgroundMode;
1587 release_dc_ptr( dc );
1589 return ret;
1593 /***********************************************************************
1594 * SetBkMode (GDI32.@)
1596 INT WINAPI SetBkMode( HDC hdc, INT mode )
1598 INT ret = 0;
1599 DC *dc;
1601 if ((mode <= 0) || (mode > BKMODE_LAST))
1603 SetLastError(ERROR_INVALID_PARAMETER);
1604 return 0;
1606 if ((dc = get_dc_ptr( hdc )))
1608 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetBkMode );
1609 mode = physdev->funcs->pSetBkMode( physdev, mode );
1610 if (mode)
1612 ret = dc->backgroundMode;
1613 dc->backgroundMode = mode;
1615 release_dc_ptr( dc );
1617 return ret;
1621 /***********************************************************************
1622 * GetROP2 (GDI32.@)
1624 INT WINAPI GetROP2( HDC hdc )
1626 INT ret = 0;
1627 DC * dc = get_dc_ptr( hdc );
1628 if (dc)
1630 ret = dc->ROPmode;
1631 release_dc_ptr( dc );
1633 return ret;
1637 /***********************************************************************
1638 * SetROP2 (GDI32.@)
1640 INT WINAPI SetROP2( HDC hdc, INT mode )
1642 INT ret = 0;
1643 DC *dc;
1645 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1647 SetLastError(ERROR_INVALID_PARAMETER);
1648 return 0;
1650 if ((dc = get_dc_ptr( hdc )))
1652 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetROP2 );
1653 mode = physdev->funcs->pSetROP2( physdev, mode );
1654 if (mode)
1656 ret = dc->ROPmode;
1657 dc->ROPmode = mode;
1659 release_dc_ptr( dc );
1661 return ret;
1665 /***********************************************************************
1666 * SetRelAbs (GDI32.@)
1668 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1670 INT ret = 0;
1671 DC *dc;
1673 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1675 SetLastError(ERROR_INVALID_PARAMETER);
1676 return 0;
1678 if ((dc = get_dc_ptr( hdc )))
1680 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetRelAbs );
1681 mode = physdev->funcs->pSetRelAbs( physdev, mode );
1682 if (mode)
1684 ret = dc->relAbsMode;
1685 dc->relAbsMode = mode;
1687 release_dc_ptr( dc );
1689 return ret;
1693 /***********************************************************************
1694 * GetPolyFillMode (GDI32.@)
1696 INT WINAPI GetPolyFillMode( HDC hdc )
1698 INT ret = 0;
1699 DC * dc = get_dc_ptr( hdc );
1700 if (dc)
1702 ret = dc->polyFillMode;
1703 release_dc_ptr( dc );
1705 return ret;
1709 /***********************************************************************
1710 * SetPolyFillMode (GDI32.@)
1712 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1714 INT ret = 0;
1715 DC *dc;
1717 if ((mode <= 0) || (mode > POLYFILL_LAST))
1719 SetLastError(ERROR_INVALID_PARAMETER);
1720 return 0;
1722 if ((dc = get_dc_ptr( hdc )))
1724 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPolyFillMode );
1725 mode = physdev->funcs->pSetPolyFillMode( physdev, mode );
1726 if (mode)
1728 ret = dc->polyFillMode;
1729 dc->polyFillMode = mode;
1731 release_dc_ptr( dc );
1733 return ret;
1737 /***********************************************************************
1738 * GetStretchBltMode (GDI32.@)
1740 INT WINAPI GetStretchBltMode( HDC hdc )
1742 INT ret = 0;
1743 DC * dc = get_dc_ptr( hdc );
1744 if (dc)
1746 ret = dc->stretchBltMode;
1747 release_dc_ptr( dc );
1749 return ret;
1753 /***********************************************************************
1754 * SetStretchBltMode (GDI32.@)
1756 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1758 INT ret = 0;
1759 DC *dc;
1761 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1763 SetLastError(ERROR_INVALID_PARAMETER);
1764 return 0;
1766 if ((dc = get_dc_ptr( hdc )))
1768 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetStretchBltMode );
1769 mode = physdev->funcs->pSetStretchBltMode( physdev, mode );
1770 if (mode)
1772 ret = dc->stretchBltMode;
1773 dc->stretchBltMode = mode;
1775 release_dc_ptr( dc );
1777 return ret;
1781 /***********************************************************************
1782 * GetMapMode (GDI32.@)
1784 INT WINAPI GetMapMode( HDC hdc )
1786 INT ret = 0;
1787 DC * dc = get_dc_ptr( hdc );
1788 if (dc)
1790 ret = dc->MapMode;
1791 release_dc_ptr( dc );
1793 return ret;
1797 /***********************************************************************
1798 * GetBrushOrgEx (GDI32.@)
1800 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1802 DC * dc = get_dc_ptr( hdc );
1803 if (!dc) return FALSE;
1804 *pt = dc->brush_org;
1805 release_dc_ptr( dc );
1806 return TRUE;
1810 /***********************************************************************
1811 * GetCurrentPositionEx (GDI32.@)
1813 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1815 DC * dc = get_dc_ptr( hdc );
1816 if (!dc) return FALSE;
1817 *pt = dc->cur_pos;
1818 release_dc_ptr( dc );
1819 return TRUE;
1823 /***********************************************************************
1824 * GetViewportExtEx (GDI32.@)
1826 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1828 DC * dc = get_dc_ptr( hdc );
1829 if (!dc) return FALSE;
1830 *size = dc->vport_ext;
1831 release_dc_ptr( dc );
1832 return TRUE;
1836 /***********************************************************************
1837 * GetViewportOrgEx (GDI32.@)
1839 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1841 DC * dc = get_dc_ptr( hdc );
1842 if (!dc) return FALSE;
1843 *pt = dc->vport_org;
1844 release_dc_ptr( dc );
1845 return TRUE;
1849 /***********************************************************************
1850 * GetWindowExtEx (GDI32.@)
1852 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1854 DC * dc = get_dc_ptr( hdc );
1855 if (!dc) return FALSE;
1856 *size = dc->wnd_ext;
1857 release_dc_ptr( dc );
1858 return TRUE;
1862 /***********************************************************************
1863 * GetWindowOrgEx (GDI32.@)
1865 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1867 DC * dc = get_dc_ptr( hdc );
1868 if (!dc) return FALSE;
1869 *pt = dc->wnd_org;
1870 release_dc_ptr( dc );
1871 return TRUE;
1875 /***********************************************************************
1876 * GetLayout (GDI32.@)
1878 * Gets left->right or right->left text layout flags of a dc.
1881 DWORD WINAPI GetLayout(HDC hdc)
1883 DWORD layout = GDI_ERROR;
1885 DC * dc = get_dc_ptr( hdc );
1886 if (dc)
1888 layout = dc->layout;
1889 release_dc_ptr( dc );
1892 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1894 return layout;
1897 /***********************************************************************
1898 * SetLayout (GDI32.@)
1900 * Sets left->right or right->left text layout flags of a dc.
1903 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1905 DWORD oldlayout = GDI_ERROR;
1907 DC * dc = get_dc_ptr( hdc );
1908 if (dc)
1910 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetLayout );
1911 layout = physdev->funcs->pSetLayout( physdev, layout );
1912 if (layout != GDI_ERROR)
1914 oldlayout = dc->layout;
1915 dc->layout = layout;
1916 if (layout != oldlayout)
1918 if (layout & LAYOUT_RTL) dc->MapMode = MM_ANISOTROPIC;
1919 DC_UpdateXforms( dc );
1922 release_dc_ptr( dc );
1925 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
1927 return oldlayout;
1930 /***********************************************************************
1931 * GetDCBrushColor (GDI32.@)
1933 COLORREF WINAPI GetDCBrushColor(HDC hdc)
1935 DC *dc;
1936 COLORREF dcBrushColor = CLR_INVALID;
1938 TRACE("hdc(%p)\n", hdc);
1940 dc = get_dc_ptr( hdc );
1941 if (dc)
1943 dcBrushColor = dc->dcBrushColor;
1944 release_dc_ptr( dc );
1947 return dcBrushColor;
1950 /***********************************************************************
1951 * SetDCBrushColor (GDI32.@)
1953 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
1955 DC *dc;
1956 COLORREF oldClr = CLR_INVALID;
1958 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
1960 dc = get_dc_ptr( hdc );
1961 if (dc)
1963 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDCBrushColor );
1964 crColor = physdev->funcs->pSetDCBrushColor( physdev, crColor );
1965 if (crColor != CLR_INVALID)
1967 oldClr = dc->dcBrushColor;
1968 dc->dcBrushColor = crColor;
1970 release_dc_ptr( dc );
1973 return oldClr;
1976 /***********************************************************************
1977 * GetDCPenColor (GDI32.@)
1979 COLORREF WINAPI GetDCPenColor(HDC hdc)
1981 DC *dc;
1982 COLORREF dcPenColor = CLR_INVALID;
1984 TRACE("hdc(%p)\n", hdc);
1986 dc = get_dc_ptr( hdc );
1987 if (dc)
1989 dcPenColor = dc->dcPenColor;
1990 release_dc_ptr( dc );
1993 return dcPenColor;
1996 /***********************************************************************
1997 * SetDCPenColor (GDI32.@)
1999 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
2001 DC *dc;
2002 COLORREF oldClr = CLR_INVALID;
2004 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2006 dc = get_dc_ptr( hdc );
2007 if (dc)
2009 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDCPenColor );
2010 crColor = physdev->funcs->pSetDCPenColor( physdev, crColor );
2011 if (crColor != CLR_INVALID)
2013 oldClr = dc->dcPenColor;
2014 dc->dcPenColor = crColor;
2016 release_dc_ptr( dc );
2019 return oldClr;
2022 /***********************************************************************
2023 * CancelDC (GDI32.@)
2025 BOOL WINAPI CancelDC(HDC hdc)
2027 FIXME("stub\n");
2028 return TRUE;
2031 /*******************************************************************
2032 * GetMiterLimit [GDI32.@]
2036 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2038 BOOL bRet = FALSE;
2039 DC *dc;
2041 TRACE("(%p,%p)\n", hdc, peLimit);
2043 dc = get_dc_ptr( hdc );
2044 if (dc)
2046 if (peLimit)
2047 *peLimit = dc->miterLimit;
2049 release_dc_ptr( dc );
2050 bRet = TRUE;
2052 return bRet;
2055 /*******************************************************************
2056 * SetMiterLimit [GDI32.@]
2060 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2062 BOOL bRet = FALSE;
2063 DC *dc;
2065 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2067 dc = get_dc_ptr( hdc );
2068 if (dc)
2070 if (peOldLimit)
2071 *peOldLimit = dc->miterLimit;
2072 dc->miterLimit = eNewLimit;
2073 release_dc_ptr( dc );
2074 bRet = TRUE;
2076 return bRet;
2079 /*******************************************************************
2080 * GdiIsMetaPrintDC [GDI32.@]
2082 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2084 FIXME("%p\n", hdc);
2085 return FALSE;
2088 /*******************************************************************
2089 * GdiIsMetaFileDC [GDI32.@]
2091 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2093 TRACE("%p\n", hdc);
2095 switch( GetObjectType( hdc ) )
2097 case OBJ_METADC:
2098 case OBJ_ENHMETADC:
2099 return TRUE;
2101 return FALSE;
2104 /*******************************************************************
2105 * GdiIsPlayMetafileDC [GDI32.@]
2107 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2109 FIXME("%p\n", hdc);
2110 return FALSE;