blob: 0f7d6a960252fb0b0f7f4d08a5611933c880b35c [file] [log] [blame]
Guido van Rossum93a35f41992-12-14 14:12:10 +00001#! /usr/local/bin/python
Guido van Rossum9cf8f331992-03-30 10:54:51 +00002
3# 'clock' -- A simple alarm clock
4
5# The alarm can be set at 5 minute intervals on a 12 hour basis.
6# It is controlled with the mouse:
7# - Click and drag around the circle to set the alarm.
8# - Click far outside the circle to clear the alarm.
9# - Click near the center to set the alarm at the last time set.
10# The alarm time is indicated by a small triangle just outside the circle,
11# and also by a digital time at the bottom.
12# The indicators disappear when the alarm is not set.
13# When the alarm goes off, it beeps every minute for five minutes,
14# and the clock turns into inverse video.
15# Click or activate the window to turn the ringing off.
16
17import stdwin
18from stdwinevents import WE_MOUSE_DOWN, WE_MOUSE_MOVE, WE_MOUSE_UP, \
19 WE_TIMER, WE_DRAW, WE_SIZE, WE_CLOSE, WE_ACTIVATE
20import mainloop
21import time
22from math import sin, cos, atan2, pi, sqrt
23
24DEFWIDTH, DEFHEIGHT = 200, 200
25
26MOUSE_EVENTS = (WE_MOUSE_DOWN, WE_MOUSE_MOVE, WE_MOUSE_UP)
27ORIGIN = 0, 0
28FARAWAY = 2000, 2000
29EVERYWHERE = ORIGIN, FARAWAY
30
31# TZDIFF = 5*3600 # THINK C 3.0 returns UCT if local time is EST
32TZDIFF = 0 # THINK C 4.0 always returns local time
33
34
35def main():
36 win = makewindow()
37 del win
38 mainloop.mainloop()
39
40def makewindow():
41 stdwin.setdefwinsize(DEFWIDTH, DEFHEIGHT + stdwin.lineheight())
42 win = stdwin.open('clock')
43 setdimensions(win)
44 win.set = 1 # True when alarm is set
45 win.time = 11*60 + 40 # Time when alarm must go off
46 win.ring = 0 # True when alarm is ringing
47 win.dispatch = cdispatch
48 mainloop.register(win)
49 settimer(win)
50 return win
51
52def cdispatch(event):
53 type, win, detail = event
54 if type == WE_DRAW:
55 drawproc(win, detail)
56 elif type == WE_TIMER:
57 settimer(win)
58 drawproc(win, EVERYWHERE)
59 elif type in MOUSE_EVENTS:
60 mouseclick(win, type, detail)
61 elif type == WE_ACTIVATE:
62 if win.ring:
63 # Turn the ringing off
64 win.ring = 0
65 win.begindrawing().invert(win.mainarea)
66 elif type == WE_SIZE:
67 win.change(EVERYWHERE)
68 setdimensions(win)
69 elif type == WE_CLOSE:
70 mainloop.unregister(win)
71
72def setdimensions(win):
73 width, height = win.getwinsize()
74 height = height - stdwin.lineheight()
75 if width < height: size = width
76 else: size = height
77 halfwidth = width/2
78 halfheight = height/2
79 win.center = halfwidth, halfheight
80 win.radius = size*45/100
81 win.width = width
82 win.height = height
83 win.corner = width, height
84 win.mainarea = ORIGIN, win.corner
85 win.lineheight = stdwin.lineheight()
86 win.farcorner = width, height + win.lineheight
87 win.statusarea = (0, height), win.farcorner
88 win.fullarea = ORIGIN, win.farcorner
89
90def settimer(win):
91 now = getlocaltime()
92 win.times = calctime(now)
93 delay = 61 - now % 60
94 win.settimer(10 * delay)
95 minutes = (now/60) % 720
96 if win.ring:
97 # Is it time to stop the alarm ringing?
98 since = (minutes - win.time + 720) % 720
99 if since >= 5:
100 # Stop it now
101 win.ring = 0
102 else:
103 # Ring again, once every minute
104 stdwin.fleep()
105 elif win.set and minutes == win.time:
106 # Start the alarm ringing
107 win.ring = 1
108 stdwin.fleep()
109
110def drawproc(win, area):
111 hours, minutes, seconds = win.times
112 d = win.begindrawing()
113 d.cliprect(area)
114 d.erase(EVERYWHERE)
115 d.circle(win.center, win.radius)
116 d.line(win.center, calcpoint(win, hours*30 + minutes/2, 0.6))
117 d.line(win.center, calcpoint(win, minutes*6, 1.0))
118 str = dd(hours) + ':' + dd(minutes)
119 p = (win.width - d.textwidth(str))/2, win.height * 3 / 4
120 d.text(p, str)
121 if win.set:
122 drawalarm(win, d)
123 drawalarmtime(win, d)
124 if win.ring:
125 d.invert(win.mainarea)
126
127def mouseclick(win, type, detail):
128 d = win.begindrawing()
129 if win.ring:
130 # First turn the ringing off
131 win.ring = 0
132 d.invert(win.mainarea)
133 h, v = detail[0]
134 ch, cv = win.center
135 x, y = h-ch, cv-v
136 dist = sqrt(x*x + y*y) / float(win.radius)
137 if dist > 1.2:
138 if win.set:
139 drawalarm(win, d)
140 erasealarmtime(win, d)
141 win.set = 0
142 elif dist < 0.8:
143 if not win.set:
144 win.set = 1
145 drawalarm(win, d)
146 drawalarmtime(win, d)
147 else:
148 # Convert to half-degrees (range 0..720)
149 alpha = atan2(y, x)
150 hdeg = alpha*360.0/pi
151 hdeg = 180.0 - hdeg
152 hdeg = (hdeg + 720.0) % 720.0
153 atime = 5*int(hdeg/5.0 + 0.5)
154 if atime <> win.time or not win.set:
155 if win.set:
156 drawalarm(win, d)
157 erasealarmtime(win, d)
158 win.set = 1
159 win.time = atime
160 drawalarm(win, d)
161 drawalarmtime(win, d)
162
163def drawalarm(win, d):
164 p1 = calcpoint(win, float(win.time)/2.0, 1.02)
165 p2 = calcpoint(win, float(win.time)/2.0 - 4.0, 1.1)
166 p3 = calcpoint(win, float(win.time)/2.0 + 4.0, 1.1)
167 d.xorline(p1, p2)
168 d.xorline(p2, p3)
169 d.xorline(p3, p1)
170
171def erasealarmtime(win, d):
172 d.erase(win.statusarea)
173
174def drawalarmtime(win, d):
175 # win.time is in the range 0..720 with origin at 12 o'clock
176 # Convert to hours (0..12) and minutes (12*(0..60))
177 hh = win.time/60
178 mm = win.time%60
179 str = 'Alarm@' + dd(hh) + ':' + dd(mm)
180 p1 = (win.width - d.textwidth(str))/2, win.height
181 d.text(p1, str)
182
183def calcpoint(win, degrees, size):
184 alpha = pi/2.0 - float(degrees) * pi/180.0
185 x, y = cos(alpha), sin(alpha)
186 h, v = win.center
187 r = float(win.radius)
188 return h + int(x*size*r), v - int(y*size*r)
189
190def calctime(now):
191 seconds = now % 60
192 minutes = (now/60) % 60
193 hours = (now/3600) % 12
194 return hours, minutes, seconds
195
196def dd(n):
197 s = `n`
198 return '0'*(2-len(s)) + s
199
200def getlocaltime():
201 return time.time() - TZDIFF
202
203main()