windowscodecs/tests: Use common wine_dbgstr_guid implementation from test.h.
[wine.git] / dlls / gdi32 / dc.c
blob9500de29bc44ff25645c9945914dae7493ee9d6c
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 DC *dc = GDI_GetObjPtr( hdc, 0 );
56 if (!dc) return NULL;
58 switch (GetObjectType( hdc ))
60 case OBJ_DC:
61 case OBJ_MEMDC:
62 case OBJ_METADC:
63 case OBJ_ENHMETADC:
64 return dc;
65 default:
66 GDI_ReleaseObj( hdc );
67 SetLastError( ERROR_INVALID_HANDLE );
68 return NULL;
73 /***********************************************************************
74 * set_initial_dc_state
76 static void set_initial_dc_state( DC *dc )
78 dc->wndOrgX = 0;
79 dc->wndOrgY = 0;
80 dc->wndExtX = 1;
81 dc->wndExtY = 1;
82 dc->vportOrgX = 0;
83 dc->vportOrgY = 0;
84 dc->vportExtX = 1;
85 dc->vportExtY = 1;
86 dc->miterLimit = 10.0f; /* 10.0 is the default, from MSDN */
87 dc->layout = 0;
88 dc->font_code_page = CP_ACP;
89 dc->ROPmode = R2_COPYPEN;
90 dc->polyFillMode = ALTERNATE;
91 dc->stretchBltMode = BLACKONWHITE;
92 dc->relAbsMode = ABSOLUTE;
93 dc->backgroundMode = OPAQUE;
94 dc->backgroundColor = RGB( 255, 255, 255 );
95 dc->dcBrushColor = RGB( 255, 255, 255 );
96 dc->dcPenColor = RGB( 0, 0, 0 );
97 dc->textColor = RGB( 0, 0, 0 );
98 dc->brushOrgX = 0;
99 dc->brushOrgY = 0;
100 dc->mapperFlags = 0;
101 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
102 dc->charExtra = 0;
103 dc->breakExtra = 0;
104 dc->breakRem = 0;
105 dc->MapMode = MM_TEXT;
106 dc->GraphicsMode = GM_COMPATIBLE;
107 dc->CursPosX = 0;
108 dc->CursPosY = 0;
109 dc->ArcDirection = AD_COUNTERCLOCKWISE;
110 dc->xformWorld2Wnd.eM11 = 1.0f;
111 dc->xformWorld2Wnd.eM12 = 0.0f;
112 dc->xformWorld2Wnd.eM21 = 0.0f;
113 dc->xformWorld2Wnd.eM22 = 1.0f;
114 dc->xformWorld2Wnd.eDx = 0.0f;
115 dc->xformWorld2Wnd.eDy = 0.0f;
116 dc->xformWorld2Vport = dc->xformWorld2Wnd;
117 dc->xformVport2World = dc->xformWorld2Wnd;
118 dc->vport2WorldValid = TRUE;
120 reset_bounds( &dc->bounds );
123 /***********************************************************************
124 * alloc_dc_ptr
126 DC *alloc_dc_ptr( WORD magic )
128 DC *dc;
130 if (!(dc = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dc) ))) return NULL;
132 dc->nulldrv.funcs = &null_driver;
133 dc->physDev = &dc->nulldrv;
134 dc->thread = GetCurrentThreadId();
135 dc->refcount = 1;
136 dc->hPen = GDI_inc_ref_count( GetStockObject( BLACK_PEN ));
137 dc->hBrush = GDI_inc_ref_count( GetStockObject( WHITE_BRUSH ));
138 dc->hFont = GDI_inc_ref_count( GetStockObject( SYSTEM_FONT ));
139 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
141 set_initial_dc_state( dc );
143 if (!(dc->hSelf = alloc_gdi_handle( dc, magic, &dc_funcs )))
145 HeapFree( GetProcessHeap(), 0, dc );
146 return NULL;
148 dc->nulldrv.hdc = dc->hSelf;
150 if (font_driver && !font_driver->pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL ))
152 free_dc_ptr( dc );
153 return NULL;
155 return dc;
160 /***********************************************************************
161 * free_dc_state
163 static void free_dc_state( DC *dc )
165 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
166 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
167 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
168 if (dc->region) DeleteObject( dc->region );
169 if (dc->path) free_gdi_path( dc->path );
170 HeapFree( GetProcessHeap(), 0, dc );
174 /***********************************************************************
175 * free_dc_ptr
177 void free_dc_ptr( DC *dc )
179 assert( dc->refcount == 1 );
181 while (dc->physDev != &dc->nulldrv)
183 PHYSDEV physdev = dc->physDev;
184 dc->physDev = physdev->next;
185 physdev->funcs->pDeleteDC( physdev );
187 GDI_dec_ref_count( dc->hPen );
188 GDI_dec_ref_count( dc->hBrush );
189 GDI_dec_ref_count( dc->hFont );
190 if (dc->hBitmap) GDI_dec_ref_count( dc->hBitmap );
191 free_gdi_handle( dc->hSelf );
192 free_dc_state( dc );
196 /***********************************************************************
197 * get_dc_ptr
199 * Retrieve a DC pointer but release the GDI lock.
201 DC *get_dc_ptr( HDC hdc )
203 DC *dc = get_dc_obj( hdc );
204 if (!dc) return NULL;
206 if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
208 dc->thread = GetCurrentThreadId();
210 else if (dc->thread != GetCurrentThreadId())
212 WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
213 GDI_ReleaseObj( hdc );
214 return NULL;
216 else InterlockedIncrement( &dc->refcount );
218 GDI_ReleaseObj( hdc );
219 return dc;
223 /***********************************************************************
224 * release_dc_ptr
226 void release_dc_ptr( DC *dc )
228 LONG ref;
230 dc->thread = 0;
231 ref = InterlockedDecrement( &dc->refcount );
232 assert( ref >= 0 );
233 if (ref) dc->thread = GetCurrentThreadId(); /* we still own it */
237 /***********************************************************************
238 * update_dc
240 * Make sure the DC vis region is up to date.
241 * This function may call up to USER so the GDI lock should _not_
242 * be held when calling it.
244 void update_dc( DC *dc )
246 if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookProc)
247 dc->hookProc( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
251 /***********************************************************************
252 * DC_DeleteObject
254 static BOOL DC_DeleteObject( HGDIOBJ handle )
256 return DeleteDC( handle );
260 /***********************************************************************
261 * DC_InitDC
263 * Setup device-specific DC values for a newly created DC.
265 void DC_InitDC( DC* dc )
267 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRealizeDefaultPalette );
268 physdev->funcs->pRealizeDefaultPalette( physdev );
269 SetTextColor( dc->hSelf, dc->textColor );
270 SetBkColor( dc->hSelf, dc->backgroundColor );
271 SelectObject( dc->hSelf, dc->hPen );
272 SelectObject( dc->hSelf, dc->hBrush );
273 SelectObject( dc->hSelf, dc->hFont );
274 update_dc_clipping( dc );
275 SetVirtualResolution( dc->hSelf, 0, 0, 0, 0 );
276 physdev = GET_DC_PHYSDEV( dc, pSetBoundsRect );
277 physdev->funcs->pSetBoundsRect( physdev, &dc->bounds, dc->bounds_enabled ? DCB_ENABLE : DCB_DISABLE );
281 /***********************************************************************
282 * DC_InvertXform
284 * Computes the inverse of the transformation xformSrc and stores it to
285 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
286 * is singular.
288 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
290 double determinant;
292 determinant = xformSrc->eM11*xformSrc->eM22 -
293 xformSrc->eM12*xformSrc->eM21;
294 if (determinant > -1e-12 && determinant < 1e-12)
295 return FALSE;
297 xformDest->eM11 = xformSrc->eM22 / determinant;
298 xformDest->eM12 = -xformSrc->eM12 / determinant;
299 xformDest->eM21 = -xformSrc->eM21 / determinant;
300 xformDest->eM22 = xformSrc->eM11 / determinant;
301 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
302 xformSrc->eDy * xformDest->eM21;
303 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
304 xformSrc->eDy * xformDest->eM22;
306 return TRUE;
309 /* Construct a transformation to do the window-to-viewport conversion */
310 static void construct_window_to_viewport(DC *dc, XFORM *xform)
312 double scaleX, scaleY;
313 scaleX = (double)dc->vportExtX / (double)dc->wndExtX;
314 scaleY = (double)dc->vportExtY / (double)dc->wndExtY;
316 if (dc->layout & LAYOUT_RTL) scaleX = -scaleX;
317 xform->eM11 = scaleX;
318 xform->eM12 = 0.0;
319 xform->eM21 = 0.0;
320 xform->eM22 = scaleY;
321 xform->eDx = (double)dc->vportOrgX - scaleX * (double)dc->wndOrgX;
322 xform->eDy = (double)dc->vportOrgY - scaleY * (double)dc->wndOrgY;
323 if (dc->layout & LAYOUT_RTL) xform->eDx = dc->vis_rect.right - dc->vis_rect.left - 1 - xform->eDx;
326 /***********************************************************************
327 * DC_UpdateXforms
329 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
330 * fields of the specified DC by creating a transformation that
331 * represents the current mapping mode and combining it with the DC's
332 * world transform. This function should be called whenever the
333 * parameters associated with the mapping mode (window and viewport
334 * extents and origins) or the world transform change.
336 void DC_UpdateXforms( DC *dc )
338 XFORM xformWnd2Vport, oldworld2vport;
340 construct_window_to_viewport(dc, &xformWnd2Vport);
342 oldworld2vport = dc->xformWorld2Vport;
343 /* Combine with the world transformation */
344 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
345 &xformWnd2Vport );
347 /* Create inverse of world-to-viewport transformation */
348 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
349 &dc->xformVport2World );
351 /* Reselect the font and pen back into the dc so that the size
352 gets updated. */
353 if (memcmp(&oldworld2vport, &dc->xformWorld2Vport, sizeof(oldworld2vport)) &&
354 !GdiIsMetaFileDC(dc->hSelf))
356 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
357 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_PEN));
362 /***********************************************************************
363 * nulldrv_SaveDC
365 INT nulldrv_SaveDC( PHYSDEV dev )
367 DC *newdc, *dc = get_nulldrv_dc( dev );
369 if (!(newdc = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*newdc )))) return 0;
370 newdc->layout = dc->layout;
371 newdc->hPen = dc->hPen;
372 newdc->hBrush = dc->hBrush;
373 newdc->hFont = dc->hFont;
374 newdc->hBitmap = dc->hBitmap;
375 newdc->hPalette = dc->hPalette;
376 newdc->ROPmode = dc->ROPmode;
377 newdc->polyFillMode = dc->polyFillMode;
378 newdc->stretchBltMode = dc->stretchBltMode;
379 newdc->relAbsMode = dc->relAbsMode;
380 newdc->backgroundMode = dc->backgroundMode;
381 newdc->backgroundColor = dc->backgroundColor;
382 newdc->textColor = dc->textColor;
383 newdc->dcBrushColor = dc->dcBrushColor;
384 newdc->dcPenColor = dc->dcPenColor;
385 newdc->brushOrgX = dc->brushOrgX;
386 newdc->brushOrgY = dc->brushOrgY;
387 newdc->mapperFlags = dc->mapperFlags;
388 newdc->textAlign = dc->textAlign;
389 newdc->charExtra = dc->charExtra;
390 newdc->breakExtra = dc->breakExtra;
391 newdc->breakRem = dc->breakRem;
392 newdc->MapMode = dc->MapMode;
393 newdc->GraphicsMode = dc->GraphicsMode;
394 newdc->CursPosX = dc->CursPosX;
395 newdc->CursPosY = dc->CursPosY;
396 newdc->ArcDirection = dc->ArcDirection;
397 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
398 newdc->xformWorld2Vport = dc->xformWorld2Vport;
399 newdc->xformVport2World = dc->xformVport2World;
400 newdc->vport2WorldValid = dc->vport2WorldValid;
401 newdc->wndOrgX = dc->wndOrgX;
402 newdc->wndOrgY = dc->wndOrgY;
403 newdc->wndExtX = dc->wndExtX;
404 newdc->wndExtY = dc->wndExtY;
405 newdc->vportOrgX = dc->vportOrgX;
406 newdc->vportOrgY = dc->vportOrgY;
407 newdc->vportExtX = dc->vportExtX;
408 newdc->vportExtY = dc->vportExtY;
409 newdc->virtual_res = dc->virtual_res;
410 newdc->virtual_size = dc->virtual_size;
412 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
414 if (dc->hClipRgn)
416 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
417 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
419 if (dc->hMetaRgn)
421 newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
422 CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
425 if (!PATH_SavePath( newdc, dc ))
427 release_dc_ptr( dc );
428 free_dc_state( newdc );
429 return 0;
432 newdc->saved_dc = dc->saved_dc;
433 dc->saved_dc = newdc;
434 return ++dc->saveLevel;
438 /***********************************************************************
439 * nulldrv_RestoreDC
441 BOOL nulldrv_RestoreDC( PHYSDEV dev, INT level )
443 DC *dcs, *first_dcs, *dc = get_nulldrv_dc( dev );
444 INT save_level;
446 /* find the state level to restore */
448 if (abs(level) > dc->saveLevel || level == 0) return FALSE;
449 if (level < 0) level = dc->saveLevel + level + 1;
450 first_dcs = dc->saved_dc;
451 for (dcs = first_dcs, save_level = dc->saveLevel; save_level > level; save_level--)
452 dcs = dcs->saved_dc;
454 /* restore the state */
456 if (!PATH_RestorePath( dc, dcs )) return FALSE;
458 dc->layout = dcs->layout;
459 dc->ROPmode = dcs->ROPmode;
460 dc->polyFillMode = dcs->polyFillMode;
461 dc->stretchBltMode = dcs->stretchBltMode;
462 dc->relAbsMode = dcs->relAbsMode;
463 dc->backgroundMode = dcs->backgroundMode;
464 dc->backgroundColor = dcs->backgroundColor;
465 dc->textColor = dcs->textColor;
466 dc->dcBrushColor = dcs->dcBrushColor;
467 dc->dcPenColor = dcs->dcPenColor;
468 dc->brushOrgX = dcs->brushOrgX;
469 dc->brushOrgY = dcs->brushOrgY;
470 dc->mapperFlags = dcs->mapperFlags;
471 dc->textAlign = dcs->textAlign;
472 dc->charExtra = dcs->charExtra;
473 dc->breakExtra = dcs->breakExtra;
474 dc->breakRem = dcs->breakRem;
475 dc->MapMode = dcs->MapMode;
476 dc->GraphicsMode = dcs->GraphicsMode;
477 dc->CursPosX = dcs->CursPosX;
478 dc->CursPosY = dcs->CursPosY;
479 dc->ArcDirection = dcs->ArcDirection;
480 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
481 dc->xformWorld2Vport = dcs->xformWorld2Vport;
482 dc->xformVport2World = dcs->xformVport2World;
483 dc->vport2WorldValid = dcs->vport2WorldValid;
484 dc->wndOrgX = dcs->wndOrgX;
485 dc->wndOrgY = dcs->wndOrgY;
486 dc->wndExtX = dcs->wndExtX;
487 dc->wndExtY = dcs->wndExtY;
488 dc->vportOrgX = dcs->vportOrgX;
489 dc->vportOrgY = dcs->vportOrgY;
490 dc->vportExtX = dcs->vportExtX;
491 dc->vportExtY = dcs->vportExtY;
492 dc->virtual_res = dcs->virtual_res;
493 dc->virtual_size = dcs->virtual_size;
495 if (dcs->hClipRgn)
497 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
498 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
500 else
502 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
503 dc->hClipRgn = 0;
505 if (dcs->hMetaRgn)
507 if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
508 CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
510 else
512 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
513 dc->hMetaRgn = 0;
515 DC_UpdateXforms( dc );
516 update_dc_clipping( dc );
518 SelectObject( dev->hdc, dcs->hBitmap );
519 SelectObject( dev->hdc, dcs->hBrush );
520 SelectObject( dev->hdc, dcs->hFont );
521 SelectObject( dev->hdc, dcs->hPen );
522 SetBkColor( dev->hdc, dcs->backgroundColor);
523 SetTextColor( dev->hdc, dcs->textColor);
524 GDISelectPalette( dev->hdc, dcs->hPalette, FALSE );
526 dc->saved_dc = dcs->saved_dc;
527 dcs->saved_dc = 0;
528 dc->saveLevel = save_level - 1;
530 /* now destroy all the saved DCs */
532 while (first_dcs)
534 DC *next = first_dcs->saved_dc;
535 free_dc_state( first_dcs );
536 first_dcs = next;
538 return TRUE;
542 /***********************************************************************
543 * reset_dc_state
545 static BOOL reset_dc_state( HDC hdc )
547 DC *dc, *dcs, *next;
549 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
551 set_initial_dc_state( dc );
552 SetBkColor( hdc, RGB( 255, 255, 255 ));
553 SetTextColor( hdc, RGB( 0, 0, 0 ));
554 SelectObject( hdc, GetStockObject( WHITE_BRUSH ));
555 SelectObject( hdc, GetStockObject( SYSTEM_FONT ));
556 SelectObject( hdc, GetStockObject( BLACK_PEN ));
557 SetVirtualResolution( hdc, 0, 0, 0, 0 );
558 GDISelectPalette( hdc, GetStockObject( DEFAULT_PALETTE ), FALSE );
559 SetBoundsRect( hdc, NULL, DCB_DISABLE );
560 AbortPath( hdc );
562 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
563 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
564 dc->hClipRgn = 0;
565 dc->hMetaRgn = 0;
566 update_dc_clipping( dc );
568 for (dcs = dc->saved_dc; dcs; dcs = next)
570 next = dcs->saved_dc;
571 free_dc_state( dcs );
573 dc->saved_dc = NULL;
574 dc->saveLevel = 0;
575 release_dc_ptr( dc );
576 return TRUE;
580 /***********************************************************************
581 * SaveDC (GDI32.@)
583 INT WINAPI SaveDC( HDC hdc )
585 DC * dc;
586 INT ret = 0;
588 if ((dc = get_dc_ptr( hdc )))
590 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSaveDC );
591 ret = physdev->funcs->pSaveDC( physdev );
592 release_dc_ptr( dc );
594 return ret;
598 /***********************************************************************
599 * RestoreDC (GDI32.@)
601 BOOL WINAPI RestoreDC( HDC hdc, INT level )
603 PHYSDEV physdev;
604 DC *dc;
605 BOOL success = FALSE;
607 TRACE("%p %d\n", hdc, level );
608 if ((dc = get_dc_ptr( hdc )))
610 update_dc( dc );
611 physdev = GET_DC_PHYSDEV( dc, pRestoreDC );
612 success = physdev->funcs->pRestoreDC( physdev, level );
613 release_dc_ptr( dc );
615 return success;
619 /***********************************************************************
620 * CreateDCW (GDI32.@)
622 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
623 const DEVMODEW *initData )
625 HDC hdc;
626 DC * dc;
627 const struct gdi_dc_funcs *funcs;
628 WCHAR buf[300];
630 GDI_CheckNotLock();
632 if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
634 if (!driver)
636 ERR( "no device found for %s\n", debugstr_w(device) );
637 return 0;
639 strcpyW(buf, driver);
642 if (!(funcs = DRIVER_load_driver( buf )))
644 ERR( "no driver found for %s\n", debugstr_w(buf) );
645 return 0;
647 if (!(dc = alloc_dc_ptr( OBJ_DC ))) return 0;
648 hdc = dc->hSelf;
650 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
652 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
653 debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
655 if (funcs->pCreateDC)
657 if (!funcs->pCreateDC( &dc->physDev, buf, device, output, initData ))
659 WARN("creation aborted by device\n" );
660 free_dc_ptr( dc );
661 return 0;
665 dc->vis_rect.left = 0;
666 dc->vis_rect.top = 0;
667 dc->vis_rect.right = GetDeviceCaps( hdc, DESKTOPHORZRES );
668 dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
670 DC_InitDC( dc );
671 release_dc_ptr( dc );
672 return hdc;
676 /***********************************************************************
677 * CreateDCA (GDI32.@)
679 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
680 const DEVMODEA *initData )
682 UNICODE_STRING driverW, deviceW, outputW;
683 DEVMODEW *initDataW;
684 HDC ret;
686 if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
687 else driverW.Buffer = NULL;
689 if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
690 else deviceW.Buffer = NULL;
692 if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
693 else outputW.Buffer = NULL;
695 initDataW = NULL;
696 if (initData)
698 /* don't convert initData for DISPLAY driver, it's not used */
699 if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
700 initDataW = GdiConvertToDevmodeW(initData);
703 ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
705 RtlFreeUnicodeString(&driverW);
706 RtlFreeUnicodeString(&deviceW);
707 RtlFreeUnicodeString(&outputW);
708 HeapFree(GetProcessHeap(), 0, initDataW);
709 return ret;
713 /***********************************************************************
714 * CreateICA (GDI32.@)
716 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
717 const DEVMODEA* initData )
719 /* Nothing special yet for ICs */
720 return CreateDCA( driver, device, output, initData );
724 /***********************************************************************
725 * CreateICW (GDI32.@)
727 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
728 const DEVMODEW* initData )
730 /* Nothing special yet for ICs */
731 return CreateDCW( driver, device, output, initData );
735 /***********************************************************************
736 * CreateCompatibleDC (GDI32.@)
738 HDC WINAPI CreateCompatibleDC( HDC hdc )
740 DC *dc, *origDC;
741 HDC ret;
742 const struct gdi_dc_funcs *funcs;
743 PHYSDEV physDev = NULL;
745 GDI_CheckNotLock();
747 if (hdc)
749 if (!(origDC = get_dc_ptr( hdc ))) return 0;
750 physDev = GET_DC_PHYSDEV( origDC, pCreateCompatibleDC );
751 funcs = physDev->funcs;
752 release_dc_ptr( origDC );
754 else funcs = DRIVER_load_driver( displayW );
756 if (!(dc = alloc_dc_ptr( OBJ_MEMDC ))) return 0;
758 TRACE("(%p): returning %p\n", hdc, dc->hSelf );
760 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
761 dc->vis_rect.left = 0;
762 dc->vis_rect.top = 0;
763 dc->vis_rect.right = 1;
764 dc->vis_rect.bottom = 1;
765 dc->device_rect = dc->vis_rect;
767 ret = dc->hSelf;
769 if (funcs->pCreateCompatibleDC && !funcs->pCreateCompatibleDC( physDev, &dc->physDev ))
771 WARN("creation aborted by device\n");
772 free_dc_ptr( dc );
773 return 0;
776 if (!dib_driver.pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL ))
778 free_dc_ptr( dc );
779 return 0;
781 physDev = GET_DC_PHYSDEV( dc, pSelectBitmap );
782 physDev->funcs->pSelectBitmap( physDev, dc->hBitmap );
784 DC_InitDC( dc );
785 release_dc_ptr( dc );
786 return ret;
790 /***********************************************************************
791 * DeleteDC (GDI32.@)
793 BOOL WINAPI DeleteDC( HDC hdc )
795 DC * dc;
797 TRACE("%p\n", hdc );
799 GDI_CheckNotLock();
801 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
802 if (dc->refcount != 1)
804 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
805 release_dc_ptr( dc );
806 return FALSE;
809 /* Call hook procedure to check whether is it OK to delete this DC */
810 if (dc->hookProc && !dc->hookProc( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ))
812 release_dc_ptr( dc );
813 return TRUE;
815 reset_dc_state( hdc );
816 free_dc_ptr( dc );
817 return TRUE;
821 /***********************************************************************
822 * ResetDCW (GDI32.@)
824 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
826 DC *dc;
827 HDC ret = 0;
829 if ((dc = get_dc_ptr( hdc )))
831 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pResetDC );
832 ret = physdev->funcs->pResetDC( physdev, devmode );
833 if (ret) /* reset the visible region */
835 dc->dirty = 0;
836 dc->vis_rect.left = 0;
837 dc->vis_rect.top = 0;
838 dc->vis_rect.right = GetDeviceCaps( hdc, DESKTOPHORZRES );
839 dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
840 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
841 dc->hVisRgn = 0;
842 update_dc_clipping( dc );
844 release_dc_ptr( dc );
846 return ret;
850 /***********************************************************************
851 * ResetDCA (GDI32.@)
853 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
855 DEVMODEW *devmodeW;
856 HDC ret;
858 if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
859 else devmodeW = NULL;
861 ret = ResetDCW(hdc, devmodeW);
863 HeapFree(GetProcessHeap(), 0, devmodeW);
864 return ret;
868 /***********************************************************************
869 * GetDeviceCaps (GDI32.@)
871 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
873 DC *dc;
874 INT ret = 0;
876 if ((dc = get_dc_ptr( hdc )))
878 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetDeviceCaps );
879 ret = physdev->funcs->pGetDeviceCaps( physdev, cap );
880 release_dc_ptr( dc );
882 return ret;
886 /***********************************************************************
887 * GetBkColor (GDI32.@)
889 COLORREF WINAPI GetBkColor( HDC hdc )
891 COLORREF ret = 0;
892 DC * dc = get_dc_ptr( hdc );
893 if (dc)
895 ret = dc->backgroundColor;
896 release_dc_ptr( dc );
898 return ret;
902 /***********************************************************************
903 * SetBkColor (GDI32.@)
905 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
907 COLORREF ret = CLR_INVALID;
908 DC * dc = get_dc_ptr( hdc );
910 TRACE("hdc=%p color=0x%08x\n", hdc, color);
912 if (dc)
914 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetBkColor );
915 ret = dc->backgroundColor;
916 dc->backgroundColor = physdev->funcs->pSetBkColor( physdev, color );
917 release_dc_ptr( dc );
919 return ret;
923 /***********************************************************************
924 * GetTextColor (GDI32.@)
926 COLORREF WINAPI GetTextColor( HDC hdc )
928 COLORREF ret = 0;
929 DC * dc = get_dc_ptr( hdc );
930 if (dc)
932 ret = dc->textColor;
933 release_dc_ptr( dc );
935 return ret;
939 /***********************************************************************
940 * SetTextColor (GDI32.@)
942 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
944 COLORREF ret = CLR_INVALID;
945 DC * dc = get_dc_ptr( hdc );
947 TRACE(" hdc=%p color=0x%08x\n", hdc, color);
949 if (dc)
951 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetTextColor );
952 ret = dc->textColor;
953 dc->textColor = physdev->funcs->pSetTextColor( physdev, color );
954 release_dc_ptr( dc );
956 return ret;
960 /***********************************************************************
961 * GetTextAlign (GDI32.@)
963 UINT WINAPI GetTextAlign( HDC hdc )
965 UINT ret = 0;
966 DC * dc = get_dc_ptr( hdc );
967 if (dc)
969 ret = dc->textAlign;
970 release_dc_ptr( dc );
972 return ret;
976 /***********************************************************************
977 * SetTextAlign (GDI32.@)
979 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
981 UINT ret = GDI_ERROR;
982 DC *dc = get_dc_ptr( hdc );
984 TRACE("hdc=%p align=%d\n", hdc, align);
986 if (dc)
988 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetTextAlign );
989 align = physdev->funcs->pSetTextAlign( physdev, align );
990 if (align != GDI_ERROR)
992 ret = dc->textAlign;
993 dc->textAlign = align;
995 release_dc_ptr( dc );
997 return ret;
1000 /***********************************************************************
1001 * GetDCOrgEx (GDI32.@)
1003 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1005 DC * dc;
1007 if (!lpp) return FALSE;
1008 if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1009 lpp->x = dc->vis_rect.left;
1010 lpp->y = dc->vis_rect.top;
1011 release_dc_ptr( dc );
1012 return TRUE;
1016 /***********************************************************************
1017 * GetGraphicsMode (GDI32.@)
1019 INT WINAPI GetGraphicsMode( HDC hdc )
1021 INT ret = 0;
1022 DC * dc = get_dc_ptr( hdc );
1023 if (dc)
1025 ret = dc->GraphicsMode;
1026 release_dc_ptr( dc );
1028 return ret;
1032 /***********************************************************************
1033 * SetGraphicsMode (GDI32.@)
1035 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1037 INT ret = 0;
1038 DC *dc = get_dc_ptr( hdc );
1040 /* One would think that setting the graphics mode to GM_COMPATIBLE
1041 * would also reset the world transformation matrix to the unity
1042 * matrix. However, in Windows, this is not the case. This doesn't
1043 * make a lot of sense to me, but that's the way it is.
1045 if (!dc) return 0;
1046 if ((mode > 0) && (mode <= GM_LAST))
1048 ret = dc->GraphicsMode;
1049 dc->GraphicsMode = mode;
1051 release_dc_ptr( dc );
1052 /* font metrics depend on the graphics mode */
1053 if (ret) SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
1054 return ret;
1058 /***********************************************************************
1059 * GetArcDirection (GDI32.@)
1061 INT WINAPI GetArcDirection( HDC hdc )
1063 INT ret = 0;
1064 DC * dc = get_dc_ptr( hdc );
1065 if (dc)
1067 ret = dc->ArcDirection;
1068 release_dc_ptr( dc );
1070 return ret;
1074 /***********************************************************************
1075 * SetArcDirection (GDI32.@)
1077 INT WINAPI SetArcDirection( HDC hdc, INT dir )
1079 DC * dc;
1080 INT ret = 0;
1082 if (dir != AD_COUNTERCLOCKWISE && dir != AD_CLOCKWISE)
1084 SetLastError(ERROR_INVALID_PARAMETER);
1085 return 0;
1088 if ((dc = get_dc_ptr( hdc )))
1090 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetArcDirection );
1091 dir = physdev->funcs->pSetArcDirection( physdev, dir );
1092 if (dir)
1094 ret = dc->ArcDirection;
1095 dc->ArcDirection = dir;
1097 release_dc_ptr( dc );
1099 return ret;
1103 /***********************************************************************
1104 * GetWorldTransform (GDI32.@)
1106 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1108 DC * dc;
1109 if (!xform) return FALSE;
1110 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1111 *xform = dc->xformWorld2Wnd;
1112 release_dc_ptr( dc );
1113 return TRUE;
1117 /***********************************************************************
1118 * GetTransform (GDI32.@)
1120 * Undocumented
1122 * Returns one of the co-ordinate space transforms
1124 * PARAMS
1125 * hdc [I] Device context.
1126 * which [I] Which xform to return:
1127 * 0x203 World -> Page transform (that set by SetWorldTransform).
1128 * 0x304 Page -> Device transform (the mapping mode transform).
1129 * 0x204 World -> Device transform (the combination of the above two).
1130 * 0x402 Device -> World transform (the inversion of the above).
1131 * xform [O] The xform.
1134 BOOL WINAPI GetTransform( HDC hdc, DWORD which, XFORM *xform )
1136 BOOL ret = TRUE;
1137 DC *dc = get_dc_ptr( hdc );
1138 if (!dc) return FALSE;
1140 switch(which)
1142 case 0x203:
1143 *xform = dc->xformWorld2Wnd;
1144 break;
1146 case 0x304:
1147 construct_window_to_viewport(dc, xform);
1148 break;
1150 case 0x204:
1151 *xform = dc->xformWorld2Vport;
1152 break;
1154 case 0x402:
1155 *xform = dc->xformVport2World;
1156 break;
1158 default:
1159 FIXME("Unknown code %x\n", which);
1160 ret = FALSE;
1163 release_dc_ptr( dc );
1164 return ret;
1168 /****************************************************************************
1169 * CombineTransform [GDI32.@]
1170 * Combines two transformation matrices.
1172 * PARAMS
1173 * xformResult [O] Stores the result of combining the two matrices
1174 * xform1 [I] Specifies the first matrix to apply
1175 * xform2 [I] Specifies the second matrix to apply
1177 * REMARKS
1178 * The same matrix can be passed in for more than one of the parameters.
1180 * RETURNS
1181 * Success: TRUE.
1182 * Failure: FALSE. Use GetLastError() to determine the cause.
1184 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1185 const XFORM *xform2 )
1187 XFORM xformTemp;
1189 /* Check for illegal parameters */
1190 if (!xformResult || !xform1 || !xform2)
1191 return FALSE;
1193 /* Create the result in a temporary XFORM, since xformResult may be
1194 * equal to xform1 or xform2 */
1195 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1196 xform1->eM12 * xform2->eM21;
1197 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1198 xform1->eM12 * xform2->eM22;
1199 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1200 xform1->eM22 * xform2->eM21;
1201 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1202 xform1->eM22 * xform2->eM22;
1203 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1204 xform1->eDy * xform2->eM21 +
1205 xform2->eDx;
1206 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1207 xform1->eDy * xform2->eM22 +
1208 xform2->eDy;
1210 /* Copy the result to xformResult */
1211 *xformResult = xformTemp;
1213 return TRUE;
1217 /***********************************************************************
1218 * SetDCHook (GDI32.@)
1220 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1222 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1224 DC *dc = get_dc_ptr( hdc );
1226 if (!dc) return FALSE;
1228 dc->dwHookData = dwHookData;
1229 dc->hookProc = hookProc;
1230 release_dc_ptr( dc );
1231 return TRUE;
1235 /***********************************************************************
1236 * GetDCHook (GDI32.@)
1238 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1240 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1242 DC *dc = get_dc_ptr( hdc );
1243 DWORD_PTR ret;
1245 if (!dc) return 0;
1246 if (proc) *proc = dc->hookProc;
1247 ret = dc->dwHookData;
1248 release_dc_ptr( dc );
1249 return ret;
1253 /***********************************************************************
1254 * SetHookFlags (GDI32.@)
1256 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1258 WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
1260 DC *dc = get_dc_obj( hdc ); /* not get_dc_ptr, this needs to work from any thread */
1261 LONG ret = 0;
1263 if (!dc) return 0;
1265 TRACE("hDC %p, flags %04x\n",hdc,flags);
1267 if (flags & DCHF_INVALIDATEVISRGN)
1268 ret = InterlockedExchange( &dc->dirty, 1 );
1269 else if (flags & DCHF_VALIDATEVISRGN || !flags)
1270 ret = InterlockedExchange( &dc->dirty, 0 );
1272 GDI_ReleaseObj( hdc );
1274 if (flags & DCHF_RESETDC) ret = reset_dc_state( hdc );
1275 return ret;
1278 /***********************************************************************
1279 * SetICMMode (GDI32.@)
1281 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1283 /*FIXME: Assume that ICM is always off, and cannot be turned on */
1284 if (iEnableICM == ICM_OFF) return ICM_OFF;
1285 if (iEnableICM == ICM_ON) return 0;
1286 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1287 return 0;
1290 /***********************************************************************
1291 * GetDeviceGammaRamp (GDI32.@)
1293 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1295 BOOL ret = FALSE;
1296 DC *dc = get_dc_ptr( hDC );
1298 TRACE("%p, %p\n", hDC, ptr);
1299 if( dc )
1301 if (GetObjectType( hDC ) != OBJ_MEMDC)
1303 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetDeviceGammaRamp );
1304 ret = physdev->funcs->pGetDeviceGammaRamp( physdev, ptr );
1306 else SetLastError( ERROR_INVALID_PARAMETER );
1307 release_dc_ptr( dc );
1309 return ret;
1312 static BOOL check_gamma_ramps(void *ptr)
1314 WORD *ramp = ptr;
1316 while (ramp < (WORD*)ptr + 3 * 256)
1318 float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
1319 unsigned i, f, l, g_n, c;
1321 f = ramp[0];
1322 l = ramp[255];
1323 if (f >= l)
1325 TRACE("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
1326 return FALSE;
1328 r_d = l - f;
1329 g_min = g_max = g_avg = 0.0;
1331 /* check gamma ramp entries to estimate the gamma */
1332 TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
1333 for (i=1, g_n=0; i<255; i++)
1335 if (ramp[i] < f || ramp[i] > l)
1337 TRACE("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
1338 return FALSE;
1340 c = ramp[i] - f;
1341 if (!c) continue; /* avoid log(0) */
1343 /* normalize entry values into 0..1 range */
1344 r_x = i/255.0; r_y = c / r_d;
1345 /* compute logarithms of values */
1346 r_lx = log(r_x); r_ly = log(r_y);
1347 /* compute gamma for this entry */
1348 r_v = r_ly / r_lx;
1349 /* compute differential (error estimate) for this entry */
1350 /* some games use table-based logarithms that magnifies the error by 128 */
1351 r_e = -r_lx * 128 / (c * r_lx * r_lx);
1353 /* compute min & max while compensating for estimated error */
1354 if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
1355 if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
1357 /* add to average */
1358 g_avg += r_v;
1359 g_n++;
1362 if (!g_n)
1364 TRACE("no gamma data, shouldn't happen\n");
1365 return FALSE;
1367 g_avg /= g_n;
1368 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
1370 /* check that the gamma is reasonably uniform across the ramp */
1371 if (g_max - g_min > 12.8)
1373 TRACE("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
1374 return FALSE;
1377 /* check that the gamma is not too bright */
1378 if (g_avg < 0.2)
1380 TRACE("too bright gamma ( %5.3f), rejected\n", g_avg);
1381 return FALSE;
1384 ramp += 256;
1387 return TRUE;
1390 /***********************************************************************
1391 * SetDeviceGammaRamp (GDI32.@)
1393 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1395 BOOL ret = FALSE;
1396 DC *dc = get_dc_ptr( hDC );
1398 TRACE("%p, %p\n", hDC, ptr);
1399 if( dc )
1401 if (GetObjectType( hDC ) != OBJ_MEMDC)
1403 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDeviceGammaRamp );
1405 if (check_gamma_ramps(ptr))
1406 ret = physdev->funcs->pSetDeviceGammaRamp( physdev, ptr );
1408 else SetLastError( ERROR_INVALID_PARAMETER );
1409 release_dc_ptr( dc );
1411 return ret;
1414 /***********************************************************************
1415 * GetColorSpace (GDI32.@)
1417 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1419 /*FIXME Need to do whatever GetColorSpace actually does */
1420 return 0;
1423 /***********************************************************************
1424 * CreateColorSpaceA (GDI32.@)
1426 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1428 FIXME( "stub\n" );
1429 return 0;
1432 /***********************************************************************
1433 * CreateColorSpaceW (GDI32.@)
1435 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1437 FIXME( "stub\n" );
1438 return 0;
1441 /***********************************************************************
1442 * DeleteColorSpace (GDI32.@)
1444 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1446 FIXME( "stub\n" );
1448 return TRUE;
1451 /***********************************************************************
1452 * SetColorSpace (GDI32.@)
1454 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1456 FIXME( "stub\n" );
1458 return hColorSpace;
1461 /***********************************************************************
1462 * GetBoundsRect (GDI32.@)
1464 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1466 PHYSDEV physdev;
1467 RECT device_rect;
1468 UINT ret;
1469 DC *dc = get_dc_ptr( hdc );
1471 if ( !dc ) return 0;
1473 physdev = GET_DC_PHYSDEV( dc, pGetBoundsRect );
1474 ret = physdev->funcs->pGetBoundsRect( physdev, &device_rect, DCB_RESET );
1475 if (!ret)
1477 release_dc_ptr( dc );
1478 return 0;
1480 if (dc->bounds_enabled && ret == DCB_SET) add_bounds_rect( &dc->bounds, &device_rect );
1482 if (rect)
1484 if (is_rect_empty( &dc->bounds ))
1486 rect->left = rect->top = rect->right = rect->bottom = 0;
1487 ret = DCB_RESET;
1489 else
1491 *rect = dc->bounds;
1492 rect->left = max( rect->left, 0 );
1493 rect->top = max( rect->top, 0 );
1494 rect->right = min( rect->right, dc->vis_rect.right - dc->vis_rect.left );
1495 rect->bottom = min( rect->bottom, dc->vis_rect.bottom - dc->vis_rect.top );
1496 ret = DCB_SET;
1498 DPtoLP( hdc, (POINT *)rect, 2 );
1500 else ret = 0;
1502 if (flags & DCB_RESET) reset_bounds( &dc->bounds );
1503 release_dc_ptr( dc );
1504 return ret;
1508 /***********************************************************************
1509 * SetBoundsRect (GDI32.@)
1511 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1513 PHYSDEV physdev;
1514 UINT ret;
1515 DC *dc;
1517 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1518 if (!(dc = get_dc_ptr( hdc ))) return 0;
1520 physdev = GET_DC_PHYSDEV( dc, pSetBoundsRect );
1521 ret = physdev->funcs->pSetBoundsRect( physdev, &dc->bounds, flags );
1522 if (!ret)
1524 release_dc_ptr( dc );
1525 return 0;
1528 ret = (dc->bounds_enabled ? DCB_ENABLE : DCB_DISABLE) |
1529 (is_rect_empty( &dc->bounds ) ? ret & DCB_SET : DCB_SET);
1531 if (flags & DCB_RESET) reset_bounds( &dc->bounds );
1533 if ((flags & DCB_ACCUMULATE) && rect)
1535 RECT rc = *rect;
1537 LPtoDP( hdc, (POINT *)&rc, 2 );
1538 add_bounds_rect( &dc->bounds, &rc );
1541 if (flags & DCB_ENABLE) dc->bounds_enabled = TRUE;
1542 if (flags & DCB_DISABLE) dc->bounds_enabled = FALSE;
1544 release_dc_ptr( dc );
1545 return ret;
1549 /***********************************************************************
1550 * GetRelAbs (GDI32.@)
1552 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1554 INT ret = 0;
1555 DC *dc = get_dc_ptr( hdc );
1556 if (dc)
1558 ret = dc->relAbsMode;
1559 release_dc_ptr( dc );
1561 return ret;
1567 /***********************************************************************
1568 * GetBkMode (GDI32.@)
1570 INT WINAPI GetBkMode( HDC hdc )
1572 INT ret = 0;
1573 DC * dc = get_dc_ptr( hdc );
1574 if (dc)
1576 ret = dc->backgroundMode;
1577 release_dc_ptr( dc );
1579 return ret;
1583 /***********************************************************************
1584 * SetBkMode (GDI32.@)
1586 INT WINAPI SetBkMode( HDC hdc, INT mode )
1588 INT ret = 0;
1589 DC *dc;
1591 if ((mode <= 0) || (mode > BKMODE_LAST))
1593 SetLastError(ERROR_INVALID_PARAMETER);
1594 return 0;
1596 if ((dc = get_dc_ptr( hdc )))
1598 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetBkMode );
1599 mode = physdev->funcs->pSetBkMode( physdev, mode );
1600 if (mode)
1602 ret = dc->backgroundMode;
1603 dc->backgroundMode = mode;
1605 release_dc_ptr( dc );
1607 return ret;
1611 /***********************************************************************
1612 * GetROP2 (GDI32.@)
1614 INT WINAPI GetROP2( HDC hdc )
1616 INT ret = 0;
1617 DC * dc = get_dc_ptr( hdc );
1618 if (dc)
1620 ret = dc->ROPmode;
1621 release_dc_ptr( dc );
1623 return ret;
1627 /***********************************************************************
1628 * SetROP2 (GDI32.@)
1630 INT WINAPI SetROP2( HDC hdc, INT mode )
1632 INT ret = 0;
1633 DC *dc;
1635 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1637 SetLastError(ERROR_INVALID_PARAMETER);
1638 return 0;
1640 if ((dc = get_dc_ptr( hdc )))
1642 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetROP2 );
1643 mode = physdev->funcs->pSetROP2( physdev, mode );
1644 if (mode)
1646 ret = dc->ROPmode;
1647 dc->ROPmode = mode;
1649 release_dc_ptr( dc );
1651 return ret;
1655 /***********************************************************************
1656 * SetRelAbs (GDI32.@)
1658 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1660 INT ret = 0;
1661 DC *dc;
1663 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1665 SetLastError(ERROR_INVALID_PARAMETER);
1666 return 0;
1668 if ((dc = get_dc_ptr( hdc )))
1670 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetRelAbs );
1671 mode = physdev->funcs->pSetRelAbs( physdev, mode );
1672 if (mode)
1674 ret = dc->relAbsMode;
1675 dc->relAbsMode = mode;
1677 release_dc_ptr( dc );
1679 return ret;
1683 /***********************************************************************
1684 * GetPolyFillMode (GDI32.@)
1686 INT WINAPI GetPolyFillMode( HDC hdc )
1688 INT ret = 0;
1689 DC * dc = get_dc_ptr( hdc );
1690 if (dc)
1692 ret = dc->polyFillMode;
1693 release_dc_ptr( dc );
1695 return ret;
1699 /***********************************************************************
1700 * SetPolyFillMode (GDI32.@)
1702 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1704 INT ret = 0;
1705 DC *dc;
1707 if ((mode <= 0) || (mode > POLYFILL_LAST))
1709 SetLastError(ERROR_INVALID_PARAMETER);
1710 return 0;
1712 if ((dc = get_dc_ptr( hdc )))
1714 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPolyFillMode );
1715 mode = physdev->funcs->pSetPolyFillMode( physdev, mode );
1716 if (mode)
1718 ret = dc->polyFillMode;
1719 dc->polyFillMode = mode;
1721 release_dc_ptr( dc );
1723 return ret;
1727 /***********************************************************************
1728 * GetStretchBltMode (GDI32.@)
1730 INT WINAPI GetStretchBltMode( HDC hdc )
1732 INT ret = 0;
1733 DC * dc = get_dc_ptr( hdc );
1734 if (dc)
1736 ret = dc->stretchBltMode;
1737 release_dc_ptr( dc );
1739 return ret;
1743 /***********************************************************************
1744 * SetStretchBltMode (GDI32.@)
1746 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1748 INT ret = 0;
1749 DC *dc;
1751 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1753 SetLastError(ERROR_INVALID_PARAMETER);
1754 return 0;
1756 if ((dc = get_dc_ptr( hdc )))
1758 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetStretchBltMode );
1759 mode = physdev->funcs->pSetStretchBltMode( physdev, mode );
1760 if (mode)
1762 ret = dc->stretchBltMode;
1763 dc->stretchBltMode = mode;
1765 release_dc_ptr( dc );
1767 return ret;
1771 /***********************************************************************
1772 * GetMapMode (GDI32.@)
1774 INT WINAPI GetMapMode( HDC hdc )
1776 INT ret = 0;
1777 DC * dc = get_dc_ptr( hdc );
1778 if (dc)
1780 ret = dc->MapMode;
1781 release_dc_ptr( dc );
1783 return ret;
1787 /***********************************************************************
1788 * GetBrushOrgEx (GDI32.@)
1790 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1792 DC * dc = get_dc_ptr( hdc );
1793 if (!dc) return FALSE;
1794 pt->x = dc->brushOrgX;
1795 pt->y = dc->brushOrgY;
1796 release_dc_ptr( dc );
1797 return TRUE;
1801 /***********************************************************************
1802 * GetCurrentPositionEx (GDI32.@)
1804 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1806 DC * dc = get_dc_ptr( hdc );
1807 if (!dc) return FALSE;
1808 pt->x = dc->CursPosX;
1809 pt->y = dc->CursPosY;
1810 release_dc_ptr( dc );
1811 return TRUE;
1815 /***********************************************************************
1816 * GetViewportExtEx (GDI32.@)
1818 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1820 DC * dc = get_dc_ptr( hdc );
1821 if (!dc) return FALSE;
1822 size->cx = dc->vportExtX;
1823 size->cy = dc->vportExtY;
1824 release_dc_ptr( dc );
1825 return TRUE;
1829 /***********************************************************************
1830 * GetViewportOrgEx (GDI32.@)
1832 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1834 DC * dc = get_dc_ptr( hdc );
1835 if (!dc) return FALSE;
1836 pt->x = dc->vportOrgX;
1837 pt->y = dc->vportOrgY;
1838 release_dc_ptr( dc );
1839 return TRUE;
1843 /***********************************************************************
1844 * GetWindowExtEx (GDI32.@)
1846 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1848 DC * dc = get_dc_ptr( hdc );
1849 if (!dc) return FALSE;
1850 size->cx = dc->wndExtX;
1851 size->cy = dc->wndExtY;
1852 release_dc_ptr( dc );
1853 return TRUE;
1857 /***********************************************************************
1858 * GetWindowOrgEx (GDI32.@)
1860 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1862 DC * dc = get_dc_ptr( hdc );
1863 if (!dc) return FALSE;
1864 pt->x = dc->wndOrgX;
1865 pt->y = dc->wndOrgY;
1866 release_dc_ptr( dc );
1867 return TRUE;
1871 /***********************************************************************
1872 * GetLayout (GDI32.@)
1874 * Gets left->right or right->left text layout flags of a dc.
1877 DWORD WINAPI GetLayout(HDC hdc)
1879 DWORD layout = GDI_ERROR;
1881 DC * dc = get_dc_ptr( hdc );
1882 if (dc)
1884 layout = dc->layout;
1885 release_dc_ptr( dc );
1888 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1890 return layout;
1893 /***********************************************************************
1894 * SetLayout (GDI32.@)
1896 * Sets left->right or right->left text layout flags of a dc.
1899 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1901 DWORD oldlayout = GDI_ERROR;
1903 DC * dc = get_dc_ptr( hdc );
1904 if (dc)
1906 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetLayout );
1907 layout = physdev->funcs->pSetLayout( physdev, layout );
1908 if (layout != GDI_ERROR)
1910 oldlayout = dc->layout;
1911 dc->layout = layout;
1912 if (layout != oldlayout)
1914 if (layout & LAYOUT_RTL) dc->MapMode = MM_ANISOTROPIC;
1915 DC_UpdateXforms( dc );
1918 release_dc_ptr( dc );
1921 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
1923 return oldlayout;
1926 /***********************************************************************
1927 * GetDCBrushColor (GDI32.@)
1929 COLORREF WINAPI GetDCBrushColor(HDC hdc)
1931 DC *dc;
1932 COLORREF dcBrushColor = CLR_INVALID;
1934 TRACE("hdc(%p)\n", hdc);
1936 dc = get_dc_ptr( hdc );
1937 if (dc)
1939 dcBrushColor = dc->dcBrushColor;
1940 release_dc_ptr( dc );
1943 return dcBrushColor;
1946 /***********************************************************************
1947 * SetDCBrushColor (GDI32.@)
1949 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
1951 DC *dc;
1952 COLORREF oldClr = CLR_INVALID;
1954 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
1956 dc = get_dc_ptr( hdc );
1957 if (dc)
1959 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDCBrushColor );
1960 crColor = physdev->funcs->pSetDCBrushColor( physdev, crColor );
1961 if (crColor != CLR_INVALID)
1963 oldClr = dc->dcBrushColor;
1964 dc->dcBrushColor = crColor;
1966 release_dc_ptr( dc );
1969 return oldClr;
1972 /***********************************************************************
1973 * GetDCPenColor (GDI32.@)
1975 COLORREF WINAPI GetDCPenColor(HDC hdc)
1977 DC *dc;
1978 COLORREF dcPenColor = CLR_INVALID;
1980 TRACE("hdc(%p)\n", hdc);
1982 dc = get_dc_ptr( hdc );
1983 if (dc)
1985 dcPenColor = dc->dcPenColor;
1986 release_dc_ptr( dc );
1989 return dcPenColor;
1992 /***********************************************************************
1993 * SetDCPenColor (GDI32.@)
1995 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
1997 DC *dc;
1998 COLORREF oldClr = CLR_INVALID;
2000 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2002 dc = get_dc_ptr( hdc );
2003 if (dc)
2005 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDCPenColor );
2006 crColor = physdev->funcs->pSetDCPenColor( physdev, crColor );
2007 if (crColor != CLR_INVALID)
2009 oldClr = dc->dcPenColor;
2010 dc->dcPenColor = crColor;
2012 release_dc_ptr( dc );
2015 return oldClr;
2018 /***********************************************************************
2019 * CancelDC (GDI32.@)
2021 BOOL WINAPI CancelDC(HDC hdc)
2023 FIXME("stub\n");
2024 return TRUE;
2027 /*******************************************************************
2028 * GetMiterLimit [GDI32.@]
2032 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2034 BOOL bRet = FALSE;
2035 DC *dc;
2037 TRACE("(%p,%p)\n", hdc, peLimit);
2039 dc = get_dc_ptr( hdc );
2040 if (dc)
2042 if (peLimit)
2043 *peLimit = dc->miterLimit;
2045 release_dc_ptr( dc );
2046 bRet = TRUE;
2048 return bRet;
2051 /*******************************************************************
2052 * SetMiterLimit [GDI32.@]
2056 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2058 BOOL bRet = FALSE;
2059 DC *dc;
2061 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2063 dc = get_dc_ptr( hdc );
2064 if (dc)
2066 if (peOldLimit)
2067 *peOldLimit = dc->miterLimit;
2068 dc->miterLimit = eNewLimit;
2069 release_dc_ptr( dc );
2070 bRet = TRUE;
2072 return bRet;
2075 /*******************************************************************
2076 * GdiIsMetaPrintDC [GDI32.@]
2078 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2080 FIXME("%p\n", hdc);
2081 return FALSE;
2084 /*******************************************************************
2085 * GdiIsMetaFileDC [GDI32.@]
2087 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2089 TRACE("%p\n", hdc);
2091 switch( GetObjectType( hdc ) )
2093 case OBJ_METADC:
2094 case OBJ_ENHMETADC:
2095 return TRUE;
2097 return FALSE;
2100 /*******************************************************************
2101 * GdiIsPlayMetafileDC [GDI32.@]
2103 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2105 FIXME("%p\n", hdc);
2106 return FALSE;