1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs cache colors combinators
4 combinators.short-circuit concurrency.promises continuations
5 destructors fry kernel literals math models namespaces opengl
6 opengl.capabilities opengl.textures sequences strings ui.backend
7 ui.gadgets ui.gadgets.tracks ui.gestures ui.pixel-formats
21 CONSTANT: default-world-pixel-format-attributes
27 CONSTANT: default-world-window-controls
37 active? focused? grab-input? fullscreen?
40 title status status-owner
41 text-handle handle images
43 pixel-format-attributes
49 TUPLE: world-attributes
50 { world-class initial: world }
52 { title string initial: "Factor Window" }
55 { pixel-format-attributes initial: $ default-world-pixel-format-attributes }
56 { window-controls initial: $ default-world-window-controls }
59 : <world-attributes> ( -- world-attributes )
60 world-attributes new ; inline
62 : find-world ( gadget -- world/f ) [ world? ] find-parent ;
64 : grab-input ( gadget -- )
65 find-world dup grab-input?>>
68 dup focused?>> [ handle>> (grab-input) ] [ drop ] if
71 : ungrab-input ( gadget -- )
72 find-world dup grab-input?>>
75 dup focused?>> [ handle>> (ungrab-input) ] [ drop ] if
78 : show-status ( string/f gadget -- )
81 [ status-owner<< ] [ status>> set-model ] bi
85 : hide-status ( gadget -- )
87 [ status-owner>> eq? ] keep
88 '[ f _ [ status-owner<< ] [ status>> set-model ] 2bi ] when
91 : window-resource ( resource -- resource )
92 dup world get-global window-resources>> push ;
94 : set-gl-context ( world -- )
96 [ handle>> select-gl-context ] bi ;
98 : with-gl-context ( world quot -- )
100 [ handle>> flush-gl-context gl-error ] bi ; inline
102 ERROR: no-world-found ;
104 : find-gl-context ( gadget -- )
106 [ set-gl-context ] [ no-world-found ] if ;
108 : (request-focus) ( child world ? -- )
109 pick parent>> pick eq? [
110 [ dup parent>> dup ] 2dip
111 [ (request-focus) ] keep
112 ] unless focus-child ;
114 M: world request-focus-on ( child gadget -- )
116 [ 2drop ] [ dup focused?>> (request-focus) ] if ;
118 : new-world ( class -- world )
119 vertical swap new-track
124 V{ } clone >>window-resources
125 <promise> >>promise ;
127 : initial-background-color ( attributes -- color )
128 window-controls>> textured-background swap member-eq?
129 [ T{ rgba f 0.0 0.0 0.0 0.0 } ]
130 [ T{ rgba f 1.0 1.0 1.0 1.0 } ] if ;
132 GENERIC# apply-world-attributes 1 ( world attributes -- world )
134 M: world apply-world-attributes
137 [ status>> >>status ]
138 [ pixel-format-attributes>> >>pixel-format-attributes ]
139 [ window-controls>> >>window-controls ]
140 [ initial-background-color >>background-color ]
141 [ grab-input?>> >>grab-input? ]
142 [ gadgets>> dup sequence? [ [ 1 track-add ] each ] [ 1 track-add ] if ]
143 [ pref-dim>> >>pref-dim ]
146 : <world> ( world-attributes -- world )
147 [ world-class>> new-world ] keep apply-world-attributes
150 : as-big-as-possible ( world gadget -- )
151 dup [ { 0 0 } >>loc over dim>> >>dim ] when 2drop ; inline
155 [ dup layers>> [ as-big-as-possible ] with each ] bi ;
157 M: world focusable-child* children>> [ t ] [ first ] if-empty ;
159 M: world children-on nip children>> ;
161 M: world remove-gadget
162 2dup layers>> member-eq?
163 [ layers>> remove-eq! drop ] [ call-next-method ] if ;
165 SYMBOL: flush-layout-cache-hook
167 flush-layout-cache-hook [ [ ] ] initialize
169 GENERIC: begin-world ( world -- )
170 GENERIC: end-world ( world -- )
171 GENERIC: resize-world ( world -- )
173 M: world begin-world drop ;
174 M: world end-world drop ;
175 M: world resize-world drop ;
182 [ [ set-gl-context ] [ resize-world ] bi ]
187 GENERIC: draw-world* ( world -- )
191 "1.0" require-gl-version
195 [ text-handle>> [ purge-cache ] when* ]
196 [ images>> [ purge-cache ] when* ]
199 : draw-world? ( world -- ? )
200 #! We don't draw deactivated worlds, or those with 0 size.
201 #! On Windows, the latter case results in GL errors.
202 { [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] } 1&& ;
204 TUPLE: world-error error world ;
206 C: <world-error> world-error
208 SYMBOL: ui-error-hook
210 : ui-error ( error -- )
211 ui-error-hook get [ call( error -- ) ] [ die drop ] if* ;
213 ui-error-hook [ [ rethrow ] ] initialize
215 : draw-world ( world -- )
219 dup [ draw-world* ] with-gl-context
220 flush-layout-cache-hook get call( -- )
222 swap f >>active? <world-error> ui-error
229 [ [ { C+ } ] dip f <key-down> ]
230 [ '[ _ send-action ] ]
234 { T{ key-down f { S+ } "DELETE" } [ \ cut-action send-action ] }
235 { T{ key-down f { S+ } "INSERT" } [ \ paste-action send-action ] }
236 { T{ key-down f { C+ } "INSERT" } [ \ copy-action send-action ] }
237 { T{ button-down f { C+ } 1 } [ drop T{ button-down f f 3 } button-gesture ] }
238 { T{ button-down f { A+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
239 { T{ button-down f { M+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
240 { T{ button-up f { C+ } 1 } [ drop T{ button-up f f 3 } button-gesture ] }
241 { T{ button-up f { A+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
242 { T{ button-up f { M+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
243 } assoc-union set-gestures
245 PREDICATE: specific-button-up < button-up #>> ;
246 PREDICATE: specific-button-down < button-down #>> ;
247 PREDICATE: specific-drag < drag #>> ;
249 : generalize-gesture ( gesture -- )
250 clone f >># button-gesture ;
252 M: world handle-gesture ( gesture gadget -- ? )
253 2dup call-next-method [
255 { [ over specific-button-up? ] [ drop generalize-gesture f ] }
256 { [ over specific-button-down? ] [ drop generalize-gesture f ] }
257 { [ over specific-drag? ] [ drop generalize-gesture f ] }
262 : close-global ( world global -- )
263 [ get-global find-world eq? ] keep '[ f _ set-global ] when ;
265 M: world world-pixel-format-attributes
266 pixel-format-attributes>> ;
268 M: world check-world-pixel-format
271 : with-world-pixel-format ( world quot -- )
272 [ dup dup world-pixel-format-attributes <pixel-format> ]
273 dip [ 2dup check-world-pixel-format ] prepose with-disposal ; inline