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 namespaces opengl opengl.textures sequences io combinators
5 combinators.short-circuit fry math.vectors math.rectangles cache
6 ui.gadgets ui.gestures ui.render ui.backend ui.gadgets.tracks
7 ui.commands ui.pixel-formats destructors ;
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
58 : <world> ( gadget title status -- world )
61 : as-big-as-possible ( world gadget -- )
62 dup [ { 0 0 } >>loc over dim>> >>dim ] when 2drop ; inline
66 [ dup layers>> [ as-big-as-possible ] with each ] bi ;
68 M: world focusable-child* gadget-child ;
70 M: world children-on nip children>> ;
72 M: world remove-gadget
74 [ layers>> delq ] [ call-next-method ] if ;
76 SYMBOL: flush-layout-cache-hook
78 flush-layout-cache-hook [ [ ] ] initialize
80 : (draw-world) ( world -- )
86 [ text-handle>> [ purge-cache ] when* ]
87 [ images>> [ purge-cache ] when* ]
90 flush-layout-cache-hook get call( -- ) ;
92 : draw-world? ( world -- ? )
93 #! We don't draw deactivated worlds, or those with 0 size.
94 #! On Windows, the latter case results in GL errors.
95 { [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] } 1&& ;
97 TUPLE: world-error error world ;
99 C: <world-error> world-error
101 SYMBOL: ui-error-hook
103 : ui-error ( error -- )
104 ui-error-hook get [ call( error -- ) ] [ die drop ] if* ;
106 ui-error-hook [ [ rethrow ] ] initialize
108 : draw-world ( world -- )
112 over <world-error> ui-error
120 [ [ { C+ } ] dip f <key-down> ]
121 [ '[ _ send-action ] ]
125 { T{ button-down f { C+ } 1 } [ drop T{ button-down f f 3 } button-gesture ] }
126 { T{ button-down f { A+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
127 { T{ button-down f { M+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
128 { T{ button-up f { C+ } 1 } [ drop T{ button-up f f 3 } button-gesture ] }
129 { T{ button-up f { A+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
130 { T{ button-up f { M+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
131 } assoc-union set-gestures
133 PREDICATE: specific-button-up < button-up #>> ;
134 PREDICATE: specific-button-down < button-down #>> ;
135 PREDICATE: specific-drag < drag #>> ;
137 : generalize-gesture ( gesture -- )
138 clone f >># button-gesture ;
140 M: world handle-gesture ( gesture gadget -- ? )
141 2dup call-next-method [
143 { [ over specific-button-up? ] [ drop generalize-gesture f ] }
144 { [ over specific-button-down? ] [ drop generalize-gesture f ] }
145 { [ over specific-drag? ] [ drop generalize-gesture f ] }
150 : close-global ( world global -- )
151 [ get-global find-world eq? ] keep '[ f _ set-global ] when ;
153 GENERIC: world-pixel-format-attributes ( world -- attributes )
155 GENERIC# check-world-pixel-format 1 ( world pixel-format -- )
157 M: world world-pixel-format-attributes
159 { windowed double-buffered T{ depth-bits { value 16 } } } ;
161 M: world check-world-pixel-format
164 : with-world-pixel-format ( world quot -- )
165 [ dup dup world-pixel-format-attributes <pixel-format> ]
166 dip [ 2dup check-world-pixel-format ] prepose with-disposal ; inline