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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (hide annotations)
Sat Jun 3 23:09:56 2006 UTC (18 years, 3 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +12 -11 lines
File MIME type: text/x-python
updated

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

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