[plt-scheme] Class questions

From: Robby Findler (robby at cs.uchicago.edu)
Date: Tue May 13 19:24:22 EDT 2003

You can do this:

(class canvas%
  (define/override (on-size w h)
    (unless (and (= w (send backing-store get-width))
                 (= h (send backing-store get-height)))
      (set! backing-store (new-backing-store w h))))
  (define/private (new-backing-store w h)
    (make-object bitmap% w h))
  (super-instantiate ())
  (inherit get-width get-height)
  (field [backing-store (new-backing-store (get-width) (get-height))]))


Robby

At Tue, 13 May 2003 18:17:41 -0500 (CDT), Ed Cavazos wrote:
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> 
> 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.