home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Shareware / Comunicatii / jyte / jyte.exe / tlbrowse.py < prev    next >
Text File  |  2002-10-17  |  8KB  |  243 lines

  1. import win32ui
  2. import win32con
  3. import win32api
  4. import string
  5. import commctrl
  6. import pythoncom
  7. from pywin.mfc import dialog
  8.  
  9. error = "TypeLib browser internal error"
  10.  
  11. FRAMEDLG_STD = win32con.WS_CAPTION | win32con.WS_SYSMENU
  12. SS_STD = win32con.WS_CHILD | win32con.WS_VISIBLE
  13. BS_STD = SS_STD  | win32con.WS_TABSTOP
  14. ES_STD = BS_STD | win32con.WS_BORDER
  15. LBS_STD = ES_STD | win32con.LBS_NOTIFY | win32con.LBS_NOINTEGRALHEIGHT | win32con.WS_VSCROLL
  16. CBS_STD = ES_STD | win32con.CBS_NOINTEGRALHEIGHT | win32con.WS_VSCROLL
  17.  
  18. typekindmap = {
  19.     pythoncom.TKIND_ENUM : 'Enumeration',
  20.     pythoncom.TKIND_RECORD : 'Record',
  21.     pythoncom.TKIND_MODULE : 'Module',
  22.     pythoncom.TKIND_INTERFACE : 'Interface',
  23.     pythoncom.TKIND_DISPATCH : 'Dispatch',
  24.     pythoncom.TKIND_COCLASS : 'CoClass',
  25.     pythoncom.TKIND_ALIAS : 'Alias',
  26.     pythoncom.TKIND_UNION : 'Union'
  27. }
  28.  
  29. TypeBrowseDialog_Parent=dialog.Dialog
  30. class TypeBrowseDialog(TypeBrowseDialog_Parent):
  31.     "Browse a type library"
  32.  
  33.     IDC_TYPELIST = 1000
  34.     IDC_MEMBERLIST = 1001
  35.     IDC_PARAMLIST = 1002
  36.     IDC_LISTVIEW = 1003
  37.  
  38.     def __init__(self, typefile = None):
  39.         TypeBrowseDialog_Parent.__init__(self, self.GetTemplate())
  40.         try:
  41.             if typefile:
  42.                 self.tlb = pythoncom.LoadTypeLib(typefile)
  43.             else:
  44.                 self.tlb = None
  45.         except pythoncom.ole_error:
  46.             self.MessageBox("The file does not contain type information")
  47.             self.tlb = None
  48.         self.HookCommand(self.CmdTypeListbox, self.IDC_TYPELIST)
  49.         self.HookCommand(self.CmdMemberListbox, self.IDC_MEMBERLIST)
  50.  
  51.     def OnAttachedObjectDeath(self):
  52.         self.tlb = None
  53.         self.typeinfo = None
  54.         self.attr = None
  55.         return TypeBrowseDialog_Parent.OnAttachedObjectDeath(self)
  56.  
  57.     def _SetupMenu(self):
  58.         menu = win32ui.CreateMenu()
  59.         flags=win32con.MF_STRING|win32con.MF_ENABLED
  60.         menu.AppendMenu(flags, win32ui.ID_FILE_OPEN, "&Open...")
  61.         menu.AppendMenu(flags, win32con.IDCANCEL, "&Close")
  62.         mainMenu = win32ui.CreateMenu()
  63.         mainMenu.AppendMenu(flags|win32con.MF_POPUP, menu.GetHandle(), "&File")
  64.         self.SetMenu(mainMenu)
  65.         self.HookCommand(self.OnFileOpen,win32ui.ID_FILE_OPEN)
  66.  
  67.     def OnFileOpen(self, id, code):
  68.         openFlags = win32con.OFN_OVERWRITEPROMPT | win32con.OFN_FILEMUSTEXIST
  69.         fspec = "Type Libraries (*.tlb, *.olb)|*.tlb;*.olb|OCX Files (*.ocx)|*.ocx|DLL's (*.dll)|*.dll|All Files (*.*)|*.*||"
  70.         dlg = win32ui.CreateFileDialog(1, None, None, openFlags, fspec)
  71.         if dlg.DoModal() == win32con.IDOK:
  72.             try:
  73.                 self.tlb = pythoncom.LoadTypeLib(dlg.GetPathName())
  74.             except pythoncom.ole_error:
  75.                 self.MessageBox("The file does not contain type information")
  76.                 self.tlb = None
  77.             self._SetupTLB()
  78.  
  79.     def OnInitDialog(self):
  80.         self._SetupMenu()
  81.         self.typelb = self.GetDlgItem(self.IDC_TYPELIST)
  82.         self.memberlb = self.GetDlgItem(self.IDC_MEMBERLIST)
  83.         self.paramlb = self.GetDlgItem(self.IDC_PARAMLIST)
  84.         self.listview = self.GetDlgItem(self.IDC_LISTVIEW)
  85.         
  86.         # Setup the listview columns
  87.         itemDetails = (commctrl.LVCFMT_LEFT, 100, "Item", 0)
  88.         self.listview.InsertColumn(0, itemDetails)
  89.         itemDetails = (commctrl.LVCFMT_LEFT, 1024, "Details", 0)
  90.         self.listview.InsertColumn(1, itemDetails)
  91.  
  92.         if self.tlb is None:
  93.             self.OnFileOpen(None,None)
  94.         else:
  95.             self._SetupTLB()
  96.         return TypeBrowseDialog_Parent.OnInitDialog(self)
  97.  
  98.     def _SetupTLB(self):
  99.         self.typelb.ResetContent()
  100.         self.memberlb.ResetContent()
  101.         self.paramlb.ResetContent()
  102.         self.typeinfo = None
  103.         self.attr = None
  104.         if self.tlb is None: return
  105.         n = self.tlb.GetTypeInfoCount()
  106.         for i in range(n):
  107.             self.typelb.AddString(self.tlb.GetDocumentation(i)[0])
  108.  
  109.     def _SetListviewTextItems(self, items):
  110.         self.listview.DeleteAllItems()
  111.         index = -1
  112.         for item in items:
  113.             index = self.listview.InsertItem(index+1,item[0])
  114.             data = item[1]
  115.             if data is None: data = ""
  116.             self.listview.SetItemText(index, 1, data)
  117.  
  118.     def SetupAllInfoTypes(self):
  119.         infos = self._GetMainInfoTypes() + self._GetMethodInfoTypes()
  120.         self._SetListviewTextItems(infos)
  121.  
  122.     def _GetMainInfoTypes(self):
  123.         pos = self.typelb.GetCurSel()
  124.         if pos<0: return []
  125.         docinfo = self.tlb.GetDocumentation(pos)
  126.         infos = [('GUID', str(self.attr[0]))]
  127.         infos.append(('Help File', docinfo[3]))
  128.         infos.append(('Help Context', str(docinfo[2])))
  129.         try:
  130.             infos.append(('Type Kind', typekindmap[self.tlb.GetTypeInfoType(pos)]))
  131.         except:
  132.             pass
  133.             
  134.         info = self.tlb.GetTypeInfo(pos)
  135.         attr = info.GetTypeAttr()
  136.         infos.append(('Attributes', str(attr)))
  137.             
  138.         for j in range(attr[8]):
  139.             flags = info.GetImplTypeFlags(j)
  140.             refInfo = info.GetRefTypeInfo(info.GetRefTypeOfImplType(j))
  141.             doc = refInfo.GetDocumentation(-1)
  142.             attr = refInfo.GetTypeAttr()
  143.             typeKind = attr[5]
  144.             typeFlags = attr[11]
  145.  
  146.             desc = doc[0]
  147.             desc = desc + ", Flags=0x%x, typeKind=0x%x, typeFlags=0x%x" % (flags, typeKind, typeFlags)
  148.             if flags & pythoncom.IMPLTYPEFLAG_FSOURCE:
  149.                 desc = desc + "(Source)"
  150.             infos.append( ('Implements', desc))
  151.  
  152.         return infos
  153.  
  154.     def _GetMethodInfoTypes(self):
  155.         pos = self.memberlb.GetCurSel()
  156.         if pos<0: return []
  157.  
  158.         realPos, isMethod = self._GetRealMemberPos(pos)
  159.         ret = []
  160.         if isMethod:
  161.             funcDesc = self.typeinfo.GetFuncDesc(realPos)
  162.             id = funcDesc[0]
  163.             ret.append(("Func Desc", str(funcDesc)))
  164.         else:
  165.             id = self.typeinfo.GetVarDesc(realPos)[0]
  166.         
  167.         docinfo = self.typeinfo.GetDocumentation(id)
  168.         ret.append(('Help String', docinfo[1]))
  169.         ret.append(('Help Context', str(docinfo[2])))
  170.         return ret
  171.  
  172.     def CmdTypeListbox(self, id, code):
  173.         if code == win32con.LBN_SELCHANGE:
  174.             pos = self.typelb.GetCurSel()
  175.             if pos >= 0:
  176.                 self.memberlb.ResetContent()
  177.                 self.typeinfo = self.tlb.GetTypeInfo(pos)
  178.                 self.attr = self.typeinfo.GetTypeAttr()
  179.                 for i in range(self.attr[7]):
  180.                     id = self.typeinfo.GetVarDesc(i)[0]
  181.                     self.memberlb.AddString(self.typeinfo.GetNames(id)[0])
  182.                 for i in range(self.attr[6]):
  183.                     id = self.typeinfo.GetFuncDesc(i)[0]
  184.                     self.memberlb.AddString(self.typeinfo.GetNames(id)[0])
  185.                 self.SetupAllInfoTypes()
  186.             return 1
  187.  
  188.     def _GetRealMemberPos(self, pos):
  189.         pos = self.memberlb.GetCurSel()
  190.         if pos >= self.attr[7]:
  191.             return pos - self.attr[7], 1
  192.         elif pos >= 0:
  193.             return pos, 0
  194.         else:
  195.             raise error, "The position is not valid"
  196.             
  197.     def CmdMemberListbox(self, id, code):
  198.         if code == win32con.LBN_SELCHANGE:
  199.             self.paramlb.ResetContent()
  200.             pos = self.memberlb.GetCurSel()
  201.             realPos, isMethod = self._GetRealMemberPos(pos)
  202.             if isMethod:
  203.                 id = self.typeinfo.GetFuncDesc(realPos)[0]
  204.                 names = self.typeinfo.GetNames(id)
  205.                 for i in range(len(names)):
  206.                     if i > 0:
  207.                         self.paramlb.AddString(names[i])
  208.             self.SetupAllInfoTypes()
  209.             return 1
  210.  
  211.     def GetTemplate(self):
  212.         "Return the template used to create this dialog"
  213.  
  214.         w = 272  # Dialog width
  215.         h = 192  # Dialog height
  216.         style = FRAMEDLG_STD | win32con.WS_VISIBLE | win32con.DS_SETFONT | win32con.WS_MINIMIZEBOX
  217.         template = [['Type Library Browser', (0, 0, w, h), style, None, (8, 'Helv')], ]
  218.         template.append([130, "&Type", -1, (10, 10, 62, 9), SS_STD | win32con.SS_LEFT])
  219.         template.append([131, None, self.IDC_TYPELIST, (10, 20, 80, 80), LBS_STD])
  220.         template.append([130, "&Members", -1, (100, 10, 62, 9), SS_STD | win32con.SS_LEFT])
  221.         template.append([131, None, self.IDC_MEMBERLIST, (100, 20, 80, 80), LBS_STD])
  222.         template.append([130, "&Parameters", -1, (190, 10, 62, 9), SS_STD | win32con.SS_LEFT])
  223.         template.append([131, None, self.IDC_PARAMLIST, (190, 20, 75, 80), LBS_STD])
  224.         
  225.         lvStyle = SS_STD | commctrl.LVS_REPORT | commctrl.LVS_AUTOARRANGE | commctrl.LVS_ALIGNLEFT | win32con.WS_BORDER | win32con.WS_TABSTOP
  226.         template.append(["SysListView32", "", self.IDC_LISTVIEW, (10, 110, 255, 65), lvStyle])
  227.  
  228.         return template
  229.  
  230. if __name__=='__main__':
  231.     import sys
  232.     fname = None
  233.     try:
  234.         fname = sys.argv[1]
  235.     except:
  236.         pass
  237.     dlg = TypeBrowseDialog(fname)
  238.     try:
  239.         win32api.GetConsoleTitle()
  240.         dlg.DoModal()
  241.     except:
  242.         dlg.CreateWindow(win32ui.GetMainFrame())
  243.