[plt-scheme] event binding help

From: Bruce Hauman (bhauman at cs.wcu.edu)
Date: Sat Feb 28 13:33:46 EST 2004

Hey Brian,

There are many ways to do what you are asking.  Here is some code that 
only implements two different ways.  One uses the event delegation model
that Java uses heavily in its GUI API.  The other just uses straight 
mutation of the button's callback.

I would advise against making a bunch of global event handlers.  It 
would really be good to find the lowest points on the event chain and 
introduce your handlers there.


(module event-binding mzscheme
   (require (lib "mred.ss" "mred")
            (lib "list.ss")
            (lib "class.ss"))

   (define event-canvas%
     (class* canvas% ()

       (define char-event-listener-list '())

       (define/public (install-char-event-listener evnt)
         (set! char-event-listener-list
               (cons evnt
                     char-event-listener-list))) 	
       (define/public (remove-char-event-listener evnt)
         (set! char-event-listener-list
              (remove evnt char-event-listener-list)))

       (define/override (on-char evnt)
         (for-each
          (lambda (event-proc) (event-proc evnt))
          char-event-listener-list)
         )
       (super-new)
   ))

   (define fr (make-object frame% "Event Test" #f))
   (define my-canv (instantiate event-canvas% (fr) (min-width 100)
                  (min-height 100)))
   (define message (new message%
                        (label "initial label for message") (parent fr)))

   (define button-proc (lambda (e c)
                         (send message set-label "no-proc")))


   (define button  (new button%  (label "Click Me") (parent fr)
                        (callback (lambda (a b) (button-proc a b)))))

   (define (disp str)
     (send message set-label str))

   (define (char-event? event code)
     (and (is-a? event key-event%)
          (eq? code (send event get-key-code))))

   (define (do-something)
     (let ((button-press-proc (lambda (a b)
                                (disp "button-press-proc")))
           (escape-key-proc (lambda (event)
                              (when (char-event? event 'escape)
                                (disp "escape-pressed")))))
       (thread (lambda ()
                 (set! button-proc
                       button-press-proc)
                 (send my-canv install-char-event-listener
                       escape-key-proc)
                 (disp "in do something1")
                 (sleep 20)
                 (send my-canv remove-char-event-listener
                       escape-key-proc)
                 (set! button-proc
                       (lambda (a b) (disp "reset")))
                 ))
       ))


   (send fr show #t)



   )




briand at aracnet.com wrote:
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> 
> Here's what the flow looks like now:
> 
> (canvas
>   (on-subwindow-event
>     (lots of event/state checking to draw line
>      ...
> 
> what I want is:
> 
> (canvas
>   (on-subwindow-event
>     (mouse-button-down
>      .. draw-line
> 
> (draw-line
>   
> And there is where I'm not sure about the next step.  I would like to
> set up draw-line so that it now automagically captures the event I'm
> interested in.  So if I get button-down, backspace, esc, etc.. it
> should automatically go to draw-line, until draw line "releases" those
> events.
> 
> Here's how I would do this in stk:
> 
> (define draw-line
>   (let ((button-press-proc
>         (escape-key-proc
>         ...
> 
>    (bind button-press-proc button-press-event ...)
>    (bind escape-key-proc  escape-key-proc ...)
>    ...
> 
> And ultimately this really follows from Tk.
> 
> Unfortunately, after perusing the documentation, it's not obvious to
> me how to do this without simply putting a brute force event filter in
> on-subwindow-event and dispatching based on mode.  And that seems like
> a very kludgey way to do it.
> 
> 
> 
> 
> Brian
> 
> 
> 
>>>>>>"Bruce" == Bruce Hauman <bhauman at cs.wcu.edu> writes:
> 
> 
>   Bruce>   For list-related administrative tasks:
>   Bruce>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
>   Bruce> briand at aracnet.com wrote:
> 
>   >> 
>   >> A simple outline of the approach which would work best with mzscheme
>   >> would be most helpful.
>   >> 
>   >> 
> 
>   Bruce> Maybe some more information would be helpful.  I am assuming
>   Bruce> the line drawing routine you are refering to is reacting to
>   Bruce> mouse input to draw the line.
> 
>   Bruce> If you are drawing on a canvas% I believe you would be
>   Bruce> interested in the on-event method, which is invoked when
>   Bruce> mouse events occur. You can have on-event forward its events
>   Bruce> to a mutable method which you can mutate with the button
>   Bruce> press callback/event.
> 
>   Bruce> I can describe this process in more detail if you think I am
>   Bruce> on the right track.
> 
>   Bruce> Bruce Hauman
> 
> 



Posted on the users mailing list.