]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/wx/lib/colourselect.py
Added Changer classes
[wxWidgets.git] / wxPython / wx / lib / colourselect.py
index fbf1999f37888bbeebeafec7f20e50f64d7cbbe9..5edba11b673e8689d946f2548f9fd9876ae43bc9 100644 (file)
@@ -8,8 +8,6 @@
 # Licence:      wxWindows license
 #----------------------------------------------------------------------------
 
-from wxPython.wx import *
-
 # creates a colour wxButton with selectable color
 # button click provides a colour selection box
 # button colour will change to new colour
@@ -29,62 +27,144 @@ from wxPython.wx import *
 # Cliff Wells, 2002/02/07
 # - Added ColourSelect Event
 
-EVT_COMMAND_COLOURSELECT = wxNewId()
+# 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
+#
+# o Updated for 2.5 compatability.
+#
 
-class ColourSelectEvent(wxPyCommandEvent):
-        def __init__(self, id, value):
-                wxPyCommandEvent.__init__(self, id = id)
-                self.SetEventType(EVT_COMMAND_COLOURSELECT)
-                self.value = value
+"""
+Provides a `ColourSelect` button that, when clicked, will display a
+colour selection dialog.  The selected colour is displayed on the
+button itself.
+"""
 
-        def GetValue(self):
-                return self.value
+#----------------------------------------------------------------------------
 
-def EVT_COLOURSELECT(win, id, func):
-    win.Connect(id, -1, EVT_COMMAND_COLOURSELECT, func)
+import  wx
 
-class ColourSelect(wxButton):
-    def __init__(self, parent, id, label = "", bcolour=(0, 0, 0),
-            pos = wxDefaultPosition, size = wxDefaultSize,
-                callback = None):
-        wxButton.__init__(self, parent, id, label, pos=pos, size=size)
-        self.SetColour(bcolour)
-        self.callback = callback
-        EVT_BUTTON(parent, self.GetId(), self.OnClick)
+#----------------------------------------------------------------------------
 
-    def GetColour(self):
-        return self.set_colour
+wxEVT_COMMAND_COLOURSELECT = wx.NewEventType()
+
+class ColourSelectEvent(wx.PyCommandEvent):
+    def __init__(self, id, value):
+        wx.PyCommandEvent.__init__(self, id = id)
+        self.SetEventType(wxEVT_COMMAND_COLOURSELECT)
+        self.value = value
 
     def GetValue(self):
-        return self.set_colour
+        return self.value
 
-    def SetColour(self, bcolour):
-        self.set_colour_val = wxColor(bcolour[0], bcolour[1], bcolour[2])
-        self.SetBackgroundColour(self.set_colour_val)
-        avg = reduce(lambda a, b: a + b, bcolour) / 3
-        fcolour = avg > 128 and (0, 0, 0) or (255, 255, 255)
-        self.SetForegroundColour(apply(wxColour, fcolour))
-        self.set_colour = bcolour
+EVT_COLOURSELECT = wx.PyEventBinder(wxEVT_COMMAND_COLOURSELECT, 1)
 
-    def SetValue(self, bcolour):
-        self.SetColour(bcolour)
+#----------------------------------------------------------------------------
+
+class ColourSelect(wx.BitmapButton):
+    def __init__(self, parent, id=wx.ID_ANY, label="", colour=wx.BLACK,
+                 pos=wx.DefaultPosition, size=wx.DefaultSize,
+                 callback=None, style=0):
+        if label:
+            w, h = parent.GetTextExtent(label)
+            w += 6
+            h += 6
+        else:
+            w, h = 20, 20
+        wx.BitmapButton.__init__(self, parent, id, wx.EmptyBitmap(w,h),
+                                 pos=pos, size=size, style=style|wx.BU_AUTODRAW)
+
+        if type(colour) == type( () ):
+            colour = wx.Colour(*colour)
+        self.colour = colour
+        self.SetLabel(label)
+        self.callback = callback
+        bmp = self.MakeBitmap()
+        self.SetBitmap(bmp)
+        parent.Bind(wx.EVT_BUTTON, self.OnClick, self)
+
+
+    def GetColour(self):
+        return self.colour
+
+    def GetValue(self):
+        return self.colour
+
+    def SetValue(self, colour):
+        self.SetColour(colour)
+
+    def SetColour(self, colour):
+        if type(colour) == tuple:
+            colour = wx.Colour(*colour)
+        if type(colour) == str:
+            colour = wx.NamedColour(colour)
+            
+        self.colour = colour
+        bmp = self.MakeBitmap()
+        self.SetBitmap(bmp)
+
+
+    def SetLabel(self, label):
+        self.label = label
+
+    def GetLabel(self):
+        return self.label
+
+
+    def MakeBitmap(self):
+        bdr = 8
+        width, height = self.GetSize()
+
+        # yes, this is weird, but it appears to work around a bug in wxMac
+        if "wxMac" in wx.PlatformInfo and width == height:
+            height -= 1
+            
+        bmp = wx.EmptyBitmap(width-bdr, height-bdr)
+        dc = wx.MemoryDC()
+        dc.SelectObject(bmp)
+        dc.SetFont(self.GetFont())
+        label = self.GetLabel()
+        # Just make a little colored bitmap
+        dc.SetBackground(wx.Brush(self.colour))
+        dc.Clear()
+
+        if label:
+            # Add a label to it
+            avg = reduce(lambda a, b: a + b, self.colour.Get()) / 3
+            fcolour = avg > 128 and wx.BLACK or wx.WHITE
+            dc.SetTextForeground(fcolour)
+            dc.DrawLabel(label, (0,0, width-bdr, height-bdr),
+                         wx.ALIGN_CENTER)
+            
+        dc.SelectObject(wx.NullBitmap)
+        return bmp
+
+
+    def SetBitmap(self, bmp):
+        self.SetBitmapLabel(bmp)
+        self.SetBitmapSelected(bmp)
+        self.SetBitmapDisabled(bmp)
+        self.SetBitmapFocus(bmp)
+        self.SetBitmapSelected(bmp)
+        self.Refresh()
+        
 
     def OnChange(self):
-        wxPostEvent(self, ColourSelectEvent(self.GetId(), self.GetValue()))
+        wx.PostEvent(self, ColourSelectEvent(self.GetId(), self.GetValue()))
         if self.callback is not None:
             self.callback()
 
     def OnClick(self, event):
-        data = wxColourData()
+        data = wx.ColourData()
         data.SetChooseFull(True)
-        data.SetColour(self.set_colour_val)
-        dlg = wxColourDialog(self.GetParent(), data)
-        changed = dlg.ShowModal() == wxID_OK
+        data.SetColour(self.colour)
+        dlg = wx.ColourDialog(wx.GetTopLevelParent(self), data)
+        changed = dlg.ShowModal() == wx.ID_OK
+
         if changed:
             data = dlg.GetColourData()
-            self.SetColour(data.GetColour().Get())
+            self.SetColour(data.GetColour())
         dlg.Destroy()
 
+        # moved after dlg.Destroy, since who knows what the callback will do...
         if changed:
-            self.OnChange() # moved after dlg.Destroy, since who knows what the callback will do...
+            self.OnChange()