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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. from __future__ import division
  6. import wx
  7. from util 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.AddGrowableCol(1, 1)
  26.         self.AddGrowableRow(1, 1)
  27.         self.Add(content, (1, 1), flag = wx.EXPAND)
  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.         
  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.         
  208.  
  209.     
  210.     def Draw(self, dc, rect, n = 0):
  211.         for brush in reversed(self):
  212.             brush.Draw(dc, rect, n)
  213.         
  214.  
  215.     
  216.     def GetBitmap(self, size, n = 0):
  217.         tb = TransparentBitmap(size)
  218.         dc = wx.MemoryDC()
  219.         dc.SelectObject(tb)
  220.         for region in reversed(self):
  221.             region.draw(dc, wx.RectS(size), n)
  222.         
  223.         dc.SelectObject(wx.NullBitmap)
  224.         return tb
  225.  
  226.     
  227.     def ytile(self):
  228.         return all((lambda .0: for c in .0:
  229. c.ytile)(self))
  230.  
  231.     ytile = property(ytile)
  232.  
  233.  
  234. class SkinList(list, SkinBase):
  235.     
  236.     def __init__(self, seq):
  237.         list.__init__(self, seq)
  238.         if not all((lambda .0: for elem in .0:
  239. callable(getattr(elem, 'Draw', None)))(seq)):
  240.             raise TypeError('SkinStack must be constructed with .Draw-able elements (you gave %r)' % seq)
  241.         
  242.  
  243.     
  244.     def Draw(self, dc, rect, n = 0):
  245.         self[n % len(self)].Draw(dc, rect, n)
  246.  
  247.     
  248.     def ytile(self):
  249.         return all((lambda .0: for c in .0:
  250. c.ytile)(self))
  251.  
  252.     ytile = property(ytile)
  253.  
  254.  
  255. class SkinGradient(SkinRegion):
  256.     __slots__ = [
  257.         'direction',
  258.         'ytile',
  259.         'colors',
  260.         '_oldrect']
  261.     
  262.     def __init__(self, direction, colors, **opts):
  263.         SkinRegion.__init__(self, **opts)
  264.         self.direction = direction
  265.         if direction == 'horizontal':
  266.             pass
  267.         self.ytile = not (self.border)
  268.         self.colors = colors
  269.         self._oldrect = None
  270.         if self.rounded is not False or len(colors) > 2:
  271.             
  272.             self.Fill = lambda gc, x, y, w, h: wx.GraphicsContext.DrawRoundedRectangle(gc, x, y, w, h, self.rounded)
  273.         else:
  274.             self.Fill = wx.GraphicsContext.DrawRectangle
  275.  
  276.     
  277.     def __repr__(self):
  278.         return '<%s %s %r>' % (self.__class__.__name__, self.direction, self.colors)
  279.  
  280.     
  281.     def _rects(self, therect):
  282.         if therect == self._oldrect:
  283.             return self._oldrects
  284.         
  285.         x = float(therect.x)
  286.         y = float(therect.y)
  287.         w = float(therect.width)
  288.         h = float(therect.height)
  289.         vert = self.direction == 'vertical'
  290.         p1 = None(float if vert else therect.Left)
  291.         lc = len(self.colors) - 1
  292.         dx = None if vert else w / float(lc)
  293.         rects = []
  294.         for i in xrange(0, lc):
  295.             (c1, c2) = self.colors[i:i + 2]
  296.             delta = None if i not in (lc, 0) else 0
  297.             if vert:
  298.                 r = (x, p1, w, dx + delta)
  299.                 gradrect = (x, p1 - delta, x, p1 + dx + delta * 2, c1, c2)
  300.             else:
  301.                 r = (p1, y, dx + delta, h)
  302.                 gradrect = (p1 - delta, y, p1 + dx + delta * 2, y, c1, c2)
  303.             rects.append((gradrect, r))
  304.             p1 = p1 + dx
  305.         
  306.         return rects
  307.  
  308.     
  309.     def Draw(self, dc, therect, n = 0):
  310.         gc = create_gc(dc)
  311.         gc.SetPen(wx.TRANSPARENT_PEN)
  312.         createb = gc.CreateLinearGradientBrush
  313.         gc.Clip(*therect)
  314.         for gradrect, fillrect in self._rects(therect):
  315.             gc.SetBrush(createb(*gradrect))
  316.             self.Fill(gc, *fillrect)
  317.         
  318.         self.Stroke(dc, gc, therect)
  319.  
  320.  
  321.  
  322. class SkinColor(wx.Color, SkinRegion):
  323.     simple = False
  324.     
  325.     def __init__(self, color, **opts):
  326.         if not any((lambda .0: for key, val in .0:
  327. val)(opts.iteritems())):
  328.             c = tuple(color)
  329.             if len(c) < 4 or c[3] == 255:
  330.                 self.simple = True
  331.             
  332.         
  333.         SkinRegion.__init__(self, **opts)
  334.         wx.Color.__init__(self)
  335.         self.Set(*color)
  336.         self.ytile = not (self.border)
  337.  
  338.     
  339.     def Fill(self, dc, rect):
  340.         if self.rounded and not (self.simple):
  341.             dc.DrawRoundedRectangle(*tuple(rect) + (self.rounded,))
  342.         else:
  343.             dc.DrawRectangle(*rect)
  344.  
  345.     
  346.     def __repr__(self):
  347.         return '<%s (%s, %s, %s, %s)>' % ((self.__class__.__name__,) + tuple(self))
  348.  
  349.     
  350.     def Draw(self, dc, rect, n = 0):
  351.         brush = wx.Brush(self)
  352.         if self.simple:
  353.             dc.SetPen(wx.TRANSPARENT_PEN)
  354.             dc.SetBrush(brush)
  355.             dc.DrawRectangle(*rect)
  356.         else:
  357.             gc = create_gc(dc)
  358.             gc.SetBrush(brush)
  359.             gc.SetPen(wx.TRANSPARENT_PEN)
  360.             gc.Clip(*rect)
  361.             self.Fill(gc, rect)
  362.             self.Stroke(dc, gc, rect, n)
  363.  
  364.  
  365. if USE_CGUI_SKIN_OBJECTS:
  366.     from cgui import SkinColor as CGUISkinColor
  367.     from cgui import SkinGradient as CGUISkinGradient
  368.     import gui.toolbox as gui
  369.     for clz in (CGUISkinColor, CGUISkinGradient):
  370.         clz.Show = new.instancemethod(gui.toolbox.ShowImage, None, clz)
  371.     
  372.     from cgui import SkinRegion
  373.     
  374.     def SkinColor(color, **opts):
  375.         color = CGUISkinColor(color)
  376.         set_skin_options(color, opts)
  377.         return color
  378.  
  379.     
  380.     def SkinGradient(direction, colors, **opts):
  381.         direction = None if direction == 'horizontal' else wx.VERTICAL
  382.         gradient = CGUISkinGradient(direction, colors)
  383.         set_skin_options(gradient, opts)
  384.         return gradient
  385.  
  386.     
  387.     def set_skin_options(obj, opts):
  388.         if not opts.get('border'):
  389.             pass
  390.         obj.SetOutline(wx.TRANSPARENT_PEN, opts.get('rounded', 0), opts.get('highlight', False), opts.get('shadow', False))
  391.  
  392.  
  393.