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
32 #include "ntgdi_private.h"
33 #include "gdi_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(dc
);
38 static BOOL
DC_DeleteObject( HGDIOBJ handle
);
40 static const struct gdi_obj_funcs dc_funcs
=
42 NULL
, /* pGetObjectW */
43 NULL
, /* pUnrealizeObject */
44 DC_DeleteObject
/* pDeleteObject */
48 static inline DC
*get_dc_obj( HDC hdc
)
51 DC
*dc
= get_any_obj_ptr( hdc
, &type
);
58 case NTGDI_OBJ_METADC
:
59 case NTGDI_OBJ_ENHMETADC
:
62 GDI_ReleaseObj( hdc
);
63 SetLastError( ERROR_INVALID_HANDLE
);
69 /***********************************************************************
70 * set_initial_dc_state
72 static void set_initial_dc_state( DC
*dc
)
74 dc
->attr
->wnd_org
.x
= 0;
75 dc
->attr
->wnd_org
.y
= 0;
76 dc
->attr
->wnd_ext
.cx
= 1;
77 dc
->attr
->wnd_ext
.cy
= 1;
78 dc
->attr
->vport_org
.x
= 0;
79 dc
->attr
->vport_org
.y
= 0;
80 dc
->attr
->vport_ext
.cx
= 1;
81 dc
->attr
->vport_ext
.cy
= 1;
82 dc
->attr
->miter_limit
= 10.0f
; /* 10.0 is the default, from MSDN */
84 dc
->font_code_page
= CP_ACP
;
85 dc
->attr
->rop_mode
= R2_COPYPEN
;
86 dc
->attr
->poly_fill_mode
= ALTERNATE
;
87 dc
->attr
->stretch_blt_mode
= BLACKONWHITE
;
88 dc
->attr
->rel_abs_mode
= ABSOLUTE
;
89 dc
->attr
->background_mode
= OPAQUE
;
90 dc
->attr
->background_color
= RGB( 255, 255, 255 );
91 dc
->attr
->brush_color
= RGB( 255, 255, 255 );
92 dc
->attr
->pen_color
= RGB( 0, 0, 0 );
93 dc
->attr
->text_color
= RGB( 0, 0, 0 );
94 dc
->attr
->brush_org
.x
= 0;
95 dc
->attr
->brush_org
.y
= 0;
97 dc
->attr
->text_align
= TA_LEFT
| TA_TOP
| TA_NOUPDATECP
;
98 dc
->attr
->char_extra
= 0;
101 dc
->attr
->map_mode
= MM_TEXT
;
102 dc
->attr
->graphics_mode
= GM_COMPATIBLE
;
103 dc
->attr
->cur_pos
.x
= 0;
104 dc
->attr
->cur_pos
.y
= 0;
105 dc
->attr
->arc_direction
= AD_COUNTERCLOCKWISE
;
106 dc
->xformWorld2Wnd
.eM11
= 1.0f
;
107 dc
->xformWorld2Wnd
.eM12
= 0.0f
;
108 dc
->xformWorld2Wnd
.eM21
= 0.0f
;
109 dc
->xformWorld2Wnd
.eM22
= 1.0f
;
110 dc
->xformWorld2Wnd
.eDx
= 0.0f
;
111 dc
->xformWorld2Wnd
.eDy
= 0.0f
;
112 dc
->xformWorld2Vport
= dc
->xformWorld2Wnd
;
113 dc
->xformVport2World
= dc
->xformWorld2Wnd
;
114 dc
->vport2WorldValid
= TRUE
;
116 reset_bounds( &dc
->bounds
);
119 /***********************************************************************
122 DC
*alloc_dc_ptr( WORD magic
)
126 if (!(dc
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*dc
) ))) return NULL
;
127 if (!(dc
->attr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*dc
->attr
))))
129 HeapFree( GetProcessHeap(), 0, dc
->attr
);
133 dc
->nulldrv
.funcs
= &null_driver
;
134 dc
->physDev
= &dc
->nulldrv
;
135 dc
->thread
= GetCurrentThreadId();
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
->obj
, magic
, &dc_funcs
)))
146 HeapFree( GetProcessHeap(), 0, dc
->attr
);
147 HeapFree( GetProcessHeap(), 0, dc
);
150 dc
->nulldrv
.hdc
= dc
->hSelf
;
151 set_gdi_client_ptr( dc
->hSelf
, dc
->attr
);
153 if (!font_driver
.pCreateDC( &dc
->physDev
, NULL
, NULL
, NULL
, NULL
))
163 /***********************************************************************
166 static void free_dc_state( DC
*dc
)
168 if (dc
->hClipRgn
) DeleteObject( dc
->hClipRgn
);
169 if (dc
->hMetaRgn
) DeleteObject( dc
->hMetaRgn
);
170 if (dc
->hVisRgn
) DeleteObject( dc
->hVisRgn
);
171 if (dc
->region
) DeleteObject( dc
->region
);
172 if (dc
->path
) free_gdi_path( dc
->path
);
173 HeapFree( GetProcessHeap(), 0, dc
->attr
);
174 HeapFree( GetProcessHeap(), 0, dc
);
178 /***********************************************************************
181 void free_dc_ptr( DC
*dc
)
183 assert( dc
->refcount
== 1 );
185 while (dc
->physDev
!= &dc
->nulldrv
)
187 PHYSDEV physdev
= dc
->physDev
;
188 dc
->physDev
= physdev
->next
;
189 physdev
->funcs
->pDeleteDC( physdev
);
191 GDI_dec_ref_count( dc
->hPen
);
192 GDI_dec_ref_count( dc
->hBrush
);
193 GDI_dec_ref_count( dc
->hFont
);
194 if (dc
->hBitmap
) GDI_dec_ref_count( dc
->hBitmap
);
195 free_gdi_handle( dc
->hSelf
);
200 /***********************************************************************
203 * Retrieve a DC pointer but release the GDI lock.
205 DC
*get_dc_ptr( HDC hdc
)
207 DC
*dc
= get_dc_obj( hdc
);
208 if (!dc
) return NULL
;
209 if (dc
->attr
->disabled
)
211 GDI_ReleaseObj( hdc
);
215 if (!InterlockedCompareExchange( &dc
->refcount
, 1, 0 ))
217 dc
->thread
= GetCurrentThreadId();
219 else if (dc
->thread
!= GetCurrentThreadId())
221 WARN( "dc %p belongs to thread %04x\n", hdc
, dc
->thread
);
222 GDI_ReleaseObj( hdc
);
225 else InterlockedIncrement( &dc
->refcount
);
227 GDI_ReleaseObj( hdc
);
232 /***********************************************************************
235 void release_dc_ptr( DC
*dc
)
240 ref
= InterlockedDecrement( &dc
->refcount
);
242 if (ref
) dc
->thread
= GetCurrentThreadId(); /* we still own it */
246 /***********************************************************************
249 * Make sure the DC vis region is up to date.
250 * This function may call up to USER so the GDI lock should _not_
251 * be held when calling it.
253 void update_dc( DC
*dc
)
255 if (InterlockedExchange( &dc
->dirty
, 0 ) && dc
->hookProc
)
256 dc
->hookProc( dc
->hSelf
, DCHC_INVALIDVISRGN
, dc
->dwHookData
, 0 );
260 /***********************************************************************
263 static BOOL
DC_DeleteObject( HGDIOBJ handle
)
265 return DeleteDC( handle
);
269 /***********************************************************************
272 * Setup device-specific DC values for a newly created DC.
274 void DC_InitDC( DC
* dc
)
276 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pRealizeDefaultPalette
);
277 physdev
->funcs
->pRealizeDefaultPalette( physdev
);
278 SetTextColor( dc
->hSelf
, dc
->attr
->text_color
);
279 SetBkColor( dc
->hSelf
, dc
->attr
->background_color
);
280 NtGdiSelectPen( dc
->hSelf
, dc
->hPen
);
281 NtGdiSelectBrush( dc
->hSelf
, dc
->hBrush
);
282 NtGdiSelectFont( dc
->hSelf
, dc
->hFont
);
283 update_dc_clipping( dc
);
284 NtGdiSetVirtualResolution( dc
->hSelf
, 0, 0, 0, 0 );
285 physdev
= GET_DC_PHYSDEV( dc
, pSetBoundsRect
);
286 physdev
->funcs
->pSetBoundsRect( physdev
, &dc
->bounds
, dc
->bounds_enabled
? DCB_ENABLE
: DCB_DISABLE
);
290 /***********************************************************************
293 * Computes the inverse of the transformation xformSrc and stores it to
294 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
297 static BOOL
DC_InvertXform( const XFORM
*xformSrc
, XFORM
*xformDest
)
301 determinant
= xformSrc
->eM11
*xformSrc
->eM22
-
302 xformSrc
->eM12
*xformSrc
->eM21
;
303 if (determinant
> -1e-12 && determinant
< 1e-12)
306 xformDest
->eM11
= xformSrc
->eM22
/ determinant
;
307 xformDest
->eM12
= -xformSrc
->eM12
/ determinant
;
308 xformDest
->eM21
= -xformSrc
->eM21
/ determinant
;
309 xformDest
->eM22
= xformSrc
->eM11
/ determinant
;
310 xformDest
->eDx
= -xformSrc
->eDx
* xformDest
->eM11
-
311 xformSrc
->eDy
* xformDest
->eM21
;
312 xformDest
->eDy
= -xformSrc
->eDx
* xformDest
->eM12
-
313 xformSrc
->eDy
* xformDest
->eM22
;
318 /* Construct a transformation to do the window-to-viewport conversion */
319 static void construct_window_to_viewport(DC
*dc
, XFORM
*xform
)
321 double scaleX
, scaleY
;
322 scaleX
= (double)dc
->attr
->vport_ext
.cx
/ (double)dc
->attr
->wnd_ext
.cx
;
323 scaleY
= (double)dc
->attr
->vport_ext
.cy
/ (double)dc
->attr
->wnd_ext
.cy
;
325 if (dc
->attr
->layout
& LAYOUT_RTL
) scaleX
= -scaleX
;
326 xform
->eM11
= scaleX
;
329 xform
->eM22
= scaleY
;
330 xform
->eDx
= (double)dc
->attr
->vport_org
.x
- scaleX
* (double)dc
->attr
->wnd_org
.x
;
331 xform
->eDy
= (double)dc
->attr
->vport_org
.y
- scaleY
* (double)dc
->attr
->wnd_org
.y
;
332 if (dc
->attr
->layout
& LAYOUT_RTL
)
333 xform
->eDx
= dc
->attr
->vis_rect
.right
- dc
->attr
->vis_rect
.left
- 1 - xform
->eDx
;
336 /***********************************************************************
339 * Compares the linear transform portion of two XFORMs (i.e. the 2x2 submatrix).
340 * Returns 0 if they match.
342 static inline int linear_xform_cmp( const XFORM
*a
, const XFORM
*b
)
344 return memcmp( a
, b
, FIELD_OFFSET( XFORM
, eDx
) );
347 /***********************************************************************
350 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
351 * fields of the specified DC by creating a transformation that
352 * represents the current mapping mode and combining it with the DC's
353 * world transform. This function should be called whenever the
354 * parameters associated with the mapping mode (window and viewport
355 * extents and origins) or the world transform change.
357 void DC_UpdateXforms( DC
*dc
)
359 XFORM xformWnd2Vport
, oldworld2vport
;
361 construct_window_to_viewport(dc
, &xformWnd2Vport
);
363 oldworld2vport
= dc
->xformWorld2Vport
;
364 /* Combine with the world transformation */
365 CombineTransform( &dc
->xformWorld2Vport
, &dc
->xformWorld2Wnd
,
368 /* Create inverse of world-to-viewport transformation */
369 dc
->vport2WorldValid
= DC_InvertXform( &dc
->xformWorld2Vport
,
370 &dc
->xformVport2World
);
372 /* Reselect the font and pen back into the dc so that the size
374 if (linear_xform_cmp( &oldworld2vport
, &dc
->xformWorld2Vport
) &&
375 GetObjectType( dc
->hSelf
) != OBJ_METADC
)
377 NtGdiSelectFont(dc
->hSelf
, dc
->hFont
);
378 NtGdiSelectPen(dc
->hSelf
, dc
->hPen
);
383 /***********************************************************************
386 BOOL CDECL
nulldrv_RestoreDC( PHYSDEV dev
, INT level
)
388 DC
*dcs
, *first_dcs
, *dc
= get_nulldrv_dc( dev
);
391 /* find the state level to restore */
393 if (abs(level
) > dc
->saveLevel
|| level
== 0) return FALSE
;
394 if (level
< 0) level
= dc
->saveLevel
+ level
+ 1;
395 first_dcs
= dc
->saved_dc
;
396 for (dcs
= first_dcs
, save_level
= dc
->saveLevel
; save_level
> level
; save_level
--)
399 /* restore the state */
401 if (!PATH_RestorePath( dc
, dcs
)) return FALSE
;
403 dc
->attr
->layout
= dcs
->attr
->layout
;
404 dc
->attr
->rop_mode
= dcs
->attr
->rop_mode
;
405 dc
->attr
->poly_fill_mode
= dcs
->attr
->poly_fill_mode
;
406 dc
->attr
->stretch_blt_mode
= dcs
->attr
->stretch_blt_mode
;
407 dc
->attr
->rel_abs_mode
= dcs
->attr
->rel_abs_mode
;
408 dc
->attr
->background_mode
= dcs
->attr
->background_mode
;
409 dc
->attr
->background_color
= dcs
->attr
->background_color
;
410 dc
->attr
->text_color
= dcs
->attr
->text_color
;
411 dc
->attr
->brush_color
= dcs
->attr
->brush_color
;
412 dc
->attr
->pen_color
= dcs
->attr
->pen_color
;
413 dc
->attr
->brush_org
= dcs
->attr
->brush_org
;
414 dc
->mapperFlags
= dcs
->mapperFlags
;
415 dc
->attr
->text_align
= dcs
->attr
->text_align
;
416 dc
->attr
->char_extra
= dcs
->attr
->char_extra
;
417 dc
->breakExtra
= dcs
->breakExtra
;
418 dc
->breakRem
= dcs
->breakRem
;
419 dc
->attr
->map_mode
= dcs
->attr
->map_mode
;
420 dc
->attr
->graphics_mode
= dcs
->attr
->graphics_mode
;
421 dc
->attr
->cur_pos
= dcs
->attr
->cur_pos
;
422 dc
->attr
->arc_direction
= dcs
->attr
->arc_direction
;
423 dc
->xformWorld2Wnd
= dcs
->xformWorld2Wnd
;
424 dc
->xformWorld2Vport
= dcs
->xformWorld2Vport
;
425 dc
->xformVport2World
= dcs
->xformVport2World
;
426 dc
->vport2WorldValid
= dcs
->vport2WorldValid
;
427 dc
->attr
->wnd_org
= dcs
->attr
->wnd_org
;
428 dc
->attr
->wnd_ext
= dcs
->attr
->wnd_ext
;
429 dc
->attr
->vport_org
= dcs
->attr
->vport_org
;
430 dc
->attr
->vport_ext
= dcs
->attr
->vport_ext
;
431 dc
->virtual_res
= dcs
->virtual_res
;
432 dc
->virtual_size
= dcs
->virtual_size
;
436 if (!dc
->hClipRgn
) dc
->hClipRgn
= NtGdiCreateRectRgn( 0, 0, 0, 0 );
437 NtGdiCombineRgn( dc
->hClipRgn
, dcs
->hClipRgn
, 0, RGN_COPY
);
441 if (dc
->hClipRgn
) DeleteObject( dc
->hClipRgn
);
446 if (!dc
->hMetaRgn
) dc
->hMetaRgn
= NtGdiCreateRectRgn( 0, 0, 0, 0 );
447 NtGdiCombineRgn( dc
->hMetaRgn
, dcs
->hMetaRgn
, 0, RGN_COPY
);
451 if (dc
->hMetaRgn
) DeleteObject( dc
->hMetaRgn
);
454 DC_UpdateXforms( dc
);
455 update_dc_clipping( dc
);
457 NtGdiSelectBitmap( dev
->hdc
, dcs
->hBitmap
);
458 NtGdiSelectBrush( dev
->hdc
, dcs
->hBrush
);
459 NtGdiSelectFont( dev
->hdc
, dcs
->hFont
);
460 NtGdiSelectPen( dev
->hdc
, dcs
->hPen
);
461 SetBkColor( dev
->hdc
, dcs
->attr
->background_color
);
462 SetTextColor( dev
->hdc
, dcs
->attr
->text_color
);
463 GDISelectPalette( dev
->hdc
, dcs
->hPalette
, FALSE
);
465 dc
->saved_dc
= dcs
->saved_dc
;
467 dc
->saveLevel
= save_level
- 1;
469 /* now destroy all the saved DCs */
473 DC
*next
= first_dcs
->saved_dc
;
474 free_dc_state( first_dcs
);
481 /***********************************************************************
484 static BOOL
reset_dc_state( HDC hdc
)
488 if (!(dc
= get_dc_ptr( hdc
))) return FALSE
;
490 set_initial_dc_state( dc
);
491 SetBkColor( hdc
, RGB( 255, 255, 255 ));
492 SetTextColor( hdc
, RGB( 0, 0, 0 ));
493 NtGdiSelectBrush( hdc
, GetStockObject( WHITE_BRUSH
));
494 NtGdiSelectFont( hdc
, GetStockObject( SYSTEM_FONT
));
495 NtGdiSelectPen( hdc
, GetStockObject( BLACK_PEN
));
496 NtGdiSetVirtualResolution( hdc
, 0, 0, 0, 0 );
497 GDISelectPalette( hdc
, GetStockObject( DEFAULT_PALETTE
), FALSE
);
498 NtGdiSetBoundsRect( hdc
, NULL
, DCB_DISABLE
);
501 if (dc
->hClipRgn
) DeleteObject( dc
->hClipRgn
);
502 if (dc
->hMetaRgn
) DeleteObject( dc
->hMetaRgn
);
505 update_dc_clipping( dc
);
507 for (dcs
= dc
->saved_dc
; dcs
; dcs
= next
)
509 next
= dcs
->saved_dc
;
510 free_dc_state( dcs
);
514 release_dc_ptr( dc
);
519 /***********************************************************************
520 * NtGdiSaveDC (win32u.@)
522 INT WINAPI
NtGdiSaveDC( HDC hdc
)
527 if (!(dc
= get_dc_ptr( hdc
))) return 0;
529 if (!(newdc
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*newdc
))))
531 release_dc_ptr( dc
);
534 if (!(newdc
->attr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*newdc
->attr
) )))
536 HeapFree( GetProcessHeap(), 0, newdc
);
537 release_dc_ptr( dc
);
541 *newdc
->attr
= *dc
->attr
;
542 newdc
->hPen
= dc
->hPen
;
543 newdc
->hBrush
= dc
->hBrush
;
544 newdc
->hFont
= dc
->hFont
;
545 newdc
->hBitmap
= dc
->hBitmap
;
546 newdc
->hPalette
= dc
->hPalette
;
547 newdc
->mapperFlags
= dc
->mapperFlags
;
548 newdc
->breakExtra
= dc
->breakExtra
;
549 newdc
->breakRem
= dc
->breakRem
;
550 newdc
->xformWorld2Wnd
= dc
->xformWorld2Wnd
;
551 newdc
->xformWorld2Vport
= dc
->xformWorld2Vport
;
552 newdc
->xformVport2World
= dc
->xformVport2World
;
553 newdc
->vport2WorldValid
= dc
->vport2WorldValid
;
554 newdc
->virtual_res
= dc
->virtual_res
;
555 newdc
->virtual_size
= dc
->virtual_size
;
557 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
561 newdc
->hClipRgn
= NtGdiCreateRectRgn( 0, 0, 0, 0 );
562 NtGdiCombineRgn( newdc
->hClipRgn
, dc
->hClipRgn
, 0, RGN_COPY
);
566 newdc
->hMetaRgn
= NtGdiCreateRectRgn( 0, 0, 0, 0 );
567 NtGdiCombineRgn( newdc
->hMetaRgn
, dc
->hMetaRgn
, 0, RGN_COPY
);
570 if (!PATH_SavePath( newdc
, dc
))
572 release_dc_ptr( dc
);
573 free_dc_state( newdc
);
577 newdc
->saved_dc
= dc
->saved_dc
;
578 dc
->saved_dc
= newdc
;
579 ret
= ++dc
->saveLevel
;
580 release_dc_ptr( dc
);
585 /***********************************************************************
586 * RestoreDC (GDI32.@)
588 BOOL WINAPI
RestoreDC( HDC hdc
, INT level
)
592 BOOL success
= FALSE
;
594 TRACE("%p %d\n", hdc
, level
);
595 if ((dc
= get_dc_ptr( hdc
)))
598 physdev
= GET_DC_PHYSDEV( dc
, pRestoreDC
);
599 success
= physdev
->funcs
->pRestoreDC( physdev
, level
);
600 release_dc_ptr( dc
);
606 /***********************************************************************
607 * CreateDCW (GDI32.@)
609 HDC WINAPI
CreateDCW( LPCWSTR driver
, LPCWSTR device
, LPCWSTR output
,
610 const DEVMODEW
*initData
)
612 const WCHAR
*display
, *p
;
615 const struct gdi_dc_funcs
*funcs
;
620 if (!device
|| !DRIVER_GetDriverName( device
, buf
, 300 ))
624 ERR( "no device found for %s\n", debugstr_w(device
) );
627 lstrcpyW(buf
, driver
);
630 if (!(funcs
= DRIVER_load_driver( buf
)))
632 ERR( "no driver found for %s\n", debugstr_w(buf
) );
635 if (!(dc
= alloc_dc_ptr( NTGDI_OBJ_DC
))) return 0;
638 dc
->hBitmap
= GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP
));
640 TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
641 debugstr_w(driver
), debugstr_w(device
), debugstr_w(output
), dc
->hSelf
);
643 if (funcs
->pCreateDC
)
645 if (!funcs
->pCreateDC( &dc
->physDev
, buf
, device
, output
, initData
))
647 WARN("creation aborted by device\n" );
653 if (is_display_device( driver
))
655 else if (is_display_device( device
))
662 /* Copy only the display name. For example, \\.\DISPLAY1 in \\.\DISPLAY1\Monitor0 */
664 while (iswdigit( *p
))
666 lstrcpynW( dc
->display
, display
, p
- display
+ 1 );
667 dc
->display
[p
- display
] = '\0';
670 dc
->attr
->vis_rect
.left
= 0;
671 dc
->attr
->vis_rect
.top
= 0;
672 dc
->attr
->vis_rect
.right
= GetDeviceCaps( hdc
, DESKTOPHORZRES
);
673 dc
->attr
->vis_rect
.bottom
= GetDeviceCaps( hdc
, DESKTOPVERTRES
);
676 release_dc_ptr( dc
);
681 /***********************************************************************
682 * NtGdiCreateCompatibleDC (win32u.@)
684 HDC WINAPI
NtGdiCreateCompatibleDC( HDC hdc
)
688 const struct gdi_dc_funcs
*funcs
;
689 PHYSDEV physDev
= NULL
;
695 if (!(origDC
= get_dc_ptr( hdc
))) return 0;
696 physDev
= GET_DC_PHYSDEV( origDC
, pCreateCompatibleDC
);
697 funcs
= physDev
->funcs
;
698 release_dc_ptr( origDC
);
700 else funcs
= DRIVER_load_driver( L
"display" );
702 if (!(dc
= alloc_dc_ptr( NTGDI_OBJ_MEMDC
))) return 0;
704 TRACE("(%p): returning %p\n", hdc
, dc
->hSelf
);
706 dc
->hBitmap
= GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP
));
707 dc
->attr
->vis_rect
.left
= 0;
708 dc
->attr
->vis_rect
.top
= 0;
709 dc
->attr
->vis_rect
.right
= 1;
710 dc
->attr
->vis_rect
.bottom
= 1;
711 dc
->device_rect
= dc
->attr
->vis_rect
;
715 if (funcs
->pCreateCompatibleDC
&& !funcs
->pCreateCompatibleDC( physDev
, &dc
->physDev
))
717 WARN("creation aborted by device\n");
722 if (!dib_driver
.pCreateDC( &dc
->physDev
, NULL
, NULL
, NULL
, NULL
))
727 physDev
= GET_DC_PHYSDEV( dc
, pSelectBitmap
);
728 physDev
->funcs
->pSelectBitmap( physDev
, dc
->hBitmap
);
731 release_dc_ptr( dc
);
736 /***********************************************************************
739 BOOL WINAPI
DeleteDC( HDC hdc
)
747 if (!(dc
= get_dc_ptr( hdc
))) return FALSE
;
748 if (dc
->refcount
!= 1)
750 FIXME( "not deleting busy DC %p refcount %u\n", dc
->hSelf
, dc
->refcount
);
751 release_dc_ptr( dc
);
755 /* Call hook procedure to check whether is it OK to delete this DC */
756 if (dc
->hookProc
&& !dc
->hookProc( dc
->hSelf
, DCHC_DELETEDC
, dc
->dwHookData
, 0 ))
758 release_dc_ptr( dc
);
761 reset_dc_state( hdc
);
767 /***********************************************************************
770 HDC WINAPI
ResetDCW( HDC hdc
, const DEVMODEW
*devmode
)
775 if ((dc
= get_dc_ptr( hdc
)))
777 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pResetDC
);
778 ret
= physdev
->funcs
->pResetDC( physdev
, devmode
);
779 if (ret
) /* reset the visible region */
782 dc
->attr
->vis_rect
.left
= 0;
783 dc
->attr
->vis_rect
.top
= 0;
784 dc
->attr
->vis_rect
.right
= GetDeviceCaps( hdc
, DESKTOPHORZRES
);
785 dc
->attr
->vis_rect
.bottom
= GetDeviceCaps( hdc
, DESKTOPVERTRES
);
786 if (dc
->hVisRgn
) DeleteObject( dc
->hVisRgn
);
788 update_dc_clipping( dc
);
790 release_dc_ptr( dc
);
796 /***********************************************************************
797 * NtGdiGetDeviceCaps (win32u.@)
799 INT WINAPI
NtGdiGetDeviceCaps( HDC hdc
, INT cap
)
804 if ((dc
= get_dc_ptr( hdc
)))
806 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pGetDeviceCaps
);
807 ret
= physdev
->funcs
->pGetDeviceCaps( physdev
, cap
);
808 release_dc_ptr( dc
);
814 /***********************************************************************
815 * SetBkColor (GDI32.@)
817 COLORREF WINAPI
SetBkColor( HDC hdc
, COLORREF color
)
819 COLORREF ret
= CLR_INVALID
;
820 DC
* dc
= get_dc_ptr( hdc
);
822 TRACE("hdc=%p color=0x%08x\n", hdc
, color
);
826 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSetBkColor
);
827 ret
= dc
->attr
->background_color
;
828 dc
->attr
->background_color
= physdev
->funcs
->pSetBkColor( physdev
, color
);
829 release_dc_ptr( dc
);
835 /***********************************************************************
836 * SetTextColor (GDI32.@)
838 COLORREF WINAPI
SetTextColor( HDC hdc
, COLORREF color
)
840 COLORREF ret
= CLR_INVALID
;
841 DC
* dc
= get_dc_ptr( hdc
);
843 TRACE(" hdc=%p color=0x%08x\n", hdc
, color
);
847 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSetTextColor
);
848 ret
= dc
->attr
->text_color
;
849 dc
->attr
->text_color
= physdev
->funcs
->pSetTextColor( physdev
, color
);
850 release_dc_ptr( dc
);
856 /***********************************************************************
857 * NtGdiGetAndSetDCDword (win32u.@)
859 BOOL WINAPI
NtGdiGetAndSetDCDword( HDC hdc
, UINT method
, DWORD value
, DWORD
*prev_value
)
864 if (!(dc
= get_dc_ptr( hdc
))) return 0;
868 case NtGdiSetMapMode
:
869 *prev_value
= dc
->attr
->map_mode
;
870 ret
= set_map_mode( dc
, value
);
874 WARN( "unknown method %u\n", method
);
879 release_dc_ptr( dc
);
884 /***********************************************************************
885 * SetGraphicsMode (GDI32.@)
887 INT WINAPI
SetGraphicsMode( HDC hdc
, INT mode
)
890 DC
*dc
= get_dc_ptr( hdc
);
892 /* One would think that setting the graphics mode to GM_COMPATIBLE
893 * would also reset the world transformation matrix to the unity
894 * matrix. However, in Windows, this is not the case. This doesn't
895 * make a lot of sense to me, but that's the way it is.
898 if ((mode
> 0) && (mode
<= GM_LAST
))
900 ret
= dc
->attr
->graphics_mode
;
901 dc
->attr
->graphics_mode
= mode
;
903 /* font metrics depend on the graphics mode */
904 if (ret
!= mode
) NtGdiSelectFont(dc
->hSelf
, dc
->hFont
);
905 release_dc_ptr( dc
);
910 /***********************************************************************
911 * NtGdiGetTransform (win32u.@)
915 * Returns one of the co-ordinate space transforms
918 * hdc [I] Device context.
919 * which [I] Which xform to return:
920 * 0x203 World -> Page transform (that set by SetWorldTransform).
921 * 0x304 Page -> Device transform (the mapping mode transform).
922 * 0x204 World -> Device transform (the combination of the above two).
923 * 0x402 Device -> World transform (the inversion of the above).
924 * xform [O] The xform.
927 BOOL WINAPI
NtGdiGetTransform( HDC hdc
, DWORD which
, XFORM
*xform
)
930 DC
*dc
= get_dc_ptr( hdc
);
931 if (!dc
) return FALSE
;
936 *xform
= dc
->xformWorld2Wnd
;
940 construct_window_to_viewport(dc
, xform
);
944 *xform
= dc
->xformWorld2Vport
;
948 *xform
= dc
->xformVport2World
;
952 FIXME("Unknown code %x\n", which
);
956 release_dc_ptr( dc
);
961 /****************************************************************************
962 * CombineTransform [GDI32.@]
963 * Combines two transformation matrices.
966 * xformResult [O] Stores the result of combining the two matrices
967 * xform1 [I] Specifies the first matrix to apply
968 * xform2 [I] Specifies the second matrix to apply
971 * The same matrix can be passed in for more than one of the parameters.
975 * Failure: FALSE. Use GetLastError() to determine the cause.
977 BOOL WINAPI
CombineTransform( LPXFORM xformResult
, const XFORM
*xform1
,
978 const XFORM
*xform2
)
982 /* Check for illegal parameters */
983 if (!xformResult
|| !xform1
|| !xform2
)
986 /* Create the result in a temporary XFORM, since xformResult may be
987 * equal to xform1 or xform2 */
988 xformTemp
.eM11
= xform1
->eM11
* xform2
->eM11
+
989 xform1
->eM12
* xform2
->eM21
;
990 xformTemp
.eM12
= xform1
->eM11
* xform2
->eM12
+
991 xform1
->eM12
* xform2
->eM22
;
992 xformTemp
.eM21
= xform1
->eM21
* xform2
->eM11
+
993 xform1
->eM22
* xform2
->eM21
;
994 xformTemp
.eM22
= xform1
->eM21
* xform2
->eM12
+
995 xform1
->eM22
* xform2
->eM22
;
996 xformTemp
.eDx
= xform1
->eDx
* xform2
->eM11
+
997 xform1
->eDy
* xform2
->eM21
+
999 xformTemp
.eDy
= xform1
->eDx
* xform2
->eM12
+
1000 xform1
->eDy
* xform2
->eM22
+
1003 /* Copy the result to xformResult */
1004 *xformResult
= xformTemp
;
1010 /***********************************************************************
1011 * SetDCHook (GDI32.@)
1013 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1015 BOOL WINAPI
SetDCHook( HDC hdc
, DCHOOKPROC hookProc
, DWORD_PTR dwHookData
)
1017 DC
*dc
= get_dc_ptr( hdc
);
1019 if (!dc
) return FALSE
;
1021 dc
->dwHookData
= dwHookData
;
1022 dc
->hookProc
= hookProc
;
1023 release_dc_ptr( dc
);
1028 /***********************************************************************
1029 * GetDCHook (GDI32.@)
1031 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1033 DWORD_PTR WINAPI
GetDCHook( HDC hdc
, DCHOOKPROC
*proc
)
1035 DC
*dc
= get_dc_ptr( hdc
);
1039 if (proc
) *proc
= dc
->hookProc
;
1040 ret
= dc
->dwHookData
;
1041 release_dc_ptr( dc
);
1046 /***********************************************************************
1047 * SetHookFlags (GDI32.@)
1049 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
1051 WORD WINAPI
SetHookFlags( HDC hdc
, WORD flags
)
1053 DC
*dc
= get_dc_obj( hdc
); /* not get_dc_ptr, this needs to work from any thread */
1058 TRACE("hDC %p, flags %04x\n",hdc
,flags
);
1060 if (flags
& DCHF_INVALIDATEVISRGN
)
1061 ret
= InterlockedExchange( &dc
->dirty
, 1 );
1062 else if (flags
& DCHF_VALIDATEVISRGN
|| !flags
)
1063 ret
= InterlockedExchange( &dc
->dirty
, 0 );
1065 if (flags
& DCHF_DISABLEDC
)
1066 ret
= InterlockedExchange( &dc
->attr
->disabled
, 1 );
1067 else if (flags
& DCHF_ENABLEDC
)
1068 ret
= InterlockedExchange( &dc
->attr
->disabled
, 0 );
1070 GDI_ReleaseObj( hdc
);
1072 if (flags
& DCHF_RESETDC
) ret
= reset_dc_state( hdc
);
1076 /***********************************************************************
1077 * NtGdiGetDeviceGammaRamp (win32u.@)
1079 BOOL WINAPI
NtGdiGetDeviceGammaRamp( HDC hdc
, void *ptr
)
1082 DC
*dc
= get_dc_ptr( hdc
);
1084 TRACE("%p, %p\n", hdc
, ptr
);
1087 if (GetObjectType( hdc
) != OBJ_MEMDC
)
1089 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pGetDeviceGammaRamp
);
1090 ret
= physdev
->funcs
->pGetDeviceGammaRamp( physdev
, ptr
);
1092 else SetLastError( ERROR_INVALID_PARAMETER
);
1093 release_dc_ptr( dc
);
1098 static BOOL
check_gamma_ramps(void *ptr
)
1102 while (ramp
< (WORD
*)ptr
+ 3 * 256)
1104 float r_x
, r_y
, r_lx
, r_ly
, r_d
, r_v
, r_e
, g_avg
, g_min
, g_max
;
1105 unsigned i
, f
, l
, g_n
, c
;
1111 TRACE("inverted or flat gamma ramp (%d->%d), rejected\n", f
, l
);
1115 g_min
= g_max
= g_avg
= 0.0;
1117 /* check gamma ramp entries to estimate the gamma */
1118 TRACE("analyzing gamma ramp (%d->%d)\n", f
, l
);
1119 for (i
=1, g_n
=0; i
<255; i
++)
1121 if (ramp
[i
] < f
|| ramp
[i
] > l
)
1123 TRACE("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i
, ramp
[i
], f
, l
);
1127 if (!c
) continue; /* avoid log(0) */
1129 /* normalize entry values into 0..1 range */
1130 r_x
= i
/255.0; r_y
= c
/ r_d
;
1131 /* compute logarithms of values */
1132 r_lx
= log(r_x
); r_ly
= log(r_y
);
1133 /* compute gamma for this entry */
1135 /* compute differential (error estimate) for this entry */
1136 /* some games use table-based logarithms that magnifies the error by 128 */
1137 r_e
= -r_lx
* 128 / (c
* r_lx
* r_lx
);
1139 /* compute min & max while compensating for estimated error */
1140 if (!g_n
|| g_min
> (r_v
+ r_e
)) g_min
= r_v
+ r_e
;
1141 if (!g_n
|| g_max
< (r_v
- r_e
)) g_max
= r_v
- r_e
;
1143 /* add to average */
1150 TRACE("no gamma data, shouldn't happen\n");
1154 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f
, 65535-l
, g_avg
);
1156 /* check that the gamma is reasonably uniform across the ramp */
1157 if (g_max
- g_min
> 12.8)
1159 TRACE("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max
, g_min
, g_avg
);
1163 /* check that the gamma is not too bright */
1166 TRACE("too bright gamma ( %5.3f), rejected\n", g_avg
);
1176 /***********************************************************************
1177 * NtGdiSetDeviceGammaRamp (win32u.@)
1179 BOOL WINAPI
NtGdiSetDeviceGammaRamp( HDC hdc
, void *ptr
)
1182 DC
*dc
= get_dc_ptr( hdc
);
1184 TRACE( "%p, %p\n", hdc
, ptr
);
1187 if (GetObjectType( hdc
) != OBJ_MEMDC
)
1189 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSetDeviceGammaRamp
);
1191 if (check_gamma_ramps(ptr
))
1192 ret
= physdev
->funcs
->pSetDeviceGammaRamp( physdev
, ptr
);
1194 else SetLastError( ERROR_INVALID_PARAMETER
);
1195 release_dc_ptr( dc
);
1201 /***********************************************************************
1202 * NtGdiGetBoundsRect (win32u.@)
1204 UINT WINAPI
NtGdiGetBoundsRect( HDC hdc
, RECT
*rect
, UINT flags
)
1209 DC
*dc
= get_dc_ptr( hdc
);
1211 if ( !dc
) return 0;
1213 physdev
= GET_DC_PHYSDEV( dc
, pGetBoundsRect
);
1214 ret
= physdev
->funcs
->pGetBoundsRect( physdev
, &device_rect
, DCB_RESET
);
1217 release_dc_ptr( dc
);
1220 if (dc
->bounds_enabled
&& ret
== DCB_SET
) add_bounds_rect( &dc
->bounds
, &device_rect
);
1224 if (is_rect_empty( &dc
->bounds
))
1226 rect
->left
= rect
->top
= rect
->right
= rect
->bottom
= 0;
1232 rect
->left
= max( rect
->left
, 0 );
1233 rect
->top
= max( rect
->top
, 0 );
1234 rect
->right
= min( rect
->right
, dc
->attr
->vis_rect
.right
- dc
->attr
->vis_rect
.left
);
1235 rect
->bottom
= min( rect
->bottom
, dc
->attr
->vis_rect
.bottom
- dc
->attr
->vis_rect
.top
);
1238 dp_to_lp( dc
, (POINT
*)rect
, 2 );
1242 if (flags
& DCB_RESET
) reset_bounds( &dc
->bounds
);
1243 release_dc_ptr( dc
);
1248 /***********************************************************************
1249 * NtGdiSetBoundsRect (win32u.@)
1251 UINT WINAPI
NtGdiSetBoundsRect( HDC hdc
, const RECT
*rect
, UINT flags
)
1257 if ((flags
& DCB_ENABLE
) && (flags
& DCB_DISABLE
)) return 0;
1258 if (!(dc
= get_dc_ptr( hdc
))) return 0;
1260 physdev
= GET_DC_PHYSDEV( dc
, pSetBoundsRect
);
1261 ret
= physdev
->funcs
->pSetBoundsRect( physdev
, &dc
->bounds
, flags
);
1264 release_dc_ptr( dc
);
1268 ret
= (dc
->bounds_enabled
? DCB_ENABLE
: DCB_DISABLE
) |
1269 (is_rect_empty( &dc
->bounds
) ? ret
& DCB_SET
: DCB_SET
);
1271 if (flags
& DCB_RESET
) reset_bounds( &dc
->bounds
);
1273 if ((flags
& DCB_ACCUMULATE
) && rect
)
1277 lp_to_dp( dc
, (POINT
*)&rc
, 2 );
1278 add_bounds_rect( &dc
->bounds
, &rc
);
1281 if (flags
& DCB_ENABLE
) dc
->bounds_enabled
= TRUE
;
1282 if (flags
& DCB_DISABLE
) dc
->bounds_enabled
= FALSE
;
1284 release_dc_ptr( dc
);
1289 /***********************************************************************
1290 * SetLayout (GDI32.@)
1292 * Sets left->right or right->left text layout flags of a dc.
1295 DWORD WINAPI
SetLayout(HDC hdc
, DWORD layout
)
1297 DWORD oldlayout
= GDI_ERROR
;
1299 DC
* dc
= get_dc_ptr( hdc
);
1302 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSetLayout
);
1303 oldlayout
= physdev
->funcs
->pSetLayout( physdev
, layout
);
1304 release_dc_ptr( dc
);
1307 TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc
, oldlayout
, layout
);
1312 /***********************************************************************
1313 * SetDCBrushColor (GDI32.@)
1315 COLORREF WINAPI
SetDCBrushColor(HDC hdc
, COLORREF crColor
)
1318 COLORREF oldClr
= CLR_INVALID
;
1320 TRACE("hdc(%p) crColor(%08x)\n", hdc
, crColor
);
1322 dc
= get_dc_ptr( hdc
);
1325 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSetDCBrushColor
);
1326 crColor
= physdev
->funcs
->pSetDCBrushColor( physdev
, crColor
);
1327 if (crColor
!= CLR_INVALID
)
1329 oldClr
= dc
->attr
->brush_color
;
1330 dc
->attr
->brush_color
= crColor
;
1332 release_dc_ptr( dc
);
1338 /***********************************************************************
1339 * SetDCPenColor (GDI32.@)
1341 COLORREF WINAPI
SetDCPenColor(HDC hdc
, COLORREF crColor
)
1344 COLORREF oldClr
= CLR_INVALID
;
1346 TRACE("hdc(%p) crColor(%08x)\n", hdc
, crColor
);
1348 dc
= get_dc_ptr( hdc
);
1351 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSetDCPenColor
);
1352 crColor
= physdev
->funcs
->pSetDCPenColor( physdev
, crColor
);
1353 if (crColor
!= CLR_INVALID
)
1355 oldClr
= dc
->attr
->pen_color
;
1356 dc
->attr
->pen_color
= crColor
;
1358 release_dc_ptr( dc
);