{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (inaki@blueleaf.cc)
-}

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

module GI.Handy.Functions
    (

 -- * Methods
-- ** enumValueRowName #method:enumValueRowName#

    enumValueRowName                        ,


-- ** init #method:init#

    init                                    ,


-- ** listBoxSeparatorHeader #method:listBoxSeparatorHeader#

    listBoxSeparatorHeader                  ,


-- ** stringUtf8Len #method:stringUtf8Len#

    stringUtf8Len                           ,


-- ** stringUtf8Truncate #method:stringUtf8Truncate#

    stringUtf8Truncate                      ,




    ) 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.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.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 qualified GI.GLib.Structs.String as GLib.String
import qualified GI.Gtk.Objects.ListBoxRow as Gtk.ListBoxRow
import {-# SOURCE #-} qualified GI.Handy.Objects.EnumValueObject as Handy.EnumValueObject

-- function hdy_string_utf8_truncate
-- Args : [Arg {argCName = "string", argType = TInterface (Name {namespace = "GLib", name = "String"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GString", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "len", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the new size of the string", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GLib", name = "String"}))
-- throws : False
-- Skip return : False

foreign import ccall "hdy_string_utf8_truncate" hdy_string_utf8_truncate ::
    Ptr GLib.String.String ->               -- string : TInterface (Name {namespace = "GLib", name = "String"})
    Word64 ->                               -- len : TBasicType TUInt64
    IO (Ptr GLib.String.String)

{- |
Cut of the end of the string /@string@/ so that /@len@/ utf8 characters remain
-}
stringUtf8Truncate ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    GLib.String.String
    {- ^ /@string@/: a 'GI.GLib.Structs.String.String' -}
    -> Word64
    {- ^ /@len@/: the new size of the string -}
    -> m GLib.String.String
    {- ^ __Returns:__ /@string@/ -}
stringUtf8Truncate string len = liftIO $ do
    string' <- unsafeManagedPtrGetPtr string
    result <- hdy_string_utf8_truncate string' len
    checkUnexpectedReturnNULL "stringUtf8Truncate" result
    result' <- (newBoxed GLib.String.String) result
    touchManagedPtr string
    return result'


-- function hdy_string_utf8_len
-- Args : [Arg {argCName = "string", argType = TInterface (Name {namespace = "GLib", name = "String"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GString", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TLong)
-- throws : False
-- Skip return : False

foreign import ccall "hdy_string_utf8_len" hdy_string_utf8_len ::
    Ptr GLib.String.String ->               -- string : TInterface (Name {namespace = "GLib", name = "String"})
    IO CLong

{- |
Computes the length of the string in utf-8 characters. See @/g_utf8_strlen/@.
-}
stringUtf8Len ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    GLib.String.String
    {- ^ /@string@/: a 'GI.GLib.Structs.String.String' -}
    -> m CLong
    {- ^ __Returns:__ the length of /@string@/ in characters -}
stringUtf8Len string = liftIO $ do
    string' <- unsafeManagedPtrGetPtr string
    result <- hdy_string_utf8_len string'
    touchManagedPtr string
    return result


-- function hdy_list_box_separator_header
-- Args : [Arg {argCName = "row", argType = TInterface (Name {namespace = "Gtk", name = "ListBoxRow"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the row to update", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "before", argType = TInterface (Name {namespace = "Gtk", name = "ListBoxRow"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "the row before @row, or %NULL if it is first", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "unused_user_data", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "unused user data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "hdy_list_box_separator_header" hdy_list_box_separator_header ::
    Ptr Gtk.ListBoxRow.ListBoxRow ->        -- row : TInterface (Name {namespace = "Gtk", name = "ListBoxRow"})
    Ptr Gtk.ListBoxRow.ListBoxRow ->        -- before : TInterface (Name {namespace = "Gtk", name = "ListBoxRow"})
    Ptr () ->                               -- unused_user_data : TBasicType TPtr
    IO ()

{- |
Separates rows by using 'GI.Gtk.Objects.Separator.Separator' as headers. The first row doesn\'t have
a separator as there is no row above it.

/Since: 0.0.6/
-}
listBoxSeparatorHeader ::
    (B.CallStack.HasCallStack, MonadIO m, Gtk.ListBoxRow.IsListBoxRow a, Gtk.ListBoxRow.IsListBoxRow b) =>
    a
    {- ^ /@row@/: the row to update -}
    -> Maybe (b)
    {- ^ /@before@/: the row before /@row@/, or 'Nothing' if it is first -}
    -> Ptr ()
    {- ^ /@unusedUserData@/: unused user data -}
    -> m ()
listBoxSeparatorHeader row before unusedUserData = liftIO $ do
    row' <- unsafeManagedPtrCastPtr row
    maybeBefore <- case before of
        Nothing -> return nullPtr
        Just jBefore -> do
            jBefore' <- unsafeManagedPtrCastPtr jBefore
            return jBefore'
    hdy_list_box_separator_header row' maybeBefore unusedUserData
    touchManagedPtr row
    whenJust before touchManagedPtr
    return ()


-- function hdy_init
-- Args : [Arg {argCName = "argc", argType = TBasicType TInt, direction = DirectionInout, mayBeNull = False, argDoc = Documentation {rawDocText = Just "Address of the <parameter>argc</parameter>\n    parameter of your main() function (or 0 if @argv is %NULL). This will be\n    changed if any arguments were handled.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything},Arg {argCName = "argv", argType = TCArray False (-1) 0 (TBasicType TUTF8), direction = DirectionInout, mayBeNull = True, argDoc = Documentation {rawDocText = Just "\n    Address of the <parameter>argv</parameter> parameter of main(), or %NULL.\n    Any options understood by Handy are stripped before return.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : [Arg {argCName = "argc", argType = TBasicType TInt, direction = DirectionInout, mayBeNull = False, argDoc = Documentation {rawDocText = Just "Address of the <parameter>argc</parameter>\n    parameter of your main() function (or 0 if @argv is %NULL). This will be\n    changed if any arguments were handled.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- returnType : Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "hdy_init" hdy_init ::
    Ptr Int32 ->                            -- argc : TBasicType TInt
    Ptr (Ptr CString) ->                    -- argv : TCArray False (-1) 0 (TBasicType TUTF8)
    IO CInt

{- |
Call this function before using any other Handy functions in your
GUI applications. If libhandy has already been initialized, the function will
simply return without processing the new arguments.
-}
init ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Maybe ([T.Text])
    {- ^ /@argv@/: 
    Address of the \<parameter>argv\<\/parameter> parameter of @/main()/@, or 'Nothing'.
    Any options understood by Handy are stripped before return. -}
    -> m ((Bool, Maybe [T.Text]))
    {- ^ __Returns:__ 'True' if initialization was successful, 'False' otherwise. -}
init argv = liftIO $ do
    let argc = case argv of
            Nothing -> 0
            Just jArgv -> fromIntegral $ length jArgv
    argc' <- allocMem :: IO (Ptr Int32)
    poke argc' argc
    maybeArgv <- case argv of
        Nothing -> return nullPtr
        Just jArgv -> do
            jArgv' <- packUTF8CArray jArgv
            return jArgv'
    maybeArgv' <- allocMem :: IO (Ptr (Ptr CString))
    poke maybeArgv' maybeArgv
    result <- hdy_init argc' maybeArgv'
    argc'' <- peek argc'
    let result' = (/= 0) result
    maybeArgv'' <- peek maybeArgv'
    maybeMaybeArgv'' <- convertIfNonNull maybeArgv'' $ \maybeArgv''' -> do
        maybeArgv'''' <- (unpackUTF8CArrayWithLength argc'') maybeArgv'''
        return maybeArgv''''
    freeMem argc'
    freeMem maybeArgv'
    return (result', maybeMaybeArgv'')


-- function hdy_enum_value_row_name
-- Args : [Arg {argCName = "value", argType = TInterface (Name {namespace = "Handy", name = "EnumValueObject"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the value from the enum from which to get a name", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "user_data", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "unused user data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TUTF8)
-- throws : False
-- Skip return : False

foreign import ccall "hdy_enum_value_row_name" hdy_enum_value_row_name ::
    Ptr Handy.EnumValueObject.EnumValueObject -> -- value : TInterface (Name {namespace = "Handy", name = "EnumValueObject"})
    Ptr () ->                               -- user_data : TBasicType TPtr
    IO CString

{- |
This is a default implementation of 'GI.Handy.Callbacks.ComboRowGetEnumValueNameFunc' to be
used with 'GI.Handy.Objects.ComboRow.comboRowSetForEnum'. If the enumeration has a nickname, it
will return it, otherwise it will return its name.

/Since: 0.0.6/
-}
enumValueRowName ::
    (B.CallStack.HasCallStack, MonadIO m, Handy.EnumValueObject.IsEnumValueObject a) =>
    a
    {- ^ /@value@/: the value from the enum from which to get a name -}
    -> Ptr ()
    {- ^ /@userData@/: unused user data -}
    -> m T.Text
    {- ^ __Returns:__ a newly allocated displayable name that represents /@value@/ -}
enumValueRowName value userData = liftIO $ do
    value' <- unsafeManagedPtrCastPtr value
    result <- hdy_enum_value_row_name value' userData
    checkUnexpectedReturnNULL "enumValueRowName" result
    result' <- cstringToText result
    freeMem result
    touchManagedPtr value
    return result'