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