home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / gui / vlist / vlist.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  10.5 KB  |  313 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from gui.skin.skinparse import makeBrush
  5. import random
  6. import wx
  7. from wx import Rect, RectPS
  8.  
  9. class VList(wx.ScrolledWindow):
  10.     
  11.     def __init__(self, parent, id = -1, style = wx.NO_BORDER | wx.FULL_REPAINT_ON_RESIZE, multiple = False):
  12.         wx.ScrolledWindow.__init__(self, parent, style = style)
  13.         if not isinstance(multiple, bool):
  14.             raise TypeError('multiple argument must be a bool')
  15.         
  16.         self._selection = set()
  17.         self.visible = list()
  18.         self.multiple = multiple
  19.         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  20.         Bind = self.Bind
  21.         Bind(wx.EVT_ERASE_BACKGROUND, (lambda e: pass))
  22.         Bind(wx.EVT_PAINT, self._VList__paint)
  23.         Bind(wx.EVT_MOUSEWHEEL, self._VList__wheel)
  24.         Bind(wx.EVT_KEY_DOWN, self._VList__keydown)
  25.         Bind(wx.EVT_LEFT_DOWN, self._VList__leftdown)
  26.         Bind(wx.EVT_LEFT_DCLICK, self._VList__leftdclick)
  27.  
  28.     
  29.     def SetBackground(self, background_brush):
  30.         self.bg = background_brush
  31.         s = background_brush.ytile
  32.         self.EnableScrolling(s, s)
  33.  
  34.     
  35.     def GetBackground(self):
  36.         return self.bg
  37.  
  38.     Background = property(GetBackground, SetBackground)
  39.     
  40.     def RefreshLine(self, line):
  41.         vis = dict((lambda .0: for y1, y2, n in .0:
  42. (n, (y1, y2)))(self.visible))
  43.         if line in vis:
  44.             (left, top, right, bottom) = self.GetItemMargins()
  45.             w = self.ClientSize.width
  46.             y = self.ViewStart[1]
  47.             (y1, y2) = vis[line]
  48.             r = wx.Rect(0, y1 - top - y, w, 0)
  49.             r.SetHeight((y2 - y1) + top + bottom)
  50.             self.RefreshRect(r, False)
  51.         
  52.  
  53.     
  54.     def RefreshLines(self, lines):
  55.         vis = dict((lambda .0: for y1, y2, n in .0:
  56. (n, (y1, y2)))(self.visible))
  57.         (left, top, right, bottom) = self.GetItemMargins()
  58.         w = self.ClientSize.width
  59.         y = self.ViewStart[1]
  60.         for line in lines:
  61.             if line in vis:
  62.                 (y1, y2) = vis[line]
  63.                 r = wx.Rect(0, y1 - top - y, w, 0)
  64.                 r.SetHeight((y2 - y1) + top + bottom)
  65.                 self.RefreshRect(r, False)
  66.                 continue
  67.         
  68.  
  69.     
  70.     def RefreshAll(self):
  71.         return self.Refresh()
  72.  
  73.     
  74.     def ScrollToLine(self, i):
  75.         pass
  76.  
  77.     
  78.     def UpdateScrollbars(self):
  79.         margins = self.GetItemMargins()
  80.         totalHeight = (sum,)((lambda .0: for n in .0:
  81. self.OnMeasureItem(n))(xrange(self._itemcount)))
  82.         self.SetScrollbars(0, 1, 0, (margins[1] + margins[3]) * self._itemcount + totalHeight)
  83.  
  84.     
  85.     def HitTest(self, .1):
  86.         (x, y) = .1
  87.         p = wx.Point(x, y) + self.ViewStart
  88.         for y1, y2, n in self.visible:
  89.             if p.y >= y1 and p.y < y2:
  90.                 return n
  91.                 continue
  92.         
  93.         return wx.NOT_FOUND
  94.  
  95.     
  96.     def GetSelection(self):
  97.         if self.multiple:
  98.             return self._selection
  99.         else:
  100.             
  101.             try:
  102.                 return list(self._selection)[0]
  103.             except IndexError:
  104.                 return -1
  105.  
  106.  
  107.     
  108.     def SetSelection(self, newsel):
  109.         if self.multiple:
  110.             pass
  111.         elif not isinstance(newsel, int):
  112.             raise TypeError('single selection lists take an integer argument to SetSelection')
  113.         
  114.         oldsel = self._selection
  115.         self._selection = set([
  116.             newsel])
  117.         s = self._selection.union(oldsel)
  118.         self.RefreshLines(min(s), max(s))
  119.  
  120.     Selection = property(GetSelection, SetSelection)
  121.     
  122.     def GetFirstVisibleLine(self):
  123.         
  124.         try:
  125.             return self.visible[0][-1]
  126.         except IndexError:
  127.             return -1
  128.  
  129.  
  130.     
  131.     def GetLastVisibleLine(self):
  132.         
  133.         try:
  134.             return self.visible[-1][-1]
  135.         except IndexError:
  136.             return -1
  137.  
  138.  
  139.     
  140.     def IsSelected(self, n):
  141.         return n in self._selection
  142.  
  143.     
  144.     def __wheel(self, e):
  145.         self.Scroll(0, self.ViewStart[1] - e.WheelRotation)
  146.  
  147.     
  148.     def __keydown(self, e):
  149.         c = e.KeyCode
  150.         sel = self._selection
  151.         if c == wx.WXK_DOWN:
  152.             
  153.             try:
  154.                 s = sel.pop()
  155.             except KeyError:
  156.                 s = -1
  157.  
  158.             if s + 1 == len(self):
  159.                 s = -1
  160.             
  161.             self._selection = set([
  162.                 s + 1])
  163.         elif c == wx.WXK_UP:
  164.             
  165.             try:
  166.                 s = sel.pop()
  167.             except KeyError:
  168.                 s = -1
  169.  
  170.             if s == 0:
  171.                 s = -1
  172.             
  173.             self._selection = None(set if s != -1 else [
  174.                 len(self) - 1])
  175.         else:
  176.             return e.Skip()
  177.         s = self._selection.union(sel)
  178.         self.RefreshLines(min(s), max(s))
  179.  
  180.     
  181.     def __paint(self, e):
  182.         dc = wx.BufferedPaintDC(self, style = wx.BUFFER_VIRTUAL_AREA)
  183.         size = self.ClientSize
  184.         (viewstartx, viewstarty) = self.ViewStart
  185.         viewend = viewstarty + size[1]
  186.         self.bg.Draw(dc, RectPS((viewstartx, viewstarty), size))
  187.         (left, top, right, bottom) = self.GetItemMargins()
  188.         r = Rect(left, top, *size)
  189.         r.SetSize((r.Width - right, 0))
  190.         visible = self.visible
  191.         visappend = visible.append
  192.         del visible[:]
  193.         selection = self._selection
  194.         measureitem = self.OnMeasureItem
  195.         drawitem = self.OnDrawItem
  196.         drawbg = self.OnDrawBackground
  197.         offset = r.Offset
  198.         for n in xrange(self._itemcount):
  199.             itemHeight = measureitem(n)
  200.             if r.Y > viewend:
  201.                 break
  202.             
  203.             if r.Y + itemHeight >= viewstarty:
  204.                 r.Height = itemHeight
  205.                 visappend((r.Y - top, r.Y + r.Height + bottom, n))
  206.                 drawbg(dc, Rect(*r), n, n in selection)
  207.                 drawitem(dc, Rect(*r), n, n in selection)
  208.             
  209.             offset((0, itemHeight + top + bottom))
  210.         
  211.  
  212.     
  213.     def _ensure_visible(self, line):
  214.         for top, bottom, n in self.visible:
  215.             if n == line:
  216.                 return None
  217.                 continue
  218.         
  219.  
  220.     
  221.     def __leftdown(self, e):
  222.         i = self.HitTest(e.Position)
  223.         cmd = e.CmdDown()
  224.         sel = self._selection
  225.         if set([
  226.             i]) != sel:
  227.             
  228.             try:
  229.                 old = self._selection.pop()
  230.             except KeyError:
  231.                 self._selection.add(i)
  232.                 self.RefreshLine(i)
  233.  
  234.             self._selection.add(i)
  235.             s = [
  236.                 old,
  237.                 i]
  238.             self.RefreshLines(min(s), max(s))
  239.         
  240.  
  241.     
  242.     def __leftdclick(self, e):
  243.         evt = wx.CommandEvent(wx.wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, self.Id)
  244.         evt.SetEventObject(self)
  245.         evt.SetInt(self.HitTest(e.Position))
  246.         self.ProcessEvent(evt)
  247.  
  248.     
  249.     def NativeScrollbars(self):
  250.         return True
  251.  
  252.     NativeScrollbars = property(NativeScrollbars)
  253.     
  254.     def GetItemMargins(self):
  255.         return (0, 0, 0, 0)
  256.  
  257.     
  258.     def SetItemCount(self, n):
  259.         self._itemcount = n
  260.         self.UpdateScrollbars()
  261.  
  262.     
  263.     def GetItemCount(self):
  264.         return self._itemcount
  265.  
  266.     __len__ = GetItemCount
  267.     ItemCount = property(GetItemCount, SetItemCount)
  268.     
  269.     def OnDrawBackground(self, dc, rect, n, selected):
  270.         if selected:
  271.             dc.Brush = wx.BLUE_BRUSH
  272.         else:
  273.             dc.Brush = wx.WHITE_BRUSH
  274.         dc.Pen = wx.TRANSPARENT_PEN
  275.         dc.DrawRectangleRect(rect)
  276.  
  277.  
  278.  
  279. class BList(VList):
  280.     
  281.     def __init__(self, parent):
  282.         VList.__init__(self, parent)
  283.         self.itembg = makeBrush('white 40% white 40%')
  284.         self.selbg = makeBrush('white 90% white 90%')
  285.  
  286.     
  287.     def OnMeasureItem(self, n):
  288.         random.seed(n)
  289.         return random.randint(15, 80)
  290.  
  291.     
  292.     def OnDrawItem(self, dc, rect, n, selected):
  293.         if not selected:
  294.             self.itembg.Draw(dc, rect, n)
  295.         else:
  296.             self.selbg.Draw(dc, rect, n)
  297.         dc.Font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
  298.         dc.DrawText('item %d' % n, rect.X, rect.Y)
  299.  
  300.     
  301.     def GetItemMargins(self):
  302.         return (3, 3, 3, 20)
  303.  
  304.  
  305. if __name__ == '__main__':
  306.     a = wx.PySimpleApp()
  307.     f = wx.Frame(None, size = (240, 650))
  308.     v = BList(f)
  309.     v.SetItemCount(65)
  310.     f.Show()
  311.     a.MainLoop()
  312.  
  313.