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

The 'GI.Pango.Structs.Language.Language' structure is used to
represent a language.

'GI.Pango.Structs.Language.Language' pointers can be efficiently
copied and compared with each other.
-}

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

module GI.Pango.Structs.Language
    (

-- * Exported types
    Language(..)                            ,
    noLanguage                              ,


 -- * Methods
-- ** fromString #method:fromString#

    languageFromString                      ,


-- ** getDefault #method:getDefault#

    languageGetDefault                      ,


-- ** getSampleString #method:getSampleString#

#if ENABLE_OVERLOADING
    LanguageGetSampleStringMethodInfo       ,
#endif
    languageGetSampleString                 ,


-- ** includesScript #method:includesScript#

#if ENABLE_OVERLOADING
    LanguageIncludesScriptMethodInfo        ,
#endif
    languageIncludesScript                  ,


-- ** matches #method:matches#

#if ENABLE_OVERLOADING
    LanguageMatchesMethodInfo               ,
#endif
    languageMatches                         ,


-- ** toString #method:toString#

#if ENABLE_OVERLOADING
    LanguageToStringMethodInfo              ,
#endif
    languageToString                        ,




    ) 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 {-# SOURCE #-} qualified GI.Pango.Enums as Pango.Enums

-- | Memory-managed wrapper type.
newtype Language = Language (ManagedPtr Language)
foreign import ccall "pango_language_get_type" c_pango_language_get_type ::
    IO GType

instance BoxedObject Language where
    boxedType _ = c_pango_language_get_type

-- | A convenience alias for `Nothing` :: `Maybe` `Language`.
noLanguage :: Maybe Language
noLanguage = Nothing


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

-- method Language::get_sample_string
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "language", argType = TInterface (Name {namespace = "Pango", name = "Language"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PangoLanguage, or %NULL", 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 "pango_language_get_sample_string" pango_language_get_sample_string ::
    Ptr Language ->                         -- language : TInterface (Name {namespace = "Pango", name = "Language"})
    IO CString

{- |
Get a string that is representative of the characters needed to
render a particular language.

The sample text may be a pangram, but is not necessarily.  It is chosen to
be demonstrative of normal text in the language, as well as exposing font
feature requirements unique to the language.  It is suitable for use
as sample text in a font selection dialog.

If /@language@/ is 'Nothing', the default language as found by
'GI.Pango.Functions.languageGetDefault' is used.

If Pango does not have a sample string for /@language@/, the classic
\"The quick brown fox...\" is returned.  This can be detected by
comparing the returned pointer value to that returned for (non-existent)
language code \"xx\".  That is, compare to:
\<informalexample>\<programlisting>
pango_language_get_sample_string (pango_language_from_string (\"xx\"))
\<\/programlisting>\<\/informalexample>
-}
languageGetSampleString ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Language
    {- ^ /@language@/: a 'GI.Pango.Structs.Language.Language', or 'Nothing' -}
    -> m T.Text
    {- ^ __Returns:__ the sample string. This value is owned by Pango
  and should not be freed. -}
languageGetSampleString language = liftIO $ do
    language' <- unsafeManagedPtrGetPtr language
    result <- pango_language_get_sample_string language'
    checkUnexpectedReturnNULL "languageGetSampleString" result
    result' <- cstringToText result
    touchManagedPtr language
    return result'

#if ENABLE_OVERLOADING
data LanguageGetSampleStringMethodInfo
instance (signature ~ (m T.Text), MonadIO m) => O.MethodInfo LanguageGetSampleStringMethodInfo Language signature where
    overloadedMethod _ = languageGetSampleString

#endif

-- XXX Could not generate method Language::get_scripts
-- Error was : Not implemented: "Don't know how to allocate \"num_scripts\" of type TBasicType TInt"
-- method Language::includes_script
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "language", argType = TInterface (Name {namespace = "Pango", name = "Language"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PangoLanguage, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "script", argType = TInterface (Name {namespace = "Pango", name = "Script"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PangoScript", 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_language_includes_script" pango_language_includes_script ::
    Ptr Language ->                         -- language : TInterface (Name {namespace = "Pango", name = "Language"})
    CInt ->                                 -- script : TInterface (Name {namespace = "Pango", name = "Script"})
    IO CInt

{- |
Determines if /@script@/ is one of the scripts used to
write /@language@/. The returned value is conservative;
if nothing is known about the language tag /@language@/,
'True' will be returned, since, as far as Pango knows,
/@script@/ might be used to write /@language@/.

This routine is used in Pango\'s itemization process when
determining if a supplied language tag is relevant to
a particular section of text. It probably is not useful for
applications in most circumstances.

This function uses 'GI.Pango.Structs.Language.languageGetScripts' internally.

/Since: 1.4/
-}
languageIncludesScript ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Language
    {- ^ /@language@/: a 'GI.Pango.Structs.Language.Language', or 'Nothing' -}
    -> Pango.Enums.Script
    {- ^ /@script@/: a 'GI.Pango.Enums.Script' -}
    -> m Bool
    {- ^ __Returns:__ 'True' if /@script@/ is one of the scripts used
to write /@language@/ or if nothing is known about /@language@/
(including the case that /@language@/ is 'Nothing'),
'False' otherwise. -}
languageIncludesScript language script = liftIO $ do
    language' <- unsafeManagedPtrGetPtr language
    let script' = (fromIntegral . fromEnum) script
    result <- pango_language_includes_script language' script'
    let result' = (/= 0) result
    touchManagedPtr language
    return result'

#if ENABLE_OVERLOADING
data LanguageIncludesScriptMethodInfo
instance (signature ~ (Pango.Enums.Script -> m Bool), MonadIO m) => O.MethodInfo LanguageIncludesScriptMethodInfo Language signature where
    overloadedMethod _ = languageIncludesScript

#endif

-- method Language::matches
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "language", argType = TInterface (Name {namespace = "Pango", name = "Language"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a language tag (see pango_language_from_string()),\n           %NULL is allowed and matches nothing but '*'", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "range_list", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a list of language ranges, separated by ';', ':',\n  ',', or space characters.\n  Each element must either be '*', or a RFC 3066 language range\n  canonicalized as by pango_language_from_string()", 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_language_matches" pango_language_matches ::
    Ptr Language ->                         -- language : TInterface (Name {namespace = "Pango", name = "Language"})
    CString ->                              -- range_list : TBasicType TUTF8
    IO CInt

{- |
Checks if a language tag matches one of the elements in a list of
language ranges. A language tag is considered to match a range
in the list if the range is \'*\', the range is exactly the tag,
or the range is a prefix of the tag, and the character after it
in the tag is \'-\'.
-}
languageMatches ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Language
    {- ^ /@language@/: a language tag (see 'GI.Pango.Functions.languageFromString'),
           'Nothing' is allowed and matches nothing but \'*\' -}
    -> T.Text
    {- ^ /@rangeList@/: a list of language ranges, separated by \';\', \':\',
  \',\', or space characters.
  Each element must either be \'*\', or a RFC 3066 language range
  canonicalized as by 'GI.Pango.Functions.languageFromString' -}
    -> m Bool
    {- ^ __Returns:__ 'True' if a match was found. -}
languageMatches language rangeList = liftIO $ do
    language' <- unsafeManagedPtrGetPtr language
    rangeList' <- textToCString rangeList
    result <- pango_language_matches language' rangeList'
    let result' = (/= 0) result
    touchManagedPtr language
    freeMem rangeList'
    return result'

#if ENABLE_OVERLOADING
data LanguageMatchesMethodInfo
instance (signature ~ (T.Text -> m Bool), MonadIO m) => O.MethodInfo LanguageMatchesMethodInfo Language signature where
    overloadedMethod _ = languageMatches

#endif

-- method Language::to_string
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "language", argType = TInterface (Name {namespace = "Pango", name = "Language"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a language tag.", 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 "pango_language_to_string" pango_language_to_string ::
    Ptr Language ->                         -- language : TInterface (Name {namespace = "Pango", name = "Language"})
    IO CString

{- |
Gets the RFC-3066 format string representing the given language tag.
-}
languageToString ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Language
    {- ^ /@language@/: a language tag. -}
    -> m T.Text
    {- ^ __Returns:__ a string representing the language tag.  This is owned by
         Pango and should not be freed. -}
languageToString language = liftIO $ do
    language' <- unsafeManagedPtrGetPtr language
    result <- pango_language_to_string language'
    checkUnexpectedReturnNULL "languageToString" result
    result' <- cstringToText result
    touchManagedPtr language
    return result'

#if ENABLE_OVERLOADING
data LanguageToStringMethodInfo
instance (signature ~ (m T.Text), MonadIO m) => O.MethodInfo LanguageToStringMethodInfo Language signature where
    overloadedMethod _ = languageToString

#endif

-- method Language::from_string
-- method type : MemberFunction
-- Args : [Arg {argCName = "language", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a string representing a language tag, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Pango", name = "Language"}))
-- throws : False
-- Skip return : False

foreign import ccall "pango_language_from_string" pango_language_from_string ::
    CString ->                              -- language : TBasicType TUTF8
    IO (Ptr Language)

{- |
Take a RFC-3066 format language tag as a string and convert it to a
'GI.Pango.Structs.Language.Language' pointer that can be efficiently copied (copy the
pointer) and compared with other language tags (compare the
pointer.)

This function first canonicalizes the string by converting it to
lowercase, mapping \'_\' to \'-\', and stripping all characters other
than letters and \'-\'.

Use 'GI.Pango.Functions.languageGetDefault' if you want to get the 'GI.Pango.Structs.Language.Language' for
the current locale of the process.
-}
languageFromString ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Maybe (T.Text)
    {- ^ /@language@/: a string representing a language tag, or 'Nothing' -}
    -> m (Maybe Language)
    {- ^ __Returns:__ an opaque pointer to a
              'GI.Pango.Structs.Language.Language' structure, or 'Nothing' if /@language@/ was
              'Nothing'.  The returned pointer will be valid forever
              after, and should not be freed. -}
languageFromString language = liftIO $ do
    maybeLanguage <- case language of
        Nothing -> return nullPtr
        Just jLanguage -> do
            jLanguage' <- textToCString jLanguage
            return jLanguage'
    result <- pango_language_from_string maybeLanguage
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (newBoxed Language) result'
        return result''
    freeMem maybeLanguage
    return maybeResult

#if ENABLE_OVERLOADING
#endif

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

foreign import ccall "pango_language_get_default" pango_language_get_default ::
    IO (Ptr Language)

{- |
Returns the 'GI.Pango.Structs.Language.Language' for the current locale of the process.
Note that this can change over the life of an application.

On Unix systems, this is the return value is derived from
\<literal>setlocale(LC_CTYPE, NULL)\<\/literal>, and the user can
affect this through the environment variables LC_ALL, LC_CTYPE or
LANG (checked in that order). The locale string typically is in
the form lang_COUNTRY, where lang is an ISO-639 language code, and
COUNTRY is an ISO-3166 country code. For instance, sv_FI for
Swedish as written in Finland or pt_BR for Portuguese as written in
Brazil.

On Windows, the C library does not use any such environment
variables, and setting them won\'t affect the behavior of functions
like @/ctime()/@. The user sets the locale through the Regional Options
in the Control Panel. The C library (in the @/setlocale()/@ function)
does not use country and language codes, but country and language
names spelled out in English.
However, this function does check the above environment
variables, and does return a Unix-style locale string based on
either said environment variables or the thread\'s current locale.

Your application should call \<literal>setlocale(LC_ALL, \"\");\<\/literal>
for the user settings to take effect.  Gtk+ does this in its initialization
functions automatically (by calling @/gtk_set_locale()/@).
See \<literal>man setlocale\<\/literal> for more details.

/Since: 1.16/
-}
languageGetDefault ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    m Language
    {- ^ __Returns:__ the default language as a
              'GI.Pango.Structs.Language.Language', must not be freed. -}
languageGetDefault  = liftIO $ do
    result <- pango_language_get_default
    checkUnexpectedReturnNULL "languageGetDefault" result
    result' <- (newBoxed Language) result
    return result'

#if ENABLE_OVERLOADING
#endif

#if ENABLE_OVERLOADING
type family ResolveLanguageMethod (t :: Symbol) (o :: *) :: * where
    ResolveLanguageMethod "includesScript" o = LanguageIncludesScriptMethodInfo
    ResolveLanguageMethod "matches" o = LanguageMatchesMethodInfo
    ResolveLanguageMethod "toString" o = LanguageToStringMethodInfo
    ResolveLanguageMethod "getSampleString" o = LanguageGetSampleStringMethodInfo
    ResolveLanguageMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveLanguageMethod t Language, O.MethodInfo info Language p) => OL.IsLabel t (Language -> 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