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 #ifdef wxTR_HAS_VARIABLE_ROW_HIGHT
39 #define USE_TR_HAS_VARIABLE_ROW_HIGHT 1
41 #define USE_TR_HAS_VARIABLE_ROW_HIGHT 0
44 // under Windows the icons are in the .rc file
46 #if !USE_TR_HAS_VARIABLE_ROW_HIGHT
50 #include "mondrian.xpm"
54 // verify that the item is ok and insult the user if it is not
55 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
56 wxMessageBox("Please select some item first!", \
57 "Tree sample error", \
58 wxOK | wxICON_EXCLAMATION, \
63 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
64 EVT_MENU(TreeTest_Quit
, MyFrame::OnQuit
)
65 EVT_MENU(TreeTest_About
, MyFrame::OnAbout
)
66 EVT_MENU(TreeTest_Dump
, MyFrame::OnDump
)
67 EVT_MENU(TreeTest_Dump_Selected
, MyFrame::OnDumpSelected
)
68 EVT_MENU(TreeTest_Rename
, MyFrame::OnRename
)
69 EVT_MENU(TreeTest_Sort
, MyFrame::OnSort
)
70 EVT_MENU(TreeTest_SortRev
, MyFrame::OnSortRev
)
71 EVT_MENU(TreeTest_Bold
, MyFrame::OnSetBold
)
72 EVT_MENU(TreeTest_UnBold
, MyFrame::OnClearBold
)
73 EVT_MENU(TreeTest_Delete
, MyFrame::OnDelete
)
74 EVT_MENU(TreeTest_DeleteChildren
, MyFrame::OnDeleteChildren
)
75 EVT_MENU(TreeTest_DeleteAll
, MyFrame::OnDeleteAll
)
76 EVT_MENU(TreeTest_Recreate
, MyFrame::OnRecreate
)
77 EVT_MENU(TreeTest_CollapseAndReset
, MyFrame::OnCollapseAndReset
)
78 EVT_MENU(TreeTest_EnsureVisible
, MyFrame::OnEnsureVisible
)
79 EVT_MENU(TreeTest_AddItem
, MyFrame::OnAddItem
)
80 EVT_MENU(TreeTest_IncIndent
, MyFrame::OnIncIndent
)
81 EVT_MENU(TreeTest_DecIndent
, MyFrame::OnDecIndent
)
82 EVT_MENU(TreeTest_IncSpacing
, MyFrame::OnIncSpacing
)
83 EVT_MENU(TreeTest_DecSpacing
, MyFrame::OnDecSpacing
)
86 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
87 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
88 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
89 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
90 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
91 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
92 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
93 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
94 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
95 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
96 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
97 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
98 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
99 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
100 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
101 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
102 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
109 // Create the main frame window
110 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
120 // My frame constructor
121 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
122 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
124 // This reduces flicker effects - even better would be to define
125 // OnEraseBackground to do nothing. When the tree control's scrollbars are
126 // show or hidden, the frame is sent a background erase event.
127 SetBackgroundColour(wxColour(255, 255, 255));
130 SetIcon(wxICON(mondrian
));
133 wxMenu
*file_menu
= new wxMenu
,
134 *tree_menu
= new wxMenu
,
135 *item_menu
= new wxMenu
;
137 file_menu
->Append(TreeTest_About
, "&About...");
138 file_menu
->AppendSeparator();
139 file_menu
->Append(TreeTest_Quit
, "E&xit");
141 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
142 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
143 tree_menu
->AppendSeparator();
144 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
145 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
146 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
147 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
148 tree_menu
->AppendSeparator();
149 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
150 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
151 tree_menu
->AppendSeparator();
152 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
153 tree_menu
->AppendSeparator();
154 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
155 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
156 tree_menu
->AppendSeparator();
157 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
158 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
160 item_menu
->AppendSeparator();
161 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
163 item_menu
->Append(TreeTest_Dump_Selected
, "Dump selected items\tAlt-S");
165 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
167 item_menu
->AppendSeparator();
168 item_menu
->Append(TreeTest_Bold
, "Make item &bold");
169 item_menu
->Append(TreeTest_UnBold
, "Make item ¬ bold");
171 wxMenuBar
*menu_bar
= new wxMenuBar
;
172 menu_bar
->Append(file_menu
, "&File");
173 menu_bar
->Append(tree_menu
, "&Tree");
174 menu_bar
->Append(item_menu
, "&Item");
175 SetMenuBar(menu_bar
);
177 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
178 wxDefaultPosition
, wxDefaultSize
,
183 #if USE_TR_HAS_VARIABLE_ROW_HIGHT
184 wxTR_HAS_VARIABLE_ROW_HIGHT
|
187 wxTextCtrl
*textCtrl
= new wxTextCtrl(this, -1, "",
188 wxDefaultPosition
, wxDefaultSize
,
189 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
191 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
192 c
->top
.SameAs(this, wxTop
);
193 c
->left
.SameAs(this, wxLeft
);
194 c
->right
.SameAs(this, wxRight
);
195 c
->height
.PercentOf(this, wxHeight
, 66);
196 m_treeCtrl
->SetConstraints(c
);
198 c
= new wxLayoutConstraints
;
199 c
->top
.Below(m_treeCtrl
);
200 c
->left
.SameAs(this, wxLeft
);
201 c
->right
.SameAs(this, wxRight
);
202 c
->bottom
.SameAs(this, wxBottom
);
203 textCtrl
->SetConstraints(c
);
206 // create a status bar with 3 panes
208 SetStatusText("", 0);
211 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
212 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
213 delete wxLog::SetActiveTarget(new wxLogStderr
);
215 // set our text control as the log target
216 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
217 delete wxLog::SetActiveTarget(logWindow
);
223 delete wxLog::SetActiveTarget(NULL
);
226 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
231 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
233 wxMessageBox("Tree test sample\n"
234 "Julian Smart (c) 1997,\n"
235 "Vadim Zeitlin (c) 1998",
237 wxOK
| wxICON_INFORMATION
, this);
240 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
242 wxTreeItemId item
= m_treeCtrl
->GetSelection();
246 static wxString s_text
;
247 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
249 if ( !s_text
.IsEmpty() )
251 m_treeCtrl
->SetItemText(item
, s_text
);
255 void MyFrame::DoSort(bool reverse
)
257 wxTreeItemId item
= m_treeCtrl
->GetSelection();
261 m_treeCtrl
->DoSortChildren(item
, reverse
);
264 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
266 wxTreeItemId root
= m_treeCtrl
->GetSelection();
270 m_treeCtrl
->GetItemsRecursively(root
, -1);
273 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
276 wxArrayTreeItemIds array
;
278 m_treeCtrl
->GetSelections(array
);
279 size_t nos
=array
.Count();
280 wxLogMessage(wxString("items selected : ")<< (int)nos
);
282 for (size_t n
=0; n
<nos
; ++n
)
283 wxLogMessage(m_treeCtrl
->GetItemText(array
.Item(n
)));
287 void MyFrame::DoSetBold(bool bold
)
289 wxTreeItemId item
= m_treeCtrl
->GetSelection();
293 m_treeCtrl
->SetItemBold(item
, bold
);
296 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
298 wxTreeItemId item
= m_treeCtrl
->GetSelection();
302 m_treeCtrl
->Delete(item
);
305 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
307 wxTreeItemId item
= m_treeCtrl
->GetSelection();
311 m_treeCtrl
->DeleteChildren(item
);
314 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
316 m_treeCtrl
->DeleteAllItems();
319 void MyFrame::OnRecreate(wxCommandEvent
& event
)
322 m_treeCtrl
->AddTestItemsToTree(3, 2);
325 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
327 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
330 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
332 m_treeCtrl
->DoEnsureVisible();
335 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
337 static int s_num
= 0;
340 text
.Printf("Item #%d", ++s_num
);
342 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
344 MyTreeCtrl::TreeCtrlIcon_File
);
347 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
349 unsigned int indent
= m_treeCtrl
->GetIndent();
351 m_treeCtrl
->SetIndent( indent
+5 );
354 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
356 unsigned int indent
= m_treeCtrl
->GetIndent();
358 m_treeCtrl
->SetIndent( indent
-5 );
361 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
363 unsigned int indent
= m_treeCtrl
->GetSpacing();
365 m_treeCtrl
->SetSpacing( indent
+5 );
368 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
370 unsigned int indent
= m_treeCtrl
->GetSpacing();
372 m_treeCtrl
->SetSpacing( indent
-5 );
375 // MyTreeCtrl implementation
376 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
378 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
379 const wxPoint
& pos
, const wxSize
& size
,
381 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
383 #if (USE_TR_HAS_VARIABLE_ROW_HIGHT && wxUSE_LIBJPEG)
384 wxImage::AddHandler(new wxJPEGHandler
);
387 image
.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG
);
390 m_reverseSort
= FALSE
;
392 // Make an image list containing small icons
393 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
395 // should correspond to TreeCtrlIcon_xxx enum
396 #if defined(__WXMSW__) && defined(__WIN16__)
397 // This is required in 16-bit Windows mode only because we can't load a specific (16x16)
398 // icon image, so it comes out stretched
399 # if USE_TR_HAS_VARIABLE_ROW_HIGHT
400 m_imageListNormal
->Add(image
.ConvertToBitmap());
402 m_imageListNormal
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
404 m_imageListNormal
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
406 # if USE_TR_HAS_VARIABLE_ROW_HIGHT
407 m_imageListNormal
->Add(image
.ConvertToBitmap());
409 m_imageListNormal
->Add(wxICON(icon1
));
411 m_imageListNormal
->Add(wxICON(icon2
));
414 SetImageList(m_imageListNormal
);
416 // Add some items to the tree
417 AddTestItemsToTree(3, 2);
420 MyTreeCtrl::~MyTreeCtrl()
422 delete m_imageListNormal
;
425 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
426 const wxTreeItemId
& item2
)
430 // just exchange 1st and 2nd items
431 return wxTreeCtrl::OnCompareItems(item2
, item1
);
435 return wxTreeCtrl::OnCompareItems(item1
, item2
);
439 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
447 for ( size_t n
= 0; n
< numChildren
; n
++ )
449 // at depth 1 elements won't have any more children
451 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
453 str
.Printf("%s child %d", "Folder", n
+ 1);
455 int image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
456 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
,
457 new MyTreeItemData(str
));
459 // remember the last child for OnEnsureVisible()
460 if ( depth
== 1 && n
== numChildren
- 1 )
465 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
471 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
474 wxTreeItemId rootId
= AddRoot("Root",
475 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
476 new MyTreeItemData("Root item"));
478 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
481 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
486 id
= GetFirstChild(idParent
, cookie
);
488 id
= GetNextChild(idParent
, cookie
);
493 wxString text
=GetItemText(id
);
496 if (ItemHasChildren(id
))
497 GetItemsRecursively(id
,-1);
499 GetItemsRecursively(idParent
, cookie
);
504 #define TREE_EVENT_HANDLER(name) \
505 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
507 wxLogMessage(#name); \
510 TREE_EVENT_HANDLER(OnBeginDrag
)
511 TREE_EVENT_HANDLER(OnBeginRDrag
)
512 TREE_EVENT_HANDLER(OnBeginLabelEdit
)
513 TREE_EVENT_HANDLER(OnEndLabelEdit
)
514 TREE_EVENT_HANDLER(OnDeleteItem
)
515 TREE_EVENT_HANDLER(OnGetInfo
)
516 TREE_EVENT_HANDLER(OnSetInfo
)
517 TREE_EVENT_HANDLER(OnItemExpanded
)
518 TREE_EVENT_HANDLER(OnItemExpanding
)
519 TREE_EVENT_HANDLER(OnItemCollapsed
)
520 TREE_EVENT_HANDLER(OnSelChanged
)
521 TREE_EVENT_HANDLER(OnSelChanging
)
523 #undef TREE_EVENT_HANDLER
525 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
527 wxLogMessage("OnItemCollapsing");
529 // for testing, prevent the user from collapsing the first child folder
530 wxTreeItemId itemId
= event
.GetItem();
531 if ( GetParent(itemId
) == GetRootItem() && !GetPrevSibling(itemId
) )
533 wxMessageBox("You can't collapse this item.");
539 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
&WXUNUSED(event
))
541 wxLogMessage("OnTreeKeyDown");
544 void MyTreeCtrl::OnItemActivated(wxTreeEvent
&WXUNUSED(event
))
546 // show some info about this item
547 wxTreeItemId itemId
= GetSelection();
548 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
552 item
->ShowInfo(this);
555 wxLogMessage("OnItemActivated");
558 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
560 wxTreeItemId id
= HitTest(event
.GetPosition());
562 wxLogMessage("No item under mouse");
565 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
567 wxLogMessage("Item '%s' under mouse", item
->GetDesc());
571 static inline const char *Bool2String(bool b
)
573 return b
? "" : "not ";
576 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
578 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
579 "%u children (%u immediately under this item).",
581 Bool2String(tree
->IsSelected(GetId())),
582 Bool2String(tree
->IsExpanded(GetId())),
583 Bool2String(tree
->IsBold(GetId())),
584 tree
->GetChildrenCount(GetId()),
585 tree
->GetChildrenCount(GetId(), FALSE
));