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.pixel-formats destructors literals strings ;
18 CONSTANT: default-world-pixel-format-attributes
19 { windowed double-buffered T{ depth-bits { value 16 } } }
21 CONSTANT: default-world-window-controls
31 active? focused? grab-input?
33 title status status-owner
34 text-handle handle images
36 pixel-format-attributes
40 TUPLE: world-attributes
41 { world-class initial: world }
43 { title string initial: "Factor Window" }
46 { pixel-format-attributes initial: $ default-world-pixel-format-attributes }
47 { window-controls initial: $ default-world-window-controls } ;
49 : <world-attributes> ( -- world-attributes )
50 world-attributes new ; inline
52 : find-world ( gadget -- world/f ) [ world? ] find-parent ;
54 : grab-input ( gadget -- )
55 find-world dup grab-input?>>
58 dup focused?>> [ handle>> (grab-input) ] [ drop ] if
61 : ungrab-input ( gadget -- )
62 find-world dup grab-input?>>
65 dup focused?>> [ handle>> (ungrab-input) ] [ drop ] if
68 : show-status ( string/f gadget -- )
71 [ (>>status-owner) ] [ status>> set-model ] bi
75 : hide-status ( gadget -- )
77 [ status-owner>> eq? ] keep
78 '[ f _ [ (>>status-owner) ] [ status>> set-model ] 2bi ] when
81 : window-resource ( resource -- resource )
82 dup world get-global window-resources>> push ;
84 : set-gl-context ( world -- )
86 [ handle>> select-gl-context ] bi ;
88 : with-gl-context ( world quot -- )
90 [ handle>> flush-gl-context gl-error ] bi ; inline
92 ERROR: no-world-found ;
94 : find-gl-context ( gadget -- )
96 [ set-gl-context ] [ no-world-found ] if ;
98 : (request-focus) ( child world ? -- )
99 pick parent>> pick eq? [
100 [ dup parent>> dup ] 2dip
101 [ (request-focus) ] keep
102 ] unless focus-child ;
104 M: world request-focus-on ( child gadget -- )
106 [ 2drop ] [ dup focused?>> (request-focus) ] if ;
108 : new-world ( class -- world )
109 vertical swap new-track
114 V{ } clone >>window-resources ;
116 : apply-world-attributes ( world attributes -- world )
119 [ status>> >>status ]
120 [ pixel-format-attributes>> >>pixel-format-attributes ]
121 [ window-controls>> >>window-controls ]
122 [ grab-input?>> >>grab-input? ]
123 [ gadgets>> [ 1 track-add ] each ]
126 : <world> ( world-attributes -- world )
127 [ world-class>> new-world ] keep apply-world-attributes
130 : as-big-as-possible ( world gadget -- )
131 dup [ { 0 0 } >>loc over dim>> >>dim ] when 2drop ; inline
135 [ dup layers>> [ as-big-as-possible ] with each ] bi ;
137 M: world focusable-child* children>> [ t ] [ first ] if-empty ;
139 M: world children-on nip children>> ;
141 M: world remove-gadget
143 [ layers>> delq ] [ call-next-method ] if ;
145 SYMBOL: flush-layout-cache-hook
147 flush-layout-cache-hook [ [ ] ] initialize
149 GENERIC: begin-world ( world -- )
150 GENERIC: end-world ( world -- )
152 GENERIC: resize-world ( world -- )
158 M: world resize-world
166 [ [ set-gl-context ] [ resize-world ] bi ]
171 GENERIC: draw-world* ( world -- )
178 [ text-handle>> [ purge-cache ] when* ]
179 [ images>> [ purge-cache ] when* ]
182 : draw-world? ( world -- ? )
183 #! We don't draw deactivated worlds, or those with 0 size.
184 #! On Windows, the latter case results in GL errors.
185 { [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] } 1&& ;
187 TUPLE: world-error error world ;
189 C: <world-error> world-error
191 SYMBOL: ui-error-hook
193 : ui-error ( error -- )
194 ui-error-hook get [ call( error -- ) ] [ die drop ] if* ;
196 ui-error-hook [ [ rethrow ] ] initialize
198 : draw-world ( world -- )
202 dup [ draw-world* ] with-gl-context
203 flush-layout-cache-hook get call( -- )
205 over <world-error> ui-error
213 [ [ { C+ } ] dip f <key-down> ]
214 [ '[ _ send-action ] ]
218 { T{ button-down f { C+ } 1 } [ drop T{ button-down f f 3 } button-gesture ] }
219 { T{ button-down f { A+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
220 { T{ button-down f { M+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
221 { T{ button-up f { C+ } 1 } [ drop T{ button-up f f 3 } button-gesture ] }
222 { T{ button-up f { A+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
223 { T{ button-up f { M+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
224 } assoc-union set-gestures
226 PREDICATE: specific-button-up < button-up #>> ;
227 PREDICATE: specific-button-down < button-down #>> ;
228 PREDICATE: specific-drag < drag #>> ;
230 : generalize-gesture ( gesture -- )
231 clone f >># button-gesture ;
233 M: world handle-gesture ( gesture gadget -- ? )
234 2dup call-next-method [
236 { [ over specific-button-up? ] [ drop generalize-gesture f ] }
237 { [ over specific-button-down? ] [ drop generalize-gesture f ] }
238 { [ over specific-drag? ] [ drop generalize-gesture f ] }
243 : close-global ( world global -- )
244 [ get-global find-world eq? ] keep '[ f _ set-global ] when ;
246 M: world world-pixel-format-attributes
247 pixel-format-attributes>> ;
249 M: world check-world-pixel-format
252 : with-world-pixel-format ( world quot -- )
253 [ dup dup world-pixel-format-attributes <pixel-format> ]
254 dip [ 2dup check-world-pixel-format ] prepose with-disposal ; inline