[WIN32SS] Improve the FILE header section. Brought to you by Adam Stachowicz. CORE...
[reactos.git] / reactos / win32ss / gdi / ntgdi / fillshap.c
1 /*
2 * PROJECT: ReactOS win32 kernel mode subsystem
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: win32ss/gdi/ntgdi/fillshap.c
5 * PURPOSE: fillshap
6 * PROGRAMMER:
7 */
8
9 #include <win32k.h>
10
11 #define NDEBUG
12 #include <debug.h>
13
14 #define Rsin(d) ((d) == 0.0 ? 0.0 : ((d) == 90.0 ? 1.0 : sin(d*M_PI/180.0)))
15 #define Rcos(d) ((d) == 0.0 ? 1.0 : ((d) == 90.0 ? 0.0 : cos(d*M_PI/180.0)))
16
17 BOOL FASTCALL
18 IntGdiPolygon(PDC dc,
19 PPOINT Points,
20 int Count)
21 {
22 SURFACE *psurf;
23 PBRUSH pbrLine, pbrFill;
24 BOOL ret = FALSE; // Default to failure
25 RECTL DestRect;
26 int CurrentPoint;
27 PDC_ATTR pdcattr;
28 POINTL BrushOrigin;
29 // int Left;
30 // int Top;
31
32 ASSERT(dc); // Caller's responsibility to pass a valid dc
33
34 if (!Points || Count < 2 )
35 {
36 EngSetLastError(ERROR_INVALID_PARAMETER);
37 return FALSE;
38 }
39
40 /*
41 // Find start x, y
42 Left = Points[0].x;
43 Top = Points[0].y;
44 for (CurrentPoint = 1; CurrentPoint < Count; ++CurrentPoint) {
45 Left = min(Left, Points[CurrentPoint].x);
46 Top = min(Top, Points[CurrentPoint].y);
47 }
48 */
49
50 pdcattr = dc->pdcattr;
51
52 /* Convert to screen coordinates */
53 IntLPtoDP(dc, Points, Count);
54 for (CurrentPoint = 0; CurrentPoint < Count; CurrentPoint++)
55 {
56 Points[CurrentPoint].x += dc->ptlDCOrig.x;
57 Points[CurrentPoint].y += dc->ptlDCOrig.y;
58 }
59 // No need to have path here.
60 {
61 DestRect.left = Points[0].x;
62 DestRect.right = Points[0].x;
63 DestRect.top = Points[0].y;
64 DestRect.bottom = Points[0].y;
65
66 for (CurrentPoint = 1; CurrentPoint < Count; ++CurrentPoint)
67 {
68 DestRect.left = min(DestRect.left, Points[CurrentPoint].x);
69 DestRect.right = max(DestRect.right, Points[CurrentPoint].x);
70 DestRect.top = min(DestRect.top, Points[CurrentPoint].y);
71 DestRect.bottom = max(DestRect.bottom, Points[CurrentPoint].y);
72 }
73
74 if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
75 DC_vUpdateFillBrush(dc);
76
77 if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
78 DC_vUpdateLineBrush(dc);
79
80 /* Special locking order to avoid lock-ups */
81 pbrFill = dc->dclevel.pbrFill;
82 pbrLine = dc->dclevel.pbrLine;
83 psurf = dc->dclevel.pSurface;
84 /* FIXME: psurf can be NULL!!!! don't assert but handle this case gracefully! */
85 ASSERT(psurf);
86
87 /* Now fill the polygon with the current fill brush. */
88 if (!(pbrFill->flAttrs & BR_IS_NULL))
89 {
90 BrushOrigin = *((PPOINTL)&pbrFill->ptOrigin);
91 BrushOrigin.x += dc->ptlDCOrig.x;
92 BrushOrigin.y += dc->ptlDCOrig.y;
93 ret = IntFillPolygon (dc,
94 psurf,
95 &dc->eboFill.BrushObject,
96 Points,
97 Count,
98 DestRect,
99 &BrushOrigin);
100 }
101
102 // Draw the Polygon Edges with the current pen ( if not a NULL pen )
103 if (!(pbrLine->flAttrs & BR_IS_NULL))
104 {
105 int i;
106
107 for (i = 0; i < Count-1; i++)
108 {
109
110 // DPRINT1("Polygon Making line from (%d,%d) to (%d,%d)\n",
111 // Points[0].x, Points[0].y,
112 // Points[1].x, Points[1].y );
113
114 ret = IntEngLineTo(&psurf->SurfObj,
115 &dc->co.ClipObj,
116 &dc->eboLine.BrushObject,
117 Points[i].x, /* From */
118 Points[i].y,
119 Points[i+1].x, /* To */
120 Points[i+1].y,
121 &DestRect,
122 ROP2_TO_MIX(pdcattr->jROP2)); /* MIX */
123 if (!ret) break;
124 }
125 /* Close the polygon */
126 if (ret)
127 {
128 ret = IntEngLineTo(&psurf->SurfObj,
129 &dc->co.ClipObj,
130 &dc->eboLine.BrushObject,
131 Points[Count-1].x, /* From */
132 Points[Count-1].y,
133 Points[0].x, /* To */
134 Points[0].y,
135 &DestRect,
136 ROP2_TO_MIX(pdcattr->jROP2)); /* MIX */
137 }
138 }
139 }
140
141 return ret;
142 }
143
144 BOOL FASTCALL
145 IntGdiPolyPolygon(DC *dc,
146 LPPOINT Points,
147 PULONG PolyCounts,
148 int Count)
149 {
150 if (PATH_IsPathOpen(dc->dclevel))
151 return PATH_PolyPolygon ( dc, Points, (PINT)PolyCounts, Count);
152
153 while (--Count >=0)
154 {
155 if (!IntGdiPolygon ( dc, Points, *PolyCounts ))
156 return FALSE;
157 Points+=*PolyCounts++;
158 }
159 return TRUE;
160 }
161
162 BOOL FASTCALL
163 IntPolygon(HDC hdc, POINT *Point, int Count)
164 {
165 PDC dc;
166 if (!(dc = DC_LockDc(hdc)))
167 {
168 EngSetLastError(ERROR_INVALID_HANDLE);
169 return FALSE;
170 }
171 return IntGdiPolygon(dc, Point, Count);
172 }
173
174
175 /******************************************************************************/
176
177 /*
178 * NtGdiEllipse
179 *
180 * Author
181 * Filip Navara
182 *
183 * Remarks
184 * This function uses optimized Bresenham's ellipse algorithm. It draws
185 * four lines of the ellipse in one pass.
186 *
187 */
188
189 BOOL APIENTRY
190 NtGdiEllipse(
191 HDC hDC,
192 int Left,
193 int Top,
194 int Right,
195 int Bottom)
196 {
197 PDC dc;
198 PDC_ATTR pdcattr;
199 RECTL RectBounds;
200 PBRUSH pbrush;
201 BOOL ret = TRUE;
202 LONG PenWidth, PenOrigWidth;
203 LONG RadiusX, RadiusY, CenterX, CenterY;
204 PBRUSH pFillBrushObj;
205 BRUSH tmpFillBrushObj;
206
207 if ((Left == Right) || (Top == Bottom)) return TRUE;
208
209 dc = DC_LockDc(hDC);
210 if (dc == NULL)
211 {
212 EngSetLastError(ERROR_INVALID_HANDLE);
213 return FALSE;
214 }
215 if (dc->dctype == DC_TYPE_INFO)
216 {
217 DC_UnlockDc(dc);
218 /* Yes, Windows really returns TRUE in this case */
219 return TRUE;
220 }
221
222 if (PATH_IsPathOpen(dc->dclevel))
223 {
224 ret = PATH_Ellipse(dc, Left, Top, Right, Bottom);
225 DC_UnlockDc(dc);
226 return ret;
227 }
228
229 if (Right < Left)
230 {
231 INT tmp = Right; Right = Left; Left = tmp;
232 }
233 if (Bottom < Top)
234 {
235 INT tmp = Bottom; Bottom = Top; Top = tmp;
236 }
237
238 pdcattr = dc->pdcattr;
239
240 if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
241 DC_vUpdateFillBrush(dc);
242
243 if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
244 DC_vUpdateLineBrush(dc);
245
246 pbrush = PEN_ShareLockPen(pdcattr->hpen);
247 if (!pbrush)
248 {
249 DPRINT1("Ellipse Fail 1\n");
250 DC_UnlockDc(dc);
251 EngSetLastError(ERROR_INTERNAL_ERROR);
252 return FALSE;
253 }
254
255 PenOrigWidth = PenWidth = pbrush->lWidth;
256 if (pbrush->ulPenStyle == PS_NULL) PenWidth = 0;
257
258 if (pbrush->ulPenStyle == PS_INSIDEFRAME)
259 {
260 if (2*PenWidth > (Right - Left)) PenWidth = (Right -Left + 1)/2;
261 if (2*PenWidth > (Bottom - Top)) PenWidth = (Bottom -Top + 1)/2;
262 Left += PenWidth / 2;
263 Right -= (PenWidth - 1) / 2;
264 Top += PenWidth / 2;
265 Bottom -= (PenWidth - 1) / 2;
266 }
267
268 if (!PenWidth) PenWidth = 1;
269 pbrush->lWidth = PenWidth;
270
271 RectBounds.left = Left;
272 RectBounds.right = Right;
273 RectBounds.top = Top;
274 RectBounds.bottom = Bottom;
275
276 IntLPtoDP(dc, (LPPOINT)&RectBounds, 2);
277
278 RectBounds.left += dc->ptlDCOrig.x;
279 RectBounds.right += dc->ptlDCOrig.x;
280 RectBounds.top += dc->ptlDCOrig.y;
281 RectBounds.bottom += dc->ptlDCOrig.y;
282
283 // Setup for dynamic width and height.
284 RadiusX = max((RectBounds.right - RectBounds.left) / 2, 2); // Needs room
285 RadiusY = max((RectBounds.bottom - RectBounds.top) / 2, 2);
286 CenterX = (RectBounds.right + RectBounds.left) / 2;
287 CenterY = (RectBounds.bottom + RectBounds.top) / 2;
288
289 DPRINT("Ellipse 1: Left: %d, Top: %d, Right: %d, Bottom: %d\n",
290 RectBounds.left,RectBounds.top,RectBounds.right,RectBounds.bottom);
291
292 DPRINT("Ellipse 2: XLeft: %d, YLeft: %d, Width: %d, Height: %d\n",
293 CenterX - RadiusX, CenterY + RadiusY, RadiusX*2, RadiusY*2);
294
295 pFillBrushObj = BRUSH_ShareLockBrush(pdcattr->hbrush);
296 if (NULL == pFillBrushObj)
297 {
298 DPRINT1("FillEllipse Fail\n");
299 EngSetLastError(ERROR_INTERNAL_ERROR);
300 ret = FALSE;
301 }
302 else
303 {
304 RtlCopyMemory(&tmpFillBrushObj, pFillBrushObj, sizeof(tmpFillBrushObj));
305 //tmpFillBrushObj.ptOrigin.x += RectBounds.left - Left;
306 //tmpFillBrushObj.ptOrigin.y += RectBounds.top - Top;
307 tmpFillBrushObj.ptOrigin.x += dc->ptlDCOrig.x;
308 tmpFillBrushObj.ptOrigin.y += dc->ptlDCOrig.y;
309
310 DC_vPrepareDCsForBlit(dc, &RectBounds, NULL, NULL);
311
312 ret = IntFillEllipse( dc,
313 CenterX - RadiusX,
314 CenterY - RadiusY,
315 RadiusX*2, // Width
316 RadiusY*2, // Height
317 &tmpFillBrushObj);
318 BRUSH_ShareUnlockBrush(pFillBrushObj);
319
320 if (ret)
321 {
322 ret = IntDrawEllipse( dc,
323 CenterX - RadiusX,
324 CenterY - RadiusY,
325 RadiusX*2, // Width
326 RadiusY*2, // Height
327 pbrush);
328 }
329
330 DC_vFinishBlit(dc, NULL);
331 }
332
333 pbrush->lWidth = PenOrigWidth;
334 PEN_ShareUnlockPen(pbrush);
335 DC_UnlockDc(dc);
336 DPRINT("Ellipse Exit.\n");
337 return ret;
338 }
339
340 #if 0
341
342 // When the fill mode is ALTERNATE, GDI fills the area between odd-numbered and
343 // even-numbered polygon sides on each scan line. That is, GDI fills the area between the
344 // first and second side, between the third and fourth side, and so on.
345
346 // WINDING Selects winding mode (fills any region with a nonzero winding value).
347 // When the fill mode is WINDING, GDI fills any region that has a nonzero winding value.
348 // This value is defined as the number of times a pen used to draw the polygon would go around the region.
349 // The direction of each edge of the polygon is important.
350
351 extern BOOL FillPolygon(PDC dc,
352 SURFOBJ *SurfObj,
353 PBRUSHOBJ BrushObj,
354 MIX RopMode,
355 CONST PPOINT Points,
356 int Count,
357 RECTL BoundRect);
358
359 #endif
360
361
362 ULONG_PTR
363 APIENTRY
364 NtGdiPolyPolyDraw( IN HDC hDC,
365 IN PPOINT UnsafePoints,
366 IN PULONG UnsafeCounts,
367 IN ULONG Count,
368 IN INT iFunc )
369 {
370 DC *dc;
371 PVOID pTemp;
372 LPPOINT SafePoints;
373 PULONG SafeCounts;
374 NTSTATUS Status = STATUS_SUCCESS;
375 BOOL Ret = TRUE;
376 ULONG nPoints = 0, nMaxPoints = 0, nInvalid = 0, i;
377
378 if (!UnsafePoints || !UnsafeCounts ||
379 Count == 0 || iFunc == 0 || iFunc > GdiPolyPolyRgn)
380 {
381 /* Windows doesn't set last error */
382 return FALSE;
383 }
384
385 _SEH2_TRY
386 {
387 ProbeForRead(UnsafePoints, Count * sizeof(POINT), 1);
388 ProbeForRead(UnsafeCounts, Count * sizeof(ULONG), 1);
389
390 /* Count points and validate poligons */
391 for (i = 0; i < Count; i++)
392 {
393 if (UnsafeCounts[i] < 2)
394 {
395 nInvalid++;
396 }
397 nPoints += UnsafeCounts[i];
398 nMaxPoints = max(nMaxPoints, UnsafeCounts[i]);
399 }
400 }
401 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
402 {
403 Status = _SEH2_GetExceptionCode();
404 }
405 _SEH2_END;
406
407 if (!NT_SUCCESS(Status))
408 {
409 /* Windows doesn't set last error */
410 return FALSE;
411 }
412
413 if (nPoints == 0 || nPoints < nMaxPoints)
414 {
415 /* If all polygon counts are zero, or we have overflow,
416 return without setting a last error code. */
417 return FALSE;
418 }
419
420 if (nInvalid != 0)
421 {
422 /* If at least one poly count is 0 or 1, fail */
423 EngSetLastError(ERROR_INVALID_PARAMETER);
424 return FALSE;
425 }
426
427 /* Allocate one buffer for both counts and points */
428 pTemp = ExAllocatePoolWithTag(PagedPool,
429 Count * sizeof(ULONG) + nPoints * sizeof(POINT),
430 TAG_SHAPE);
431 if (!pTemp)
432 {
433 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
434 return FALSE;
435 }
436
437 SafeCounts = pTemp;
438 SafePoints = (PVOID)(SafeCounts + Count);
439
440 _SEH2_TRY
441 {
442 /* Pointers already probed! */
443 RtlCopyMemory(SafeCounts, UnsafeCounts, Count * sizeof(ULONG));
444 RtlCopyMemory(SafePoints, UnsafePoints, nPoints * sizeof(POINT));
445 }
446 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
447 {
448 Status = _SEH2_GetExceptionCode();
449 }
450 _SEH2_END;
451
452 if (!NT_SUCCESS(Status))
453 {
454 ExFreePoolWithTag(pTemp, TAG_SHAPE);
455 return FALSE;
456 }
457
458 /* Special handling for GdiPolyPolyRgn */
459 if (iFunc == GdiPolyPolyRgn)
460 {
461 INT iMode = (INT)(UINT_PTR)hDC;
462 HRGN hrgn;
463
464 hrgn = GreCreatePolyPolygonRgn(SafePoints, SafeCounts, Count, iMode);
465
466 ExFreePoolWithTag(pTemp, TAG_SHAPE);
467 return (ULONG_PTR)hrgn;
468 }
469
470 dc = DC_LockDc(hDC);
471 if (!dc)
472 {
473 EngSetLastError(ERROR_INVALID_HANDLE);
474 ExFreePoolWithTag(pTemp, TAG_SHAPE);
475 return FALSE;
476 }
477
478 if (dc->dctype == DC_TYPE_INFO)
479 {
480 DC_UnlockDc(dc);
481 ExFreePoolWithTag(pTemp, TAG_SHAPE);
482 /* Yes, Windows really returns TRUE in this case */
483 return TRUE;
484 }
485
486 DC_vPrepareDCsForBlit(dc, NULL, NULL, NULL);
487
488 if (dc->pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
489 DC_vUpdateFillBrush(dc);
490
491 if (dc->pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
492 DC_vUpdateLineBrush(dc);
493
494 /* Perform the actual work */
495 switch (iFunc)
496 {
497 case GdiPolyPolygon:
498 Ret = IntGdiPolyPolygon(dc, SafePoints, SafeCounts, Count);
499 break;
500 case GdiPolyPolyLine:
501 Ret = IntGdiPolyPolyline(dc, SafePoints, SafeCounts, Count);
502 break;
503 case GdiPolyBezier:
504 Ret = IntGdiPolyBezier(dc, SafePoints, *SafeCounts);
505 break;
506 case GdiPolyLineTo:
507 Ret = IntGdiPolylineTo(dc, SafePoints, *SafeCounts);
508 break;
509 case GdiPolyBezierTo:
510 Ret = IntGdiPolyBezierTo(dc, SafePoints, *SafeCounts);
511 break;
512 default:
513 EngSetLastError(ERROR_INVALID_PARAMETER);
514 Ret = FALSE;
515 }
516
517 /* Cleanup and return */
518 DC_vFinishBlit(dc, NULL);
519 DC_UnlockDc(dc);
520 ExFreePoolWithTag(pTemp, TAG_SHAPE);
521
522 return (ULONG_PTR)Ret;
523 }
524
525
526 BOOL
527 FASTCALL
528 IntRectangle(PDC dc,
529 int LeftRect,
530 int TopRect,
531 int RightRect,
532 int BottomRect)
533 {
534 SURFACE *psurf = NULL;
535 PBRUSH pbrLine, pbrFill;
536 BOOL ret = FALSE; // Default to failure
537 RECTL DestRect;
538 MIX Mix;
539 PDC_ATTR pdcattr;
540 POINTL BrushOrigin;
541
542 ASSERT ( dc ); // Caller's responsibility to set this up
543
544 pdcattr = dc->pdcattr;
545
546 // Rectangle Path only.
547 if ( PATH_IsPathOpen(dc->dclevel) )
548 {
549 return PATH_Rectangle ( dc, LeftRect, TopRect, RightRect, BottomRect );
550 }
551
552 /* Make sure rectangle is not inverted */
553 DestRect.left = min(LeftRect, RightRect);
554 DestRect.right = max(LeftRect, RightRect);
555 DestRect.top = min(TopRect, BottomRect);
556 DestRect.bottom = max(TopRect, BottomRect);
557
558 IntLPtoDP(dc, (LPPOINT)&DestRect, 2);
559
560 DestRect.left += dc->ptlDCOrig.x;
561 DestRect.right += dc->ptlDCOrig.x;
562 DestRect.top += dc->ptlDCOrig.y;
563 DestRect.bottom += dc->ptlDCOrig.y;
564
565 /* In GM_COMPATIBLE, don't include bottom and right edges */
566 if (pdcattr->iGraphicsMode == GM_COMPATIBLE)
567 {
568 DestRect.right--;
569 DestRect.bottom--;
570 }
571
572 DC_vPrepareDCsForBlit(dc, &DestRect, NULL, NULL);
573
574 if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
575 DC_vUpdateFillBrush(dc);
576
577 if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
578 DC_vUpdateLineBrush(dc);
579
580 pbrFill = dc->dclevel.pbrFill;
581 pbrLine = dc->dclevel.pbrLine;
582 if (!pbrLine)
583 {
584 ret = FALSE;
585 goto cleanup;
586 }
587
588 psurf = dc->dclevel.pSurface;
589 if (!psurf)
590 {
591 ret = FALSE;
592 goto cleanup;
593 }
594
595 if (pbrFill)
596 {
597 if (!(pbrFill->flAttrs & BR_IS_NULL))
598 {
599 BrushOrigin = *((PPOINTL)&pbrFill->ptOrigin);
600 BrushOrigin.x += dc->ptlDCOrig.x;
601 BrushOrigin.y += dc->ptlDCOrig.y;
602 ret = IntEngBitBlt(&psurf->SurfObj,
603 NULL,
604 NULL,
605 &dc->co.ClipObj,
606 NULL,
607 &DestRect,
608 NULL,
609 NULL,
610 &dc->eboFill.BrushObject,
611 &BrushOrigin,
612 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY));
613 }
614 }
615
616 // Draw the rectangle with the current pen
617
618 ret = TRUE; // Change default to success
619
620 if (!(pbrLine->flAttrs & BR_IS_NULL))
621 {
622 Mix = ROP2_TO_MIX(pdcattr->jROP2);
623 ret = ret && IntEngLineTo(&psurf->SurfObj,
624 &dc->co.ClipObj,
625 &dc->eboLine.BrushObject,
626 DestRect.left, DestRect.top, DestRect.right, DestRect.top,
627 &DestRect, // Bounding rectangle
628 Mix);
629
630 ret = ret && IntEngLineTo(&psurf->SurfObj,
631 &dc->co.ClipObj,
632 &dc->eboLine.BrushObject,
633 DestRect.right, DestRect.top, DestRect.right, DestRect.bottom,
634 &DestRect, // Bounding rectangle
635 Mix);
636
637 ret = ret && IntEngLineTo(&psurf->SurfObj,
638 &dc->co.ClipObj,
639 &dc->eboLine.BrushObject,
640 DestRect.right, DestRect.bottom, DestRect.left, DestRect.bottom,
641 &DestRect, // Bounding rectangle
642 Mix);
643
644 ret = ret && IntEngLineTo(&psurf->SurfObj,
645 &dc->co.ClipObj,
646 &dc->eboLine.BrushObject,
647 DestRect.left, DestRect.bottom, DestRect.left, DestRect.top,
648 &DestRect, // Bounding rectangle
649 Mix);
650 }
651
652 cleanup:
653 DC_vFinishBlit(dc, NULL);
654
655 /* Move current position in DC?
656 MSDN: The current position is neither used nor updated by Rectangle. */
657
658 return ret;
659 }
660
661 BOOL
662 APIENTRY
663 NtGdiRectangle(HDC hDC,
664 int LeftRect,
665 int TopRect,
666 int RightRect,
667 int BottomRect)
668 {
669 DC *dc;
670 BOOL ret; // Default to failure
671
672 dc = DC_LockDc(hDC);
673 if (!dc)
674 {
675 EngSetLastError(ERROR_INVALID_HANDLE);
676 return FALSE;
677 }
678 if (dc->dctype == DC_TYPE_INFO)
679 {
680 DC_UnlockDc(dc);
681 /* Yes, Windows really returns TRUE in this case */
682 return TRUE;
683 }
684
685 /* Do we rotate or shear? */
686 if (!(dc->pdcattr->mxWorldToDevice.flAccel & XFORM_SCALE))
687 {
688 POINTL DestCoords[4];
689 ULONG PolyCounts = 4;
690
691 DestCoords[0].x = DestCoords[3].x = LeftRect;
692 DestCoords[0].y = DestCoords[1].y = TopRect;
693 DestCoords[1].x = DestCoords[2].x = RightRect;
694 DestCoords[2].y = DestCoords[3].y = BottomRect;
695 // Use IntGdiPolyPolygon so to support PATH.
696 ret = IntGdiPolyPolygon(dc, DestCoords, &PolyCounts, 1);
697 }
698 else
699 {
700 ret = IntRectangle(dc, LeftRect, TopRect, RightRect, BottomRect );
701 }
702
703 DC_UnlockDc(dc);
704
705 return ret;
706 }
707
708
709 BOOL
710 FASTCALL
711 IntRoundRect(
712 PDC dc,
713 int Left,
714 int Top,
715 int Right,
716 int Bottom,
717 int xCurveDiameter,
718 int yCurveDiameter)
719 {
720 PDC_ATTR pdcattr;
721 PBRUSH pbrLine, pbrFill;
722 RECTL RectBounds;
723 LONG PenWidth, PenOrigWidth;
724 BOOL ret = TRUE; // Default to success
725 BRUSH brushTemp;
726
727 ASSERT ( dc ); // Caller's responsibility to set this up
728
729 if ( PATH_IsPathOpen(dc->dclevel) )
730 return PATH_RoundRect ( dc, Left, Top, Right, Bottom,
731 xCurveDiameter, yCurveDiameter );
732
733 if ((Left == Right) || (Top == Bottom)) return TRUE;
734
735 xCurveDiameter = max(abs( xCurveDiameter ), 1);
736 yCurveDiameter = max(abs( yCurveDiameter ), 1);
737
738 if (Right < Left)
739 {
740 INT tmp = Right; Right = Left; Left = tmp;
741 }
742 if (Bottom < Top)
743 {
744 INT tmp = Bottom; Bottom = Top; Top = tmp;
745 }
746
747 pdcattr = dc->pdcattr;
748
749 if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
750 DC_vUpdateFillBrush(dc);
751
752 if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
753 DC_vUpdateLineBrush(dc);
754
755 pbrLine = PEN_ShareLockPen(pdcattr->hpen);
756 if (!pbrLine)
757 {
758 /* Nothing to do, as we don't have a bitmap */
759 EngSetLastError(ERROR_INTERNAL_ERROR);
760 return FALSE;
761 }
762
763 PenOrigWidth = PenWidth = pbrLine->lWidth;
764 if (pbrLine->ulPenStyle == PS_NULL) PenWidth = 0;
765
766 if (pbrLine->ulPenStyle == PS_INSIDEFRAME)
767 {
768 if (2*PenWidth > (Right - Left)) PenWidth = (Right -Left + 1)/2;
769 if (2*PenWidth > (Bottom - Top)) PenWidth = (Bottom -Top + 1)/2;
770 Left += PenWidth / 2;
771 Right -= (PenWidth - 1) / 2;
772 Top += PenWidth / 2;
773 Bottom -= (PenWidth - 1) / 2;
774 }
775
776 if (!PenWidth) PenWidth = 1;
777 pbrLine->lWidth = PenWidth;
778
779 RectBounds.left = Left;
780 RectBounds.top = Top;
781 RectBounds.right = Right;
782 RectBounds.bottom = Bottom;
783
784 IntLPtoDP(dc, (LPPOINT)&RectBounds, 2);
785
786 RectBounds.left += dc->ptlDCOrig.x;
787 RectBounds.top += dc->ptlDCOrig.y;
788 RectBounds.right += dc->ptlDCOrig.x;
789 RectBounds.bottom += dc->ptlDCOrig.y;
790
791 pbrFill = BRUSH_ShareLockBrush(pdcattr->hbrush);
792 if (!pbrFill)
793 {
794 DPRINT1("FillRound Fail\n");
795 EngSetLastError(ERROR_INTERNAL_ERROR);
796 ret = FALSE;
797 }
798 else
799 {
800
801 DC_vPrepareDCsForBlit(dc, &RectBounds, NULL, NULL);
802
803 RtlCopyMemory(&brushTemp, pbrFill, sizeof(brushTemp));
804 brushTemp.ptOrigin.x += RectBounds.left - Left;
805 brushTemp.ptOrigin.y += RectBounds.top - Top;
806 ret = IntFillRoundRect( dc,
807 RectBounds.left,
808 RectBounds.top,
809 RectBounds.right,
810 RectBounds.bottom,
811 xCurveDiameter,
812 yCurveDiameter,
813 &brushTemp);
814 BRUSH_ShareUnlockBrush(pbrFill);
815
816 if (ret)
817 {
818 ret = IntDrawRoundRect( dc,
819 RectBounds.left,
820 RectBounds.top,
821 RectBounds.right,
822 RectBounds.bottom,
823 xCurveDiameter,
824 yCurveDiameter,
825 pbrLine);
826 }
827
828 DC_vFinishBlit(dc, NULL);
829 }
830
831
832 pbrLine->lWidth = PenOrigWidth;
833 PEN_ShareUnlockPen(pbrLine);
834 return ret;
835 }
836
837 BOOL
838 APIENTRY
839 NtGdiRoundRect(
840 HDC hDC,
841 int LeftRect,
842 int TopRect,
843 int RightRect,
844 int BottomRect,
845 int Width,
846 int Height)
847 {
848 DC *dc = DC_LockDc(hDC);
849 BOOL ret = FALSE; /* Default to failure */
850
851 DPRINT("NtGdiRoundRect(0x%p,%i,%i,%i,%i,%i,%i)\n",hDC,LeftRect,TopRect,RightRect,BottomRect,Width,Height);
852 if ( !dc )
853 {
854 DPRINT1("NtGdiRoundRect() - hDC is invalid\n");
855 EngSetLastError(ERROR_INVALID_HANDLE);
856 }
857 else if (dc->dctype == DC_TYPE_INFO)
858 {
859 DC_UnlockDc(dc);
860 /* Yes, Windows really returns TRUE in this case */
861 ret = TRUE;
862 }
863 else
864 {
865 ret = IntRoundRect ( dc, LeftRect, TopRect, RightRect, BottomRect, Width, Height );
866 DC_UnlockDc ( dc );
867 }
868
869 return ret;
870 }
871
872 BOOL
873 NTAPI
874 GreGradientFill(
875 HDC hdc,
876 PTRIVERTEX pVertex,
877 ULONG nVertex,
878 PVOID pMesh,
879 ULONG nMesh,
880 ULONG ulMode)
881 {
882 PDC pdc;
883 SURFACE *psurf;
884 EXLATEOBJ exlo;
885 RECTL rclExtent;
886 POINTL ptlDitherOrg;
887 ULONG i;
888 BOOL bRet;
889
890 /* Check parameters */
891 if (ulMode & GRADIENT_FILL_TRIANGLE)
892 {
893 PGRADIENT_TRIANGLE pTriangle = (PGRADIENT_TRIANGLE)pMesh;
894
895 for (i = 0; i < nMesh; i++, pTriangle++)
896 {
897 if (pTriangle->Vertex1 >= nVertex ||
898 pTriangle->Vertex2 >= nVertex ||
899 pTriangle->Vertex3 >= nVertex)
900 {
901 EngSetLastError(ERROR_INVALID_PARAMETER);
902 return FALSE;
903 }
904 }
905 }
906 else
907 {
908 PGRADIENT_RECT pRect = (PGRADIENT_RECT)pMesh;
909 for (i = 0; i < nMesh; i++, pRect++)
910 {
911 if (pRect->UpperLeft >= nVertex || pRect->LowerRight >= nVertex)
912 {
913 EngSetLastError(ERROR_INVALID_PARAMETER);
914 return FALSE;
915 }
916 }
917 }
918
919 /* Lock the output DC */
920 pdc = DC_LockDc(hdc);
921 if(!pdc)
922 {
923 EngSetLastError(ERROR_INVALID_HANDLE);
924 return FALSE;
925 }
926
927 if(pdc->dctype == DC_TYPE_INFO)
928 {
929 DC_UnlockDc(pdc);
930 /* Yes, Windows really returns TRUE in this case */
931 return TRUE;
932 }
933
934 if (!pdc->dclevel.pSurface)
935 {
936 /* Memory DC with no surface selected */
937 DC_UnlockDc(pdc);
938 return TRUE; // CHECKME
939 }
940
941 /* Calculate extent */
942 rclExtent.left = rclExtent.right = pVertex->x;
943 rclExtent.top = rclExtent.bottom = pVertex->y;
944 for (i = 0; i < nVertex; i++)
945 {
946 rclExtent.left = min(rclExtent.left, (pVertex + i)->x);
947 rclExtent.right = max(rclExtent.right, (pVertex + i)->x);
948 rclExtent.top = min(rclExtent.top, (pVertex + i)->y);
949 rclExtent.bottom = max(rclExtent.bottom, (pVertex + i)->y);
950 }
951 IntLPtoDP(pdc, (LPPOINT)&rclExtent, 2);
952
953 rclExtent.left += pdc->ptlDCOrig.x;
954 rclExtent.right += pdc->ptlDCOrig.x;
955 rclExtent.top += pdc->ptlDCOrig.y;
956 rclExtent.bottom += pdc->ptlDCOrig.y;
957
958 ptlDitherOrg.x = ptlDitherOrg.y = 0;
959 IntLPtoDP(pdc, (LPPOINT)&ptlDitherOrg, 1);
960
961 ptlDitherOrg.x += pdc->ptlDCOrig.x;
962 ptlDitherOrg.y += pdc->ptlDCOrig.y;
963
964 DC_vPrepareDCsForBlit(pdc, &rclExtent, NULL, NULL);
965
966 psurf = pdc->dclevel.pSurface;
967
968 EXLATEOBJ_vInitialize(&exlo, &gpalRGB, psurf->ppal, 0, 0, 0);
969
970 bRet = IntEngGradientFill(&psurf->SurfObj,
971 &pdc->co.ClipObj,
972 &exlo.xlo,
973 pVertex,
974 nVertex,
975 pMesh,
976 nMesh,
977 &rclExtent,
978 &ptlDitherOrg,
979 ulMode);
980
981 EXLATEOBJ_vCleanup(&exlo);
982 DC_vFinishBlit(pdc, NULL);
983 DC_UnlockDc(pdc);
984
985 return bRet;
986 }
987
988 BOOL
989 APIENTRY
990 NtGdiGradientFill(
991 HDC hdc,
992 PTRIVERTEX pVertex,
993 ULONG nVertex,
994 PVOID pMesh,
995 ULONG nMesh,
996 ULONG ulMode)
997 {
998 BOOL bRet;
999 PTRIVERTEX SafeVertex;
1000 PVOID SafeMesh;
1001 ULONG cbVertex, cbMesh;
1002
1003 /* Validate parameters */
1004 if (!pVertex || !nVertex || !pMesh || !nMesh)
1005 {
1006 EngSetLastError(ERROR_INVALID_PARAMETER);
1007 return FALSE;
1008 }
1009
1010 switch (ulMode)
1011 {
1012 case GRADIENT_FILL_RECT_H:
1013 case GRADIENT_FILL_RECT_V:
1014 cbMesh = nMesh * sizeof(GRADIENT_RECT);
1015 break;
1016 case GRADIENT_FILL_TRIANGLE:
1017 cbMesh = nMesh * sizeof(GRADIENT_TRIANGLE);
1018 break;
1019 default:
1020 EngSetLastError(ERROR_INVALID_PARAMETER);
1021 return FALSE;
1022 }
1023
1024 cbVertex = nVertex * sizeof(TRIVERTEX) ;
1025 if(cbVertex + cbMesh <= cbVertex)
1026 {
1027 /* Overflow */
1028 return FALSE ;
1029 }
1030
1031 /* Allocate a kernel mode buffer */
1032 SafeVertex = ExAllocatePoolWithTag(PagedPool, cbVertex + cbMesh, TAG_SHAPE);
1033 if(!SafeVertex)
1034 {
1035 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
1036 return FALSE;
1037 }
1038
1039 SafeMesh = (PVOID)((ULONG_PTR)SafeVertex + cbVertex);
1040
1041 /* Copy the parameters to kernel mode */
1042 _SEH2_TRY
1043 {
1044 ProbeForRead(pVertex, cbVertex, 1);
1045 ProbeForRead(pMesh, cbMesh, 1);
1046 RtlCopyMemory(SafeVertex, pVertex, cbVertex);
1047 RtlCopyMemory(SafeMesh, pMesh, cbMesh);
1048 }
1049 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1050 {
1051 ExFreePoolWithTag(SafeVertex, TAG_SHAPE);
1052 SetLastNtError(_SEH2_GetExceptionCode());
1053 _SEH2_YIELD(return FALSE;)
1054 }
1055 _SEH2_END;
1056
1057 /* Call the internal function */
1058 bRet = GreGradientFill(hdc, SafeVertex, nVertex, SafeMesh, nMesh, ulMode);
1059
1060 /* Cleanup and return result */
1061 ExFreePoolWithTag(SafeVertex, TAG_SHAPE);
1062 return bRet;
1063 }
1064
1065 BOOL APIENTRY
1066 NtGdiExtFloodFill(
1067 HDC hDC,
1068 INT XStart,
1069 INT YStart,
1070 COLORREF Color,
1071 UINT FillType)
1072 {
1073 PDC dc;
1074 #if 0
1075 PDC_ATTR pdcattr;
1076 #endif
1077 SURFACE *psurf;
1078 EXLATEOBJ exlo;
1079 BOOL Ret = FALSE;
1080 RECTL DestRect;
1081 POINTL Pt;
1082 ULONG ConvColor;
1083
1084 dc = DC_LockDc(hDC);
1085 if (!dc)
1086 {
1087 EngSetLastError(ERROR_INVALID_HANDLE);
1088 return FALSE;
1089 }
1090 if (dc->dctype == DC_TYPE_INFO)
1091 {
1092 DC_UnlockDc(dc);
1093 /* Yes, Windows really returns TRUE in this case */
1094 return TRUE;
1095 }
1096
1097 if (!dc->dclevel.pSurface)
1098 {
1099 Ret = FALSE;
1100 goto cleanup;
1101 }
1102
1103 #if 0
1104 pdcattr = dc->pdcattr;
1105 #endif
1106
1107 Pt.x = XStart;
1108 Pt.y = YStart;
1109 IntLPtoDP(dc, (LPPOINT)&Pt, 1);
1110
1111 DC_vPrepareDCsForBlit(dc, &DestRect, NULL, NULL);
1112
1113 /// FIXME: what about prgnVIS? And what about REAL clipping?
1114 psurf = dc->dclevel.pSurface;
1115 if (dc->prgnRao)
1116 {
1117 Ret = REGION_PtInRegion(dc->prgnRao, Pt.x, Pt.y);
1118 if (Ret)
1119 REGION_GetRgnBox(dc->prgnRao, (LPRECT)&DestRect);
1120 else
1121 {
1122 DC_vFinishBlit(dc, NULL);
1123 goto cleanup;
1124 }
1125 }
1126 else
1127 {
1128 RECTL_vSetRect(&DestRect, 0, 0, psurf->SurfObj.sizlBitmap.cx, psurf->SurfObj.sizlBitmap.cy);
1129 }
1130
1131 EXLATEOBJ_vInitialize(&exlo, &gpalRGB, psurf->ppal, 0, 0xffffff, 0);
1132
1133 /* Only solid fills supported for now
1134 * How to support pattern brushes and non standard surfaces (not offering dib functions):
1135 * Version a (most likely slow): call DrvPatBlt for every pixel
1136 * Version b: create a flood mask and let MaskBlt blit a masked brush */
1137 ConvColor = XLATEOBJ_iXlate(&exlo.xlo, Color);
1138 Ret = DIB_XXBPP_FloodFillSolid(&psurf->SurfObj, &dc->eboFill.BrushObject, &DestRect, &Pt, ConvColor, FillType);
1139
1140 DC_vFinishBlit(dc, NULL);
1141
1142 EXLATEOBJ_vCleanup(&exlo);
1143
1144 cleanup:
1145 DC_UnlockDc(dc);
1146 return Ret;
1147 }
1148
1149 /* EOF */