1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
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 OnContextMenu(wxContextMenuEvent
& 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
,
92 wxTreeItemIdValue cookie
= 0);
94 void CreateImageList(int size
= 16);
95 void CreateButtonsImageList(int size
= 11);
97 void AddTestItemsToTree(size_t numChildren
, size_t depth
);
99 void DoSortChildren(const wxTreeItemId
& item
, bool reverse
= FALSE
)
100 { m_reverseSort
= reverse
; wxTreeCtrl::SortChildren(item
); }
101 void DoEnsureVisible() { if (m_lastItem
.IsOk()) EnsureVisible(m_lastItem
); }
103 void DoToggleIcon(const wxTreeItemId
& item
);
105 void ShowMenu(wxTreeItemId id
, const wxPoint
& pt
);
107 int ImageSize(void) const { return m_imageSize
; }
109 void SetLastItem(wxTreeItemId id
) { m_lastItem
= id
; }
112 virtual int OnCompareItems(const wxTreeItemId
& i1
, const wxTreeItemId
& i2
);
114 // is this the test item which we use in several event handlers?
115 bool IsTestItem(const wxTreeItemId
& item
)
117 // the test item is the first child folder
118 return GetItemParent(item
) == GetRootItem() && !GetPrevSibling(item
);
122 void AddItemsRecursively(const wxTreeItemId
& idParent
,
127 int m_imageSize
; // current size of images
128 bool m_reverseSort
; // flag for OnCompareItems
129 wxTreeItemId m_lastItem
, // for OnEnsureVisible()
130 m_draggedItem
; // item being dragged right now
132 // NB: due to an ugly wxMSW hack you _must_ use DECLARE_DYNAMIC_CLASS()
133 // if you want your overloaded OnCompareItems() to be called.
134 // OTOH, if you don't want it you may omit the next line - this will
135 // make default (alphabetical) sorting much faster under wxMSW.
136 DECLARE_DYNAMIC_CLASS(MyTreeCtrl
)
137 DECLARE_EVENT_TABLE()
140 // Define a new frame type
141 class MyFrame
: public wxFrame
145 MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
);
149 void OnQuit(wxCommandEvent
& event
);
150 void OnAbout(wxCommandEvent
& event
);
152 void OnTogButtons(wxCommandEvent
& event
)
153 { TogStyle(event
.GetId(), wxTR_HAS_BUTTONS
); }
154 void OnTogTwist(wxCommandEvent
& event
)
155 { TogStyle(event
.GetId(), wxTR_TWIST_BUTTONS
); }
156 void OnTogLines(wxCommandEvent
& event
)
157 { TogStyle(event
.GetId(), wxTR_NO_LINES
); }
158 void OnTogEdit(wxCommandEvent
& event
)
159 { TogStyle(event
.GetId(), wxTR_EDIT_LABELS
); }
160 void OnTogHideRoot(wxCommandEvent
& event
)
161 { TogStyle(event
.GetId(), wxTR_HIDE_ROOT
); }
162 void OnTogRootLines(wxCommandEvent
& event
)
163 { TogStyle(event
.GetId(), wxTR_LINES_AT_ROOT
); }
164 void OnTogBorder(wxCommandEvent
& event
)
165 { TogStyle(event
.GetId(), wxTR_ROW_LINES
); }
166 void OnTogFullHighlight(wxCommandEvent
& event
)
167 { TogStyle(event
.GetId(), wxTR_FULL_ROW_HIGHLIGHT
); }
169 void OnResetStyle(wxCommandEvent
& WXUNUSED(event
))
170 { CreateTreeWithDefStyle(); }
172 void OnSetFgColour(wxCommandEvent
& event
);
173 void OnSetBgColour(wxCommandEvent
& event
);
175 void OnDump(wxCommandEvent
& event
);
176 #ifndef NO_MULTIPLE_SELECTION
177 void OnDumpSelected(wxCommandEvent
& event
);
178 void OnSelect(wxCommandEvent
& event
);
179 void OnUnselect(wxCommandEvent
& event
);
180 void OnToggleSel(wxCommandEvent
& event
);
181 #endif // NO_MULTIPLE_SELECTION
182 void OnDelete(wxCommandEvent
& event
);
183 void OnDeleteChildren(wxCommandEvent
& event
);
184 void OnDeleteAll(wxCommandEvent
& event
);
186 void OnRecreate(wxCommandEvent
& event
);
187 void OnToggleButtons(wxCommandEvent
& event
);
188 void OnToggleImages(wxCommandEvent
& event
);
189 void OnSetImageSize(wxCommandEvent
& event
);
190 void OnCollapseAndReset(wxCommandEvent
& event
);
192 void OnSetBold(wxCommandEvent
& WXUNUSED(event
)) { DoSetBold(TRUE
); }
193 void OnClearBold(wxCommandEvent
& WXUNUSED(event
)) { DoSetBold(FALSE
); }
195 void OnEnsureVisible(wxCommandEvent
& event
);
197 void OnCount(wxCommandEvent
& event
);
198 void OnCountRec(wxCommandEvent
& event
);
200 void OnRename(wxCommandEvent
& event
);
201 void OnSort(wxCommandEvent
& WXUNUSED(event
)) { DoSort(); }
202 void OnSortRev(wxCommandEvent
& WXUNUSED(event
)) { DoSort(TRUE
); }
204 void OnAddItem(wxCommandEvent
& event
);
205 void OnInsertItem(wxCommandEvent
& event
);
207 void OnIncIndent(wxCommandEvent
& event
);
208 void OnDecIndent(wxCommandEvent
& event
);
210 void OnIncSpacing(wxCommandEvent
& event
);
211 void OnDecSpacing(wxCommandEvent
& event
);
213 void OnToggleIcon(wxCommandEvent
& event
);
215 void OnIdle(wxIdleEvent
& event
);
216 void OnSize(wxSizeEvent
& event
);
219 void TogStyle(int id
, long flag
);
221 void DoSort(bool reverse
= FALSE
);
225 void CreateTreeWithDefStyle();
226 void CreateTree(long style
);
228 MyTreeCtrl
*m_treeCtrl
;
229 wxTextCtrl
*m_textCtrl
;
231 void DoSetBold(bool bold
= TRUE
);
233 DECLARE_EVENT_TABLE()
236 // menu and control ids
245 TreeTest_TogHideRoot
,
246 TreeTest_TogRootLines
,
248 TreeTest_TogFullHighlight
,
249 TreeTest_SetFgColour
,
250 TreeTest_SetBgColour
,
253 TreeTest_DumpSelected
,
262 TreeTest_DeleteChildren
,
265 TreeTest_ToggleImages
,
266 TreeTest_ToggleButtons
,
267 TreeTest_SetImageSize
,
269 TreeTest_CollapseAndReset
,
270 TreeTest_EnsureVisible
,