Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 1 | # srcwin.py -- a source listing window |
| 2 | |
| 3 | import stdwin |
| 4 | from stdwinevents import * |
| 5 | import basewin |
| 6 | |
| 7 | WIDTH = 40 |
| 8 | MAXHEIGHT = 24 |
| 9 | |
| 10 | class SourceWindow(basewin.BaseWindow): |
| 11 | |
| 12 | def init(self, filename): |
| 13 | self.filename = filename |
| 14 | # |
| 15 | f = open(self.filename, 'r') # raise exception if not found |
| 16 | self.contents = f.read() |
| 17 | f.seek(0) |
| 18 | self.linecount = len(f.readlines()) |
| 19 | f.close() |
| 20 | # |
| 21 | self.lineheight = lh = stdwin.lineheight() |
| 22 | self.leftmargin = stdwin.textwidth('00000000') |
| 23 | self.rightmargin = 30000 # Infinity |
| 24 | self.bottom = lh * self.linecount |
| 25 | # |
| 26 | stdwin.setdefwinpos(0, 0) |
| 27 | width = WIDTH*stdwin.textwidth('0') |
| 28 | height = lh*min(MAXHEIGHT, self.linecount) |
| 29 | stdwin.setdefwinsize(width, height) |
| 30 | self = basewin.BaseWindow.init(self, filename) |
| 31 | # |
| 32 | self.win.setdocsize(0, self.bottom + lh) |
| 33 | self.initeditor() |
| 34 | return self |
| 35 | |
| 36 | def initeditor(self): |
| 37 | r = (self.leftmargin, 0), (self.rightmargin, self.bottom) |
| 38 | self.editor = self.win.textcreate(r) |
| 39 | self.editor.settext(self.contents) |
| 40 | |
| 41 | def closeeditor(self): |
| 42 | self.editor.close() |
| 43 | |
| 44 | def reopen(self): |
| 45 | self.closeeditor() |
| 46 | basewin.BaseWindow.reopen(self) |
| 47 | self.initeditor() |
| 48 | |
| 49 | def close(self): |
| 50 | self.closeeditor() |
| 51 | basewin.BaseWindow.close(self) |
| 52 | |
| 53 | # Override this method to format line numbers differently |
| 54 | def getmark(self, lineno): |
| 55 | return `lineno` |
| 56 | |
| 57 | def dispatch(self, event): |
| 58 | if event[0] == WE_NULL: return # Dummy tested by mainloop |
| 59 | if event[0] == WE_DRAW or not self.editor.event(event): |
| 60 | basewin.BaseWindow.dispatch(self, event) |
| 61 | |
| 62 | def draw(self, detail): |
| 63 | dummy = self.editor.draw(detail) |
| 64 | # Draw line numbers |
| 65 | (left, top), (right, bottom) = detail |
| 66 | topline = top/self.lineheight |
| 67 | botline = bottom/self.lineheight + 1 |
| 68 | botline = min(self.linecount, botline) |
| 69 | d = self.win.begindrawing() |
| 70 | try: |
| 71 | h, v = 0, self.lineheight * topline |
| 72 | for lineno in range(topline+1, botline+1): |
| 73 | d.text((h, v), self.getmark(lineno)) |
| 74 | v = v + self.lineheight |
| 75 | finally: |
| 76 | d.close() |
| 77 | |
| 78 | def changemark(self, lineno): |
| 79 | left = 0 |
| 80 | top = (lineno-1) * self.lineheight |
| 81 | right = self.leftmargin |
| 82 | bottom = lineno * self.lineheight |
| 83 | d = self.win.begindrawing() |
| 84 | try: |
| 85 | d.erase((left, top), (right, bottom)) |
| 86 | d.text((left, top), self.getmark(lineno)) |
| 87 | finally: |
| 88 | d.close() |
| 89 | |
| 90 | def showline(self, lineno): |
| 91 | left = 0 |
| 92 | top = (lineno-1) * self.lineheight |
| 93 | right = self.leftmargin |
| 94 | bottom = lineno * self.lineheight |
| 95 | self.win.show((left, top), (right, bottom)) |
| 96 | |
| 97 | |
| 98 | TESTFILE = 'srcwin.py' |
| 99 | |
| 100 | def test(): |
| 101 | import mainloop |
| 102 | sw = SourceWindow().init(TESTFILE) |
| 103 | mainloop.mainloop() |