X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fbff5d1ba5c7d696bb429715bf0c339dd0bb425b..b4414c1f3778c54fb0ddfd2eea63f940bbee6d0f:/utils/wxPython/lib/buttons.py diff --git a/utils/wxPython/lib/buttons.py b/utils/wxPython/lib/buttons.py index ed1a381cfa..7d63710314 100644 --- a/utils/wxPython/lib/buttons.py +++ b/utils/wxPython/lib/buttons.py @@ -35,6 +35,7 @@ class wxGenButtonEvent(wxPyCommandEvent): def __init__(self, eventType, ID): wxPyCommandEvent.__init__(self, eventType, ID) self.isDown = false + self.theButton = None def SetIsDown(self, isDown): self.isDown = isDown @@ -42,16 +43,23 @@ class wxGenButtonEvent(wxPyCommandEvent): def GetIsDown(self): return self.isDown + def SetButtonObj(self, btn): + self.theButton = btn + + def GetButtonObj(self): + return self.theButton + #---------------------------------------------------------------------- -class wxGenButton(wxWindow): +class wxGenButton(wxControl): def __init__(self, parent, ID, label, pos = wxDefaultPosition, size = wxDefaultSize, style = 0, validator = wxDefaultValidator, name = "genbutton"): - wxWindow.__init__(self, parent, ID, pos, size, style, name) - self.SetValidator(validator) + if style == 0: + style = wxNO_BORDER + wxControl.__init__(self, parent, ID, pos, size, style, validator, name) self.up = true self.bezelWidth = 2 @@ -60,21 +68,11 @@ class wxGenButton(wxWindow): 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) @@ -86,6 +84,34 @@ class wxGenButton(wxWindow): 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): @@ -108,6 +134,7 @@ class wxGenButton(wxWindow): def InitColours(self): faceClr = wxSystemSettings_GetSystemColour(wxSYS_COLOUR_BTNFACE) textClr = wxSystemSettings_GetSystemColour(wxSYS_COLOUR_BTNTEXT) + self.faceDnClr = faceClr self.SetBackgroundColour(faceClr) self.SetForegroundColour(textClr) @@ -115,13 +142,34 @@ class wxGenButton(wxWindow): 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) + self.focusIndPen = wxPen(textClr, 1, wxDOT) + + + def SetBackgroundColour(self, colour): + wxWindow.SetBackgroundColour(self, colour) - self.focusIndPen = wxPen(textClr, 1, wxUSER_DASH) + # 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) @@ -162,7 +210,7 @@ class wxGenButton(wxWindow): bw = self.bezelWidth dc.SetLogicalFunction(wxINVERT) self.focusIndPen.SetColour(self.GetForegroundColour()) - self.focusIndPen.SetDashes([1,2,1,2]) # This isn't quite working the way I expected... + ##self.focusIndPen.SetDashes([1,2,1,2]) # This isn't quite working the way I expected... dc.SetPen(self.focusIndPen) dc.SetBrush(wxTRANSPARENT_BRUSH) dc.DrawRectangle(bw+2,bw+2, w-bw*2-4, h-bw*2-4) @@ -174,13 +222,17 @@ class wxGenButton(wxWindow): 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) if self.hasFocus and self.useFocusInd: self.DrawFocusIndicator(dc, width, height) + def OnEraseBackground(self, event): pass @@ -242,6 +294,7 @@ class wxGenButton(wxWindow): self.Refresh() event.Skip() + def OnKeyUp(self, event): if self.hasFocus and event.KeyCode() == ord(" "): self.up = true @@ -257,12 +310,12 @@ class wxGenBitmapButton(wxGenButton): 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 @@ -273,17 +326,32 @@ class wxGenBitmapButton(wxGenButton): 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(): @@ -295,7 +363,8 @@ class wxGenBitmapButton(wxGenButton): bw,bh = bmp.GetWidth(), bmp.GetHeight() if not self.up: dw = dy = 1 - dc.DrawBitmap(bmp, (width-bw)/2+dw, (height-bh)/2+dy, true) + hasMask = bmp.GetMask() != None + dc.DrawBitmap(bmp, (width-bw)/2+dw, (height-bh)/2+dy, hasMask) @@ -312,6 +381,7 @@ class __ToggleMixin: def OnLeftDown(self, event): if not self.IsEnabled(): return + self.saveUp = self.up self.up = not self.up self.CaptureMouse() self.SetFocus() @@ -320,7 +390,8 @@ class __ToggleMixin: def OnLeftUp(self, event): if not self.IsEnabled(): return - self.Notify() + if self.up != self.saveUp: + self.Notify() self.ReleaseMouse() self.Refresh()