1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays binary-search combinators
4 concurrency.flags deques kernel make math math.order
5 math.rectangles math.vectors models namespaces sequences threads
6 vectors vocabs.loader ;
9 ! Values for orientation slot
10 CONSTANT: horizontal { 1 0 }
11 CONSTANT: vertical { 0 1 }
17 { orientation initial: { 0 1 } }
19 { visible? initial: t }
23 { graft-state initial: { f f } }
29 M: gadget equal? 2drop f ;
31 M: gadget hashcode* nip identity-hashcode ;
33 : gadget-child ( gadget -- child ) children>> first ; inline
35 : nth-gadget ( n gadget -- child ) children>> nth ; inline
37 : <gadget> ( -- gadget )
40 : control-value ( control -- value )
43 : set-control-value ( value control -- )
46 : relative-loc ( fromgadget togadget -- loc )
50 [ [ parent>> ] dip relative-loc ] [ drop loc>> ] 2bi v+
53 GENERIC: user-input* ( str gadget -- ? )
55 M: gadget user-input* 2drop t ;
57 GENERIC: temp-im-input ( str gadget -- ? )
59 M: gadget temp-im-input 2drop t ;
61 GENERIC: children-on ( rect gadget -- seq )
63 M: gadget children-on nip children>> ;
67 :: (fast-children-on) ( point axis children quot -- i )
70 quot call( value -- loc ) v-
72 ] search drop ; inline
76 :: fast-children-on ( rect axis children quot -- slice )
77 rect loc>> axis children quot (fast-children-on) 0 or
78 rect rect-bounds v+ axis children quot (fast-children-on) ?1+
79 children <slice> ; inline
81 M: gadget contains-rect?
82 dup visible?>> [ call-next-method ] [ 2drop f ] if ;
84 M: gadget contains-point?
85 dup visible?>> [ call-next-method ] [ 2drop f ] if ;
87 : pick-up ( point gadget -- child/f )
88 2dup [ dup point>rect ] dip children-on
89 [ contains-point? ] with find-last nip
90 [ [ loc>> v- ] [ pick-up ] bi ] [ nip ] ?if ;
92 : max-dims ( seq -- dim )
93 [ 0 0 ] dip [ first2 swapd [ max ] 2bi@ ] each 2array ;
95 : sum-dims ( seq -- dim )
96 [ 0 0 ] dip [ first2 swapd [ + ] 2bi@ ] each 2array ;
98 : each-child ( gadget quot -- )
99 [ children>> ] dip each ; inline
102 GENERIC: gadget-selection? ( gadget -- ? )
104 M: gadget gadget-selection? drop f ;
106 GENERIC: gadget-selection ( gadget -- string/f )
108 M: gadget gadget-selection drop f ;
111 GENERIC: gadget-text* ( gadget -- )
113 GENERIC: gadget-text-separator ( gadget -- str )
115 M: gadget gadget-text-separator
116 orientation>> vertical = "\n" "" ? ;
118 : gadget-seq-text ( seq gadget -- )
119 gadget-text-separator '[ _ % ] [ gadget-text* ] interleave ;
121 M: gadget gadget-text*
122 [ children>> ] keep gadget-seq-text ;
124 M: array gadget-text*
125 [ gadget-text* ] each ;
127 : gadget-text ( gadget -- string ) [ gadget-text* ] "" make ;
133 SYMBOL: ui-notify-flag
135 : notify-ui-thread ( -- ) ui-notify-flag get-global raise-flag ;
137 : invalidate ( gadget -- )
138 \ invalidate >>layout-state drop ;
140 : forget-pref-dim ( gadget -- ) f >>pref-dim drop ;
142 : layout-queue ( -- queue )
145 CONSTANT: layout-queue-limit 8000
147 : layout-later ( gadget -- )
149 [ push notify-ui-thread ]
150 [ length layout-queue-limit > [ yield ] when ] bi
153 : invalidate* ( gadget -- )
154 \ invalidate* >>layout-state
157 [ layout-later ] [ parent>> [ relayout ] when* ] if ;
161 : relayout ( gadget -- )
162 dup layout-state>> \ invalidate* eq?
163 [ drop ] [ invalidate* ] if ;
165 : relayout-1 ( gadget -- )
167 [ drop ] [ dup invalidate layout-later ] if ;
169 : show-gadget ( gadget -- ) t >>visible? drop ;
171 : hide-gadget ( gadget -- ) f >>visible? drop ;
177 GENERIC: dim-changed ( gadget -- )
179 M: gadget dim-changed
180 in-layout? get [ invalidate ] [ invalidate* ] if ;
187 [ [ nip ] [ call-next-method ] 2bi dim-changed ] if ;
189 GENERIC: pref-dim* ( gadget -- dim )
191 : pref-dim ( gadget -- dim )
193 [ pref-dim* ] [ ] [ layout-state>> ] tri
194 [ drop ] [ dupd pref-dim<< ] if
197 : pref-dims ( gadgets -- seq ) [ pref-dim ] map ; inline
199 M: gadget pref-dim* dim>> ;
201 GENERIC: layout* ( gadget -- )
203 M: object layout* drop ;
205 : prefer ( gadget -- ) dup pref-dim >>dim drop ;
207 : layout ( gadget -- )
211 dup [ layout ] each-child
214 GENERIC: graft* ( gadget -- )
216 M: object graft* drop ;
218 GENERIC: ungraft* ( gadget -- )
220 M: object ungraft* drop ;
224 : graft-queue ( -- dlist )
225 \ graft-queue get [ "UI not running" throw ] unless* ;
227 : unqueue-graft ( gadget -- )
228 [ graft-node>> graft-queue delete-node ]
229 [ [ first { t t } { f f } ? ] change-graft-state drop ] bi ;
231 : (queue-graft) ( gadget flags -- )
233 dup graft-queue push-front* >>graft-node drop
236 : queue-graft ( gadget -- )
237 { f t } (queue-graft) ;
239 : queue-ungraft ( gadget -- )
240 { t f } (queue-graft) ;
242 : graft-later ( gadget -- )
246 { { t f } [ unqueue-graft ] }
247 { { f f } [ queue-graft ] }
250 : graft ( gadget -- )
251 dup graft-later [ graft ] each-child ;
253 : ungraft-later ( gadget -- )
257 { { f t } [ unqueue-graft ] }
258 { { t t } [ queue-ungraft ] }
261 : ungraft ( gadget -- )
262 dup [ ungraft ] each-child ungraft-later ;
264 : activate-control ( gadget -- )
272 : deactivate-control ( gadget -- )
273 dup model>> dup [ 2dup remove-connection ] when 2drop ;
275 : notify ( gadget -- )
277 [ first { f f } { t t } ? >>graft-state ] [ ] bi
279 { { f t } [ dup activate-control graft* ] }
280 { { t f } [ dup deactivate-control ungraft* ] }
283 : notify-queued ( -- )
284 graft-queue [ notify ] slurp-deque ;
286 : (unparent) ( gadget -- )
291 : (clear-gadget) ( gadget -- )
292 dup [ (unparent) ] each-child
293 f >>focus f >>children drop ;
295 : unfocus-gadget ( child gadget -- )
296 [ nip ] [ focus>> eq? ] 2bi [ f >>focus ] when drop ;
300 : not-in-layout ( -- )
302 [ "Cannot add/remove gadgets in layout*" throw ] when ;
304 GENERIC: remove-gadget ( gadget parent -- )
306 M: gadget remove-gadget 2drop ;
308 : unparent ( gadget -- )
316 [ children>> remove! drop ]
323 : clear-gadget ( gadget -- )
325 [ (clear-gadget) ] [ relayout ] bi ;
329 : (add-gadget) ( child parent -- )
333 [ [ ?push ] change-children drop ]
334 [ graft-state>> second [ graft ] [ drop ] if ]
339 : add-gadget ( parent child -- parent )
344 : add-gadgets ( parent children -- parent )
346 over '[ _ (add-gadget) ] each
349 : parents ( gadget -- seq )
350 [ parent>> ] follow ;
352 : each-parent ( gadget quot -- ? )
353 [ parents ] dip all? ; inline
355 : find-parent ( gadget quot -- parent )
356 [ parents ] dip find nip ; inline
358 : screen-loc ( gadget -- loc )
359 parents { 0 0 } [ loc>> v+ ] reduce ;
363 : (screen-rect) ( gadget -- loc ext )
365 [ rect-extent ] dip (screen-rect)
366 [ [ nip ] [ v+ ] 2bi ] dip [ v+ ] [ vmin ] 2bi*
373 : screen-rect ( gadget -- rect )
374 (screen-rect) <extent-rect> ;
376 : child? ( parent child -- ? )
378 { [ 2dup eq? ] [ 2drop t ] }
379 { [ dup not ] [ 2drop f ] }
383 GENERIC: focusable-child* ( gadget -- child/t )
385 M: gadget focusable-child* drop t ;
387 M: f focusable-child* drop f ;
389 : focusable-child ( gadget -- child )
391 dup t eq? [ drop ] [ nip focusable-child ] if ;
393 GENERIC: request-focus-on ( child gadget -- )
395 M: gadget request-focus-on parent>> request-focus-on ;
397 M: f request-focus-on 2drop ;
399 : request-focus ( gadget -- )
400 [ focusable-child ] keep request-focus-on ;
402 : focus-path ( gadget -- seq )
405 GENERIC: preedit? ( gadget -- ? )
407 M: gadget preedit? drop f ;
409 USE-WHEN-LOADED: ui.gadgets.prettyprint { "ui.gadgets" "prettyprint" }