]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/stattext.py
d93b16d156ad2c0dc9d3042f3e47f15867cbaa28
[wxWidgets.git] / wxPython / wxPython / lib / stattext.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.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
15 """
16 """
17
18 from wxPython.wx import *
19
20 #----------------------------------------------------------------------
21
22 class wxGenStaticText(wxPyControl):
23 labelDelta = 1
24
25 def __init__(self, parent, ID, label,
26 pos = wxDefaultPosition, size = wxDefaultSize,
27 style = 0,
28 name = "genstattext"):
29 wxPyControl.__init__(self, parent, ID, pos, size, style, wxDefaultValidator, name)
30
31 wxPyControl.SetLabel(self, label) # don't check wxST_NO_AUTORESIZE yet
32 self.SetPosition(pos)
33 font = parent.GetFont()
34 if not font.Ok():
35 font = wxSystemSettings_GetSystemFont(wxSYS_DEFAULT_GUI_FONT)
36 wxPyControl.SetFont(self, font) # same here
37
38 clr = parent.GetBackgroundColour()
39 if not clr.Ok():
40 clr = wxSystemSettings_GetSystemColour(wxSYS_COLOUR_BTNFACE)
41 self.SetBackgroundColour(clr)
42
43 clr = parent.GetForegroundColour()
44 if not clr.Ok():
45 clr = wxSystemSettings_GetSystemColour(wxSYS_COLOUR_BTNTEXT)
46 self.SetForegroundColour(clr)
47
48 rw, rh = size
49 bw, bh = self.GetBestSize()
50 if rw == -1: rw = bw
51 if rh == -1: rh = bh
52 self.SetSize(wxSize(rw, rh))
53
54 EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
55 EVT_PAINT(self, self.OnPaint)
56
57
58 def SetLabel(self, label):
59 """
60 Sets the static text label and updates the control's size to exactly
61 fit the label unless the control has wxST_NO_AUTORESIZE flag.
62 """
63 wxPyControl.SetLabel(self, label)
64 style = self.GetWindowStyleFlag()
65 if not style & wxST_NO_AUTORESIZE:
66 self.SetSize(self.GetBestSize())
67 self.Refresh()
68
69
70 def SetFont(self, font):
71 """
72 Sets the static text font and updates the control's size to exactly
73 fit the label unless the control has wxST_NO_AUTORESIZE flag.
74 """
75 wxPyControl.SetFont(self, font)
76 style = self.GetWindowStyleFlag()
77 if not style & wxST_NO_AUTORESIZE:
78 self.SetSize(self.GetBestSize())
79 self.Refresh()
80
81
82 def DoGetBestSize(self):
83 """Overridden base class virtual. Determines the best size of the
84 button based on the label size."""
85 label = self.GetLabel()
86 maxWidth = totalHeight = 0
87 for line in label.split('\n'):
88 if line == '':
89 w, h = self.GetTextExtent('W') # empty lines have height too
90 else:
91 w, h = self.GetTextExtent(line)
92 totalHeight += h
93 maxWidth = max(maxWidth, w)
94 return wxSize(maxWidth, totalHeight)
95
96
97 def AcceptsFocus(self):
98 """Overridden base class virtual."""
99 return False
100
101
102 def OnPaint(self, event):
103 dc = wxBufferedPaintDC(self)
104 #dc = wxPaintDC(self)
105 width, height = self.GetClientSize()
106 if not width or not height:
107 return
108 dc.SetBackground(wxBrush(self.GetBackgroundColour(), wxSOLID))
109 dc.SetTextForeground(self.GetForegroundColour())
110 dc.Clear()
111 dc.SetFont(self.GetFont())
112 label = self.GetLabel()
113 style = self.GetWindowStyleFlag()
114 x = y = 0
115 for line in label.split('\n'):
116 if line == '':
117 w, h = self.GetTextExtent('W') # empty lines have height too
118 else:
119 w, h = self.GetTextExtent(line)
120 if style & wxALIGN_RIGHT:
121 x = width - w
122 if style & wxALIGN_CENTER:
123 x = (width - w)/2
124 dc.DrawText(line, x, y)
125 y += h
126
127
128 def OnEraseBackground(self, event):
129 pass
130
131
132
133
134 #----------------------------------------------------------------------
135
136