[plt-scheme] grid% - first go

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


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)
          ((define child-width (ceiling (/ width columns)))
           (define child-height (ceiling (/ height (ceiling (/ (length children) columns)))))
           (define (place a-list n)
               ((empty? a-list) '())
                 (let* ((child (car a-list))
                        (width (first child))
                        (height (second child))
                        (x (* child-width (modulo n columns)))
                        (y (* child-height (quotient n columns))))
                    (+ 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))
         (lambda (x)
           (if (> (first x) max-h) (set! max-h (first x)))
           (if (> (second x) max-w) (set! max-w (second x))))
        (values (* max-w columns) (* max-w (ceiling (/ (length a-list) columns))))))

Best regards


BUGS :-(

It doesn't do buttons% to well yet

Posted on the users mailing list.