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
45 #ifdef NO_VARIABLE_HEIGHT
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 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
96 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
97 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
98 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
99 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
100 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
101 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
102 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
103 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
104 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
105 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
112 // Create the main frame window
113 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
123 // My frame constructor
124 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
125 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
127 // This reduces flicker effects - even better would be to define
128 // OnEraseBackground to do nothing. When the tree control's scrollbars are
129 // show or hidden, the frame is sent a background erase event.
130 SetBackgroundColour(wxColour(255, 255, 255));
133 SetIcon(wxICON(mondrian
));
136 wxMenu
*file_menu
= new wxMenu
,
137 *tree_menu
= new wxMenu
,
138 *item_menu
= new wxMenu
;
140 file_menu
->Append(TreeTest_About
, "&About...");
141 file_menu
->AppendSeparator();
142 file_menu
->Append(TreeTest_Quit
, "E&xit");
144 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
145 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
146 tree_menu
->AppendSeparator();
147 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
148 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
149 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
150 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
151 tree_menu
->AppendSeparator();
152 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
153 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
154 tree_menu
->AppendSeparator();
155 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
156 tree_menu
->AppendSeparator();
157 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
158 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
159 tree_menu
->AppendSeparator();
160 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
161 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
163 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
164 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
166 item_menu
->AppendSeparator();
167 item_menu
->Append(TreeTest_Bold
, "Make item &bold");
168 item_menu
->Append(TreeTest_UnBold
, "Make item ¬ bold");
169 item_menu
->AppendSeparator();
170 item_menu
->Append(TreeTest_ToggleIcon
, "Toggle the items &icon");
172 #ifndef NO_MULTIPLE_SELECTION
173 item_menu
->AppendSeparator();
174 item_menu
->Append(TreeTest_DumpSelected
, "Dump selected items\tAlt-D");
175 item_menu
->Append(TreeTest_Select
, "Select current item\tAlt-S");
176 item_menu
->Append(TreeTest_Unselect
, "Unselect everything\tAlt-U");
179 wxMenuBar
*menu_bar
= new wxMenuBar
;
180 menu_bar
->Append(file_menu
, "&File");
181 menu_bar
->Append(tree_menu
, "&Tree");
182 menu_bar
->Append(item_menu
, "&Item");
183 SetMenuBar(menu_bar
);
185 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
186 wxDefaultPosition
, wxDefaultSize
,
189 #ifndef NO_MULTIPLE_SELECTION
192 #ifndef NO_VARIABLE_HEIGHT
193 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
196 wxTextCtrl
*textCtrl
= new wxTextCtrl(this, -1, "",
197 wxDefaultPosition
, wxDefaultSize
,
198 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
200 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
201 c
->top
.SameAs(this, wxTop
);
202 c
->left
.SameAs(this, wxLeft
);
203 c
->right
.SameAs(this, wxRight
);
204 c
->height
.PercentOf(this, wxHeight
, 66);
205 m_treeCtrl
->SetConstraints(c
);
207 c
= new wxLayoutConstraints
;
208 c
->top
.Below(m_treeCtrl
);
209 c
->left
.SameAs(this, wxLeft
);
210 c
->right
.SameAs(this, wxRight
);
211 c
->bottom
.SameAs(this, wxBottom
);
212 textCtrl
->SetConstraints(c
);
215 // create a status bar with 3 panes
217 SetStatusText("", 0);
220 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
221 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
222 delete wxLog::SetActiveTarget(new wxLogStderr
);
224 // set our text control as the log target
225 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
226 delete wxLog::SetActiveTarget(logWindow
);
232 delete wxLog::SetActiveTarget(NULL
);
235 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
240 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
242 wxMessageBox("Tree test sample\n"
243 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
245 wxOK
| wxICON_INFORMATION
, this);
248 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
250 wxTreeItemId item
= m_treeCtrl
->GetSelection();
254 // old code - now we edit in place
256 static wxString s_text
;
257 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
259 if ( !s_text
.IsEmpty() )
261 m_treeCtrl
->SetItemText(item
, s_text
);
265 // TODO demonstrate creating a custom edit control...
266 (void)m_treeCtrl
->EditLabel(item
);
269 void MyFrame::DoSort(bool reverse
)
271 wxTreeItemId item
= m_treeCtrl
->GetSelection();
275 m_treeCtrl
->DoSortChildren(item
, reverse
);
278 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
280 wxTreeItemId root
= m_treeCtrl
->GetSelection();
284 m_treeCtrl
->GetItemsRecursively(root
, -1);
287 #ifndef NO_MULTIPLE_SELECTION
289 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
291 wxArrayTreeItemIds array
;
293 size_t count
= m_treeCtrl
->GetSelections(array
);
294 wxLogMessage(_T("%u items selected"), count
);
296 for ( size_t n
= 0; n
< count
; n
++ )
298 wxLogMessage("\t%s", m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
302 void MyFrame::OnSelect(wxCommandEvent
& event
)
304 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
307 void MyFrame::OnUnselect(wxCommandEvent
& event
)
309 m_treeCtrl
->UnselectAll();
312 #endif // NO_MULTIPLE_SELECTION
314 void MyFrame::DoSetBold(bool bold
)
316 wxTreeItemId item
= m_treeCtrl
->GetSelection();
320 m_treeCtrl
->SetItemBold(item
, bold
);
323 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
325 wxTreeItemId item
= m_treeCtrl
->GetSelection();
329 m_treeCtrl
->Delete(item
);
332 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
334 wxTreeItemId item
= m_treeCtrl
->GetSelection();
338 m_treeCtrl
->DeleteChildren(item
);
341 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
343 m_treeCtrl
->DeleteAllItems();
346 void MyFrame::OnRecreate(wxCommandEvent
& event
)
349 m_treeCtrl
->AddTestItemsToTree(3, 2);
352 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
354 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
357 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
359 m_treeCtrl
->DoEnsureVisible();
362 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
364 static int s_num
= 0;
367 text
.Printf("Item #%d", ++s_num
);
369 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
371 MyTreeCtrl::TreeCtrlIcon_File */ );
374 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
376 unsigned int indent
= m_treeCtrl
->GetIndent();
378 m_treeCtrl
->SetIndent( indent
+5 );
381 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
383 unsigned int indent
= m_treeCtrl
->GetIndent();
385 m_treeCtrl
->SetIndent( indent
-5 );
388 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
390 unsigned int indent
= m_treeCtrl
->GetSpacing();
392 m_treeCtrl
->SetSpacing( indent
+5 );
395 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
397 unsigned int indent
= m_treeCtrl
->GetSpacing();
399 m_treeCtrl
->SetSpacing( indent
-5 );
402 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
404 wxTreeItemId item
= m_treeCtrl
->GetSelection();
408 m_treeCtrl
->DoToggleIcon(item
);
411 // MyTreeCtrl implementation
412 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
414 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
415 const wxPoint
& pos
, const wxSize
& size
,
417 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
419 #ifndef NO_VARIABLE_HEIGHT
421 wxImage::AddHandler(new wxJPEGHandler
);
424 image
.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG
);
428 m_reverseSort
= FALSE
;
430 // Make an image list containing small icons
431 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
433 // should correspond to TreeCtrlIcon_xxx enum
434 #if defined(__WXMSW__) && defined(__WIN16__)
435 // This is required in 16-bit Windows mode only because we can't load a specific (16x16)
436 // icon image, so it comes out stretched
437 # ifndef NO_VARIABLE_HEIGHT
438 m_imageListNormal
->Add(image
.ConvertToBitmap());
440 m_imageListNormal
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
442 m_imageListNormal
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
444 # ifndef NO_VARIABLE_HEIGHT
445 m_imageListNormal
->Add(image
.ConvertToBitmap());
447 m_imageListNormal
->Add(wxICON(icon1
));
449 m_imageListNormal
->Add(wxICON(icon2
));
452 SetImageList(m_imageListNormal
);
454 // Add some items to the tree
455 AddTestItemsToTree(3, 2);
458 MyTreeCtrl::~MyTreeCtrl()
460 delete m_imageListNormal
;
463 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
464 const wxTreeItemId
& item2
)
468 // just exchange 1st and 2nd items
469 return wxTreeCtrl::OnCompareItems(item2
, item1
);
473 return wxTreeCtrl::OnCompareItems(item1
, item2
);
477 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
485 for ( size_t n
= 0; n
< numChildren
; n
++ )
487 // at depth 1 elements won't have any more children
489 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
491 str
.Printf("%s child %d", "Folder", n
+ 1);
493 int image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
494 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
,
495 new MyTreeItemData(str
));
497 // remember the last child for OnEnsureVisible()
498 if ( depth
== 1 && n
== numChildren
- 1 )
503 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
509 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
512 wxTreeItemId rootId
= AddRoot("Root",
513 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
514 new MyTreeItemData("Root item"));
516 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
519 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
524 id
= GetFirstChild(idParent
, cookie
);
526 id
= GetNextChild(idParent
, cookie
);
531 wxString text
= GetItemText(id
);
534 if (ItemHasChildren(id
))
535 GetItemsRecursively(id
,-1);
537 GetItemsRecursively(idParent
, cookie
);
540 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
542 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
543 : TreeCtrlIcon_Folder
;
545 SetItemImage(item
, image
);
550 #define TREE_EVENT_HANDLER(name) \
551 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
553 wxLogMessage(#name); \
556 TREE_EVENT_HANDLER(OnBeginDrag
)
557 TREE_EVENT_HANDLER(OnBeginRDrag
)
558 TREE_EVENT_HANDLER(OnDeleteItem
)
559 TREE_EVENT_HANDLER(OnGetInfo
)
560 TREE_EVENT_HANDLER(OnSetInfo
)
561 TREE_EVENT_HANDLER(OnItemExpanded
)
562 TREE_EVENT_HANDLER(OnItemExpanding
)
563 TREE_EVENT_HANDLER(OnItemCollapsed
)
564 TREE_EVENT_HANDLER(OnSelChanged
)
565 TREE_EVENT_HANDLER(OnSelChanging
)
567 #undef TREE_EVENT_HANDLER
569 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
571 wxLogMessage("OnBeginLabelEdit");
573 // for testing, prevent this items label editing
574 wxTreeItemId itemId
= event
.GetItem();
575 if ( IsTestItem(itemId
) )
577 wxMessageBox("You can't edit this item.");
583 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
585 wxLogMessage("OnEndLabelEdit");
587 // don't allow anything except letters in the labels
588 if ( !event
.GetLabel().IsWord() )
590 wxMessageBox("The label should contain only letters.");
596 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
598 wxLogMessage("OnItemCollapsing");
600 // for testing, prevent the user from collapsing the first child folder
601 wxTreeItemId itemId
= event
.GetItem();
602 if ( IsTestItem(itemId
) )
604 wxMessageBox("You can't collapse this item.");
610 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
&WXUNUSED(event
))
612 wxLogMessage("OnTreeKeyDown");
615 void MyTreeCtrl::OnItemActivated(wxTreeEvent
&WXUNUSED(event
))
617 // show some info about this item
618 wxTreeItemId itemId
= GetSelection();
619 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
623 item
->ShowInfo(this);
626 wxLogMessage("OnItemActivated");
629 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
631 wxTreeItemId id
= HitTest(event
.GetPosition());
633 wxLogMessage("No item under mouse");
636 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
638 wxLogMessage("Item '%s' under mouse", item
->GetDesc());
642 static inline const char *Bool2String(bool b
)
644 return b
? "" : "not ";
647 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
649 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
650 "%u children (%u immediately under this item).",
652 Bool2String(tree
->IsSelected(GetId())),
653 Bool2String(tree
->IsExpanded(GetId())),
654 Bool2String(tree
->IsBold(GetId())),
655 tree
->GetChildrenCount(GetId()),
656 tree
->GetChildrenCount(GetId(), FALSE
));