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 | |
| 7 | import stdwin |
Guido van Rossum | a4ddbd8 | 1990-12-26 15:37:05 +0000 | [diff] [blame] | 8 | from stdwinevents import * |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | a4ddbd8 | 1990-12-26 15:37:05 +0000 | [diff] [blame] | 10 | # XXX Old version of stdwinevents, should go |
| 11 | import stdwinsupport |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 12 | S = stdwinsupport # Shorthand |
| 13 | |
| 14 | windows = [] # List of open windows |
| 15 | |
| 16 | |
| 17 | # Open a window |
| 18 | |
| 19 | def open(title): # Open a generic window |
| 20 | w = stdwin.open(title) |
| 21 | stdwin.setdefwinsize(0, 0) |
| 22 | # Set default event handlers |
| 23 | w.draw = nop |
| 24 | w.char = nop |
| 25 | w.mdown = nop |
| 26 | w.mmove = nop |
| 27 | w.mup = nop |
| 28 | w.m2down = m2down |
| 29 | w.m2up = m2up |
| 30 | w.size = nop |
| 31 | w.move = nop |
| 32 | w.activate = w.deactivate = nop |
| 33 | w.timer = nop |
| 34 | # default command handlers |
| 35 | w.close = close |
| 36 | w.tab = tab |
| 37 | w.enter = enter |
| 38 | w.backspace = backspace |
| 39 | w.arrow = arrow |
| 40 | w.kleft = w.kup = w.kright = w.kdown = nop |
| 41 | windows.append(w) |
| 42 | return w |
| 43 | |
| 44 | |
| 45 | # Generic event dispatching |
| 46 | |
| 47 | def mainloop(): # Handle events until no windows left |
| 48 | while windows: |
| 49 | treatevent(stdwin.getevent()) |
| 50 | |
| 51 | def treatevent(e): # Handle a stdwin event |
| 52 | type, w, detail = e |
| 53 | if type = S.we_draw: |
| 54 | w.draw(w, detail) |
| 55 | elif type = S.we_menu: |
| 56 | m, item = detail |
| 57 | m.action[item](w, m, item) |
| 58 | elif type = S.we_command: |
| 59 | treatcommand(w, detail) |
| 60 | elif type = S.we_char: |
| 61 | w.char(w, detail) |
| 62 | elif type = S.we_mouse_down: |
| 63 | if detail[1] > 1: w.m2down(w, detail) |
| 64 | else: w.mdown(w, detail) |
| 65 | elif type = S.we_mouse_move: |
| 66 | w.mmove(w, detail) |
| 67 | elif type = S.we_mouse_up: |
| 68 | if detail[1] > 1: w.m2up(w, detail) |
| 69 | else: w.mup(w, detail) |
| 70 | elif type = S.we_size: |
| 71 | w.size(w, w.getwinsize()) |
| 72 | elif type = S.we_activate: |
| 73 | w.activate(w) |
| 74 | elif type = S.we_deactivate: |
| 75 | w.deactivate(w) |
| 76 | elif type = S.we_move: |
| 77 | w.move(w) |
| 78 | elif type = S.we_timer: |
| 79 | w.timer(w) |
Guido van Rossum | a4ddbd8 | 1990-12-26 15:37:05 +0000 | [diff] [blame] | 80 | elif type = WE_CLOSE: |
| 81 | w.close(w) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 82 | |
| 83 | def treatcommand(w, type): # Handle a we_command event |
| 84 | if type = S.wc_close: |
| 85 | w.close(w) |
| 86 | elif type = S.wc_return: |
| 87 | w.enter(w) |
| 88 | elif type = S.wc_tab: |
| 89 | w.tab(w) |
| 90 | elif type = S.wc_backspace: |
| 91 | w.backspace(w) |
| 92 | elif type in (S.wc_left, S.wc_up, S.wc_right, S.wc_down): |
| 93 | w.arrow(w, type) |
| 94 | |
| 95 | |
| 96 | # Methods |
| 97 | |
| 98 | def close(w): # Close method |
| 99 | for i in range(len(windows)): |
| 100 | if windows[i] is w: |
| 101 | del windows[i] |
| 102 | break |
| 103 | |
| 104 | def arrow(w, detail): # Arrow key method |
| 105 | if detail = S.wc_left: |
| 106 | w.kleft(w) |
| 107 | elif detail = S.wc_up: |
| 108 | w.kup(w) |
| 109 | elif detail = S.wc_right: |
| 110 | w.kright(w) |
| 111 | elif detail = S.wc_down: |
| 112 | w.kdown(w) |
| 113 | |
| 114 | |
| 115 | # Trivial methods |
| 116 | |
| 117 | def tab(w): w.char(w, '\t') |
| 118 | def enter(w): w.char(w, '\n') # 'return' is a Python reserved word |
| 119 | def backspace(w): w.char(w, '\b') |
| 120 | def m2down(w, detail): w.mdown(w, detail) |
| 121 | def m2up(w, detail): w.mup(w, detail) |
| 122 | def nop(args): pass |