1999-04-29 23:29:54 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
import time
|
|
|
|
import wx
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class CustomStatusBar(wx.StatusBar):
|
1999-04-29 23:29:54 -04:00
|
|
|
def __init__(self, parent, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.StatusBar.__init__(self, parent, -1)
|
|
|
|
|
|
|
|
# This status bar has three fields
|
1999-04-29 23:29:54 -04:00
|
|
|
self.SetFieldsCount(3)
|
2004-01-12 22:17:17 -05:00
|
|
|
# Sets the three fields to be relative widths to each other.
|
|
|
|
self.SetStatusWidths([-2, -1, -2])
|
1999-04-29 23:29:54 -04:00
|
|
|
self.log = log
|
2003-03-25 01:35:27 -05:00
|
|
|
self.sizeChanged = False
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(wx.EVT_SIZE, self.OnSize)
|
|
|
|
self.Bind(wx.EVT_IDLE, self.OnIdle)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Field 0 ... just text
|
1999-04-29 23:29:54 -04:00
|
|
|
self.SetStatusText("A Custom StatusBar...", 0)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# This will fall into field 1 (the second field)
|
|
|
|
self.cb = wx.CheckBox(self, 1001, "toggle clock")
|
|
|
|
self.Bind(wx.EVT_CHECKBOX, self.OnToggleClock, self.cb)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.cb.SetValue(True)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2000-10-30 16:08:42 -05:00
|
|
|
# set the initial position of the checkbox
|
|
|
|
self.Reposition()
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# We're going to use a timer to drive a 'clock' in the last
|
|
|
|
# field.
|
|
|
|
self.timer = wx.PyTimer(self.Notify)
|
1999-04-29 23:29:54 -04:00
|
|
|
self.timer.Start(1000)
|
|
|
|
self.Notify()
|
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Handles events from the timer we started in __init__().
|
|
|
|
# We're using it to drive a 'clock' in field 2 (the third field).
|
1999-04-29 23:29:54 -04:00
|
|
|
def Notify(self):
|
|
|
|
t = time.localtime(time.time())
|
|
|
|
st = time.strftime("%d-%b-%Y %I:%M:%S", t)
|
|
|
|
self.SetStatusText(st, 2)
|
|
|
|
self.log.WriteText("tick...\n")
|
|
|
|
|
2000-10-30 16:08:42 -05:00
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
# the checkbox was clicked
|
|
|
|
def OnToggleClock(self, event):
|
|
|
|
if self.cb.GetValue():
|
|
|
|
self.timer.Start(1000)
|
|
|
|
self.Notify()
|
|
|
|
else:
|
|
|
|
self.timer.Stop()
|
|
|
|
|
|
|
|
|
2000-10-30 16:08:42 -05:00
|
|
|
def OnSize(self, evt):
|
|
|
|
self.Reposition() # for normal size events
|
|
|
|
|
|
|
|
# Set a flag so the idle time handler will also do the repositioning.
|
|
|
|
# It is done this way to get around a buglet where GetFieldRect is not
|
|
|
|
# accurate during the EVT_SIZE resulting from a frame maximize.
|
2003-03-25 01:35:27 -05:00
|
|
|
self.sizeChanged = True
|
2000-10-30 16:08:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
def OnIdle(self, evt):
|
|
|
|
if self.sizeChanged:
|
|
|
|
self.Reposition()
|
|
|
|
|
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
# reposition the checkbox
|
2000-10-30 16:08:42 -05:00
|
|
|
def Reposition(self):
|
1999-04-29 23:29:54 -04:00
|
|
|
rect = self.GetFieldRect(1)
|
2003-12-08 20:23:28 -05:00
|
|
|
self.cb.SetPosition((rect.x+2, rect.y+2))
|
|
|
|
self.cb.SetSize((rect.width-4, rect.height-4))
|
2003-03-25 01:35:27 -05:00
|
|
|
self.sizeChanged = False
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class TestCustomStatusBar(wx.Frame):
|
1999-04-29 23:29:54 -04:00
|
|
|
def __init__(self, parent, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Frame.__init__(self, parent, -1, 'Test Custom StatusBar')
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
self.sb = CustomStatusBar(self, log)
|
|
|
|
self.SetStatusBar(self.sb)
|
2003-12-08 20:23:28 -05:00
|
|
|
tc = wx.TextCtrl(self, -1, "", style=wx.TE_READONLY|wx.TE_MULTILINE)
|
2002-08-13 19:59:08 -04:00
|
|
|
|
|
|
|
self.SetSize((500, 300))
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
def OnCloseWindow(self, event):
|
|
|
|
self.sb.timer.Stop()
|
|
|
|
del self.sb.timer
|
|
|
|
self.Destroy()
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
win = TestCustomStatusBar(frame, log)
|
|
|
|
frame.otherWin = win
|
2003-03-25 01:35:27 -05:00
|
|
|
win.Show(True)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
overview = """\
|
2003-07-02 19:13:10 -04:00
|
|
|
A status bar is a narrow window that can be placed along the bottom of
|
|
|
|
a frame to give small amounts of status information. It can contain
|
|
|
|
one or more fields, one or more of which can be variable length
|
2003-12-08 20:23:28 -05:00
|
|
|
according to the size of the window.
|
2003-07-02 19:13:10 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
This example demonstrates how to create a custom status bar with actual
|
|
|
|
gadgets embedded in it. In this case, the first field is just plain text,
|
|
|
|
The second one has a checkbox that enables the timer, and the third
|
|
|
|
field has a clock that shows the current time when it is enabled.
|
2003-07-02 19:13:10 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
"""
|
2003-07-02 19:13:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
2004-03-04 19:06:33 -05:00
|
|
|
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|