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

    The 'GI.Pango.Structs.AttrFloat.AttrFloat' structure is used to represent attributes with
    a float or double value.
    -}

    #define ENABLE_OVERLOADING \
           (!defined(__HADDOCK_VERSION__))

    module GI.Pango.Structs.AttrFloat
        (

    -- * Exported types
        AttrFloat(..)                           ,
        newZeroAttrFloat                        ,
        noAttrFloat                             ,

     -- * Properties
    -- ** attr #attr:attr#
    {- | the common portion of the attribute
    -}
    #if ENABLE_OVERLOADING
        attrFloat_attr                          ,
    #endif
        getAttrFloatAttr                        ,

    -- ** value #attr:value#
    {- | the value of the attribute
    -}
    #if ENABLE_OVERLOADING
        attrFloat_value                         ,
    #endif
        getAttrFloatValue                       ,
        setAttrFloatValue                       ,

        ) 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

    import {-# SOURCE #-} qualified GI.Pango.Structs.Attribute as Pango.Attribute

    -- | Memory-managed wrapper type.
    newtype AttrFloat = AttrFloat (ManagedPtr AttrFloat)
    instance WrappedPtr AttrFloat where
        wrappedPtrCalloc = callocBytes 24
        wrappedPtrCopy = \p -> withManagedPtr p (copyBytes 24 >=> wrapPtr AttrFloat)
        wrappedPtrFree = Just ptr_to_g_free

    -- | Construct a `AttrFloat` struct initialized to zero.
    newZeroAttrFloat :: MonadIO m => m AttrFloat
    newZeroAttrFloat = liftIO $ wrappedPtrCalloc >>= wrapPtr AttrFloat

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

    -- | A convenience alias for `Nothing` :: `Maybe` `AttrFloat`.
    noAttrFloat :: Maybe AttrFloat
    noAttrFloat = Nothing

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

    @
    'Data.GI.Base.Attributes.get' attrFloat #attr
    @
    -}
    getAttrFloatAttr :: MonadIO m => AttrFloat -> m Pango.Attribute.Attribute
    getAttrFloatAttr s = liftIO $ withManagedPtr s $ \ptr -> do
        let val = ptr `plusPtr` 0 :: (Ptr Pango.Attribute.Attribute)
        val' <- (newPtr Pango.Attribute.Attribute) val
        return val'

    #if ENABLE_OVERLOADING
    data AttrFloatAttrFieldInfo
    instance AttrInfo AttrFloatAttrFieldInfo where
        type AttrAllowedOps AttrFloatAttrFieldInfo = '[ 'AttrGet]
        type AttrSetTypeConstraint AttrFloatAttrFieldInfo = (~) (Ptr Pango.Attribute.Attribute)
        type AttrBaseTypeConstraint AttrFloatAttrFieldInfo = (~) AttrFloat
        type AttrGetType AttrFloatAttrFieldInfo = Pango.Attribute.Attribute
        type AttrLabel AttrFloatAttrFieldInfo = "attr"
        type AttrOrigin AttrFloatAttrFieldInfo = AttrFloat
        attrGet _ = getAttrFloatAttr
        attrSet _ = undefined
        attrConstruct = undefined
        attrClear _ = undefined

    attrFloat_attr :: AttrLabelProxy "attr"
    attrFloat_attr = AttrLabelProxy

    #endif

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

    @
    'Data.GI.Base.Attributes.get' attrFloat #value
    @
    -}
    getAttrFloatValue :: MonadIO m => AttrFloat -> m Double
    getAttrFloatValue s = liftIO $ withManagedPtr s $ \ptr -> do
        val <- peek (ptr `plusPtr` 16) :: IO CDouble
        let val' = realToFrac val
        return val'

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

    @
    'Data.GI.Base.Attributes.set' attrFloat [ #value 'Data.GI.Base.Attributes.:=' value ]
    @
    -}
    setAttrFloatValue :: MonadIO m => AttrFloat -> Double -> m ()
    setAttrFloatValue s val = liftIO $ withManagedPtr s $ \ptr -> do
        let val' = realToFrac val
        poke (ptr `plusPtr` 16) (val' :: CDouble)

    #if ENABLE_OVERLOADING
    data AttrFloatValueFieldInfo
    instance AttrInfo AttrFloatValueFieldInfo where
        type AttrAllowedOps AttrFloatValueFieldInfo = '[ 'AttrSet, 'AttrGet]
        type AttrSetTypeConstraint AttrFloatValueFieldInfo = (~) Double
        type AttrBaseTypeConstraint AttrFloatValueFieldInfo = (~) AttrFloat
        type AttrGetType AttrFloatValueFieldInfo = Double
        type AttrLabel AttrFloatValueFieldInfo = "value"
        type AttrOrigin AttrFloatValueFieldInfo = AttrFloat
        attrGet _ = getAttrFloatValue
        attrSet _ = setAttrFloatValue
        attrConstruct = undefined
        attrClear _ = undefined

    attrFloat_value :: AttrLabelProxy "value"
    attrFloat_value = AttrLabelProxy

    #endif

    #if ENABLE_OVERLOADING
    instance O.HasAttributeList AttrFloat
    type instance O.AttributeList AttrFloat = AttrFloatAttributeList
    type AttrFloatAttributeList = ('[ '("attr", AttrFloatAttrFieldInfo), '("value", AttrFloatValueFieldInfo)] :: [(Symbol, *)])
    #endif

    #if ENABLE_OVERLOADING
    type family ResolveAttrFloatMethod (t :: Symbol) (o :: *) :: * where
        ResolveAttrFloatMethod l o = O.MethodResolutionFailed l o

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

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