home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / system-config-printer / gtkspinner.py < prev    next >
Encoding:
Python Source  |  2010-09-28  |  2.5 KB  |  83 lines

  1. #!/usr/bin/env python
  2.  
  3. ## system-config-printer
  4.  
  5. ## Copyright (C) 2009, 2010 Red Hat, Inc
  6. ## Author: Tim Waugh <twaugh@redhat.com>
  7.  
  8. ## This program is free software; you can redistribute it and/or modify
  9. ## it under the terms of the GNU General Public License as published by
  10. ## the Free Software Foundation; either version 2 of the License, or
  11. ## (at your option) any later version.
  12.  
  13. ## This program is distributed in the hope that it will be useful,
  14. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ## GNU General Public License for more details.
  17.  
  18. ## You should have received a copy of the GNU General Public License
  19. ## along with this program; if not, write to the Free Software
  20. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. import glib
  23. import gobject
  24. import gtk
  25.  
  26. class Spinner:
  27.     def __init__ (self, image):
  28.         self.image = image
  29.         frames = []
  30.         theme = gtk.icon_theme_get_default ()
  31.         icon_info = theme.lookup_icon ("process-working", 22, 0)
  32.         if icon_info != None:
  33.             size = icon_info.get_base_size ()
  34.             icon = icon_info.get_filename ()
  35.             try:
  36.                 pixbuf = gtk.gdk.pixbuf_new_from_file (icon)
  37.                 grid_width = pixbuf.get_width ()
  38.                 grid_height = pixbuf.get_height ()
  39.                 y = 0
  40.                 while y < grid_height:
  41.                     x = 0
  42.                     while x < grid_width:
  43.                         frame = pixbuf.subpixbuf (x, y, size, size)
  44.                         frames.append (frame)
  45.                         x += size
  46.  
  47.                     y += size
  48.             except gobject.GError:
  49.                 # Failed to load icon.
  50.                 pass
  51.  
  52.         self.frames = frames
  53.         self.n_frames = len (frames)
  54.         self._rest ()
  55.  
  56.     def _set_frame (self, n):
  57.         self._current_frame = n
  58.         if self.n_frames < 2:
  59.             self.image.clear ()
  60.             return
  61.  
  62.         self.image.set_from_pixbuf (self.frames[n])
  63.  
  64.     def _rest (self):
  65.         self._set_frame (0)
  66.  
  67.     def _next_frame (self):
  68.         n = self._current_frame + 1
  69.         if n >= self.n_frames:
  70.             n = 1
  71.  
  72.         gtk.gdk.threads_enter ()
  73.         self._set_frame (n)
  74.         gtk.gdk.threads_leave ()
  75.         return True
  76.  
  77.     def start (self, timeout=125):
  78.         self._task = glib.timeout_add (timeout, self._next_frame)
  79.  
  80.     def stop (self):
  81.         glib.source_remove (self._task)
  82.         self._rest ()
  83.