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"
38 #define USE_TR_HAS_VARIABLE_ROW_HIGHT 1
40 // under Windows the icons are in the .rc file
42 #if !USE_TR_HAS_VARIABLE_ROW_HIGHT
46 #include "mondrian.xpm"
50 // verify that the item is ok and insult the user if it is not
51 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
52 wxMessageBox("Please select some item first!", \
53 "Tree sample error", \
54 wxOK | wxICON_EXCLAMATION, \
59 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
60 EVT_MENU(TreeTest_Quit
, MyFrame::OnQuit
)
61 EVT_MENU(TreeTest_About
, MyFrame::OnAbout
)
62 EVT_MENU(TreeTest_Dump
, MyFrame::OnDump
)
63 EVT_MENU(TreeTest_Dump_Selected
, MyFrame::OnDumpSelected
)
64 EVT_MENU(TreeTest_Rename
, MyFrame::OnRename
)
65 EVT_MENU(TreeTest_Sort
, MyFrame::OnSort
)
66 EVT_MENU(TreeTest_SortRev
, MyFrame::OnSortRev
)
67 EVT_MENU(TreeTest_Bold
, MyFrame::OnSetBold
)
68 EVT_MENU(TreeTest_UnBold
, MyFrame::OnClearBold
)
69 EVT_MENU(TreeTest_Delete
, MyFrame::OnDelete
)
70 EVT_MENU(TreeTest_DeleteChildren
, MyFrame::OnDeleteChildren
)
71 EVT_MENU(TreeTest_DeleteAll
, MyFrame::OnDeleteAll
)
72 EVT_MENU(TreeTest_Recreate
, MyFrame::OnRecreate
)
73 EVT_MENU(TreeTest_CollapseAndReset
, MyFrame::OnCollapseAndReset
)
74 EVT_MENU(TreeTest_EnsureVisible
, MyFrame::OnEnsureVisible
)
75 EVT_MENU(TreeTest_AddItem
, MyFrame::OnAddItem
)
76 EVT_MENU(TreeTest_IncIndent
, MyFrame::OnIncIndent
)
77 EVT_MENU(TreeTest_DecIndent
, MyFrame::OnDecIndent
)
78 EVT_MENU(TreeTest_IncSpacing
, MyFrame::OnIncSpacing
)
79 EVT_MENU(TreeTest_DecSpacing
, MyFrame::OnDecSpacing
)
82 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
83 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
84 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
85 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
86 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
87 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
88 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
89 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
90 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
91 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
92 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
93 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
94 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
95 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
96 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
97 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
98 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
105 // Create the main frame window
106 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
116 // My frame constructor
117 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
118 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
120 // This reduces flicker effects - even better would be to define
121 // OnEraseBackground to do nothing. When the tree control's scrollbars are
122 // show or hidden, the frame is sent a background erase event.
123 SetBackgroundColour(wxColour(255, 255, 255));
126 SetIcon(wxICON(mondrian
));
129 wxMenu
*file_menu
= new wxMenu
,
130 *tree_menu
= new wxMenu
,
131 *item_menu
= new wxMenu
;
133 file_menu
->Append(TreeTest_About
, "&About...");
134 file_menu
->AppendSeparator();
135 file_menu
->Append(TreeTest_Quit
, "E&xit");
137 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
138 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
139 tree_menu
->AppendSeparator();
140 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
141 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
142 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
143 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
144 tree_menu
->AppendSeparator();
145 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
146 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
147 tree_menu
->AppendSeparator();
148 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
149 tree_menu
->AppendSeparator();
150 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
151 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
152 tree_menu
->AppendSeparator();
153 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
154 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
156 item_menu
->AppendSeparator();
157 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
158 item_menu
->Append(TreeTest_Dump_Selected
, "Dump selected items\tAlt-S");
159 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
161 item_menu
->AppendSeparator();
162 item_menu
->Append(TreeTest_Bold
, "Make item &bold");
163 item_menu
->Append(TreeTest_UnBold
, "Make item ¬ bold");
165 wxMenuBar
*menu_bar
= new wxMenuBar
;
166 menu_bar
->Append(file_menu
, "&File");
167 menu_bar
->Append(tree_menu
, "&Tree");
168 menu_bar
->Append(item_menu
, "&Item");
169 SetMenuBar(menu_bar
);
171 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
172 wxDefaultPosition
, wxDefaultSize
,
175 #if USE_TR_HAS_VARIABLE_ROW_HIGHT
176 wxTR_HAS_VARIABLE_ROW_HIGHT
|
179 wxTextCtrl
*textCtrl
= new wxTextCtrl(this, -1, "",
180 wxDefaultPosition
, wxDefaultSize
,
181 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
183 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
184 c
->top
.SameAs(this, wxTop
);
185 c
->left
.SameAs(this, wxLeft
);
186 c
->right
.SameAs(this, wxRight
);
187 c
->height
.PercentOf(this, wxHeight
, 66);
188 m_treeCtrl
->SetConstraints(c
);
190 c
= new wxLayoutConstraints
;
191 c
->top
.Below(m_treeCtrl
);
192 c
->left
.SameAs(this, wxLeft
);
193 c
->right
.SameAs(this, wxRight
);
194 c
->bottom
.SameAs(this, wxBottom
);
195 textCtrl
->SetConstraints(c
);
198 // create a status bar with 3 panes
200 SetStatusText("", 0);
203 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
204 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
205 delete wxLog::SetActiveTarget(new wxLogStderr
);
207 // set our text control as the log target
208 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
209 delete wxLog::SetActiveTarget(logWindow
);
215 delete wxLog::SetActiveTarget(NULL
);
218 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
223 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
225 wxMessageBox("Tree test sample\n"
226 "Julian Smart (c) 1997,\n"
227 "Vadim Zeitlin (c) 1998",
229 wxOK
| wxICON_INFORMATION
, this);
232 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
234 wxTreeItemId item
= m_treeCtrl
->GetSelection();
238 static wxString s_text
;
239 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
241 if ( !s_text
.IsEmpty() )
243 m_treeCtrl
->SetItemText(item
, s_text
);
247 void MyFrame::DoSort(bool reverse
)
249 wxTreeItemId item
= m_treeCtrl
->GetSelection();
253 m_treeCtrl
->DoSortChildren(item
, reverse
);
256 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
258 wxTreeItemId root
= m_treeCtrl
->GetSelection();
262 m_treeCtrl
->GetItemsRecursively(root
, -1);
265 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
267 wxArrayTreeItemIds array
;
269 m_treeCtrl
->GetSelections(array
);
270 size_t nos
=array
.Count();
271 wxLogMessage(wxString("items selected : ")<< (int)nos
);
273 for (size_t n
=0; n
<nos
; ++n
)
274 wxLogMessage(m_treeCtrl
->GetItemText(array
.Item(n
)));
277 void MyFrame::DoSetBold(bool bold
)
279 wxTreeItemId item
= m_treeCtrl
->GetSelection();
283 m_treeCtrl
->SetItemBold(item
, bold
);
286 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
288 wxTreeItemId item
= m_treeCtrl
->GetSelection();
292 m_treeCtrl
->Delete(item
);
295 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
297 wxTreeItemId item
= m_treeCtrl
->GetSelection();
301 m_treeCtrl
->DeleteChildren(item
);
304 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
306 m_treeCtrl
->DeleteAllItems();
309 void MyFrame::OnRecreate(wxCommandEvent
& event
)
312 m_treeCtrl
->AddTestItemsToTree(3, 2);
315 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
317 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
320 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
322 m_treeCtrl
->DoEnsureVisible();
325 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
327 static int s_num
= 0;
330 text
.Printf("Item #%d", ++s_num
);
332 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
334 MyTreeCtrl::TreeCtrlIcon_File
);
337 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
339 unsigned int indent
= m_treeCtrl
->GetIndent();
341 m_treeCtrl
->SetIndent( indent
+5 );
344 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
346 unsigned int indent
= m_treeCtrl
->GetIndent();
348 m_treeCtrl
->SetIndent( indent
-5 );
351 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
353 unsigned int indent
= m_treeCtrl
->GetSpacing();
355 m_treeCtrl
->SetSpacing( indent
+5 );
358 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
360 unsigned int indent
= m_treeCtrl
->GetSpacing();
362 m_treeCtrl
->SetSpacing( indent
-5 );
365 // MyTreeCtrl implementation
366 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
368 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
369 const wxPoint
& pos
, const wxSize
& size
,
371 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
373 #if USE_TR_HAS_VARIABLE_ROW_HIGHT
374 wxImage::AddHandler(new wxJPEGHandler
);
377 image
.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG
);
380 m_reverseSort
= FALSE
;
382 // Make an image list containing small icons
383 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
385 // should correspond to TreeCtrlIcon_xxx enum
386 #if defined(__WXMSW__) && defined(__WIN16__)
387 // This is required in 16-bit Windows mode only because we can't load a specific (16x16)
388 // icon image, so it comes out stretched
389 # if USE_TR_HAS_VARIABLE_ROW_HIGHT
390 m_imageListNormal
->Add(image
.ConvertToBitmap());
392 m_imageListNormal
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
394 m_imageListNormal
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
396 # if USE_TR_HAS_VARIABLE_ROW_HIGHT
397 m_imageListNormal
->Add(image
.ConvertToBitmap());
399 m_imageListNormal
->Add(wxICON(icon1
));
401 m_imageListNormal
->Add(wxICON(icon2
));
404 SetImageList(m_imageListNormal
);
406 // Add some items to the tree
407 AddTestItemsToTree(3, 2);
410 MyTreeCtrl::~MyTreeCtrl()
412 delete m_imageListNormal
;
415 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
416 const wxTreeItemId
& item2
)
420 // just exchange 1st and 2nd items
421 return wxTreeCtrl::OnCompareItems(item2
, item1
);
425 return wxTreeCtrl::OnCompareItems(item1
, item2
);
429 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
437 for ( size_t n
= 0; n
< numChildren
; n
++ )
439 // at depth 1 elements won't have any more children
441 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
443 str
.Printf("%s child %d", "Folder", n
+ 1);
445 int image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
446 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
,
447 new MyTreeItemData(str
));
449 // remember the last child for OnEnsureVisible()
450 if ( depth
== 1 && n
== numChildren
- 1 )
455 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
461 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
464 wxTreeItemId rootId
= AddRoot("Root",
465 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
466 new MyTreeItemData("Root item"));
468 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
471 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
476 id
= GetFirstChild(idParent
, cookie
);
478 id
= GetNextChild(idParent
, cookie
);
483 wxString text
=GetItemText(id
);
486 if (ItemHasChildren(id
))
487 GetItemsRecursively(id
,-1);
489 GetItemsRecursively(idParent
, cookie
);
494 #define TREE_EVENT_HANDLER(name) \
495 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
497 wxLogMessage(#name); \
500 TREE_EVENT_HANDLER(OnBeginDrag
)
501 TREE_EVENT_HANDLER(OnBeginRDrag
)
502 TREE_EVENT_HANDLER(OnBeginLabelEdit
)
503 TREE_EVENT_HANDLER(OnEndLabelEdit
)
504 TREE_EVENT_HANDLER(OnDeleteItem
)
505 TREE_EVENT_HANDLER(OnGetInfo
)
506 TREE_EVENT_HANDLER(OnSetInfo
)
507 TREE_EVENT_HANDLER(OnItemExpanded
)
508 TREE_EVENT_HANDLER(OnItemExpanding
)
509 TREE_EVENT_HANDLER(OnItemCollapsed
)
510 TREE_EVENT_HANDLER(OnSelChanged
)
511 TREE_EVENT_HANDLER(OnSelChanging
)
513 #undef TREE_EVENT_HANDLER
515 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
517 wxLogMessage("OnItemCollapsing");
519 // for testing, prevent the user from collapsing the first child folder
520 wxTreeItemId itemId
= event
.GetItem();
521 if ( GetParent(itemId
) == GetRootItem() && !GetPrevSibling(itemId
) )
523 wxMessageBox("You can't collapse this item.");
529 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
&WXUNUSED(event
))
531 wxLogMessage("OnTreeKeyDown");
534 void MyTreeCtrl::OnItemActivated(wxTreeEvent
&WXUNUSED(event
))
536 // show some info about this item
537 wxTreeItemId itemId
= GetSelection();
538 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
542 item
->ShowInfo(this);
545 wxLogMessage("OnItemActivated");
548 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
550 wxTreeItemId id
= HitTest(event
.GetPosition());
552 wxLogMessage("No item under mouse");
555 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
557 wxLogMessage("Item '%s' under mouse", item
->GetDesc());
561 static inline const char *Bool2String(bool b
)
563 return b
? "" : "not ";
566 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
568 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
569 "%u children (%u immediately under this item).",
571 Bool2String(tree
->IsSelected(GetId())),
572 Bool2String(tree
->IsExpanded(GetId())),
573 Bool2String(tree
->IsBold(GetId())),
574 tree
->GetChildrenCount(GetId()),
575 tree
->GetChildrenCount(GetId(), FALSE
));