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