mfplat: Implement GetScanline0AndPitch() for d3d11 buffers.
[wine/zf.git] / dlls / gdi32 / dc.c
blob830fabf6e7814cad0dd119c574626d84bb51a985
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 <assert.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "winternl.h"
31 #include "winerror.h"
32 #include "gdi_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(dc);
37 static BOOL DC_DeleteObject( HGDIOBJ handle );
39 static const struct gdi_obj_funcs dc_funcs =
41 NULL, /* pSelectObject */
42 NULL, /* pGetObjectA */
43 NULL, /* pGetObjectW */
44 NULL, /* pUnrealizeObject */
45 DC_DeleteObject /* pDeleteObject */
49 static inline DC *get_dc_obj( HDC hdc )
51 WORD type;
52 DC *dc = get_any_obj_ptr( hdc, &type );
53 if (!dc) return NULL;
55 switch (type)
57 case OBJ_DC:
58 case OBJ_MEMDC:
59 case OBJ_METADC:
60 case OBJ_ENHMETADC:
61 return dc;
62 default:
63 GDI_ReleaseObj( hdc );
64 SetLastError( ERROR_INVALID_HANDLE );
65 return NULL;
70 /***********************************************************************
71 * set_initial_dc_state
73 static void set_initial_dc_state( DC *dc )
75 dc->wnd_org.x = 0;
76 dc->wnd_org.y = 0;
77 dc->wnd_ext.cx = 1;
78 dc->wnd_ext.cy = 1;
79 dc->vport_org.x = 0;
80 dc->vport_org.y = 0;
81 dc->vport_ext.cx = 1;
82 dc->vport_ext.cy = 1;
83 dc->miterLimit = 10.0f; /* 10.0 is the default, from MSDN */
84 dc->layout = 0;
85 dc->font_code_page = CP_ACP;
86 dc->ROPmode = R2_COPYPEN;
87 dc->polyFillMode = ALTERNATE;
88 dc->stretchBltMode = BLACKONWHITE;
89 dc->relAbsMode = ABSOLUTE;
90 dc->backgroundMode = OPAQUE;
91 dc->backgroundColor = RGB( 255, 255, 255 );
92 dc->dcBrushColor = RGB( 255, 255, 255 );
93 dc->dcPenColor = RGB( 0, 0, 0 );
94 dc->textColor = RGB( 0, 0, 0 );
95 dc->brush_org.x = 0;
96 dc->brush_org.y = 0;
97 dc->mapperFlags = 0;
98 dc->textAlign = TA_LEFT | TA_TOP | TA_NOUPDATECP;
99 dc->charExtra = 0;
100 dc->breakExtra = 0;
101 dc->breakRem = 0;
102 dc->MapMode = MM_TEXT;
103 dc->GraphicsMode = GM_COMPATIBLE;
104 dc->cur_pos.x = 0;
105 dc->cur_pos.y = 0;
106 dc->ArcDirection = AD_COUNTERCLOCKWISE;
107 dc->xformWorld2Wnd.eM11 = 1.0f;
108 dc->xformWorld2Wnd.eM12 = 0.0f;
109 dc->xformWorld2Wnd.eM21 = 0.0f;
110 dc->xformWorld2Wnd.eM22 = 1.0f;
111 dc->xformWorld2Wnd.eDx = 0.0f;
112 dc->xformWorld2Wnd.eDy = 0.0f;
113 dc->xformWorld2Vport = dc->xformWorld2Wnd;
114 dc->xformVport2World = dc->xformWorld2Wnd;
115 dc->vport2WorldValid = TRUE;
117 reset_bounds( &dc->bounds );
120 /***********************************************************************
121 * alloc_dc_ptr
123 DC *alloc_dc_ptr( WORD magic )
125 DC *dc;
127 if (!(dc = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dc) ))) return NULL;
129 dc->nulldrv.funcs = &null_driver;
130 dc->physDev = &dc->nulldrv;
131 dc->thread = GetCurrentThreadId();
132 dc->refcount = 1;
133 dc->hPen = GDI_inc_ref_count( GetStockObject( BLACK_PEN ));
134 dc->hBrush = GDI_inc_ref_count( GetStockObject( WHITE_BRUSH ));
135 dc->hFont = GDI_inc_ref_count( GetStockObject( SYSTEM_FONT ));
136 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
138 set_initial_dc_state( dc );
140 if (!(dc->hSelf = alloc_gdi_handle( dc, magic, &dc_funcs )))
142 HeapFree( GetProcessHeap(), 0, dc );
143 return NULL;
145 dc->nulldrv.hdc = dc->hSelf;
147 if (!font_driver.pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL ))
149 free_dc_ptr( dc );
150 return NULL;
152 return dc;
157 /***********************************************************************
158 * free_dc_state
160 static void free_dc_state( DC *dc )
162 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
163 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
164 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
165 if (dc->region) DeleteObject( dc->region );
166 if (dc->path) free_gdi_path( dc->path );
167 HeapFree( GetProcessHeap(), 0, dc );
171 /***********************************************************************
172 * free_dc_ptr
174 void free_dc_ptr( DC *dc )
176 assert( dc->refcount == 1 );
178 while (dc->physDev != &dc->nulldrv)
180 PHYSDEV physdev = dc->physDev;
181 dc->physDev = physdev->next;
182 physdev->funcs->pDeleteDC( physdev );
184 GDI_dec_ref_count( dc->hPen );
185 GDI_dec_ref_count( dc->hBrush );
186 GDI_dec_ref_count( dc->hFont );
187 if (dc->hBitmap) GDI_dec_ref_count( dc->hBitmap );
188 free_gdi_handle( dc->hSelf );
189 free_dc_state( dc );
193 /***********************************************************************
194 * get_dc_ptr
196 * Retrieve a DC pointer but release the GDI lock.
198 DC *get_dc_ptr( HDC hdc )
200 DC *dc = get_dc_obj( hdc );
201 if (!dc) return NULL;
202 if (dc->disabled)
204 GDI_ReleaseObj( hdc );
205 return NULL;
208 if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
210 dc->thread = GetCurrentThreadId();
212 else if (dc->thread != GetCurrentThreadId())
214 WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
215 GDI_ReleaseObj( hdc );
216 return NULL;
218 else InterlockedIncrement( &dc->refcount );
220 GDI_ReleaseObj( hdc );
221 return dc;
225 /***********************************************************************
226 * release_dc_ptr
228 void release_dc_ptr( DC *dc )
230 LONG ref;
232 dc->thread = 0;
233 ref = InterlockedDecrement( &dc->refcount );
234 assert( ref >= 0 );
235 if (ref) dc->thread = GetCurrentThreadId(); /* we still own it */
239 /***********************************************************************
240 * update_dc
242 * Make sure the DC vis region is up to date.
243 * This function may call up to USER so the GDI lock should _not_
244 * be held when calling it.
246 void update_dc( DC *dc )
248 if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookProc)
249 dc->hookProc( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
253 /***********************************************************************
254 * DC_DeleteObject
256 static BOOL DC_DeleteObject( HGDIOBJ handle )
258 return DeleteDC( handle );
262 /***********************************************************************
263 * DC_InitDC
265 * Setup device-specific DC values for a newly created DC.
267 void DC_InitDC( DC* dc )
269 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRealizeDefaultPalette );
270 physdev->funcs->pRealizeDefaultPalette( physdev );
271 SetTextColor( dc->hSelf, dc->textColor );
272 SetBkColor( dc->hSelf, dc->backgroundColor );
273 SelectObject( dc->hSelf, dc->hPen );
274 SelectObject( dc->hSelf, dc->hBrush );
275 SelectObject( dc->hSelf, dc->hFont );
276 update_dc_clipping( dc );
277 SetVirtualResolution( dc->hSelf, 0, 0, 0, 0 );
278 physdev = GET_DC_PHYSDEV( dc, pSetBoundsRect );
279 physdev->funcs->pSetBoundsRect( physdev, &dc->bounds, dc->bounds_enabled ? DCB_ENABLE : DCB_DISABLE );
283 /***********************************************************************
284 * DC_InvertXform
286 * Computes the inverse of the transformation xformSrc and stores it to
287 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
288 * is singular.
290 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
292 double determinant;
294 determinant = xformSrc->eM11*xformSrc->eM22 -
295 xformSrc->eM12*xformSrc->eM21;
296 if (determinant > -1e-12 && determinant < 1e-12)
297 return FALSE;
299 xformDest->eM11 = xformSrc->eM22 / determinant;
300 xformDest->eM12 = -xformSrc->eM12 / determinant;
301 xformDest->eM21 = -xformSrc->eM21 / determinant;
302 xformDest->eM22 = xformSrc->eM11 / determinant;
303 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
304 xformSrc->eDy * xformDest->eM21;
305 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
306 xformSrc->eDy * xformDest->eM22;
308 return TRUE;
311 /* Construct a transformation to do the window-to-viewport conversion */
312 static void construct_window_to_viewport(DC *dc, XFORM *xform)
314 double scaleX, scaleY;
315 scaleX = (double)dc->vport_ext.cx / (double)dc->wnd_ext.cx;
316 scaleY = (double)dc->vport_ext.cy / (double)dc->wnd_ext.cy;
318 if (dc->layout & LAYOUT_RTL) scaleX = -scaleX;
319 xform->eM11 = scaleX;
320 xform->eM12 = 0.0;
321 xform->eM21 = 0.0;
322 xform->eM22 = scaleY;
323 xform->eDx = (double)dc->vport_org.x - scaleX * (double)dc->wnd_org.x;
324 xform->eDy = (double)dc->vport_org.y - scaleY * (double)dc->wnd_org.y;
325 if (dc->layout & LAYOUT_RTL) xform->eDx = dc->vis_rect.right - dc->vis_rect.left - 1 - xform->eDx;
328 /***********************************************************************
329 * linear_xform_cmp
331 * Compares the linear transform portion of two XFORMs (i.e. the 2x2 submatrix).
332 * Returns 0 if they match.
334 static inline int linear_xform_cmp( const XFORM *a, const XFORM *b )
336 return memcmp( a, b, FIELD_OFFSET( XFORM, eDx ) );
339 /***********************************************************************
340 * DC_UpdateXforms
342 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
343 * fields of the specified DC by creating a transformation that
344 * represents the current mapping mode and combining it with the DC's
345 * world transform. This function should be called whenever the
346 * parameters associated with the mapping mode (window and viewport
347 * extents and origins) or the world transform change.
349 void DC_UpdateXforms( DC *dc )
351 XFORM xformWnd2Vport, oldworld2vport;
353 construct_window_to_viewport(dc, &xformWnd2Vport);
355 oldworld2vport = dc->xformWorld2Vport;
356 /* Combine with the world transformation */
357 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
358 &xformWnd2Vport );
360 /* Create inverse of world-to-viewport transformation */
361 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
362 &dc->xformVport2World );
364 /* Reselect the font and pen back into the dc so that the size
365 gets updated. */
366 if (linear_xform_cmp( &oldworld2vport, &dc->xformWorld2Vport ) &&
367 !GdiIsMetaFileDC(dc->hSelf))
369 SelectObject(dc->hSelf, dc->hFont);
370 SelectObject(dc->hSelf, dc->hPen);
375 /***********************************************************************
376 * nulldrv_SaveDC
378 INT CDECL nulldrv_SaveDC( PHYSDEV dev )
380 DC *newdc, *dc = get_nulldrv_dc( dev );
382 if (!(newdc = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*newdc )))) return 0;
383 newdc->layout = dc->layout;
384 newdc->hPen = dc->hPen;
385 newdc->hBrush = dc->hBrush;
386 newdc->hFont = dc->hFont;
387 newdc->hBitmap = dc->hBitmap;
388 newdc->hPalette = dc->hPalette;
389 newdc->ROPmode = dc->ROPmode;
390 newdc->polyFillMode = dc->polyFillMode;
391 newdc->stretchBltMode = dc->stretchBltMode;
392 newdc->relAbsMode = dc->relAbsMode;
393 newdc->backgroundMode = dc->backgroundMode;
394 newdc->backgroundColor = dc->backgroundColor;
395 newdc->textColor = dc->textColor;
396 newdc->dcBrushColor = dc->dcBrushColor;
397 newdc->dcPenColor = dc->dcPenColor;
398 newdc->brush_org = dc->brush_org;
399 newdc->mapperFlags = dc->mapperFlags;
400 newdc->textAlign = dc->textAlign;
401 newdc->charExtra = dc->charExtra;
402 newdc->breakExtra = dc->breakExtra;
403 newdc->breakRem = dc->breakRem;
404 newdc->MapMode = dc->MapMode;
405 newdc->GraphicsMode = dc->GraphicsMode;
406 newdc->cur_pos = dc->cur_pos;
407 newdc->ArcDirection = dc->ArcDirection;
408 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
409 newdc->xformWorld2Vport = dc->xformWorld2Vport;
410 newdc->xformVport2World = dc->xformVport2World;
411 newdc->vport2WorldValid = dc->vport2WorldValid;
412 newdc->wnd_org = dc->wnd_org;
413 newdc->wnd_ext = dc->wnd_ext;
414 newdc->vport_org = dc->vport_org;
415 newdc->vport_ext = dc->vport_ext;
416 newdc->virtual_res = dc->virtual_res;
417 newdc->virtual_size = dc->virtual_size;
419 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
421 if (dc->hClipRgn)
423 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
424 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
426 if (dc->hMetaRgn)
428 newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
429 CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
432 if (!PATH_SavePath( newdc, dc ))
434 release_dc_ptr( dc );
435 free_dc_state( newdc );
436 return 0;
439 newdc->saved_dc = dc->saved_dc;
440 dc->saved_dc = newdc;
441 return ++dc->saveLevel;
445 /***********************************************************************
446 * nulldrv_RestoreDC
448 BOOL CDECL nulldrv_RestoreDC( PHYSDEV dev, INT level )
450 DC *dcs, *first_dcs, *dc = get_nulldrv_dc( dev );
451 INT save_level;
453 /* find the state level to restore */
455 if (abs(level) > dc->saveLevel || level == 0) return FALSE;
456 if (level < 0) level = dc->saveLevel + level + 1;
457 first_dcs = dc->saved_dc;
458 for (dcs = first_dcs, save_level = dc->saveLevel; save_level > level; save_level--)
459 dcs = dcs->saved_dc;
461 /* restore the state */
463 if (!PATH_RestorePath( dc, dcs )) return FALSE;
465 dc->layout = dcs->layout;
466 dc->ROPmode = dcs->ROPmode;
467 dc->polyFillMode = dcs->polyFillMode;
468 dc->stretchBltMode = dcs->stretchBltMode;
469 dc->relAbsMode = dcs->relAbsMode;
470 dc->backgroundMode = dcs->backgroundMode;
471 dc->backgroundColor = dcs->backgroundColor;
472 dc->textColor = dcs->textColor;
473 dc->dcBrushColor = dcs->dcBrushColor;
474 dc->dcPenColor = dcs->dcPenColor;
475 dc->brush_org = dcs->brush_org;
476 dc->mapperFlags = dcs->mapperFlags;
477 dc->textAlign = dcs->textAlign;
478 dc->charExtra = dcs->charExtra;
479 dc->breakExtra = dcs->breakExtra;
480 dc->breakRem = dcs->breakRem;
481 dc->MapMode = dcs->MapMode;
482 dc->GraphicsMode = dcs->GraphicsMode;
483 dc->cur_pos = dcs->cur_pos;
484 dc->ArcDirection = dcs->ArcDirection;
485 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
486 dc->xformWorld2Vport = dcs->xformWorld2Vport;
487 dc->xformVport2World = dcs->xformVport2World;
488 dc->vport2WorldValid = dcs->vport2WorldValid;
489 dc->wnd_org = dcs->wnd_org;
490 dc->wnd_ext = dcs->wnd_ext;
491 dc->vport_org = dcs->vport_org;
492 dc->vport_ext = dcs->vport_ext;
493 dc->virtual_res = dcs->virtual_res;
494 dc->virtual_size = dcs->virtual_size;
496 if (dcs->hClipRgn)
498 if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
499 CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
501 else
503 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
504 dc->hClipRgn = 0;
506 if (dcs->hMetaRgn)
508 if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
509 CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
511 else
513 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
514 dc->hMetaRgn = 0;
516 DC_UpdateXforms( dc );
517 update_dc_clipping( dc );
519 SelectObject( dev->hdc, dcs->hBitmap );
520 SelectObject( dev->hdc, dcs->hBrush );
521 SelectObject( dev->hdc, dcs->hFont );
522 SelectObject( dev->hdc, dcs->hPen );
523 SetBkColor( dev->hdc, dcs->backgroundColor);
524 SetTextColor( dev->hdc, dcs->textColor);
525 GDISelectPalette( dev->hdc, dcs->hPalette, FALSE );
527 dc->saved_dc = dcs->saved_dc;
528 dcs->saved_dc = 0;
529 dc->saveLevel = save_level - 1;
531 /* now destroy all the saved DCs */
533 while (first_dcs)
535 DC *next = first_dcs->saved_dc;
536 free_dc_state( first_dcs );
537 first_dcs = next;
539 return TRUE;
543 /***********************************************************************
544 * reset_dc_state
546 static BOOL reset_dc_state( HDC hdc )
548 DC *dc, *dcs, *next;
550 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
552 set_initial_dc_state( dc );
553 SetBkColor( hdc, RGB( 255, 255, 255 ));
554 SetTextColor( hdc, RGB( 0, 0, 0 ));
555 SelectObject( hdc, GetStockObject( WHITE_BRUSH ));
556 SelectObject( hdc, GetStockObject( SYSTEM_FONT ));
557 SelectObject( hdc, GetStockObject( BLACK_PEN ));
558 SetVirtualResolution( hdc, 0, 0, 0, 0 );
559 GDISelectPalette( hdc, GetStockObject( DEFAULT_PALETTE ), FALSE );
560 SetBoundsRect( hdc, NULL, DCB_DISABLE );
561 AbortPath( hdc );
563 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
564 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
565 dc->hClipRgn = 0;
566 dc->hMetaRgn = 0;
567 update_dc_clipping( dc );
569 for (dcs = dc->saved_dc; dcs; dcs = next)
571 next = dcs->saved_dc;
572 free_dc_state( dcs );
574 dc->saved_dc = NULL;
575 dc->saveLevel = 0;
576 release_dc_ptr( dc );
577 return TRUE;
581 /***********************************************************************
582 * SaveDC (GDI32.@)
584 INT WINAPI SaveDC( HDC hdc )
586 DC * dc;
587 INT ret = 0;
589 if ((dc = get_dc_ptr( hdc )))
591 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSaveDC );
592 ret = physdev->funcs->pSaveDC( physdev );
593 release_dc_ptr( dc );
595 return ret;
599 /***********************************************************************
600 * RestoreDC (GDI32.@)
602 BOOL WINAPI RestoreDC( HDC hdc, INT level )
604 PHYSDEV physdev;
605 DC *dc;
606 BOOL success = FALSE;
608 TRACE("%p %d\n", hdc, level );
609 if ((dc = get_dc_ptr( hdc )))
611 update_dc( dc );
612 physdev = GET_DC_PHYSDEV( dc, pRestoreDC );
613 success = physdev->funcs->pRestoreDC( physdev, level );
614 release_dc_ptr( dc );
616 return success;
620 /***********************************************************************
621 * CreateDCW (GDI32.@)
623 HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
624 const DEVMODEW *initData )
626 const WCHAR *display, *p;
627 HDC hdc;
628 DC * dc;
629 const struct gdi_dc_funcs *funcs;
630 WCHAR buf[300];
632 GDI_CheckNotLock();
634 if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
636 if (!driver)
638 ERR( "no device found for %s\n", debugstr_w(device) );
639 return 0;
641 lstrcpyW(buf, driver);
644 if (!(funcs = DRIVER_load_driver( buf )))
646 ERR( "no driver found for %s\n", debugstr_w(buf) );
647 return 0;
649 if (!(dc = alloc_dc_ptr( OBJ_DC ))) return 0;
650 hdc = dc->hSelf;
652 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
654 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
655 debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
657 if (funcs->pCreateDC)
659 if (!funcs->pCreateDC( &dc->physDev, buf, device, output, initData ))
661 WARN("creation aborted by device\n" );
662 free_dc_ptr( dc );
663 return 0;
667 if (is_display_device( driver ))
668 display = driver;
669 else if (is_display_device( device ))
670 display = device;
671 else
672 display = NULL;
674 if (display)
676 /* Copy only the display name. For example, \\.\DISPLAY1 in \\.\DISPLAY1\Monitor0 */
677 p = display + 12;
678 while (iswdigit( *p ))
679 ++p;
680 lstrcpynW( dc->display, display, p - display + 1 );
681 dc->display[p - display] = '\0';
684 dc->vis_rect.left = 0;
685 dc->vis_rect.top = 0;
686 dc->vis_rect.right = GetDeviceCaps( hdc, DESKTOPHORZRES );
687 dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
689 DC_InitDC( dc );
690 release_dc_ptr( dc );
691 return hdc;
695 /***********************************************************************
696 * CreateDCA (GDI32.@)
698 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
699 const DEVMODEA *initData )
701 UNICODE_STRING driverW, deviceW, outputW;
702 DEVMODEW *initDataW;
703 HDC ret;
705 if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
706 else driverW.Buffer = NULL;
708 if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
709 else deviceW.Buffer = NULL;
711 if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
712 else outputW.Buffer = NULL;
714 initDataW = NULL;
715 if (initData)
717 /* don't convert initData for DISPLAY driver, it's not used */
718 if (!driverW.Buffer || wcsicmp( driverW.Buffer, L"display" ))
719 initDataW = GdiConvertToDevmodeW(initData);
722 ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
724 RtlFreeUnicodeString(&driverW);
725 RtlFreeUnicodeString(&deviceW);
726 RtlFreeUnicodeString(&outputW);
727 HeapFree(GetProcessHeap(), 0, initDataW);
728 return ret;
732 /***********************************************************************
733 * CreateICA (GDI32.@)
735 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
736 const DEVMODEA* initData )
738 /* Nothing special yet for ICs */
739 return CreateDCA( driver, device, output, initData );
743 /***********************************************************************
744 * CreateICW (GDI32.@)
746 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
747 const DEVMODEW* initData )
749 /* Nothing special yet for ICs */
750 return CreateDCW( driver, device, output, initData );
754 /***********************************************************************
755 * CreateCompatibleDC (GDI32.@)
757 HDC WINAPI CreateCompatibleDC( HDC hdc )
759 DC *dc, *origDC;
760 HDC ret;
761 const struct gdi_dc_funcs *funcs;
762 PHYSDEV physDev = NULL;
764 GDI_CheckNotLock();
766 if (hdc)
768 if (!(origDC = get_dc_ptr( hdc ))) return 0;
769 physDev = GET_DC_PHYSDEV( origDC, pCreateCompatibleDC );
770 funcs = physDev->funcs;
771 release_dc_ptr( origDC );
773 else funcs = DRIVER_load_driver( L"display" );
775 if (!(dc = alloc_dc_ptr( OBJ_MEMDC ))) return 0;
777 TRACE("(%p): returning %p\n", hdc, dc->hSelf );
779 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
780 dc->vis_rect.left = 0;
781 dc->vis_rect.top = 0;
782 dc->vis_rect.right = 1;
783 dc->vis_rect.bottom = 1;
784 dc->device_rect = dc->vis_rect;
786 ret = dc->hSelf;
788 if (funcs->pCreateCompatibleDC && !funcs->pCreateCompatibleDC( physDev, &dc->physDev ))
790 WARN("creation aborted by device\n");
791 free_dc_ptr( dc );
792 return 0;
795 if (!dib_driver.pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL ))
797 free_dc_ptr( dc );
798 return 0;
800 physDev = GET_DC_PHYSDEV( dc, pSelectBitmap );
801 physDev->funcs->pSelectBitmap( physDev, dc->hBitmap );
803 DC_InitDC( dc );
804 release_dc_ptr( dc );
805 return ret;
809 /***********************************************************************
810 * DeleteDC (GDI32.@)
812 BOOL WINAPI DeleteDC( HDC hdc )
814 DC * dc;
816 TRACE("%p\n", hdc );
818 GDI_CheckNotLock();
820 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
821 if (dc->refcount != 1)
823 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
824 release_dc_ptr( dc );
825 return FALSE;
828 /* Call hook procedure to check whether is it OK to delete this DC */
829 if (dc->hookProc && !dc->hookProc( dc->hSelf, DCHC_DELETEDC, dc->dwHookData, 0 ))
831 release_dc_ptr( dc );
832 return TRUE;
834 reset_dc_state( hdc );
835 free_dc_ptr( dc );
836 return TRUE;
840 /***********************************************************************
841 * ResetDCW (GDI32.@)
843 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
845 DC *dc;
846 HDC ret = 0;
848 if ((dc = get_dc_ptr( hdc )))
850 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pResetDC );
851 ret = physdev->funcs->pResetDC( physdev, devmode );
852 if (ret) /* reset the visible region */
854 dc->dirty = 0;
855 dc->vis_rect.left = 0;
856 dc->vis_rect.top = 0;
857 dc->vis_rect.right = GetDeviceCaps( hdc, DESKTOPHORZRES );
858 dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
859 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
860 dc->hVisRgn = 0;
861 update_dc_clipping( dc );
863 release_dc_ptr( dc );
865 return ret;
869 /***********************************************************************
870 * ResetDCA (GDI32.@)
872 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
874 DEVMODEW *devmodeW;
875 HDC ret;
877 if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
878 else devmodeW = NULL;
880 ret = ResetDCW(hdc, devmodeW);
882 HeapFree(GetProcessHeap(), 0, devmodeW);
883 return ret;
887 /***********************************************************************
888 * GetDeviceCaps (GDI32.@)
890 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
892 DC *dc;
893 INT ret = 0;
895 if ((dc = get_dc_ptr( hdc )))
897 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetDeviceCaps );
898 ret = physdev->funcs->pGetDeviceCaps( physdev, cap );
899 release_dc_ptr( dc );
901 return ret;
905 /***********************************************************************
906 * GetBkColor (GDI32.@)
908 COLORREF WINAPI GetBkColor( HDC hdc )
910 COLORREF ret = 0;
911 DC * dc = get_dc_ptr( hdc );
912 if (dc)
914 ret = dc->backgroundColor;
915 release_dc_ptr( dc );
917 return ret;
921 /***********************************************************************
922 * SetBkColor (GDI32.@)
924 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
926 COLORREF ret = CLR_INVALID;
927 DC * dc = get_dc_ptr( hdc );
929 TRACE("hdc=%p color=0x%08x\n", hdc, color);
931 if (dc)
933 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetBkColor );
934 ret = dc->backgroundColor;
935 dc->backgroundColor = physdev->funcs->pSetBkColor( physdev, color );
936 release_dc_ptr( dc );
938 return ret;
942 /***********************************************************************
943 * GetTextColor (GDI32.@)
945 COLORREF WINAPI GetTextColor( HDC hdc )
947 COLORREF ret = 0;
948 DC * dc = get_dc_ptr( hdc );
949 if (dc)
951 ret = dc->textColor;
952 release_dc_ptr( dc );
954 return ret;
958 /***********************************************************************
959 * SetTextColor (GDI32.@)
961 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
963 COLORREF ret = CLR_INVALID;
964 DC * dc = get_dc_ptr( hdc );
966 TRACE(" hdc=%p color=0x%08x\n", hdc, color);
968 if (dc)
970 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetTextColor );
971 ret = dc->textColor;
972 dc->textColor = physdev->funcs->pSetTextColor( physdev, color );
973 release_dc_ptr( dc );
975 return ret;
979 /***********************************************************************
980 * GetTextAlign (GDI32.@)
982 UINT WINAPI GetTextAlign( HDC hdc )
984 UINT ret = 0;
985 DC * dc = get_dc_ptr( hdc );
986 if (dc)
988 ret = dc->textAlign;
989 release_dc_ptr( dc );
991 return ret;
995 /***********************************************************************
996 * SetTextAlign (GDI32.@)
998 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
1000 UINT ret = GDI_ERROR;
1001 DC *dc = get_dc_ptr( hdc );
1003 TRACE("hdc=%p align=%d\n", hdc, align);
1005 if (dc)
1007 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetTextAlign );
1008 align = physdev->funcs->pSetTextAlign( physdev, align );
1009 if (align != GDI_ERROR)
1011 ret = dc->textAlign;
1012 dc->textAlign = align;
1014 release_dc_ptr( dc );
1016 return ret;
1019 /***********************************************************************
1020 * GetDCOrgEx (GDI32.@)
1022 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1024 DC * dc;
1026 if (!lpp) return FALSE;
1027 if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1028 lpp->x = dc->vis_rect.left;
1029 lpp->y = dc->vis_rect.top;
1030 release_dc_ptr( dc );
1031 return TRUE;
1035 /***********************************************************************
1036 * GetGraphicsMode (GDI32.@)
1038 INT WINAPI GetGraphicsMode( HDC hdc )
1040 INT ret = 0;
1041 DC * dc = get_dc_ptr( hdc );
1042 if (dc)
1044 ret = dc->GraphicsMode;
1045 release_dc_ptr( dc );
1047 return ret;
1051 /***********************************************************************
1052 * SetGraphicsMode (GDI32.@)
1054 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1056 INT ret = 0;
1057 DC *dc = get_dc_ptr( hdc );
1059 /* One would think that setting the graphics mode to GM_COMPATIBLE
1060 * would also reset the world transformation matrix to the unity
1061 * matrix. However, in Windows, this is not the case. This doesn't
1062 * make a lot of sense to me, but that's the way it is.
1064 if (!dc) return 0;
1065 if ((mode > 0) && (mode <= GM_LAST))
1067 ret = dc->GraphicsMode;
1068 dc->GraphicsMode = mode;
1070 /* font metrics depend on the graphics mode */
1071 if (ret != mode) SelectObject(dc->hSelf, dc->hFont);
1072 release_dc_ptr( dc );
1073 return ret;
1077 /***********************************************************************
1078 * GetArcDirection (GDI32.@)
1080 INT WINAPI GetArcDirection( HDC hdc )
1082 INT ret = 0;
1083 DC * dc = get_dc_ptr( hdc );
1084 if (dc)
1086 ret = dc->ArcDirection;
1087 release_dc_ptr( dc );
1089 return ret;
1093 /***********************************************************************
1094 * SetArcDirection (GDI32.@)
1096 INT WINAPI SetArcDirection( HDC hdc, INT dir )
1098 DC * dc;
1099 INT ret = 0;
1101 if (dir != AD_COUNTERCLOCKWISE && dir != AD_CLOCKWISE)
1103 SetLastError(ERROR_INVALID_PARAMETER);
1104 return 0;
1107 if ((dc = get_dc_ptr( hdc )))
1109 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetArcDirection );
1110 dir = physdev->funcs->pSetArcDirection( physdev, dir );
1111 if (dir)
1113 ret = dc->ArcDirection;
1114 dc->ArcDirection = dir;
1116 release_dc_ptr( dc );
1118 return ret;
1122 /***********************************************************************
1123 * GetWorldTransform (GDI32.@)
1125 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1127 DC * dc;
1128 if (!xform) return FALSE;
1129 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1130 *xform = dc->xformWorld2Wnd;
1131 release_dc_ptr( dc );
1132 return TRUE;
1136 /***********************************************************************
1137 * GetTransform (GDI32.@)
1139 * Undocumented
1141 * Returns one of the co-ordinate space transforms
1143 * PARAMS
1144 * hdc [I] Device context.
1145 * which [I] Which xform to return:
1146 * 0x203 World -> Page transform (that set by SetWorldTransform).
1147 * 0x304 Page -> Device transform (the mapping mode transform).
1148 * 0x204 World -> Device transform (the combination of the above two).
1149 * 0x402 Device -> World transform (the inversion of the above).
1150 * xform [O] The xform.
1153 BOOL WINAPI GetTransform( HDC hdc, DWORD which, XFORM *xform )
1155 BOOL ret = TRUE;
1156 DC *dc = get_dc_ptr( hdc );
1157 if (!dc) return FALSE;
1159 switch(which)
1161 case 0x203:
1162 *xform = dc->xformWorld2Wnd;
1163 break;
1165 case 0x304:
1166 construct_window_to_viewport(dc, xform);
1167 break;
1169 case 0x204:
1170 *xform = dc->xformWorld2Vport;
1171 break;
1173 case 0x402:
1174 *xform = dc->xformVport2World;
1175 break;
1177 default:
1178 FIXME("Unknown code %x\n", which);
1179 ret = FALSE;
1182 release_dc_ptr( dc );
1183 return ret;
1187 /****************************************************************************
1188 * CombineTransform [GDI32.@]
1189 * Combines two transformation matrices.
1191 * PARAMS
1192 * xformResult [O] Stores the result of combining the two matrices
1193 * xform1 [I] Specifies the first matrix to apply
1194 * xform2 [I] Specifies the second matrix to apply
1196 * REMARKS
1197 * The same matrix can be passed in for more than one of the parameters.
1199 * RETURNS
1200 * Success: TRUE.
1201 * Failure: FALSE. Use GetLastError() to determine the cause.
1203 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1204 const XFORM *xform2 )
1206 XFORM xformTemp;
1208 /* Check for illegal parameters */
1209 if (!xformResult || !xform1 || !xform2)
1210 return FALSE;
1212 /* Create the result in a temporary XFORM, since xformResult may be
1213 * equal to xform1 or xform2 */
1214 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1215 xform1->eM12 * xform2->eM21;
1216 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1217 xform1->eM12 * xform2->eM22;
1218 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1219 xform1->eM22 * xform2->eM21;
1220 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1221 xform1->eM22 * xform2->eM22;
1222 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1223 xform1->eDy * xform2->eM21 +
1224 xform2->eDx;
1225 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1226 xform1->eDy * xform2->eM22 +
1227 xform2->eDy;
1229 /* Copy the result to xformResult */
1230 *xformResult = xformTemp;
1232 return TRUE;
1236 /***********************************************************************
1237 * SetDCHook (GDI32.@)
1239 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1241 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1243 DC *dc = get_dc_ptr( hdc );
1245 if (!dc) return FALSE;
1247 dc->dwHookData = dwHookData;
1248 dc->hookProc = hookProc;
1249 release_dc_ptr( dc );
1250 return TRUE;
1254 /***********************************************************************
1255 * GetDCHook (GDI32.@)
1257 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1259 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1261 DC *dc = get_dc_ptr( hdc );
1262 DWORD_PTR ret;
1264 if (!dc) return 0;
1265 if (proc) *proc = dc->hookProc;
1266 ret = dc->dwHookData;
1267 release_dc_ptr( dc );
1268 return ret;
1272 /***********************************************************************
1273 * SetHookFlags (GDI32.@)
1275 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1277 WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
1279 DC *dc = get_dc_obj( hdc ); /* not get_dc_ptr, this needs to work from any thread */
1280 LONG ret = 0;
1282 if (!dc) return 0;
1284 TRACE("hDC %p, flags %04x\n",hdc,flags);
1286 if (flags & DCHF_INVALIDATEVISRGN)
1287 ret = InterlockedExchange( &dc->dirty, 1 );
1288 else if (flags & DCHF_VALIDATEVISRGN || !flags)
1289 ret = InterlockedExchange( &dc->dirty, 0 );
1291 if (flags & DCHF_DISABLEDC)
1292 ret = InterlockedExchange( &dc->disabled, 1 );
1293 else if (flags & DCHF_ENABLEDC)
1294 ret = InterlockedExchange( &dc->disabled, 0 );
1296 GDI_ReleaseObj( hdc );
1298 if (flags & DCHF_RESETDC) ret = reset_dc_state( hdc );
1299 return ret;
1302 /***********************************************************************
1303 * SetICMMode (GDI32.@)
1305 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1307 /*FIXME: Assume that ICM is always off, and cannot be turned on */
1308 if (iEnableICM == ICM_OFF) return ICM_OFF;
1309 if (iEnableICM == ICM_ON) return 0;
1310 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1311 return 0;
1314 /***********************************************************************
1315 * GetDeviceGammaRamp (GDI32.@)
1317 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1319 BOOL ret = FALSE;
1320 DC *dc = get_dc_ptr( hDC );
1322 TRACE("%p, %p\n", hDC, ptr);
1323 if( dc )
1325 if (GetObjectType( hDC ) != OBJ_MEMDC)
1327 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetDeviceGammaRamp );
1328 ret = physdev->funcs->pGetDeviceGammaRamp( physdev, ptr );
1330 else SetLastError( ERROR_INVALID_PARAMETER );
1331 release_dc_ptr( dc );
1333 return ret;
1336 static BOOL check_gamma_ramps(void *ptr)
1338 WORD *ramp = ptr;
1340 while (ramp < (WORD*)ptr + 3 * 256)
1342 float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
1343 unsigned i, f, l, g_n, c;
1345 f = ramp[0];
1346 l = ramp[255];
1347 if (f >= l)
1349 TRACE("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
1350 return FALSE;
1352 r_d = l - f;
1353 g_min = g_max = g_avg = 0.0;
1355 /* check gamma ramp entries to estimate the gamma */
1356 TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
1357 for (i=1, g_n=0; i<255; i++)
1359 if (ramp[i] < f || ramp[i] > l)
1361 TRACE("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
1362 return FALSE;
1364 c = ramp[i] - f;
1365 if (!c) continue; /* avoid log(0) */
1367 /* normalize entry values into 0..1 range */
1368 r_x = i/255.0; r_y = c / r_d;
1369 /* compute logarithms of values */
1370 r_lx = log(r_x); r_ly = log(r_y);
1371 /* compute gamma for this entry */
1372 r_v = r_ly / r_lx;
1373 /* compute differential (error estimate) for this entry */
1374 /* some games use table-based logarithms that magnifies the error by 128 */
1375 r_e = -r_lx * 128 / (c * r_lx * r_lx);
1377 /* compute min & max while compensating for estimated error */
1378 if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
1379 if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
1381 /* add to average */
1382 g_avg += r_v;
1383 g_n++;
1386 if (!g_n)
1388 TRACE("no gamma data, shouldn't happen\n");
1389 return FALSE;
1391 g_avg /= g_n;
1392 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
1394 /* check that the gamma is reasonably uniform across the ramp */
1395 if (g_max - g_min > 12.8)
1397 TRACE("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
1398 return FALSE;
1401 /* check that the gamma is not too bright */
1402 if (g_avg < 0.2)
1404 TRACE("too bright gamma ( %5.3f), rejected\n", g_avg);
1405 return FALSE;
1408 ramp += 256;
1411 return TRUE;
1414 /***********************************************************************
1415 * SetDeviceGammaRamp (GDI32.@)
1417 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1419 BOOL ret = FALSE;
1420 DC *dc = get_dc_ptr( hDC );
1422 TRACE("%p, %p\n", hDC, ptr);
1423 if( dc )
1425 if (GetObjectType( hDC ) != OBJ_MEMDC)
1427 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDeviceGammaRamp );
1429 if (check_gamma_ramps(ptr))
1430 ret = physdev->funcs->pSetDeviceGammaRamp( physdev, ptr );
1432 else SetLastError( ERROR_INVALID_PARAMETER );
1433 release_dc_ptr( dc );
1435 return ret;
1438 /***********************************************************************
1439 * GetColorSpace (GDI32.@)
1441 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1443 /*FIXME Need to do whatever GetColorSpace actually does */
1444 return 0;
1447 /***********************************************************************
1448 * CreateColorSpaceA (GDI32.@)
1450 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1452 FIXME( "stub\n" );
1453 return 0;
1456 /***********************************************************************
1457 * CreateColorSpaceW (GDI32.@)
1459 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1461 FIXME( "stub\n" );
1462 return 0;
1465 /***********************************************************************
1466 * DeleteColorSpace (GDI32.@)
1468 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1470 FIXME( "stub\n" );
1472 return TRUE;
1475 /***********************************************************************
1476 * SetColorSpace (GDI32.@)
1478 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1480 FIXME( "stub\n" );
1482 return hColorSpace;
1485 /***********************************************************************
1486 * GetBoundsRect (GDI32.@)
1488 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1490 PHYSDEV physdev;
1491 RECT device_rect;
1492 UINT ret;
1493 DC *dc = get_dc_ptr( hdc );
1495 if ( !dc ) return 0;
1497 physdev = GET_DC_PHYSDEV( dc, pGetBoundsRect );
1498 ret = physdev->funcs->pGetBoundsRect( physdev, &device_rect, DCB_RESET );
1499 if (!ret)
1501 release_dc_ptr( dc );
1502 return 0;
1504 if (dc->bounds_enabled && ret == DCB_SET) add_bounds_rect( &dc->bounds, &device_rect );
1506 if (rect)
1508 if (is_rect_empty( &dc->bounds ))
1510 rect->left = rect->top = rect->right = rect->bottom = 0;
1511 ret = DCB_RESET;
1513 else
1515 *rect = dc->bounds;
1516 rect->left = max( rect->left, 0 );
1517 rect->top = max( rect->top, 0 );
1518 rect->right = min( rect->right, dc->vis_rect.right - dc->vis_rect.left );
1519 rect->bottom = min( rect->bottom, dc->vis_rect.bottom - dc->vis_rect.top );
1520 ret = DCB_SET;
1522 dp_to_lp( dc, (POINT *)rect, 2 );
1524 else ret = 0;
1526 if (flags & DCB_RESET) reset_bounds( &dc->bounds );
1527 release_dc_ptr( dc );
1528 return ret;
1532 /***********************************************************************
1533 * SetBoundsRect (GDI32.@)
1535 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1537 PHYSDEV physdev;
1538 UINT ret;
1539 DC *dc;
1541 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1542 if (!(dc = get_dc_ptr( hdc ))) return 0;
1544 physdev = GET_DC_PHYSDEV( dc, pSetBoundsRect );
1545 ret = physdev->funcs->pSetBoundsRect( physdev, &dc->bounds, flags );
1546 if (!ret)
1548 release_dc_ptr( dc );
1549 return 0;
1552 ret = (dc->bounds_enabled ? DCB_ENABLE : DCB_DISABLE) |
1553 (is_rect_empty( &dc->bounds ) ? ret & DCB_SET : DCB_SET);
1555 if (flags & DCB_RESET) reset_bounds( &dc->bounds );
1557 if ((flags & DCB_ACCUMULATE) && rect)
1559 RECT rc = *rect;
1561 lp_to_dp( dc, (POINT *)&rc, 2 );
1562 add_bounds_rect( &dc->bounds, &rc );
1565 if (flags & DCB_ENABLE) dc->bounds_enabled = TRUE;
1566 if (flags & DCB_DISABLE) dc->bounds_enabled = FALSE;
1568 release_dc_ptr( dc );
1569 return ret;
1573 /***********************************************************************
1574 * GetRelAbs (GDI32.@)
1576 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1578 INT ret = 0;
1579 DC *dc = get_dc_ptr( hdc );
1580 if (dc)
1582 ret = dc->relAbsMode;
1583 release_dc_ptr( dc );
1585 return ret;
1591 /***********************************************************************
1592 * GetBkMode (GDI32.@)
1594 INT WINAPI GetBkMode( HDC hdc )
1596 INT ret = 0;
1597 DC * dc = get_dc_ptr( hdc );
1598 if (dc)
1600 ret = dc->backgroundMode;
1601 release_dc_ptr( dc );
1603 return ret;
1607 /***********************************************************************
1608 * SetBkMode (GDI32.@)
1610 INT WINAPI SetBkMode( HDC hdc, INT mode )
1612 INT ret = 0;
1613 DC *dc;
1615 if ((mode <= 0) || (mode > BKMODE_LAST))
1617 SetLastError(ERROR_INVALID_PARAMETER);
1618 return 0;
1620 if ((dc = get_dc_ptr( hdc )))
1622 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetBkMode );
1623 mode = physdev->funcs->pSetBkMode( physdev, mode );
1624 if (mode)
1626 ret = dc->backgroundMode;
1627 dc->backgroundMode = mode;
1629 release_dc_ptr( dc );
1631 return ret;
1635 /***********************************************************************
1636 * GetROP2 (GDI32.@)
1638 INT WINAPI GetROP2( HDC hdc )
1640 INT ret = 0;
1641 DC * dc = get_dc_ptr( hdc );
1642 if (dc)
1644 ret = dc->ROPmode;
1645 release_dc_ptr( dc );
1647 return ret;
1651 /***********************************************************************
1652 * SetROP2 (GDI32.@)
1654 INT WINAPI SetROP2( HDC hdc, INT mode )
1656 INT ret = 0;
1657 DC *dc;
1659 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1661 SetLastError(ERROR_INVALID_PARAMETER);
1662 return 0;
1664 if ((dc = get_dc_ptr( hdc )))
1666 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetROP2 );
1667 mode = physdev->funcs->pSetROP2( physdev, mode );
1668 if (mode)
1670 ret = dc->ROPmode;
1671 dc->ROPmode = mode;
1673 release_dc_ptr( dc );
1675 return ret;
1679 /***********************************************************************
1680 * SetRelAbs (GDI32.@)
1682 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1684 INT ret = 0;
1685 DC *dc;
1687 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1689 SetLastError(ERROR_INVALID_PARAMETER);
1690 return 0;
1692 if ((dc = get_dc_ptr( hdc )))
1694 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetRelAbs );
1695 mode = physdev->funcs->pSetRelAbs( physdev, mode );
1696 if (mode)
1698 ret = dc->relAbsMode;
1699 dc->relAbsMode = mode;
1701 release_dc_ptr( dc );
1703 return ret;
1707 /***********************************************************************
1708 * GetPolyFillMode (GDI32.@)
1710 INT WINAPI GetPolyFillMode( HDC hdc )
1712 INT ret = 0;
1713 DC * dc = get_dc_ptr( hdc );
1714 if (dc)
1716 ret = dc->polyFillMode;
1717 release_dc_ptr( dc );
1719 return ret;
1723 /***********************************************************************
1724 * SetPolyFillMode (GDI32.@)
1726 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1728 INT ret = 0;
1729 DC *dc;
1731 if ((mode <= 0) || (mode > POLYFILL_LAST))
1733 SetLastError(ERROR_INVALID_PARAMETER);
1734 return 0;
1736 if ((dc = get_dc_ptr( hdc )))
1738 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPolyFillMode );
1739 mode = physdev->funcs->pSetPolyFillMode( physdev, mode );
1740 if (mode)
1742 ret = dc->polyFillMode;
1743 dc->polyFillMode = mode;
1745 release_dc_ptr( dc );
1747 return ret;
1751 /***********************************************************************
1752 * GetStretchBltMode (GDI32.@)
1754 INT WINAPI GetStretchBltMode( HDC hdc )
1756 INT ret = 0;
1757 DC * dc = get_dc_ptr( hdc );
1758 if (dc)
1760 ret = dc->stretchBltMode;
1761 release_dc_ptr( dc );
1763 return ret;
1767 /***********************************************************************
1768 * SetStretchBltMode (GDI32.@)
1770 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1772 INT ret = 0;
1773 DC *dc;
1775 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1777 SetLastError(ERROR_INVALID_PARAMETER);
1778 return 0;
1780 if ((dc = get_dc_ptr( hdc )))
1782 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetStretchBltMode );
1783 mode = physdev->funcs->pSetStretchBltMode( physdev, mode );
1784 if (mode)
1786 ret = dc->stretchBltMode;
1787 dc->stretchBltMode = mode;
1789 release_dc_ptr( dc );
1791 return ret;
1795 /***********************************************************************
1796 * GetMapMode (GDI32.@)
1798 INT WINAPI GetMapMode( HDC hdc )
1800 INT ret = 0;
1801 DC * dc = get_dc_ptr( hdc );
1802 if (dc)
1804 ret = dc->MapMode;
1805 release_dc_ptr( dc );
1807 return ret;
1811 /***********************************************************************
1812 * GetBrushOrgEx (GDI32.@)
1814 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1816 DC * dc = get_dc_ptr( hdc );
1817 if (!dc) return FALSE;
1818 *pt = dc->brush_org;
1819 release_dc_ptr( dc );
1820 return TRUE;
1824 /***********************************************************************
1825 * GetCurrentPositionEx (GDI32.@)
1827 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1829 DC * dc = get_dc_ptr( hdc );
1830 if (!dc) return FALSE;
1831 *pt = dc->cur_pos;
1832 release_dc_ptr( dc );
1833 return TRUE;
1837 /***********************************************************************
1838 * GetViewportExtEx (GDI32.@)
1840 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1842 DC * dc = get_dc_ptr( hdc );
1843 if (!dc) return FALSE;
1844 *size = dc->vport_ext;
1845 release_dc_ptr( dc );
1846 return TRUE;
1850 /***********************************************************************
1851 * GetViewportOrgEx (GDI32.@)
1853 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1855 DC * dc = get_dc_ptr( hdc );
1856 if (!dc) return FALSE;
1857 *pt = dc->vport_org;
1858 release_dc_ptr( dc );
1859 return TRUE;
1863 /***********************************************************************
1864 * GetWindowExtEx (GDI32.@)
1866 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1868 DC * dc = get_dc_ptr( hdc );
1869 if (!dc) return FALSE;
1870 *size = dc->wnd_ext;
1871 release_dc_ptr( dc );
1872 return TRUE;
1876 /***********************************************************************
1877 * GetWindowOrgEx (GDI32.@)
1879 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1881 DC * dc = get_dc_ptr( hdc );
1882 if (!dc) return FALSE;
1883 *pt = dc->wnd_org;
1884 release_dc_ptr( dc );
1885 return TRUE;
1889 /***********************************************************************
1890 * GetLayout (GDI32.@)
1892 * Gets left->right or right->left text layout flags of a dc.
1895 DWORD WINAPI GetLayout(HDC hdc)
1897 DWORD layout = GDI_ERROR;
1899 DC * dc = get_dc_ptr( hdc );
1900 if (dc)
1902 layout = dc->layout;
1903 release_dc_ptr( dc );
1906 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1908 return layout;
1911 /***********************************************************************
1912 * SetLayout (GDI32.@)
1914 * Sets left->right or right->left text layout flags of a dc.
1917 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1919 DWORD oldlayout = GDI_ERROR;
1921 DC * dc = get_dc_ptr( hdc );
1922 if (dc)
1924 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetLayout );
1925 layout = physdev->funcs->pSetLayout( physdev, layout );
1926 if (layout != GDI_ERROR)
1928 oldlayout = dc->layout;
1929 dc->layout = layout;
1930 if (layout != oldlayout)
1932 if (layout & LAYOUT_RTL) dc->MapMode = MM_ANISOTROPIC;
1933 DC_UpdateXforms( dc );
1936 release_dc_ptr( dc );
1939 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
1941 return oldlayout;
1944 /***********************************************************************
1945 * GetDCBrushColor (GDI32.@)
1947 COLORREF WINAPI GetDCBrushColor(HDC hdc)
1949 DC *dc;
1950 COLORREF dcBrushColor = CLR_INVALID;
1952 TRACE("hdc(%p)\n", hdc);
1954 dc = get_dc_ptr( hdc );
1955 if (dc)
1957 dcBrushColor = dc->dcBrushColor;
1958 release_dc_ptr( dc );
1961 return dcBrushColor;
1964 /***********************************************************************
1965 * SetDCBrushColor (GDI32.@)
1967 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
1969 DC *dc;
1970 COLORREF oldClr = CLR_INVALID;
1972 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
1974 dc = get_dc_ptr( hdc );
1975 if (dc)
1977 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDCBrushColor );
1978 crColor = physdev->funcs->pSetDCBrushColor( physdev, crColor );
1979 if (crColor != CLR_INVALID)
1981 oldClr = dc->dcBrushColor;
1982 dc->dcBrushColor = crColor;
1984 release_dc_ptr( dc );
1987 return oldClr;
1990 /***********************************************************************
1991 * GetDCPenColor (GDI32.@)
1993 COLORREF WINAPI GetDCPenColor(HDC hdc)
1995 DC *dc;
1996 COLORREF dcPenColor = CLR_INVALID;
1998 TRACE("hdc(%p)\n", hdc);
2000 dc = get_dc_ptr( hdc );
2001 if (dc)
2003 dcPenColor = dc->dcPenColor;
2004 release_dc_ptr( dc );
2007 return dcPenColor;
2010 /***********************************************************************
2011 * SetDCPenColor (GDI32.@)
2013 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
2015 DC *dc;
2016 COLORREF oldClr = CLR_INVALID;
2018 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2020 dc = get_dc_ptr( hdc );
2021 if (dc)
2023 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDCPenColor );
2024 crColor = physdev->funcs->pSetDCPenColor( physdev, crColor );
2025 if (crColor != CLR_INVALID)
2027 oldClr = dc->dcPenColor;
2028 dc->dcPenColor = crColor;
2030 release_dc_ptr( dc );
2033 return oldClr;
2036 /***********************************************************************
2037 * CancelDC (GDI32.@)
2039 BOOL WINAPI CancelDC(HDC hdc)
2041 FIXME("stub\n");
2042 return TRUE;
2045 /*******************************************************************
2046 * GetMiterLimit [GDI32.@]
2050 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2052 BOOL bRet = FALSE;
2053 DC *dc;
2055 TRACE("(%p,%p)\n", hdc, peLimit);
2057 dc = get_dc_ptr( hdc );
2058 if (dc)
2060 if (peLimit)
2061 *peLimit = dc->miterLimit;
2063 release_dc_ptr( dc );
2064 bRet = TRUE;
2066 return bRet;
2069 /*******************************************************************
2070 * SetMiterLimit [GDI32.@]
2074 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2076 BOOL bRet = FALSE;
2077 DC *dc;
2079 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2081 dc = get_dc_ptr( hdc );
2082 if (dc)
2084 if (peOldLimit)
2085 *peOldLimit = dc->miterLimit;
2086 dc->miterLimit = eNewLimit;
2087 release_dc_ptr( dc );
2088 bRet = TRUE;
2090 return bRet;
2093 /*******************************************************************
2094 * GdiIsMetaPrintDC [GDI32.@]
2096 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2098 FIXME("%p\n", hdc);
2099 return FALSE;
2102 /*******************************************************************
2103 * GdiIsMetaFileDC [GDI32.@]
2105 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2107 TRACE("%p\n", hdc);
2109 switch( GetObjectType( hdc ) )
2111 case OBJ_METADC:
2112 case OBJ_ENHMETADC:
2113 return TRUE;
2115 return FALSE;
2118 /*******************************************************************
2119 * GdiIsPlayMetafileDC [GDI32.@]
2121 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2123 FIXME("%p\n", hdc);
2124 return FALSE;