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? fullscreen?
39 title status status-owner
40 text-handle handle images
42 pixel-format-attributes
47 TUPLE: world-attributes
48 { world-class initial: world }
50 { title string initial: "Factor Window" }
53 { pixel-format-attributes initial: $ default-world-pixel-format-attributes }
54 { window-controls initial: $ default-world-window-controls }
57 : <world-attributes> ( -- world-attributes )
58 world-attributes new ; inline
60 : find-world ( gadget -- world/f ) [ world? ] find-parent ;
62 : grab-input ( gadget -- )
63 find-world dup grab-input?>>
66 dup focused?>> [ handle>> (grab-input) ] [ drop ] if
69 : ungrab-input ( gadget -- )
70 find-world dup grab-input?>>
73 dup focused?>> [ handle>> (ungrab-input) ] [ drop ] if
76 : show-status ( string/f gadget -- )
79 [ (>>status-owner) ] [ status>> set-model ] bi
83 : hide-status ( gadget -- )
85 [ status-owner>> eq? ] keep
86 '[ f _ [ (>>status-owner) ] [ status>> set-model ] 2bi ] when
89 : window-resource ( resource -- resource )
90 dup world get-global window-resources>> push ;
92 : set-gl-context ( world -- )
94 [ handle>> select-gl-context ] bi ;
96 : with-gl-context ( world quot -- )
98 [ handle>> flush-gl-context gl-error ] bi ; inline
100 ERROR: no-world-found ;
102 : find-gl-context ( gadget -- )
104 [ set-gl-context ] [ no-world-found ] if ;
106 : (request-focus) ( child world ? -- )
107 pick parent>> pick eq? [
108 [ dup parent>> dup ] 2dip
109 [ (request-focus) ] keep
110 ] unless focus-child ;
112 M: world request-focus-on ( child gadget -- )
114 [ 2drop ] [ dup focused?>> (request-focus) ] if ;
116 : new-world ( class -- world )
117 vertical swap new-track
122 V{ } clone >>window-resources ;
124 : initial-background-color ( attributes -- color )
125 window-controls>> textured-background swap member-eq?
126 [ T{ rgba f 0.0 0.0 0.0 0.0 } ]
127 [ T{ rgba f 1.0 1.0 1.0 1.0 } ] if ;
129 GENERIC# apply-world-attributes 1 ( world attributes -- world )
130 M: world apply-world-attributes
133 [ status>> >>status ]
134 [ pixel-format-attributes>> >>pixel-format-attributes ]
135 [ window-controls>> >>window-controls ]
136 [ initial-background-color >>background-color ]
137 [ grab-input?>> >>grab-input? ]
138 [ gadgets>> dup sequence? [ [ 1 track-add ] each ] [ 1 track-add ] if ]
139 [ pref-dim>> >>pref-dim ]
142 : <world> ( world-attributes -- world )
143 [ world-class>> new-world ] keep apply-world-attributes
146 : as-big-as-possible ( world gadget -- )
147 dup [ { 0 0 } >>loc over dim>> >>dim ] when 2drop ; inline
151 [ dup layers>> [ as-big-as-possible ] with each ] bi ;
153 M: world focusable-child* children>> [ t ] [ first ] if-empty ;
155 M: world children-on nip children>> ;
157 M: world remove-gadget
158 2dup layers>> member-eq?
159 [ layers>> remove-eq! drop ] [ call-next-method ] if ;
161 SYMBOL: flush-layout-cache-hook
163 flush-layout-cache-hook [ [ ] ] initialize
165 GENERIC: begin-world ( world -- )
166 GENERIC: end-world ( world -- )
168 GENERIC: resize-world ( world -- )
174 M: world resize-world
182 [ [ set-gl-context ] [ resize-world ] bi ]
187 GENERIC: draw-world* ( world -- )
194 [ text-handle>> [ purge-cache ] when* ]
195 [ images>> [ purge-cache ] when* ]
198 : draw-world? ( world -- ? )
199 #! We don't draw deactivated worlds, or those with 0 size.
200 #! On Windows, the latter case results in GL errors.
201 { [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] } 1&& ;
203 TUPLE: world-error error world ;
205 C: <world-error> world-error
207 SYMBOL: ui-error-hook
209 : ui-error ( error -- )
210 ui-error-hook get [ call( error -- ) ] [ die drop ] if* ;
212 ui-error-hook [ [ rethrow ] ] initialize
214 : draw-world ( world -- )
218 dup [ draw-world* ] with-gl-context
219 flush-layout-cache-hook get call( -- )
221 swap f >>active? <world-error> ui-error
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 ( gesture gadget -- ? )
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