[plt-scheme] The missing drawing context

From: Jens Axel Soegaard (jensaxel at soegaard.net)
Date: Thu Mar 19 16:14:33 EDT 2009

Hi All,

In the code below I need to calculate the size of
the string snips. In order to do that I need a
drawing context. Since the string snips belong
to a pasteboard, I thought I could get the
drawing context with

     (send pasteboard get-dc)

but all I get is #f.

The pasteboard belongs to an editor snip, since
I want to use the pasteboard from within DrScheme.

Most likely I am missing something obvious - but what?

/Jens Axel

#lang scheme/gui

(define (make-string-snip string)
   (make-object string-snip% string))

(define (insert-snip editor snip)
   (send editor insert snip))

(define (insert-snips editor snips)
   (for-each (? (s) (insert-snip editor s)) snips))

(define (get-width dc snip)
   (let ([w (box 0)])
     (send snip get-extent dc 0 0 w)
     (unbox w)))

(define pasteboard (new pasteboard%))
(define editor-snip (new editor-snip%
                          [editor pasteboard]
                          [min-width 100]
                          [min-height 100]))

(define snips (map make-string-snip '("Foo" "Bar" "Baz" "Qux")))
(insert-snips pasteboard snips)

(let ([dc (send pasteboard get-dc)])
   (if dc
       (map (? (s) (get-width dc s)) snips)

Posted on the users mailing list.