1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
29 #include "wx/colordlg.h"
32 #include "wx/imaglist.h"
33 #include "wx/treectrl.h"
38 // this is not supported by native control
39 #define NO_VARIABLE_HEIGHT
44 // under Windows the icons are in the .rc file
51 #include "mondrian.xpm"
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(wxT("Please select some item first!"), \
57 wxT("Tree sample error"), \
58 wxOK | wxICON_EXCLAMATION, \
63 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
65 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
66 EVT_SIZE(MyFrame::OnSize
)
74 MENU_LINK(TogHideRoot
)
75 MENU_LINK(TogRootLines
)
77 MENU_LINK(TogFullHighlight
)
78 MENU_LINK(SetFgColour
)
79 MENU_LINK(SetBgColour
)
81 #ifndef NO_MULTIPLE_SELECTION
82 MENU_LINK(DumpSelected
)
86 #endif // NO_MULTIPLE_SELECTION
95 MENU_LINK(DeleteChildren
)
98 MENU_LINK(ToggleImages
)
99 MENU_LINK(ToggleButtons
)
100 MENU_LINK(SetImageSize
)
101 MENU_LINK(CollapseAndReset
)
102 MENU_LINK(EnsureVisible
)
104 MENU_LINK(InsertItem
)
107 MENU_LINK(IncSpacing
)
108 MENU_LINK(DecSpacing
)
109 MENU_LINK(ToggleIcon
)
114 #if USE_GENERIC_TREECTRL
115 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
117 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
119 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
120 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
121 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
122 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
123 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
124 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
125 #if 0 // there are so many of those that logging them causes flicker
126 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
128 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
129 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
130 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
131 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
132 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
133 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRightClick
)
135 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
136 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
137 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
138 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
139 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
140 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
147 // Create the main frame window
148 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
158 // My frame constructor
159 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
160 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)),
161 m_treeCtrl(NULL
), m_textCtrl(NULL
)
163 // This reduces flicker effects - even better would be to define
164 // OnEraseBackground to do nothing. When the tree control's scrollbars are
165 // show or hidden, the frame is sent a background erase event.
166 SetBackgroundColour(wxColour(255, 255, 255));
169 SetIcon(wxICON(mondrian
));
173 wxMenu
*file_menu
= new wxMenu
,
174 *style_menu
= new wxMenu
,
175 *tree_menu
= new wxMenu
,
176 *item_menu
= new wxMenu
;
178 file_menu
->Append(TreeTest_About
, wxT("&About..."));
179 file_menu
->AppendSeparator();
180 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
182 style_menu
->Append(TreeTest_TogButtons
, "Toggle &normal buttons");
183 style_menu
->Append(TreeTest_TogTwist
, "Toggle &twister buttons");
184 style_menu
->Append(TreeTest_ToggleButtons
, "Toggle image &buttons");
185 style_menu
->AppendSeparator();
186 style_menu
->Append(TreeTest_TogLines
, "Toggle &connecting lines");
187 style_menu
->Append(TreeTest_TogRootLines
, "Toggle &lines at root");
188 style_menu
->Append(TreeTest_TogHideRoot
, "Toggle &hidden root");
189 style_menu
->Append(TreeTest_TogBorder
, "Toggle &item border");
190 style_menu
->Append(TreeTest_TogFullHighlight
, "Toggle &full row highlight");
191 style_menu
->Append(TreeTest_TogEdit
, "Toggle &edit mode");
192 #ifndef NO_MULTIPLE_SELECTION
193 style_menu
->Append(TreeTest_ToggleSel
, wxT("Toggle &selection mode"));
194 #endif // NO_MULTIPLE_SELECTION
195 style_menu
->Append(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
196 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
197 style_menu
->AppendSeparator();
198 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
199 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
201 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
202 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
203 tree_menu
->AppendSeparator();
204 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
205 tree_menu
->Append(TreeTest_InsertItem
, "&Insert a new item");
206 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
207 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
208 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
209 tree_menu
->AppendSeparator();
210 tree_menu
->Append(TreeTest_Count
, "Count children of current item");
211 tree_menu
->Append(TreeTest_CountRec
, "Recursively count children of current item");
212 tree_menu
->AppendSeparator();
213 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
214 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
215 tree_menu
->AppendSeparator();
216 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
217 tree_menu
->AppendSeparator();
218 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
219 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
220 tree_menu
->AppendSeparator();
221 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
222 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
224 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
225 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
227 item_menu
->AppendSeparator();
228 item_menu
->Append(TreeTest_SetBold
, "Make item &bold");
229 item_menu
->Append(TreeTest_ClearBold
, "Make item ¬ bold");
230 item_menu
->AppendSeparator();
231 item_menu
->Append(TreeTest_ToggleIcon
, "Toggle the item's &icon");
233 #ifndef NO_MULTIPLE_SELECTION
234 item_menu
->AppendSeparator();
235 item_menu
->Append(TreeTest_DumpSelected
, "Dump selected items\tAlt-D");
236 item_menu
->Append(TreeTest_Select
, "Select current item\tAlt-S");
237 item_menu
->Append(TreeTest_Unselect
, "Unselect everything\tAlt-U");
238 #endif // NO_MULTIPLE_SELECTION
240 wxMenuBar
*menu_bar
= new wxMenuBar
;
241 menu_bar
->Append(file_menu
, "&File");
242 menu_bar
->Append(style_menu
, "&Style");
243 menu_bar
->Append(tree_menu
, "&Tree");
244 menu_bar
->Append(item_menu
, "&Item");
245 SetMenuBar(menu_bar
);
247 //menu_bar->Check(TreeTest_ToggleImages, TRUE);
248 #endif // wxUSE_MENUS
250 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
251 wxDefaultPosition
, wxDefaultSize
,
252 wxTR_DEFAULT_STYLE
| wxTR_EDIT_LABELS
|
253 #ifndef NO_VARIABLE_HEIGHT
254 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
258 m_textCtrl
= new wxTextCtrl(this, -1, "",
259 wxDefaultPosition
, wxDefaultSize
,
260 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
262 // create a status bar with 3 panes
264 SetStatusText("", 0);
267 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
268 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
269 delete wxLog::SetActiveTarget(new wxLogStderr
);
271 // set our text control as the log target
272 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
273 delete wxLog::SetActiveTarget(logWindow
);
279 delete wxLog::SetActiveTarget(NULL
);
282 void MyFrame::TogStyle(long flag
)
284 m_treeCtrl
->SetWindowStyle(m_treeCtrl
->GetWindowStyle() ^ flag
);
287 void MyFrame::OnSize(wxSizeEvent
& event
)
289 if ( m_treeCtrl
&& m_textCtrl
)
291 Resize(GetClientSize());
297 void MyFrame::Resize(const wxSize
& size
)
299 m_treeCtrl
->SetSize(0, 0, size
.x
, 2*size
.y
/3);
300 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3);
303 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
308 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
310 wxMessageBox("Tree test sample\n"
311 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
313 wxOK
| wxICON_INFORMATION
, this);
316 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
318 wxTreeItemId item
= m_treeCtrl
->GetSelection();
322 // old code - now we edit in place
324 static wxString s_text
;
325 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
327 if ( !s_text
.IsEmpty() )
329 m_treeCtrl
->SetItemText(item
, s_text
);
333 // TODO demonstrate creating a custom edit control...
334 (void)m_treeCtrl
->EditLabel(item
);
337 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
339 wxTreeItemId item
= m_treeCtrl
->GetSelection();
343 int i
= m_treeCtrl
->GetChildrenCount( item
, FALSE
);
345 wxLogMessage(wxT("%d children"), i
);
348 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
350 wxTreeItemId item
= m_treeCtrl
->GetSelection();
354 int i
= m_treeCtrl
->GetChildrenCount( item
);
356 wxLogMessage(wxT("%d children"), i
);
359 void MyFrame::DoSort(bool reverse
)
361 wxTreeItemId item
= m_treeCtrl
->GetSelection();
365 m_treeCtrl
->DoSortChildren(item
, reverse
);
368 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
370 wxTreeItemId root
= m_treeCtrl
->GetSelection();
374 m_treeCtrl
->GetItemsRecursively(root
, -1);
377 #ifndef NO_MULTIPLE_SELECTION
379 void MyFrame::OnToggleSel(wxCommandEvent
& WXUNUSED(event
))
381 TogStyle(wxTR_MULTIPLE
);
383 long style
= m_treeCtrl
->GetWindowStyle();
384 if ( style
& wxTR_MULTIPLE
)
385 style
&= ~wxTR_MULTIPLE
;
387 style
|= wxTR_MULTIPLE
;
391 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
392 wxDefaultPosition
, wxDefaultSize
,
394 Resize(GetClientSize());
398 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
400 wxArrayTreeItemIds array
;
402 size_t count
= m_treeCtrl
->GetSelections(array
);
403 wxLogMessage(wxT("%u items selected"), count
);
405 for ( size_t n
= 0; n
< count
; n
++ )
407 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
411 void MyFrame::OnSelect(wxCommandEvent
& event
)
413 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
416 void MyFrame::OnUnselect(wxCommandEvent
& event
)
418 m_treeCtrl
->UnselectAll();
421 #endif // NO_MULTIPLE_SELECTION
423 void MyFrame::DoSetBold(bool bold
)
425 wxTreeItemId item
= m_treeCtrl
->GetSelection();
429 m_treeCtrl
->SetItemBold(item
, bold
);
432 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
434 wxTreeItemId item
= m_treeCtrl
->GetSelection();
438 m_treeCtrl
->Delete(item
);
441 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
443 wxTreeItemId item
= m_treeCtrl
->GetSelection();
447 m_treeCtrl
->DeleteChildren(item
);
450 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
452 m_treeCtrl
->DeleteAllItems();
455 void MyFrame::OnRecreate(wxCommandEvent
& event
)
458 m_treeCtrl
->AddTestItemsToTree(5, 2);
461 void MyFrame::OnSetImageSize(wxCommandEvent
& event
)
463 int size
= wxGetNumberFromUser("Enter the size for the images to use",
466 m_treeCtrl
->ImageSize());
470 m_treeCtrl
->CreateImageList(size
);
471 wxGetApp().SetShowImages(TRUE
);
474 void MyFrame::OnToggleImages(wxCommandEvent
& event
)
476 if ( wxGetApp().ShowImages() )
478 m_treeCtrl
->CreateImageList(-1);
479 wxGetApp().SetShowImages(FALSE
);
483 m_treeCtrl
->CreateImageList(0);
484 wxGetApp().SetShowImages(TRUE
);
488 void MyFrame::OnToggleButtons(wxCommandEvent
& event
)
490 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
491 if ( wxGetApp().ShowButtons() )
493 m_treeCtrl
->CreateButtonsImageList(-1);
494 wxGetApp().SetShowButtons(FALSE
);
498 m_treeCtrl
->CreateButtonsImageList(15);
499 wxGetApp().SetShowButtons(TRUE
);
504 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
506 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
509 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
511 m_treeCtrl
->DoEnsureVisible();
514 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
516 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
517 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, "2nd item");
520 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
522 static int s_num
= 0;
525 text
.Printf(wxT("Item #%d"), ++s_num
);
527 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
529 MyTreeCtrl::TreeCtrlIcon_File */ );
532 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
534 unsigned int indent
= m_treeCtrl
->GetIndent();
536 m_treeCtrl
->SetIndent( indent
+5 );
539 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
541 unsigned int indent
= m_treeCtrl
->GetIndent();
543 m_treeCtrl
->SetIndent( indent
-5 );
546 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
548 unsigned int indent
= m_treeCtrl
->GetSpacing();
550 m_treeCtrl
->SetSpacing( indent
+5 );
553 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
555 unsigned int indent
= m_treeCtrl
->GetSpacing();
557 m_treeCtrl
->SetSpacing( indent
-5 );
560 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
562 wxTreeItemId item
= m_treeCtrl
->GetSelection();
566 m_treeCtrl
->DoToggleIcon(item
);
569 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
571 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
573 m_treeCtrl
->SetForegroundColour(col
);
576 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
578 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
580 m_treeCtrl
->SetBackgroundColour(col
);
583 // MyTreeCtrl implementation
584 #if USE_GENERIC_TREECTRL
585 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
587 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
590 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
591 const wxPoint
& pos
, const wxSize
& size
,
593 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
595 m_reverseSort
= FALSE
;
599 // Add some items to the tree
600 AddTestItemsToTree(5, 2);
603 void MyTreeCtrl::CreateImageList(int size
)
615 // Make an image list containing small icons
616 wxImageList
*images
= new wxImageList(size
, size
, TRUE
);
618 // should correspond to TreeCtrlIcon_xxx enum
619 #if defined(__WXMSW__) && defined(__WIN16__)
620 images
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
621 images
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
622 images
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
623 images
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
624 images
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
628 icons
[0] = wxICON(icon1
);
629 icons
[1] = wxICON(icon2
);
630 icons
[2] = wxICON(icon3
);
631 icons
[3] = wxICON(icon4
);
632 icons
[4] = wxICON(icon5
);
634 int sizeOrig
= icons
[0].GetWidth();
635 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
637 if ( size
== sizeOrig
)
639 images
->Add(icons
[i
]);
643 images
->Add(wxImage(icons
[i
]).Rescale(size
, size
).
649 AssignImageList(images
);
652 void MyTreeCtrl::CreateButtonsImageList(int size
)
654 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
657 SetButtonsImageList(NULL
);
661 // Make an image list containing small icons
662 wxImageList
*images
= new wxImageList(size
, size
, TRUE
);
664 // should correspond to TreeCtrlIcon_xxx enum
665 #if defined(__WXMSW__) && defined(__WIN16__)
666 images
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
667 images
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
668 images
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
669 images
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
670 images
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
674 icons
[0] = wxICON(icon3
); // closed
675 icons
[1] = wxICON(icon3
); // closed, selected
676 icons
[2] = wxICON(icon5
); // open
677 icons
[3] = wxICON(icon5
); // open, selected
679 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
681 int sizeOrig
= icons
[i
].GetWidth();
682 if ( size
== sizeOrig
)
684 images
->Add(icons
[i
]);
688 images
->Add(wxImage(icons
[i
]).Rescale(size
, size
).
694 AssignButtonsImageList(images
);
698 MyTreeCtrl::~MyTreeCtrl()
702 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
703 const wxTreeItemId
& item2
)
707 // just exchange 1st and 2nd items
708 return wxTreeCtrl::OnCompareItems(item2
, item1
);
712 return wxTreeCtrl::OnCompareItems(item1
, item2
);
716 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
723 bool hasChildren
= depth
> 1;
726 for ( size_t n
= 0; n
< numChildren
; n
++ )
728 // at depth 1 elements won't have any more children
730 str
.Printf(wxT("%s child %d"), wxT("Folder"), n
+ 1);
732 str
.Printf(wxT("%s child %d.%d"), wxT("File"), folder
, n
+ 1);
734 // here we pass to AppendItem() normal and selected item images (we
735 // suppose that selected image follows the normal one in the enum)
737 if ( wxGetApp().ShowImages() )
739 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
740 imageSel
= image
+ 1;
744 image
= imageSel
= -1;
746 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
747 new MyTreeItemData(str
));
749 // and now we also set the expanded one (only for the folders)
750 if ( hasChildren
&& wxGetApp().ShowImages() )
752 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
753 wxTreeItemIcon_Expanded
);
756 // remember the last child for OnEnsureVisible()
757 if ( !hasChildren
&& n
== numChildren
- 1 )
762 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
768 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
771 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
772 wxTreeItemId rootId
= AddRoot("Root",
774 new MyTreeItemData("Root item"));
777 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
780 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
782 // set some colours/fonts for testing
783 SetItemFont(rootId
, *wxITALIC_FONT
);
786 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
787 SetItemTextColour(id
, *wxBLUE
);
789 id
= GetNextChild(rootId
, cookie
);
790 id
= GetNextChild(rootId
, cookie
);
791 SetItemTextColour(id
, *wxRED
);
792 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
795 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
800 id
= GetFirstChild(idParent
, cookie
);
802 id
= GetNextChild(idParent
, cookie
);
807 wxString text
= GetItemText(id
);
810 if (ItemHasChildren(id
))
811 GetItemsRecursively(id
,-1);
813 GetItemsRecursively(idParent
, cookie
);
816 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
818 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
819 : TreeCtrlIcon_Folder
;
821 SetItemImage(item
, image
);
826 #define TREE_EVENT_HANDLER(name) \
827 void MyTreeCtrl::name(wxTreeEvent& event) \
829 wxLogMessage(wxT(#name)); \
833 TREE_EVENT_HANDLER(OnBeginRDrag
)
834 TREE_EVENT_HANDLER(OnDeleteItem
)
835 TREE_EVENT_HANDLER(OnGetInfo
)
836 TREE_EVENT_HANDLER(OnSetInfo
)
837 TREE_EVENT_HANDLER(OnItemExpanded
)
838 TREE_EVENT_HANDLER(OnItemExpanding
)
839 TREE_EVENT_HANDLER(OnItemCollapsed
)
840 TREE_EVENT_HANDLER(OnSelChanged
)
841 TREE_EVENT_HANDLER(OnSelChanging
)
842 TREE_EVENT_HANDLER(OnTreeKeyDown
)
844 #undef TREE_EVENT_HANDLER
846 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
848 // need to explicitly allow drag
849 if ( event
.GetItem() != GetRootItem() )
851 m_draggedItem
= event
.GetItem();
853 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
854 GetItemText(m_draggedItem
).c_str());
860 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
864 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
866 wxTreeItemId itemSrc
= m_draggedItem
,
867 itemDst
= event
.GetItem();
868 m_draggedItem
= (wxTreeItemId
)0l;
870 // where to copy the item?
871 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
873 // copy to the parent then
874 itemDst
= GetParent(itemDst
);
877 if ( !itemDst
.IsOk() )
879 wxLogMessage(wxT("OnEndDrag: can't drop here."));
884 wxString text
= GetItemText(itemSrc
);
885 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
886 text
.c_str(), GetItemText(itemDst
).c_str());
888 // just do append here - we could also insert it just before/after the item
889 // on which it was dropped, but this requires slightly more work... we also
890 // completely ignore the client data and icon of the old item but could
891 // copy them as well.
893 // Finally, we only copy one item here but we might copy the entire tree if
894 // we were dragging a folder.
895 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
896 AppendItem(itemDst
, text
, image
);
899 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
901 wxLogMessage(wxT("OnBeginLabelEdit"));
903 // for testing, prevent this item's label editing
904 wxTreeItemId itemId
= event
.GetItem();
905 if ( IsTestItem(itemId
) )
907 wxMessageBox(wxT("You can't edit this item."));
913 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
915 wxLogMessage(wxT("OnEndLabelEdit"));
917 // don't allow anything except letters in the labels
918 if ( !event
.GetLabel().IsWord() )
920 wxMessageBox(wxT("The label should contain only letters."));
926 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
928 wxLogMessage(wxT("OnItemCollapsing"));
930 // for testing, prevent the user from collapsing the first child folder
931 wxTreeItemId itemId
= event
.GetItem();
932 if ( IsTestItem(itemId
) )
934 wxMessageBox(wxT("You can't collapse this item."));
940 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
942 // show some info about this item
943 wxTreeItemId itemId
= event
.GetItem();
944 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
948 item
->ShowInfo(this);
951 wxLogMessage(wxT("OnItemActivated"));
954 void MyTreeCtrl::OnItemRightClick(wxTreeEvent
& event
)
956 ShowMenu(event
.GetItem(), event
.GetPoint());
959 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
961 wxPoint pt
= event
.GetPosition();
962 ShowMenu(HitTest(pt
), pt
);
965 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
970 title
<< _T("Menu for ") << GetItemText(id
);
974 title
= _T("Menu for no particular item");
979 menu
.Append(TreeTest_About
, _T("&About..."));
980 menu
.Append(TreeTest_Dump
, _T("&Dump"));
982 PopupMenu(&menu
, pt
);
983 #endif // wxUSE_MENUS
986 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
988 wxTreeItemId id
= HitTest(event
.GetPosition());
990 wxLogMessage(wxT("No item under mouse"));
993 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
995 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
999 static inline const wxChar
*Bool2String(bool b
)
1001 return b
? wxT("") : wxT("not ");
1004 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1006 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1007 wxT("%u children (%u immediately under this item)."),
1009 Bool2String(tree
->IsSelected(GetId())),
1010 Bool2String(tree
->IsExpanded(GetId())),
1011 Bool2String(tree
->IsBold(GetId())),
1012 tree
->GetChildrenCount(GetId()),
1013 tree
->GetChildrenCount(GetId(), FALSE
));