PyXR

c:\python24\lib\site-packages\win32 \ com \ client \ tlbrowse.py



0001 import win32ui
0002 import win32con
0003 import win32api
0004 import string
0005 import commctrl
0006 import pythoncom
0007 from pywin.mfc import dialog
0008 
0009 error = "TypeLib browser internal error"
0010 
0011 FRAMEDLG_STD = win32con.WS_CAPTION | win32con.WS_SYSMENU
0012 SS_STD = win32con.WS_CHILD | win32con.WS_VISIBLE
0013 BS_STD = SS_STD  | win32con.WS_TABSTOP
0014 ES_STD = BS_STD | win32con.WS_BORDER
0015 LBS_STD = ES_STD | win32con.LBS_NOTIFY | win32con.LBS_NOINTEGRALHEIGHT | win32con.WS_VSCROLL
0016 CBS_STD = ES_STD | win32con.CBS_NOINTEGRALHEIGHT | win32con.WS_VSCROLL
0017 
0018 typekindmap = {
0019         pythoncom.TKIND_ENUM : 'Enumeration',
0020         pythoncom.TKIND_RECORD : 'Record',
0021         pythoncom.TKIND_MODULE : 'Module',
0022         pythoncom.TKIND_INTERFACE : 'Interface',
0023         pythoncom.TKIND_DISPATCH : 'Dispatch',
0024         pythoncom.TKIND_COCLASS : 'CoClass',
0025         pythoncom.TKIND_ALIAS : 'Alias',
0026         pythoncom.TKIND_UNION : 'Union'
0027 }
0028 
0029 TypeBrowseDialog_Parent=dialog.Dialog
0030 class TypeBrowseDialog(TypeBrowseDialog_Parent):
0031         "Browse a type library"
0032 
0033         IDC_TYPELIST = 1000
0034         IDC_MEMBERLIST = 1001
0035         IDC_PARAMLIST = 1002
0036         IDC_LISTVIEW = 1003
0037 
0038         def __init__(self, typefile = None):
0039                 TypeBrowseDialog_Parent.__init__(self, self.GetTemplate())
0040                 try:
0041                         if typefile:
0042                                 self.tlb = pythoncom.LoadTypeLib(typefile)
0043                         else:
0044                                 self.tlb = None
0045                 except pythoncom.ole_error:
0046                         self.MessageBox("The file does not contain type information")
0047                         self.tlb = None
0048                 self.HookCommand(self.CmdTypeListbox, self.IDC_TYPELIST)
0049                 self.HookCommand(self.CmdMemberListbox, self.IDC_MEMBERLIST)
0050 
0051         def OnAttachedObjectDeath(self):
0052                 self.tlb = None
0053                 self.typeinfo = None
0054                 self.attr = None
0055                 return TypeBrowseDialog_Parent.OnAttachedObjectDeath(self)
0056 
0057         def _SetupMenu(self):
0058                 menu = win32ui.CreateMenu()
0059                 flags=win32con.MF_STRING|win32con.MF_ENABLED
0060                 menu.AppendMenu(flags, win32ui.ID_FILE_OPEN, "&Open...")
0061                 menu.AppendMenu(flags, win32con.IDCANCEL, "&Close")
0062                 mainMenu = win32ui.CreateMenu()
0063                 mainMenu.AppendMenu(flags|win32con.MF_POPUP, menu.GetHandle(), "&File")
0064                 self.SetMenu(mainMenu)
0065                 self.HookCommand(self.OnFileOpen,win32ui.ID_FILE_OPEN)
0066 
0067         def OnFileOpen(self, id, code):
0068                 openFlags = win32con.OFN_OVERWRITEPROMPT | win32con.OFN_FILEMUSTEXIST
0069                 fspec = "Type Libraries (*.tlb, *.olb)|*.tlb;*.olb|OCX Files (*.ocx)|*.ocx|DLL's (*.dll)|*.dll|All Files (*.*)|*.*||"
0070                 dlg = win32ui.CreateFileDialog(1, None, None, openFlags, fspec)
0071                 if dlg.DoModal() == win32con.IDOK:
0072                         try:
0073                                 self.tlb = pythoncom.LoadTypeLib(dlg.GetPathName())
0074                         except pythoncom.ole_error:
0075                                 self.MessageBox("The file does not contain type information")
0076                                 self.tlb = None
0077                         self._SetupTLB()
0078 
0079         def OnInitDialog(self):
0080                 self._SetupMenu()
0081                 self.typelb = self.GetDlgItem(self.IDC_TYPELIST)
0082                 self.memberlb = self.GetDlgItem(self.IDC_MEMBERLIST)
0083                 self.paramlb = self.GetDlgItem(self.IDC_PARAMLIST)
0084                 self.listview = self.GetDlgItem(self.IDC_LISTVIEW)
0085                 
0086                 # Setup the listview columns
0087                 itemDetails = (commctrl.LVCFMT_LEFT, 100, "Item", 0)
0088                 self.listview.InsertColumn(0, itemDetails)
0089                 itemDetails = (commctrl.LVCFMT_LEFT, 1024, "Details", 0)
0090                 self.listview.InsertColumn(1, itemDetails)
0091 
0092                 if self.tlb is None:
0093                         self.OnFileOpen(None,None)
0094                 else:
0095                         self._SetupTLB()
0096                 return TypeBrowseDialog_Parent.OnInitDialog(self)
0097 
0098         def _SetupTLB(self):
0099                 self.typelb.ResetContent()
0100                 self.memberlb.ResetContent()
0101                 self.paramlb.ResetContent()
0102                 self.typeinfo = None
0103                 self.attr = None
0104                 if self.tlb is None: return
0105                 n = self.tlb.GetTypeInfoCount()
0106                 for i in range(n):
0107                         self.typelb.AddString(self.tlb.GetDocumentation(i)[0])
0108 
0109         def _SetListviewTextItems(self, items):
0110                 self.listview.DeleteAllItems()
0111                 index = -1
0112                 for item in items:
0113                         index = self.listview.InsertItem(index+1,item[0])
0114                         data = item[1]
0115                         if data is None: data = ""
0116                         self.listview.SetItemText(index, 1, data)
0117 
0118         def SetupAllInfoTypes(self):
0119                 infos = self._GetMainInfoTypes() + self._GetMethodInfoTypes()
0120                 self._SetListviewTextItems(infos)
0121 
0122         def _GetMainInfoTypes(self):
0123                 pos = self.typelb.GetCurSel()
0124                 if pos<0: return []
0125                 docinfo = self.tlb.GetDocumentation(pos)
0126                 infos = [('GUID', str(self.attr[0]))]
0127                 infos.append(('Help File', docinfo[3]))
0128                 infos.append(('Help Context', str(docinfo[2])))
0129                 try:
0130                         infos.append(('Type Kind', typekindmap[self.tlb.GetTypeInfoType(pos)]))
0131                 except:
0132                         pass
0133                         
0134                 info = self.tlb.GetTypeInfo(pos)
0135                 attr = info.GetTypeAttr()
0136                 infos.append(('Attributes', str(attr)))
0137                         
0138                 for j in range(attr[8]):
0139                         flags = info.GetImplTypeFlags(j)
0140                         refInfo = info.GetRefTypeInfo(info.GetRefTypeOfImplType(j))
0141                         doc = refInfo.GetDocumentation(-1)
0142                         attr = refInfo.GetTypeAttr()
0143                         typeKind = attr[5]
0144                         typeFlags = attr[11]
0145 
0146                         desc = doc[0]
0147                         desc = desc + ", Flags=0x%x, typeKind=0x%x, typeFlags=0x%x" % (flags, typeKind, typeFlags)
0148                         if flags & pythoncom.IMPLTYPEFLAG_FSOURCE:
0149                                 desc = desc + "(Source)"
0150                         infos.append( ('Implements', desc))
0151 
0152                 return infos
0153 
0154         def _GetMethodInfoTypes(self):
0155                 pos = self.memberlb.GetCurSel()
0156                 if pos<0: return []
0157 
0158                 realPos, isMethod = self._GetRealMemberPos(pos)
0159                 ret = []
0160                 if isMethod:
0161                         funcDesc = self.typeinfo.GetFuncDesc(realPos)
0162                         id = funcDesc[0]
0163                         ret.append(("Func Desc", str(funcDesc)))
0164                 else:
0165                         id = self.typeinfo.GetVarDesc(realPos)[0]
0166                 
0167                 docinfo = self.typeinfo.GetDocumentation(id)
0168                 ret.append(('Help String', docinfo[1]))
0169                 ret.append(('Help Context', str(docinfo[2])))
0170                 return ret
0171 
0172         def CmdTypeListbox(self, id, code):
0173                 if code == win32con.LBN_SELCHANGE:
0174                         pos = self.typelb.GetCurSel()
0175                         if pos >= 0:
0176                                 self.memberlb.ResetContent()
0177                                 self.typeinfo = self.tlb.GetTypeInfo(pos)
0178                                 self.attr = self.typeinfo.GetTypeAttr()
0179                                 for i in range(self.attr[7]):
0180                                         id = self.typeinfo.GetVarDesc(i)[0]
0181                                         self.memberlb.AddString(self.typeinfo.GetNames(id)[0])
0182                                 for i in range(self.attr[6]):
0183                                         id = self.typeinfo.GetFuncDesc(i)[0]
0184                                         self.memberlb.AddString(self.typeinfo.GetNames(id)[0])
0185                                 self.SetupAllInfoTypes()
0186                         return 1
0187 
0188         def _GetRealMemberPos(self, pos):
0189                 pos = self.memberlb.GetCurSel()
0190                 if pos >= self.attr[7]:
0191                         return pos - self.attr[7], 1
0192                 elif pos >= 0:
0193                         return pos, 0
0194                 else:
0195                         raise error, "The position is not valid"
0196                         
0197         def CmdMemberListbox(self, id, code):
0198                 if code == win32con.LBN_SELCHANGE:
0199                         self.paramlb.ResetContent()
0200                         pos = self.memberlb.GetCurSel()
0201                         realPos, isMethod = self._GetRealMemberPos(pos)
0202                         if isMethod:
0203                                 id = self.typeinfo.GetFuncDesc(realPos)[0]
0204                                 names = self.typeinfo.GetNames(id)
0205                                 for i in range(len(names)):
0206                                         if i > 0:
0207                                                 self.paramlb.AddString(names[i])
0208                         self.SetupAllInfoTypes()
0209                         return 1
0210 
0211         def GetTemplate(self):
0212                 "Return the template used to create this dialog"
0213 
0214                 w = 272  # Dialog width
0215                 h = 192  # Dialog height
0216                 style = FRAMEDLG_STD | win32con.WS_VISIBLE | win32con.DS_SETFONT | win32con.WS_MINIMIZEBOX
0217                 template = [['Type Library Browser', (0, 0, w, h), style, None, (8, 'Helv')], ]
0218                 template.append([130, "&Type", -1, (10, 10, 62, 9), SS_STD | win32con.SS_LEFT])
0219                 template.append([131, None, self.IDC_TYPELIST, (10, 20, 80, 80), LBS_STD])
0220                 template.append([130, "&Members", -1, (100, 10, 62, 9), SS_STD | win32con.SS_LEFT])
0221                 template.append([131, None, self.IDC_MEMBERLIST, (100, 20, 80, 80), LBS_STD])
0222                 template.append([130, "&Parameters", -1, (190, 10, 62, 9), SS_STD | win32con.SS_LEFT])
0223                 template.append([131, None, self.IDC_PARAMLIST, (190, 20, 75, 80), LBS_STD])
0224                 
0225                 lvStyle = SS_STD | commctrl.LVS_REPORT | commctrl.LVS_AUTOARRANGE | commctrl.LVS_ALIGNLEFT | win32con.WS_BORDER | win32con.WS_TABSTOP
0226                 template.append(["SysListView32", "", self.IDC_LISTVIEW, (10, 110, 255, 65), lvStyle])
0227 
0228                 return template
0229 
0230 if __name__=='__main__':
0231         import sys
0232         fname = None
0233         try:
0234                 fname = sys.argv[1]
0235         except:
0236                 pass
0237         dlg = TypeBrowseDialog(fname)
0238         try:
0239                 win32api.GetConsoleTitle()
0240                 dlg.DoModal()
0241         except:
0242                 dlg.CreateWindow(win32ui.GetMainFrame())
0243 

Generated by PyXR 0.9.4
SourceForge.net Logo