home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / twit / mac_widgets.py < prev    next >
Encoding:
Python Source  |  1996-09-26  |  8.7 KB  |  315 lines  |  [TEXT/Pyth]

  1. from FrameWork import *
  2. import Win
  3. import Qd
  4. import Controls
  5. import Ctl
  6. import TE
  7. import List
  8. import os
  9. import string
  10. import macfs
  11.  
  12. SCROLLBAR=16
  13. MARGIN=2
  14. ICONSIZE=16
  15. TEXTWIDTH=4096 # More-or-less random value
  16.  
  17. TEXTFONT=4
  18. TEXTSIZE=9
  19.  
  20. PIC_BREAK=513
  21. picture_cache={}
  22.  
  23. class MT_TextWidget:
  24.     def __init__(self, wid, r):
  25.         self.wid = wid
  26.         self.rect = r
  27.         left, top, right, bottom = r
  28.         self.terect = left+MARGIN+ICONSIZE, top+MARGIN, \
  29.                 right-(MARGIN+SCROLLBAR), bottom-(MARGIN+SCROLLBAR)
  30.         dr = self.terect[0], self.terect[1], TEXTWIDTH, self.terect[3]
  31.         Qd.SetPort(wid)
  32.         Qd.TextFont(TEXTFONT)
  33.         Qd.TextSize(TEXTSIZE)
  34.         self.ted = TE.TENew(dr, self.terect)
  35.         self.ted.TEAutoView(1)
  36.         self.activate(1)
  37.         
  38.         rect = right-SCROLLBAR, top, right, bottom-SCROLLBAR+1
  39.         self.bary = Ctl.NewControl(self.wid, rect, "", 1, 0, 0, 0, 16, 0)
  40.         rect = left, bottom-SCROLLBAR, right-SCROLLBAR+1, bottom
  41.         self.barx = Ctl.NewControl(self.wid, rect, "", 1, 0, 0, 0, 16, 0)
  42.         
  43.         self.have_data = 0
  44.         self.line_index = []
  45.         
  46.     def close(self):
  47.         del self.barx
  48.         del self.bary
  49.         del self.ted
  50.         
  51.     def scrollbars(self):
  52.         pass
  53.         
  54.     def setcontent(self, file):
  55.         self.line_index = []
  56.         if file == None:
  57.             data = ''
  58.             self.have_data = 0
  59.         else:
  60.             try:
  61.                 fp = open(file, 'rb') # NOTE the binary
  62.                 data = fp.read()
  63.                 self.have_data = 1
  64.             except IOError, arg:
  65.                 data = 'Cannot open file:\r'+`arg`
  66.                 self.have_data = 0
  67.         if len(data) > 32767:
  68.             self.have_data = 0
  69.             data = 'File too big'
  70.         self.ted.TESetText(data)
  71.         if self.have_data:
  72.             cur = 0
  73.             while 1:
  74.                 self.line_index.append(cur)
  75.                 try:
  76.                     cur = string.index(data, '\r', cur+1)
  77.                 except ValueError:
  78.                     break
  79.             self.line_index.append(len(data))
  80.         Win.InvalRect(self.rect)
  81.         self.ted.TESetSelect(0,0)
  82.         self.ted.TECalText()
  83.         self.ted.TESelView()
  84.         self.setscrollbars()
  85.         
  86.     def setscrollbars(self):
  87.         docleft, doctop, docright, docbot = self.ted.destRect
  88.         winleft, wintop, winright, winbot = self.ted.viewRect
  89.         docbot = self.ted.nLines*self.ted.lineHeight + doctop
  90.         self.setbar(self.barx, docleft, docright, winleft, winright)
  91.         self.setbar(self.bary, doctop, docbot, wintop, winbot)
  92.         
  93.     def setbar(self, bar, minmin, maxmax, curmin, curmax):
  94.         if maxmax-minmin > 32767 or (curmin <= minmin and curmax >= maxmax):
  95.             bar.SetControlMinimum(0)
  96.             bar.SetControlMaximum(0)
  97.             bar.SetControlValue(0)
  98.             return
  99.         bar.SetControlMinimum(minmin)
  100.         bar.SetControlMaximum(maxmax-(curmax-curmin))
  101.         bar.SetControlValue(curmin)
  102.  
  103.     def update(self, rgn):
  104.         Qd.EraseRect(self.terect)
  105.         Qd.FrameRect(self.rect)
  106.         self.ted.TEUpdate(self.terect)
  107.         
  108.     def activate(self, onoff):
  109.         if onoff:
  110.             self.ted.TEActivate()
  111.         else:
  112.             self.ted.TEDeactivate()
  113.  
  114.     def select(self, line):
  115.         if line == None or line <= 0 or not self.have_data:
  116.             self.ted.TESetSelect(0,0)
  117.         else:
  118.             line = line - 1
  119.             if line > len(self.line_index)-1: line = len(self.line_index)-1
  120.             if line == 1:
  121.                 self.ted.TESetSelect(0, self.line_index[1])
  122.             else:
  123.                 self.ted.TESetSelect(self.line_index[line]+1, self.line_index[line+1])
  124.         self.setscrollbars()
  125.         
  126.     def click(self, where, modifiers):
  127.         # First check scrollbars
  128.         ctltype, control = Ctl.FindControl(where, self.wid)
  129.         if ctltype and control:
  130.             partcode = control.TrackControl(where)
  131.             if partcode:
  132.                 self.controlhit(control, partcode)
  133.             return None, 0
  134.         off = self.ted.TEGetOffset(where)
  135.         inborder = where[0] < self.terect[0]
  136.         l, t, r, b = self.terect
  137.         if l <= where[0] <= r and t <= where[1] <= b or inborder:
  138.             return self.offsettoline(off), inborder
  139.         return None, 0    # In the grow box or something.
  140.         
  141.     def offsettoline(self, offset):
  142.         for i in range(len(self.line_index)):
  143.             if offset < self.line_index[i]:
  144.                 return i   # Not i-1: 1-based line numbers in files
  145.         return None
  146.  
  147.     def controlhit(self, control, partcode):
  148.         if partcode <> Controls.inThumb:
  149.             if control == self.barx:
  150.                 if partcode == Controls.inUpButton:
  151.                     delta = -10
  152.                 if partcode == Controls.inDownButton:
  153.                     delta = 10
  154.                 if partcode == Controls.inPageUp:
  155.                     delta = 10-(self.terect[2]-self.terect[0])
  156.                 if partcode == Controls.inPageDown:
  157.                     delta = (self.terect[2]-self.terect[0])-10
  158.                 old = control.GetControlValue()
  159.                 control.SetControlValue(old+delta)
  160.             if control == self.bary:
  161.                 if partcode == Controls.inUpButton:
  162.                     delta = -self.ted.lineHeight
  163.                 if partcode == Controls.inDownButton:
  164.                     delta = self.ted.lineHeight
  165.                 if partcode == Controls.inPageUp:
  166.                     delta = self.ted.lineHeight-(self.terect[3]-self.terect[1])
  167.                 if partcode == Controls.inPageDown:
  168.                     delta = (self.terect[3]-self.terect[1])-self.ted.lineHeight
  169.                 old = control.GetControlValue()
  170.                 control.SetControlValue(old+delta)
  171.         newx = self.barx.GetControlValue()
  172.         newy = self.bary.GetControlValue()
  173.         oldx = self.ted.viewRect[0]
  174.         oldy = self.ted.viewRect[1]
  175.         self.ted.TEPinScroll(oldx-newx, oldy-newy)
  176.         self.setscrollbars() # XXXX Bibbert, maar hoe anders?
  177.             
  178. class MT_IconTextWidget(MT_TextWidget):
  179.     def __init__(self, wid, r):
  180.         MT_TextWidget.__init__(self, wid, r)
  181.         self.breakpointlist = []
  182.         self.curline = None
  183.         self.iconrect = (self.rect[0]+1, self.rect[1]+1, 
  184.                 self.terect[0]-1, self.terect[3]-1)
  185.         self.curlinerange = (self.terect[1]+self.ted.lineHeight,
  186.                 self.terect[3]-2*self.ted.lineHeight)
  187.         self.piccurrent = 512
  188.         
  189.     def setbreaks(self, list):
  190.         self.breakpointlist = list[:]
  191.         Qd.SetPort(self.wid)
  192.         Win.InvalRect(self.iconrect)
  193.         
  194.     def setcurline(self, line, pic=512):
  195.         self.curline = line
  196.         self.piccurrent = pic
  197.         Qd.SetPort(self.wid)
  198.         self.showline(line)
  199.  
  200.     def showline(self, line):
  201.         if line <= 0: line = 1
  202.         if line >= len(self.line_index): line = len(self.line_index)-1
  203.         if line < 0: return
  204.         off = self.line_index[line]
  205.         x, y = self.ted.TEGetPoint(off)
  206.         if self.curlinerange[0] <= y <= self.curlinerange[1]:
  207.             return # It is in view
  208.         middle = (self.curlinerange[0]+self.curlinerange[1])/2
  209.         self.ted.TEPinScroll(0, middle-y) # Of andersom?
  210.         self.setscrollbars()
  211.         
  212.     def setscrollbars(self):
  213.         MT_TextWidget.setscrollbars(self)
  214.         Win.InvalRect(self.iconrect)
  215.                 
  216.     def update(self, rgn):
  217.         MT_TextWidget.update(self, rgn)
  218.         self.drawallicons()
  219.         
  220.     def drawallicons(self):
  221.         Qd.EraseRect(self.iconrect)
  222.         Qd.MoveTo(self.iconrect[2], self.iconrect[1])
  223.         Qd.LineTo(self.iconrect[2], self.iconrect[3])
  224.         topoffset = self.ted.TEGetOffset((self.terect[0], self.terect[1]))
  225.         botoffset = self.ted.TEGetOffset((self.terect[0], self.terect[3]))
  226.         topline = self.offsettoline(topoffset)
  227.         botline = self.offsettoline(botoffset)
  228.         if topline == None: topline = 1 # ???
  229.         if botline == None: botline = len(self.line_index)
  230.         for i in self.breakpointlist:
  231.             if topline <= i <= botline:
  232.                 self.draw1icon(i, PIC_BREAK)
  233.         if self.curline <> None and topline <= self.curline <= botline:
  234.             self.draw1icon(self.curline, self.piccurrent)
  235.             
  236.     def draw1icon(self, line, which):
  237.         offset = self.line_index[line]
  238.         botx, boty = self.ted.TEGetPoint(offset)
  239.         rect = self.rect[0]+2, boty-self.ted.lineHeight, \
  240.             self.rect[0]+ICONSIZE-2, boty
  241.         if not picture_cache.has_key(which):
  242.             picture_cache[which] = Qd.GetPicture(which)
  243.         self.drawicon(rect, picture_cache[which])
  244.         
  245.     def drawicon(self, rect, which):
  246.         Qd.DrawPicture(which, rect)
  247.  
  248. class MT_IndexList:
  249.     def __init__(self, wid, rect, width):
  250.         # wid is the window (dialog) where our list is going to be in
  251.         # rect is it's item rectangle (as in dialog item)
  252.         self.rect = rect
  253.         rect2 = rect[0]+1, rect[1]+1, rect[2]-16, rect[3]-1
  254.         self.list = List.LNew(rect2, (0, 0, width, 0), (0,0), 0, wid,
  255.                     0, 1, 0, 1)
  256.         self.wid = wid
  257.         self.width = width
  258.     
  259.     def setcontent(self, *content):
  260.         self.list.LDelRow(0, 1)
  261.         self.list.LSetDrawingMode(0)
  262.         self.list.LAddRow(len(content[0]), 0)
  263.         for x in range(len(content)):
  264.             column = content[x]
  265.             for y in range(len(column)):
  266.                 self.list.LSetCell(column[y], (x, y))
  267.         self.list.LSetDrawingMode(1)
  268.         Win.InvalRect(self.rect)
  269.  
  270.     def deselectall(self):
  271.         while 1:
  272.             ok, pt = self.list.LGetSelect(1, (0,0))
  273.             if not ok: return
  274.             self.list.LSetSelect(0, pt)
  275.             
  276.     def select(self, num):
  277.         self.deselectall()
  278.         if num < 0:
  279.             return
  280.         for i in range(self.width):
  281.             self.list.LSetSelect(1, (i, num))
  282.             
  283.     def click(self, where, modifiers):
  284.         is_double = self.list.LClick(where, modifiers)
  285.         ok, (x, y) = self.list.LGetSelect(1, (0, 0))
  286.         if ok:
  287.             return y, is_double
  288.         else:
  289.             return None, is_double
  290.             
  291.     # draw a frame around the list, List Manager doesn't do that
  292.     def drawframe(self):
  293.         Qd.SetPort(self.wid)
  294.         Qd.FrameRect(self.rect)
  295.         
  296.     def update(self, rgn):
  297.         self.drawframe()
  298.         self.list.LUpdate(rgn)
  299.         
  300.     def activate(self, onoff):
  301.         self.list.LActivate(onoff)
  302.         
  303. class MT_AnyList(MT_IndexList):
  304.  
  305.     def click(self, where, modifiers):
  306.         is_double = self.list.LClick(where, modifiers)
  307.         ok, (x, y) = self.list.LGetSelect(1, (0, 0))
  308.         if ok:
  309.             self.select(y)
  310.             field0 = self.list.LGetCell(1000,(0,y))
  311.         else:
  312.             field0 = None
  313.         return field0, is_double
  314.     
  315.