blob: 9dac882317aa2868afd5e3c2cca899b85c056738 [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
Guido van Rossum0b744801991-08-16 13:24:58 +00007import stdwin, stdwinq
Guido van Rossuma4ddbd81990-12-26 15:37:05 +00008from stdwinevents import *
Guido van Rossumc6360141990-10-13 19:23:40 +00009
Guido van Rossuma4ddbd81990-12-26 15:37:05 +000010# XXX Old version of stdwinevents, should go
11import stdwinsupport
Guido van Rossumc6360141990-10-13 19:23:40 +000012S = stdwinsupport # Shorthand
13
14windows = [] # List of open windows
15
16
17# Open a window
18
19def 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
47def mainloop(): # Handle events until no windows left
48 while windows:
Guido van Rossum0b744801991-08-16 13:24:58 +000049 treatevent(stdwinq.getevent())
Guido van Rossumc6360141990-10-13 19:23:40 +000050
51def 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 Rossuma4ddbd81990-12-26 15:37:05 +000080 elif type = WE_CLOSE:
81 w.close(w)
Guido van Rossumc6360141990-10-13 19:23:40 +000082
83def 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
98def close(w): # Close method
99 for i in range(len(windows)):
100 if windows[i] is w:
101 del windows[i]
102 break
103
104def 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
117def tab(w): w.char(w, '\t')
118def enter(w): w.char(w, '\n') # 'return' is a Python reserved word
119def backspace(w): w.char(w, '\b')
120def m2down(w, detail): w.mdown(w, detail)
121def m2up(w, detail): w.mup(w, detail)
122def nop(args): pass