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"
39 #define NO_ADVANCED_FEATURES
42 // under Windows the icons are in the .rc file
46 #include "mondrian.xpm"
49 // verify that the item is ok and insult the user if it is not
50 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
51 wxMessageBox("Please select some item first!", \
52 "Tree sample error", \
53 wxOK | wxICON_EXCLAMATION, \
58 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
59 EVT_MENU(TreeTest_Quit
, MyFrame::OnQuit
)
60 EVT_MENU(TreeTest_About
, MyFrame::OnAbout
)
61 EVT_MENU(TreeTest_Dump
, MyFrame::OnDump
)
62 EVT_MENU(TreeTest_Dump_Selected
, MyFrame::OnDumpSelected
)
63 EVT_MENU(TreeTest_Rename
, MyFrame::OnRename
)
64 EVT_MENU(TreeTest_Sort
, MyFrame::OnSort
)
65 EVT_MENU(TreeTest_SortRev
, MyFrame::OnSortRev
)
66 EVT_MENU(TreeTest_Bold
, MyFrame::OnSetBold
)
67 EVT_MENU(TreeTest_UnBold
, MyFrame::OnClearBold
)
68 EVT_MENU(TreeTest_Delete
, MyFrame::OnDelete
)
69 EVT_MENU(TreeTest_DeleteChildren
, MyFrame::OnDeleteChildren
)
70 EVT_MENU(TreeTest_DeleteAll
, MyFrame::OnDeleteAll
)
71 EVT_MENU(TreeTest_Recreate
, MyFrame::OnRecreate
)
72 EVT_MENU(TreeTest_CollapseAndReset
, MyFrame::OnCollapseAndReset
)
73 EVT_MENU(TreeTest_EnsureVisible
, MyFrame::OnEnsureVisible
)
74 EVT_MENU(TreeTest_AddItem
, MyFrame::OnAddItem
)
75 EVT_MENU(TreeTest_IncIndent
, MyFrame::OnIncIndent
)
76 EVT_MENU(TreeTest_DecIndent
, MyFrame::OnDecIndent
)
77 EVT_MENU(TreeTest_IncSpacing
, MyFrame::OnIncSpacing
)
78 EVT_MENU(TreeTest_DecSpacing
, MyFrame::OnDecSpacing
)
81 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
82 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
83 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
84 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
85 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
86 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
87 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
88 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
89 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
90 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
91 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
92 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
93 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
94 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
95 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
96 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
97 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
104 // Create the main frame window
105 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
115 // My frame constructor
116 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
117 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
119 // This reduces flicker effects - even better would be to define
120 // OnEraseBackground to do nothing. When the tree control's scrollbars are
121 // show or hidden, the frame is sent a background erase event.
122 SetBackgroundColour(wxColour(255, 255, 255));
125 SetIcon(wxICON(mondrian
));
128 wxMenu
*file_menu
= new wxMenu
,
129 *tree_menu
= new wxMenu
,
130 *item_menu
= new wxMenu
;
132 file_menu
->Append(TreeTest_About
, "&About...");
133 file_menu
->AppendSeparator();
134 file_menu
->Append(TreeTest_Quit
, "E&xit");
136 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
137 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
138 tree_menu
->AppendSeparator();
139 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
140 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
141 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
142 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
143 tree_menu
->AppendSeparator();
144 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
145 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
146 tree_menu
->AppendSeparator();
147 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
148 tree_menu
->AppendSeparator();
149 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
150 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
151 tree_menu
->AppendSeparator();
152 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
153 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
155 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
156 #ifndef NO_ADVANCED_FEATURES
157 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 #ifndef NO_ADVANCED_FEATURES
177 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
180 wxTextCtrl
*textCtrl
= new wxTextCtrl(this, -1, "",
181 wxDefaultPosition
, wxDefaultSize
,
182 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
184 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
185 c
->top
.SameAs(this, wxTop
);
186 c
->left
.SameAs(this, wxLeft
);
187 c
->right
.SameAs(this, wxRight
);
188 c
->height
.PercentOf(this, wxHeight
, 66);
189 m_treeCtrl
->SetConstraints(c
);
191 c
= new wxLayoutConstraints
;
192 c
->top
.Below(m_treeCtrl
);
193 c
->left
.SameAs(this, wxLeft
);
194 c
->right
.SameAs(this, wxRight
);
195 c
->bottom
.SameAs(this, wxBottom
);
196 textCtrl
->SetConstraints(c
);
199 // create a status bar with 3 panes
201 SetStatusText("", 0);
204 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
205 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
206 delete wxLog::SetActiveTarget(new wxLogStderr
);
208 // set our text control as the log target
209 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
210 delete wxLog::SetActiveTarget(logWindow
);
216 delete wxLog::SetActiveTarget(NULL
);
219 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
224 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
226 wxMessageBox("Tree test sample\n"
227 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
229 wxOK
| wxICON_INFORMATION
, this);
232 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
234 wxTreeItemId item
= m_treeCtrl
->GetSelection();
238 // old code - now we edit in place
240 static wxString s_text
;
241 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
243 if ( !s_text
.IsEmpty() )
245 m_treeCtrl
->SetItemText(item
, s_text
);
249 // TODO demonstrate creating a custom edit control...
250 (void)m_treeCtrl
->EditLabel(item
);
253 void MyFrame::DoSort(bool reverse
)
255 wxTreeItemId item
= m_treeCtrl
->GetSelection();
259 m_treeCtrl
->DoSortChildren(item
, reverse
);
262 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
264 wxTreeItemId root
= m_treeCtrl
->GetSelection();
268 m_treeCtrl
->GetItemsRecursively(root
, -1);
271 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
273 #ifndef NO_ADVANCED_FEATURES
274 wxArrayTreeItemIds array
;
276 m_treeCtrl
->GetSelections(array
);
277 size_t nos
=array
.Count();
278 wxLogMessage(wxString("items selected : ")<< (int)nos
);
280 for (size_t n
=0; n
<nos
; ++n
)
281 wxLogMessage(m_treeCtrl
->GetItemText(array
.Item(n
)));
285 void MyFrame::DoSetBold(bool bold
)
287 wxTreeItemId item
= m_treeCtrl
->GetSelection();
291 m_treeCtrl
->SetItemBold(item
, bold
);
294 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
296 wxTreeItemId item
= m_treeCtrl
->GetSelection();
300 m_treeCtrl
->Delete(item
);
303 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
305 wxTreeItemId item
= m_treeCtrl
->GetSelection();
309 m_treeCtrl
->DeleteChildren(item
);
312 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
314 m_treeCtrl
->DeleteAllItems();
317 void MyFrame::OnRecreate(wxCommandEvent
& event
)
320 m_treeCtrl
->AddTestItemsToTree(3, 2);
323 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
325 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
328 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
330 m_treeCtrl
->DoEnsureVisible();
333 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
335 static int s_num
= 0;
338 text
.Printf("Item #%d", ++s_num
);
340 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
342 MyTreeCtrl::TreeCtrlIcon_File */ );
345 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
347 unsigned int indent
= m_treeCtrl
->GetIndent();
349 m_treeCtrl
->SetIndent( indent
+5 );
352 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
354 unsigned int indent
= m_treeCtrl
->GetIndent();
356 m_treeCtrl
->SetIndent( indent
-5 );
359 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
361 unsigned int indent
= m_treeCtrl
->GetSpacing();
363 m_treeCtrl
->SetSpacing( indent
+5 );
366 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
368 unsigned int indent
= m_treeCtrl
->GetSpacing();
370 m_treeCtrl
->SetSpacing( indent
-5 );
373 // MyTreeCtrl implementation
374 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
376 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
377 const wxPoint
& pos
, const wxSize
& size
,
379 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
381 #if (USE_TR_HAS_VARIABLE_ROW_HIGHT && wxUSE_LIBJPEG)
382 wxImage::AddHandler(new wxJPEGHandler
);
385 image
.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG
);
388 m_reverseSort
= FALSE
;
390 // Make an image list containing small icons
391 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
393 // should correspond to TreeCtrlIcon_xxx enum
394 #if defined(__WXMSW__) && defined(__WIN16__)
395 // This is required in 16-bit Windows mode only because we can't load a specific (16x16)
396 // icon image, so it comes out stretched
397 # if USE_TR_HAS_VARIABLE_ROW_HIGHT
398 m_imageListNormal
->Add(image
.ConvertToBitmap());
400 m_imageListNormal
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
402 m_imageListNormal
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
404 # if USE_TR_HAS_VARIABLE_ROW_HIGHT
405 m_imageListNormal
->Add(image
.ConvertToBitmap());
407 m_imageListNormal
->Add(wxICON(icon1
));
409 m_imageListNormal
->Add(wxICON(icon2
));
412 SetImageList(m_imageListNormal
);
414 // Add some items to the tree
415 AddTestItemsToTree(3, 2);
418 MyTreeCtrl::~MyTreeCtrl()
420 delete m_imageListNormal
;
423 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
424 const wxTreeItemId
& item2
)
428 // just exchange 1st and 2nd items
429 return wxTreeCtrl::OnCompareItems(item2
, item1
);
433 return wxTreeCtrl::OnCompareItems(item1
, item2
);
437 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
445 for ( size_t n
= 0; n
< numChildren
; n
++ )
447 // at depth 1 elements won't have any more children
449 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
451 str
.Printf("%s child %d", "Folder", n
+ 1);
453 // int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
454 int image
= depth
== 1 ? -1 : TreeCtrlIcon_Folder
;
455 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
,
456 new MyTreeItemData(str
));
458 // remember the last child for OnEnsureVisible()
459 if ( depth
== 1 && n
== numChildren
- 1 )
464 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
470 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
473 wxTreeItemId rootId
= AddRoot("Root",
474 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
475 new MyTreeItemData("Root item"));
477 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
480 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
485 id
= GetFirstChild(idParent
, cookie
);
487 id
= GetNextChild(idParent
, cookie
);
492 wxString text
=GetItemText(id
);
495 if (ItemHasChildren(id
))
496 GetItemsRecursively(id
,-1);
498 GetItemsRecursively(idParent
, cookie
);
503 #define TREE_EVENT_HANDLER(name) \
504 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
506 wxLogMessage(#name); \
509 TREE_EVENT_HANDLER(OnBeginDrag
)
510 TREE_EVENT_HANDLER(OnBeginRDrag
)
511 TREE_EVENT_HANDLER(OnDeleteItem
)
512 TREE_EVENT_HANDLER(OnGetInfo
)
513 TREE_EVENT_HANDLER(OnSetInfo
)
514 TREE_EVENT_HANDLER(OnItemExpanded
)
515 TREE_EVENT_HANDLER(OnItemExpanding
)
516 TREE_EVENT_HANDLER(OnItemCollapsed
)
517 TREE_EVENT_HANDLER(OnSelChanged
)
518 TREE_EVENT_HANDLER(OnSelChanging
)
520 #undef TREE_EVENT_HANDLER
522 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
524 wxLogMessage("OnBeginLabelEdit");
526 // for testing, prevent this items label editing
527 wxTreeItemId itemId
= event
.GetItem();
528 if ( IsTestItem(itemId
) )
530 wxMessageBox("You can't edit this item.");
536 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
538 wxLogMessage("OnEndLabelEdit");
540 // don't allow anything except letters in the labels
541 if ( !event
.GetLabel().IsWord() )
543 wxMessageBox("The label should contain only letters.");
549 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
551 wxLogMessage("OnItemCollapsing");
553 // for testing, prevent the user from collapsing the first child folder
554 wxTreeItemId itemId
= event
.GetItem();
555 if ( IsTestItem(itemId
) )
557 wxMessageBox("You can't collapse this item.");
563 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
&WXUNUSED(event
))
565 wxLogMessage("OnTreeKeyDown");
568 void MyTreeCtrl::OnItemActivated(wxTreeEvent
&WXUNUSED(event
))
570 // show some info about this item
571 wxTreeItemId itemId
= GetSelection();
572 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
576 item
->ShowInfo(this);
579 wxLogMessage("OnItemActivated");
582 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
584 wxTreeItemId id
= HitTest(event
.GetPosition());
586 wxLogMessage("No item under mouse");
589 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
591 wxLogMessage("Item '%s' under mouse", item
->GetDesc());
595 static inline const char *Bool2String(bool b
)
597 return b
? "" : "not ";
600 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
602 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
603 "%u children (%u immediately under this item).",
605 Bool2String(tree
->IsSelected(GetId())),
606 Bool2String(tree
->IsExpanded(GetId())),
607 Bool2String(tree
->IsBold(GetId())),
608 tree
->GetChildrenCount(GetId()),
609 tree
->GetChildrenCount(GetId(), FALSE
));