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 } ;
55 : <world-attributes> ( -- world-attributes )
56 world-attributes new ; inline
58 : find-world ( gadget -- world/f ) [ world? ] find-parent ;
60 : grab-input ( gadget -- )
61 find-world dup grab-input?>>
64 dup focused?>> [ handle>> (grab-input) ] [ drop ] if
67 : ungrab-input ( gadget -- )
68 find-world dup grab-input?>>
71 dup focused?>> [ handle>> (ungrab-input) ] [ drop ] if
74 : show-status ( string/f gadget -- )
77 [ (>>status-owner) ] [ status>> set-model ] bi
81 : hide-status ( gadget -- )
83 [ status-owner>> eq? ] keep
84 '[ f _ [ (>>status-owner) ] [ status>> set-model ] 2bi ] when
87 : window-resource ( resource -- resource )
88 dup world get-global window-resources>> push ;
90 : set-gl-context ( world -- )
92 [ handle>> select-gl-context ] bi ;
94 : with-gl-context ( world quot -- )
96 [ handle>> flush-gl-context gl-error ] bi ; inline
98 ERROR: no-world-found ;
100 : find-gl-context ( gadget -- )
102 [ set-gl-context ] [ no-world-found ] if ;
104 : (request-focus) ( child world ? -- )
105 pick parent>> pick eq? [
106 [ dup parent>> dup ] 2dip
107 [ (request-focus) ] keep
108 ] unless focus-child ;
110 M: world request-focus-on ( child gadget -- )
112 [ 2drop ] [ dup focused?>> (request-focus) ] if ;
114 : new-world ( class -- world )
115 vertical swap new-track
120 V{ } clone >>window-resources ;
122 : initial-background-color ( attributes -- color )
123 window-controls>> textured-background swap member-eq?
124 [ T{ rgba f 0.0 0.0 0.0 0.0 } ]
125 [ T{ rgba f 1.0 1.0 1.0 1.0 } ] if ;
127 : apply-world-attributes ( world attributes -- world )
130 [ status>> >>status ]
131 [ pixel-format-attributes>> >>pixel-format-attributes ]
132 [ window-controls>> >>window-controls ]
133 [ initial-background-color >>background-color ]
134 [ grab-input?>> >>grab-input? ]
135 [ gadgets>> [ 1 track-add ] each ]
138 : <world> ( world-attributes -- world )
139 [ world-class>> new-world ] keep apply-world-attributes
142 : as-big-as-possible ( world gadget -- )
143 dup [ { 0 0 } >>loc over dim>> >>dim ] when 2drop ; inline
147 [ dup layers>> [ as-big-as-possible ] with each ] bi ;
149 M: world focusable-child* children>> [ t ] [ first ] if-empty ;
151 M: world children-on nip children>> ;
153 M: world remove-gadget
154 2dup layers>> member-eq?
155 [ layers>> remove-eq! drop ] [ call-next-method ] if ;
157 SYMBOL: flush-layout-cache-hook
159 flush-layout-cache-hook [ [ ] ] initialize
161 GENERIC: begin-world ( world -- )
162 GENERIC: end-world ( world -- )
164 GENERIC: resize-world ( world -- )
170 M: world resize-world
178 [ [ set-gl-context ] [ resize-world ] bi ]
183 GENERIC: draw-world* ( world -- )
190 [ text-handle>> [ purge-cache ] when* ]
191 [ images>> [ purge-cache ] when* ]
194 : draw-world? ( world -- ? )
195 #! We don't draw deactivated worlds, or those with 0 size.
196 #! On Windows, the latter case results in GL errors.
197 { [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] } 1&& ;
199 TUPLE: world-error error world ;
201 C: <world-error> world-error
203 SYMBOL: ui-error-hook
205 : ui-error ( error -- )
206 ui-error-hook get [ call( error -- ) ] [ die drop ] if* ;
208 ui-error-hook [ [ rethrow ] ] initialize
210 : draw-world ( world -- )
214 dup [ draw-world* ] with-gl-context
215 flush-layout-cache-hook get call( -- )
217 over <world-error> ui-error
225 [ [ { C+ } ] dip f <key-down> ]
226 [ '[ _ send-action ] ]
230 { T{ button-down f { C+ } 1 } [ drop T{ button-down f f 3 } button-gesture ] }
231 { T{ button-down f { A+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
232 { T{ button-down f { M+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
233 { T{ button-up f { C+ } 1 } [ drop T{ button-up f f 3 } button-gesture ] }
234 { T{ button-up f { A+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
235 { T{ button-up f { M+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
236 } assoc-union set-gestures
238 PREDICATE: specific-button-up < button-up #>> ;
239 PREDICATE: specific-button-down < button-down #>> ;
240 PREDICATE: specific-drag < drag #>> ;
242 : generalize-gesture ( gesture -- )
243 clone f >># button-gesture ;
245 M: world handle-gesture ( gesture gadget -- ? )
246 2dup call-next-method [
248 { [ over specific-button-up? ] [ drop generalize-gesture f ] }
249 { [ over specific-button-down? ] [ drop generalize-gesture f ] }
250 { [ over specific-drag? ] [ drop generalize-gesture f ] }
255 : close-global ( world global -- )
256 [ get-global find-world eq? ] keep '[ f _ set-global ] when ;
258 M: world world-pixel-format-attributes
259 pixel-format-attributes>> ;
261 M: world check-world-pixel-format
264 : with-world-pixel-format ( world quot -- )
265 [ dup dup world-pixel-format-attributes <pixel-format> ]
266 dip [ 2dup check-world-pixel-format ] prepose with-disposal ; inline