{-# LANGUAGE ScopedTypeVariables #-}

{-# LINE 2 "./Graphics/UI/Gtk/Misc/IMContextSimple.chs" #-}
-- -*-haskell-*-
-- GIMP Toolkit (GTK) Widget IMContextSimple
--
-- Author : Andy Stewart
--
-- Created: 24 Mar 2010
--
-- Copyright (C) 2010 Andy Stewart
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
--
-- |
-- Maintainer : gtk2hs-users@lists.sourceforge.net
-- Stability : provisional
-- Portability : portable (depends on GHC)
--
-- An input method context supporting table-based input methods
--
module Graphics.UI.Gtk.Misc.IMContextSimple (

-- * Class Hierarchy
--
-- |
-- @
-- | 'GObject'
-- | +----'IMContext'
-- | +----IMContextSimple
-- @

-- * Types
  IMContextSimple,
  IMContextSimpleClass,
  castToIMContextSimple,
  toIMContextSimple,

-- * Constructors
  imContextSimpleNew,

-- * Methods
  imContextSimpleAddTable,
  ) where

import Control.Monad (liftM)
import Data.Map (Map)

import System.Glib.FFI
import System.Glib.UTFString
import Graphics.UI.Gtk.Types
{-# LINE 58 "./Graphics/UI/Gtk/Misc/IMContextSimple.chs" #-}


{-# LINE 60 "./Graphics/UI/Gtk/Misc/IMContextSimple.chs" #-}

import qualified Data.Map as M

--------------------
-- Constructors

-- | Creates a new 'IMContextSimple'.
--
imContextSimpleNew :: IO IMContextSimple
imContextSimpleNew :: IO IMContextSimple
imContextSimpleNew =
  (ForeignPtr IMContextSimple -> IMContextSimple,
 FinalizerPtr IMContextSimple)
-> IO (Ptr IMContextSimple) -> IO IMContextSimple
forall obj.
GObjectClass obj =>
(ForeignPtr obj -> obj, FinalizerPtr obj) -> IO (Ptr obj) -> IO obj
wrapNewGObject (ForeignPtr IMContextSimple -> IMContextSimple,
 FinalizerPtr IMContextSimple)
forall {a}.
(ForeignPtr IMContextSimple -> IMContextSimple, FinalizerPtr a)
mkIMContextSimple (IO (Ptr IMContextSimple) -> IO IMContextSimple)
-> IO (Ptr IMContextSimple) -> IO IMContextSimple
forall a b. (a -> b) -> a -> b
$
  (Ptr IMContext -> Ptr IMContextSimple)
-> IO (Ptr IMContext) -> IO (Ptr IMContextSimple)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Ptr IMContext -> Ptr IMContextSimple
forall a b. Ptr a -> Ptr b
castPtr IO (Ptr IMContext)
gtk_im_context_simple_new
{-# LINE 72 "./Graphics/UI/Gtk/Misc/IMContextSimple.chs" #-}

--------------------
-- Methods

-- | Adds an additional table to search to the input context. Each row of the table consists of
-- @maxSeqLen@ key symbols followed by two 'Int' interpreted as the high and low words of a gunicode
-- value. Tables are searched starting from the last added.
--
-- The table must be sorted in dictionary order on the numeric value of the key symbol fields. (Values
-- beyond the length of the sequence should be zero.)
--
imContextSimpleAddTable :: (IMContextSimpleClass self, GlibString string) => self
 -> Map string string -- ^ @data@ - the table
 -> Int -- ^ @maxSeqLen@ - Maximum length of a sequence in the table
                 -- (cannot be greater than 'MaxComposeLen')
 -> Int -- ^ @nSeqs@ - number of sequences in the table
 -> IO ()
imContextSimpleAddTable :: forall self string.
(IMContextSimpleClass self, GlibString string) =>
self -> Map string string -> Int -> Int -> IO ()
imContextSimpleAddTable self
self Map string string
table Int
maxSeqLen Int
nSeqs = do
  [(CString, CString)]
tableList <- ((string, string) -> IO (CString, CString))
-> [(string, string)] -> IO [(CString, CString)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (\(string
x,string
y) -> do
                       CString
nx <- string -> IO CString
forall s. GlibString s => s -> IO CString
newUTFString string
x
                       CString
ny <- string -> IO CString
forall s. GlibString s => s -> IO CString
newUTFString string
y
                       (CString, CString) -> IO (CString, CString)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (CString
nx, CString
ny)) (Map string string -> [(string, string)]
forall k a. Map k a -> [(k, a)]
M.toList Map string string
table)
  [CString] -> (Ptr CString -> IO ()) -> IO ()
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray (((CString, CString) -> [CString])
-> [(CString, CString)] -> [CString]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\(CString
x,CString
y) -> [CString
x, CString
y]) [(CString, CString)]
tableList) ((Ptr CString -> IO ()) -> IO ())
-> (Ptr CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Ptr CString
tablePtr :: Ptr CString) ->
      (\(IMContextSimple ForeignPtr IMContextSimple
arg1) Ptr CUShort
arg2 CInt
arg3 CInt
arg4 -> ForeignPtr IMContextSimple
-> (Ptr IMContextSimple -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr IMContextSimple
arg1 ((Ptr IMContextSimple -> IO ()) -> IO ())
-> (Ptr IMContextSimple -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr IMContextSimple
argPtr1 ->Ptr IMContextSimple -> Ptr CUShort -> CInt -> CInt -> IO ()
gtk_im_context_simple_add_table Ptr IMContextSimple
argPtr1 Ptr CUShort
arg2 CInt
arg3 CInt
arg4)
{-# LINE 96 "./Graphics/UI/Gtk/Misc/IMContextSimple.chs" #-}
        (self -> IMContextSimple
forall o. IMContextSimpleClass o => o -> IMContextSimple
toIMContextSimple self
self)
        (Ptr CString -> Ptr CUShort
forall a b. Ptr a -> Ptr b
castPtr Ptr CString
tablePtr)
        (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
maxSeqLen)
        (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
nSeqs)

foreign import ccall safe "gtk_im_context_simple_new"
  gtk_im_context_simple_new :: (IO (Ptr IMContext))

foreign import ccall safe "gtk_im_context_simple_add_table"
  gtk_im_context_simple_add_table :: ((Ptr IMContextSimple) -> ((Ptr CUShort) -> (CInt -> (CInt -> (IO ())))))