textwin.py 2.41 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2 3 4 5 6
# Module 'textwin'

# Text windows, a subclass of gwin

import stdwin
import gwin
Guido van Rossum's avatar
Guido van Rossum committed
7
from stdwinevents import *
Guido van Rossum's avatar
Guido van Rossum committed
8 9 10 11 12 13 14 15 16 17 18 19


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:
20
		stdwin.setcutbuffer(0, s)
Guido van Rossum's avatar
Guido van Rossum committed
21 22 23 24 25 26
		w.text.replace('')
		fixsize(w)

def copy(w, m, id):
	s = w.text.getfocustext()
	if s:
27
		stdwin.setcutbuffer(0, s)
Guido van Rossum's avatar
Guido van Rossum committed
28 29 30
		fixeditmenu(w)

def paste(w, m, id):
31
	w.text.replace(stdwin.getcutbuffer(0))
Guido van Rossum's avatar
Guido van Rossum committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	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)
51
		m.enable(2, (stdwin.getcutbuffer(0) <> ''))
Guido van Rossum's avatar
Guido van Rossum committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

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
Guido van Rossum's avatar
Guido van Rossum committed
69
	void = w.text.event(WE_COMMAND, w, WC_BACKSPACE)
Guido van Rossum's avatar
Guido van Rossum committed
70 71 72 73 74 75 76
	fixsize(w)

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

def mdown(w, detail):			# Mouse down method
Guido van Rossum's avatar
Guido van Rossum committed
77
	void = w.text.event(WE_MOUSE_DOWN, w, detail)
Guido van Rossum's avatar
Guido van Rossum committed
78 79 80
	fixeditmenu(w)

def mmove(w, detail):			# Mouse move method
Guido van Rossum's avatar
Guido van Rossum committed
81
	void = w.text.event(WE_MOUSE_MOVE, w, detail)
Guido van Rossum's avatar
Guido van Rossum committed
82 83

def mup(w, detail):			# Mouse up method
Guido van Rossum's avatar
Guido van Rossum committed
84
	void = w.text.event(WE_MOUSE_UP, w, detail)
Guido van Rossum's avatar
Guido van Rossum committed
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
	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