53e828389e0367f9920b4be768ccc31fa063fb7c
[wxWidgets.git] / wxPython / wxPython / lib / colourselect.py
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 #----------------------------------------------------------------------------
10
11 from wxPython.wx import *
12
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
17
18 # Updates:
19 # call back to function if changes made
20
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
24 # colour
25 # - Added id argument
26 # - Rearranged arguments to more closely follow wx conventions
27 # - Simplified some of the code
28
29
30 class ColourSelect(wxButton):
31 def __init__(self, parent, id, label = "", bcolour=(0, 0, 0),
32 pos = wxDefaultPosition, size = wxDefaultSize,
33 callback = None):
34 wxButton.__init__(self, parent, id, label, pos=pos, size=size)
35 self.SetColour(bcolour)
36 self.callback = callback
37 EVT_BUTTON(parent, self.GetId(), self.OnClick)
38
39 def GetColour(self):
40 return self.set_colour
41
42 def GetValue(self):
43 return self.set_colour
44
45 def SetColour(self, bcolour):
46 self.set_colour_val = wxColor(bcolour[0], bcolour[1], bcolour[2])
47 self.SetBackgroundColour(self.set_colour_val)
48 avg = reduce(lambda a, b: a + b, bcolour) / 3
49 fcolour = avg > 128 and (0, 0, 0) or (255, 255, 255)
50 self.SetForegroundColour(apply(wxColour, fcolour))
51 self.set_colour = bcolour
52
53 def SetValue(self, bcolour):
54 self.SetColour(bcolour)
55
56 def OnChange(self):
57 if self.callback is not None:
58 self.callback()
59
60 def OnClick(self, event):
61 data = wxColourData()
62 data.SetChooseFull(true)
63 data.SetColour(self.set_colour_val)
64 dlg = wxColourDialog(self.GetParent(), data)
65 changed = dlg.ShowModal() == wxID_OK
66 if changed:
67 data = dlg.GetColourData()
68 self.SetColour(data.GetColour().Get())
69 dlg.Destroy()
70
71 if changed:
72 self.OnChange() # moved after dlg.Destroy, since who knows what the callback will do...