summaryrefslogtreecommitdiffstats
path: root/Lib/stdwin/textwin.py
blob: 778717fe14f07f9bcbbee2e367056b7e5b34b9cc (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
# Module 'textwin'

# Text windows, a subclass of gwin

import stdwin
import gwin
from stdwinevents import *


def fixsize(w):
	docwidth, docheight = w.text.getrect()[1]
	winheight = w.getwinsize()[1]
	if winheight > docheight: docheight = winheight
	w.setdocsize(0, docheight)
	fixeditmenu(w)

def cut(w, m, id):
	s = w.text.getfocustext()
	if s:
		stdwin.setcutbuffer(0, s)
		w.text.replace('')
		fixsize(w)

def copy(w, m, id):
	s = w.text.getfocustext()
	if s:
		stdwin.setcutbuffer(0, s)
		fixeditmenu(w)

def paste(w, m, id):
	w.text.replace(stdwin.getcutbuffer(0))
	fixsize(w)

def addeditmenu(w):
	m = w.editmenu = w.menucreate('Edit')
	m.action = []
	m.additem('Cut', 'X')
	m.action.append(cut)
	m.additem('Copy', 'C')
	m.action.append(copy)
	m.additem('Paste', 'V')
	m.action.append(paste)

def fixeditmenu(w):
	m = w.editmenu
	f = w.text.getfocus()
	can_copy = (f[0] < f[1])
	m.enable(1, can_copy)
	if not w.readonly:
		m.enable(0, can_copy)
		m.enable(2, (stdwin.getcutbuffer(0) <> ''))

def draw(w, area):			# Draw method
	w.text.draw(area)

def size(w, newsize):			# Size method
	w.text.move((0, 0), newsize)
	fixsize(w)

def close(w):				# Close method
	del w.text  # Break circular ref
	gwin.close(w)

def char(w, c):				# Char method
	w.text.replace(c)
	fixsize(w)

def backspace(w):			# Backspace method
	void = w.text.event(WE_COMMAND, w, WC_BACKSPACE)
	fixsize(w)

def arrow(w, detail):			# Arrow method
	w.text.arrow(detail)
	fixeditmenu(w)

def mdown(w, detail):			# Mouse down method
	void = w.text.event(WE_MOUSE_DOWN, w, detail)
	fixeditmenu(w)

def mmove(w, detail):			# Mouse move method
	void = w.text.event(WE_MOUSE_MOVE, w, detail)

def mup(w, detail):			# Mouse up method
	void = w.text.event(WE_MOUSE_UP, w, detail)
	fixeditmenu(w)

def activate(w):			# Activate method
	fixeditmenu(w)

def open(title, str):			# Display a string in a window
	w = gwin.open(title)
	w.readonly = 0
	w.text = w.textcreate((0, 0), w.getwinsize())
	w.text.replace(str)
	w.text.setfocus(0, 0)
	addeditmenu(w)
	fixsize(w)
	w.draw = draw
	w.size = size
	w.close = close
	w.mdown = mdown
	w.mmove = mmove
	w.mup = mup
	w.char = char
	w.backspace = backspace
	w.arrow = arrow
	w.activate = activate
	return w

def open_readonly(title, str):		# Same with char input disabled
	w = open(title, str)
	w.readonly = 1
	w.char = w.backspace = gwin.nop
	# Disable Cut and Paste menu item; leave Copy alone
	w.editmenu.enable(0, 0)
	w.editmenu.enable(2, 0)
	return w