""" MultiStateButton class @copyright: Copyright (c) 2006 Open Source Applications Foundation @license: http://osafoundation.org/Chandler_0.1_license_terms.htm """ import wx class MultiStateButton(wx.lib.buttons.GenBitmapButton): """ A bitmap button that has more than one possible bitmap in its default state. These bitmaps are passed in as a list of names, which are also the names of the PNG bitmaps. foo = MultiStateButton(view, 100, multibitmaps=["MailState", "NoMailState"]) will create a button """ kBitmapFileExtensions = ["png", "gif", "jpg", "tiff", "psd"] def __init__(self, parent, ID, label="", pos = wx.DefaultPosition, size = wx.DefaultSize, style = 0, validator = wx.DefaultValidator, name = "multistatebutton", multibitmaps = ()): firstBitmap = (len(multibitmaps) > 0 and multibitmaps[0] or "") super(wx.lib.buttons.GenBitmapButton, self).__init__(self, parent, ID, firstBitmap, pos, size, style, validator, name) self.AddStates(multibitmaps) def AddStates(multibitmaps): for name in multibitmaps: found = False for ext in kBitmapFileExtensions: try: img = wx.Image('/Users/rae/wokr/osaf/rae-button/' + name + '.' + ext) self.bits[name] = img.ConvertToBitmap() Found = True break except IOError: pass if not found: ##Reid # should raise an error here somehow pass def SetState(state): bitmap = self.bits.get(state) if bitmap != None: self.SetBitmapLabel(bitmap) theWindow = wx.Frame(parent=None, id=-1, title="MultiButton testing") box = wx.BoxSizer(wx.Horizontal) box.Add(MultiStateButton(theWindow, 1010, multibitmaps=['button1', 'button2'])) box.Add(MultiStateButton(theWindow, 1010, multibitmaps=['button3', 'button4'])) box.Add(MultiStateButton(theWindow, 1010, multibitmaps=['button1', 'button2', 'button3', 'button4', 'button5'])) theWindow.Show()