diff --git a/wxPython/demo/GenericButtons.py b/wxPython/demo/GenericButtons.py index c9972a82a4..beb8a52451 100644 --- a/wxPython/demo/GenericButtons.py +++ b/wxPython/demo/GenericButtons.py @@ -10,6 +10,7 @@ class TestPanel(wx.Panel): def __init__(self, parent, log): wx.Panel.__init__(self, parent, -1) self.log = log + ##self.SetBackgroundColour("sky blue") sizer = wx.FlexGridSizer(1, 3, 20, 20) @@ -43,6 +44,7 @@ class TestPanel(wx.Panel): b.SetFont(wx.Font(20, wx.SWISS, wx.NORMAL, wx.BOLD, False)) b.SetBezelWidth(5) ###b.SetBestSize() + b.SetSizeHints(wx.DefaultSize) b.SetBackgroundColour("Navy") b.SetForegroundColour(wx.WHITE) b.SetToolTipString("This is a BIG button...") diff --git a/wxPython/demo/StaticBitmap.py b/wxPython/demo/StaticBitmap.py index d45f28b9cd..1b9f3c2b03 100644 --- a/wxPython/demo/StaticBitmap.py +++ b/wxPython/demo/StaticBitmap.py @@ -2,25 +2,36 @@ import wx import images + +USE_GENERIC = 0 + +if USE_GENERIC: + from wx.lib.stattext import GenStaticText as StaticText + from wx.lib.statbmp import GenStaticBitmap as StaticBitmap +else: + StaticText = wx.StaticText + StaticBitmap = wx.StaticBitmap + + #---------------------------------------------------------------------- class TestPanel(wx.Panel): def __init__(self, parent, log): wx.Panel.__init__(self, parent, -1) self.log = log - self.count = 0 + ##self.SetBackgroundColour("sky blue") - wx.StaticText(self, -1, "This is a wx.StaticBitmap.", (45, 15)) + StaticText(self, -1, "This is a wx.StaticBitmap.", (45, 15)) bmp = images.getTest2Bitmap() mask = wx.Mask(bmp, wx.BLUE) bmp.SetMask(mask) - wx.StaticBitmap(self, -1, bmp, (80, 50), (bmp.GetWidth(), bmp.GetHeight())) + StaticBitmap(self, -1, bmp, (80, 50), (bmp.GetWidth(), bmp.GetHeight())) bmp = images.getRobinBitmap() - wx.StaticBitmap(self, -1, bmp, (80, 150)) + StaticBitmap(self, -1, bmp, (80, 150)) - wx.StaticText(self, -1, "Hey, if Ousterhout can do it, so can I.", (200, 175)) + StaticText(self, -1, "Hey, if Ousterhout can do it, so can I.", (200, 175)) #---------------------------------------------------------------------- diff --git a/wxPython/demo/StaticText.py b/wxPython/demo/StaticText.py index 8abe09c42d..968c02e3c8 100644 --- a/wxPython/demo/StaticText.py +++ b/wxPython/demo/StaticText.py @@ -3,6 +3,7 @@ import wx USE_GENERIC = 0 + if USE_GENERIC: from wx.lib.stattext import GenStaticText as StaticText else: @@ -14,30 +15,35 @@ else: class TestPanel(wx.Panel): def __init__(self, parent): wx.Panel.__init__(self, parent, -1) + ##self.SetBackgroundColour("sky blue") StaticText(self, -1, "This is an example of static text", (20, 10)) StaticText(self, -1, "using the wx.StaticText Control.", (20, 30)) StaticText( - self, -1, "Is this yellow?", (20, 70), (90, -1) + self, -1, "Is this yellow?", (20, 70), (120, -1) ).SetBackgroundColour('Yellow') StaticText( - self, -1, "align center", (120, 70), (90, -1), wx.ALIGN_CENTER + self, -1, "align center", (160, 70), (120, -1), wx.ALIGN_CENTER ).SetBackgroundColour('Yellow') StaticText( - self, -1, "align right", (220, 70), (90, -1), wx.ALIGN_RIGHT + self, -1, "align right", (300, 70), (120, -1), wx.ALIGN_RIGHT ).SetBackgroundColour('Yellow') str = "This is a different font." - text = StaticText(self, -1, str, (20, 100)) + text = StaticText(self, -1, str, (20, 120)) font = wx.Font(18, wx.SWISS, wx.NORMAL, wx.NORMAL) text.SetFont(font) text.SetSize(text.GetBestSize()) - StaticText(self, -1, "Multi-line wx.StaticText\nline 2\nline 3\n\nafter empty line", (20,150)) - StaticText(self, -1, "Align right multi-line\nline 2\nline 3\n\nafter empty line", (220,150), style=wx.ALIGN_RIGHT) + StaticText(self, -1, + "Multi-line wx.StaticText\nline 2\nline 3\n\nafter empty line", + (20,170)) + StaticText(self, -1, + "Align right multi-line\nline 2\nline 3\n\nafter empty line", + (220,170), style=wx.ALIGN_RIGHT) #--------------------------------------------------------------------------- diff --git a/wxPython/docs/CHANGES.txt b/wxPython/docs/CHANGES.txt index ec796a6927..ad7f25d0ae 100644 --- a/wxPython/docs/CHANGES.txt +++ b/wxPython/docs/CHANGES.txt @@ -34,6 +34,13 @@ Deprecated the wx.iewin module. Deprecated the wx.Sizer.AddWindow, AddSizer, AddSpacer methods as well as their Insert* and Prepend* counterparts. +Added a generic StaticBitmap class in wx.lib.statbmp for the same +reasons that stattext was created, so it could be mouse sensitive on +all platforms like normal windows. Also updated stattext.py and +buttons.py to handle attribute (font & colour) defaults and +inheritance the new way. If you have custom controls of your own you +should review stattxt.py or one of the others to see how it is to be +done. diff --git a/wxPython/wx/lib/buttons.py b/wxPython/wx/lib/buttons.py index 7078664585..68260c0572 100644 --- a/wxPython/wx/lib/buttons.py +++ b/wxPython/wx/lib/buttons.py @@ -76,11 +76,7 @@ class GenButton(wx.PyControl): self.useFocusInd = True self.SetLabel(label) - self.SetPosition(pos) - font = parent.GetFont() - if not font.Ok(): - font = wx.SystemSettings.GetSystemFont(wx.SYS_DEFAULT_GUI_FONT) - self.SetFont(font) + self.InheritAttributes() self.SetBestSize(size) self.InitColours() @@ -103,23 +99,15 @@ class GenButton(wx.PyControl): and set a good size. """ if size is None: - size = wx.Size(-1,-1) - if type(size) == type(()): - size = wx.Size(size[0], size[1]) - size = wx.Size(size.width, size.height) # make a copy - - best = self.GetBestSize() - if size.width == -1: - size.width = best.width - if size.height == -1: - size.height = best.height - - self.SetSize(size) + size = wx.DefaultSize + wx.PyControl.SetBestSize(self, size) def DoGetBestSize(self): - """Overridden base class virtual. Determines the best size of the - button based on the label and bezel size.""" + """ + Overridden base class virtual. Determines the best size of the + button based on the label and bezel size. + """ w, h, useMin = self._GetLabelSize() defSize = wx.Button.GetDefaultSize() width = 12 + w @@ -138,6 +126,22 @@ class GenButton(wx.PyControl): return self.IsShown() and self.IsEnabled() + def GetDefaultAttributes(self): + """ + Overridden base class virtual. By default we should use + the same font/colour attributes as the native Button. + """ + return wx.Button.GetClassDefaultAttributes() + + + def ShouldInheritColours(self): + """ + Overridden base class virtual. Buttons usually don't inherit + the parent's colours. + """ + return False + + def Enable(self, enable=True): wx.PyControl.Enable(self, enable) self.Refresh() @@ -161,32 +165,12 @@ class GenButton(wx.PyControl): def InitColours(self): - faceClr = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE) - textClr = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNTEXT) - self.faceDnClr = faceClr - self.SetBackgroundColour(faceClr) - self.SetForegroundColour(textClr) - - shadowClr = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNSHADOW) - highlightClr = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNHIGHLIGHT) - self.shadowPen = wx.Pen(shadowClr, 1, wx.SOLID) - self.highlightPen = wx.Pen(highlightClr, 1, wx.SOLID) - if wx.Platform == "__WXMAC__": - self.focusIndPen = wx.Pen(textClr, 1, wx.SOLID) - else: - self.focusIndPen = wx.Pen(textClr, 1, wx.USER_DASH) - self.focusIndPen.SetDashes([1,1]) - self.focusIndPen.SetCap(wx.CAP_BUTT) - self.focusClr = highlightClr - - - def SetBackgroundColour(self, colour): - wx.PyControl.SetBackgroundColour(self, colour) - colour = self.GetBackgroundColour() - - # 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() + """ + Calculate a new set of highlight and shadow colours based on + the background colour. Works okay if the colour is dark... + """ + faceClr = self.GetBackgroundColour() + r, g, b = faceClr.Get() fr, fg, fb = min(255,r+32), min(255,g+32), min(255,b+32) self.faceDnClr = wx.Colour(fr, fg, fb) sr, sg, sb = max(0,r-32), max(0,g-32), max(0,b-32) @@ -195,6 +179,24 @@ class GenButton(wx.PyControl): self.highlightPen = wx.Pen(wx.Colour(hr,hg,hb), 1, wx.SOLID) self.focusClr = wx.Colour(hr, hg, hb) + textClr = self.GetForegroundColour() + if wx.Platform == "__WXMAC__": + self.focusIndPen = wx.Pen(textClr, 1, wx.SOLID) + else: + self.focusIndPen = wx.Pen(textClr, 1, wx.USER_DASH) + self.focusIndPen.SetDashes([1,1]) + self.focusIndPen.SetCap(wx.CAP_BUTT) + + + def SetBackgroundColour(self, colour): + wx.PyControl.SetBackgroundColour(self, colour) + self.InitColours() + + + def SetForegroundColour(self, colour): + wx.PyControl.SetForegroundColour(self, colour) + self.InitColours() + def _GetLabelSize(self): """ used internally """ @@ -245,11 +247,6 @@ class GenButton(wx.PyControl): def DrawFocusIndicator(self, dc, w, h): bw = self.bezelWidth -## if self.hasFocus: -## self.focusIndPen.SetColour(self.GetForegroundColour()) -## else: -## #self.focusIndPen.SetColour(self.GetBackgroundColour()) -## self.focusIndPen.SetColour(self.GetForegroundColour()) self.focusIndPen.SetColour(self.focusClr) dc.SetLogicalFunction(wx.INVERT) dc.SetPen(self.focusIndPen) diff --git a/wxPython/wx/lib/statbmp.py b/wxPython/wx/lib/statbmp.py new file mode 100644 index 0000000000..f0e0ff488c --- /dev/null +++ b/wxPython/wx/lib/statbmp.py @@ -0,0 +1,86 @@ +#---------------------------------------------------------------------- +# Name: wx.lib.statbmp +# Purpose: A generic StaticBitmap class. +# +# Author: Robin Dunn +# +# Created: 12-May-2004 +# RCS-ID: $Id$ +# Copyright: (c) 2004 by Total Control Software +# Licence: wxWindows license +#---------------------------------------------------------------------- + +import wx + +#---------------------------------------------------------------------- + +class GenStaticBitmap(wx.PyControl): + labelDelta = 1 + + def __init__(self, parent, ID, bitmap, + pos = wx.DefaultPosition, size = wx.DefaultSize, + style = 0, + name = "genstatbmp"): + wx.PyControl.__init__(self, parent, ID, pos, size, style|wx.NO_BORDER, + wx.DefaultValidator, name) + self._bitmap = bitmap + self.InheritAttributes() + self.SetBestSize(size) + + self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) + self.Bind(wx.EVT_PAINT, self.OnPaint) + + + def SetBitmap(self, bitmap): + self._bitmap = bitmap + self.SetBestSize( (bitmap.GetWidth(), bitmap.GetHeight()) ) + self.Refresh() + + + def GetBitmap(self): + return self._bitmap + + + def DoGetBestSize(self): + """ + Overridden base class virtual. Determines the best size of the + control based on the bitmap size. + """ + return wx.Size(self._bitmap.GetWidth(), self._bitmap.GetHeight()) + + + def AcceptsFocus(self): + """Overridden base class virtual.""" + return False + + + def GetDefaultAttributes(self): + """ + Overridden base class virtual. By default we should use + the same font/colour attributes as the native StaticBitmap. + """ + return wx.StaticBitmap.GetClassDefaultAttributes() + + + def ShouldInheritColours(self): + """ + Overridden base class virtual. If the parent has non-default + colours then we want this control to inherit them. + """ + return True + + + def OnPaint(self, event): + dc = wx.PaintDC(self) + dc.DrawBitmap(self._bitmap, 0, 0, True) + + + def OnEraseBackground(self, event): + pass + + + + +#---------------------------------------------------------------------- + + diff --git a/wxPython/wx/lib/stattext.py b/wxPython/wx/lib/stattext.py index ba49e318f8..1c1ac8fca4 100644 --- a/wxPython/wx/lib/stattext.py +++ b/wxPython/wx/lib/stattext.py @@ -32,27 +32,9 @@ class GenStaticText(wx.PyControl): wx.DefaultValidator, name) wx.PyControl.SetLabel(self, label) # don't check wx.ST_NO_AUTORESIZE yet - self.SetPosition(pos) - font = parent.GetFont() - if not font.Ok(): - font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) - wx.PyControl.SetFont(self, font) # same here - - self.defBackClr = parent.GetBackgroundColour() - if not self.defBackClr.Ok(): - self.defBackClr = wx.SystemSettings.GetColour(wx.SYS_COLOUR_3DFACE) - self.SetBackgroundColour(self.defBackClr) - - clr = parent.GetForegroundColour() - if not clr.Ok(): - clr = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNTEXT) - self.SetForegroundColour(clr) - - rw, rh = size - bw, bh = self.GetBestSize() - if rw == -1: rw = bw - if rh == -1: rh = bh - self.SetSize(wx.Size(rw, rh)) + self.defBackClr = self.GetBackgroundColour() + self.InheritAttributes() + self.SetBestSize(size) self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) self.Bind(wx.EVT_PAINT, self.OnPaint) @@ -66,7 +48,9 @@ class GenStaticText(wx.PyControl): wx.PyControl.SetLabel(self, label) style = self.GetWindowStyleFlag() if not style & wx.ST_NO_AUTORESIZE: - self.SetSize(self.GetBestSize()) + best = self.GetBestSize() + self.SetSize(best) + self.SetSizeHints(best) self.Refresh() @@ -78,13 +62,17 @@ class GenStaticText(wx.PyControl): wx.PyControl.SetFont(self, font) style = self.GetWindowStyleFlag() if not style & wx.ST_NO_AUTORESIZE: - self.SetSize(self.GetBestSize()) + best = self.GetBestSize() + self.SetSize(best) + self.SetSizeHints(best) self.Refresh() def DoGetBestSize(self): - """Overridden base class virtual. Determines the best size of the - button based on the label size.""" + """ + Overridden base class virtual. Determines the best size of + the button based on the label size. + """ label = self.GetLabel() maxWidth = totalHeight = 0 for line in label.split('\n'): @@ -102,6 +90,22 @@ class GenStaticText(wx.PyControl): return False + def GetDefaultAttributes(self): + """ + Overridden base class virtual. By default we should use + the same font/colour attributes as the native StaticText. + """ + return wx.StaticText.GetClassDefaultAttributes() + + + def ShouldInheritColours(self): + """ + Overridden base class virtual. If the parent has non-default + colours then we want this control to inherit them. + """ + return True + + def OnPaint(self, event): dc = wx.BufferedPaintDC(self) #dc = wx.PaintDC(self)