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

A 'GI.Poppler.Structs.Point.Point' is used to describe a location point on a page
-}

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

module GI.Poppler.Structs.Point
    (

-- * Exported types
    Point(..)                               ,
    newZeroPoint                            ,
    noPoint                                 ,


 -- * Methods
-- ** copy #method:copy#

#if ENABLE_OVERLOADING
    PointCopyMethodInfo                     ,
#endif
    pointCopy                               ,


-- ** free #method:free#

#if ENABLE_OVERLOADING
    PointFreeMethodInfo                     ,
#endif
    pointFree                               ,


-- ** new #method:new#

    pointNew                                ,




 -- * Properties
-- ** x #attr:x#
{- | x coordinate
-}
    getPointX                               ,
#if ENABLE_OVERLOADING
    point_x                                 ,
#endif
    setPointX                               ,


-- ** y #attr:y#
{- | y coordinate
-}
    getPointY                               ,
#if ENABLE_OVERLOADING
    point_y                                 ,
#endif
    setPointY                               ,




    ) 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.GClosure as B.GClosure
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.GI.Base.Properties as B.Properties
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
import qualified GHC.OverloadedLabels as OL


-- | Memory-managed wrapper type.
newtype Point = Point (ManagedPtr Point)
foreign import ccall "poppler_point_get_type" c_poppler_point_get_type ::
    IO GType

instance BoxedObject Point where
    boxedType _ = c_poppler_point_get_type

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

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


-- | A convenience alias for `Nothing` :: `Maybe` `Point`.
noPoint :: Maybe Point
noPoint = 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' point #x
@
-}
getPointX :: MonadIO m => Point -> m Double
getPointX s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 0) :: IO CDouble
    let val' = realToFrac val
    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' point [ #x 'Data.GI.Base.Attributes.:=' value ]
@
-}
setPointX :: MonadIO m => Point -> Double -> m ()
setPointX s val = liftIO $ withManagedPtr s $ \ptr -> do
    let val' = realToFrac val
    poke (ptr `plusPtr` 0) (val' :: CDouble)

#if ENABLE_OVERLOADING
data PointXFieldInfo
instance AttrInfo PointXFieldInfo where
    type AttrAllowedOps PointXFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint PointXFieldInfo = (~) Double
    type AttrBaseTypeConstraint PointXFieldInfo = (~) Point
    type AttrGetType PointXFieldInfo = Double
    type AttrLabel PointXFieldInfo = "x"
    type AttrOrigin PointXFieldInfo = Point
    attrGet _ = getPointX
    attrSet _ = setPointX
    attrConstruct = undefined
    attrClear _ = undefined

point_x :: AttrLabelProxy "x"
point_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' point #y
@
-}
getPointY :: MonadIO m => Point -> m Double
getPointY s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 8) :: IO CDouble
    let val' = realToFrac val
    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' point [ #y 'Data.GI.Base.Attributes.:=' value ]
@
-}
setPointY :: MonadIO m => Point -> Double -> m ()
setPointY s val = liftIO $ withManagedPtr s $ \ptr -> do
    let val' = realToFrac val
    poke (ptr `plusPtr` 8) (val' :: CDouble)

#if ENABLE_OVERLOADING
data PointYFieldInfo
instance AttrInfo PointYFieldInfo where
    type AttrAllowedOps PointYFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint PointYFieldInfo = (~) Double
    type AttrBaseTypeConstraint PointYFieldInfo = (~) Point
    type AttrGetType PointYFieldInfo = Double
    type AttrLabel PointYFieldInfo = "y"
    type AttrOrigin PointYFieldInfo = Point
    attrGet _ = getPointY
    attrSet _ = setPointY
    attrConstruct = undefined
    attrClear _ = undefined

point_y :: AttrLabelProxy "y"
point_y = AttrLabelProxy

#endif



#if ENABLE_OVERLOADING
instance O.HasAttributeList Point
type instance O.AttributeList Point = PointAttributeList
type PointAttributeList = ('[ '("x", PointXFieldInfo), '("y", PointYFieldInfo)] :: [(Symbol, *)])
#endif

-- method Point::new
-- method type : Constructor
-- Args : []
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Poppler", name = "Point"}))
-- throws : False
-- Skip return : False

foreign import ccall "poppler_point_new" poppler_point_new ::
    IO (Ptr Point)

{- |
Creates a new 'GI.Poppler.Structs.Point.Point'. It must be freed with 'GI.Poppler.Structs.Point.pointFree' after use.

/Since: 0.26/
-}
pointNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    m Point
    {- ^ __Returns:__ a new 'GI.Poppler.Structs.Point.Point' -}
pointNew  = liftIO $ do
    result <- poppler_point_new
    checkUnexpectedReturnNULL "pointNew" result
    result' <- (wrapBoxed Point) result
    return result'

#if ENABLE_OVERLOADING
#endif

-- method Point::copy
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "point", argType = TInterface (Name {namespace = "Poppler", name = "Point"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PopplerPoint to copy", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Poppler", name = "Point"}))
-- throws : False
-- Skip return : False

foreign import ccall "poppler_point_copy" poppler_point_copy ::
    Ptr Point ->                            -- point : TInterface (Name {namespace = "Poppler", name = "Point"})
    IO (Ptr Point)

{- |
Creates a copy of /@point@/. The copy must be freed with 'GI.Poppler.Structs.Point.pointFree'
after use.

/Since: 0.26/
-}
pointCopy ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Point
    {- ^ /@point@/: a 'GI.Poppler.Structs.Point.Point' to copy -}
    -> m Point
    {- ^ __Returns:__ a new allocated copy of /@point@/ -}
pointCopy point = liftIO $ do
    point' <- unsafeManagedPtrGetPtr point
    result <- poppler_point_copy point'
    checkUnexpectedReturnNULL "pointCopy" result
    result' <- (wrapBoxed Point) result
    touchManagedPtr point
    return result'

#if ENABLE_OVERLOADING
data PointCopyMethodInfo
instance (signature ~ (m Point), MonadIO m) => O.MethodInfo PointCopyMethodInfo Point signature where
    overloadedMethod _ = pointCopy

#endif

-- method Point::free
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "point", argType = TInterface (Name {namespace = "Poppler", name = "Point"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PopplerPoint", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "poppler_point_free" poppler_point_free ::
    Ptr Point ->                            -- point : TInterface (Name {namespace = "Poppler", name = "Point"})
    IO ()

{- |
Frees the memory used by /@point@/

/Since: 0.26/
-}
pointFree ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Point
    {- ^ /@point@/: a 'GI.Poppler.Structs.Point.Point' -}
    -> m ()
pointFree point = liftIO $ do
    point' <- unsafeManagedPtrGetPtr point
    poppler_point_free point'
    touchManagedPtr point
    return ()

#if ENABLE_OVERLOADING
data PointFreeMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo PointFreeMethodInfo Point signature where
    overloadedMethod _ = pointFree

#endif

#if ENABLE_OVERLOADING
type family ResolvePointMethod (t :: Symbol) (o :: *) :: * where
    ResolvePointMethod "copy" o = PointCopyMethodInfo
    ResolvePointMethod "free" o = PointFreeMethodInfo
    ResolvePointMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolvePointMethod t Point, O.MethodInfo info Point p) => OL.IsLabel t (Point -> 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