~ubuntu-branches/ubuntu/trusty/key-mon/trusty

« back to all changes in this revision

Viewing changes to src/keymon/shaped_window.py

  • Committer: Bazaar Package Importer
  • Author(s): Dustin Kirkland
  • Date: 2011-05-11 17:07:37 UTC
  • Revision ID: james.westby@ubuntu.com-20110511170737-kqt6f3s091hyz4k5
Tags: upstream-1.6
ImportĀ upstreamĀ versionĀ 1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
# Copyright 2010 Google Inc.
 
4
#
 
5
# Licensed under the Apache License, Version 2.0 (the "License");
 
6
# you may not use this file except in compliance with the License.
 
7
# You may obtain a copy of the License at
 
8
#
 
9
#      http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
# Unless required by applicable law or agreed to in writing, software
 
12
# distributed under the License is distributed on an "AS IS" BASIS,
 
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
# See the License for the specific language governing permissions and
 
15
# limitations under the License.
 
16
 
 
17
"""Create a shaped window to show mouse events.
 
18
 
 
19
Thanks to mathias.gumz for the original code.
 
20
"""
 
21
import gobject
 
22
import gtk
 
23
 
 
24
import lazy_pixbuf_creator
 
25
 
 
26
class ShapedWindow(gtk.Window):
 
27
  """Create a window shaped as fname."""
 
28
  def __init__(self, fname, scale=1.0):
 
29
    gtk.Window.__init__(self)
 
30
    self.connect('size-allocate', self._on_size_allocate)
 
31
    self.set_decorated(False)
 
32
    self.set_keep_above(True)
 
33
    self.scale = scale
 
34
    self.is_shown = False
 
35
    self.name_fnames = {
 
36
      'mouse' : [fname],
 
37
    }
 
38
    self.pixbufs = lazy_pixbuf_creator.LazyPixbufCreator(self.name_fnames,
 
39
                                                         self.scale)
 
40
    self.pixbuf = self.pixbufs.get('mouse')
 
41
    self.resize(self.pixbuf.get_width(), self.pixbuf.get_height())
 
42
 
 
43
    # a pixmap widget to contain the pixmap
 
44
    self.image = gtk.Image()
 
45
    bitmap, self.mask = self.pixbuf.render_pixmap_and_mask()
 
46
    self.image.set_from_pixmap(bitmap, self.mask)
 
47
    self.image.show()
 
48
    self.add(self.image)
 
49
 
 
50
  def _on_size_allocate(self, win, unused_allocation):
 
51
    """Called when first allocated."""
 
52
    # Set the window shape
 
53
    win.shape_combine_mask(self.mask, 0, 0)
 
54
    win.set_property('skip-taskbar-hint', True)
 
55
    if not win.is_composited():
 
56
      print 'Unable to fade the window'
 
57
    else:
 
58
      win.set_opacity(0.5)
 
59
 
 
60
  def center_on_cursor(self):
 
61
    root = gtk.gdk.screen_get_default().get_root_window()
 
62
    x, y, _ = root.get_pointer()
 
63
    w, h = self.get_size()
 
64
    new_x, new_y = x - w/2, y - h/2
 
65
    pos = self.get_position()
 
66
    if pos[0] != new_x or pos[1] != new_y:
 
67
      self.move(new_x, new_y)
 
68
 
 
69
  def show(self):
 
70
    self.is_shown = True
 
71
    self.present()
 
72
 
 
73
  def hide(self):
 
74
    self.is_shown = False
 
75
    gtk.Window.hide(self)
 
76
 
 
77
  def fade_away(self):
 
78
    """Make the window fade in a little bit."""
 
79
    self.present()
 
80
    gobject.timeout_add(200, self.hide)