]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/colourselect.py
Can't call ShiftDown when it is a CommandEvent
[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 # creates a colour wxButton with selectable color
12 # button click provides a colour selection box
13 # button colour will change to new colour
14 # GetColour method to get the selected colour
15
16 # Updates:
17 # call back to function if changes made
18
19 # Cliff Wells, logiplexsoftware@earthlink.net:
20 # - Made ColourSelect into "is a button" rather than "has a button"
21 # - Added label parameter and logic to adjust the label colour according to the background
22 # colour
23 # - Added id argument
24 # - Rearranged arguments to more closely follow wx conventions
25 # - Simplified some of the code
26
27 # Cliff Wells, 2002/02/07
28 # - Added ColourSelect Event
29
30 # 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
31 #
32 # o Updated for 2.5 compatability.
33 #
34
35 #----------------------------------------------------------------------------
36
37 import wx
38
39 #----------------------------------------------------------------------------
40
41 wxEVT_COMMAND_COLOURSELECT = wx.NewEventType()
42
43 class ColourSelectEvent(wx.PyCommandEvent):
44 def __init__(self, id, value):
45 wx.PyCommandEvent.__init__(self, id = id)
46 self.SetEventType(wxEVT_COMMAND_COLOURSELECT)
47 self.value = value
48
49 def GetValue(self):
50 return self.value
51
52 EVT_COLOURSELECT = wx.PyEventBinder(wxEVT_COMMAND_COLOURSELECT, 1)
53
54 #----------------------------------------------------------------------------
55
56 class ColourSelect(wx.BitmapButton):
57 def __init__(self, parent, id, label="", colour=wx.BLACK,
58 pos=wx.DefaultPosition, size=wx.DefaultSize,
59 callback=None, style=0):
60 if label:
61 w, h = parent.GetTextExtent(label)
62 w += 6
63 h += 6
64 else:
65 w, h = 20, 20
66 wx.BitmapButton.__init__(self, parent, id, wx.EmptyBitmap(w,h),
67 pos=pos, size=size, style=style|wx.BU_AUTODRAW)
68
69 if type(colour) == type( () ):
70 colour = wx.Colour(*colour)
71 self.colour = colour
72 self.SetLabel(label)
73 self.callback = callback
74 bmp = self.MakeBitmap()
75 self.SetBitmap(bmp)
76 parent.Bind(wx.EVT_BUTTON, self.OnClick, self)
77
78
79 def GetColour(self):
80 return self.colour
81
82 def GetValue(self):
83 return self.colour
84
85 def SetValue(self, colour):
86 self.SetColour(colour)
87
88 def SetColour(self, colour):
89 if type(colour) == tuple:
90 colour = wx.Colour(*colour)
91 if type(colour) == str:
92 colour = wx.NamedColour(colour)
93
94 self.colour = colour
95 bmp = self.MakeBitmap()
96 self.SetBitmap(bmp)
97
98
99 def MakeBitmap(self):
100 bdr = 10
101 width, height = self.GetSize()
102 bmp = wx.EmptyBitmap(width-bdr, height-bdr)
103 dc = wx.MemoryDC()
104 dc.SelectObject(bmp)
105 dc.SetFont(self.GetFont())
106 label = self.GetLabel()
107 # Just make a little colored bitmap
108 dc.SetBackground(wx.Brush(self.colour))
109 dc.Clear()
110
111 if label:
112 # Add a label to it
113 avg = reduce(lambda a, b: a + b, self.colour.Get()) / 3
114 fcolour = avg > 128 and wx.BLACK or wx.WHITE
115 dc.SetTextForeground(fcolour)
116 dc.DrawLabel(label, (0,0, width-bdr, height-bdr),
117 wx.ALIGN_CENTER)
118
119 dc.SelectObject(wx.NullBitmap)
120 return bmp
121
122
123 def SetBitmap(self, bmp):
124 self.SetBitmapLabel(bmp)
125 self.SetBitmapSelected(bmp)
126 self.SetBitmapDisabled(bmp)
127 self.SetBitmapFocus(bmp)
128 self.SetBitmapSelected(bmp)
129
130
131 def OnChange(self):
132 wx.PostEvent(self, ColourSelectEvent(self.GetId(), self.GetValue()))
133 if self.callback is not None:
134 self.callback()
135
136 def OnClick(self, event):
137 data = wx.ColourData()
138 data.SetChooseFull(True)
139 data.SetColour(self.colour)
140 dlg = wx.ColourDialog(self.GetParent(), data)
141 changed = dlg.ShowModal() == wx.ID_OK
142
143 if changed:
144 data = dlg.GetColourData()
145 self.SetColour(data.GetColour())
146
147 dlg.Destroy()
148
149 # moved after dlg.Destroy, since who knows what the callback will do...
150 if changed:
151 self.OnChange()
152