[plt-scheme] streams; event-handling

From: Nicholas Chubrich (chubrich at cs.brandeis.edu)
Date: Tue Apr 11 22:55:03 EDT 2006

I've recently been playing with the drawing canvas as a way of getting
aquainted with MrEd.  My canvas class draws freehand lines when the
left-mouse button is down, and a straight line between two right-clicks.  
What bothers me is that the basic control structure is set.  There is a
single function on-event that simply runs at the next event, so I have to
set a flag in the canvas object to record state; I also have to record
previous points, and in a different way depending on what state I am in.  
It brings back bad memories of C programming.  My natural instinct is to
use an "event stream"; something like this (with a bit of fantasy syntax):

(define (dispatch stream)
  (cond ((send (car stream) button-down? 'left)
         (draw-free (send (car stream) get-point) (cdr stream)))
        ((send (car stream) button-down? 'right)
         (draw-straight (send (car stream) get-point) (cdr stream)))
        (else (dispatch (cdr stream)))))

(define (draw-straight point stream)
  (cond ((send (car stream) button-down? 'right)
         (line point (send (car stream) get-point)))
        ((or (send (car stream) moving?) (send (car stream) button-up? 'right))
         (draw-straight point (cdr stream)))
        (else (dispatch stream))))

draw-free would be defined similarly; the key difference is that the
recursive call passes (send (car stream) get-point) instead of just point.
	What is the 'PLT way' of structuring an event-driven program in 
this spirit?  Is there any way to do it without overkill (threads, for 
instance)?  Am I better off just using state changes?  Or am I completely  
missing something? 

---Nick Chubrich.


Posted on the users mailing list.