home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Lib / lib-stdwin / srcwin.py < prev    next >
Text File  |  2000-12-21  |  3KB  |  130 lines

  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.  
  11. class TextWindow(basewin.BaseWindow):
  12.     
  13.     def __init__(self, title, contents):
  14.         self.contents = contents
  15.         self.linecount = countlines(self.contents)
  16.         #
  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
  22.         #
  23.         width = WIDTH*stdwin.textwidth('0')
  24.         height = lh*min(MAXHEIGHT, self.linecount)
  25.         stdwin.setdefwinsize(width, height)
  26.         basewin.BaseWindow.__init__(self, title)
  27.         #
  28.         self.win.setdocsize(0, self.bottom)
  29.         self.initeditor()
  30.     
  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)
  35.     
  36.     def closeeditor(self):
  37.         self.editor.close()
  38.     
  39. #    def reopen(self):
  40. #        self.closeeditor()
  41. #        basewin.BaseWindow.reopen(self)
  42. #        self.initeditor()
  43.     
  44.     # Override the following two methods to format line numbers differently
  45.     
  46.     def getmark(self, lineno):
  47.         return `lineno`
  48.     
  49.     def getmargin(self):
  50.         return stdwin.textwidth(`self.linecount + 1` + ' ')
  51.     
  52.     # Event dispatcher, called from mainloop.mainloop()
  53.     
  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.     
  59.     # Event handlers
  60.     
  61.     def close(self):
  62.         self.closeeditor()
  63.         basewin.BaseWindow.close(self)
  64.     
  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.     
  81.     # Calls from outside
  82.     
  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()
  94.  
  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))
  101.  
  102.  
  103. # Subroutine to count the number of lines in a string
  104.  
  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
  111.  
  112.  
  113. class SourceWindow(TextWindow):
  114.  
  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)
  121.  
  122. # ------------------------------ testing ------------------------------
  123.  
  124. TESTFILE = 'srcwin.py'
  125.  
  126. def test():
  127.     import mainloop
  128.     sw = SourceWindow(TESTFILE)
  129.     mainloop.mainloop()
  130.