]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/colourselect.py
Added wxAutoNSAutoreleasePool to Create(Tool|Status)Bar
[wxWidgets.git] / wxPython / wx / 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
47
48
49 class ColourSelect(wxBitmapButton):
50 def __init__(self, parent, id, label="", colour=wxBLACK,
51 pos=wxDefaultPosition, size=wxDefaultSize,
52 callback=None, style=0):
53 if label:
54 w, h = parent.GetTextExtent(label)
55 w += 6
56 h += 6
57 else:
58 w, h = 20, 20
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)
63 self.colour = colour
64 self.SetLabel(label)
65 self.callback = callback
66 bmp = self.MakeBitmap()
67 self.SetBitmap(bmp)
68 EVT_BUTTON(parent, self.GetId(), self.OnClick)
69
70
71 def GetColour(self):
72 return self.colour
73
74
75 def GetValue(self):
76 return self.colour
77
78
79 def SetValue(self, colour):
80 self.SetColour(colour)
81
82
83 def SetColour(self, colour):
84 if type(colour) == type( () ):
85 colour = wxColour(*colour)
86 self.colour = colour
87 bmp = self.MakeBitmap()
88 self.SetBitmap(bmp)
89
90
91 def MakeBitmap(self):
92 bdr = 10
93 sz = self.GetSize()
94 bmp = wxEmptyBitmap(sz.width-bdr, sz.height-bdr)
95 dc = wxMemoryDC()
96 dc.SelectObject(bmp)
97 label = self.GetLabel()
98 # Just make a little colored bitmap
99 dc.SetBackground(wxBrush(self.colour))
100 dc.Clear()
101 if label:
102 # Add a label to it
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),
107 wxALIGN_CENTER)
108
109 dc.SelectObject(wxNullBitmap)
110 return bmp
111
112
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)
119
120
121 def OnChange(self):
122 wxPostEvent(self, ColourSelectEvent(self.GetId(), self.GetValue()))
123 if self.callback is not None:
124 self.callback()
125
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
132 if changed:
133 data = dlg.GetColourData()
134 self.SetColour(data.GetColour())
135 dlg.Destroy()
136
137 if changed:
138 self.OnChange() # moved after dlg.Destroy, since who knows what the callback will do...
139