]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/worlds/worlds.factor
8f38cee988c308db60eb461bf28a8d6f957b3fe0
[factor.git] / basis / ui / gadgets / worlds / worlds.factor
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 ;
8 IN: ui.gadgets.worlds
9
10 SYMBOLS:
11     close-button
12     minimize-button
13     maximize-button
14     resize-handles
15     small-title-bar
16     normal-title-bar
17     textured-background ;
18
19 CONSTANT: default-world-pixel-format-attributes
20     {
21         windowed
22         double-buffered
23         T{ depth-bits { value 16 } }
24     }
25
26 CONSTANT: default-world-window-controls
27     {
28         normal-title-bar
29         close-button
30         minimize-button
31         maximize-button
32         resize-handles
33     }
34
35 TUPLE: world < track
36     active? focused? grab-input?
37     layers
38     title status status-owner
39     text-handle handle images
40     window-loc
41     pixel-format-attributes
42     background-color
43     window-controls
44     window-resources ;
45
46 TUPLE: world-attributes
47     { world-class initial: world }
48     grab-input?
49     { title string initial: "Factor Window" }
50     status
51     gadgets
52     { pixel-format-attributes initial: $ default-world-pixel-format-attributes }
53     { window-controls initial: $ default-world-window-controls } ;
54
55 : <world-attributes> ( -- world-attributes )
56     world-attributes new ; inline
57
58 : find-world ( gadget -- world/f ) [ world? ] find-parent ;
59
60 : grab-input ( gadget -- )
61     find-world dup grab-input?>>
62     [ drop ] [
63         t >>grab-input?
64         dup focused?>> [ handle>> (grab-input) ] [ drop ] if
65     ] if ;
66
67 : ungrab-input ( gadget -- )
68     find-world dup grab-input?>>
69     [
70         f >>grab-input?
71         dup focused?>> [ handle>> (ungrab-input) ] [ drop ] if
72     ] [ drop ] if ;
73     
74 : show-status ( string/f gadget -- )
75     dup find-world dup [
76         dup status>> [
77             [ (>>status-owner) ] [ status>> set-model ] bi
78         ] [ 3drop ] if
79     ] [ 3drop ] if ;
80
81 : hide-status ( gadget -- )
82     dup find-world dup [
83         [ status-owner>> eq? ] keep
84         '[ f _ [ (>>status-owner) ] [ status>> set-model ] 2bi ] when
85     ] [ 2drop ] if ;
86
87 : window-resource ( resource -- resource )
88     dup world get-global window-resources>> push ;
89
90 : set-gl-context ( world -- )
91     [ world set-global ]
92     [ handle>> select-gl-context ] bi ;
93
94 : with-gl-context ( world quot -- )
95     '[ set-gl-context @ ]
96     [ handle>> flush-gl-context gl-error ] bi ; inline
97
98 ERROR: no-world-found ;
99
100 : find-gl-context ( gadget -- )
101     find-world dup
102     [ set-gl-context ] [ no-world-found ] if ;
103
104 : (request-focus) ( child world ? -- )
105     pick parent>> pick eq? [
106         [ dup parent>> dup ] 2dip
107         [ (request-focus) ] keep
108     ] unless focus-child ;
109
110 M: world request-focus-on ( child gadget -- )
111     2dup eq?
112     [ 2drop ] [ dup focused?>> (request-focus) ] if ;
113
114 : new-world ( class -- world )
115     vertical swap new-track
116         t >>root?
117         f >>active?
118         { 0 0 } >>window-loc
119         f >>grab-input?
120         V{ } clone >>window-resources ;
121
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 ;
126
127 : apply-world-attributes ( world attributes -- world )
128     {
129         [ title>> >>title ]
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 ]
136     } cleave ;
137
138 : <world> ( world-attributes -- world )
139     [ world-class>> new-world ] keep apply-world-attributes
140     dup request-focus ;
141
142 : as-big-as-possible ( world gadget -- )
143     dup [ { 0 0 } >>loc over dim>> >>dim ] when 2drop ; inline
144
145 M: world layout*
146     [ call-next-method ]
147     [ dup layers>> [ as-big-as-possible ] with each ] bi ;
148
149 M: world focusable-child* children>> [ t ] [ first ] if-empty ;
150
151 M: world children-on nip children>> ;
152
153 M: world remove-gadget
154     2dup layers>> member-eq?
155     [ layers>> remove-eq! drop ] [ call-next-method ] if ;
156
157 SYMBOL: flush-layout-cache-hook
158
159 flush-layout-cache-hook [ [ ] ] initialize
160
161 GENERIC: begin-world ( world -- )
162 GENERIC: end-world ( world -- )
163
164 GENERIC: resize-world ( world -- )
165
166 M: world begin-world
167     drop ;
168 M: world end-world
169     drop ;
170 M: world resize-world
171     drop ;
172
173 M: world (>>dim)
174     [ call-next-method ]
175     [
176         dup active?>> [
177             dup handle>>
178             [ [ set-gl-context ] [ resize-world ] bi ]
179             [ drop ] if
180         ] [ drop ] if
181     ] bi ;
182
183 GENERIC: draw-world* ( world -- )
184
185 M: world draw-world*
186     check-extensions
187     {
188         [ init-gl ]
189         [ draw-gadget ]
190         [ text-handle>> [ purge-cache ] when* ]
191         [ images>> [ purge-cache ] when* ]
192     } cleave ;
193
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&& ;
198
199 TUPLE: world-error error world ;
200
201 C: <world-error> world-error
202
203 SYMBOL: ui-error-hook
204
205 : ui-error ( error -- )
206     ui-error-hook get [ call( error -- ) ] [ die drop ] if* ;
207
208 ui-error-hook [ [ rethrow ] ] initialize
209
210 : draw-world ( world -- )
211     dup draw-world? [
212         dup world [
213             [
214                 dup [ draw-world* ] with-gl-context
215                 flush-layout-cache-hook get call( -- )
216             ] [
217                 over <world-error> ui-error
218                 f >>active? drop
219             ] recover
220         ] with-variable
221     ] [ drop ] if ;
222
223 world
224 action-gestures [
225     [ [ { C+ } ] dip f <key-down> ]
226     [ '[ _ send-action ] ]
227     bi*
228 ] H{ } assoc-map-as
229 H{
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
237
238 PREDICATE: specific-button-up < button-up #>> ;
239 PREDICATE: specific-button-down < button-down #>> ;
240 PREDICATE: specific-drag < drag #>> ;
241
242 : generalize-gesture ( gesture -- )
243     clone f >># button-gesture ;
244
245 M: world handle-gesture ( gesture gadget -- ? )
246     2dup call-next-method [
247         {
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 ] }
251             [ 2drop t ]
252         } cond
253     ] [ 2drop f ] if ;
254
255 : close-global ( world global -- )
256     [ get-global find-world eq? ] keep '[ f _ set-global ] when ;
257
258 M: world world-pixel-format-attributes
259     pixel-format-attributes>> ;
260
261 M: world check-world-pixel-format
262     2drop ;
263
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