+#----------------------------------------------------------------------------
+
+class ColourSelect(wx.BitmapButton):
+ def __init__(self, parent, id, label="", colour=wx.BLACK,
+ pos=wx.DefaultPosition, size=wx.DefaultSize,
+ callback=None, style=0):
+ if label:
+ w, h = parent.GetTextExtent(label)
+ w += 6
+ h += 6
+ else:
+ w, h = 20, 20
+ wx.BitmapButton.__init__(self, parent, id, wx.EmptyBitmap(w,h),
+ pos=pos, size=size, style=style|wx.BU_AUTODRAW)
+
+ if type(colour) == type( () ):
+ colour = wx.Colour(*colour)
+ self.colour = colour
+ self.SetLabel(label)
+ self.callback = callback
+ bmp = self.MakeBitmap()
+ self.SetBitmap(bmp)
+ parent.Bind(wx.EVT_BUTTON, self.OnClick, self)
+
+
+ def GetColour(self):
+ return self.colour
+
+ def GetValue(self):
+ return self.colour
+
+ def SetValue(self, colour):
+ self.SetColour(colour)
+
+ def SetColour(self, colour):
+ if type(colour) == tuple:
+ colour = wx.Colour(*colour)
+ if type(colour) == str:
+ colour = wx.NamedColour(colour)
+
+ self.colour = colour
+ bmp = self.MakeBitmap()
+ self.SetBitmap(bmp)
+
+
+ def SetLabel(self, label):
+ self.label = label
+
+ def GetLabel(self):
+ return self.label
+
+
+ def MakeBitmap(self):
+ bdr = 8
+ width, height = self.GetSize()
+
+ # yes, this is weird, but it appears to work around a bug in wxMac
+ if "wxMac" in wx.PlatformInfo and width == height:
+ height -= 1
+
+ bmp = wx.EmptyBitmap(width-bdr, height-bdr)
+ dc = wx.MemoryDC()
+ dc.SelectObject(bmp)
+ dc.SetFont(self.GetFont())
+ label = self.GetLabel()
+ # Just make a little colored bitmap
+ dc.SetBackground(wx.Brush(self.colour))
+ dc.Clear()
+
+ if label:
+ # Add a label to it
+ avg = reduce(lambda a, b: a + b, self.colour.Get()) / 3
+ fcolour = avg > 128 and wx.BLACK or wx.WHITE
+ dc.SetTextForeground(fcolour)
+ dc.DrawLabel(label, (0,0, width-bdr, height-bdr),
+ wx.ALIGN_CENTER)
+
+ dc.SelectObject(wx.NullBitmap)
+ return bmp
+
+
+ def SetBitmap(self, bmp):
+ self.SetBitmapLabel(bmp)
+ self.SetBitmapSelected(bmp)
+ self.SetBitmapDisabled(bmp)
+ self.SetBitmapFocus(bmp)
+ self.SetBitmapSelected(bmp)
+ self.Refresh()
+