2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: Functions for creation and destruction of DCs
5 * FILE: win32ss/gdi/ntgdi/dcobjs.c
6 * PROGRAMER: Timo Kreuzer (timo.kreuzer@rectos.org)
16 DC_vUpdateFillBrush(PDC pdc
)
18 PDC_ATTR pdcattr
= pdc
->pdcattr
;
21 /* Check if the brush handle has changed */
22 if (pdcattr
->hbrush
!= pdc
->dclevel
.pbrFill
->BaseObject
.hHmgr
)
24 /* Try to lock the new brush */
25 pbrFill
= BRUSH_ShareLockBrush(pdcattr
->hbrush
);
28 /* Unlock old brush, set new brush */
29 BRUSH_ShareUnlockBrush(pdc
->dclevel
.pbrFill
);
30 pdc
->dclevel
.pbrFill
= pbrFill
;
32 /* Mark eboFill as dirty */
33 pdcattr
->ulDirty_
|= DIRTY_FILL
;
37 /* Invalid brush handle, restore old one */
38 pdcattr
->hbrush
= pdc
->dclevel
.pbrFill
->BaseObject
.hHmgr
;
42 /* Check if the EBRUSHOBJ needs update */
43 if (pdcattr
->ulDirty_
& DIRTY_FILL
)
46 EBRUSHOBJ_vUpdateFromDC(&pdc
->eboFill
, pdc
->dclevel
.pbrFill
, pdc
);
49 /* Check for DC brush */
50 if (pdcattr
->hbrush
== StockObjects
[DC_BRUSH
])
52 /* Update the eboFill's solid color */
53 EBRUSHOBJ_vSetSolidRGBColor(&pdc
->eboFill
, pdcattr
->crBrushClr
);
57 pdcattr
->ulDirty_
&= ~(DIRTY_FILL
| DC_BRUSH_DIRTY
);
62 DC_vUpdateLineBrush(PDC pdc
)
64 PDC_ATTR pdcattr
= pdc
->pdcattr
;
67 /* Check if the pen handle has changed */
68 if (pdcattr
->hpen
!= pdc
->dclevel
.pbrLine
->BaseObject
.hHmgr
)
70 /* Try to lock the new pen */
71 pbrLine
= PEN_ShareLockPen(pdcattr
->hpen
);
74 /* Unlock old brush, set new brush */
75 BRUSH_ShareUnlockBrush(pdc
->dclevel
.pbrLine
);
76 pdc
->dclevel
.pbrLine
= pbrLine
;
78 /* Mark eboLine as dirty */
79 pdcattr
->ulDirty_
|= DIRTY_LINE
;
83 /* Invalid pen handle, restore old one */
84 pdcattr
->hpen
= pdc
->dclevel
.pbrLine
->BaseObject
.hHmgr
;
88 /* Check if the EBRUSHOBJ needs update */
89 if (pdcattr
->ulDirty_
& DIRTY_LINE
)
92 EBRUSHOBJ_vUpdateFromDC(&pdc
->eboLine
, pdc
->dclevel
.pbrLine
, pdc
);
95 /* Check for DC pen */
96 if (pdcattr
->hpen
== StockObjects
[DC_PEN
])
98 /* Update the eboLine's solid color */
99 EBRUSHOBJ_vSetSolidRGBColor(&pdc
->eboLine
, pdcattr
->crPenClr
);
103 pdcattr
->ulDirty_
&= ~(DIRTY_LINE
| DC_PEN_DIRTY
);
108 DC_vUpdateTextBrush(PDC pdc
)
110 PDC_ATTR pdcattr
= pdc
->pdcattr
;
112 /* Timo : The text brush should never be changed.
113 * Jérôme : Yeah, but its palette must be updated anyway! */
114 if(pdcattr
->ulDirty_
& DIRTY_TEXT
)
115 EBRUSHOBJ_vUpdateFromDC(&pdc
->eboText
, pbrDefaultBrush
, pdc
);
117 /* Update the eboText's solid color */
118 EBRUSHOBJ_vSetSolidRGBColor(&pdc
->eboText
, pdcattr
->crForegroundClr
);
121 pdcattr
->ulDirty_
&= ~DIRTY_TEXT
;
126 DC_vUpdateBackgroundBrush(PDC pdc
)
128 PDC_ATTR pdcattr
= pdc
->pdcattr
;
130 if(pdcattr
->ulDirty_
& DIRTY_BACKGROUND
)
131 EBRUSHOBJ_vUpdateFromDC(&pdc
->eboBackground
, pbrDefaultBrush
, pdc
);
133 /* Update the eboBackground's solid color */
134 EBRUSHOBJ_vSetSolidRGBColor(&pdc
->eboBackground
, pdcattr
->crBackgroundClr
);
137 pdcattr
->ulDirty_
&= ~DIRTY_BACKGROUND
;
142 DC_vSetBrushOrigin(PDC pdc
, LONG x
, LONG y
)
144 /* Set the brush origin */
145 pdc
->dclevel
.ptlBrushOrigin
.x
= x
;
146 pdc
->dclevel
.ptlBrushOrigin
.y
= y
;
148 /* Set the fill origin */
149 pdc
->ptlFillOrigin
.x
= x
+ pdc
->ptlDCOrig
.x
;
150 pdc
->ptlFillOrigin
.y
= y
+ pdc
->ptlDCOrig
.y
;
154 * \name NtGdiSetBrushOrg
156 * \brief Sets the brush origin that GDI uses when drawing with pattern
157 * brushes. The brush origin is relative to the DC origin.
161 _Success_(return!=FALSE
)
169 _Out_opt_ LPPOINT pptOut
)
174 pdc
= DC_LockDc(hdc
);
177 EngSetLastError(ERROR_INVALID_HANDLE
);
181 /* Check if the old origin was requested */
184 /* Enter SEH for buffer transfer */
187 /* Probe and copy the old origin */
188 ProbeForWrite(pptOut
, sizeof(POINT
), 1);
189 *pptOut
= pdc
->pdcattr
->ptlBrushOrigin
;
191 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
194 _SEH2_YIELD(return FALSE
);
199 /* Call the internal function */
200 DC_vSetBrushOrigin(pdc
, x
, y
);
202 /* Unlock the DC and return success */
212 BOOL ForceBackground
)
215 HPALETTE oldPal
= NULL
;
218 // FIXME: Mark the palette as a [fore\back]ground pal
219 pdc
= DC_LockDc(hDC
);
225 /* Check if this is a valid palette handle */
226 ppal
= PALETTE_ShareLockPalette(hpal
);
233 /// FIXME: we shouldn't dereference pSurface when the PDEV is not locked
234 /* Is this a valid palette for this depth? */
235 if ((!pdc
->dclevel
.pSurface
) ||
236 (BitsPerFormat(pdc
->dclevel
.pSurface
->SurfObj
.iBitmapFormat
) <= 8
237 && (ppal
->flFlags
& PAL_INDEXED
)) ||
238 (BitsPerFormat(pdc
->dclevel
.pSurface
->SurfObj
.iBitmapFormat
) > 8))
240 /* Get old palette, set new one */
241 oldPal
= pdc
->dclevel
.hpal
;
242 pdc
->dclevel
.hpal
= hpal
;
243 DC_vSelectPalette(pdc
, ppal
);
245 /* Mark the brushes invalid */
246 pdc
->pdcattr
->ulDirty_
|= DIRTY_FILL
| DIRTY_LINE
|
247 DIRTY_BACKGROUND
| DIRTY_TEXT
;
250 if(pdc
->dctype
== DCTYPE_MEMORY
)
252 // This didn't work anyway
253 //IntGdiRealizePalette(hDC);
256 PALETTE_ShareUnlockPalette(ppal
);
274 if (hDC
== NULL
|| hBrush
== NULL
) return NULL
;
276 pDC
= DC_LockDc(hDC
);
282 /* Simply return the user mode value, without checking */
283 hOrgBrush
= pDC
->pdcattr
->hbrush
;
284 pDC
->pdcattr
->hbrush
= hBrush
;
285 DC_vUpdateFillBrush(pDC
);
304 if (hDC
== NULL
|| hPen
== NULL
) return NULL
;
306 pDC
= DC_LockDc(hDC
);
312 /* Simply return the user mode value, without checking */
313 hOrgPen
= pDC
->pdcattr
->hpen
;
314 pDC
->pdcattr
->hpen
= hPen
;
315 DC_vUpdateLineBrush(pDC
);
324 DC_bIsBitmapCompatible(PDC pdc
, PSURFACE psurf
)
328 /* Must be an API bitmap */
329 if (!(psurf
->flags
& API_BITMAP
)) return FALSE
;
331 /* DIB sections are always compatible */
332 if (psurf
->hSecure
!= NULL
) return TRUE
;
334 /* See if this is the same PDEV */
335 if (psurf
->SurfObj
.hdev
== (HDEV
)pdc
->ppdev
)
338 /* Get the bit depth of the bitmap */
339 cBitsPixel
= gajBitsPerFormat
[psurf
->SurfObj
.iBitmapFormat
];
341 /* 1 BPP is compatible */
342 if ((cBitsPixel
== 1) || (cBitsPixel
== pdc
->ppdev
->gdiinfo
.cBitsPixel
))
359 PSURFACE psurfNew
, psurfOld
;
363 /* Verify parameters */
364 if (hdc
== NULL
|| hbmp
== NULL
) return NULL
;
366 /* First lock the DC */
367 pdc
= DC_LockDc(hdc
);
373 /* Must be a memory dc to select a bitmap */
374 if (pdc
->dctype
!= DC_TYPE_MEMORY
)
380 /* Save the old bitmap */
381 psurfOld
= pdc
->dclevel
.pSurface
;
383 /* Check if there is a bitmap selected */
386 /* Get the old bitmap's handle */
387 hbmpOld
= psurfOld
->BaseObject
.hHmgr
;
391 /* Use the default bitmap */
392 hbmpOld
= StockObjects
[DEFAULT_BITMAP
];
395 /* Check if the new bitmap is already selected */
398 /* Unlock the DC and return the old bitmap */
403 /* Check if the default bitmap was passed */
404 if (hbmp
== StockObjects
[DEFAULT_BITMAP
])
408 /* Default bitmap is 1x1 pixel */
409 pdc
->dclevel
.sizl
.cx
= 1;
410 pdc
->dclevel
.sizl
.cy
= 1;
414 /* Reference the new bitmap and check if it's valid */
415 psurfNew
= SURFACE_ShareLockSurface(hbmp
);
422 /* Check if the bitmap is compatible with the dc */
423 if (!DC_bIsBitmapCompatible(pdc
, psurfNew
))
425 /* Dereference the bitmap, unlock the DC and fail. */
426 SURFACE_ShareUnlockSurface(psurfNew
);
431 /* Set the bitmap's hdc and check if it was set before */
432 hdcOld
= InterlockedCompareExchangePointer((PVOID
*)&psurfNew
->hdc
, hdc
, 0);
435 /* The bitmap is already selected into a different DC */
436 ASSERT(hdcOld
!= hdc
);
438 /* Dereference the bitmap, unlock the DC and fail. */
439 SURFACE_ShareUnlockSurface(psurfNew
);
444 /* Copy the bitmap size */
445 pdc
->dclevel
.sizl
= psurfNew
->SurfObj
.sizlBitmap
;
447 /* Check if the bitmap is a dibsection */
448 if (psurfNew
->hSecure
)
450 /* Set DIBSECTION attribute */
451 pdc
->pdcattr
->ulDirty_
|= DC_DIBSECTION
;
455 /* Remove DIBSECTION attribute */
456 pdc
->pdcattr
->ulDirty_
&= ~DC_DIBSECTION
;
460 /* Select the new bitmap */
461 pdc
->dclevel
.pSurface
= psurfNew
;
463 /* Check if there was a bitmap selected before */
466 /* Reset hdc of the old bitmap, it isn't selected anymore */
467 psurfOld
->hdc
= NULL
;
469 /* Dereference the old bitmap */
470 SURFACE_ShareUnlockSurface(psurfOld
);
473 /* Mark the DC brushes and the RAO region invalid */
474 pdc
->pdcattr
->ulDirty_
|= DIRTY_FILL
| DIRTY_LINE
;
475 pdc
->fs
|= DC_FLAG_DIRTY_RAO
;
477 /* Update the system region */
478 REGION_SetRectRgn(pdc
->prgnVis
,
481 pdc
->dclevel
.sizl
.cx
,
482 pdc
->dclevel
.sizl
.cy
);
487 /* Return the old bitmap handle */
499 PPATH pPath
, pNewPath
;
500 BOOL success
= FALSE
;
504 pdc
= DC_LockDc(hDC
);
507 EngSetLastError(ERROR_INVALID_PARAMETER
);
510 pdcattr
= pdc
->pdcattr
;
512 pPath
= PATH_LockPath(pdc
->dclevel
.hPath
);
519 /* Check that path is closed */
520 if (pPath
->state
!= PATH_Closed
)
522 EngSetLastError(ERROR_CAN_NOT_COMPLETE
);
527 /* Construct a region from the path */
528 RgnPath
= IntSysCreateRectpRgn(0, 0, 0, 0);
531 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
536 pNewPath
= PATH_FlattenPath(pPath
);
538 success
= PATH_PathToRegion(pNewPath
, pdcattr
->jFillMode
, RgnPath
);
540 PATH_UnlockPath(pNewPath
);
541 PATH_Delete(pNewPath
->BaseObject
.hHmgr
);
543 if (success
) success
= IntGdiExtSelectClipRgn(pdc
, RgnPath
, Mode
) != ERROR
;
545 REGION_Delete(RgnPath
);
548 PATH_UnlockPath(pPath
);
549 PATH_Delete(pdc
->dclevel
.hPath
);
550 pdc
->dclevel
.flPath
&= ~DCPATH_ACTIVE
;
551 pdc
->dclevel
.hPath
= NULL
;
567 // Legacy crap that will die with font engine rewrite
568 if (!NT_SUCCESS(TextIntRealizeFont(hlfntNew
, NULL
)))
573 /* Get the current selected font */
574 hlfntOld
= pdc
->dclevel
.plfnt
->BaseObject
.hHmgr
;
576 /* Check if a new font should be selected */
577 if (hlfntNew
!= hlfntOld
)
579 /* Lock the new font */
580 plfntNew
= LFONT_ShareLockFont(hlfntNew
);
583 /* Success, dereference the old font */
584 LFONT_ShareUnlockFont(pdc
->dclevel
.plfnt
);
586 /* Select the new font */
587 pdc
->dclevel
.plfnt
= plfntNew
;
588 pdc
->pdcattr
->hlfntNew
= hlfntNew
;
590 /* Update dirty flags */
591 pdc
->pdcattr
->ulDirty_
|= DIRTY_CHARSET
;
592 pdc
->pdcattr
->ulDirty_
&= ~SLOW_WIDTHS
;
596 /* Failed, restore old, return NULL */
597 pdc
->pdcattr
->hlfntNew
= hlfntOld
;
614 /* Check parameters */
615 if ((hdc
== NULL
) || (hfont
== NULL
))
621 pdc
= DC_LockDc(hdc
);
627 /* Call the internal function */
628 hfontOld
= DC_hSelectFont(pdc
, hfont
);
633 /* Return the previously selected font */
639 NtGdiGetDCObject(HDC hDC
, INT ObjectType
)
645 /* From Wine: GetCurrentObject does not SetLastError() on a null object */
646 if(!hDC
) return NULL
;
648 if(!(pdc
= DC_LockDc(hDC
)))
652 pdcattr
= pdc
->pdcattr
;
654 if (pdcattr
->ulDirty_
& (DIRTY_FILL
| DC_BRUSH_DIRTY
))
655 DC_vUpdateFillBrush(pdc
);
657 if (pdcattr
->ulDirty_
& (DIRTY_LINE
| DC_PEN_DIRTY
))
658 DC_vUpdateLineBrush(pdc
);
662 case GDI_OBJECT_TYPE_EXTPEN
:
663 case GDI_OBJECT_TYPE_PEN
:
664 SelObject
= pdcattr
->hpen
;
667 case GDI_OBJECT_TYPE_BRUSH
:
668 SelObject
= pdcattr
->hbrush
;
671 case GDI_OBJECT_TYPE_PALETTE
:
672 SelObject
= pdc
->dclevel
.hpal
;
675 case GDI_OBJECT_TYPE_FONT
:
676 SelObject
= pdcattr
->hlfntNew
;
679 case GDI_OBJECT_TYPE_BITMAP
:
681 SURFACE
*psurf
= pdc
->dclevel
.pSurface
;
682 SelObject
= psurf
? psurf
->BaseObject
.hHmgr
: StockObjects
[DEFAULT_BITMAP
];
686 case GDI_OBJECT_TYPE_COLORSPACE
:
687 DPRINT1("FIXME: NtGdiGetCurrentObject() ObjectType OBJ_COLORSPACE not supported yet!\n");
688 // SelObject = dc->dclevel.pColorSpace.BaseObject.hHmgr; ?
694 EngSetLastError(ERROR_INVALID_PARAMETER
);
702 /* See WINE, MSDN, OSR and Feng Yuan - Windows Graphics Programming Win32 GDI and DirectDraw
704 * 1st: http://www.codeproject.com/gdi/cliprgnguide.asp is wrong!
706 * The intersection of the clip with the meta region is not Rao it's API!
707 * Go back and read 7.2 Clipping pages 418-19:
709 * 1) The Rao region is the intersection of the API region and the system region,
710 * named after the Microsoft engineer who initially proposed it.
711 * 2) The Rao region can be calculated from the API region and the system region.
714 * API region is the intersection of the meta region and the clipping region,
715 * clearly named after the fact that it is controlled by GDI API calls.
726 PREGION prgnSrc
= NULL
;
728 pdc
= DC_LockDc(hdc
);
731 EngSetLastError(ERROR_INVALID_HANDLE
);
738 prgnSrc
= pdc
->dclevel
.prgnClip
;
742 prgnSrc
= pdc
->dclevel
.prgnMeta
;
746 if (pdc
->fs
& DC_FLAG_DIRTY_RAO
)
747 CLIPPING_UpdateGCRegion(pdc
);
750 prgnSrc
= pdc
->prgnAPI
;
752 else if (pdc
->dclevel
.prgnClip
)
754 prgnSrc
= pdc
->dclevel
.prgnClip
;
756 else if (pdc
->dclevel
.prgnMeta
)
758 prgnSrc
= pdc
->dclevel
.prgnMeta
;
763 prgnSrc
= pdc
->prgnVis
;
772 PREGION prgnDest
= REGION_LockRgn(hrgnDest
);
775 ret
= IntGdiCombineRgn(prgnDest
, prgnSrc
, 0, RGN_COPY
) == ERROR
? -1 : 1;
776 if ((ret
== 1) && (iCode
== SYSRGN
))
778 /// \todo FIXME This is not really correct, since we already modified the region
779 ret
= REGION_bOffsetRgn(prgnDest
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
781 REGION_UnlockRgn(prgnDest
);
798 OUT OPTIONAL PVOID pvBuf
)