/[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.1 - (show annotations)
Sat Jun 3 00:07:32 2006 UTC (18 years, 7 months ago) by joko
Branch: MAIN
File MIME type: text/x-python
+ initial commit

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_desc_ = "Python Sample Shell Extension (context menu)"
22 _reg_clsid_ = "{CED0336C-C9EE-4a7f-8D7F-C660393C381F}"
23 _com_interfaces_ = [shell.IID_IShellExtInit, shell.IID_IContextMenu]
24 _public_methods_ = IContextMenu_Methods + IShellExtInit_Methods
25
26 def Initialize(self, folder, dataobj, hkey):
27 print "Init", folder, dataobj, hkey
28 self.dataobj = dataobj
29
30 def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags):
31 print "QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags
32 # Query the items clicked on
33 format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL
34 sm = self.dataobj.GetData(format_etc)
35 num_files = shell.DragQueryFile(sm.data_handle, -1)
36 if num_files>1:
37 msg = "&Hello from Python (with %d files selected)" % num_files
38 else:
39 fname = shell.DragQueryFile(sm.data_handle, 0)
40 msg = "&Hello from Python (with '%s' selected)" % fname
41 idCmd = idCmdFirst
42 items = []
43 if (uFlags & 0x000F) == shellcon.CMF_NORMAL: # Check == here, since CMF_NORMAL=0
44 print "CMF_NORMAL..."
45 items.append(msg)
46 elif uFlags & shellcon.CMF_VERBSONLY:
47 print "CMF_VERBSONLY..."
48 items.append(msg + " - shortcut")
49 elif uFlags & shellcon.CMF_EXPLORE:
50 print "CMF_EXPLORE..."
51 items.append(msg + " - normal file, right-click in Explorer")
52 elif uFlags & CMF_DEFAULTONLY:
53 print "CMF_DEFAULTONLY...\r\n"
54 else:
55 print "** unknown flags", uFlags
56 win32gui.InsertMenu(hMenu, indexMenu,
57 win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
58 0, None)
59 indexMenu += 1
60 for item in items:
61 win32gui.InsertMenu(hMenu, indexMenu,
62 win32con.MF_STRING|win32con.MF_BYPOSITION,
63 idCmd, item)
64 indexMenu += 1
65 idCmd += 1
66
67 win32gui.InsertMenu(hMenu, indexMenu,
68 win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
69 0, None)
70 indexMenu += 1
71 return idCmd-idCmdFirst # Must return number of menu items we added.
72
73 def InvokeCommand(self, ci):
74 mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci
75 win32gui.MessageBox(hwnd, "Hello", "Wow", win32con.MB_OK)
76
77 def GetCommandString(self, cmd, typ):
78 return "Hello from Python!!"
79
80 def DllRegisterServer():
81 import _winreg
82 key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,
83 "Python.File\\shellex")
84 subkey = _winreg.CreateKey(key, "ContextMenuHandlers")
85 subkey2 = _winreg.CreateKey(subkey, "PythonSample")
86 _winreg.SetValueEx(subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_)
87 print ShellExtension._reg_desc_, "registration complete."
88
89 def DllUnregisterServer():
90 import _winreg
91 try:
92 key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
93 "Python.File\\shellex\\ContextMenuHandlers\\PythonSample")
94 except WindowsError, details:
95 import errno
96 if details.errno != errno.ENOENT:
97 raise
98 print ShellExtension._reg_desc_, "unregistration complete."
99
100 if __name__=='__main__':
101 from win32com.server import register
102 register.UseCommandLine(ShellExtension,
103 finalize_register = DllRegisterServer,
104 finalize_unregister = DllUnregisterServer)

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