[Win32k]
[reactos.git] / reactos / subsystems / win32 / win32k / objects / region.c
index c7c6c4a..d0bd3ba 100644 (file)
@@ -2073,14 +2073,12 @@ FASTCALL
 REGION_AllocUserRgnWithHandle(INT nRgn)
 {
     PROSRGNDATA pRgn;
-    INT Index;
     PGDI_TABLE_ENTRY Entry;
 
     pRgn = REGION_AllocRgnWithHandle(nRgn);
     if (pRgn)
     {
-       Index = GDI_HANDLE_GET_INDEX(pRgn->BaseObject.hHmgr);
-       Entry = &GdiHandleTable->Entries[Index];
+       Entry = GDI_HANDLE_GET_ENTRY(GdiHandleTable, pRgn->BaseObject.hHmgr);
        Entry->UserData = AllocateObjectAttr();
     }
     return pRgn;
@@ -2090,23 +2088,18 @@ PROSRGNDATA
 FASTCALL
 RGNOBJAPI_Lock(HRGN hRgn, PRGN_ATTR *ppRgn_Attr)
 {
-  INT Index;
   PGDI_TABLE_ENTRY Entry;
-  PROSRGNDATA pRgn;
   PRGN_ATTR pRgn_Attr;
-  HANDLE pid;
+  PROSRGNDATA pRgn = NULL;
 
   pRgn = REGION_LockRgn(hRgn);
 
-  if (pRgn)
+  if (pRgn && GDIOBJ_OwnedByCurrentProcess(hRgn))
   {
-     Index = GDI_HANDLE_GET_INDEX(hRgn);
-     Entry = &GdiHandleTable->Entries[Index];
+     Entry = GDI_HANDLE_GET_ENTRY(GdiHandleTable, hRgn);
      pRgn_Attr = Entry->UserData;
-     pid = (HANDLE)((ULONG_PTR)Entry->ProcessId & ~0x1);
 
-     if ( pid == NtCurrentTeb()->ClientId.UniqueProcess &&
-          pRgn_Attr )
+     if ( pRgn_Attr )
      {
         _SEH2_TRY
         {
@@ -2151,20 +2144,15 @@ VOID
 FASTCALL
 RGNOBJAPI_Unlock(PROSRGNDATA pRgn)
 {
-  INT Index;
   PGDI_TABLE_ENTRY Entry;
   PRGN_ATTR pRgn_Attr;
-  HANDLE pid;
 
-  if (pRgn)
+  if (pRgn && GDIOBJ_OwnedByCurrentProcess(pRgn->BaseObject.hHmgr))
   {
-     Index = GDI_HANDLE_GET_INDEX(pRgn->BaseObject.hHmgr);
-     Entry = &GdiHandleTable->Entries[Index];
+     Entry = GDI_HANDLE_GET_ENTRY(GdiHandleTable, pRgn->BaseObject.hHmgr);
      pRgn_Attr = Entry->UserData;
-     pid = (HANDLE)((ULONG_PTR)Entry->ProcessId & ~0x1);
 
-     if ( pid == NtCurrentTeb()->ClientId.UniqueProcess &&
-          pRgn_Attr )
+     if ( pRgn_Attr )
      {
         _SEH2_TRY
         {
@@ -2336,20 +2324,20 @@ IntGdiCombineRgn(PROSRGNDATA destRgn,
            else if (src2Rgn == NULL)
            {
               DPRINT1("IntGdiCombineRgn requires hSrc2 != NULL for combine mode %d!\n", CombineMode);
-              SetLastWin32Error(ERROR_INVALID_HANDLE);
+              EngSetLastError(ERROR_INVALID_HANDLE);
            }
         }
      }
      else
      {
         DPRINT("IntGdiCombineRgn: hSrc1 unavailable\n");
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_HANDLE);
      }
   }
   else
   {
      DPRINT("IntGdiCombineRgn: hDest unavailable\n");
-     SetLastWin32Error(ERROR_INVALID_HANDLE);
+     EngSetLastError(ERROR_INVALID_HANDLE);
   }
   return result;
 }
@@ -2451,7 +2439,7 @@ IntGdiPaintRgn(
     REGION_FreeRgnByHandle(tmpVisRgn);
 
     // Fill the region
-    return TRUE;
+    return bRet;
 }
 
 BOOL
@@ -3216,6 +3204,27 @@ IntCreatePolyPolygonRgn(
     return hrgn;
 }
 
+BOOL
+FASTCALL
+IntRectInRegion(
+    HRGN  hRgn,
+    LPRECTL rc
+)
+{
+    PROSRGNDATA Rgn;
+    BOOL Ret;
+
+    if (!(Rgn = RGNOBJAPI_Lock(hRgn, NULL)))
+    {
+        return ERROR;
+    }
+
+    Ret = REGION_RectInRegion(Rgn, rc);
+    RGNOBJAPI_Unlock(Rgn);
+    return Ret;
+}
+
+
 //
 // NtGdi Exported Functions
 //
@@ -3231,14 +3240,14 @@ NtGdiCombineRgn(HRGN  hDest,
 
   if ( CombineMode > RGN_COPY && CombineMode < RGN_AND)
   {
-     SetLastWin32Error(ERROR_INVALID_PARAMETER);
+     EngSetLastError(ERROR_INVALID_PARAMETER);
      return ERROR;
   }
 
   destRgn = RGNOBJAPI_Lock(hDest, NULL);
   if (!destRgn)
   {
-     SetLastWin32Error(ERROR_INVALID_HANDLE);
+     EngSetLastError(ERROR_INVALID_HANDLE);
      return ERROR;
   }
 
@@ -3246,7 +3255,7 @@ NtGdiCombineRgn(HRGN  hDest,
   if (!src1Rgn)
   {
      RGNOBJAPI_Unlock(destRgn);
-     SetLastWin32Error(ERROR_INVALID_HANDLE);
+     EngSetLastError(ERROR_INVALID_HANDLE);
      return ERROR;
   }
 
@@ -3285,7 +3294,7 @@ NtGdiCreateRectRgn(INT LeftRect, INT TopRect, INT RightRect, INT BottomRect)
     /* Allocate region data structure with space for 1 RECTL */
     if (!(pRgn = REGION_AllocUserRgnWithHandle(1)))
     {
-        SetLastWin32Error(ERROR_NOT_ENOUGH_MEMORY);
+        EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
         return NULL;
     }
     hRgn = pRgn->BaseObject.hHmgr;
@@ -3486,6 +3495,7 @@ NtGdiExtCreateRegion(
     DWORD dwSize = 0;
     NTSTATUS Status = STATUS_SUCCESS;
     MATRIX matrix;
+    XFORMOBJ xo;
 
     DPRINT("NtGdiExtCreateRegion\n");
     _SEH2_TRY
@@ -3518,7 +3528,7 @@ NtGdiExtCreateRegion(
 
     if (Region == NULL)
     {
-        SetLastWin32Error(ERROR_NOT_ENOUGH_MEMORY);
+        EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
         return FALSE;
     }
     hRgn = Region->BaseObject.hHmgr;
@@ -3531,13 +3541,14 @@ NtGdiExtCreateRegion(
 
             /* Init the XFORMOBJ from the Xform struct */
             Status = STATUS_INVALID_PARAMETER;
-            ret = XFORMOBJ_iSetXform((XFORMOBJ*)&matrix, (XFORML*)Xform);
+            XFORMOBJ_vInit(&xo, &matrix);
+            ret = XFORMOBJ_iSetXform(&xo, (XFORML*)Xform);
 
             /* Check for error, also no scale and shear allowed */
             if (ret != DDI_ERROR && ret != GX_GENERAL)
             {
                 /* Apply the coordinate transformation on the rects */
-                if (XFORMOBJ_bApplyXform((XFORMOBJ*)&matrix,
+                if (XFORMOBJ_bApplyXform(&xo,
                                          XF_LTOL,
                                          nCount * 2,
                                          RgnData->Buffer,
@@ -3562,7 +3573,7 @@ NtGdiExtCreateRegion(
     _SEH2_END;
     if (!NT_SUCCESS(Status))
     {
-        SetLastWin32Error(ERROR_INVALID_PARAMETER);
+        EngSetLastError(ERROR_INVALID_PARAMETER);
         RGNOBJAPI_Unlock(Region);
         GreDeleteObject(hRgn);
         return NULL;
@@ -3637,83 +3648,6 @@ NtGdiFrameRgn(
 }
 
 
-/* See wine, msdn, osr and  Feng Yuan - Windows Graphics Programming Win32 Gdi And Directdraw
-
-   1st: http://www.codeproject.com/gdi/cliprgnguide.asp is wrong!
-
-   The intersection of the clip with the meta region is not Rao it's API!
-   Go back and read 7.2 Clipping pages 418-19:
-   Rao = API & Vis:
-   1) The Rao region is the intersection of the API region and the system region,
-      named after the Microsoft engineer who initially proposed it.
-   2) The Rao region can be calculated from the API region and the system region.
-
-   API:
-      API region is the intersection of the meta region and the clipping region,
-      clearly named after the fact that it is controlled by GDI API calls.
-*/
-INT APIENTRY
-NtGdiGetRandomRgn(
-    HDC hDC,
-    HRGN hDest,
-    INT iCode
-)
-{
-    INT ret = 0;
-    PDC pDC;
-    HRGN hSrc = NULL;
-    POINT org;
-
-    pDC = DC_LockDc(hDC);
-    if (pDC == NULL)
-    {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
-        return -1;
-    }
-
-    switch (iCode)
-    {
-    case CLIPRGN:
-        hSrc = pDC->rosdc.hClipRgn;
-//        if (pDC->dclevel.prgnClip) hSrc = ((PROSRGNDATA)pDC->dclevel.prgnClip)->BaseObject.hHmgr;
-        break;
-    case METARGN:
-        if (pDC->dclevel.prgnMeta) hSrc = ((PROSRGNDATA)pDC->dclevel.prgnMeta)->BaseObject.hHmgr;
-        break;
-    case APIRGN:
-        if (pDC->prgnAPI) hSrc = ((PROSRGNDATA)pDC->prgnAPI)->BaseObject.hHmgr;
-//        else if (pDC->dclevel.prgnClip) hSrc = ((PROSRGNDATA)pDC->dclevel.prgnClip)->BaseObject.hHmgr;
-        else if (pDC->rosdc.hClipRgn) hSrc = pDC->rosdc.hClipRgn;
-        else if (pDC->dclevel.prgnMeta) hSrc = ((PROSRGNDATA)pDC->dclevel.prgnMeta)->BaseObject.hHmgr;
-        break;
-    case SYSRGN:
-        if (pDC->prgnVis) hSrc = ((PROSRGNDATA)pDC->prgnVis)->BaseObject.hHmgr;
-        break;
-    default:
-        hSrc = 0;
-    }
-    if (hSrc)
-    {
-        if (NtGdiCombineRgn(hDest, hSrc, 0, RGN_COPY) == ERROR)
-        {
-            ret = -1;
-        }
-        else
-        {
-            ret = 1;
-        }
-    }
-    if (iCode == SYSRGN)
-    {
-        IntGdiGetDCOrg(pDC, &org);
-        NtGdiOffsetRgn(hDest, org.x, org.y );
-    }
-
-    DC_UnlockDc(pDC);
-
-    return ret;
-}
-
 INT APIENTRY
 NtGdiGetRgnBox(
     HRGN hRgn,
@@ -3768,7 +3702,7 @@ NtGdiInvertRgn(
 
     if (!(RgnData = RGNOBJAPI_Lock(hRgn, NULL)))
     {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_HANDLE);
         return FALSE;
     }
 
@@ -3852,16 +3786,9 @@ NtGdiRectInRegion(
     LPRECTL unsaferc
 )
 {
-    PROSRGNDATA Rgn;
     RECTL rc = {0};
-    BOOL Ret;
     NTSTATUS Status = STATUS_SUCCESS;
 
-    if (!(Rgn = RGNOBJAPI_Lock(hRgn, NULL)))
-    {
-        return ERROR;
-    }
-
     _SEH2_TRY
     {
         ProbeForRead(unsaferc, sizeof(RECT), 1);
@@ -3875,15 +3802,12 @@ NtGdiRectInRegion(
 
     if (!NT_SUCCESS(Status))
     {
-        RGNOBJAPI_Unlock(Rgn);
         SetLastNtError(Status);
         DPRINT1("NtGdiRectInRegion: bogus rc\n");
         return ERROR;
     }
 
-    Ret = REGION_RectInRegion(Rgn, &rc);
-    RGNOBJAPI_Unlock(Rgn);
-    return Ret;
+    return IntRectInRegion(hRgn, &rc);
 }
 
 BOOL
@@ -3921,7 +3845,7 @@ NtGdiUnionRectWithRgn(
 
     if (!(Rgn = RGNOBJAPI_Lock(hDest, NULL)))
     {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_HANDLE);
         return NULL;
     }