;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname textbox-demo-ti-before) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) (require 2htdp/image) (require 2htdp/universe) (require "student-extras.rkt") ; Datatype def'n: ; (define-struct/contract tb ([contents string?] [cursor natural?])) ; ; Interpretation: a textbox, with the text `contents`, ; with `cursor` characters preceding the insertion-point(caret/cursor). ; Thus: (<= 0 cursor (string-length contents)) ; ; racket auto-creates constructer as above, getters, and a predicate: ; tb-contents : (-> tb? string?) ; tb-cursor : (-> tb? natural?) ; tb? : (-> any/c boolean?) ; examples of data (make-tb "hello" 0) ; cursor before entire word (make-tb "hello" 1) (make-tb "hello" 5) (make-tb "hello" 3) (make-tb "h" 0) (make-tb "h" 1) (define EMPTY-TEXTBOX (make-tb "" 0)) ; template: (define/c (func-for-tb a-tb) (-> tb? ...?) (... (tb-contents a-tb) (tb-cursor a-tb))) ; draw-tb ;========== implement `draw` (check-expect (draw (make-tb "hello" 3)) (overlay/align "left" "center" (beside (text "hel" 20 'blue) (rectangle 1 17 'solid 'orange) (text "lo" 20 'blue)) (rectangle 200 20 'outline 'green))) (check-expect (draw (make-tb "hello" 0)) (overlay/align "left" "center" (beside (rectangle 1 17 'solid 'orange) (text "hello" 20 'blue)) (rectangle 200 20 'outline 'green))) (check-expect (draw (make-tb "hello" 5)) (overlay/align "left" "center" (beside (text "hello" 20 'blue) (rectangle 1 17 'solid 'orange)) (rectangle 200 20 'outline 'green))) (check-expect (draw (make-tb "" 0)) (overlay/align "left" "center" (rectangle 1 17 'solid 'orange) (rectangle 200 20 'outline 'green))) ; draw : tb? -> image? ; Create an image corresponding to the tb. ; (define (draw a-tb) (... (tb-contents a-tb) ... (tb-cursor a-tb) ...)) ;=========== (check-expect (handle-key (make-tb "hello" 5) "right") (make-tb "hello" 5)) (check-expect (handle-key (make-tb "hello" 0) "left") (make-tb "hello" 0)) (check-expect (handle-key (make-tb "hello" 0) "\b") (make-tb "hello" 0)) (check-expect (handle-key (make-tb "hello" 3) "x") (make-tb "helxlo" 4)) (check-expect (handle-key (make-tb "hello" 3) "right") (make-tb "hello" 4)) (check-expect (handle-key (make-tb "hello" 3) "left") (make-tb "hello" 2)) (check-expect (handle-key (make-tb "hello" 3) "\b") (make-tb "helo" 2)) ; handle-key : tb?, key-event? -> tb? ; Update `a-tb` to incorporate `key`. ; (define (handle-key a-tb key) (cond [(key=? key "right") (make-tb (tb-contents a-tb) (min (add1 (tb-cursor a-tb)) (string-length (tb-contents a-tb))))] [(key=? key "left") ...] [(key=? key "\b") ...] [else (make-tb (tb-contents a-tb) (tb-cursor a-tb))] )) #;(big-bang (make-tb "" 0) [on-draw draw] [on-key handle-key])