]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
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 | ||
15 | """ | |
16 | """ | |
17 | ||
18 | import wx | |
19 | ||
20 | #---------------------------------------------------------------------- | |
21 | ||
22 | class GenStaticText(wx.PyControl): | |
23 | labelDelta = 1 | |
24 | ||
25 | def __init__(self, parent, ID, label, | |
26 | pos = wx.DefaultPosition, size = wx.DefaultSize, | |
27 | style = 0, | |
28 | name = "genstattext"): | |
29 | wx.PyControl.__init__(self, parent, ID, pos, size, style|wx.NO_BORDER, | |
30 | wx.DefaultValidator, name) | |
31 | ||
32 | wx.PyControl.SetLabel(self, label) # don't check wx.ST_NO_AUTORESIZE yet | |
33 | self.SetPosition(pos) | |
34 | font = parent.GetFont() | |
35 | if not font.Ok(): | |
36 | font = wx.SystemSettings.GetSystemFont(wx.SYS_DEFAULT_GUI_FONT) | |
37 | wx.PyControl.SetFont(self, font) # same here | |
38 | ||
39 | self.defBackClr = parent.GetBackgroundColour() | |
40 | if not self.defBackClr.Ok(): | |
41 | self.defBackClr = wx.SystemSettings.GetSystemColour(wx.SYS_COLOUR_3DFACE) | |
42 | self.SetBackgroundColour(self.defBackClr) | |
43 | ||
44 | clr = parent.GetForegroundColour() | |
45 | if not clr.Ok(): | |
46 | clr = wx.SystemSettings_GetSystemColour(wx.SYS_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(wx.Size(rw, rh)) | |
54 | ||
55 | wx.EVT_ERASE_BACKGROUND(self, self.OnEraseBackground) | |
56 | wx.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 wx.ST_NO_AUTORESIZE flag. | |
63 | """ | |
64 | wx.PyControl.SetLabel(self, label) | |
65 | style = self.GetWindowStyleFlag() | |
66 | if not style & wx.ST_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 wx.ST_NO_AUTORESIZE flag. | |
75 | """ | |
76 | wx.PyControl.SetFont(self, font) | |
77 | style = self.GetWindowStyleFlag() | |
78 | if not style & wx.ST_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 wx.Size(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 = wx.BufferedPaintDC(self) | |
105 | #dc = wx.PaintDC(self) | |
106 | width, height = self.GetClientSize() | |
107 | if not width or not height: | |
108 | return | |
109 | ||
110 | clr = self.GetBackgroundColour() | |
111 | backBrush = wx.Brush(clr, wx.SOLID) | |
112 | if wx.Platform == "__WXMAC__" and clr == self.defBackClr: | |
113 | # if colour is still the default then 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 & wx.ALIGN_RIGHT: | |
129 | x = width - w | |
130 | if style & wx.ALIGN_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 | #---------------------------------------------------------------------- | |
1fded56b | 143 | |
1fded56b | 144 |