#lang racket/gui

(define cell-editor%
  (class text%
    (init)
    (super-new)
    ))

(define cell-editor-admin%
  (class editor-admin%
    (init-field grid editor)
    (super-new)
    
    (define/override (get-dc (x #f) (y #f))
      (when x (set-box! x 0))
      (when y (set-box! y 0))
      (send grid get-dc) )
    
    (define/override (get-max-view x y w h (full? #f))
      (get-view x y w h full?))
    
    (define/override (get-view x y w h (full? #f))
      (when x (set-box! x 0))
      (when y (set-box! y 0))
      (when w (set-box! w (send grid get-width)))
      (when h (set-box! h (send grid get-height))) )
    
    (define/override (grab-caret (domain 'global))
      (when (eq? domain 'global)
        (send grid set-focus)))
    
    (define/override (modified modified?) (displayln "modified") (values))
    (define/override (needs-update x y w h)
      (displayln "needs-update")
      (send editor refresh 0 0 (send grid get-width) (send grid get-height) 'show-caret #f))
    (define/override (popup-menu menu x y) (displayln "popup-menu") #f)
    (define/override (refresh-delayed?) (displayln "refresh-delayed?") #f)
    (define/override (resized refresh?)
      (displayln "resized")
      (send editor refresh 0 0 (send grid get-width) (send grid get-height) 'show-caret #f))
    (define/override (scroll-to x y w h (refresh? #t) (bias 'none))
      (displayln "scroll-to")
      (send editor refresh 0 0 (send grid get-width) (send grid get-height) 'show-caret #f))
    (define/override (update-cursor) (displayln "update-cursor") (values))
    ))

(define grid%
  (class canvas%
    (init parent editor (min-width 100) (min-height 100))
    (super-new (parent parent)
               (min-width min-width) (min-height min-height)
               (stretchable-width #f) (stretchable-height #f)
               (style '(border)))
    
    (define admin (new cell-editor-admin% (grid this) (editor editor)))
    (send editor set-admin admin)
    ;(send editor insert (make-object string-snip% "this"))
    ))

(define root (new frame% (label "Grid Test")))

(define editor (new cell-editor%))
(define grid (new grid% (parent root) (editor editor)))

(send root show #t)