;; Code from class, 8/24/2007 (define-type GUI [label (text string?)] [button (text string?) (enabled? boolean?)] [choice (items (listof string?)) (selected number?)] [vertical (top GUI?) (bottom GUI?)] [horizontal (left GUI?) (right GUI?)]) (define g1 (vertical (horizontal (label "Pick a fruit:") (choice '("Apple" "Banana" "Coconut") 0)) (horizontal (button "Ok" false) (button "Cancel" true)))) ;; can-click? : GUI -> boolean (define (can-click? g) (type-case GUI g [label (t) false] [button (t on?) on?] [choice (i n) false] [vertical (t b) (or (can-click? t) (can-click? b))] [horizontal (l r) (or (can-click? l) (can-click? r))])) (test (can-click? (label "Pick a fruit:")) false) (test (can-click? (button "Ok" false)) false) (test (can-click? (button "Cancel" true)) true) (test (can-click? (choice '("Apple" "Banana" "Coconut") 0)) false) (test (can-click? g1) true) ;; read-screen : GUI -> list-of-string (define (read-screen g) (type-case GUI g [label (t) (list t)] [button (t on?) (list t)] [choice (i n) i] [vertical (t b) (append (read-screen t) (read-screen b))] [horizontal (l r) (append (read-screen l) (read-screen r))])) (test (read-screen (label "Pick a fruit:")) '("Pick a fruit:")) (test (read-screen (button "Ok" false)) '("Ok")) (test (read-screen (choice '("Apple" "Banana" "Coconut") 0)) '("Apple" "Banana" "Coconut")) ;; need at least one more test here (test (read-screen g1) '("Pick a fruit:" "Apple" "Banana" "Coconut" "Ok" "Cancel")) ;; enable-button : GUI string -> GUI (define (enable-button g name) (type-case GUI g [label (t) g] [button (t on?) (button t (or on? (string=? t name)))] [choice (i sel) g] [vertical (t b) (vertical (enable-button t name) (enable-button b name))] [horizontal (l r) (horizontal (enable-button l name) (enable-button r name))])) (test (enable-button (label "Pick a fruit:") "Ok") (label "Pick a fruit:")) (test (enable-button (button "Ok" false) "Cancel") (button "Ok" false)) ;; need more tests here (test (enable-button (vertical (horizontal (label "Pick a fruit:") (choice '("Apple" "Banana" "Coconut") 0)) (horizontal (button "Ok" false) (button "Cancel" true))) "Ok") (vertical (horizontal (label "Pick a fruit:") (choice '("Apple" "Banana" "Coconut") 0)) (horizontal (button "Ok" true) (button "Cancel" true)))) (define (prefix n s) (string-append (number->string n) " " s)) ;; show-depth : GUI num -> GUI (define (show-depth-at g d) (type-case GUI g [label (t) (label (prefix d t))] [button (t on?) (button (prefix d t) on?)] [choice (i sel) (choice (map (lambda (s) (prefix d s)) i) sel)] [vertical (t b) (vertical (show-depth-at t (+ 1 d)) (show-depth-at b (+ 1 d)))] [horizontal (l r) (horizontal (show-depth-at l (+ 1 d)) (show-depth-at r (+ 1 d)))])) (define (show-depth g) (show-depth-at g 0)) (test (show-depth (label "Hello")) (label "0 Hello")) ;; need more tests here (test (show-depth (vertical (label "Pick a fruit:") (horizontal (button "Ok" false) (button "Cancel" true)))) (vertical (label "1 Pick a fruit:") (horizontal (button "2 Ok" false) (button "2 Cancel" true)))) ;; A list-of-string is either ;; - empty ;; - (cons string list-of-string) (define (has-label? l name) (cond [(empty? l) false] [(cons? l) (or (string=? name (first l)) (has-label? (rest l) name))])) (test (has-label? empty "Apple") false) (test (has-label? '("Apple" "Banana") "Apple") true) (test (has-label? '("Apple" "Banana") "Coconut") false)