]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/ticker.py
1 #----------------------------------------------------------------------
3 # Purpose: A news-ticker style scrolling text control
9 # Copyright: (c) 2004 by Chris Mellon
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------
13 """News-ticker style scrolling text control
15 Can scroll from right to left or left to right.
16 Speed of the ticking is controlled by two parameters:
17 Frames per Second(FPS): How many times per second the ticker updates
18 Pixels per Frame(PPF): How many pixels the text moves each update
19 Low FPS with high PPF will result in "jumpy" text, lower PPF with higher FPS
20 is smoother (but blurrier and more CPU intensive) text.
25 #----------------------------------------------------------------------
27 class Ticker(wx
.PyControl
):
31 text
=wx
.EmptyString
, #text in the ticker
32 fgcolor
= wx
.BLACK
, #text/foreground color
33 bgcolor
= wx
.WHITE
, #background color
34 start
=True, #if True, the ticker starts immediately
35 ppf
=2, #pixels per frame
36 fps
=20, #frames per second
37 direction
="rtl", #direction of ticking, rtl or ltr
38 pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
, style
=wx
.NO_BORDER
,
41 wx
.PyControl
.__init
__(self
, parent
, id=id, pos
=pos
, size
=size
, style
=style
, name
=name
)
42 self
.timer
= wx
.Timer(owner
=self
)
43 self
._extent
= (-1, -1) #cache value for the GetTextExtent call
45 self
._fps
= fps
#frames per second
46 self
._ppf
= ppf
#pixels per frame
47 self
.SetDirection(direction
)
49 self
.SetBestFittingSize(size
)
50 self
.SetForegroundColour(fgcolor
)
51 self
.SetBackgroundColour(bgcolor
)
52 wx
.EVT_TIMER(self
, -1, self
.OnTick
)
53 wx
.EVT_PAINT(self
, self
.OnPaint
)
54 wx
.EVT_ERASE_BACKGROUND(self
, self
.OnErase
)
60 """Stop moving the text"""
65 """Starts the text moving"""
66 if not self
.timer
.IsRunning():
67 self
.timer
.Start(1000 / self
._fps
)
71 """Is the ticker ticking? ie, is the text moving?"""
72 return self
.timer
.IsRunning()
75 def SetFPS(self
, fps
):
76 """Adjust the update speed of the ticker"""
83 """Update speed of the ticker"""
87 def SetPPF(self
, ppf
):
88 """Set the number of pixels per frame the ticker moves - ie, how "jumpy" it is"""
93 """Pixels per frame"""
97 def SetFont(self
, font
):
98 self
._extent
= (-1, -1)
99 wx
.Control
.SetFont(self
, font
)
102 def SetDirection(self
, dir):
103 """Sets the direction of the ticker: right to left(rtl) or left to right (ltr)"""
104 if dir == "ltr" or dir == "rtl":
105 if self
._offset
<> 0:
106 #Change the offset so it's correct for the new direction
107 self
._offset
= self
._extent
[0] + self
.GetSize()[0] - self
._offset
113 def GetDirection(self
):
117 def SetText(self
, text
):
118 """Set the ticker text."""
120 self
._extent
= (-1, -1)
122 self
.Refresh() #Refresh here to clear away the old text.
129 def UpdateExtent(self
, dc
):
130 """Updates the cached text extent if needed"""
132 self
._extent
= (-1, -1)
134 if self
._extent
== (-1, -1):
135 self
._extent
= dc
.GetTextExtent(self
.GetText())
138 def DrawText(self
, dc
):
139 """Draws the ticker text at the current offset using the provided DC"""
140 dc
.SetTextForeground(self
.GetForegroundColour())
141 dc
.SetFont(self
.GetFont())
142 self
.UpdateExtent(dc
)
143 if self
._dir
== "ltr":
144 offx
= self
._offset
- self
._extent
[0]
146 offx
= self
.GetSize()[0] - self
._offset
147 offy
= (self
.GetSize()[1] - self
._extent
[1]) / 2 #centered vertically
148 dc
.DrawText(self
._text
, offx
, offy
)
151 def OnTick(self
, evt
):
152 self
._offset
+= self
._ppf
153 w1
= self
.GetSize()[0]
155 if self
._offset
>= w1
+w2
:
160 def OnPaint(self
, evt
):
161 dc
= wx
.BufferedPaintDC(self
)
162 brush
= wx
.Brush(self
.GetBackgroundColour())
163 dc
.SetBackground(brush
)
168 def OnErase(self
, evt
):
169 """Noop because of double buffering"""
173 def AcceptsFocus(self
):
174 """Non-interactive, so don't accept focus"""
178 def DoGetBestSize(self
):
179 """Width we don't care about, height is either -1, or the character
180 height of our text with a little extra padding
182 if self
._extent
== (-1, -1):
184 h
= self
.GetCharHeight()
186 h
= self
.GetTextExtent(self
.GetText())[1]
192 def ShouldInheritColours(self
):
193 """Don't get colours from our parent..."""
199 if __name__
== '__main__':
200 app
= wx
.PySimpleApp()
203 t
= Ticker(p
, text
="Some sample ticker text")
204 #set ticker properties here if you want
205 s
= wx
.BoxSizer(wx
.VERTICAL
)
206 s
.Add(t
, flag
=wx
.GROW
, proportion
=0)