summaryrefslogtreecommitdiffstats
path: root/Lib/stdwin/gwin.py
blob: 12ed90bba010edb3934c1b1a647dcbb9f5d04bd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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.

# XXX This is really obsoleted by "mainloop.py".
# XXX Also you should to it class-oriented...

import stdwin, stdwinq
from stdwinevents import *

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:
		treatevent(stdwinq.getevent())

def treatevent(e):			# Handle a stdwin event
	type, w, detail = e
	if type == WE_DRAW:
		w.draw(w, detail)
	elif type == WE_MENU:
		m, item = detail
		m.action[item](w, m, item)
	elif type == WE_COMMAND:
		treatcommand(w, detail)
	elif type == WE_CHAR:
		w.char(w, detail)
	elif type == WE_MOUSE_DOWN:
		if detail[1] > 1: w.m2down(w, detail)
		else: w.mdown(w, detail)
	elif type == WE_MOUSE_MOVE:
		w.mmove(w, detail)
	elif type == WE_MOUSE_UP:
		if detail[1] > 1: w.m2up(w, detail)
		else: w.mup(w, detail)
	elif type == WE_SIZE:
		w.size(w, w.getwinsize())
	elif type == WE_ACTIVATE:
		w.activate(w)
	elif type == WE_DEACTIVATE:
		w.deactivate(w)
	elif type == WE_MOVE:
		w.move(w)
	elif type == WE_TIMER:
		w.timer(w)
	elif type == WE_CLOSE:
		w.close(w)

def treatcommand(w, type):		# Handle a we_command event
	if type == WC_CLOSE:
		w.close(w)
	elif type == WC_RETURN:
		w.enter(w)
	elif type == WC_TAB:
		w.tab(w)
	elif type == WC_BACKSPACE:
		w.backspace(w)
	elif type in (WC_LEFT, WC_UP, WC_RIGHT, WC_DOWN):
		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
	if detail == WC_LEFT:
		w.kleft(w)
	elif detail == WC_UP:
		w.kup(w)
	elif detail == WC_RIGHT:
		w.kright(w)
	elif detail == WC_DOWN:
		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