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"
28 // under Windows the icons are in the .rc file
32 #include "mondrian.xpm"
37 #include "wx/imaglist.h"
38 #include "wx/treectrl.h"
44 // verify that the item is ok and insult the user if it is not
45 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
46 wxMessageBox("Please select some item first!", \
47 "Tree sample error", \
48 wxOK | wxICON_EXCLAMATION, \
53 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
54 EVT_MENU(TreeTest_Quit
, MyFrame::OnQuit
)
55 EVT_MENU(TreeTest_About
, MyFrame::OnAbout
)
56 EVT_MENU(TreeTest_Dump
, MyFrame::OnDump
)
57 EVT_MENU(TreeTest_Rename
, MyFrame::OnRename
)
58 EVT_MENU(TreeTest_Sort
, MyFrame::OnSort
)
59 EVT_MENU(TreeTest_SortRev
, MyFrame::OnSortRev
)
60 EVT_MENU(TreeTest_Bold
, MyFrame::OnSetBold
)
61 EVT_MENU(TreeTest_UnBold
, MyFrame::OnClearBold
)
62 EVT_MENU(TreeTest_Delete
, MyFrame::OnDelete
)
63 EVT_MENU(TreeTest_DeleteChildren
, MyFrame::OnDeleteChildren
)
64 EVT_MENU(TreeTest_DeleteAll
, MyFrame::OnDeleteAll
)
65 EVT_MENU(TreeTest_Recreate
, MyFrame::OnRecreate
)
66 EVT_MENU(TreeTest_CollapseAndReset
, MyFrame::OnCollapseAndReset
)
67 EVT_MENU(TreeTest_EnsureVisible
, MyFrame::OnEnsureVisible
)
68 EVT_MENU(TreeTest_AddItem
, MyFrame::OnAddItem
)
69 EVT_MENU(TreeTest_IncIndent
, MyFrame::OnIncIndent
)
70 EVT_MENU(TreeTest_DecIndent
, MyFrame::OnDecIndent
)
71 EVT_MENU(TreeTest_IncSpacing
, MyFrame::OnIncSpacing
)
72 EVT_MENU(TreeTest_DecSpacing
, MyFrame::OnDecSpacing
)
75 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
76 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
77 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
78 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
79 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
80 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
81 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
82 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
83 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
84 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
85 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
86 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
87 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
88 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
89 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
90 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
91 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
98 // Create the main frame window
99 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
109 // My frame constructor
110 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
111 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
113 // This reduces flicker effects - even better would be to define
114 // OnEraseBackground to do nothing. When the tree control's scrollbars are
115 // show or hidden, the frame is sent a background erase event.
116 SetBackgroundColour(wxColour(255, 255, 255));
119 SetIcon(wxICON(mondrian
));
122 wxMenu
*file_menu
= new wxMenu
,
123 *tree_menu
= new wxMenu
,
124 *item_menu
= new wxMenu
;
126 file_menu
->Append(TreeTest_About
, "&About...");
127 file_menu
->AppendSeparator();
128 file_menu
->Append(TreeTest_Quit
, "E&xit");
130 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
131 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
132 tree_menu
->AppendSeparator();
133 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
134 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
135 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
136 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
137 tree_menu
->AppendSeparator();
138 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
139 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
140 tree_menu
->AppendSeparator();
141 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
142 tree_menu
->AppendSeparator();
143 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
144 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
145 tree_menu
->AppendSeparator();
146 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
147 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
149 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
150 item_menu
->AppendSeparator();
151 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
152 item_menu
->AppendSeparator();
153 item_menu
->Append(TreeTest_Bold
, "Make item &bold");
154 item_menu
->Append(TreeTest_UnBold
, "Make item ¬ bold");
156 wxMenuBar
*menu_bar
= new wxMenuBar
;
157 menu_bar
->Append(file_menu
, "&File");
158 menu_bar
->Append(tree_menu
, "&Tree");
159 menu_bar
->Append(item_menu
, "&Item");
160 SetMenuBar(menu_bar
);
162 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
163 wxDefaultPosition
, wxDefaultSize
,
164 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
165 wxTextCtrl
*textCtrl
= new wxTextCtrl(this, -1, "",
166 wxDefaultPosition
, wxDefaultSize
,
167 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
169 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
170 c
->top
.SameAs(this, wxTop
);
171 c
->left
.SameAs(this, wxLeft
);
172 c
->right
.SameAs(this, wxRight
);
173 c
->height
.PercentOf(this, wxHeight
, 66);
174 m_treeCtrl
->SetConstraints(c
);
176 c
= new wxLayoutConstraints
;
177 c
->top
.Below(m_treeCtrl
);
178 c
->left
.SameAs(this, wxLeft
);
179 c
->right
.SameAs(this, wxRight
);
180 c
->bottom
.SameAs(this, wxBottom
);
181 textCtrl
->SetConstraints(c
);
184 // create a status bar with 3 panes
186 SetStatusText("", 0);
189 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
190 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
191 delete wxLog::SetActiveTarget(new wxLogStderr
);
193 // set our text control as the log target
194 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
195 delete wxLog::SetActiveTarget(logWindow
);
201 delete wxLog::SetActiveTarget(NULL
);
204 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
209 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
211 wxMessageBox("Tree test sample\n"
212 "Julian Smart (c) 1997,\n"
213 "Vadim Zeitlin (c) 1998",
215 wxOK
| wxICON_INFORMATION
, this);
218 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
220 wxTreeItemId item
= m_treeCtrl
->GetSelection();
224 static wxString s_text
;
225 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
227 if ( !s_text
.IsEmpty() )
229 m_treeCtrl
->SetItemText(item
, s_text
);
233 void MyFrame::DoSort(bool reverse
)
235 wxTreeItemId item
= m_treeCtrl
->GetSelection();
239 m_treeCtrl
->DoSortChildren(item
, reverse
);
242 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
244 wxTreeItemId root
= m_treeCtrl
->GetSelection();
248 m_treeCtrl
->GetItemsRecursively(root
, -1);
251 void MyFrame::DoSetBold(bool bold
)
253 wxTreeItemId item
= m_treeCtrl
->GetSelection();
257 m_treeCtrl
->SetItemBold(item
, bold
);
260 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
262 wxTreeItemId item
= m_treeCtrl
->GetSelection();
266 m_treeCtrl
->Delete(item
);
269 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
271 wxTreeItemId item
= m_treeCtrl
->GetSelection();
275 m_treeCtrl
->DeleteChildren(item
);
278 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
280 m_treeCtrl
->DeleteAllItems();
283 void MyFrame::OnRecreate(wxCommandEvent
& event
)
286 m_treeCtrl
->AddTestItemsToTree(3, 2);
289 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
291 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
294 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
296 m_treeCtrl
->DoEnsureVisible();
299 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
301 static int s_num
= 0;
304 text
.Printf("Item #%d", ++s_num
);
306 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
308 MyTreeCtrl::TreeCtrlIcon_File
);
311 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
313 unsigned int indent
= m_treeCtrl
->GetIndent();
315 m_treeCtrl
->SetIndent( indent
+5 );
318 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
320 unsigned int indent
= m_treeCtrl
->GetIndent();
322 m_treeCtrl
->SetIndent( indent
-5 );
325 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
327 unsigned int indent
= m_treeCtrl
->GetSpacing();
329 m_treeCtrl
->SetSpacing( indent
+5 );
332 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
334 unsigned int indent
= m_treeCtrl
->GetSpacing();
336 m_treeCtrl
->SetSpacing( indent
-5 );
339 // MyTreeCtrl implementation
340 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
342 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
343 const wxPoint
& pos
, const wxSize
& size
,
345 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
347 m_reverseSort
= FALSE
;
349 // Make an image list containing small icons
350 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
352 // should correspond to TreeCtrlIcon_xxx enum
353 #if defined(__WXMSW__) && defined(__WIN16__)
354 // This is required in 16-bit Windows mode only because we can't load a specific (16x16)
355 // icon image, so it comes out stretched
356 m_imageListNormal
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
357 m_imageListNormal
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
359 m_imageListNormal
->Add(wxICON(icon1
));
360 m_imageListNormal
->Add(wxICON(icon2
));
363 SetImageList(m_imageListNormal
);
365 // Add some items to the tree
366 AddTestItemsToTree(3, 2);
369 MyTreeCtrl::~MyTreeCtrl()
371 delete m_imageListNormal
;
374 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
375 const wxTreeItemId
& item2
)
379 // just exchange 1st and 2nd items
380 return wxTreeCtrl::OnCompareItems(item2
, item1
);
384 return wxTreeCtrl::OnCompareItems(item1
, item2
);
388 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
396 for ( size_t n
= 0; n
< numChildren
; n
++ )
398 // at depth 1 elements won't have any more children
400 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
402 str
.Printf("%s child %d", "Folder", n
+ 1);
404 int image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
405 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
,
406 new MyTreeItemData(str
));
408 // remember the last child for OnEnsureVisible()
409 if ( depth
== 1 && n
== numChildren
- 1 )
414 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
420 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
423 wxTreeItemId rootId
= AddRoot("Root",
424 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
425 new MyTreeItemData("Root item"));
427 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
430 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
435 id
= GetFirstChild(idParent
, cookie
);
437 id
= GetNextChild(idParent
, cookie
);
442 wxString text
=GetItemText(id
);
445 if (ItemHasChildren(id
))
446 GetItemsRecursively(id
,-1);
448 GetItemsRecursively(idParent
, cookie
);
453 #define TREE_EVENT_HANDLER(name) \
454 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
456 wxLogMessage(#name); \
459 TREE_EVENT_HANDLER(OnBeginDrag
)
460 TREE_EVENT_HANDLER(OnBeginRDrag
)
461 TREE_EVENT_HANDLER(OnBeginLabelEdit
)
462 TREE_EVENT_HANDLER(OnEndLabelEdit
)
463 TREE_EVENT_HANDLER(OnDeleteItem
)
464 TREE_EVENT_HANDLER(OnGetInfo
)
465 TREE_EVENT_HANDLER(OnSetInfo
)
466 TREE_EVENT_HANDLER(OnItemExpanded
)
467 TREE_EVENT_HANDLER(OnItemExpanding
)
468 TREE_EVENT_HANDLER(OnItemCollapsed
)
469 TREE_EVENT_HANDLER(OnSelChanged
)
470 TREE_EVENT_HANDLER(OnSelChanging
)
472 #undef TREE_EVENT_HANDLER
474 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
476 wxLogMessage("OnItemCollapsing");
478 // for testing, prevent the user from collapsing the first child folder
479 wxTreeItemId itemId
= event
.GetItem();
480 if ( GetParent(itemId
) == GetRootItem() && !GetPrevSibling(itemId
) )
482 wxMessageBox("You can't collapse this item.");
488 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
&WXUNUSED(event
))
490 wxLogMessage("OnTreeKeyDown");
493 void MyTreeCtrl::OnItemActivated(wxTreeEvent
&WXUNUSED(event
))
495 // show some info about this item
496 wxTreeItemId itemId
= GetSelection();
497 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
501 item
->ShowInfo(this);
504 wxLogMessage("OnItemActivated");
507 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
509 wxTreeItemId id
= HitTest(event
.GetPosition());
511 wxLogMessage("No item under mouse");
514 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
516 wxLogMessage("Item '%s' under mouse", item
->GetDesc());
520 static inline const char *Bool2String(bool b
)
522 return b
? "" : "not ";
525 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
527 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
528 "%u children (%u immediately under this item).",
530 Bool2String(tree
->IsSelected(GetId())),
531 Bool2String(tree
->IsExpanded(GetId())),
532 Bool2String(tree
->IsBold(GetId())),
533 tree
->GetChildrenCount(GetId()),
534 tree
->GetChildrenCount(GetId(), FALSE
));