1 ! Copyright (C) 2010 Anton Gorenko, Philipp Brüschweiler.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.accessors alien.c-types alien.data
4 alien.strings arrays assocs classes.struct command-line
5 destructors gdk.ffi gdk.gl.ffi glib.ffi gobject.ffi gtk.ffi
6 gtk.gl.ffi io.backend io.backend.unix.multiplexers
7 io.encodings.utf8 io.thread kernel libc literals locals math
8 math.bitwise math.order math.vectors namespaces sequences
9 strings system threads ui ui.backend ui.clipboards ui.commands
10 ui.event-loop ui.gadgets ui.gadgets.editors ui.gadgets.menus
11 ui.gadgets.private ui.gadgets.worlds ui.gestures
12 ui.pixel-formats ui.pixel-formats.private ui.private ;
15 SINGLETON: gtk-ui-backend
18 TUPLE: window-handle < handle window fullscreen? im-context im-menu ;
20 : <window-handle> ( window im-context -- window-handle )
25 TUPLE: gtk-clipboard handle ;
27 C: <gtk-clipboard> gtk-clipboard
29 PIXEL-FORMAT-ATTRIBUTE-TABLE: gl-config-attribs ${ GDK_GL_USE_GL GDK_GL_RGBA } H{
30 { double-buffered ${ GDK_GL_DOUBLEBUFFER } }
31 { stereo ${ GDK_GL_STEREO } }
32 ! { offscreen ${ GDK_GL_DRAWABLE_TYPE 2 } }
33 ! { fullscreen ${ GDK_GL_DRAWABLE_TYPE 1 } }
34 ! { windowed ${ GDK_GL_DRAWABLE_TYPE 1 } }
35 { color-bits ${ GDK_GL_BUFFER_SIZE } }
36 { red-bits ${ GDK_GL_RED_SIZE } }
37 { green-bits ${ GDK_GL_GREEN_SIZE } }
38 { blue-bits ${ GDK_GL_BLUE_SIZE } }
39 { alpha-bits ${ GDK_GL_ALPHA_SIZE } }
40 { accum-red-bits ${ GDK_GL_ACCUM_RED_SIZE } }
41 { accum-green-bits ${ GDK_GL_ACCUM_GREEN_SIZE } }
42 { accum-blue-bits ${ GDK_GL_ACCUM_BLUE_SIZE } }
43 { accum-alpha-bits ${ GDK_GL_ACCUM_ALPHA_SIZE } }
44 { depth-bits ${ GDK_GL_DEPTH_SIZE } }
45 { stencil-bits ${ GDK_GL_STENCIL_SIZE } }
46 { aux-buffers ${ GDK_GL_AUX_BUFFERS } }
47 { sample-buffers ${ GDK_GL_SAMPLE_BUFFERS } }
48 { samples ${ GDK_GL_SAMPLES } }
51 M: gtk-ui-backend (make-pixel-format)
52 nip >gl-config-attribs-int-array gdk_gl_config_new ;
54 M: gtk-ui-backend (free-pixel-format)
55 handle>> g_object_unref ;
57 M: gtk-ui-backend (pixel-format-attribute)
58 [ handle>> ] [ >gl-config-attribs ] bi*
59 { int } [ gdk_gl_config_get_attrib drop ]
64 GDK_POINTER_MOTION_MASK
65 GDK_POINTER_MOTION_HINT_MASK
69 GDK_BUTTON_RELEASE_MASK
77 { S+ $ GDK_SHIFT_MASK }
78 { C+ $ GDK_CONTROL_MASK }
79 { A+ $ GDK_MOD1_MASK }
82 CONSTANT: action-key-codes
84 { $ GDK_BackSpace "BACKSPACE" }
86 { $ GDK_Return "RET" }
87 { $ GDK_KP_Enter "ENTER" }
88 { $ GDK_Escape "ESC" }
89 { $ GDK_Delete "DELETE" }
93 { $ GDK_Right "RIGHT" }
95 { $ GDK_Page_Up "PAGE_UP" }
96 { $ GDK_Page_Down "PAGE_DOWN" }
98 { $ GDK_Begin "BEGIN" }
113 : event-modifiers ( event -- seq )
114 state>> modifiers modifier ;
116 : event-loc ( event -- loc )
117 [ x>> ] [ y>> ] bi [ >fixnum ] bi@ 2array ;
119 : event-dim ( event -- dim )
120 [ width>> ] [ height>> ] bi 2array ;
122 : scroll-direction ( event -- pair )
124 { $ GDK_SCROLL_UP { 0 -1 } }
125 { $ GDK_SCROLL_DOWN { 0 1 } }
126 { $ GDK_SCROLL_LEFT { -1 0 } }
127 { $ GDK_SCROLL_RIGHT { 1 0 } }
130 : mouse-event>gesture ( event -- modifiers button loc )
131 [ event-modifiers ] [ button>> ] [ event-loc ] tri ;
133 : on-motion ( sender event user-data -- result )
135 [ GdkEventMotion memory>struct event-loc ] dip window
136 move-hand fire-motion t ;
138 : on-enter ( sender event user-data -- result )
141 : on-leave ( sender event user-data -- result )
142 3drop forget-rollover t ;
144 : on-button-press ( sender event user-data -- result )
146 GdkEventButton memory>struct
147 mouse-event>gesture [ <button-down> ] dip
148 ] dip window send-button-down t ;
150 : on-button-release ( sender event user-data -- result )
152 GdkEventButton memory>struct
153 mouse-event>gesture [ <button-up> ] dip
154 ] dip window send-button-up t ;
156 : on-scroll ( sender event user-data -- result )
158 GdkEventScroll memory>struct
159 [ scroll-direction ] [ event-loc ] bi
160 ] dip window send-scroll t ;
162 : key-sym ( event -- sym/f action? )
163 keyval>> dup action-key-codes at [ t ]
164 [ gdk_keyval_to_unicode [ f ] [ 1string ] if-zero f ] ?if ;
166 : key-event>gesture ( event -- mods sym/f action? )
167 GdkEventKey memory>struct
168 [ event-modifiers ] [ key-sym ] bi ;
170 : on-key-press ( sender event user-data -- result )
171 drop swap [ key-event>gesture <key-down> ] [ window ] bi*
172 propagate-key-gesture t ;
174 : on-key-release ( sender event user-data -- result )
175 drop swap [ key-event>gesture <key-up> ] [ window ] bi*
176 propagate-key-gesture t ;
178 : on-focus-in ( sender event user-data -- result )
179 2drop window focus-world t ;
181 : on-focus-out ( sender event user-data -- result )
182 2drop window unfocus-world t ;
184 : on-expose ( sender event user-data -- result )
185 2drop window relayout t ;
187 : on-configure ( sender event user-data -- result )
188 drop [ window ] dip GdkEventConfigure memory>struct
189 [ event-loc >>window-loc ] [ event-dim >>dim ] bi
192 : on-delete ( sender event user-data -- result )
193 2drop window ungraft t ;
195 : init-clipboard ( -- )
197 clipboard "CLIPBOARD"
199 utf8 string>alien gdk_atom_intern_static_string
200 gtk_clipboard_get <gtk-clipboard> swap set-global
203 : io-source-prepare ( source timeout -- result )
206 : io-source-check ( source -- result )
207 poll_fds>> 0 g_slist_nth_data GPollFD memory>struct
210 : io-source-dispatch ( source callback user_data -- result )
212 0 mx get wait-for-events
215 CONSTANT: poll-fd-events
225 : create-poll-fd ( -- poll-fd )
226 GPollFD malloc-struct &free
228 poll-fd-events >>events ;
230 HOOK: init-io-event-source io-backend ( -- )
232 M: c-io-backend init-io-event-source
235 M: object init-io-event-source
236 GSourceFuncs malloc-struct &free
237 [ io-source-prepare ] GSourceFuncsPrepareFunc >>prepare
238 [ io-source-check ] GSourceFuncsCheckFunc >>check
239 [ io-source-dispatch ] GSourceFuncsDispatchFunc >>dispatch
240 GSource heap-size g_source_new &g_source_unref
241 [ create-poll-fd g_source_add_poll ]
242 [ f g_source_attach drop ] bi ;
246 : set-timeout*-value ( alien value -- )
247 swap 0 set-alien-signed-4 ; inline
249 : timeout-prepare ( source timeout* -- result )
250 nip next-timeout get-global nano-count [-]
251 [ 1,000,000 /i set-timeout*-value ] keep 0 = ;
253 : timeout-check ( source -- result )
254 drop next-timeout get-global nano-count [-] 0 = ;
256 : timeout-dispatch ( source callback user_data -- result )
257 3drop sleep-time [ 1,000,000,000 ] unless* nano-count +
258 next-timeout set-global
261 : init-timeout ( -- )
262 GSourceFuncs malloc-struct &free
263 [ timeout-prepare ] GSourceFuncsPrepareFunc >>prepare
264 [ timeout-check ] GSourceFuncsCheckFunc >>check
265 [ timeout-dispatch ] GSourceFuncsDispatchFunc >>dispatch
266 GSource heap-size g_source_new &g_source_unref
267 f g_source_attach drop
268 nano-count next-timeout set-global ;
270 M: gtk-ui-backend (with-ui)
284 : connect-signal-with-data ( object signal-name callback data -- )
285 [ utf8 string>alien ] 2dip g_signal_connect drop ;
287 : connect-signal ( object signal-name callback -- )
288 f connect-signal-with-data ;
290 :: connect-signals ( win -- )
291 win events-mask gtk_widget_add_events
293 win "expose-event" [ on-expose yield ]
294 GtkWidget:expose-event connect-signal
295 win "configure-event" [ on-configure yield ]
296 GtkWidget:configure-event connect-signal
297 win "motion-notify-event" [ on-motion yield ]
298 GtkWidget:motion-notify-event connect-signal
299 win "leave-notify-event" [ on-leave yield ]
300 GtkWidget:leave-notify-event connect-signal
301 win "enter-notify-event" [ on-enter yield ]
302 GtkWidget:enter-notify-event connect-signal
303 win "button-press-event" [ on-button-press yield ]
304 GtkWidget:button-press-event connect-signal
305 win "button-release-event" [ on-button-release yield ]
306 GtkWidget:button-release-event connect-signal
307 win "scroll-event" [ on-scroll yield ]
308 GtkWidget:scroll-event connect-signal
309 win "key-press-event" [ on-key-press yield ]
310 GtkWidget:key-press-event connect-signal
311 win "key-release-event" [ on-key-release yield ]
312 GtkWidget:key-release-event connect-signal
313 win "focus-in-event" [ on-focus-in yield ]
314 GtkWidget:focus-in-event connect-signal
315 win "focus-out-event" [ on-focus-out yield ]
316 GtkWidget:focus-out-event connect-signal
317 win "delete-event" [ on-delete yield ]
318 GtkWidget:delete-event connect-signal ;
320 ! ----------------------
322 GENERIC: support-input-methods? ( gadget -- ? )
323 GENERIC: get-cursor-surrounding ( gadget -- text cursor-pos )
324 GENERIC: delete-cursor-surrounding ( offset count gadget -- )
325 GENERIC: set-preedit-string ( str cursor-pos gadget -- )
326 GENERIC: get-cursor-loc&dim ( gadget -- loc dim )
328 M: gadget support-input-methods? drop f ;
330 M: editor support-input-methods? drop t ;
332 M: editor get-cursor-surrounding
333 dup editor-caret first2 [ swap editor-line ] dip ;
335 M: editor delete-cursor-surrounding
338 M: editor set-preedit-string
339 nip dup [ editor-caret ] keep
340 [ user-input* drop ] 2dip
343 M: editor get-cursor-loc&dim
344 [ caret-loc ] [ caret-dim ] bi ;
346 ! ----------------------
348 : on-retrieve-surrounding ( im-context win -- ? )
349 window world-focus dup support-input-methods? [
350 get-cursor-surrounding [ utf8 string>alien -1 ] dip
351 gtk_im_context_set_surrounding t
354 : on-delete-surrounding ( im-context offset n win -- ? )
355 window world-focus dup support-input-methods?
356 [ delete-cursor-surrounding t ] [ 3drop f ] if nip ;
358 : get-preedit-string ( im-context -- str cursor-pos )
359 { void* int } [ f swap gtk_im_context_get_preedit_string ]
361 [ [ utf8 alien>string ] [ g_free ] bi ] dip ;
363 : on-preedit-changed ( im-context user-data -- )
364 window world-focus dup support-input-methods? [
365 [ get-preedit-string ] dip set-preedit-string
368 : on-commit ( sender str user_data -- )
369 [ drop ] [ utf8 alien>string ] [ window ] tri* user-input ;
371 : gadget-location ( gadget -- loc )
372 [ loc>> ] [ parent>> [ gadget-location v+ ] when* ] bi ;
374 : gadget-cursor-location ( gadget -- rectangle )
375 [ gadget-location ] [ get-cursor-loc&dim ] bi [ v+ ] dip
376 [ first2 ] bi@ GdkRectangle <struct-boa> ;
378 : update-cursor-location ( im-context gadget -- )
379 gadget-cursor-location gtk_im_context_set_cursor_location ;
381 ! has to be called before the window signal handler
382 :: im-on-key-event ( sender event im-context -- result )
383 sender window world-focus :> gadget
384 gadget support-input-methods? [
385 im-context gadget update-cursor-location
386 im-context event gtk_im_context_filter_keypress
387 ] [ im-context gtk_im_context_reset f ] if ;
389 : im-on-focus-in ( sender event user-data -- result )
390 2drop window handle>> im-context>>
391 [ gtk_im_context_focus_in ] [ gtk_im_context_reset ] bi f ;
393 : im-on-focus-out ( sender event user-data -- result )
394 2drop window handle>> im-context>>
395 [ gtk_im_context_focus_out ] [ gtk_im_context_reset ] bi f ;
397 : im-on-destroy ( sender user-data -- )
398 nip [ f gtk_im_context_set_client_window ]
399 [ g_object_unref ] bi ;
403 : com-input-method ( world -- )
404 find-world handle>> im-menu>> f f f f 0
405 gtk_get_current_event_time gtk_menu_popup ;
407 : im-menu ( world -- )
408 { com-input-method } show-commands-menu ;
410 editor "input-method" f {
411 { T{ button-down f { S+ C+ } 3 } im-menu }
416 :: configure-im ( win im -- )
417 im win gtk_widget_get_window gtk_im_context_set_client_window
418 im f gtk_im_context_set_use_preedit
421 im menu gtk_im_multicontext_append_menuitems
422 menu win window handle>> im-menu<<
424 im "commit" [ on-commit yield ]
425 GtkIMContext:commit win connect-signal-with-data
426 im "retrieve-surrounding" [ on-retrieve-surrounding yield ]
427 GtkIMContext:retrieve-surrounding win connect-signal-with-data
428 im "delete-surrounding" [ on-delete-surrounding yield ]
429 GtkIMContext:delete-surrounding win connect-signal-with-data
430 im "preedit-changed" [ on-preedit-changed yield ]
431 GtkIMContext:preedit-changed win connect-signal-with-data
433 win "key-press-event" [ im-on-key-event yield ]
434 GtkWidget:key-press-event im connect-signal-with-data
435 win "key-release-event" [ im-on-key-event yield ]
436 GtkWidget:key-release-event im connect-signal-with-data
437 win "focus-in-event" [ im-on-focus-in yield ]
438 GtkWidget:focus-out-event im connect-signal-with-data
439 win "focus-out-event" [ im-on-focus-out yield ]
440 GtkWidget:focus-out-event im connect-signal-with-data
441 win "destroy" [ im-on-destroy yield ]
442 GtkObject:destroy im connect-signal-with-data ;
444 CONSTANT: window-controls>decor-flags
447 { minimize-button $ GDK_DECOR_MINIMIZE }
448 { maximize-button $ GDK_DECOR_MAXIMIZE }
449 { resize-handles $ GDK_DECOR_RESIZEH }
450 { small-title-bar $ GDK_DECOR_TITLE }
451 { normal-title-bar $ GDK_DECOR_TITLE }
452 { textured-background 0 }
455 CONSTANT: window-controls>func-flags
457 { close-button $ GDK_FUNC_CLOSE }
458 { minimize-button $ GDK_FUNC_MINIMIZE }
459 { maximize-button $ GDK_FUNC_MAXIMIZE }
460 { resize-handles $ GDK_FUNC_RESIZE }
461 { small-title-bar 0 }
462 { normal-title-bar 0 }
463 { textured-background 0 }
466 : configure-window-controls ( win controls -- )
468 small-title-bar swap member-eq?
469 GDK_WINDOW_TYPE_HINT_UTILITY GDK_WINDOW_TYPE_HINT_NORMAL ?
470 gtk_window_set_type_hint
472 [ gtk_widget_get_window ] dip
473 window-controls>decor-flags symbols>flags
474 GDK_DECOR_BORDER bitor gdk_window_set_decorations
476 [ gtk_widget_get_window ] dip
477 window-controls>func-flags symbols>flags
478 GDK_FUNC_MOVE bitor gdk_window_set_functions
481 : setup-gl ( world -- ? )
483 [ handle>> window>> ] [ handle>> ] bi*
484 f t GDK_GL_RGBA_TYPE gtk_widget_set_gl_capability
485 ] with-world-pixel-format ;
487 : auto-position ( win loc -- )
489 drop dup window topmost-window =
490 GTK_WIN_POS_CENTER GTK_WIN_POS_NONE ?
491 gtk_window_set_position
492 ] [ first2 gtk_window_move ] if ;
494 M:: gtk-ui-backend (open-window) ( world -- )
495 GTK_WINDOW_TOPLEVEL gtk_window_new :> win
496 gtk_im_multicontext_new :> im
498 win im <window-handle> world handle<<
500 world win register-window
502 win world [ window-loc>> auto-position ]
503 [ dim>> first2 gtk_window_set_default_size ] 2bi
507 win gtk_widget_realize
508 win world window-controls>> configure-window-controls
513 win gtk_widget_show_all ;
515 M: gtk-ui-backend (close-window) ( handle -- )
516 window>> [ gtk_widget_destroy ] [ unregister-window ] bi
517 event-loop? [ gtk_main_quit ] unless ;
519 M: gtk-ui-backend set-title
520 swap [ handle>> window>> ] [ utf8 string>alien ] bi*
521 gtk_window_set_title ;
523 M: gtk-ui-backend (set-fullscreen)
524 [ handle>> ] dip [ >>fullscreen? ] keep
526 [ gtk_window_fullscreen ]
527 [ gtk_window_unfullscreen ] if ;
529 M: gtk-ui-backend (fullscreen?)
530 handle>> fullscreen?>> ;
532 M: gtk-ui-backend raise-window*
533 handle>> window>> gtk_window_present ;
535 : set-cursor ( win cursor -- )
537 [ gtk_widget_get_window ] dip
538 gdk_cursor_new &gdk_cursor_unref
539 gdk_window_set_cursor
542 M: gtk-ui-backend (grab-input)
544 [ gtk_grab_add ] [ GDK_BLANK_CURSOR set-cursor ] bi ;
546 M: gtk-ui-backend (ungrab-input)
548 [ gtk_grab_remove ] [ GDK_LEFT_PTR set-cursor ] bi ;
550 M: window-handle select-gl-context ( handle -- )
552 [ gtk_widget_get_gl_window ] [ gtk_widget_get_gl_context ] bi
553 gdk_gl_drawable_make_current drop ;
555 M: window-handle flush-gl-context ( handle -- )
556 window>> gtk_widget_get_gl_window
557 gdk_gl_drawable_swap_buffers ;
559 M: gtk-ui-backend beep
562 M:: gtk-ui-backend system-alert ( caption text -- )
563 f GTK_DIALOG_MODAL GTK_MESSAGE_WARNING GTK_BUTTONS_OK
564 caption utf8 string>alien f gtk_message_dialog_new
565 [ text utf8 string>alien f gtk_message_dialog_format_secondary_text ]
566 [ gtk_dialog_run drop ]
567 [ gtk_widget_destroy ] tri ;
569 M: gtk-clipboard clipboard-contents
571 handle>> gtk_clipboard_wait_for_text
572 [ &g_free utf8 alien>string ] [ f ] if*
575 M: gtk-clipboard set-clipboard-contents
576 swap [ handle>> ] [ utf8 string>alien ] bi*
577 -1 gtk_clipboard_set_text ;
579 gtk-ui-backend ui-backend set-global
581 [ "ui.tools" ] main-vocab-hook set-global