[plt-scheme] transparent background in an editor-canvas%

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon May 12 11:55:03 EDT 2003

At Mon, 12 May 2003 14:33:43 +0200 (CEST), Berticolas Copimasso wrote:
> we are on version 203 under win 2k  and we'd like to
> have a transparent background in an editor-canvas% we
> have wrote : 
> (define ec (instantiate editor-canvas% () (parent
> frame)))
> (define dc (send ec get-dc))
> (define txt (instantiate text%))
> (send dc set-background (make-object color%
> "transparent"))
> (send dc set-text-foreground (make-object color%
> "BLUE"))
> (send ec set-editor txt)
> and the text is black on a white background.

Short answer: I don't think it will work.

Long answer:

In this example, the text background is transparent with respect to the
canvas, but the canvas is white. ("Transparent" is not a color name, so
the result is white.)

There's currently no way to create a transparent canvas. If you want
the canvas background to look the same as a panel, you can override the
canvas's `on-paint' method and fill in the canvas with a 'panel brush.
Well, then you also have to do the ame for the text%. And even so, you
end up with the canvas border. Also, under OS X at least, there's an
alignment problem with the panel pattern.

Below is my best attempt, in any case. If there were a way to remove
the border around an editor-canvas% (and I've been planning something
along those lines), it would be closer. I'm not sure about the OS X
pattern-alignment problem.



(define panel-brush (make-object brush% "white" 'panel))
(define (fill-as-panel dc)
  (let ([old-b (send dc get-brush)])
    (send dc set-brush panel-brush)
    (send dc draw-rectangle -1 -1 32000 32000)
    (send dc set-brush old-b)))

(define transparent-looking-editor-canvas%
  (class editor-canvas%
    (rename [super-on-paint on-paint])
    (inherit get-dc)
    (define/override (on-paint)
      (fill-as-panel (get-dc))
    (super-instantiate ())))

(define transparent-looking-editor-canvas%
  (class editor-canvas%
    (rename [super-on-paint on-paint])
    (inherit get-dc)
    (define/override (on-paint)
      (fill-as-panel (get-dc))
    (super-instantiate ())))

(define transparent-looking-text%
  (class text%
    (rename [super-on-paint on-paint])
    (inherit get-dc)
    (define/override (on-paint before? dc . rest)
      (when before?
        (fill-as-panel dc))
      (super-on-paint before? dc . rest))
    (super-instantiate ())))

(define frame (make-object frame% "Example"))
(define ec 
  (instantiate transparent-looking-editor-canvas% () 
    (parent frame)))
(define dc (send ec get-dc))
(define txt (instantiate transparent-looking-text% ()))
(send ec set-editor txt)
(send frame show #t)

Posted on the users mailing list.