{-# LANGUAGE TypeApplications #-}


-- | Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria
-- 
-- A t'GI.Pango.Structs.ScriptIter.ScriptIter' is used to iterate through a string
-- and identify ranges in different scripts.

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

module GI.Pango.Structs.ScriptIter
    ( 

-- * Exported types
    ScriptIter(..)                          ,


 -- * Methods
-- ** Overloaded methods #method:Overloaded methods#

#if defined(ENABLE_OVERLOADING)
    ResolveScriptIterMethod                 ,
#endif


-- ** free #method:free#

#if defined(ENABLE_OVERLOADING)
    ScriptIterFreeMethodInfo                ,
#endif
    scriptIterFree                          ,


-- ** getRange #method:getRange#

#if defined(ENABLE_OVERLOADING)
    ScriptIterGetRangeMethodInfo            ,
#endif
    scriptIterGetRange                      ,


-- ** new #method:new#

    scriptIterNew                           ,


-- ** next #method:next#

#if defined(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.BasicTypes as B.Types
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.GI.Base.Signals as B.Signals
import qualified Control.Monad.IO.Class as MIO
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

import {-# SOURCE #-} qualified GI.Pango.Enums as Pango.Enums

-- | Memory-managed wrapper type.
newtype ScriptIter = ScriptIter (SP.ManagedPtr ScriptIter)
    deriving (ScriptIter -> ScriptIter -> Bool
(ScriptIter -> ScriptIter -> Bool)
-> (ScriptIter -> ScriptIter -> Bool) -> Eq ScriptIter
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ScriptIter -> ScriptIter -> Bool
$c/= :: ScriptIter -> ScriptIter -> Bool
== :: ScriptIter -> ScriptIter -> Bool
$c== :: ScriptIter -> ScriptIter -> Bool
Eq)

instance SP.ManagedPtrNewtype ScriptIter where
    toManagedPtr :: ScriptIter -> ManagedPtr ScriptIter
toManagedPtr (ScriptIter ManagedPtr ScriptIter
p) = ManagedPtr ScriptIter
p

foreign import ccall "pango_script_iter_get_type" c_pango_script_iter_get_type :: 
    IO GType

type instance O.ParentTypes ScriptIter = '[]
instance O.HasParentTypes ScriptIter

instance B.Types.TypedObject ScriptIter where
    glibType :: IO GType
glibType = IO GType
c_pango_script_iter_get_type

instance B.Types.GBoxed ScriptIter

-- | Convert 'ScriptIter' to and from 'Data.GI.Base.GValue.GValue' with 'Data.GI.Base.GValue.toGValue' and 'Data.GI.Base.GValue.fromGValue'.
instance B.GValue.IsGValue ScriptIter where
    toGValue :: ScriptIter -> IO GValue
toGValue ScriptIter
o = do
        GType
gtype <- IO GType
c_pango_script_iter_get_type
        ScriptIter -> (Ptr ScriptIter -> IO GValue) -> IO GValue
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
B.ManagedPtr.withManagedPtr ScriptIter
o (GType
-> (GValue -> Ptr ScriptIter -> IO ())
-> Ptr ScriptIter
-> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
B.GValue.buildGValue GType
gtype GValue -> Ptr ScriptIter -> IO ()
forall a. GValue -> Ptr a -> IO ()
B.GValue.set_boxed)
        
    fromGValue :: GValue -> IO ScriptIter
fromGValue GValue
gv = do
        Ptr ScriptIter
ptr <- GValue -> IO (Ptr ScriptIter)
forall b. GValue -> IO (Ptr b)
B.GValue.get_boxed GValue
gv :: IO (Ptr ScriptIter)
        (ManagedPtr ScriptIter -> ScriptIter)
-> Ptr ScriptIter -> IO ScriptIter
forall a.
(HasCallStack, GBoxed a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
B.ManagedPtr.newBoxed ManagedPtr ScriptIter -> ScriptIter
ScriptIter Ptr ScriptIter
ptr
        
    


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

-- method ScriptIter::new
-- method type : Constructor
-- Args: [ Arg
--           { argCName = "text"
--           , argType = TBasicType TUTF8
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "a UTF-8 string" , sinceVersion = Nothing }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       , Arg
--           { argCName = "length"
--           , argType = TBasicType TInt
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText =
--                     Just "length of @text, or -1 if @text is nul-terminated."
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       ]
-- Lengths: []
-- returnType: Just
--               (TInterface Name { namespace = "Pango" , name = "ScriptIter" })
-- throws : False
-- Skip return : False

foreign import ccall "pango_script_iter_new" pango_script_iter_new :: 
    CString ->                              -- text : TBasicType TUTF8
    Int32 ->                                -- length : TBasicType TInt
    IO (Ptr ScriptIter)

-- | Create a new t'GI.Pango.Structs.ScriptIter.ScriptIter', used to break a string of
-- Unicode text into runs by Unicode script. No copy is made of
-- /@text@/, so the caller needs to make sure it remains valid until
-- the iterator is freed with 'GI.Pango.Structs.ScriptIter.scriptIterFree'.
-- 
-- /Since: 1.4/
scriptIterNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    T.Text
    -- ^ /@text@/: a UTF-8 string
    -> Int32
    -- ^ /@length@/: length of /@text@/, or -1 if /@text@/ is nul-terminated.
    -> m ScriptIter
    -- ^ __Returns:__ the new script iterator, initialized
    --  to point at the first range in the text, which should be
    --  freed with 'GI.Pango.Structs.ScriptIter.scriptIterFree'. If the string is
    --  empty, it will point at an empty range.
scriptIterNew :: Text -> Int32 -> m ScriptIter
scriptIterNew Text
text Int32
length_ = IO ScriptIter -> m ScriptIter
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ScriptIter -> m ScriptIter) -> IO ScriptIter -> m ScriptIter
forall a b. (a -> b) -> a -> b
$ do
    CString
text' <- Text -> IO CString
textToCString Text
text
    Ptr ScriptIter
result <- CString -> Int32 -> IO (Ptr ScriptIter)
pango_script_iter_new CString
text' Int32
length_
    Text -> Ptr ScriptIter -> IO ()
forall a. HasCallStack => Text -> Ptr a -> IO ()
checkUnexpectedReturnNULL Text
"scriptIterNew" Ptr ScriptIter
result
    ScriptIter
result' <- ((ManagedPtr ScriptIter -> ScriptIter)
-> Ptr ScriptIter -> IO ScriptIter
forall a.
(HasCallStack, GBoxed a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
wrapBoxed ManagedPtr ScriptIter -> ScriptIter
ScriptIter) Ptr ScriptIter
result
    CString -> IO ()
forall a. Ptr a -> IO ()
freeMem CString
text'
    ScriptIter -> IO ScriptIter
forall (m :: * -> *) a. Monad m => a -> m a
return ScriptIter
result'

#if defined(ENABLE_OVERLOADING)
#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 t'GI.Pango.Structs.ScriptIter.ScriptIter' created with 'GI.Pango.Structs.ScriptIter.scriptIterNew'.
-- 
-- /Since: 1.4/
scriptIterFree ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    ScriptIter
    -- ^ /@iter@/: a t'GI.Pango.Structs.ScriptIter.ScriptIter'
    -> m ()
scriptIterFree :: ScriptIter -> m ()
scriptIterFree ScriptIter
iter = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
    Ptr ScriptIter
iter' <- ScriptIter -> IO (Ptr ScriptIter)
forall a. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr a)
unsafeManagedPtrGetPtr ScriptIter
iter
    Ptr ScriptIter -> IO ()
pango_script_iter_free Ptr ScriptIter
iter'
    ScriptIter -> IO ()
forall a. ManagedPtrNewtype a => a -> IO ()
touchManagedPtr ScriptIter
iter
    () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

#if defined(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 CInt ->                             -- 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)
-- 
-- Note that while the type of the /@script@/ argument is declared
-- as PangoScript, as of Pango 1.18, this function simply returns
-- GUnicodeScript values. Callers must be prepared to handle unknown
-- values.
-- 
-- /Since: 1.4/
scriptIterGetRange ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    ScriptIter
    -- ^ /@iter@/: a t'GI.Pango.Structs.ScriptIter.ScriptIter'
    -> m ((T.Text, T.Text, Pango.Enums.Script))
scriptIterGetRange :: ScriptIter -> m (Text, Text, Script)
scriptIterGetRange ScriptIter
iter = IO (Text, Text, Script) -> m (Text, Text, Script)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Text, Text, Script) -> m (Text, Text, Script))
-> IO (Text, Text, Script) -> m (Text, Text, Script)
forall a b. (a -> b) -> a -> b
$ do
    Ptr ScriptIter
iter' <- ScriptIter -> IO (Ptr ScriptIter)
forall a. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr a)
unsafeManagedPtrGetPtr ScriptIter
iter
    Ptr CString
start <- IO (Ptr CString)
forall a. Storable a => IO (Ptr a)
callocMem :: IO (Ptr CString)
    Ptr CString
end <- IO (Ptr CString)
forall a. Storable a => IO (Ptr a)
callocMem :: IO (Ptr CString)
    Ptr CInt
script <- IO (Ptr CInt)
forall a. Storable a => IO (Ptr a)
allocMem :: IO (Ptr CInt)
    Ptr ScriptIter -> Ptr CString -> Ptr CString -> Ptr CInt -> IO ()
pango_script_iter_get_range Ptr ScriptIter
iter' Ptr CString
start Ptr CString
end Ptr CInt
script
    CString
start' <- Ptr CString -> IO CString
forall a. Storable a => Ptr a -> IO a
peek Ptr CString
start
    Text
start'' <- HasCallStack => CString -> IO Text
CString -> IO Text
cstringToText CString
start'
    CString -> IO ()
forall a. Ptr a -> IO ()
freeMem CString
start'
    CString
end' <- Ptr CString -> IO CString
forall a. Storable a => Ptr a -> IO a
peek Ptr CString
end
    Text
end'' <- HasCallStack => CString -> IO Text
CString -> IO Text
cstringToText CString
end'
    CString -> IO ()
forall a. Ptr a -> IO ()
freeMem CString
end'
    CInt
script' <- Ptr CInt -> IO CInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CInt
script
    let script'' :: Script
script'' = (Int -> Script
forall a. Enum a => Int -> a
toEnum (Int -> Script) -> (CInt -> Int) -> CInt -> Script
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral) CInt
script'
    ScriptIter -> IO ()
forall a. ManagedPtrNewtype a => a -> IO ()
touchManagedPtr ScriptIter
iter
    Ptr CString -> IO ()
forall a. Ptr a -> IO ()
freeMem Ptr CString
start
    Ptr CString -> IO ()
forall a. Ptr a -> IO ()
freeMem Ptr CString
end
    Ptr CInt -> IO ()
forall a. Ptr a -> IO ()
freeMem Ptr CInt
script
    (Text, Text, Script) -> IO (Text, Text, Script)
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
start'', Text
end'', Script
script'')

#if defined(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 t'GI.Pango.Structs.ScriptIter.ScriptIter' to the next range. If /@iter@/
-- is already at the end, it is left unchanged and 'P.False'
-- is returned.
-- 
-- /Since: 1.4/
scriptIterNext ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    ScriptIter
    -- ^ /@iter@/: a t'GI.Pango.Structs.ScriptIter.ScriptIter'
    -> m Bool
    -- ^ __Returns:__ 'P.True' if /@iter@/ was successfully advanced.
scriptIterNext :: ScriptIter -> m Bool
scriptIterNext ScriptIter
iter = IO Bool -> m Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ do
    Ptr ScriptIter
iter' <- ScriptIter -> IO (Ptr ScriptIter)
forall a. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr a)
unsafeManagedPtrGetPtr ScriptIter
iter
    CInt
result <- Ptr ScriptIter -> IO CInt
pango_script_iter_next Ptr ScriptIter
iter'
    let result' :: Bool
result' = (CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
0) CInt
result
    ScriptIter -> IO ()
forall a. ManagedPtrNewtype a => a -> IO ()
touchManagedPtr ScriptIter
iter
    Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
result'

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

#endif

#if defined(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) => OL.IsLabel t (ScriptIter -> p) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.overloadedMethod @info
#else
    fromLabel _ = O.overloadedMethod @info
#endif

#endif