Guido van Rossum | b950629 | 1992-04-13 18:38:20 +0000 | [diff] [blame^] | 1 | import cd |
| 2 | import stdwin |
| 3 | from stdwinevents import * |
| 4 | import mainloop |
| 5 | |
| 6 | def main(): |
| 7 | player = cd.open() |
| 8 | stdwin.setdefscrollbars(0, 0) |
| 9 | win = stdwin.open('CD') |
| 10 | win.player = player |
| 11 | win.dispatch = cddispatch |
| 12 | mainloop.register(win) |
| 13 | win.settimer(10) |
| 14 | mainloop.mainloop() |
| 15 | |
| 16 | def cddispatch(type, win, detail): |
| 17 | if type == WE_NULL: |
| 18 | pass |
| 19 | elif type == WE_CLOSE: |
| 20 | mainloop.unregister(win) |
| 21 | win.close() |
| 22 | elif type == WE_DRAW: |
| 23 | draw(win) |
| 24 | elif type == WE_TIMER: |
| 25 | update(win) |
| 26 | elif type == WE_MOUSE_UP: |
| 27 | left, top, right, bottom, v1, v2 = getgeo(win) |
| 28 | h, v = detail[0] |
| 29 | if left < h < right: |
| 30 | if top < v < v1: |
| 31 | but1(win) |
| 32 | elif v1 < v < v2: |
| 33 | but2(win) |
| 34 | elif v2 < v < bottom: |
| 35 | but3(win) |
| 36 | else: |
| 37 | stdwin.fleep() |
| 38 | |
| 39 | def but1(win): |
| 40 | update(win) |
| 41 | |
| 42 | def but2(win): |
| 43 | win.player.togglepause() |
| 44 | update(win) |
| 45 | |
| 46 | def but3(win): |
| 47 | win.player.stop() |
| 48 | update(win) |
| 49 | |
| 50 | def update(win): |
| 51 | d = win.begindrawing() |
| 52 | drawstatus(win, d) |
| 53 | d.enddrawing() |
| 54 | win.settimer(10) |
| 55 | |
| 56 | statedict = ['ERROR', 'NODISK', 'READY', 'PLAYING', 'PAUSED', 'STILL'] |
| 57 | |
| 58 | def draw(win): |
| 59 | left, top, right, bottom, v1, v2 = getgeo(win) |
| 60 | d = win.begindrawing() |
| 61 | drawstatus(win, d) |
| 62 | box(d, left, v1, right, v2, 'Play/Pause') |
| 63 | box(d, left, v2, right, bottom, 'Stop') |
| 64 | d.enddrawing() |
| 65 | |
| 66 | def drawstatus(win, d): |
| 67 | left, top, right, bottom, v1, v2 = getgeo(win) |
| 68 | status = win.player.getstatus() |
| 69 | state = status[0] |
| 70 | if 0 <= state < len(statedict): |
| 71 | message = statedict[state] |
| 72 | else: |
| 73 | message = `status` |
| 74 | message = message + ' track ' + `status[1]` + ' of ' + `status[12]` |
| 75 | d.erase((left, top), (right, v1)) |
| 76 | box(d, left, top, right, v1, message) |
| 77 | |
| 78 | def box(d, left, top, right, bottom, label): |
| 79 | R = (left+1, top+1), (right-1, bottom-1) |
| 80 | width = d.textwidth(label) |
| 81 | height = d.lineheight() |
| 82 | h = (left + right - width) / 2 |
| 83 | v = (top + bottom - height) / 2 |
| 84 | d.box(R) |
| 85 | d.cliprect(R) |
| 86 | d.text((h, v), label) |
| 87 | d.noclip() |
| 88 | |
| 89 | def getgeo(win): |
| 90 | (left, top), (right, bottom) = (0, 0), win.getwinsize() |
| 91 | v1 = top + (bottom - top) / 3 |
| 92 | v2 = top + (bottom - top) * 2 / 3 |
| 93 | return left, top, right, bottom, v1, v2 |