[plt-scheme] grid% - first go
On Sunday, December 28, 2003, at 04:51 PM, Guenther Schmidt wrote:
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> Hi,
>
> below is a first draft for the grid% element.
>
> Any help for improving highly welcome! :-)
>
>
> (define grid%
> (class pane%
> (init (columns-init 1))
> (field (columns columns-init))
> (inherit get-children)
> (define/override (place-children children width height)
> (local
> ((define child-width (ceiling (/ width columns)))
> (define child-height (ceiling (/ height (ceiling (/ (length
> children) columns)))))
> (define (place a-list n)
> (cond
> ((empty? a-list) '())
> (else (cons (let* ((child
> (car a-list))
> (width (first child))
> (height (second child))
> (x (* child-width (modulo n columns)))
> (y (* child-height (quotient n columns))))
> (list (+ x (round (/ (-
> child-width width) 2))) (+ y (round (/ (-
> child-height height) 2)))
> width height))
> (place (cdr a-list) (+ n 1)))))))
> (place children 0)))
> (define/override (container-size a-list)
> (let ((max-h 0) (max-w 0))
> (for-each
> (lambda (x)
> (if (> (first x) max-h) (set! max-h (first x)))
> (if (> (second x) max-w) (set! max-w (second x))))
> a-list)
FWIW, this can be written as
(let ([max-h (apply max (map first a-list))]
[max-w (apply max (map second a-list)])
(values ... ...))
> (values (* max-w columns) (* max-w (ceiling (/ (length a-list)
> columns))))))
> (super-new)))
In general, I suspect that you need more information about/from
the children. To see, create a frame with a gui-grid in it and do
something like this:
(for-each (lambda (i) (new text-field (parent the-grid) (label i)
(callback void)))
(build-list 22 number->string))
-- Matthias