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 OnItemMenu(wxTreeEvent
& event
);
78 void OnGetInfo(wxTreeEvent
& event
);
79 void OnSetInfo(wxTreeEvent
& event
);
80 void OnItemExpanded(wxTreeEvent
& event
);
81 void OnItemExpanding(wxTreeEvent
& event
);
82 void OnItemCollapsed(wxTreeEvent
& event
);
83 void OnItemCollapsing(wxTreeEvent
& event
);
84 void OnSelChanged(wxTreeEvent
& event
);
85 void OnSelChanging(wxTreeEvent
& event
);
86 void OnTreeKeyDown(wxTreeEvent
& event
);
87 void OnItemActivated(wxTreeEvent
& event
);
88 void OnItemRClick(wxTreeEvent
& event
);
90 void OnRMouseDown(wxMouseEvent
& event
);
91 void OnRMouseUp(wxMouseEvent
& event
);
92 void OnRMouseDClick(wxMouseEvent
& event
);
94 void GetItemsRecursively(const wxTreeItemId
& idParent
,
95 wxTreeItemIdValue cookie
= 0);
97 void CreateImageList(int size
= 16);
98 void CreateButtonsImageList(int size
= 11);
100 void AddTestItemsToTree(size_t numChildren
, size_t depth
);
102 void DoSortChildren(const wxTreeItemId
& item
, bool reverse
= false)
103 { m_reverseSort
= reverse
; wxTreeCtrl::SortChildren(item
); }
104 void DoEnsureVisible() { if (m_lastItem
.IsOk()) EnsureVisible(m_lastItem
); }
106 void DoToggleIcon(const wxTreeItemId
& item
);
108 void ShowMenu(wxTreeItemId id
, const wxPoint
& pt
);
110 int ImageSize(void) const { return m_imageSize
; }
112 void SetLastItem(wxTreeItemId id
) { m_lastItem
= id
; }
115 virtual int OnCompareItems(const wxTreeItemId
& i1
, const wxTreeItemId
& i2
);
117 // is this the test item which we use in several event handlers?
118 bool IsTestItem(const wxTreeItemId
& item
)
120 // the test item is the first child folder
121 return GetItemParent(item
) == GetRootItem() && !GetPrevSibling(item
);
125 void AddItemsRecursively(const wxTreeItemId
& idParent
,
130 void LogEvent(const wxChar
*name
, const wxTreeEvent
& event
);
132 int m_imageSize
; // current size of images
133 bool m_reverseSort
; // flag for OnCompareItems
134 wxTreeItemId m_lastItem
, // for OnEnsureVisible()
135 m_draggedItem
; // item being dragged right now
137 // NB: due to an ugly wxMSW hack you _must_ use DECLARE_DYNAMIC_CLASS()
138 // if you want your overloaded OnCompareItems() to be called.
139 // OTOH, if you don't want it you may omit the next line - this will
140 // make default (alphabetical) sorting much faster under wxMSW.
141 DECLARE_DYNAMIC_CLASS(MyTreeCtrl
)
142 DECLARE_EVENT_TABLE()
145 // Define a new frame type
146 class MyFrame
: public wxFrame
150 MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
);
154 void OnQuit(wxCommandEvent
& event
);
155 void OnAbout(wxCommandEvent
& event
);
157 void OnTogButtons(wxCommandEvent
& event
)
158 { TogStyle(event
.GetId(), wxTR_HAS_BUTTONS
); }
159 void OnTogTwist(wxCommandEvent
& event
)
160 { TogStyle(event
.GetId(), wxTR_TWIST_BUTTONS
); }
161 void OnTogLines(wxCommandEvent
& event
)
162 { TogStyle(event
.GetId(), wxTR_NO_LINES
); }
163 void OnTogEdit(wxCommandEvent
& event
)
164 { TogStyle(event
.GetId(), wxTR_EDIT_LABELS
); }
165 void OnTogHideRoot(wxCommandEvent
& event
)
166 { TogStyle(event
.GetId(), wxTR_HIDE_ROOT
); }
167 void OnTogRootLines(wxCommandEvent
& event
)
168 { TogStyle(event
.GetId(), wxTR_LINES_AT_ROOT
); }
169 void OnTogBorder(wxCommandEvent
& event
)
170 { TogStyle(event
.GetId(), wxTR_ROW_LINES
); }
171 void OnTogFullHighlight(wxCommandEvent
& event
)
172 { TogStyle(event
.GetId(), wxTR_FULL_ROW_HIGHLIGHT
); }
174 void OnResetStyle(wxCommandEvent
& WXUNUSED(event
))
175 { CreateTreeWithDefStyle(); }
177 void OnSetFgColour(wxCommandEvent
& event
);
178 void OnSetBgColour(wxCommandEvent
& event
);
180 void OnHighlight(wxCommandEvent
& event
);
181 void OnDump(wxCommandEvent
& event
);
182 #ifndef NO_MULTIPLE_SELECTION
183 void OnDumpSelected(wxCommandEvent
& event
);
184 void OnSelect(wxCommandEvent
& event
);
185 void OnUnselect(wxCommandEvent
& event
);
186 void OnToggleSel(wxCommandEvent
& event
);
187 #endif // NO_MULTIPLE_SELECTION
188 void OnSelectRoot(wxCommandEvent
& event
);
189 void OnDelete(wxCommandEvent
& event
);
190 void OnDeleteChildren(wxCommandEvent
& event
);
191 void OnDeleteAll(wxCommandEvent
& event
);
193 void OnRecreate(wxCommandEvent
& event
);
194 void OnToggleButtons(wxCommandEvent
& event
);
195 void OnToggleImages(wxCommandEvent
& event
);
196 void OnSetImageSize(wxCommandEvent
& event
);
197 void OnCollapseAndReset(wxCommandEvent
& event
);
199 void OnSetBold(wxCommandEvent
& WXUNUSED(event
)) { DoSetBold(true); }
200 void OnClearBold(wxCommandEvent
& WXUNUSED(event
)) { DoSetBold(false); }
202 void OnEnsureVisible(wxCommandEvent
& event
);
204 void OnCount(wxCommandEvent
& event
);
205 void OnCountRec(wxCommandEvent
& event
);
207 void OnRename(wxCommandEvent
& event
);
208 void OnSort(wxCommandEvent
& WXUNUSED(event
)) { DoSort(); }
209 void OnSortRev(wxCommandEvent
& WXUNUSED(event
)) { DoSort(true); }
211 void OnAddItem(wxCommandEvent
& event
);
212 void OnInsertItem(wxCommandEvent
& event
);
214 void OnIncIndent(wxCommandEvent
& event
);
215 void OnDecIndent(wxCommandEvent
& event
);
217 void OnIncSpacing(wxCommandEvent
& event
);
218 void OnDecSpacing(wxCommandEvent
& event
);
220 void OnToggleIcon(wxCommandEvent
& event
);
222 void OnIdle(wxIdleEvent
& event
);
223 void OnSize(wxSizeEvent
& event
);
226 void TogStyle(int id
, long flag
);
228 void DoSort(bool reverse
= false);
232 void CreateTreeWithDefStyle();
233 void CreateTree(long style
);
236 MyTreeCtrl
*m_treeCtrl
;
238 wxTextCtrl
*m_textCtrl
;
241 void DoSetBold(bool bold
= true);
243 DECLARE_EVENT_TABLE()
246 // menu and control ids
249 TreeTest_Quit
= wxID_EXIT
,
250 TreeTest_About
= wxID_ABOUT
,
251 TreeTest_TogButtons
= wxID_HIGHEST
,
255 TreeTest_TogHideRoot
,
256 TreeTest_TogRootLines
,
258 TreeTest_TogFullHighlight
,
259 TreeTest_SetFgColour
,
260 TreeTest_SetBgColour
,
264 TreeTest_DumpSelected
,
273 TreeTest_DeleteChildren
,
276 TreeTest_ToggleImages
,
277 TreeTest_ToggleButtons
,
278 TreeTest_SetImageSize
,
280 TreeTest_CollapseAndReset
,
281 TreeTest_EnsureVisible
,