1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/colordlg.h"
25 #include "wx/numdlg.h"
28 #include "wx/imaglist.h"
29 #include "wx/treectrl.h"
33 // this is not supported by native control
34 #define NO_VARIABLE_HEIGHT
46 #include "../sample.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(wxT("Please select some item first!"), \
52 wxT("Tree sample error"), \
53 wxOK | wxICON_EXCLAMATION, \
58 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
60 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
61 EVT_IDLE(MyFrame::OnIdle
)
62 EVT_SIZE(MyFrame::OnSize
)
71 MENU_LINK(TogHideRoot
)
72 MENU_LINK(TogRootLines
)
74 MENU_LINK(TogFullHighlight
)
75 MENU_LINK(SetFgColour
)
76 MENU_LINK(SetBgColour
)
81 #ifndef NO_MULTIPLE_SELECTION
82 MENU_LINK(DumpSelected
)
86 #endif // NO_MULTIPLE_SELECTION
95 MENU_LINK(DeleteChildren
)
98 MENU_LINK(ToggleImages
)
99 MENU_LINK(ToggleButtons
)
100 MENU_LINK(SetImageSize
)
101 MENU_LINK(CollapseAndReset
)
102 MENU_LINK(EnsureVisible
)
104 MENU_LINK(InsertItem
)
107 MENU_LINK(IncSpacing
)
108 MENU_LINK(DecSpacing
)
109 MENU_LINK(ToggleIcon
)
110 MENU_LINK(SelectRoot
)
115 #if USE_GENERIC_TREECTRL
116 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
118 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
120 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
121 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
122 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
123 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
124 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
125 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
126 #if 0 // there are so many of those that logging them causes flicker
127 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
129 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
130 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
131 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
132 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
133 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
135 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
136 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
137 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
138 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
140 // so many differents ways to handle right mouse button clicks...
141 EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu
)
142 // EVT_TREE_ITEM_MENU is the preferred event for creating context menus
143 // on a tree control, because it includes the point of the click or item,
144 // meaning that no additional placement calculations are required.
145 EVT_TREE_ITEM_MENU(TreeTest_Ctrl
, MyTreeCtrl::OnItemMenu
)
146 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRClick
)
148 EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown
)
149 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
150 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
157 if ( !wxApp::OnInit() )
160 // Create the main frame window
161 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
171 // My frame constructor
172 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
173 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
179 // This reduces flicker effects - even better would be to define
180 // OnEraseBackground to do nothing. When the tree control's scrollbars are
181 // show or hidden, the frame is sent a background erase event.
182 SetBackgroundColour(wxColour(255, 255, 255));
185 SetIcon(wxICON(sample
));
189 wxMenu
*file_menu
= new wxMenu
,
190 *style_menu
= new wxMenu
,
191 *tree_menu
= new wxMenu
,
192 *item_menu
= new wxMenu
;
194 file_menu
->Append(TreeTest_About
, wxT("&About..."));
195 file_menu
->AppendSeparator();
196 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
198 style_menu
->AppendCheckItem(TreeTest_TogButtons
, wxT("Toggle &normal buttons"));
199 style_menu
->AppendCheckItem(TreeTest_TogTwist
, wxT("Toggle &twister buttons"));
200 style_menu
->AppendCheckItem(TreeTest_ToggleButtons
, wxT("Toggle image &buttons"));
201 style_menu
->AppendSeparator();
202 style_menu
->AppendCheckItem(TreeTest_TogLines
, wxT("Toggle &connecting lines"));
203 style_menu
->AppendCheckItem(TreeTest_TogRootLines
, wxT("Toggle &lines at root"));
204 style_menu
->AppendCheckItem(TreeTest_TogHideRoot
, wxT("Toggle &hidden root"));
205 style_menu
->AppendCheckItem(TreeTest_TogBorder
, wxT("Toggle &item border"));
206 style_menu
->AppendCheckItem(TreeTest_TogFullHighlight
, wxT("Toggle &full row highlight"));
207 style_menu
->AppendCheckItem(TreeTest_TogEdit
, wxT("Toggle &edit mode"));
208 #ifndef NO_MULTIPLE_SELECTION
209 style_menu
->AppendCheckItem(TreeTest_ToggleSel
, wxT("Toggle &selection mode"));
210 #endif // NO_MULTIPLE_SELECTION
211 style_menu
->AppendCheckItem(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
212 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
213 style_menu
->AppendSeparator();
214 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
215 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
216 style_menu
->AppendSeparator();
217 style_menu
->Append(TreeTest_ResetStyle
, wxT("&Reset to default\tF10"));
219 tree_menu
->Append(TreeTest_Recreate
, wxT("&Recreate the tree"));
220 tree_menu
->Append(TreeTest_CollapseAndReset
, wxT("C&ollapse and reset"));
221 tree_menu
->AppendSeparator();
222 tree_menu
->Append(TreeTest_AddItem
, wxT("Append a &new item"));
223 tree_menu
->Append(TreeTest_InsertItem
, wxT("&Insert a new item"));
224 tree_menu
->Append(TreeTest_Delete
, wxT("&Delete this item"));
225 tree_menu
->Append(TreeTest_DeleteChildren
, wxT("Delete &children"));
226 tree_menu
->Append(TreeTest_DeleteAll
, wxT("Delete &all items"));
227 tree_menu
->Append(TreeTest_SelectRoot
, wxT("Select root item"));
228 tree_menu
->AppendSeparator();
229 tree_menu
->Append(TreeTest_Count
, wxT("Count children of current item"));
230 tree_menu
->Append(TreeTest_CountRec
, wxT("Recursively count children of current item"));
231 tree_menu
->AppendSeparator();
232 tree_menu
->Append(TreeTest_Sort
, wxT("Sort children of current item"));
233 tree_menu
->Append(TreeTest_SortRev
, wxT("Sort in reversed order"));
234 tree_menu
->AppendSeparator();
235 tree_menu
->Append(TreeTest_EnsureVisible
, wxT("Make the last item &visible"));
236 tree_menu
->AppendSeparator();
237 tree_menu
->Append(TreeTest_IncIndent
, wxT("Add 5 points to indentation\tAlt-I"));
238 tree_menu
->Append(TreeTest_DecIndent
, wxT("Reduce indentation by 5 points\tAlt-R"));
239 tree_menu
->AppendSeparator();
240 tree_menu
->Append(TreeTest_IncSpacing
, wxT("Add 5 points to spacing\tCtrl-I"));
241 tree_menu
->Append(TreeTest_DecSpacing
, wxT("Reduce spacing by 5 points\tCtrl-R"));
243 item_menu
->Append(TreeTest_Dump
, wxT("&Dump item children"));
244 item_menu
->Append(TreeTest_Rename
, wxT("&Rename item..."));
246 item_menu
->AppendSeparator();
247 item_menu
->Append(TreeTest_SetBold
, wxT("Make item &bold"));
248 item_menu
->Append(TreeTest_ClearBold
, wxT("Make item ¬ bold"));
249 item_menu
->AppendSeparator();
250 item_menu
->Append(TreeTest_ToggleIcon
, wxT("Toggle the item's &icon"));
252 #ifndef NO_MULTIPLE_SELECTION
253 item_menu
->AppendSeparator();
254 item_menu
->Append(TreeTest_DumpSelected
, wxT("Dump selected items\tAlt-D"));
255 item_menu
->Append(TreeTest_Select
, wxT("Select current item\tAlt-S"));
256 item_menu
->Append(TreeTest_Unselect
, wxT("Unselect everything\tAlt-U"));
257 #endif // NO_MULTIPLE_SELECTION
259 wxMenuBar
*menu_bar
= new wxMenuBar
;
260 menu_bar
->Append(file_menu
, wxT("&File"));
261 menu_bar
->Append(style_menu
, wxT("&Style"));
262 menu_bar
->Append(tree_menu
, wxT("&Tree"));
263 menu_bar
->Append(item_menu
, wxT("&Item"));
264 SetMenuBar(menu_bar
);
265 #endif // wxUSE_MENUS
267 m_panel
= new wxPanel(this);
270 // create the controls
271 m_textCtrl
= new wxTextCtrl(m_panel
, wxID_ANY
, wxT(""),
272 wxDefaultPosition
, wxDefaultSize
,
273 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
276 CreateTreeWithDefStyle();
278 menu_bar
->Check(TreeTest_ToggleImages
, true);
281 // create a status bar
283 #endif // wxUSE_STATUSBAR
287 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
288 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
289 delete wxLog::SetActiveTarget(new wxLogStderr
);
291 // set our text control as the log target
292 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
293 delete wxLog::SetActiveTarget(logWindow
);
301 delete wxLog::SetActiveTarget(NULL
);
305 void MyFrame::CreateTreeWithDefStyle()
307 long style
= wxTR_DEFAULT_STYLE
|
308 #ifndef NO_VARIABLE_HEIGHT
309 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
313 CreateTree(style
| wxSUNKEN_BORDER
);
315 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
317 wxMenuBar
*mbar
= GetMenuBar();
318 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_HAS_BUTTONS
) != 0);
319 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_TWIST_BUTTONS
) != 0);
320 mbar
->Check(TreeTest_TogLines
, (style
& wxTR_NO_LINES
) == 0);
321 mbar
->Check(TreeTest_TogRootLines
, (style
& wxTR_LINES_AT_ROOT
) != 0);
322 mbar
->Check(TreeTest_TogHideRoot
, (style
& wxTR_HIDE_ROOT
) != 0);
323 mbar
->Check(TreeTest_TogEdit
, (style
& wxTR_EDIT_LABELS
) != 0);
324 mbar
->Check(TreeTest_TogBorder
, (style
& wxTR_ROW_LINES
) != 0);
325 mbar
->Check(TreeTest_TogFullHighlight
, (style
& wxTR_FULL_ROW_HIGHLIGHT
) != 0);
328 void MyFrame::CreateTree(long style
)
330 m_treeCtrl
= new MyTreeCtrl(m_panel
, TreeTest_Ctrl
,
331 wxDefaultPosition
, wxDefaultSize
,
336 void MyFrame::TogStyle(int id
, long flag
)
338 long style
= m_treeCtrl
->GetWindowStyle() ^ flag
;
340 // most treectrl styles can't be changed on the fly using the native
341 // control and the tree must be recreated
343 m_treeCtrl
->SetWindowStyle(style
);
349 GetMenuBar()->Check(id
, (style
& flag
) != 0);
352 void MyFrame::OnIdle(wxIdleEvent
& event
)
357 wxTreeItemId idRoot
= m_treeCtrl
->GetRootItem();
359 SetStatusText(wxString::Format
361 _T("Root/last item is %svisible/%svisible"),
362 m_treeCtrl
->IsVisible(idRoot
) ? _T("") : _T("not "),
363 m_treeCtrl
->IsVisible(m_treeCtrl
->GetLastChild(idRoot
))
364 ? _T("") : _T("not ")
367 #endif // wxUSE_STATUSBAR
372 void MyFrame::OnSize(wxSizeEvent
& event
)
386 void MyFrame::Resize()
388 wxSize size
= GetClientSize();
389 m_treeCtrl
->SetSize(0, 0, size
.x
, size
.y
394 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3);
398 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
403 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
405 wxMessageBox(wxT("Tree test sample\n")
406 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
407 wxT("About tree test"),
408 wxOK
| wxICON_INFORMATION
, this);
411 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
413 wxTreeItemId item
= m_treeCtrl
->GetSelection();
417 // old code - now we edit in place
419 static wxString s_text
;
420 s_text
= wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
422 if ( !s_text
.empty() )
424 m_treeCtrl
->SetItemText(item
, s_text
);
428 // TODO demonstrate creating a custom edit control...
429 (void)m_treeCtrl
->EditLabel(item
);
432 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
434 wxTreeItemId item
= m_treeCtrl
->GetSelection();
438 int i
= m_treeCtrl
->GetChildrenCount( item
, false );
440 wxLogMessage(wxT("%d children"), i
);
443 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
445 wxTreeItemId item
= m_treeCtrl
->GetSelection();
449 int i
= m_treeCtrl
->GetChildrenCount( item
);
451 wxLogMessage(wxT("%d children"), i
);
454 void MyFrame::DoSort(bool reverse
)
456 wxTreeItemId item
= m_treeCtrl
->GetSelection();
460 m_treeCtrl
->DoSortChildren(item
, reverse
);
463 void MyFrame::OnHighlight(wxCommandEvent
& WXUNUSED(event
))
465 wxTreeItemId id
= m_treeCtrl
->GetSelection();
470 if ( !m_treeCtrl
->GetBoundingRect(id
, r
, true /* text, not full row */) )
472 wxLogMessage(_T("Failed to get bounding item rect"));
476 wxClientDC
dc(m_treeCtrl
);
478 dc
.SetPen(*wxTRANSPARENT_PEN
);
480 m_treeCtrl
->Update();
483 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
485 wxTreeItemId root
= m_treeCtrl
->GetSelection();
489 m_treeCtrl
->GetItemsRecursively(root
);
492 #ifndef NO_MULTIPLE_SELECTION
494 void MyFrame::OnToggleSel(wxCommandEvent
& event
)
496 TogStyle(event
.GetId(), wxTR_MULTIPLE
);
499 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
501 wxArrayTreeItemIds array
;
503 size_t count
= m_treeCtrl
->GetSelections(array
);
504 wxLogMessage(wxT("%u items selected"), unsigned(count
));
506 for ( size_t n
= 0; n
< count
; n
++ )
508 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
512 void MyFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
514 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
517 void MyFrame::OnSelectRoot(wxCommandEvent
& WXUNUSED(event
))
519 m_treeCtrl
->SelectItem(m_treeCtrl
->GetRootItem());
522 void MyFrame::OnUnselect(wxCommandEvent
& WXUNUSED(event
))
524 m_treeCtrl
->UnselectAll();
527 #endif // NO_MULTIPLE_SELECTION
529 void MyFrame::DoSetBold(bool bold
)
531 wxTreeItemId item
= m_treeCtrl
->GetSelection();
535 m_treeCtrl
->SetItemBold(item
, bold
);
538 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
540 wxTreeItemId item
= m_treeCtrl
->GetSelection();
544 m_treeCtrl
->Delete(item
);
547 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
549 wxTreeItemId item
= m_treeCtrl
->GetSelection();
553 m_treeCtrl
->DeleteChildren(item
);
556 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
558 m_treeCtrl
->DeleteAllItems();
561 void MyFrame::OnRecreate(wxCommandEvent
& event
)
564 m_treeCtrl
->AddTestItemsToTree(5, 2);
567 void MyFrame::OnSetImageSize(wxCommandEvent
& WXUNUSED(event
))
569 int size
= wxGetNumberFromUser(wxT("Enter the size for the images to use"),
571 wxT("TreeCtrl sample"),
572 m_treeCtrl
->ImageSize());
576 m_treeCtrl
->CreateImageList(size
);
577 wxGetApp().SetShowImages(true);
580 void MyFrame::OnToggleImages(wxCommandEvent
& WXUNUSED(event
))
582 if ( wxGetApp().ShowImages() )
584 m_treeCtrl
->CreateImageList(-1);
585 wxGetApp().SetShowImages(false);
589 m_treeCtrl
->CreateImageList(0);
590 wxGetApp().SetShowImages(true);
594 void MyFrame::OnToggleButtons(wxCommandEvent
& WXUNUSED(event
))
596 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
597 if ( wxGetApp().ShowButtons() )
599 m_treeCtrl
->CreateButtonsImageList(-1);
600 wxGetApp().SetShowButtons(false);
604 m_treeCtrl
->CreateButtonsImageList(15);
605 wxGetApp().SetShowButtons(true);
610 void MyFrame::OnCollapseAndReset(wxCommandEvent
& WXUNUSED(event
))
612 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
615 void MyFrame::OnEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
617 m_treeCtrl
->DoEnsureVisible();
620 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
622 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
623 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, wxT("2nd item"));
626 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
628 static int s_num
= 0;
631 text
.Printf(wxT("Item #%d"), ++s_num
);
633 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
635 MyTreeCtrl::TreeCtrlIcon_File */ );
638 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
640 unsigned int indent
= m_treeCtrl
->GetIndent();
642 m_treeCtrl
->SetIndent( indent
+5 );
645 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
647 unsigned int indent
= m_treeCtrl
->GetIndent();
649 m_treeCtrl
->SetIndent( indent
-5 );
652 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
654 unsigned int indent
= m_treeCtrl
->GetSpacing();
656 m_treeCtrl
->SetSpacing( indent
+5 );
659 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
661 unsigned int indent
= m_treeCtrl
->GetSpacing();
663 m_treeCtrl
->SetSpacing( indent
-5 );
666 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
668 wxTreeItemId item
= m_treeCtrl
->GetSelection();
672 m_treeCtrl
->DoToggleIcon(item
);
675 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
677 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
679 m_treeCtrl
->SetForegroundColour(col
);
682 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
684 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
686 m_treeCtrl
->SetBackgroundColour(col
);
689 // MyTreeCtrl implementation
690 #if USE_GENERIC_TREECTRL
691 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
693 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
696 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
697 const wxPoint
& pos
, const wxSize
& size
,
699 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
701 m_reverseSort
= false;
705 // Add some items to the tree
706 AddTestItemsToTree(5, 2);
709 void MyTreeCtrl::CreateImageList(int size
)
721 // Make an image list containing small icons
722 wxImageList
*images
= new wxImageList(size
, size
, true);
724 // should correspond to TreeCtrlIcon_xxx enum
727 icons
[0] = wxIcon(icon1_xpm
);
728 icons
[1] = wxIcon(icon2_xpm
);
729 icons
[2] = wxIcon(icon3_xpm
);
730 icons
[3] = wxIcon(icon4_xpm
);
731 icons
[4] = wxIcon(icon5_xpm
);
733 int sizeOrig
= icons
[0].GetWidth();
734 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
736 if ( size
== sizeOrig
)
738 images
->Add(icons
[i
]);
742 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
746 AssignImageList(images
);
749 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
750 void MyTreeCtrl::CreateButtonsImageList(int size
)
754 SetButtonsImageList(NULL
);
758 // Make an image list containing small icons
759 wxImageList
*images
= new wxImageList(size
, size
, true);
761 // should correspond to TreeCtrlIcon_xxx enum
764 icons
[0] = wxIcon(icon3_xpm
); // closed
765 icons
[1] = wxIcon(icon3_xpm
); // closed, selected
766 icons
[2] = wxIcon(icon5_xpm
); // open
767 icons
[3] = wxIcon(icon5_xpm
); // open, selected
769 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
771 int sizeOrig
= icons
[i
].GetWidth();
772 if ( size
== sizeOrig
)
774 images
->Add(icons
[i
]);
778 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
782 AssignButtonsImageList(images
);
784 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size
))
789 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
790 const wxTreeItemId
& item2
)
794 // just exchange 1st and 2nd items
795 return wxTreeCtrl::OnCompareItems(item2
, item1
);
799 return wxTreeCtrl::OnCompareItems(item1
, item2
);
803 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
810 bool hasChildren
= depth
> 1;
813 for ( size_t n
= 0; n
< numChildren
; n
++ )
815 // at depth 1 elements won't have any more children
817 str
.Printf(wxT("%s child %u"), wxT("Folder"), unsigned(n
+ 1));
819 str
.Printf(wxT("%s child %u.%u"), wxT("File"), unsigned(folder
), unsigned(n
+ 1));
821 // here we pass to AppendItem() normal and selected item images (we
822 // suppose that selected image follows the normal one in the enum)
824 if ( wxGetApp().ShowImages() )
826 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
827 imageSel
= image
+ 1;
831 image
= imageSel
= -1;
833 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
834 new MyTreeItemData(str
));
836 // and now we also set the expanded one (only for the folders)
837 if ( hasChildren
&& wxGetApp().ShowImages() )
839 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
840 wxTreeItemIcon_Expanded
);
843 // remember the last child for OnEnsureVisible()
844 if ( !hasChildren
&& n
== numChildren
- 1 )
849 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
855 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
858 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
859 wxTreeItemId rootId
= AddRoot(wxT("Root"),
861 new MyTreeItemData(wxT("Root item")));
864 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
867 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
869 // set some colours/fonts for testing
870 SetItemFont(rootId
, *wxITALIC_FONT
);
872 wxTreeItemIdValue cookie
;
873 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
874 SetItemTextColour(id
, *wxBLUE
);
876 id
= GetNextChild(rootId
, cookie
);
877 id
= GetNextChild(rootId
, cookie
);
878 SetItemTextColour(id
, *wxRED
);
879 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
882 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
,
883 wxTreeItemIdValue cookie
)
888 id
= GetFirstChild(idParent
, cookie
);
890 id
= GetNextChild(idParent
, cookie
);
895 wxString text
= GetItemText(id
);
898 if (ItemHasChildren(id
))
899 GetItemsRecursively(id
);
901 GetItemsRecursively(idParent
, cookie
);
904 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
906 int image
= (GetItemImage(item
) == TreeCtrlIcon_Folder
)
908 : TreeCtrlIcon_Folder
;
909 SetItemImage(item
, image
, wxTreeItemIcon_Normal
);
911 image
= (GetItemImage(item
) == TreeCtrlIcon_FolderSelected
)
912 ? TreeCtrlIcon_FileSelected
913 : TreeCtrlIcon_FolderSelected
;
914 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
917 void MyTreeCtrl::LogEvent(const wxChar
*name
, const wxTreeEvent
& event
)
919 wxTreeItemId item
= event
.GetItem();
922 text
<< _T('"') << GetItemText(item
).c_str() << _T('"');
924 text
= _T("invalid item");
925 wxLogMessage(wxT("%s(%s)"), name
, text
.c_str());
929 #define TREE_EVENT_HANDLER(name) \
930 void MyTreeCtrl::name(wxTreeEvent& event) \
932 LogEvent(_T(#name), event); \
933 SetLastItem(wxTreeItemId()); \
937 TREE_EVENT_HANDLER(OnBeginRDrag
)
938 TREE_EVENT_HANDLER(OnDeleteItem
)
939 TREE_EVENT_HANDLER(OnGetInfo
)
940 TREE_EVENT_HANDLER(OnSetInfo
)
941 TREE_EVENT_HANDLER(OnItemExpanded
)
942 TREE_EVENT_HANDLER(OnItemExpanding
)
943 TREE_EVENT_HANDLER(OnItemCollapsed
)
944 TREE_EVENT_HANDLER(OnSelChanged
)
945 TREE_EVENT_HANDLER(OnSelChanging
)
947 #undef TREE_EVENT_HANDLER
949 void LogKeyEvent(const wxChar
*name
, const wxKeyEvent
& event
)
952 long keycode
= event
.GetKeyCode();
956 case WXK_BACK
: key
= wxT("BACK"); break;
957 case WXK_TAB
: key
= wxT("TAB"); break;
958 case WXK_RETURN
: key
= wxT("RETURN"); break;
959 case WXK_ESCAPE
: key
= wxT("ESCAPE"); break;
960 case WXK_SPACE
: key
= wxT("SPACE"); break;
961 case WXK_DELETE
: key
= wxT("DELETE"); break;
962 case WXK_START
: key
= wxT("START"); break;
963 case WXK_LBUTTON
: key
= wxT("LBUTTON"); break;
964 case WXK_RBUTTON
: key
= wxT("RBUTTON"); break;
965 case WXK_CANCEL
: key
= wxT("CANCEL"); break;
966 case WXK_MBUTTON
: key
= wxT("MBUTTON"); break;
967 case WXK_CLEAR
: key
= wxT("CLEAR"); break;
968 case WXK_SHIFT
: key
= wxT("SHIFT"); break;
969 case WXK_ALT
: key
= wxT("ALT"); break;
970 case WXK_CONTROL
: key
= wxT("CONTROL"); break;
971 case WXK_MENU
: key
= wxT("MENU"); break;
972 case WXK_PAUSE
: key
= wxT("PAUSE"); break;
973 case WXK_CAPITAL
: key
= wxT("CAPITAL"); break;
974 case WXK_END
: key
= wxT("END"); break;
975 case WXK_HOME
: key
= wxT("HOME"); break;
976 case WXK_LEFT
: key
= wxT("LEFT"); break;
977 case WXK_UP
: key
= wxT("UP"); break;
978 case WXK_RIGHT
: key
= wxT("RIGHT"); break;
979 case WXK_DOWN
: key
= wxT("DOWN"); break;
980 case WXK_SELECT
: key
= wxT("SELECT"); break;
981 case WXK_PRINT
: key
= wxT("PRINT"); break;
982 case WXK_EXECUTE
: key
= wxT("EXECUTE"); break;
983 case WXK_SNAPSHOT
: key
= wxT("SNAPSHOT"); break;
984 case WXK_INSERT
: key
= wxT("INSERT"); break;
985 case WXK_HELP
: key
= wxT("HELP"); break;
986 case WXK_NUMPAD0
: key
= wxT("NUMPAD0"); break;
987 case WXK_NUMPAD1
: key
= wxT("NUMPAD1"); break;
988 case WXK_NUMPAD2
: key
= wxT("NUMPAD2"); break;
989 case WXK_NUMPAD3
: key
= wxT("NUMPAD3"); break;
990 case WXK_NUMPAD4
: key
= wxT("NUMPAD4"); break;
991 case WXK_NUMPAD5
: key
= wxT("NUMPAD5"); break;
992 case WXK_NUMPAD6
: key
= wxT("NUMPAD6"); break;
993 case WXK_NUMPAD7
: key
= wxT("NUMPAD7"); break;
994 case WXK_NUMPAD8
: key
= wxT("NUMPAD8"); break;
995 case WXK_NUMPAD9
: key
= wxT("NUMPAD9"); break;
996 case WXK_MULTIPLY
: key
= wxT("MULTIPLY"); break;
997 case WXK_ADD
: key
= wxT("ADD"); break;
998 case WXK_SEPARATOR
: key
= wxT("SEPARATOR"); break;
999 case WXK_SUBTRACT
: key
= wxT("SUBTRACT"); break;
1000 case WXK_DECIMAL
: key
= wxT("DECIMAL"); break;
1001 case WXK_DIVIDE
: key
= wxT("DIVIDE"); break;
1002 case WXK_F1
: key
= wxT("F1"); break;
1003 case WXK_F2
: key
= wxT("F2"); break;
1004 case WXK_F3
: key
= wxT("F3"); break;
1005 case WXK_F4
: key
= wxT("F4"); break;
1006 case WXK_F5
: key
= wxT("F5"); break;
1007 case WXK_F6
: key
= wxT("F6"); break;
1008 case WXK_F7
: key
= wxT("F7"); break;
1009 case WXK_F8
: key
= wxT("F8"); break;
1010 case WXK_F9
: key
= wxT("F9"); break;
1011 case WXK_F10
: key
= wxT("F10"); break;
1012 case WXK_F11
: key
= wxT("F11"); break;
1013 case WXK_F12
: key
= wxT("F12"); break;
1014 case WXK_F13
: key
= wxT("F13"); break;
1015 case WXK_F14
: key
= wxT("F14"); break;
1016 case WXK_F15
: key
= wxT("F15"); break;
1017 case WXK_F16
: key
= wxT("F16"); break;
1018 case WXK_F17
: key
= wxT("F17"); break;
1019 case WXK_F18
: key
= wxT("F18"); break;
1020 case WXK_F19
: key
= wxT("F19"); break;
1021 case WXK_F20
: key
= wxT("F20"); break;
1022 case WXK_F21
: key
= wxT("F21"); break;
1023 case WXK_F22
: key
= wxT("F22"); break;
1024 case WXK_F23
: key
= wxT("F23"); break;
1025 case WXK_F24
: key
= wxT("F24"); break;
1026 case WXK_NUMLOCK
: key
= wxT("NUMLOCK"); break;
1027 case WXK_SCROLL
: key
= wxT("SCROLL"); break;
1028 case WXK_PAGEUP
: key
= wxT("PAGEUP"); break;
1029 case WXK_PAGEDOWN
: key
= wxT("PAGEDOWN"); break;
1030 case WXK_NUMPAD_SPACE
: key
= wxT("NUMPAD_SPACE"); break;
1031 case WXK_NUMPAD_TAB
: key
= wxT("NUMPAD_TAB"); break;
1032 case WXK_NUMPAD_ENTER
: key
= wxT("NUMPAD_ENTER"); break;
1033 case WXK_NUMPAD_F1
: key
= wxT("NUMPAD_F1"); break;
1034 case WXK_NUMPAD_F2
: key
= wxT("NUMPAD_F2"); break;
1035 case WXK_NUMPAD_F3
: key
= wxT("NUMPAD_F3"); break;
1036 case WXK_NUMPAD_F4
: key
= wxT("NUMPAD_F4"); break;
1037 case WXK_NUMPAD_HOME
: key
= wxT("NUMPAD_HOME"); break;
1038 case WXK_NUMPAD_LEFT
: key
= wxT("NUMPAD_LEFT"); break;
1039 case WXK_NUMPAD_UP
: key
= wxT("NUMPAD_UP"); break;
1040 case WXK_NUMPAD_RIGHT
: key
= wxT("NUMPAD_RIGHT"); break;
1041 case WXK_NUMPAD_DOWN
: key
= wxT("NUMPAD_DOWN"); break;
1042 case WXK_NUMPAD_PAGEUP
: key
= wxT("NUMPAD_PAGEUP"); break;
1043 case WXK_NUMPAD_PAGEDOWN
: key
= wxT("NUMPAD_PAGEDOWN"); break;
1044 case WXK_NUMPAD_END
: key
= wxT("NUMPAD_END"); break;
1045 case WXK_NUMPAD_BEGIN
: key
= wxT("NUMPAD_BEGIN"); break;
1046 case WXK_NUMPAD_INSERT
: key
= wxT("NUMPAD_INSERT"); break;
1047 case WXK_NUMPAD_DELETE
: key
= wxT("NUMPAD_DELETE"); break;
1048 case WXK_NUMPAD_EQUAL
: key
= wxT("NUMPAD_EQUAL"); break;
1049 case WXK_NUMPAD_MULTIPLY
: key
= wxT("NUMPAD_MULTIPLY"); break;
1050 case WXK_NUMPAD_ADD
: key
= wxT("NUMPAD_ADD"); break;
1051 case WXK_NUMPAD_SEPARATOR
: key
= wxT("NUMPAD_SEPARATOR"); break;
1052 case WXK_NUMPAD_SUBTRACT
: key
= wxT("NUMPAD_SUBTRACT"); break;
1053 case WXK_NUMPAD_DECIMAL
: key
= wxT("NUMPAD_DECIMAL"); break;
1057 if ( keycode
< 128 && wxIsprint((int)keycode
) )
1058 key
.Printf(wxT("'%c'"), (char)keycode
);
1059 else if ( keycode
> 0 && keycode
< 27 )
1060 key
.Printf(_("Ctrl-%c"), wxT('A') + keycode
- 1);
1062 key
.Printf(wxT("unknown (%ld)"), keycode
);
1067 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1070 event
.ControlDown() ? wxT('C') : wxT('-'),
1071 event
.AltDown() ? wxT('A') : wxT('-'),
1072 event
.ShiftDown() ? wxT('S') : wxT('-'),
1073 event
.MetaDown() ? wxT('M') : wxT('-'));
1076 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
& event
)
1078 LogKeyEvent(wxT("Tree key down "), event
.GetKeyEvent());
1083 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
1085 // need to explicitly allow drag
1086 if ( event
.GetItem() != GetRootItem() )
1088 m_draggedItem
= event
.GetItem();
1090 wxPoint clientpt
= event
.GetPoint();
1091 wxPoint screenpt
= ClientToScreen(clientpt
);
1093 wxLogMessage(wxT("OnBeginDrag: started dragging %s at screen coords (%i,%i)"),
1094 GetItemText(m_draggedItem
).c_str(),
1095 screenpt
.x
, screenpt
.y
);
1101 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1105 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
1107 wxTreeItemId itemSrc
= m_draggedItem
,
1108 itemDst
= event
.GetItem();
1109 m_draggedItem
= (wxTreeItemId
)0l;
1111 // where to copy the item?
1112 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
1114 // copy to the parent then
1115 itemDst
= GetItemParent(itemDst
);
1118 if ( !itemDst
.IsOk() )
1120 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1125 wxString text
= GetItemText(itemSrc
);
1126 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1127 text
.c_str(), GetItemText(itemDst
).c_str());
1129 // just do append here - we could also insert it just before/after the item
1130 // on which it was dropped, but this requires slightly more work... we also
1131 // completely ignore the client data and icon of the old item but could
1132 // copy them as well.
1134 // Finally, we only copy one item here but we might copy the entire tree if
1135 // we were dragging a folder.
1136 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
1137 AppendItem(itemDst
, text
, image
);
1140 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
1142 wxLogMessage(wxT("OnBeginLabelEdit"));
1144 // for testing, prevent this item's label editing
1145 wxTreeItemId itemId
= event
.GetItem();
1146 if ( IsTestItem(itemId
) )
1148 wxMessageBox(wxT("You can't edit this item."));
1152 else if ( itemId
== GetRootItem() )
1154 // test that it is possible to change the text of the item being edited
1155 SetItemText(itemId
, _T("Editing root item"));
1159 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
1161 wxLogMessage(wxT("OnEndLabelEdit"));
1163 // don't allow anything except letters in the labels
1164 if ( !event
.GetLabel().IsWord() )
1166 wxMessageBox(wxT("The new label should be a single word."));
1172 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
1174 wxLogMessage(wxT("OnItemCollapsing"));
1176 // for testing, prevent the user from collapsing the first child folder
1177 wxTreeItemId itemId
= event
.GetItem();
1178 if ( IsTestItem(itemId
) )
1180 wxMessageBox(wxT("You can't collapse this item."));
1186 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
1188 // show some info about this item
1189 wxTreeItemId itemId
= event
.GetItem();
1190 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1194 item
->ShowInfo(this);
1197 wxLogMessage(wxT("OnItemActivated"));
1200 void MyTreeCtrl::OnItemMenu(wxTreeEvent
& event
)
1202 wxTreeItemId itemId
= event
.GetItem();
1203 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1205 wxPoint clientpt
= event
.GetPoint();
1206 wxPoint screenpt
= ClientToScreen(clientpt
);
1208 wxLogMessage(wxT("OnItemMenu for item \"%s\" at screen coords (%i, %i)"),
1209 item
? item
->GetDesc() : _T(""), screenpt
.x
, screenpt
.y
);
1211 ShowMenu(itemId
, clientpt
);
1215 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1217 wxPoint pt
= event
.GetPosition();
1219 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt
.x
, pt
.y
);
1222 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
1227 title
<< wxT("Menu for ") << GetItemText(id
);
1231 title
= wxT("Menu for no particular item");
1236 menu
.Append(TreeTest_About
, wxT("&About..."));
1237 menu
.AppendSeparator();
1238 menu
.Append(TreeTest_Highlight
, wxT("&Highlight item"));
1239 menu
.Append(TreeTest_Dump
, wxT("&Dump"));
1241 PopupMenu(&menu
, pt
);
1242 #endif // wxUSE_MENUS
1245 void MyTreeCtrl::OnItemRClick(wxTreeEvent
& event
)
1247 wxTreeItemId itemId
= event
.GetItem();
1248 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1251 wxLogMessage(wxT("Item \"%s\" right clicked"), item
? item
->GetDesc()
1257 void MyTreeCtrl::OnRMouseDown(wxMouseEvent
& event
)
1259 wxLogMessage(wxT("Right mouse button down"));
1264 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
1266 wxLogMessage(wxT("Right mouse button up"));
1271 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
1273 wxTreeItemId id
= HitTest(event
.GetPosition());
1275 wxLogMessage(wxT("No item under mouse"));
1278 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
1280 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1286 static inline const wxChar
*Bool2String(bool b
)
1288 return b
? wxT("") : wxT("not ");
1291 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1293 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1294 wxT("%u children (%u immediately under this item)."),
1296 Bool2String(tree
->IsSelected(GetId())),
1297 Bool2String(tree
->IsExpanded(GetId())),
1298 Bool2String(tree
->IsBold(GetId())),
1299 unsigned(tree
->GetChildrenCount(GetId())),
1300 unsigned(tree
->GetChildrenCount(GetId(), false)));