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

An opaque struct representing an iterator which points to a
certain position in an animation.
-}

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

module GI.GdkPixbuf.Objects.PixbufAnimationIter
    (

-- * Exported types
    PixbufAnimationIter(..)                 ,
    IsPixbufAnimationIter                   ,
    toPixbufAnimationIter                   ,
    noPixbufAnimationIter                   ,


 -- * Methods
-- ** advance #method:advance#

#if ENABLE_OVERLOADING
    PixbufAnimationIterAdvanceMethodInfo    ,
#endif
    pixbufAnimationIterAdvance              ,


-- ** getDelayTime #method:getDelayTime#

#if ENABLE_OVERLOADING
    PixbufAnimationIterGetDelayTimeMethodInfo,
#endif
    pixbufAnimationIterGetDelayTime         ,


-- ** getPixbuf #method:getPixbuf#

#if ENABLE_OVERLOADING
    PixbufAnimationIterGetPixbufMethodInfo  ,
#endif
    pixbufAnimationIterGetPixbuf            ,


-- ** onCurrentlyLoadingFrame #method:onCurrentlyLoadingFrame#

#if ENABLE_OVERLOADING
    PixbufAnimationIterOnCurrentlyLoadingFrameMethodInfo,
#endif
    pixbufAnimationIterOnCurrentlyLoadingFrame,




    ) 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 qualified GI.GLib.Structs.TimeVal as GLib.TimeVal
import qualified GI.GObject.Objects.Object as GObject.Object
import {-# SOURCE #-} qualified GI.GdkPixbuf.Objects.Pixbuf as GdkPixbuf.Pixbuf

-- | Memory-managed wrapper type.
newtype PixbufAnimationIter = PixbufAnimationIter (ManagedPtr PixbufAnimationIter)
foreign import ccall "gdk_pixbuf_animation_iter_get_type"
    c_gdk_pixbuf_animation_iter_get_type :: IO GType

instance GObject PixbufAnimationIter where
    gobjectType _ = c_gdk_pixbuf_animation_iter_get_type


-- | Type class for types which can be safely cast to `PixbufAnimationIter`, for instance with `toPixbufAnimationIter`.
class GObject o => IsPixbufAnimationIter o
#if MIN_VERSION_base(4,9,0)
instance {-# OVERLAPPABLE #-} (GObject a, O.UnknownAncestorError PixbufAnimationIter a) =>
    IsPixbufAnimationIter a
#endif
instance IsPixbufAnimationIter PixbufAnimationIter
instance GObject.Object.IsObject PixbufAnimationIter

-- | Cast to `PixbufAnimationIter`, for types for which this is known to be safe. For general casts, use `Data.GI.Base.ManagedPtr.castTo`.
toPixbufAnimationIter :: (MonadIO m, IsPixbufAnimationIter o) => o -> m PixbufAnimationIter
toPixbufAnimationIter = liftIO . unsafeCastTo PixbufAnimationIter

-- | A convenience alias for `Nothing` :: `Maybe` `PixbufAnimationIter`.
noPixbufAnimationIter :: Maybe PixbufAnimationIter
noPixbufAnimationIter = Nothing

#if ENABLE_OVERLOADING
type family ResolvePixbufAnimationIterMethod (t :: Symbol) (o :: *) :: * where
    ResolvePixbufAnimationIterMethod "advance" o = PixbufAnimationIterAdvanceMethodInfo
    ResolvePixbufAnimationIterMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolvePixbufAnimationIterMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolvePixbufAnimationIterMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolvePixbufAnimationIterMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolvePixbufAnimationIterMethod "getv" o = GObject.Object.ObjectGetvMethodInfo
    ResolvePixbufAnimationIterMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolvePixbufAnimationIterMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolvePixbufAnimationIterMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolvePixbufAnimationIterMethod "onCurrentlyLoadingFrame" o = PixbufAnimationIterOnCurrentlyLoadingFrameMethodInfo
    ResolvePixbufAnimationIterMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolvePixbufAnimationIterMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolvePixbufAnimationIterMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolvePixbufAnimationIterMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolvePixbufAnimationIterMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolvePixbufAnimationIterMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolvePixbufAnimationIterMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolvePixbufAnimationIterMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolvePixbufAnimationIterMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolvePixbufAnimationIterMethod "getDelayTime" o = PixbufAnimationIterGetDelayTimeMethodInfo
    ResolvePixbufAnimationIterMethod "getPixbuf" o = PixbufAnimationIterGetPixbufMethodInfo
    ResolvePixbufAnimationIterMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolvePixbufAnimationIterMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolvePixbufAnimationIterMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolvePixbufAnimationIterMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolvePixbufAnimationIterMethod l o = O.MethodResolutionFailed l o

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

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

#if ENABLE_OVERLOADING
instance O.HasAttributeList PixbufAnimationIter
type instance O.AttributeList PixbufAnimationIter = PixbufAnimationIterAttributeList
type PixbufAnimationIterAttributeList = ('[ ] :: [(Symbol, *)])
#endif

#if ENABLE_OVERLOADING
#endif

#if ENABLE_OVERLOADING
type instance O.SignalList PixbufAnimationIter = PixbufAnimationIterSignalList
type PixbufAnimationIterSignalList = ('[ '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, *)])

#endif

-- method PixbufAnimationIter::advance
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "iter", argType = TInterface (Name {namespace = "GdkPixbuf", name = "PixbufAnimationIter"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GdkPixbufAnimationIter", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "current_time", argType = TInterface (Name {namespace = "GLib", name = "TimeVal"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "current time", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "gdk_pixbuf_animation_iter_advance" gdk_pixbuf_animation_iter_advance ::
    Ptr PixbufAnimationIter ->              -- iter : TInterface (Name {namespace = "GdkPixbuf", name = "PixbufAnimationIter"})
    Ptr GLib.TimeVal.TimeVal ->             -- current_time : TInterface (Name {namespace = "GLib", name = "TimeVal"})
    IO CInt

{- |
Possibly advances an animation to a new frame. Chooses the frame based
on the start time passed to 'GI.GdkPixbuf.Objects.PixbufAnimation.pixbufAnimationGetIter'.

/@currentTime@/ would normally come from 'GI.GLib.Functions.getCurrentTime', and
must be greater than or equal to the time passed to
'GI.GdkPixbuf.Objects.PixbufAnimation.pixbufAnimationGetIter', and must increase or remain
unchanged each time 'GI.GdkPixbuf.Objects.PixbufAnimationIter.pixbufAnimationIterGetPixbuf' is
called. That is, you can\'t go backward in time; animations only
play forward.

As a shortcut, pass 'Nothing' for the current time and 'GI.GLib.Functions.getCurrentTime'
will be invoked on your behalf. So you only need to explicitly pass
/@currentTime@/ if you\'re doing something odd like playing the animation
at double speed.

If this function returns 'False', there\'s no need to update the animation
display, assuming the display had been rendered prior to advancing;
if 'True', you need to call 'GI.GdkPixbuf.Objects.PixbufAnimationIter.pixbufAnimationIterGetPixbuf'
and update the display with the new pixbuf.
-}
pixbufAnimationIterAdvance ::
    (B.CallStack.HasCallStack, MonadIO m, IsPixbufAnimationIter a) =>
    a
    {- ^ /@iter@/: a 'GI.GdkPixbuf.Objects.PixbufAnimationIter.PixbufAnimationIter' -}
    -> Maybe (GLib.TimeVal.TimeVal)
    {- ^ /@currentTime@/: current time -}
    -> m Bool
    {- ^ __Returns:__ 'True' if the image may need updating -}
pixbufAnimationIterAdvance iter currentTime = liftIO $ do
    iter' <- unsafeManagedPtrCastPtr iter
    maybeCurrentTime <- case currentTime of
        Nothing -> return nullPtr
        Just jCurrentTime -> do
            jCurrentTime' <- unsafeManagedPtrGetPtr jCurrentTime
            return jCurrentTime'
    result <- gdk_pixbuf_animation_iter_advance iter' maybeCurrentTime
    let result' = (/= 0) result
    touchManagedPtr iter
    whenJust currentTime touchManagedPtr
    return result'

#if ENABLE_OVERLOADING
data PixbufAnimationIterAdvanceMethodInfo
instance (signature ~ (Maybe (GLib.TimeVal.TimeVal) -> m Bool), MonadIO m, IsPixbufAnimationIter a) => O.MethodInfo PixbufAnimationIterAdvanceMethodInfo a signature where
    overloadedMethod _ = pixbufAnimationIterAdvance

#endif

-- method PixbufAnimationIter::get_delay_time
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "iter", argType = TInterface (Name {namespace = "GdkPixbuf", name = "PixbufAnimationIter"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an animation iterator", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TInt)
-- throws : False
-- Skip return : False

foreign import ccall "gdk_pixbuf_animation_iter_get_delay_time" gdk_pixbuf_animation_iter_get_delay_time ::
    Ptr PixbufAnimationIter ->              -- iter : TInterface (Name {namespace = "GdkPixbuf", name = "PixbufAnimationIter"})
    IO Int32

{- |
Gets the number of milliseconds the current pixbuf should be displayed,
or -1 if the current pixbuf should be displayed forever. @/g_timeout_add()/@
conveniently takes a timeout in milliseconds, so you can use a timeout
to schedule the next update.

Note that some formats, like GIF, might clamp the timeout values in the
image file to avoid updates that are just too quick. The minimum timeout
for GIF images is currently 20 milliseconds.
-}
pixbufAnimationIterGetDelayTime ::
    (B.CallStack.HasCallStack, MonadIO m, IsPixbufAnimationIter a) =>
    a
    {- ^ /@iter@/: an animation iterator -}
    -> m Int32
    {- ^ __Returns:__ delay time in milliseconds (thousandths of a second) -}
pixbufAnimationIterGetDelayTime iter = liftIO $ do
    iter' <- unsafeManagedPtrCastPtr iter
    result <- gdk_pixbuf_animation_iter_get_delay_time iter'
    touchManagedPtr iter
    return result

#if ENABLE_OVERLOADING
data PixbufAnimationIterGetDelayTimeMethodInfo
instance (signature ~ (m Int32), MonadIO m, IsPixbufAnimationIter a) => O.MethodInfo PixbufAnimationIterGetDelayTimeMethodInfo a signature where
    overloadedMethod _ = pixbufAnimationIterGetDelayTime

#endif

-- method PixbufAnimationIter::get_pixbuf
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "iter", argType = TInterface (Name {namespace = "GdkPixbuf", name = "PixbufAnimationIter"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an animation iterator", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GdkPixbuf", name = "Pixbuf"}))
-- throws : False
-- Skip return : False

foreign import ccall "gdk_pixbuf_animation_iter_get_pixbuf" gdk_pixbuf_animation_iter_get_pixbuf ::
    Ptr PixbufAnimationIter ->              -- iter : TInterface (Name {namespace = "GdkPixbuf", name = "PixbufAnimationIter"})
    IO (Ptr GdkPixbuf.Pixbuf.Pixbuf)

{- |
Gets the current pixbuf which should be displayed; the pixbuf might not
be the same size as the animation itself
('GI.GdkPixbuf.Objects.PixbufAnimation.pixbufAnimationGetWidth', 'GI.GdkPixbuf.Objects.PixbufAnimation.pixbufAnimationGetHeight').
This pixbuf should be displayed for
'GI.GdkPixbuf.Objects.PixbufAnimationIter.pixbufAnimationIterGetDelayTime' milliseconds. The caller
of this function does not own a reference to the returned pixbuf;
the returned pixbuf will become invalid when the iterator advances
to the next frame, which may happen anytime you call
'GI.GdkPixbuf.Objects.PixbufAnimationIter.pixbufAnimationIterAdvance'. Copy the pixbuf to keep it
(don\'t just add a reference), as it may get recycled as you advance
the iterator.
-}
pixbufAnimationIterGetPixbuf ::
    (B.CallStack.HasCallStack, MonadIO m, IsPixbufAnimationIter a) =>
    a
    {- ^ /@iter@/: an animation iterator -}
    -> m GdkPixbuf.Pixbuf.Pixbuf
    {- ^ __Returns:__ the pixbuf to be displayed -}
pixbufAnimationIterGetPixbuf iter = liftIO $ do
    iter' <- unsafeManagedPtrCastPtr iter
    result <- gdk_pixbuf_animation_iter_get_pixbuf iter'
    checkUnexpectedReturnNULL "pixbufAnimationIterGetPixbuf" result
    result' <- (newObject GdkPixbuf.Pixbuf.Pixbuf) result
    touchManagedPtr iter
    return result'

#if ENABLE_OVERLOADING
data PixbufAnimationIterGetPixbufMethodInfo
instance (signature ~ (m GdkPixbuf.Pixbuf.Pixbuf), MonadIO m, IsPixbufAnimationIter a) => O.MethodInfo PixbufAnimationIterGetPixbufMethodInfo a signature where
    overloadedMethod _ = pixbufAnimationIterGetPixbuf

#endif

-- method PixbufAnimationIter::on_currently_loading_frame
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "iter", argType = TInterface (Name {namespace = "GdkPixbuf", name = "PixbufAnimationIter"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GdkPixbufAnimationIter", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "gdk_pixbuf_animation_iter_on_currently_loading_frame" gdk_pixbuf_animation_iter_on_currently_loading_frame ::
    Ptr PixbufAnimationIter ->              -- iter : TInterface (Name {namespace = "GdkPixbuf", name = "PixbufAnimationIter"})
    IO CInt

{- |
Used to determine how to respond to the area_updated signal on
'GI.GdkPixbuf.Objects.PixbufLoader.PixbufLoader' when loading an animation. area_updated is emitted
for an area of the frame currently streaming in to the loader. So if
you\'re on the currently loading frame, you need to redraw the screen for
the updated area.
-}
pixbufAnimationIterOnCurrentlyLoadingFrame ::
    (B.CallStack.HasCallStack, MonadIO m, IsPixbufAnimationIter a) =>
    a
    {- ^ /@iter@/: a 'GI.GdkPixbuf.Objects.PixbufAnimationIter.PixbufAnimationIter' -}
    -> m Bool
    {- ^ __Returns:__ 'True' if the frame we\'re on is partially loaded, or the last frame -}
pixbufAnimationIterOnCurrentlyLoadingFrame iter = liftIO $ do
    iter' <- unsafeManagedPtrCastPtr iter
    result <- gdk_pixbuf_animation_iter_on_currently_loading_frame iter'
    let result' = (/= 0) result
    touchManagedPtr iter
    return result'

#if ENABLE_OVERLOADING
data PixbufAnimationIterOnCurrentlyLoadingFrameMethodInfo
instance (signature ~ (m Bool), MonadIO m, IsPixbufAnimationIter a) => O.MethodInfo PixbufAnimationIterOnCurrentlyLoadingFrameMethodInfo a signature where
    overloadedMethod _ = pixbufAnimationIterOnCurrentlyLoadingFrame

#endif