blob: 0cf5bde6611c2f903e9ad34f8ec7277d1a394eda [file] [log] [blame]
Guido van Rossum7045dd01991-08-16 13:28:28 +00001# Standard main loop for *all* STDWIN applications.
2# This requires that applications:
3# - register their windows on creation and unregister them when closed
4# - have a 'dispatch' function as a window member
5
6
7import stdwin, stdwinq
8from stdwinevents import *
9
10
11# List of windows known to the main loop.
12#
13windows = []
14
15
16# Function to register a window.
17#
18def register(win):
19 # First test the dispatch function by passing it a null event --
20 # this catches registration of unconforming windows.
21 win.dispatch(WE_NULL, win, None)
22 if win not in windows:
23 windows.append(win)
24
25
26# Function to unregister a window.
27# It is not an error to unregister an already unregistered window
28# (this is useful for cleanup actions).
29#
30def unregister(win):
31 if win in windows:
32 windows.remove(win) # Not in 0.9.1
Guido van Rossum3f4f9171991-11-19 20:41:07 +000033 # 0.9.1 solution:
34 #for i in range(len(windows)):
35 # if windows[i] = win:
36 # del windows[i]
37 # break
Guido van Rossum7045dd01991-08-16 13:28:28 +000038
39
40# Interfaces used by WindowSched.
41#
42def countwindows():
43 return len(windows)
44#
45def anywindow():
46 if windows:
47 return windows[0]
48 else:
49 return None
50
51
52# Event processing main loop.
53# Return when there are no windows left, or when an unhandled
54# exception occurs. (It is safe to restart the main loop after
55# an unsuccessful exit.)
56# Python's stdwin.getevent() turns WE_COMMAND/WC_CANCEL events
57# into KeyboardInterrupt exceptions; these are turned back in events.
58#
59def mainloop():
60 while windows:
61 try:
62 dispatch(stdwinq.getevent())
63 except KeyboardInterrupt:
64 dispatch(WE_COMMAND, stdwin.getactive(), WC_CANCEL)
65
66
67# Dispatch a single event.
68# Windows not in the windows list don't get their events:
69# events for such windows are silently ignored.
70#
71def dispatch(event):
72 if event[1] in windows:
73 event[1].dispatch(event)