]>
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. 
  17     * Speed of the ticking is controlled by two parameters: 
  19       - Frames per Second(FPS): How many times per second the ticker updates 
  21       - Pixels per Frame(PPF): How many pixels the text moves each update 
  23 Low FPS with high PPF will result in "jumpy" text, lower PPF with higher FPS 
  24 is smoother (but blurrier and more CPU intensive) text. 
  29 #---------------------------------------------------------------------- 
  31 class Ticker(wx
.PyControl
): 
  35             text
=wx
.EmptyString
,        #text in the ticker 
  36             fgcolor 
= wx
.BLACK
,         #text/foreground color 
  37             bgcolor 
= wx
.WHITE
,         #background color 
  38             start
=True,                 #if True, the ticker starts immediately 
  39             ppf
=2,                      #pixels per frame 
  40             fps
=20,                     #frames per second 
  41             direction
="rtl",            #direction of ticking, rtl or ltr 
  42             pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
, style
=wx
.NO_BORDER
,  
  45         wx
.PyControl
.__init
__(self
, parent
, id=id, pos
=pos
, size
=size
, style
=style
, name
=name
) 
  46         self
.timer 
= wx
.Timer(owner
=self
) 
  47         self
._extent 
= (-1, -1)  #cache value for the GetTextExtent call 
  49         self
._fps 
= fps  
#frames per second 
  50         self
._ppf 
= ppf  
#pixels per frame 
  51         self
.SetDirection(direction
) 
  53         self
.SetBestFittingSize(size
) 
  54         self
.SetForegroundColour(fgcolor
) 
  55         self
.SetBackgroundColour(bgcolor
) 
  56         wx
.EVT_TIMER(self
, -1, self
.OnTick
) 
  57         wx
.EVT_PAINT(self
, self
.OnPaint
) 
  58         wx
.EVT_ERASE_BACKGROUND(self
, self
.OnErase
) 
  64         """Stop moving the text""" 
  69         """Starts the text moving""" 
  70         if not self
.timer
.IsRunning(): 
  71             self
.timer
.Start(1000 / self
._fps
) 
  75         """Is the ticker ticking? ie, is the text moving?""" 
  76         return self
.timer
.IsRunning() 
  79     def SetFPS(self
, fps
): 
  80         """Adjust the update speed of the ticker""" 
  87         """Update speed of the ticker""" 
  91     def SetPPF(self
, ppf
): 
  92         """Set the number of pixels per frame the ticker moves - ie, how "jumpy" it is""" 
  97         """Pixels per frame""" 
 101     def SetFont(self
, font
): 
 102         self
._extent 
= (-1, -1) 
 103         wx
.Control
.SetFont(self
, font
) 
 106     def SetDirection(self
, dir): 
 107         """Sets the direction of the ticker: right to left(rtl) or left to right (ltr)""" 
 108         if dir == "ltr" or dir == "rtl": 
 109             if self
._offset 
<> 0: 
 110                 #Change the offset so it's correct for the new direction 
 111                 self
._offset 
= self
._extent
[0] + self
.GetSize()[0] - self
._offset
 
 117     def GetDirection(self
): 
 121     def SetText(self
, text
): 
 122         """Set the ticker text.""" 
 124         self
._extent 
= (-1, -1) 
 126             self
.Refresh() #Refresh here to clear away the old text. 
 133     def UpdateExtent(self
, dc
): 
 134         """Updates the cached text extent if needed""" 
 136             self
._extent 
= (-1, -1) 
 138         if self
._extent 
== (-1, -1): 
 139             self
._extent 
= dc
.GetTextExtent(self
.GetText()) 
 142     def DrawText(self
, dc
): 
 143         """Draws the ticker text at the current offset using the provided DC""" 
 144         dc
.SetTextForeground(self
.GetForegroundColour()) 
 145         dc
.SetFont(self
.GetFont()) 
 146         self
.UpdateExtent(dc
) 
 147         if self
._dir 
== "ltr": 
 148             offx 
= self
._offset 
- self
._extent
[0] 
 150             offx 
= self
.GetSize()[0] - self
._offset
 
 151         offy 
= (self
.GetSize()[1] - self
._extent
[1]) / 2 #centered vertically 
 152         dc
.DrawText(self
._text
, offx
, offy
) 
 155     def OnTick(self
, evt
): 
 156         self
._offset 
+= self
._ppf
 
 157         w1 
= self
.GetSize()[0] 
 159         if self
._offset 
>= w1
+w2
: 
 164     def OnPaint(self
, evt
): 
 165         dc 
= wx
.BufferedPaintDC(self
) 
 166         brush 
= wx
.Brush(self
.GetBackgroundColour()) 
 167         dc
.SetBackground(brush
) 
 172     def OnErase(self
, evt
): 
 173         """Noop because of double buffering""" 
 177     def AcceptsFocus(self
): 
 178         """Non-interactive, so don't accept focus""" 
 182     def DoGetBestSize(self
): 
 183         """Width we don't care about, height is either -1, or the character 
 184         height of our text with a little extra padding 
 186         if self
._extent 
== (-1, -1): 
 188                 h 
= self
.GetCharHeight() 
 190                 h 
= self
.GetTextExtent(self
.GetText())[1] 
 196     def ShouldInheritColours(self
):  
 197         """Don't get colours from our parent...""" 
 203 if __name__ 
== '__main__': 
 204     app 
= wx
.PySimpleApp() 
 207     t 
= Ticker(p
, text
="Some sample ticker text")    
 208     #set ticker properties here if you want 
 209     s 
= wx
.BoxSizer(wx
.VERTICAL
) 
 210     s
.Add(t
, flag
=wx
.GROW
, proportion
=0)