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