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"
31 #include "wx/imaglist.h"
32 #include "wx/treectrl.h"
37 #define NO_MULTIPLE_SELECTION
38 #define NO_VARIABLE_HEIGHT
43 // under Windows the icons are in the .rc file
49 #include "mondrian.xpm"
52 // verify that the item is ok and insult the user if it is not
53 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
54 wxMessageBox("Please select some item first!", \
55 "Tree sample error", \
56 wxOK | wxICON_EXCLAMATION, \
61 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
62 EVT_MENU(TreeTest_Quit
, MyFrame::OnQuit
)
63 EVT_MENU(TreeTest_About
, MyFrame::OnAbout
)
64 EVT_MENU(TreeTest_Dump
, MyFrame::OnDump
)
65 #ifndef NO_MULTIPLE_SELECTION
66 EVT_MENU(TreeTest_DumpSelected
, MyFrame::OnDumpSelected
)
67 EVT_MENU(TreeTest_Select
, MyFrame::OnSelect
)
68 EVT_MENU(TreeTest_Unselect
, MyFrame::OnUnselect
)
69 #endif // NO_MULTIPLE_SELECTION
70 EVT_MENU(TreeTest_Rename
, MyFrame::OnRename
)
71 EVT_MENU(TreeTest_Sort
, MyFrame::OnSort
)
72 EVT_MENU(TreeTest_SortRev
, MyFrame::OnSortRev
)
73 EVT_MENU(TreeTest_Bold
, MyFrame::OnSetBold
)
74 EVT_MENU(TreeTest_UnBold
, MyFrame::OnClearBold
)
75 EVT_MENU(TreeTest_Delete
, MyFrame::OnDelete
)
76 EVT_MENU(TreeTest_DeleteChildren
, MyFrame::OnDeleteChildren
)
77 EVT_MENU(TreeTest_DeleteAll
, MyFrame::OnDeleteAll
)
78 EVT_MENU(TreeTest_Recreate
, MyFrame::OnRecreate
)
79 EVT_MENU(TreeTest_CollapseAndReset
, MyFrame::OnCollapseAndReset
)
80 EVT_MENU(TreeTest_EnsureVisible
, MyFrame::OnEnsureVisible
)
81 EVT_MENU(TreeTest_AddItem
, MyFrame::OnAddItem
)
82 EVT_MENU(TreeTest_IncIndent
, MyFrame::OnIncIndent
)
83 EVT_MENU(TreeTest_DecIndent
, MyFrame::OnDecIndent
)
84 EVT_MENU(TreeTest_IncSpacing
, MyFrame::OnIncSpacing
)
85 EVT_MENU(TreeTest_DecSpacing
, MyFrame::OnDecSpacing
)
86 EVT_MENU(TreeTest_ToggleIcon
, MyFrame::OnToggleIcon
)
89 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
90 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
91 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
92 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
93 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
94 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
95 #if 0 // there are so many of those that logging them causes flicker
96 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
98 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
99 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
100 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
101 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
102 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
103 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
104 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
105 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
106 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
107 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
114 // Create the main frame window
115 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
125 // My frame constructor
126 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
127 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
129 // This reduces flicker effects - even better would be to define
130 // OnEraseBackground to do nothing. When the tree control's scrollbars are
131 // show or hidden, the frame is sent a background erase event.
132 SetBackgroundColour(wxColour(255, 255, 255));
135 SetIcon(wxICON(mondrian
));
138 wxMenu
*file_menu
= new wxMenu
,
139 *tree_menu
= new wxMenu
,
140 *item_menu
= new wxMenu
;
142 file_menu
->Append(TreeTest_About
, "&About...");
143 file_menu
->AppendSeparator();
144 file_menu
->Append(TreeTest_Quit
, "E&xit");
146 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
147 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
148 tree_menu
->AppendSeparator();
149 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
150 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
151 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
152 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
153 tree_menu
->AppendSeparator();
154 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
155 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
156 tree_menu
->AppendSeparator();
157 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
158 tree_menu
->AppendSeparator();
159 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
160 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
161 tree_menu
->AppendSeparator();
162 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
163 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
165 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
166 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
168 item_menu
->AppendSeparator();
169 item_menu
->Append(TreeTest_Bold
, "Make item &bold");
170 item_menu
->Append(TreeTest_UnBold
, "Make item ¬ bold");
171 item_menu
->AppendSeparator();
172 item_menu
->Append(TreeTest_ToggleIcon
, "Toggle the items &icon");
174 #ifndef NO_MULTIPLE_SELECTION
175 item_menu
->AppendSeparator();
176 item_menu
->Append(TreeTest_DumpSelected
, "Dump selected items\tAlt-D");
177 item_menu
->Append(TreeTest_Select
, "Select current item\tAlt-S");
178 item_menu
->Append(TreeTest_Unselect
, "Unselect everything\tAlt-U");
181 wxMenuBar
*menu_bar
= new wxMenuBar
;
182 menu_bar
->Append(file_menu
, "&File");
183 menu_bar
->Append(tree_menu
, "&Tree");
184 menu_bar
->Append(item_menu
, "&Item");
185 SetMenuBar(menu_bar
);
187 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
188 wxDefaultPosition
, wxDefaultSize
,
191 #ifndef NO_MULTIPLE_SELECTION
194 #ifndef NO_VARIABLE_HEIGHT
195 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
198 wxTextCtrl
*textCtrl
= new wxTextCtrl(this, -1, "",
199 wxDefaultPosition
, wxDefaultSize
,
200 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
202 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
203 c
->top
.SameAs(this, wxTop
);
204 c
->left
.SameAs(this, wxLeft
);
205 c
->right
.SameAs(this, wxRight
);
206 c
->height
.PercentOf(this, wxHeight
, 66);
207 m_treeCtrl
->SetConstraints(c
);
209 c
= new wxLayoutConstraints
;
210 c
->top
.Below(m_treeCtrl
);
211 c
->left
.SameAs(this, wxLeft
);
212 c
->right
.SameAs(this, wxRight
);
213 c
->bottom
.SameAs(this, wxBottom
);
214 textCtrl
->SetConstraints(c
);
217 // create a status bar with 3 panes
219 SetStatusText("", 0);
222 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
223 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
224 delete wxLog::SetActiveTarget(new wxLogStderr
);
226 // set our text control as the log target
227 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
228 delete wxLog::SetActiveTarget(logWindow
);
234 delete wxLog::SetActiveTarget(NULL
);
237 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
242 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
244 wxMessageBox("Tree test sample\n"
245 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
247 wxOK
| wxICON_INFORMATION
, this);
250 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
252 wxTreeItemId item
= m_treeCtrl
->GetSelection();
256 // old code - now we edit in place
258 static wxString s_text
;
259 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
261 if ( !s_text
.IsEmpty() )
263 m_treeCtrl
->SetItemText(item
, s_text
);
267 // TODO demonstrate creating a custom edit control...
268 (void)m_treeCtrl
->EditLabel(item
);
271 void MyFrame::DoSort(bool reverse
)
273 wxTreeItemId item
= m_treeCtrl
->GetSelection();
277 m_treeCtrl
->DoSortChildren(item
, reverse
);
280 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
282 wxTreeItemId root
= m_treeCtrl
->GetSelection();
286 m_treeCtrl
->GetItemsRecursively(root
, -1);
289 #ifndef NO_MULTIPLE_SELECTION
291 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
293 wxArrayTreeItemIds array
;
295 size_t count
= m_treeCtrl
->GetSelections(array
);
296 wxLogMessage(_T("%u items selected"), count
);
298 for ( size_t n
= 0; n
< count
; n
++ )
300 wxLogMessage("\t%s", m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
304 void MyFrame::OnSelect(wxCommandEvent
& event
)
306 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
309 void MyFrame::OnUnselect(wxCommandEvent
& event
)
311 m_treeCtrl
->UnselectAll();
314 #endif // NO_MULTIPLE_SELECTION
316 void MyFrame::DoSetBold(bool bold
)
318 wxTreeItemId item
= m_treeCtrl
->GetSelection();
322 m_treeCtrl
->SetItemBold(item
, bold
);
325 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
327 wxTreeItemId item
= m_treeCtrl
->GetSelection();
331 m_treeCtrl
->Delete(item
);
334 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
336 wxTreeItemId item
= m_treeCtrl
->GetSelection();
340 m_treeCtrl
->DeleteChildren(item
);
343 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
345 m_treeCtrl
->DeleteAllItems();
348 void MyFrame::OnRecreate(wxCommandEvent
& event
)
351 m_treeCtrl
->AddTestItemsToTree(3, 2);
354 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
356 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
359 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
361 m_treeCtrl
->DoEnsureVisible();
364 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
366 static int s_num
= 0;
369 text
.Printf("Item #%d", ++s_num
);
371 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
373 MyTreeCtrl::TreeCtrlIcon_File */ );
376 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
378 unsigned int indent
= m_treeCtrl
->GetIndent();
380 m_treeCtrl
->SetIndent( indent
+5 );
383 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
385 unsigned int indent
= m_treeCtrl
->GetIndent();
387 m_treeCtrl
->SetIndent( indent
-5 );
390 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
392 unsigned int indent
= m_treeCtrl
->GetSpacing();
394 m_treeCtrl
->SetSpacing( indent
+5 );
397 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
399 unsigned int indent
= m_treeCtrl
->GetSpacing();
401 m_treeCtrl
->SetSpacing( indent
-5 );
404 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
406 wxTreeItemId item
= m_treeCtrl
->GetSelection();
410 m_treeCtrl
->DoToggleIcon(item
);
413 // MyTreeCtrl implementation
414 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
416 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
417 const wxPoint
& pos
, const wxSize
& size
,
419 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
421 #ifndef NO_VARIABLE_HEIGHT
423 wxImage::AddHandler(new wxJPEGHandler
);
426 image
.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG
);
430 m_reverseSort
= FALSE
;
432 // Make an image list containing small icons
433 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
435 // should correspond to TreeCtrlIcon_xxx enum
436 #if defined(__WXMSW__) && defined(__WIN16__)
437 // This is required in 16-bit Windows mode only because we can't load a specific (16x16)
438 // icon image, so it comes out stretched
439 # ifndef NO_VARIABLE_HEIGHT
440 m_imageListNormal
->Add(image
.ConvertToBitmap());
442 m_imageListNormal
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
444 m_imageListNormal
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
445 m_imageListNormal
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
446 m_imageListNormal
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
447 m_imageListNormal
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
449 # ifndef NO_VARIABLE_HEIGHT
450 m_imageListNormal
->Add(image
.ConvertToBitmap());
452 m_imageListNormal
->Add(wxICON(icon1
));
454 m_imageListNormal
->Add(wxICON(icon2
));
455 m_imageListNormal
->Add(wxICON(icon3
));
456 m_imageListNormal
->Add(wxICON(icon4
));
457 m_imageListNormal
->Add(wxICON(icon5
));
460 SetImageList(m_imageListNormal
);
462 // Add some items to the tree
463 AddTestItemsToTree(3, 2);
466 MyTreeCtrl::~MyTreeCtrl()
468 delete m_imageListNormal
;
471 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
472 const wxTreeItemId
& item2
)
476 // just exchange 1st and 2nd items
477 return wxTreeCtrl::OnCompareItems(item2
, item1
);
481 return wxTreeCtrl::OnCompareItems(item1
, item2
);
485 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
492 bool hasChildren
= depth
> 1;
495 for ( size_t n
= 0; n
< numChildren
; n
++ )
497 // at depth 1 elements won't have any more children
499 str
.Printf("%s child %d", "Folder", n
+ 1);
501 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
503 // here we pass to AppendItem() normal and selected item images (we
504 // suppose that selected image follows the normal one in the enum)
505 int image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
506 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
+ 1,
507 new MyTreeItemData(str
));
509 // and now we also set the expanded one (only for the folders)
512 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
513 wxTreeItemIcon_Expanded
);
516 // remember the last child for OnEnsureVisible()
517 if ( !hasChildren
&& n
== numChildren
- 1 )
522 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
528 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
531 wxTreeItemId rootId
= AddRoot("Root",
532 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
533 new MyTreeItemData("Root item"));
534 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
536 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
539 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
544 id
= GetFirstChild(idParent
, cookie
);
546 id
= GetNextChild(idParent
, cookie
);
551 wxString text
= GetItemText(id
);
554 if (ItemHasChildren(id
))
555 GetItemsRecursively(id
,-1);
557 GetItemsRecursively(idParent
, cookie
);
560 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
562 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
563 : TreeCtrlIcon_Folder
;
565 SetItemImage(item
, image
);
570 #define TREE_EVENT_HANDLER(name) \
571 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
573 wxLogMessage(#name); \
576 TREE_EVENT_HANDLER(OnBeginDrag
)
577 TREE_EVENT_HANDLER(OnBeginRDrag
)
578 TREE_EVENT_HANDLER(OnDeleteItem
)
579 TREE_EVENT_HANDLER(OnGetInfo
)
580 TREE_EVENT_HANDLER(OnSetInfo
)
581 TREE_EVENT_HANDLER(OnItemExpanded
)
582 TREE_EVENT_HANDLER(OnItemExpanding
)
583 TREE_EVENT_HANDLER(OnItemCollapsed
)
584 TREE_EVENT_HANDLER(OnSelChanged
)
585 TREE_EVENT_HANDLER(OnSelChanging
)
587 #undef TREE_EVENT_HANDLER
589 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
591 wxLogMessage("OnBeginLabelEdit");
593 // for testing, prevent this items label editing
594 wxTreeItemId itemId
= event
.GetItem();
595 if ( IsTestItem(itemId
) )
597 wxMessageBox("You can't edit this item.");
603 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
605 wxLogMessage("OnEndLabelEdit");
607 // don't allow anything except letters in the labels
608 if ( !event
.GetLabel().IsWord() )
610 wxMessageBox("The label should contain only letters.");
616 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
618 wxLogMessage("OnItemCollapsing");
620 // for testing, prevent the user from collapsing the first child folder
621 wxTreeItemId itemId
= event
.GetItem();
622 if ( IsTestItem(itemId
) )
624 wxMessageBox("You can't collapse this item.");
630 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
&WXUNUSED(event
))
632 wxLogMessage("OnTreeKeyDown");
635 void MyTreeCtrl::OnItemActivated(wxTreeEvent
&WXUNUSED(event
))
637 // show some info about this item
638 wxTreeItemId itemId
= GetSelection();
639 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
643 item
->ShowInfo(this);
646 wxLogMessage("OnItemActivated");
649 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
651 wxTreeItemId id
= HitTest(event
.GetPosition());
653 wxLogMessage("No item under mouse");
656 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
658 wxLogMessage("Item '%s' under mouse", item
->GetDesc());
662 static inline const char *Bool2String(bool b
)
664 return b
? "" : "not ";
667 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
669 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
670 "%u children (%u immediately under this item).",
672 Bool2String(tree
->IsSelected(GetId())),
673 Bool2String(tree
->IsExpanded(GetId())),
674 Bool2String(tree
->IsBold(GetId())),
675 tree
->GetChildrenCount(GetId()),
676 tree
->GetChildrenCount(GetId(), FALSE
));