Advertisement
tuxor

bubble.py

Dec 8th, 2011
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. #! /usr/bin/env python
  2. import pygtk
  3. pygtk.require('2.0')
  4. import gtk, cairo
  5. from sys import argv,exit
  6. from math import pi
  7. from glib import timeout_add
  8.  
  9. class Icon(object):
  10.   def __init__(self, cr, width, height,alpha,png_file):
  11.    img = cairo.ImageSurface.create_from_png(png_file)
  12.    img_width=img.get_width()
  13.    img_height=img.get_height()  
  14.    
  15.    self.alpha = alpha
  16.    scalew=0.8*width/img_width
  17.    scaleh=0.8*height/img_height
  18.    size=min(scalew,scaleh)
  19.    cr.scale(size,size)
  20.  
  21.    cr.set_source_rgba(0.07,0.07,0.07,self.alpha)
  22.    self.draw_rounded_rectangle(cr, 0, 0, width/(size*10.0), width/size,height/size)
  23.    cr.fill()
  24.  
  25.    cr.set_source_surface(img,0.1*width/size+0.5*(0.8*width/size-img_width),0.1*height/size+0.5*(0.8*height/size-img_height))
  26.    cr.paint()
  27.  
  28.   def draw_rounded_rectangle (self, cr, x, y, radius, width, height):
  29.    cr.move_to(x + radius, y)
  30.    cr.line_to(x + width - radius,y)
  31.    cr.arc (x + width - radius, y + radius,radius,-90.0 * pi / 180.0,0.0 * pi / 180.0)
  32.    cr.line_to(x + width, y + height - radius)
  33.    cr.arc (x + width - radius,y + height - radius,radius,0.0 * pi / 180.0,90.0 * pi / 180.0)
  34.    cr.line_to(x + radius,y + height)
  35.    cr.arc (x + radius,y + height - radius,radius,90.0 * pi / 180.0, 180.0 * pi / 180.0)
  36.    cr.line_to(x,y + radius)
  37.    cr.arc (x + radius,y + radius,radius,180.0 * pi / 180.0,270.0 * pi / 180.0)
  38.    cr.close_path()
  39.  
  40. class RunOsd(gtk.Window):
  41.   def __init__(self,png_file):
  42.    super(RunOsd, self).__init__()
  43.  
  44.    self.png_file=png_file
  45.  
  46.    monitorw=gtk.gdk.screen_width()
  47.    monitorh=gtk.gdk.screen_height()
  48.    scalew=monitorw/640.0
  49.    scaleh=monitorh/480.0
  50.    scale = min(scalew, scaleh)
  51.    self.size=int(130.0*max(1,scale))
  52.    self.alpha=1
  53.  
  54.    self.set_decorated(False)
  55.    self.set_resizable(False)
  56.    self.set_keep_above(True)
  57.    self.set_skip_taskbar_hint(True)
  58.  
  59.    self.set_size_request(self.size,self.size)
  60.    self.set_uposition(int((monitorw-self.size)*0.5),int(monitorh-self.size*1.43))
  61.    rgba = gtk.gdk.Screen.get_rgba_colormap(self.get_screen())
  62.    self.set_colormap(rgba)
  63.    self.set_opacity(self.alpha)
  64.    
  65.    pm = gtk.gdk.Pixmap(None, self.get_size()[0],self.get_size()[1], 1)
  66.    pmcr = pm.cairo_create()
  67.    pmcr.rectangle(0,0,1,1)
  68.    pmcr.fill()
  69.    self.input_shape_combine_mask(pm, 0, 0)
  70.    
  71.    self.darea = gtk.DrawingArea()
  72.    self.darea.connect("expose-event", self.expose)
  73.    self.darea.connect("button_press_event",self.button_press_cb)
  74.    self.darea.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.BUTTON_PRESS_MASK)
  75.    self.add(self.darea)
  76.  
  77.    self.connect("destroy", lambda w: gtk.main_quit())
  78.    
  79.    self.show_all()
  80.    self.stick()
  81.    timeout_add(3000,self.osd_fade_start)
  82.  
  83.   def expose(self,widget,event):
  84.    self.cr = widget.window.cairo_create()
  85.  
  86.    self.cr.set_operator(cairo.OPERATOR_CLEAR)
  87.    self.cr.rectangle(0.0, 0.0, self.size,self.size)
  88.    self.cr.fill()
  89.    self.cr.set_operator(cairo.OPERATOR_OVER)
  90.  
  91.    Icon(self.cr, self.size, self.size,0.75,self.png_file)
  92.  
  93.   def button_press_cb(self,widget,event):
  94.    self.osd_fade_start()
  95.  
  96.   def osd_fade_start(self):
  97.    timeout_add(25,self.osd_fade)
  98.    return False
  99.  
  100.   def osd_fade(self):
  101.    self.alpha = self.alpha - 0.15
  102.    self.set_opacity(self.alpha)
  103.    if self.alpha > 0:
  104.       return True
  105.    if self.alpha <= 0:
  106.       gtk.main_quit()
  107.       return False
  108.  
  109. if __name__ == "__main__":
  110.   if len(argv) < 2:
  111.    print "need png!"
  112.    exit(1)
  113.   window = RunOsd(argv[1])
  114.   gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement