1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #define USE_GENERIC_TREECTRL 0
14 #if USE_GENERIC_TREECTRL
15 #include "wx/generic/treectlg.h"
17 #define wxTreeCtrl wxGenericTreeCtrl
18 #define sm_classwxTreeCtrl sm_classwxGenericTreeCtrl
22 // Define a new application type
23 class MyApp
: public wxApp
26 MyApp() { m_showImages
= TRUE
; m_showButtons
= FALSE
; }
30 void SetShowImages(bool show
) { m_showImages
= show
; }
31 bool ShowImages() const { return m_showImages
; }
33 void SetShowButtons(bool show
) { m_showButtons
= show
; }
34 bool ShowButtons() const { return m_showButtons
; }
37 bool m_showImages
, m_showButtons
;
40 class MyTreeItemData
: public wxTreeItemData
43 MyTreeItemData(const wxString
& desc
) : m_desc(desc
) { }
45 void ShowInfo(wxTreeCtrl
*tree
);
46 const wxChar
*GetDesc() const { return m_desc
.c_str(); }
52 class MyTreeCtrl
: public wxTreeCtrl
58 TreeCtrlIcon_FileSelected
,
60 TreeCtrlIcon_FolderSelected
,
61 TreeCtrlIcon_FolderOpened
65 MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
66 const wxPoint
& pos
, const wxSize
& size
,
68 virtual ~MyTreeCtrl();
70 void OnBeginDrag(wxTreeEvent
& event
);
71 void OnBeginRDrag(wxTreeEvent
& event
);
72 void OnEndDrag(wxTreeEvent
& event
);
73 void OnBeginLabelEdit(wxTreeEvent
& event
);
74 void OnEndLabelEdit(wxTreeEvent
& event
);
75 void OnDeleteItem(wxTreeEvent
& event
);
76 void OnRMouseUp(wxMouseEvent
& event
);
77 void OnGetInfo(wxTreeEvent
& event
);
78 void OnTreeRMouseClick(wxTreeEvent
& event
);
79 void OnItemRightClick(wxTreeEvent
& event
);
80 void OnSetInfo(wxTreeEvent
& event
);
81 void OnItemExpanded(wxTreeEvent
& event
);
82 void OnItemExpanding(wxTreeEvent
& event
);
83 void OnItemCollapsed(wxTreeEvent
& event
);
84 void OnItemCollapsing(wxTreeEvent
& event
);
85 void OnSelChanged(wxTreeEvent
& event
);
86 void OnSelChanging(wxTreeEvent
& event
);
87 void OnTreeKeyDown(wxTreeEvent
& event
);
88 void OnItemActivated(wxTreeEvent
& event
);
89 void OnRMouseDClick(wxMouseEvent
& event
);
91 void GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
);
93 void CreateImageList(int size
= 16);
94 void CreateButtonsImageList(int size
= 11);
96 void AddTestItemsToTree(size_t numChildren
, size_t depth
);
98 void DoSortChildren(const wxTreeItemId
& item
, bool reverse
= FALSE
)
99 { m_reverseSort
= reverse
; wxTreeCtrl::SortChildren(item
); }
100 void DoEnsureVisible() { EnsureVisible(m_lastItem
); }
102 void DoToggleIcon(const wxTreeItemId
& item
);
104 void ShowMenu(wxTreeItemId id
, const wxPoint
& pt
);
106 int ImageSize(void) const { return m_imageSize
; }
109 virtual int OnCompareItems(const wxTreeItemId
& i1
, const wxTreeItemId
& i2
);
111 // is this the test item which we use in several event handlers?
112 bool IsTestItem(const wxTreeItemId
& item
)
114 // the test item is the first child folder
115 return GetParent(item
) == GetRootItem() && !GetPrevSibling(item
);
119 void AddItemsRecursively(const wxTreeItemId
& idParent
,
124 int m_imageSize
; // current size of images
125 bool m_reverseSort
; // flag for OnCompareItems
126 wxTreeItemId m_lastItem
, // for OnEnsureVisible()
127 m_draggedItem
; // item being dragged right now
129 // NB: due to an ugly wxMSW hack you _must_ use DECLARE_DYNAMIC_CLASS()
130 // if you want your overloaded OnCompareItems() to be called.
131 // OTOH, if you don't want it you may omit the next line - this will
132 // make default (alphabetical) sorting much faster under wxMSW.
133 DECLARE_DYNAMIC_CLASS(MyTreeCtrl
)
134 DECLARE_EVENT_TABLE()
137 // Define a new frame type
138 class MyFrame
: public wxFrame
142 MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
);
146 void OnQuit(wxCommandEvent
& event
);
147 void OnAbout(wxCommandEvent
& event
);
149 void OnTogButtons(wxCommandEvent
& event
) { TogStyle(wxTR_HAS_BUTTONS
); }
150 void OnTogTwist(wxCommandEvent
& event
) { TogStyle(wxTR_TWIST_BUTTONS
); }
151 void OnTogLines(wxCommandEvent
& event
) { TogStyle(wxTR_NO_LINES
); }
152 void OnTogEdit(wxCommandEvent
& event
) { TogStyle(wxTR_EDIT_LABELS
); }
153 void OnTogHideRoot(wxCommandEvent
& event
) { TogStyle(wxTR_HIDE_ROOT
); }
154 void OnTogRootLines(wxCommandEvent
& event
) { TogStyle(wxTR_LINES_AT_ROOT
); }
155 void OnTogBorder(wxCommandEvent
& event
) { TogStyle(wxTR_ROW_LINES
); }
157 void OnDump(wxCommandEvent
& event
);
158 #ifndef NO_MULTIPLE_SELECTION
159 void OnDumpSelected(wxCommandEvent
& event
);
160 void OnSelect(wxCommandEvent
& event
);
161 void OnUnselect(wxCommandEvent
& event
);
162 void OnToggleSel(wxCommandEvent
& event
);
163 #endif // NO_MULTIPLE_SELECTION
164 void OnDelete(wxCommandEvent
& event
);
165 void OnDeleteChildren(wxCommandEvent
& event
);
166 void OnDeleteAll(wxCommandEvent
& event
);
168 void OnRecreate(wxCommandEvent
& event
);
169 void OnToggleButtons(wxCommandEvent
& event
);
170 void OnToggleImages(wxCommandEvent
& event
);
171 void OnSetImageSize(wxCommandEvent
& event
);
172 void OnCollapseAndReset(wxCommandEvent
& event
);
174 void OnSetBold(wxCommandEvent
& WXUNUSED(event
)) { DoSetBold(TRUE
); }
175 void OnClearBold(wxCommandEvent
& WXUNUSED(event
)) { DoSetBold(FALSE
); }
177 void OnEnsureVisible(wxCommandEvent
& event
);
179 void OnCount(wxCommandEvent
& event
);
180 void OnCountRec(wxCommandEvent
& event
);
182 void OnRename(wxCommandEvent
& event
);
183 void OnSort(wxCommandEvent
& event
) { DoSort(); }
184 void OnSortRev(wxCommandEvent
& event
) { DoSort(TRUE
); }
186 void OnAddItem(wxCommandEvent
& event
);
187 void OnInsertItem(wxCommandEvent
& event
);
189 void OnIncIndent(wxCommandEvent
& event
);
190 void OnDecIndent(wxCommandEvent
& event
);
192 void OnIncSpacing(wxCommandEvent
& event
);
193 void OnDecSpacing(wxCommandEvent
& event
);
195 void OnToggleIcon(wxCommandEvent
& event
);
197 void OnSize(wxSizeEvent
& event
);
200 void TogStyle(long flag
);
202 void DoSort(bool reverse
= FALSE
);
204 void Resize(const wxSize
& size
);
206 MyTreeCtrl
*m_treeCtrl
;
207 wxTextCtrl
*m_textCtrl
;
209 void DoSetBold(bool bold
= TRUE
);
211 DECLARE_EVENT_TABLE()
214 // menu and control ids
223 TreeTest_TogHideRoot
,
224 TreeTest_TogRootLines
,
227 TreeTest_DumpSelected
,
236 TreeTest_DeleteChildren
,
239 TreeTest_ToggleImages
,
240 TreeTest_ToggleButtons
,
241 TreeTest_SetImageSize
,
243 TreeTest_CollapseAndReset
,
244 TreeTest_EnsureVisible
,