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

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

module GI.Poppler.Structs.Point
    ( 

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


 -- * Methods
-- ** copy #method:copy#
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    PointCopyMethodInfo                     ,
#endif
    pointCopy                               ,


-- ** free #method:free#
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    PointFreeMethodInfo                     ,
#endif
    pointFree                               ,


-- ** new #method:new#
    pointNew                                ,




 -- * Properties
-- ** x #attr:x#
    getPointX                               ,
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    point_x                                 ,
#endif
    setPointX                               ,


-- ** y #attr:y#
    getPointY                               ,
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    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.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
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


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


noPoint :: Maybe Point
noPoint = Nothing

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'

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 defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
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


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'

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 defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
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 defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
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 defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
#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 defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
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 defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
data PointFreeMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo PointFreeMethodInfo Point signature where
    overloadedMethod _ = pointFree

#endif

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
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) => O.IsLabelProxy t (Point -> p) where
    fromLabelProxy _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)

#if MIN_VERSION_base(4,9,0)
instance (info ~ ResolvePointMethod t Point, O.MethodInfo info Point p) => O.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

#endif