[plt-scheme] Make a grid / table control in GUI

From: Stephen De Gabrielle (spdegabrielle at gmail.com)
Date: Tue Sep 8 13:09:55 EDT 2009

How would you implement that?

Another  iteration

#lang scheme/gui

(define grid-pasteboard%
  (class pasteboard%
    (init-field w h)
    (define/override (insert snip r c) (super insert snip (* c w) (* r h)))

;; accepts data-grid (list of columns)
(define data-grid-pasteboard%
  (class pasteboard%
    (init-field data-grid
                (col-widths '(75 55 65))
                (row-height 16)

    ;; create new list that is the cumulative sum of the source list
    (define (cumulative-sums offsets)
      (foldl (lambda (offset cum-list)
               (append cum-list (list (+ offset (last cum-list)))))
             (list 0) ; start from 0
    ;; column widths -> col offsets
    (define (column-offsets col-widths)
      (cumulative-sums (drop-right col-widths 1)))

    (define col-offset
      (let ((offsets (column-offsets col-widths)))
        (lambda (col) (list-ref offsets col))))
    ;; rows are all the same height
    (define (row-offset row)
      (* row row-height))
    ;; initialise pasteboard

    (define/override (insert snip row col)
      (super insert snip (col-offset col) (row-offset row)))
    (define (grid-ref datagrid row col)
      (list-ref (list-ref datagrid col) row)
    (for ([col (in-range (length data-grid))])
      (for ([row (in-range (length (car data-grid)))])
        (insert (make-object string-snip% (grid-ref data-grid row col))
                row col)

(define f (new frame% [label "Simple Edit"]
               [width 200]
               [height 200]))
(define c (new editor-canvas% [parent f]))
(define t (new  grid-pasteboard% (w 66) (h 16)))
(send t insert (make-object string-snip% "at23") 2 3)
(send t insert (make-object string-snip% "at23") 2 2)
(send t insert (make-object string-snip% "at23") 3 2)
(send t insert (make-object string-snip% "at23") 1 1)
(send t insert (make-object string-snip% "at23") 0 0)
(send c set-editor t)
(send f show #t)


(define f2 (new frame% [label "Simple Edit"]
                [width 200]
                [height 200]))
(define c2 (new editor-canvas% [parent f2]))
(define t2 (new data-grid-pasteboard%
                 '(("name" "Stephen" "Patrick")
                   ("age" "39" "38")
                   ("eye-colour" "green" "blue")
(send c2 set-editor t2)
(send f2 show #t)

On Tue, Sep 8, 2009 at 2:01 PM, praimon <praimon at gmail.com> wrote:

> Hi,
> Thanks for your work. It seems we have this conversation every year or two.
> One of the most important features of the directory-table widget is the
> ability
> to manually resize the columns. Otherwise, either some of the data will be
> hidden/lost or the table needs to be too large (so as to accomodate
> any conceivable
> data entry).
> regards,
> praimon
> On Tue, Sep 8, 2009 at 2:15 AM, Stephen De
> Gabrielle<spdegabrielle at gmail.com> wrote:
> > Hi, people have asked about a data grid on this list a few times.
> >
> > This is the simplest possible approach. I've tried and abandoned other
> > approaches when I realised they were too much work for my available free
> > time.
> >
> > Comments appreciated.
> >


Stephen De Gabrielle
stephen.degabrielle at acm.org
Telephone +44 (0)20 85670911
Mobile        +44 (0)79 85189045
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090908/29708d36/attachment.html>

Posted on the users mailing list.