~george-edison55/stackapplet/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#=========================
#
# AppIndicator for GTK
#  drop-in replacement
#
#    Copyright 2010
#     Nathan Osman
#
#=========================

# We require PyGTK
import gtk
import gobject

# We also need os and sys
import os
import sys

# Types
CATEGORY_APPLICATION_STATUS = 0

# Status
STATUS_ACTIVE    = 0
STATUS_ATTENTION = 1

# Locations to search for the given icon
search_locations = [ os.path.join(os.path.dirname(sys.executable), "images"),
                     os.path.join(os.path.dirname(sys.path[0]), "images"),
                     os.path.join(os.path.dirname(sys.path[0]), "../images"),
                     '/usr/share/pixmaps' ]

def get_icon_filename(icon_name):
	
	# Determine where the icon is
	global search_locations
	
	for folder in search_locations:
		
		filename = os.path.join(folder, icon_name + ".png")
		
		if os.path.isfile(filename):
			return filename
	
	return None
		

# The main class
class Indicator:
	
	# Constructor
	
	def __init__ (self,unknown,icon,category):
		
		# Store the settings		
		self.inactive_icon = get_icon_filename(icon)
		self.active_icon   = ""  # Blank until the user calls set_attention_icon
		
		# Create the status icon
		self.icon = gtk.StatusIcon()
		
		# Initialize to the default icon
		self.icon.set_from_file(self.inactive_icon)
		
		# Set the rest of the vars
		self.menu = None    # We have no menu yet
	
	def set_menu(self,menu):
		
		# Save a copy of the menu
		self.menu = menu
		
		# Now attach the icon's signal
		# to the menu so that it becomes displayed
		# whenever the user clicks it
		self.icon.connect("activate", self.show_menu)
	
	def set_status(self, status):
		
		# Status defines whether the active or inactive
		# icon should be displayed.
		if status == STATUS_ACTIVE:
			self.icon.set_from_file(self.inactive_icon)
		else:
			self.icon.set_from_file(self.active_icon)
	
	def set_icon(self, icon):
		
		# Set the new icon
		self.icon.set_from_file(get_icon_filename(icon))
	
	def set_attention_icon(self, icon):
		
		# Set the icon filename as the attention icon
		self.active_icon = get_icon_filename(icon)
	
	def show_menu(self, widget):
		
		# Show the menu
		self.menu.popup(None,None,None,0,0)
		
		# Get the location and size of the window
		mouse_rect = self.menu.get_window().get_frame_extents()
		
		self.x = mouse_rect.x
		self.y = mouse_rect.y
		self.right  = self.x + mouse_rect.width
		self.bottom = self.y + mouse_rect.height
		
		# Set a timer to poll the menu
		self.timer = gobject.timeout_add(100, self.check_mouse)
	
	def check_mouse(self):
		
		if not self.menu.get_window().is_visible():
			return
		
		# Now check the global mouse coords
		root = self.menu.get_screen().get_root_window()
		
		x,y,z = root.get_pointer()
		
		if x < self.x or x > self.right or y < self.y or y > self.bottom:
			self.hide_menu()
		else:
			return True
	
	def hide_menu(self):
		
		self.menu.popdown()