twain_32/tests: Compile with -D__WINESRC__.
[wine/multimedia.git] / dlls / gdi32 / dc.c
blob231f1976f0dcbc8092e5ecdb31bd676d51462bb3
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->module = gdi32_module;
135 dc->thread = GetCurrentThreadId();
136 dc->refcount = 1;
137 dc->hPen = GDI_inc_ref_count( GetStockObject( BLACK_PEN ));
138 dc->hBrush = GDI_inc_ref_count( GetStockObject( WHITE_BRUSH ));
139 dc->hFont = GDI_inc_ref_count( GetStockObject( SYSTEM_FONT ));
140 dc->hPalette = GetStockObject( DEFAULT_PALETTE );
142 set_initial_dc_state( dc );
144 if (!(dc->hSelf = alloc_gdi_handle( dc, magic, &dc_funcs )))
146 HeapFree( GetProcessHeap(), 0, dc );
147 return NULL;
149 dc->nulldrv.hdc = dc->hSelf;
151 if (font_driver && !font_driver->pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL ))
153 free_dc_ptr( dc );
154 return NULL;
156 return dc;
161 /***********************************************************************
162 * free_dc_state
164 static void free_dc_state( DC *dc )
166 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
167 if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
168 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
169 if (dc->region) DeleteObject( dc->region );
170 if (dc->path) free_gdi_path( dc->path );
171 HeapFree( GetProcessHeap(), 0, dc );
175 /***********************************************************************
176 * free_dc_ptr
178 void free_dc_ptr( DC *dc )
180 assert( dc->refcount == 1 );
182 while (dc->physDev != &dc->nulldrv)
184 PHYSDEV physdev = dc->physDev;
185 dc->physDev = physdev->next;
186 physdev->funcs->pDeleteDC( physdev );
188 GDI_dec_ref_count( dc->hPen );
189 GDI_dec_ref_count( dc->hBrush );
190 GDI_dec_ref_count( dc->hFont );
191 if (dc->hBitmap) GDI_dec_ref_count( dc->hBitmap );
192 free_gdi_handle( dc->hSelf );
193 free_dc_state( dc );
197 /***********************************************************************
198 * get_dc_ptr
200 * Retrieve a DC pointer but release the GDI lock.
202 DC *get_dc_ptr( HDC hdc )
204 DC *dc = get_dc_obj( hdc );
205 if (!dc) return NULL;
207 if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
209 dc->thread = GetCurrentThreadId();
211 else if (dc->thread != GetCurrentThreadId())
213 WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
214 GDI_ReleaseObj( hdc );
215 return NULL;
217 else InterlockedIncrement( &dc->refcount );
219 GDI_ReleaseObj( hdc );
220 return dc;
224 /***********************************************************************
225 * release_dc_ptr
227 void release_dc_ptr( DC *dc )
229 LONG ref;
231 dc->thread = 0;
232 ref = InterlockedDecrement( &dc->refcount );
233 assert( ref >= 0 );
234 if (ref) dc->thread = GetCurrentThreadId(); /* we still own it */
238 /***********************************************************************
239 * update_dc
241 * Make sure the DC vis region is up to date.
242 * This function may call up to USER so the GDI lock should _not_
243 * be held when calling it.
245 void update_dc( DC *dc )
247 if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookProc)
248 dc->hookProc( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
252 /***********************************************************************
253 * DC_DeleteObject
255 static BOOL DC_DeleteObject( HGDIOBJ handle )
257 return DeleteDC( handle );
261 /***********************************************************************
262 * DC_InitDC
264 * Setup device-specific DC values for a newly created DC.
266 void DC_InitDC( DC* dc )
268 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRealizeDefaultPalette );
269 physdev->funcs->pRealizeDefaultPalette( physdev );
270 SetTextColor( dc->hSelf, dc->textColor );
271 SetBkColor( dc->hSelf, dc->backgroundColor );
272 SelectObject( dc->hSelf, dc->hPen );
273 SelectObject( dc->hSelf, dc->hBrush );
274 SelectObject( dc->hSelf, dc->hFont );
275 update_dc_clipping( dc );
276 SetVirtualResolution( dc->hSelf, 0, 0, 0, 0 );
277 physdev = GET_DC_PHYSDEV( dc, pSetBoundsRect );
278 physdev->funcs->pSetBoundsRect( physdev, &dc->bounds, dc->bounds_enabled ? DCB_ENABLE : DCB_DISABLE );
282 /***********************************************************************
283 * DC_InvertXform
285 * Computes the inverse of the transformation xformSrc and stores it to
286 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
287 * is singular.
289 static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
291 double determinant;
293 determinant = xformSrc->eM11*xformSrc->eM22 -
294 xformSrc->eM12*xformSrc->eM21;
295 if (determinant > -1e-12 && determinant < 1e-12)
296 return FALSE;
298 xformDest->eM11 = xformSrc->eM22 / determinant;
299 xformDest->eM12 = -xformSrc->eM12 / determinant;
300 xformDest->eM21 = -xformSrc->eM21 / determinant;
301 xformDest->eM22 = xformSrc->eM11 / determinant;
302 xformDest->eDx = -xformSrc->eDx * xformDest->eM11 -
303 xformSrc->eDy * xformDest->eM21;
304 xformDest->eDy = -xformSrc->eDx * xformDest->eM12 -
305 xformSrc->eDy * xformDest->eM22;
307 return TRUE;
310 /* Construct a transformation to do the window-to-viewport conversion */
311 static void construct_window_to_viewport(DC *dc, XFORM *xform)
313 double scaleX, scaleY;
314 scaleX = (double)dc->vportExtX / (double)dc->wndExtX;
315 scaleY = (double)dc->vportExtY / (double)dc->wndExtY;
317 if (dc->layout & LAYOUT_RTL) scaleX = -scaleX;
318 xform->eM11 = scaleX;
319 xform->eM12 = 0.0;
320 xform->eM21 = 0.0;
321 xform->eM22 = scaleY;
322 xform->eDx = (double)dc->vportOrgX - scaleX * (double)dc->wndOrgX;
323 xform->eDy = (double)dc->vportOrgY - scaleY * (double)dc->wndOrgY;
324 if (dc->layout & LAYOUT_RTL) xform->eDx = dc->vis_rect.right - dc->vis_rect.left - 1 - xform->eDx;
327 /***********************************************************************
328 * DC_UpdateXforms
330 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
331 * fields of the specified DC by creating a transformation that
332 * represents the current mapping mode and combining it with the DC's
333 * world transform. This function should be called whenever the
334 * parameters associated with the mapping mode (window and viewport
335 * extents and origins) or the world transform change.
337 void DC_UpdateXforms( DC *dc )
339 XFORM xformWnd2Vport, oldworld2vport;
341 construct_window_to_viewport(dc, &xformWnd2Vport);
343 oldworld2vport = dc->xformWorld2Vport;
344 /* Combine with the world transformation */
345 CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
346 &xformWnd2Vport );
348 /* Create inverse of world-to-viewport transformation */
349 dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
350 &dc->xformVport2World );
352 /* Reselect the font and pen back into the dc so that the size
353 gets updated. */
354 if (memcmp(&oldworld2vport, &dc->xformWorld2Vport, sizeof(oldworld2vport)) &&
355 !GdiIsMetaFileDC(dc->hSelf))
357 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
358 SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_PEN));
363 /***********************************************************************
364 * nulldrv_SaveDC
366 INT nulldrv_SaveDC( PHYSDEV dev )
368 DC *newdc, *dc = get_nulldrv_dc( dev );
370 if (!(newdc = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*newdc )))) return 0;
371 newdc->layout = dc->layout;
372 newdc->hPen = dc->hPen;
373 newdc->hBrush = dc->hBrush;
374 newdc->hFont = dc->hFont;
375 newdc->hBitmap = dc->hBitmap;
376 newdc->hPalette = dc->hPalette;
377 newdc->ROPmode = dc->ROPmode;
378 newdc->polyFillMode = dc->polyFillMode;
379 newdc->stretchBltMode = dc->stretchBltMode;
380 newdc->relAbsMode = dc->relAbsMode;
381 newdc->backgroundMode = dc->backgroundMode;
382 newdc->backgroundColor = dc->backgroundColor;
383 newdc->textColor = dc->textColor;
384 newdc->dcBrushColor = dc->dcBrushColor;
385 newdc->dcPenColor = dc->dcPenColor;
386 newdc->brushOrgX = dc->brushOrgX;
387 newdc->brushOrgY = dc->brushOrgY;
388 newdc->mapperFlags = dc->mapperFlags;
389 newdc->textAlign = dc->textAlign;
390 newdc->charExtra = dc->charExtra;
391 newdc->breakExtra = dc->breakExtra;
392 newdc->breakRem = dc->breakRem;
393 newdc->MapMode = dc->MapMode;
394 newdc->GraphicsMode = dc->GraphicsMode;
395 newdc->CursPosX = dc->CursPosX;
396 newdc->CursPosY = dc->CursPosY;
397 newdc->ArcDirection = dc->ArcDirection;
398 newdc->xformWorld2Wnd = dc->xformWorld2Wnd;
399 newdc->xformWorld2Vport = dc->xformWorld2Vport;
400 newdc->xformVport2World = dc->xformVport2World;
401 newdc->vport2WorldValid = dc->vport2WorldValid;
402 newdc->wndOrgX = dc->wndOrgX;
403 newdc->wndOrgY = dc->wndOrgY;
404 newdc->wndExtX = dc->wndExtX;
405 newdc->wndExtY = dc->wndExtY;
406 newdc->vportOrgX = dc->vportOrgX;
407 newdc->vportOrgY = dc->vportOrgY;
408 newdc->vportExtX = dc->vportExtX;
409 newdc->vportExtY = dc->vportExtY;
410 newdc->virtual_res = dc->virtual_res;
411 newdc->virtual_size = dc->virtual_size;
413 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
415 if (dc->hClipRgn)
417 newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
418 CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
420 if (dc->hMetaRgn)
422 newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
423 CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
426 if (!PATH_SavePath( newdc, dc ))
428 release_dc_ptr( dc );
429 free_dc_state( newdc );
430 return 0;
433 newdc->saved_dc = dc->saved_dc;
434 dc->saved_dc = newdc;
435 return ++dc->saveLevel;
439 /***********************************************************************
440 * nulldrv_RestoreDC
442 BOOL nulldrv_RestoreDC( PHYSDEV dev, INT level )
444 DC *dcs, *first_dcs, *dc = get_nulldrv_dc( dev );
445 INT save_level;
447 /* find the state level to restore */
449 if (abs(level) > dc->saveLevel || level == 0) return FALSE;
450 if (level < 0) level = dc->saveLevel + level + 1;
451 first_dcs = dc->saved_dc;
452 for (dcs = first_dcs, save_level = dc->saveLevel; save_level > level; save_level--)
453 dcs = dcs->saved_dc;
455 /* restore the state */
457 if (!PATH_RestorePath( dc, dcs )) return FALSE;
459 dc->layout = dcs->layout;
460 dc->ROPmode = dcs->ROPmode;
461 dc->polyFillMode = dcs->polyFillMode;
462 dc->stretchBltMode = dcs->stretchBltMode;
463 dc->relAbsMode = dcs->relAbsMode;
464 dc->backgroundMode = dcs->backgroundMode;
465 dc->backgroundColor = dcs->backgroundColor;
466 dc->textColor = dcs->textColor;
467 dc->dcBrushColor = dcs->dcBrushColor;
468 dc->dcPenColor = dcs->dcPenColor;
469 dc->brushOrgX = dcs->brushOrgX;
470 dc->brushOrgY = dcs->brushOrgY;
471 dc->mapperFlags = dcs->mapperFlags;
472 dc->textAlign = dcs->textAlign;
473 dc->charExtra = dcs->charExtra;
474 dc->breakExtra = dcs->breakExtra;
475 dc->breakRem = dcs->breakRem;
476 dc->MapMode = dcs->MapMode;
477 dc->GraphicsMode = dcs->GraphicsMode;
478 dc->CursPosX = dcs->CursPosX;
479 dc->CursPosY = dcs->CursPosY;
480 dc->ArcDirection = dcs->ArcDirection;
481 dc->xformWorld2Wnd = dcs->xformWorld2Wnd;
482 dc->xformWorld2Vport = dcs->xformWorld2Vport;
483 dc->xformVport2World = dcs->xformVport2World;
484 dc->vport2WorldValid = dcs->vport2WorldValid;
485 dc->wndOrgX = dcs->wndOrgX;
486 dc->wndOrgY = dcs->wndOrgY;
487 dc->wndExtX = dcs->wndExtX;
488 dc->wndExtY = dcs->wndExtY;
489 dc->vportOrgX = dcs->vportOrgX;
490 dc->vportOrgY = dcs->vportOrgY;
491 dc->vportExtX = dcs->vportExtX;
492 dc->vportExtY = dcs->vportExtY;
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 HDC hdc;
627 DC * dc;
628 const struct gdi_dc_funcs *funcs;
629 HMODULE module;
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 strcpyW(buf, driver);
644 if (!(funcs = DRIVER_load_driver( buf, &module )))
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->module = module;
653 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
655 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
656 debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
658 if (funcs->pCreateDC)
660 if (!funcs->pCreateDC( &dc->physDev, buf, device, output, initData ))
662 WARN("creation aborted by device\n" );
663 free_dc_ptr( dc );
664 return 0;
668 dc->vis_rect.left = 0;
669 dc->vis_rect.top = 0;
670 dc->vis_rect.right = GetDeviceCaps( hdc, DESKTOPHORZRES );
671 dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
673 DC_InitDC( dc );
674 release_dc_ptr( dc );
675 return hdc;
679 /***********************************************************************
680 * CreateDCA (GDI32.@)
682 HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
683 const DEVMODEA *initData )
685 UNICODE_STRING driverW, deviceW, outputW;
686 DEVMODEW *initDataW;
687 HDC ret;
689 if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
690 else driverW.Buffer = NULL;
692 if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
693 else deviceW.Buffer = NULL;
695 if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
696 else outputW.Buffer = NULL;
698 initDataW = NULL;
699 if (initData)
701 /* don't convert initData for DISPLAY driver, it's not used */
702 if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
703 initDataW = GdiConvertToDevmodeW(initData);
706 ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
708 RtlFreeUnicodeString(&driverW);
709 RtlFreeUnicodeString(&deviceW);
710 RtlFreeUnicodeString(&outputW);
711 HeapFree(GetProcessHeap(), 0, initDataW);
712 return ret;
716 /***********************************************************************
717 * CreateICA (GDI32.@)
719 HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
720 const DEVMODEA* initData )
722 /* Nothing special yet for ICs */
723 return CreateDCA( driver, device, output, initData );
727 /***********************************************************************
728 * CreateICW (GDI32.@)
730 HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
731 const DEVMODEW* initData )
733 /* Nothing special yet for ICs */
734 return CreateDCW( driver, device, output, initData );
738 /***********************************************************************
739 * CreateCompatibleDC (GDI32.@)
741 HDC WINAPI CreateCompatibleDC( HDC hdc )
743 DC *dc, *origDC;
744 HDC ret;
745 const struct gdi_dc_funcs *funcs = &null_driver;
746 PHYSDEV physDev = NULL;
748 GDI_CheckNotLock();
750 if (hdc)
752 if (!(origDC = get_dc_ptr( hdc ))) return 0;
753 physDev = GET_DC_PHYSDEV( origDC, pCreateCompatibleDC );
754 funcs = physDev->funcs;
755 release_dc_ptr( origDC );
758 if (!(dc = alloc_dc_ptr( OBJ_MEMDC ))) return 0;
760 TRACE("(%p): returning %p\n", hdc, dc->hSelf );
762 dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
763 dc->vis_rect.left = 0;
764 dc->vis_rect.top = 0;
765 dc->vis_rect.right = 1;
766 dc->vis_rect.bottom = 1;
767 dc->device_rect = dc->vis_rect;
769 ret = dc->hSelf;
771 if (!funcs->pCreateCompatibleDC( physDev, &dc->physDev ))
773 WARN("creation aborted by device\n");
774 free_dc_ptr( dc );
775 return 0;
778 if (!dib_driver.pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL ))
780 free_dc_ptr( dc );
781 return 0;
783 physDev = GET_DC_PHYSDEV( dc, pSelectBitmap );
784 physDev->funcs->pSelectBitmap( physDev, dc->hBitmap );
786 DC_InitDC( dc );
787 release_dc_ptr( dc );
788 return ret;
792 /***********************************************************************
793 * DeleteDC (GDI32.@)
795 BOOL WINAPI DeleteDC( HDC hdc )
797 DC * dc;
799 TRACE("%p\n", hdc );
801 GDI_CheckNotLock();
803 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
804 if (dc->refcount != 1)
806 FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
807 release_dc_ptr( dc );
808 return FALSE;
811 /* Call hook procedure to check whether is it OK to delete this DC */
812 if (dc->hookProc && !dc->hookProc( hdc, DCHC_DELETEDC, dc->dwHookData, 0 ))
814 release_dc_ptr( dc );
815 return TRUE;
817 reset_dc_state( hdc );
818 free_dc_ptr( dc );
819 return TRUE;
823 /***********************************************************************
824 * ResetDCW (GDI32.@)
826 HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
828 DC *dc;
829 HDC ret = 0;
831 if ((dc = get_dc_ptr( hdc )))
833 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pResetDC );
834 ret = physdev->funcs->pResetDC( physdev, devmode );
835 if (ret) /* reset the visible region */
837 dc->dirty = 0;
838 dc->vis_rect.left = 0;
839 dc->vis_rect.top = 0;
840 dc->vis_rect.right = GetDeviceCaps( hdc, DESKTOPHORZRES );
841 dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
842 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
843 dc->hVisRgn = 0;
844 update_dc_clipping( dc );
846 release_dc_ptr( dc );
848 return ret;
852 /***********************************************************************
853 * ResetDCA (GDI32.@)
855 HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
857 DEVMODEW *devmodeW;
858 HDC ret;
860 if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
861 else devmodeW = NULL;
863 ret = ResetDCW(hdc, devmodeW);
865 HeapFree(GetProcessHeap(), 0, devmodeW);
866 return ret;
870 /***********************************************************************
871 * GetDeviceCaps (GDI32.@)
873 INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
875 DC *dc;
876 INT ret = 0;
878 if ((dc = get_dc_ptr( hdc )))
880 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetDeviceCaps );
881 ret = physdev->funcs->pGetDeviceCaps( physdev, cap );
882 release_dc_ptr( dc );
884 return ret;
888 /***********************************************************************
889 * GetBkColor (GDI32.@)
891 COLORREF WINAPI GetBkColor( HDC hdc )
893 COLORREF ret = 0;
894 DC * dc = get_dc_ptr( hdc );
895 if (dc)
897 ret = dc->backgroundColor;
898 release_dc_ptr( dc );
900 return ret;
904 /***********************************************************************
905 * SetBkColor (GDI32.@)
907 COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
909 COLORREF ret = CLR_INVALID;
910 DC * dc = get_dc_ptr( hdc );
912 TRACE("hdc=%p color=0x%08x\n", hdc, color);
914 if (dc)
916 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetBkColor );
917 ret = dc->backgroundColor;
918 dc->backgroundColor = physdev->funcs->pSetBkColor( physdev, color );
919 release_dc_ptr( dc );
921 return ret;
925 /***********************************************************************
926 * GetTextColor (GDI32.@)
928 COLORREF WINAPI GetTextColor( HDC hdc )
930 COLORREF ret = 0;
931 DC * dc = get_dc_ptr( hdc );
932 if (dc)
934 ret = dc->textColor;
935 release_dc_ptr( dc );
937 return ret;
941 /***********************************************************************
942 * SetTextColor (GDI32.@)
944 COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
946 COLORREF ret = CLR_INVALID;
947 DC * dc = get_dc_ptr( hdc );
949 TRACE(" hdc=%p color=0x%08x\n", hdc, color);
951 if (dc)
953 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetTextColor );
954 ret = dc->textColor;
955 dc->textColor = physdev->funcs->pSetTextColor( physdev, color );
956 release_dc_ptr( dc );
958 return ret;
962 /***********************************************************************
963 * GetTextAlign (GDI32.@)
965 UINT WINAPI GetTextAlign( HDC hdc )
967 UINT ret = 0;
968 DC * dc = get_dc_ptr( hdc );
969 if (dc)
971 ret = dc->textAlign;
972 release_dc_ptr( dc );
974 return ret;
978 /***********************************************************************
979 * SetTextAlign (GDI32.@)
981 UINT WINAPI SetTextAlign( HDC hdc, UINT align )
983 UINT ret = GDI_ERROR;
984 DC *dc = get_dc_ptr( hdc );
986 TRACE("hdc=%p align=%d\n", hdc, align);
988 if (dc)
990 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetTextAlign );
991 align = physdev->funcs->pSetTextAlign( physdev, align );
992 if (align != GDI_ERROR)
994 ret = dc->textAlign;
995 dc->textAlign = align;
997 release_dc_ptr( dc );
999 return ret;
1002 /***********************************************************************
1003 * GetDCOrgEx (GDI32.@)
1005 BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
1007 DC * dc;
1009 if (!lpp) return FALSE;
1010 if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1011 lpp->x = dc->vis_rect.left;
1012 lpp->y = dc->vis_rect.top;
1013 release_dc_ptr( dc );
1014 return TRUE;
1018 /***********************************************************************
1019 * GetGraphicsMode (GDI32.@)
1021 INT WINAPI GetGraphicsMode( HDC hdc )
1023 INT ret = 0;
1024 DC * dc = get_dc_ptr( hdc );
1025 if (dc)
1027 ret = dc->GraphicsMode;
1028 release_dc_ptr( dc );
1030 return ret;
1034 /***********************************************************************
1035 * SetGraphicsMode (GDI32.@)
1037 INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
1039 INT ret = 0;
1040 DC *dc = get_dc_ptr( hdc );
1042 /* One would think that setting the graphics mode to GM_COMPATIBLE
1043 * would also reset the world transformation matrix to the unity
1044 * matrix. However, in Windows, this is not the case. This doesn't
1045 * make a lot of sense to me, but that's the way it is.
1047 if (!dc) return 0;
1048 if ((mode > 0) && (mode <= GM_LAST))
1050 ret = dc->GraphicsMode;
1051 dc->GraphicsMode = mode;
1053 release_dc_ptr( dc );
1054 /* font metrics depend on the graphics mode */
1055 if (ret) SelectObject(dc->hSelf, GetCurrentObject(dc->hSelf, OBJ_FONT));
1056 return ret;
1060 /***********************************************************************
1061 * GetArcDirection (GDI32.@)
1063 INT WINAPI GetArcDirection( HDC hdc )
1065 INT ret = 0;
1066 DC * dc = get_dc_ptr( hdc );
1067 if (dc)
1069 ret = dc->ArcDirection;
1070 release_dc_ptr( dc );
1072 return ret;
1076 /***********************************************************************
1077 * SetArcDirection (GDI32.@)
1079 INT WINAPI SetArcDirection( HDC hdc, INT dir )
1081 DC * dc;
1082 INT ret = 0;
1084 if (dir != AD_COUNTERCLOCKWISE && dir != AD_CLOCKWISE)
1086 SetLastError(ERROR_INVALID_PARAMETER);
1087 return 0;
1090 if ((dc = get_dc_ptr( hdc )))
1092 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetArcDirection );
1093 dir = physdev->funcs->pSetArcDirection( physdev, dir );
1094 if (dir)
1096 ret = dc->ArcDirection;
1097 dc->ArcDirection = dir;
1099 release_dc_ptr( dc );
1101 return ret;
1105 /***********************************************************************
1106 * GetWorldTransform (GDI32.@)
1108 BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
1110 DC * dc;
1111 if (!xform) return FALSE;
1112 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1113 *xform = dc->xformWorld2Wnd;
1114 release_dc_ptr( dc );
1115 return TRUE;
1119 /***********************************************************************
1120 * GetTransform (GDI32.@)
1122 * Undocumented
1124 * Returns one of the co-ordinate space transforms
1126 * PARAMS
1127 * hdc [I] Device context.
1128 * which [I] Which xform to return:
1129 * 0x203 World -> Page transform (that set by SetWorldTransform).
1130 * 0x304 Page -> Device transform (the mapping mode transform).
1131 * 0x204 World -> Device transform (the combination of the above two).
1132 * 0x402 Device -> World transform (the inversion of the above).
1133 * xform [O] The xform.
1136 BOOL WINAPI GetTransform( HDC hdc, DWORD which, XFORM *xform )
1138 BOOL ret = TRUE;
1139 DC *dc = get_dc_ptr( hdc );
1140 if (!dc) return FALSE;
1142 switch(which)
1144 case 0x203:
1145 *xform = dc->xformWorld2Wnd;
1146 break;
1148 case 0x304:
1149 construct_window_to_viewport(dc, xform);
1150 break;
1152 case 0x204:
1153 *xform = dc->xformWorld2Vport;
1154 break;
1156 case 0x402:
1157 *xform = dc->xformVport2World;
1158 break;
1160 default:
1161 FIXME("Unknown code %x\n", which);
1162 ret = FALSE;
1165 release_dc_ptr( dc );
1166 return ret;
1170 /****************************************************************************
1171 * CombineTransform [GDI32.@]
1172 * Combines two transformation matrices.
1174 * PARAMS
1175 * xformResult [O] Stores the result of combining the two matrices
1176 * xform1 [I] Specifies the first matrix to apply
1177 * xform2 [I] Specifies the second matrix to apply
1179 * REMARKS
1180 * The same matrix can be passed in for more than one of the parameters.
1182 * RETURNS
1183 * Success: TRUE.
1184 * Failure: FALSE. Use GetLastError() to determine the cause.
1186 BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
1187 const XFORM *xform2 )
1189 XFORM xformTemp;
1191 /* Check for illegal parameters */
1192 if (!xformResult || !xform1 || !xform2)
1193 return FALSE;
1195 /* Create the result in a temporary XFORM, since xformResult may be
1196 * equal to xform1 or xform2 */
1197 xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
1198 xform1->eM12 * xform2->eM21;
1199 xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
1200 xform1->eM12 * xform2->eM22;
1201 xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
1202 xform1->eM22 * xform2->eM21;
1203 xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
1204 xform1->eM22 * xform2->eM22;
1205 xformTemp.eDx = xform1->eDx * xform2->eM11 +
1206 xform1->eDy * xform2->eM21 +
1207 xform2->eDx;
1208 xformTemp.eDy = xform1->eDx * xform2->eM12 +
1209 xform1->eDy * xform2->eM22 +
1210 xform2->eDy;
1212 /* Copy the result to xformResult */
1213 *xformResult = xformTemp;
1215 return TRUE;
1219 /***********************************************************************
1220 * SetDCHook (GDI32.@)
1222 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1224 BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1226 DC *dc = get_dc_ptr( hdc );
1228 if (!dc) return FALSE;
1230 dc->dwHookData = dwHookData;
1231 dc->hookProc = hookProc;
1232 release_dc_ptr( dc );
1233 return TRUE;
1237 /***********************************************************************
1238 * GetDCHook (GDI32.@)
1240 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1242 DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
1244 DC *dc = get_dc_ptr( hdc );
1245 DWORD_PTR ret;
1247 if (!dc) return 0;
1248 if (proc) *proc = dc->hookProc;
1249 ret = dc->dwHookData;
1250 release_dc_ptr( dc );
1251 return ret;
1255 /***********************************************************************
1256 * SetHookFlags (GDI32.@)
1258 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1260 WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
1262 DC *dc = get_dc_obj( hdc ); /* not get_dc_ptr, this needs to work from any thread */
1263 LONG ret = 0;
1265 if (!dc) return 0;
1267 TRACE("hDC %p, flags %04x\n",hdc,flags);
1269 if (flags & DCHF_INVALIDATEVISRGN)
1270 ret = InterlockedExchange( &dc->dirty, 1 );
1271 else if (flags & DCHF_VALIDATEVISRGN || !flags)
1272 ret = InterlockedExchange( &dc->dirty, 0 );
1274 GDI_ReleaseObj( hdc );
1276 if (flags & DCHF_RESETDC) ret = reset_dc_state( hdc );
1277 return ret;
1280 /***********************************************************************
1281 * SetICMMode (GDI32.@)
1283 INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
1285 /*FIXME: Assume that ICM is always off, and cannot be turned on */
1286 if (iEnableICM == ICM_OFF) return ICM_OFF;
1287 if (iEnableICM == ICM_ON) return 0;
1288 if (iEnableICM == ICM_QUERY) return ICM_OFF;
1289 return 0;
1292 /***********************************************************************
1293 * GetDeviceGammaRamp (GDI32.@)
1295 BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1297 BOOL ret = FALSE;
1298 DC *dc = get_dc_ptr( hDC );
1300 TRACE("%p, %p\n", hDC, ptr);
1301 if( dc )
1303 if (GetObjectType( hDC ) != OBJ_MEMDC)
1305 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetDeviceGammaRamp );
1306 ret = physdev->funcs->pGetDeviceGammaRamp( physdev, ptr );
1308 else SetLastError( ERROR_INVALID_PARAMETER );
1309 release_dc_ptr( dc );
1311 return ret;
1314 static BOOL check_gamma_ramps(void *ptr)
1316 WORD *ramp = ptr;
1318 while (ramp < (WORD*)ptr + 3 * 256)
1320 float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
1321 unsigned i, f, l, g_n, c;
1323 f = ramp[0];
1324 l = ramp[255];
1325 if (f >= l)
1327 TRACE("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
1328 return FALSE;
1330 r_d = l - f;
1331 g_min = g_max = g_avg = 0.0;
1333 /* check gamma ramp entries to estimate the gamma */
1334 TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
1335 for (i=1, g_n=0; i<255; i++)
1337 if (ramp[i] < f || ramp[i] > l)
1339 TRACE("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
1340 return FALSE;
1342 c = ramp[i] - f;
1343 if (!c) continue; /* avoid log(0) */
1345 /* normalize entry values into 0..1 range */
1346 r_x = i/255.0; r_y = c / r_d;
1347 /* compute logarithms of values */
1348 r_lx = log(r_x); r_ly = log(r_y);
1349 /* compute gamma for this entry */
1350 r_v = r_ly / r_lx;
1351 /* compute differential (error estimate) for this entry */
1352 /* some games use table-based logarithms that magnifies the error by 128 */
1353 r_e = -r_lx * 128 / (c * r_lx * r_lx);
1355 /* compute min & max while compensating for estimated error */
1356 if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
1357 if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
1359 /* add to average */
1360 g_avg += r_v;
1361 g_n++;
1364 if (!g_n)
1366 TRACE("no gamma data, shouldn't happen\n");
1367 return FALSE;
1369 g_avg /= g_n;
1370 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
1372 /* check that the gamma is reasonably uniform across the ramp */
1373 if (g_max - g_min > 12.8)
1375 TRACE("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
1376 return FALSE;
1379 /* check that the gamma is not too bright */
1380 if (g_avg < 0.2)
1382 TRACE("too bright gamma ( %5.3f), rejected\n", g_avg);
1383 return FALSE;
1386 ramp += 256;
1389 return TRUE;
1392 /***********************************************************************
1393 * SetDeviceGammaRamp (GDI32.@)
1395 BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
1397 BOOL ret = FALSE;
1398 DC *dc = get_dc_ptr( hDC );
1400 TRACE("%p, %p\n", hDC, ptr);
1401 if( dc )
1403 if (GetObjectType( hDC ) != OBJ_MEMDC)
1405 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDeviceGammaRamp );
1407 if (check_gamma_ramps(ptr))
1408 ret = physdev->funcs->pSetDeviceGammaRamp( physdev, ptr );
1410 else SetLastError( ERROR_INVALID_PARAMETER );
1411 release_dc_ptr( dc );
1413 return ret;
1416 /***********************************************************************
1417 * GetColorSpace (GDI32.@)
1419 HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
1421 /*FIXME Need to do whatever GetColorSpace actually does */
1422 return 0;
1425 /***********************************************************************
1426 * CreateColorSpaceA (GDI32.@)
1428 HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
1430 FIXME( "stub\n" );
1431 return 0;
1434 /***********************************************************************
1435 * CreateColorSpaceW (GDI32.@)
1437 HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
1439 FIXME( "stub\n" );
1440 return 0;
1443 /***********************************************************************
1444 * DeleteColorSpace (GDI32.@)
1446 BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
1448 FIXME( "stub\n" );
1450 return TRUE;
1453 /***********************************************************************
1454 * SetColorSpace (GDI32.@)
1456 HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
1458 FIXME( "stub\n" );
1460 return hColorSpace;
1463 /***********************************************************************
1464 * GetBoundsRect (GDI32.@)
1466 UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1468 PHYSDEV physdev;
1469 RECT device_rect;
1470 UINT ret;
1471 DC *dc = get_dc_ptr( hdc );
1473 if ( !dc ) return 0;
1475 physdev = GET_DC_PHYSDEV( dc, pGetBoundsRect );
1476 ret = physdev->funcs->pGetBoundsRect( physdev, &device_rect, DCB_RESET );
1477 if (!ret)
1479 release_dc_ptr( dc );
1480 return 0;
1482 if (dc->bounds_enabled && ret == DCB_SET) add_bounds_rect( &dc->bounds, &device_rect );
1484 if (rect)
1486 if (is_rect_empty( &dc->bounds ))
1488 rect->left = rect->top = rect->right = rect->bottom = 0;
1489 ret = DCB_RESET;
1491 else
1493 *rect = dc->bounds;
1494 rect->left = max( rect->left, 0 );
1495 rect->top = max( rect->top, 0 );
1496 rect->right = min( rect->right, dc->vis_rect.right - dc->vis_rect.left );
1497 rect->bottom = min( rect->bottom, dc->vis_rect.bottom - dc->vis_rect.top );
1498 ret = DCB_SET;
1500 DPtoLP( hdc, (POINT *)rect, 2 );
1502 else ret = 0;
1504 if (flags & DCB_RESET) reset_bounds( &dc->bounds );
1505 release_dc_ptr( dc );
1506 return ret;
1510 /***********************************************************************
1511 * SetBoundsRect (GDI32.@)
1513 UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1515 PHYSDEV physdev;
1516 UINT ret;
1517 DC *dc;
1519 if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1520 if (!(dc = get_dc_ptr( hdc ))) return 0;
1522 physdev = GET_DC_PHYSDEV( dc, pSetBoundsRect );
1523 ret = physdev->funcs->pSetBoundsRect( physdev, &dc->bounds, flags );
1524 if (!ret)
1526 release_dc_ptr( dc );
1527 return 0;
1530 ret = (dc->bounds_enabled ? DCB_ENABLE : DCB_DISABLE) |
1531 (is_rect_empty( &dc->bounds ) ? ret & DCB_SET : DCB_SET);
1533 if (flags & DCB_RESET) reset_bounds( &dc->bounds );
1535 if ((flags & DCB_ACCUMULATE) && rect)
1537 RECT rc = *rect;
1539 LPtoDP( hdc, (POINT *)&rc, 2 );
1540 add_bounds_rect( &dc->bounds, &rc );
1543 if (flags & DCB_ENABLE) dc->bounds_enabled = TRUE;
1544 if (flags & DCB_DISABLE) dc->bounds_enabled = FALSE;
1546 release_dc_ptr( dc );
1547 return ret;
1551 /***********************************************************************
1552 * GetRelAbs (GDI32.@)
1554 INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
1556 INT ret = 0;
1557 DC *dc = get_dc_ptr( hdc );
1558 if (dc)
1560 ret = dc->relAbsMode;
1561 release_dc_ptr( dc );
1563 return ret;
1569 /***********************************************************************
1570 * GetBkMode (GDI32.@)
1572 INT WINAPI GetBkMode( HDC hdc )
1574 INT ret = 0;
1575 DC * dc = get_dc_ptr( hdc );
1576 if (dc)
1578 ret = dc->backgroundMode;
1579 release_dc_ptr( dc );
1581 return ret;
1585 /***********************************************************************
1586 * SetBkMode (GDI32.@)
1588 INT WINAPI SetBkMode( HDC hdc, INT mode )
1590 INT ret = 0;
1591 DC *dc;
1593 if ((mode <= 0) || (mode > BKMODE_LAST))
1595 SetLastError(ERROR_INVALID_PARAMETER);
1596 return 0;
1598 if ((dc = get_dc_ptr( hdc )))
1600 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetBkMode );
1601 mode = physdev->funcs->pSetBkMode( physdev, mode );
1602 if (mode)
1604 ret = dc->backgroundMode;
1605 dc->backgroundMode = mode;
1607 release_dc_ptr( dc );
1609 return ret;
1613 /***********************************************************************
1614 * GetROP2 (GDI32.@)
1616 INT WINAPI GetROP2( HDC hdc )
1618 INT ret = 0;
1619 DC * dc = get_dc_ptr( hdc );
1620 if (dc)
1622 ret = dc->ROPmode;
1623 release_dc_ptr( dc );
1625 return ret;
1629 /***********************************************************************
1630 * SetROP2 (GDI32.@)
1632 INT WINAPI SetROP2( HDC hdc, INT mode )
1634 INT ret = 0;
1635 DC *dc;
1637 if ((mode < R2_BLACK) || (mode > R2_WHITE))
1639 SetLastError(ERROR_INVALID_PARAMETER);
1640 return 0;
1642 if ((dc = get_dc_ptr( hdc )))
1644 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetROP2 );
1645 mode = physdev->funcs->pSetROP2( physdev, mode );
1646 if (mode)
1648 ret = dc->ROPmode;
1649 dc->ROPmode = mode;
1651 release_dc_ptr( dc );
1653 return ret;
1657 /***********************************************************************
1658 * SetRelAbs (GDI32.@)
1660 INT WINAPI SetRelAbs( HDC hdc, INT mode )
1662 INT ret = 0;
1663 DC *dc;
1665 if ((mode != ABSOLUTE) && (mode != RELATIVE))
1667 SetLastError(ERROR_INVALID_PARAMETER);
1668 return 0;
1670 if ((dc = get_dc_ptr( hdc )))
1672 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetRelAbs );
1673 mode = physdev->funcs->pSetRelAbs( physdev, mode );
1674 if (mode)
1676 ret = dc->relAbsMode;
1677 dc->relAbsMode = mode;
1679 release_dc_ptr( dc );
1681 return ret;
1685 /***********************************************************************
1686 * GetPolyFillMode (GDI32.@)
1688 INT WINAPI GetPolyFillMode( HDC hdc )
1690 INT ret = 0;
1691 DC * dc = get_dc_ptr( hdc );
1692 if (dc)
1694 ret = dc->polyFillMode;
1695 release_dc_ptr( dc );
1697 return ret;
1701 /***********************************************************************
1702 * SetPolyFillMode (GDI32.@)
1704 INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
1706 INT ret = 0;
1707 DC *dc;
1709 if ((mode <= 0) || (mode > POLYFILL_LAST))
1711 SetLastError(ERROR_INVALID_PARAMETER);
1712 return 0;
1714 if ((dc = get_dc_ptr( hdc )))
1716 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPolyFillMode );
1717 mode = physdev->funcs->pSetPolyFillMode( physdev, mode );
1718 if (mode)
1720 ret = dc->polyFillMode;
1721 dc->polyFillMode = mode;
1723 release_dc_ptr( dc );
1725 return ret;
1729 /***********************************************************************
1730 * GetStretchBltMode (GDI32.@)
1732 INT WINAPI GetStretchBltMode( HDC hdc )
1734 INT ret = 0;
1735 DC * dc = get_dc_ptr( hdc );
1736 if (dc)
1738 ret = dc->stretchBltMode;
1739 release_dc_ptr( dc );
1741 return ret;
1745 /***********************************************************************
1746 * SetStretchBltMode (GDI32.@)
1748 INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
1750 INT ret = 0;
1751 DC *dc;
1753 if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
1755 SetLastError(ERROR_INVALID_PARAMETER);
1756 return 0;
1758 if ((dc = get_dc_ptr( hdc )))
1760 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetStretchBltMode );
1761 mode = physdev->funcs->pSetStretchBltMode( physdev, mode );
1762 if (mode)
1764 ret = dc->stretchBltMode;
1765 dc->stretchBltMode = mode;
1767 release_dc_ptr( dc );
1769 return ret;
1773 /***********************************************************************
1774 * GetMapMode (GDI32.@)
1776 INT WINAPI GetMapMode( HDC hdc )
1778 INT ret = 0;
1779 DC * dc = get_dc_ptr( hdc );
1780 if (dc)
1782 ret = dc->MapMode;
1783 release_dc_ptr( dc );
1785 return ret;
1789 /***********************************************************************
1790 * GetBrushOrgEx (GDI32.@)
1792 BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
1794 DC * dc = get_dc_ptr( hdc );
1795 if (!dc) return FALSE;
1796 pt->x = dc->brushOrgX;
1797 pt->y = dc->brushOrgY;
1798 release_dc_ptr( dc );
1799 return TRUE;
1803 /***********************************************************************
1804 * GetCurrentPositionEx (GDI32.@)
1806 BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
1808 DC * dc = get_dc_ptr( hdc );
1809 if (!dc) return FALSE;
1810 pt->x = dc->CursPosX;
1811 pt->y = dc->CursPosY;
1812 release_dc_ptr( dc );
1813 return TRUE;
1817 /***********************************************************************
1818 * GetViewportExtEx (GDI32.@)
1820 BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
1822 DC * dc = get_dc_ptr( hdc );
1823 if (!dc) return FALSE;
1824 size->cx = dc->vportExtX;
1825 size->cy = dc->vportExtY;
1826 release_dc_ptr( dc );
1827 return TRUE;
1831 /***********************************************************************
1832 * GetViewportOrgEx (GDI32.@)
1834 BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
1836 DC * dc = get_dc_ptr( hdc );
1837 if (!dc) return FALSE;
1838 pt->x = dc->vportOrgX;
1839 pt->y = dc->vportOrgY;
1840 release_dc_ptr( dc );
1841 return TRUE;
1845 /***********************************************************************
1846 * GetWindowExtEx (GDI32.@)
1848 BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
1850 DC * dc = get_dc_ptr( hdc );
1851 if (!dc) return FALSE;
1852 size->cx = dc->wndExtX;
1853 size->cy = dc->wndExtY;
1854 release_dc_ptr( dc );
1855 return TRUE;
1859 /***********************************************************************
1860 * GetWindowOrgEx (GDI32.@)
1862 BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
1864 DC * dc = get_dc_ptr( hdc );
1865 if (!dc) return FALSE;
1866 pt->x = dc->wndOrgX;
1867 pt->y = dc->wndOrgY;
1868 release_dc_ptr( dc );
1869 return TRUE;
1873 /***********************************************************************
1874 * GetLayout (GDI32.@)
1876 * Gets left->right or right->left text layout flags of a dc.
1879 DWORD WINAPI GetLayout(HDC hdc)
1881 DWORD layout = GDI_ERROR;
1883 DC * dc = get_dc_ptr( hdc );
1884 if (dc)
1886 layout = dc->layout;
1887 release_dc_ptr( dc );
1890 TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1892 return layout;
1895 /***********************************************************************
1896 * SetLayout (GDI32.@)
1898 * Sets left->right or right->left text layout flags of a dc.
1901 DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
1903 DWORD oldlayout = GDI_ERROR;
1905 DC * dc = get_dc_ptr( hdc );
1906 if (dc)
1908 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetLayout );
1909 layout = physdev->funcs->pSetLayout( physdev, layout );
1910 if (layout != GDI_ERROR)
1912 oldlayout = dc->layout;
1913 dc->layout = layout;
1914 if (layout != oldlayout)
1916 if (layout & LAYOUT_RTL) dc->MapMode = MM_ANISOTROPIC;
1917 DC_UpdateXforms( dc );
1920 release_dc_ptr( dc );
1923 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
1925 return oldlayout;
1928 /***********************************************************************
1929 * GetDCBrushColor (GDI32.@)
1931 COLORREF WINAPI GetDCBrushColor(HDC hdc)
1933 DC *dc;
1934 COLORREF dcBrushColor = CLR_INVALID;
1936 TRACE("hdc(%p)\n", hdc);
1938 dc = get_dc_ptr( hdc );
1939 if (dc)
1941 dcBrushColor = dc->dcBrushColor;
1942 release_dc_ptr( dc );
1945 return dcBrushColor;
1948 /***********************************************************************
1949 * SetDCBrushColor (GDI32.@)
1951 COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
1953 DC *dc;
1954 COLORREF oldClr = CLR_INVALID;
1956 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
1958 dc = get_dc_ptr( hdc );
1959 if (dc)
1961 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDCBrushColor );
1962 crColor = physdev->funcs->pSetDCBrushColor( physdev, crColor );
1963 if (crColor != CLR_INVALID)
1965 oldClr = dc->dcBrushColor;
1966 dc->dcBrushColor = crColor;
1968 release_dc_ptr( dc );
1971 return oldClr;
1974 /***********************************************************************
1975 * GetDCPenColor (GDI32.@)
1977 COLORREF WINAPI GetDCPenColor(HDC hdc)
1979 DC *dc;
1980 COLORREF dcPenColor = CLR_INVALID;
1982 TRACE("hdc(%p)\n", hdc);
1984 dc = get_dc_ptr( hdc );
1985 if (dc)
1987 dcPenColor = dc->dcPenColor;
1988 release_dc_ptr( dc );
1991 return dcPenColor;
1994 /***********************************************************************
1995 * SetDCPenColor (GDI32.@)
1997 COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
1999 DC *dc;
2000 COLORREF oldClr = CLR_INVALID;
2002 TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2004 dc = get_dc_ptr( hdc );
2005 if (dc)
2007 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDCPenColor );
2008 crColor = physdev->funcs->pSetDCPenColor( physdev, crColor );
2009 if (crColor != CLR_INVALID)
2011 oldClr = dc->dcPenColor;
2012 dc->dcPenColor = crColor;
2014 release_dc_ptr( dc );
2017 return oldClr;
2020 /***********************************************************************
2021 * CancelDC (GDI32.@)
2023 BOOL WINAPI CancelDC(HDC hdc)
2025 FIXME("stub\n");
2026 return TRUE;
2029 /*******************************************************************
2030 * GetMiterLimit [GDI32.@]
2034 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
2036 BOOL bRet = FALSE;
2037 DC *dc;
2039 TRACE("(%p,%p)\n", hdc, peLimit);
2041 dc = get_dc_ptr( hdc );
2042 if (dc)
2044 if (peLimit)
2045 *peLimit = dc->miterLimit;
2047 release_dc_ptr( dc );
2048 bRet = TRUE;
2050 return bRet;
2053 /*******************************************************************
2054 * SetMiterLimit [GDI32.@]
2058 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
2060 BOOL bRet = FALSE;
2061 DC *dc;
2063 TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);
2065 dc = get_dc_ptr( hdc );
2066 if (dc)
2068 if (peOldLimit)
2069 *peOldLimit = dc->miterLimit;
2070 dc->miterLimit = eNewLimit;
2071 release_dc_ptr( dc );
2072 bRet = TRUE;
2074 return bRet;
2077 /*******************************************************************
2078 * GdiIsMetaPrintDC [GDI32.@]
2080 BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
2082 FIXME("%p\n", hdc);
2083 return FALSE;
2086 /*******************************************************************
2087 * GdiIsMetaFileDC [GDI32.@]
2089 BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
2091 TRACE("%p\n", hdc);
2093 switch( GetObjectType( hdc ) )
2095 case OBJ_METADC:
2096 case OBJ_ENHMETADC:
2097 return TRUE;
2099 return FALSE;
2102 /*******************************************************************
2103 * GdiIsPlayMetafileDC [GDI32.@]
2105 BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
2107 FIXME("%p\n", hdc);
2108 return FALSE;