]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/wx/lib/throbber.py
reduce the number of objects for a quicker startup time
[wxWidgets.git] / wxPython / wx / lib / throbber.py
index a4063d4be5fc440d23975c9d68ff7f9aa8d1a90b..2812cdd9691f43f635c2d4b4c7c5cbc76fc3405a 100644 (file)
@@ -16,6 +16,11 @@ can continue unencumbered.
 #
 # $Id$
 #
 #
 # $Id$
 #
+# 12/12/2003 - Jeff Grimmett (grimmtooth@softhome.net)
+#
+# o 2.5 compatability update.
+#
+
 
 import os
 import wx
 
 import os
 import wx
@@ -23,8 +28,7 @@ import wx
 # ------------------------------------------------------------------------------
 
 THROBBER_EVENT = wx.NewEventType()
 # ------------------------------------------------------------------------------
 
 THROBBER_EVENT = wx.NewEventType()
-def EVT_UPDATE_THROBBER(win, func):
-    win.Connect(-1, -1, THROBBER_EVENT, func)
+EVT_UPDATE_THROBBER = wx.PyEventBinder(THROBBER_EVENT, 0)
 
 class UpdateThrobberEvent(wx.PyEvent):
     def __init__(self):
 
 class UpdateThrobberEvent(wx.PyEvent):
     def __init__(self):
@@ -33,7 +37,7 @@ class UpdateThrobberEvent(wx.PyEvent):
 
 # ------------------------------------------------------------------------------
 
 
 # ------------------------------------------------------------------------------
 
-class Throbber(wx.Panel):
+class Throbber(wx.PyPanel):
     """
     The first argument is either the name of a file that will be split into frames
     (a composite image) or a list of  strings of image names that will be treated
     """
     The first argument is either the name of a file that will be split into frames
     (a composite image) or a list of  strings of image names that will be treated
@@ -55,8 +59,13 @@ class Throbber(wx.Panel):
                  overlay = None,  # optional image to overlay on animation
                  reverse = 0,     # reverse direction at end of animation
                  style = 0,       # window style
                  overlay = None,  # optional image to overlay on animation
                  reverse = 0,     # reverse direction at end of animation
                  style = 0,       # window style
-                 name = "throbber"):
-        wx.Panel.__init__(self, parent, id, pos, size, style, name)
+                 name = "throbber",
+                 rest = 0,
+                 current = 0,
+                 direction = 1,
+                 sequence = None
+                 ):
+        wx.PyPanel.__init__(self, parent, id, pos, size, style, name)
         self.name = name
         self.label = label
         self.running = (1 != 1)
         self.name = name
         self.label = label
         self.running = (1 != 1)
@@ -85,8 +94,9 @@ class Throbber(wx.Panel):
             self.labelX = (width - extentX)/2
             self.labelY = (height - extentY)/2
         self.frameDelay = frameDelay
             self.labelX = (width - extentX)/2
             self.labelY = (height - extentY)/2
         self.frameDelay = frameDelay
-        self.current = 0
-        self.direction = 1
+        self.rest = rest
+        self.current = current
+        self.direction = direction
         self.autoReverse = reverse
         self.overlay = overlay
         if overlay is not None:
         self.autoReverse = reverse
         self.overlay = overlay
         if overlay is not None:
@@ -112,18 +122,22 @@ class Throbber(wx.Panel):
         # while the throbber is running.  self.sequence[0] should always
         # refer to whatever frame is to be shown when 'resting' and be sure
         # that no item in self.sequence >= self.frames or < 0!!!
         # while the throbber is running.  self.sequence[0] should always
         # refer to whatever frame is to be shown when 'resting' and be sure
         # that no item in self.sequence >= self.frames or < 0!!!
-        self.sequence = range(self.frames)
+        self.SetSequence(sequence)
 
         self.SetClientSize((width, height))
 
         timerID  = wx.NewId()
         self.timer = wx.Timer(self, timerID)
 
 
         self.SetClientSize((width, height))
 
         timerID  = wx.NewId()
         self.timer = wx.Timer(self, timerID)
 
-        EVT_UPDATE_THROBBER(self, self.Rotate)
-        wx.EVT_PAINT(self, self.OnPaint)
-        wx.EVT_TIMER(self, timerID, self.OnTimer)
-        wx.EVT_WINDOW_DESTROY(self, self.OnDestroyWindow)
-            
+        self.Bind(EVT_UPDATE_THROBBER, self.Update)
+        self.Bind(wx.EVT_PAINT, self.OnPaint)
+        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
+        self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroyWindow)
+
+
+    def DoGetBestSize(self):
+        return (self.width, self.height)
+    
 
     def OnTimer(self, event):
         wx.PostEvent(self, UpdateThrobberEvent())
 
     def OnTimer(self, event):
         wx.PostEvent(self, UpdateThrobberEvent())
@@ -135,13 +149,13 @@ class Throbber(wx.Panel):
 
 
     def Draw(self, dc):
 
 
     def Draw(self, dc):
-        dc.DrawBitmap(self.submaps[self.sequence[self.current]], (0, 0), True)
+        dc.DrawBitmap(self.submaps[self.sequence[self.current]], 0, 0, True)
         if self.overlay and self.showOverlay:
         if self.overlay and self.showOverlay:
-            dc.DrawBitmap(self.overlay, (self.overlayX, self.overlayY), True)
+            dc.DrawBitmap(self.overlay, self.overlayX, self.overlayY, True)
         if self.label and self.showLabel:
         if self.label and self.showLabel:
-            dc.DrawText(self.label, (self.labelX, self.labelY))
+            dc.DrawText(self.label, self.labelX, self.labelY)
             dc.SetTextForeground(wx.WHITE)
             dc.SetTextForeground(wx.WHITE)
-            dc.DrawText(self.label, (self.labelX-1, self.labelY-1))
+            dc.DrawText(self.label, self.labelX-1, self.labelY-1)
 
 
     def OnPaint(self, event):
 
 
     def OnPaint(self, event):
@@ -149,18 +163,21 @@ class Throbber(wx.Panel):
         event.Skip()
 
 
         event.Skip()
 
 
-    def Rotate(self, event):
-        self.current += self.direction
+    def Update(self, event):
+        self.Next()
+
+
+    def Wrap(self):
         if self.current >= len(self.sequence):
             if self.autoReverse:
                 self.Reverse()
                 self.current = len(self.sequence) - 1
             else:
         if self.current >= len(self.sequence):
             if self.autoReverse:
                 self.Reverse()
                 self.current = len(self.sequence) - 1
             else:
-                self.current = 1
-        if self.current < 1:
+                self.current = 0
+        if self.current < 0:
             if self.autoReverse:
                 self.Reverse()
             if self.autoReverse:
                 self.Reverse()
-                self.current = 1
+                self.current = 0
             else:
                 self.current = len(self.sequence) - 1
         self.Draw(wx.ClientDC(self))
             else:
                 self.current = len(self.sequence) - 1
         self.Draw(wx.ClientDC(self))
@@ -177,7 +194,7 @@ class Throbber(wx.Panel):
     def Rest(self):
         """Stop the animation and return to frame 0"""
         self.Stop()
     def Rest(self):
         """Stop the animation and return to frame 0"""
         self.Stop()
-        self.current = 0
+        self.current = self.rest
         self.Draw(wx.ClientDC(self))
 
 
         self.Draw(wx.ClientDC(self))
 
 
@@ -205,6 +222,65 @@ class Throbber(wx.Panel):
             self.running = not self.running
 
 
             self.running = not self.running
 
 
+    def SetCurrent(self, current):
+        """Set current image"""
+        running = self.Running()
+        if not running:
+            #FIXME: need to make sure value is within range!!!
+            self.current = current
+            self.Draw(wx.ClientDC(self))
+
+
+    def SetRest(self, rest):
+        """Set rest image"""
+        self.rest = rest
+
+
+    def SetSequence(self, sequence = None):
+        """Order to display images"""
+
+        # self.sequence can be changed, but it's not recommended doing it
+        # while the throbber is running.  self.sequence[0] should always
+        # refer to whatever frame is to be shown when 'resting' and be sure
+        # that no item in self.sequence >= self.frames or < 0!!!
+
+        running = self.Running()
+        self.Stop()
+
+        if sequence is not None:
+            #FIXME: need to make sure values are within range!!!
+            self.sequence = sequence
+        else:
+            self.sequence = range(self.frames)
+
+        if running:
+            self.Start()
+            
+
+    def Increment(self):
+        """Display next image in sequence"""
+        self.current += 1
+        self.Wrap()
+
+
+    def Decrement(self):
+        """Display previous image in sequence"""
+        self.current -= 1
+        self.Wrap()
+
+
+    def Next(self):
+        """Display next image in sequence according to direction"""
+        self.current += self.direction
+        self.Wrap()
+
+
+    def Previous(self):
+        """Display previous image in sequence according to direction"""
+        self.current -= self.direction
+        self.Wrap()
+
+
     def SetFrameDelay(self, frameDelay = 0.05):
         """Delay between each frame"""
         self.frameDelay = frameDelay
     def SetFrameDelay(self, frameDelay = 0.05):
         """Delay between each frame"""
         self.frameDelay = frameDelay