home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / gui / skin / skinobjects.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  14.7 KB  |  394 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. from __future__ import division
  6. import wx
  7. from util.primitives.funcs import autoassign
  8. create_gc = wx.GraphicsContext.Create
  9. from gui.toolbox import TransparentBitmap
  10. from operator import itemgetter
  11. from cgui import Bitmap_Draw
  12. from config import platformName
  13. import new
  14. wx.Bitmap.Draw = new.instancemethod(Bitmap_Draw, None, wx.Bitmap)
  15. USE_CGUI_SKIN_OBJECTS = False
  16. if platformName == 'win':
  17.     USE_CGUI_SKIN_OBJECTS = True
  18.  
  19.  
  20. class MarginSizer(wx.GridBagSizer):
  21.     
  22.     def __init__(self, margins, content):
  23.         wx.GridBagSizer.__init__(self)
  24.         self.SetEmptyCellSize(wx.Size(0, 0))
  25.         self.Add(content, (1, 1), flag = wx.EXPAND)
  26.         self.AddGrowableCol(1, 1)
  27.         self.AddGrowableRow(1, 1)
  28.         self.SetMargins(margins)
  29.  
  30.     
  31.     def SetMargins(self, margins):
  32.         if len(self.GetChildren()) > 2:
  33.             self.Detach(1)
  34.             self.Detach(1)
  35.         
  36.         self.Add(wx.Size(margins.left, margins.top), (0, 0))
  37.         self.Add(wx.Size(margins.right, margins.bottom), (2, 2))
  38.  
  39.  
  40.  
  41. class Margins(list):
  42.     
  43.     def __init__(self, a = None):
  44.         if a is None:
  45.             list.__init__(self, [
  46.                 0,
  47.                 0,
  48.                 0,
  49.                 0])
  50.         elif isinstance(a, basestring):
  51.             list.__init__(self, (lambda .0: for c in .0:
  52. int(c))(a.split()))
  53.         elif isinstance(a, int):
  54.             list.__init__(self, [
  55.                 a] * 4)
  56.         elif len(a) == 2:
  57.             a = [
  58.                 a[0],
  59.                 a[1],
  60.                 a[0],
  61.                 a[1]]
  62.         
  63.         list.__init__(self, (lambda .0: for c in .0:
  64. int(c))(a))
  65.  
  66.     
  67.     def Sizer(self, content):
  68.         return MarginSizer(self, content)
  69.  
  70.     left = property(itemgetter(0))
  71.     top = property(itemgetter(1))
  72.     right = property(itemgetter(2))
  73.     bottom = property(itemgetter(3))
  74.     x = property((lambda self: self[0] + self[2]))
  75.     y = property((lambda self: self[1] + self[3]))
  76.     TopLeft = property((lambda self: wx.Point(self[0], self[1])))
  77.     BottomRight = property((lambda self: wx.Point(self[2], self[3])))
  78.     
  79.     def __neg__(self):
  80.         return []([ -a for a in self ])
  81.  
  82.  
  83.  
  84. class SkinBase(object):
  85.     
  86.     def Show(self):
  87.         ShowImage = ShowImage
  88.         import gui.toolbox
  89.         ShowImage(self)
  90.  
  91.  
  92.  
  93. class SkinRegion(SkinBase):
  94.     
  95.     def __init__(self, border = None, rounded = False, highlight = False, shadow = False):
  96.         if not isinstance(border, (type(None), wx.Pen)):
  97.             raise TypeError('border must be a Pen or None')
  98.         isinstance(border, (type(None), wx.Pen))
  99.         autoassign(self, locals())
  100.         self.pen = None if border is None else border
  101.  
  102.     
  103.     def GetBitmap(self, size, n = 0):
  104.         tb = TransparentBitmap(size)
  105.         dc = wx.MemoryDC()
  106.         dc.SelectObject(tb)
  107.         self.draw(dc, wx.RectS(size), n)
  108.         dc.SelectObject(wx.NullBitmap)
  109.         return tb
  110.  
  111.     
  112.     def Stroke(self, dc, gc, rect, n = 0):
  113.         pen = self.pen
  114.         penw = pen.Width // 2
  115.         rect = wx.Rect(*rect)
  116.         rect.Deflate(penw, penw)
  117.         pen.SetCap(wx.CAP_PROJECTING)
  118.         gc.SetPen(pen)
  119.         dc.SetPen(pen)
  120.         dc.Brush = wx.TRANSPARENT_BRUSH
  121.         gc.Brush = wx.TRANSPARENT_BRUSH
  122.         if self.rounded:
  123.             if self.border:
  124.                 gc.DrawRoundedRectangle(*tuple(rect) + (self.rounded * 0.97,))
  125.             
  126.             rect.Inflate(penw, penw)
  127.             self.stroke_highlights_rounded(gc, rect)
  128.         elif self.border:
  129.             offset = int(pen.Width % 2 == 0)
  130.             dl = dc.DrawLine
  131.             x = wx.Point(offset, 0)
  132.             y = wx.Point(0, offset)
  133.             for a, b in [
  134.                 (rect.TopLeft, rect.BottomLeft + y),
  135.                 (rect.BottomLeft + y, rect.BottomRight + y + x),
  136.                 (rect.BottomRight + y + x, rect.TopRight + x),
  137.                 (rect.TopRight + x, rect.TopLeft)]:
  138.                 dl(*tuple(a) + tuple(b))
  139.             
  140.         
  141.         rect.Inflate(penw, penw)
  142.         self.stroke_highlights(gc, rect)
  143.  
  144.     
  145.     def stroke_highlights_rounded(self, gc, rect):
  146.         pass
  147.  
  148.     
  149.     def stroke_highlights(self, gc, rect):
  150.         hw = max(2, self.pen.Width)
  151.         seq = []
  152.         if self.highlight:
  153.             c1 = wx.Color(255, 255, 255, 100)
  154.             c2 = wx.Color(255, 255, 255, 150)
  155.             seq.extend([
  156.                 ((rect.x, rect.y), [
  157.                     (rect.x, rect.Bottom),
  158.                     (rect.x + hw, rect.Bottom - hw),
  159.                     (rect.x + hw, rect.y + hw),
  160.                     (rect.x, rect.y)], gc.CreateLinearGradientBrush(rect.x, rect.y, rect.x + hw, rect.y, c1, c2)),
  161.                 ((rect.x, rect.y), [
  162.                     (rect.Right + 1, rect.y),
  163.                     (rect.Right - hw, rect.y + hw),
  164.                     (rect.x + hw, rect.y + hw),
  165.                     (rect.x, rect.y)], gc.CreateLinearGradientBrush(rect.x, rect.y, rect.x, rect.y + hw, c1, c2))])
  166.         
  167.         if self.shadow:
  168.             sc1 = wx.Color(0, 0, 0, 50)
  169.             sc2 = wx.Color(0, 0, 0, 100)
  170.             seq.extend([
  171.                 ((rect.Right + 1, rect.Bottom + 1), [
  172.                     (rect.x, rect.Bottom + 1),
  173.                     (rect.x + hw, (rect.Bottom - hw) + 1),
  174.                     ((rect.Right - hw) + 1, (rect.Bottom - hw) + 1),
  175.                     (rect.Right + 1, rect.Bottom + 1)], gc.CreateLinearGradientBrush(rect.x, (rect.Bottom - hw) + 1, rect.x, rect.Bottom + 1, sc1, sc2)),
  176.                 ((rect.Right + 1, rect.Bottom + 1), [
  177.                     (rect.Right + 1, rect.y),
  178.                     ((rect.Right - hw) + 1, rect.y + hw),
  179.                     ((rect.Right - hw) + 1, (rect.Bottom - hw) + 1),
  180.                     (rect.Right + 1, rect.Bottom + 1)], gc.CreateLinearGradientBrush((rect.Right - hw) + 1, rect.Bottom, rect.Right + 1, rect.Bottom, sc1, sc2))])
  181.         
  182.         if seq:
  183.             for origin, pts, brush in seq:
  184.                 p = gc.CreatePath()
  185.                 p.MoveToPoint(*origin)
  186.                 for pt in pts:
  187.                     p.AddLineToPoint(*pt)
  188.                 
  189.                 gc.SetBrush(brush)
  190.                 gc.FillPath(p)
  191.             
  192.         
  193.  
  194.     
  195.     def draw(self, *a, **k):
  196.         self.Draw(*a, **k)
  197.  
  198.  
  199.  
  200. class SkinStack(list, SkinBase):
  201.     
  202.     def __init__(self, seq):
  203.         list.__init__(self, seq)
  204.         if not all((lambda .0: for elem in .0:
  205. callable(getattr(elem, 'Draw', None)))(seq)):
  206.             raise TypeError('SkinStack must be constructed with .Draw-able elements (you gave %r)' % seq)
  207.         all((lambda .0: for elem in .0:
  208. callable(getattr(elem, 'Draw', None)))(seq))
  209.  
  210.     
  211.     def Draw(self, dc, rect, n = 0):
  212.         for brush in reversed(self):
  213.             brush.Draw(dc, rect, n)
  214.         
  215.  
  216.     
  217.     def GetBitmap(self, size, n = 0):
  218.         tb = TransparentBitmap(size)
  219.         dc = wx.MemoryDC()
  220.         dc.SelectObject(tb)
  221.         for region in reversed(self):
  222.             region.draw(dc, wx.RectS(size), n)
  223.         
  224.         dc.SelectObject(wx.NullBitmap)
  225.         return tb
  226.  
  227.     
  228.     def ytile(self):
  229.         return all((lambda .0: for c in .0:
  230. c.ytile)(self))
  231.  
  232.     ytile = property(ytile)
  233.  
  234.  
  235. class SkinList(list, SkinBase):
  236.     
  237.     def __init__(self, seq):
  238.         list.__init__(self, seq)
  239.         if not all((lambda .0: for elem in .0:
  240. callable(getattr(elem, 'Draw', None)))(seq)):
  241.             raise TypeError('SkinStack must be constructed with .Draw-able elements (you gave %r)' % seq)
  242.         all((lambda .0: for elem in .0:
  243. callable(getattr(elem, 'Draw', None)))(seq))
  244.  
  245.     
  246.     def Draw(self, dc, rect, n = 0):
  247.         self[n % len(self)].Draw(dc, rect, n)
  248.  
  249.     
  250.     def ytile(self):
  251.         return all((lambda .0: for c in .0:
  252. c.ytile)(self))
  253.  
  254.     ytile = property(ytile)
  255.  
  256.  
  257. class SkinGradient(SkinRegion):
  258.     __slots__ = [
  259.         'direction',
  260.         'ytile',
  261.         'colors',
  262.         '_oldrect']
  263.     
  264.     def __init__(self, direction, colors, **opts):
  265.         SkinRegion.__init__(self, **opts)
  266.         self.direction = direction
  267.         if direction == 'horizontal':
  268.             pass
  269.         self.ytile = not (self.border)
  270.         self.colors = colors
  271.         self._oldrect = None
  272.         if self.rounded is not False or len(colors) > 2:
  273.             
  274.             self.Fill = lambda gc, x, y, w, h: wx.GraphicsContext.DrawRoundedRectangle(gc, x, y, w, h, self.rounded)
  275.         else:
  276.             self.Fill = wx.GraphicsContext.DrawRectangle
  277.  
  278.     
  279.     def __repr__(self):
  280.         return '<%s %s %r>' % (self.__class__.__name__, self.direction, self.colors)
  281.  
  282.     
  283.     def _rects(self, therect):
  284.         if therect == self._oldrect:
  285.             return self._oldrects
  286.         x = float(therect.x)
  287.         y = float(therect.y)
  288.         w = float(therect.width)
  289.         h = float(therect.height)
  290.         vert = self.direction == 'vertical'
  291.         p1 = therect == self._oldrect(float if vert else therect.Left)
  292.         lc = len(self.colors) - 1
  293.         dx = None if vert else w / float(lc)
  294.         rects = []
  295.         for i in xrange(0, lc):
  296.             (c1, c2) = self.colors[i:i + 2]
  297.             delta = None if i not in (lc, 0) else 0
  298.             if vert:
  299.                 r = (x, p1, w, dx + delta)
  300.                 gradrect = (x, p1 - delta, x, p1 + dx + delta * 2, c1, c2)
  301.             else:
  302.                 r = (p1, y, dx + delta, h)
  303.                 gradrect = (p1 - delta, y, p1 + dx + delta * 2, y, c1, c2)
  304.             rects.append((gradrect, r))
  305.             p1 = p1 + dx
  306.         
  307.         return rects
  308.  
  309.     
  310.     def Draw(self, dc, therect, n = 0):
  311.         gc = create_gc(dc)
  312.         gc.SetPen(wx.TRANSPARENT_PEN)
  313.         createb = gc.CreateLinearGradientBrush
  314.         gc.Clip(*therect)
  315.         for gradrect, fillrect in self._rects(therect):
  316.             gc.SetBrush(createb(*gradrect))
  317.             self.Fill(gc, *fillrect)
  318.         
  319.         self.Stroke(dc, gc, therect)
  320.  
  321.  
  322.  
  323. class SkinColor(wx.Color, SkinRegion):
  324.     simple = False
  325.     
  326.     def __init__(self, color, **opts):
  327.         if not any((lambda .0: for key, val in .0:
  328. val)(opts.iteritems())):
  329.             c = tuple(color)
  330.             if len(c) < 4 or c[3] == 255:
  331.                 self.simple = True
  332.             
  333.         
  334.         SkinRegion.__init__(self, **opts)
  335.         wx.Color.__init__(self)
  336.         self.Set(*color)
  337.         self.ytile = not (self.border)
  338.  
  339.     
  340.     def Fill(self, dc, rect):
  341.         if self.rounded and not (self.simple):
  342.             dc.DrawRoundedRectangle(*tuple(rect) + (self.rounded,))
  343.         else:
  344.             dc.DrawRectangle(*rect)
  345.  
  346.     
  347.     def __repr__(self):
  348.         return '<%s (%s, %s, %s, %s)>' % ((self.__class__.__name__,) + tuple(self))
  349.  
  350.     
  351.     def Draw(self, dc, rect, n = 0):
  352.         brush = wx.Brush(self)
  353.         if self.simple:
  354.             dc.SetPen(wx.TRANSPARENT_PEN)
  355.             dc.SetBrush(brush)
  356.             dc.DrawRectangle(*rect)
  357.         else:
  358.             gc = create_gc(dc)
  359.             gc.SetBrush(brush)
  360.             gc.SetPen(wx.TRANSPARENT_PEN)
  361.             gc.Clip(*rect)
  362.             self.Fill(gc, rect)
  363.             self.Stroke(dc, gc, rect, n)
  364.  
  365.  
  366. if USE_CGUI_SKIN_OBJECTS:
  367.     from cgui import SkinColor as CGUISkinColor
  368.     from cgui import SkinGradient as CGUISkinGradient
  369.     import gui.toolbox as gui
  370.     for clz in (CGUISkinColor, CGUISkinGradient):
  371.         clz.Show = new.instancemethod(gui.toolbox.ShowImage, None, clz)
  372.     
  373.     from cgui import SkinRegion
  374.     
  375.     def SkinColor(color, **opts):
  376.         color = CGUISkinColor(color)
  377.         set_skin_options(color, opts)
  378.         return color
  379.  
  380.     
  381.     def SkinGradient(direction, colors, **opts):
  382.         direction = None if direction == 'horizontal' else wx.VERTICAL
  383.         gradient = CGUISkinGradient(direction, colors)
  384.         set_skin_options(gradient, opts)
  385.         return gradient
  386.  
  387.     
  388.     def set_skin_options(obj, opts):
  389.         if not opts.get('border'):
  390.             pass
  391.         obj.SetOutline(wx.TRANSPARENT_PEN, opts.get('rounded', 0), opts.get('highlight', False), opts.get('shadow', False))
  392.  
  393.  
  394.