#if __GLASGOW_HASKELL__ >= 709
{-# LANGUAGE Safe #-}
#else
{-# LANGUAGE Trustworthy #-}
#endif
-----------------------------------------------------------------------------
-- |
-- Module      :  Graphics.Win32.GDI.Region
-- Copyright   :  (c) Alastair Reid, 1997-2003
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  Esa Ilari Vuokko <ei@vuokko.info>
-- Stability   :  provisional
-- Portability :  portable
--
-- A collection of FFI declarations for interfacing with Win32.
--
-----------------------------------------------------------------------------

module Graphics.Win32.GDI.Region where

import System.Win32.Types
import Graphics.Win32.GDI.Types

import Foreign

#include "windows_cconv.h"

----------------------------------------------------------------
-- Regions
----------------------------------------------------------------

badRegion :: RegionType -> Bool
badRegion :: RegionType -> Bool
badRegion RegionType
n = RegionType
n RegionType -> RegionType -> Bool
forall a. Eq a => a -> a -> Bool
== RegionType
0 Bool -> Bool -> Bool
|| RegionType
n RegionType -> RegionType -> Bool
forall a. Eq a => a -> a -> Bool
== RegionType
forall a. Num a => a
gDI_ERROR

combineRgn :: HRGN -> HRGN -> HRGN -> ClippingMode -> IO RegionType
combineRgn :: HRGN -> HRGN -> HRGN -> RegionType -> IO RegionType
combineRgn HRGN
dest HRGN
src1 HRGN
src2 RegionType
mode =
  HRGN -> (Ptr () -> IO RegionType) -> IO RegionType
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr HRGN
dest ((Ptr () -> IO RegionType) -> IO RegionType)
-> (Ptr () -> IO RegionType) -> IO RegionType
forall a b. (a -> b) -> a -> b
$ \ Ptr ()
p_dest ->
  HRGN -> (Ptr () -> IO RegionType) -> IO RegionType
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr HRGN
src1 ((Ptr () -> IO RegionType) -> IO RegionType)
-> (Ptr () -> IO RegionType) -> IO RegionType
forall a b. (a -> b) -> a -> b
$ \ Ptr ()
p_src1 ->
  HRGN -> (Ptr () -> IO RegionType) -> IO RegionType
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr HRGN
src2 ((Ptr () -> IO RegionType) -> IO RegionType)
-> (Ptr () -> IO RegionType) -> IO RegionType
forall a b. (a -> b) -> a -> b
$ \ Ptr ()
p_src2 ->
  (RegionType -> Bool) -> String -> IO RegionType -> IO RegionType
forall a. (a -> Bool) -> String -> IO a -> IO a
failIf RegionType -> Bool
badRegion String
"CombineRgn" (IO RegionType -> IO RegionType) -> IO RegionType -> IO RegionType
forall a b. (a -> b) -> a -> b
$ Ptr () -> Ptr () -> Ptr () -> RegionType -> IO RegionType
c_CombineRgn Ptr ()
p_dest Ptr ()
p_src1 Ptr ()
p_src2 RegionType
mode
foreign import WINDOWS_CCONV unsafe "windows.h CombineRgn"
  c_CombineRgn :: PRGN -> PRGN -> PRGN -> ClippingMode -> IO RegionType

offsetRgn :: HRGN -> INT -> INT -> IO RegionType
offsetRgn :: HRGN -> RegionType -> RegionType -> IO RegionType
offsetRgn HRGN
rgn RegionType
xoff RegionType
yoff =
  HRGN -> (Ptr () -> IO RegionType) -> IO RegionType
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr HRGN
rgn ((Ptr () -> IO RegionType) -> IO RegionType)
-> (Ptr () -> IO RegionType) -> IO RegionType
forall a b. (a -> b) -> a -> b
$ \ Ptr ()
p_rgn ->
  (RegionType -> Bool) -> String -> IO RegionType -> IO RegionType
forall a. (a -> Bool) -> String -> IO a -> IO a
failIf RegionType -> Bool
badRegion String
"OffsetRgn" (IO RegionType -> IO RegionType) -> IO RegionType -> IO RegionType
forall a b. (a -> b) -> a -> b
$ Ptr () -> RegionType -> RegionType -> IO RegionType
c_OffsetRgn Ptr ()
p_rgn RegionType
xoff RegionType
yoff
foreign import WINDOWS_CCONV unsafe "windows.h OffsetRgn"
  c_OffsetRgn :: PRGN -> INT -> INT -> IO RegionType

getRgnBox :: HRGN -> LPRECT -> IO RegionType
getRgnBox :: HRGN -> LPRECT -> IO RegionType
getRgnBox HRGN
rgn LPRECT
p_rect =
  HRGN -> (Ptr () -> IO RegionType) -> IO RegionType
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr HRGN
rgn ((Ptr () -> IO RegionType) -> IO RegionType)
-> (Ptr () -> IO RegionType) -> IO RegionType
forall a b. (a -> b) -> a -> b
$ \ Ptr ()
p_rgn ->
  (RegionType -> Bool) -> String -> IO RegionType -> IO RegionType
forall a. (a -> Bool) -> String -> IO a -> IO a
failIf RegionType -> Bool
badRegion String
"GetRgnBox" (IO RegionType -> IO RegionType) -> IO RegionType -> IO RegionType
forall a b. (a -> b) -> a -> b
$ Ptr () -> LPRECT -> IO RegionType
c_GetRgnBox Ptr ()
p_rgn LPRECT
p_rect
foreign import WINDOWS_CCONV unsafe "windows.h GetRgnBox"
  c_GetRgnBox :: PRGN -> LPRECT -> IO RegionType

createEllipticRgn :: INT -> INT -> INT -> INT -> IO HRGN
createEllipticRgn :: RegionType -> RegionType -> RegionType -> RegionType -> IO HRGN
createEllipticRgn RegionType
x0 RegionType
y0 RegionType
x1 RegionType
y1 = do
  ptr <- String -> IO (Ptr ()) -> IO (Ptr ())
forall a. String -> IO (Ptr a) -> IO (Ptr a)
failIfNull String
"CreateEllipticRgn" (IO (Ptr ()) -> IO (Ptr ())) -> IO (Ptr ()) -> IO (Ptr ())
forall a b. (a -> b) -> a -> b
$ RegionType -> RegionType -> RegionType -> RegionType -> IO (Ptr ())
c_CreateEllipticRgn RegionType
x0 RegionType
y0 RegionType
x1 RegionType
y1
  newForeignHANDLE ptr
foreign import WINDOWS_CCONV unsafe "windows.h CreateEllipticRgn"
  c_CreateEllipticRgn :: INT -> INT -> INT -> INT -> IO PRGN

createEllipticRgnIndirect :: LPRECT -> IO HRGN
createEllipticRgnIndirect :: LPRECT -> IO HRGN
createEllipticRgnIndirect LPRECT
rp = do
  ptr <- String -> IO (Ptr ()) -> IO (Ptr ())
forall a. String -> IO (Ptr a) -> IO (Ptr a)
failIfNull String
"CreateEllipticRgnIndirect" (IO (Ptr ()) -> IO (Ptr ())) -> IO (Ptr ()) -> IO (Ptr ())
forall a b. (a -> b) -> a -> b
$ LPRECT -> IO (Ptr ())
c_CreateEllipticRgnIndirect LPRECT
rp
  newForeignHANDLE ptr
foreign import WINDOWS_CCONV unsafe "windows.h CreateEllipticRgnIndirect"
  c_CreateEllipticRgnIndirect :: LPRECT -> IO PRGN

createRectRgn :: INT -> INT -> INT -> INT -> IO HRGN
createRectRgn :: RegionType -> RegionType -> RegionType -> RegionType -> IO HRGN
createRectRgn RegionType
x0 RegionType
y0 RegionType
x1 RegionType
y1 = do
  ptr <- String -> IO (Ptr ()) -> IO (Ptr ())
forall a. String -> IO (Ptr a) -> IO (Ptr a)
failIfNull String
"CreateRectRgn" (IO (Ptr ()) -> IO (Ptr ())) -> IO (Ptr ()) -> IO (Ptr ())
forall a b. (a -> b) -> a -> b
$ RegionType -> RegionType -> RegionType -> RegionType -> IO (Ptr ())
c_CreateRectRgn RegionType
x0 RegionType
y0 RegionType
x1 RegionType
y1
  newForeignHANDLE ptr
foreign import WINDOWS_CCONV unsafe "windows.h CreateRectRgn"
  c_CreateRectRgn :: INT -> INT -> INT -> INT -> IO PRGN

createRectRgnIndirect :: LPRECT -> IO HRGN
createRectRgnIndirect :: LPRECT -> IO HRGN
createRectRgnIndirect LPRECT
rp = do
  ptr <- String -> IO (Ptr ()) -> IO (Ptr ())
forall a. String -> IO (Ptr a) -> IO (Ptr a)
failIfNull String
"CreateRectRgnIndirect" (IO (Ptr ()) -> IO (Ptr ())) -> IO (Ptr ()) -> IO (Ptr ())
forall a b. (a -> b) -> a -> b
$ LPRECT -> IO (Ptr ())
c_CreateRectRgnIndirect LPRECT
rp
  newForeignHANDLE ptr
foreign import WINDOWS_CCONV unsafe "windows.h CreateRectRgnIndirect"
  c_CreateRectRgnIndirect :: LPRECT -> IO PRGN

createRoundRectRgn :: INT -> INT -> INT -> INT -> INT -> INT -> IO HRGN
createRoundRectRgn :: RegionType
-> RegionType
-> RegionType
-> RegionType
-> RegionType
-> RegionType
-> IO HRGN
createRoundRectRgn RegionType
x0 RegionType
y0 RegionType
x1 RegionType
y1 RegionType
h RegionType
w = do
  ptr <- String -> IO (Ptr ()) -> IO (Ptr ())
forall a. String -> IO (Ptr a) -> IO (Ptr a)
failIfNull String
"CreateRoundRectRgn" (IO (Ptr ()) -> IO (Ptr ())) -> IO (Ptr ()) -> IO (Ptr ())
forall a b. (a -> b) -> a -> b
$ RegionType
-> RegionType
-> RegionType
-> RegionType
-> RegionType
-> RegionType
-> IO (Ptr ())
c_CreateRoundRectRgn RegionType
x0 RegionType
y0 RegionType
x1 RegionType
y1 RegionType
h RegionType
w
  newForeignHANDLE ptr
foreign import WINDOWS_CCONV unsafe "windows.h CreateRoundRectRgn"
  c_CreateRoundRectRgn :: INT -> INT -> INT -> INT -> INT -> INT -> IO PRGN

createPolygonRgn :: [POINT] -> PolyFillMode -> IO HRGN
createPolygonRgn :: [POINT] -> RegionType -> IO HRGN
createPolygonRgn [POINT]
ps RegionType
mode =
  [POINT] -> (Ptr POINT -> Int -> IO HRGN) -> IO HRGN
forall a. [POINT] -> (Ptr POINT -> Int -> IO a) -> IO a
withPOINTArray [POINT]
ps ((Ptr POINT -> Int -> IO HRGN) -> IO HRGN)
-> (Ptr POINT -> Int -> IO HRGN) -> IO HRGN
forall a b. (a -> b) -> a -> b
$ \ Ptr POINT
point_array Int
npoints -> do
  ptr <- String -> IO (Ptr ()) -> IO (Ptr ())
forall a. String -> IO (Ptr a) -> IO (Ptr a)
failIfNull String
"CreatePolygonRgn" (IO (Ptr ()) -> IO (Ptr ())) -> IO (Ptr ()) -> IO (Ptr ())
forall a b. (a -> b) -> a -> b
$
    Ptr POINT -> Int -> RegionType -> IO (Ptr ())
c_CreatePolygonRgn Ptr POINT
point_array Int
npoints RegionType
mode
  newForeignHANDLE ptr
foreign import WINDOWS_CCONV unsafe "windows.h CreatePolygonRgn"
  c_CreatePolygonRgn :: Ptr POINT -> Int -> PolyFillMode -> IO PRGN

-- Needs to do proper error test for EqualRgn; GSL ???

foreign import WINDOWS_CCONV unsafe "windows.h EqualRgn"
  equalRgn :: PRGN -> PRGN -> IO Bool

fillRgn :: HDC -> HRGN -> HBRUSH -> IO ()
fillRgn :: Ptr () -> HRGN -> Ptr () -> IO ()
fillRgn Ptr ()
dc HRGN
rgn Ptr ()
brush =
  HRGN -> (Ptr () -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr HRGN
rgn ((Ptr () -> IO ()) -> IO ()) -> (Ptr () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr ()
p_rgn ->
  String -> IO Bool -> IO ()
failIfFalse_ String
"FillRgn" (IO Bool -> IO ()) -> IO Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr () -> Ptr () -> Ptr () -> IO Bool
c_FillRgn Ptr ()
dc Ptr ()
p_rgn Ptr ()
brush
foreign import WINDOWS_CCONV unsafe "windows.h FillRgn"
  c_FillRgn :: HDC -> PRGN -> HBRUSH -> IO Bool

invertRgn :: HDC -> HRGN -> IO ()
invertRgn :: Ptr () -> HRGN -> IO ()
invertRgn Ptr ()
dc HRGN
rgn =
  HRGN -> (Ptr () -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr HRGN
rgn ((Ptr () -> IO ()) -> IO ()) -> (Ptr () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr ()
p_rgn ->
  String -> IO Bool -> IO ()
failIfFalse_ String
"InvertRgn" (IO Bool -> IO ()) -> IO Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr () -> Ptr () -> IO Bool
c_InvertRgn Ptr ()
dc Ptr ()
p_rgn
foreign import WINDOWS_CCONV unsafe "windows.h InvertRgn"
  c_InvertRgn :: HDC -> PRGN -> IO Bool

paintRgn :: HDC -> HRGN -> IO ()
paintRgn :: Ptr () -> HRGN -> IO ()
paintRgn Ptr ()
dc HRGN
rgn =
  HRGN -> (Ptr () -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr HRGN
rgn ((Ptr () -> IO ()) -> IO ()) -> (Ptr () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr ()
p_rgn ->
  String -> IO Bool -> IO ()
failIfFalse_ String
"PaintRgn" (IO Bool -> IO ()) -> IO Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr () -> Ptr () -> IO Bool
c_PaintRgn Ptr ()
dc Ptr ()
p_rgn
foreign import WINDOWS_CCONV unsafe "windows.h PaintRgn"
  c_PaintRgn :: HDC -> PRGN -> IO Bool

frameRgn :: HDC -> HRGN -> HBRUSH -> Int -> Int -> IO ()
frameRgn :: Ptr () -> HRGN -> Ptr () -> Int -> Int -> IO ()
frameRgn Ptr ()
dc HRGN
rgn Ptr ()
brush Int
w Int
h =
  HRGN -> (Ptr () -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr HRGN
rgn ((Ptr () -> IO ()) -> IO ()) -> (Ptr () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr ()
p_rgn ->
  String -> IO Bool -> IO ()
failIfFalse_ String
"FrameRgn" (IO Bool -> IO ()) -> IO Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr () -> Ptr () -> Ptr () -> Int -> Int -> IO Bool
c_FrameRgn Ptr ()
dc Ptr ()
p_rgn Ptr ()
brush Int
w Int
h
foreign import WINDOWS_CCONV unsafe "windows.h FrameRgn"
  c_FrameRgn :: HDC -> PRGN -> HBRUSH -> Int -> Int -> IO Bool

ptInRegion :: HRGN -> Int -> Int -> IO Bool
ptInRegion :: HRGN -> Int -> Int -> IO Bool
ptInRegion HRGN
rgn Int
x Int
y =
  HRGN -> (Ptr () -> IO Bool) -> IO Bool
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr HRGN
rgn ((Ptr () -> IO Bool) -> IO Bool) -> (Ptr () -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ Ptr ()
p_rgn ->
  Ptr () -> Int -> Int -> IO Bool
c_PtInRegion Ptr ()
p_rgn Int
x Int
y
foreign import WINDOWS_CCONV unsafe "windows.h PtInRegion"
  c_PtInRegion :: PRGN -> Int -> Int -> IO Bool

rectInRegion :: HRGN -> RECT -> IO Bool
rectInRegion :: HRGN -> RECT -> IO Bool
rectInRegion HRGN
rgn RECT
rect =
  HRGN -> (Ptr () -> IO Bool) -> IO Bool
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr HRGN
rgn ((Ptr () -> IO Bool) -> IO Bool) -> (Ptr () -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ Ptr ()
p_rgn ->
  RECT -> (LPRECT -> IO Bool) -> IO Bool
forall a. RECT -> (LPRECT -> IO a) -> IO a
withRECT RECT
rect ((LPRECT -> IO Bool) -> IO Bool) -> (LPRECT -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ Ptr () -> LPRECT -> IO Bool
c_RectInRegion Ptr ()
p_rgn
foreign import WINDOWS_CCONV unsafe "windows.h RectInRegion"
  c_RectInRegion :: PRGN -> Ptr RECT -> IO Bool

----------------------------------------------------------------
-- End
----------------------------------------------------------------