#lang plai (define-type GUI [label (name string?)] [button (name string?) (enabled boolean?)] [choice (items (listof string?)) (selected integer?)] [hpanel (left GUI?) (right GUI?)] [vpanel (top GUI?) (bottom GUI?)]) ;; read-screen : GUI -> list-of-string ;(define (read-screen gui) ; (type-case GUI gui ; [label (n) ... n ...] ; [button (n e?) ... n ... e? ...] ; [choice (is sel) ... (read-los is) ... sel ...] ; [hpanel (l r) ... (read-screen l) ... (read-screen r) ...] ; [vpanel (t b) ... (read-screen t) .... (read-screen b) ...]) ; read-los : list-of-string -> list->string (define (read-screen gui) (type-case GUI gui [label (n) (list n)] [button (n e?) (list n)] [choice (is sel) is] [hpanel (l r) (append (read-screen l) (read-screen r))] [vpanel (t b) (append (read-screen t) (read-screen b))])) (define gui1 (vpanel (hpanel (label "Pick a fruit:") (choice (list "Apple" "Banana" "Coconut") 0)) (hpanel (button "Ok" #f) (button "Cancel" #t)))) (test (read-screen (label "Pick a fruit:")) (list "Pick a fruit:")) (test (read-screen (button "Ok" #t)) (list "Ok")) (test (read-screen (choice (list "Apple" "Banana") 0)) (list "Apple" "Banana")) (test (read-screen (hpanel (label "Pick a fruit:") (button "Ok" #t))) (list "Pick a fruit:" "Ok")) (test (read-screen (vpanel (label "Pick a fruit:") (button "Ok" #t))) (list "Pick a fruit:" "Ok")) (test (read-screen gui1) (list "Pick a fruit:" "Apple" "Banana" "Coconut" "Ok" "Cancel")) ;; enable-all-buttons : gui -> gui (define (enable-all-buttons gui) (type-case GUI gui [label (n) gui] [button (n e?) (button n #t)] [choice (is sel) gui] [hpanel (l r) (hpanel (enable-all-buttons l) (enable-all-buttons r))] [vpanel (t b) (vpanel (enable-all-buttons t) (enable-all-buttons b))])) (test (enable-all-buttons (label "Pick a fruit:")) (label "Pick a fruit:")) (test (enable-all-buttons (button "Ok" #t)) (button "Ok" #t)) (test (enable-all-buttons (button "Ok" #f)) (button "Ok" #t)) (test (enable-all-buttons (choice (list "Apple" "Banana") 0)) (choice (list "Apple" "Banana") 0)) (test (enable-all-buttons (hpanel (label "Pick a fruit:") (button "Ok" #t))) (hpanel (label "Pick a fruit:") (button "Ok" #t))) (test (enable-all-buttons (hpanel (label "Pick a fruit:") (button "Ok" #f))) (hpanel (label "Pick a fruit:") (button "Ok" #t))) (test (enable-all-buttons (vpanel (label "Pick a fruit:") (button "Ok" #f))) (vpanel (label "Pick a fruit:") (button "Ok" #t))) (test (enable-all-buttons gui1) (vpanel (hpanel (label "Pick a fruit:") (choice (list "Apple" "Banana" "Coconut") 0)) (hpanel (button "Ok" #t) (button "Cancel" #t)))) ;; show-depth : GUI number -> GUI ;; d is how many panels we've gone inside (define (show-depth gui d) (type-case GUI gui [label (n) (label (add-number d n))] [button (n e?) (button (add-number d n) e?)] [choice (is sel) (choice (map (lambda (s) (add-number d s)) is) sel)] [hpanel (l r) (hpanel (show-depth l (add1 d)) (show-depth r (add1 d)))] [vpanel (t b) (vpanel (show-depth t (add1 d)) (show-depth b (add1 d)))])) ;; add-number : num string -> string (define (add-number d str) (string-append (number->string d) " " str)) (test (show-depth (label "Pick a fruit:") 0) (label "0 Pick a fruit:")) (test (show-depth (label "Pick a fruit:") 17) (label "17 Pick a fruit:")) (test (show-depth (button "Ok" #t) 0) (button "0 Ok" #t)) (test (show-depth (button "Ok" #f) 0) (button "0 Ok" #f)) (test (show-depth (choice (list "Apple" "Banana") 0) 0) (choice (list "0 Apple" "0 Banana") 0)) (test (show-depth (hpanel (label "Pick a fruit:") (button "Ok" #t)) 0) (hpanel (label "1 Pick a fruit:") (button "1 Ok" #t))) (test (show-depth (vpanel (label "Pick a fruit:") (button "Ok" #f)) 0) (vpanel (label "1 Pick a fruit:") (button "1 Ok" #f))) (test (show-depth gui1 0) (vpanel (hpanel (label "2 Pick a fruit:") (choice (list "2 Apple" "2 Banana" "2 Coconut") 0)) (hpanel (button "2 Ok" #f) (button "2 Cancel" #t))))