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.gl opengl.textures sequences strings
7 ui.backend 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 { orientation initial: $ vertical } ;
61 : <world-attributes> ( -- world-attributes )
62 world-attributes new ; inline
64 : find-world ( gadget -- world/f ) [ world? ] find-parent ;
66 : grab-input ( gadget -- )
67 find-world dup grab-input?>>
70 dup focused?>> [ handle>> (grab-input) ] [ drop ] if
73 : ungrab-input ( gadget -- )
74 find-world dup grab-input?>>
77 dup focused?>> [ handle>> (ungrab-input) ] [ drop ] if
80 : show-status ( string/f gadget -- )
83 [ status-owner<< ] [ status>> set-model ] bi
87 : hide-status ( gadget -- )
89 [ status-owner>> eq? ] keep
90 '[ f _ [ status-owner<< ] [ status>> set-model ] 2bi ] when
93 : window-resource ( resource -- resource )
94 dup world get-global window-resources>> push ;
96 : set-gl-context ( world -- )
98 [ handle>> select-gl-context ] bi ;
100 : with-gl-context ( world quot -- )
101 '[ set-gl-context @ ]
102 [ handle>> flush-gl-context gl-error ] bi ; inline
104 ERROR: no-world-found ;
106 : find-gl-context ( gadget -- )
107 find-world [ set-gl-context ] [ no-world-found ] if* ;
109 : (request-focus) ( child world ? -- )
110 pick parent>> pick eq? [
111 [ dup parent>> dup ] 2dip
112 [ (request-focus) ] keep
113 ] unless focus-child ;
115 M: world request-focus-on
117 [ 2drop ] [ dup focused?>> (request-focus) ] if ;
119 : new-world ( class -- world )
120 vertical swap new-track
125 V{ } clone >>window-resources
126 <promise> >>promise ;
128 : initial-background-color ( attributes -- color )
129 window-controls>> textured-background swap member-eq?
130 [ T{ rgba f 0.0 0.0 0.0 0.0 } ]
131 [ T{ rgba f 1.0 1.0 1.0 1.0 } ] if ;
133 GENERIC#: apply-world-attributes 1 ( world attributes -- world )
135 M: world apply-world-attributes
138 [ status>> >>status ]
139 [ pixel-format-attributes>> >>pixel-format-attributes ]
140 [ window-controls>> >>window-controls ]
141 [ initial-background-color >>background-color ]
142 [ grab-input?>> >>grab-input? ]
143 [ gadgets>> dup sequence? [ [ 1 track-add ] each ] [ 1 track-add ] if ]
144 [ pref-dim>> >>pref-dim ]
146 [ orientation>> >>orientation ]
149 : <world> ( world-attributes -- world )
150 [ world-class>> new-world ] keep apply-world-attributes
153 : as-big-as-possible ( world gadget -- )
154 dup [ { 0 0 } >>loc over dim>> >>dim ] when 2drop ; inline
158 [ dup layers>> [ as-big-as-possible ] with each ] bi ;
160 M: world focusable-child* children>> [ t ] [ first ] if-empty ;
162 M: world children-on nip children>> ;
164 M: world remove-gadget
165 2dup layers>> member-eq?
166 [ layers>> remove-eq! drop ] [ call-next-method ] if ;
168 SYMBOL: flush-layout-cache-hook
170 flush-layout-cache-hook [ [ ] ] initialize
172 GENERIC: begin-world ( world -- )
173 GENERIC: end-world ( world -- )
174 GENERIC: resize-world ( world -- )
176 M: world begin-world drop ;
177 M: world end-world drop ;
178 M: world resize-world drop ;
185 [ [ set-gl-context ] [ resize-world ] bi ]
190 GENERIC: draw-world* ( world -- )
196 [ text-handle>> [ purge-cache ] when* ]
197 [ images>> [ purge-cache ] when* ]
200 : draw-world? ( world -- ? )
201 ! We don't draw deactivated worlds, or those with 0 size.
202 ! On Windows, the latter case results in GL errors.
203 { [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] } 1&& ;
205 TUPLE: world-error error world ;
207 C: <world-error> world-error
209 SYMBOL: ui-error-hook ! ( error -- )
211 : ui-error ( error -- )
212 ui-error-hook get [ call( error -- ) ] [ die drop ] if* ;
214 ui-error-hook [ [ rethrow ] ] initialize
216 : draw-world ( world -- )
219 dup [ draw-world* ] with-gl-context
220 flush-layout-cache-hook get call( -- )
222 swap f >>active? <world-error> rethrow
228 [ [ { C+ } ] dip f <key-down> ]
229 [ '[ _ send-action ] ]
233 { T{ key-down f { S+ } "DELETE" } [ \ cut-action send-action ] }
234 { T{ key-down f { S+ } "INSERT" } [ \ paste-action send-action ] }
235 { T{ key-down f { C+ } "INSERT" } [ \ copy-action send-action ] }
236 { T{ button-down f { C+ } 1 } [ drop T{ button-down f f 3 } button-gesture ] }
237 { T{ button-down f { A+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
238 { T{ button-down f { M+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
239 { T{ button-up f { C+ } 1 } [ drop T{ button-up f f 3 } button-gesture ] }
240 { T{ button-up f { A+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
241 { T{ button-up f { M+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
242 } assoc-union set-gestures
244 PREDICATE: specific-button-up < button-up #>> ;
245 PREDICATE: specific-button-down < button-down #>> ;
246 PREDICATE: specific-drag < drag #>> ;
248 : generalize-gesture ( gesture -- )
249 clone f >># button-gesture ;
251 M: world handle-gesture
252 2dup call-next-method [
254 { [ over specific-button-up? ] [ drop generalize-gesture f ] }
255 { [ over specific-button-down? ] [ drop generalize-gesture f ] }
256 { [ over specific-drag? ] [ drop generalize-gesture f ] }
261 : close-global ( world global -- )
262 [ get-global find-world eq? ] keep '[ f _ set-global ] when ;
264 M: world world-pixel-format-attributes
265 pixel-format-attributes>> ;
267 M: world check-world-pixel-format
270 : with-world-pixel-format ( world quot -- )
271 [ dup dup world-pixel-format-attributes <pixel-format> ]
272 dip [ 2dup check-world-pixel-format ] prepose with-disposal ; inline