]>
Commit | Line | Data |
---|---|---|
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 | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation | |
14 | #pragma interface | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/wx.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/log.h" | |
29 | ||
30 | #include "wx/image.h" | |
31 | #include "wx/imaglist.h" | |
32 | #include "wx/treectrl.h" | |
33 | ||
34 | #include "math.h" | |
35 | ||
36 | #ifdef __WIN32__ | |
37 | // this is not supported by native control | |
38 | #define NO_VARIABLE_HEIGHT | |
39 | #endif | |
40 | ||
41 | #include "treetest.h" | |
42 | ||
43 | // under Windows the icons are in the .rc file | |
44 | #ifndef __WXMSW__ | |
45 | #include "icon1.xpm" | |
46 | #include "icon2.xpm" | |
47 | #include "icon3.xpm" | |
48 | #include "icon4.xpm" | |
49 | #include "icon5.xpm" | |
50 | #include "mondrian.xpm" | |
51 | #endif | |
52 | ||
53 | // verify that the item is ok and insult the user if it is not | |
54 | #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \ | |
55 | wxMessageBox("Please select some item first!", \ | |
56 | "Tree sample error", \ | |
57 | wxOK | wxICON_EXCLAMATION, \ | |
58 | this); \ | |
59 | return; \ | |
60 | } | |
61 | ||
62 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
63 | EVT_SIZE(MyFrame::OnSize) | |
64 | ||
65 | EVT_MENU(TreeTest_Quit, MyFrame::OnQuit) | |
66 | EVT_MENU(TreeTest_About, MyFrame::OnAbout) | |
67 | EVT_MENU(TreeTest_Dump, MyFrame::OnDump) | |
68 | #ifndef NO_MULTIPLE_SELECTION | |
69 | EVT_MENU(TreeTest_DumpSelected, MyFrame::OnDumpSelected) | |
70 | EVT_MENU(TreeTest_Select, MyFrame::OnSelect) | |
71 | EVT_MENU(TreeTest_Unselect, MyFrame::OnUnselect) | |
72 | EVT_MENU(TreeTest_ToggleSel, MyFrame::OnToggleSel) | |
73 | #endif // NO_MULTIPLE_SELECTION | |
74 | EVT_MENU(TreeTest_Rename, MyFrame::OnRename) | |
75 | EVT_MENU(TreeTest_Count, MyFrame::OnCount) | |
76 | EVT_MENU(TreeTest_CountRec, MyFrame::OnCountRec) | |
77 | EVT_MENU(TreeTest_Sort, MyFrame::OnSort) | |
78 | EVT_MENU(TreeTest_SortRev, MyFrame::OnSortRev) | |
79 | EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold) | |
80 | EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold) | |
81 | EVT_MENU(TreeTest_Delete, MyFrame::OnDelete) | |
82 | EVT_MENU(TreeTest_DeleteChildren, MyFrame::OnDeleteChildren) | |
83 | EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll) | |
84 | EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate) | |
85 | EVT_MENU(TreeTest_ToggleImages, MyFrame::OnToggleImages) | |
86 | EVT_MENU(TreeTest_SetImageSize, MyFrame::OnSetImageSize) | |
87 | EVT_MENU(TreeTest_CollapseAndReset, MyFrame::OnCollapseAndReset) | |
88 | EVT_MENU(TreeTest_EnsureVisible, MyFrame::OnEnsureVisible) | |
89 | EVT_MENU(TreeTest_AddItem, MyFrame::OnAddItem) | |
90 | EVT_MENU(TreeTest_InsertItem, MyFrame::OnInsertItem) | |
91 | EVT_MENU(TreeTest_IncIndent, MyFrame::OnIncIndent) | |
92 | EVT_MENU(TreeTest_DecIndent, MyFrame::OnDecIndent) | |
93 | EVT_MENU(TreeTest_IncSpacing, MyFrame::OnIncSpacing) | |
94 | EVT_MENU(TreeTest_DecSpacing, MyFrame::OnDecSpacing) | |
95 | EVT_MENU(TreeTest_ToggleIcon, MyFrame::OnToggleIcon) | |
96 | END_EVENT_TABLE() | |
97 | ||
98 | BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) | |
99 | EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag) | |
100 | EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag) | |
101 | EVT_TREE_END_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnEndDrag) | |
102 | EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit) | |
103 | EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit) | |
104 | EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem) | |
105 | #if 0 // there are so many of those that logging them causes flicker | |
106 | EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo) | |
107 | #endif | |
108 | EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo) | |
109 | EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded) | |
110 | EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding) | |
111 | EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed) | |
112 | EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing) | |
113 | EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged) | |
114 | EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging) | |
115 | EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown) | |
116 | EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated) | |
117 | EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick) | |
118 | END_EVENT_TABLE() | |
119 | ||
120 | IMPLEMENT_APP(MyApp) | |
121 | ||
122 | bool MyApp::OnInit() | |
123 | { | |
124 | // Create the main frame window | |
125 | MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 600); | |
126 | ||
127 | // Show the frame | |
128 | frame->Show(TRUE); | |
129 | SetTopWindow(frame); | |
130 | ||
131 | return TRUE; | |
132 | } | |
133 | ||
134 | ||
135 | // My frame constructor | |
136 | MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) | |
137 | : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)), | |
138 | m_treeCtrl(NULL), m_textCtrl(NULL) | |
139 | { | |
140 | // This reduces flicker effects - even better would be to define | |
141 | // OnEraseBackground to do nothing. When the tree control's scrollbars are | |
142 | // show or hidden, the frame is sent a background erase event. | |
143 | SetBackgroundColour(wxColour(255, 255, 255)); | |
144 | ||
145 | // Give it an icon | |
146 | SetIcon(wxICON(mondrian)); | |
147 | ||
148 | // Make a menubar | |
149 | wxMenu *file_menu = new wxMenu, | |
150 | *tree_menu = new wxMenu, | |
151 | *item_menu = new wxMenu; | |
152 | ||
153 | file_menu->Append(TreeTest_About, "&About..."); | |
154 | file_menu->AppendSeparator(); | |
155 | file_menu->Append(TreeTest_Quit, "E&xit\tAlt-X"); | |
156 | ||
157 | #ifndef NO_MULTIPLE_SELECTION | |
158 | tree_menu->Append(TreeTest_ToggleSel, "&Toggle selection mode"); | |
159 | #endif // NO_MULTIPLE_SELECTION | |
160 | tree_menu->Append(TreeTest_ToggleImages, "&Show images", "", TRUE); | |
161 | tree_menu->Append(TreeTest_SetImageSize, "Set image si&ze..."); | |
162 | tree_menu->Append(TreeTest_Recreate, "&Recreate the tree"); | |
163 | tree_menu->Append(TreeTest_CollapseAndReset, "C&ollapse and reset"); | |
164 | tree_menu->AppendSeparator(); | |
165 | tree_menu->Append(TreeTest_AddItem, "Append a &new item"); | |
166 | tree_menu->Append(TreeTest_InsertItem, "&Insert a new item"); | |
167 | tree_menu->Append(TreeTest_Delete, "&Delete this item"); | |
168 | tree_menu->Append(TreeTest_DeleteChildren, "Delete &children"); | |
169 | tree_menu->Append(TreeTest_DeleteAll, "Delete &all items"); | |
170 | tree_menu->AppendSeparator(); | |
171 | tree_menu->Append(TreeTest_Count, "Count children of current item"); | |
172 | tree_menu->Append(TreeTest_CountRec, "Recursively count children of current item"); | |
173 | tree_menu->AppendSeparator(); | |
174 | tree_menu->Append(TreeTest_Sort, "Sort children of current item"); | |
175 | tree_menu->Append(TreeTest_SortRev, "Sort in reversed order"); | |
176 | tree_menu->AppendSeparator(); | |
177 | tree_menu->Append(TreeTest_EnsureVisible, "Make the last item &visible"); | |
178 | tree_menu->AppendSeparator(); | |
179 | tree_menu->Append(TreeTest_IncIndent, "Add 5 points to indentation\tAlt-I"); | |
180 | tree_menu->Append(TreeTest_DecIndent, "Reduce indentation by 5 points\tAlt-R"); | |
181 | tree_menu->AppendSeparator(); | |
182 | tree_menu->Append(TreeTest_IncSpacing, "Add 5 points to spacing\tCtrl-I"); | |
183 | tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R"); | |
184 | ||
185 | item_menu->Append(TreeTest_Dump, "&Dump item children"); | |
186 | item_menu->Append(TreeTest_Rename, "&Rename item..."); | |
187 | ||
188 | item_menu->AppendSeparator(); | |
189 | item_menu->Append(TreeTest_Bold, "Make item &bold"); | |
190 | item_menu->Append(TreeTest_UnBold, "Make item ¬ bold"); | |
191 | item_menu->AppendSeparator(); | |
192 | item_menu->Append(TreeTest_ToggleIcon, "Toggle the items &icon"); | |
193 | ||
194 | #ifndef NO_MULTIPLE_SELECTION | |
195 | item_menu->AppendSeparator(); | |
196 | item_menu->Append(TreeTest_DumpSelected, "Dump selected items\tAlt-D"); | |
197 | item_menu->Append(TreeTest_Select, "Select current item\tAlt-S"); | |
198 | item_menu->Append(TreeTest_Unselect, "Unselect everything\tAlt-U"); | |
199 | #endif // NO_MULTIPLE_SELECTION | |
200 | ||
201 | wxMenuBar *menu_bar = new wxMenuBar; | |
202 | menu_bar->Append(file_menu, "&File"); | |
203 | menu_bar->Append(tree_menu, "&Tree"); | |
204 | menu_bar->Append(item_menu, "&Item"); | |
205 | SetMenuBar(menu_bar); | |
206 | ||
207 | menu_bar->Check(TreeTest_ToggleImages, TRUE); | |
208 | ||
209 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, | |
210 | wxDefaultPosition, wxDefaultSize, | |
211 | wxTR_HAS_BUTTONS | | |
212 | wxTR_EDIT_LABELS | | |
213 | #ifndef NO_VARIABLE_HEIGHT | |
214 | wxTR_HAS_VARIABLE_ROW_HEIGHT | | |
215 | #endif | |
216 | wxSUNKEN_BORDER); | |
217 | ||
218 | m_treeCtrl->SetBackgroundColour(wxColour(204, 205, 79)); | |
219 | ||
220 | m_textCtrl = new wxTextCtrl(this, -1, "", | |
221 | wxDefaultPosition, wxDefaultSize, | |
222 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
223 | ||
224 | // create a status bar with 3 panes | |
225 | CreateStatusBar(3); | |
226 | SetStatusText("", 0); | |
227 | ||
228 | #ifdef __WXMOTIF__ | |
229 | // For some reason, we get a memcpy crash in wxLogStream::DoLogStream | |
230 | // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc? | |
231 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
232 | #else | |
233 | // set our text control as the log target | |
234 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(m_textCtrl); | |
235 | delete wxLog::SetActiveTarget(logWindow); | |
236 | #endif | |
237 | } | |
238 | ||
239 | MyFrame::~MyFrame() | |
240 | { | |
241 | delete wxLog::SetActiveTarget(NULL); | |
242 | } | |
243 | ||
244 | void MyFrame::OnSize(wxSizeEvent& event) | |
245 | { | |
246 | if ( m_treeCtrl && m_textCtrl ) | |
247 | { | |
248 | Resize(GetClientSize()); | |
249 | } | |
250 | ||
251 | event.Skip(); | |
252 | } | |
253 | ||
254 | void MyFrame::Resize(const wxSize& size) | |
255 | { | |
256 | m_treeCtrl->SetSize(0, 0, size.x, 2*size.y/3); | |
257 | m_textCtrl->SetSize(0, 2*size.y/3, size.x, size.y/3); | |
258 | } | |
259 | ||
260 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
261 | { | |
262 | Close(TRUE); | |
263 | } | |
264 | ||
265 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
266 | { | |
267 | wxMessageBox("Tree test sample\n" | |
268 | "(c) Julian Smart 1997, Vadim Zeitlin 1998", | |
269 | "About tree test", | |
270 | wxOK | wxICON_INFORMATION, this); | |
271 | } | |
272 | ||
273 | void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event)) | |
274 | { | |
275 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
276 | ||
277 | CHECK_ITEM( item ); | |
278 | ||
279 | // old code - now we edit in place | |
280 | #if 0 | |
281 | static wxString s_text; | |
282 | s_text = wxGetTextFromUser("New name: ", "Tree sample question", | |
283 | s_text, this); | |
284 | if ( !s_text.IsEmpty() ) | |
285 | { | |
286 | m_treeCtrl->SetItemText(item, s_text); | |
287 | } | |
288 | #endif // 0 | |
289 | ||
290 | // TODO demonstrate creating a custom edit control... | |
291 | (void)m_treeCtrl->EditLabel(item); | |
292 | } | |
293 | ||
294 | void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event)) | |
295 | { | |
296 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
297 | ||
298 | CHECK_ITEM( item ); | |
299 | ||
300 | int i = m_treeCtrl->GetChildrenCount( item, FALSE ); | |
301 | ||
302 | wxLogMessage(wxT("%d children"), i); | |
303 | } | |
304 | ||
305 | void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event)) | |
306 | { | |
307 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
308 | ||
309 | CHECK_ITEM( item ); | |
310 | ||
311 | int i = m_treeCtrl->GetChildrenCount( item ); | |
312 | ||
313 | wxLogMessage(wxT("%d children"), i); | |
314 | } | |
315 | ||
316 | void MyFrame::DoSort(bool reverse) | |
317 | { | |
318 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
319 | ||
320 | CHECK_ITEM( item ); | |
321 | ||
322 | m_treeCtrl->DoSortChildren(item, reverse); | |
323 | } | |
324 | ||
325 | void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) | |
326 | { | |
327 | wxTreeItemId root = m_treeCtrl->GetSelection(); | |
328 | ||
329 | CHECK_ITEM( root ); | |
330 | ||
331 | m_treeCtrl->GetItemsRecursively(root, -1); | |
332 | } | |
333 | ||
334 | #ifndef NO_MULTIPLE_SELECTION | |
335 | ||
336 | void MyFrame::OnToggleSel(wxCommandEvent& WXUNUSED(event)) | |
337 | { | |
338 | long style = m_treeCtrl->GetWindowStyle(); | |
339 | if ( style & wxTR_MULTIPLE ) | |
340 | style &= ~wxTR_MULTIPLE; | |
341 | else | |
342 | style |= wxTR_MULTIPLE; | |
343 | ||
344 | delete m_treeCtrl; | |
345 | ||
346 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, | |
347 | wxDefaultPosition, wxDefaultSize, | |
348 | style); | |
349 | Resize(GetClientSize()); | |
350 | } | |
351 | ||
352 | void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event)) | |
353 | { | |
354 | wxArrayTreeItemIds array; | |
355 | ||
356 | size_t count = m_treeCtrl->GetSelections(array); | |
357 | wxLogMessage(wxT("%u items selected"), count); | |
358 | ||
359 | for ( size_t n = 0; n < count; n++ ) | |
360 | { | |
361 | wxLogMessage("\t%s", m_treeCtrl->GetItemText(array.Item(n)).c_str()); | |
362 | } | |
363 | } | |
364 | ||
365 | void MyFrame::OnSelect(wxCommandEvent& event) | |
366 | { | |
367 | m_treeCtrl->SelectItem(m_treeCtrl->GetSelection()); | |
368 | } | |
369 | ||
370 | void MyFrame::OnUnselect(wxCommandEvent& event) | |
371 | { | |
372 | m_treeCtrl->UnselectAll(); | |
373 | } | |
374 | ||
375 | #endif // NO_MULTIPLE_SELECTION | |
376 | ||
377 | void MyFrame::DoSetBold(bool bold) | |
378 | { | |
379 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
380 | ||
381 | CHECK_ITEM( item ); | |
382 | ||
383 | m_treeCtrl->SetItemBold(item, bold); | |
384 | } | |
385 | ||
386 | void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) | |
387 | { | |
388 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
389 | ||
390 | CHECK_ITEM( item ); | |
391 | ||
392 | m_treeCtrl->Delete(item); | |
393 | } | |
394 | ||
395 | void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event)) | |
396 | { | |
397 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
398 | ||
399 | CHECK_ITEM( item ); | |
400 | ||
401 | m_treeCtrl->DeleteChildren(item); | |
402 | } | |
403 | ||
404 | void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) | |
405 | { | |
406 | m_treeCtrl->DeleteAllItems(); | |
407 | } | |
408 | ||
409 | void MyFrame::OnRecreate(wxCommandEvent& event) | |
410 | { | |
411 | OnDeleteAll(event); | |
412 | m_treeCtrl->AddTestItemsToTree(3, 2); | |
413 | } | |
414 | ||
415 | void MyFrame::OnSetImageSize(wxCommandEvent& event) | |
416 | { | |
417 | long size = wxGetNumberFromUser("Enter the size for the images to use", | |
418 | "Size: ", | |
419 | "TreeCtrl sample", | |
420 | 32); | |
421 | if ( size == -1 ) | |
422 | return; | |
423 | ||
424 | m_treeCtrl->CreateImageList((int)size); | |
425 | ||
426 | OnRecreate(event); | |
427 | } | |
428 | ||
429 | void MyFrame::OnToggleImages(wxCommandEvent& event) | |
430 | { | |
431 | wxGetApp().SetShowImages(!wxGetApp().ShowImages()); | |
432 | ||
433 | OnRecreate(event); | |
434 | } | |
435 | ||
436 | void MyFrame::OnCollapseAndReset(wxCommandEvent& event) | |
437 | { | |
438 | m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem()); | |
439 | } | |
440 | ||
441 | void MyFrame::OnEnsureVisible(wxCommandEvent& event) | |
442 | { | |
443 | m_treeCtrl->DoEnsureVisible(); | |
444 | } | |
445 | ||
446 | void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event)) | |
447 | { | |
448 | int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File : -1; | |
449 | m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), image, "2nd item"); | |
450 | } | |
451 | ||
452 | void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event)) | |
453 | { | |
454 | static int s_num = 0; | |
455 | ||
456 | wxString text; | |
457 | text.Printf("Item #%d", ++s_num); | |
458 | ||
459 | m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(), | |
460 | text /*, | |
461 | MyTreeCtrl::TreeCtrlIcon_File */ ); | |
462 | } | |
463 | ||
464 | void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event)) | |
465 | { | |
466 | unsigned int indent = m_treeCtrl->GetIndent(); | |
467 | if (indent < 100) | |
468 | m_treeCtrl->SetIndent( indent+5 ); | |
469 | } | |
470 | ||
471 | void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event)) | |
472 | { | |
473 | unsigned int indent = m_treeCtrl->GetIndent(); | |
474 | if (indent > 10) | |
475 | m_treeCtrl->SetIndent( indent-5 ); | |
476 | } | |
477 | ||
478 | void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event)) | |
479 | { | |
480 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
481 | if (indent < 100) | |
482 | m_treeCtrl->SetSpacing( indent+5 ); | |
483 | } | |
484 | ||
485 | void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event)) | |
486 | { | |
487 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
488 | if (indent > 10) | |
489 | m_treeCtrl->SetSpacing( indent-5 ); | |
490 | } | |
491 | ||
492 | void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event)) | |
493 | { | |
494 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
495 | ||
496 | CHECK_ITEM( item ); | |
497 | ||
498 | m_treeCtrl->DoToggleIcon(item); | |
499 | } | |
500 | ||
501 | // MyTreeCtrl implementation | |
502 | IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl) | |
503 | ||
504 | MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, | |
505 | const wxPoint& pos, const wxSize& size, | |
506 | long style) | |
507 | : wxTreeCtrl(parent, id, pos, size, style) | |
508 | { | |
509 | m_reverseSort = FALSE; | |
510 | ||
511 | CreateImageList(); | |
512 | ||
513 | // Add some items to the tree | |
514 | AddTestItemsToTree(3, 2); | |
515 | } | |
516 | ||
517 | void MyTreeCtrl::CreateImageList(int size) | |
518 | { | |
519 | delete m_imageListNormal; | |
520 | ||
521 | if ( size == -1 ) | |
522 | { | |
523 | m_imageListNormal = NULL; | |
524 | ||
525 | return; | |
526 | } | |
527 | ||
528 | // Make an image list containing small icons | |
529 | m_imageListNormal = new wxImageList(size, size, TRUE); | |
530 | ||
531 | // should correspond to TreeCtrlIcon_xxx enum | |
532 | #if defined(__WXMSW__) && defined(__WIN16__) | |
533 | m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE)); | |
534 | m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE)); | |
535 | m_imageListNormal->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE)); | |
536 | m_imageListNormal->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE)); | |
537 | m_imageListNormal->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE)); | |
538 | #else | |
539 | wxIcon icons[5]; | |
540 | icons[0] = wxICON(icon1); | |
541 | icons[1] = wxICON(icon2); | |
542 | icons[2] = wxICON(icon3); | |
543 | icons[3] = wxICON(icon4); | |
544 | icons[4] = wxICON(icon5); | |
545 | ||
546 | int sizeOrig = icons[0].GetWidth(); | |
547 | for ( size_t i = 0; i < WXSIZEOF(icons); i++ ) | |
548 | { | |
549 | if ( size == sizeOrig ) | |
550 | { | |
551 | m_imageListNormal->Add(icons[i]); | |
552 | } | |
553 | else | |
554 | { | |
555 | m_imageListNormal->Add(wxImage(icons[i]).Rescale(size, size). | |
556 | ConvertToBitmap()); | |
557 | } | |
558 | } | |
559 | #endif | |
560 | ||
561 | SetImageList(m_imageListNormal); | |
562 | } | |
563 | ||
564 | MyTreeCtrl::~MyTreeCtrl() | |
565 | { | |
566 | delete m_imageListNormal; | |
567 | } | |
568 | ||
569 | int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1, | |
570 | const wxTreeItemId& item2) | |
571 | { | |
572 | if ( m_reverseSort ) | |
573 | { | |
574 | // just exchange 1st and 2nd items | |
575 | return wxTreeCtrl::OnCompareItems(item2, item1); | |
576 | } | |
577 | else | |
578 | { | |
579 | return wxTreeCtrl::OnCompareItems(item1, item2); | |
580 | } | |
581 | } | |
582 | ||
583 | void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent, | |
584 | size_t numChildren, | |
585 | size_t depth, | |
586 | size_t folder) | |
587 | { | |
588 | if ( depth > 0 ) | |
589 | { | |
590 | bool hasChildren = depth > 1; | |
591 | ||
592 | wxString str; | |
593 | for ( size_t n = 0; n < numChildren; n++ ) | |
594 | { | |
595 | // at depth 1 elements won't have any more children | |
596 | if ( hasChildren ) | |
597 | str.Printf("%s child %d", "Folder", n + 1); | |
598 | else | |
599 | str.Printf("%s child %d.%d", "File", folder, n + 1); | |
600 | ||
601 | // here we pass to AppendItem() normal and selected item images (we | |
602 | // suppose that selected image follows the normal one in the enum) | |
603 | int image, imageSel; | |
604 | if ( wxGetApp().ShowImages() ) | |
605 | { | |
606 | image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder; | |
607 | imageSel = image + 1; | |
608 | } | |
609 | else | |
610 | { | |
611 | image = imageSel = -1; | |
612 | } | |
613 | wxTreeItemId id = AppendItem(idParent, str, image, imageSel, | |
614 | new MyTreeItemData(str)); | |
615 | ||
616 | // and now we also set the expanded one (only for the folders) | |
617 | if ( hasChildren && wxGetApp().ShowImages() ) | |
618 | { | |
619 | SetItemImage(id, TreeCtrlIcon_FolderOpened, | |
620 | wxTreeItemIcon_Expanded); | |
621 | } | |
622 | ||
623 | // remember the last child for OnEnsureVisible() | |
624 | if ( !hasChildren && n == numChildren - 1 ) | |
625 | { | |
626 | m_lastItem = id; | |
627 | } | |
628 | ||
629 | AddItemsRecursively(id, numChildren, depth - 1, n + 1); | |
630 | } | |
631 | } | |
632 | //else: done! | |
633 | } | |
634 | ||
635 | void MyTreeCtrl::AddTestItemsToTree(size_t numChildren, | |
636 | size_t depth) | |
637 | { | |
638 | int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder : -1; | |
639 | wxTreeItemId rootId = AddRoot("Root", | |
640 | image, image, | |
641 | new MyTreeItemData("Root item")); | |
642 | if ( image != -1 ) | |
643 | { | |
644 | SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded); | |
645 | } | |
646 | ||
647 | AddItemsRecursively(rootId, numChildren, depth, 0); | |
648 | ||
649 | // set some colours/fonts for testing | |
650 | SetItemFont(rootId, *wxITALIC_FONT); | |
651 | ||
652 | long cookie; | |
653 | wxTreeItemId id = GetFirstChild(rootId, cookie); | |
654 | SetItemTextColour(id, *wxBLUE); | |
655 | ||
656 | id = GetNextChild(rootId, cookie); | |
657 | id = GetNextChild(rootId, cookie); | |
658 | SetItemTextColour(id, *wxRED); | |
659 | SetItemBackgroundColour(id, *wxLIGHT_GREY); | |
660 | } | |
661 | ||
662 | void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie) | |
663 | { | |
664 | wxTreeItemId id; | |
665 | ||
666 | if( cookie == -1 ) | |
667 | id = GetFirstChild(idParent, cookie); | |
668 | else | |
669 | id = GetNextChild(idParent, cookie); | |
670 | ||
671 | if(id <= 0) | |
672 | return; | |
673 | ||
674 | wxString text = GetItemText(id); | |
675 | wxLogMessage(text); | |
676 | ||
677 | if (ItemHasChildren(id)) | |
678 | GetItemsRecursively(id,-1); | |
679 | ||
680 | GetItemsRecursively(idParent, cookie); | |
681 | } | |
682 | ||
683 | void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item) | |
684 | { | |
685 | int image = GetItemImage(item) == TreeCtrlIcon_Folder ? TreeCtrlIcon_File | |
686 | : TreeCtrlIcon_Folder; | |
687 | ||
688 | SetItemImage(item, image); | |
689 | } | |
690 | ||
691 | ||
692 | // avoid repetition | |
693 | #define TREE_EVENT_HANDLER(name) \ | |
694 | void MyTreeCtrl::name(wxTreeEvent& event) \ | |
695 | { \ | |
696 | wxLogMessage(#name); \ | |
697 | event.Skip(); \ | |
698 | } | |
699 | ||
700 | TREE_EVENT_HANDLER(OnBeginRDrag) | |
701 | TREE_EVENT_HANDLER(OnDeleteItem) | |
702 | TREE_EVENT_HANDLER(OnGetInfo) | |
703 | TREE_EVENT_HANDLER(OnSetInfo) | |
704 | TREE_EVENT_HANDLER(OnItemExpanded) | |
705 | TREE_EVENT_HANDLER(OnItemExpanding) | |
706 | TREE_EVENT_HANDLER(OnItemCollapsed) | |
707 | TREE_EVENT_HANDLER(OnSelChanged) | |
708 | TREE_EVENT_HANDLER(OnSelChanging) | |
709 | TREE_EVENT_HANDLER(OnTreeKeyDown) | |
710 | ||
711 | #undef TREE_EVENT_HANDLER | |
712 | ||
713 | void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event) | |
714 | { | |
715 | // need to explicitly allow drag | |
716 | if ( event.GetItem() != GetRootItem() ) | |
717 | { | |
718 | m_draggedItem = event.GetItem(); | |
719 | ||
720 | wxLogMessage("OnBeginDrag: started dragging %s", | |
721 | GetItemText(m_draggedItem).c_str()); | |
722 | ||
723 | event.Allow(); | |
724 | } | |
725 | else | |
726 | { | |
727 | wxLogMessage("OnBeginDrag: this item can't be dragged."); | |
728 | } | |
729 | } | |
730 | ||
731 | void MyTreeCtrl::OnEndDrag(wxTreeEvent& event) | |
732 | { | |
733 | wxTreeItemId itemSrc = m_draggedItem, | |
734 | itemDst = event.GetItem(); | |
735 | m_draggedItem = (wxTreeItemId)0l; | |
736 | ||
737 | // where to copy the item? | |
738 | if ( itemDst.IsOk() && !ItemHasChildren(itemDst) ) | |
739 | { | |
740 | // copy to the parent then | |
741 | itemDst = GetParent(itemDst); | |
742 | } | |
743 | ||
744 | if ( !itemDst.IsOk() ) | |
745 | { | |
746 | wxLogMessage("OnEndDrag: can't drop here."); | |
747 | ||
748 | return; | |
749 | } | |
750 | ||
751 | wxString text = GetItemText(itemSrc); | |
752 | wxLogMessage("OnEndDrag: '%s' copied to '%s'.", | |
753 | text.c_str(), GetItemText(itemDst).c_str()); | |
754 | ||
755 | // just do append here - we could also insert it just before/after the item | |
756 | // on which it was dropped, but this requires slightly more work... we also | |
757 | // completely ignore the client data and icon of the old item but could | |
758 | // copy them as well. | |
759 | // | |
760 | // Finally, we only copy one item here but we might copy the entire tree if | |
761 | // we were dragging a folder. | |
762 | int image = wxGetApp().ShowImages() ? TreeCtrlIcon_File : -1; | |
763 | AppendItem(itemDst, text, image); | |
764 | } | |
765 | ||
766 | void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event) | |
767 | { | |
768 | wxLogMessage("OnBeginLabelEdit"); | |
769 | ||
770 | // for testing, prevent this items label editing | |
771 | wxTreeItemId itemId = event.GetItem(); | |
772 | if ( IsTestItem(itemId) ) | |
773 | { | |
774 | wxMessageBox("You can't edit this item."); | |
775 | ||
776 | event.Veto(); | |
777 | } | |
778 | } | |
779 | ||
780 | void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event) | |
781 | { | |
782 | wxLogMessage("OnEndLabelEdit"); | |
783 | ||
784 | // don't allow anything except letters in the labels | |
785 | if ( !event.GetLabel().IsWord() ) | |
786 | { | |
787 | wxMessageBox("The label should contain only letters."); | |
788 | ||
789 | event.Veto(); | |
790 | } | |
791 | } | |
792 | ||
793 | void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event) | |
794 | { | |
795 | wxLogMessage("OnItemCollapsing"); | |
796 | ||
797 | // for testing, prevent the user from collapsing the first child folder | |
798 | wxTreeItemId itemId = event.GetItem(); | |
799 | if ( IsTestItem(itemId) ) | |
800 | { | |
801 | wxMessageBox("You can't collapse this item."); | |
802 | ||
803 | event.Veto(); | |
804 | } | |
805 | } | |
806 | ||
807 | void MyTreeCtrl::OnItemActivated(wxTreeEvent& event) | |
808 | { | |
809 | // show some info about this item | |
810 | wxTreeItemId itemId = event.GetItem(); | |
811 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId); | |
812 | ||
813 | if ( item != NULL ) | |
814 | { | |
815 | item->ShowInfo(this); | |
816 | } | |
817 | ||
818 | wxLogMessage("OnItemActivated"); | |
819 | } | |
820 | ||
821 | void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event) | |
822 | { | |
823 | wxTreeItemId id = HitTest(event.GetPosition()); | |
824 | if ( !id ) | |
825 | wxLogMessage("No item under mouse"); | |
826 | else | |
827 | { | |
828 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(id); | |
829 | if ( item ) | |
830 | wxLogMessage("Item '%s' under mouse", item->GetDesc()); | |
831 | } | |
832 | } | |
833 | ||
834 | static inline const char *Bool2String(bool b) | |
835 | { | |
836 | return b ? "" : "not "; | |
837 | } | |
838 | ||
839 | void MyTreeItemData::ShowInfo(wxTreeCtrl *tree) | |
840 | { | |
841 | wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n" | |
842 | "%u children (%u immediately under this item).", | |
843 | m_desc.c_str(), | |
844 | Bool2String(tree->IsSelected(GetId())), | |
845 | Bool2String(tree->IsExpanded(GetId())), | |
846 | Bool2String(tree->IsBold(GetId())), | |
847 | tree->GetChildrenCount(GetId()), | |
848 | tree->GetChildrenCount(GetId(), FALSE)); | |
849 | } |