;;; load the persons stuff (load "persons.scm") ;;; some utitlities (define first car) (define rest cdr) ;;; some file globals (define top-frame #f) ; needed to be here because of possible other ; manipulations as in quit-button-callback (define a-person (make-object p2% "dummy" "dummy" "dummy" "dummy")) (define counter 0) (define persons '()) (define number-of-persons 0) (define address-file "addresses") (define dialog-title "Adressausgabe") ;;; some other constants ;; sizes (define initial-text-field-width 200) (define initial-text-field-height 30) (define initial-frame-width 700) (define initial-frame-height 100) (define initial-button-width 70) ;; other magical numbers (define button-label-const 0) (define button-callback-const 1) ;; in vp1 name-const is the first element ;; (define first-name-const 0) (define name-const 1) ;; in vp2 is address-const the first element ;; reminder: list-ref start with 0 (define address-const 0) (define email-const 1) (define vp1 #f) ;; first pane contents First Name, Name (define vp2 #f) ;; second pane contents Address, Email ;;(send top-frame show #t) (define (button-list-template) `(("&previous" ,previous-button-callback) ("&next" ,next-button-callback) ("&Quit" ,quit-button-callback))) ;;; callbacks (define (quit-button-callback button event) (send top-frame on-exit)) (define (change-persons-data data) ;; set the data for a-person to the new values ;; from the list of persons (set! a-person (send a-person build-from-list data))) (define (update-persons-data) (let* ((el (list-ref persons counter))) (change-persons-data el) (fill-dialog a-person)) (values)) (define (previous-button-callback button event) (if (= counter 0) (set! counter number-of-persons) (set! counter (- counter 1))) (update-persons-data)) (define (next-button-callback button event) (if (= counter number-of-persons) (set! counter 0) (set! counter (+ counter 1))) (update-persons-data)) ;;; Vertical panes with simplte text fields for the ;;; objects examples (define first-vertical-pane-template '("First Name: " "Name: ")) (define second-vertical-pane-template '("Address: " "Email: ")) (define (make-button label parent callback) ;; make a new button with parent and callback (make-object button% label parent callback)) (define (fill-dialog person) ;; possible I would be better of with naming all ;; the text fields (let ((elements (send vp1 get-children))) (send (list-ref elements first-name-const) set-value (send a-person first-name)) (send (list-ref elements name-const) set-value (send a-person last-name)) (set! elements (send vp2 get-children)) (send (list-ref elements address-const) set-value (ivar a-person address)) (send (list-ref elements email-const) set-value (ivar a-person email)))) (define (make-button-pane parent) ;; two buttons to click through a list of addresses ;; one quit button for finishing the program (let ((button-pane (make-object vertical-pane% parent))) (let loop ((btn-list (button-list-template))) (cond ((null? btn-list) '()) (else (cons (send (make-button (list-ref (first btn-list) button-label-const) button-pane ;void) (list-ref (first btn-list) button-callback-const)) min-width initial-button-width) (loop (rest btn-list)))))) button-pane)) (define (make-vertical-pane parent elements) ;; a vertical pane just for displayint the contents of a list of ;; addresses (let ((vertical-pane (make-object vertical-pane% parent))) (let loop ((el elements)) (cond ((null? el) '()) (else (cons (make-single-text-field (first el) ; label for the field vertical-pane void ; callback here do nothing initial-text-field-width initial-text-field-height) ;initial size (loop (cdr el)))))) vertical-pane)) (define (make-single-text-field label parent callback req-width req-height) ;; make the single text field (let ((tf (make-object text-field% label parent callback "" '(single)))) ;; do not allow filling and/or resizing (send tf min-height req-height) (send tf stretchable-height #f) (send tf min-width req-width) ;(send tf stretchable-width #f) tf)) (define (make-dialog) (let* ((frame (make-object frame% dialog-title #f initial-frame-width initial-frame-height)) (hp (make-object horizontal-pane% frame))) ;; md 1 (set! vp1 (make-vertical-pane hp first-vertical-pane-template)) ; md 2 (set! vp2 (make-vertical-pane hp second-vertical-pane-template)) ; md 2 (let ((btn-pane (make-button-pane hp))) ; md 2 ;(send frame stretchable-width #f) (send frame stretchable-height #f) frame))) (define (main) (set! persons (list-of-persons address-file)) (set! number-of-persons (- (length persons) 1)) ;; counting for list-ref starts with 0 (set! top-frame (make-dialog)) (update-persons-data) (send top-frame show #t))