]>
Commit | Line | Data |
---|---|---|
cf694132 | 1 | |
8fa876ca RD |
2 | import time |
3 | import wx | |
cf694132 RD |
4 | |
5 | #--------------------------------------------------------------------------- | |
6 | ||
8fa876ca | 7 | class CustomStatusBar(wx.StatusBar): |
cf694132 | 8 | def __init__(self, parent, log): |
8fa876ca RD |
9 | wx.StatusBar.__init__(self, parent, -1) |
10 | ||
11 | # This status bar has three fields | |
cf694132 | 12 | self.SetFieldsCount(3) |
95bfd958 RD |
13 | # Sets the three fields to be relative widths to each other. |
14 | self.SetStatusWidths([-2, -1, -2]) | |
cf694132 | 15 | self.log = log |
1e4a197e | 16 | self.sizeChanged = False |
8fa876ca RD |
17 | self.Bind(wx.EVT_SIZE, self.OnSize) |
18 | self.Bind(wx.EVT_IDLE, self.OnIdle) | |
cf694132 | 19 | |
8fa876ca | 20 | # Field 0 ... just text |
cf694132 RD |
21 | self.SetStatusText("A Custom StatusBar...", 0) |
22 | ||
8fa876ca RD |
23 | # This will fall into field 1 (the second field) |
24 | self.cb = wx.CheckBox(self, 1001, "toggle clock") | |
25 | self.Bind(wx.EVT_CHECKBOX, self.OnToggleClock, self.cb) | |
1e4a197e | 26 | self.cb.SetValue(True) |
cf694132 | 27 | |
c368d904 RD |
28 | # set the initial position of the checkbox |
29 | self.Reposition() | |
cf694132 | 30 | |
8fa876ca RD |
31 | # We're going to use a timer to drive a 'clock' in the last |
32 | # field. | |
33 | self.timer = wx.PyTimer(self.Notify) | |
cf694132 RD |
34 | self.timer.Start(1000) |
35 | self.Notify() | |
36 | ||
37 | ||
8fa876ca RD |
38 | # Handles events from the timer we started in __init__(). |
39 | # We're using it to drive a 'clock' in field 2 (the third field). | |
cf694132 RD |
40 | def Notify(self): |
41 | t = time.localtime(time.time()) | |
42 | st = time.strftime("%d-%b-%Y %I:%M:%S", t) | |
43 | self.SetStatusText(st, 2) | |
44 | self.log.WriteText("tick...\n") | |
45 | ||
c368d904 | 46 | |
cf694132 RD |
47 | # the checkbox was clicked |
48 | def OnToggleClock(self, event): | |
49 | if self.cb.GetValue(): | |
50 | self.timer.Start(1000) | |
51 | self.Notify() | |
52 | else: | |
53 | self.timer.Stop() | |
54 | ||
55 | ||
c368d904 RD |
56 | def OnSize(self, evt): |
57 | self.Reposition() # for normal size events | |
58 | ||
59 | # Set a flag so the idle time handler will also do the repositioning. | |
60 | # It is done this way to get around a buglet where GetFieldRect is not | |
61 | # accurate during the EVT_SIZE resulting from a frame maximize. | |
1e4a197e | 62 | self.sizeChanged = True |
c368d904 RD |
63 | |
64 | ||
65 | def OnIdle(self, evt): | |
66 | if self.sizeChanged: | |
67 | self.Reposition() | |
68 | ||
69 | ||
cf694132 | 70 | # reposition the checkbox |
c368d904 | 71 | def Reposition(self): |
cf694132 | 72 | rect = self.GetFieldRect(1) |
8fa876ca RD |
73 | self.cb.SetPosition((rect.x+2, rect.y+2)) |
74 | self.cb.SetSize((rect.width-4, rect.height-4)) | |
1e4a197e | 75 | self.sizeChanged = False |
cf694132 RD |
76 | |
77 | ||
78 | ||
8fa876ca | 79 | class TestCustomStatusBar(wx.Frame): |
cf694132 | 80 | def __init__(self, parent, log): |
8fa876ca | 81 | wx.Frame.__init__(self, parent, -1, 'Test Custom StatusBar') |
cf694132 RD |
82 | |
83 | self.sb = CustomStatusBar(self, log) | |
84 | self.SetStatusBar(self.sb) | |
8fa876ca | 85 | tc = wx.TextCtrl(self, -1, "", style=wx.TE_READONLY|wx.TE_MULTILINE) |
eb0f373c RD |
86 | |
87 | self.SetSize((500, 300)) | |
8fa876ca | 88 | self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) |
cf694132 RD |
89 | |
90 | def OnCloseWindow(self, event): | |
91 | self.sb.timer.Stop() | |
92 | del self.sb.timer | |
93 | self.Destroy() | |
94 | ||
95 | #--------------------------------------------------------------------------- | |
96 | ||
97 | def runTest(frame, nb, log): | |
98 | win = TestCustomStatusBar(frame, log) | |
99 | frame.otherWin = win | |
1e4a197e | 100 | win.Show(True) |
cf694132 RD |
101 | |
102 | #--------------------------------------------------------------------------- | |
103 | ||
104 | ||
cf694132 | 105 | overview = """\ |
1fded56b RD |
106 | A status bar is a narrow window that can be placed along the bottom of |
107 | a frame to give small amounts of status information. It can contain | |
108 | one or more fields, one or more of which can be variable length | |
8fa876ca | 109 | according to the size of the window. |
1fded56b | 110 | |
8fa876ca RD |
111 | This example demonstrates how to create a custom status bar with actual |
112 | gadgets embedded in it. In this case, the first field is just plain text, | |
113 | The second one has a checkbox that enables the timer, and the third | |
114 | field has a clock that shows the current time when it is enabled. | |
1fded56b | 115 | |
8fa876ca | 116 | """ |
1fded56b RD |
117 | |
118 | ||
119 | if __name__ == '__main__': | |
120 | import sys,os | |
121 | import run | |
8eca4fef | 122 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |