get the right number of visible lines

This commit is contained in:
Jon Rafkind 2010-09-29 23:33:08 -06:00
parent 8cba0a0627
commit a9b9f0c0de

View File

@ -450,90 +450,86 @@ module browser threading seems wrong.
;; links two editor's together so they scroll in tandem
(define (linked-scroller %)
(class %
(super-new)
(field [linked #f])
(init-field [line-numbers? #f])
(super-new)
(field [linked #f])
(init-field [line-numbers? #f])
(inherit insert line-start-position line-end-position)
(inherit insert line-start-position line-end-position)
(define/public (link-to! who)
(set! linked who))
(define/public (link-to! who)
(set! linked who))
#;
(define/override (scroll-editor-to . args)
(printf "Scroll editor to ~a\n" args))
#;
(define/override (scroll-to-position . args)
(printf "Scroll-to-position ~a\n" args))
#;
(define/override (scroll-editor-to . args)
(printf "Scroll editor to ~a\n" args))
(define (visible? want-start want-end)
(define start (box 0))
(define end (box 0))
(send this get-visible-line-range start end)
(and (>= want-start (unbox start))
(<= want-end (unbox end))))
#;
(define/override (scroll-to-position . args)
(printf "Scroll-to-position ~a\n" args))
(define/public (scroll-to-line start end)
(when (not (visible? start end))
(send this scroll-to-position
(send this line-end-position start)
#f
(send this line-end-position end))))
(define self (gensym))
(define (visible? want-start want-end)
(define start (box 0))
(define end (box 0))
(send this get-visible-line-range start end)
#;
(printf "Visible line range ~a ~a ~a\n" (unbox start) (unbox end) self)
(and (>= want-start (unbox start))
(<= want-end (unbox end))))
(define/augment (after-delete start length)
(when (not line-numbers?)
(when linked
(send linked ensure-length (send this last-line))))
(inner (void) after-delete start length))
(define/public (scroll-to-line start end)
#;
(printf "Need to scroll to ~a ~a ~a\n" start end self)
;; dont need to scroll unless the range of lines is out of view
(when (not (visible? start end))
(send this scroll-to-position
(send this line-end-position start)
#f
(send this line-end-position end))))
(define/augment (after-insert start length)
(update-numbers)
#;
(when (not line-numbers?)
(when linked
(send linked ensure-length (send this last-line))))
(inner (void) after-insert start length))
(define/augment (after-delete start length)
(update-numbers)
(inner (void) after-delete start length))
(define/public (update-numbers)
(when (and (not line-numbers?) linked)
(send linked ensure-length (send this last-line))))
(define/augment (after-insert start length)
(update-numbers)
(inner (void) after-insert start length))
(define/public (ensure-length length)
(define lines (send this last-line))
#;
(printf "Line count has ~a needs ~a\n" lines length)
(when line-numbers?
(when (> lines (add1 length))
(send this delete
(line-start-position (add1 length))
(line-end-position lines)
#f
))
(send this begin-edit-sequence)
(for ([line (in-range (add1 lines) (add1 (add1 length)))])
#;
(printf "Insert line ~a\n" line)
(insert (format "~a\n" line)))
(send this end-edit-sequence)))
(define/public (update-numbers)
(when (and (not line-numbers?) linked)
(send linked ensure-length (send this last-line))))
(define/override (on-paint . args)
(define start (box 0))
(define end (box 0))
(define (current-time) (current-inexact-milliseconds))
(send this get-visible-line-range start end)
#;
(printf "text: Repaint at ~a to ~a at ~a!\n" (unbox start) (unbox end) (current-time))
(when linked
(send linked scroll-to-line (unbox start) (unbox end)))
(super on-paint . args))
#;
(define/override (on-scroll-on-change . args)
(printf "Scroller on-scroll-on-change ~a\n" args))
#;
(define/override (scroll-to . args)
(printf "Scrolled to ~a\n" args)
#;
(super on-scroll event))))
;; make sure the set of line numbers is complete
(define/public (ensure-length length)
(define lines (send this last-line))
(when line-numbers?
(when (> lines (add1 length))
(send this delete
(line-start-position (add1 length))
(line-end-position lines)
#f))
(send this begin-edit-sequence)
(for ([line (in-range (add1 lines) (add1 (add1 length)))])
#;
(printf "Insert line ~a\n" line)
(insert (format "~a\n" line)))
(send this end-edit-sequence)))
(define/override (on-paint . args)
(define start (box 0))
(define end (box 0))
(define (current-time) (current-inexact-milliseconds))
;; pass #f to avoid getting visible line ranges from multiple sources
(send this get-visible-line-range start end #f)
#;
(printf "text: Repaint at ~a to ~a at ~a!\n" (unbox start) (unbox end) (current-time))
;; update the linked editor when the main widget is redrawn
(when (and (not line-numbers?) linked)
#;
(printf "Send linked scroll to ~a ~a ~a\n" (unbox start) (unbox end) self)
(send linked scroll-to-line (unbox start) (unbox end)))
(super on-paint . args))
))
;; an editor that does not respond to key presses
(define (uneditable %)