]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/colourselect.py
need wxUSE_WCHAR_T around wxConvLocal
[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 # Cliff Wells, 2002/02/07
30 # - Added ColourSelect Event
31
32 EVT_COMMAND_COLOURSELECT = wxNewId()
33
34 class ColourSelectEvent(wxPyCommandEvent):
35 def __init__(self, id, value):
36 wxPyCommandEvent.__init__(self, id = id)
37 self.SetEventType(EVT_COMMAND_COLOURSELECT)
38 self.value = value
39
40 def GetValue(self):
41 return self.value
42
43 def EVT_COLOURSELECT(win, id, func):
44 win.Connect(id, -1, EVT_COMMAND_COLOURSELECT, func)
45
46 class ColourSelect(wxButton):
47 def __init__(self, parent, id, label = "", bcolour=(0, 0, 0),
48 pos = wxDefaultPosition, size = wxDefaultSize,
49 callback = None):
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)
54
55 def GetColour(self):
56 return self.set_colour
57
58 def GetValue(self):
59 return self.set_colour
60
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
68
69 def SetValue(self, bcolour):
70 self.SetColour(bcolour)
71
72 def OnChange(self):
73 wxPostEvent(self, ColourSelectEvent(self.GetId(), self.GetValue()))
74 if self.callback is not None:
75 self.callback()
76
77 def OnClick(self, event):
78 data = wxColourData()
79 data.SetChooseFull(true)
80 data.SetColour(self.set_colour_val)
81 dlg = wxColourDialog(self.GetParent(), data)
82 changed = dlg.ShowModal() == wxID_OK
83 if changed:
84 data = dlg.GetColourData()
85 self.SetColour(data.GetColour().Get())
86 dlg.Destroy()
87
88 if changed:
89 self.OnChange() # moved after dlg.Destroy, since who knows what the callback will do...
90