/[cvs]/nfo/projects/mess/src/client/windows/context_menu.py
ViewVC logotype

Contents of /nfo/projects/mess/src/client/windows/context_menu.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Sat Jun 3 22:27:45 2006 UTC (18 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.6: +4 -1 lines
File MIME type: text/x-python
+ update: Add context menu to directory backgrounds (for Mess/Settings)

1 # A sample context menu handler.
2 # Adds a 'Hello from Python' menu entry to .py files. When clicked, a
3 # simple message box is displayed.
4 #
5 # To demostrate:
6 # * Execute this script to register the context menu.
7 # * Open Windows Explorer, and browse to a directory with a .py file.
8 # * Right-Click on a .py file - locate and click on 'Hello from Python' on
9 # the context menu.
10
11 # see also: http://www.codeproject.com/shell/ShellExtGuide2.asp
12
13 import pythoncom
14 from win32com.shell import shell, shellcon
15 import win32gui, win32gui_struct
16 import win32con
17 import win32api, winerror
18
19 IContextMenu_Methods = ["QueryContextMenu", "InvokeCommand", "GetCommandString"]
20 IShellExtInit_Methods = ["Initialize"]
21
22 class ShellExtension:
23 _reg_progid_ = "Python.ShellExtension.ContextMenu"
24 _reg_name_ = "MessClient"
25 _reg_desc_ = "Mess Shell Extension"
26 _reg_clsid_ = "{CED0336C-C9EE-4a7f-8D7F-C660393C381F}"
27 _com_interfaces_ = [shell.IID_IShellExtInit, shell.IID_IContextMenu]
28 _public_methods_ = IContextMenu_Methods + IShellExtInit_Methods
29
30 def Initialize(self, pidlFolder, dataobj, hkey):
31 print "Init", pidlFolder, dataobj, hkey
32 self.dataobj = dataobj
33
34 def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags):
35
36 # debug
37 print "QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags
38
39 # clicking on a directory background leaves us with dataobj == None
40 if self.dataobj:
41
42 # query the items clicked on
43 format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL
44 sm = self.dataobj.GetData(format_etc)
45
46 # how many items are selected
47 num_files = shell.DragQueryFile(sm.data_handle, -1)
48
49 # grab all items
50 self.files_selected = []
51 for i in range(0, num_files):
52 fname = shell.DragQueryFile(sm.data_handle, i)
53 if fname == 0: continue
54 self.files_selected.append(fname)
55
56
57 # calculate items to be added to context menu
58 items = []
59
60 # CMF_NORMAL: e.g. files on desktop
61 # CMF_VERBSONLY: e.g. shortcuts
62 # CMF_EXPLORE: regular files/folders via explorer
63
64 # Check == here, since CMF_NORMAL=0
65 if ((uFlags & 0x000F) == shellcon.CMF_NORMAL) or (uFlags & shellcon.CMF_VERBSONLY) or (uFlags & shellcon.CMF_EXPLORE):
66 if self.dataobj:
67 items.append("Add to A&rchive")
68 items.append("Named Shortcut &1")
69 items.append("Named Shortcut &2")
70 elif uFlags & CMF_DEFAULTONLY:
71 print "CMF_DEFAULTONLY...\r\n"
72 items.append("hello world - 1!")
73 else:
74 print "** unknown flags", uFlags
75 items.append("hello world - 2!")
76
77
78 # build context menu(s)
79 idCmd = idCmdFirst
80
81 # add seperator line
82 win32gui.InsertMenu(hMenu, indexMenu,
83 win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
84 idCmd, None)
85 indexMenu += 1
86 idCmd += 1
87
88 # create main menu entries
89 for item in items:
90 win32gui.InsertMenu(hMenu, indexMenu,
91 win32con.MF_STRING|win32con.MF_BYPOSITION,
92 idCmd, item)
93 indexMenu += 1
94 idCmd += 1
95
96 # create submenu entries
97 hSubmenu = win32gui.CreatePopupMenu()
98 win32gui.InsertMenu(hSubmenu, 0,
99 win32con.MF_BYPOSITION,
100 idCmd, "&Settings...")
101 idCmd += 1
102
103 # insert the submenu into a new container menu entry
104 item, extras = win32gui_struct.PackMENUITEMINFO(
105 wID = idCmd,
106 hSubMenu = hSubmenu,
107 text = "&Mess"
108 )
109 win32gui.InsertMenuItem(hMenu, indexMenu, 1, item)
110 indexMenu += 1
111 idCmd += 1
112
113 # add seperator line
114 win32gui.InsertMenu(hMenu, indexMenu,
115 win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
116 idCmd, None)
117 indexMenu += 1
118 idCmd += 1
119
120 return idCmd-idCmdFirst # Must return number of menu items we added.
121
122 def GetCommandString(self, cmdId, typ):
123 #if cmdId == 0:
124 # return "Hello from Python!!"
125 #else:
126 # return "abc"
127 return self._reg_name_ + "_" + cmdId;
128
129 def InvokeCommand(self, ci):
130 mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci
131
132 # If lpVerb really points to a string, ignore this function call and bail out.
133 if win32api.HIWORD( verb ) != 0:
134 return winerror.E_INVALIDARG
135
136 function_id = win32api.LOWORD(verb)
137 # TODO: establish mapping with building the context menus in "QueryContextMenu"
138 if function_id == 1:
139 method = "Add to archive"
140 elif function_id == 2:
141 method = "Shortcut 1"
142 elif function_id == 3:
143 method = "Shortcut 2"
144 else:
145 method = "Unknown"
146
147 # do action(s)
148 if hasattr(self, 'files_selected'):
149 win32gui.MessageBox(hwnd, "\n".join(self.files_selected), method, win32con.MB_OK)
150 else:
151 win32gui.MessageBox(hwnd, "No selection!", method, win32con.MB_OK)
152
153
154 def DllRegisterServer():
155 import _winreg
156
157 # extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html)
158
159 # for directory background
160 background_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Directory\\Background\\shellex")
161 background_subkey = _winreg.CreateKey(background_key, "ContextMenuHandlers")
162 background_subkey2 = _winreg.CreateKey(background_subkey, ShellExtension._reg_name_)
163 _winreg.SetValueEx(background_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_)
164
165 # for folders
166 folder_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Folder\\shellex")
167 folder_subkey = _winreg.CreateKey(folder_key, "ContextMenuHandlers")
168 folder_subkey2 = _winreg.CreateKey(folder_subkey, ShellExtension._reg_name_)
169 _winreg.SetValueEx(folder_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_)
170
171 # for files
172 file_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "*\\shellex")
173 file_subkey = _winreg.CreateKey(file_key, "ContextMenuHandlers")
174 file_subkey2 = _winreg.CreateKey(file_subkey, ShellExtension._reg_name_)
175 _winreg.SetValueEx(file_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_)
176
177 print ShellExtension._reg_desc_, "registration complete."
178
179
180 def DllUnregisterServer():
181 import _winreg
182
183 # extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html)
184
185 try:
186 # for directory background
187 background_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
188 "Directory\\Background\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_)
189 # for folders
190 folder_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
191 "Folder\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_)
192 # for files
193 file_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
194 "*\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_)
195 except WindowsError, details:
196 import errno
197 if details.errno != errno.ENOENT:
198 raise
199 print ShellExtension._reg_desc_, "unregistration complete."
200
201 if __name__=='__main__':
202 from win32com.server import register
203 register.UseCommandLine(ShellExtension,
204 finalize_register = DllRegisterServer,
205 finalize_unregister = DllUnregisterServer)

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed