]>
Commit | Line | Data |
---|---|---|
457814b5 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: treetest.cpp | |
3 | // Purpose: wxTreeCtrl sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
3a5a2f56 | 9 | // Licence: wxWindows license |
457814b5 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
3a5a2f56 VZ |
13 | #pragma implementation |
14 | #pragma interface | |
457814b5 JS |
15 | #endif |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
3a5a2f56 | 21 | #pragma hdrstop |
457814b5 JS |
22 | #endif |
23 | ||
24 | #ifndef WX_PRECOMP | |
3a5a2f56 | 25 | #include "wx/wx.h" |
457814b5 JS |
26 | #endif |
27 | ||
3a5a2f56 VZ |
28 | #include "wx/log.h" |
29 | ||
91b8de8d | 30 | #include "wx/image.h" |
3a5a2f56 | 31 | #include "wx/imaglist.h" |
457814b5 JS |
32 | #include "wx/treectrl.h" |
33 | ||
cf724bce RR |
34 | #include "math.h" |
35 | ||
457814b5 JS |
36 | #include "treetest.h" |
37 | ||
622d090e | 38 | #ifdef wxTR_HAS_VARIABLE_ROW_HIGHT |
91b8de8d | 39 | #define USE_TR_HAS_VARIABLE_ROW_HIGHT 1 |
622d090e SB |
40 | #else |
41 | #define USE_TR_HAS_VARIABLE_ROW_HIGHT 0 | |
42 | #endif | |
91b8de8d RR |
43 | |
44 | // under Windows the icons are in the .rc file | |
45 | #ifndef __WXMSW__ | |
46 | #if !USE_TR_HAS_VARIABLE_ROW_HIGHT | |
47 | #include "icon1.xpm" | |
48 | #endif | |
49 | #include "icon2.xpm" | |
50 | #include "mondrian.xpm" | |
51 | #endif | |
52 | ||
53 | ||
e1ee62bd VZ |
54 | // verify that the item is ok and insult the user if it is not |
55 | #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \ | |
56 | wxMessageBox("Please select some item first!", \ | |
57 | "Tree sample error", \ | |
58 | wxOK | wxICON_EXCLAMATION, \ | |
59 | this); \ | |
60 | return; \ | |
5f939e78 VZ |
61 | } |
62 | ||
457814b5 | 63 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
5f939e78 VZ |
64 | EVT_MENU(TreeTest_Quit, MyFrame::OnQuit) |
65 | EVT_MENU(TreeTest_About, MyFrame::OnAbout) | |
66 | EVT_MENU(TreeTest_Dump, MyFrame::OnDump) | |
91b8de8d | 67 | EVT_MENU(TreeTest_Dump_Selected, MyFrame::OnDumpSelected) |
5f939e78 VZ |
68 | EVT_MENU(TreeTest_Rename, MyFrame::OnRename) |
69 | EVT_MENU(TreeTest_Sort, MyFrame::OnSort) | |
70 | EVT_MENU(TreeTest_SortRev, MyFrame::OnSortRev) | |
71 | EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold) | |
72 | EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold) | |
73 | EVT_MENU(TreeTest_Delete, MyFrame::OnDelete) | |
74 | EVT_MENU(TreeTest_DeleteChildren, MyFrame::OnDeleteChildren) | |
75 | EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll) | |
76 | EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate) | |
77 | EVT_MENU(TreeTest_CollapseAndReset, MyFrame::OnCollapseAndReset) | |
78 | EVT_MENU(TreeTest_EnsureVisible, MyFrame::OnEnsureVisible) | |
79 | EVT_MENU(TreeTest_AddItem, MyFrame::OnAddItem) | |
cf724bce RR |
80 | EVT_MENU(TreeTest_IncIndent, MyFrame::OnIncIndent) |
81 | EVT_MENU(TreeTest_DecIndent, MyFrame::OnDecIndent) | |
82 | EVT_MENU(TreeTest_IncSpacing, MyFrame::OnIncSpacing) | |
83 | EVT_MENU(TreeTest_DecSpacing, MyFrame::OnDecSpacing) | |
457814b5 JS |
84 | END_EVENT_TABLE() |
85 | ||
86 | BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) | |
5f939e78 VZ |
87 | EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag) |
88 | EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag) | |
89 | EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit) | |
90 | EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit) | |
91 | EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem) | |
92 | EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo) | |
93 | EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo) | |
94 | EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded) | |
95 | EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding) | |
96 | EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed) | |
97 | EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing) | |
98 | EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged) | |
99 | EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging) | |
100 | EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown) | |
101 | EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated) | |
102 | EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick) | |
457814b5 JS |
103 | END_EVENT_TABLE() |
104 | ||
105 | IMPLEMENT_APP(MyApp) | |
106 | ||
c89a6106 | 107 | bool MyApp::OnInit() |
457814b5 | 108 | { |
5f939e78 VZ |
109 | // Create the main frame window |
110 | MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340); | |
457814b5 | 111 | |
5f939e78 VZ |
112 | // Show the frame |
113 | frame->Show(TRUE); | |
114 | SetTopWindow(frame); | |
457814b5 | 115 | |
5f939e78 | 116 | return TRUE; |
3a5a2f56 | 117 | } |
457814b5 | 118 | |
457814b5 | 119 | |
3a5a2f56 VZ |
120 | // My frame constructor |
121 | MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) | |
122 | : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)) | |
123 | { | |
5f939e78 VZ |
124 | // This reduces flicker effects - even better would be to define |
125 | // OnEraseBackground to do nothing. When the tree control's scrollbars are | |
126 | // show or hidden, the frame is sent a background erase event. | |
127 | SetBackgroundColour(wxColour(255, 255, 255)); | |
128 | ||
129 | // Give it an icon | |
130 | SetIcon(wxICON(mondrian)); | |
131 | ||
132 | // Make a menubar | |
133 | wxMenu *file_menu = new wxMenu, | |
134 | *tree_menu = new wxMenu, | |
135 | *item_menu = new wxMenu; | |
136 | ||
137 | file_menu->Append(TreeTest_About, "&About..."); | |
138 | file_menu->AppendSeparator(); | |
139 | file_menu->Append(TreeTest_Quit, "E&xit"); | |
140 | ||
141 | tree_menu->Append(TreeTest_Recreate, "&Recreate the tree"); | |
142 | tree_menu->Append(TreeTest_CollapseAndReset, "C&ollapse and reset"); | |
143 | tree_menu->AppendSeparator(); | |
144 | tree_menu->Append(TreeTest_AddItem, "Append a &new item"); | |
145 | tree_menu->Append(TreeTest_Delete, "&Delete this item"); | |
146 | tree_menu->Append(TreeTest_DeleteChildren, "Delete &children"); | |
147 | tree_menu->Append(TreeTest_DeleteAll, "Delete &all items"); | |
148 | tree_menu->AppendSeparator(); | |
149 | tree_menu->Append(TreeTest_Sort, "Sort children of current item"); | |
150 | tree_menu->Append(TreeTest_SortRev, "Sort in reversed order"); | |
151 | tree_menu->AppendSeparator(); | |
152 | tree_menu->Append(TreeTest_EnsureVisible, "Make the last item &visible"); | |
cf724bce RR |
153 | tree_menu->AppendSeparator(); |
154 | tree_menu->Append(TreeTest_IncIndent, "Add 5 points to indentation\tAlt-I"); | |
155 | tree_menu->Append(TreeTest_DecIndent, "Reduce indentation by 5 points\tAlt-R"); | |
156 | tree_menu->AppendSeparator(); | |
157 | tree_menu->Append(TreeTest_IncSpacing, "Add 5 points to spacing\tCtrl-I"); | |
158 | tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R"); | |
5f939e78 | 159 | |
5f939e78 | 160 | item_menu->AppendSeparator(); |
91b8de8d | 161 | item_menu->Append(TreeTest_Dump, "&Dump item children"); |
622d090e | 162 | #ifdef wxTR_MULTIPLE |
91b8de8d | 163 | item_menu->Append(TreeTest_Dump_Selected, "Dump selected items\tAlt-S"); |
622d090e | 164 | #endif |
5f939e78 | 165 | item_menu->Append(TreeTest_Rename, "&Rename item..."); |
91b8de8d | 166 | |
5f939e78 VZ |
167 | item_menu->AppendSeparator(); |
168 | item_menu->Append(TreeTest_Bold, "Make item &bold"); | |
169 | item_menu->Append(TreeTest_UnBold, "Make item ¬ bold"); | |
170 | ||
171 | wxMenuBar *menu_bar = new wxMenuBar; | |
172 | menu_bar->Append(file_menu, "&File"); | |
173 | menu_bar->Append(tree_menu, "&Tree"); | |
174 | menu_bar->Append(item_menu, "&Item"); | |
175 | SetMenuBar(menu_bar); | |
176 | ||
177 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, | |
178 | wxDefaultPosition, wxDefaultSize, | |
91b8de8d | 179 | wxTR_HAS_BUTTONS | |
622d090e | 180 | #ifdef wxTR_MULTIPLE |
91b8de8d | 181 | wxTR_MULTIPLE | |
622d090e | 182 | #endif |
91b8de8d RR |
183 | #if USE_TR_HAS_VARIABLE_ROW_HIGHT |
184 | wxTR_HAS_VARIABLE_ROW_HIGHT | | |
185 | #endif | |
186 | wxSUNKEN_BORDER); | |
5f939e78 VZ |
187 | wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "", |
188 | wxDefaultPosition, wxDefaultSize, | |
189 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
190 | ||
191 | wxLayoutConstraints *c = new wxLayoutConstraints; | |
192 | c->top.SameAs(this, wxTop); | |
193 | c->left.SameAs(this, wxLeft); | |
194 | c->right.SameAs(this, wxRight); | |
195 | c->height.PercentOf(this, wxHeight, 66); | |
196 | m_treeCtrl->SetConstraints(c); | |
197 | ||
198 | c = new wxLayoutConstraints; | |
199 | c->top.Below(m_treeCtrl); | |
200 | c->left.SameAs(this, wxLeft); | |
201 | c->right.SameAs(this, wxRight); | |
202 | c->bottom.SameAs(this, wxBottom); | |
203 | textCtrl->SetConstraints(c); | |
204 | SetAutoLayout(TRUE); | |
205 | ||
206 | // create a status bar with 3 panes | |
207 | CreateStatusBar(3); | |
208 | SetStatusText("", 0); | |
3a5a2f56 | 209 | |
a367b9b3 | 210 | #ifdef __WXMOTIF__ |
5f939e78 VZ |
211 | // For some reason, we get a memcpy crash in wxLogStream::DoLogStream |
212 | // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc? | |
213 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
a367b9b3 | 214 | #else |
5f939e78 VZ |
215 | // set our text control as the log target |
216 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl); | |
217 | delete wxLog::SetActiveTarget(logWindow); | |
a367b9b3 | 218 | #endif |
457814b5 JS |
219 | } |
220 | ||
c89a6106 | 221 | MyFrame::~MyFrame() |
457814b5 | 222 | { |
5f939e78 | 223 | delete wxLog::SetActiveTarget(NULL); |
457814b5 JS |
224 | } |
225 | ||
3a5a2f56 | 226 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 227 | { |
5f939e78 | 228 | Close(TRUE); |
457814b5 JS |
229 | } |
230 | ||
3a5a2f56 | 231 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 232 | { |
5f939e78 VZ |
233 | wxMessageBox("Tree test sample\n" |
234 | "Julian Smart (c) 1997,\n" | |
235 | "Vadim Zeitlin (c) 1998", | |
236 | "About tree test", | |
237 | wxOK | wxICON_INFORMATION, this); | |
e1ee62bd VZ |
238 | } |
239 | ||
240 | void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event)) | |
241 | { | |
5f939e78 | 242 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 243 | |
5f939e78 | 244 | CHECK_ITEM( item ); |
e1ee62bd | 245 | |
5f939e78 VZ |
246 | static wxString s_text; |
247 | s_text = wxGetTextFromUser("New name: ", "Tree sample question", | |
248 | s_text, this); | |
249 | if ( !s_text.IsEmpty() ) | |
250 | { | |
251 | m_treeCtrl->SetItemText(item, s_text); | |
252 | } | |
e1ee62bd VZ |
253 | } |
254 | ||
255 | void MyFrame::DoSort(bool reverse) | |
256 | { | |
5f939e78 | 257 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 258 | |
5f939e78 | 259 | CHECK_ITEM( item ); |
457814b5 | 260 | |
5f939e78 | 261 | m_treeCtrl->DoSortChildren(item, reverse); |
457814b5 JS |
262 | } |
263 | ||
4832f7c0 VZ |
264 | void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) |
265 | { | |
5f939e78 | 266 | wxTreeItemId root = m_treeCtrl->GetSelection(); |
e1ee62bd | 267 | |
5f939e78 | 268 | CHECK_ITEM( root ); |
e1ee62bd | 269 | |
5f939e78 | 270 | m_treeCtrl->GetItemsRecursively(root, -1); |
4832f7c0 VZ |
271 | } |
272 | ||
91b8de8d RR |
273 | void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event)) |
274 | { | |
622d090e SB |
275 | #ifdef wxTR_MULTIPLE |
276 | wxArrayTreeItemIds array; | |
91b8de8d RR |
277 | |
278 | m_treeCtrl->GetSelections(array); | |
279 | size_t nos=array.Count(); | |
280 | wxLogMessage(wxString("items selected : ")<< (int)nos); | |
281 | ||
282 | for (size_t n=0; n<nos; ++n) | |
283 | wxLogMessage(m_treeCtrl->GetItemText(array.Item(n))); | |
622d090e | 284 | #endif |
91b8de8d RR |
285 | } |
286 | ||
add28c55 VZ |
287 | void MyFrame::DoSetBold(bool bold) |
288 | { | |
5f939e78 | 289 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 290 | |
5f939e78 | 291 | CHECK_ITEM( item ); |
e1ee62bd | 292 | |
5f939e78 | 293 | m_treeCtrl->SetItemBold(item, bold); |
add28c55 VZ |
294 | } |
295 | ||
ff5bf259 VZ |
296 | void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) |
297 | { | |
5f939e78 | 298 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 299 | |
5f939e78 | 300 | CHECK_ITEM( item ); |
e1ee62bd | 301 | |
5f939e78 | 302 | m_treeCtrl->Delete(item); |
ff5bf259 VZ |
303 | } |
304 | ||
372edb9d VZ |
305 | void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event)) |
306 | { | |
5f939e78 | 307 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 308 | |
5f939e78 | 309 | CHECK_ITEM( item ); |
e1ee62bd | 310 | |
5f939e78 | 311 | m_treeCtrl->DeleteChildren(item); |
372edb9d VZ |
312 | } |
313 | ||
ff5bf259 VZ |
314 | void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) |
315 | { | |
5f939e78 | 316 | m_treeCtrl->DeleteAllItems(); |
ff5bf259 VZ |
317 | } |
318 | ||
319 | void MyFrame::OnRecreate(wxCommandEvent& event) | |
320 | { | |
5f939e78 VZ |
321 | OnDeleteAll(event); |
322 | m_treeCtrl->AddTestItemsToTree(3, 2); | |
ff5bf259 VZ |
323 | } |
324 | ||
dd3646fd VZ |
325 | void MyFrame::OnCollapseAndReset(wxCommandEvent& event) |
326 | { | |
5f939e78 | 327 | m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem()); |
dd3646fd VZ |
328 | } |
329 | ||
f65635b5 VZ |
330 | void MyFrame::OnEnsureVisible(wxCommandEvent& event) |
331 | { | |
332 | m_treeCtrl->DoEnsureVisible(); | |
333 | } | |
334 | ||
5f939e78 VZ |
335 | void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event)) |
336 | { | |
337 | static int s_num = 0; | |
338 | ||
339 | wxString text; | |
340 | text.Printf("Item #%d", ++s_num); | |
341 | ||
342 | m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(), | |
112c5086 RR |
343 | text /*, |
344 | MyTreeCtrl::TreeCtrlIcon_File */ ); | |
5f939e78 VZ |
345 | } |
346 | ||
cf724bce RR |
347 | void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event)) |
348 | { | |
349 | unsigned int indent = m_treeCtrl->GetIndent(); | |
350 | if (indent < 100) | |
351 | m_treeCtrl->SetIndent( indent+5 ); | |
352 | } | |
353 | ||
354 | void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event)) | |
355 | { | |
356 | unsigned int indent = m_treeCtrl->GetIndent(); | |
357 | if (indent > 10) | |
358 | m_treeCtrl->SetIndent( indent-5 ); | |
359 | } | |
360 | ||
361 | void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event)) | |
362 | { | |
363 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
364 | if (indent < 100) | |
365 | m_treeCtrl->SetSpacing( indent+5 ); | |
366 | } | |
367 | ||
368 | void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event)) | |
369 | { | |
370 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
371 | if (indent > 10) | |
372 | m_treeCtrl->SetSpacing( indent-5 ); | |
373 | } | |
374 | ||
3a5a2f56 | 375 | // MyTreeCtrl implementation |
23fd5130 VZ |
376 | IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl) |
377 | ||
3a5a2f56 VZ |
378 | MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, |
379 | const wxPoint& pos, const wxSize& size, | |
380 | long style) | |
381 | : wxTreeCtrl(parent, id, pos, size, style) | |
457814b5 | 382 | { |
87c13572 | 383 | #if (USE_TR_HAS_VARIABLE_ROW_HIGHT && wxUSE_LIBJPEG) |
91b8de8d RR |
384 | wxImage::AddHandler(new wxJPEGHandler); |
385 | wxImage image; | |
386 | ||
387 | image.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG ); | |
388 | #endif | |
389 | ||
5f939e78 | 390 | m_reverseSort = FALSE; |
e1ee62bd | 391 | |
5f939e78 VZ |
392 | // Make an image list containing small icons |
393 | m_imageListNormal = new wxImageList(16, 16, TRUE); | |
457814b5 | 394 | |
5f939e78 | 395 | // should correspond to TreeCtrlIcon_xxx enum |
f60d0f94 | 396 | #if defined(__WXMSW__) && defined(__WIN16__) |
5f939e78 VZ |
397 | // This is required in 16-bit Windows mode only because we can't load a specific (16x16) |
398 | // icon image, so it comes out stretched | |
91b8de8d RR |
399 | # if USE_TR_HAS_VARIABLE_ROW_HIGHT |
400 | m_imageListNormal->Add(image.ConvertToBitmap()); | |
401 | # else | |
5f939e78 | 402 | m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE)); |
91b8de8d | 403 | # endif |
5f939e78 | 404 | m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE)); |
f60d0f94 | 405 | #else |
91b8de8d RR |
406 | # if USE_TR_HAS_VARIABLE_ROW_HIGHT |
407 | m_imageListNormal->Add(image.ConvertToBitmap()); | |
408 | # else | |
5f939e78 | 409 | m_imageListNormal->Add(wxICON(icon1)); |
91b8de8d | 410 | # endif |
5f939e78 | 411 | m_imageListNormal->Add(wxICON(icon2)); |
f60d0f94 | 412 | #endif |
457814b5 | 413 | |
5f939e78 | 414 | SetImageList(m_imageListNormal); |
457814b5 | 415 | |
5f939e78 VZ |
416 | // Add some items to the tree |
417 | AddTestItemsToTree(3, 2); | |
457814b5 JS |
418 | } |
419 | ||
3a5a2f56 | 420 | MyTreeCtrl::~MyTreeCtrl() |
457814b5 | 421 | { |
5f939e78 | 422 | delete m_imageListNormal; |
457814b5 JS |
423 | } |
424 | ||
e1ee62bd VZ |
425 | int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1, |
426 | const wxTreeItemId& item2) | |
427 | { | |
5f939e78 VZ |
428 | if ( m_reverseSort ) |
429 | { | |
430 | // just exchange 1st and 2nd items | |
431 | return wxTreeCtrl::OnCompareItems(item2, item1); | |
432 | } | |
433 | else | |
434 | { | |
435 | return wxTreeCtrl::OnCompareItems(item1, item2); | |
436 | } | |
e1ee62bd VZ |
437 | } |
438 | ||
3a5a2f56 VZ |
439 | void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent, |
440 | size_t numChildren, | |
4832f7c0 VZ |
441 | size_t depth, |
442 | size_t folder) | |
457814b5 | 443 | { |
f65635b5 | 444 | if ( depth > 0 ) |
3a5a2f56 | 445 | { |
f65635b5 VZ |
446 | wxString str; |
447 | for ( size_t n = 0; n < numChildren; n++ ) | |
448 | { | |
449 | // at depth 1 elements won't have any more children | |
450 | if (depth == 1) | |
451 | str.Printf("%s child %d.%d", "File", folder, n + 1); | |
452 | else | |
453 | str.Printf("%s child %d", "Folder", n + 1); | |
454 | ||
112c5086 RR |
455 | // int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder; |
456 | int image = depth == 1 ? -1 : TreeCtrlIcon_Folder; | |
f65635b5 VZ |
457 | wxTreeItemId id = AppendItem(idParent, str, image, image, |
458 | new MyTreeItemData(str)); | |
459 | ||
460 | // remember the last child for OnEnsureVisible() | |
461 | if ( depth == 1 && n == numChildren - 1 ) | |
462 | { | |
463 | m_lastItem = id; | |
464 | } | |
465 | ||
466 | AddItemsRecursively(id, numChildren, depth - 1, n + 1); | |
467 | } | |
3a5a2f56 | 468 | } |
f65635b5 | 469 | //else: done! |
457814b5 JS |
470 | } |
471 | ||
3a5a2f56 VZ |
472 | void MyTreeCtrl::AddTestItemsToTree(size_t numChildren, |
473 | size_t depth) | |
457814b5 | 474 | { |
5f939e78 VZ |
475 | wxTreeItemId rootId = AddRoot("Root", |
476 | TreeCtrlIcon_Folder, TreeCtrlIcon_Folder, | |
477 | new MyTreeItemData("Root item")); | |
457814b5 | 478 | |
5f939e78 | 479 | AddItemsRecursively(rootId, numChildren, depth, 0); |
4832f7c0 VZ |
480 | } |
481 | ||
482 | void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie) | |
483 | { | |
5f939e78 | 484 | wxTreeItemId id; |
4832f7c0 | 485 | |
5f939e78 VZ |
486 | if( cookie == -1 ) |
487 | id = GetFirstChild(idParent, cookie); | |
488 | else | |
489 | id = GetNextChild(idParent, cookie); | |
4832f7c0 | 490 | |
5f939e78 VZ |
491 | if(id <= 0) |
492 | return; | |
4832f7c0 | 493 | |
5f939e78 VZ |
494 | wxString text=GetItemText(id); |
495 | wxLogMessage(text); | |
4832f7c0 | 496 | |
5f939e78 VZ |
497 | if (ItemHasChildren(id)) |
498 | GetItemsRecursively(id,-1); | |
4832f7c0 | 499 | |
5f939e78 | 500 | GetItemsRecursively(idParent, cookie); |
457814b5 JS |
501 | } |
502 | ||
4832f7c0 | 503 | |
3a5a2f56 | 504 | // avoid repetition |
5f939e78 VZ |
505 | #define TREE_EVENT_HANDLER(name) \ |
506 | void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \ | |
507 | { \ | |
508 | wxLogMessage(#name); \ | |
509 | } | |
457814b5 | 510 | |
3a5a2f56 VZ |
511 | TREE_EVENT_HANDLER(OnBeginDrag) |
512 | TREE_EVENT_HANDLER(OnBeginRDrag) | |
513 | TREE_EVENT_HANDLER(OnBeginLabelEdit) | |
514 | TREE_EVENT_HANDLER(OnEndLabelEdit) | |
515 | TREE_EVENT_HANDLER(OnDeleteItem) | |
516 | TREE_EVENT_HANDLER(OnGetInfo) | |
517 | TREE_EVENT_HANDLER(OnSetInfo) | |
518 | TREE_EVENT_HANDLER(OnItemExpanded) | |
519 | TREE_EVENT_HANDLER(OnItemExpanding) | |
520 | TREE_EVENT_HANDLER(OnItemCollapsed) | |
521 | TREE_EVENT_HANDLER(OnSelChanged) | |
522 | TREE_EVENT_HANDLER(OnSelChanging) | |
523 | ||
524 | #undef TREE_EVENT_HANDLER | |
525 | ||
526 | void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event) | |
457814b5 | 527 | { |
5f939e78 | 528 | wxLogMessage("OnItemCollapsing"); |
f135ff73 | 529 | |
5f939e78 VZ |
530 | // for testing, prevent the user from collapsing the first child folder |
531 | wxTreeItemId itemId = event.GetItem(); | |
532 | if ( GetParent(itemId) == GetRootItem() && !GetPrevSibling(itemId) ) | |
533 | { | |
534 | wxMessageBox("You can't collapse this item."); | |
457814b5 | 535 | |
5f939e78 VZ |
536 | event.Veto(); |
537 | } | |
457814b5 JS |
538 | } |
539 | ||
6daa0637 | 540 | void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent&WXUNUSED(event)) |
435fe83e | 541 | { |
5f939e78 | 542 | wxLogMessage("OnTreeKeyDown"); |
435fe83e RR |
543 | } |
544 | ||
545 | void MyTreeCtrl::OnItemActivated(wxTreeEvent&WXUNUSED(event)) | |
457814b5 | 546 | { |
5f939e78 VZ |
547 | // show some info about this item |
548 | wxTreeItemId itemId = GetSelection(); | |
549 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId); | |
457814b5 | 550 | |
5f939e78 VZ |
551 | if ( item != NULL ) |
552 | { | |
553 | item->ShowInfo(this); | |
554 | } | |
457814b5 | 555 | |
5f939e78 VZ |
556 | wxLogMessage("OnItemActivated"); |
557 | } | |
558 | ||
559 | void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event) | |
560 | { | |
561 | wxTreeItemId id = HitTest(event.GetPosition()); | |
562 | if ( !id ) | |
563 | wxLogMessage("No item under mouse"); | |
564 | else | |
565 | { | |
566 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(id); | |
567 | if ( item ) | |
568 | wxLogMessage("Item '%s' under mouse", item->GetDesc()); | |
569 | } | |
457814b5 JS |
570 | } |
571 | ||
3a5a2f56 | 572 | static inline const char *Bool2String(bool b) |
457814b5 | 573 | { |
5f939e78 | 574 | return b ? "" : "not "; |
457814b5 JS |
575 | } |
576 | ||
9769e589 | 577 | void MyTreeItemData::ShowInfo(wxTreeCtrl *tree) |
457814b5 | 578 | { |
5f939e78 VZ |
579 | wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n" |
580 | "%u children (%u immediately under this item).", | |
581 | m_desc.c_str(), | |
582 | Bool2String(tree->IsSelected(GetId())), | |
583 | Bool2String(tree->IsExpanded(GetId())), | |
584 | Bool2String(tree->IsBold(GetId())), | |
585 | tree->GetChildrenCount(GetId()), | |
586 | tree->GetChildrenCount(GetId(), FALSE)); | |
457814b5 | 587 | } |