| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | # srcwin.py -- a source listing window | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import stdwin | 
					
						
							|  |  |  | from stdwinevents import * | 
					
						
							|  |  |  | import basewin | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WIDTH = 40 | 
					
						
							|  |  |  | MAXHEIGHT = 24 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TextWindow(basewin.BaseWindow): | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1993-12-17 15:25:27 +00:00
										 |  |  | 	def __init__(self, title, contents): | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | 		self.contents = contents | 
					
						
							|  |  |  | 		self.linecount = countlines(self.contents) | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 		# | 
					
						
							|  |  |  | 		self.lineheight = lh = stdwin.lineheight() | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | 		self.leftmargin = self.getmargin() | 
					
						
							|  |  |  | 		self.top = 0 | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 		self.rightmargin = 30000 # Infinity | 
					
						
							|  |  |  | 		self.bottom = lh * self.linecount | 
					
						
							|  |  |  | 		# | 
					
						
							|  |  |  | 		width = WIDTH*stdwin.textwidth('0') | 
					
						
							|  |  |  | 		height = lh*min(MAXHEIGHT, self.linecount) | 
					
						
							|  |  |  | 		stdwin.setdefwinsize(width, height) | 
					
						
							| 
									
										
										
										
											1993-12-17 15:25:27 +00:00
										 |  |  | 		basewin.BaseWindow.__init__(self, title) | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 		# | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | 		self.win.setdocsize(0, self.bottom) | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 		self.initeditor() | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	def initeditor(self): | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | 		r = (self.leftmargin, self.top), (self.rightmargin, self.bottom) | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 		self.editor = self.win.textcreate(r) | 
					
						
							|  |  |  | 		self.editor.settext(self.contents) | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	def closeeditor(self): | 
					
						
							|  |  |  | 		self.editor.close() | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | #	def reopen(self): | 
					
						
							|  |  |  | #		self.closeeditor() | 
					
						
							|  |  |  | #		basewin.BaseWindow.reopen(self) | 
					
						
							|  |  |  | #		self.initeditor() | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | 	# Override the following two methods to format line numbers differently | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	def getmark(self, lineno): | 
					
						
							|  |  |  | 		return `lineno` | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | 	def getmargin(self): | 
					
						
							|  |  |  | 		return stdwin.textwidth(`self.linecount + 1` + ' ') | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	# Event dispatcher, called from mainloop.mainloop() | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 	def dispatch(self, event): | 
					
						
							|  |  |  | 		if event[0] == WE_NULL: return # Dummy tested by mainloop | 
					
						
							|  |  |  | 		if event[0] == WE_DRAW or not self.editor.event(event): | 
					
						
							|  |  |  | 			basewin.BaseWindow.dispatch(self, event) | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | 	# Event handlers | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	def close(self): | 
					
						
							|  |  |  | 		self.closeeditor() | 
					
						
							|  |  |  | 		basewin.BaseWindow.close(self) | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 	def draw(self, detail): | 
					
						
							|  |  |  | 		dummy = self.editor.draw(detail) | 
					
						
							|  |  |  | 		# Draw line numbers | 
					
						
							|  |  |  | 		(left, top), (right, bottom) = detail | 
					
						
							|  |  |  | 		topline = top/self.lineheight | 
					
						
							|  |  |  | 		botline = bottom/self.lineheight + 1 | 
					
						
							|  |  |  | 		botline = min(self.linecount, botline) | 
					
						
							|  |  |  | 		d = self.win.begindrawing() | 
					
						
							|  |  |  | 		try: | 
					
						
							|  |  |  | 			h, v = 0, self.lineheight * topline | 
					
						
							|  |  |  | 			for lineno in range(topline+1, botline+1): | 
					
						
							|  |  |  | 				d.text((h, v), self.getmark(lineno)) | 
					
						
							|  |  |  | 				v = v + self.lineheight | 
					
						
							|  |  |  | 		finally: | 
					
						
							|  |  |  | 			d.close() | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | 	# Calls from outside | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	def changemark(self, lineno): # redraw the mark for a line | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 		left = 0 | 
					
						
							|  |  |  | 		top = (lineno-1) * self.lineheight | 
					
						
							|  |  |  | 		right = self.leftmargin | 
					
						
							|  |  |  | 		bottom = lineno * self.lineheight | 
					
						
							|  |  |  | 		d = self.win.begindrawing() | 
					
						
							|  |  |  | 		try: | 
					
						
							|  |  |  | 			d.erase((left, top), (right, bottom)) | 
					
						
							|  |  |  | 			d.text((left, top), self.getmark(lineno)) | 
					
						
							|  |  |  | 		finally: | 
					
						
							|  |  |  | 			d.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | 	def showline(self, lineno): # scroll to make a line visible | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 		left = 0 | 
					
						
							|  |  |  | 		top = (lineno-1) * self.lineheight | 
					
						
							|  |  |  | 		right = self.leftmargin | 
					
						
							|  |  |  | 		bottom = lineno * self.lineheight | 
					
						
							|  |  |  | 		self.win.show((left, top), (right, bottom)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | # Subroutine to count the number of lines in a string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def countlines(text): | 
					
						
							|  |  |  | 	n = 0 | 
					
						
							|  |  |  | 	for c in text: | 
					
						
							|  |  |  | 		if c == '\n': n = n+1 | 
					
						
							|  |  |  | 	if text and text[-1] != '\n': n = n+1 # Partial last line | 
					
						
							|  |  |  | 	return n | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SourceWindow(TextWindow): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-12-17 15:25:27 +00:00
										 |  |  | 	def __init__(self, filename): | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | 		self.filename = filename | 
					
						
							|  |  |  | 		f = open(self.filename, 'r') | 
					
						
							|  |  |  | 		contents = f.read() | 
					
						
							|  |  |  | 		f.close() | 
					
						
							| 
									
										
										
										
											1993-12-17 15:25:27 +00:00
										 |  |  | 		TextWindow.__init__(self, self.filename, contents) | 
					
						
							| 
									
										
										
										
											1992-01-27 16:58:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # ------------------------------ testing ------------------------------ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | TESTFILE = 'srcwin.py' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test(): | 
					
						
							|  |  |  | 	import mainloop | 
					
						
							| 
									
										
										
										
											1993-12-17 15:25:27 +00:00
										 |  |  | 	sw = SourceWindow(TESTFILE) | 
					
						
							| 
									
										
										
										
											1992-01-22 22:21:31 +00:00
										 |  |  | 	mainloop.mainloop() |