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 alien.syntax arrays assocs classes.struct
5 command-line destructors gdk.ffi gdk.gl.ffi glib.ffi
6 gobject.ffi gtk.ffi gtk.gl.ffi io.backend
7 io.backend.unix.multiplexers io.encodings.utf8 io.thread kernel
8 libc literals locals math math.bitwise math.order math.vectors
9 namespaces sequences strings system threads ui ui.backend
10 ui.clipboards ui.commands ui.event-loop ui.gadgets
11 ui.gadgets.editors ui.gadgets.menus ui.gadgets.private
12 ui.gadgets.worlds ui.gestures ui.pixel-formats
13 ui.pixel-formats.private ui.private ;
16 SINGLETON: gtk-ui-backend
19 TUPLE: window-handle < handle window fullscreen? im-context im-menu ;
21 : <window-handle> ( window im-context -- window-handle )
26 TUPLE: gtk-clipboard handle ;
28 C: <gtk-clipboard> gtk-clipboard
30 PIXEL-FORMAT-ATTRIBUTE-TABLE: gl-config-attribs ${ GDK_GL_USE_GL GDK_GL_RGBA } H{
31 { double-buffered ${ GDK_GL_DOUBLEBUFFER } }
32 { stereo ${ GDK_GL_STEREO } }
33 ! { offscreen ${ GDK_GL_DRAWABLE_TYPE 2 } }
34 ! { fullscreen ${ GDK_GL_DRAWABLE_TYPE 1 } }
35 ! { windowed ${ GDK_GL_DRAWABLE_TYPE 1 } }
36 { color-bits ${ GDK_GL_BUFFER_SIZE } }
37 { red-bits ${ GDK_GL_RED_SIZE } }
38 { green-bits ${ GDK_GL_GREEN_SIZE } }
39 { blue-bits ${ GDK_GL_BLUE_SIZE } }
40 { alpha-bits ${ GDK_GL_ALPHA_SIZE } }
41 { accum-red-bits ${ GDK_GL_ACCUM_RED_SIZE } }
42 { accum-green-bits ${ GDK_GL_ACCUM_GREEN_SIZE } }
43 { accum-blue-bits ${ GDK_GL_ACCUM_BLUE_SIZE } }
44 { accum-alpha-bits ${ GDK_GL_ACCUM_ALPHA_SIZE } }
45 { depth-bits ${ GDK_GL_DEPTH_SIZE } }
46 { stencil-bits ${ GDK_GL_STENCIL_SIZE } }
47 { aux-buffers ${ GDK_GL_AUX_BUFFERS } }
48 { sample-buffers ${ GDK_GL_SAMPLE_BUFFERS } }
49 { samples ${ GDK_GL_SAMPLES } }
52 M: gtk-ui-backend (make-pixel-format)
53 nip >gl-config-attribs-int-array gdk_gl_config_new ;
55 M: gtk-ui-backend (free-pixel-format)
56 handle>> g_object_unref ;
58 M: gtk-ui-backend (pixel-format-attribute)
59 [ handle>> ] [ >gl-config-attribs ] bi*
60 { int } [ gdk_gl_config_get_attrib drop ]
65 GDK_POINTER_MOTION_MASK
66 GDK_POINTER_MOTION_HINT_MASK
70 GDK_BUTTON_RELEASE_MASK
78 { S+ $ GDK_SHIFT_MASK }
79 { C+ $ GDK_CONTROL_MASK }
80 { A+ $ GDK_MOD1_MASK }
83 CONSTANT: action-key-codes
85 { $ GDK_BackSpace "BACKSPACE" }
87 { $ GDK_Return "RET" }
88 { $ GDK_KP_Enter "ENTER" }
89 { $ GDK_Escape "ESC" }
90 { $ GDK_Delete "DELETE" }
94 { $ GDK_Right "RIGHT" }
96 { $ GDK_Page_Up "PAGE_UP" }
97 { $ GDK_Page_Down "PAGE_DOWN" }
99 { $ GDK_Begin "BEGIN" }
114 : event-modifiers ( event -- seq )
115 state>> modifiers modifier ;
117 : event-loc ( event -- loc )
118 [ x>> ] [ y>> ] bi [ >fixnum ] bi@ 2array ;
120 : event-dim ( event -- dim )
121 [ width>> ] [ height>> ] bi 2array ;
123 : scroll-direction ( event -- pair )
125 { $ GDK_SCROLL_UP { 0 -1 } }
126 { $ GDK_SCROLL_DOWN { 0 1 } }
127 { $ GDK_SCROLL_LEFT { -1 0 } }
128 { $ GDK_SCROLL_RIGHT { 1 0 } }
131 : mouse-event>gesture ( event -- modifiers button loc )
132 [ event-modifiers ] [ button>> ] [ event-loc ] tri ;
134 : on-motion ( sender event user-data -- result )
136 [ GdkEventMotion memory>struct event-loc ] dip window
137 move-hand fire-motion t ;
139 : on-enter ( sender event user-data -- result )
142 : on-leave ( sender event user-data -- result )
143 3drop forget-rollover t ;
145 : on-button-press ( sender event user-data -- result )
147 GdkEventButton memory>struct
148 mouse-event>gesture [ <button-down> ] dip
149 ] dip window send-button-down t ;
151 : on-button-release ( sender event user-data -- result )
153 GdkEventButton memory>struct
154 mouse-event>gesture [ <button-up> ] dip
155 ] dip window send-button-up t ;
157 : on-scroll ( sender event user-data -- result )
159 GdkEventScroll memory>struct
160 [ scroll-direction ] [ event-loc ] bi
161 ] dip window send-scroll t ;
163 : key-sym ( event -- sym/f action? )
164 keyval>> dup action-key-codes at [ t ]
165 [ gdk_keyval_to_unicode [ f ] [ 1string ] if-zero f ] ?if ;
167 : key-event>gesture ( event -- mods sym/f action? )
168 GdkEventKey memory>struct
169 [ event-modifiers ] [ key-sym ] bi ;
171 : on-key-press ( sender event user-data -- result )
172 drop swap [ key-event>gesture <key-down> ] [ window ] bi*
173 propagate-key-gesture t ;
175 : on-key-release ( sender event user-data -- result )
176 drop swap [ key-event>gesture <key-up> ] [ window ] bi*
177 propagate-key-gesture t ;
179 : on-focus-in ( sender event user-data -- result )
180 2drop window focus-world t ;
182 : on-focus-out ( sender event user-data -- result )
183 2drop window unfocus-world t ;
185 : on-expose ( sender event user-data -- result )
186 2drop window relayout t ;
188 : on-configure ( sender event user-data -- result )
189 drop [ window ] dip GdkEventConfigure memory>struct
190 [ event-loc >>window-loc ] [ event-dim >>dim ] bi
193 : on-delete ( sender event user-data -- result )
194 2drop window ungraft t ;
196 : init-clipboard ( -- )
198 clipboard "CLIPBOARD"
200 utf8 string>alien gdk_atom_intern_static_string
201 gtk_clipboard_get <gtk-clipboard> swap set-global
204 : io-source-prepare ( source timeout -- result )
207 : io-source-check ( source -- result )
208 poll_fds>> 0 g_slist_nth_data GPollFD memory>struct
211 : io-source-dispatch ( source callback user_data -- result )
213 0 mx get wait-for-events
216 CONSTANT: poll-fd-events
226 : create-poll-fd ( -- poll-fd )
227 GPollFD malloc-struct &free
229 poll-fd-events >>events ;
231 HOOK: init-io-event-source io-backend ( -- )
233 M: f init-io-event-source ;
234 M: c-io-backend init-io-event-source ;
236 M: object init-io-event-source
237 GSourceFuncs malloc-struct &free
238 [ io-source-prepare ] GSourceFuncsPrepareFunc >>prepare
239 [ io-source-check ] GSourceFuncsCheckFunc >>check
240 [ io-source-dispatch ] GSourceFuncsDispatchFunc >>dispatch
241 GSource heap-size g_source_new &g_source_unref
242 [ create-poll-fd g_source_add_poll ]
243 [ f g_source_attach drop ] bi ;
247 : set-timeout*-value ( alien value -- )
248 swap 0 set-alien-signed-4 ; inline
250 : timeout-prepare ( source timeout* -- result )
251 nip next-timeout get-global nano-count [-]
252 [ 1,000,000 /i set-timeout*-value ] keep 0 = ;
254 : timeout-check ( source -- result )
255 drop next-timeout get-global nano-count [-] 0 = ;
257 : timeout-dispatch ( source callback user_data -- result )
258 3drop sleep-time [ 1,000,000,000 ] unless* nano-count +
259 next-timeout set-global
262 : init-timeout ( -- )
263 GSourceFuncs malloc-struct &free
264 [ timeout-prepare ] GSourceFuncsPrepareFunc >>prepare
265 [ timeout-check ] GSourceFuncsCheckFunc >>check
266 [ timeout-dispatch ] GSourceFuncsDispatchFunc >>dispatch
267 GSource heap-size g_source_new &g_source_unref
268 f g_source_attach drop
269 nano-count next-timeout set-global ;
272 ! This file is not in a resource.txt because it can be
273 ! overwritten when deploying. See 'Vocabulary icons'
275 "vocab:ui/backend/gtk/icon.png"
276 normalize-path utf8 string>alien
277 { { pointer: GError initial: f } }
278 [ gtk_window_set_default_icon_from_file ] with-out-parameters
281 M: gtk-ui-backend (with-ui)
296 : connect-signal-with-data ( object signal-name callback data -- )
297 [ utf8 string>alien ] 2dip g_signal_connect drop ;
299 : connect-signal ( object signal-name callback -- )
300 f connect-signal-with-data ;
302 :: connect-signals ( win -- )
303 win events-mask gtk_widget_add_events
305 win "expose-event" [ on-expose yield ]
306 GtkWidget:expose-event connect-signal
307 win "configure-event" [ on-configure yield ]
308 GtkWidget:configure-event connect-signal
309 win "motion-notify-event" [ on-motion yield ]
310 GtkWidget:motion-notify-event connect-signal
311 win "leave-notify-event" [ on-leave yield ]
312 GtkWidget:leave-notify-event connect-signal
313 win "enter-notify-event" [ on-enter yield ]
314 GtkWidget:enter-notify-event connect-signal
315 win "button-press-event" [ on-button-press yield ]
316 GtkWidget:button-press-event connect-signal
317 win "button-release-event" [ on-button-release yield ]
318 GtkWidget:button-release-event connect-signal
319 win "scroll-event" [ on-scroll yield ]
320 GtkWidget:scroll-event connect-signal
321 win "key-press-event" [ on-key-press yield ]
322 GtkWidget:key-press-event connect-signal
323 win "key-release-event" [ on-key-release yield ]
324 GtkWidget:key-release-event connect-signal
325 win "focus-in-event" [ on-focus-in yield ]
326 GtkWidget:focus-in-event connect-signal
327 win "focus-out-event" [ on-focus-out yield ]
328 GtkWidget:focus-out-event connect-signal
329 win "delete-event" [ on-delete yield ]
330 GtkWidget:delete-event connect-signal ;
332 ! ----------------------
334 GENERIC: support-input-methods? ( gadget -- ? )
335 GENERIC: get-cursor-surrounding ( gadget -- text cursor-pos )
336 GENERIC: delete-cursor-surrounding ( offset count gadget -- )
337 GENERIC: set-preedit-string ( str cursor-pos gadget -- )
338 GENERIC: get-cursor-loc&dim ( gadget -- loc dim )
340 M: gadget support-input-methods? drop f ;
342 M: editor support-input-methods? drop t ;
344 M: editor get-cursor-surrounding
345 dup editor-caret first2 [ swap editor-line ] dip ;
347 M: editor delete-cursor-surrounding
350 M: editor set-preedit-string
351 nip dup [ editor-caret ] keep
352 [ user-input* drop ] 2dip
355 M: editor get-cursor-loc&dim
356 [ caret-loc ] [ caret-dim ] bi ;
358 ! ----------------------
360 : on-retrieve-surrounding ( im-context win -- ? )
361 window world-focus dup support-input-methods? [
362 get-cursor-surrounding [ utf8 string>alien -1 ] dip
363 gtk_im_context_set_surrounding t
366 : on-delete-surrounding ( im-context offset n win -- ? )
367 window world-focus dup support-input-methods?
368 [ delete-cursor-surrounding t ] [ 3drop f ] if nip ;
370 : get-preedit-string ( im-context -- str cursor-pos )
371 { void* int } [ f swap gtk_im_context_get_preedit_string ]
373 [ [ utf8 alien>string ] [ g_free ] bi ] dip ;
375 : on-preedit-changed ( im-context user-data -- )
376 window world-focus dup support-input-methods? [
377 [ get-preedit-string ] dip set-preedit-string
380 : on-commit ( sender str user_data -- )
381 [ drop ] [ utf8 alien>string ] [ window ] tri* user-input ;
383 : gadget-location ( gadget -- loc )
384 [ loc>> ] [ parent>> [ gadget-location v+ ] when* ] bi ;
386 : gadget-cursor-location ( gadget -- rectangle )
387 [ gadget-location ] [ get-cursor-loc&dim ] bi [ v+ ] dip
388 [ first2 ] bi@ GdkRectangle <struct-boa> ;
390 : update-cursor-location ( im-context gadget -- )
391 gadget-cursor-location gtk_im_context_set_cursor_location ;
393 ! has to be called before the window signal handler
394 :: im-on-key-event ( sender event im-context -- result )
395 sender window world-focus :> gadget
396 gadget support-input-methods? [
397 im-context gadget update-cursor-location
398 im-context event gtk_im_context_filter_keypress
399 ] [ im-context gtk_im_context_reset f ] if ;
401 : im-on-focus-in ( sender event user-data -- result )
402 2drop window handle>> im-context>>
403 [ gtk_im_context_focus_in ] [ gtk_im_context_reset ] bi f ;
405 : im-on-focus-out ( sender event user-data -- result )
406 2drop window handle>> im-context>>
407 [ gtk_im_context_focus_out ] [ gtk_im_context_reset ] bi f ;
409 : im-on-destroy ( sender user-data -- )
410 nip [ f gtk_im_context_set_client_window ]
411 [ g_object_unref ] bi ;
415 : com-input-method ( world -- )
416 find-world handle>> im-menu>> f f f f 0
417 gtk_get_current_event_time gtk_menu_popup ;
419 : im-menu ( world -- )
420 { com-input-method } show-commands-menu ;
422 editor "input-method" f {
423 { T{ button-down f { S+ C+ } 3 } im-menu }
428 :: configure-im ( win im -- )
429 im win gtk_widget_get_window gtk_im_context_set_client_window
430 im f gtk_im_context_set_use_preedit
433 im menu gtk_im_multicontext_append_menuitems
434 menu win window handle>> im-menu<<
436 im "commit" [ on-commit yield ]
437 GtkIMContext:commit win connect-signal-with-data
438 im "retrieve-surrounding" [ on-retrieve-surrounding yield ]
439 GtkIMContext:retrieve-surrounding win connect-signal-with-data
440 im "delete-surrounding" [ on-delete-surrounding yield ]
441 GtkIMContext:delete-surrounding win connect-signal-with-data
442 im "preedit-changed" [ on-preedit-changed yield ]
443 GtkIMContext:preedit-changed win connect-signal-with-data
445 win "key-press-event" [ im-on-key-event yield ]
446 GtkWidget:key-press-event im connect-signal-with-data
447 win "key-release-event" [ im-on-key-event yield ]
448 GtkWidget:key-release-event im connect-signal-with-data
449 win "focus-in-event" [ im-on-focus-in yield ]
450 GtkWidget:focus-out-event im connect-signal-with-data
451 win "focus-out-event" [ im-on-focus-out yield ]
452 GtkWidget:focus-out-event im connect-signal-with-data
453 win "destroy" [ im-on-destroy yield ]
454 GtkObject:destroy im connect-signal-with-data ;
456 CONSTANT: window-controls>decor-flags
459 { minimize-button $ GDK_DECOR_MINIMIZE }
460 { maximize-button $ GDK_DECOR_MAXIMIZE }
461 { resize-handles $ GDK_DECOR_RESIZEH }
462 { small-title-bar $ GDK_DECOR_TITLE }
463 { normal-title-bar $ GDK_DECOR_TITLE }
464 { textured-background 0 }
467 CONSTANT: window-controls>func-flags
469 { close-button $ GDK_FUNC_CLOSE }
470 { minimize-button $ GDK_FUNC_MINIMIZE }
471 { maximize-button $ GDK_FUNC_MAXIMIZE }
472 { resize-handles $ GDK_FUNC_RESIZE }
473 { small-title-bar 0 }
474 { normal-title-bar 0 }
475 { textured-background 0 }
478 : configure-window-controls ( win controls -- )
480 small-title-bar swap member-eq?
481 GDK_WINDOW_TYPE_HINT_UTILITY GDK_WINDOW_TYPE_HINT_NORMAL ?
482 gtk_window_set_type_hint
484 [ gtk_widget_get_window ] dip
485 window-controls>decor-flags symbols>flags
486 GDK_DECOR_BORDER bitor gdk_window_set_decorations
488 [ gtk_widget_get_window ] dip
489 window-controls>func-flags symbols>flags
490 GDK_FUNC_MOVE bitor gdk_window_set_functions
493 : setup-gl ( world -- ? )
495 [ handle>> window>> ] [ handle>> ] bi*
496 f t GDK_GL_RGBA_TYPE gtk_widget_set_gl_capability
497 ] with-world-pixel-format ;
499 : auto-position ( win loc -- )
501 drop dup window topmost-window =
502 GTK_WIN_POS_CENTER GTK_WIN_POS_NONE ?
503 gtk_window_set_position
504 ] [ first2 gtk_window_move ] if ;
506 M:: gtk-ui-backend (open-window) ( world -- )
507 GTK_WINDOW_TOPLEVEL gtk_window_new :> win
508 gtk_im_multicontext_new :> im
510 win im <window-handle> world handle<<
512 world win register-window
514 win world [ window-loc>> auto-position ]
515 [ dim>> first2 gtk_window_set_default_size ] 2bi
519 win gtk_widget_realize
520 win world window-controls>> configure-window-controls
525 win gtk_widget_show_all ;
527 M: gtk-ui-backend (close-window) ( handle -- )
528 window>> [ gtk_widget_destroy ] [ unregister-window ] bi
529 event-loop? [ gtk_main_quit ] unless ;
531 M: gtk-ui-backend set-title
532 swap [ handle>> window>> ] [ utf8 string>alien ] bi*
533 gtk_window_set_title ;
535 M: gtk-ui-backend (set-fullscreen)
536 [ handle>> ] dip [ >>fullscreen? ] keep
538 [ gtk_window_fullscreen ]
539 [ gtk_window_unfullscreen ] if ;
541 M: gtk-ui-backend (fullscreen?)
542 handle>> fullscreen?>> ;
544 M: gtk-ui-backend raise-window*
545 handle>> window>> gtk_window_present ;
547 : set-cursor ( win cursor -- )
549 [ gtk_widget_get_window ] dip
550 gdk_cursor_new &gdk_cursor_unref
551 gdk_window_set_cursor
554 M: gtk-ui-backend (grab-input)
556 [ gtk_grab_add ] [ GDK_BLANK_CURSOR set-cursor ] bi ;
558 M: gtk-ui-backend (ungrab-input)
560 [ gtk_grab_remove ] [ GDK_LEFT_PTR set-cursor ] bi ;
562 M: window-handle select-gl-context ( handle -- )
564 [ gtk_widget_get_gl_window ] [ gtk_widget_get_gl_context ] bi
565 gdk_gl_drawable_make_current drop ;
567 M: window-handle flush-gl-context ( handle -- )
568 window>> gtk_widget_get_gl_window
569 gdk_gl_drawable_swap_buffers ;
571 M: gtk-ui-backend beep
574 M:: gtk-ui-backend system-alert ( caption text -- )
575 f GTK_DIALOG_MODAL GTK_MESSAGE_WARNING GTK_BUTTONS_OK
576 caption utf8 string>alien f gtk_message_dialog_new
577 [ text utf8 string>alien f gtk_message_dialog_format_secondary_text ]
578 [ gtk_dialog_run drop ]
579 [ gtk_widget_destroy ] tri ;
581 M: gtk-clipboard clipboard-contents
583 handle>> gtk_clipboard_wait_for_text
584 [ &g_free utf8 alien>string ] [ f ] if*
587 M: gtk-clipboard set-clipboard-contents
588 swap [ handle>> ] [ utf8 string>alien ] bi*
589 -1 gtk_clipboard_set_text ;
591 gtk-ui-backend ui-backend set-global
593 [ "ui.tools" ] main-vocab-hook set-global