]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/gadgets.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / ui / gadgets / gadgets.factor
1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays hashtables kernel models math namespaces
4 make sequences quotations math.vectors combinators sorting
5 binary-search vectors dlists deques models threads
6 concurrency.flags math.order math.geometry.rect fry ;
7 IN: ui.gadgets
8
9 SYMBOL: ui-notify-flag
10
11 : notify-ui-thread ( -- ) ui-notify-flag get-global raise-flag ;
12
13 TUPLE: gadget < rect pref-dim parent children orientation focus
14 visible? root? clipped? layout-state graft-state graft-node
15 interior boundary model ;
16
17 M: gadget equal? 2drop f ;
18
19 M: gadget hashcode* drop gadget hashcode* ;
20
21 M: gadget model-changed 2drop ;
22
23 : gadget-child ( gadget -- child ) children>> first ;
24
25 : nth-gadget ( n gadget -- child ) children>> nth ;
26
27 : init-gadget ( gadget -- gadget )
28     init-rect
29     { 0 1 } >>orientation
30     t >>visible?
31     { f f } >>graft-state ; inline
32
33 : new-gadget ( class -- gadget ) new init-gadget ; inline
34
35 : <gadget> ( -- gadget )
36     gadget new-gadget ;
37
38 : activate-control ( gadget -- )
39     dup model>> dup [
40         2dup add-connection
41         swap model-changed
42     ] [
43         2drop
44     ] if ;
45
46 : deactivate-control ( gadget -- )
47     dup model>> dup [ 2dup remove-connection ] when 2drop ;
48
49 : control-value ( control -- value )
50     model>> value>> ;
51
52 : set-control-value ( value control -- )
53     model>> set-model ;
54
55 : relative-loc ( fromgadget togadget -- loc )
56     2dup eq? [
57         2drop { 0 0 }
58     ] [
59         over rect-loc [ [ parent>> ] dip relative-loc ] dip v+
60     ] if ;
61
62 GENERIC: user-input* ( str gadget -- ? )
63
64 M: gadget user-input* 2drop t ;
65
66 GENERIC: children-on ( rect/point gadget -- seq )
67
68 M: gadget children-on nip children>> ;
69
70 : ((fast-children-on)) ( gadget dim axis -- <=> )
71     [ swap loc>> v- ] dip v. 0 <=> ;
72
73 : (fast-children-on) ( dim axis children -- i )
74     -rot '[ _ _ ((fast-children-on)) ] search drop ;
75
76 : fast-children-on ( rect axis children -- from to )
77     [ [ rect-loc ] 2dip (fast-children-on) 0 or ]
78     [ [ rect-bounds v+ ] 2dip (fast-children-on) ?1+ ]
79     3bi ;
80
81 : inside? ( bounds gadget -- ? )
82     dup visible?>> [ intersects? ] [ 2drop f ] if ;
83
84 : (pick-up) ( point gadget -- gadget )
85     dupd children-on [ inside? ] with find-last nip ;
86
87 : pick-up ( point gadget -- child/f )
88     2dup (pick-up) dup
89     [ nip [ rect-loc v- ] keep pick-up ] [ drop nip ] if ;
90
91 : max-dim ( dims -- dim ) { 0 0 } [ vmax ] reduce ;
92
93 : dim-sum ( seq -- dim ) { 0 0 } [ v+ ] reduce ;
94
95 : each-child ( gadget quot -- )
96     [ children>> ] dip each ; inline
97
98 ! Selection protocol
99 GENERIC: gadget-selection? ( gadget -- ? )
100
101 M: gadget gadget-selection? drop f ;
102
103 GENERIC: gadget-selection ( gadget -- string/f )
104
105 M: gadget gadget-selection drop f ;
106
107 ! Text protocol
108 GENERIC: gadget-text* ( gadget -- )
109
110 GENERIC: gadget-text-separator ( gadget -- str )
111
112 M: gadget gadget-text-separator
113     orientation>> { 0 1 } = "\n" "" ? ;
114
115 : gadget-seq-text ( seq gadget -- )
116     gadget-text-separator swap
117     [ dup % ] [ gadget-text* ] interleave drop ;
118
119 M: gadget gadget-text*
120     dup children>> swap gadget-seq-text ;
121
122 M: array gadget-text*
123     [ gadget-text* ] each ;
124
125 : gadget-text ( gadget -- string ) [ gadget-text* ] "" make ;
126
127 : invalidate ( gadget -- )
128     \ invalidate >>layout-state drop ;
129
130 : forget-pref-dim ( gadget -- ) f >>pref-dim drop ;
131
132 : layout-queue ( -- queue ) \ layout-queue get ;
133
134 : layout-later ( gadget -- )
135     #! When unit testing gadgets without the UI running, the
136     #! invalid queue is not initialized and we simply ignore
137     #! invalidation requests.
138     layout-queue [ push-front notify-ui-thread ] [ drop ] if* ;
139
140 DEFER: relayout
141
142 : invalidate* ( gadget -- )
143     \ invalidate* >>layout-state
144     dup forget-pref-dim
145     dup root?>>
146     [ layout-later ] [ parent>> [ relayout ] when* ] if ;
147
148 : relayout ( gadget -- )
149     dup layout-state>> \ invalidate* eq?
150     [ drop ] [ invalidate* ] if ;
151
152 : relayout-1 ( gadget -- )
153     dup layout-state>>
154     [ drop ] [ dup invalidate layout-later ] if ;
155
156 : show-gadget ( gadget -- ) t >>visible? drop ;
157                               
158 : hide-gadget ( gadget -- ) f >>visible? drop ;
159
160 DEFER: in-layout?
161
162 GENERIC: dim-changed ( gadget -- )
163
164 M: gadget dim-changed
165     in-layout? get [ invalidate ] [ invalidate* ] if ;
166
167 M: gadget (>>dim) ( dim gadget -- )
168     2dup dim>> =
169     [ 2drop ]
170     [ [ nip ] [ call-next-method ] 2bi dim-changed ] if ;
171
172 GENERIC: pref-dim* ( gadget -- dim )
173
174 : ?set-gadget-pref-dim ( dim gadget -- )
175     dup layout-state>>
176     [ 2drop ] [ (>>pref-dim) ] if ;
177
178 : pref-dim ( gadget -- dim )
179     dup pref-dim>> [ ] [
180         [ pref-dim* dup ] keep ?set-gadget-pref-dim
181     ] ?if ;
182
183 : pref-dims ( gadgets -- seq ) [ pref-dim ] map ;
184
185 M: gadget pref-dim* rect-dim ;
186
187 GENERIC: layout* ( gadget -- )
188
189 M: gadget layout* drop ;
190
191 : prefer ( gadget -- ) dup pref-dim >>dim drop ;
192
193 : validate ( gadget -- ) f >>layout-state drop ;
194
195 : layout ( gadget -- )
196     dup layout-state>> [
197         dup validate
198         dup layout*
199         dup [ layout ] each-child
200     ] when drop ;
201
202 : graft-queue ( -- dlist ) \ graft-queue get ;
203
204 : unqueue-graft ( gadget -- )
205     [ graft-node>> graft-queue delete-node ]
206     [ [ first { t t } { f f } ? ] change-graft-state drop ] bi ;
207
208 : (queue-graft) ( gadget flags -- )
209     >>graft-state
210     dup graft-queue push-front* >>graft-node drop
211     notify-ui-thread ;
212
213 : queue-graft ( gadget -- )
214     { f t } (queue-graft) ;
215
216 : queue-ungraft ( gadget -- )
217     { t f } (queue-graft) ;
218
219 : graft-later ( gadget -- )
220     dup graft-state>> {
221         { { f t } [ drop ] }
222         { { t t } [ drop ] }
223         { { t f } [ unqueue-graft ] }
224         { { f f } [ queue-graft ] }
225     } case ;
226
227 : ungraft-later ( gadget -- )
228     dup graft-state>> {
229         { { f f } [ drop ] }
230         { { t f } [ drop ] }
231         { { f t } [ unqueue-graft ] }
232         { { t t } [ queue-ungraft ] }
233     } case ;
234
235 GENERIC: graft* ( gadget -- )
236
237 M: gadget graft* drop ;
238
239 : graft ( gadget -- )
240     dup graft-later [ graft ] each-child ;
241
242 GENERIC: ungraft* ( gadget -- )
243
244 M: gadget ungraft* drop ;
245
246 : ungraft ( gadget -- )
247     dup [ ungraft ] each-child ungraft-later ;
248
249 : (unparent) ( gadget -- )
250     dup ungraft
251     dup forget-pref-dim
252     f >>parent drop ;
253
254 : unfocus-gadget ( child gadget -- )
255     [ nip ] [ focus>> eq? ] 2bi [ f >>focus ] when drop ;
256
257 SYMBOL: in-layout?
258
259 : not-in-layout ( -- )
260     in-layout? get
261     [ "Cannot add/remove gadgets in layout*" throw ] when ;
262
263 : unparent ( gadget -- )
264     not-in-layout
265     [
266         dup parent>> dup [
267             over (unparent)
268             [ unfocus-gadget ] 2keep
269             [ children>> delete ] keep
270             relayout
271         ] [
272             2drop
273         ] if
274     ] when* ;
275
276 : (clear-gadget) ( gadget -- )
277     dup [ (unparent) ] each-child
278     f >>focus f >>children drop ;
279
280 : clear-gadget ( gadget -- )
281     not-in-layout
282     dup (clear-gadget) relayout ;
283
284 : ((add-gadget)) ( parent child -- parent )
285     over children>> ?push >>children ;
286
287 : (add-gadget) ( parent child -- parent )
288     dup unparent
289     over >>parent
290     tuck ((add-gadget))
291     tuck graft-state>> second [ graft ] [ drop  ] if ;
292
293 : add-gadget ( parent child -- parent )
294     not-in-layout
295     (add-gadget)
296     dup relayout ;
297
298 : add-gadgets ( parent children -- parent )
299     not-in-layout
300     [ (add-gadget) ] each
301     dup relayout ;
302
303 : parents ( gadget -- seq )
304     [ parent>> ] follow ;
305
306 : each-parent ( gadget quot -- ? )
307     [ parents ] dip all? ; inline
308
309 : find-parent ( gadget quot -- parent )
310     [ parents ] dip find nip ; inline
311
312 : screen-loc ( gadget -- loc )
313     parents { 0 0 } [ rect-loc v+ ] reduce ;
314
315 : (screen-rect) ( gadget -- loc ext )
316     dup parent>> [
317         [ rect-extent ] dip (screen-rect)
318         [ [ nip ] [ v+ ] 2bi ] dip [ v+ ] [ vmin ] 2bi*
319     ] [
320         rect-extent
321     ] if* ;
322
323 : screen-rect ( gadget -- rect )
324     (screen-rect) <extent-rect> ;
325
326 : child? ( parent child -- ? )
327     {
328         { [ 2dup eq? ] [ 2drop t ] }
329         { [ dup not ] [ 2drop f ] }
330         [ parent>> child? ]
331     } cond ;
332
333 GENERIC: focusable-child* ( gadget -- child/t )
334
335 M: gadget focusable-child* drop t ;
336
337 : focusable-child ( gadget -- child )
338     dup focusable-child*
339     dup t eq? [ drop ] [ nip focusable-child ] if ;
340
341 GENERIC: request-focus-on ( child gadget -- )
342
343 M: gadget request-focus-on parent>> request-focus-on ;
344
345 M: f request-focus-on 2drop ;
346
347 : request-focus ( gadget -- )
348     [ focusable-child ] keep request-focus-on ;
349
350 : focus-path ( world -- seq )
351     [ focus>> ] follow ;