[plt-scheme] Class questions

From: Ed Cavazos (proteus at theworld.com)
Date: Tue May 13 19:17:41 EDT 2003

This sorta expresses what I'm trying to do:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define extended-canvas%
  (class canvas%
    
    (define backing-store
      (make-object bitmap%
        (send this get-width)
        (send this get-height)
        #f))
  
    (super-instantiate ())))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

So at the time of instantiation, I'd like to set a private field
called 'backing-store' to a value that is dependent upon the size of 
'self'. I'm not sure if that's possible but thought I'd ask. I guess
it's too early to send messages to 'self' when 'self', in this case a
canvas, hasn't yet been fully established. This similar attempt worked:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define extended-canvas%
  (class canvas%
    
    (override on-size)
    
    (define backing-store 'foo)
    
    (define on-size
      (lambda (width height)
        (set! backing-store
              (make-object bitmap%
                (send this get-width)
                (send this get-height)
                #f))))
  
    (super-instantiate ())))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Which is nice since the backing-store is re-allocated whenever the
canvas% is resized. If I use this version, I just need to make sure
that on-size is called at the time of instantiation.

Separate question... Is it possible to override the draw-* methods
(such as draw-line, draw-point, etc) ? If there was a class I could
use as a base then it would be clearer to me, howerver I'm not sure
how to create a derivative of a canvas device context. Let's say I had
the extended-canvas% class as shown above and then wanted to override
draw-line such that it wrote to the backing-store bitmap. It doesn't
seem right to add a method to extended-canvas% called draw-line since
the native draw-line operates on a device context.

Ed



Posted on the users mailing list.