1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #define USE_GENERIC_TREECTRL 0
13 #if USE_GENERIC_TREECTRL
14 #include "wx/generic/treectlg.h"
16 #define wxTreeCtrl wxGenericTreeCtrl
17 #define sm_classwxTreeCtrl sm_classwxGenericTreeCtrl
21 // Define a new application type
22 class MyApp
: public wxApp
25 MyApp() { m_showImages
= true; m_showStates
= true; m_showButtons
= false; }
29 void SetShowImages(bool show
) { m_showImages
= show
; }
30 bool ShowImages() const { return m_showImages
; }
32 void SetShowStates(bool show
) { m_showStates
= show
; }
33 bool ShowStates() const { return m_showStates
; }
35 void SetShowButtons(bool show
) { m_showButtons
= show
; }
36 bool ShowButtons() const { return m_showButtons
; }
39 bool m_showImages
, m_showStates
, m_showButtons
;
42 class MyTreeItemData
: public wxTreeItemData
45 MyTreeItemData(const wxString
& desc
) : m_desc(desc
) { }
47 void ShowInfo(wxTreeCtrl
*tree
);
48 const wxChar
*GetDesc() const { return m_desc
.c_str(); }
54 class MyTreeCtrl
: public wxTreeCtrl
60 TreeCtrlIcon_FileSelected
,
62 TreeCtrlIcon_FolderSelected
,
63 TreeCtrlIcon_FolderOpened
66 MyTreeCtrl() { m_alternateImages
= false; m_alternateStates
= false; }
67 MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
68 const wxPoint
& pos
, const wxSize
& size
,
70 virtual ~MyTreeCtrl(){};
72 void OnBeginDrag(wxTreeEvent
& event
);
73 void OnBeginRDrag(wxTreeEvent
& event
);
74 void OnEndDrag(wxTreeEvent
& event
);
75 void OnBeginLabelEdit(wxTreeEvent
& event
);
76 void OnEndLabelEdit(wxTreeEvent
& event
);
77 void OnDeleteItem(wxTreeEvent
& event
);
78 void OnContextMenu(wxContextMenuEvent
& event
);
79 void OnItemMenu(wxTreeEvent
& event
);
80 void OnGetInfo(wxTreeEvent
& event
);
81 void OnSetInfo(wxTreeEvent
& event
);
82 void OnItemExpanded(wxTreeEvent
& event
);
83 void OnItemExpanding(wxTreeEvent
& event
);
84 void OnItemCollapsed(wxTreeEvent
& event
);
85 void OnItemCollapsing(wxTreeEvent
& event
);
86 void OnSelChanged(wxTreeEvent
& event
);
87 void OnSelChanging(wxTreeEvent
& event
);
88 void OnTreeKeyDown(wxTreeEvent
& event
);
89 void OnItemActivated(wxTreeEvent
& event
);
90 void OnItemStateClick(wxTreeEvent
& event
);
91 void OnItemRClick(wxTreeEvent
& event
);
93 void OnRMouseDown(wxMouseEvent
& event
);
94 void OnRMouseUp(wxMouseEvent
& event
);
95 void OnRMouseDClick(wxMouseEvent
& event
);
97 void GetItemsRecursively(const wxTreeItemId
& idParent
,
98 wxTreeItemIdValue cookie
= 0);
100 void CreateImageList(int size
= 16);
101 void CreateButtonsImageList(int size
= 11);
102 void CreateStateImageList(bool del
= false);
104 void AddTestItemsToTree(size_t numChildren
, size_t depth
);
106 void DoSortChildren(const wxTreeItemId
& item
, bool reverse
= false)
107 { m_reverseSort
= reverse
; wxTreeCtrl::SortChildren(item
); }
108 void DoEnsureVisible() { if (m_lastItem
.IsOk()) EnsureVisible(m_lastItem
); }
110 void DoToggleIcon(const wxTreeItemId
& item
);
111 void DoToggleState(const wxTreeItemId
& item
);
113 void ShowMenu(wxTreeItemId id
, const wxPoint
& pt
);
115 int ImageSize(void) const { return m_imageSize
; }
117 void SetLastItem(wxTreeItemId id
) { m_lastItem
= id
; }
119 void SetAlternateImages(bool show
) { m_alternateImages
= show
; }
120 bool AlternateImages() const { return m_alternateImages
; }
122 void SetAlternateStates(bool show
) { m_alternateStates
= show
; }
123 bool AlternateStates() const { return m_alternateStates
; }
125 void ResetBrokenStateImages()
127 const size_t count
= GetStateImageList()->GetImageCount();
128 int state
= count
> 0 ? count
- 1 : wxTREE_ITEMSTATE_NONE
;
129 DoResetBrokenStateImages(GetRootItem(), 0, state
);
133 virtual int OnCompareItems(const wxTreeItemId
& i1
, const wxTreeItemId
& i2
);
135 // is this the test item which we use in several event handlers?
136 bool IsTestItem(const wxTreeItemId
& item
)
138 // the test item is the first child folder
139 return GetItemParent(item
) == GetRootItem() && !GetPrevSibling(item
);
143 void AddItemsRecursively(const wxTreeItemId
& idParent
,
148 void DoResetBrokenStateImages(const wxTreeItemId
& idParent
,
149 wxTreeItemIdValue cookie
, int state
);
151 void LogEvent(const wxChar
*name
, const wxTreeEvent
& event
);
153 int m_imageSize
; // current size of images
154 bool m_reverseSort
; // flag for OnCompareItems
155 wxTreeItemId m_lastItem
, // for OnEnsureVisible()
156 m_draggedItem
; // item being dragged right now
157 bool m_alternateImages
;
158 bool m_alternateStates
;
160 // NB: due to an ugly wxMSW hack you _must_ use DECLARE_DYNAMIC_CLASS()
161 // if you want your overloaded OnCompareItems() to be called.
162 // OTOH, if you don't want it you may omit the next line - this will
163 // make default (alphabetical) sorting much faster under wxMSW.
164 DECLARE_DYNAMIC_CLASS(MyTreeCtrl
)
165 DECLARE_EVENT_TABLE()
168 // Define a new frame type
169 class MyFrame
: public wxFrame
173 MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
);
177 void OnQuit(wxCommandEvent
& event
);
178 void OnAbout(wxCommandEvent
& event
);
179 void OnClearLog(wxCommandEvent
& event
);
181 void OnTogButtons(wxCommandEvent
& event
)
182 { TogStyle(event
.GetId(), wxTR_HAS_BUTTONS
); }
183 void OnTogTwist(wxCommandEvent
& event
)
184 { TogStyle(event
.GetId(), wxTR_TWIST_BUTTONS
); }
185 void OnTogLines(wxCommandEvent
& event
)
186 { TogStyle(event
.GetId(), wxTR_NO_LINES
); }
187 void OnTogEdit(wxCommandEvent
& event
)
188 { TogStyle(event
.GetId(), wxTR_EDIT_LABELS
); }
189 void OnTogHideRoot(wxCommandEvent
& event
)
190 { TogStyle(event
.GetId(), wxTR_HIDE_ROOT
); }
191 void OnTogRootLines(wxCommandEvent
& event
)
192 { TogStyle(event
.GetId(), wxTR_LINES_AT_ROOT
); }
193 void OnTogBorder(wxCommandEvent
& event
)
194 { TogStyle(event
.GetId(), wxTR_ROW_LINES
); }
195 void OnTogFullHighlight(wxCommandEvent
& event
)
196 { TogStyle(event
.GetId(), wxTR_FULL_ROW_HIGHLIGHT
); }
198 void OnResetStyle(wxCommandEvent
& WXUNUSED(event
))
199 { CreateTreeWithDefStyle(); }
201 void OnSetFgColour(wxCommandEvent
& event
);
202 void OnSetBgColour(wxCommandEvent
& event
);
204 void OnHighlight(wxCommandEvent
& event
);
205 void OnDump(wxCommandEvent
& event
);
206 #ifndef NO_MULTIPLE_SELECTION
207 void OnDumpSelected(wxCommandEvent
& event
);
208 void OnSelect(wxCommandEvent
& event
);
209 void OnUnselect(wxCommandEvent
& event
);
210 void OnToggleSel(wxCommandEvent
& event
);
211 void OnSelectChildren(wxCommandEvent
& event
);
212 #endif // NO_MULTIPLE_SELECTION
213 void OnSelectRoot(wxCommandEvent
& event
);
214 void OnSetFocusedRoot(wxCommandEvent
& event
);
215 void OnClearFocused(wxCommandEvent
& event
);
216 void OnDelete(wxCommandEvent
& event
);
217 void OnDeleteChildren(wxCommandEvent
& event
);
218 void OnDeleteAll(wxCommandEvent
& event
);
220 void OnRecreate(wxCommandEvent
& event
);
221 void OnToggleButtons(wxCommandEvent
& event
);
222 void OnToggleImages(wxCommandEvent
& event
);
223 void OnToggleStates(wxCommandEvent
& event
);
224 void OnToggleBell(wxCommandEvent
& event
);
225 void OnToggleAlternateImages(wxCommandEvent
& event
);
226 void OnToggleAlternateStates(wxCommandEvent
& event
);
227 void OnSetImageSize(wxCommandEvent
& event
);
228 void OnCollapseAndReset(wxCommandEvent
& event
);
230 void OnSetBold(wxCommandEvent
& WXUNUSED(event
)) { DoSetBold(true); }
231 void OnClearBold(wxCommandEvent
& WXUNUSED(event
)) { DoSetBold(false); }
233 void OnEnsureVisible(wxCommandEvent
& event
);
234 void OnSetFocus(wxCommandEvent
& event
);
236 void OnCount(wxCommandEvent
& event
);
237 void OnCountRec(wxCommandEvent
& event
);
239 void OnRename(wxCommandEvent
& event
);
240 void OnSort(wxCommandEvent
& WXUNUSED(event
)) { DoSort(); }
241 void OnSortRev(wxCommandEvent
& WXUNUSED(event
)) { DoSort(true); }
243 void OnAddItem(wxCommandEvent
& event
);
244 void OnAddManyItems(wxCommandEvent
& event
);
245 void OnInsertItem(wxCommandEvent
& event
);
247 void OnIncIndent(wxCommandEvent
& event
);
248 void OnDecIndent(wxCommandEvent
& event
);
250 void OnIncSpacing(wxCommandEvent
& event
);
251 void OnDecSpacing(wxCommandEvent
& event
);
253 void OnToggleIcon(wxCommandEvent
& event
);
254 void OnToggleState(wxCommandEvent
& event
);
256 void OnShowFirstVisible(wxCommandEvent
& WXUNUSED(event
))
257 { DoShowFirstOrLast(&wxTreeCtrl::GetFirstVisibleItem
, "first visible"); }
258 #ifdef wxHAS_LAST_VISIBLE // we don't have it currently but may add later
259 void OnShowLastVisible(wxCommandEvent
& WXUNUSED(event
))
260 { DoShowFirstOrLast(&wxTreeCtrl::GetLastVisibleItem
, "last visible"); }
261 #endif // wxHAS_LAST_VISIBLE
263 void OnShowNextVisible(wxCommandEvent
& WXUNUSED(event
))
264 { DoShowRelativeItem(&wxTreeCtrl::GetNextVisible
, "next visible"); }
265 void OnShowPrevVisible(wxCommandEvent
& WXUNUSED(event
))
266 { DoShowRelativeItem(&wxTreeCtrl::GetPrevVisible
, "previous visible"); }
268 void OnShowParent(wxCommandEvent
& WXUNUSED(event
))
269 { DoShowRelativeItem(&wxTreeCtrl::GetItemParent
, "parent"); }
270 void OnShowPrevSibling(wxCommandEvent
& WXUNUSED(event
))
271 { DoShowRelativeItem(&wxTreeCtrl::GetPrevSibling
, "previous sibling"); }
272 void OnShowNextSibling(wxCommandEvent
& WXUNUSED(event
))
273 { DoShowRelativeItem(&wxTreeCtrl::GetNextSibling
, "next sibling"); }
275 void OnScrollTo(wxCommandEvent
& event
);
276 void OnSelectLast(wxCommandEvent
& event
);
278 void OnIdle(wxIdleEvent
& event
);
279 void OnSize(wxSizeEvent
& event
);
282 void TogStyle(int id
, long flag
);
284 void DoSort(bool reverse
= false);
288 void CreateTreeWithDefStyle();
289 void CreateTree(long style
);
291 // common parts of OnShowFirst/LastVisible() and OnShowNext/PrevVisible()
292 typedef wxTreeItemId (wxTreeCtrl::*TreeFunc0_t
)() const;
293 void DoShowFirstOrLast(TreeFunc0_t pfn
, const wxString
& label
);
295 typedef wxTreeItemId (wxTreeCtrl::*TreeFunc1_t
)(const wxTreeItemId
&) const;
296 void DoShowRelativeItem(TreeFunc1_t pfn
, const wxString
& label
);
300 MyTreeCtrl
*m_treeCtrl
;
302 wxTextCtrl
*m_textCtrl
;
305 void DoSetBold(bool bold
= true);
307 DECLARE_EVENT_TABLE()
310 // menu and control ids
313 TreeTest_Quit
= wxID_EXIT
,
314 TreeTest_About
= wxID_ABOUT
,
315 TreeTest_ClearLog
= wxID_CLEAR
,
316 TreeTest_TogButtons
= wxID_HIGHEST
,
320 TreeTest_TogHideRoot
,
321 TreeTest_TogRootLines
,
323 TreeTest_TogFullHighlight
,
324 TreeTest_SetFgColour
,
325 TreeTest_SetBgColour
,
329 TreeTest_DumpSelected
,
338 TreeTest_DeleteChildren
,
341 TreeTest_ToggleImages
,
342 TreeTest_ToggleStates
,
344 TreeTest_ToggleAlternateImages
,
345 TreeTest_ToggleAlternateStates
,
346 TreeTest_ToggleButtons
,
347 TreeTest_SetImageSize
,
349 TreeTest_CollapseAndReset
,
350 TreeTest_EnsureVisible
,
353 TreeTest_AddManyItems
,
360 TreeTest_ToggleState
,
364 TreeTest_ClearFocused
,
365 TreeTest_SetFocusedRoot
,
366 TreeTest_SelectChildren
,
367 TreeTest_ShowFirstVisible
,
368 TreeTest_ShowLastVisible
,
369 TreeTest_ShowNextVisible
,
370 TreeTest_ShowPrevVisible
,
372 TreeTest_ShowPrevSibling
,
373 TreeTest_ShowNextSibling
,