]>
git.saurik.com Git - wxWidgets.git/blob - 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.
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 BUFFERED
= 0 # In unbuffered mode we can let the theme shine through,
23 # is there a way to do this when buffering?
25 #----------------------------------------------------------------------
27 class GenStaticText(wx
.PyControl
):
30 def __init__(self
, parent
, ID
, label
,
31 pos
= wx
.DefaultPosition
, size
= wx
.DefaultSize
,
33 name
= "genstattext"):
34 wx
.PyControl
.__init
__(self
, parent
, ID
, pos
, size
, style|wx
.NO_BORDER
,
35 wx
.DefaultValidator
, name
)
37 wx
.PyControl
.SetLabel(self
, label
) # don't check wx.ST_NO_AUTORESIZE yet
38 self
.InheritAttributes()
39 self
.SetInitialSize(size
)
41 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
43 self
.defBackClr
= self
.GetBackgroundColour()
44 self
.Bind(wx
.EVT_ERASE_BACKGROUND
, self
.OnEraseBackground
)
46 self
.SetBackgroundStyle(wx
.BG_STYLE_SYSTEM
)
50 def SetLabel(self
, label
):
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.
55 wx
.PyControl
.SetLabel(self
, label
)
56 style
= self
.GetWindowStyleFlag()
57 self
.InvalidateBestSize()
58 if not style
& wx
.ST_NO_AUTORESIZE
:
59 self
.SetSize(self
.GetBestSize())
63 def SetFont(self
, font
):
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.
68 wx
.PyControl
.SetFont(self
, font
)
69 style
= self
.GetWindowStyleFlag()
70 self
.InvalidateBestSize()
71 if not style
& wx
.ST_NO_AUTORESIZE
:
72 self
.SetSize(self
.GetBestSize())
76 def DoGetBestSize(self
):
78 Overridden base class virtual. Determines the best size of
79 the control based on the label size and the current font.
81 label
= self
.GetLabel()
84 font
= wx
.SystemSettings
.GetFont(wx
.SYS_DEFAULT_GUI_FONT
)
85 dc
= wx
.ClientDC(self
)
88 maxWidth
= totalHeight
= 0
89 for line
in label
.split('\n'):
91 w
, h
= dc
.GetTextExtent('W') # empty lines have height too
93 w
, h
= dc
.GetTextExtent(line
)
95 maxWidth
= max(maxWidth
, w
)
96 best
= wx
.Size(maxWidth
, totalHeight
)
97 self
.CacheBestSize(best
)
101 def AcceptsFocus(self
):
102 """Overridden base class virtual."""
106 def GetDefaultAttributes(self
):
108 Overridden base class virtual. By default we should use
109 the same font/colour attributes as the native StaticText.
111 return wx
.StaticText
.GetClassDefaultAttributes()
114 def ShouldInheritColours(self
):
116 Overridden base class virtual. If the parent has non-default
117 colours then we want this control to inherit them.
122 def OnPaint(self
, event
):
124 dc
= wx
.BufferedPaintDC(self
)
126 dc
= wx
.PaintDC(self
)
127 width
, height
= self
.GetClientSize()
128 if not width
or not height
:
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
)
141 dc
.SetTextForeground(self
.GetForegroundColour())
143 dc
.SetTextForeground(wx
.SystemSettings
.GetColour(wx
.SYS_COLOUR_GRAYTEXT
))
145 dc
.SetFont(self
.GetFont())
146 label
= self
.GetLabel()
147 style
= self
.GetWindowStyleFlag()
149 for line
in label
.split('\n'):
151 w
, h
= self
.GetTextExtent('W') # empty lines have height too
153 w
, h
= self
.GetTextExtent(line
)
154 if style
& wx
.ALIGN_RIGHT
:
156 if style
& wx
.ALIGN_CENTER
:
158 dc
.DrawText(line
, x
, y
)
162 def OnEraseBackground(self
, event
):
168 #----------------------------------------------------------------------