dberube4

Nice Button Source

Feb 24th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | None | 0 0
  1. '''
  2. Wx NiceButton. By dberube4 aka (Gabriel Dube)
  3. '''
  4.  
  5. import wx
  6. import wx.lib.newevent
  7.  
  8. class NiceButton(wx.Control):
  9.    
  10.     #Constants
  11.    
  12.     HIGHLIGHT_NONE = 0
  13.     HIGHLIGHT_BORDER = 1
  14.     HIGHLIGHT_TEXT = 2
  15.     HIGHLIGHT_ALL = 3
  16.    
  17.     SHAPE_RECTANGLE = 0
  18.     SHAPE_RECTANGLE_ROUNDED = 1
  19.     SHAPE_RECTANGLE_ROUNDED_2 = 2
  20.     SHAPE_RECTANGLE_ROUNDED_3 = 3
  21.    
  22.     def __init__(self, parent, id = -1, text = '', pos =(0,0) , size = (80, 30)):
  23.        
  24.         super(NiceButton, self).__init__(parent, id, pos, size, style = wx.NO_BORDER)
  25.         self.SetDoubleBuffered(True)
  26.        
  27.         #--- Variables ---#
  28.         self.parent = parent
  29.         self.Text = text
  30.         self.Font = wx.Font(9, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False)
  31.         self.palette = {'BackgroundClear': wx.Colour(220, 220, 220),
  32.                         'BackgroundDark': wx.Colour(180, 180, 180),
  33.                         'Foreground': wx.Colour(50, 50, 50),
  34.                         'Highlight': wx.Colour(40, 100, 255),
  35.                         'Shadow' : wx.Colour(140, 140, 140)}
  36.        
  37.         self.isPressed = False
  38.         self.isHovered = False
  39.         self.HighlightPolicy = self.HIGHLIGHT_BORDER
  40.         self.ShapePolicy = self.SHAPE_RECTANGLE_ROUNDED
  41.        
  42.         #--- Events ---#
  43.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  44.         self.Bind(wx.EVT_SIZE, self.OnSize)
  45.         self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvent)
  46.        
  47.         self.Clicked, self.EVT_CLICKED = wx.lib.newevent.NewCommandEvent()
  48.    
  49.     def OnPaint(self, event):
  50.        
  51.         dc = wx.PaintDC(self)
  52.         w, h = self.GetSize()
  53.        
  54.         gc = wx.GraphicsContext.Create(dc)
  55.        
  56.         if self.isHovered:
  57.             if self.HighlightPolicy == 1 or self.HighlightPolicy == 3:
  58.                 gc.SetPen(wx.Pen(self.palette['Highlight'], 1, wx.SOLID))
  59.             else:
  60.                 gc.SetPen(wx.Pen(self.palette['Shadow'], 1, wx.SOLID))
  61.         else:
  62.             gc.SetPen(wx.Pen(self.palette['Shadow'], 1, wx.SOLID))
  63.        
  64.         if self.isPressed:
  65.             brush = gc.CreateLinearGradientBrush(w//2, h, w//2, 0, self.palette['BackgroundClear'], self.palette['BackgroundDark'])
  66.         else:
  67.             brush = gc.CreateLinearGradientBrush(w//2, h, w//2, 0, self.palette['BackgroundDark'], self.palette['BackgroundClear'])
  68.            
  69.         gc.SetBrush(brush)
  70.        
  71.         path = gc.CreatePath()
  72.        
  73.         if self.ShapePolicy == 1:
  74.             path.AddRoundedRectangle(0, 0, w-2, h-2, 5)
  75.            
  76.         elif self.ShapePolicy == 0:
  77.             path.AddRectangle(0, 0, w-2, h-2)
  78.            
  79.         elif self.ShapePolicy == 2:
  80.             path.AddRoundedRectangle(0, 0, w-2, h-2, 15)
  81.            
  82.         elif self.ShapePolicy == 3:
  83.             path.AddRoundedRectangle(0, 0, w-2, h-2, 25)
  84.        
  85.         gc.DrawPath(path)
  86.  
  87.         dc.SetFont(self.Font)
  88.        
  89.         if self.isHovered:
  90.             if self.HighlightPolicy == 2 or self.HighlightPolicy == 3:
  91.                     dc.SetTextForeground(self.palette['Highlight'])
  92.             else:
  93.                 dc.SetTextForeground(self.palette['Foreground'])
  94.         else:
  95.             dc.SetTextForeground(self.palette['Foreground'])
  96.            
  97.         x2, y2 = dc.GetTextExtent(self.Text)
  98.         txt = dc.DrawText(self.Text, (w//2)-(x2//2), (h//2)-(y2//2)-1)
  99.    
  100.     def OnMouseEvent(self, e):
  101.        
  102.         if e.LeftUp():
  103.            
  104.             if self.isPressed:
  105.                 event = self.Clicked(self.GetId())
  106.                 self.GetEventHandler().ProcessEvent(event)
  107.            
  108.             self.isPressed = False
  109.             self.Refresh()
  110.  
  111.         elif e.LeftDown():
  112.             self.isPressed = True
  113.             self.Refresh()
  114.            
  115.         elif e.Entering():
  116.             self.isHovered = True
  117.             self.Refresh()
  118.            
  119.         elif e.Leaving():
  120.             self.isHovered = False
  121.             self.isPressed = False
  122.             self.Refresh()
  123.  
  124.     def OnSize(self, event):
  125.         self.Refresh()
Advertisement
Add Comment
Please, Sign In to add comment