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

The #PangoAttrIterator structure is used to represent an
iterator through a #PangoAttrList. A new iterator is created
with pango_attr_list_get_iterator(). Once the iterator
is created, it can be advanced through the style changes
in the text using pango_attr_iterator_next(). At each
style change, the range of the current style segment and the
attributes currently in effect can be queried.
-}

module GI.Pango.Structs.AttrIterator
    ( 

-- * Exported types
    AttrIterator(..)                        ,
    noAttrIterator                          ,


 -- * Methods
-- ** attrIteratorDestroy
    attrIteratorDestroy                     ,


-- ** attrIteratorGetAttrs
    attrIteratorGetAttrs                    ,


-- ** attrIteratorGetFont
    attrIteratorGetFont                     ,


-- ** attrIteratorNext
    attrIteratorNext                        ,


-- ** attrIteratorRange
    attrIteratorRange                       ,




    ) where

import Prelude ()
import Data.GI.Base.ShortPrelude

import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map

import GI.Pango.Types
import GI.Pango.Callbacks

newtype AttrIterator = AttrIterator (ForeignPtr AttrIterator)
noAttrIterator :: Maybe AttrIterator
noAttrIterator = Nothing

-- method AttrIterator::destroy
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Pango" "AttrIterator", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Pango" "AttrIterator", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "pango_attr_iterator_destroy" pango_attr_iterator_destroy :: 
    Ptr AttrIterator ->                     -- _obj : TInterface "Pango" "AttrIterator"
    IO ()


attrIteratorDestroy ::
    (MonadIO m) =>
    AttrIterator ->                         -- _obj
    m ()
attrIteratorDestroy _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    pango_attr_iterator_destroy _obj'
    touchManagedPtr _obj
    return ()

-- method AttrIterator::get_attrs
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Pango" "AttrIterator", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Pango" "AttrIterator", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TGSList (TInterface "Pango" "Attribute")
-- throws : False
-- Skip return : False

foreign import ccall "pango_attr_iterator_get_attrs" pango_attr_iterator_get_attrs :: 
    Ptr AttrIterator ->                     -- _obj : TInterface "Pango" "AttrIterator"
    IO (Ptr (GSList (Ptr Attribute)))


attrIteratorGetAttrs ::
    (MonadIO m) =>
    AttrIterator ->                         -- _obj
    m [Attribute]
attrIteratorGetAttrs _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    result <- pango_attr_iterator_get_attrs _obj'
    checkUnexpectedReturnNULL "pango_attr_iterator_get_attrs" result
    result' <- unpackGSList result
    result'' <- mapM (wrapPtr Attribute) result'
    g_slist_free result
    touchManagedPtr _obj
    return result''

-- method AttrIterator::get_font
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Pango" "AttrIterator", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "desc", argType = TInterface "Pango" "FontDescription", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "language", argType = TInterface "Pango" "Language", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "extra_attrs", argType = TGSList (TInterface "Pango" "Attribute"), direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Pango" "AttrIterator", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "desc", argType = TInterface "Pango" "FontDescription", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "language", argType = TInterface "Pango" "Language", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "extra_attrs", argType = TGSList (TInterface "Pango" "Attribute"), direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "pango_attr_iterator_get_font" pango_attr_iterator_get_font :: 
    Ptr AttrIterator ->                     -- _obj : TInterface "Pango" "AttrIterator"
    Ptr FontDescription ->                  -- desc : TInterface "Pango" "FontDescription"
    Ptr Language ->                         -- language : TInterface "Pango" "Language"
    Ptr (GSList (Ptr Attribute)) ->         -- extra_attrs : TGSList (TInterface "Pango" "Attribute")
    IO ()


attrIteratorGetFont ::
    (MonadIO m) =>
    AttrIterator ->                         -- _obj
    FontDescription ->                      -- desc
    Maybe (Language) ->                     -- language
    [Attribute] ->                          -- extra_attrs
    m ()
attrIteratorGetFont _obj desc language extra_attrs = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    let desc' = unsafeManagedPtrGetPtr desc
    maybeLanguage <- case language of
        Nothing -> return nullPtr
        Just jLanguage -> do
            let jLanguage' = unsafeManagedPtrGetPtr jLanguage
            return jLanguage'
    let extra_attrs' = map unsafeManagedPtrGetPtr extra_attrs
    extra_attrs'' <- packGSList extra_attrs'
    pango_attr_iterator_get_font _obj' desc' maybeLanguage extra_attrs''
    touchManagedPtr _obj
    touchManagedPtr desc
    whenJust language touchManagedPtr
    mapM_ touchManagedPtr extra_attrs
    return ()

-- method AttrIterator::next
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Pango" "AttrIterator", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Pango" "AttrIterator", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TBoolean
-- throws : False
-- Skip return : False

foreign import ccall "pango_attr_iterator_next" pango_attr_iterator_next :: 
    Ptr AttrIterator ->                     -- _obj : TInterface "Pango" "AttrIterator"
    IO CInt


attrIteratorNext ::
    (MonadIO m) =>
    AttrIterator ->                         -- _obj
    m Bool
attrIteratorNext _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    result <- pango_attr_iterator_next _obj'
    let result' = (/= 0) result
    touchManagedPtr _obj
    return result'

-- method AttrIterator::range
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Pango" "AttrIterator", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "start", argType = TBasicType TInt32, direction = DirectionOut, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything},Arg {argName = "end", argType = TBasicType TInt32, direction = DirectionOut, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Pango" "AttrIterator", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "pango_attr_iterator_range" pango_attr_iterator_range :: 
    Ptr AttrIterator ->                     -- _obj : TInterface "Pango" "AttrIterator"
    Ptr Int32 ->                            -- start : TBasicType TInt32
    Ptr Int32 ->                            -- end : TBasicType TInt32
    IO ()


attrIteratorRange ::
    (MonadIO m) =>
    AttrIterator ->                         -- _obj
    m (Int32,Int32)
attrIteratorRange _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    start <- allocMem :: IO (Ptr Int32)
    end <- allocMem :: IO (Ptr Int32)
    pango_attr_iterator_range _obj' start end
    start' <- peek start
    end' <- peek end
    touchManagedPtr _obj
    freeMem start
    freeMem end
    return (start', end')