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

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.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


-- 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 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

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_'')

foreign import ccall "wrapper"
    mk_PixbufSaveFunc :: C_PixbufSaveFunc -> IO (FunPtr C_PixbufSaveFunc)

type PixbufSaveFunc =
    ByteString ->
    IO ((Bool, GError))

noPixbufSaveFunc :: Maybe PixbufSaveFunc
noPixbufSaveFunc = Nothing

type PixbufSaveFunc_WithClosures =
    ByteString ->
    Ptr () ->
    IO ((Bool, GError))

noPixbufSaveFunc_WithClosures :: Maybe PixbufSaveFunc_WithClosures
noPixbufSaveFunc_WithClosures = Nothing

drop_closures_PixbufSaveFunc :: PixbufSaveFunc -> PixbufSaveFunc_WithClosures
drop_closures_PixbufSaveFunc _f buf _ = _f buf

genClosure_PixbufSaveFunc :: PixbufSaveFunc -> IO Closure
genClosure_PixbufSaveFunc cb = do
    let cb' = drop_closures_PixbufSaveFunc cb
    let cb'' = wrap_PixbufSaveFunc Nothing cb'
    mk_PixbufSaveFunc cb'' >>= newCClosure


wrap_PixbufSaveFunc ::
    Maybe (Ptr (FunPtr C_PixbufSaveFunc)) ->
    PixbufSaveFunc_WithClosures ->
    Ptr Word8 ->
    Word64 ->
    Ptr (Ptr GError) ->
    Ptr () ->
    IO CInt
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 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

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 ()

foreign import ccall "wrapper"
    mk_PixbufDestroyNotify :: C_PixbufDestroyNotify -> IO (FunPtr C_PixbufDestroyNotify)

type PixbufDestroyNotify =
    Ptr Word8 ->
    IO ()

noPixbufDestroyNotify :: Maybe PixbufDestroyNotify
noPixbufDestroyNotify = Nothing

type PixbufDestroyNotify_WithClosures =
    Ptr Word8 ->
    Ptr () ->
    IO ()

noPixbufDestroyNotify_WithClosures :: Maybe PixbufDestroyNotify_WithClosures
noPixbufDestroyNotify_WithClosures = Nothing

drop_closures_PixbufDestroyNotify :: PixbufDestroyNotify -> PixbufDestroyNotify_WithClosures
drop_closures_PixbufDestroyNotify _f pixels _ = _f pixels

genClosure_PixbufDestroyNotify :: PixbufDestroyNotify -> IO Closure
genClosure_PixbufDestroyNotify cb = do
    let cb' = drop_closures_PixbufDestroyNotify cb
    let cb'' = wrap_PixbufDestroyNotify Nothing cb'
    mk_PixbufDestroyNotify cb'' >>= newCClosure


wrap_PixbufDestroyNotify ::
    Maybe (Ptr (FunPtr C_PixbufDestroyNotify)) ->
    PixbufDestroyNotify_WithClosures ->
    Ptr Word8 ->
    Ptr () ->
    IO ()
wrap_PixbufDestroyNotify funptrptr _cb pixels data_ = do
    _cb  pixels data_
    maybeReleaseFunPtr funptrptr