From 759c8b5299cc794217c1b83cb384ea7a85c60f48 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Sat, 9 Nov 2002 21:37:31 +0000 Subject: [PATCH] The generic buttons can create their own disabled bitmap if needed git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@17787 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/demo/GenericButtons.py | 15 +++++++++- wxPython/wxPython/lib/buttons.py | 22 +++++++++++---- wxPython/wxPython/lib/imageutils.py | 44 +++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 wxPython/wxPython/lib/imageutils.py diff --git a/wxPython/demo/GenericButtons.py b/wxPython/demo/GenericButtons.py index ad30e9752c..9909df1c03 100644 --- a/wxPython/demo/GenericButtons.py +++ b/wxPython/demo/GenericButtons.py @@ -46,6 +46,12 @@ class TestPanel(wxPanel): EVT_BUTTON(self, b.GetId(), self.OnButton) sizer.Add(b) + bmp = images.getTest2Bitmap() + b = wxGenBitmapButton(self, -1, bmp) + EVT_BUTTON(self, b.GetId(), self.OnButton) + sizer.Add(b) + b.Enable(FALSE) + b = wxGenBitmapButton(self, -1, None) EVT_BUTTON(self, b.GetId(), self.OnButton) bmp = images.getBulb1Bitmap() @@ -58,7 +64,6 @@ class TestPanel(wxPanel): b.SetBitmapSelected(bmp) b.SetBestSize() sizer.Add(b) - sizer.Add(10,10) b = wxGenToggleButton(self, -1, "Toggle Button") EVT_BUTTON(self, b.GetId(), self.OnToggleButton) @@ -128,3 +133,11 @@ def runTest(frame, nb, log): import wxPython.lib.buttons overview = wxPython.lib.buttons.__doc__ + + + +if __name__ == '__main__': + import sys,os + import run + run.main(['', os.path.basename(sys.argv[0])]) + diff --git a/wxPython/wxPython/lib/buttons.py b/wxPython/wxPython/lib/buttons.py index da73bee67e..d28c281fd0 100644 --- a/wxPython/wxPython/lib/buttons.py +++ b/wxPython/wxPython/lib/buttons.py @@ -28,6 +28,8 @@ wxGenBitmapToggleButton the same but with bitmaps. """ from wxPython.wx import * +import imageutils + #---------------------------------------------------------------------- @@ -344,10 +346,10 @@ class wxGenBitmapButton(wxGenButton): pos = wxDefaultPosition, size = wxDefaultSize, style = 0, validator = wxDefaultValidator, name = "genbutton"): - self.bmpLabel = bitmap self.bmpDisabled = None self.bmpFocus = None self.bmpSelected = None + self.SetBitmapLabel(bitmap) wxGenButton.__init__(self, parent, ID, "", pos, size, style, validator, name) @@ -374,9 +376,19 @@ class wxGenBitmapButton(wxGenButton): """Set bitmap to display when the button is selected (pressed down)""" self.bmpSelected = bitmap - def SetBitmapLabel(self, bitmap): - """Set the bitmap to display normally. This is the only one that is required.""" + def SetBitmapLabel(self, bitmap, createOthers=true): + """ + Set the bitmap to display normally. + This is the only one that is required. If + createOthers is true, then the other bitmaps + will be generated on the fly. Currently, + only the disabled bitmap is generated. + """ self.bmpLabel = bitmap + if bitmap is not None and createOthers: + image = wxImageFromBitmap(bitmap) + imageutils.grayOut(image) + self.SetBitmapDisabled(wxBitmapFromImage(image)) def _GetLabelSize(self): @@ -457,10 +469,10 @@ class wxGenBitmapTextButton(wxGenBitmapButton): # generic bitmapped button w pos_x = (width-bw-tw)/2+dw # adjust for bitmap and text to centre if bmp !=None: - dc.DrawBitmap(bmp, pos_x, (height-bh)/2+dy, hasMask) # draw bitmap if available + dc.DrawBitmap(bmp, pos_x, (height-bh)/2+dy, hasMask) # draw bitmap if available pos_x = pos_x + 2 # extra spacing from bitmap - dc.DrawText(label, pos_x + dw+bw, (height-th)/2+dy) # draw the text + dc.DrawText(label, pos_x + dw+bw, (height-th)/2+dy) # draw the text #---------------------------------------------------------------------- diff --git a/wxPython/wxPython/lib/imageutils.py b/wxPython/wxPython/lib/imageutils.py new file mode 100644 index 0000000000..c0e9024fdd --- /dev/null +++ b/wxPython/wxPython/lib/imageutils.py @@ -0,0 +1,44 @@ +#---------------------------------------------------------------------- +# Name: wxPython.lib.imageutils +# Purpose: A collection of functions for simple image manipulations +# +# Author: Robb Shecter +# +# Created: 7-Nov-2002 +# RCS-ID: $Id$ +# Copyright: (c) 2002 by +# Licence: wxWindows license +#---------------------------------------------------------------------- + + + +def grayOut(anImage): + """ + Convert the given image (in place) to a grayed-out + version, appropriate for a 'disabled' appearance. + """ + factor = 0.7 # 0 < f < 1. Higher is grayer. + if anImage.HasMask(): + maskColor = (anImage.GetMaskRed(), anImage.GetMaskGreen(), anImage.GetMaskBlue()) + else: + maskColor = None + data = map(ord, list(anImage.GetData())) + + for i in range(0, len(data), 3): + pixel = (data[i], data[i+1], data[i+2]) + pixel = makeGray(pixel, factor, maskColor) + for x in range(3): + data[i+x] = pixel[x] + anImage.SetData(''.join(map(chr, data))) + + +def makeGray((r,g,b), factor, maskColor): + """ + Make a pixel grayed-out. If the pixel + matches the maskColor, it won't be + changed. + """ + if (r,g,b) != maskColor: + return map(lambda x: ((230 - x) * factor) + x, (r,g,b)) + else: + return (r,g,b)