TextEdit.py 3.16 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2
# Text editing widget

3 4 5
# NB: this always assumes fixed bounds.
# For auto-growing TextEdit windows, different code would be needed.

Guido van Rossum's avatar
Guido van Rossum committed
6 7
from stdwinevents import *

Guido van Rossum's avatar
Guido van Rossum committed
8
class TextEdit:
Guido van Rossum's avatar
Guido van Rossum committed
9
	#
10
	def create(self, parent, (cols, rows)):
Guido van Rossum's avatar
Guido van Rossum committed
11 12 13 14 15 16
		parent.addchild(self)
		self.parent = parent
		self.cols = cols
		self.rows = rows
		self.text = ''
		# Creation of the editor is done in realize()
17 18 19 20
		self.editor = None
		self.dh = self.dv = 0
		return self
	#
21
	def createboxed(self, parent, (cols, rows), (dh, dv)):
22 23 24
		self = self.create(parent, (cols, rows))
		self.dh = max(0, dh)
		self.dv = max(0, dv)
Guido van Rossum's avatar
Guido van Rossum committed
25 26
		return self
	#
27 28 29
	def settext(self, text):
		self.editor.settext(text)
	#
30 31 32
	def gettext(self):
		return self.editor.gettext(text)
	#
Guido van Rossum's avatar
Guido van Rossum committed
33 34 35 36 37 38 39
	# Downcalls from parent to child
	#
	def destroy(self):
		del self.parent
		del self.editor
		del self.window
	#
40
	def getminsize(self, m, (width, height)):
41 42 43 44 45 46 47 48 49 50 51 52 53
		width = max(0, width - 2*self.dh)
		height = max(0, height - 2*self.dv)
		if width > 0 and self.editor:
			(left, top), (right, bottom) = self.editor.getrect()
			act_width, act_height = right - left, bottom - top
			if width >= act_width:
				width = width + 2*self.dh
				height = max(height, act_height) + 2*self.dv
				return width, height
		width = max(width, self.cols*m.textwidth('in')/2) + 2*self.dh
		height = max(height, self.rows*m.lineheight()) + 2*self.dv
		return width, height
	#
Guido van Rossum's avatar
Guido van Rossum committed
54 55 56
	def setbounds(self, bounds):
		self.bounds = bounds
		if self.editor:
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
			(left, top), (right, bottom) = bounds
			left = left + self.dh
			top = top + self.dv
			right = right - self.dh
			bottom = bottom - self.dv
			self.editor.move((left, top), (right, bottom))
			if self.dh and self.dv:
				(left, top), (right, bottom) = bounds
				left = left + 1
				top = top + 1
				right = right - 1
				bottom = bottom - 1
				bounds = (left, top), (right, bottom)
			self.editor.setview(bounds)
	#
	def getbounds(self):
		return self.bounds
	#
Guido van Rossum's avatar
Guido van Rossum committed
75 76
	def realize(self):
		self.window = self.parent.getwindow()
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
		(left, top), (right, bottom) = self.bounds
		left = left + self.dh
		top = top + self.dv
		right = right - self.dh
		bottom = bottom - self.dv
		self.editor = \
			self.window.textcreate((left, top), (right, bottom))
		self.editor.setactive(0)
		bounds = self.bounds
		if self.dh and self.dv:
			(left, top), (right, bottom) = bounds
			left = left + 1
			top = top + 1
			right = right - 1
			bottom = bottom - 1
			bounds = (left, top), (right, bottom)
		self.editor.setview(bounds)
94
		self.editor.settext(self.text)
Guido van Rossum's avatar
Guido van Rossum committed
95 96 97
		self.parent.need_mouse(self)
		self.parent.need_keybd(self)
		self.parent.need_altdraw(self)
98
	#
99
	def draw(self, d, area):
100 101 102
		if self.dh and self.dv:
			d.box(self.bounds)
	#
Guido van Rossum's avatar
Guido van Rossum committed
103 104 105 106 107 108 109
	def altdraw(self, area):
		self.editor.draw(area)
	#
	# Event downcalls
	#
	def mouse_down(self, detail):
		x = self.editor.event(WE_MOUSE_DOWN, self.window, detail)
110
	#
Guido van Rossum's avatar
Guido van Rossum committed
111 112
	def mouse_move(self, detail):
		x = self.editor.event(WE_MOUSE_MOVE, self.window, detail)
113
	#
Guido van Rossum's avatar
Guido van Rossum committed
114 115 116
	def mouse_up(self, detail):
		x = self.editor.event(WE_MOUSE_UP, self.window, detail)
	#
117
	def keybd(self, type, detail):
Guido van Rossum's avatar
Guido van Rossum committed
118 119
		x = self.editor.event(type, self.window, detail)
	#
120 121 122 123 124 125 126
	def activate(self):
		self.editor.setfocus(0, 30000)
		self.editor.setactive(1)
	#
	def deactivate(self):
		self.editor.setactive(0)
	#