Mercurial > cpython-lru_cache-weakref
annotate Lib/lib-stdwin/srcwin.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 560ec7030817 |
| children |
| rev | line source |
|---|---|
| 622 | 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 | |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
10 |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
11 class TextWindow(basewin.BaseWindow): |
| 622 | 12 |
|
1435
560ec7030817
* Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents:
647
diff
changeset
|
13 def __init__(self, title, contents): |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
14 self.contents = contents |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
15 self.linecount = countlines(self.contents) |
| 622 | 16 # |
| 17 self.lineheight = lh = stdwin.lineheight() | |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
18 self.leftmargin = self.getmargin() |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
19 self.top = 0 |
| 622 | 20 self.rightmargin = 30000 # Infinity |
| 21 self.bottom = lh * self.linecount | |
| 22 # | |
| 23 width = WIDTH*stdwin.textwidth('0') | |
| 24 height = lh*min(MAXHEIGHT, self.linecount) | |
| 25 stdwin.setdefwinsize(width, height) | |
|
1435
560ec7030817
* Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents:
647
diff
changeset
|
26 basewin.BaseWindow.__init__(self, title) |
| 622 | 27 # |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
28 self.win.setdocsize(0, self.bottom) |
| 622 | 29 self.initeditor() |
| 30 | |
| 31 def initeditor(self): | |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
32 r = (self.leftmargin, self.top), (self.rightmargin, self.bottom) |
| 622 | 33 self.editor = self.win.textcreate(r) |
| 34 self.editor.settext(self.contents) | |
| 35 | |
| 36 def closeeditor(self): | |
| 37 self.editor.close() | |
| 38 | |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
39 # def reopen(self): |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
40 # self.closeeditor() |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
41 # basewin.BaseWindow.reopen(self) |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
42 # self.initeditor() |
| 622 | 43 |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
44 # Override the following two methods to format line numbers differently |
| 622 | 45 |
| 46 def getmark(self, lineno): | |
| 47 return `lineno` | |
| 48 | |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
49 def getmargin(self): |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
50 return stdwin.textwidth(`self.linecount + 1` + ' ') |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
51 |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
52 # Event dispatcher, called from mainloop.mainloop() |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
53 |
| 622 | 54 def dispatch(self, event): |
| 55 if event[0] == WE_NULL: return # Dummy tested by mainloop | |
| 56 if event[0] == WE_DRAW or not self.editor.event(event): | |
| 57 basewin.BaseWindow.dispatch(self, event) | |
| 58 | |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
59 # Event handlers |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
60 |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
61 def close(self): |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
62 self.closeeditor() |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
63 basewin.BaseWindow.close(self) |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
64 |
| 622 | 65 def draw(self, detail): |
| 66 dummy = self.editor.draw(detail) | |
| 67 # Draw line numbers | |
| 68 (left, top), (right, bottom) = detail | |
| 69 topline = top/self.lineheight | |
| 70 botline = bottom/self.lineheight + 1 | |
| 71 botline = min(self.linecount, botline) | |
| 72 d = self.win.begindrawing() | |
| 73 try: | |
| 74 h, v = 0, self.lineheight * topline | |
| 75 for lineno in range(topline+1, botline+1): | |
| 76 d.text((h, v), self.getmark(lineno)) | |
| 77 v = v + self.lineheight | |
| 78 finally: | |
| 79 d.close() | |
| 80 | |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
81 # Calls from outside |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
82 |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
83 def changemark(self, lineno): # redraw the mark for a line |
| 622 | 84 left = 0 |
| 85 top = (lineno-1) * self.lineheight | |
| 86 right = self.leftmargin | |
| 87 bottom = lineno * self.lineheight | |
| 88 d = self.win.begindrawing() | |
| 89 try: | |
| 90 d.erase((left, top), (right, bottom)) | |
| 91 d.text((left, top), self.getmark(lineno)) | |
| 92 finally: | |
| 93 d.close() | |
| 94 | |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
95 def showline(self, lineno): # scroll to make a line visible |
| 622 | 96 left = 0 |
| 97 top = (lineno-1) * self.lineheight | |
| 98 right = self.leftmargin | |
| 99 bottom = lineno * self.lineheight | |
| 100 self.win.show((left, top), (right, bottom)) | |
| 101 | |
| 102 | |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
103 # Subroutine to count the number of lines in a string |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
104 |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
105 def countlines(text): |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
106 n = 0 |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
107 for c in text: |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
108 if c == '\n': n = n+1 |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
109 if text and text[-1] != '\n': n = n+1 # Partial last line |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
110 return n |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
111 |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
112 |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
113 class SourceWindow(TextWindow): |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
114 |
|
1435
560ec7030817
* Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents:
647
diff
changeset
|
115 def __init__(self, filename): |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
116 self.filename = filename |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
117 f = open(self.filename, 'r') |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
118 contents = f.read() |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
119 f.close() |
|
1435
560ec7030817
* Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents:
647
diff
changeset
|
120 TextWindow.__init__(self, self.filename, contents) |
|
647
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
121 |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
122 # ------------------------------ testing ------------------------------ |
|
8bba14f7db8a
Split in TextWindow and SourceWindow.
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
123 |
| 622 | 124 TESTFILE = 'srcwin.py' |
| 125 | |
| 126 def test(): | |
| 127 import mainloop | |
|
1435
560ec7030817
* Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents:
647
diff
changeset
|
128 sw = SourceWindow(TESTFILE) |
| 622 | 129 mainloop.mainloop() |
