]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/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
)
46 class ColourSelect(wxButton
):
47 def __init__(self
, parent
, id, label
= "", bcolour
=(0, 0, 0),
48 pos
= wxDefaultPosition
, size
= wxDefaultSize
,
50 wxButton
.__init
__(self
, parent
, id, label
, pos
=pos
, size
=size
)
51 self
.SetColour(bcolour
)
52 self
.callback
= callback
53 EVT_BUTTON(parent
, self
.GetId(), self
.OnClick
)
56 return self
.set_colour
59 return self
.set_colour
61 def SetColour(self
, bcolour
):
62 self
.set_colour_val
= wxColor(bcolour
[0], bcolour
[1], bcolour
[2])
63 self
.SetBackgroundColour(self
.set_colour_val
)
64 avg
= reduce(lambda a
, b
: a
+ b
, bcolour
) / 3
65 fcolour
= avg
> 128 and (0, 0, 0) or (255, 255, 255)
66 self
.SetForegroundColour(apply(wxColour
, fcolour
))
67 self
.set_colour
= bcolour
69 def SetValue(self
, bcolour
):
70 self
.SetColour(bcolour
)
73 wxPostEvent(self
, ColourSelectEvent(self
.GetId(), self
.GetValue()))
74 if self
.callback
is not None:
77 def OnClick(self
, event
):
79 data
.SetChooseFull(true
)
80 data
.SetColour(self
.set_colour_val
)
81 dlg
= wxColourDialog(self
.GetParent(), data
)
82 changed
= dlg
.ShowModal() == wxID_OK
84 data
= dlg
.GetColourData()
85 self
.SetColour(data
.GetColour().Get())
89 self
.OnChange() # moved after dlg.Destroy, since who knows what the callback will do...