1 # srcwin.py -- a source listing window
4 from stdwinevents
import *
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()
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
)
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
):
41 # basewin.BaseWindow.reopen(self)
44 # Override the following two methods to format line numbers differently
46 def getmark(self
, lineno
):
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
)
63 basewin
.BaseWindow
.close(self
)
65 def draw(self
, detail
):
66 dummy
= self
.editor
.draw(detail
)
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()
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
83 def changemark(self
, lineno
): # redraw the mark for a line
85 top
= (lineno
-1) * self
.lineheight
86 right
= self
.leftmargin
87 bottom
= lineno
* self
.lineheight
88 d
= self
.win
.begindrawing()
90 d
.erase((left
, top
), (right
, bottom
))
91 d
.text((left
, top
), self
.getmark(lineno
))
95 def showline(self
, lineno
): # scroll to make a line visible
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
):
108 if c
== '\n': n
= n
+1
109 if text
and text
[-1] != '\n': n
= n
+1 # Partial last line
113 class SourceWindow(TextWindow
):
115 def __init__(self
, filename
):
116 self
.filename
= filename
117 f
= open(self
.filename
, 'r')
120 TextWindow
.__init
__(self
, self
.filename
, contents
)
122 # ------------------------------ testing ------------------------------
124 TESTFILE
= 'srcwin.py'
128 sw
= SourceWindow(TESTFILE
)