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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by joko, Sat Jun 3 00:07:32 2006 UTC revision 1.4 by joko, Sat Jun 3 16:33:30 2006 UTC
# Line 8  Line 8 
8  # * Right-Click on a .py file - locate and click on 'Hello from Python' on  # * Right-Click on a .py file - locate and click on 'Hello from Python' on
9  #   the context menu.  #   the context menu.
10    
11    # see also: http://www.codeproject.com/shell/ShellExtGuide2.asp
12    
13  import pythoncom  import pythoncom
14  from win32com.shell import shell, shellcon  from win32com.shell import shell, shellcon
15  import win32gui  import win32gui
16  import win32con  import win32con
17    import win32api, winerror
18    
19  IContextMenu_Methods = ["QueryContextMenu", "InvokeCommand", "GetCommandString"]  IContextMenu_Methods = ["QueryContextMenu", "InvokeCommand", "GetCommandString"]
20  IShellExtInit_Methods = ["Initialize"]  IShellExtInit_Methods = ["Initialize"]
21    
22  class ShellExtension:  class ShellExtension:
23      _reg_progid_ = "Python.ShellExtension.ContextMenu"      _reg_progid_ = "Python.ShellExtension.ContextMenu"
24      _reg_desc_ = "Python Sample Shell Extension (context menu)"      _reg_name_ = "MessClient"
25        _reg_desc_ = "Mess Shell Extension"
26      _reg_clsid_ = "{CED0336C-C9EE-4a7f-8D7F-C660393C381F}"      _reg_clsid_ = "{CED0336C-C9EE-4a7f-8D7F-C660393C381F}"
27      _com_interfaces_ = [shell.IID_IShellExtInit, shell.IID_IContextMenu]      _com_interfaces_ = [shell.IID_IShellExtInit, shell.IID_IContextMenu]
28      _public_methods_ = IContextMenu_Methods + IShellExtInit_Methods      _public_methods_ = IContextMenu_Methods + IShellExtInit_Methods
# Line 28  class ShellExtension: Line 32  class ShellExtension:
32          self.dataobj = dataobj          self.dataobj = dataobj
33    
34      def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags):      def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags):
35            
36            # debug
37          print "QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags          print "QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags
38          # Query the items clicked on          
39            # query the items clicked on
40          format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL          format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL
41          sm = self.dataobj.GetData(format_etc)          sm = self.dataobj.GetData(format_etc)
42          num_files = shell.DragQueryFile(sm.data_handle, -1)          num_files = shell.DragQueryFile(sm.data_handle, -1)
43          if num_files>1:          
44              msg = "&Hello from Python (with %d files selected)" % num_files          #if num_files>1:
45          else:          #    msg = "&Hello from Python (with %d files selected)" % num_files
46              fname = shell.DragQueryFile(sm.data_handle, 0)          #else:
47              msg = "&Hello from Python (with '%s' selected)" % fname          #    fname = shell.DragQueryFile(sm.data_handle, 0)
48            #    msg = "&Hello from Python (with '%s' selected)" % fname
49            self.files_selected = []
50            for i in range(0, num_files):
51              fname = shell.DragQueryFile(sm.data_handle, i)
52              if fname == 0: continue
53              self.files_selected.append(fname)
54    
55            # build context menu(s)
56            msg = "A&rchiveMe"
57          idCmd = idCmdFirst          idCmd = idCmdFirst
58          items = []          items = []
59          if (uFlags & 0x000F) == shellcon.CMF_NORMAL: # Check == here, since CMF_NORMAL=0          if (uFlags & 0x000F) == shellcon.CMF_NORMAL: # Check == here, since CMF_NORMAL=0
60              print "CMF_NORMAL..."              print "CMF_NORMAL..."
61              items.append(msg)              items.append(msg + " - normal")
62          elif uFlags & shellcon.CMF_VERBSONLY:          elif uFlags & shellcon.CMF_VERBSONLY:
63              print "CMF_VERBSONLY..."              print "CMF_VERBSONLY..."
64              items.append(msg + " - shortcut")              items.append(msg + " - shortcut")
65          elif uFlags & shellcon.CMF_EXPLORE:          elif uFlags & shellcon.CMF_EXPLORE:
66              print "CMF_EXPLORE..."              print "CMF_EXPLORE..."
67              items.append(msg + " - normal file, right-click in Explorer")              #items.append(msg + " - normal file, right-click in Explorer")
68                items.append(msg)
69                items.append("Named Shortcut &1")
70                items.append("Named Shortcut &2")
71          elif uFlags & CMF_DEFAULTONLY:          elif uFlags & CMF_DEFAULTONLY:
72              print "CMF_DEFAULTONLY...\r\n"              print "CMF_DEFAULTONLY...\r\n"
73                items.append("hello world - 1!")
74          else:          else:
75              print "** unknown flags", uFlags              print "** unknown flags", uFlags
76                items.append("hello world - 2!")
77            
78            # create menu entries
79          win32gui.InsertMenu(hMenu, indexMenu,          win32gui.InsertMenu(hMenu, indexMenu,
80                              win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,                              win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
81                              0, None)                              idCmd, None)
82          indexMenu += 1          indexMenu += 1
83            idCmd += 1
84            
85          for item in items:          for item in items:
86              win32gui.InsertMenu(hMenu, indexMenu,              win32gui.InsertMenu(hMenu, indexMenu,
87                                  win32con.MF_STRING|win32con.MF_BYPOSITION,                                  win32con.MF_STRING|win32con.MF_BYPOSITION,
# Line 66  class ShellExtension: Line 91  class ShellExtension:
91    
92          win32gui.InsertMenu(hMenu, indexMenu,          win32gui.InsertMenu(hMenu, indexMenu,
93                              win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,                              win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
94                              0, None)                              idCmd, None)
95          indexMenu += 1          indexMenu += 1
96            idCmd += 1
97            
98          return idCmd-idCmdFirst # Must return number of menu items we added.          return idCmd-idCmdFirst # Must return number of menu items we added.
99    
100        def GetCommandString(self, cmdId, typ):
101            #if cmdId == 0:
102            #  return "Hello from Python!!"
103            #else:
104            #  return "abc"
105            return self._reg_name_ + "_" + cmdId;
106    
107      def InvokeCommand(self, ci):      def InvokeCommand(self, ci):
108          mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci          mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci
         win32gui.MessageBox(hwnd, "Hello", "Wow", win32con.MB_OK)  
109    
110      def GetCommandString(self, cmd, typ):          # If lpVerb really points to a string, ignore this function call and bail out.
111          return "Hello from Python!!"          if win32api.HIWORD( verb ) != 0:
112              return winerror.E_INVALIDARG
113            
114            function_id = win32api.LOWORD(verb)
115            # TODO: establish mapping with building the context menus in "QueryContextMenu"
116            if function_id == 1:
117              method = "ArchiveMe"
118            elif function_id == 2:
119              method = "Shortcut 1"
120            elif function_id == 3:
121              method = "Shortcut 2"
122            else:
123              method = "Unknown"
124            
125            # do action(s)
126            win32gui.MessageBox(hwnd, "\n".join(self.files_selected), method, win32con.MB_OK)
127    
128    
129  def DllRegisterServer():  def DllRegisterServer():
130      import _winreg      import _winreg
131      key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,      
132                              "Python.File\\shellex")      # extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html)
133      subkey = _winreg.CreateKey(key, "ContextMenuHandlers")  
134      subkey2 = _winreg.CreateKey(subkey, "PythonSample")      # for folders
135      _winreg.SetValueEx(subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_)      folder_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Folder\\shellex")
136        folder_subkey = _winreg.CreateKey(folder_key, "ContextMenuHandlers")
137        folder_subkey2 = _winreg.CreateKey(folder_subkey, ShellExtension._reg_name_)
138        _winreg.SetValueEx(folder_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_)
139    
140        # for files
141        file_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "*\\shellex")
142        file_subkey = _winreg.CreateKey(file_key, "ContextMenuHandlers")
143        file_subkey2 = _winreg.CreateKey(file_subkey, ShellExtension._reg_name_)
144        _winreg.SetValueEx(file_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_)
145    
146      print ShellExtension._reg_desc_, "registration complete."      print ShellExtension._reg_desc_, "registration complete."
147    
148    
149  def DllUnregisterServer():  def DllUnregisterServer():
150      import _winreg      import _winreg
151    
152        # extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html)
153    
154      try:      try:
155          key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,          # for folders
156                                  "Python.File\\shellex\\ContextMenuHandlers\\PythonSample")          folder_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
157                                    "Folder\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_)
158            # for files
159            file_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
160                                    "*\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_)
161      except WindowsError, details:      except WindowsError, details:
162          import errno          import errno
163          if details.errno != errno.ENOENT:          if details.errno != errno.ENOENT:

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.4

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