[plt-scheme] creating new widgets for MrEd
Hi,
I just added an entry to the comments in the Scheme cookbook in the
'GUI: Aggregating
Widgets in a Single Widget' section
-- http://schemecookbook.org/Cookbook/GUIWidgetAggregation
It is an example of a simple widget created from drawing toolkit elements,
rather than existing widgets.
I'd appreciate any comments/edits to improve it. (technically or
pedagogically)
Cheers, Stephen
; make new widget'scatter-chart%'
;; do this all in Pretty Big language (DrScheme only)
(define scatter-chart% (class pane% ()
(init-field parent)
; Make the drawing area
(super-instantiate () (parent parent) ;
instantiate this FIRST
[min-width 100] [stretchable-width #f]
(min-height 200) [stretchable-height #f])
;; internal data
(define list-of-points '()) ;; start with empty list
(define canvas (new canvas% [parent this]
(paint-callback (lambda
(canvas dc) (draw-chart)))
[min-width 100]
[stretchable-width #f]
[min-height 200]
[stretchable-height #f]
))
(define dc (send canvas get-dc)) ; Get the
canvas's drawing context
; Make some pens and brushes for use later
(define no-pen (make-object pen% "BLACK" 1
'transparent))
(define blue-brush (make-object brush% "BLUE" 'solid))
(define yellow-brush (make-object brush%
"YELLOW" 'solid))
;; public methods
;; add-point : new-point
(define/public (add-point new-point)
(set! list-of-points (cons new-point
list-of-points)) ;; add to the list
(send dc draw-rectangle 0 new-point 200 1))
;; and draw the line
;; draw-chart : draw a chart based on the
list of integers supplied
;; see
http://download.plt-scheme.org/doc/371/html/mred/mred-Z-H-397.html
(define/public (draw-chart)
(send dc set-pen no-pen)
(send dc set-brush blue-brush)
(send dc draw-rectangle 0 0 100 200) ; big
blue rectangle
(send dc set-brush yellow-brush)
(map
(lambda (y) (send dc draw-rectangle 0 y
200 1)) ; draw a line for each integer.
list-of-points ))
(draw-chart))) ; draw it for the first time
when this class is instantiated
; Make a frame
(define frame (new frame% [label "scatter-chart% Example"] [width 100]
[height 200]))
; Show the frame
(send frame show #t)
; Wait a second to let the window get ready
;(sleep/yield 1)
(define new-my-widget (new scatter-chart% (parent frame) )) ;
instantiate the widget in the frame
(define new-his-widget (new scatter-chart% (parent frame) )) ; make another one
(send new-my-widget draw-chart)
; Draw the face
(define (go)
(map (lambda (y)
(send new-my-widget add-point y) ; add a point at a time
(sleep/yield 0.05)) ; draw it slowly
'(10 30 22 77 78 79 17 65 97 54 55 56 57 91 33 55 95 99 125 155
157 159 188 187 186 191))) ;; list of datapoints
(go) ;; !!!
--
Cheers,
Stephen
--
Stephen De Gabrielle
s.degabrielle at ucl.ac.uk
Telephone +44 (0)20 7679 5242 (x45242)
Mobile 079 851 890 45
http://www.uclic.ucl.ac.uk/annb/MaSI.html
University College London Interaction Centre
Remax House - 31/32 Alfred Place
London - WC1E 7DP
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070921/d8c7522d/attachment.html>