{- |
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.Pango.Structs.ScriptIter.ScriptIter' is used to iterate through a string
and identify ranges in different scripts.
-}

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

module GI.Pango.Structs.ScriptIter
    (

-- * Exported types
    ScriptIter(..)                          ,
    noScriptIter                            ,


 -- * Methods
-- ** free #method:free#

#if ENABLE_OVERLOADING
    ScriptIterFreeMethodInfo                ,
#endif
    scriptIterFree                          ,


-- ** getRange #method:getRange#

#if ENABLE_OVERLOADING
    ScriptIterGetRangeMethodInfo            ,
#endif
    scriptIterGetRange                      ,


-- ** next #method:next#

#if ENABLE_OVERLOADING
    ScriptIterNextMethodInfo                ,
#endif
    scriptIterNext                          ,




    ) 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.Enums as Pango.Enums

-- | Memory-managed wrapper type.
newtype ScriptIter = ScriptIter (ManagedPtr ScriptIter)
-- XXX Wrapping a foreign struct/union with no known destructor or size, leak?
instance WrappedPtr ScriptIter where
    wrappedPtrCalloc = return nullPtr
    wrappedPtrCopy = return
    wrappedPtrFree = Nothing

-- | A convenience alias for `Nothing` :: `Maybe` `ScriptIter`.
noScriptIter :: Maybe ScriptIter
noScriptIter = Nothing


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

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

foreign import ccall "pango_script_iter_free" pango_script_iter_free ::
    Ptr ScriptIter ->                       -- iter : TInterface (Name {namespace = "Pango", name = "ScriptIter"})
    IO ()

{- |
Frees a 'GI.Pango.Structs.ScriptIter.ScriptIter' created with @/pango_script_iter_new()/@.

/Since: 1.4/
-}
scriptIterFree ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    ScriptIter
    {- ^ /@iter@/: a 'GI.Pango.Structs.ScriptIter.ScriptIter' -}
    -> m ()
scriptIterFree iter = liftIO $ do
    iter' <- unsafeManagedPtrGetPtr iter
    pango_script_iter_free iter'
    touchManagedPtr iter
    return ()

#if ENABLE_OVERLOADING
data ScriptIterFreeMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo ScriptIterFreeMethodInfo ScriptIter signature where
    overloadedMethod _ = scriptIterFree

#endif

-- method ScriptIter::get_range
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "iter", argType = TInterface (Name {namespace = "Pango", name = "ScriptIter"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PangoScriptIter", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "start", argType = TBasicType TUTF8, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "location to store start position of the range, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything},Arg {argCName = "end", argType = TBasicType TUTF8, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "location to store end position of the range, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything},Arg {argCName = "script", argType = TInterface (Name {namespace = "Pango", name = "Script"}), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "location to store script for range, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "pango_script_iter_get_range" pango_script_iter_get_range ::
    Ptr ScriptIter ->                       -- iter : TInterface (Name {namespace = "Pango", name = "ScriptIter"})
    Ptr CString ->                          -- start : TBasicType TUTF8
    Ptr CString ->                          -- end : TBasicType TUTF8
    Ptr CUInt ->                            -- script : TInterface (Name {namespace = "Pango", name = "Script"})
    IO ()

{- |
Gets information about the range to which /@iter@/ currently points.
The range is the set of locations p where *start \<= p \< *end.
(That is, it doesn\'t include the character stored at *end)

/Since: 1.4/
-}
scriptIterGetRange ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    ScriptIter
    {- ^ /@iter@/: a 'GI.Pango.Structs.ScriptIter.ScriptIter' -}
    -> m ((T.Text, T.Text, Pango.Enums.Script))
scriptIterGetRange iter = liftIO $ do
    iter' <- unsafeManagedPtrGetPtr iter
    start <- allocMem :: IO (Ptr CString)
    end <- allocMem :: IO (Ptr CString)
    script <- allocMem :: IO (Ptr CUInt)
    pango_script_iter_get_range iter' start end script
    start' <- peek start
    start'' <- cstringToText start'
    freeMem start'
    end' <- peek end
    end'' <- cstringToText end'
    freeMem end'
    script' <- peek script
    let script'' = (toEnum . fromIntegral) script'
    touchManagedPtr iter
    freeMem start
    freeMem end
    freeMem script
    return (start'', end'', script'')

#if ENABLE_OVERLOADING
data ScriptIterGetRangeMethodInfo
instance (signature ~ (m ((T.Text, T.Text, Pango.Enums.Script))), MonadIO m) => O.MethodInfo ScriptIterGetRangeMethodInfo ScriptIter signature where
    overloadedMethod _ = scriptIterGetRange

#endif

-- method ScriptIter::next
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "iter", argType = TInterface (Name {namespace = "Pango", name = "ScriptIter"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PangoScriptIter", 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 "pango_script_iter_next" pango_script_iter_next ::
    Ptr ScriptIter ->                       -- iter : TInterface (Name {namespace = "Pango", name = "ScriptIter"})
    IO CInt

{- |
Advances a 'GI.Pango.Structs.ScriptIter.ScriptIter' to the next range. If /@iter@/
is already at the end, it is left unchanged and 'False'
is returned.

/Since: 1.4/
-}
scriptIterNext ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    ScriptIter
    {- ^ /@iter@/: a 'GI.Pango.Structs.ScriptIter.ScriptIter' -}
    -> m Bool
    {- ^ __Returns:__ 'True' if /@iter@/ was successfully advanced. -}
scriptIterNext iter = liftIO $ do
    iter' <- unsafeManagedPtrGetPtr iter
    result <- pango_script_iter_next iter'
    let result' = (/= 0) result
    touchManagedPtr iter
    return result'

#if ENABLE_OVERLOADING
data ScriptIterNextMethodInfo
instance (signature ~ (m Bool), MonadIO m) => O.MethodInfo ScriptIterNextMethodInfo ScriptIter signature where
    overloadedMethod _ = scriptIterNext

#endif

#if ENABLE_OVERLOADING
type family ResolveScriptIterMethod (t :: Symbol) (o :: *) :: * where
    ResolveScriptIterMethod "free" o = ScriptIterFreeMethodInfo
    ResolveScriptIterMethod "next" o = ScriptIterNextMethodInfo
    ResolveScriptIterMethod "getRange" o = ScriptIterGetRangeMethodInfo
    ResolveScriptIterMethod l o = O.MethodResolutionFailed l o

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

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