srcwin.py :  » Mobile » Python-for-PalmOS » Python-1.5.2+reduced-1.0 » Lib » lib-stdwin » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Mobile » Python for PalmOS 
Python for PalmOS » Python 1.5.2 reduced 1.0 » Lib » lib stdwin » srcwin.py
# srcwin.py -- a source listing window

import stdwin
from stdwinevents import *
import basewin

WIDTH = 40
MAXHEIGHT = 24


class TextWindow(basewin.BaseWindow):
  
  def __init__(self, title, contents):
    self.contents = contents
    self.linecount = countlines(self.contents)
    #
    self.lineheight = lh = stdwin.lineheight()
    self.leftmargin = self.getmargin()
    self.top = 0
    self.rightmargin = 30000 # Infinity
    self.bottom = lh * self.linecount
    #
    width = WIDTH*stdwin.textwidth('0')
    height = lh*min(MAXHEIGHT, self.linecount)
    stdwin.setdefwinsize(width, height)
    basewin.BaseWindow.__init__(self, title)
    #
    self.win.setdocsize(0, self.bottom)
    self.initeditor()
  
  def initeditor(self):
    r = (self.leftmargin, self.top), (self.rightmargin, self.bottom)
    self.editor = self.win.textcreate(r)
    self.editor.settext(self.contents)
  
  def closeeditor(self):
    self.editor.close()
  
#  def reopen(self):
#    self.closeeditor()
#    basewin.BaseWindow.reopen(self)
#    self.initeditor()
  
  # Override the following two methods to format line numbers differently
  
  def getmark(self, lineno):
    return `lineno`
  
  def getmargin(self):
    return stdwin.textwidth(`self.linecount + 1` + ' ')
  
  # Event dispatcher, called from mainloop.mainloop()
  
  def dispatch(self, event):
    if event[0] == WE_NULL: return # Dummy tested by mainloop
    if event[0] == WE_DRAW or not self.editor.event(event):
      basewin.BaseWindow.dispatch(self, event)
  
  # Event handlers
  
  def close(self):
    self.closeeditor()
    basewin.BaseWindow.close(self)
  
  def draw(self, detail):
    dummy = self.editor.draw(detail)
    # Draw line numbers
    (left, top), (right, bottom) = detail
    topline = top/self.lineheight
    botline = bottom/self.lineheight + 1
    botline = min(self.linecount, botline)
    d = self.win.begindrawing()
    try:
      h, v = 0, self.lineheight * topline
      for lineno in range(topline+1, botline+1):
        d.text((h, v), self.getmark(lineno))
        v = v + self.lineheight
    finally:
      d.close()
  
  # Calls from outside
  
  def changemark(self, lineno): # redraw the mark for a line
    left = 0
    top = (lineno-1) * self.lineheight
    right = self.leftmargin
    bottom = lineno * self.lineheight
    d = self.win.begindrawing()
    try:
      d.erase((left, top), (right, bottom))
      d.text((left, top), self.getmark(lineno))
    finally:
      d.close()

  def showline(self, lineno): # scroll to make a line visible
    left = 0
    top = (lineno-1) * self.lineheight
    right = self.leftmargin
    bottom = lineno * self.lineheight
    self.win.show((left, top), (right, bottom))


# Subroutine to count the number of lines in a string

def countlines(text):
  n = 0
  for c in text:
    if c == '\n': n = n+1
  if text and text[-1] != '\n': n = n+1 # Partial last line
  return n


class SourceWindow(TextWindow):

  def __init__(self, filename):
    self.filename = filename
    f = open(self.filename, 'r')
    contents = f.read()
    f.close()
    TextWindow.__init__(self, self.filename, contents)

# ------------------------------ testing ------------------------------

TESTFILE = 'srcwin.py'

def test():
  import mainloop
  sw = SourceWindow(TESTFILE)
  mainloop.mainloop()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.