/[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.3 by joko, Sat Jun 3 15:11:27 2006 UTC revision 1.7 by joko, Sat Jun 3 22:27:45 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, win32gui_struct
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"]
# Line 24  class ShellExtension: Line 27  class ShellExtension:
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
29    
30      def Initialize(self, folder, dataobj, hkey):      def Initialize(self, pidlFolder, dataobj, hkey):
31          print "Init", folder, dataobj, hkey          print "Init", pidlFolder, dataobj, hkey
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          # debug
37          print "QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags          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          # query the items clicked on            # grab all items
50          format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL            self.files_selected = []
51          sm = self.dataobj.GetData(format_etc)            for i in range(0, num_files):
52          num_files = shell.DragQueryFile(sm.data_handle, -1)              fname = shell.DragQueryFile(sm.data_handle, i)
53                        if fname == 0: continue
54          #if num_files>1:              self.files_selected.append(fname)
         #    msg = "&Hello from Python (with %d files selected)" % num_files  
         #else:  
         #    fname = shell.DragQueryFile(sm.data_handle, 0)  
         #    msg = "&Hello from Python (with '%s' selected)" % fname  
         self.files_selected = []  
         for i in range(0, num_files):  
           fname = shell.DragQueryFile(sm.data_handle, i)  
           if fname == 0: continue  
           self.files_selected.append(fname)  
55    
56          # build context menu(s)  
57          msg = "A&rchiveMe"          # calculate items to be added to context menu
         idCmd = idCmdFirst  
58          items = []          items = []
59          if (uFlags & 0x000F) == shellcon.CMF_NORMAL: # Check == here, since CMF_NORMAL=0          
60              print "CMF_NORMAL..."          # CMF_NORMAL: e.g. files on desktop
61              items.append(msg)          # CMF_VERBSONLY: e.g. shortcuts
62          elif uFlags & shellcon.CMF_VERBSONLY:          # CMF_EXPLORE: regular files/folders via explorer
63              print "CMF_VERBSONLY..."          
64              items.append(msg + " - shortcut")          # Check == here, since CMF_NORMAL=0
65          elif uFlags & shellcon.CMF_EXPLORE:          if ((uFlags & 0x000F) == shellcon.CMF_NORMAL) or (uFlags & shellcon.CMF_VERBSONLY) or (uFlags & shellcon.CMF_EXPLORE):
66              print "CMF_EXPLORE..."              if self.dataobj:
67              #items.append(msg + " - normal file, right-click in Explorer")                items.append("Add to A&rchive")
68              items.append(msg)                items.append("Named Shortcut &1")
69                  items.append("Named Shortcut &2")
70          elif uFlags & CMF_DEFAULTONLY:          elif uFlags & CMF_DEFAULTONLY:
71              print "CMF_DEFAULTONLY...\r\n"              print "CMF_DEFAULTONLY...\r\n"
72              items.append("hello world - 1!")              items.append("hello world - 1!")
73          else:          else:
74              print "** unknown flags", uFlags              print "** unknown flags", uFlags
75              items.append("hello world - 2!")              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,          win32gui.InsertMenu(hMenu, indexMenu,
83                              win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,                              win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
84                              0, None)                              idCmd, None)
85          indexMenu += 1          indexMenu += 1
86            idCmd += 1
87            
88            # create main menu entries
89          for item in items:          for item in items:
90              win32gui.InsertMenu(hMenu, indexMenu,              win32gui.InsertMenu(hMenu, indexMenu,
91                                  win32con.MF_STRING|win32con.MF_BYPOSITION,                                  win32con.MF_STRING|win32con.MF_BYPOSITION,
92                                  idCmd, item)                                  idCmd, item)
93              indexMenu += 1              indexMenu += 1
94              idCmd += 1              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,          win32gui.InsertMenu(hMenu, indexMenu,
115                              win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,                              win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
116                              0, None)                              idCmd, None)
117          indexMenu += 1          indexMenu += 1
118            idCmd += 1
119    
120          return idCmd-idCmdFirst # Must return number of menu items we added.          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):      def InvokeCommand(self, ci):
130          mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci          mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci
         win32gui.MessageBox(hwnd, "\n".join(self.files_selected), "ArchiveMe", win32con.MB_OK)  
131    
132      def GetCommandString(self, cmd, typ):          # If lpVerb really points to a string, ignore this function call and bail out.
133          return "Hello from Python!!"          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():  def DllRegisterServer():
# Line 99  def DllRegisterServer(): Line 156  def DllRegisterServer():
156            
157      # extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html)      # 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      # for folders
166      folder_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Folder\\shellex")      folder_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Folder\\shellex")
167      folder_subkey = _winreg.CreateKey(folder_key, "ContextMenuHandlers")      folder_subkey = _winreg.CreateKey(folder_key, "ContextMenuHandlers")
# Line 120  def DllUnregisterServer(): Line 183  def DllUnregisterServer():
183      # extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html)      # extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html)
184    
185      try:      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          # for folders
190          folder_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,          folder_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
191                                  "Folder\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_)                                  "Folder\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_)

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.7

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