]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/stattext.py
Added $Id keyword, a bit of cleanup and also some backwards
[wxWidgets.git] / wxPython / wx / lib / stattext.py
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.
6 #
7 # Author: Robin Dunn
8 #
9 # Created: 8-July-2002
10 # RCS-ID: $Id$
11 # Copyright: (c) 2002 by Total Control Software
12 # Licence: wxWindows license
13 #----------------------------------------------------------------------
14 # 12/12/2003 - Jeff Grimmett (grimmtooth@softhome.net)
15 #
16 # o 2.5 compatability update.
17 # o Untested.
18 #
19
20 import wx
21
22 #----------------------------------------------------------------------
23
24 class GenStaticText(wx.PyControl):
25 labelDelta = 1
26
27 def __init__(self, parent, ID, label,
28 pos = wx.DefaultPosition, size = wx.DefaultSize,
29 style = 0,
30 name = "genstattext"):
31 wx.PyControl.__init__(self, parent, ID, pos, size, style|wx.NO_BORDER,
32 wx.DefaultValidator, name)
33
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)
38
39 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
40 self.Bind(wx.EVT_PAINT, self.OnPaint)
41
42
43 def SetLabel(self, label):
44 """
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.
47 """
48 wx.PyControl.SetLabel(self, label)
49 style = self.GetWindowStyleFlag()
50 if not style & wx.ST_NO_AUTORESIZE:
51 best = self.GetBestSize()
52 self.SetSize(best)
53 self.SetSizeHints(best)
54 self.Refresh()
55
56
57 def SetFont(self, font):
58 """
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.
61 """
62 wx.PyControl.SetFont(self, font)
63 style = self.GetWindowStyleFlag()
64 if not style & wx.ST_NO_AUTORESIZE:
65 best = self.GetBestSize()
66 self.SetSize(best)
67 self.SetSizeHints(best)
68 self.Refresh()
69
70
71 def DoGetBestSize(self):
72 """
73 Overridden base class virtual. Determines the best size of
74 the button based on the label size.
75 """
76 label = self.GetLabel()
77 maxWidth = totalHeight = 0
78 for line in label.split('\n'):
79 if line == '':
80 w, h = self.GetTextExtent('W') # empty lines have height too
81 else:
82 w, h = self.GetTextExtent(line)
83 totalHeight += h
84 maxWidth = max(maxWidth, w)
85 return wx.Size(maxWidth, totalHeight)
86
87
88 def AcceptsFocus(self):
89 """Overridden base class virtual."""
90 return False
91
92
93 def GetDefaultAttributes(self):
94 """
95 Overridden base class virtual. By default we should use
96 the same font/colour attributes as the native StaticText.
97 """
98 return wx.StaticText.GetClassDefaultAttributes()
99
100
101 def ShouldInheritColours(self):
102 """
103 Overridden base class virtual. If the parent has non-default
104 colours then we want this control to inherit them.
105 """
106 return True
107
108
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:
114 return
115
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)
122
123 dc.SetTextForeground(self.GetForegroundColour())
124 dc.Clear()
125 dc.SetFont(self.GetFont())
126 label = self.GetLabel()
127 style = self.GetWindowStyleFlag()
128 x = y = 0
129 for line in label.split('\n'):
130 if line == '':
131 w, h = self.GetTextExtent('W') # empty lines have height too
132 else:
133 w, h = self.GetTextExtent(line)
134 if style & wx.ALIGN_RIGHT:
135 x = width - w
136 if style & wx.ALIGN_CENTER:
137 x = (width - w)/2
138 dc.DrawText(line, x, y)
139 y += h
140
141
142 def OnEraseBackground(self, event):
143 pass
144
145
146
147
148 #----------------------------------------------------------------------
149
150