blob: 9142d02e73fa91aaeb9c30605a25f9b3eabede8c [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
33 for i in range(len(windows)):
34 if windows[i] = win:
35 del windows[i]
36 break
37
38
39# Interfaces used by WindowSched.
40#
41def countwindows():
42 return len(windows)
43#
44def anywindow():
45 if windows:
46 return windows[0]
47 else:
48 return None
49
50
51# Event processing main loop.
52# Return when there are no windows left, or when an unhandled
53# exception occurs. (It is safe to restart the main loop after
54# an unsuccessful exit.)
55# Python's stdwin.getevent() turns WE_COMMAND/WC_CANCEL events
56# into KeyboardInterrupt exceptions; these are turned back in events.
57#
58def mainloop():
59 while windows:
60 try:
61 dispatch(stdwinq.getevent())
62 except KeyboardInterrupt:
63 dispatch(WE_COMMAND, stdwin.getactive(), WC_CANCEL)
64
65
66# Dispatch a single event.
67# Windows not in the windows list don't get their events:
68# events for such windows are silently ignored.
69#
70def dispatch(event):
71 if event[1] in windows:
72 event[1].dispatch(event)