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 colors 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 ;
19 CONSTANT: default-world-pixel-format-attributes
23 T{ depth-bits { value 16 } }
26 CONSTANT: default-world-window-controls
36 active? focused? grab-input?
38 title status status-owner
39 text-handle handle images
41 pixel-format-attributes
46 TUPLE: world-attributes
47 { world-class initial: world }
49 { title string initial: "Factor Window" }
52 { pixel-format-attributes initial: $ default-world-pixel-format-attributes }
53 { window-controls initial: $ default-world-window-controls }
56 : <world-attributes> ( -- world-attributes )
57 world-attributes new ; inline
59 : find-world ( gadget -- world/f ) [ world? ] find-parent ;
61 : grab-input ( gadget -- )
62 find-world dup grab-input?>>
65 dup focused?>> [ handle>> (grab-input) ] [ drop ] if
68 : ungrab-input ( gadget -- )
69 find-world dup grab-input?>>
72 dup focused?>> [ handle>> (ungrab-input) ] [ drop ] if
75 : show-status ( string/f gadget -- )
78 [ (>>status-owner) ] [ status>> set-model ] bi
82 : hide-status ( gadget -- )
84 [ status-owner>> eq? ] keep
85 '[ f _ [ (>>status-owner) ] [ status>> set-model ] 2bi ] when
88 : window-resource ( resource -- resource )
89 dup world get-global window-resources>> push ;
91 : set-gl-context ( world -- )
93 [ handle>> select-gl-context ] bi ;
95 : with-gl-context ( world quot -- )
97 [ handle>> flush-gl-context gl-error ] bi ; inline
99 ERROR: no-world-found ;
101 : find-gl-context ( gadget -- )
103 [ set-gl-context ] [ no-world-found ] if ;
105 : (request-focus) ( child world ? -- )
106 pick parent>> pick eq? [
107 [ dup parent>> dup ] 2dip
108 [ (request-focus) ] keep
109 ] unless focus-child ;
111 M: world request-focus-on ( child gadget -- )
113 [ 2drop ] [ dup focused?>> (request-focus) ] if ;
115 : new-world ( class -- world )
116 vertical swap new-track
121 V{ } clone >>window-resources ;
123 : initial-background-color ( attributes -- color )
124 window-controls>> textured-background swap member-eq?
125 [ T{ rgba f 0.0 0.0 0.0 0.0 } ]
126 [ T{ rgba f 1.0 1.0 1.0 1.0 } ] if ;
128 : apply-world-attributes ( world attributes -- world )
131 [ status>> >>status ]
132 [ pixel-format-attributes>> >>pixel-format-attributes ]
133 [ window-controls>> >>window-controls ]
134 [ initial-background-color >>background-color ]
135 [ grab-input?>> >>grab-input? ]
136 [ gadgets>> dup sequence? [ [ 1 track-add ] each ] [ 1 track-add ] if ]
137 [ pref-dim>> >>pref-dim ]
140 : <world> ( world-attributes -- world )
141 [ world-class>> new-world ] keep apply-world-attributes
144 : as-big-as-possible ( world gadget -- )
145 dup [ { 0 0 } >>loc over dim>> >>dim ] when 2drop ; inline
149 [ dup layers>> [ as-big-as-possible ] with each ] bi ;
151 M: world focusable-child* children>> [ t ] [ first ] if-empty ;
153 M: world children-on nip children>> ;
155 M: world remove-gadget
156 2dup layers>> member-eq?
157 [ layers>> remove-eq! drop ] [ call-next-method ] if ;
159 SYMBOL: flush-layout-cache-hook
161 flush-layout-cache-hook [ [ ] ] initialize
163 GENERIC: begin-world ( world -- )
164 GENERIC: end-world ( world -- )
166 GENERIC: resize-world ( world -- )
172 M: world resize-world
180 [ [ set-gl-context ] [ resize-world ] bi ]
185 GENERIC: draw-world* ( world -- )
192 [ text-handle>> [ purge-cache ] when* ]
193 [ images>> [ purge-cache ] when* ]
196 : draw-world? ( world -- ? )
197 #! We don't draw deactivated worlds, or those with 0 size.
198 #! On Windows, the latter case results in GL errors.
199 { [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] } 1&& ;
201 TUPLE: world-error error world ;
203 C: <world-error> world-error
205 SYMBOL: ui-error-hook
207 : ui-error ( error -- )
208 ui-error-hook get [ call( error -- ) ] [ die drop ] if* ;
210 ui-error-hook [ [ rethrow ] ] initialize
212 : draw-world ( world -- )
216 dup [ draw-world* ] with-gl-context
217 flush-layout-cache-hook get call( -- )
219 over <world-error> ui-error
227 [ [ { C+ } ] dip f <key-down> ]
228 [ '[ _ send-action ] ]
232 { T{ button-down f { C+ } 1 } [ drop T{ button-down f f 3 } button-gesture ] }
233 { T{ button-down f { A+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
234 { T{ button-down f { M+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
235 { T{ button-up f { C+ } 1 } [ drop T{ button-up f f 3 } button-gesture ] }
236 { T{ button-up f { A+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
237 { T{ button-up f { M+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
238 } assoc-union set-gestures
240 PREDICATE: specific-button-up < button-up #>> ;
241 PREDICATE: specific-button-down < button-down #>> ;
242 PREDICATE: specific-drag < drag #>> ;
244 : generalize-gesture ( gesture -- )
245 clone f >># button-gesture ;
247 M: world handle-gesture ( gesture gadget -- ? )
248 2dup call-next-method [
250 { [ over specific-button-up? ] [ drop generalize-gesture f ] }
251 { [ over specific-button-down? ] [ drop generalize-gesture f ] }
252 { [ over specific-drag? ] [ drop generalize-gesture f ] }
257 : close-global ( world global -- )
258 [ get-global find-world eq? ] keep '[ f _ set-global ] when ;
260 M: world world-pixel-format-attributes
261 pixel-format-attributes>> ;
263 M: world check-world-pixel-format
266 : with-world-pixel-format ( world quot -- )
267 [ dup dup world-pixel-format-attributes <pixel-format> ]
268 dip [ 2dup check-world-pixel-format ] prepose with-disposal ; inline