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