     gadget-cursor-location gtk_im_context_set_cursor_location ;
 
 ! has to be called before the window signal handler
-:: im-on-key-event ( sender event im-context -- result )
-    sender window world-focus :> gadget
+:: im-on-key-event ( win event im-context -- ? )
+    win window world-focus :> gadget
     gadget support-input-methods? [
         im-context gadget update-cursor-location
         im-context event gtk_im_context_filter_keypress
     ] [ im-context gtk_im_context_reset f ] if ;
 
-: im-on-focus-in ( sender event user-data -- result )
-    2drop window handle>> im-context>>
+: im-on-focus-in ( win event im-context -- ? )
+    2nip
     [ gtk_im_context_focus_in ] [ gtk_im_context_reset ] bi f ;
 
-: im-on-focus-out ( sender event user-data -- result )
-    2drop window handle>> im-context>>
+: im-on-focus-out ( win event im-context -- ? )
+    2nip
     [ gtk_im_context_focus_out ] [ gtk_im_context_reset ] bi f ;
 
-: im-on-destroy ( sender user-data -- )
+: im-on-destroy ( win im-context -- )
     nip [ f gtk_im_context_set_client_window ]
-    [ g_object_unref ] bi ;
+    ! weird GLib-GObject-WARNING message appears after calling this code
+    ! [ g_object_unref ] bi ;
+    [ drop ] bi ;
 
 :: configure-im ( win im -- )
     im win gtk_widget_get_window gtk_im_context_set_client_window

: mouse-event>gesture ( event -- modifiers button loc )
    [ event-modifiers ] [ button>> ] [ event-loc ] tri ;

: on-motion ( sender event user-data -- result )
    drop swap
    [ GdkEventMotion memory>struct event-loc ] dip window
    move-hand fire-motion t ;

: on-enter ( sender event user-data -- result )
    on-motion ;

: on-leave ( sender event user-data -- result )
    3drop forget-rollover t ;

: on-button-press ( sender event user-data -- result )
    drop swap [
        GdkEventButton memory>struct
        mouse-event>gesture [ <button-down> ] dip
    ] dip window send-button-down t ;

: on-button-release ( sender event user-data -- result )
    drop swap [
        GdkEventButton memory>struct
        mouse-event>gesture [ <button-up> ] dip
    ] dip window send-button-up t ;

: on-scroll ( sender event user-data -- result )
    drop swap [
        GdkEventScroll memory>struct
        [ scroll-direction ] [ event-loc ] bi

    win "focus-in-event" [ on-focus-in yield ]
    GtkWidget:focus-in-event connect-signal
    win "focus-out-event" [ on-focus-out yield ]
    GtkWidget:focus-out-event connect-signal ;

! Window state events

: on-expose ( win event user-data -- ? )
    2drop window relayout t ;

: on-configure ( win event user-data -- ? )
    drop [ window ] [ GdkEventConfigure memory>struct ] bi*
    [ event-loc >>window-loc ] [ event-dim >>dim  ] bi
    relayout-1 f ;

: on-delete ( win event user-data -- ? )
    2drop window ungraft t ;

:: connect-win-state-signals ( win -- )
    win "expose-event" [ on-expose yield ]
    GtkWidget:expose-event connect-signal
    win "configure-event" [ on-configure yield ]
    GtkWidget:configure-event connect-signal
    win "delete-event" [ on-delete yield ]
    GtkWidget:delete-event connect-signal ;

! Input methods

GENERIC: support-input-methods? ( gadget -- ? )
GENERIC: get-cursor-surrounding ( gadget -- text cursor-pos )
