]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/stattext.py
docstring update
[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 BUFFERED = 0 # In unbuffered mode we can let the theme shine through,
23 # is there a way to do this when buffering?
24
25 #----------------------------------------------------------------------
26
27 class GenStaticText(wx.PyControl):
28 labelDelta = 1
29
30 def __init__(self, parent, ID, label,
31 pos = wx.DefaultPosition, size = wx.DefaultSize,
32 style = 0,
33 name = "genstattext"):
34 wx.PyControl.__init__(self, parent, ID, pos, size, style|wx.NO_BORDER,
35 wx.DefaultValidator, name)
36
37 wx.PyControl.SetLabel(self, label) # don't check wx.ST_NO_AUTORESIZE yet
38 self.InheritAttributes()
39 self.SetBestFittingSize(size)
40
41 self.Bind(wx.EVT_PAINT, self.OnPaint)
42 if BUFFERED:
43 self.defBackClr = self.GetBackgroundColour()
44 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
45 else:
46 self.SetBackgroundStyle(wx.BG_STYLE_SYSTEM)
47
48
49
50 def SetLabel(self, label):
51 """
52 Sets the static text label and updates the control's size to exactly
53 fit the label unless the control has wx.ST_NO_AUTORESIZE flag.
54 """
55 wx.PyControl.SetLabel(self, label)
56 style = self.GetWindowStyleFlag()
57 self.InvalidateBestSize()
58 if not style & wx.ST_NO_AUTORESIZE:
59 self.SetBestFittingSize(self.GetBestSize())
60 self.Refresh()
61
62
63 def SetFont(self, font):
64 """
65 Sets the static text font and updates the control's size to exactly
66 fit the label unless the control has wx.ST_NO_AUTORESIZE flag.
67 """
68 wx.PyControl.SetFont(self, font)
69 style = self.GetWindowStyleFlag()
70 self.InvalidateBestSize()
71 if not style & wx.ST_NO_AUTORESIZE:
72 self.SetBestFittingSize(self.GetBestSize())
73 self.Refresh()
74
75
76 def DoGetBestSize(self):
77 """
78 Overridden base class virtual. Determines the best size of
79 the control based on the label size and the current font.
80 """
81 label = self.GetLabel()
82 font = self.GetFont()
83 if not font:
84 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
85 dc = wx.ClientDC(self)
86 dc.SetFont(font)
87
88 maxWidth = totalHeight = 0
89 for line in label.split('\n'):
90 if line == '':
91 w, h = dc.GetTextExtent('W') # empty lines have height too
92 else:
93 w, h = dc.GetTextExtent(line)
94 totalHeight += h
95 maxWidth = max(maxWidth, w)
96 best = wx.Size(maxWidth, totalHeight)
97 self.CacheBestSize(best)
98 return best
99
100
101 def AcceptsFocus(self):
102 """Overridden base class virtual."""
103 return False
104
105
106 def GetDefaultAttributes(self):
107 """
108 Overridden base class virtual. By default we should use
109 the same font/colour attributes as the native StaticText.
110 """
111 return wx.StaticText.GetClassDefaultAttributes()
112
113
114 def ShouldInheritColours(self):
115 """
116 Overridden base class virtual. If the parent has non-default
117 colours then we want this control to inherit them.
118 """
119 return True
120
121
122 def OnPaint(self, event):
123 if BUFFERED:
124 dc = wx.BufferedPaintDC(self)
125 else:
126 dc = wx.PaintDC(self)
127 width, height = self.GetClientSize()
128 if not width or not height:
129 return
130
131 if BUFFERED:
132 clr = self.GetBackgroundColour()
133 backBrush = wx.Brush(clr, wx.SOLID)
134 if wx.Platform == "__WXMAC__" and clr == self.defBackClr:
135 # if colour is still the default then use the striped background on Mac
136 backBrush.MacSetTheme(1) # 1 == kThemeBrushDialogBackgroundActive
137 dc.SetBackground(backBrush)
138 dc.Clear()
139
140 dc.SetTextForeground(self.GetForegroundColour())
141 dc.SetFont(self.GetFont())
142 label = self.GetLabel()
143 style = self.GetWindowStyleFlag()
144 x = y = 0
145 for line in label.split('\n'):
146 if line == '':
147 w, h = self.GetTextExtent('W') # empty lines have height too
148 else:
149 w, h = self.GetTextExtent(line)
150 if style & wx.ALIGN_RIGHT:
151 x = width - w
152 if style & wx.ALIGN_CENTER:
153 x = (width - w)/2
154 dc.DrawText(line, x, y)
155 y += h
156
157
158 def OnEraseBackground(self, event):
159 pass
160
161
162
163
164 #----------------------------------------------------------------------
165
166