/[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.3 - (show annotations)
Sat Jun 3 15:11:27 2006 UTC (18 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.2: +24 -11 lines
File MIME type: text/x-python
+ now operates on multiple files or folders

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 import pythoncom
12 from win32com.shell import shell, shellcon
13 import win32gui
14 import win32con
15
16 IContextMenu_Methods = ["QueryContextMenu", "InvokeCommand", "GetCommandString"]
17 IShellExtInit_Methods = ["Initialize"]
18
19 class ShellExtension:
20 _reg_progid_ = "Python.ShellExtension.ContextMenu"
21 _reg_name_ = "MessClient"
22 _reg_desc_ = "Mess Shell Extension"
23 _reg_clsid_ = "{CED0336C-C9EE-4a7f-8D7F-C660393C381F}"
24 _com_interfaces_ = [shell.IID_IShellExtInit, shell.IID_IContextMenu]
25 _public_methods_ = IContextMenu_Methods + IShellExtInit_Methods
26
27 def Initialize(self, folder, dataobj, hkey):
28 print "Init", folder, dataobj, hkey
29 self.dataobj = dataobj
30
31 def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags):
32
33 # debug
34 print "QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags
35
36 # query the items clicked on
37 format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL
38 sm = self.dataobj.GetData(format_etc)
39 num_files = shell.DragQueryFile(sm.data_handle, -1)
40
41 #if num_files>1:
42 # msg = "&Hello from Python (with %d files selected)" % num_files
43 #else:
44 # fname = shell.DragQueryFile(sm.data_handle, 0)
45 # msg = "&Hello from Python (with '%s' selected)" % fname
46 self.files_selected = []
47 for i in range(0, num_files):
48 fname = shell.DragQueryFile(sm.data_handle, i)
49 if fname == 0: continue
50 self.files_selected.append(fname)
51
52 # build context menu(s)
53 msg = "A&rchiveMe"
54 idCmd = idCmdFirst
55 items = []
56 if (uFlags & 0x000F) == shellcon.CMF_NORMAL: # Check == here, since CMF_NORMAL=0
57 print "CMF_NORMAL..."
58 items.append(msg)
59 elif uFlags & shellcon.CMF_VERBSONLY:
60 print "CMF_VERBSONLY..."
61 items.append(msg + " - shortcut")
62 elif uFlags & shellcon.CMF_EXPLORE:
63 print "CMF_EXPLORE..."
64 #items.append(msg + " - normal file, right-click in Explorer")
65 items.append(msg)
66 elif uFlags & CMF_DEFAULTONLY:
67 print "CMF_DEFAULTONLY...\r\n"
68 items.append("hello world - 1!")
69 else:
70 print "** unknown flags", uFlags
71 items.append("hello world - 2!")
72 win32gui.InsertMenu(hMenu, indexMenu,
73 win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
74 0, None)
75 indexMenu += 1
76 for item in items:
77 win32gui.InsertMenu(hMenu, indexMenu,
78 win32con.MF_STRING|win32con.MF_BYPOSITION,
79 idCmd, item)
80 indexMenu += 1
81 idCmd += 1
82
83 win32gui.InsertMenu(hMenu, indexMenu,
84 win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
85 0, None)
86 indexMenu += 1
87 return idCmd-idCmdFirst # Must return number of menu items we added.
88
89 def InvokeCommand(self, ci):
90 mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci
91 win32gui.MessageBox(hwnd, "\n".join(self.files_selected), "ArchiveMe", win32con.MB_OK)
92
93 def GetCommandString(self, cmd, typ):
94 return "Hello from Python!!"
95
96
97 def DllRegisterServer():
98 import _winreg
99
100 # extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html)
101
102 # for folders
103 folder_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Folder\\shellex")
104 folder_subkey = _winreg.CreateKey(folder_key, "ContextMenuHandlers")
105 folder_subkey2 = _winreg.CreateKey(folder_subkey, ShellExtension._reg_name_)
106 _winreg.SetValueEx(folder_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_)
107
108 # for files
109 file_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "*\\shellex")
110 file_subkey = _winreg.CreateKey(file_key, "ContextMenuHandlers")
111 file_subkey2 = _winreg.CreateKey(file_subkey, ShellExtension._reg_name_)
112 _winreg.SetValueEx(file_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_)
113
114 print ShellExtension._reg_desc_, "registration complete."
115
116
117 def DllUnregisterServer():
118 import _winreg
119
120 # extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html)
121
122 try:
123 # for folders
124 folder_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
125 "Folder\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_)
126 # for files
127 file_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
128 "*\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_)
129 except WindowsError, details:
130 import errno
131 if details.errno != errno.ENOENT:
132 raise
133 print ShellExtension._reg_desc_, "unregistration complete."
134
135 if __name__=='__main__':
136 from win32com.server import register
137 register.UseCommandLine(ShellExtension,
138 finalize_register = DllRegisterServer,
139 finalize_unregister = DllUnregisterServer)

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