]>
Commit | Line | Data |
---|---|---|
457814b5 | 1 | ///////////////////////////////////////////////////////////////////////////// |
5301d933 | 2 | // Name: treectrl.h |
457814b5 JS |
3 | // Purpose: wxTreeCtrl sample |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
6aa89a22 | 7 | // Copyright: (c) Julian Smart |
526954c5 | 8 | // Licence: wxWindows licence |
457814b5 JS |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
484523cf JS |
11 | #define USE_GENERIC_TREECTRL 0 |
12 | ||
13 | #if USE_GENERIC_TREECTRL | |
14 | #include "wx/generic/treectlg.h" | |
15 | #ifndef wxTreeCtrl | |
16 | #define wxTreeCtrl wxGenericTreeCtrl | |
17 | #define sm_classwxTreeCtrl sm_classwxGenericTreeCtrl | |
18 | #endif | |
19 | #endif | |
20 | ||
457814b5 | 21 | // Define a new application type |
5a43154b VZ |
22 | class MyApp : public wxApp |
23 | { | |
24 | public: | |
4754ab16 | 25 | MyApp() { m_showImages = true; m_showStates = true; m_showButtons = false; } |
2c8e4738 | 26 | |
5f939e78 | 27 | bool OnInit(); |
2c8e4738 VZ |
28 | |
29 | void SetShowImages(bool show) { m_showImages = show; } | |
30 | bool ShowImages() const { return m_showImages; } | |
31 | ||
4754ab16 RR |
32 | void SetShowStates(bool show) { m_showStates = show; } |
33 | bool ShowStates() const { return m_showStates; } | |
34 | ||
618a5e38 RR |
35 | void SetShowButtons(bool show) { m_showButtons = show; } |
36 | bool ShowButtons() const { return m_showButtons; } | |
37 | ||
2c8e4738 | 38 | private: |
4754ab16 | 39 | bool m_showImages, m_showStates, m_showButtons; |
5a43154b | 40 | }; |
457814b5 | 41 | |
5a43154b VZ |
42 | class MyTreeItemData : public wxTreeItemData |
43 | { | |
44 | public: | |
5f939e78 | 45 | MyTreeItemData(const wxString& desc) : m_desc(desc) { } |
5a43154b | 46 | |
5f939e78 | 47 | void ShowInfo(wxTreeCtrl *tree); |
4693b20c | 48 | const wxChar *GetDesc() const { return m_desc.c_str(); } |
5a43154b VZ |
49 | |
50 | private: | |
5f939e78 | 51 | wxString m_desc; |
457814b5 JS |
52 | }; |
53 | ||
5a43154b | 54 | class MyTreeCtrl : public wxTreeCtrl |
457814b5 JS |
55 | { |
56 | public: | |
5f939e78 VZ |
57 | enum |
58 | { | |
59 | TreeCtrlIcon_File, | |
74b31181 VZ |
60 | TreeCtrlIcon_FileSelected, |
61 | TreeCtrlIcon_Folder, | |
62 | TreeCtrlIcon_FolderSelected, | |
63 | TreeCtrlIcon_FolderOpened | |
5f939e78 VZ |
64 | }; |
65 | ||
4754ab16 | 66 | MyTreeCtrl() { m_alternateImages = false; m_alternateStates = false; } |
5f939e78 VZ |
67 | MyTreeCtrl(wxWindow *parent, const wxWindowID id, |
68 | const wxPoint& pos, const wxSize& size, | |
69 | long style); | |
925e9792 | 70 | virtual ~MyTreeCtrl(){}; |
5f939e78 VZ |
71 | |
72 | void OnBeginDrag(wxTreeEvent& event); | |
73 | void OnBeginRDrag(wxTreeEvent& event); | |
5888ef1e | 74 | void OnEndDrag(wxTreeEvent& event); |
5f939e78 VZ |
75 | void OnBeginLabelEdit(wxTreeEvent& event); |
76 | void OnEndLabelEdit(wxTreeEvent& event); | |
77 | void OnDeleteItem(wxTreeEvent& event); | |
ae8c4b33 | 78 | void OnContextMenu(wxContextMenuEvent& event); |
ef46ef23 | 79 | void OnItemMenu(wxTreeEvent& event); |
5f939e78 VZ |
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); | |
4754ab16 | 90 | void OnItemStateClick(wxTreeEvent& event); |
2a7eaf12 | 91 | void OnItemRClick(wxTreeEvent& event); |
c545c230 VZ |
92 | |
93 | void OnRMouseDown(wxMouseEvent& event); | |
94 | void OnRMouseUp(wxMouseEvent& event); | |
5f939e78 VZ |
95 | void OnRMouseDClick(wxMouseEvent& event); |
96 | ||
2d75caaa VZ |
97 | void GetItemsRecursively(const wxTreeItemId& idParent, |
98 | wxTreeItemIdValue cookie = 0); | |
5f939e78 | 99 | |
dee1b66f | 100 | void CreateImageList(int size = 16); |
618a5e38 | 101 | void CreateButtonsImageList(int size = 11); |
4754ab16 | 102 | void CreateStateImageList(bool del = false); |
2c8e4738 | 103 | |
5f939e78 VZ |
104 | void AddTestItemsToTree(size_t numChildren, size_t depth); |
105 | ||
9013f513 | 106 | void DoSortChildren(const wxTreeItemId& item, bool reverse = false) |
9dfbf520 | 107 | { m_reverseSort = reverse; wxTreeCtrl::SortChildren(item); } |
1cf1238d | 108 | void DoEnsureVisible() { if (m_lastItem.IsOk()) EnsureVisible(m_lastItem); } |
e1ee62bd | 109 | |
9dfbf520 | 110 | void DoToggleIcon(const wxTreeItemId& item); |
4754ab16 | 111 | void DoToggleState(const wxTreeItemId& item); |
9dfbf520 | 112 | |
e3d0aa77 VZ |
113 | void ShowMenu(wxTreeItemId id, const wxPoint& pt); |
114 | ||
618a5e38 RR |
115 | int ImageSize(void) const { return m_imageSize; } |
116 | ||
1cf1238d JS |
117 | void SetLastItem(wxTreeItemId id) { m_lastItem = id; } |
118 | ||
52734360 VZ |
119 | void SetAlternateImages(bool show) { m_alternateImages = show; } |
120 | bool AlternateImages() const { return m_alternateImages; } | |
121 | ||
4754ab16 RR |
122 | void SetAlternateStates(bool show) { m_alternateStates = show; } |
123 | bool AlternateStates() const { return m_alternateStates; } | |
124 | ||
df4a099c VZ |
125 | void ResetBrokenStateImages() |
126 | { | |
127 | const size_t count = GetStateImageList()->GetImageCount(); | |
128 | int state = count > 0 ? count - 1 : wxTREE_ITEMSTATE_NONE; | |
129 | DoResetBrokenStateImages(GetRootItem(), 0, state); | |
130 | } | |
131 | ||
e1ee62bd | 132 | protected: |
5f939e78 | 133 | virtual int OnCompareItems(const wxTreeItemId& i1, const wxTreeItemId& i2); |
e1ee62bd | 134 | |
5ea47806 VZ |
135 | // is this the test item which we use in several event handlers? |
136 | bool IsTestItem(const wxTreeItemId& item) | |
137 | { | |
138 | // the test item is the first child folder | |
99006e44 | 139 | return GetItemParent(item) == GetRootItem() && !GetPrevSibling(item); |
5ea47806 VZ |
140 | } |
141 | ||
5a43154b | 142 | private: |
5f939e78 VZ |
143 | void AddItemsRecursively(const wxTreeItemId& idParent, |
144 | size_t nChildren, | |
145 | size_t depth, | |
146 | size_t folder); | |
147 | ||
df4a099c VZ |
148 | void DoResetBrokenStateImages(const wxTreeItemId& idParent, |
149 | wxTreeItemIdValue cookie, int state); | |
150 | ||
5c2603c2 VZ |
151 | void LogEvent(const wxChar *name, const wxTreeEvent& event); |
152 | ||
618a5e38 | 153 | int m_imageSize; // current size of images |
5888ef1e VZ |
154 | bool m_reverseSort; // flag for OnCompareItems |
155 | wxTreeItemId m_lastItem, // for OnEnsureVisible() | |
156 | m_draggedItem; // item being dragged right now | |
52734360 | 157 | bool m_alternateImages; |
4754ab16 | 158 | bool m_alternateStates; |
5f939e78 VZ |
159 | |
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() | |
457814b5 JS |
166 | }; |
167 | ||
168 | // Define a new frame type | |
169 | class MyFrame: public wxFrame | |
019bf128 UU |
170 | { |
171 | public: | |
5f939e78 VZ |
172 | // ctor and dtor |
173 | MyFrame(const wxString& title, int x, int y, int w, int h); | |
174 | virtual ~MyFrame(); | |
f135ff73 | 175 | |
5f939e78 VZ |
176 | // menu callbacks |
177 | void OnQuit(wxCommandEvent& event); | |
178 | void OnAbout(wxCommandEvent& event); | |
f87a111f | 179 | void OnClearLog(wxCommandEvent& event); |
f135ff73 | 180 | |
11ab1dfb VZ |
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); } | |
197 | ||
198 | void OnResetStyle(wxCommandEvent& WXUNUSED(event)) | |
199 | { CreateTreeWithDefStyle(); } | |
618a5e38 | 200 | |
d88a2485 VZ |
201 | void OnSetFgColour(wxCommandEvent& event); |
202 | void OnSetBgColour(wxCommandEvent& event); | |
203 | ||
e525c4ff | 204 | void OnHighlight(wxCommandEvent& event); |
5f939e78 | 205 | void OnDump(wxCommandEvent& event); |
9dfbf520 | 206 | #ifndef NO_MULTIPLE_SELECTION |
91b8de8d | 207 | void OnDumpSelected(wxCommandEvent& event); |
9dfbf520 VZ |
208 | void OnSelect(wxCommandEvent& event); |
209 | void OnUnselect(wxCommandEvent& event); | |
7cc8c988 | 210 | void OnToggleSel(wxCommandEvent& event); |
5cb3a695 | 211 | void OnSelectChildren(wxCommandEvent& event); |
9dfbf520 | 212 | #endif // NO_MULTIPLE_SELECTION |
dc631754 | 213 | void OnSelectRoot(wxCommandEvent& event); |
5708ae18 VZ |
214 | void OnSetFocusedRoot(wxCommandEvent& event); |
215 | void OnClearFocused(wxCommandEvent& event); | |
5f939e78 VZ |
216 | void OnDelete(wxCommandEvent& event); |
217 | void OnDeleteChildren(wxCommandEvent& event); | |
218 | void OnDeleteAll(wxCommandEvent& event); | |
2c8e4738 | 219 | |
5f939e78 | 220 | void OnRecreate(wxCommandEvent& event); |
618a5e38 | 221 | void OnToggleButtons(wxCommandEvent& event); |
2c8e4738 | 222 | void OnToggleImages(wxCommandEvent& event); |
4754ab16 | 223 | void OnToggleStates(wxCommandEvent& event); |
27bc9194 | 224 | void OnToggleBell(wxCommandEvent& event); |
52734360 | 225 | void OnToggleAlternateImages(wxCommandEvent& event); |
4754ab16 | 226 | void OnToggleAlternateStates(wxCommandEvent& event); |
2c8e4738 | 227 | void OnSetImageSize(wxCommandEvent& event); |
5f939e78 | 228 | void OnCollapseAndReset(wxCommandEvent& event); |
add28c55 | 229 | |
9013f513 WS |
230 | void OnSetBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(true); } |
231 | void OnClearBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(false); } | |
f65635b5 | 232 | |
5f939e78 | 233 | void OnEnsureVisible(wxCommandEvent& event); |
5cfb5f66 | 234 | void OnSetFocus(wxCommandEvent& event); |
5f939e78 | 235 | |
5fd11f09 RR |
236 | void OnCount(wxCommandEvent& event); |
237 | void OnCountRec(wxCommandEvent& event); | |
75c74ca0 | 238 | |
5f939e78 | 239 | void OnRename(wxCommandEvent& event); |
cf36ec25 | 240 | void OnSort(wxCommandEvent& WXUNUSED(event)) { DoSort(); } |
9013f513 | 241 | void OnSortRev(wxCommandEvent& WXUNUSED(event)) { DoSort(true); } |
5f939e78 VZ |
242 | |
243 | void OnAddItem(wxCommandEvent& event); | |
4e1e8dc5 | 244 | void OnAddManyItems(wxCommandEvent& event); |
75c74ca0 | 245 | void OnInsertItem(wxCommandEvent& event); |
9dfbf520 | 246 | |
cf724bce RR |
247 | void OnIncIndent(wxCommandEvent& event); |
248 | void OnDecIndent(wxCommandEvent& event); | |
249 | ||
250 | void OnIncSpacing(wxCommandEvent& event); | |
251 | void OnDecSpacing(wxCommandEvent& event); | |
e1ee62bd | 252 | |
9dfbf520 | 253 | void OnToggleIcon(wxCommandEvent& event); |
4754ab16 | 254 | void OnToggleState(wxCommandEvent& event); |
9dfbf520 | 255 | |
f73eddd2 VZ |
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 | |
262 | ||
263 | void OnShowNextVisible(wxCommandEvent& WXUNUSED(event)) | |
0c6afdbf | 264 | { DoShowRelativeItem(&wxTreeCtrl::GetNextVisible, "next visible"); } |
f73eddd2 | 265 | void OnShowPrevVisible(wxCommandEvent& WXUNUSED(event)) |
0c6afdbf VZ |
266 | { DoShowRelativeItem(&wxTreeCtrl::GetPrevVisible, "previous visible"); } |
267 | ||
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"); } | |
f73eddd2 | 274 | |
51a5c8df | 275 | void OnScrollTo(wxCommandEvent& event); |
e95f0816 | 276 | void OnSelectLast(wxCommandEvent& event); |
51a5c8df | 277 | |
a67c5664 | 278 | void OnIdle(wxIdleEvent& event); |
7cc8c988 VZ |
279 | void OnSize(wxSizeEvent& event); |
280 | ||
f135ff73 | 281 | private: |
11ab1dfb | 282 | void TogStyle(int id, long flag); |
618a5e38 | 283 | |
9013f513 | 284 | void DoSort(bool reverse = false); |
e1ee62bd | 285 | |
11ab1dfb VZ |
286 | void Resize(); |
287 | ||
288 | void CreateTreeWithDefStyle(); | |
289 | void CreateTree(long style); | |
7cc8c988 | 290 | |
f73eddd2 VZ |
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); | |
294 | ||
295 | typedef wxTreeItemId (wxTreeCtrl::*TreeFunc1_t)(const wxTreeItemId&) const; | |
0c6afdbf | 296 | void DoShowRelativeItem(TreeFunc1_t pfn, const wxString& label); |
f73eddd2 VZ |
297 | |
298 | ||
998cee67 | 299 | wxPanel *m_panel; |
5f939e78 | 300 | MyTreeCtrl *m_treeCtrl; |
b29903d4 | 301 | #if wxUSE_LOG |
7cc8c988 | 302 | wxTextCtrl *m_textCtrl; |
b29903d4 | 303 | #endif // wxUSE_LOG |
457814b5 | 304 | |
9013f513 | 305 | void DoSetBold(bool bold = true); |
add28c55 | 306 | |
5f939e78 | 307 | DECLARE_EVENT_TABLE() |
5a43154b | 308 | }; |
457814b5 | 309 | |
5a43154b VZ |
310 | // menu and control ids |
311 | enum | |
312 | { | |
525bc4fc JS |
313 | TreeTest_Quit = wxID_EXIT, |
314 | TreeTest_About = wxID_ABOUT, | |
f87a111f | 315 | TreeTest_ClearLog = wxID_CLEAR, |
525bc4fc | 316 | TreeTest_TogButtons = wxID_HIGHEST, |
618a5e38 RR |
317 | TreeTest_TogTwist, |
318 | TreeTest_TogLines, | |
319 | TreeTest_TogEdit, | |
320 | TreeTest_TogHideRoot, | |
321 | TreeTest_TogRootLines, | |
322 | TreeTest_TogBorder, | |
33984936 | 323 | TreeTest_TogFullHighlight, |
d88a2485 VZ |
324 | TreeTest_SetFgColour, |
325 | TreeTest_SetBgColour, | |
11ab1dfb | 326 | TreeTest_ResetStyle, |
e525c4ff | 327 | TreeTest_Highlight, |
5f939e78 | 328 | TreeTest_Dump, |
9dfbf520 | 329 | TreeTest_DumpSelected, |
5fd11f09 RR |
330 | TreeTest_Count, |
331 | TreeTest_CountRec, | |
5f939e78 VZ |
332 | TreeTest_Sort, |
333 | TreeTest_SortRev, | |
618a5e38 RR |
334 | TreeTest_SetBold, |
335 | TreeTest_ClearBold, | |
5f939e78 VZ |
336 | TreeTest_Rename, |
337 | TreeTest_Delete, | |
338 | TreeTest_DeleteChildren, | |
339 | TreeTest_DeleteAll, | |
340 | TreeTest_Recreate, | |
2c8e4738 | 341 | TreeTest_ToggleImages, |
4754ab16 | 342 | TreeTest_ToggleStates, |
27bc9194 | 343 | TreeTest_ToggleBell, |
52734360 | 344 | TreeTest_ToggleAlternateImages, |
4754ab16 | 345 | TreeTest_ToggleAlternateStates, |
618a5e38 | 346 | TreeTest_ToggleButtons, |
2c8e4738 | 347 | TreeTest_SetImageSize, |
7cc8c988 | 348 | TreeTest_ToggleSel, |
5f939e78 VZ |
349 | TreeTest_CollapseAndReset, |
350 | TreeTest_EnsureVisible, | |
5cfb5f66 | 351 | TreeTest_SetFocus, |
5f939e78 | 352 | TreeTest_AddItem, |
4e1e8dc5 | 353 | TreeTest_AddManyItems, |
75c74ca0 | 354 | TreeTest_InsertItem, |
cf724bce RR |
355 | TreeTest_IncIndent, |
356 | TreeTest_DecIndent, | |
357 | TreeTest_IncSpacing, | |
358 | TreeTest_DecSpacing, | |
9dfbf520 | 359 | TreeTest_ToggleIcon, |
4754ab16 | 360 | TreeTest_ToggleState, |
9dfbf520 VZ |
361 | TreeTest_Select, |
362 | TreeTest_Unselect, | |
dc631754 | 363 | TreeTest_SelectRoot, |
5708ae18 VZ |
364 | TreeTest_ClearFocused, |
365 | TreeTest_SetFocusedRoot, | |
5cb3a695 | 366 | TreeTest_SelectChildren, |
f73eddd2 VZ |
367 | TreeTest_ShowFirstVisible, |
368 | TreeTest_ShowLastVisible, | |
369 | TreeTest_ShowNextVisible, | |
370 | TreeTest_ShowPrevVisible, | |
0c6afdbf VZ |
371 | TreeTest_ShowParent, |
372 | TreeTest_ShowPrevSibling, | |
373 | TreeTest_ShowNextSibling, | |
51a5c8df | 374 | TreeTest_ScrollTo, |
e95f0816 | 375 | TreeTest_SelectLast, |
9dfbf520 | 376 | TreeTest_Ctrl = 1000 |
f65635b5 | 377 | }; |