]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
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 | #---------------------------------------------------------------------------- | |
1fded56b | 10 | |
d14a1e28 RD |
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 | ||
b881fc78 RD |
30 | # 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
31 | # | |
32 | # o Updated for 2.5 compatability. | |
33 | # | |
34 | ||
35 | #---------------------------------------------------------------------------- | |
36 | ||
37 | import wx | |
d14a1e28 | 38 | |
b881fc78 RD |
39 | #---------------------------------------------------------------------------- |
40 | ||
41 | wxEVT_COMMAND_COLOURSELECT = wx.NewEventType() | |
42 | ||
43 | class ColourSelectEvent(wx.PyCommandEvent): | |
7722248d | 44 | def __init__(self, id, value): |
b881fc78 RD |
45 | wx.PyCommandEvent.__init__(self, id = id) |
46 | self.SetEventType(wxEVT_COMMAND_COLOURSELECT) | |
7722248d | 47 | self.value = value |
d14a1e28 | 48 | |
7722248d RD |
49 | def GetValue(self): |
50 | return self.value | |
d14a1e28 | 51 | |
b881fc78 | 52 | EVT_COLOURSELECT = wx.PyEventBinder(wxEVT_COMMAND_COLOURSELECT, 1) |
7722248d | 53 | |
b881fc78 | 54 | #---------------------------------------------------------------------------- |
7722248d | 55 | |
b881fc78 RD |
56 | class ColourSelect(wx.BitmapButton): |
57 | def __init__(self, parent, id, label="", colour=wx.BLACK, | |
58 | pos=wx.DefaultPosition, size=wx.DefaultSize, | |
7722248d RD |
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 | |
b881fc78 RD |
66 | wx.BitmapButton.__init__(self, parent, id, wx.EmptyBitmap(w,h), |
67 | pos=pos, size=size, style=style|wx.BU_AUTODRAW) | |
68 | ||
7722248d | 69 | if type(colour) == type( () ): |
b881fc78 | 70 | colour = wx.Colour(*colour) |
7722248d RD |
71 | self.colour = colour |
72 | self.SetLabel(label) | |
d14a1e28 | 73 | self.callback = callback |
7722248d RD |
74 | bmp = self.MakeBitmap() |
75 | self.SetBitmap(bmp) | |
b881fc78 | 76 | parent.Bind(wx.EVT_BUTTON, self.OnClick, self) |
d14a1e28 | 77 | |
7722248d | 78 | |
d14a1e28 | 79 | def GetColour(self): |
7722248d | 80 | return self.colour |
d14a1e28 | 81 | |
7722248d RD |
82 | def GetValue(self): |
83 | return self.colour | |
84 | ||
7722248d RD |
85 | def SetValue(self, colour): |
86 | self.SetColour(colour) | |
87 | ||
7722248d RD |
88 | def SetColour(self, colour): |
89 | if type(colour) == type( () ): | |
90 | colour = wxColour(*colour) | |
b881fc78 | 91 | |
7722248d RD |
92 | self.colour = colour |
93 | bmp = self.MakeBitmap() | |
94 | self.SetBitmap(bmp) | |
95 | ||
96 | ||
97 | def MakeBitmap(self): | |
98 | bdr = 10 | |
b881fc78 RD |
99 | width, height = self.GetSize() |
100 | bmp = wx.EmptyBitmap(width-bdr, height-bdr) | |
101 | dc = wx.MemoryDC() | |
7722248d RD |
102 | dc.SelectObject(bmp) |
103 | label = self.GetLabel() | |
104 | # Just make a little colored bitmap | |
b881fc78 | 105 | dc.SetBackground(wx.Brush(self.colour)) |
7722248d | 106 | dc.Clear() |
b881fc78 | 107 | |
7722248d RD |
108 | if label: |
109 | # Add a label to it | |
110 | avg = reduce(lambda a, b: a + b, self.colour.Get()) / 3 | |
b881fc78 | 111 | fcolour = avg > 128 and wx.BLACK or wx.WHITE |
7722248d | 112 | dc.SetTextForeground(fcolour) |
b881fc78 RD |
113 | dc.DrawLabel(label, (0,0, width-bdr, height-bdr), |
114 | wx.ALIGN_CENTER) | |
7722248d | 115 | |
b881fc78 | 116 | dc.SelectObject(wx.NullBitmap) |
7722248d RD |
117 | return bmp |
118 | ||
119 | ||
120 | def SetBitmap(self, bmp): | |
121 | self.SetBitmapLabel(bmp) | |
122 | self.SetBitmapSelected(bmp) | |
123 | self.SetBitmapDisabled(bmp) | |
124 | self.SetBitmapFocus(bmp) | |
125 | self.SetBitmapSelected(bmp) | |
d14a1e28 | 126 | |
d14a1e28 RD |
127 | |
128 | def OnChange(self): | |
b881fc78 | 129 | wx.PostEvent(self, ColourSelectEvent(self.GetId(), self.GetValue())) |
d14a1e28 RD |
130 | if self.callback is not None: |
131 | self.callback() | |
132 | ||
133 | def OnClick(self, event): | |
b881fc78 | 134 | data = wx.ColourData() |
d14a1e28 | 135 | data.SetChooseFull(True) |
7722248d | 136 | data.SetColour(self.colour) |
b881fc78 RD |
137 | dlg = wx.ColourDialog(self.GetParent(), data) |
138 | changed = dlg.ShowModal() == wx.ID_OK | |
139 | ||
d14a1e28 RD |
140 | if changed: |
141 | data = dlg.GetColourData() | |
7722248d | 142 | self.SetColour(data.GetColour()) |
b881fc78 | 143 | |
d14a1e28 RD |
144 | dlg.Destroy() |
145 | ||
b881fc78 | 146 | # moved after dlg.Destroy, since who knows what the callback will do... |
d14a1e28 | 147 | if changed: |
b881fc78 | 148 | self.OnChange() |
1fded56b | 149 |