Guido van Rossum | 7045dd0 | 1991-08-16 13:28:28 +0000 | [diff] [blame] | 1 | # 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 | |
| 7 | import stdwin, stdwinq |
| 8 | from stdwinevents import * |
| 9 | |
| 10 | |
| 11 | # List of windows known to the main loop. |
| 12 | # |
| 13 | windows = [] |
| 14 | |
| 15 | |
| 16 | # Function to register a window. |
| 17 | # |
| 18 | def 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 | # |
| 30 | def unregister(win): |
| 31 | if win in windows: |
| 32 | windows.remove(win) # Not in 0.9.1 |
Guido van Rossum | 3f4f917 | 1991-11-19 20:41:07 +0000 | [diff] [blame] | 33 | # 0.9.1 solution: |
| 34 | #for i in range(len(windows)): |
| 35 | # if windows[i] = win: |
| 36 | # del windows[i] |
| 37 | # break |
Guido van Rossum | 7045dd0 | 1991-08-16 13:28:28 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | # Interfaces used by WindowSched. |
| 41 | # |
| 42 | def countwindows(): |
| 43 | return len(windows) |
| 44 | # |
| 45 | def 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 | # |
| 59 | def 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 | # |
| 71 | def dispatch(event): |
| 72 | if event[1] in windows: |
| 73 | event[1].dispatch(event) |