]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/colourselect.py
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 from wxPython
.wx
import *
13 # creates a colour wxButton with selectable color
14 # button click provides a colour selection box
15 # button colour will change to new colour
16 # GetColour method to get the selected colour
19 # call back to function if changes made
21 # Cliff Wells, logiplexsoftware@earthlink.net:
22 # - Made ColourSelect into "is a button" rather than "has a button"
23 # - Added label parameter and logic to adjust the label colour according to the background
26 # - Rearranged arguments to more closely follow wx conventions
27 # - Simplified some of the code
29 # Cliff Wells, 2002/02/07
30 # - Added ColourSelect Event
32 EVT_COMMAND_COLOURSELECT
= wxNewId()
34 class ColourSelectEvent(wxPyCommandEvent
):
35 def __init__(self
, id, value
):
36 wxPyCommandEvent
.__init
__(self
, id = id)
37 self
.SetEventType(EVT_COMMAND_COLOURSELECT
)
43 def EVT_COLOURSELECT(win
, id, func
):
44 win
.Connect(id, -1, EVT_COMMAND_COLOURSELECT
, func
)
49 class ColourSelect(wxBitmapButton
):
50 def __init__(self
, parent
, id, label
="", colour
=wxBLACK
,
51 pos
=wxDefaultPosition
, size
=wxDefaultSize
,
52 callback
=None, style
=0):
54 w
, h
= parent
.GetTextExtent(label
)
59 wxBitmapButton
.__init
__(self
, parent
, id, wxEmptyBitmap(w
,h
),
60 pos
=pos
, size
=size
, style
=style|wxBU_AUTODRAW
)
61 if type(colour
) == type( () ):
62 colour
= wxColour(*colour
)
65 self
.callback
= callback
66 bmp
= self
.MakeBitmap()
68 EVT_BUTTON(parent
, self
.GetId(), self
.OnClick
)
79 def SetValue(self
, colour
):
80 self
.SetColour(colour
)
83 def SetColour(self
, colour
):
84 if type(colour
) == type( () ):
85 colour
= wxColour(*colour
)
87 bmp
= self
.MakeBitmap()
94 bmp
= wxEmptyBitmap(sz
.width
-bdr
, sz
.height
-bdr
)
97 label
= self
.GetLabel()
98 # Just make a little colored bitmap
99 dc
.SetBackground(wxBrush(self
.colour
))
103 avg
= reduce(lambda a
, b
: a
+ b
, self
.colour
.Get()) / 3
104 fcolour
= avg
> 128 and wxBLACK
or wxWHITE
105 dc
.SetTextForeground(fcolour
)
106 dc
.DrawLabel(label
, (0,0, sz
.width
-bdr
, sz
.height
-bdr
),
109 dc
.SelectObject(wxNullBitmap
)
113 def SetBitmap(self
, bmp
):
114 self
.SetBitmapLabel(bmp
)
115 self
.SetBitmapSelected(bmp
)
116 self
.SetBitmapDisabled(bmp
)
117 self
.SetBitmapFocus(bmp
)
118 self
.SetBitmapSelected(bmp
)
122 wxPostEvent(self
, ColourSelectEvent(self
.GetId(), self
.GetValue()))
123 if self
.callback
is not None:
126 def OnClick(self
, event
):
127 data
= wxColourData()
128 data
.SetChooseFull(True)
129 data
.SetColour(self
.colour
)
130 dlg
= wxColourDialog(self
.GetParent(), data
)
131 changed
= dlg
.ShowModal() == wxID_OK
133 data
= dlg
.GetColourData()
134 self
.SetColour(data
.GetColour())
138 self
.OnChange() # moved after dlg.Destroy, since who knows what the callback will do...