[plt-scheme] grid% - first go

From: Guenther Schmidt (gue.schmidt at web.de)
Date: Sun Dec 28 16:51:47 EST 2003

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)
        (values (* max-w columns) (* max-w (ceiling (/ (length a-list) columns))))))
    (super-new)))


Best regards

Guenther


BUGS :-(

It doesn't do buttons% to well yet



Posted on the users mailing list.