]> git.saurik.com Git - wxWidgets.git/blame - wxPython/wx/lib/colourselect.py
Themed backgrounds for flat generic buttons
[wxWidgets.git] / wxPython / wx / lib / colourselect.py
CommitLineData
d14a1e28
RD
1#----------------------------------------------------------------------------
2# Name: ColourSelect.py
3# Purpose: Colour Box Selection Control
4#
5# Author: Lorne White, Lorne.White@telusplanet.net
6#
7# Created: Feb 25, 2001
8# Licence: wxWindows license
9#----------------------------------------------------------------------------
1fded56b 10
d14a1e28
RD
11# creates a colour wxButton with selectable color
12# button click provides a colour selection box
13# button colour will change to new colour
14# GetColour method to get the selected colour
15
16# Updates:
17# call back to function if changes made
18
19# Cliff Wells, logiplexsoftware@earthlink.net:
20# - Made ColourSelect into "is a button" rather than "has a button"
21# - Added label parameter and logic to adjust the label colour according to the background
22# colour
23# - Added id argument
24# - Rearranged arguments to more closely follow wx conventions
25# - Simplified some of the code
26
27# Cliff Wells, 2002/02/07
28# - Added ColourSelect Event
29
b881fc78
RD
30# 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
31#
32# o Updated for 2.5 compatability.
33#
34
4e5d278c
RD
35"""
36Provides a `ColourSelect` button that, when clicked, will display a
37colour selection dialog. The selected colour is displayed on the
38button itself.
39"""
40
b881fc78
RD
41#----------------------------------------------------------------------------
42
43import wx
d14a1e28 44
b881fc78
RD
45#----------------------------------------------------------------------------
46
47wxEVT_COMMAND_COLOURSELECT = wx.NewEventType()
48
49class ColourSelectEvent(wx.PyCommandEvent):
7722248d 50 def __init__(self, id, value):
b881fc78
RD
51 wx.PyCommandEvent.__init__(self, id = id)
52 self.SetEventType(wxEVT_COMMAND_COLOURSELECT)
7722248d 53 self.value = value
d14a1e28 54
7722248d
RD
55 def GetValue(self):
56 return self.value
d14a1e28 57
b881fc78 58EVT_COLOURSELECT = wx.PyEventBinder(wxEVT_COMMAND_COLOURSELECT, 1)
7722248d 59
b881fc78 60#----------------------------------------------------------------------------
7722248d 61
b881fc78
RD
62class ColourSelect(wx.BitmapButton):
63 def __init__(self, parent, id, label="", colour=wx.BLACK,
64 pos=wx.DefaultPosition, size=wx.DefaultSize,
7722248d
RD
65 callback=None, style=0):
66 if label:
67 w, h = parent.GetTextExtent(label)
68 w += 6
69 h += 6
70 else:
71 w, h = 20, 20
b881fc78
RD
72 wx.BitmapButton.__init__(self, parent, id, wx.EmptyBitmap(w,h),
73 pos=pos, size=size, style=style|wx.BU_AUTODRAW)
74
7722248d 75 if type(colour) == type( () ):
b881fc78 76 colour = wx.Colour(*colour)
7722248d
RD
77 self.colour = colour
78 self.SetLabel(label)
d14a1e28 79 self.callback = callback
7722248d
RD
80 bmp = self.MakeBitmap()
81 self.SetBitmap(bmp)
b881fc78 82 parent.Bind(wx.EVT_BUTTON, self.OnClick, self)
d14a1e28 83
7722248d 84
d14a1e28 85 def GetColour(self):
7722248d 86 return self.colour
d14a1e28 87
7722248d
RD
88 def GetValue(self):
89 return self.colour
90
7722248d
RD
91 def SetValue(self, colour):
92 self.SetColour(colour)
93
7722248d 94 def SetColour(self, colour):
a27d1f42
RD
95 if type(colour) == tuple:
96 colour = wx.Colour(*colour)
97 if type(colour) == str:
98 colour = wx.NamedColour(colour)
b881fc78 99
7722248d
RD
100 self.colour = colour
101 bmp = self.MakeBitmap()
102 self.SetBitmap(bmp)
103
104
edf3b4dc
RD
105 def SetLabel(self, label):
106 self.label = label
107
108 def GetLabel(self):
109 return self.label
110
111
7722248d 112 def MakeBitmap(self):
1b722770 113 bdr = 8
b881fc78 114 width, height = self.GetSize()
89977ceb
RD
115
116 # yes, this is weird, but it appears to work around a bug in wxMac
117 if "wxMac" in wx.PlatformInfo and width == height:
118 height -= 1
119
b881fc78
RD
120 bmp = wx.EmptyBitmap(width-bdr, height-bdr)
121 dc = wx.MemoryDC()
7722248d 122 dc.SelectObject(bmp)
6dfb13b1 123 dc.SetFont(self.GetFont())
7722248d
RD
124 label = self.GetLabel()
125 # Just make a little colored bitmap
b881fc78 126 dc.SetBackground(wx.Brush(self.colour))
7722248d 127 dc.Clear()
b881fc78 128
7722248d
RD
129 if label:
130 # Add a label to it
131 avg = reduce(lambda a, b: a + b, self.colour.Get()) / 3
b881fc78 132 fcolour = avg > 128 and wx.BLACK or wx.WHITE
7722248d 133 dc.SetTextForeground(fcolour)
b881fc78
RD
134 dc.DrawLabel(label, (0,0, width-bdr, height-bdr),
135 wx.ALIGN_CENTER)
7722248d 136
b881fc78 137 dc.SelectObject(wx.NullBitmap)
7722248d
RD
138 return bmp
139
140
141 def SetBitmap(self, bmp):
142 self.SetBitmapLabel(bmp)
143 self.SetBitmapSelected(bmp)
144 self.SetBitmapDisabled(bmp)
145 self.SetBitmapFocus(bmp)
146 self.SetBitmapSelected(bmp)
4e4a10ba
RD
147 self.Refresh()
148
d14a1e28
RD
149
150 def OnChange(self):
b881fc78 151 wx.PostEvent(self, ColourSelectEvent(self.GetId(), self.GetValue()))
d14a1e28
RD
152 if self.callback is not None:
153 self.callback()
154
155 def OnClick(self, event):
b881fc78 156 data = wx.ColourData()
d14a1e28 157 data.SetChooseFull(True)
7722248d 158 data.SetColour(self.colour)
bbc5a006 159 dlg = wx.ColourDialog(wx.GetTopLevelParent(self), data)
b881fc78
RD
160 changed = dlg.ShowModal() == wx.ID_OK
161
d14a1e28
RD
162 if changed:
163 data = dlg.GetColourData()
7722248d 164 self.SetColour(data.GetColour())
d14a1e28
RD
165 dlg.Destroy()
166
b881fc78 167 # moved after dlg.Destroy, since who knows what the callback will do...
d14a1e28 168 if changed:
b881fc78 169 self.OnChange()
1fded56b 170