-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtask-7-view.rkt
82 lines (62 loc) · 2.1 KB
/
task-7-view.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#lang racket/gui
(provide
WIDTH
HEIGHT
;; DC [Hashof Ref Number] -> Void
paint-grid
;; Natural Natural -> Ref
xy->A0
;; Natural -> LETTERS
x->A
;; Natural -> INDEX
y->0)
;; ---------------------------------------------------------------------------------------------------
(require 7GUI/task-7-exp)
(define HSIZE 100)
(define VSIZE 30)
(define X-OFFSET 2)
(define Y-OFFSET 10)
(define WIDTH (* (+ (string-length LETTERS) 1) HSIZE))
(define HEIGHT (* 101 VSIZE))
(define (A->x letter)
(for/first ((l (in-string LETTERS)) (i (in-naturals)) #:when (equal? l letter))
(+ (* (+ i 1) HSIZE) X-OFFSET)))
(define (0->y index)
(+ (* (+ index 1) VSIZE) Y-OFFSET))
(define ((finder range SIZE) x0)
(define x (- x0 SIZE))
(and (positive? x)
(for/first ((r range) (i (in-naturals)) #:when (<= (+ (* i SIZE)) x (+ (* (+ i 1) SIZE)))) r)))
(define (xy->A0 x y) (list (x->A x) (y->0 y)))
(define x->A (finder (in-string LETTERS) HSIZE))
(define y->0 (finder (in-range 100) VSIZE))
(define (paint-grid dc content)
(send dc clear)
(paint-hint dc)
(paint-axes dc)
(paint-cells dc content))
(define (paint-hint dc)
(let* ([current-font (send dc get-font)])
(send dc set-font small-font)
(send dc draw-text "click for content" X-OFFSET 2)
(send dc draw-text "double for formula" X-OFFSET 15)
(send dc set-font current-font)))
(define (paint-axes dc)
(send dc set-brush solid-gray)
(for ((letter (in-string LETTERS)) (i (in-naturals)))
(define x (* (+ i 1) HSIZE))
(send dc draw-rectangle x 0 HSIZE VSIZE)
(send dc draw-line x 0 x HEIGHT)
(send dc draw-text (string letter) (A->x letter) Y-OFFSET))
(for ((i (in-range 100)))
(define y (* (+ i 1) VSIZE))
(send dc draw-line 0 y WIDTH y)
(send dc draw-text (~a i) X-OFFSET (0->y i))))
(define (paint-cells dc content)
(for (((key value) (in-hash content)))
(match-define (list letter index) key)
(define x0 (A->x letter))
(define y0 (0->y index))
(send dc draw-text (~a value) x0 y0)))
(define small-font (make-object font% 12 'roman))
(define solid-gray (new brush% [color "lightgray"]))