1 # dirctrl.py: extended version of the GenericDirCtrl
2 # arch-tag: extended version of the GenericDirCtrl
3 # author: Alberto Griggio <albgrig@tiscalinet.it>
13 class DirCtrl(wx
.GenericDirCtrl
):
14 def __init__(self
, *args
, **kwds
):
15 wx
.GenericDirCtrl
.__init
__(self
, *args
, **kwds
)
16 self
._handler
= wx
.EvtHandler()
17 self
.PushEventHandler(self
._handler
)
18 self
._added
_items
= {}
19 self
._virtual
_tree
_done
= {}
20 self
.tree
= wx
.PyTypeCast(self
.GetTreeCtrl(), 'wxTreeCtrl')
21 imglist
= self
.tree
.GetImageList()
22 if wx
.Platform
== '__WXGTK__': bmpfile
= 'zip.xpm'
23 else: bmpfile
= 'zip_win.xpm'
24 bmp
= wx
.Bitmap(os
.path
.join('icons', bmpfile
), wx
.BITMAP_TYPE_XPM
)
25 self
.zip_image
= imglist
.Add(bmp
)
27 wx
.EVT_TREE_ITEM_EXPANDING(self
._handler
, self
.tree
.GetId(),
29 wx
.EVT_TREE_ITEM_COLLAPSED(self
._handler
, self
.tree
.GetId(),
31 wx
.EVT_TREE_SEL_CHANGED(self
._handler
, self
.tree
.GetId(),
34 def build_path(self
, node
):
35 if self
.tree
.GetItemParent(node
) == self
.tree
.GetRootItem():
36 return self
.tree
.GetItemText(node
)
37 return os
.path
.join(self
.build_path(self
.tree
.GetItemParent(node
)),
38 self
.tree
.GetItemText(node
))
40 def add_virtual_tree(self
, node
, path
, zipfilename
):
41 zvfs
= vfs
.get_dirtree(zipfilename
)
42 def add_rec(n
, p
, vfsn
):
43 for vfschild
in vfsn
.children
:
44 label
= vfschild
.name
.replace('/', '')
45 child
= self
.tree
.AppendItem(n
, label
, 0, 1)
46 p1
= p
.replace('#zip:', '/')
47 if not p1
.endswith('/'): p1
+= '/'
49 if p
.endswith('#zip:'): p2
= p
+ label
50 else: p2
= p
+ '/' + label
51 self
._added
_items
[p1
] = (p2
, child
)
52 add_rec(child
, p2
, vfschild
)
53 add_rec(node
, path
, zvfs
.root
)
54 self
._virtual
_tree
_done
[path
] = 1
56 def on_expand(self
, event
):
57 item
= event
.GetItem()
59 path
= self
.build_path(item
)
60 if path
in self
._added
_items
:
63 # first search the subdirs without children, in case there
64 # are some zipfiles there aswell (otherwise it will be
65 # impossible to select them)
66 child
, cookie
= self
.tree
.GetFirstChild(item
, 0)
68 if not child
.Ok(): break
69 if not self
.tree
.ItemHasChildren(child
):
70 if glob
.glob(os
.path
.join(
71 path
, self
.tree
.GetItemText(child
), '*.zip')):
72 self
.tree
.SetItemHasChildren(child
)
73 child
, cookie
= self
.tree
.GetNextChild(item
, cookie
)
74 # then add the zipfiles on this directory
75 for name
in glob
.glob(os
.path
.join(path
, '*.zip')):
76 i
= self
.tree
.AppendItem(item
, os
.path
.basename(name
),
78 self
._added
_items
[name
] = (name
+ '#zip:', i
)
83 def on_collapse(self
, event
):
84 item
= event
.GetItem()
85 if self
.build_path(item
) in self
._added
_items
:
89 def on_sel_changed(self
, event
):
90 item
= event
.GetItem()
91 path
= self
.build_path(item
)
92 if path
in self
._added
_items
:
93 p
, n
= self
._added
_items
[path
]
94 if p
.endswith('#zip:') and p
not in self
._virtual
_tree
_done
:
95 self
.add_virtual_tree(n
, p
, path
)
99 item
= self
.tree
.GetSelection()
100 if item
!= self
.tree
.GetRootItem():
101 path
= self
.build_path(item
)
102 if path
in self
._added
_items
:
103 return self
._added
_items
[path
][0]
104 return wx
.GenericDirCtrl
.GetPath(self
)
106 def SetPath(self
, path
):
107 index
= path
.find('#zip:')
109 if index
== len(path
)-5: path
= path
[:-5]
110 else: path
= path
.replace('#zip:', '/')
111 if path
in self
._added
_items
:
112 item
= self
._added
_items
[path
][1]
113 self
.tree
.SelectItem(item
)
114 self
.tree
.EnsureVisible(item
)
117 if vfs
.is_virtual(path
):
118 # this is a zipfile, but not added to the tree yet:
120 wx
.GenericDirCtrl
.SetPath(self
, path
)
121 self
.tree
.Expand(self
.tree
.GetSelection())
122 wx
.CallAfter(self
.SetPath
, path
+ '#zip:')
124 wx
.GenericDirCtrl
.SetPath(self
, path
)
127 sel
= self
.tree
.GetSelection()
128 if not sel
.Ok(): return
129 sel
= self
.tree
.GetItemParent(sel
)
130 if not sel
.Ok() or sel
== self
.tree
.GetRootItem(): return
131 self
.tree
.SelectItem(sel
)
133 # end of class DirCtrl