-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculon-gui.rkt
166 lines (137 loc) · 4.24 KB
/
calculon-gui.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#lang racket/gui
(require "calculon.rkt")
;; A simple calculator
;; TODO
;; - Unit testing (at least the callbacks)
;; - Define a button with min-width 30 and use it for the buttons
;; - Allow expressions with parenthesis
;; GUI-related functions
;; Push a button of an operation (+, -, ...)
(define (push-operation op)
(send display$ set-value (string-append (send display$ get-value) op)))
;; Push a button which is not an op (digit, decimal value)
(define (push-number button-value)
(define current-display (send display$ get-value))
(define result
(match current-display
[(regexp #rx"^0") button-value]
[_ (string-append current-display button-value)]))
(send display$ set-value result))
(define frame (new frame% [label "Calculon"]))
(define display$ (new text-field%
(label "")
(parent frame)
(init-value "0")))
;; Row 0: ? ? sqrt C
(define row0 (new horizontal-panel% [parent frame]))
(new button% [parent row0]
[label ""]
[min-width 30]
[enabled #f])
(new button% [parent row0]
[label ""]
[min-width 30]
[enabled #f])
(new button% [parent row0]
[label ""]
[min-width 30]
[enabled #f])
(new button% [parent row0]
[label "R"]
[min-width 30]
[callback (λ (b e)
(send display$ set-value "0"))])
;; Row 1: 9 8 7 +
(define row1 (new horizontal-panel% [parent frame]))
;; ASK: I want a button whose min-width is always 30. I'm afraid
;; I don't know how to use inheritance (yet).
(new button% [parent row1]
[label "9"]
[min-width 30]
[callback (λ (button event)
(push-number 9))])
(new button% [parent row1]
[label "8"]
[min-width 30]
[callback (λ (button event)
(push-number button))])
(new button% [parent row1]
[label "7"]
[min-width 30]
[callback (λ (button event)
(push-number button))])
(new button% [parent row1]
[label "+"]
[min-width 30]
[callback (λ (button event)
(push-operation "+"))])
;; Row 2: 6 5 4 -
(define row2 (new horizontal-panel% [parent frame]))
(new button% [parent row2]
[label "6"]
[min-width 30]
[callback (λ (button event)
(push-number button))])
(new button% [parent row2]
[label "5"]
[min-width 30]
[callback (λ (button event)
(push-number button))])
(new button% [parent row2]
[label "4"]
[min-width 30]
[callback (λ (button event)
(push-number button))])
(new button% [parent row2]
[label "-"]
[min-width 30]
[callback (λ (button event)
(push-operation "-"))])
;; Row 3: 3 2 1 *
(define row3 (new horizontal-panel% [parent frame]))
(new button% [parent row3]
[label "3"]
[min-width 30]
[callback (λ (button event)
(push-number button))])
(new button% [parent row3]
[label "2"]
[min-width 30]
[callback (λ (button event)
(push-number button))])
(new button% [parent row3]
[label "1"]
[min-width 30]
[callback (λ (button event)
(push-number button))])
(new button% [parent row3]
[label "×"]
[min-width 30]
[callback (λ (button event)
(push-operation "*"))])
;; Row 4: 0 . = /
(define row4 (new horizontal-panel% [parent frame]))
(new button% [parent row4]
[label "0"]
[min-width 30]
[callback (λ (button event)
(push-number button))])
(new button% [parent row4]
[label "."]
[min-width 30]
[callback (λ (button event)
(push-number button))]) #| find a better name for the function;
also do something with the decimal point
(again) |#
(new button% [parent row4]
[label "="]
[min-width 30]
[callback (λ (b e) ; ASK: do we still need the args if unused?
(define expr (send display$ get-value))
(send display$ set-value (push-equal expr)))])
(new button% [parent row4]
[label "÷"]
[min-width 30]
[callback (λ (button event)
(push-operation "/"))])
(send frame show #t)