{-# LINE 2 "./Graphics/UI/Gtk/WebKit/DOM/EventTargetClosures.chs" #-}
{-# CFILES events.c #-}
module Graphics.UI.Gtk.WebKit.DOM.EventTargetClosures
       (eventTargetAddEventListener) where
import System.Glib.FFI (newStablePtr, StablePtr, Ptr(..), toBool, CInt(..), CChar,
        withForeignPtr, fromBool)
import System.Glib.UTFString
import Control.Applicative
import Graphics.UI.Gtk.WebKit.Types
{-# LINE 10 "./Graphics/UI/Gtk/WebKit/DOM/EventTargetClosures.chs" #-}
import System.Glib.GError
import Control.Monad (void)

newtype GClosure = GClosure (Ptr (GClosure))
{-# LINE 14 "./Graphics/UI/Gtk/WebKit/DOM/EventTargetClosures.chs" #-}

foreign import ccall unsafe "gtk2hs_closure_new"
  gtk2hs_closure_new :: StablePtr a -> IO (Ptr GClosure)

eventTargetAddEventListener ::
                         (GObjectClass self, EventClass event, GlibString string) =>
                           self -> string -> Bool -> (self -> event -> IO ()) -> IO (IO ())
eventTargetAddEventListener self eventName bubble user = do
  sptr <- newStablePtr action
  gclosurePtr <- gtk2hs_closure_new sptr
  toBool <$> (
    withUTFString eventName $ \ eventNamePtr ->
      (\(EventTarget arg1) arg2 (GClosure arg3) arg4 -> withForeignPtr arg1 $ \argPtr1 ->webkit_dom_event_target_add_event_listener_closure argPtr1 arg2 arg3 arg4)
{-# LINE 27 "./Graphics/UI/Gtk/WebKit/DOM/EventTargetClosures.chs" #-}
        (unsafeCastGObject $ toGObject self)
        eventNamePtr
        (GClosure gclosurePtr)
        (fromBool bubble))
  return . void $
    withUTFString eventName $ \ eventNamePtr ->
      (\(EventTarget arg1) arg2 (GClosure arg3) arg4 -> withForeignPtr arg1 $ \argPtr1 ->webkit_dom_event_target_remove_event_listener_closure argPtr1 arg2 arg3 arg4)
{-# LINE 34 "./Graphics/UI/Gtk/WebKit/DOM/EventTargetClosures.chs" #-}
        (unsafeCastGObject $ toGObject self)
        eventNamePtr
        (GClosure gclosurePtr)
        (fromBool bubble)
  where action :: Ptr GObject -> Ptr GObject -> IO ()
        action obj1 obj2 =
          failOnGError $
          makeNewGObject (GObject, objectUnrefFromMainloop) (return obj2) >>= \obj2' ->
          makeNewGObject (GObject, objectUnrefFromMainloop) (return obj1) >>= \obj1' ->
          user (unsafeCastGObject obj1') (unsafeCastGObject obj2')

foreign import ccall safe "webkit_dom_event_target_add_event_listener_closure"
  webkit_dom_event_target_add_event_listener_closure :: ((Ptr EventTarget) -> ((Ptr CChar) -> ((Ptr GClosure) -> (CInt -> (IO CInt)))))

foreign import ccall safe "webkit_dom_event_target_remove_event_listener_closure"
  webkit_dom_event_target_remove_event_listener_closure :: ((Ptr EventTarget) -> ((Ptr CChar) -> ((Ptr GClosure) -> (CInt -> (IO CInt)))))