EVT_BUTTON(self, b.GetId(), self.OnButton)
b.Enable(false)
- b = wxGenButton(self, -1, 'bigger', (195,50), (120, 55))
+ b = wxGenButton(self, -1, 'bigger', (195,50))
EVT_BUTTON(self, b.GetId(), self.OnButton)
b.SetFont(wxFont(20, wxSWISS, wxNORMAL, wxBOLD, false))
b.SetBezelWidth(5)
- b.SetBackgroundColour(wxBLUE)
+ b.SetBestSize()
+ b.SetBackgroundColour(wxNamedColour("Navy"))
b.SetForegroundColour(wxWHITE)
#b.SetUseFocusIndicator(false)
bmp = wxBitmap('bitmaps/test2.bmp', wxBITMAP_TYPE_BMP)
- b = wxGenBitmapButton(self, -1, bmp, (10, 130),
- (bmp.GetWidth()+16, bmp.GetHeight()+16))
+ b = wxGenBitmapButton(self, -1, bmp, (10, 130))
EVT_BUTTON(self, b.GetId(), self.OnButton)
- b = wxGenBitmapButton(self, -1, None, (100, 130), (48,48))
+ b = wxGenBitmapButton(self, -1, None, (100, 130))
EVT_BUTTON(self, b.GetId(), self.OnButton)
bmp = wxBitmap('bitmaps/lb1.bmp', wxBITMAP_TYPE_BMP)
mask = wxMaskColour(bmp, wxBLUE)
mask = wxMaskColour(bmp, wxBLUE)
bmp.SetMask(mask)
b.SetBitmapSelected(bmp)
+ b.SetBestSize()
-
- b = wxGenToggleButton(self, -1, "Toggle Button", (10, 230), (85, 26))
+ b = wxGenToggleButton(self, -1, "Toggle Button", (10, 230))
EVT_BUTTON(self, b.GetId(), self.OnToggleButton)
- b = wxGenBitmapToggleButton(self, -1, None, (100, 230), (48,48))
+ b = wxGenBitmapToggleButton(self, -1, None, (100, 230))
EVT_BUTTON(self, b.GetId(), self.OnToggleButton)
bmp = wxBitmap('bitmaps/lb1.bmp', wxBITMAP_TYPE_BMP)
mask = wxMaskColour(bmp, wxBLUE)
bmp.SetMask(mask)
b.SetBitmapSelected(bmp)
b.SetToggle(true)
+ b.SetBestSize()
def OnButton(self, event):
def __init__(self, eventType, ID):
wxPyCommandEvent.__init__(self, eventType, ID)
self.isDown = false
+ self.theButton = None
def SetIsDown(self, isDown):
self.isDown = isDown
def GetIsDown(self):
return self.isDown
+ def SetButtonObj(self, btn):
+ self.theButton = btn
+
+ def GetButtonObj(self):
+ return self.theButton
+
#----------------------------------------------------------------------
self.SetLabel(label)
self.SetPosition(pos)
- if type(size) == type(()):
- size = wxSize(size[0], size[1])
- w = size.width
- h = size.height
- dsize = wxSize(75,23) ### wxButton_GetDefaultSize()
- if self.bezelWidth > 2:
- dsize.width = dsize.width + self.bezelWidth - 2
- dsize.height = dsize.height + self.bezelWidth - 2
- if w == -1: w = dsize.width
- if h == -1: h = dsize.height
- self.SetSize(wxSize(w,h))
font = parent.GetFont()
if not font.Ok():
font = wxSystemSettings_GetSystemFont(wxSYS_DEFAULT_GUI_FONT)
self.SetFont(font)
+ self.SetBestSize(size)
self.InitColours()
EVT_LEFT_DOWN(self, self.OnLeftDown)
EVT_KEY_UP(self, self.OnKeyUp)
+ def SetBestSize(self, size=None):
+ """
+ Given the current font and bezel width settings, calculate
+ and set a good size.
+ """
+ if size is None:
+ size = wxSize(-1,-1)
+ if type(size) == type(()):
+ size = wxSize(size[0], size[1])
+
+ # make a new size so we don't mess with the one passed in
+ size = wxSize(size.width, size.height)
+
+ w, h, useMin = self._GetLabelSize()
+ defSize = wxButton_GetDefaultSize()
+ if size.width == -1:
+ size.width = 12 + w
+ if useMin and size.width < defSize.width:
+ size.width = defSize.width
+ if size.height == -1:
+ size.height = 11 + h
+ if useMin and size.height < defSize.height:
+ size.height = defSize.height
+
+ size.width = size.width + self.bezelWidth - 1
+ size.height = size.height + self.bezelWidth - 1
+
+ self.SetSize(size)
def SetBezelWidth(self, width):
def InitColours(self):
faceClr = wxSystemSettings_GetSystemColour(wxSYS_COLOUR_BTNFACE)
textClr = wxSystemSettings_GetSystemColour(wxSYS_COLOUR_BTNTEXT)
+ self.faceDnClr = faceClr
self.SetBackgroundColour(faceClr)
self.SetForegroundColour(textClr)
highlightClr = wxSystemSettings_GetSystemColour(wxSYS_COLOUR_BTNHIGHLIGHT)
self.shadowPen = wxPen(shadowClr, 1, wxSOLID)
self.highlightPen = wxPen(highlightClr, 1, wxSOLID)
-
self.focusIndPen = wxPen(textClr, 1, wxUSER_DASH)
+ def SetBackgroundColour(self, colour):
+ wxWindow.SetBackgroundColour(self, colour)
+
+ # Calculate a new set of highlight and shadow colours based on
+ # the new background colour. Works okay if the colour is dark...
+ r, g, b = colour.Get()
+ fr, fg, fb = min(255,r+32), min(255,g+32), min(255,b+32)
+ self.faceDnClr = wxColour(fr, fg, fb)
+ sr, sg, sb = max(0,r-32), max(0,g-32), max(0,b-32)
+ self.shadowPen = wxPen(wxColour(sr,sg,sb), 1, wxSOLID)
+ hr, hg, hb = min(255,r+64), min(255,g+64), min(255,b+64)
+ self.highlightPen = wxPen(wxColour(hr,hg,hb), 1, wxSOLID)
+
+
+ def _GetLabelSize(self):
+ """ used internally """
+ w, h = self.GetTextExtent(self.GetLabel())
+ return w, h, true
+
+
def Notify(self):
evt = wxGenButtonEvent(wxEVT_COMMAND_BUTTON_CLICKED, self.GetId())
evt.SetIsDown(not self.up)
+ evt.SetButtonObj(self)
self.GetEventHandler().ProcessEvent(evt)
x2 = width-1
y2 = height-1
dc = wxPaintDC(self)
- dc.SetBackground(wxBrush(self.GetBackgroundColour(), wxSOLID))
+ if self.up:
+ dc.SetBackground(wxBrush(self.GetBackgroundColour(), wxSOLID))
+ else:
+ dc.SetBackground(wxBrush(self.faceDnClr, wxSOLID))
dc.Clear()
self.DrawBezel(dc, x1, y1, x2, y2)
self.DrawLabel(dc, width, height)
pos = wxDefaultPosition, size = wxDefaultSize,
style = 0, validator = wxDefaultValidator,
name = "genbutton"):
- wxGenButton.__init__(self, parent, ID, "", pos, size, style, validator, name)
-
self.bmpLabel = bitmap
self.bmpDisabled = None
self.bmpFocus = None
self.bmpSelected = None
+ wxGenButton.__init__(self, parent, ID, "", pos, size, style, validator, name)
+
def GetBitmapLabel(self):
return self.bmpLabel
def GetBitmapSelected(self):
return self.bmpSelected
+
def SetBitmapDisabled(self, bitmap):
+ """Set bitmap to display when the button is disabled"""
self.bmpDisabled = bitmap
+
def SetBitmapFocus(self, bitmap):
+ """Set bitmap to display when the button has the focus"""
self.bmpFocus = bitmap
self.SetUseFocusIndicator(false)
+
def SetBitmapSelected(self, bitmap):
+ """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."""
self.bmpLabel = bitmap
+ def _GetLabelSize(self):
+ """ used internally """
+ if not self.bmpLabel:
+ return -1, -1, false
+ return self.bmpLabel.GetWidth()+2, self.bmpLabel.GetHeight()+2, false
+
+
def DrawLabel(self, dc, width, height, dw=0, dy=0):
bmp = self.bmpLabel
if self.bmpDisabled and not self.IsEnabled():