cpython/Lib/lib-stdwin/gwin.py

122 lines
2.6 KiB
Python
Raw Normal View History

1990-10-13 19:23:40 +00:00
# Module 'gwin'
# Generic stdwin windows
# This is used as a base class from which to derive other window types.
# The mainloop() function here is an event dispatcher for all window types.
1991-12-26 13:04:16 +00:00
# XXX This is really obsoleted by "mainloop.py".
# XXX Also you should to it class-oriented...
1991-08-16 13:24:58 +00:00
import stdwin, stdwinq
from stdwinevents import *
1990-10-13 19:23:40 +00:00
windows = [] # List of open windows
# Open a window
def open(title): # Open a generic window
w = stdwin.open(title)
stdwin.setdefwinsize(0, 0)
# Set default event handlers
w.draw = nop
w.char = nop
w.mdown = nop
w.mmove = nop
w.mup = nop
w.m2down = m2down
w.m2up = m2up
w.size = nop
w.move = nop
w.activate = w.deactivate = nop
w.timer = nop
# default command handlers
w.close = close
w.tab = tab
w.enter = enter
w.backspace = backspace
w.arrow = arrow
w.kleft = w.kup = w.kright = w.kdown = nop
windows.append(w)
return w
# Generic event dispatching
def mainloop(): # Handle events until no windows left
while windows:
1991-08-16 13:24:58 +00:00
treatevent(stdwinq.getevent())
1990-10-13 19:23:40 +00:00
def treatevent(e): # Handle a stdwin event
type, w, detail = e
1992-01-01 19:35:13 +00:00
if type == WE_DRAW:
1990-10-13 19:23:40 +00:00
w.draw(w, detail)
1992-01-01 19:35:13 +00:00
elif type == WE_MENU:
1990-10-13 19:23:40 +00:00
m, item = detail
m.action[item](w, m, item)
1992-01-01 19:35:13 +00:00
elif type == WE_COMMAND:
1990-10-13 19:23:40 +00:00
treatcommand(w, detail)
1992-01-01 19:35:13 +00:00
elif type == WE_CHAR:
1990-10-13 19:23:40 +00:00
w.char(w, detail)
1992-01-01 19:35:13 +00:00
elif type == WE_MOUSE_DOWN:
1990-10-13 19:23:40 +00:00
if detail[1] > 1: w.m2down(w, detail)
else: w.mdown(w, detail)
1992-01-01 19:35:13 +00:00
elif type == WE_MOUSE_MOVE:
1990-10-13 19:23:40 +00:00
w.mmove(w, detail)
1992-01-01 19:35:13 +00:00
elif type == WE_MOUSE_UP:
1990-10-13 19:23:40 +00:00
if detail[1] > 1: w.m2up(w, detail)
else: w.mup(w, detail)
1992-01-01 19:35:13 +00:00
elif type == WE_SIZE:
1990-10-13 19:23:40 +00:00
w.size(w, w.getwinsize())
1992-01-01 19:35:13 +00:00
elif type == WE_ACTIVATE:
1990-10-13 19:23:40 +00:00
w.activate(w)
1992-01-01 19:35:13 +00:00
elif type == WE_DEACTIVATE:
1990-10-13 19:23:40 +00:00
w.deactivate(w)
1992-01-01 19:35:13 +00:00
elif type == WE_MOVE:
1990-10-13 19:23:40 +00:00
w.move(w)
1992-01-01 19:35:13 +00:00
elif type == WE_TIMER:
1990-10-13 19:23:40 +00:00
w.timer(w)
1992-01-01 19:35:13 +00:00
elif type == WE_CLOSE:
w.close(w)
1990-10-13 19:23:40 +00:00
def treatcommand(w, type): # Handle a we_command event
1992-01-01 19:35:13 +00:00
if type == WC_CLOSE:
1990-10-13 19:23:40 +00:00
w.close(w)
1992-01-01 19:35:13 +00:00
elif type == WC_RETURN:
1990-10-13 19:23:40 +00:00
w.enter(w)
1992-01-01 19:35:13 +00:00
elif type == WC_TAB:
1990-10-13 19:23:40 +00:00
w.tab(w)
1992-01-01 19:35:13 +00:00
elif type == WC_BACKSPACE:
1990-10-13 19:23:40 +00:00
w.backspace(w)
1991-12-26 13:04:16 +00:00
elif type in (WC_LEFT, WC_UP, WC_RIGHT, WC_DOWN):
1990-10-13 19:23:40 +00:00
w.arrow(w, type)
# Methods
def close(w): # Close method
for i in range(len(windows)):
if windows[i] is w:
del windows[i]
break
def arrow(w, detail): # Arrow key method
1992-01-01 19:35:13 +00:00
if detail == WC_LEFT:
1990-10-13 19:23:40 +00:00
w.kleft(w)
1992-01-01 19:35:13 +00:00
elif detail == WC_UP:
1990-10-13 19:23:40 +00:00
w.kup(w)
1992-01-01 19:35:13 +00:00
elif detail == WC_RIGHT:
1990-10-13 19:23:40 +00:00
w.kright(w)
1992-01-01 19:35:13 +00:00
elif detail == WC_DOWN:
1990-10-13 19:23:40 +00:00
w.kdown(w)
# Trivial methods
def tab(w): w.char(w, '\t')
def enter(w): w.char(w, '\n') # 'return' is a Python reserved word
def backspace(w): w.char(w, '\b')
def m2down(w, detail): w.mdown(w, detail)
def m2up(w, detail): w.mup(w, detail)
def nop(args): pass