[plt-scheme] flowing text into two text% objects

From: Robby Findler (robby at cs.uchicago.edu)
Date: Thu Nov 29 14:00:13 EST 2007

Here's one way to do it. (I changed a few things from what you have--
the scroll bars are on the left one, not the right one and I had to
change the way you make the window to do that, but mostly the change
is the new class at the top of the file.)

The hack here is that I'm using on-paint to tell when the
editor-canvas% has scrolled. I believe that this is safe, but it would
be nice to get that guarantee out of the documentation, or perhaps
another method with a more fitting name :).

Robby

(require (lib "class.ss")
         (lib "mred.ss" "mred"))

(define synched-editor-canvas%
  (class editor-canvas%
    (init-field other-one)
    (define visible-region #f)
    (inherit call-as-primary-owner get-editor)
    (define/override (on-paint)
      (super on-paint)
      (call-as-primary-owner
       (λ ()
         (let ([admin (send (get-editor) get-admin)])
           (let ([xb (box 0)]
                 [yb (box 0)]
                 [wb (box 0)]
                 [hb (box 0)])
             (send admin get-view xb yb wb hb)
             (let ([x (unbox xb)]
                   [y (unbox yb)]
                   [w (unbox wb)]
                   [h (unbox hb)])
               (let ([new-region (list x y w h)])
                 (unless (equal? new-region visible-region)
                   (set! visible-region new-region)
                   (send other-one scroll-to x (+ y h) w h #t)))))))))
    (super-new)))

(define lorem-ipsum
  "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris
ultrices. Ut rutrum lacus quis tortor. Cras commodo massa vel nisl.
Phasellus porta malesuada nulla. Cras quis tortor. Pellentesque ut
urna. Integer ipsum elit, pulvinar sollicitudin, pulvinar et, feugiat
nec, turpis. Aliquam accumsan. Aenean bibendum turpis at nisl. Aliquam
in augue et felis bibendum vulputate. Nunc non arcu.

Donec erat arcu, tempor sit amet, posuere sit amet, dignissim ac,
enim. Donec eget tellus. Suspendisse hendrerit arcu posuere magna. Sed
non purus eu libero molestie consectetuer. Nunc id ligula vel ipsum
cursus consectetuer. Nullam tristique porttitor lorem. Sed odio dolor,
venenatis in, mollis eu, elementum id, enim. Sed varius varius leo.
Mauris eleifend nonummy est. Morbi ut mi. Curabitur eget libero.
Phasellus leo urna, volutpat sed, laoreet vel, semper ac, mauris.
Pellentesque habitant morbi tristique senectus et netus et malesuada
fames ac turpis egestas.

Fusce ut massa ac risus porta eleifend. Mauris non enim. Pellentesque
consequat, lacus quis tempus sollicitudin, lectus elit tempor orci, a
tempor dolor felis et nunc. Sed eget nunc quis metus tempus mattis.
Donec aliquet euismod velit. Nullam sit amet eros. Mauris dui. Aliquam
ligula enim, commodo vitae, imperdiet quis, convallis id, ante.
Curabitur sit amet metus quis nulla euismod tincidunt. Integer
interdum. Vestibulum bibendum, velit imperdiet faucibus ullamcorper,
libero eros ornare sem, sed commodo justo quam a sem. Vestibulum nunc.
In tincidunt pede at pede. Duis ante lacus, sodales ac, lacinia vel,
commodo at, ipsum. Phasellus malesuada. Ut malesuada dolor. Quisque
dolor ante, blandit in, faucibus id, laoreet nec, libero.

Vestibulum adipiscing mollis nisi. Aliquam nec sem eget tortor
suscipit mattis. Pellentesque habitant morbi tristique senectus et
netus et malesuada fames ac turpis egestas. Phasellus et massa. Sed
nunc nulla, lobortis quis, tincidunt sed, fringilla at, enim. Ut
varius pharetra erat. Phasellus pharetra nulla sed felis. Nulla
facilisi. Vestibulum vulputate nisi a quam. Suspendisse fringilla odio
vitae purus. Sed dapibus nonummy justo. Integer mollis ligula vel odio
sollicitudin euismod. Fusce vel turpis. Aliquam semper lacinia diam.
Mauris fermentum, pede nec rhoncus vehicula, lorem turpis cursus diam,
a tempor ligula justo vulputate tortor. Sed sodales orci ut metus. Sed
et tortor.")

(define f (new frame%
               (label "Foo")
               (min-width 800)
               (min-height 400)
               ))

(define v (new vertical-pane%
               (parent f)))

(define h (new horizontal-pane%
               (parent v)
               ))

(define txt (new text%
                 (auto-wrap #t)
                 ))

(define right (new editor-canvas%
                  (parent h)
                  (editor txt)
                  (style '(hide-vscroll))
                  (min-width 300)
                  (min-height 200)
                  (scrolls-per-page 1)
                  ))

(define space (new horizontal-panel%
                   (parent h)
                   (min-width 100)))

(define left (new synched-editor-canvas%
                   [other-one right]
                   (parent h)
                   (editor txt)
                   (min-width 300)
                   (min-height 200)))
(send h change-children reverse)

(define start (box 0))
(define end (box 0))

(define b (new button%
               (parent v)
               (label "Extent")
               (callback
                (lambda (w e)
                  (define last (send txt last-position))
                  (printf "~a~n" last)
                  (send txt get-visible-position-range start end #f)
                  (printf "~a, ~a~n"
                          (unbox start)
                          (unbox end))))))

(send f show #t)



(send txt insert lorem-ipsum)
;; Scroll the text to the top; this gets everything.
(send txt scroll-to-position 0 #f 'same 'none)

Posted on the users mailing list.