]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: treectrl.h | |
3 | // Purpose: wxTreeCtrl sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #define USE_GENERIC_TREECTRL 0 | |
13 | ||
14 | #if USE_GENERIC_TREECTRL | |
15 | #include "wx/generic/treectlg.h" | |
16 | #ifndef wxTreeCtrl | |
17 | #define wxTreeCtrl wxGenericTreeCtrl | |
18 | #define sm_classwxTreeCtrl sm_classwxGenericTreeCtrl | |
19 | #endif | |
20 | #endif | |
21 | ||
22 | // Define a new application type | |
23 | class MyApp : public wxApp | |
24 | { | |
25 | public: | |
26 | MyApp() { m_showImages = true; m_showStates = true; m_showButtons = false; } | |
27 | ||
28 | bool OnInit(); | |
29 | ||
30 | void SetShowImages(bool show) { m_showImages = show; } | |
31 | bool ShowImages() const { return m_showImages; } | |
32 | ||
33 | void SetShowStates(bool show) { m_showStates = show; } | |
34 | bool ShowStates() const { return m_showStates; } | |
35 | ||
36 | void SetShowButtons(bool show) { m_showButtons = show; } | |
37 | bool ShowButtons() const { return m_showButtons; } | |
38 | ||
39 | private: | |
40 | bool m_showImages, m_showStates, m_showButtons; | |
41 | }; | |
42 | ||
43 | class MyTreeItemData : public wxTreeItemData | |
44 | { | |
45 | public: | |
46 | MyTreeItemData(const wxString& desc) : m_desc(desc) { } | |
47 | ||
48 | void ShowInfo(wxTreeCtrl *tree); | |
49 | const wxChar *GetDesc() const { return m_desc.c_str(); } | |
50 | ||
51 | private: | |
52 | wxString m_desc; | |
53 | }; | |
54 | ||
55 | class MyTreeCtrl : public wxTreeCtrl | |
56 | { | |
57 | public: | |
58 | enum | |
59 | { | |
60 | TreeCtrlIcon_File, | |
61 | TreeCtrlIcon_FileSelected, | |
62 | TreeCtrlIcon_Folder, | |
63 | TreeCtrlIcon_FolderSelected, | |
64 | TreeCtrlIcon_FolderOpened | |
65 | }; | |
66 | ||
67 | MyTreeCtrl() { m_alternateImages = false; m_alternateStates = false; } | |
68 | MyTreeCtrl(wxWindow *parent, const wxWindowID id, | |
69 | const wxPoint& pos, const wxSize& size, | |
70 | long style); | |
71 | virtual ~MyTreeCtrl(){}; | |
72 | ||
73 | void OnBeginDrag(wxTreeEvent& event); | |
74 | void OnBeginRDrag(wxTreeEvent& event); | |
75 | void OnEndDrag(wxTreeEvent& event); | |
76 | void OnBeginLabelEdit(wxTreeEvent& event); | |
77 | void OnEndLabelEdit(wxTreeEvent& event); | |
78 | void OnDeleteItem(wxTreeEvent& event); | |
79 | void OnContextMenu(wxContextMenuEvent& event); | |
80 | void OnItemMenu(wxTreeEvent& event); | |
81 | void OnGetInfo(wxTreeEvent& event); | |
82 | void OnSetInfo(wxTreeEvent& event); | |
83 | void OnItemExpanded(wxTreeEvent& event); | |
84 | void OnItemExpanding(wxTreeEvent& event); | |
85 | void OnItemCollapsed(wxTreeEvent& event); | |
86 | void OnItemCollapsing(wxTreeEvent& event); | |
87 | void OnSelChanged(wxTreeEvent& event); | |
88 | void OnSelChanging(wxTreeEvent& event); | |
89 | void OnTreeKeyDown(wxTreeEvent& event); | |
90 | void OnItemActivated(wxTreeEvent& event); | |
91 | void OnItemStateClick(wxTreeEvent& event); | |
92 | void OnItemRClick(wxTreeEvent& event); | |
93 | ||
94 | void OnRMouseDown(wxMouseEvent& event); | |
95 | void OnRMouseUp(wxMouseEvent& event); | |
96 | void OnRMouseDClick(wxMouseEvent& event); | |
97 | ||
98 | void GetItemsRecursively(const wxTreeItemId& idParent, | |
99 | wxTreeItemIdValue cookie = 0); | |
100 | ||
101 | void CreateImageList(int size = 16); | |
102 | void CreateButtonsImageList(int size = 11); | |
103 | void CreateStateImageList(bool del = false); | |
104 | ||
105 | void AddTestItemsToTree(size_t numChildren, size_t depth); | |
106 | ||
107 | void DoSortChildren(const wxTreeItemId& item, bool reverse = false) | |
108 | { m_reverseSort = reverse; wxTreeCtrl::SortChildren(item); } | |
109 | void DoEnsureVisible() { if (m_lastItem.IsOk()) EnsureVisible(m_lastItem); } | |
110 | ||
111 | void DoToggleIcon(const wxTreeItemId& item); | |
112 | void DoToggleState(const wxTreeItemId& item); | |
113 | ||
114 | void ShowMenu(wxTreeItemId id, const wxPoint& pt); | |
115 | ||
116 | int ImageSize(void) const { return m_imageSize; } | |
117 | ||
118 | void SetLastItem(wxTreeItemId id) { m_lastItem = id; } | |
119 | ||
120 | void SetAlternateImages(bool show) { m_alternateImages = show; } | |
121 | bool AlternateImages() const { return m_alternateImages; } | |
122 | ||
123 | void SetAlternateStates(bool show) { m_alternateStates = show; } | |
124 | bool AlternateStates() const { return m_alternateStates; } | |
125 | ||
126 | void ResetBrokenStateImages() | |
127 | { | |
128 | const size_t count = GetStateImageList()->GetImageCount(); | |
129 | int state = count > 0 ? count - 1 : wxTREE_ITEMSTATE_NONE; | |
130 | DoResetBrokenStateImages(GetRootItem(), 0, state); | |
131 | } | |
132 | ||
133 | protected: | |
134 | virtual int OnCompareItems(const wxTreeItemId& i1, const wxTreeItemId& i2); | |
135 | ||
136 | // is this the test item which we use in several event handlers? | |
137 | bool IsTestItem(const wxTreeItemId& item) | |
138 | { | |
139 | // the test item is the first child folder | |
140 | return GetItemParent(item) == GetRootItem() && !GetPrevSibling(item); | |
141 | } | |
142 | ||
143 | private: | |
144 | void AddItemsRecursively(const wxTreeItemId& idParent, | |
145 | size_t nChildren, | |
146 | size_t depth, | |
147 | size_t folder); | |
148 | ||
149 | void DoResetBrokenStateImages(const wxTreeItemId& idParent, | |
150 | wxTreeItemIdValue cookie, int state); | |
151 | ||
152 | void LogEvent(const wxChar *name, const wxTreeEvent& event); | |
153 | ||
154 | int m_imageSize; // current size of images | |
155 | bool m_reverseSort; // flag for OnCompareItems | |
156 | wxTreeItemId m_lastItem, // for OnEnsureVisible() | |
157 | m_draggedItem; // item being dragged right now | |
158 | bool m_alternateImages; | |
159 | bool m_alternateStates; | |
160 | ||
161 | // NB: due to an ugly wxMSW hack you _must_ use DECLARE_DYNAMIC_CLASS() | |
162 | // if you want your overloaded OnCompareItems() to be called. | |
163 | // OTOH, if you don't want it you may omit the next line - this will | |
164 | // make default (alphabetical) sorting much faster under wxMSW. | |
165 | DECLARE_DYNAMIC_CLASS(MyTreeCtrl) | |
166 | DECLARE_EVENT_TABLE() | |
167 | }; | |
168 | ||
169 | // Define a new frame type | |
170 | class MyFrame: public wxFrame | |
171 | { | |
172 | public: | |
173 | // ctor and dtor | |
174 | MyFrame(const wxString& title, int x, int y, int w, int h); | |
175 | virtual ~MyFrame(); | |
176 | ||
177 | // menu callbacks | |
178 | void OnQuit(wxCommandEvent& event); | |
179 | void OnAbout(wxCommandEvent& event); | |
180 | void OnClearLog(wxCommandEvent& event); | |
181 | ||
182 | void OnTogButtons(wxCommandEvent& event) | |
183 | { TogStyle(event.GetId(), wxTR_HAS_BUTTONS); } | |
184 | void OnTogTwist(wxCommandEvent& event) | |
185 | { TogStyle(event.GetId(), wxTR_TWIST_BUTTONS); } | |
186 | void OnTogLines(wxCommandEvent& event) | |
187 | { TogStyle(event.GetId(), wxTR_NO_LINES); } | |
188 | void OnTogEdit(wxCommandEvent& event) | |
189 | { TogStyle(event.GetId(), wxTR_EDIT_LABELS); } | |
190 | void OnTogHideRoot(wxCommandEvent& event) | |
191 | { TogStyle(event.GetId(), wxTR_HIDE_ROOT); } | |
192 | void OnTogRootLines(wxCommandEvent& event) | |
193 | { TogStyle(event.GetId(), wxTR_LINES_AT_ROOT); } | |
194 | void OnTogBorder(wxCommandEvent& event) | |
195 | { TogStyle(event.GetId(), wxTR_ROW_LINES); } | |
196 | void OnTogFullHighlight(wxCommandEvent& event) | |
197 | { TogStyle(event.GetId(), wxTR_FULL_ROW_HIGHLIGHT); } | |
198 | ||
199 | void OnResetStyle(wxCommandEvent& WXUNUSED(event)) | |
200 | { CreateTreeWithDefStyle(); } | |
201 | ||
202 | void OnSetFgColour(wxCommandEvent& event); | |
203 | void OnSetBgColour(wxCommandEvent& event); | |
204 | ||
205 | void OnHighlight(wxCommandEvent& event); | |
206 | void OnDump(wxCommandEvent& event); | |
207 | #ifndef NO_MULTIPLE_SELECTION | |
208 | void OnDumpSelected(wxCommandEvent& event); | |
209 | void OnSelect(wxCommandEvent& event); | |
210 | void OnUnselect(wxCommandEvent& event); | |
211 | void OnToggleSel(wxCommandEvent& event); | |
212 | void OnSelectChildren(wxCommandEvent& event); | |
213 | #endif // NO_MULTIPLE_SELECTION | |
214 | void OnSelectRoot(wxCommandEvent& event); | |
215 | void OnDelete(wxCommandEvent& event); | |
216 | void OnDeleteChildren(wxCommandEvent& event); | |
217 | void OnDeleteAll(wxCommandEvent& event); | |
218 | ||
219 | void OnRecreate(wxCommandEvent& event); | |
220 | void OnToggleButtons(wxCommandEvent& event); | |
221 | void OnToggleImages(wxCommandEvent& event); | |
222 | void OnToggleStates(wxCommandEvent& event); | |
223 | void OnToggleAlternateImages(wxCommandEvent& event); | |
224 | void OnToggleAlternateStates(wxCommandEvent& event); | |
225 | void OnSetImageSize(wxCommandEvent& event); | |
226 | void OnCollapseAndReset(wxCommandEvent& event); | |
227 | ||
228 | void OnSetBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(true); } | |
229 | void OnClearBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(false); } | |
230 | ||
231 | void OnEnsureVisible(wxCommandEvent& event); | |
232 | void OnSetFocus(wxCommandEvent& event); | |
233 | ||
234 | void OnCount(wxCommandEvent& event); | |
235 | void OnCountRec(wxCommandEvent& event); | |
236 | ||
237 | void OnRename(wxCommandEvent& event); | |
238 | void OnSort(wxCommandEvent& WXUNUSED(event)) { DoSort(); } | |
239 | void OnSortRev(wxCommandEvent& WXUNUSED(event)) { DoSort(true); } | |
240 | ||
241 | void OnAddItem(wxCommandEvent& event); | |
242 | void OnInsertItem(wxCommandEvent& event); | |
243 | ||
244 | void OnIncIndent(wxCommandEvent& event); | |
245 | void OnDecIndent(wxCommandEvent& event); | |
246 | ||
247 | void OnIncSpacing(wxCommandEvent& event); | |
248 | void OnDecSpacing(wxCommandEvent& event); | |
249 | ||
250 | void OnToggleIcon(wxCommandEvent& event); | |
251 | void OnToggleState(wxCommandEvent& event); | |
252 | ||
253 | void OnShowFirstVisible(wxCommandEvent& WXUNUSED(event)) | |
254 | { DoShowFirstOrLast(&wxTreeCtrl::GetFirstVisibleItem, "first visible"); } | |
255 | #ifdef wxHAS_LAST_VISIBLE // we don't have it currently but may add later | |
256 | void OnShowLastVisible(wxCommandEvent& WXUNUSED(event)) | |
257 | { DoShowFirstOrLast(&wxTreeCtrl::GetLastVisibleItem, "last visible"); } | |
258 | #endif // wxHAS_LAST_VISIBLE | |
259 | ||
260 | void OnShowNextVisible(wxCommandEvent& WXUNUSED(event)) | |
261 | { DoShowRelativeItem(&wxTreeCtrl::GetNextVisible, "next visible"); } | |
262 | void OnShowPrevVisible(wxCommandEvent& WXUNUSED(event)) | |
263 | { DoShowRelativeItem(&wxTreeCtrl::GetPrevVisible, "previous visible"); } | |
264 | ||
265 | void OnShowParent(wxCommandEvent& WXUNUSED(event)) | |
266 | { DoShowRelativeItem(&wxTreeCtrl::GetItemParent, "parent"); } | |
267 | void OnShowPrevSibling(wxCommandEvent& WXUNUSED(event)) | |
268 | { DoShowRelativeItem(&wxTreeCtrl::GetPrevSibling, "previous sibling"); } | |
269 | void OnShowNextSibling(wxCommandEvent& WXUNUSED(event)) | |
270 | { DoShowRelativeItem(&wxTreeCtrl::GetNextSibling, "next sibling"); } | |
271 | ||
272 | void OnScrollTo(wxCommandEvent& event); | |
273 | ||
274 | void OnIdle(wxIdleEvent& event); | |
275 | void OnSize(wxSizeEvent& event); | |
276 | ||
277 | private: | |
278 | void TogStyle(int id, long flag); | |
279 | ||
280 | void DoSort(bool reverse = false); | |
281 | ||
282 | void Resize(); | |
283 | ||
284 | void CreateTreeWithDefStyle(); | |
285 | void CreateTree(long style); | |
286 | ||
287 | // common parts of OnShowFirst/LastVisible() and OnShowNext/PrevVisible() | |
288 | typedef wxTreeItemId (wxTreeCtrl::*TreeFunc0_t)() const; | |
289 | void DoShowFirstOrLast(TreeFunc0_t pfn, const wxString& label); | |
290 | ||
291 | typedef wxTreeItemId (wxTreeCtrl::*TreeFunc1_t)(const wxTreeItemId&) const; | |
292 | void DoShowRelativeItem(TreeFunc1_t pfn, const wxString& label); | |
293 | ||
294 | ||
295 | wxPanel *m_panel; | |
296 | MyTreeCtrl *m_treeCtrl; | |
297 | #if wxUSE_LOG | |
298 | wxTextCtrl *m_textCtrl; | |
299 | #endif // wxUSE_LOG | |
300 | ||
301 | void DoSetBold(bool bold = true); | |
302 | ||
303 | DECLARE_EVENT_TABLE() | |
304 | }; | |
305 | ||
306 | // menu and control ids | |
307 | enum | |
308 | { | |
309 | TreeTest_Quit = wxID_EXIT, | |
310 | TreeTest_About = wxID_ABOUT, | |
311 | TreeTest_ClearLog = wxID_CLEAR, | |
312 | TreeTest_TogButtons = wxID_HIGHEST, | |
313 | TreeTest_TogTwist, | |
314 | TreeTest_TogLines, | |
315 | TreeTest_TogEdit, | |
316 | TreeTest_TogHideRoot, | |
317 | TreeTest_TogRootLines, | |
318 | TreeTest_TogBorder, | |
319 | TreeTest_TogFullHighlight, | |
320 | TreeTest_SetFgColour, | |
321 | TreeTest_SetBgColour, | |
322 | TreeTest_ResetStyle, | |
323 | TreeTest_Highlight, | |
324 | TreeTest_Dump, | |
325 | TreeTest_DumpSelected, | |
326 | TreeTest_Count, | |
327 | TreeTest_CountRec, | |
328 | TreeTest_Sort, | |
329 | TreeTest_SortRev, | |
330 | TreeTest_SetBold, | |
331 | TreeTest_ClearBold, | |
332 | TreeTest_Rename, | |
333 | TreeTest_Delete, | |
334 | TreeTest_DeleteChildren, | |
335 | TreeTest_DeleteAll, | |
336 | TreeTest_Recreate, | |
337 | TreeTest_ToggleImages, | |
338 | TreeTest_ToggleStates, | |
339 | TreeTest_ToggleAlternateImages, | |
340 | TreeTest_ToggleAlternateStates, | |
341 | TreeTest_ToggleButtons, | |
342 | TreeTest_SetImageSize, | |
343 | TreeTest_ToggleSel, | |
344 | TreeTest_CollapseAndReset, | |
345 | TreeTest_EnsureVisible, | |
346 | TreeTest_SetFocus, | |
347 | TreeTest_AddItem, | |
348 | TreeTest_InsertItem, | |
349 | TreeTest_IncIndent, | |
350 | TreeTest_DecIndent, | |
351 | TreeTest_IncSpacing, | |
352 | TreeTest_DecSpacing, | |
353 | TreeTest_ToggleIcon, | |
354 | TreeTest_ToggleState, | |
355 | TreeTest_Select, | |
356 | TreeTest_Unselect, | |
357 | TreeTest_SelectRoot, | |
358 | TreeTest_SelectChildren, | |
359 | TreeTest_ShowFirstVisible, | |
360 | TreeTest_ShowLastVisible, | |
361 | TreeTest_ShowNextVisible, | |
362 | TreeTest_ShowPrevVisible, | |
363 | TreeTest_ShowParent, | |
364 | TreeTest_ShowPrevSibling, | |
365 | TreeTest_ShowNextSibling, | |
366 | TreeTest_ScrollTo, | |
367 | TreeTest_Ctrl = 1000 | |
368 | }; |