     win "delete-event" [ on-delete yield ]
     GtkWidget:delete-event connect-signal ;
 
-: on-retrieve-surrounding ( im-context user-data -- ? )
-    window focusable-editor [| im-context editor |
-        editor editor-caret first2 :> ( x y )
-        im-context
-        y editor editor-line utf8 string>alien
-        -1 x
+! ----------------------
+
+GENERIC: support-input-methods? ( gadget -- ? )
+GENERIC: get-cursor-surrounding ( gadget -- text cursor-pos )
+GENERIC: delete-cursor-surrounding ( offset count gadget -- )
+GENERIC: set-preedit-string ( str cursor-pos gadget -- )
+GENERIC: get-cursor-loc&dim ( gadget -- loc dim )
+
+M: gadget support-input-methods? drop f ;
+
+M: editor support-input-methods? drop t ;
+
+M: editor get-cursor-surrounding
+    dup editor-caret first2 [ swap editor-line ] dip ;
+
+M: editor delete-cursor-surrounding
+    3drop ;
+
+M: editor set-preedit-string
+    nip dup [ editor-caret ] keep
+    [ user-input* drop ] 2dip
+    set-caret ;
+
+M: editor get-cursor-loc&dim
+    [ caret-loc ] [ caret-dim ] bi ;
+
+! ----------------------
+
+: on-retrieve-surrounding ( im-context win -- ? )
+    window world-focus dup support-input-methods? [
+        get-cursor-surrounding [ utf8 string>alien -1 ] dip
         gtk_im_context_set_surrounding t
-    ] [ drop f ] if* ;
-
-:: on-delete-surrounding ( im-context offset n user-data -- ? )
-    user-data window :> world
-    world focusable-editor [| editor |
-        editor editor-caret first2 :> ( x y )
-        x offset + y [ 2array ] [ [ n + ] dip 2array ] 2bi
-        editor remove-doc-range
-        world update-im-cursor-location
-        t
-    ] [ f ] if* ;
+    ] [ 2drop f ] if ;
+
+: on-delete-surrounding ( im-context offset n win -- ? )
+    window world-focus dup support-input-methods?
+    [ delete-cursor-surrounding t ] [ 3drop f ] if nip ;
+
+: get-preedit-string ( im-context -- str cursor-pos )
+    { void* int } [ f swap gtk_im_context_get_preedit_string ]
+    [ [ [ utf8 alien>string ] [ g_free ] bi ] dip ]
+    with-out-parameters ;
+            
+: on-preedit-changed ( im-context user-data -- )
+    window world-focus dup support-input-methods? [
+        [ get-preedit-string ] dip set-preedit-string
+    ] [ 2drop ] if ;
 
 : on-commit ( sender str user_data -- )
-    [ drop ] [ utf8 alien>string ] [ window ] tri*
-    [ user-input ]
-    [ [ f swap key-down boa ] dip handle-key-gesture ] 2bi ;
+    [ drop ] [ utf8 alien>string ] [ window ] tri* user-input ;
+
+: gadget-location ( gadget -- loc )
+    [ loc>> ] [ parent>> [ gadget-location v+ ] when* ] bi ;
+
+: gadget-cursor-location ( gadget -- rectangle )
+    [ gadget-location ] [ get-cursor-loc&dim ] bi [ v+ ] dip
+    [ first2 ] bi@ GdkRectangle <struct-boa> ;
+
+: update-cursor-location ( im-context gadget -- )
+    gadget-cursor-location gtk_im_context_set_cursor_location ;
 
 ! has to be called before the window signal handler
-: im-on-key-event ( sender event user-data -- result )
-    [ drop ] 2dip swap gtk_im_context_filter_keypress ;
+:: im-on-key-event ( sender event im-context -- result )
+    sender 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>> gtk_im_context_focus_in ]
-    [ update-im-cursor-location ] bi f ;
+    2drop window handle>> im-context>>
+    [ 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>> gtk_im_context_focus_out ]
-    [ update-im-cursor-location ] bi f ;
-
-: im-on-motion ( sender event user-data -- result )
-    2drop window update-im-cursor-location f ;
+    2drop window handle>> im-context>>
+    [ gtk_im_context_focus_out ] [ gtk_im_context_reset ] bi f ;
 
 : im-on-destroy ( sender user-data -- result )
     nip [ f gtk_im_context_set_client_window ]
     [ g_object_unref ] bi f ;
 
+! for testing only
+
+: com-input-method ( world -- )
+    find-world handle>> im-menu>> f f f f 0
+    gtk_get_current_event_time gtk_menu_popup ;
+
+: im-menu ( world -- )
+    { com-input-method } show-commands-menu ;
+
+editor "input-method" f  {
+    { T{ button-down f { S+ C+ } 3 } im-menu }
+} define-command-map
+
+! --------
+
 :: configure-im ( win im -- )
     im win gtk_widget_get_window gtk_im_context_set_client_window
     im f gtk_im_context_set_use_preedit
+
+    gtk_menu_new :> menu
+    im menu gtk_im_multicontext_append_menuitems
+    menu win window handle>> im-menu<<
     
     im "commit" [ on-commit yield ]
     GtkIMContext:commit win connect-signal-with-data

! Copyright (C) 2010 Anton Gorenko, Philipp Brüschweiler.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.accessors alien.c-types alien.data
alien.enums alien.strings arrays ascii assocs classes.struct
combinators combinators.short-circuit command-line destructors
documents gdk.ffi gdk.gl.ffi glib.ffi gobject.ffi gtk.ffi
gtk.gl.ffi io.backend.unix.multiplexers io.encodings.utf8
io.thread kernel libc literals locals math math.bitwise
math.order math.vectors namespaces sequences strings system
threads ui ui.backend ui.clipboards ui.event-loop ui.gadgets
ui.gadgets.editors ui.gadgets.line-support ui.gadgets.private
ui.gadgets.worlds ui.gestures ui.pixel-formats
ui.pixel-formats.private ui.private ;
RENAME: windows ui.private => ui:windows
IN: ui.backend.gtk

SINGLETON: gtk-ui-backend

TUPLE: handle ;
TUPLE: window-handle < handle window fullscreen? im-context ;

: <window-handle> ( window im-context -- window-handle )
    window-handle new

    gtk_window_set_title ;

M: gtk-ui-backend (set-fullscreen)
    [ handle>> ] dip [ >>fullscreen? ] keep
    [ window>> ] dip
    [ gtk_window_fullscreen ]
    [ gtk_window_unfullscreen ] if ;

M: gtk-ui-backend (fullscreen?)
    handle>> fullscreen?>> ;
