Improved some error messages for command line processing.
[python/dscho.git] / Lib / lib-stdwin / srcwin.py
blob29b78010a616b9060aec78d8427c38f5db4075fa
1 # srcwin.py -- a source listing window
3 import stdwin
4 from stdwinevents import *
5 import basewin
7 WIDTH = 40
8 MAXHEIGHT = 24
11 class TextWindow(basewin.BaseWindow):
13 def __init__(self, title, contents):
14 self.contents = contents
15 self.linecount = countlines(self.contents)
17 self.lineheight = lh = stdwin.lineheight()
18 self.leftmargin = self.getmargin()
19 self.top = 0
20 self.rightmargin = 30000 # Infinity
21 self.bottom = lh * self.linecount
23 width = WIDTH*stdwin.textwidth('0')
24 height = lh*min(MAXHEIGHT, self.linecount)
25 stdwin.setdefwinsize(width, height)
26 basewin.BaseWindow.__init__(self, title)
28 self.win.setdocsize(0, self.bottom)
29 self.initeditor()
31 def initeditor(self):
32 r = (self.leftmargin, self.top), (self.rightmargin, self.bottom)
33 self.editor = self.win.textcreate(r)
34 self.editor.settext(self.contents)
36 def closeeditor(self):
37 self.editor.close()
39 # def reopen(self):
40 # self.closeeditor()
41 # basewin.BaseWindow.reopen(self)
42 # self.initeditor()
44 # Override the following two methods to format line numbers differently
46 def getmark(self, lineno):
47 return `lineno`
49 def getmargin(self):
50 return stdwin.textwidth(`self.linecount + 1` + ' ')
52 # Event dispatcher, called from mainloop.mainloop()
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)
59 # Event handlers
61 def close(self):
62 self.closeeditor()
63 basewin.BaseWindow.close(self)
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()
81 # Calls from outside
83 def changemark(self, lineno): # redraw the mark for a line
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()
95 def showline(self, lineno): # scroll to make a line visible
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))
103 # Subroutine to count the number of lines in a string
105 def countlines(text):
106 n = 0
107 for c in text:
108 if c == '\n': n = n+1
109 if text and text[-1] != '\n': n = n+1 # Partial last line
110 return n
113 class SourceWindow(TextWindow):
115 def __init__(self, filename):
116 self.filename = filename
117 f = open(self.filename, 'r')
118 contents = f.read()
119 f.close()
120 TextWindow.__init__(self, self.filename, contents)
122 # ------------------------------ testing ------------------------------
124 TESTFILE = 'srcwin.py'
126 def test():
127 import mainloop
128 sw = SourceWindow(TESTFILE)
129 mainloop.mainloop()