]>
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: | |
9013f513 | 26 | MyApp() { m_showImages = 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 | ||
618a5e38 RR |
33 | void SetShowButtons(bool show) { m_showButtons = show; } |
34 | bool ShowButtons() const { return m_showButtons; } | |
35 | ||
2c8e4738 | 36 | private: |
618a5e38 | 37 | bool m_showImages, m_showButtons; |
5a43154b | 38 | }; |
457814b5 | 39 | |
5a43154b VZ |
40 | class MyTreeItemData : public wxTreeItemData |
41 | { | |
42 | public: | |
5f939e78 | 43 | MyTreeItemData(const wxString& desc) : m_desc(desc) { } |
5a43154b | 44 | |
5f939e78 | 45 | void ShowInfo(wxTreeCtrl *tree); |
4693b20c | 46 | const wxChar *GetDesc() const { return m_desc.c_str(); } |
5a43154b VZ |
47 | |
48 | private: | |
5f939e78 | 49 | wxString m_desc; |
457814b5 JS |
50 | }; |
51 | ||
5a43154b | 52 | class MyTreeCtrl : public wxTreeCtrl |
457814b5 JS |
53 | { |
54 | public: | |
5f939e78 VZ |
55 | enum |
56 | { | |
57 | TreeCtrlIcon_File, | |
74b31181 VZ |
58 | TreeCtrlIcon_FileSelected, |
59 | TreeCtrlIcon_Folder, | |
60 | TreeCtrlIcon_FolderSelected, | |
61 | TreeCtrlIcon_FolderOpened | |
5f939e78 VZ |
62 | }; |
63 | ||
52734360 | 64 | MyTreeCtrl() { m_alternateImages = false; } |
5f939e78 VZ |
65 | MyTreeCtrl(wxWindow *parent, const wxWindowID id, |
66 | const wxPoint& pos, const wxSize& size, | |
67 | long style); | |
925e9792 | 68 | virtual ~MyTreeCtrl(){}; |
5f939e78 VZ |
69 | |
70 | void OnBeginDrag(wxTreeEvent& event); | |
71 | void OnBeginRDrag(wxTreeEvent& event); | |
5888ef1e | 72 | void OnEndDrag(wxTreeEvent& event); |
5f939e78 VZ |
73 | void OnBeginLabelEdit(wxTreeEvent& event); |
74 | void OnEndLabelEdit(wxTreeEvent& event); | |
75 | void OnDeleteItem(wxTreeEvent& event); | |
ae8c4b33 | 76 | void OnContextMenu(wxContextMenuEvent& event); |
ef46ef23 | 77 | void OnItemMenu(wxTreeEvent& event); |
5f939e78 VZ |
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); | |
2a7eaf12 | 88 | void OnItemRClick(wxTreeEvent& event); |
c545c230 VZ |
89 | |
90 | void OnRMouseDown(wxMouseEvent& event); | |
91 | void OnRMouseUp(wxMouseEvent& event); | |
5f939e78 VZ |
92 | void OnRMouseDClick(wxMouseEvent& event); |
93 | ||
2d75caaa VZ |
94 | void GetItemsRecursively(const wxTreeItemId& idParent, |
95 | wxTreeItemIdValue cookie = 0); | |
5f939e78 | 96 | |
dee1b66f | 97 | void CreateImageList(int size = 16); |
618a5e38 | 98 | void CreateButtonsImageList(int size = 11); |
2c8e4738 | 99 | |
5f939e78 VZ |
100 | void AddTestItemsToTree(size_t numChildren, size_t depth); |
101 | ||
9013f513 | 102 | void DoSortChildren(const wxTreeItemId& item, bool reverse = false) |
9dfbf520 | 103 | { m_reverseSort = reverse; wxTreeCtrl::SortChildren(item); } |
1cf1238d | 104 | void DoEnsureVisible() { if (m_lastItem.IsOk()) EnsureVisible(m_lastItem); } |
e1ee62bd | 105 | |
9dfbf520 VZ |
106 | void DoToggleIcon(const wxTreeItemId& item); |
107 | ||
e3d0aa77 VZ |
108 | void ShowMenu(wxTreeItemId id, const wxPoint& pt); |
109 | ||
618a5e38 RR |
110 | int ImageSize(void) const { return m_imageSize; } |
111 | ||
1cf1238d JS |
112 | void SetLastItem(wxTreeItemId id) { m_lastItem = id; } |
113 | ||
52734360 VZ |
114 | void SetAlternateImages(bool show) { m_alternateImages = show; } |
115 | bool AlternateImages() const { return m_alternateImages; } | |
116 | ||
e1ee62bd | 117 | protected: |
5f939e78 | 118 | virtual int OnCompareItems(const wxTreeItemId& i1, const wxTreeItemId& i2); |
e1ee62bd | 119 | |
5ea47806 VZ |
120 | // is this the test item which we use in several event handlers? |
121 | bool IsTestItem(const wxTreeItemId& item) | |
122 | { | |
123 | // the test item is the first child folder | |
99006e44 | 124 | return GetItemParent(item) == GetRootItem() && !GetPrevSibling(item); |
5ea47806 VZ |
125 | } |
126 | ||
5a43154b | 127 | private: |
5f939e78 VZ |
128 | void AddItemsRecursively(const wxTreeItemId& idParent, |
129 | size_t nChildren, | |
130 | size_t depth, | |
131 | size_t folder); | |
132 | ||
5c2603c2 VZ |
133 | void LogEvent(const wxChar *name, const wxTreeEvent& event); |
134 | ||
618a5e38 | 135 | int m_imageSize; // current size of images |
5888ef1e VZ |
136 | bool m_reverseSort; // flag for OnCompareItems |
137 | wxTreeItemId m_lastItem, // for OnEnsureVisible() | |
138 | m_draggedItem; // item being dragged right now | |
52734360 | 139 | bool m_alternateImages; |
5f939e78 VZ |
140 | |
141 | // NB: due to an ugly wxMSW hack you _must_ use DECLARE_DYNAMIC_CLASS() | |
142 | // if you want your overloaded OnCompareItems() to be called. | |
143 | // OTOH, if you don't want it you may omit the next line - this will | |
144 | // make default (alphabetical) sorting much faster under wxMSW. | |
145 | DECLARE_DYNAMIC_CLASS(MyTreeCtrl) | |
146 | DECLARE_EVENT_TABLE() | |
457814b5 JS |
147 | }; |
148 | ||
149 | // Define a new frame type | |
150 | class MyFrame: public wxFrame | |
019bf128 UU |
151 | { |
152 | public: | |
5f939e78 VZ |
153 | // ctor and dtor |
154 | MyFrame(const wxString& title, int x, int y, int w, int h); | |
155 | virtual ~MyFrame(); | |
f135ff73 | 156 | |
5f939e78 VZ |
157 | // menu callbacks |
158 | void OnQuit(wxCommandEvent& event); | |
159 | void OnAbout(wxCommandEvent& event); | |
f87a111f | 160 | void OnClearLog(wxCommandEvent& event); |
f135ff73 | 161 | |
11ab1dfb VZ |
162 | void OnTogButtons(wxCommandEvent& event) |
163 | { TogStyle(event.GetId(), wxTR_HAS_BUTTONS); } | |
164 | void OnTogTwist(wxCommandEvent& event) | |
165 | { TogStyle(event.GetId(), wxTR_TWIST_BUTTONS); } | |
166 | void OnTogLines(wxCommandEvent& event) | |
167 | { TogStyle(event.GetId(), wxTR_NO_LINES); } | |
168 | void OnTogEdit(wxCommandEvent& event) | |
169 | { TogStyle(event.GetId(), wxTR_EDIT_LABELS); } | |
170 | void OnTogHideRoot(wxCommandEvent& event) | |
171 | { TogStyle(event.GetId(), wxTR_HIDE_ROOT); } | |
172 | void OnTogRootLines(wxCommandEvent& event) | |
173 | { TogStyle(event.GetId(), wxTR_LINES_AT_ROOT); } | |
174 | void OnTogBorder(wxCommandEvent& event) | |
175 | { TogStyle(event.GetId(), wxTR_ROW_LINES); } | |
176 | void OnTogFullHighlight(wxCommandEvent& event) | |
177 | { TogStyle(event.GetId(), wxTR_FULL_ROW_HIGHLIGHT); } | |
178 | ||
179 | void OnResetStyle(wxCommandEvent& WXUNUSED(event)) | |
180 | { CreateTreeWithDefStyle(); } | |
618a5e38 | 181 | |
d88a2485 VZ |
182 | void OnSetFgColour(wxCommandEvent& event); |
183 | void OnSetBgColour(wxCommandEvent& event); | |
184 | ||
e525c4ff | 185 | void OnHighlight(wxCommandEvent& event); |
5f939e78 | 186 | void OnDump(wxCommandEvent& event); |
9dfbf520 | 187 | #ifndef NO_MULTIPLE_SELECTION |
91b8de8d | 188 | void OnDumpSelected(wxCommandEvent& event); |
9dfbf520 VZ |
189 | void OnSelect(wxCommandEvent& event); |
190 | void OnUnselect(wxCommandEvent& event); | |
7cc8c988 | 191 | void OnToggleSel(wxCommandEvent& event); |
9dfbf520 | 192 | #endif // NO_MULTIPLE_SELECTION |
dc631754 | 193 | void OnSelectRoot(wxCommandEvent& event); |
5f939e78 VZ |
194 | void OnDelete(wxCommandEvent& event); |
195 | void OnDeleteChildren(wxCommandEvent& event); | |
196 | void OnDeleteAll(wxCommandEvent& event); | |
2c8e4738 | 197 | |
5f939e78 | 198 | void OnRecreate(wxCommandEvent& event); |
618a5e38 | 199 | void OnToggleButtons(wxCommandEvent& event); |
2c8e4738 | 200 | void OnToggleImages(wxCommandEvent& event); |
52734360 | 201 | void OnToggleAlternateImages(wxCommandEvent& event); |
2c8e4738 | 202 | void OnSetImageSize(wxCommandEvent& event); |
5f939e78 | 203 | void OnCollapseAndReset(wxCommandEvent& event); |
add28c55 | 204 | |
9013f513 WS |
205 | void OnSetBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(true); } |
206 | void OnClearBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(false); } | |
f65635b5 | 207 | |
5f939e78 VZ |
208 | void OnEnsureVisible(wxCommandEvent& event); |
209 | ||
5fd11f09 RR |
210 | void OnCount(wxCommandEvent& event); |
211 | void OnCountRec(wxCommandEvent& event); | |
75c74ca0 | 212 | |
5f939e78 | 213 | void OnRename(wxCommandEvent& event); |
cf36ec25 | 214 | void OnSort(wxCommandEvent& WXUNUSED(event)) { DoSort(); } |
9013f513 | 215 | void OnSortRev(wxCommandEvent& WXUNUSED(event)) { DoSort(true); } |
5f939e78 VZ |
216 | |
217 | void OnAddItem(wxCommandEvent& event); | |
75c74ca0 | 218 | void OnInsertItem(wxCommandEvent& event); |
9dfbf520 | 219 | |
cf724bce RR |
220 | void OnIncIndent(wxCommandEvent& event); |
221 | void OnDecIndent(wxCommandEvent& event); | |
222 | ||
223 | void OnIncSpacing(wxCommandEvent& event); | |
224 | void OnDecSpacing(wxCommandEvent& event); | |
e1ee62bd | 225 | |
9dfbf520 VZ |
226 | void OnToggleIcon(wxCommandEvent& event); |
227 | ||
f73eddd2 VZ |
228 | void OnShowFirstVisible(wxCommandEvent& WXUNUSED(event)) |
229 | { DoShowFirstOrLast(&wxTreeCtrl::GetFirstVisibleItem, "first visible"); } | |
230 | #ifdef wxHAS_LAST_VISIBLE // we don't have it currently but may add later | |
231 | void OnShowLastVisible(wxCommandEvent& WXUNUSED(event)) | |
232 | { DoShowFirstOrLast(&wxTreeCtrl::GetLastVisibleItem, "last visible"); } | |
233 | #endif // wxHAS_LAST_VISIBLE | |
234 | ||
235 | void OnShowNextVisible(wxCommandEvent& WXUNUSED(event)) | |
236 | { DoShowNextOrPrev(&wxTreeCtrl::GetNextVisible, "next visible"); } | |
237 | void OnShowPrevVisible(wxCommandEvent& WXUNUSED(event)) | |
238 | { DoShowNextOrPrev(&wxTreeCtrl::GetPrevVisible, "previous visible"); } | |
239 | ||
a67c5664 | 240 | void OnIdle(wxIdleEvent& event); |
7cc8c988 VZ |
241 | void OnSize(wxSizeEvent& event); |
242 | ||
f135ff73 | 243 | private: |
11ab1dfb | 244 | void TogStyle(int id, long flag); |
618a5e38 | 245 | |
9013f513 | 246 | void DoSort(bool reverse = false); |
e1ee62bd | 247 | |
11ab1dfb VZ |
248 | void Resize(); |
249 | ||
250 | void CreateTreeWithDefStyle(); | |
251 | void CreateTree(long style); | |
7cc8c988 | 252 | |
f73eddd2 VZ |
253 | // common parts of OnShowFirst/LastVisible() and OnShowNext/PrevVisible() |
254 | typedef wxTreeItemId (wxTreeCtrl::*TreeFunc0_t)() const; | |
255 | void DoShowFirstOrLast(TreeFunc0_t pfn, const wxString& label); | |
256 | ||
257 | typedef wxTreeItemId (wxTreeCtrl::*TreeFunc1_t)(const wxTreeItemId&) const; | |
258 | void DoShowNextOrPrev(TreeFunc1_t pfn, const wxString& label); | |
259 | ||
260 | ||
998cee67 | 261 | wxPanel *m_panel; |
5f939e78 | 262 | MyTreeCtrl *m_treeCtrl; |
b29903d4 | 263 | #if wxUSE_LOG |
7cc8c988 | 264 | wxTextCtrl *m_textCtrl; |
b29903d4 | 265 | #endif // wxUSE_LOG |
457814b5 | 266 | |
9013f513 | 267 | void DoSetBold(bool bold = true); |
add28c55 | 268 | |
5f939e78 | 269 | DECLARE_EVENT_TABLE() |
5a43154b | 270 | }; |
457814b5 | 271 | |
5a43154b VZ |
272 | // menu and control ids |
273 | enum | |
274 | { | |
525bc4fc JS |
275 | TreeTest_Quit = wxID_EXIT, |
276 | TreeTest_About = wxID_ABOUT, | |
f87a111f | 277 | TreeTest_ClearLog = wxID_CLEAR, |
525bc4fc | 278 | TreeTest_TogButtons = wxID_HIGHEST, |
618a5e38 RR |
279 | TreeTest_TogTwist, |
280 | TreeTest_TogLines, | |
281 | TreeTest_TogEdit, | |
282 | TreeTest_TogHideRoot, | |
283 | TreeTest_TogRootLines, | |
284 | TreeTest_TogBorder, | |
33984936 | 285 | TreeTest_TogFullHighlight, |
d88a2485 VZ |
286 | TreeTest_SetFgColour, |
287 | TreeTest_SetBgColour, | |
11ab1dfb | 288 | TreeTest_ResetStyle, |
e525c4ff | 289 | TreeTest_Highlight, |
5f939e78 | 290 | TreeTest_Dump, |
9dfbf520 | 291 | TreeTest_DumpSelected, |
5fd11f09 RR |
292 | TreeTest_Count, |
293 | TreeTest_CountRec, | |
5f939e78 VZ |
294 | TreeTest_Sort, |
295 | TreeTest_SortRev, | |
618a5e38 RR |
296 | TreeTest_SetBold, |
297 | TreeTest_ClearBold, | |
5f939e78 VZ |
298 | TreeTest_Rename, |
299 | TreeTest_Delete, | |
300 | TreeTest_DeleteChildren, | |
301 | TreeTest_DeleteAll, | |
302 | TreeTest_Recreate, | |
2c8e4738 | 303 | TreeTest_ToggleImages, |
52734360 | 304 | TreeTest_ToggleAlternateImages, |
618a5e38 | 305 | TreeTest_ToggleButtons, |
2c8e4738 | 306 | TreeTest_SetImageSize, |
7cc8c988 | 307 | TreeTest_ToggleSel, |
5f939e78 VZ |
308 | TreeTest_CollapseAndReset, |
309 | TreeTest_EnsureVisible, | |
310 | TreeTest_AddItem, | |
75c74ca0 | 311 | TreeTest_InsertItem, |
cf724bce RR |
312 | TreeTest_IncIndent, |
313 | TreeTest_DecIndent, | |
314 | TreeTest_IncSpacing, | |
315 | TreeTest_DecSpacing, | |
9dfbf520 VZ |
316 | TreeTest_ToggleIcon, |
317 | TreeTest_Select, | |
318 | TreeTest_Unselect, | |
dc631754 | 319 | TreeTest_SelectRoot, |
f73eddd2 VZ |
320 | TreeTest_ShowFirstVisible, |
321 | TreeTest_ShowLastVisible, | |
322 | TreeTest_ShowNextVisible, | |
323 | TreeTest_ShowPrevVisible, | |
9dfbf520 | 324 | TreeTest_Ctrl = 1000 |
f65635b5 | 325 | }; |