]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/stattext.py
Applied patch that converts the throbber to using timers instead of threads
[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|wxNO_BORDER,
30 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 self.defBackClr = parent.GetBackgroundColour()
40 if not self.defBackClr.Ok():
41 self.defBackClr = wxSystemSettings_GetSystemColour(wxSYS_COLOUR_3DFACE)
42 self.SetBackgroundColour(self.defBackClr)
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 self.Refresh()
69
70
71 def SetFont(self, font):
72 """
73 Sets the static text font and updates the control's size to exactly
74 fit the label unless the control has wxST_NO_AUTORESIZE flag.
75 """
76 wxPyControl.SetFont(self, font)
77 style = self.GetWindowStyleFlag()
78 if not style & wxST_NO_AUTORESIZE:
79 self.SetSize(self.GetBestSize())
80 self.Refresh()
81
82
83 def DoGetBestSize(self):
84 """Overridden base class virtual. Determines the best size of the
85 button based on the label size."""
86 label = self.GetLabel()
87 maxWidth = totalHeight = 0
88 for line in label.split('\n'):
89 if line == '':
90 w, h = self.GetTextExtent('W') # empty lines have height too
91 else:
92 w, h = self.GetTextExtent(line)
93 totalHeight += h
94 maxWidth = max(maxWidth, w)
95 return wxSize(maxWidth, totalHeight)
96
97
98 def AcceptsFocus(self):
99 """Overridden base class virtual."""
100 return False
101
102
103 def OnPaint(self, event):
104 dc = wxBufferedPaintDC(self)
105 #dc = wxPaintDC(self)
106 width, height = self.GetClientSize()
107 if not width or not height:
108 return
109
110 clr = self.GetBackgroundColour()
111 backBrush = wxBrush(clr, wxSOLID)
112 if wxPlatform == "__WXMAC__" and clr == self.defBackClr:
113 # if colour still the default the use the striped background on Mac
114 backBrush.SetMacTheme(1) # 1 == kThemeBrushDialogBackgroundActive
115 dc.SetBackground(backBrush)
116
117 dc.SetTextForeground(self.GetForegroundColour())
118 dc.Clear()
119 dc.SetFont(self.GetFont())
120 label = self.GetLabel()
121 style = self.GetWindowStyleFlag()
122 x = y = 0
123 for line in label.split('\n'):
124 if line == '':
125 w, h = self.GetTextExtent('W') # empty lines have height too
126 else:
127 w, h = self.GetTextExtent(line)
128 if style & wxALIGN_RIGHT:
129 x = width - w
130 if style & wxALIGN_CENTER:
131 x = (width - w)/2
132 dc.DrawText(line, x, y)
133 y += h
134
135
136 def OnEraseBackground(self, event):
137 pass
138
139
140
141
142 #----------------------------------------------------------------------
143
144