forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckdirtreectrl.cpp
158 lines (140 loc) · 4.25 KB
/
checkdirtreectrl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "checkdirtreectrl.h"
#include "wx/dir.h"
#include "wx/filename.h"
BEGIN_EVENT_TABLE(CheckDirTreeCtrl, wxCheckTreeCtrl)
EVT_TREE_ITEM_EXPANDED(wxID_ANY, CheckDirTreeCtrl::OnItemExpading)
EVT_CHECKTREE_ITEM_UNSELECTED(wxID_ANY, CheckDirTreeCtrl::OnItemUnchecked)
EVT_CHECKTREE_ITEM_SELECTED(wxID_ANY, CheckDirTreeCtrl::OnItemChecked)
END_EVENT_TABLE()
CheckDirTreeCtrl::CheckDirTreeCtrl(wxWindow *parent, const wxString &rootPath, wxWindowID id)
: wxCheckTreeCtrl(parent, id)
{
BuildTree(rootPath);
}
void CheckDirTreeCtrl::BuildTree(const wxString &rootPath)
{
m_root = rootPath;
DeleteAllItems();
if(m_root.IsEmpty()){
return;
}
wxFileName filename(m_root, wxEmptyString);
wxTreeItemId item = AddRoot(filename.GetPath(), false, new DirTreeData(filename));
AddChildren(item);
Expand(item);
}
void CheckDirTreeCtrl::AddChildren(const wxTreeItemId &item)
{
DirTreeData *data = dynamic_cast<DirTreeData*>(GetItemData(item));
if(data){
wxString path = data->GetDir().GetPath();
wxDir dir(path);
if(wxDir::Exists(path)){
wxFileName filename(path, wxEmptyString);
GetChildren(item, dir, filename.GetFullPath());
}
}
}
CheckDirTreeCtrl::~CheckDirTreeCtrl()
{
}
void CheckDirTreeCtrl::OnItemUnchecked(wxCheckTreeCtrlEvent &event)
{
// RecursiveCheck(event.GetItem(), false);
Check(event.GetItem(), false);
event.Skip();
}
void CheckDirTreeCtrl::OnItemChecked(wxCheckTreeCtrlEvent &event)
{
// RecursiveCheck(event.GetItem());
Check(event.GetItem(), true);
event.Skip();
}
void CheckDirTreeCtrl::GetChildren(const wxTreeItemId &parent, const wxDir &dir, const wxString &path)
{
wxString filename;
if(!dir.IsOpened()){
return;
}
//enumerate all directories
bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS);
//add siblings
while(cont){
wxFileName dirname(path + wxT("/") + filename, wxEmptyString);
wxTreeItemId item = AppendItem(parent, filename, IsChecked(parent), new DirTreeData(dirname));
wxDir child(dirname.GetFullPath());
if(child.IsOpened() && child.HasSubDirs()){
//add dummy item under this node with the parent path
AppendItem(item, wxT("<Dummy>"), IsChecked(item), new DirTreeData(dirname));
}
cont = dir.GetNext(&filename);
}
}
void CheckDirTreeCtrl::OnItemExpading(wxTreeEvent &event)
{
wxTreeItemId item = event.GetItem();
if(item.IsOk()){
//get the item data
DirTreeData *data = dynamic_cast<DirTreeData*>(GetItemData(item));
if(data){
wxFileName filename = data->GetDir();
wxTreeItemIdValue cookie;
wxTreeItemId child = GetFirstChild(item, cookie);
if(child.IsOk()){
wxString text = GetItemText(child);
if(text == wxT("<Dummy>")){
//we have a dummy node, remove it and replace it with actual nodes
Delete(child);
//add all children of the parent dir
AddChildren(item);
Expand(item);
}
}
}
}
event.Skip();
}
void CheckDirTreeCtrl::GetUnselectedDirs(wxArrayString &arr)
{
//go over all loaded items, and return full path of the
//unselected items
std::list<wxTreeItemId> items;
GetItemChildrenRecursive(GetRootItem(), items);
std::list<wxTreeItemId>::iterator iter = items.begin();
for(; iter != items.end(); iter++){
wxTreeItemId item = *iter;
if(!IsChecked(item)){
//get the tree item data
DirTreeData *data = dynamic_cast<DirTreeData*>(GetItemData(item));
if(data){
wxString text = GetItemText(item);
//skip the dummy items insertd into the tree
if(item != wxT("<Dummy>")){
arr.Add(data->GetDir().GetPath());
}
}
}
}
}
void CheckDirTreeCtrl::GetSelectedDirs(wxArrayString &arr)
{
//go over all loaded items, and return full path of the
//selected items
std::list<wxTreeItemId> items;
GetItemChildrenRecursive(GetRootItem(), items);
std::list<wxTreeItemId>::iterator iter = items.begin();
for(; iter != items.end(); iter++){
wxTreeItemId item = *iter;
if(IsChecked(item)){
//get the tree item data
DirTreeData *data = dynamic_cast<DirTreeData*>(GetItemData(item));
if(data){
wxString text = GetItemText(item);
//skip the dummy items insertd into the tree
if(item != wxT("<Dummy>")){
arr.Add(data->GetDir().GetPath());
}
}
}
}
}