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

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

module GI.GdkPixbuf.Callbacks
    (

 -- * Signals
-- ** PixbufDestroyNotify #signal:PixbufDestroyNotify#

    C_PixbufDestroyNotify                   ,
    PixbufDestroyNotify                     ,
    PixbufDestroyNotify_WithClosures        ,
    drop_closures_PixbufDestroyNotify       ,
    dynamic_PixbufDestroyNotify             ,
    genClosure_PixbufDestroyNotify          ,
    mk_PixbufDestroyNotify                  ,
    noPixbufDestroyNotify                   ,
    noPixbufDestroyNotify_WithClosures      ,
    wrap_PixbufDestroyNotify                ,


-- ** PixbufSaveFunc #signal:PixbufSaveFunc#

    C_PixbufSaveFunc                        ,
    PixbufSaveFunc                          ,
    PixbufSaveFunc_WithClosures             ,
    drop_closures_PixbufSaveFunc            ,
    dynamic_PixbufSaveFunc                  ,
    genClosure_PixbufSaveFunc               ,
    mk_PixbufSaveFunc                       ,
    noPixbufSaveFunc                        ,
    noPixbufSaveFunc_WithClosures           ,
    wrap_PixbufSaveFunc                     ,




    ) 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


-- callback PixbufSaveFunc
--          -> Callable {returnType = Just (TBasicType TBoolean), returnMayBeNull = False, returnTransfer = TransferNothing, returnDocumentation = Documentation {rawDocText = Just "%TRUE if successful, %FALSE (with @error set) if failed.", sinceVersion = Nothing}, args = [Arg {argCName = "buf", argType = TCArray False (-1) 1 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "bytes to be written.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "count", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "number of bytes in @buf.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "error", argType = TError, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "A location to return an error.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything},Arg {argCName = "data", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "user data passed to gdk_pixbuf_save_to_callback().", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = 3, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}], skipReturn = False, callableThrows = False, callableDeprecated = Nothing, callableDocumentation = Documentation {rawDocText = Just "Specifies the type of the function passed to\ngdk_pixbuf_save_to_callback().  It is called once for each block of\nbytes that is \"written\" by gdk_pixbuf_save_to_callback().  If\nsuccessful it should return %TRUE.  If an error occurs it should set\n@error and return %FALSE, in which case gdk_pixbuf_save_to_callback()\nwill fail with the same error.", sinceVersion = Just "2.4"}}
-- | Type for the callback on the (unwrapped) C side.
type C_PixbufSaveFunc =
    Ptr Word8 ->
    Word64 ->
    Ptr (Ptr GError) ->
    Ptr () ->
    IO CInt

-- Args : [Arg {argCName = "buf", argType = TCArray False (-1) 1 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "bytes to be written.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "count", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "number of bytes in @buf.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "error", argType = TError, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "A location to return an error.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything},Arg {argCName = "data", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "user data passed to gdk_pixbuf_save_to_callback().", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = 3, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : [Arg {argCName = "count", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "number of bytes in @buf.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- returnType : Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "dynamic" __dynamic_C_PixbufSaveFunc :: FunPtr C_PixbufSaveFunc -> C_PixbufSaveFunc

-- | Given a pointer to a foreign C function, wrap it into a function callable from Haskell.
dynamic_PixbufSaveFunc ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    FunPtr C_PixbufSaveFunc
    -> ByteString
    {- ^ /@buf@/: bytes to be written. -}
    -> Ptr ()
    {- ^ /@data@/: user data passed to @/gdk_pixbuf_save_to_callback()/@. -}
    -> m ((Bool, GError))
    {- ^ __Returns:__ 'True' if successful, 'False' (with /@error@/ set) if failed. -}
dynamic_PixbufSaveFunc __funPtr buf data_ = liftIO $ do
    let count = fromIntegral $ B.length buf
    buf' <- packByteString buf
    error_ <- allocMem :: IO (Ptr (Ptr GError))
    result <- (__dynamic_C_PixbufSaveFunc __funPtr) buf' count error_ data_
    let result' = (/= 0) result
    error_' <- peek error_
    error_'' <- (wrapBoxed GError) error_'
    freeMem buf'
    freeMem error_
    return (result', error_'')

-- | Generate a function pointer callable from C code, from a `C_PixbufSaveFunc`.
foreign import ccall "wrapper"
    mk_PixbufSaveFunc :: C_PixbufSaveFunc -> IO (FunPtr C_PixbufSaveFunc)

{- |
Specifies the type of the function passed to
@/gdk_pixbuf_save_to_callback()/@.  It is called once for each block of
bytes that is \"written\" by @/gdk_pixbuf_save_to_callback()/@.  If
successful it should return 'True'.  If an error occurs it should set
/@error@/ and return 'False', in which case @/gdk_pixbuf_save_to_callback()/@
will fail with the same error.

/Since: 2.4/
-}
type PixbufSaveFunc =
    ByteString
    {- ^ /@buf@/: bytes to be written. -}
    -> IO ((Bool, GError))
    {- ^ __Returns:__ 'True' if successful, 'False' (with /@error@/ set) if failed. -}

-- | A convenience synonym for @`Nothing` :: `Maybe` `PixbufSaveFunc`@.
noPixbufSaveFunc :: Maybe PixbufSaveFunc
noPixbufSaveFunc = Nothing

{- |
Specifies the type of the function passed to
@/gdk_pixbuf_save_to_callback()/@.  It is called once for each block of
bytes that is \"written\" by @/gdk_pixbuf_save_to_callback()/@.  If
successful it should return 'True'.  If an error occurs it should set
/@error@/ and return 'False', in which case @/gdk_pixbuf_save_to_callback()/@
will fail with the same error.

/Since: 2.4/
-}
type PixbufSaveFunc_WithClosures =
    ByteString
    {- ^ /@buf@/: bytes to be written. -}
    -> Ptr ()
    {- ^ /@data@/: user data passed to @/gdk_pixbuf_save_to_callback()/@. -}
    -> IO ((Bool, GError))
    {- ^ __Returns:__ 'True' if successful, 'False' (with /@error@/ set) if failed. -}

-- | A convenience synonym for @`Nothing` :: `Maybe` `PixbufSaveFunc_WithClosures`@.
noPixbufSaveFunc_WithClosures :: Maybe PixbufSaveFunc_WithClosures
noPixbufSaveFunc_WithClosures = Nothing

-- | A simple wrapper that ignores the closure arguments.
drop_closures_PixbufSaveFunc :: PixbufSaveFunc -> PixbufSaveFunc_WithClosures
drop_closures_PixbufSaveFunc _f buf _ = _f buf

-- | Wrap the callback into a `GClosure`.
genClosure_PixbufSaveFunc :: MonadIO m => PixbufSaveFunc -> m (GClosure C_PixbufSaveFunc)
genClosure_PixbufSaveFunc cb = liftIO $ do
    let cb' = drop_closures_PixbufSaveFunc cb
    let cb'' = wrap_PixbufSaveFunc Nothing cb'
    mk_PixbufSaveFunc cb'' >>= B.GClosure.newGClosure


-- | Wrap a `PixbufSaveFunc` into a `C_PixbufSaveFunc`.
wrap_PixbufSaveFunc ::
    Maybe (Ptr (FunPtr C_PixbufSaveFunc)) ->
    PixbufSaveFunc_WithClosures ->
    C_PixbufSaveFunc
wrap_PixbufSaveFunc funptrptr _cb buf count error_ data_ = do
    buf' <- (unpackByteStringWithLength count) buf
    (result, outerror_) <- _cb  buf' data_
    outerror_' <- B.ManagedPtr.disownBoxed outerror_
    poke error_ outerror_'
    maybeReleaseFunPtr funptrptr
    let result' = (fromIntegral . fromEnum) result
    return result'


-- callback PixbufDestroyNotify
--          -> Callable {returnType = Nothing, returnMayBeNull = False, returnTransfer = TransferNothing, returnDocumentation = Documentation {rawDocText = Nothing, sinceVersion = Nothing}, args = [Arg {argCName = "pixels", argType = TCArray False (-1) (-1) (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "The pixel array of the pixbuf\n  that is being finalized.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "data", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "User closure data.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = 1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}], skipReturn = False, callableThrows = False, callableDeprecated = Nothing, callableDocumentation = Documentation {rawDocText = Just "A function of this type is responsible for freeing the pixel array\nof a pixbuf.  The gdk_pixbuf_new_from_data() function lets you\npass in a pre-allocated pixel array so that a pixbuf can be\ncreated from it; in this case you will need to pass in a function\nof #GdkPixbufDestroyNotify so that the pixel data can be freed\nwhen the pixbuf is finalized.", sinceVersion = Nothing}}
-- | Type for the callback on the (unwrapped) C side.
type C_PixbufDestroyNotify =
    Ptr Word8 ->
    Ptr () ->
    IO ()

-- Args : [Arg {argCName = "pixels", argType = TCArray False (-1) (-1) (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "The pixel array of the pixbuf\n  that is being finalized.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "data", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "User closure data.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = 1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "dynamic" __dynamic_C_PixbufDestroyNotify :: FunPtr C_PixbufDestroyNotify -> C_PixbufDestroyNotify

-- | Given a pointer to a foreign C function, wrap it into a function callable from Haskell.
dynamic_PixbufDestroyNotify ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    FunPtr C_PixbufDestroyNotify
    -> Ptr Word8
    {- ^ /@pixels@/: The pixel array of the pixbuf
  that is being finalized. -}
    -> Ptr ()
    {- ^ /@data@/: User closure data. -}
    -> m ()
dynamic_PixbufDestroyNotify __funPtr pixels data_ = liftIO $ do
    (__dynamic_C_PixbufDestroyNotify __funPtr) pixels data_
    return ()

-- | Generate a function pointer callable from C code, from a `C_PixbufDestroyNotify`.
foreign import ccall "wrapper"
    mk_PixbufDestroyNotify :: C_PixbufDestroyNotify -> IO (FunPtr C_PixbufDestroyNotify)

{- |
A function of this type is responsible for freeing the pixel array
of a pixbuf.  The 'GI.GdkPixbuf.Objects.Pixbuf.pixbufNewFromData' function lets you
pass in a pre-allocated pixel array so that a pixbuf can be
created from it; in this case you will need to pass in a function
of 'GI.GdkPixbuf.Callbacks.PixbufDestroyNotify' so that the pixel data can be freed
when the pixbuf is finalized.
-}
type PixbufDestroyNotify =
    Ptr Word8
    {- ^ /@pixels@/: The pixel array of the pixbuf
  that is being finalized. -}
    -> IO ()

-- | A convenience synonym for @`Nothing` :: `Maybe` `PixbufDestroyNotify`@.
noPixbufDestroyNotify :: Maybe PixbufDestroyNotify
noPixbufDestroyNotify = Nothing

{- |
A function of this type is responsible for freeing the pixel array
of a pixbuf.  The 'GI.GdkPixbuf.Objects.Pixbuf.pixbufNewFromData' function lets you
pass in a pre-allocated pixel array so that a pixbuf can be
created from it; in this case you will need to pass in a function
of 'GI.GdkPixbuf.Callbacks.PixbufDestroyNotify' so that the pixel data can be freed
when the pixbuf is finalized.
-}
type PixbufDestroyNotify_WithClosures =
    Ptr Word8
    {- ^ /@pixels@/: The pixel array of the pixbuf
  that is being finalized. -}
    -> Ptr ()
    {- ^ /@data@/: User closure data. -}
    -> IO ()

-- | A convenience synonym for @`Nothing` :: `Maybe` `PixbufDestroyNotify_WithClosures`@.
noPixbufDestroyNotify_WithClosures :: Maybe PixbufDestroyNotify_WithClosures
noPixbufDestroyNotify_WithClosures = Nothing

-- | A simple wrapper that ignores the closure arguments.
drop_closures_PixbufDestroyNotify :: PixbufDestroyNotify -> PixbufDestroyNotify_WithClosures
drop_closures_PixbufDestroyNotify _f pixels _ = _f pixels

-- | Wrap the callback into a `GClosure`.
genClosure_PixbufDestroyNotify :: MonadIO m => PixbufDestroyNotify -> m (GClosure C_PixbufDestroyNotify)
genClosure_PixbufDestroyNotify cb = liftIO $ do
    let cb' = drop_closures_PixbufDestroyNotify cb
    let cb'' = wrap_PixbufDestroyNotify Nothing cb'
    mk_PixbufDestroyNotify cb'' >>= B.GClosure.newGClosure


-- | Wrap a `PixbufDestroyNotify` into a `C_PixbufDestroyNotify`.
wrap_PixbufDestroyNotify ::
    Maybe (Ptr (FunPtr C_PixbufDestroyNotify)) ->
    PixbufDestroyNotify_WithClosures ->
    C_PixbufDestroyNotify
wrap_PixbufDestroyNotify funptrptr _cb pixels data_ = do
    _cb  pixels data_
    maybeReleaseFunPtr funptrptr