Created
June 19, 2016 00:04
-
-
Save greggirwin/5b44de9fa016535d0d2f37bed485a133 to your computer and use it in GitHub Desktop.
Red Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [ | |
Title: "Calculator" | |
File: %calculator.r | |
Version: 0.0.1 | |
;Date: 18-Jun-2016 | |
Author: [ | |
REBOL version ["Jeff Kreis" "Allen Kamp" "Carl Sassenrath"] | |
Red port "Gregg Irwin" | |
] | |
Purpose: "Simple numeric calculator." | |
Tabs: 4 | |
Needs: View | |
] | |
auto-clear: true | |
calculate: does [ | |
if error? try [display/text: form do display/text][ | |
display/text: "Error" | |
display/color: red | |
] | |
auto-clear: true | |
] | |
clear-display: does [ | |
clear display/text | |
display/color: snow | |
auto-clear: false | |
] | |
key-in: func [value][ | |
if auto-clear [clear-display] | |
append display/text value | |
] | |
calculator: layout [ | |
backdrop water | |
space 4x4 | |
origin 10x10 | |
style k: button 40x26 font-size 12 [key-in face/text] | |
style kc: k [clear-display] | |
style k=: k [calculate] | |
text "WARNING! Int ops round right now" water return | |
;!! TBD figure out key event capture/bubble process, when to use done/stop, | |
; and how to prevent dupe events safely, or what the target widget's | |
; behavior is that we need to know about. | |
display: field "0" 172x26 bold snow right on-key [ | |
switch event/key [ | |
;!! Keys are always uppercase if ctrl? is true | |
#"=" #"^M" [calculate] ; = or Enter | |
#"c" #"C" [clear-display] | |
] | |
'done | |
] return | |
kc "C" k "(" k ")" k " / " return | |
k "7" k "8" k "9" k " * " return | |
k "4" k "5" k "6" k " - " return | |
k "1" k "2" k "3" k " + " return | |
k "0" k "-" k "." k= "=" return | |
] | |
system/view/capturing?: yes | |
view/options center-face calculator [ | |
actors: object [ | |
; on-detect: func [f e][ | |
; if e/type = 'key [ | |
; print ['detect mold e/key e/ctrl?] | |
; ] | |
; ] | |
on-key: func [f e][ | |
; print ['key mold e/key e/ctrl?] | |
if find "0123456789()." e/key [key-in e/key return 'done] | |
; Put spaces around operators | |
if find "+-*/" e/key [key-in form reduce ["" e/key ""] return 'done] | |
; It's a special key | |
switch e/key [ | |
;!! Keys are always uppercase if ctrl? is true | |
#"=" #"^M" [calculate] ; = or Enter | |
#"c" #"C" [clear-display] | |
#"q" #"Q" [if e/ctrl? [quit]] ; ctrl+q | |
#"^[" [quit] ; esc | |
] | |
] | |
] | |
] | |
system/view/capturing?: no |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment