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"
29 #include "wx/colordlg.h"
32 #include "wx/imaglist.h"
33 #include "wx/treectrl.h"
38 // this is not supported by native control
39 #define NO_VARIABLE_HEIGHT
44 // under Windows the icons are in the .rc file
51 #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(wxT("Please select some item first!"), \
57 wxT("Tree sample error"), \
58 wxOK | wxICON_EXCLAMATION, \
63 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
65 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
66 EVT_SIZE(MyFrame::OnSize
)
75 MENU_LINK(TogHideRoot
)
76 MENU_LINK(TogRootLines
)
78 MENU_LINK(TogFullHighlight
)
79 MENU_LINK(SetFgColour
)
80 MENU_LINK(SetBgColour
)
84 #ifndef NO_MULTIPLE_SELECTION
85 MENU_LINK(DumpSelected
)
89 #endif // NO_MULTIPLE_SELECTION
98 MENU_LINK(DeleteChildren
)
101 MENU_LINK(ToggleImages
)
102 MENU_LINK(ToggleButtons
)
103 MENU_LINK(SetImageSize
)
104 MENU_LINK(CollapseAndReset
)
105 MENU_LINK(EnsureVisible
)
107 MENU_LINK(InsertItem
)
110 MENU_LINK(IncSpacing
)
111 MENU_LINK(DecSpacing
)
112 MENU_LINK(ToggleIcon
)
117 #if USE_GENERIC_TREECTRL
118 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
120 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
122 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
123 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
124 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
125 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
126 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
127 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
128 #if 0 // there are so many of those that logging them causes flicker
129 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
131 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
132 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
133 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
134 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
135 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
136 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRightClick
)
138 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
139 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
140 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
141 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
142 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
143 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
150 // Create the main frame window
151 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
161 // My frame constructor
162 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
163 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)),
164 m_treeCtrl(NULL
), m_textCtrl(NULL
)
166 // This reduces flicker effects - even better would be to define
167 // OnEraseBackground to do nothing. When the tree control's scrollbars are
168 // show or hidden, the frame is sent a background erase event.
169 SetBackgroundColour(wxColour(255, 255, 255));
172 SetIcon(wxICON(mondrian
));
176 wxMenu
*file_menu
= new wxMenu
,
177 *style_menu
= new wxMenu
,
178 *tree_menu
= new wxMenu
,
179 *item_menu
= new wxMenu
;
181 file_menu
->Append(TreeTest_About
, wxT("&About..."));
182 file_menu
->AppendSeparator();
183 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
185 style_menu
->Append(TreeTest_TogButtons
, "Toggle &normal buttons", _T(""), TRUE
);
186 style_menu
->Append(TreeTest_TogTwist
, "Toggle &twister buttons", _T(""), TRUE
);
187 style_menu
->Append(TreeTest_ToggleButtons
, "Toggle image &buttons", _T(""), TRUE
);
188 style_menu
->AppendSeparator();
189 style_menu
->Append(TreeTest_TogLines
, "Toggle &connecting lines", _T(""), TRUE
);
190 style_menu
->Append(TreeTest_TogRootLines
, "Toggle &lines at root", _T(""), TRUE
);
191 style_menu
->Append(TreeTest_TogHideRoot
, "Toggle &hidden root", _T(""), TRUE
);
192 style_menu
->Append(TreeTest_TogBorder
, "Toggle &item border", _T(""), TRUE
);
193 style_menu
->Append(TreeTest_TogFullHighlight
, "Toggle &full row highlight", _T(""), TRUE
);
194 style_menu
->Append(TreeTest_TogEdit
, "Toggle &edit mode", _T(""), TRUE
);
195 #ifndef NO_MULTIPLE_SELECTION
196 style_menu
->Append(TreeTest_ToggleSel
, wxT("Toggle &selection mode"), _T(""), TRUE
);
197 #endif // NO_MULTIPLE_SELECTION
198 style_menu
->Append(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"), _T(""), TRUE
);
199 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
200 style_menu
->AppendSeparator();
201 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
202 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
203 style_menu
->AppendSeparator();
204 style_menu
->Append(TreeTest_ResetStyle
, wxT("&Reset to default\tF10"));
206 tree_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
207 tree_menu
->Append(TreeTest_CollapseAndReset
, "C&ollapse and reset");
208 tree_menu
->AppendSeparator();
209 tree_menu
->Append(TreeTest_AddItem
, "Append a &new item");
210 tree_menu
->Append(TreeTest_InsertItem
, "&Insert a new item");
211 tree_menu
->Append(TreeTest_Delete
, "&Delete this item");
212 tree_menu
->Append(TreeTest_DeleteChildren
, "Delete &children");
213 tree_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
214 tree_menu
->AppendSeparator();
215 tree_menu
->Append(TreeTest_Count
, "Count children of current item");
216 tree_menu
->Append(TreeTest_CountRec
, "Recursively count children of current item");
217 tree_menu
->AppendSeparator();
218 tree_menu
->Append(TreeTest_Sort
, "Sort children of current item");
219 tree_menu
->Append(TreeTest_SortRev
, "Sort in reversed order");
220 tree_menu
->AppendSeparator();
221 tree_menu
->Append(TreeTest_EnsureVisible
, "Make the last item &visible");
222 tree_menu
->AppendSeparator();
223 tree_menu
->Append(TreeTest_IncIndent
, "Add 5 points to indentation\tAlt-I");
224 tree_menu
->Append(TreeTest_DecIndent
, "Reduce indentation by 5 points\tAlt-R");
225 tree_menu
->AppendSeparator();
226 tree_menu
->Append(TreeTest_IncSpacing
, "Add 5 points to spacing\tCtrl-I");
227 tree_menu
->Append(TreeTest_DecSpacing
, "Reduce spacing by 5 points\tCtrl-R");
229 item_menu
->Append(TreeTest_Dump
, "&Dump item children");
230 item_menu
->Append(TreeTest_Rename
, "&Rename item...");
232 item_menu
->AppendSeparator();
233 item_menu
->Append(TreeTest_SetBold
, "Make item &bold");
234 item_menu
->Append(TreeTest_ClearBold
, "Make item ¬ bold");
235 item_menu
->AppendSeparator();
236 item_menu
->Append(TreeTest_ToggleIcon
, "Toggle the item's &icon");
238 #ifndef NO_MULTIPLE_SELECTION
239 item_menu
->AppendSeparator();
240 item_menu
->Append(TreeTest_DumpSelected
, "Dump selected items\tAlt-D");
241 item_menu
->Append(TreeTest_Select
, "Select current item\tAlt-S");
242 item_menu
->Append(TreeTest_Unselect
, "Unselect everything\tAlt-U");
243 #endif // NO_MULTIPLE_SELECTION
245 wxMenuBar
*menu_bar
= new wxMenuBar
;
246 menu_bar
->Append(file_menu
, "&File");
247 menu_bar
->Append(style_menu
, "&Style");
248 menu_bar
->Append(tree_menu
, "&Tree");
249 menu_bar
->Append(item_menu
, "&Item");
250 SetMenuBar(menu_bar
);
251 #endif // wxUSE_MENUS
253 // create the controls
254 m_textCtrl
= new wxTextCtrl(this, -1, "",
255 wxDefaultPosition
, wxDefaultSize
,
256 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
258 CreateTreeWithDefStyle();
260 menu_bar
->Check(TreeTest_ToggleImages
, TRUE
);
262 // create a status bar with 3 panes
264 SetStatusText("", 0);
267 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
268 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
269 delete wxLog::SetActiveTarget(new wxLogStderr
);
271 // set our text control as the log target
272 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
273 delete wxLog::SetActiveTarget(logWindow
);
279 delete wxLog::SetActiveTarget(NULL
);
282 void MyFrame::CreateTreeWithDefStyle()
284 long style
= wxTR_DEFAULT_STYLE
|
285 #ifndef NO_VARIABLE_HEIGHT
286 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
290 CreateTree(style
| wxSUNKEN_BORDER
);
292 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
294 wxMenuBar
*mbar
= GetMenuBar();
295 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_HAS_BUTTONS
) != 0);
296 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_TWIST_BUTTONS
) != 0);
297 mbar
->Check(TreeTest_TogLines
, (style
& wxTR_NO_LINES
) == 0);
298 mbar
->Check(TreeTest_TogRootLines
, (style
& wxTR_LINES_AT_ROOT
) != 0);
299 mbar
->Check(TreeTest_TogHideRoot
, (style
& wxTR_HIDE_ROOT
) != 0);
300 mbar
->Check(TreeTest_TogEdit
, (style
& wxTR_EDIT_LABELS
) != 0);
301 mbar
->Check(TreeTest_TogBorder
, (style
& wxTR_ROW_LINES
) != 0);
302 mbar
->Check(TreeTest_TogFullHighlight
, (style
& wxTR_FULL_ROW_HIGHLIGHT
) != 0);
305 void MyFrame::CreateTree(long style
)
307 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
308 wxDefaultPosition
, wxDefaultSize
,
313 void MyFrame::TogStyle(int id
, long flag
)
315 long style
= m_treeCtrl
->GetWindowStyle() ^ flag
;
317 // most treectrl styles can't be changed on the fly using the native
318 // control and the tree must be recreated
320 m_treeCtrl
->SetWindowStyle(style
);
326 GetMenuBar()->Check(id
, (style
& flag
) != 0);
329 void MyFrame::OnSize(wxSizeEvent
& event
)
331 if ( m_treeCtrl
&& m_textCtrl
)
339 void MyFrame::Resize()
341 wxSize size
= GetClientSize();
342 m_treeCtrl
->SetSize(0, 0, size
.x
, 2*size
.y
/3);
343 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3);
346 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
351 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
353 wxMessageBox("Tree test sample\n"
354 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
356 wxOK
| wxICON_INFORMATION
, this);
359 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
361 wxTreeItemId item
= m_treeCtrl
->GetSelection();
365 // old code - now we edit in place
367 static wxString s_text
;
368 s_text
= wxGetTextFromUser("New name: ", "Tree sample question",
370 if ( !s_text
.IsEmpty() )
372 m_treeCtrl
->SetItemText(item
, s_text
);
376 // TODO demonstrate creating a custom edit control...
377 (void)m_treeCtrl
->EditLabel(item
);
380 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
382 wxTreeItemId item
= m_treeCtrl
->GetSelection();
386 int i
= m_treeCtrl
->GetChildrenCount( item
, FALSE
);
388 wxLogMessage(wxT("%d children"), i
);
391 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
393 wxTreeItemId item
= m_treeCtrl
->GetSelection();
397 int i
= m_treeCtrl
->GetChildrenCount( item
);
399 wxLogMessage(wxT("%d children"), i
);
402 void MyFrame::DoSort(bool reverse
)
404 wxTreeItemId item
= m_treeCtrl
->GetSelection();
408 m_treeCtrl
->DoSortChildren(item
, reverse
);
411 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
413 wxTreeItemId root
= m_treeCtrl
->GetSelection();
417 m_treeCtrl
->GetItemsRecursively(root
, -1);
420 #ifndef NO_MULTIPLE_SELECTION
422 void MyFrame::OnToggleSel(wxCommandEvent
& event
)
424 TogStyle(event
.GetId(), wxTR_MULTIPLE
);
427 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
429 wxArrayTreeItemIds array
;
431 size_t count
= m_treeCtrl
->GetSelections(array
);
432 wxLogMessage(wxT("%u items selected"), count
);
434 for ( size_t n
= 0; n
< count
; n
++ )
436 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
440 void MyFrame::OnSelect(wxCommandEvent
& event
)
442 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
445 void MyFrame::OnUnselect(wxCommandEvent
& event
)
447 m_treeCtrl
->UnselectAll();
450 #endif // NO_MULTIPLE_SELECTION
452 void MyFrame::DoSetBold(bool bold
)
454 wxTreeItemId item
= m_treeCtrl
->GetSelection();
458 m_treeCtrl
->SetItemBold(item
, bold
);
461 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
463 wxTreeItemId item
= m_treeCtrl
->GetSelection();
467 m_treeCtrl
->Delete(item
);
470 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
472 wxTreeItemId item
= m_treeCtrl
->GetSelection();
476 m_treeCtrl
->DeleteChildren(item
);
479 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
481 m_treeCtrl
->DeleteAllItems();
484 void MyFrame::OnRecreate(wxCommandEvent
& event
)
487 m_treeCtrl
->AddTestItemsToTree(5, 2);
490 void MyFrame::OnSetImageSize(wxCommandEvent
& event
)
492 int size
= wxGetNumberFromUser("Enter the size for the images to use",
495 m_treeCtrl
->ImageSize());
499 m_treeCtrl
->CreateImageList(size
);
500 wxGetApp().SetShowImages(TRUE
);
503 void MyFrame::OnToggleImages(wxCommandEvent
& event
)
505 if ( wxGetApp().ShowImages() )
507 m_treeCtrl
->CreateImageList(-1);
508 wxGetApp().SetShowImages(FALSE
);
512 m_treeCtrl
->CreateImageList(0);
513 wxGetApp().SetShowImages(TRUE
);
517 void MyFrame::OnToggleButtons(wxCommandEvent
& event
)
519 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
520 if ( wxGetApp().ShowButtons() )
522 m_treeCtrl
->CreateButtonsImageList(-1);
523 wxGetApp().SetShowButtons(FALSE
);
527 m_treeCtrl
->CreateButtonsImageList(15);
528 wxGetApp().SetShowButtons(TRUE
);
533 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
535 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
538 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
540 m_treeCtrl
->DoEnsureVisible();
543 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
545 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
546 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, "2nd item");
549 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
551 static int s_num
= 0;
554 text
.Printf(wxT("Item #%d"), ++s_num
);
556 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
558 MyTreeCtrl::TreeCtrlIcon_File */ );
561 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
563 unsigned int indent
= m_treeCtrl
->GetIndent();
565 m_treeCtrl
->SetIndent( indent
+5 );
568 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
570 unsigned int indent
= m_treeCtrl
->GetIndent();
572 m_treeCtrl
->SetIndent( indent
-5 );
575 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
577 unsigned int indent
= m_treeCtrl
->GetSpacing();
579 m_treeCtrl
->SetSpacing( indent
+5 );
582 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
584 unsigned int indent
= m_treeCtrl
->GetSpacing();
586 m_treeCtrl
->SetSpacing( indent
-5 );
589 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
591 wxTreeItemId item
= m_treeCtrl
->GetSelection();
595 m_treeCtrl
->DoToggleIcon(item
);
598 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
600 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
602 m_treeCtrl
->SetForegroundColour(col
);
605 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
607 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
609 m_treeCtrl
->SetBackgroundColour(col
);
612 // MyTreeCtrl implementation
613 #if USE_GENERIC_TREECTRL
614 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
616 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
619 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
620 const wxPoint
& pos
, const wxSize
& size
,
622 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
624 m_reverseSort
= FALSE
;
628 // Add some items to the tree
629 AddTestItemsToTree(5, 2);
632 void MyTreeCtrl::CreateImageList(int size
)
644 // Make an image list containing small icons
645 wxImageList
*images
= new wxImageList(size
, size
, TRUE
);
647 // should correspond to TreeCtrlIcon_xxx enum
648 #if defined(__WXMSW__) && defined(__WIN16__)
649 images
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
650 images
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
651 images
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
652 images
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
653 images
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
657 icons
[0] = wxICON(icon1
);
658 icons
[1] = wxICON(icon2
);
659 icons
[2] = wxICON(icon3
);
660 icons
[3] = wxICON(icon4
);
661 icons
[4] = wxICON(icon5
);
663 int sizeOrig
= icons
[0].GetWidth();
664 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
666 if ( size
== sizeOrig
)
668 images
->Add(icons
[i
]);
672 images
->Add(wxImage(icons
[i
]).Rescale(size
, size
).
678 AssignImageList(images
);
681 void MyTreeCtrl::CreateButtonsImageList(int size
)
683 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
686 SetButtonsImageList(NULL
);
690 // Make an image list containing small icons
691 wxImageList
*images
= new wxImageList(size
, size
, TRUE
);
693 // should correspond to TreeCtrlIcon_xxx enum
694 #if defined(__WXMSW__) && defined(__WIN16__)
695 images
->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE
));
696 images
->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE
));
697 images
->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE
));
698 images
->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE
));
699 images
->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE
));
703 icons
[0] = wxICON(icon3
); // closed
704 icons
[1] = wxICON(icon3
); // closed, selected
705 icons
[2] = wxICON(icon5
); // open
706 icons
[3] = wxICON(icon5
); // open, selected
708 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
710 int sizeOrig
= icons
[i
].GetWidth();
711 if ( size
== sizeOrig
)
713 images
->Add(icons
[i
]);
717 images
->Add(wxImage(icons
[i
]).Rescale(size
, size
).
723 AssignButtonsImageList(images
);
727 MyTreeCtrl::~MyTreeCtrl()
731 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
732 const wxTreeItemId
& item2
)
736 // just exchange 1st and 2nd items
737 return wxTreeCtrl::OnCompareItems(item2
, item1
);
741 return wxTreeCtrl::OnCompareItems(item1
, item2
);
745 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
752 bool hasChildren
= depth
> 1;
755 for ( size_t n
= 0; n
< numChildren
; n
++ )
757 // at depth 1 elements won't have any more children
759 str
.Printf(wxT("%s child %d"), wxT("Folder"), n
+ 1);
761 str
.Printf(wxT("%s child %d.%d"), wxT("File"), folder
, n
+ 1);
763 // here we pass to AppendItem() normal and selected item images (we
764 // suppose that selected image follows the normal one in the enum)
766 if ( wxGetApp().ShowImages() )
768 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
769 imageSel
= image
+ 1;
773 image
= imageSel
= -1;
775 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
776 new MyTreeItemData(str
));
778 // and now we also set the expanded one (only for the folders)
779 if ( hasChildren
&& wxGetApp().ShowImages() )
781 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
782 wxTreeItemIcon_Expanded
);
785 // remember the last child for OnEnsureVisible()
786 if ( !hasChildren
&& n
== numChildren
- 1 )
791 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
797 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
800 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
801 wxTreeItemId rootId
= AddRoot("Root",
803 new MyTreeItemData("Root item"));
806 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
809 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
811 // set some colours/fonts for testing
812 SetItemFont(rootId
, *wxITALIC_FONT
);
815 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
816 SetItemTextColour(id
, *wxBLUE
);
818 id
= GetNextChild(rootId
, cookie
);
819 id
= GetNextChild(rootId
, cookie
);
820 SetItemTextColour(id
, *wxRED
);
821 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
824 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
829 id
= GetFirstChild(idParent
, cookie
);
831 id
= GetNextChild(idParent
, cookie
);
836 wxString text
= GetItemText(id
);
839 if (ItemHasChildren(id
))
840 GetItemsRecursively(id
,-1);
842 GetItemsRecursively(idParent
, cookie
);
845 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
847 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
848 : TreeCtrlIcon_Folder
;
850 SetItemImage(item
, image
);
855 #define TREE_EVENT_HANDLER(name) \
856 void MyTreeCtrl::name(wxTreeEvent& event) \
858 wxLogMessage(wxT(#name)); \
862 TREE_EVENT_HANDLER(OnBeginRDrag
)
863 TREE_EVENT_HANDLER(OnDeleteItem
)
864 TREE_EVENT_HANDLER(OnGetInfo
)
865 TREE_EVENT_HANDLER(OnSetInfo
)
866 TREE_EVENT_HANDLER(OnItemExpanded
)
867 TREE_EVENT_HANDLER(OnItemExpanding
)
868 TREE_EVENT_HANDLER(OnItemCollapsed
)
869 TREE_EVENT_HANDLER(OnSelChanged
)
870 TREE_EVENT_HANDLER(OnSelChanging
)
871 TREE_EVENT_HANDLER(OnTreeKeyDown
)
873 #undef TREE_EVENT_HANDLER
875 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
877 // need to explicitly allow drag
878 if ( event
.GetItem() != GetRootItem() )
880 m_draggedItem
= event
.GetItem();
882 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
883 GetItemText(m_draggedItem
).c_str());
889 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
893 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
895 wxTreeItemId itemSrc
= m_draggedItem
,
896 itemDst
= event
.GetItem();
897 m_draggedItem
= (wxTreeItemId
)0l;
899 // where to copy the item?
900 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
902 // copy to the parent then
903 itemDst
= GetParent(itemDst
);
906 if ( !itemDst
.IsOk() )
908 wxLogMessage(wxT("OnEndDrag: can't drop here."));
913 wxString text
= GetItemText(itemSrc
);
914 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
915 text
.c_str(), GetItemText(itemDst
).c_str());
917 // just do append here - we could also insert it just before/after the item
918 // on which it was dropped, but this requires slightly more work... we also
919 // completely ignore the client data and icon of the old item but could
920 // copy them as well.
922 // Finally, we only copy one item here but we might copy the entire tree if
923 // we were dragging a folder.
924 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
925 AppendItem(itemDst
, text
, image
);
928 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
930 wxLogMessage(wxT("OnBeginLabelEdit"));
932 // for testing, prevent this item's label editing
933 wxTreeItemId itemId
= event
.GetItem();
934 if ( IsTestItem(itemId
) )
936 wxMessageBox(wxT("You can't edit this item."));
942 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
944 wxLogMessage(wxT("OnEndLabelEdit"));
946 // don't allow anything except letters in the labels
947 if ( !event
.GetLabel().IsWord() )
949 wxMessageBox(wxT("The label should contain only letters."));
955 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
957 wxLogMessage(wxT("OnItemCollapsing"));
959 // for testing, prevent the user from collapsing the first child folder
960 wxTreeItemId itemId
= event
.GetItem();
961 if ( IsTestItem(itemId
) )
963 wxMessageBox(wxT("You can't collapse this item."));
969 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
971 // show some info about this item
972 wxTreeItemId itemId
= event
.GetItem();
973 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
977 item
->ShowInfo(this);
980 wxLogMessage(wxT("OnItemActivated"));
983 void MyTreeCtrl::OnItemRightClick(wxTreeEvent
& event
)
985 ShowMenu(event
.GetItem(), event
.GetPoint());
988 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
990 wxPoint pt
= event
.GetPosition();
991 ShowMenu(HitTest(pt
), pt
);
994 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
999 title
<< _T("Menu for ") << GetItemText(id
);
1003 title
= _T("Menu for no particular item");
1008 menu
.Append(TreeTest_About
, _T("&About..."));
1009 menu
.Append(TreeTest_Dump
, _T("&Dump"));
1011 PopupMenu(&menu
, pt
);
1012 #endif // wxUSE_MENUS
1015 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
1017 wxTreeItemId id
= HitTest(event
.GetPosition());
1019 wxLogMessage(wxT("No item under mouse"));
1022 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
1024 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1028 static inline const wxChar
*Bool2String(bool b
)
1030 return b
? wxT("") : wxT("not ");
1033 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1035 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1036 wxT("%u children (%u immediately under this item)."),
1038 Bool2String(tree
->IsSelected(GetId())),
1039 Bool2String(tree
->IsExpanded(GetId())),
1040 Bool2String(tree
->IsBold(GetId())),
1041 tree
->GetChildrenCount(GetId()),
1042 tree
->GetChildrenCount(GetId(), FALSE
));