]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/backend/gtk/gtk.factor
Merge branch 'gtk' into gtk-image-loader
[factor.git] / basis / ui / backend / gtk / gtk.factor
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 ;
14 IN: ui.backend.gtk
15
16 SINGLETON: gtk-ui-backend
17
18 TUPLE: handle ;
19 TUPLE: window-handle < handle window fullscreen? im-context im-menu ;
20
21 : <window-handle> ( window im-context -- window-handle )
22     window-handle new
23         swap >>im-context
24         swap >>window ;
25
26 TUPLE: gtk-clipboard handle ;
27
28 C: <gtk-clipboard> gtk-clipboard
29
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 } }
50 }
51
52 M: gtk-ui-backend (make-pixel-format)
53     nip >gl-config-attribs-int-array gdk_gl_config_new ;
54
55 M: gtk-ui-backend (free-pixel-format)
56     handle>> g_object_unref ;
57
58 M: gtk-ui-backend (pixel-format-attribute)
59     [ handle>> ] [ >gl-config-attribs ] bi*
60     { int } [ gdk_gl_config_get_attrib drop ]
61     with-out-parameters ;
62
63 CONSTANT: events-mask
64     flags{
65         GDK_POINTER_MOTION_MASK
66         GDK_POINTER_MOTION_HINT_MASK
67         GDK_ENTER_NOTIFY_MASK
68         GDK_LEAVE_NOTIFY_MASK
69         GDK_BUTTON_PRESS_MASK
70         GDK_BUTTON_RELEASE_MASK
71         GDK_KEY_PRESS_MASK
72         GDK_KEY_RELEASE_MASK
73         GDK_FOCUS_CHANGE_MASK
74     }
75
76 CONSTANT: modifiers
77     {
78         { S+ $ GDK_SHIFT_MASK }
79         { C+ $ GDK_CONTROL_MASK }
80         { A+ $ GDK_MOD1_MASK }
81     }
82
83 CONSTANT: action-key-codes
84     H{
85         { $ GDK_BackSpace "BACKSPACE" }
86         { $ GDK_Tab "TAB" }
87         { $ GDK_Return "RET" }
88         { $ GDK_KP_Enter "ENTER" }
89         { $ GDK_Escape "ESC" }
90         { $ GDK_Delete "DELETE" }
91         { $ GDK_Home "HOME" }
92         { $ GDK_Left "LEFT" }
93         { $ GDK_Up "UP" }
94         { $ GDK_Right "RIGHT" }
95         { $ GDK_Down "DOWN" }
96         { $ GDK_Page_Up "PAGE_UP" }
97         { $ GDK_Page_Down "PAGE_DOWN" }
98         { $ GDK_End "END" }
99         { $ GDK_Begin "BEGIN" }
100         { $ GDK_F1 "F1" }
101         { $ GDK_F2 "F2" }
102         { $ GDK_F3 "F3" }
103         { $ GDK_F4 "F4" }
104         { $ GDK_F5 "F5" }
105         { $ GDK_F6 "F6" }
106         { $ GDK_F7 "F7" }
107         { $ GDK_F8 "F8" }
108         { $ GDK_F9 "F9" }
109         { $ GDK_F10 "F10" }
110         { $ GDK_F11 "F11" }
111         { $ GDK_F12 "F12" }
112     }
113
114 : event-modifiers ( event -- seq )
115     state>> modifiers modifier ;
116
117 : event-loc ( event -- loc )
118     [ x>> ] [ y>> ] bi [ >fixnum ] bi@ 2array ;
119
120 : event-dim ( event -- dim )
121     [ width>> ] [ height>> ] bi 2array ;
122
123 : scroll-direction ( event -- pair )
124     direction>> {
125         { $ GDK_SCROLL_UP { 0 -1 } }
126         { $ GDK_SCROLL_DOWN { 0 1 } }
127         { $ GDK_SCROLL_LEFT { -1 0 } }
128         { $ GDK_SCROLL_RIGHT { 1 0 } }
129     } at ;
130
131 : mouse-event>gesture ( event -- modifiers button loc )
132     [ event-modifiers ] [ button>> ] [ event-loc ] tri ;
133
134 : on-motion ( sender event user-data -- result )
135     drop swap
136     [ GdkEventMotion memory>struct event-loc ] dip window
137     move-hand fire-motion t ;
138
139 : on-enter ( sender event user-data -- result )
140     on-motion ;
141
142 : on-leave ( sender event user-data -- result )
143     3drop forget-rollover t ;
144
145 : on-button-press ( sender event user-data -- result )
146     drop swap [
147         GdkEventButton memory>struct
148         mouse-event>gesture [ <button-down> ] dip
149     ] dip window send-button-down t ;
150
151 : on-button-release ( sender event user-data -- result )
152     drop swap [
153         GdkEventButton memory>struct
154         mouse-event>gesture [ <button-up> ] dip
155     ] dip window send-button-up t ;
156
157 : on-scroll ( sender event user-data -- result )
158     drop swap [
159         GdkEventScroll memory>struct
160         [ scroll-direction ] [ event-loc ] bi
161     ] dip window send-scroll t ;
162
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 ;
166
167 : key-event>gesture ( event -- mods sym/f action? )
168     GdkEventKey memory>struct
169     [ event-modifiers ] [ key-sym ] bi ;
170   
171 : on-key-press ( sender event user-data -- result )
172     drop swap [ key-event>gesture <key-down> ] [ window ] bi*
173     propagate-key-gesture t ;
174
175 : on-key-release ( sender event user-data -- result )
176     drop swap [ key-event>gesture <key-up> ] [ window ] bi*
177     propagate-key-gesture t ;
178
179 : on-focus-in ( sender event user-data -- result )
180     2drop window focus-world t ;
181
182 : on-focus-out ( sender event user-data -- result )
183     2drop window unfocus-world t ;
184
185 : on-expose ( sender event user-data -- result )
186     2drop window relayout t ;
187
188 : on-configure ( sender event user-data -- result )
189     drop [ window ] dip GdkEventConfigure memory>struct
190     [ event-loc >>window-loc ] [ event-dim >>dim  ] bi
191     relayout-1 f ;
192
193 : on-delete ( sender event user-data -- result )
194     2drop window ungraft t ;
195
196 : init-clipboard ( -- )
197     selection "PRIMARY"
198     clipboard "CLIPBOARD"
199     [
200         utf8 string>alien gdk_atom_intern_static_string
201         gtk_clipboard_get <gtk-clipboard> swap set-global
202     ] 2bi@ ;
203
204 : io-source-prepare ( source timeout -- result )
205     2drop f ;
206
207 : io-source-check ( source -- result )
208     poll_fds>> 0 g_slist_nth_data GPollFD memory>struct
209     revents>> 0 = not ;
210
211 : io-source-dispatch ( source callback user_data -- result )
212      3drop
213      0 mx get wait-for-events
214      yield t ;
215
216 CONSTANT: poll-fd-events
217     flags{
218         G_IO_IN
219         G_IO_OUT
220         G_IO_PRI
221         G_IO_ERR
222         G_IO_HUP
223         G_IO_NVAL
224     }
225
226 : create-poll-fd ( -- poll-fd )
227     GPollFD malloc-struct &free
228         mx get fd>> >>fd
229         poll-fd-events >>events ;
230
231 HOOK: init-io-event-source io-backend ( -- )
232
233 M: f init-io-event-source ;
234 M: c-io-backend init-io-event-source ;
235
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 ;
244
245 SYMBOL: next-timeout
246
247 : set-timeout*-value ( alien value -- )
248     swap 0 set-alien-signed-4 ; inline
249
250 : timeout-prepare ( source timeout* -- result )
251     nip next-timeout get-global nano-count [-]
252     [ 1,000,000 /i set-timeout*-value ] keep 0 = ;
253
254 : timeout-check ( source -- result )
255     drop next-timeout get-global nano-count [-] 0 = ;
256
257 : timeout-dispatch ( source callback user_data -- result )
258     3drop sleep-time [ 1,000,000,000 ] unless* nano-count +
259     next-timeout set-global
260     yield t ;
261
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 ;
270
271 : load-icon ( -- )
272     ! This file is not in a resource.txt because it can be
273     ! overwritten when deploying. See 'Vocabulary icons'
274     ! in the docs.
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
279     handle-GError drop ;
280
281 M: gtk-ui-backend (with-ui)
282     [
283         f f gtk_init
284         f f gtk_gl_init
285         load-icon
286         init-clipboard
287         start-ui
288         stop-io-thread
289         [
290             init-io-event-source
291             init-timeout
292             gtk_main
293         ] with-destructors
294     ] ui-running ;
295
296 : connect-signal-with-data ( object signal-name callback data -- )
297     [ utf8 string>alien ] 2dip g_signal_connect drop ;
298
299 : connect-signal ( object signal-name callback -- )
300     f connect-signal-with-data ;
301
302 :: connect-signals ( win -- )
303     win events-mask gtk_widget_add_events
304     
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 ;
331
332 ! ----------------------
333
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 )
339
340 M: gadget support-input-methods? drop f ;
341
342 M: editor support-input-methods? drop t ;
343
344 M: editor get-cursor-surrounding
345     dup editor-caret first2 [ swap editor-line ] dip ;
346
347 M: editor delete-cursor-surrounding
348     3drop ;
349
350 M: editor set-preedit-string
351     nip dup [ editor-caret ] keep
352     [ user-input* drop ] 2dip
353     set-caret ;
354
355 M: editor get-cursor-loc&dim
356     [ caret-loc ] [ caret-dim ] bi ;
357
358 ! ----------------------
359
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
364     ] [ 2drop f ] if ;
365
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 ;
369
370 : get-preedit-string ( im-context -- str cursor-pos )
371     { void* int } [ f swap gtk_im_context_get_preedit_string ]
372     with-out-parameters 
373     [ [ utf8 alien>string ] [ g_free ] bi ] dip ;
374             
375 : on-preedit-changed ( im-context user-data -- )
376     window world-focus dup support-input-methods? [
377         [ get-preedit-string ] dip set-preedit-string
378     ] [ 2drop ] if ;
379
380 : on-commit ( sender str user_data -- )
381     [ drop ] [ utf8 alien>string ] [ window ] tri* user-input ;
382
383 : gadget-location ( gadget -- loc )
384     [ loc>> ] [ parent>> [ gadget-location v+ ] when* ] bi ;
385
386 : gadget-cursor-location ( gadget -- rectangle )
387     [ gadget-location ] [ get-cursor-loc&dim ] bi [ v+ ] dip
388     [ first2 ] bi@ GdkRectangle <struct-boa> ;
389
390 : update-cursor-location ( im-context gadget -- )
391     gadget-cursor-location gtk_im_context_set_cursor_location ;
392
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 ;
400
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 ;
404
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 ;
408
409 : im-on-destroy ( sender user-data -- )
410     nip [ f gtk_im_context_set_client_window ]
411     [ g_object_unref ] bi ;
412
413 ! for testing only
414
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 ;
418
419 : im-menu ( world -- )
420     { com-input-method } show-commands-menu ;
421
422 editor "input-method" f  {
423     { T{ button-down f { S+ C+ } 3 } im-menu }
424 } define-command-map
425
426 ! --------
427
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
431
432     gtk_menu_new :> menu
433     im menu gtk_im_multicontext_append_menuitems
434     menu win window handle>> im-menu<<
435     
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
444
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 ;
455
456 CONSTANT: window-controls>decor-flags
457     H{
458         { close-button 0 }
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 }
465     }
466     
467 CONSTANT: window-controls>func-flags
468     H{
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 }
476     }
477
478 : configure-window-controls ( win controls -- )
479     [
480         small-title-bar swap member-eq?
481         GDK_WINDOW_TYPE_HINT_UTILITY GDK_WINDOW_TYPE_HINT_NORMAL ?
482         gtk_window_set_type_hint
483     ] [
484         [ gtk_widget_get_window ] dip
485         window-controls>decor-flags symbols>flags
486         GDK_DECOR_BORDER bitor gdk_window_set_decorations
487     ] [
488         [ gtk_widget_get_window ] dip
489         window-controls>func-flags symbols>flags
490         GDK_FUNC_MOVE bitor gdk_window_set_functions
491     ] 2tri ;
492
493 : setup-gl ( world -- ? )
494     [
495         [ handle>> window>> ] [ handle>> ] bi*
496         f t GDK_GL_RGBA_TYPE gtk_widget_set_gl_capability
497     ] with-world-pixel-format ;
498
499 : auto-position ( win loc -- )
500     dup { 0 0 } = [
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 ;
505
506 M:: gtk-ui-backend (open-window) ( world -- )
507     GTK_WINDOW_TOPLEVEL gtk_window_new :> win
508     gtk_im_multicontext_new :> im
509     
510     win im <window-handle> world handle<<
511
512     world win register-window
513     
514     win world [ window-loc>> auto-position ]
515     [ dim>> first2 gtk_window_set_default_size ] 2bi
516     
517     world setup-gl drop
518
519     win gtk_widget_realize
520     win world window-controls>> configure-window-controls
521     
522     win im configure-im
523     win connect-signals
524
525     win gtk_widget_show_all ;
526
527 M: gtk-ui-backend (close-window) ( handle -- )
528     window>> [ gtk_widget_destroy ] [ unregister-window ] bi
529     event-loop? [ gtk_main_quit ] unless ;
530
531 M: gtk-ui-backend set-title
532     swap [ handle>> window>> ] [ utf8 string>alien ] bi*
533     gtk_window_set_title ;
534
535 M: gtk-ui-backend (set-fullscreen)
536     [ handle>> ] dip [ >>fullscreen? ] keep
537     [ window>> ] dip
538     [ gtk_window_fullscreen ]
539     [ gtk_window_unfullscreen ] if ;
540
541 M: gtk-ui-backend (fullscreen?)
542     handle>> fullscreen?>> ;
543     
544 M: gtk-ui-backend raise-window*
545     handle>> window>> gtk_window_present ;
546
547 : set-cursor ( win cursor -- )
548     [
549         [ gtk_widget_get_window ] dip
550         gdk_cursor_new &gdk_cursor_unref
551         gdk_window_set_cursor
552     ] with-destructors ;
553
554 M: gtk-ui-backend (grab-input)
555     window>>
556     [ gtk_grab_add ] [ GDK_BLANK_CURSOR set-cursor ] bi ;
557
558 M: gtk-ui-backend (ungrab-input)
559     window>>
560     [ gtk_grab_remove ] [ GDK_LEFT_PTR set-cursor ] bi ;
561
562 M: window-handle select-gl-context ( handle -- )
563     window>>
564     [ gtk_widget_get_gl_window ] [ gtk_widget_get_gl_context ] bi
565     gdk_gl_drawable_make_current drop ;
566
567 M: window-handle flush-gl-context ( handle -- )
568     window>> gtk_widget_get_gl_window
569     gdk_gl_drawable_swap_buffers ;
570
571 M: gtk-ui-backend beep
572     gdk_beep ;
573
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 ;
580
581 M: gtk-clipboard clipboard-contents
582     [
583         handle>> gtk_clipboard_wait_for_text
584         [ &g_free utf8 alien>string ] [ f ] if*
585     ] with-destructors ;
586
587 M: gtk-clipboard set-clipboard-contents
588     swap [ handle>> ] [ utf8 string>alien ] bi*
589     -1 gtk_clipboard_set_text ;
590
591 gtk-ui-backend ui-backend set-global
592
593 [ "ui.tools" ] main-vocab-hook set-global