home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / gui / uberwidgets / ubutton.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  11.1 KB  |  335 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import wx
  5. from wx import Size
  6. from gui.skin import get as skinget
  7. from gui.uberwidgets.ucontrol import UControl
  8. from time import time
  9.  
  10. class UButton(UControl):
  11.     
  12.     def __init__(self, parent, id = -1, skinkey = 'Button', label = '', bitmap = None, bitmapSize = None, menuMode = False, callback = None, style = wx.BU_LEFT):
  13.         self.bitmap = bitmap
  14.         self.bitmapSize = bitmapSize
  15.         self.Active = False
  16.         self.menuMode = menuMode
  17.         UControl.__init__(self, parent, skinkey, id, label, style = style | wx.NO_BORDER | wx.FULL_REPAINT_ON_RESIZE)
  18.         self.BindHover(self.OnHover)
  19.         self.BindFocus(self.OnFocus)
  20.         Bind = self.Bind
  21.         Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
  22.         Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
  23.         Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
  24.         Bind(wx.EVT_KEY_UP, self.OnKeyUp)
  25.         Bind((wx.EVT_SIZE,), (lambda e: self.Cut(self.BackgroundRegion.GetBitmap(e.Size))))
  26.         self.UpdateSkin()
  27.         if 'wxMSW' in wx.PlatformInfo:
  28.             self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDown)
  29.         
  30.         if callback is not None:
  31.             self.Bind(wx.EVT_BUTTON, callback)
  32.         
  33.  
  34.     
  35.     def __repr__(self):
  36.         return '<%s "%s">' % (self.__class__.__name__, self.LabelText)
  37.  
  38.     
  39.     def OnFocus(self, focused):
  40.         if self._native and focused:
  41.             self._nativebutton.SetFocus()
  42.         
  43.         if self.Active and not focused:
  44.             self.Active = False
  45.             self.ChooseBG()
  46.         
  47.         self.Refresh(False)
  48.  
  49.     
  50.     def Enable(self, val):
  51.         UControl.Enable(self, val)
  52.         self.ChooseBG()
  53.         self.Refresh(False)
  54.  
  55.     
  56.     def OnKeyDown(self, e):
  57.         e.Skip()
  58.         if self._native and not (self.Enabled) or not self.IsShown():
  59.             return None
  60.         if e.KeyCode == wx.WXK_SPACE:
  61.             self.OnLeftDown()
  62.         
  63.  
  64.     
  65.     def OnKeyUp(self, e):
  66.         e.Skip()
  67.         if self._native and not (self.Enabled) or not self.IsShown():
  68.             return None
  69.         c = e.KeyCode
  70.         if c == wx.WXK_SPACE:
  71.             self.OnLeftUp()
  72.         
  73.  
  74.     
  75.     def OnLeftDown(self, e = None):
  76.         if e:
  77.             e.Skip()
  78.         
  79.         if self._native and not (self.Enabled) or not self.IsShown():
  80.             return None
  81.         if not self.menuMode:
  82.             if isinstance(e, wx.MouseEvent):
  83.                 self.CaptureMouse()
  84.             
  85.             if self.FindFocus() is not self:
  86.                 self.SetFocus()
  87.             
  88.         else:
  89.             self._fire()
  90.         self.Active = True
  91.         self.ChooseBG()
  92.         self.Refresh(False)
  93.  
  94.     
  95.     def OnLeftUp(self, e = None, fire = True):
  96.         if e:
  97.             e.Skip()
  98.         
  99.         if self._native or not self.IsEnabled():
  100.             return None
  101.         while self.HasCapture():
  102.             self.ReleaseMouse()
  103.             continue
  104.             not self.IsEnabled()
  105.         if self.Active and fire and not (self.menuMode):
  106.             self._fire()
  107.         
  108.         self.Active = False
  109.         self.ChooseBG()
  110.         self.Refresh(False)
  111.  
  112.     
  113.     def OnHover(self, hover):
  114.         if self.HasCapture():
  115.             self.Active = hover
  116.         
  117.         self.ChooseBG()
  118.         self.Refresh(False)
  119.  
  120.     
  121.     def ChooseBG(self):
  122.         bgs = self.bgs
  123.         if not self.IsEnabled():
  124.             bg = 'disabled'
  125.         else:
  126.             bg = ''
  127.             if self.menuMode:
  128.                 if self.Active or self.Hover:
  129.                     bg = 'hover'
  130.                 
  131.             elif self.Active:
  132.                 bg += 'active'
  133.             
  134.             if self.Hover:
  135.                 bg += 'hover'
  136.             
  137.             if not bg:
  138.                 bg = 'normal'
  139.             
  140.         newbg = getattr(bgs, bg)
  141.         if newbg is not getattr(self, 'BackgroundRegion', None):
  142.             self.BackgroundRegion = newbg
  143.         
  144.         return bg
  145.  
  146.     
  147.     def UpdateSkin(self):
  148.         UControl.UpdateSkin(self)
  149.         if skinget('Button.Native', False):
  150.             self.Native = True
  151.         else:
  152.             self.bgs = skinget('Button.Backgrounds')
  153.             self.ChooseBG()
  154.         font = skinget('Button.Font', None)
  155.         if isinstance(font, wx.Font):
  156.             self.Font = font
  157.         
  158.  
  159.     
  160.     def GetDefaultAttributes(self):
  161.         return wx.Button.GetClassDefaultAttributes()
  162.  
  163.     
  164.     def GetContentSize(self):
  165.         dc = wx.ClientDC(self)
  166.         dc.Font = self.Font
  167.         size = Size(*dc.GetTextExtent(self.LabelText))
  168.         b = self.bitmap
  169.         s = self.WindowStyle
  170.         padding = self.Padding
  171.         if b is not None:
  172.             (imgw, imgh) = None if self.bitmapSize is not None else (b.Width, b.Height)
  173.             if s & wx.BU_LEFT or s & wx.BU_RIGHT:
  174.                 size.IncBy(padding.width + imgw, 0)
  175.                 size = Size(size.width, max(imgh, size.height))
  176.             else:
  177.                 size.IncBy(0, padding.height + imgh)
  178.                 size = Size(max(imgw, size.width), size.height)
  179.         
  180.         size.IncBy(padding.width * 2, padding.height * 2)
  181.         if not s & wx.BU_EXACTFIT:
  182.             size = Size(max(size.width, 65), max(size.height, 17))
  183.         
  184.         return size
  185.  
  186.     
  187.     def _fire(self, e = None):
  188.         if e:
  189.             e.Skip()
  190.         
  191.         evt = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, self.Id)
  192.         evt.SetTimestamp(time())
  193.         evt.SetEventObject(self)
  194.         self.AddPendingEvent(evt)
  195.  
  196.     
  197.     def Draw(self, dc, rect):
  198.         padding = self.Padding
  199.         rect.Deflate(padding.x, padding.y)
  200.         dc.Font = self.Font
  201.         if not self.IsEnabled():
  202.             fg = 'disabled'
  203.         else:
  204.             fg = ''
  205.             if self.Active:
  206.                 fg += 'active'
  207.             
  208.             if self.Hover:
  209.                 fg += 'hover'
  210.             
  211.             if not fg:
  212.                 fg = 'normal'
  213.             
  214.         dc.SetTextForeground(self.skin.fontcolors.get(fg, self.ForegroundColour))
  215.         s = self.WindowStyle
  216.         bitmap = self.Bitmap
  217.         bitmapSize = self.BitmapSize
  218.         bitmapSize = self.BitmapSize
  219.         if self.FindFocus() is self and not (self.menuMode):
  220.             self.DrawFocusRect(dc, rect)
  221.         
  222.         if bitmap is not None:
  223.             bitmap = bitmap.Resized(self.BitmapSize)
  224.             if s & wx.BU_LEFT:
  225.                 dc.DrawBitmap(bitmap, rect.Left, rect.Top + rect.Height / 2 - bitmapSize.height / 2)
  226.                 rect.Subtract(left = bitmapSize.width + padding.width)
  227.             elif s & wx.BU_RIGHT:
  228.                 dc.DrawBitmap(bitmap, rect.Right - bitmapSize.width, rect.Top + rect.Height / 2 - bitmapSize.height / 2)
  229.                 rect.Subtract(right = bitmapSize.width + padding.width)
  230.             
  231.         
  232.         dc.DrawLabel(self.LabelText, rect, indexAccel = self.Label.find('&'), alignment = wx.ALIGN_CENTER)
  233.  
  234.     
  235.     def AcceptsFocus(self):
  236.         if self.IsShown() and self.IsEnabled():
  237.             pass
  238.         return not (self.menuMode)
  239.  
  240.     
  241.     def SetLabel(self, newLabel):
  242.         oldLabel = self.Label
  243.         i = oldLabel.find('&')
  244.         if i != -1 and i < len(oldLabel) - 1 and oldLabel[i + 1] != '&':
  245.             self.KeyCatcher.RemoveDown('alt+%s' % newLabel[i + 1], oldLabel[i + 1])
  246.         
  247.         i = newLabel.find('&')
  248.         if i != -1 and i < len(newLabel) - 1 and newLabel[i + 1] != '&':
  249.             print self, 'adding alt+%s' % newLabel[i + 1]
  250.             self.KeyCatcher.OnDown('alt+%s' % str(newLabel[i + 1]), self._fire)
  251.         
  252.         UControl.SetLabel(self, newLabel)
  253.  
  254.     Label = property(UControl.GetLabel, SetLabel)
  255.     
  256.     def SetBitmap(self, bitmap):
  257.         self.bitmap = bitmap
  258.         self.Refresh(False)
  259.  
  260.     Bitmap = property((lambda self: self.bitmap), SetBitmap)
  261.     
  262.     def SetBitmapSize(self, size):
  263.         self.bitmapSize = size
  264.  
  265.     
  266.     def GetBitmapSize(self):
  267.         if self.bitmapSize is not None:
  268.             return self.bitmapSize
  269.         if self.bitmap is not None:
  270.             return Size(self.bitmap.Width, self.bitmap.Height)
  271.         return None
  272.  
  273.     BitmapSize = property(GetBitmapSize, SetBitmapSize)
  274.     
  275.     def SetNative(self, native):
  276.         if native and not (self._native):
  277.             print self.Id
  278.             self._nativebutton = wx.Button(self, self.Id, label = self.Label, size = self.Size, pos = self.Position, style = self.WindowStyle)
  279.             self._nativebutton.SetFocus()
  280.         elif not native and self._native:
  281.             self._nativebutton.Destroy()
  282.         
  283.         if self._native != native:
  284.             self.Parent.Layout()
  285.             self.Parent.Refresh()
  286.         
  287.         self._native = native
  288.  
  289.     Native = property((lambda self: self._native), SetNative)
  290.  
  291. if __name__ == '__main__':
  292.     from tests.testapp import testapp
  293.     from gui import skin
  294.     a = testapp('../../..')
  295.     f = wx.Frame(None, style = wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
  296.     p = wx.Panel(f)
  297.     p.Sizer = s = wx.BoxSizer(wx.VERTICAL)
  298.     bitmap = skin.load_bitmap('error.png')
  299.     b = UButton(p, label = '&Digsby', bitmap = bitmap)
  300.     s.Add(b)
  301.     b.Bind(wx.EVT_BUTTON, (lambda e, but = b: setattr(but, 'Native', not getattr(but, 'Native'))))
  302.     b = UButton(p, label = 'Digsby &Rocks', bitmap = bitmap)
  303.     s.Add(b)
  304.     b = UButton(p, label = '&Exact Fit', style = wx.BU_EXACTFIT)
  305.     s.Add(b)
  306.     b = UButton(p, label = '&Cancel')
  307.     s.Add(b)
  308.     b = UButton(p, label = 'Disab&led')
  309.     s.Add(b)
  310.     b.Enable(False)
  311.     b = UButton(p, label = 'Digs&by', bitmap = bitmap, style = wx.BU_RIGHT, menuMode = True)
  312.     s.Add(b, 0, wx.EXPAND)
  313.     b = UButton(p, label = 'Digsby Rocks', bitmap = bitmap, style = wx.BU_RIGHT)
  314.     s.Add(b, 0, wx.EXPAND)
  315.     b = UButton(p, label = '&OK')
  316.     s.Add(b, 0, wx.EXPAND)
  317.     b = UButton(p, label = 'Cancel')
  318.     s.Add(b, 0, wx.EXPAND)
  319.     s.AddSpacer((30, 30))
  320.     from gui.uberwidgets.UberButton import UberButton
  321.     hs = wx.BoxSizer(wx.HORIZONTAL)
  322.     for x in xrange(4):
  323.         b = UberButton(p, label = 'UberButton')
  324.         hs.Add(b, 1, wx.EXPAND)
  325.     
  326.     s.Add(hs, 0, wx.EXPAND)
  327.     
  328.     def printsrc(e):
  329.         print e.EventObject
  330.  
  331.     p.Bind(wx.EVT_BUTTON, printsrc)
  332.     f.Show()
  333.     a.MainLoop()
  334.  
  335.