Commit | Line | Data |
---|---|---|
d14a1e28 RD |
1 | """ |
2 | wxPyColourChooser | |
3 | Copyright (C) 2002 Michael Gilfix <mgilfix@eecs.tufts.edu> | |
1fded56b | 4 | |
d14a1e28 | 5 | This file is part of wxPyColourChooser. |
1fded56b | 6 | |
d14a1e28 RD |
7 | This version of wxPyColourChooser is open source; you can redistribute it |
8 | and/or modify it under the licensed terms. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
13 | """ | |
14 | ||
15 | from wxPython.wx import * | |
16 | ||
17 | class PyColourBox(wxPanel): | |
18 | """A Colour Selection Box | |
19 | ||
20 | The Colour selection box implements button like behavior but contains | |
21 | a solid-filled, coloured sub-box. Placing the colour in a sub-box allows | |
22 | for filling in the main panel's background for a high-lighting effect. | |
23 | """ | |
24 | def __init__(self, parent, id, colour=(0, 0, 0), size=(25, 20)): | |
25 | """Creates a new colour box instance and initializes the colour | |
26 | content.""" | |
27 | wxPanel.__init__(self, parent, id, | |
28 | size=wxSize(size[0], size[1])) | |
29 | ||
30 | self.colour_box = wxPanel(self, -1, style=wxSIMPLE_BORDER) | |
31 | ||
32 | sizer = wxGridSizer(1, 1) | |
33 | sizer.Add(self.colour_box, 0, wxALIGN_CENTER_VERTICAL | wxALIGN_CENTER_HORIZONTAL) | |
34 | sizer.SetItemMinSize(self.colour_box, size[0] - 5, size[1] - 5) | |
35 | self.SetAutoLayout(True) | |
36 | self.SetSizer(sizer) | |
37 | self.Layout() | |
38 | ||
39 | self.real_bg = self.GetBackgroundColour() | |
40 | self.SetColourTuple(colour) | |
41 | ||
42 | def GetColourBox(self): | |
43 | """Returns a reference to the internal box object containing the | |
44 | color. This function is useful for setting up event handlers for | |
45 | the box.""" | |
46 | return self.colour_box | |
47 | ||
48 | def GetColour(self): | |
49 | """Returns a wxColour object indicating the box's current colour.""" | |
50 | return self.colour_box.GetBackgroundColour() | |
51 | ||
52 | def SetColour(self, colour): | |
53 | """Accepts a wxColour object and sets the box's current color.""" | |
54 | self.colour_box.SetBackgroundColour(colour) | |
55 | self.colour_box.Refresh() | |
56 | ||
57 | def SetColourTuple(self, colour): | |
58 | """Sets the box's current couple to the given tuple.""" | |
59 | self.colour = colour | |
60 | self.colour_box.SetBackgroundColour(wxColour(*self.colour)) | |
61 | ||
62 | def Update(self): | |
63 | wxPanel.Update(self) | |
64 | self.colour_box.Update() | |
65 | ||
66 | def SetHighlight(self, val): | |
67 | """Accepts a boolean 'val' toggling the box's highlighting.""" | |
68 | # XXX This code has been disabled for now until I can figure out | |
69 | # how to get this to work reliably across all platforms. | |
70 | if val: | |
71 | #A wxColourPtr is returned in windows, making this difficult | |
72 | red =(self.real_bg.Red() - 45) % 255 | |
73 | green =(self.real_bg.Green() - 45) % 255 | |
74 | blue =(self.real_bg.Blue() - 45) % 255 | |
75 | new_colour = wxColour(red, green, blue) | |
76 | self.SetBackgroundColour(new_colour) | |
77 | else: | |
78 | self.SetBackgroundColour(self.real_bg) | |
79 | self.Refresh() |