1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # # Creates a taskbar icon for web2py |
---|
4 | # # Author: Mark Larsen, mostly stolen from Mark Hammond's |
---|
5 | # # C:\Python25\Lib\site-packages\win32\Demos\win32gui_taskbar.py |
---|
6 | # # 11/7/08 |
---|
7 | # dual licensed under the web2py license (LGPL) and the Python license. |
---|
8 | |
---|
9 | from __future__ import print_function |
---|
10 | import os |
---|
11 | import sys |
---|
12 | import base64 |
---|
13 | import win32con |
---|
14 | import win32api |
---|
15 | import win32gui |
---|
16 | |
---|
17 | |
---|
18 | class TaskBarIcon: |
---|
19 | |
---|
20 | def __init__(self, iconPath=None): |
---|
21 | |
---|
22 | self.iconPath = iconPath |
---|
23 | self.status = [] |
---|
24 | |
---|
25 | msg_TaskbarRestart = \ |
---|
26 | win32api.RegisterWindowMessage('TaskbarCreated') |
---|
27 | message_map = { |
---|
28 | msg_TaskbarRestart: self.OnRestart, |
---|
29 | win32con.WM_DESTROY: self.OnDestroy, |
---|
30 | win32con.WM_COMMAND: self.OnCommand, |
---|
31 | win32con.WM_USER + 20: self.OnTaskbarNotify, |
---|
32 | } |
---|
33 | |
---|
34 | # Register the Window class. |
---|
35 | |
---|
36 | wc = win32gui.WNDCLASS() |
---|
37 | hinst = wc.hInstance = win32api.GetModuleHandle(None) |
---|
38 | wc.lpszClassName = 'web2pyTaskbar' |
---|
39 | wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW |
---|
40 | wc.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) |
---|
41 | wc.hbrBackground = win32con.COLOR_WINDOW |
---|
42 | wc.lpfnWndProc = message_map # could also specify a wndproc. |
---|
43 | classAtom = win32gui.RegisterClass(wc) |
---|
44 | |
---|
45 | # Create the Window. |
---|
46 | |
---|
47 | style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU |
---|
48 | self.hwnd = win32gui.CreateWindow( |
---|
49 | classAtom, |
---|
50 | 'web2pyTaskbar', |
---|
51 | style, |
---|
52 | 0, |
---|
53 | 0, |
---|
54 | win32con.CW_USEDEFAULT, |
---|
55 | win32con.CW_USEDEFAULT, |
---|
56 | 0, |
---|
57 | 0, |
---|
58 | hinst, |
---|
59 | None, |
---|
60 | ) |
---|
61 | win32gui.UpdateWindow(self.hwnd) |
---|
62 | self.SetServerStopped() |
---|
63 | |
---|
64 | def __createIcon(self): |
---|
65 | |
---|
66 | # try and use custom icon |
---|
67 | |
---|
68 | if self.iconPath and os.path.isfile(self.iconPath): |
---|
69 | hicon = self.__loadFromFile(self.iconPath) |
---|
70 | else: |
---|
71 | try: |
---|
72 | fp = 'tmp.ico' |
---|
73 | icFH = file(fp, 'wb') |
---|
74 | if self.serverState == self.EnumServerState.STOPPED: |
---|
75 | icFH.write(base64.b64decode(self.__getIconStopped())) |
---|
76 | elif self.serverState == self.EnumServerState.RUNNING: |
---|
77 | icFH.write(base64.b64decode(self.__getIconRunning())) |
---|
78 | icFH.close() |
---|
79 | hicon = self.__loadFromFile(fp) |
---|
80 | os.unlink(fp) |
---|
81 | except: |
---|
82 | print("Can't load web2py icons - using default") |
---|
83 | hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) |
---|
84 | |
---|
85 | flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE\ |
---|
86 | | win32gui.NIF_TIP |
---|
87 | nid = ( |
---|
88 | self.hwnd, |
---|
89 | 0, |
---|
90 | flags, |
---|
91 | win32con.WM_USER + 20, |
---|
92 | hicon, |
---|
93 | 'web2py Framework', |
---|
94 | ) |
---|
95 | try: |
---|
96 | win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid) |
---|
97 | except: |
---|
98 | try: |
---|
99 | win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) |
---|
100 | except win32api.error: |
---|
101 | |
---|
102 | # This is common when windows is starting, and this code is hit |
---|
103 | # before the taskbar has been created. |
---|
104 | |
---|
105 | print('Failed to add the taskbar icon - is explorer running?') |
---|
106 | |
---|
107 | # but keep running anyway - when explorer starts, we get the |
---|
108 | |
---|
109 | def OnRestart( |
---|
110 | self, |
---|
111 | hwnd, |
---|
112 | msg, |
---|
113 | wparam, |
---|
114 | lparam, |
---|
115 | ): |
---|
116 | self._DoCreateIcons() |
---|
117 | |
---|
118 | def OnDestroy( |
---|
119 | self, |
---|
120 | hwnd, |
---|
121 | msg, |
---|
122 | wparam, |
---|
123 | lparam, |
---|
124 | ): |
---|
125 | nid = (self.hwnd, 0) |
---|
126 | win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) |
---|
127 | |
---|
128 | def OnTaskbarNotify( |
---|
129 | self, |
---|
130 | hwnd, |
---|
131 | msg, |
---|
132 | wparam, |
---|
133 | lparam, |
---|
134 | ): |
---|
135 | if lparam == win32con.WM_LBUTTONUP: |
---|
136 | pass |
---|
137 | elif lparam == win32con.WM_LBUTTONDBLCLK: |
---|
138 | pass |
---|
139 | elif lparam == win32con.WM_RBUTTONUP: |
---|
140 | menu = win32gui.CreatePopupMenu() |
---|
141 | win32gui.AppendMenu(menu, win32con.MF_STRING, 1023, |
---|
142 | 'Toggle Display') |
---|
143 | win32gui.AppendMenu(menu, win32con.MF_SEPARATOR, 0, '') |
---|
144 | if self.serverState == self.EnumServerState.STOPPED: |
---|
145 | win32gui.AppendMenu(menu, win32con.MF_STRING, 1024, |
---|
146 | 'Start Server') |
---|
147 | win32gui.AppendMenu(menu, win32con.MF_STRING |
---|
148 | | win32con.MF_GRAYED, 1025, |
---|
149 | 'Restart Server') |
---|
150 | win32gui.AppendMenu(menu, win32con.MF_STRING |
---|
151 | | win32con.MF_GRAYED, 1026, |
---|
152 | 'Stop Server') |
---|
153 | else: |
---|
154 | win32gui.AppendMenu(menu, win32con.MF_STRING |
---|
155 | | win32con.MF_GRAYED, 1024, |
---|
156 | 'Start Server') |
---|
157 | win32gui.AppendMenu(menu, win32con.MF_STRING, 1025, |
---|
158 | 'Restart Server') |
---|
159 | win32gui.AppendMenu(menu, win32con.MF_STRING, 1026, |
---|
160 | 'Stop Server') |
---|
161 | win32gui.AppendMenu(menu, win32con.MF_SEPARATOR, 0, '') |
---|
162 | win32gui.AppendMenu(menu, win32con.MF_STRING, 1027, |
---|
163 | 'Quit (pid:%i)' % os.getpid()) |
---|
164 | pos = win32gui.GetCursorPos() |
---|
165 | |
---|
166 | # See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp |
---|
167 | |
---|
168 | win32gui.SetForegroundWindow(self.hwnd) |
---|
169 | win32gui.TrackPopupMenu( |
---|
170 | menu, |
---|
171 | win32con.TPM_LEFTALIGN, |
---|
172 | pos[0], |
---|
173 | pos[1], |
---|
174 | 0, |
---|
175 | self.hwnd, |
---|
176 | None, |
---|
177 | ) |
---|
178 | win32api.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0) |
---|
179 | return 1 |
---|
180 | |
---|
181 | def OnCommand( |
---|
182 | self, |
---|
183 | hwnd, |
---|
184 | msg, |
---|
185 | wparam, |
---|
186 | lparam, |
---|
187 | ): |
---|
188 | id = win32api.LOWORD(wparam) |
---|
189 | if id == 1023: |
---|
190 | self.status.append(self.EnumStatus.TOGGLE) |
---|
191 | elif id == 1024: |
---|
192 | self.status.append(self.EnumStatus.START) |
---|
193 | elif id == 1025: |
---|
194 | self.status.append(self.EnumStatus.RESTART) |
---|
195 | elif id == 1026: |
---|
196 | self.status.append(self.EnumStatus.STOP) |
---|
197 | elif id == 1027: |
---|
198 | self.status.append(self.EnumStatus.QUIT) |
---|
199 | self.Destroy() |
---|
200 | else: |
---|
201 | print('Unknown command -', id) |
---|
202 | |
---|
203 | def Destroy(self): |
---|
204 | win32gui.DestroyWindow(self.hwnd) |
---|
205 | |
---|
206 | def SetServerRunning(self): |
---|
207 | self.serverState = self.EnumServerState.RUNNING |
---|
208 | self.__createIcon() |
---|
209 | |
---|
210 | def SetServerStopped(self): |
---|
211 | self.serverState = self.EnumServerState.STOPPED |
---|
212 | self.__createIcon() |
---|
213 | |
---|
214 | def __getIconRunning(self): |
---|
215 | return 'AAABAAEAEBAQAAAAAAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAIXMGAABe/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABERAgAAIAAAEAACAAAgAAABEAIiACIgAAABAgAgIAIAEAECACAgAgABEAIiACACAAAAAAAAAAAAICACIiAiIAICAgIAACACAgICAgAAIAICAgICIiAiIAICAgIAACACAgICAgAAIAICAgICIiAiIAAAAAAAAAAAD//wAAhe8AAL3vAADMYwAA9a0AALWtAADMbQAA//8AAKwjAABV7QAAVe0AAFQjAABV7QAAVe0AAFQjAAD//wAA' |
---|
216 | |
---|
217 | def __getIconStopped(self): |
---|
218 | return 'AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJCdIAIXMGAABe/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAzMzMzMzMzAwERMjMzIzAzEDMyMzMjAzMxAzIiMyAjMzMwMjMjAzIzEzECMyAjMjMxEzAiAyMyMzMzMwAzMzMzIyMyACMiIzIyMjAzAyMyMjIyAjMwIzIyMjAyIiMCIzIyAjIzMyAyMjAyMjMzIwIyAjIyIiMiIDAzMzMzMzMzB//gAAhe0AAJ3rAADMYwAA9a0AALGNAADMLQAA/n8AAKwjAABVrQAAUc0AAFQjAABF5QAAVekAABQhAAB//gAA' |
---|
219 | |
---|
220 | def __loadFromFile(self, iconPath): |
---|
221 | hinst = win32api.GetModuleHandle(None) |
---|
222 | icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE |
---|
223 | hicon = win32gui.LoadImage( |
---|
224 | hinst, |
---|
225 | iconPath, |
---|
226 | win32con.IMAGE_ICON, |
---|
227 | 0, |
---|
228 | 0, |
---|
229 | icon_flags, |
---|
230 | ) |
---|
231 | return hicon |
---|
232 | |
---|
233 | class EnumStatus: |
---|
234 | |
---|
235 | TOGGLE = 0 |
---|
236 | START = 1 |
---|
237 | STOP = 2 |
---|
238 | RESTART = 3 |
---|
239 | QUIT = 4 |
---|
240 | |
---|
241 | class EnumServerState: |
---|
242 | |
---|
243 | RUNNING = 0 |
---|
244 | STOPPED = 1 |
---|