1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
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!
16 #pragma interface "treetest.cpp"
17 #pragma implementation "treetest.cpp"
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/colordlg.h"
35 #include "wx/imaglist.h"
36 #include "wx/treectrl.h"
41 // this is not supported by native control
42 #define NO_VARIABLE_HEIGHT
47 // under Windows the icons are in the .rc file
54 #include "mondrian.xpm"
57 // verify that the item is ok and insult the user if it is not
58 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
59 wxMessageBox(wxT("Please select some item first!"), \
60 wxT("Tree sample error"), \
61 wxOK | wxICON_EXCLAMATION, \
66 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
68 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
69 EVT_SIZE(MyFrame::OnSize
)
77 MENU_LINK(TogHideRoot
)
78 MENU_LINK(TogRootLines
)
80 MENU_LINK(TogFullHighlight
)
81 MENU_LINK(SetFgColour
)
82 MENU_LINK(SetBgColour
)
84 #ifndef NO_MULTIPLE_SELECTION
85 MENU_LINK(DumpSelected
)
89 #endif // NO_MULTIPLE_SELECTION
98 MENU_LINK(DeleteChildren
)
101 MENU_LINK(ToggleImages
)
102 MENU_LINK(ToggleButtons
)
103 MENU_LINK(SetImageSize
)
104 MENU_LINK(CollapseAndReset
)
105 MENU_LINK(EnsureVisible
)
107 MENU_LINK(InsertItem
)
110 MENU_LINK(IncSpacing
)
111 MENU_LINK(DecSpacing
)
112 MENU_LINK(ToggleIcon
)
117 #if USE_GENERIC_TREECTRL
118 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
120 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
122 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
123 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
124 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
125 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
126 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
127 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
128 #if 0 // there are so many of those that logging them causes flicker
129 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
131 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
132 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
133 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
134 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
135 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
136 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRightClick
)
138 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
139 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
140 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
141 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
142 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
143 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
150 // Create the main frame window
151 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
161 // My frame constructor
162 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
163 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)),
164 m_treeCtrl(NULL
), m_textCtrl(NULL
)
166 // This reduces flicker effects - even better would be to define
167 // OnEraseBackground to do nothing. When the tree control's scrollbars are
168 // show or hidden, the frame is sent a background erase event.
169 SetBackgroundColour(wxColour(255, 255, 255));
172 SetIcon(wxICON(mondrian
));
176 wxMenu
*file_menu
= new wxMenu
,
177 *style_menu
= new wxMenu
,
178 *tree_menu
= new wxMenu
,
179 *item_menu
= new wxMenu
;
181 file_menu
->Append(TreeTest_About
, wxT("&About..."));
182 file_menu
->AppendSeparator();
183 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
185 style_menu
->Append(TreeTest_TogButtons
, "Toggle &normal buttons");
186 style_menu
->Append(TreeTest_TogTwist
, "Toggle &twister buttons");
187 style_menu
->Append(TreeTest_ToggleButtons
, "Toggle image &buttons");
188 style_menu
->AppendSeparator();
189 style_menu
->Append(TreeTest_TogLines
, "Toggle &connecting lines");
190 style_menu
->Append(TreeTest_TogRootLines
, "Toggle &lines at root");
191 style_menu
->Append(TreeTest_TogHideRoot
, "Toggle &hidden root");
192 style_menu
->Append(TreeTest_TogBorder
, "Toggle &item border");
193 style_menu
->Append(TreeTest_TogFullHighlight
, "Toggle &full row highlight");
194 style_menu
->Append(TreeTest_TogEdit
, "Toggle &edit mode");
195 #ifndef NO_MULTIPLE_SELECTION
196 style_menu
->Append(TreeTest_ToggleSel
, wxT("Toggle &selection mode"));
197 #endif // NO_MULTIPLE_SELECTION
198 style_menu
->Append(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
199 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
200 style_menu
->AppendSeparator();
201 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
202 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
204 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
205 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
206 tree_menu
->AppendSeparator();
207 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
208 tree_menu
->Append(TreeTest_InsertItem
, "&Insert a new item");
209 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
210 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
211 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
212 tree_menu
->AppendSeparator();
213 tree_menu
->Append(TreeTest_Count
, "Count children of current item");
214 tree_menu
->Append(TreeTest_CountRec
, "Recursively count children of current item");
215 tree_menu
->AppendSeparator();
216 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
217 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
218 tree_menu
->AppendSeparator();
219 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
220 tree_menu
->AppendSeparator();
221 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
222 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
223 tree_menu
->AppendSeparator();
224 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
225 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
227 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
228 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
230 item_menu
->AppendSeparator();
231 item_menu
->Append(TreeTest_SetBold
, "Make item &bold");
232 item_menu
->Append(TreeTest_ClearBold
, "Make item ¬ bold");
233 item_menu
->AppendSeparator();
234 item_menu
->Append(TreeTest_ToggleIcon
, "Toggle the item's &icon");
236 #ifndef NO_MULTIPLE_SELECTION
237 item_menu
->AppendSeparator();
238 item_menu
->Append(TreeTest_DumpSelected
, "Dump selected items\tAlt-D");
239 item_menu
->Append(TreeTest_Select
, "Select current item\tAlt-S");
240 item_menu
->Append(TreeTest_Unselect
, "Unselect everything\tAlt-U");
241 #endif // NO_MULTIPLE_SELECTION
243 wxMenuBar
*menu_bar
= new wxMenuBar
;
244 menu_bar
->Append(file_menu
, "&File");
245 menu_bar
->Append(style_menu
, "&Style");
246 menu_bar
->Append(tree_menu
, "&Tree");
247 menu_bar
->Append(item_menu
, "&Item");
248 SetMenuBar(menu_bar
);
250 //menu_bar->Check(TreeTest_ToggleImages, TRUE);
251 #endif // wxUSE_MENUS
253 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
254 wxDefaultPosition
, wxDefaultSize
,
255 wxTR_DEFAULT_STYLE
| wxTR_EDIT_LABELS
|
256 #ifndef NO_VARIABLE_HEIGHT
257 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
261 m_textCtrl
= new wxTextCtrl(this, -1, "",
262 wxDefaultPosition
, wxDefaultSize
,
263 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
265 // create a status bar with 3 panes
267 SetStatusText("", 0);
270 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
271 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
272 delete wxLog::SetActiveTarget(new wxLogStderr
);
274 // set our text control as the log target
275 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
276 delete wxLog::SetActiveTarget(logWindow
);
282 delete wxLog::SetActiveTarget(NULL
);
285 void MyFrame::TogStyle(long flag
)
287 m_treeCtrl
->SetWindowStyle(m_treeCtrl
->GetWindowStyle() ^ flag
);
290 void MyFrame::OnSize(wxSizeEvent
& event
)
292 if ( m_treeCtrl
&& m_textCtrl
)
294 Resize(GetClientSize());
300 void MyFrame::Resize(const wxSize
& size
)
302 m_treeCtrl
->SetSize(0, 0, size
.x
, 2*size
.y
/3);
303 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3);
306 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
311 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
313 wxMessageBox("Tree test sample\n"
314 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
316 wxOK
| wxICON_INFORMATION
, this);
319 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
321 wxTreeItemId item
= m_treeCtrl
->GetSelection();
325 // old code - now we edit in place
327 static wxString s_text
;
328 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
330 if ( !s_text
.IsEmpty() )
332 m_treeCtrl
->SetItemText(item
, s_text
);
336 // TODO demonstrate creating a custom edit control...
337 (void)m_treeCtrl
->EditLabel(item
);
340 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
342 wxTreeItemId item
= m_treeCtrl
->GetSelection();
346 int i
= m_treeCtrl
->GetChildrenCount( item
, FALSE
);
348 wxLogMessage(wxT("%d children"), i
);
351 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
353 wxTreeItemId item
= m_treeCtrl
->GetSelection();
357 int i
= m_treeCtrl
->GetChildrenCount( item
);
359 wxLogMessage(wxT("%d children"), i
);
362 void MyFrame::DoSort(bool reverse
)
364 wxTreeItemId item
= m_treeCtrl
->GetSelection();
368 m_treeCtrl
->DoSortChildren(item
, reverse
);
371 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
373 wxTreeItemId root
= m_treeCtrl
->GetSelection();
377 m_treeCtrl
->GetItemsRecursively(root
, -1);
380 #ifndef NO_MULTIPLE_SELECTION
382 void MyFrame::OnToggleSel(wxCommandEvent
& WXUNUSED(event
))
384 TogStyle(wxTR_MULTIPLE
);
386 long style
= m_treeCtrl
->GetWindowStyle();
387 if ( style
& wxTR_MULTIPLE
)
388 style
&= ~wxTR_MULTIPLE
;
390 style
|= wxTR_MULTIPLE
;
394 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
395 wxDefaultPosition
, wxDefaultSize
,
397 Resize(GetClientSize());
401 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
403 wxArrayTreeItemIds array
;
405 size_t count
= m_treeCtrl
->GetSelections(array
);
406 wxLogMessage(wxT("%u items selected"), count
);
408 for ( size_t n
= 0; n
< count
; n
++ )
410 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
414 void MyFrame::OnSelect(wxCommandEvent
& event
)
416 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
419 void MyFrame::OnUnselect(wxCommandEvent
& event
)
421 m_treeCtrl
->UnselectAll();
424 #endif // NO_MULTIPLE_SELECTION
426 void MyFrame::DoSetBold(bool bold
)
428 wxTreeItemId item
= m_treeCtrl
->GetSelection();
432 m_treeCtrl
->SetItemBold(item
, bold
);
435 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
437 wxTreeItemId item
= m_treeCtrl
->GetSelection();
441 m_treeCtrl
->Delete(item
);
444 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
446 wxTreeItemId item
= m_treeCtrl
->GetSelection();
450 m_treeCtrl
->DeleteChildren(item
);
453 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
455 m_treeCtrl
->DeleteAllItems();
458 void MyFrame::OnRecreate(wxCommandEvent
& event
)
461 m_treeCtrl
->AddTestItemsToTree(5, 2);
464 void MyFrame::OnSetImageSize(wxCommandEvent
& event
)
466 int size
= wxGetNumberFromUser("Enter the size for the images to use",
469 m_treeCtrl
->ImageSize());
473 m_treeCtrl
->CreateImageList(size
);
474 wxGetApp().SetShowImages(TRUE
);
477 void MyFrame::OnToggleImages(wxCommandEvent
& event
)
479 if ( wxGetApp().ShowImages() )
481 m_treeCtrl
->CreateImageList(-1);
482 wxGetApp().SetShowImages(FALSE
);
486 m_treeCtrl
->CreateImageList(0);
487 wxGetApp().SetShowImages(TRUE
);
491 void MyFrame::OnToggleButtons(wxCommandEvent
& event
)
493 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
494 if ( wxGetApp().ShowButtons() )
496 m_treeCtrl
->CreateButtonsImageList(-1);
497 wxGetApp().SetShowButtons(FALSE
);
501 m_treeCtrl
->CreateButtonsImageList(15);
502 wxGetApp().SetShowButtons(TRUE
);
507 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
509 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
512 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
514 m_treeCtrl
->DoEnsureVisible();
517 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
519 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
520 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, "2nd item");
523 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
525 static int s_num
= 0;
528 text
.Printf(wxT("Item #%d"), ++s_num
);
530 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
532 MyTreeCtrl::TreeCtrlIcon_File */ );
535 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
537 unsigned int indent
= m_treeCtrl
->GetIndent();
539 m_treeCtrl
->SetIndent( indent
+5 );
542 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
544 unsigned int indent
= m_treeCtrl
->GetIndent();
546 m_treeCtrl
->SetIndent( indent
-5 );
549 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
551 unsigned int indent
= m_treeCtrl
->GetSpacing();
553 m_treeCtrl
->SetSpacing( indent
+5 );
556 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
558 unsigned int indent
= m_treeCtrl
->GetSpacing();
560 m_treeCtrl
->SetSpacing( indent
-5 );
563 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
565 wxTreeItemId item
= m_treeCtrl
->GetSelection();
569 m_treeCtrl
->DoToggleIcon(item
);
572 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
574 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
576 m_treeCtrl
->SetForegroundColour(col
);
579 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
581 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
583 m_treeCtrl
->SetBackgroundColour(col
);
586 // MyTreeCtrl implementation
587 #if USE_GENERIC_TREECTRL
588 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
590 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
593 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
594 const wxPoint
& pos
, const wxSize
& size
,
596 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
598 m_reverseSort
= FALSE
;
602 // Add some items to the tree
603 AddTestItemsToTree(5, 2);
606 void MyTreeCtrl::CreateImageList(int size
)
618 // Make an image list containing small icons
619 wxImageList
*images
= new wxImageList(size
, size
, TRUE
);
621 // should correspond to TreeCtrlIcon_xxx enum
622 #if defined(__WXMSW__) && defined(__WIN16__)
623 images
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
624 images
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
625 images
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
626 images
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
627 images
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
631 icons
[0] = wxICON(icon1
);
632 icons
[1] = wxICON(icon2
);
633 icons
[2] = wxICON(icon3
);
634 icons
[3] = wxICON(icon4
);
635 icons
[4] = wxICON(icon5
);
637 int sizeOrig
= icons
[0].GetWidth();
638 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
640 if ( size
== sizeOrig
)
642 images
->Add(icons
[i
]);
646 images
->Add(wxImage(icons
[i
]).Rescale(size
, size
).
652 AssignImageList(images
);
655 void MyTreeCtrl::CreateButtonsImageList(int size
)
657 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
660 SetButtonsImageList(NULL
);
664 // Make an image list containing small icons
665 wxImageList
*images
= new wxImageList(size
, size
, TRUE
);
667 // should correspond to TreeCtrlIcon_xxx enum
668 #if defined(__WXMSW__) && defined(__WIN16__)
669 images
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
670 images
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
671 images
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
672 images
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
673 images
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
677 icons
[0] = wxICON(icon3
); // closed
678 icons
[1] = wxICON(icon3
); // closed, selected
679 icons
[2] = wxICON(icon5
); // open
680 icons
[3] = wxICON(icon5
); // open, selected
682 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
684 int sizeOrig
= icons
[i
].GetWidth();
685 if ( size
== sizeOrig
)
687 images
->Add(icons
[i
]);
691 images
->Add(wxImage(icons
[i
]).Rescale(size
, size
).
697 AssignButtonsImageList(images
);
701 MyTreeCtrl::~MyTreeCtrl()
705 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
706 const wxTreeItemId
& item2
)
710 // just exchange 1st and 2nd items
711 return wxTreeCtrl::OnCompareItems(item2
, item1
);
715 return wxTreeCtrl::OnCompareItems(item1
, item2
);
719 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
726 bool hasChildren
= depth
> 1;
729 for ( size_t n
= 0; n
< numChildren
; n
++ )
731 // at depth 1 elements won't have any more children
733 str
.Printf(wxT("%s child %d"), wxT("Folder"), n
+ 1);
735 str
.Printf(wxT("%s child %d.%d"), wxT("File"), folder
, n
+ 1);
737 // here we pass to AppendItem() normal and selected item images (we
738 // suppose that selected image follows the normal one in the enum)
740 if ( wxGetApp().ShowImages() )
742 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
743 imageSel
= image
+ 1;
747 image
= imageSel
= -1;
749 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
750 new MyTreeItemData(str
));
752 // and now we also set the expanded one (only for the folders)
753 if ( hasChildren
&& wxGetApp().ShowImages() )
755 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
756 wxTreeItemIcon_Expanded
);
759 // remember the last child for OnEnsureVisible()
760 if ( !hasChildren
&& n
== numChildren
- 1 )
765 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
771 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
774 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
775 wxTreeItemId rootId
= AddRoot("Root",
777 new MyTreeItemData("Root item"));
780 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
783 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
785 // set some colours/fonts for testing
786 SetItemFont(rootId
, *wxITALIC_FONT
);
789 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
790 SetItemTextColour(id
, *wxBLUE
);
792 id
= GetNextChild(rootId
, cookie
);
793 id
= GetNextChild(rootId
, cookie
);
794 SetItemTextColour(id
, *wxRED
);
795 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
798 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
803 id
= GetFirstChild(idParent
, cookie
);
805 id
= GetNextChild(idParent
, cookie
);
810 wxString text
= GetItemText(id
);
813 if (ItemHasChildren(id
))
814 GetItemsRecursively(id
,-1);
816 GetItemsRecursively(idParent
, cookie
);
819 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
821 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
822 : TreeCtrlIcon_Folder
;
824 SetItemImage(item
, image
);
829 #define TREE_EVENT_HANDLER(name) \
830 void MyTreeCtrl::name(wxTreeEvent& event) \
832 wxLogMessage(wxT(#name)); \
836 TREE_EVENT_HANDLER(OnBeginRDrag
)
837 TREE_EVENT_HANDLER(OnDeleteItem
)
838 TREE_EVENT_HANDLER(OnGetInfo
)
839 TREE_EVENT_HANDLER(OnSetInfo
)
840 TREE_EVENT_HANDLER(OnItemExpanded
)
841 TREE_EVENT_HANDLER(OnItemExpanding
)
842 TREE_EVENT_HANDLER(OnItemCollapsed
)
843 TREE_EVENT_HANDLER(OnSelChanged
)
844 TREE_EVENT_HANDLER(OnSelChanging
)
845 TREE_EVENT_HANDLER(OnTreeKeyDown
)
847 #undef TREE_EVENT_HANDLER
849 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
851 // need to explicitly allow drag
852 if ( event
.GetItem() != GetRootItem() )
854 m_draggedItem
= event
.GetItem();
856 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
857 GetItemText(m_draggedItem
).c_str());
863 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
867 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
869 wxTreeItemId itemSrc
= m_draggedItem
,
870 itemDst
= event
.GetItem();
871 m_draggedItem
= (wxTreeItemId
)0l;
873 // where to copy the item?
874 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
876 // copy to the parent then
877 itemDst
= GetParent(itemDst
);
880 if ( !itemDst
.IsOk() )
882 wxLogMessage(wxT("OnEndDrag: can't drop here."));
887 wxString text
= GetItemText(itemSrc
);
888 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
889 text
.c_str(), GetItemText(itemDst
).c_str());
891 // just do append here - we could also insert it just before/after the item
892 // on which it was dropped, but this requires slightly more work... we also
893 // completely ignore the client data and icon of the old item but could
894 // copy them as well.
896 // Finally, we only copy one item here but we might copy the entire tree if
897 // we were dragging a folder.
898 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
899 AppendItem(itemDst
, text
, image
);
902 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
904 wxLogMessage(wxT("OnBeginLabelEdit"));
906 // for testing, prevent this item's label editing
907 wxTreeItemId itemId
= event
.GetItem();
908 if ( IsTestItem(itemId
) )
910 wxMessageBox(wxT("You can't edit this item."));
916 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
918 wxLogMessage(wxT("OnEndLabelEdit"));
920 // don't allow anything except letters in the labels
921 if ( !event
.GetLabel().IsWord() )
923 wxMessageBox(wxT("The label should contain only letters."));
929 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
931 wxLogMessage(wxT("OnItemCollapsing"));
933 // for testing, prevent the user from collapsing the first child folder
934 wxTreeItemId itemId
= event
.GetItem();
935 if ( IsTestItem(itemId
) )
937 wxMessageBox(wxT("You can't collapse this item."));
943 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
945 // show some info about this item
946 wxTreeItemId itemId
= event
.GetItem();
947 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
951 item
->ShowInfo(this);
954 wxLogMessage(wxT("OnItemActivated"));
957 void MyTreeCtrl::OnItemRightClick(wxTreeEvent
& event
)
959 ShowMenu(event
.GetItem(), event
.GetPoint());
962 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
964 wxPoint pt
= event
.GetPosition();
965 ShowMenu(HitTest(pt
), pt
);
968 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
973 title
<< _T("Menu for ") << GetItemText(id
);
977 title
= _T("Menu for no particular item");
982 menu
.Append(TreeTest_About
, _T("&About..."));
983 menu
.Append(TreeTest_Dump
, _T("&Dump"));
985 PopupMenu(&menu
, pt
);
986 #endif // wxUSE_MENUS
989 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
991 wxTreeItemId id
= HitTest(event
.GetPosition());
993 wxLogMessage(wxT("No item under mouse"));
996 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
998 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1002 static inline const wxChar
*Bool2String(bool b
)
1004 return b
? wxT("") : wxT("not ");
1007 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1009 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1010 wxT("%u children (%u immediately under this item)."),
1012 Bool2String(tree
->IsSelected(GetId())),
1013 Bool2String(tree
->IsExpanded(GetId())),
1014 Bool2String(tree
->IsBold(GetId())),
1015 tree
->GetChildrenCount(GetId()),
1016 tree
->GetChildrenCount(GetId(), FALSE
));