home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / usr / lib / pygtk / 2.0 / demos / pixbufs.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2010-05-11  |  6KB  |  167 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. '''Pixbufs
  5.  
  6. A GdkPixbuf represents an image, normally in RGB or RGBA format.
  7. Pixbufs are normally used to load files from disk and perform image scaling.
  8. This demo is not all that educational, but looks cool. It was written by
  9. Extreme Pixbuf Hacker Federico Mena Quintero. It also shows off how to use
  10. GtkDrawingArea to do a simple animation.
  11. Look at the Image demo for additional pixbuf usage examples.'''
  12. import os
  13. import math
  14. import gobject
  15. import gtk
  16. FRAME_DELAY = 50
  17. CYCLE_LEN = 60
  18. IMAGE_DIR = os.path.join(os.path.dirname(__file__), 'images')
  19. BACKGROUND_NAME = 'background.jpg'
  20. image_names = [
  21.     'apple-red.png',
  22.     'gnome-applets.png',
  23.     'gnome-calendar.png',
  24.     'gnome-foot.png',
  25.     'gnome-gmush.png',
  26.     'gnome-gimp.png',
  27.     'gnome-gsame.png',
  28.     'gnu-keys.png']
  29.  
  30. class PixbufsDemo(gtk.Window):
  31.     frame = None
  32.     background = None
  33.     images = []
  34.     back_width = 0
  35.     back_height = 0
  36.     timeout_id = 0
  37.     frame_num = 0
  38.     timeout_id = None
  39.     
  40.     def __init__(self, parent = None):
  41.         gtk.Window.__init__(self)
  42.         
  43.         try:
  44.             self.set_screen(parent.get_screen())
  45.         except AttributeError:
  46.             self.connect('destroy', (lambda : gtk.main_quit()))
  47.  
  48.         self.connect('destroy', self.cleanup_callback)
  49.         self.set_title(self.__class__.__name__)
  50.         self.set_resizable(False)
  51.         if not self.load_pixbufs():
  52.             dialog = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, 'Failed to load an image')
  53.             dialog.connect('response', (lambda d, r: d.destroy()))
  54.             dialog.show()
  55.         else:
  56.             self.set_size_request(self.back_width, self.back_height)
  57.             self.frame = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.back_width, self.back_height)
  58.             da = gtk.DrawingArea()
  59.             da.connect('expose_event', self.expose_cb)
  60.             self.add(da)
  61.             self.timeout_id = gobject.timeout_add(FRAME_DELAY, self.timeout)
  62.             self.show_all()
  63.  
  64.     
  65.     def load_pixbufs(self):
  66.         ''' Loads the images for the demo and returns whether the
  67.             operation succeeded.
  68.         '''
  69.         if self.background is not None:
  70.             return True
  71.         
  72.         try:
  73.             self.background = gtk.gdk.pixbuf_new_from_file(os.path.join(IMAGE_DIR, BACKGROUND_NAME))
  74.         except gobject.GError:
  75.             self.background is not None
  76.             error = self.background is not None
  77.             return False
  78.  
  79.         self.back_width = self.background.get_width()
  80.         self.back_height = self.background.get_height()
  81.         for filename in image_names:
  82.             
  83.             try:
  84.                 self.images.append(gtk.gdk.pixbuf_new_from_file(os.path.join(IMAGE_DIR, filename)))
  85.             continue
  86.             except gobject.GError:
  87.                 self.background is not None
  88.                 error = self.background is not None
  89.                 return False
  90.             
  91.  
  92.         
  93.         return True
  94.  
  95.     
  96.     def expose_cb(self, draw_area, event):
  97.         ''' Expose callback for the drawing area. '''
  98.         rowstride = self.frame.get_rowstride()
  99.         pixels = self.frame.get_pixels()
  100.         draw_area.window.draw_rgb_image(draw_area.style.black_gc, event.area.x, event.area.y, event.area.width, event.area.height, 'normal', pixels, rowstride, event.area.x, event.area.y)
  101.         return True
  102.  
  103.     
  104.     def cleanup_callback(self, win):
  105.         if self.timeout_id is not None:
  106.             gobject.source_remove(self.timeout_id)
  107.             self.timeout_id = None
  108.         
  109.  
  110.     
  111.     def timeout(self):
  112.         ''' Timeout handler to regenerate the frame. '''
  113.         self.background.copy_area(0, 0, self.back_width, self.back_height, self.frame, 0, 0)
  114.         f = float(self.frame_num % CYCLE_LEN) / float(CYCLE_LEN)
  115.         xmid = self.back_width / 2
  116.         ymid = self.back_height / 2
  117.         radius = min(xmid, ymid) / 2
  118.         N_IMAGES = len(image_names)
  119.         for i_name in image_names:
  120.             i = image_names.index(i_name)
  121.             ang = 2 * math.pi * i / N_IMAGES - f * 2 * math.pi
  122.             iw = self.images[i].get_width()
  123.             ih = self.images[i].get_height()
  124.             r = radius + (radius / 3) * math.sin(f * 2 * math.pi)
  125.             xpos = math.floor((xmid + r * math.cos(ang) - iw / 2) + 0.5)
  126.             ypos = math.floor((ymid + r * math.sin(ang) - ih / 2) + 0.5)
  127.             if i % 2 == 0:
  128.                 k = math.cos(f * 2 * math.pi)
  129.             else:
  130.                 k = math.sin(f * 2 * math.pi)
  131.             k = 2 * k * k
  132.             k = max(0.25, k)
  133.             r1 = gtk.gdk.Rectangle()
  134.             r1.x = int(xpos)
  135.             r1.y = int(ypos)
  136.             r1.width = iw * k
  137.             r1.height = ih * k
  138.             r2 = gtk.gdk.Rectangle()
  139.             r2.x = 0
  140.             r2.y = 0
  141.             r2.width = self.back_width
  142.             r2.height = self.back_height
  143.             dest = r1.intersect(r2)
  144.             if dest is not None:
  145.                 if i % 2 == 0:
  146.                     alpha = int(max(127, math.fabs(255 * math.cos(f * 2 * math.pi))))
  147.                 else:
  148.                     alpha = int(max(127, math.fabs(255 * math.sin(f * 2 * math.pi))))
  149.                 self.images[i].composite(self.frame, dest.x, dest.y, dest.width, dest.height, xpos, ypos, k, k, gtk.gdk.INTERP_NEAREST, alpha)
  150.                 continue
  151.         
  152.         if self is not None:
  153.             self.queue_draw()
  154.         
  155.         self.frame_num += 1
  156.         return True
  157.  
  158.  
  159.  
  160. def main():
  161.     PixbufsDemo()
  162.     gtk.main()
  163.  
  164. if __name__ == '__main__':
  165.     main()
  166.  
  167.