[racket] gui issue > frame updating control thread painter > questions about how to resolve the problem, attain eventual reactive semantics

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Fri Apr 20 08:26:15 EDT 2012

The right approach here, I believe, is to have some fields in the
canvas% that hold the most recently seen x and y coordinates. In
on-event, update those fields and call the refresh method of the
canvas. In on-paint, draw based on those coordinates.


On Thu, Apr 19, 2012 at 1:32 PM, Patrick Mahoney
<paddy.mahoney at gmail.com> wrote:
> #lang racket
> #|Hello all, I'm  PMah. |#
> (require racket/gui/base)
> #|Problem: I have a message% control called x-pos parented by a frame%
> called parent1. I have a canvas% called canvas parented under frame%
> parent2. I would like x-pos to always show the current x coordinate of the
> mouse position over canvas, and for this to be kept up to date as the mouse
> moves. Ultimately I'd love to move to a reactive semantics. |#
> #|Setting up the first frame and parent. I'm also going to grab the
> eventspace for this frame while I'm at it.|#
> (define parent1 (new frame%
>                               [label "Parent1"]
>                               [width 200]
>                               [height 200]
>                               [enabled #t]))
> (define x-pos (new message%
>                    [parent parent1]
>                    [label "No x-pos yet."]))
> (send parent1 show #t)
> (define eventspace1 (send parent1 get-eventspace))
> #|The second eventspace*frame*pos combo-|#
> (define parent2 (new frame%
>                               [label "Parent2"]
>                               [width 200]
>                               [height 200]
>                               [enabled #t]))
> #|Here comes the canvas subclass-I'm looking to override on-event method to
> grab the x-component of the mouse pos, and I want to have that constantly
> updating the x-pos message%. I really want reactive behavior, but I ran into
> some issues with the frtime-namely, differences in bindings due to the
> mzscheme bindings. the require spec (prefix-in ..) doesn't appear to be in
> the language. I'm probably doing something wrong. |#
> (define y-pos (class canvas%
>     (inherit get-dc)
> #|My notion of scope in objects is not precise. With those defines within
> the scope of the on-event bindings, do the defines recalculate each time
> on-event receives a message? lo, I wish I had some sort of channel protocol
> to pass messages between threads, along with control.
> The mushrooms kick in now.
> |#
>     (define/override (on-event mouse-event)
>       (define min-x (dc:min-x (get-dc)))
>       (define max-x (dc:max-x (get-dc)))
>       (define min-y (dc:min-y (get-dc)))
>       (define max-y (dc:max-y (get-dc)))
>       (define x-pos (send mouse-event get-x))
>       (define y-pos (send mouse-event get-y))
>       (call-in-other-eventspace (make-eventspace) (draw-diagnostics max-x
> max-y x-pos y-pos)))
>     (super-new)))
> (define
> (send parent2 show #t)
> #|I have tried some of the examples on
> http://groups.google.com/group/plt-scheme/browse_thread/thread/28af25a01200bc3c/7937bb0314cc231e?lnk=raot
> Basically, one using a channel, probably incorrectly and the second without.
> Now what I believe I've done is to add the thunk to the queue in the frame%
> parent1's eventspace, but I haven't transferred control. The other
> eventspace's thread still has control. Kind of?
> |#
> (define (call-in-other-eventspace e thunk)
>    (let ([ch (make-channel)])
>      (parameterize ([current-eventspace e])
>        (queue-callback (lambda ()
>                          (channel-put ch (thunk)))))
>      (channel-get ch)))
>   #|This blocks both frames updating in some way-im not able to transfer
> control smoothly to the thread in the message% message1 workspace, and so
> the x-pos label updates only sporadically when the other eventspace thread
> yields? |#
> (define (call-in-other-eventspace e thunk)
>    (parameterize ([current-eventspace e])
>        (queue-callback thunk)))
> #|How can I get x-pos to update automatically on each on-event send here?
> (draw-diagnostics max-x max-y x-pos y-pos) appears to terminate here in all
> cases. Part 2: how can I apply frtime to make the update relationship I want
> between canvas mouse position and message text  (or other racket reactive
> work if it exists.). I really would like to understand this idiom and get it
> in my toolchest for gui programming. Thanks all! Racket is an impressive
> contribution to computer science. |#
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

Posted on the users mailing list.