Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 1 | # Module 'gwin' |
| 2 | # Generic stdwin windows |
| 3 | |
| 4 | # This is used as a base class from which to derive other window types. |
| 5 | # The mainloop() function here is an event dispatcher for all window types. |
| 6 | |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 7 | # XXX This is really obsoleted by "mainloop.py". |
| 8 | # XXX Also you should to it class-oriented... |
| 9 | |
Guido van Rossum | 0b74480 | 1991-08-16 13:24:58 +0000 | [diff] [blame] | 10 | import stdwin, stdwinq |
Guido van Rossum | a4ddbd8 | 1990-12-26 15:37:05 +0000 | [diff] [blame] | 11 | from stdwinevents import * |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 13 | windows = [] # List of open windows |
| 14 | |
| 15 | |
| 16 | # Open a window |
| 17 | |
| 18 | def open(title): # Open a generic window |
| 19 | w = stdwin.open(title) |
| 20 | stdwin.setdefwinsize(0, 0) |
| 21 | # Set default event handlers |
| 22 | w.draw = nop |
| 23 | w.char = nop |
| 24 | w.mdown = nop |
| 25 | w.mmove = nop |
| 26 | w.mup = nop |
| 27 | w.m2down = m2down |
| 28 | w.m2up = m2up |
| 29 | w.size = nop |
| 30 | w.move = nop |
| 31 | w.activate = w.deactivate = nop |
| 32 | w.timer = nop |
| 33 | # default command handlers |
| 34 | w.close = close |
| 35 | w.tab = tab |
| 36 | w.enter = enter |
| 37 | w.backspace = backspace |
| 38 | w.arrow = arrow |
| 39 | w.kleft = w.kup = w.kright = w.kdown = nop |
| 40 | windows.append(w) |
| 41 | return w |
| 42 | |
| 43 | |
| 44 | # Generic event dispatching |
| 45 | |
| 46 | def mainloop(): # Handle events until no windows left |
| 47 | while windows: |
Guido van Rossum | 0b74480 | 1991-08-16 13:24:58 +0000 | [diff] [blame] | 48 | treatevent(stdwinq.getevent()) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 49 | |
| 50 | def treatevent(e): # Handle a stdwin event |
| 51 | type, w, detail = e |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 52 | if type = WE_DRAW: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 53 | w.draw(w, detail) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 54 | elif type = WE_MENU: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 55 | m, item = detail |
| 56 | m.action[item](w, m, item) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 57 | elif type = WE_COMMAND: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 58 | treatcommand(w, detail) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 59 | elif type = WE_CHAR: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 60 | w.char(w, detail) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 61 | elif type = WE_MOUSE_DOWN: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 62 | if detail[1] > 1: w.m2down(w, detail) |
| 63 | else: w.mdown(w, detail) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 64 | elif type = WE_MOUSE_MOVE: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 65 | w.mmove(w, detail) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 66 | elif type = WE_MOUSE_UP: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 67 | if detail[1] > 1: w.m2up(w, detail) |
| 68 | else: w.mup(w, detail) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 69 | elif type = WE_SIZE: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 70 | w.size(w, w.getwinsize()) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 71 | elif type = WE_ACTIVATE: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 72 | w.activate(w) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 73 | elif type = WE_DEACTIVATE: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 74 | w.deactivate(w) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 75 | elif type = WE_MOVE: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 76 | w.move(w) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 77 | elif type = WE_TIMER: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 78 | w.timer(w) |
Guido van Rossum | a4ddbd8 | 1990-12-26 15:37:05 +0000 | [diff] [blame] | 79 | elif type = WE_CLOSE: |
| 80 | w.close(w) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 81 | |
| 82 | def treatcommand(w, type): # Handle a we_command event |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 83 | if type = WC_CLOSE: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 84 | w.close(w) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 85 | elif type = WC_RETURN: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 86 | w.enter(w) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 87 | elif type = WC_TAB: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 88 | w.tab(w) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 89 | elif type = WC_BACKSPACE: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 90 | w.backspace(w) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 91 | elif type in (WC_LEFT, WC_UP, WC_RIGHT, WC_DOWN): |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 92 | w.arrow(w, type) |
| 93 | |
| 94 | |
| 95 | # Methods |
| 96 | |
| 97 | def close(w): # Close method |
| 98 | for i in range(len(windows)): |
| 99 | if windows[i] is w: |
| 100 | del windows[i] |
| 101 | break |
| 102 | |
| 103 | def arrow(w, detail): # Arrow key method |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 104 | if detail = WC_LEFT: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 105 | w.kleft(w) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 106 | elif detail = WC_UP: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 107 | w.kup(w) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 108 | elif detail = WC_RIGHT: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 109 | w.kright(w) |
Guido van Rossum | 61f9d0d | 1991-12-26 13:04:16 +0000 | [diff] [blame] | 110 | elif detail = WC_DOWN: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 111 | w.kdown(w) |
| 112 | |
| 113 | |
| 114 | # Trivial methods |
| 115 | |
| 116 | def tab(w): w.char(w, '\t') |
| 117 | def enter(w): w.char(w, '\n') # 'return' is a Python reserved word |
| 118 | def backspace(w): w.char(w, '\b') |
| 119 | def m2down(w, detail): w.mdown(w, detail) |
| 120 | def m2up(w, detail): w.mup(w, detail) |
| 121 | def nop(args): pass |