]>
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 | protected: | |
127 | virtual int OnCompareItems(const wxTreeItemId& i1, const wxTreeItemId& i2); | |
128 | ||
129 | // is this the test item which we use in several event handlers? | |
130 | bool IsTestItem(const wxTreeItemId& item) | |
131 | { | |
132 | // the test item is the first child folder | |
133 | return GetItemParent(item) == GetRootItem() && !GetPrevSibling(item); | |
134 | } | |
135 | ||
136 | private: | |
137 | void AddItemsRecursively(const wxTreeItemId& idParent, | |
138 | size_t nChildren, | |
139 | size_t depth, | |
140 | size_t folder); | |
141 | ||
142 | void LogEvent(const wxChar *name, const wxTreeEvent& event); | |
143 | ||
144 | int m_imageSize; // current size of images | |
145 | bool m_reverseSort; // flag for OnCompareItems | |
146 | wxTreeItemId m_lastItem, // for OnEnsureVisible() | |
147 | m_draggedItem; // item being dragged right now | |
148 | bool m_alternateImages; | |
149 | bool m_alternateStates; | |
150 | ||
151 | // NB: due to an ugly wxMSW hack you _must_ use DECLARE_DYNAMIC_CLASS() | |
152 | // if you want your overloaded OnCompareItems() to be called. | |
153 | // OTOH, if you don't want it you may omit the next line - this will | |
154 | // make default (alphabetical) sorting much faster under wxMSW. | |
155 | DECLARE_DYNAMIC_CLASS(MyTreeCtrl) | |
156 | DECLARE_EVENT_TABLE() | |
157 | }; | |
158 | ||
159 | // Define a new frame type | |
160 | class MyFrame: public wxFrame | |
161 | { | |
162 | public: | |
163 | // ctor and dtor | |
164 | MyFrame(const wxString& title, int x, int y, int w, int h); | |
165 | virtual ~MyFrame(); | |
166 | ||
167 | // menu callbacks | |
168 | void OnQuit(wxCommandEvent& event); | |
169 | void OnAbout(wxCommandEvent& event); | |
170 | void OnClearLog(wxCommandEvent& event); | |
171 | ||
172 | void OnTogButtons(wxCommandEvent& event) | |
173 | { TogStyle(event.GetId(), wxTR_HAS_BUTTONS); } | |
174 | void OnTogTwist(wxCommandEvent& event) | |
175 | { TogStyle(event.GetId(), wxTR_TWIST_BUTTONS); } | |
176 | void OnTogLines(wxCommandEvent& event) | |
177 | { TogStyle(event.GetId(), wxTR_NO_LINES); } | |
178 | void OnTogEdit(wxCommandEvent& event) | |
179 | { TogStyle(event.GetId(), wxTR_EDIT_LABELS); } | |
180 | void OnTogHideRoot(wxCommandEvent& event) | |
181 | { TogStyle(event.GetId(), wxTR_HIDE_ROOT); } | |
182 | void OnTogRootLines(wxCommandEvent& event) | |
183 | { TogStyle(event.GetId(), wxTR_LINES_AT_ROOT); } | |
184 | void OnTogBorder(wxCommandEvent& event) | |
185 | { TogStyle(event.GetId(), wxTR_ROW_LINES); } | |
186 | void OnTogFullHighlight(wxCommandEvent& event) | |
187 | { TogStyle(event.GetId(), wxTR_FULL_ROW_HIGHLIGHT); } | |
188 | ||
189 | void OnResetStyle(wxCommandEvent& WXUNUSED(event)) | |
190 | { CreateTreeWithDefStyle(); } | |
191 | ||
192 | void OnSetFgColour(wxCommandEvent& event); | |
193 | void OnSetBgColour(wxCommandEvent& event); | |
194 | ||
195 | void OnHighlight(wxCommandEvent& event); | |
196 | void OnDump(wxCommandEvent& event); | |
197 | #ifndef NO_MULTIPLE_SELECTION | |
198 | void OnDumpSelected(wxCommandEvent& event); | |
199 | void OnSelect(wxCommandEvent& event); | |
200 | void OnUnselect(wxCommandEvent& event); | |
201 | void OnToggleSel(wxCommandEvent& event); | |
202 | #endif // NO_MULTIPLE_SELECTION | |
203 | void OnSelectRoot(wxCommandEvent& event); | |
204 | void OnDelete(wxCommandEvent& event); | |
205 | void OnDeleteChildren(wxCommandEvent& event); | |
206 | void OnDeleteAll(wxCommandEvent& event); | |
207 | ||
208 | void OnRecreate(wxCommandEvent& event); | |
209 | void OnToggleButtons(wxCommandEvent& event); | |
210 | void OnToggleImages(wxCommandEvent& event); | |
211 | void OnToggleStates(wxCommandEvent& event); | |
212 | void OnToggleAlternateImages(wxCommandEvent& event); | |
213 | void OnToggleAlternateStates(wxCommandEvent& event); | |
214 | void OnSetImageSize(wxCommandEvent& event); | |
215 | void OnCollapseAndReset(wxCommandEvent& event); | |
216 | ||
217 | void OnSetBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(true); } | |
218 | void OnClearBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(false); } | |
219 | ||
220 | void OnEnsureVisible(wxCommandEvent& event); | |
221 | ||
222 | void OnCount(wxCommandEvent& event); | |
223 | void OnCountRec(wxCommandEvent& event); | |
224 | ||
225 | void OnRename(wxCommandEvent& event); | |
226 | void OnSort(wxCommandEvent& WXUNUSED(event)) { DoSort(); } | |
227 | void OnSortRev(wxCommandEvent& WXUNUSED(event)) { DoSort(true); } | |
228 | ||
229 | void OnAddItem(wxCommandEvent& event); | |
230 | void OnInsertItem(wxCommandEvent& event); | |
231 | ||
232 | void OnIncIndent(wxCommandEvent& event); | |
233 | void OnDecIndent(wxCommandEvent& event); | |
234 | ||
235 | void OnIncSpacing(wxCommandEvent& event); | |
236 | void OnDecSpacing(wxCommandEvent& event); | |
237 | ||
238 | void OnToggleIcon(wxCommandEvent& event); | |
239 | void OnToggleState(wxCommandEvent& event); | |
240 | ||
241 | void OnShowFirstVisible(wxCommandEvent& WXUNUSED(event)) | |
242 | { DoShowFirstOrLast(&wxTreeCtrl::GetFirstVisibleItem, "first visible"); } | |
243 | #ifdef wxHAS_LAST_VISIBLE // we don't have it currently but may add later | |
244 | void OnShowLastVisible(wxCommandEvent& WXUNUSED(event)) | |
245 | { DoShowFirstOrLast(&wxTreeCtrl::GetLastVisibleItem, "last visible"); } | |
246 | #endif // wxHAS_LAST_VISIBLE | |
247 | ||
248 | void OnShowNextVisible(wxCommandEvent& WXUNUSED(event)) | |
249 | { DoShowNextOrPrev(&wxTreeCtrl::GetNextVisible, "next visible"); } | |
250 | void OnShowPrevVisible(wxCommandEvent& WXUNUSED(event)) | |
251 | { DoShowNextOrPrev(&wxTreeCtrl::GetPrevVisible, "previous visible"); } | |
252 | ||
253 | void OnIdle(wxIdleEvent& event); | |
254 | void OnSize(wxSizeEvent& event); | |
255 | ||
256 | private: | |
257 | void TogStyle(int id, long flag); | |
258 | ||
259 | void DoSort(bool reverse = false); | |
260 | ||
261 | void Resize(); | |
262 | ||
263 | void CreateTreeWithDefStyle(); | |
264 | void CreateTree(long style); | |
265 | ||
266 | // common parts of OnShowFirst/LastVisible() and OnShowNext/PrevVisible() | |
267 | typedef wxTreeItemId (wxTreeCtrl::*TreeFunc0_t)() const; | |
268 | void DoShowFirstOrLast(TreeFunc0_t pfn, const wxString& label); | |
269 | ||
270 | typedef wxTreeItemId (wxTreeCtrl::*TreeFunc1_t)(const wxTreeItemId&) const; | |
271 | void DoShowNextOrPrev(TreeFunc1_t pfn, const wxString& label); | |
272 | ||
273 | ||
274 | wxPanel *m_panel; | |
275 | MyTreeCtrl *m_treeCtrl; | |
276 | #if wxUSE_LOG | |
277 | wxTextCtrl *m_textCtrl; | |
278 | #endif // wxUSE_LOG | |
279 | ||
280 | void DoSetBold(bool bold = true); | |
281 | ||
282 | DECLARE_EVENT_TABLE() | |
283 | }; | |
284 | ||
285 | // menu and control ids | |
286 | enum | |
287 | { | |
288 | TreeTest_Quit = wxID_EXIT, | |
289 | TreeTest_About = wxID_ABOUT, | |
290 | TreeTest_ClearLog = wxID_CLEAR, | |
291 | TreeTest_TogButtons = wxID_HIGHEST, | |
292 | TreeTest_TogTwist, | |
293 | TreeTest_TogLines, | |
294 | TreeTest_TogEdit, | |
295 | TreeTest_TogHideRoot, | |
296 | TreeTest_TogRootLines, | |
297 | TreeTest_TogBorder, | |
298 | TreeTest_TogFullHighlight, | |
299 | TreeTest_SetFgColour, | |
300 | TreeTest_SetBgColour, | |
301 | TreeTest_ResetStyle, | |
302 | TreeTest_Highlight, | |
303 | TreeTest_Dump, | |
304 | TreeTest_DumpSelected, | |
305 | TreeTest_Count, | |
306 | TreeTest_CountRec, | |
307 | TreeTest_Sort, | |
308 | TreeTest_SortRev, | |
309 | TreeTest_SetBold, | |
310 | TreeTest_ClearBold, | |
311 | TreeTest_Rename, | |
312 | TreeTest_Delete, | |
313 | TreeTest_DeleteChildren, | |
314 | TreeTest_DeleteAll, | |
315 | TreeTest_Recreate, | |
316 | TreeTest_ToggleImages, | |
317 | TreeTest_ToggleStates, | |
318 | TreeTest_ToggleAlternateImages, | |
319 | TreeTest_ToggleAlternateStates, | |
320 | TreeTest_ToggleButtons, | |
321 | TreeTest_SetImageSize, | |
322 | TreeTest_ToggleSel, | |
323 | TreeTest_CollapseAndReset, | |
324 | TreeTest_EnsureVisible, | |
325 | TreeTest_AddItem, | |
326 | TreeTest_InsertItem, | |
327 | TreeTest_IncIndent, | |
328 | TreeTest_DecIndent, | |
329 | TreeTest_IncSpacing, | |
330 | TreeTest_DecSpacing, | |
331 | TreeTest_ToggleIcon, | |
332 | TreeTest_ToggleState, | |
333 | TreeTest_Select, | |
334 | TreeTest_Unselect, | |
335 | TreeTest_SelectRoot, | |
336 | TreeTest_ShowFirstVisible, | |
337 | TreeTest_ShowLastVisible, | |
338 | TreeTest_ShowNextVisible, | |
339 | TreeTest_ShowPrevVisible, | |
340 | TreeTest_Ctrl = 1000 | |
341 | }; |