Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 242 lines (210 sloc) 9.927 kb
d5f490f get_list pa check_files nerabs vec sm skor ziher, svoj start bos mogu pa...
Dudu authored
1 # Synopsis : Windows System tray icon.
8cfbdd6 ok moj zadnji commit za to seminarsko pomoje. dokumentacija, pa se credi...
Dudu authored
2 # Programmer : Simon Brunning
d5f490f get_list pa check_files nerabs vec sm skor ziher, svoj start bos mogu pa...
Dudu authored
3 # Notes : Based on (i.e. ripped off from) Mark Hammond's
4 # win32gui_taskbar.py and win32gui_menu.py demos from PyWin32
86c0373 meow
Dudu authored
5 # - and now ripped off by Rok Ritlop for academic purposes.
d5f490f get_list pa check_files nerabs vec sm skor ziher, svoj start bos mogu pa...
Dudu authored
6
7 import os
8 import sys
9 import wx
10 import win32api
11 import win32con
12 import win32gui_struct
13 try:
14 import winxpgui as win32gui
15 except ImportError:
16 import win32gui
17
18 class SysTrayIcon(object):
8cfbdd6 ok moj zadnji commit za to seminarsko pomoje. dokumentacija, pa se credi...
Dudu authored
19
d5f490f get_list pa check_files nerabs vec sm skor ziher, svoj start bos mogu pa...
Dudu authored
20 QUIT = 'QUIT'
21 SPECIAL_ACTIONS = [QUIT]
22
23 FIRST_ID = 1023
24
25 def start(self,
26 icon,
27 hover_text,
28 menu_options,
29 on_quit=None,
30 default_menu_index=None,
31 window_class_name=None,):
32
33 self.icon = icon
34 self.hover_text = hover_text
35 self.on_quit = on_quit
36
37 self.atr = {}
38 menu_options = menu_options + (('Quit', None, None, self.QUIT),)
39 self._next_action_id = self.FIRST_ID
40 self.menu_actions_by_id = set()
41 self.menu_options = self._add_ids_to_menu_options(list(menu_options))
42 self.menu_actions_by_id = dict(self.menu_actions_by_id)
43 del self._next_action_id
44
45
46 self.default_menu_index = (default_menu_index or 0)
47 self.window_class_name = window_class_name or "SysTrayIconPy"
48
49 message_map = {win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
50 win32con.WM_DESTROY: self.destroy,
51 win32con.WM_COMMAND: self.command,
52 win32con.WM_USER+20 : self.notify,}
53 # Register the Window class.
54 window_class = win32gui.WNDCLASS()
55 hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
56 window_class.lpszClassName = self.window_class_name
57 window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
58 window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
59 window_class.hbrBackground = win32con.COLOR_WINDOW
60 window_class.lpfnWndProc = message_map # could also specify a wndproc.
61 try:
62 self.classAtom = win32gui.RegisterClass(window_class)
63 except:
64 print "faild register"
65 # Create the Window.
66 style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
67 self.hwnd = win32gui.CreateWindow(self.classAtom,
68 self.window_class_name,
69 style,
70 0,
71 0,
72 win32con.CW_USEDEFAULT,
73 win32con.CW_USEDEFAULT,
74 0,
75 0,
76 hinst,
77 None)
78 win32gui.UpdateWindow(self.hwnd)
79 self.notify_id = None
80 self.refresh_icon()
81
82 win32gui.PumpMessages()
83
84 def _add_ids_to_menu_options(self, menu_options):
85 result = []
86 for menu_option in menu_options:
87 option_text, option_icon, extra_attribute, option_action = menu_option
88 if callable(option_action) or option_action in self.SPECIAL_ACTIONS:
89 self.menu_actions_by_id.add((self._next_action_id, option_action))
90 self.atr[self._next_action_id] = extra_attribute
91 result.append(menu_option + (self._next_action_id,))
92 elif non_string_iterable(option_action):
93 result.append((option_text,
94 option_icon,
95 extra_attribute,
96 self._add_ids_to_menu_options(option_action),
97 self._next_action_id))
98 else:
99 print 'Unknown item', option_text, option_icon, option_action
100 self._next_action_id += 1
101 return result
102
103 def refresh_icon(self):
104 # Try and find a custom icon
105 hinst = win32gui.GetModuleHandle(None)
106 if os.path.isfile(self.icon):
107 icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
108 hicon = win32gui.LoadImage(hinst,
109 self.icon,
110 win32con.IMAGE_ICON,
111 0,
112 0,
113 icon_flags)
114 else:
115 print "Can't find icon file - using default."
116 hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
117
118 if self.notify_id: message = win32gui.NIM_MODIFY
119 else: message = win32gui.NIM_ADD
120 self.notify_id = (self.hwnd,
121 0,
122 win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
123 win32con.WM_USER+20,
124 hicon,
125 self.hover_text)
126 win32gui.Shell_NotifyIcon(message, self.notify_id)
127
128 def restart(self, hwnd, msg, wparam, lparam):
129 self.refresh_icon()
130
131 def destroy(self, hwnd, msg, wparam, lparam):
132 if self.on_quit: self.on_quit(self)
133 nid = (self.hwnd, 0)
134 win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)
135 win32gui.PostQuitMessage(0) # Terminate the app.
136
137 def notify(self, hwnd, msg, wparam, lparam):
138 if lparam==win32con.WM_LBUTTONDBLCLK:
139 self.execute_menu_option(self.default_menu_index + self.FIRST_ID)
140 elif lparam==win32con.WM_RBUTTONUP:
141 self.show_menu()
142 elif lparam==win32con.WM_LBUTTONUP:
143 pass
144 return True
145
146 def show_menu(self):
147 menu = win32gui.CreatePopupMenu()
148 self.create_menu(menu, self.menu_options)
149 #win32gui.SetMenuDefaultItem(menu, 1000, 0)
150
151 pos = win32gui.GetCursorPos()
152 # See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp
153 win32gui.SetForegroundWindow(self.hwnd)
154 win32gui.TrackPopupMenu(menu,
155 win32con.TPM_LEFTALIGN,
156 pos[0],
157 pos[1],
158 0,
159 self.hwnd,
160 None)
161 win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
162
163 def create_menu(self, menu, menu_options):
164 for menu_option in menu_options[::-1]:
165 #print menu_option
166 option_text, option_icon, extra_attribute, option_action, option_id= menu_option
167 if option_icon:
168 option_icon = self.prep_menu_icon(option_icon)
169
170 if option_id in self.menu_actions_by_id:
171 item, extras = win32gui_struct.PackMENUITEMINFO(text=option_text,
172 hbmpItem=option_icon,
173 wID=option_id)
174 win32gui.InsertMenuItem(menu, 0, 1, item)
175 else:
176 submenu = win32gui.CreatePopupMenu()
177 self.create_menu(submenu, option_action)
178 item, extras = win32gui_struct.PackMENUITEMINFO(text=option_text,
179 hbmpItem=option_icon,
180 hSubMenu=submenu)
181 win32gui.InsertMenuItem(menu, 0, 1, item)
182
183 def prep_menu_icon(self, icon):
184 # First load the icon.
185 ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
186 ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
187 hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE)
188
189 hdcBitmap = win32gui.CreateCompatibleDC(0)
190 hdcScreen = win32gui.GetDC(0)
191 hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y)
192 hbmOld = win32gui.SelectObject(hdcBitmap, hbm)
193 # Fill the background.
194 brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU)
195 win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush)
196 # unclear if brush needs to be feed. Best clue I can find is:
197 # "GetSysColorBrush returns a cached brush instead of allocating a new
198 # one." - implies no DeleteObject
199 # draw the icon
200 win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL)
201 win32gui.SelectObject(hdcBitmap, hbmOld)
202 win32gui.DeleteDC(hdcBitmap)
203
204 return hbm
205
206 def command(self, hwnd, msg, wparam, lparam):
207 id = win32gui.LOWORD(wparam)
208 self.execute_menu_option(id)
209
210 def execute_menu_option(self, id):
211 #print id
212 try:
213 menu_action = self.menu_actions_by_id[id]
214 if menu_action == self.QUIT:
215 app = wx.PySimpleApp()
216 retCode = wx.MessageBox("Are you sure you want to quit?", "Quit",wx.YES_NO | wx.ICON_QUESTION)
217 #print retCode
218 #print "2 to quit"
219 if retCode == 2:
220 win32gui.DestroyWindow(self.hwnd)
221 win32gui.UnregisterClass(self.classAtom,None)
222 else:
223 pass
224 else:
225 menu_action(self,self.atr[id])
226 except Exception:
227 if id == 'quit':
228 win32gui.DestroyWindow(self.hwnd)
229 win32gui.UnregisterClass(self.classAtom,None)
230
231
232 def non_string_iterable(obj):
233 try:
234 iter(obj)
235 except TypeError:
236 return False
237 else:
238 return not isinstance(obj, basestring)
239
240
241
242
Something went wrong with that request. Please try again.