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