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
4 call namespaces opengl sequences io combinators
5 combinators.short-circuit fry math.vectors math.rectangles cache
6 ui.gadgets ui.gestures ui.render ui.text ui.text.private
7 ui.backend ui.gadgets.tracks ui.commands ;
13 title status status-owner
14 text-handle handle images
17 : find-world ( gadget -- world/f ) [ world? ] find-parent ;
19 : show-status ( string/f gadget -- )
22 [ (>>status-owner) ] [ status>> set-model ] bi
26 : hide-status ( gadget -- )
28 [ status-owner>> eq? ] keep
29 '[ f _ [ (>>status-owner) ] [ status>> set-model ] 2bi ] when
32 ERROR: no-world-found ;
34 : find-gl-context ( gadget -- )
36 [ handle>> select-gl-context ] [ no-world-found ] if ;
38 : (request-focus) ( child world ? -- )
39 pick parent>> pick eq? [
40 [ dup parent>> dup ] 2dip
41 [ (request-focus) ] keep
42 ] unless focus-child ;
44 M: world request-focus-on ( child gadget -- )
46 [ 2drop ] [ dup focused?>> (request-focus) ] if ;
48 : new-world ( gadget title status class -- world )
49 vertical swap new-track
56 dup init-text-rendering
59 : <world> ( gadget title status -- world )
62 : as-big-as-possible ( world gadget -- )
63 dup [ { 0 0 } >>loc over dim>> >>dim ] when 2drop ; inline
67 [ dup layers>> [ as-big-as-possible ] with each ] bi ;
69 M: world focusable-child* gadget-child ;
71 M: world children-on nip children>> ;
73 M: world remove-gadget
75 [ layers>> delq ] [ call-next-method ] if ;
77 : (draw-world) ( world -- )
82 [ finish-text-rendering ]
83 [ images>> [ purge-cache ] when* ]
87 : draw-world? ( world -- ? )
88 #! We don't draw deactivated worlds, or those with 0 size.
89 #! On Windows, the latter case results in GL errors.
90 { [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] } 1&& ;
92 TUPLE: world-error error world ;
94 C: <world-error> world-error
98 : ui-error ( error -- )
99 ui-error-hook get [ call( error -- ) ] [ die drop ] if* ;
101 ui-error-hook [ [ rethrow ] ] initialize
103 : draw-world ( world -- )
107 over <world-error> ui-error
115 [ [ { C+ } ] dip f <key-down> ]
116 [ '[ _ send-action ] ]
120 { T{ button-down f { C+ } 1 } [ drop T{ button-down f f 3 } button-gesture ] }
121 { T{ button-down f { A+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
122 { T{ button-down f { M+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
123 { T{ button-up f { C+ } 1 } [ drop T{ button-up f f 3 } button-gesture ] }
124 { T{ button-up f { A+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
125 { T{ button-up f { M+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
126 } assoc-union set-gestures
128 PREDICATE: specific-button-up < button-up #>> ;
129 PREDICATE: specific-button-down < button-down #>> ;
130 PREDICATE: specific-drag < drag #>> ;
132 : generalize-gesture ( gesture -- )
133 clone f >># button-gesture ;
135 M: world handle-gesture ( gesture gadget -- ? )
136 2dup call-next-method [
138 { [ over specific-button-up? ] [ drop generalize-gesture f ] }
139 { [ over specific-button-down? ] [ drop generalize-gesture f ] }
140 { [ over specific-drag? ] [ drop generalize-gesture f ] }
145 : close-global ( world global -- )
146 [ get-global find-world eq? ] keep '[ f _ set-global ] when ;