1 #----------------------------------------------------------------------------
2 # Name: ColourSelect.py
3 # Purpose: Colour Box Selection Control
5 # Author: Lorne White, Lorne.White@telusplanet.net
7 # Created: Feb 25, 2001
8 # Licence: wxWindows license
9 #----------------------------------------------------------------------------
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
17 # call back to function if changes made
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
24 # - Rearranged arguments to more closely follow wx conventions
25 # - Simplified some of the code
27 # Cliff Wells, 2002/02/07
28 # - Added ColourSelect Event
30 # 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
32 # o Updated for 2.5 compatability.
35 #----------------------------------------------------------------------------
39 #----------------------------------------------------------------------------
41 wxEVT_COMMAND_COLOURSELECT
= wx
.NewEventType()
43 class ColourSelectEvent(wx
.PyCommandEvent
):
44 def __init__(self
, id, value
):
45 wx
.PyCommandEvent
.__init
__(self
, id = id)
46 self
.SetEventType(wxEVT_COMMAND_COLOURSELECT
)
52 EVT_COLOURSELECT
= wx
.PyEventBinder(wxEVT_COMMAND_COLOURSELECT
, 1)
54 #----------------------------------------------------------------------------
56 class ColourSelect(wx
.BitmapButton
):
57 def __init__(self
, parent
, id, label
="", colour
=wx
.BLACK
,
58 pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
,
59 callback
=None, style
=0):
61 w
, h
= parent
.GetTextExtent(label
)
66 wx
.BitmapButton
.__init
__(self
, parent
, id, wx
.EmptyBitmap(w
,h
),
67 pos
=pos
, size
=size
, style
=style|wx
.BU_AUTODRAW
)
69 if type(colour
) == type( () ):
70 colour
= wx
.Colour(*colour
)
73 self
.callback
= callback
74 bmp
= self
.MakeBitmap()
76 parent
.Bind(wx
.EVT_BUTTON
, self
.OnClick
, self
)
85 def SetValue(self
, colour
):
86 self
.SetColour(colour
)
88 def SetColour(self
, colour
):
89 if type(colour
) == type( () ):
90 colour
= wxColour(*colour
)
93 bmp
= self
.MakeBitmap()
99 width
, height
= self
.GetSize()
100 bmp
= wx
.EmptyBitmap(width
-bdr
, height
-bdr
)
103 label
= self
.GetLabel()
104 # Just make a little colored bitmap
105 dc
.SetBackground(wx
.Brush(self
.colour
))
110 avg
= reduce(lambda a
, b
: a
+ b
, self
.colour
.Get()) / 3
111 fcolour
= avg
> 128 and wx
.BLACK
or wx
.WHITE
112 dc
.SetTextForeground(fcolour
)
113 dc
.DrawLabel(label
, (0,0, width
-bdr
, height
-bdr
),
116 dc
.SelectObject(wx
.NullBitmap
)
120 def SetBitmap(self
, bmp
):
121 self
.SetBitmapLabel(bmp
)
122 self
.SetBitmapSelected(bmp
)
123 self
.SetBitmapDisabled(bmp
)
124 self
.SetBitmapFocus(bmp
)
125 self
.SetBitmapSelected(bmp
)
129 wx
.PostEvent(self
, ColourSelectEvent(self
.GetId(), self
.GetValue()))
130 if self
.callback
is not None:
133 def OnClick(self
, event
):
134 data
= wx
.ColourData()
135 data
.SetChooseFull(True)
136 data
.SetColour(self
.colour
)
137 dlg
= wx
.ColourDialog(self
.GetParent(), data
)
138 changed
= dlg
.ShowModal() == wx
.ID_OK
141 data
= dlg
.GetColourData()
142 self
.SetColour(data
.GetColour())
146 # moved after dlg.Destroy, since who knows what the callback will do...