hiertest.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import win32ui
  2. import os
  3. import commctrl
  4. from pywin.tools import hierlist
  5. from pywin.mfc import docview, window
  6. # directory listbox
  7. # This has obvious limitations - doesnt track subdirs, etc. Demonstrates
  8. # simple use of Python code for querying the tree as needed.
  9. # Only use strings, and lists of strings (from curdir())
  10. class DirHierList(hierlist.HierList):
  11. def __init__(self, root, listBoxID = win32ui.IDC_LIST1):
  12. hierlist.HierList.__init__(self, root, win32ui.IDB_HIERFOLDERS, listBoxID)
  13. def GetText(self, item):
  14. return os.path.basename(item)
  15. def GetSubList(self, item):
  16. if os.path.isdir(item):
  17. ret = [os.path.join(item, fname) for fname in os.listdir(item)]
  18. else:
  19. ret = None
  20. return ret
  21. # if the item is a dir, it is expandable.
  22. def IsExpandable(self, item):
  23. return os.path.isdir(item)
  24. def GetSelectedBitmapColumn(self, item):
  25. return self.GetBitmapColumn(item)+6 # Use different color for selection
  26. class TestDocument(docview.Document):
  27. def __init__(self, template):
  28. docview.Document.__init__(self, template)
  29. self.hierlist = hierlist.HierListWithItems(HLIFileDir("\\"), win32ui.IDB_HIERFOLDERS, win32ui.AFX_IDW_PANE_FIRST)
  30. class HierListView(docview.TreeView):
  31. def OnInitialUpdate(self):
  32. rc = self._obj_.OnInitialUpdate()
  33. self.hierList = self.GetDocument().hierlist
  34. self.hierList.HierInit(self.GetParent())
  35. self.hierList.SetStyle(commctrl.TVS_HASLINES | commctrl.TVS_LINESATROOT | commctrl.TVS_HASBUTTONS)
  36. return rc
  37. class HierListFrame(window.MDIChildWnd):
  38. pass
  39. def GetTestRoot():
  40. tree1 = ('Tree 1',[('Item 1','Item 1 data'),'Item 2',3])
  41. tree2 = ('Tree 2',[('Item 2.1','Item 2 data'),'Item 2.2',2.3])
  42. return ('Root',[tree1,tree2,'Item 3'])
  43. def demoboth():
  44. template = docview.DocTemplate(win32ui.IDR_PYTHONTYPE, TestDocument, HierListFrame, HierListView)
  45. template.OpenDocumentFile(None).SetTitle("Hierlist demo")
  46. demomodeless()
  47. def demomodeless():
  48. testList2=DirHierList("\\")
  49. dlg=hierlist.HierDialog('hier list test',testList2)
  50. dlg.CreateWindow()
  51. def demodlg ():
  52. testList2=DirHierList("\\")
  53. dlg=hierlist.HierDialog('hier list test',testList2)
  54. dlg.DoModal()
  55. def demo():
  56. template = docview.DocTemplate(win32ui.IDR_PYTHONTYPE, TestDocument, HierListFrame, HierListView)
  57. template.OpenDocumentFile(None).SetTitle("Hierlist demo")
  58. #
  59. # Demo/Test for HierList items.
  60. #
  61. # Easy to make a better directory program.
  62. #
  63. class HLIFileDir(hierlist.HierListItem):
  64. def __init__( self, filename ):
  65. self.filename = filename
  66. hierlist.HierListItem.__init__(self)
  67. def GetText(self):
  68. try:
  69. return "%-20s %d bytes" % (os.path.basename(self.filename), os.stat(self.filename)[6])
  70. except os.error as details:
  71. return "%-20s - %s" % (self.filename, details[1])
  72. def IsExpandable(self):
  73. return os.path.isdir(self.filename)
  74. def GetSubList(self):
  75. ret = []
  76. for newname in os.listdir(self.filename):
  77. if newname not in ['.', '..']:
  78. ret.append( HLIFileDir( os.path.join(self.filename,newname ) ) )
  79. return ret
  80. def demohli():
  81. template = docview.DocTemplate(win32ui.IDR_PYTHONTYPE, TestDocument, hierlist.HierListFrame, hierlist.HierListView)
  82. template.OpenDocumentFile(None).SetTitle("Hierlist demo")
  83. if __name__=='__main__':
  84. import demoutils
  85. if demoutils.HaveGoodGUI():
  86. demoboth()
  87. else:
  88. demodlg()