]> git.saurik.com Git - wxWidgets.git/blame - wxPython/wx/lib/stattext.py
Fix "warning: operation on 'y' may be undefined".
[wxWidgets.git] / wxPython / wx / lib / stattext.py
CommitLineData
d14a1e28
RD
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#----------------------------------------------------------------------
b881fc78
RD
14# 12/12/2003 - Jeff Grimmett (grimmtooth@softhome.net)
15#
16# o 2.5 compatability update.
17# o Untested.
18#
d14a1e28
RD
19
20import wx
21
2e23d849
RD
22BUFFERED = 0 # In unbuffered mode we can let the theme shine through,
23 # is there a way to do this when buffering?
24
d14a1e28
RD
25#----------------------------------------------------------------------
26
27class 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
969d9b6f 38 self.InheritAttributes()
5193b348 39 self.SetBestFittingSize(size)
d14a1e28 40
2e23d849
RD
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
d14a1e28
RD
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()
2e23d849 57 self.InvalidateBestSize()
d14a1e28 58 if not style & wx.ST_NO_AUTORESIZE:
2e23d849 59 self.SetBestFittingSize(self.GetBestSize())
d14a1e28
RD
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()
2e23d849 70 self.InvalidateBestSize()
d14a1e28 71 if not style & wx.ST_NO_AUTORESIZE:
2e23d849 72 self.SetBestFittingSize(self.GetBestSize())
d14a1e28
RD
73 self.Refresh()
74
75
76 def DoGetBestSize(self):
969d9b6f
RD
77 """
78 Overridden base class virtual. Determines the best size of
2e23d849 79 the control based on the label size and the current font.
969d9b6f 80 """
d14a1e28 81 label = self.GetLabel()
2e23d849
RD
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
d14a1e28
RD
88 maxWidth = totalHeight = 0
89 for line in label.split('\n'):
90 if line == '':
2e23d849 91 w, h = dc.GetTextExtent('W') # empty lines have height too
d14a1e28 92 else:
2e23d849 93 w, h = dc.GetTextExtent(line)
d14a1e28
RD
94 totalHeight += h
95 maxWidth = max(maxWidth, w)
2e23d849
RD
96 best = wx.Size(maxWidth, totalHeight)
97 self.CacheBestSize(best)
98 return best
d14a1e28
RD
99
100
101 def AcceptsFocus(self):
102 """Overridden base class virtual."""
103 return False
104
105
969d9b6f
RD
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
969d9b6f 120
2e23d849 121
d14a1e28 122 def OnPaint(self, event):
2e23d849
RD
123 if BUFFERED:
124 dc = wx.BufferedPaintDC(self)
125 else:
126 dc = wx.PaintDC(self)
d14a1e28
RD
127 width, height = self.GetClientSize()
128 if not width or not height:
129 return
130
2e23d849
RD
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()
d14a1e28
RD
139
140 dc.SetTextForeground(self.GetForegroundColour())
d14a1e28
RD
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
d7403ad2 154 dc.DrawText(line, x, y)
d14a1e28
RD
155 y += h
156
157
158 def OnEraseBackground(self, event):
159 pass
160
161
162
163
164#----------------------------------------------------------------------
1fded56b 165
1fded56b 166