1 #----------------------------------------------------------------------
2 # Name: wx.lib.stattext
3 # Purpose: A generic wxGenStaticText class. Using this should
4 # eliminate some of the platform differences in wxStaticText,
5 # such as background colours and mouse sensitivity.
11 # Copyright: (c) 2002 by Total Control Software
12 # Licence: wxWindows license
13 #----------------------------------------------------------------------
14 # 12/12/2003 - Jeff Grimmett (grimmtooth@softhome.net)
16 # o 2.5 compatability update.
22 #----------------------------------------------------------------------
24 class GenStaticText(wx.PyControl):
27 def __init__(self, parent, ID, label,
28 pos = wx.DefaultPosition, size = wx.DefaultSize,
30 name = "genstattext"):
31 wx.PyControl.__init__(self, parent, ID, pos, size, style|wx.NO_BORDER,
32 wx.DefaultValidator, name)
34 wx.PyControl.SetLabel(self, label) # don't check wx.ST_NO_AUTORESIZE yet
35 self.defBackClr = self.GetBackgroundColour()
36 self.InheritAttributes()
37 self.SetBestSize(size)
39 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
40 self.Bind(wx.EVT_PAINT, self.OnPaint)
43 def SetLabel(self, label):
45 Sets the static text label and updates the control's size to exactly
46 fit the label unless the control has wx.ST_NO_AUTORESIZE flag.
48 wx.PyControl.SetLabel(self, label)
49 style = self.GetWindowStyleFlag()
50 if not style & wx.ST_NO_AUTORESIZE:
51 best = self.GetBestSize()
53 self.SetSizeHints(best)
57 def SetFont(self, font):
59 Sets the static text font and updates the control's size to exactly
60 fit the label unless the control has wx.ST_NO_AUTORESIZE flag.
62 wx.PyControl.SetFont(self, font)
63 style = self.GetWindowStyleFlag()
64 if not style & wx.ST_NO_AUTORESIZE:
65 best = self.GetBestSize()
67 self.SetSizeHints(best)
71 def DoGetBestSize(self):
73 Overridden base class virtual. Determines the best size of
74 the button based on the label size.
76 label = self.GetLabel()
77 maxWidth = totalHeight = 0
78 for line in label.split('\n'):
80 w, h = self.GetTextExtent('W') # empty lines have height too
82 w, h = self.GetTextExtent(line)
84 maxWidth = max(maxWidth, w)
85 return wx.Size(maxWidth, totalHeight)
88 def AcceptsFocus(self):
89 """Overridden base class virtual."""
93 def GetDefaultAttributes(self):
95 Overridden base class virtual. By default we should use
96 the same font/colour attributes as the native StaticText.
98 return wx.StaticText.GetClassDefaultAttributes()
101 def ShouldInheritColours(self):
103 Overridden base class virtual. If the parent has non-default
104 colours then we want this control to inherit them.
109 def OnPaint(self, event):
110 dc = wx.BufferedPaintDC(self)
111 #dc = wx.PaintDC(self)
112 width, height = self.GetClientSize()
113 if not width or not height:
116 clr = self.GetBackgroundColour()
117 backBrush = wx.Brush(clr, wx.SOLID)
118 if wx.Platform == "__WXMAC__" and clr == self.defBackClr:
119 # if colour is still the default then use the striped background on Mac
120 backBrush.MacSetTheme(1) # 1 == kThemeBrushDialogBackgroundActive
121 dc.SetBackground(backBrush)
123 dc.SetTextForeground(self.GetForegroundColour())
125 dc.SetFont(self.GetFont())
126 label = self.GetLabel()
127 style = self.GetWindowStyleFlag()
129 for line in label.split('\n'):
131 w, h = self.GetTextExtent('W') # empty lines have height too
133 w, h = self.GetTextExtent(line)
134 if style & wx.ALIGN_RIGHT:
136 if style & wx.ALIGN_CENTER:
138 dc.DrawText(line, x, y)
142 def OnEraseBackground(self, event):
148 #----------------------------------------------------------------------