File: ticker.py

package info (click to toggle)
wxpython4.0 4.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 232,540 kB
  • sloc: cpp: 958,937; python: 233,059; ansic: 150,441; makefile: 51,662; sh: 8,687; perl: 1,563; javascript: 584; php: 326; xml: 200
file content (294 lines) | stat: -rw-r--r-- 7,922 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#----------------------------------------------------------------------
# Name:        wx.lib.ticker
# Purpose:     A news-ticker style scrolling text control
#
# Author:      Chris Mellon
#
# Created:     29-Aug-2004
# Copyright:   (c) 2004 by Chris Mellon
# Licence:     wxWindows license
# Tags:        phoenix-port, unittest, documented, py3-port
#----------------------------------------------------------------------

"""
News-ticker style scrolling text control

    * Can scroll from right to left or left to right.

    * Speed of the ticking is controlled by two parameters:

      - Frames per Second(FPS): How many times per second the ticker updates

      - Pixels per Frame(PPF): How many pixels the text moves each update

Low FPS with high PPF will result in "jumpy" text, lower PPF with higher FPS
is smoother (but blurrier and more CPU intensive) text.
"""

import wx

#----------------------------------------------------------------------

class Ticker(wx.Control):
    def __init__(self,
            parent,
            id=-1,
            text=wx.EmptyString,
            fgcolor = wx.BLACK,
            bgcolor = wx.WHITE,
            start=True,
            ppf=2,
            fps=20,
            direction="rtl",
            pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.NO_BORDER,
            name="Ticker"
        ):
        """
        Default class constructor.

        :param wx.Window `parent`: the parent
        :param integer `id`: an identifier for the control: a value of -1 is taken to mean a default
        :param string `text`: text in the ticker
        :param wx.Colour `fgcolor`: text/foreground color
        :param wx.Colour `bgcolor`: background color
        :param boolean `start`: if True, the ticker starts immediately
        :param int `ppf`: pixels per frame
        :param int `fps`: frames per second
        :param `direction`: direction of ticking, 'rtl' or 'ltr'
        :param wx.Point `pos`: the control position. A value of (-1, -1) indicates a default position,
         chosen by either the windowing system or wxPython, depending on platform
        :param `name`: the control name

        """
        wx.Control.__init__(self, parent, id=id, pos=pos, size=size, style=style, name=name)
        self.timer = wx.Timer(owner=self)
        self._extent = (-1, -1)  #cache value for the GetTextExtent call
        self._offset = 0
        self._fps = fps  #frames per second
        self._ppf = ppf  #pixels per frame
        self.SetDirection(direction)
        self.SetText(text)
        self.SetInitialSize(size)
        self.SetForegroundColour(fgcolor)
        self.SetBackgroundColour(bgcolor)
        self.Bind(wx.EVT_TIMER, self.OnTick)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnErase)
        if start:
            self.Start()


    def Stop(self):
        """Stop moving the text"""
        self.timer.Stop()


    def Start(self):
        """Starts the text moving"""
        if not self.timer.IsRunning():
            self.timer.Start(1000 // self._fps)


    def IsTicking(self):
        """Is the ticker ticking? ie, is the text moving?"""
        return self.timer.IsRunning()


    def SetFPS(self, fps):
        """
        Adjust the update speed of the ticker.

        :param int `fps`: frames per second.

        """
        self._fps = fps
        self.Stop()
        self.Start()


    def GetFPS(self):
        """
        Get the frames per second speed of the ticker.
        """
        return self._fps


    def SetPPF(self, ppf):
        """
        Set the number of pixels per frame the ticker moves - ie,
        how "jumpy" it is.

        :param int `ppf`: the pixels per frame setting.

        """
        self._ppf = ppf


    def GetPPF(self):
        """Get pixels per frame setting."""
        return self._ppf


    def SetFont(self, font):
        """
        Set the font for the control.

        :param wx.Font `font`: the font to be used.

        """
        self._extent = (-1, -1)
        wx.Control.SetFont(self, font)


    def SetDirection(self, dir):
        """
        Sets the direction of the ticker: right to left (rtl) or
        left to right (ltr).

        :param `dir`: the direction 'rtl' or 'ltr'

        """
        if dir == "ltr" or dir == "rtl":
            if self._offset != 0:
                #Change the offset so it's correct for the new direction
                self._offset = self._extent[0] + self.GetSize()[0] - self._offset
            self._dir = dir
        else:
            raise TypeError


    def GetDirection(self):
        """Get the set direction."""
        return self._dir


    def SetText(self, text):
        """
        Set the ticker text.

        :param string `text`: the ticker text

        """
        self._text = text
        self._extent = (-1, -1)
        if not self._text:
            self.Refresh() #Refresh here to clear away the old text.


    def GetText(self):
        """Get the current ticker text."""
        return self._text


    def UpdateExtent(self, dc):
        """
        Updates the cached text extent if needed.

        :param wx.DC `dc`: the dc to use.

        """
        if not self._text:
            self._extent = (-1, -1)
            return
        if self._extent == (-1, -1):
            self._extent = dc.GetTextExtent(self.GetText())


    def DrawText(self, dc):
        """
        Draws the ticker text at the current offset using the provided DC.

        :param wx.DC `dc`: the dc to use.

        """
        dc.SetTextForeground(self.GetForegroundColour())
        dc.SetFont(self.GetFont())
        self.UpdateExtent(dc)
        if self._dir == "ltr":
            offx = self._offset - self._extent[0]
        else:
            offx = self.GetSize()[0] - self._offset
        offy = (self.GetSize()[1] - self._extent[1]) // 2 #centered vertically
        dc.DrawText(self._text, offx, offy)


    def OnTick(self, evt):
        """
        Handles the ``wx.EVT_TIMER`` event for :class:`Ticker`.

        :param `evt`: a :class:`TimerEvent` event to be processed.

        """
        self._offset += self._ppf
        w1 = self.GetSize()[0]
        w2 = self._extent[0]
        if self._offset >= w1+w2:
            self._offset = 0
        self.Refresh()


    def OnPaint(self, evt):
        """
        Handles the ``wx.EVT_PAINT`` event for :class:`Ticker`.

        :param `evt`: a :class:`PaintEvent` event to be processed.

        """
        dc = wx.BufferedPaintDC(self)
        brush = wx.Brush(self.GetBackgroundColour())
        dc.SetBackground(brush)
        dc.Clear()
        self.DrawText(dc)


    def OnErase(self, evt):
        """
        Noop because of double buffering

        Handles the ``wx.EVT_ERASE_BACKGROUND`` event for :class:`Ticker`.

        :param `evt`: a :class:`EraseEvent` event to be processed.

        """
        pass


    def AcceptsFocus(self):
        """Non-interactive, so don't accept focus"""
        return False


    def DoGetBestSize(self):
        """
        Width we don't care about, height is either -1, or the character
        height of our text with a little extra padding
        """
        if self._extent == (-1, -1):
            if not self._text:
                h = self.GetCharHeight()
            else:
                h = self.GetTextExtent(self.GetText())[1]
        else:
            h = self._extent[1]
        return (100, h+5)


    def ShouldInheritColours(self):
        """Don't get colours from our parent."""
        return False



#testcase/demo
if __name__ == '__main__':
    app = wx.App()
    f = wx.Frame(None)
    p = wx.Panel(f)
    t = Ticker(p, text="Some sample ticker text")
    #set ticker properties here if you want
    s = wx.BoxSizer(wx.VERTICAL)
    s.Add(t, flag=wx.GROW, proportion=0)
    p.SetSizer(s)
    f.Show()
    app.MainLoop()