{- |
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 'GI.Pango.Structs.Item.Item' structure stores information about a segment of text.
-}

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

module GI.Pango.Structs.Item
    (

-- * Exported types
    Item(..)                                ,
    newZeroItem                             ,
    noItem                                  ,


 -- * Methods
-- ** copy #method:copy#

#if ENABLE_OVERLOADING
    ItemCopyMethodInfo                      ,
#endif
    itemCopy                                ,


-- ** free #method:free#

#if ENABLE_OVERLOADING
    ItemFreeMethodInfo                      ,
#endif
    itemFree                                ,


-- ** new #method:new#

    itemNew                                 ,


-- ** split #method:split#

#if ENABLE_OVERLOADING
    ItemSplitMethodInfo                     ,
#endif
    itemSplit                               ,




 -- * Properties
-- ** analysis #attr:analysis#
{- | analysis results for the item.
-}
    getItemAnalysis                         ,
#if ENABLE_OVERLOADING
    item_analysis                           ,
#endif


-- ** length #attr:length#
{- | length of this item in bytes.
-}
    getItemLength                           ,
#if ENABLE_OVERLOADING
    item_length                             ,
#endif
    setItemLength                           ,


-- ** numChars #attr:numChars#
{- | number of Unicode characters in the item.
-}
    getItemNumChars                         ,
#if ENABLE_OVERLOADING
    item_numChars                           ,
#endif
    setItemNumChars                         ,


-- ** offset #attr:offset#
{- | byte offset of the start of this item in text.
-}
    getItemOffset                           ,
#if ENABLE_OVERLOADING
    item_offset                             ,
#endif
    setItemOffset                           ,




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

-- | Memory-managed wrapper type.
newtype Item = Item (ManagedPtr Item)
foreign import ccall "pango_item_get_type" c_pango_item_get_type ::
    IO GType

instance BoxedObject Item where
    boxedType _ = c_pango_item_get_type

-- | Construct a `Item` struct initialized to zero.
newZeroItem :: MonadIO m => m Item
newZeroItem = liftIO $ callocBoxedBytes 64 >>= wrapBoxed Item

instance tag ~ 'AttrSet => Constructible Item tag where
    new _ attrs = do
        o <- newZeroItem
        GI.Attributes.set o attrs
        return o


-- | A convenience alias for `Nothing` :: `Maybe` `Item`.
noItem :: Maybe Item
noItem = Nothing

{- |
Get the value of the “@offset@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' item #offset
@
-}
getItemOffset :: MonadIO m => Item -> m Int32
getItemOffset s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 0) :: IO Int32
    return val

{- |
Set the value of the “@offset@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' item [ #offset 'Data.GI.Base.Attributes.:=' value ]
@
-}
setItemOffset :: MonadIO m => Item -> Int32 -> m ()
setItemOffset s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 0) (val :: Int32)

#if ENABLE_OVERLOADING
data ItemOffsetFieldInfo
instance AttrInfo ItemOffsetFieldInfo where
    type AttrAllowedOps ItemOffsetFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint ItemOffsetFieldInfo = (~) Int32
    type AttrBaseTypeConstraint ItemOffsetFieldInfo = (~) Item
    type AttrGetType ItemOffsetFieldInfo = Int32
    type AttrLabel ItemOffsetFieldInfo = "offset"
    type AttrOrigin ItemOffsetFieldInfo = Item
    attrGet _ = getItemOffset
    attrSet _ = setItemOffset
    attrConstruct = undefined
    attrClear _ = undefined

item_offset :: AttrLabelProxy "offset"
item_offset = AttrLabelProxy

#endif


{- |
Get the value of the “@length@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' item #length
@
-}
getItemLength :: MonadIO m => Item -> m Int32
getItemLength s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 4) :: IO Int32
    return val

{- |
Set the value of the “@length@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' item [ #length 'Data.GI.Base.Attributes.:=' value ]
@
-}
setItemLength :: MonadIO m => Item -> Int32 -> m ()
setItemLength s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 4) (val :: Int32)

#if ENABLE_OVERLOADING
data ItemLengthFieldInfo
instance AttrInfo ItemLengthFieldInfo where
    type AttrAllowedOps ItemLengthFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint ItemLengthFieldInfo = (~) Int32
    type AttrBaseTypeConstraint ItemLengthFieldInfo = (~) Item
    type AttrGetType ItemLengthFieldInfo = Int32
    type AttrLabel ItemLengthFieldInfo = "length"
    type AttrOrigin ItemLengthFieldInfo = Item
    attrGet _ = getItemLength
    attrSet _ = setItemLength
    attrConstruct = undefined
    attrClear _ = undefined

item_length :: AttrLabelProxy "length"
item_length = AttrLabelProxy

#endif


{- |
Get the value of the “@num_chars@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' item #numChars
@
-}
getItemNumChars :: MonadIO m => Item -> m Int32
getItemNumChars s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 8) :: IO Int32
    return val

{- |
Set the value of the “@num_chars@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' item [ #numChars 'Data.GI.Base.Attributes.:=' value ]
@
-}
setItemNumChars :: MonadIO m => Item -> Int32 -> m ()
setItemNumChars s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 8) (val :: Int32)

#if ENABLE_OVERLOADING
data ItemNumCharsFieldInfo
instance AttrInfo ItemNumCharsFieldInfo where
    type AttrAllowedOps ItemNumCharsFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint ItemNumCharsFieldInfo = (~) Int32
    type AttrBaseTypeConstraint ItemNumCharsFieldInfo = (~) Item
    type AttrGetType ItemNumCharsFieldInfo = Int32
    type AttrLabel ItemNumCharsFieldInfo = "num_chars"
    type AttrOrigin ItemNumCharsFieldInfo = Item
    attrGet _ = getItemNumChars
    attrSet _ = setItemNumChars
    attrConstruct = undefined
    attrClear _ = undefined

item_numChars :: AttrLabelProxy "numChars"
item_numChars = AttrLabelProxy

#endif


{- |
Get the value of the “@analysis@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' item #analysis
@
-}
getItemAnalysis :: MonadIO m => Item -> m Pango.Analysis.Analysis
getItemAnalysis s = liftIO $ withManagedPtr s $ \ptr -> do
    let val = ptr `plusPtr` 16 :: (Ptr Pango.Analysis.Analysis)
    val' <- (newPtr Pango.Analysis.Analysis) val
    return val'

#if ENABLE_OVERLOADING
data ItemAnalysisFieldInfo
instance AttrInfo ItemAnalysisFieldInfo where
    type AttrAllowedOps ItemAnalysisFieldInfo = '[ 'AttrGet]
    type AttrSetTypeConstraint ItemAnalysisFieldInfo = (~) (Ptr Pango.Analysis.Analysis)
    type AttrBaseTypeConstraint ItemAnalysisFieldInfo = (~) Item
    type AttrGetType ItemAnalysisFieldInfo = Pango.Analysis.Analysis
    type AttrLabel ItemAnalysisFieldInfo = "analysis"
    type AttrOrigin ItemAnalysisFieldInfo = Item
    attrGet _ = getItemAnalysis
    attrSet _ = undefined
    attrConstruct = undefined
    attrClear _ = undefined

item_analysis :: AttrLabelProxy "analysis"
item_analysis = AttrLabelProxy

#endif



#if ENABLE_OVERLOADING
instance O.HasAttributeList Item
type instance O.AttributeList Item = ItemAttributeList
type ItemAttributeList = ('[ '("offset", ItemOffsetFieldInfo), '("length", ItemLengthFieldInfo), '("numChars", ItemNumCharsFieldInfo), '("analysis", ItemAnalysisFieldInfo)] :: [(Symbol, *)])
#endif

-- method Item::new
-- method type : Constructor
-- Args : []
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Pango", name = "Item"}))
-- throws : False
-- Skip return : False

foreign import ccall "pango_item_new" pango_item_new ::
    IO (Ptr Item)

{- |
Creates a new 'GI.Pango.Structs.Item.Item' structure initialized to default values.
-}
itemNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    m Item
    {- ^ __Returns:__ the newly allocated 'GI.Pango.Structs.Item.Item', which should
              be freed with 'GI.Pango.Structs.Item.itemFree'. -}
itemNew  = liftIO $ do
    result <- pango_item_new
    checkUnexpectedReturnNULL "itemNew" result
    result' <- (wrapBoxed Item) result
    return result'

#if ENABLE_OVERLOADING
#endif

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

foreign import ccall "pango_item_copy" pango_item_copy ::
    Ptr Item ->                             -- item : TInterface (Name {namespace = "Pango", name = "Item"})
    IO (Ptr Item)

{- |
Copy an existing 'GI.Pango.Structs.Item.Item' structure.
-}
itemCopy ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Item
    {- ^ /@item@/: a 'GI.Pango.Structs.Item.Item', may be 'Nothing' -}
    -> m (Maybe Item)
    {- ^ __Returns:__ the newly allocated 'GI.Pango.Structs.Item.Item', which
              should be freed with 'GI.Pango.Structs.Item.itemFree', or 'Nothing' if
              /@item@/ was 'Nothing'. -}
itemCopy item = liftIO $ do
    item' <- unsafeManagedPtrGetPtr item
    result <- pango_item_copy item'
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (wrapBoxed Item) result'
        return result''
    touchManagedPtr item
    return maybeResult

#if ENABLE_OVERLOADING
data ItemCopyMethodInfo
instance (signature ~ (m (Maybe Item)), MonadIO m) => O.MethodInfo ItemCopyMethodInfo Item signature where
    overloadedMethod _ = itemCopy

#endif

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

foreign import ccall "pango_item_free" pango_item_free ::
    Ptr Item ->                             -- item : TInterface (Name {namespace = "Pango", name = "Item"})
    IO ()

{- |
Free a 'GI.Pango.Structs.Item.Item' and all associated memory.
-}
itemFree ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Item
    {- ^ /@item@/: a 'GI.Pango.Structs.Item.Item', may be 'Nothing' -}
    -> m ()
itemFree item = liftIO $ do
    item' <- unsafeManagedPtrGetPtr item
    pango_item_free item'
    touchManagedPtr item
    return ()

#if ENABLE_OVERLOADING
data ItemFreeMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo ItemFreeMethodInfo Item signature where
    overloadedMethod _ = itemFree

#endif

-- method Item::split
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "orig", argType = TInterface (Name {namespace = "Pango", name = "Item"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PangoItem", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "split_index", argType = TBasicType TInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "byte index of position to split item, relative to the start of the item", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "split_offset", argType = TBasicType TInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "number of chars between start of @orig and @split_index", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Pango", name = "Item"}))
-- throws : False
-- Skip return : False

foreign import ccall "pango_item_split" pango_item_split ::
    Ptr Item ->                             -- orig : TInterface (Name {namespace = "Pango", name = "Item"})
    Int32 ->                                -- split_index : TBasicType TInt
    Int32 ->                                -- split_offset : TBasicType TInt
    IO (Ptr Item)

{- |
Modifies /@orig@/ to cover only the text after /@splitIndex@/, and
returns a new item that covers the text before /@splitIndex@/ that
used to be in /@orig@/. You can think of /@splitIndex@/ as the length of
the returned item. /@splitIndex@/ may not be 0, and it may not be
greater than or equal to the length of /@orig@/ (that is, there must
be at least one byte assigned to each item, you can\'t create a
zero-length item). /@splitOffset@/ is the length of the first item in
chars, and must be provided because the text used to generate the
item isn\'t available, so 'GI.Pango.Structs.Item.itemSplit' can\'t count the char
length of the split items itself.
-}
itemSplit ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Item
    {- ^ /@orig@/: a 'GI.Pango.Structs.Item.Item' -}
    -> Int32
    {- ^ /@splitIndex@/: byte index of position to split item, relative to the start of the item -}
    -> Int32
    {- ^ /@splitOffset@/: number of chars between start of /@orig@/ and /@splitIndex@/ -}
    -> m Item
    {- ^ __Returns:__ new item representing text before /@splitIndex@/, which
              should be freed with 'GI.Pango.Structs.Item.itemFree'. -}
itemSplit orig splitIndex splitOffset = liftIO $ do
    orig' <- unsafeManagedPtrGetPtr orig
    result <- pango_item_split orig' splitIndex splitOffset
    checkUnexpectedReturnNULL "itemSplit" result
    result' <- (wrapBoxed Item) result
    touchManagedPtr orig
    return result'

#if ENABLE_OVERLOADING
data ItemSplitMethodInfo
instance (signature ~ (Int32 -> Int32 -> m Item), MonadIO m) => O.MethodInfo ItemSplitMethodInfo Item signature where
    overloadedMethod _ = itemSplit

#endif

#if ENABLE_OVERLOADING
type family ResolveItemMethod (t :: Symbol) (o :: *) :: * where
    ResolveItemMethod "copy" o = ItemCopyMethodInfo
    ResolveItemMethod "free" o = ItemFreeMethodInfo
    ResolveItemMethod "split" o = ItemSplitMethodInfo
    ResolveItemMethod l o = O.MethodResolutionFailed l o

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

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