Snip responds to scroll input

21 views
Skip to first unread message

Kshitij Sachan

unread,
Jul 10, 2019, 1:07:12 PM7/10/19
to Racket Users
I want to make a snip that resizes based on vertical scroll input. I currently have the following snip code:

#lang racket

(require racket/gui
         racket/gui/base)

;; snip-class
(define test-snip-class
  (make-object
   (class snip-class%
     (inherit set-classname)
     
     (super-new)
     (set-classname "(lib test-snip)")
     ;; not sure if necessary
     (define/override (read f)
       (define w-b (box 0.0))
       (define h-b (box 0.0))
       (send f get w-b)
       (send f get h-b)
       (new scroll-snip% [w (unbox w-b)] [h (unbox h-b)]))
     )))

(define scroll-step-pixels 12)

;; snip stuff
(define scroll-snip%
  (class snip%
    (inherit get-admin set-snipclass set-flags)
    (init-field [w 250] [h 250])
    
    (super-new)
    (set-snipclass test-snip-class)
    (send (get-the-snip-class-list) add test-snip-class)
    (set-flags (list 'handles-events))
    
    (define/override (get-extent dc x y width height descent space lspace rspace)
      (define (maybe-set-box! b v) (when b (set-box! b v)))
        (maybe-set-box! width w)
        (maybe-set-box! height h)
        (maybe-set-box! descent 1.0)
        (maybe-set-box! space 1.0)
        (maybe-set-box! lspace 1.0)
        (maybe-set-box! rspace 1.0))

    (define/override (find-scroll-step y) (inexact->exact (round (/ y scroll-step-pixels))))
    (define/override (get-num-scroll-steps) (quotient h scroll-step-pixels))
    (define/override (get-scroll-step-offset step) (* step scroll-step-pixels))
      
    
    (define/override (on-char dc x y editorx editory event)
      (cond
        [(equal? (send event get-key-code) 'down)
         (define admin (get-admin))
         (set! w (+ w 20))
         (set! h (+ h 20))
         (when admin
           (send admin resized this #t))]
        [(equal? (send event get-key-code) 'up)
         (define admin (get-admin))
         (set! w (- w 20))
         (set! h (- h 20))
         (when admin
           (send admin resized this #t))]
        ;; NOT WORKING!
        [(equal? (send event get-key-code) 'wheel-up)
         (print "wheelup")
         (define admin (get-admin))
         (set! w (+ w 20))
         (set! h (+ h 20))
         (when admin
           (send admin resized this #t))]
        [(equal? (send event get-key-code) 'wheel-down)
         (print "wheelup")
         (define admin (get-admin))
         (set! w (- w 20))
         (set! h (- h 20))
         (when admin
           (send admin resized this #t))]
        [(equal? (send event get-key-code) 'escape)
         (print "wheelup")
         (define admin (get-admin))
         (set! w 150)
         (set! h 150)
         (when admin
           (send admin resized this #t))]))

    (define/override (on-event dc x y editorx editory event)
      (cond
        [(send event button-down?)
        (define admin (get-admin))
        (set! w (+ w 20))
        (set! h (+ h 20))
        (when admin
          (send admin resized this #t))]))
                      
    (define/override (draw dc x y left top right bottom dx dy draw-caret)
      (send dc set-brush "red" 'solid)
      (send dc set-pen "black" 3 'solid)
      (send dc draw-ellipse (+ x 1.0) (+ y 1.0) (- w 2) (- h 2)))

    (define/override (copy)
      (new scroll-snip%))
    ))

I tried implementing this behavior in two different ways:
1. find-scroll-step, get-num-scroll-steps, and get-scroll-step-offset methods - I doubt this is the purpose these methods, but I couldn't find any examples online and the documentation wasn't entirely clear on what they do.
2. Overriding on-char - this seemed more promising because 'wheel-up and 'wheel-down are listed as key codes for a key event, but my snip is still unresponsive to scrolling.

Worse, I suspect that it may not be possible to scroll within a snip in the interactions window because currently every time I scroll it just scrolls the interactions window itself (seems like the interactions window, rather than the snip, is in focus?). 

Please let me know how I can implement scrolling on my snip to resize.
Reply all
Reply to author
Forward
0 new messages