]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/stattext.py
Fix or add some DoGetBestSize's
[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.SetPosition(pos)
36 font = parent.GetFont()
37 if not font.Ok():
38 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
39 wx.PyControl.SetFont(self, font) # same here
40
41 self.defBackClr = parent.GetBackgroundColour()
42 if not self.defBackClr.Ok():
43 self.defBackClr = wx.SystemSettings.GetColour(wx.SYS_COLOUR_3DFACE)
44 self.SetBackgroundColour(self.defBackClr)
45
46 clr = parent.GetForegroundColour()
47 if not clr.Ok():
48 clr = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNTEXT)
49 self.SetForegroundColour(clr)
50
51 rw, rh = size
52 bw, bh = self.GetBestSize()
53 if rw == -1: rw = bw
54 if rh == -1: rh = bh
55 self.SetSize(wx.Size(rw, rh))
56
57 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
58 self.Bind(wx.EVT_PAINT, self.OnPaint)
59
60
61 def SetLabel(self, label):
62 """
63 Sets the static text label and updates the control's size to exactly
64 fit the label unless the control has wx.ST_NO_AUTORESIZE flag.
65 """
66 wx.PyControl.SetLabel(self, label)
67 style = self.GetWindowStyleFlag()
68 if not style & wx.ST_NO_AUTORESIZE:
69 self.SetSize(self.GetBestSize())
70 self.Refresh()
71
72
73 def SetFont(self, font):
74 """
75 Sets the static text font and updates the control's size to exactly
76 fit the label unless the control has wx.ST_NO_AUTORESIZE flag.
77 """
78 wx.PyControl.SetFont(self, font)
79 style = self.GetWindowStyleFlag()
80 if not style & wx.ST_NO_AUTORESIZE:
81 self.SetSize(self.GetBestSize())
82 self.Refresh()
83
84
85 def DoGetBestSize(self):
86 """Overridden base class virtual. Determines the best size of the
87 button based on the label size."""
88 label = self.GetLabel()
89 maxWidth = totalHeight = 0
90 for line in label.split('\n'):
91 if line == '':
92 w, h = self.GetTextExtent('W') # empty lines have height too
93 else:
94 w, h = self.GetTextExtent(line)
95 totalHeight += h
96 maxWidth = max(maxWidth, w)
97 return wx.Size(maxWidth, totalHeight)
98
99
100 def AcceptsFocus(self):
101 """Overridden base class virtual."""
102 return False
103
104
105 def OnPaint(self, event):
106 dc = wx.BufferedPaintDC(self)
107 #dc = wx.PaintDC(self)
108 width, height = self.GetClientSize()
109 if not width or not height:
110 return
111
112 clr = self.GetBackgroundColour()
113 backBrush = wx.Brush(clr, wx.SOLID)
114 if wx.Platform == "__WXMAC__" and clr == self.defBackClr:
115 # if colour is still the default then use the striped background on Mac
116 backBrush.MacSetTheme(1) # 1 == kThemeBrushDialogBackgroundActive
117 dc.SetBackground(backBrush)
118
119 dc.SetTextForeground(self.GetForegroundColour())
120 dc.Clear()
121 dc.SetFont(self.GetFont())
122 label = self.GetLabel()
123 style = self.GetWindowStyleFlag()
124 x = y = 0
125 for line in label.split('\n'):
126 if line == '':
127 w, h = self.GetTextExtent('W') # empty lines have height too
128 else:
129 w, h = self.GetTextExtent(line)
130 if style & wx.ALIGN_RIGHT:
131 x = width - w
132 if style & wx.ALIGN_CENTER:
133 x = (width - w)/2
134 dc.DrawText(line, (x, y))
135 y += h
136
137
138 def OnEraseBackground(self, event):
139 pass
140
141
142
143
144 #----------------------------------------------------------------------
145
146