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 // Create the main frame window
158 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
168 // My frame constructor
169 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
170 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
176 // This reduces flicker effects - even better would be to define
177 // OnEraseBackground to do nothing. When the tree control's scrollbars are
178 // show or hidden, the frame is sent a background erase event.
179 SetBackgroundColour(wxColour(255, 255, 255));
182 SetIcon(wxICON(sample
));
186 wxMenu
*file_menu
= new wxMenu
,
187 *style_menu
= new wxMenu
,
188 *tree_menu
= new wxMenu
,
189 *item_menu
= new wxMenu
;
191 file_menu
->Append(TreeTest_About
, wxT("&About..."));
192 file_menu
->AppendSeparator();
193 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
195 style_menu
->AppendCheckItem(TreeTest_TogButtons
, wxT("Toggle &normal buttons"));
196 style_menu
->AppendCheckItem(TreeTest_TogTwist
, wxT("Toggle &twister buttons"));
197 style_menu
->AppendCheckItem(TreeTest_ToggleButtons
, wxT("Toggle image &buttons"));
198 style_menu
->AppendSeparator();
199 style_menu
->AppendCheckItem(TreeTest_TogLines
, wxT("Toggle &connecting lines"));
200 style_menu
->AppendCheckItem(TreeTest_TogRootLines
, wxT("Toggle &lines at root"));
201 style_menu
->AppendCheckItem(TreeTest_TogHideRoot
, wxT("Toggle &hidden root"));
202 style_menu
->AppendCheckItem(TreeTest_TogBorder
, wxT("Toggle &item border"));
203 style_menu
->AppendCheckItem(TreeTest_TogFullHighlight
, wxT("Toggle &full row highlight"));
204 style_menu
->AppendCheckItem(TreeTest_TogEdit
, wxT("Toggle &edit mode"));
205 #ifndef NO_MULTIPLE_SELECTION
206 style_menu
->AppendCheckItem(TreeTest_ToggleSel
, wxT("Toggle &selection mode"));
207 #endif // NO_MULTIPLE_SELECTION
208 style_menu
->AppendCheckItem(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
209 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
210 style_menu
->AppendSeparator();
211 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
212 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
213 style_menu
->AppendSeparator();
214 style_menu
->Append(TreeTest_ResetStyle
, wxT("&Reset to default\tF10"));
216 tree_menu
->Append(TreeTest_Recreate
, wxT("&Recreate the tree"));
217 tree_menu
->Append(TreeTest_CollapseAndReset
, wxT("C&ollapse and reset"));
218 tree_menu
->AppendSeparator();
219 tree_menu
->Append(TreeTest_AddItem
, wxT("Append a &new item"));
220 tree_menu
->Append(TreeTest_InsertItem
, wxT("&Insert a new item"));
221 tree_menu
->Append(TreeTest_Delete
, wxT("&Delete this item"));
222 tree_menu
->Append(TreeTest_DeleteChildren
, wxT("Delete &children"));
223 tree_menu
->Append(TreeTest_DeleteAll
, wxT("Delete &all items"));
224 tree_menu
->Append(TreeTest_SelectRoot
, wxT("Select root item"));
225 tree_menu
->AppendSeparator();
226 tree_menu
->Append(TreeTest_Count
, wxT("Count children of current item"));
227 tree_menu
->Append(TreeTest_CountRec
, wxT("Recursively count children of current item"));
228 tree_menu
->AppendSeparator();
229 tree_menu
->Append(TreeTest_Sort
, wxT("Sort children of current item"));
230 tree_menu
->Append(TreeTest_SortRev
, wxT("Sort in reversed order"));
231 tree_menu
->AppendSeparator();
232 tree_menu
->Append(TreeTest_EnsureVisible
, wxT("Make the last item &visible"));
233 tree_menu
->AppendSeparator();
234 tree_menu
->Append(TreeTest_IncIndent
, wxT("Add 5 points to indentation\tAlt-I"));
235 tree_menu
->Append(TreeTest_DecIndent
, wxT("Reduce indentation by 5 points\tAlt-R"));
236 tree_menu
->AppendSeparator();
237 tree_menu
->Append(TreeTest_IncSpacing
, wxT("Add 5 points to spacing\tCtrl-I"));
238 tree_menu
->Append(TreeTest_DecSpacing
, wxT("Reduce spacing by 5 points\tCtrl-R"));
240 item_menu
->Append(TreeTest_Dump
, wxT("&Dump item children"));
241 item_menu
->Append(TreeTest_Rename
, wxT("&Rename item..."));
243 item_menu
->AppendSeparator();
244 item_menu
->Append(TreeTest_SetBold
, wxT("Make item &bold"));
245 item_menu
->Append(TreeTest_ClearBold
, wxT("Make item ¬ bold"));
246 item_menu
->AppendSeparator();
247 item_menu
->Append(TreeTest_ToggleIcon
, wxT("Toggle the item's &icon"));
249 #ifndef NO_MULTIPLE_SELECTION
250 item_menu
->AppendSeparator();
251 item_menu
->Append(TreeTest_DumpSelected
, wxT("Dump selected items\tAlt-D"));
252 item_menu
->Append(TreeTest_Select
, wxT("Select current item\tAlt-S"));
253 item_menu
->Append(TreeTest_Unselect
, wxT("Unselect everything\tAlt-U"));
254 #endif // NO_MULTIPLE_SELECTION
256 wxMenuBar
*menu_bar
= new wxMenuBar
;
257 menu_bar
->Append(file_menu
, wxT("&File"));
258 menu_bar
->Append(style_menu
, wxT("&Style"));
259 menu_bar
->Append(tree_menu
, wxT("&Tree"));
260 menu_bar
->Append(item_menu
, wxT("&Item"));
261 SetMenuBar(menu_bar
);
262 #endif // wxUSE_MENUS
264 m_panel
= new wxPanel(this);
267 // create the controls
268 m_textCtrl
= new wxTextCtrl(m_panel
, wxID_ANY
, wxT(""),
269 wxDefaultPosition
, wxDefaultSize
,
270 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
273 CreateTreeWithDefStyle();
275 menu_bar
->Check(TreeTest_ToggleImages
, true);
278 // create a status bar
280 #endif // wxUSE_STATUSBAR
284 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
285 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
286 delete wxLog::SetActiveTarget(new wxLogStderr
);
288 // set our text control as the log target
289 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
290 delete wxLog::SetActiveTarget(logWindow
);
298 delete wxLog::SetActiveTarget(NULL
);
302 void MyFrame::CreateTreeWithDefStyle()
304 long style
= wxTR_DEFAULT_STYLE
|
305 #ifndef NO_VARIABLE_HEIGHT
306 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
310 CreateTree(style
| wxSUNKEN_BORDER
);
312 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
314 wxMenuBar
*mbar
= GetMenuBar();
315 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_HAS_BUTTONS
) != 0);
316 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_TWIST_BUTTONS
) != 0);
317 mbar
->Check(TreeTest_TogLines
, (style
& wxTR_NO_LINES
) == 0);
318 mbar
->Check(TreeTest_TogRootLines
, (style
& wxTR_LINES_AT_ROOT
) != 0);
319 mbar
->Check(TreeTest_TogHideRoot
, (style
& wxTR_HIDE_ROOT
) != 0);
320 mbar
->Check(TreeTest_TogEdit
, (style
& wxTR_EDIT_LABELS
) != 0);
321 mbar
->Check(TreeTest_TogBorder
, (style
& wxTR_ROW_LINES
) != 0);
322 mbar
->Check(TreeTest_TogFullHighlight
, (style
& wxTR_FULL_ROW_HIGHLIGHT
) != 0);
325 void MyFrame::CreateTree(long style
)
327 m_treeCtrl
= new MyTreeCtrl(m_panel
, TreeTest_Ctrl
,
328 wxDefaultPosition
, wxDefaultSize
,
333 void MyFrame::TogStyle(int id
, long flag
)
335 long style
= m_treeCtrl
->GetWindowStyle() ^ flag
;
337 // most treectrl styles can't be changed on the fly using the native
338 // control and the tree must be recreated
340 m_treeCtrl
->SetWindowStyle(style
);
346 GetMenuBar()->Check(id
, (style
& flag
) != 0);
349 void MyFrame::OnIdle(wxIdleEvent
& event
)
354 wxTreeItemId idRoot
= m_treeCtrl
->GetRootItem();
356 SetStatusText(wxString::Format
358 _T("Root/last item is %svisible/%svisible"),
359 m_treeCtrl
->IsVisible(idRoot
) ? _T("") : _T("not "),
360 m_treeCtrl
->IsVisible(m_treeCtrl
->GetLastChild(idRoot
))
361 ? _T("") : _T("not ")
364 #endif // wxUSE_STATUSBAR
369 void MyFrame::OnSize(wxSizeEvent
& event
)
383 void MyFrame::Resize()
385 wxSize size
= GetClientSize();
386 m_treeCtrl
->SetSize(0, 0, size
.x
, size
.y
391 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3);
395 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
400 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
402 wxMessageBox(wxT("Tree test sample\n")
403 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
404 wxT("About tree test"),
405 wxOK
| wxICON_INFORMATION
, this);
408 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
410 wxTreeItemId item
= m_treeCtrl
->GetSelection();
414 // old code - now we edit in place
416 static wxString s_text
;
417 s_text
= wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
419 if ( !s_text
.empty() )
421 m_treeCtrl
->SetItemText(item
, s_text
);
425 // TODO demonstrate creating a custom edit control...
426 (void)m_treeCtrl
->EditLabel(item
);
429 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
431 wxTreeItemId item
= m_treeCtrl
->GetSelection();
435 int i
= m_treeCtrl
->GetChildrenCount( item
, false );
437 wxLogMessage(wxT("%d children"), i
);
440 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
442 wxTreeItemId item
= m_treeCtrl
->GetSelection();
446 int i
= m_treeCtrl
->GetChildrenCount( item
);
448 wxLogMessage(wxT("%d children"), i
);
451 void MyFrame::DoSort(bool reverse
)
453 wxTreeItemId item
= m_treeCtrl
->GetSelection();
457 m_treeCtrl
->DoSortChildren(item
, reverse
);
460 void MyFrame::OnHighlight(wxCommandEvent
& WXUNUSED(event
))
462 wxTreeItemId id
= m_treeCtrl
->GetSelection();
467 if ( !m_treeCtrl
->GetBoundingRect(id
, r
, true /* text, not full row */) )
469 wxLogMessage(_T("Failed to get bounding item rect"));
473 wxClientDC
dc(m_treeCtrl
);
475 dc
.SetPen(*wxTRANSPARENT_PEN
);
477 m_treeCtrl
->Update();
480 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
482 wxTreeItemId root
= m_treeCtrl
->GetSelection();
486 m_treeCtrl
->GetItemsRecursively(root
);
489 #ifndef NO_MULTIPLE_SELECTION
491 void MyFrame::OnToggleSel(wxCommandEvent
& event
)
493 TogStyle(event
.GetId(), wxTR_MULTIPLE
);
496 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
498 wxArrayTreeItemIds array
;
500 size_t count
= m_treeCtrl
->GetSelections(array
);
501 wxLogMessage(wxT("%u items selected"), unsigned(count
));
503 for ( size_t n
= 0; n
< count
; n
++ )
505 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
509 void MyFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
511 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
514 void MyFrame::OnSelectRoot(wxCommandEvent
& WXUNUSED(event
))
516 m_treeCtrl
->SelectItem(m_treeCtrl
->GetRootItem());
519 void MyFrame::OnUnselect(wxCommandEvent
& WXUNUSED(event
))
521 m_treeCtrl
->UnselectAll();
524 #endif // NO_MULTIPLE_SELECTION
526 void MyFrame::DoSetBold(bool bold
)
528 wxTreeItemId item
= m_treeCtrl
->GetSelection();
532 m_treeCtrl
->SetItemBold(item
, bold
);
535 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
537 wxTreeItemId item
= m_treeCtrl
->GetSelection();
541 m_treeCtrl
->Delete(item
);
544 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
546 wxTreeItemId item
= m_treeCtrl
->GetSelection();
550 m_treeCtrl
->DeleteChildren(item
);
553 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
555 m_treeCtrl
->DeleteAllItems();
558 void MyFrame::OnRecreate(wxCommandEvent
& event
)
561 m_treeCtrl
->AddTestItemsToTree(5, 2);
564 void MyFrame::OnSetImageSize(wxCommandEvent
& WXUNUSED(event
))
566 int size
= wxGetNumberFromUser(wxT("Enter the size for the images to use"),
568 wxT("TreeCtrl sample"),
569 m_treeCtrl
->ImageSize());
573 m_treeCtrl
->CreateImageList(size
);
574 wxGetApp().SetShowImages(true);
577 void MyFrame::OnToggleImages(wxCommandEvent
& WXUNUSED(event
))
579 if ( wxGetApp().ShowImages() )
581 m_treeCtrl
->CreateImageList(-1);
582 wxGetApp().SetShowImages(false);
586 m_treeCtrl
->CreateImageList(0);
587 wxGetApp().SetShowImages(true);
591 void MyFrame::OnToggleButtons(wxCommandEvent
& WXUNUSED(event
))
593 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
594 if ( wxGetApp().ShowButtons() )
596 m_treeCtrl
->CreateButtonsImageList(-1);
597 wxGetApp().SetShowButtons(false);
601 m_treeCtrl
->CreateButtonsImageList(15);
602 wxGetApp().SetShowButtons(true);
607 void MyFrame::OnCollapseAndReset(wxCommandEvent
& WXUNUSED(event
))
609 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
612 void MyFrame::OnEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
614 m_treeCtrl
->DoEnsureVisible();
617 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
619 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
620 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, wxT("2nd item"));
623 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
625 static int s_num
= 0;
628 text
.Printf(wxT("Item #%d"), ++s_num
);
630 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
632 MyTreeCtrl::TreeCtrlIcon_File */ );
635 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
637 unsigned int indent
= m_treeCtrl
->GetIndent();
639 m_treeCtrl
->SetIndent( indent
+5 );
642 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
644 unsigned int indent
= m_treeCtrl
->GetIndent();
646 m_treeCtrl
->SetIndent( indent
-5 );
649 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
651 unsigned int indent
= m_treeCtrl
->GetSpacing();
653 m_treeCtrl
->SetSpacing( indent
+5 );
656 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
658 unsigned int indent
= m_treeCtrl
->GetSpacing();
660 m_treeCtrl
->SetSpacing( indent
-5 );
663 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
665 wxTreeItemId item
= m_treeCtrl
->GetSelection();
669 m_treeCtrl
->DoToggleIcon(item
);
672 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
674 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
676 m_treeCtrl
->SetForegroundColour(col
);
679 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
681 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
683 m_treeCtrl
->SetBackgroundColour(col
);
686 // MyTreeCtrl implementation
687 #if USE_GENERIC_TREECTRL
688 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
690 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
693 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
694 const wxPoint
& pos
, const wxSize
& size
,
696 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
698 m_reverseSort
= false;
702 // Add some items to the tree
703 AddTestItemsToTree(5, 2);
706 void MyTreeCtrl::CreateImageList(int size
)
718 // Make an image list containing small icons
719 wxImageList
*images
= new wxImageList(size
, size
, true);
721 // should correspond to TreeCtrlIcon_xxx enum
724 icons
[0] = wxIcon(icon1_xpm
);
725 icons
[1] = wxIcon(icon2_xpm
);
726 icons
[2] = wxIcon(icon3_xpm
);
727 icons
[3] = wxIcon(icon4_xpm
);
728 icons
[4] = wxIcon(icon5_xpm
);
730 int sizeOrig
= icons
[0].GetWidth();
731 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
733 if ( size
== sizeOrig
)
735 images
->Add(icons
[i
]);
739 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
743 AssignImageList(images
);
746 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
747 void MyTreeCtrl::CreateButtonsImageList(int size
)
751 SetButtonsImageList(NULL
);
755 // Make an image list containing small icons
756 wxImageList
*images
= new wxImageList(size
, size
, true);
758 // should correspond to TreeCtrlIcon_xxx enum
761 icons
[0] = wxIcon(icon3_xpm
); // closed
762 icons
[1] = wxIcon(icon3_xpm
); // closed, selected
763 icons
[2] = wxIcon(icon5_xpm
); // open
764 icons
[3] = wxIcon(icon5_xpm
); // open, selected
766 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
768 int sizeOrig
= icons
[i
].GetWidth();
769 if ( size
== sizeOrig
)
771 images
->Add(icons
[i
]);
775 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
779 AssignButtonsImageList(images
);
781 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size
))
786 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
787 const wxTreeItemId
& item2
)
791 // just exchange 1st and 2nd items
792 return wxTreeCtrl::OnCompareItems(item2
, item1
);
796 return wxTreeCtrl::OnCompareItems(item1
, item2
);
800 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
807 bool hasChildren
= depth
> 1;
810 for ( size_t n
= 0; n
< numChildren
; n
++ )
812 // at depth 1 elements won't have any more children
814 str
.Printf(wxT("%s child %u"), wxT("Folder"), unsigned(n
+ 1));
816 str
.Printf(wxT("%s child %u.%u"), wxT("File"), unsigned(folder
), unsigned(n
+ 1));
818 // here we pass to AppendItem() normal and selected item images (we
819 // suppose that selected image follows the normal one in the enum)
821 if ( wxGetApp().ShowImages() )
823 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
824 imageSel
= image
+ 1;
828 image
= imageSel
= -1;
830 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
831 new MyTreeItemData(str
));
833 // and now we also set the expanded one (only for the folders)
834 if ( hasChildren
&& wxGetApp().ShowImages() )
836 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
837 wxTreeItemIcon_Expanded
);
840 // remember the last child for OnEnsureVisible()
841 if ( !hasChildren
&& n
== numChildren
- 1 )
846 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
852 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
855 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
856 wxTreeItemId rootId
= AddRoot(wxT("Root"),
858 new MyTreeItemData(wxT("Root item")));
861 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
864 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
866 // set some colours/fonts for testing
867 SetItemFont(rootId
, *wxITALIC_FONT
);
869 wxTreeItemIdValue cookie
;
870 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
871 SetItemTextColour(id
, *wxBLUE
);
873 id
= GetNextChild(rootId
, cookie
);
874 id
= GetNextChild(rootId
, cookie
);
875 SetItemTextColour(id
, *wxRED
);
876 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
879 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
,
880 wxTreeItemIdValue cookie
)
885 id
= GetFirstChild(idParent
, cookie
);
887 id
= GetNextChild(idParent
, cookie
);
892 wxString text
= GetItemText(id
);
895 if (ItemHasChildren(id
))
896 GetItemsRecursively(id
);
898 GetItemsRecursively(idParent
, cookie
);
901 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
903 int image
= (GetItemImage(item
) == TreeCtrlIcon_Folder
)
905 : TreeCtrlIcon_Folder
;
906 SetItemImage(item
, image
, wxTreeItemIcon_Normal
);
908 image
= (GetItemImage(item
) == TreeCtrlIcon_FolderSelected
)
909 ? TreeCtrlIcon_FileSelected
910 : TreeCtrlIcon_FolderSelected
;
911 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
914 void MyTreeCtrl::LogEvent(const wxChar
*name
, const wxTreeEvent
& event
)
916 wxTreeItemId item
= event
.GetItem();
919 text
<< _T('"') << GetItemText(item
).c_str() << _T('"');
921 text
= _T("invalid item");
922 wxLogMessage(wxT("%s(%s)"), name
, text
.c_str());
926 #define TREE_EVENT_HANDLER(name) \
927 void MyTreeCtrl::name(wxTreeEvent& event) \
929 LogEvent(_T(#name), event); \
930 SetLastItem(wxTreeItemId()); \
934 TREE_EVENT_HANDLER(OnBeginRDrag
)
935 TREE_EVENT_HANDLER(OnDeleteItem
)
936 TREE_EVENT_HANDLER(OnGetInfo
)
937 TREE_EVENT_HANDLER(OnSetInfo
)
938 TREE_EVENT_HANDLER(OnItemExpanded
)
939 TREE_EVENT_HANDLER(OnItemExpanding
)
940 TREE_EVENT_HANDLER(OnItemCollapsed
)
941 TREE_EVENT_HANDLER(OnSelChanged
)
942 TREE_EVENT_HANDLER(OnSelChanging
)
944 #undef TREE_EVENT_HANDLER
946 void LogKeyEvent(const wxChar
*name
, const wxKeyEvent
& event
)
949 long keycode
= event
.GetKeyCode();
953 case WXK_BACK
: key
= wxT("BACK"); break;
954 case WXK_TAB
: key
= wxT("TAB"); break;
955 case WXK_RETURN
: key
= wxT("RETURN"); break;
956 case WXK_ESCAPE
: key
= wxT("ESCAPE"); break;
957 case WXK_SPACE
: key
= wxT("SPACE"); break;
958 case WXK_DELETE
: key
= wxT("DELETE"); break;
959 case WXK_START
: key
= wxT("START"); break;
960 case WXK_LBUTTON
: key
= wxT("LBUTTON"); break;
961 case WXK_RBUTTON
: key
= wxT("RBUTTON"); break;
962 case WXK_CANCEL
: key
= wxT("CANCEL"); break;
963 case WXK_MBUTTON
: key
= wxT("MBUTTON"); break;
964 case WXK_CLEAR
: key
= wxT("CLEAR"); break;
965 case WXK_SHIFT
: key
= wxT("SHIFT"); break;
966 case WXK_ALT
: key
= wxT("ALT"); break;
967 case WXK_CONTROL
: key
= wxT("CONTROL"); break;
968 case WXK_MENU
: key
= wxT("MENU"); break;
969 case WXK_PAUSE
: key
= wxT("PAUSE"); break;
970 case WXK_CAPITAL
: key
= wxT("CAPITAL"); break;
971 case WXK_END
: key
= wxT("END"); break;
972 case WXK_HOME
: key
= wxT("HOME"); break;
973 case WXK_LEFT
: key
= wxT("LEFT"); break;
974 case WXK_UP
: key
= wxT("UP"); break;
975 case WXK_RIGHT
: key
= wxT("RIGHT"); break;
976 case WXK_DOWN
: key
= wxT("DOWN"); break;
977 case WXK_SELECT
: key
= wxT("SELECT"); break;
978 case WXK_PRINT
: key
= wxT("PRINT"); break;
979 case WXK_EXECUTE
: key
= wxT("EXECUTE"); break;
980 case WXK_SNAPSHOT
: key
= wxT("SNAPSHOT"); break;
981 case WXK_INSERT
: key
= wxT("INSERT"); break;
982 case WXK_HELP
: key
= wxT("HELP"); break;
983 case WXK_NUMPAD0
: key
= wxT("NUMPAD0"); break;
984 case WXK_NUMPAD1
: key
= wxT("NUMPAD1"); break;
985 case WXK_NUMPAD2
: key
= wxT("NUMPAD2"); break;
986 case WXK_NUMPAD3
: key
= wxT("NUMPAD3"); break;
987 case WXK_NUMPAD4
: key
= wxT("NUMPAD4"); break;
988 case WXK_NUMPAD5
: key
= wxT("NUMPAD5"); break;
989 case WXK_NUMPAD6
: key
= wxT("NUMPAD6"); break;
990 case WXK_NUMPAD7
: key
= wxT("NUMPAD7"); break;
991 case WXK_NUMPAD8
: key
= wxT("NUMPAD8"); break;
992 case WXK_NUMPAD9
: key
= wxT("NUMPAD9"); break;
993 case WXK_MULTIPLY
: key
= wxT("MULTIPLY"); break;
994 case WXK_ADD
: key
= wxT("ADD"); break;
995 case WXK_SEPARATOR
: key
= wxT("SEPARATOR"); break;
996 case WXK_SUBTRACT
: key
= wxT("SUBTRACT"); break;
997 case WXK_DECIMAL
: key
= wxT("DECIMAL"); break;
998 case WXK_DIVIDE
: key
= wxT("DIVIDE"); break;
999 case WXK_F1
: key
= wxT("F1"); break;
1000 case WXK_F2
: key
= wxT("F2"); break;
1001 case WXK_F3
: key
= wxT("F3"); break;
1002 case WXK_F4
: key
= wxT("F4"); break;
1003 case WXK_F5
: key
= wxT("F5"); break;
1004 case WXK_F6
: key
= wxT("F6"); break;
1005 case WXK_F7
: key
= wxT("F7"); break;
1006 case WXK_F8
: key
= wxT("F8"); break;
1007 case WXK_F9
: key
= wxT("F9"); break;
1008 case WXK_F10
: key
= wxT("F10"); break;
1009 case WXK_F11
: key
= wxT("F11"); break;
1010 case WXK_F12
: key
= wxT("F12"); break;
1011 case WXK_F13
: key
= wxT("F13"); break;
1012 case WXK_F14
: key
= wxT("F14"); break;
1013 case WXK_F15
: key
= wxT("F15"); break;
1014 case WXK_F16
: key
= wxT("F16"); break;
1015 case WXK_F17
: key
= wxT("F17"); break;
1016 case WXK_F18
: key
= wxT("F18"); break;
1017 case WXK_F19
: key
= wxT("F19"); break;
1018 case WXK_F20
: key
= wxT("F20"); break;
1019 case WXK_F21
: key
= wxT("F21"); break;
1020 case WXK_F22
: key
= wxT("F22"); break;
1021 case WXK_F23
: key
= wxT("F23"); break;
1022 case WXK_F24
: key
= wxT("F24"); break;
1023 case WXK_NUMLOCK
: key
= wxT("NUMLOCK"); break;
1024 case WXK_SCROLL
: key
= wxT("SCROLL"); break;
1025 case WXK_PAGEUP
: key
= wxT("PAGEUP"); break;
1026 case WXK_PAGEDOWN
: key
= wxT("PAGEDOWN"); break;
1027 case WXK_NUMPAD_SPACE
: key
= wxT("NUMPAD_SPACE"); break;
1028 case WXK_NUMPAD_TAB
: key
= wxT("NUMPAD_TAB"); break;
1029 case WXK_NUMPAD_ENTER
: key
= wxT("NUMPAD_ENTER"); break;
1030 case WXK_NUMPAD_F1
: key
= wxT("NUMPAD_F1"); break;
1031 case WXK_NUMPAD_F2
: key
= wxT("NUMPAD_F2"); break;
1032 case WXK_NUMPAD_F3
: key
= wxT("NUMPAD_F3"); break;
1033 case WXK_NUMPAD_F4
: key
= wxT("NUMPAD_F4"); break;
1034 case WXK_NUMPAD_HOME
: key
= wxT("NUMPAD_HOME"); break;
1035 case WXK_NUMPAD_LEFT
: key
= wxT("NUMPAD_LEFT"); break;
1036 case WXK_NUMPAD_UP
: key
= wxT("NUMPAD_UP"); break;
1037 case WXK_NUMPAD_RIGHT
: key
= wxT("NUMPAD_RIGHT"); break;
1038 case WXK_NUMPAD_DOWN
: key
= wxT("NUMPAD_DOWN"); break;
1039 case WXK_NUMPAD_PAGEUP
: key
= wxT("NUMPAD_PAGEUP"); break;
1040 case WXK_NUMPAD_PAGEDOWN
: key
= wxT("NUMPAD_PAGEDOWN"); break;
1041 case WXK_NUMPAD_END
: key
= wxT("NUMPAD_END"); break;
1042 case WXK_NUMPAD_BEGIN
: key
= wxT("NUMPAD_BEGIN"); break;
1043 case WXK_NUMPAD_INSERT
: key
= wxT("NUMPAD_INSERT"); break;
1044 case WXK_NUMPAD_DELETE
: key
= wxT("NUMPAD_DELETE"); break;
1045 case WXK_NUMPAD_EQUAL
: key
= wxT("NUMPAD_EQUAL"); break;
1046 case WXK_NUMPAD_MULTIPLY
: key
= wxT("NUMPAD_MULTIPLY"); break;
1047 case WXK_NUMPAD_ADD
: key
= wxT("NUMPAD_ADD"); break;
1048 case WXK_NUMPAD_SEPARATOR
: key
= wxT("NUMPAD_SEPARATOR"); break;
1049 case WXK_NUMPAD_SUBTRACT
: key
= wxT("NUMPAD_SUBTRACT"); break;
1050 case WXK_NUMPAD_DECIMAL
: key
= wxT("NUMPAD_DECIMAL"); break;
1054 if ( keycode
< 128 && wxIsprint((int)keycode
) )
1055 key
.Printf(wxT("'%c'"), (char)keycode
);
1056 else if ( keycode
> 0 && keycode
< 27 )
1057 key
.Printf(_("Ctrl-%c"), wxT('A') + keycode
- 1);
1059 key
.Printf(wxT("unknown (%ld)"), keycode
);
1064 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1067 event
.ControlDown() ? wxT('C') : wxT('-'),
1068 event
.AltDown() ? wxT('A') : wxT('-'),
1069 event
.ShiftDown() ? wxT('S') : wxT('-'),
1070 event
.MetaDown() ? wxT('M') : wxT('-'));
1073 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
& event
)
1075 LogKeyEvent(wxT("Tree key down "), event
.GetKeyEvent());
1080 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
1082 // need to explicitly allow drag
1083 if ( event
.GetItem() != GetRootItem() )
1085 m_draggedItem
= event
.GetItem();
1087 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
1088 GetItemText(m_draggedItem
).c_str());
1094 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1098 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
1100 wxTreeItemId itemSrc
= m_draggedItem
,
1101 itemDst
= event
.GetItem();
1102 m_draggedItem
= (wxTreeItemId
)0l;
1104 // where to copy the item?
1105 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
1107 // copy to the parent then
1108 itemDst
= GetItemParent(itemDst
);
1111 if ( !itemDst
.IsOk() )
1113 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1118 wxString text
= GetItemText(itemSrc
);
1119 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1120 text
.c_str(), GetItemText(itemDst
).c_str());
1122 // just do append here - we could also insert it just before/after the item
1123 // on which it was dropped, but this requires slightly more work... we also
1124 // completely ignore the client data and icon of the old item but could
1125 // copy them as well.
1127 // Finally, we only copy one item here but we might copy the entire tree if
1128 // we were dragging a folder.
1129 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
1130 AppendItem(itemDst
, text
, image
);
1133 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
1135 wxLogMessage(wxT("OnBeginLabelEdit"));
1137 // for testing, prevent this item's label editing
1138 wxTreeItemId itemId
= event
.GetItem();
1139 if ( IsTestItem(itemId
) )
1141 wxMessageBox(wxT("You can't edit this item."));
1145 else if ( itemId
== GetRootItem() )
1147 // test that it is possible to change the text of the item being edited
1148 SetItemText(itemId
, _T("Editing root item"));
1152 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
1154 wxLogMessage(wxT("OnEndLabelEdit"));
1156 // don't allow anything except letters in the labels
1157 if ( !event
.GetLabel().IsWord() )
1159 wxMessageBox(wxT("The new label should be a single word."));
1165 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
1167 wxLogMessage(wxT("OnItemCollapsing"));
1169 // for testing, prevent the user from collapsing the first child folder
1170 wxTreeItemId itemId
= event
.GetItem();
1171 if ( IsTestItem(itemId
) )
1173 wxMessageBox(wxT("You can't collapse this item."));
1179 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
1181 // show some info about this item
1182 wxTreeItemId itemId
= event
.GetItem();
1183 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1187 item
->ShowInfo(this);
1190 wxLogMessage(wxT("OnItemActivated"));
1193 void MyTreeCtrl::OnItemMenu(wxTreeEvent
& event
)
1195 wxTreeItemId itemId
= event
.GetItem();
1196 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1199 wxLogMessage(wxT("OnItemMenu for item \"%s\""), item
? item
->GetDesc()
1205 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1207 wxPoint pt
= event
.GetPosition();
1209 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt
.x
, pt
.y
);
1211 // check if event was generated by keyboard (MSW-specific?)
1212 if ( pt
.x
== -1 && pt
.y
== -1 ) //(this is how MSW indicates it)
1214 if ( !HasFlag(wxTR_MULTIPLE
) )
1215 item
= GetSelection();
1217 // attempt to guess where to show the menu
1220 // if an item was clicked, show menu to the right of it
1222 GetBoundingRect(item
, rect
, true /* only the label */);
1223 pt
= wxPoint(rect
.GetRight(), rect
.GetTop());
1230 else // event was generated by mouse, use supplied coords
1232 pt
= ScreenToClient(pt
);
1239 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
1244 title
<< wxT("Menu for ") << GetItemText(id
);
1248 title
= wxT("Menu for no particular item");
1253 menu
.Append(TreeTest_About
, wxT("&About..."));
1254 menu
.AppendSeparator();
1255 menu
.Append(TreeTest_Highlight
, wxT("&Highlight item"));
1256 menu
.Append(TreeTest_Dump
, wxT("&Dump"));
1258 PopupMenu(&menu
, pt
);
1259 #endif // wxUSE_MENUS
1262 void MyTreeCtrl::OnItemRClick(wxTreeEvent
& event
)
1264 wxTreeItemId itemId
= event
.GetItem();
1265 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1268 wxLogMessage(wxT("Item \"%s\" right clicked"), item
? item
->GetDesc()
1274 void MyTreeCtrl::OnRMouseDown(wxMouseEvent
& event
)
1276 wxLogMessage(wxT("Right mouse button down"));
1281 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
1283 wxLogMessage(wxT("Right mouse button up"));
1288 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
1290 wxTreeItemId id
= HitTest(event
.GetPosition());
1292 wxLogMessage(wxT("No item under mouse"));
1295 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
1297 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1303 static inline const wxChar
*Bool2String(bool b
)
1305 return b
? wxT("") : wxT("not ");
1308 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1310 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1311 wxT("%u children (%u immediately under this item)."),
1313 Bool2String(tree
->IsSelected(GetId())),
1314 Bool2String(tree
->IsExpanded(GetId())),
1315 Bool2String(tree
->IsBold(GetId())),
1316 unsigned(tree
->GetChildrenCount(GetId())),
1317 unsigned(tree
->GetChildrenCount(GetId(), false)));