/
TreeView.cpp
369 lines (301 loc) · 8.47 KB
/
TreeView.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "TreeView.h"
#include <wx/popupwin.h>
#include <wx/sizer.h>
#include <wx/textctrl.h>
#include <algorithm>
namespace wxutil
{
TreeView::TreeView(wxWindow* parent, TreeModel::Ptr model, long style) :
wxDataViewCtrl(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style),
_searchPopup(NULL)
{
EnableAutoColumnWidthFix();
if (model)
{
AssociateModel(model.get());
}
Connect(wxEVT_CHAR, wxKeyEventHandler(TreeView::_onChar), NULL, this);
Connect(EV_TREEVIEW_SEARCH_EVENT, SearchEventHandler(TreeView::_onSearch), NULL, this);
Connect(wxEVT_DATAVIEW_ITEM_ACTIVATED, wxDataViewEventHandler(TreeView::_onItemActivated), NULL, this);
}
TreeView* TreeView::Create(wxWindow* parent, long style)
{
return new TreeView(parent, TreeModel::Ptr(), style);
}
// Construct a TreeView using the given TreeModel, which will be associated
// with this view (refcount is automatically decreased by one).
TreeView* TreeView::CreateWithModel(wxWindow* parent, TreeModel::Ptr model, long style)
{
return new TreeView(parent, model, style);
}
TreeView::~TreeView()
{}
bool TreeView::AssociateModel(wxDataViewModel* model)
{
// We're changing models, so unselect everything first,
// even if it's the same model again, the tree might have changed.
UnselectAll();
// Pass the call to the regular routine
return wxDataViewCtrl::AssociateModel(model);
}
// Enable the automatic recalculation of column widths
void TreeView::EnableAutoColumnWidthFix(bool enable)
{
if (enable)
{
Connect(wxEVT_DATAVIEW_ITEM_EXPANDED, wxDataViewEventHandler(TreeView::_onItemExpanded), NULL, this);
}
else
{
Disconnect(wxEVT_DATAVIEW_ITEM_EXPANDED, wxDataViewEventHandler(TreeView::_onItemExpanded), NULL, this);
}
}
void TreeView::TriggerColumnSizeEvent(const wxDataViewItem& item)
{
if (GetModel() == NULL) return;
// Trigger a column size event on the first row
wxDataViewItemArray children;
GetModel()->GetChildren(item, children);
std::for_each(children.begin(), children.end(), [&](wxDataViewItem& item)
{
GetModel()->ItemChanged(item);
});
}
void TreeView::ExpandTopLevelItems()
{
TreeModel* model = dynamic_cast<TreeModel*>(GetModel());
if (model == nullptr) return;
// Expand the first layer
wxDataViewItemArray children;
model->GetChildren(model->GetRoot(), children);
std::for_each(children.begin(), children.end(), [&](const wxDataViewItem& item)
{
Expand(item);
});
}
void TreeView::ResetSortingOnAllColumns()
{
#if wxCHECK_VERSION(3, 1, 0)
ResetAllSortColumns();
#else
// We don't have ResetAllSortColumns in wxWidgets 3.0.x
wxDataViewColumn* col = GetSortingColumn();
if (col != nullptr)
{
col->UnsetAsSortKey();
}
#endif
}
void TreeView::AddSearchColumn(const TreeModel::Column& column)
{
// Only text columns are supported right now
assert(column.type == TreeModel::Column::String || column.type == TreeModel::Column::IconText);
_colsToSearch.push_back(column);
}
bool TreeView::HasActiveSearchPopup()
{
return _searchPopup != NULL;
}
#if !defined(__linux__)
void TreeView::Rebuild()
{
TreeModel* model = dynamic_cast<TreeModel*>(GetModel());
if (model == nullptr) return;
// Trigger a rebuild of the tree
wxDataViewItemArray children;
wxDataViewItem root = model->GetRoot();
model->GetChildren(root, children);
// By calling deleted and added, the internal dataview's nodes
// are invalidated which effectively is a rebuild of everything.
for (auto child : children)
{
model->ItemDeleted(root, child);
model->ItemAdded(root, child);
}
}
#endif
void TreeView::_onItemExpanded(wxDataViewEvent& ev)
{
// This should force a recalculation of the column width
if (GetModel() != NULL)
{
GetModel()->ItemChanged(ev.GetItem());
}
ev.Skip();
}
void TreeView::_onItemActivated(wxDataViewEvent& ev)
{
if (!IsExpanded(ev.GetItem()))
{
Expand(ev.GetItem());
}
else
{
Collapse(ev.GetItem());
}
}
wxDEFINE_EVENT(EV_TREEVIEW_SEARCH_EVENT, TreeView::SearchEvent);
TreeView::SearchEvent::SearchEvent(int id) :
wxEvent(id, EV_TREEVIEW_SEARCH_EVENT)
{}
TreeView::SearchEvent::SearchEvent(const wxString& searchString, int id) :
wxEvent(id, EV_TREEVIEW_SEARCH_EVENT),
_searchString(searchString)
{}
// You *must* copy here the data to be transported
TreeView::SearchEvent::SearchEvent(const TreeView::SearchEvent& ev) :
wxEvent(ev),
_searchString(ev._searchString)
{}
// Required for sending with wxPostEvent()
wxEvent* TreeView::SearchEvent::Clone() const
{
return new SearchEvent(*this);
}
const wxString& TreeView::SearchEvent::GetSearchString() const
{
return _searchString;
}
// The custom popup window containing our search box
class TreeView::SearchPopupWindow :
public wxPopupTransientWindow
{
private:
TreeView* _owner;
wxTextCtrl* _entry;
public:
SearchPopupWindow(TreeView* owner) :
wxPopupTransientWindow(owner),
_owner(owner),
_entry(NULL)
{
SetSizer(new wxBoxSizer(wxVERTICAL));
_entry = new wxTextCtrl(this, wxID_ANY);
GetSizer()->Add(_entry, 1, wxEXPAND | wxALL, 6);
Layout();
Fit();
// Position this control in the bottom right corner
wxPoint popupPos = owner->GetScreenPosition() + owner->GetSize() - GetSize();
Position(popupPos, wxSize(0, 0));
Connect(wxEVT_CHAR, wxKeyEventHandler(SearchPopupWindow::OnChar), NULL, this);
}
void OnChar(wxKeyEvent& ev)
{
HandleKey(ev);
}
virtual void OnDismiss()
{
// Send an event to the parent TreeView
SearchEvent searchEvent("", SearchEvent::POPUP_DISMISSED);
_owner->HandleWindowEvent(searchEvent);
wxPopupTransientWindow::OnDismiss();
}
void HandleKey(wxKeyEvent& ev)
{
// Adapted this from the wxWidgets docs
wxChar uc = ev.GetUnicodeKey();
if (uc != WXK_NONE)
{
// It's a "normal" character. Notice that this includes
// control characters in 1..31 range, e.g. WXK_RETURN or
// WXK_BACK, so check for them explicitly.
if (uc >= 32)
{
_entry->SetValue(_entry->GetValue() + ev.GetUnicodeKey());
// Send an event to the parent TreeView
SearchEvent searchEvent(_entry->GetValue(), SearchEvent::SEARCH);
_owner->HandleWindowEvent(searchEvent);
}
else if (ev.GetKeyCode() == WXK_ESCAPE)
{
DismissAndNotify();
}
else if (ev.GetKeyCode() == WXK_BACK)
{
_entry->SetValue(_entry->GetValue().RemoveLast(1));
// Send an event to the parent TreeView
SearchEvent searchEvent(_entry->GetValue(), SearchEvent::SEARCH);
_owner->HandleWindowEvent(searchEvent);
}
}
else // No Unicode equivalent.
{
// Cursor events are special
if (ev.GetKeyCode() == WXK_UP || ev.GetKeyCode() == WXK_DOWN)
{
SearchEvent searchEvent(_entry->GetValue(),
ev.GetKeyCode() == WXK_UP ? SearchEvent::SEARCH_PREV_MATCH : SearchEvent::SEARCH_NEXT_MATCH);
_owner->HandleWindowEvent(searchEvent);
}
}
}
};
void TreeView::_onChar(wxKeyEvent& ev)
{
if (GetModel() == NULL || _colsToSearch.empty())
{
ev.Skip();
return;
}
// Adapted this from the wxWidgets docs
wxChar uc = ev.GetUnicodeKey();
if (uc != WXK_NONE && uc >= 32)
{
// It's a "normal" character, start the search
if (_searchPopup == NULL)
{
_searchPopup = new SearchPopupWindow(this);
_searchPopup->Popup();
_curSearchMatch = wxDataViewItem();
}
// Handle the first key immediately
_searchPopup->HandleKey(ev);
}
// Don't eat the event
ev.Skip();
}
void TreeView::_onSearch(SearchEvent& ev)
{
if (GetModel() == NULL)
{
ev.Skip(); // no model attached
return;
}
TreeModel* model = dynamic_cast<TreeModel*>(GetModel());
if (model == NULL)
{
ev.Skip(); // not a TreeModel
return;
}
wxDataViewItem oldMatch = _curSearchMatch;
// Handle the search
switch (ev.GetId())
{
case SearchEvent::SEARCH:
_curSearchMatch = model->FindNextString(ev.GetSearchString(), _colsToSearch);
break;
case SearchEvent::SEARCH_NEXT_MATCH:
_curSearchMatch = model->FindNextString(ev.GetSearchString(), _colsToSearch, _curSearchMatch);
break;
case SearchEvent::SEARCH_PREV_MATCH:
_curSearchMatch = model->FindPrevString(ev.GetSearchString(), _colsToSearch, _curSearchMatch);
break;
case SearchEvent::POPUP_DISMISSED:
_searchPopup = NULL;
_curSearchMatch = wxDataViewItem();
break;
};
if (oldMatch != _curSearchMatch && _curSearchMatch.IsOk())
{
UnselectAll();
Select(_curSearchMatch);
EnsureVisible(_curSearchMatch);
// Synthesise a selection changed signal
wxDataViewEvent le(wxEVT_DATAVIEW_SELECTION_CHANGED, GetId());
le.SetEventObject(this);
le.SetModel(GetModel());
le.SetItem(_curSearchMatch);
ProcessWindowEvent(le);
}
}
} // namespace