"""MultiStateButton class@copyright: Copyright (c) 2006 Open Source Applications Foundation@license: http://osafoundation.org/Chandler_0.1_license_terms.htm"""import osimport wxfrom wx.lib.buttons import GenBitmapButtonclass MultiStateButton(GenBitmapButton): """ A MultiStateButton can have more one of multiple bitmaps in its default state. These bitmaps are passed in as a list of names, which are also the names of the PNG/GIF/JPG/TIFF/PSD bitmaps. mailButton = MultiStateButton(view, 100, multibitmaps=["NoMailState", "MailState"]) will create a button with the two bitmaps MailState.png and NoMailState.png. The default initial value will be the first named state; in this case "NoMailState". When desired (presumably when mail comes in), the state of the button can be changed thusly: mailButton.SetState("MailState") and the button's bitmap will change to MailState.png. Later, after all the mail has been read, the button can be changed back: mailButton.SetState("NoMailState") """ kBitmapFileExtensions = ["png", "gif", "jpg", "tiff", "psd"] kBitsKey = "bitmap" def __init__(self, parent, ID, label="", pos = wx.DefaultPosition, size = wx.DefaultSize, style = 0, validator = wx.DefaultValidator, name = "multistatebutton", multibitmaps = ()): self.bits = {} self.unrolledStateName="" ##rae - Should really assert firstBitmap is not None somehow here super(MultiStateButton, self).__init__(parent, ID, firstBitmap, pos, size, style, validator, name) firstBitmapName, firstBitmap = self.AddStates(multibitmaps) self.stateName = firstBitmapName self.unrolledStateName=self.stateName # calls to Bind must come after call to super's __init__ self.Bind(wx.EVT_ENTER_WINDOW, self.RolloverStart) self.Bind(wx.EVT_LEAVE_WINDOW, self.RolloverFinish) def RolloverStart(self, event): self.unrolledStateName = self.stateName self.SetState("button3") def RolloverFinish(self, event): self.SetState(self.unrolledStateName) def AddStates(self, multibitmaps): firstFound = None firstNameFound = None for name in multibitmaps: if type(name) == types.StringType: found = False for ext in MultiStateButton.kBitmapFileExtensions: try: img = wx.Image('%s.%s' % name, ext) self.bits[name] = {kBitsKey: img.ConvertToBitmap()} if not found: firstFound = self.bits[name][kBitsKey] firstNameFound = name found = True break except IOError: pass if not found: ##rae # should raise an error here somehow pass else return firstNameFound, firstFound def SetState(self, inStateName): if(inStateName != self.stateName): bitmapDict = self.bits.get(inStateName) if bitmapDict != None: bitmap = bitmapDict[kBitsKey] self.SetBitmapLabel(bitmap) self.stateName = inStateName self.Refresh() # will the app need this call to Update? Perhaps not. Try removing it. self.Update() def GetState(self): return self.stateName