{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (garetxe@gmail.com)

Defines the position and size of a rectangle. It is identical to
'GI.Cairo.Structs.RectangleInt.RectangleInt'.
-}

#define ENABLE_OVERLOADING (MIN_VERSION_haskell_gi_overloading(1,0,0) \
       && !defined(__HADDOCK_VERSION__))

module GI.Gdk.Structs.Rectangle
    (

-- * Exported types
    Rectangle(..)                           ,
    newZeroRectangle                        ,
    noRectangle                             ,


 -- * Methods
-- ** equal #method:equal#

#if ENABLE_OVERLOADING
    RectangleEqualMethodInfo                ,
#endif
    rectangleEqual                          ,


-- ** intersect #method:intersect#

#if ENABLE_OVERLOADING
    RectangleIntersectMethodInfo            ,
#endif
    rectangleIntersect                      ,


-- ** union #method:union#

#if ENABLE_OVERLOADING
    RectangleUnionMethodInfo                ,
#endif
    rectangleUnion                          ,




 -- * Properties
-- ** height #attr:height#
{- | /No description available in the introspection data./
-}
    getRectangleHeight                      ,
#if ENABLE_OVERLOADING
    rectangle_height                        ,
#endif
    setRectangleHeight                      ,


-- ** width #attr:width#
{- | /No description available in the introspection data./
-}
    getRectangleWidth                       ,
#if ENABLE_OVERLOADING
    rectangle_width                         ,
#endif
    setRectangleWidth                       ,


-- ** x #attr:x#
{- | /No description available in the introspection data./
-}
    getRectangleX                           ,
#if ENABLE_OVERLOADING
    rectangle_x                             ,
#endif
    setRectangleX                           ,


-- ** y #attr:y#
{- | /No description available in the introspection data./
-}
    getRectangleY                           ,
#if ENABLE_OVERLOADING
    rectangle_y                             ,
#endif
    setRectangleY                           ,




    ) where

import Data.GI.Base.ShortPrelude
import qualified Data.GI.Base.ShortPrelude as SP
import qualified Data.GI.Base.Overloading as O
import qualified Prelude as P

import qualified Data.GI.Base.Attributes as GI.Attributes
import qualified Data.GI.Base.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GValue as B.GValue
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP


-- | Memory-managed wrapper type.
newtype Rectangle = Rectangle (ManagedPtr Rectangle)
foreign import ccall "gdk_rectangle_get_type" c_gdk_rectangle_get_type ::
    IO GType

instance BoxedObject Rectangle where
    boxedType _ = c_gdk_rectangle_get_type

-- | Construct a `Rectangle` struct initialized to zero.
newZeroRectangle :: MonadIO m => m Rectangle
newZeroRectangle = liftIO $ callocBoxedBytes 16 >>= wrapBoxed Rectangle

instance tag ~ 'AttrSet => Constructible Rectangle tag where
    new _ attrs = do
        o <- newZeroRectangle
        GI.Attributes.set o attrs
        return o


-- | A convenience alias for `Nothing` :: `Maybe` `Rectangle`.
noRectangle :: Maybe Rectangle
noRectangle = Nothing

{- |
Get the value of the “@x@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' rectangle #x
@
-}
getRectangleX :: MonadIO m => Rectangle -> m Int32
getRectangleX s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 0) :: IO Int32
    return val

{- |
Set the value of the “@x@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' rectangle [ #x 'Data.GI.Base.Attributes.:=' value ]
@
-}
setRectangleX :: MonadIO m => Rectangle -> Int32 -> m ()
setRectangleX s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 0) (val :: Int32)

#if ENABLE_OVERLOADING
data RectangleXFieldInfo
instance AttrInfo RectangleXFieldInfo where
    type AttrAllowedOps RectangleXFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint RectangleXFieldInfo = (~) Int32
    type AttrBaseTypeConstraint RectangleXFieldInfo = (~) Rectangle
    type AttrGetType RectangleXFieldInfo = Int32
    type AttrLabel RectangleXFieldInfo = "x"
    type AttrOrigin RectangleXFieldInfo = Rectangle
    attrGet _ = getRectangleX
    attrSet _ = setRectangleX
    attrConstruct = undefined
    attrClear _ = undefined

rectangle_x :: AttrLabelProxy "x"
rectangle_x = AttrLabelProxy

#endif


{- |
Get the value of the “@y@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' rectangle #y
@
-}
getRectangleY :: MonadIO m => Rectangle -> m Int32
getRectangleY s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 4) :: IO Int32
    return val

{- |
Set the value of the “@y@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' rectangle [ #y 'Data.GI.Base.Attributes.:=' value ]
@
-}
setRectangleY :: MonadIO m => Rectangle -> Int32 -> m ()
setRectangleY s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 4) (val :: Int32)

#if ENABLE_OVERLOADING
data RectangleYFieldInfo
instance AttrInfo RectangleYFieldInfo where
    type AttrAllowedOps RectangleYFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint RectangleYFieldInfo = (~) Int32
    type AttrBaseTypeConstraint RectangleYFieldInfo = (~) Rectangle
    type AttrGetType RectangleYFieldInfo = Int32
    type AttrLabel RectangleYFieldInfo = "y"
    type AttrOrigin RectangleYFieldInfo = Rectangle
    attrGet _ = getRectangleY
    attrSet _ = setRectangleY
    attrConstruct = undefined
    attrClear _ = undefined

rectangle_y :: AttrLabelProxy "y"
rectangle_y = AttrLabelProxy

#endif


{- |
Get the value of the “@width@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' rectangle #width
@
-}
getRectangleWidth :: MonadIO m => Rectangle -> m Int32
getRectangleWidth s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 8) :: IO Int32
    return val

{- |
Set the value of the “@width@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' rectangle [ #width 'Data.GI.Base.Attributes.:=' value ]
@
-}
setRectangleWidth :: MonadIO m => Rectangle -> Int32 -> m ()
setRectangleWidth s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 8) (val :: Int32)

#if ENABLE_OVERLOADING
data RectangleWidthFieldInfo
instance AttrInfo RectangleWidthFieldInfo where
    type AttrAllowedOps RectangleWidthFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint RectangleWidthFieldInfo = (~) Int32
    type AttrBaseTypeConstraint RectangleWidthFieldInfo = (~) Rectangle
    type AttrGetType RectangleWidthFieldInfo = Int32
    type AttrLabel RectangleWidthFieldInfo = "width"
    type AttrOrigin RectangleWidthFieldInfo = Rectangle
    attrGet _ = getRectangleWidth
    attrSet _ = setRectangleWidth
    attrConstruct = undefined
    attrClear _ = undefined

rectangle_width :: AttrLabelProxy "width"
rectangle_width = AttrLabelProxy

#endif


{- |
Get the value of the “@height@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' rectangle #height
@
-}
getRectangleHeight :: MonadIO m => Rectangle -> m Int32
getRectangleHeight s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 12) :: IO Int32
    return val

{- |
Set the value of the “@height@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' rectangle [ #height 'Data.GI.Base.Attributes.:=' value ]
@
-}
setRectangleHeight :: MonadIO m => Rectangle -> Int32 -> m ()
setRectangleHeight s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 12) (val :: Int32)

#if ENABLE_OVERLOADING
data RectangleHeightFieldInfo
instance AttrInfo RectangleHeightFieldInfo where
    type AttrAllowedOps RectangleHeightFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint RectangleHeightFieldInfo = (~) Int32
    type AttrBaseTypeConstraint RectangleHeightFieldInfo = (~) Rectangle
    type AttrGetType RectangleHeightFieldInfo = Int32
    type AttrLabel RectangleHeightFieldInfo = "height"
    type AttrOrigin RectangleHeightFieldInfo = Rectangle
    attrGet _ = getRectangleHeight
    attrSet _ = setRectangleHeight
    attrConstruct = undefined
    attrClear _ = undefined

rectangle_height :: AttrLabelProxy "height"
rectangle_height = AttrLabelProxy

#endif



#if ENABLE_OVERLOADING
instance O.HasAttributeList Rectangle
type instance O.AttributeList Rectangle = RectangleAttributeList
type RectangleAttributeList = ('[ '("x", RectangleXFieldInfo), '("y", RectangleYFieldInfo), '("width", RectangleWidthFieldInfo), '("height", RectangleHeightFieldInfo)] :: [(Symbol, *)])
#endif

-- method Rectangle::equal
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "rect1", argType = TInterface (Name {namespace = "Gdk", name = "Rectangle"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GdkRectangle", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "rect2", argType = TInterface (Name {namespace = "Gdk", name = "Rectangle"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GdkRectangle", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "gdk_rectangle_equal" gdk_rectangle_equal ::
    Ptr Rectangle ->                        -- rect1 : TInterface (Name {namespace = "Gdk", name = "Rectangle"})
    Ptr Rectangle ->                        -- rect2 : TInterface (Name {namespace = "Gdk", name = "Rectangle"})
    IO CInt

{- |
Checks if the two given rectangles are equal.

/Since: 3.20/
-}
rectangleEqual ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Rectangle
    {- ^ /@rect1@/: a 'GI.Gdk.Structs.Rectangle.Rectangle' -}
    -> Rectangle
    {- ^ /@rect2@/: a 'GI.Gdk.Structs.Rectangle.Rectangle' -}
    -> m Bool
    {- ^ __Returns:__ 'True' if the rectangles are equal. -}
rectangleEqual rect1 rect2 = liftIO $ do
    rect1' <- unsafeManagedPtrGetPtr rect1
    rect2' <- unsafeManagedPtrGetPtr rect2
    result <- gdk_rectangle_equal rect1' rect2'
    let result' = (/= 0) result
    touchManagedPtr rect1
    touchManagedPtr rect2
    return result'

#if ENABLE_OVERLOADING
data RectangleEqualMethodInfo
instance (signature ~ (Rectangle -> m Bool), MonadIO m) => O.MethodInfo RectangleEqualMethodInfo Rectangle signature where
    overloadedMethod _ = rectangleEqual

#endif

-- method Rectangle::intersect
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "src1", argType = TInterface (Name {namespace = "Gdk", name = "Rectangle"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GdkRectangle", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "src2", argType = TInterface (Name {namespace = "Gdk", name = "Rectangle"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GdkRectangle", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "dest", argType = TInterface (Name {namespace = "Gdk", name = "Rectangle"}), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "return location for the\nintersection of @src1 and @src2, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = True, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "gdk_rectangle_intersect" gdk_rectangle_intersect ::
    Ptr Rectangle ->                        -- src1 : TInterface (Name {namespace = "Gdk", name = "Rectangle"})
    Ptr Rectangle ->                        -- src2 : TInterface (Name {namespace = "Gdk", name = "Rectangle"})
    Ptr Rectangle ->                        -- dest : TInterface (Name {namespace = "Gdk", name = "Rectangle"})
    IO CInt

{- |
Calculates the intersection of two rectangles. It is allowed for
/@dest@/ to be the same as either /@src1@/ or /@src2@/. If the rectangles
do not intersect, /@dest@/’s width and height is set to 0 and its x
and y values are undefined. If you are only interested in whether
the rectangles intersect, but not in the intersecting area itself,
pass 'Nothing' for /@dest@/.
-}
rectangleIntersect ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Rectangle
    {- ^ /@src1@/: a 'GI.Gdk.Structs.Rectangle.Rectangle' -}
    -> Rectangle
    {- ^ /@src2@/: a 'GI.Gdk.Structs.Rectangle.Rectangle' -}
    -> m ((Bool, Rectangle))
    {- ^ __Returns:__ 'True' if the rectangles intersect. -}
rectangleIntersect src1 src2 = liftIO $ do
    src1' <- unsafeManagedPtrGetPtr src1
    src2' <- unsafeManagedPtrGetPtr src2
    dest <- callocBoxedBytes 16 :: IO (Ptr Rectangle)
    result <- gdk_rectangle_intersect src1' src2' dest
    let result' = (/= 0) result
    dest' <- (wrapBoxed Rectangle) dest
    touchManagedPtr src1
    touchManagedPtr src2
    return (result', dest')

#if ENABLE_OVERLOADING
data RectangleIntersectMethodInfo
instance (signature ~ (Rectangle -> m ((Bool, Rectangle))), MonadIO m) => O.MethodInfo RectangleIntersectMethodInfo Rectangle signature where
    overloadedMethod _ = rectangleIntersect

#endif

-- method Rectangle::union
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "src1", argType = TInterface (Name {namespace = "Gdk", name = "Rectangle"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GdkRectangle", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "src2", argType = TInterface (Name {namespace = "Gdk", name = "Rectangle"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GdkRectangle", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "dest", argType = TInterface (Name {namespace = "Gdk", name = "Rectangle"}), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "return location for the union of @src1 and @src2", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = True, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "gdk_rectangle_union" gdk_rectangle_union ::
    Ptr Rectangle ->                        -- src1 : TInterface (Name {namespace = "Gdk", name = "Rectangle"})
    Ptr Rectangle ->                        -- src2 : TInterface (Name {namespace = "Gdk", name = "Rectangle"})
    Ptr Rectangle ->                        -- dest : TInterface (Name {namespace = "Gdk", name = "Rectangle"})
    IO ()

{- |
Calculates the union of two rectangles.
The union of rectangles /@src1@/ and /@src2@/ is the smallest rectangle which
includes both /@src1@/ and /@src2@/ within it.
It is allowed for /@dest@/ to be the same as either /@src1@/ or /@src2@/.

Note that this function does not ignore \'empty\' rectangles (ie. with
zero width or height).
-}
rectangleUnion ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Rectangle
    {- ^ /@src1@/: a 'GI.Gdk.Structs.Rectangle.Rectangle' -}
    -> Rectangle
    {- ^ /@src2@/: a 'GI.Gdk.Structs.Rectangle.Rectangle' -}
    -> m (Rectangle)
rectangleUnion src1 src2 = liftIO $ do
    src1' <- unsafeManagedPtrGetPtr src1
    src2' <- unsafeManagedPtrGetPtr src2
    dest <- callocBoxedBytes 16 :: IO (Ptr Rectangle)
    gdk_rectangle_union src1' src2' dest
    dest' <- (wrapBoxed Rectangle) dest
    touchManagedPtr src1
    touchManagedPtr src2
    return dest'

#if ENABLE_OVERLOADING
data RectangleUnionMethodInfo
instance (signature ~ (Rectangle -> m (Rectangle)), MonadIO m) => O.MethodInfo RectangleUnionMethodInfo Rectangle signature where
    overloadedMethod _ = rectangleUnion

#endif

#if ENABLE_OVERLOADING
type family ResolveRectangleMethod (t :: Symbol) (o :: *) :: * where
    ResolveRectangleMethod "equal" o = RectangleEqualMethodInfo
    ResolveRectangleMethod "intersect" o = RectangleIntersectMethodInfo
    ResolveRectangleMethod "union" o = RectangleUnionMethodInfo
    ResolveRectangleMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveRectangleMethod t Rectangle, O.MethodInfo info Rectangle p) => O.IsLabelProxy t (Rectangle -> p) where
    fromLabelProxy _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)

#if MIN_VERSION_base(4,9,0)
instance (info ~ ResolveRectangleMethod t Rectangle, O.MethodInfo info Rectangle p) => O.IsLabel t (Rectangle -> p) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#else
    fromLabel _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#endif
#endif

#endif