Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Wx NiceButton. By dberube4 aka (Gabriel Dube)
- @Contact : [email protected]
- '''
- import wx
- import wx.lib.newevent
- class NiceButton(wx.Control):
- #Constants
- HIGHLIGHT_NONE = 0
- HIGHLIGHT_BORDER = 1
- HIGHLIGHT_TEXT = 2
- HIGHLIGHT_ALL = 3
- SHAPE_RECTANGLE = 0
- SHAPE_RECTANGLE_ROUNDED = 1
- SHAPE_RECTANGLE_ROUNDED_2 = 2
- SHAPE_RECTANGLE_ROUNDED_3 = 3
- def __init__(self, parent, id = -1, text = '', pos =(0,0) , size = (80, 30)):
- super(NiceButton, self).__init__(parent, id, pos, size, style = wx.NO_BORDER)
- self.SetDoubleBuffered(True)
- #--- Variables ---#
- self.parent = parent
- self.Text = text
- self.Font = wx.Font(9, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False)
- self.palette = {'BackgroundClear': wx.Colour(220, 220, 220),
- 'BackgroundDark': wx.Colour(180, 180, 180),
- 'Foreground': wx.Colour(50, 50, 50),
- 'Highlight': wx.Colour(40, 100, 255),
- 'Shadow' : wx.Colour(140, 140, 140)}
- self.isPressed = False
- self.isHovered = False
- self.HighlightPolicy = self.HIGHLIGHT_BORDER
- self.ShapePolicy = self.SHAPE_RECTANGLE_ROUNDED
- #--- Events ---#
- self.Bind(wx.EVT_PAINT, self.OnPaint)
- self.Bind(wx.EVT_SIZE, self.OnSize)
- self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvent)
- self.Clicked, self.EVT_CLICKED = wx.lib.newevent.NewCommandEvent()
- def OnPaint(self, event):
- dc = wx.PaintDC(self)
- w, h = self.GetSize()
- gc = wx.GraphicsContext.Create(dc)
- if self.isHovered:
- if self.HighlightPolicy == 1 or self.HighlightPolicy == 3:
- gc.SetPen(wx.Pen(self.palette['Highlight'], 1, wx.SOLID))
- else:
- gc.SetPen(wx.Pen(self.palette['Shadow'], 1, wx.SOLID))
- else:
- gc.SetPen(wx.Pen(self.palette['Shadow'], 1, wx.SOLID))
- if self.isPressed:
- brush = gc.CreateLinearGradientBrush(w//2, h, w//2, 0, self.palette['BackgroundClear'], self.palette['BackgroundDark'])
- else:
- brush = gc.CreateLinearGradientBrush(w//2, h, w//2, 0, self.palette['BackgroundDark'], self.palette['BackgroundClear'])
- gc.SetBrush(brush)
- path = gc.CreatePath()
- if self.ShapePolicy == 1:
- path.AddRoundedRectangle(0, 0, w-2, h-2, 5)
- elif self.ShapePolicy == 0:
- path.AddRectangle(0, 0, w-2, h-2)
- elif self.ShapePolicy == 2:
- path.AddRoundedRectangle(0, 0, w-2, h-2, 15)
- elif self.ShapePolicy == 3:
- path.AddRoundedRectangle(0, 0, w-2, h-2, 25)
- gc.DrawPath(path)
- dc.SetFont(self.Font)
- if self.isHovered:
- if self.HighlightPolicy == 2 or self.HighlightPolicy == 3:
- dc.SetTextForeground(self.palette['Highlight'])
- else:
- dc.SetTextForeground(self.palette['Foreground'])
- else:
- dc.SetTextForeground(self.palette['Foreground'])
- x2, y2 = dc.GetTextExtent(self.Text)
- txt = dc.DrawText(self.Text, (w//2)-(x2//2), (h//2)-(y2//2)-1)
- def OnMouseEvent(self, e):
- if e.LeftUp():
- if self.isPressed:
- event = self.Clicked(self.GetId())
- self.GetEventHandler().ProcessEvent(event)
- self.isPressed = False
- self.Refresh()
- elif e.LeftDown():
- self.isPressed = True
- self.Refresh()
- elif e.Entering():
- self.isHovered = True
- self.Refresh()
- elif e.Leaving():
- self.isHovered = False
- self.isPressed = False
- self.Refresh()
- def OnSize(self, event):
- self.Refresh()
Advertisement
Add Comment
Please, Sign In to add comment