1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs continuations kernel math models call
4 namespaces opengl sequences io combinators combinators.short-circuit
5 fry math.vectors ui.gadgets ui.gestures ui.render ui.text ui.text.private
6 ui.backend ui.gadgets.tracks math.rectangles ;
16 : find-world ( gadget -- world/f ) [ world? ] find-parent ;
18 : show-status ( string/f gadget -- )
20 status>> dup [ set-model ] [ 2drop ] if
23 : hide-status ( gadget -- ) f swap show-status ;
25 ERROR: no-world-found ;
27 : find-gl-context ( gadget -- )
29 [ handle>> select-gl-context ] [ no-world-found ] if ;
31 : (request-focus) ( child world ? -- )
32 pick parent>> pick eq? [
33 [ dup parent>> dup ] 2dip
34 [ (request-focus) ] keep
35 ] unless focus-child ;
37 M: world request-focus-on ( child gadget -- )
39 [ 2drop ] [ dup focused?>> (request-focus) ] if ;
41 : new-world ( gadget title status class -- world )
42 vertical swap new-track
49 dup init-text-rendering
52 : <world> ( gadget title status -- world )
57 dup glass>> dup [ swap dim>> >>dim drop ] [ 2drop ] if ;
59 M: world focusable-child* gadget-child ;
61 M: world children-on nip children>> ;
63 : (draw-world) ( world -- )
65 [ init-gl ] [ draw-gadget ] [ finish-text-rendering ] tri
68 : draw-world? ( world -- ? )
69 #! We don't draw deactivated worlds, or those with 0 size.
70 #! On Windows, the latter case results in GL errors.
71 { [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] } 1&& ;
73 TUPLE: world-error error world ;
75 C: <world-error> world-error
79 : ui-error ( error -- )
80 ui-error-hook get [ call( error -- ) ] [ die drop ] if* ;
82 ui-error-hook [ [ rethrow ] ] initialize
84 : draw-world ( world -- )
88 over <world-error> ui-error
95 { T{ key-down f { C+ } "z" } [ undo-action send-action ] }
96 { T{ key-down f { C+ } "Z" } [ redo-action send-action ] }
97 { T{ key-down f { C+ } "x" } [ cut-action send-action ] }
98 { T{ key-down f { C+ } "c" } [ copy-action send-action ] }
99 { T{ key-down f { C+ } "v" } [ paste-action send-action ] }
100 { T{ key-down f { C+ } "a" } [ select-all-action send-action ] }
101 { T{ button-down f { C+ } 1 } [ drop T{ button-down f f 3 } button-gesture ] }
102 { T{ button-down f { A+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
103 { T{ button-down f { M+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
104 { T{ button-up f { C+ } 1 } [ drop T{ button-up f f 3 } button-gesture ] }
105 { T{ button-up f { A+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
106 { T{ button-up f { M+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
109 PREDICATE: specific-button-up < button-up #>> ;
110 PREDICATE: specific-button-down < button-down #>> ;
111 PREDICATE: specific-drag < drag #>> ;
113 : generalize-gesture ( gesture -- )
114 clone f >># button-gesture ;
116 M: world handle-gesture ( gesture gadget -- ? )
117 2dup call-next-method [
119 { [ over specific-button-up? ] [ drop generalize-gesture f ] }
120 { [ over specific-button-down? ] [ drop generalize-gesture f ] }
121 { [ over specific-drag? ] [ drop generalize-gesture f ] }
126 : close-global ( world global -- )
127 [ get-global find-world eq? ] keep '[ f _ set-global ] when ;