;;;; ;;;; STk adaptation of the Tk widget demo. ;;;; ;;;; This demonstration script creates a canvas widget with a text ;;;; item that can be edited and reconfigured in various ways. ;;;; (define (demo-ctext) (define config-fill #f) (define current #f) (define (Enter c) (set! current (car (find-items c 'withtag "current"))) (set! config-fill (slot-ref current 'fill)) (slot-set! current 'fill "black")) (define (Leave c) (slot-set! current 'fill config-fill)) (define (Insert c string) (unless (equal? string "") (catch c (delete-chars c "text" "sel.first" "sel.last")) (text-insert c "text" 'insert string))) (define (Paste c x y) (catch (text-insert c "text" (format #f "@~A,~A" x y) (selection 'get)))) (define (B1-press c x y) (let ((pos (format #f "@~A,~A" x y))) (icursor c "current" pos) (focus c "current") (focus c) (text-selection c 'from "current" pos))) (define (B1-move c x y) (text-selection c 'to "current" (format #f "@~A,~A" x y))) (define (Bs c) (when (catch (delete-chars c "text" "sel.first" "sel.last")) (let ((char (- (text-index c "text" "insert") 1))) (if (>= char 0) (delete-chars c "text" char))))) (define (Del c) (when (catch (delete-chars c "text" "sel.first" "sel.last")) (delete-chars c "text" "insert"))) (let* ((w (make-demo-toplevel "ctext" "Canvas Text Demonstration" "This window displays a string of text to demonstrate the text facilities of canvas widgets. You can click in the boxes to adjust the position of the text relative to its positioning point or change its justification. The text also supports the following simple bindings for editing: 1. You can point, click, and type. 2. You can also select with button 1. 3. You can copy the selection to the mouse position with button 2. 4. Backspace and Control+h delete the character just before the insertion cursor. 5. Delete deletes the character just after the insertion cursor. ")) (font "-*-Helvetica-Medium-R-Normal--*-240-*-*-*-*-*-*") (c (make :parent w :width 500 :height 400)) (txt (make :parent c :coords '(250 200) :width 440 :anchor "n" :font font :justify "left" :text "This is just a string of text to demonstrate the text facilities of canvas widgets. You can point, click, and type. You can also select and then delete with Control-d."))) (pack c :side "top" :expand #t :fill "both") ;; First, create the text item and give it bindings so it can be edited. (add-tag txt "text") (bind c "text" "<1>" (lambda (x y) (B1-press c x y))) (bind c "text" "" (lambda (x y) (B1-move c x y))) (bind c "text" "" (lambda (x y) (B1-move c x y))) (bind c "text" "" (lambda (|A|) (Insert c |A|))) (bind c "text" "" (lambda () (Insert c "\n"))) (bind c "text" "" (lambda () (BS c))) (bind c "text" "" (lambda () (BS c))) (bind c "text" "" (lambda () (Del c))) (bind c "text" "<2>" (lambda (x y) (Paste c x y))) (let* ((x 50) (y 50) (color "LightSkyBlue1") (txt-config (lambda (x y option value color) (let ((item (make :parent c :coords (list x y (+ x 30) (+ y 30)) :outline "black" :tags "square" :fill color :width 1))) (bind item "<1>" (lambda () (slot-set! txt option value))))))) ;; Create some items that allow the text's anchor position to be edited. (txt-config (+ x 00) (+ y 00) 'anchor "se" "LightSkyBlue1") (txt-config (+ x 30) (+ y 00) 'anchor "s" "LightSkyBlue1") (txt-config (+ x 60) (+ y 00) 'anchor "sw" "LightSkyBlue1") (txt-config (+ x 00) (+ y 30) 'anchor "e" "LightSkyBlue1") (txt-config (+ x 30) (+ y 30) 'anchor "center" "LightSkyBlue1") (txt-config (+ x 60) (+ y 30) 'anchor "w" "LightSkyBlue1") (txt-config (+ x 00) (+ y 60) 'anchor "ne" "LightSkyBlue1") (txt-config (+ x 30) (+ y 60) 'anchor "n" "LightSkyBlue1") (txt-config (+ x 60) (+ y 60) 'anchor "nw" "LightSkyBlue1") ;; Create some items that allow the text's justification to be changed. (txt-config (+ x 300) y 'justify "left" "SeaGreen2") (txt-config (+ x 330) y 'justify "center" "SeaGreen2") (txt-config (+ x 360) y 'justify "right" "SeaGreen2") ;; The two little red squares and the both titles (let ((item1 (make :parent c :coords (list (+ x 40) (+ y 40) (+ x 50) (+ y 50)) :outline "black" :fill "red")) (item2 (make :parent c :coords '(245 195 255 205) :outline "black" :fill "red"))) (bind item1 "<1>" (lambda () (slot-set! txt 'anchor "center"))) (make :parent c :text "Text Position" :anchor "s" :font font :fill "brown":coords (list (+ x 45) (- y 5))) (make :parent c :text "Justification" :anchor "s" :font font :fill "brown":coords (list (+ x 345) (- y 5)))) ;; Associate bindings to squares (bind c "square" "" (lambda () (Enter c))) (bind c "square" "" (lambda () (Leave c))))))