]>
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$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
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: | |
618a5e38 | 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 | ||
64 | MyTreeCtrl() { } | |
65 | MyTreeCtrl(wxWindow *parent, const wxWindowID id, | |
66 | const wxPoint& pos, const wxSize& size, | |
67 | long style); | |
68 | virtual ~MyTreeCtrl(); | |
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); | |
e3d0aa77 | 76 | void OnRMouseUp(wxMouseEvent& event); |
5f939e78 | 77 | void OnGetInfo(wxTreeEvent& event); |
e3d0aa77 VZ |
78 | void OnTreeRMouseClick(wxTreeEvent& event); |
79 | void OnItemRightClick(wxTreeEvent& event); | |
5f939e78 VZ |
80 | void OnSetInfo(wxTreeEvent& event); |
81 | void OnItemExpanded(wxTreeEvent& event); | |
82 | void OnItemExpanding(wxTreeEvent& event); | |
83 | void OnItemCollapsed(wxTreeEvent& event); | |
84 | void OnItemCollapsing(wxTreeEvent& event); | |
85 | void OnSelChanged(wxTreeEvent& event); | |
86 | void OnSelChanging(wxTreeEvent& event); | |
87 | void OnTreeKeyDown(wxTreeEvent& event); | |
88 | void OnItemActivated(wxTreeEvent& event); | |
89 | void OnRMouseDClick(wxMouseEvent& event); | |
90 | ||
91 | void GetItemsRecursively(const wxTreeItemId& idParent, long cookie); | |
92 | ||
dee1b66f | 93 | void CreateImageList(int size = 16); |
618a5e38 | 94 | void CreateButtonsImageList(int size = 11); |
2c8e4738 | 95 | |
5f939e78 VZ |
96 | void AddTestItemsToTree(size_t numChildren, size_t depth); |
97 | ||
98 | void DoSortChildren(const wxTreeItemId& item, bool reverse = FALSE) | |
9dfbf520 | 99 | { m_reverseSort = reverse; wxTreeCtrl::SortChildren(item); } |
5f939e78 | 100 | void DoEnsureVisible() { EnsureVisible(m_lastItem); } |
e1ee62bd | 101 | |
9dfbf520 VZ |
102 | void DoToggleIcon(const wxTreeItemId& item); |
103 | ||
e3d0aa77 VZ |
104 | void ShowMenu(wxTreeItemId id, const wxPoint& pt); |
105 | ||
618a5e38 RR |
106 | int ImageSize(void) const { return m_imageSize; } |
107 | ||
e1ee62bd | 108 | protected: |
5f939e78 | 109 | virtual int OnCompareItems(const wxTreeItemId& i1, const wxTreeItemId& i2); |
e1ee62bd | 110 | |
5ea47806 VZ |
111 | // is this the test item which we use in several event handlers? |
112 | bool IsTestItem(const wxTreeItemId& item) | |
113 | { | |
114 | // the test item is the first child folder | |
115 | return GetParent(item) == GetRootItem() && !GetPrevSibling(item); | |
116 | } | |
117 | ||
5a43154b | 118 | private: |
5f939e78 VZ |
119 | void AddItemsRecursively(const wxTreeItemId& idParent, |
120 | size_t nChildren, | |
121 | size_t depth, | |
122 | size_t folder); | |
123 | ||
618a5e38 | 124 | int m_imageSize; // current size of images |
5888ef1e VZ |
125 | bool m_reverseSort; // flag for OnCompareItems |
126 | wxTreeItemId m_lastItem, // for OnEnsureVisible() | |
127 | m_draggedItem; // item being dragged right now | |
5f939e78 VZ |
128 | |
129 | // NB: due to an ugly wxMSW hack you _must_ use DECLARE_DYNAMIC_CLASS() | |
130 | // if you want your overloaded OnCompareItems() to be called. | |
131 | // OTOH, if you don't want it you may omit the next line - this will | |
132 | // make default (alphabetical) sorting much faster under wxMSW. | |
133 | DECLARE_DYNAMIC_CLASS(MyTreeCtrl) | |
134 | DECLARE_EVENT_TABLE() | |
457814b5 JS |
135 | }; |
136 | ||
137 | // Define a new frame type | |
138 | class MyFrame: public wxFrame | |
019bf128 UU |
139 | { |
140 | public: | |
5f939e78 VZ |
141 | // ctor and dtor |
142 | MyFrame(const wxString& title, int x, int y, int w, int h); | |
143 | virtual ~MyFrame(); | |
f135ff73 | 144 | |
5f939e78 VZ |
145 | // menu callbacks |
146 | void OnQuit(wxCommandEvent& event); | |
147 | void OnAbout(wxCommandEvent& event); | |
f135ff73 | 148 | |
618a5e38 RR |
149 | void OnTogButtons(wxCommandEvent& event) { TogStyle(wxTR_HAS_BUTTONS); } |
150 | void OnTogTwist(wxCommandEvent& event) { TogStyle(wxTR_TWIST_BUTTONS); } | |
151 | void OnTogLines(wxCommandEvent& event) { TogStyle(wxTR_NO_LINES); } | |
152 | void OnTogEdit(wxCommandEvent& event) { TogStyle(wxTR_EDIT_LABELS); } | |
153 | void OnTogHideRoot(wxCommandEvent& event) { TogStyle(wxTR_HIDE_ROOT); } | |
154 | void OnTogRootLines(wxCommandEvent& event) { TogStyle(wxTR_LINES_AT_ROOT); } | |
155 | void OnTogBorder(wxCommandEvent& event) { TogStyle(wxTR_ROW_LINES); } | |
156 | ||
5f939e78 | 157 | void OnDump(wxCommandEvent& event); |
9dfbf520 | 158 | #ifndef NO_MULTIPLE_SELECTION |
91b8de8d | 159 | void OnDumpSelected(wxCommandEvent& event); |
9dfbf520 VZ |
160 | void OnSelect(wxCommandEvent& event); |
161 | void OnUnselect(wxCommandEvent& event); | |
7cc8c988 | 162 | void OnToggleSel(wxCommandEvent& event); |
9dfbf520 | 163 | #endif // NO_MULTIPLE_SELECTION |
5f939e78 VZ |
164 | void OnDelete(wxCommandEvent& event); |
165 | void OnDeleteChildren(wxCommandEvent& event); | |
166 | void OnDeleteAll(wxCommandEvent& event); | |
2c8e4738 | 167 | |
5f939e78 | 168 | void OnRecreate(wxCommandEvent& event); |
618a5e38 | 169 | void OnToggleButtons(wxCommandEvent& event); |
2c8e4738 VZ |
170 | void OnToggleImages(wxCommandEvent& event); |
171 | void OnSetImageSize(wxCommandEvent& event); | |
5f939e78 | 172 | void OnCollapseAndReset(wxCommandEvent& event); |
add28c55 | 173 | |
5f939e78 VZ |
174 | void OnSetBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(TRUE); } |
175 | void OnClearBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(FALSE); } | |
f65635b5 | 176 | |
5f939e78 VZ |
177 | void OnEnsureVisible(wxCommandEvent& event); |
178 | ||
5fd11f09 RR |
179 | void OnCount(wxCommandEvent& event); |
180 | void OnCountRec(wxCommandEvent& event); | |
75c74ca0 | 181 | |
5f939e78 VZ |
182 | void OnRename(wxCommandEvent& event); |
183 | void OnSort(wxCommandEvent& event) { DoSort(); } | |
184 | void OnSortRev(wxCommandEvent& event) { DoSort(TRUE); } | |
185 | ||
186 | void OnAddItem(wxCommandEvent& event); | |
75c74ca0 | 187 | void OnInsertItem(wxCommandEvent& event); |
9dfbf520 | 188 | |
cf724bce RR |
189 | void OnIncIndent(wxCommandEvent& event); |
190 | void OnDecIndent(wxCommandEvent& event); | |
191 | ||
192 | void OnIncSpacing(wxCommandEvent& event); | |
193 | void OnDecSpacing(wxCommandEvent& event); | |
e1ee62bd | 194 | |
9dfbf520 VZ |
195 | void OnToggleIcon(wxCommandEvent& event); |
196 | ||
7cc8c988 VZ |
197 | void OnSize(wxSizeEvent& event); |
198 | ||
f135ff73 | 199 | private: |
618a5e38 RR |
200 | void TogStyle(long flag); |
201 | ||
5f939e78 | 202 | void DoSort(bool reverse = FALSE); |
e1ee62bd | 203 | |
7cc8c988 VZ |
204 | void Resize(const wxSize& size); |
205 | ||
5f939e78 | 206 | MyTreeCtrl *m_treeCtrl; |
7cc8c988 | 207 | wxTextCtrl *m_textCtrl; |
457814b5 | 208 | |
5f939e78 | 209 | void DoSetBold(bool bold = TRUE); |
add28c55 | 210 | |
5f939e78 | 211 | DECLARE_EVENT_TABLE() |
5a43154b | 212 | }; |
457814b5 | 213 | |
5a43154b VZ |
214 | // menu and control ids |
215 | enum | |
216 | { | |
5f939e78 VZ |
217 | TreeTest_Quit, |
218 | TreeTest_About, | |
618a5e38 RR |
219 | TreeTest_TogButtons, |
220 | TreeTest_TogTwist, | |
221 | TreeTest_TogLines, | |
222 | TreeTest_TogEdit, | |
223 | TreeTest_TogHideRoot, | |
224 | TreeTest_TogRootLines, | |
225 | TreeTest_TogBorder, | |
5f939e78 | 226 | TreeTest_Dump, |
9dfbf520 | 227 | TreeTest_DumpSelected, |
5fd11f09 RR |
228 | TreeTest_Count, |
229 | TreeTest_CountRec, | |
5f939e78 VZ |
230 | TreeTest_Sort, |
231 | TreeTest_SortRev, | |
618a5e38 RR |
232 | TreeTest_SetBold, |
233 | TreeTest_ClearBold, | |
5f939e78 VZ |
234 | TreeTest_Rename, |
235 | TreeTest_Delete, | |
236 | TreeTest_DeleteChildren, | |
237 | TreeTest_DeleteAll, | |
238 | TreeTest_Recreate, | |
2c8e4738 | 239 | TreeTest_ToggleImages, |
618a5e38 | 240 | TreeTest_ToggleButtons, |
2c8e4738 | 241 | TreeTest_SetImageSize, |
7cc8c988 | 242 | TreeTest_ToggleSel, |
5f939e78 VZ |
243 | TreeTest_CollapseAndReset, |
244 | TreeTest_EnsureVisible, | |
245 | TreeTest_AddItem, | |
75c74ca0 | 246 | TreeTest_InsertItem, |
cf724bce RR |
247 | TreeTest_IncIndent, |
248 | TreeTest_DecIndent, | |
249 | TreeTest_IncSpacing, | |
250 | TreeTest_DecSpacing, | |
9dfbf520 VZ |
251 | TreeTest_ToggleIcon, |
252 | TreeTest_Select, | |
253 | TreeTest_Unselect, | |
254 | TreeTest_Ctrl = 1000 | |
f65635b5 | 255 | }; |