]>
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: | |
2c8e4738 VZ |
26 | MyApp() { m_showImages = TRUE; } |
27 | ||
5f939e78 | 28 | bool OnInit(); |
2c8e4738 VZ |
29 | |
30 | void SetShowImages(bool show) { m_showImages = show; } | |
31 | bool ShowImages() const { return m_showImages; } | |
32 | ||
33 | private: | |
34 | bool m_showImages; | |
5a43154b | 35 | }; |
457814b5 | 36 | |
5a43154b VZ |
37 | class MyTreeItemData : public wxTreeItemData |
38 | { | |
39 | public: | |
5f939e78 | 40 | MyTreeItemData(const wxString& desc) : m_desc(desc) { } |
5a43154b | 41 | |
5f939e78 VZ |
42 | void ShowInfo(wxTreeCtrl *tree); |
43 | const char *GetDesc() const { return m_desc.c_str(); } | |
5a43154b VZ |
44 | |
45 | private: | |
5f939e78 | 46 | wxString m_desc; |
457814b5 JS |
47 | }; |
48 | ||
5a43154b | 49 | class MyTreeCtrl : public wxTreeCtrl |
457814b5 JS |
50 | { |
51 | public: | |
5f939e78 VZ |
52 | enum |
53 | { | |
54 | TreeCtrlIcon_File, | |
74b31181 VZ |
55 | TreeCtrlIcon_FileSelected, |
56 | TreeCtrlIcon_Folder, | |
57 | TreeCtrlIcon_FolderSelected, | |
58 | TreeCtrlIcon_FolderOpened | |
5f939e78 VZ |
59 | }; |
60 | ||
61 | MyTreeCtrl() { } | |
62 | MyTreeCtrl(wxWindow *parent, const wxWindowID id, | |
63 | const wxPoint& pos, const wxSize& size, | |
64 | long style); | |
65 | virtual ~MyTreeCtrl(); | |
66 | ||
67 | void OnBeginDrag(wxTreeEvent& event); | |
68 | void OnBeginRDrag(wxTreeEvent& event); | |
5888ef1e | 69 | void OnEndDrag(wxTreeEvent& event); |
5f939e78 VZ |
70 | void OnBeginLabelEdit(wxTreeEvent& event); |
71 | void OnEndLabelEdit(wxTreeEvent& event); | |
72 | void OnDeleteItem(wxTreeEvent& event); | |
73 | void OnGetInfo(wxTreeEvent& event); | |
74 | void OnSetInfo(wxTreeEvent& event); | |
75 | void OnItemExpanded(wxTreeEvent& event); | |
76 | void OnItemExpanding(wxTreeEvent& event); | |
77 | void OnItemCollapsed(wxTreeEvent& event); | |
78 | void OnItemCollapsing(wxTreeEvent& event); | |
79 | void OnSelChanged(wxTreeEvent& event); | |
80 | void OnSelChanging(wxTreeEvent& event); | |
81 | void OnTreeKeyDown(wxTreeEvent& event); | |
82 | void OnItemActivated(wxTreeEvent& event); | |
83 | void OnRMouseDClick(wxMouseEvent& event); | |
84 | ||
85 | void GetItemsRecursively(const wxTreeItemId& idParent, long cookie); | |
86 | ||
dee1b66f | 87 | void CreateImageList(int size = 16); |
2c8e4738 | 88 | |
5f939e78 VZ |
89 | void AddTestItemsToTree(size_t numChildren, size_t depth); |
90 | ||
91 | void DoSortChildren(const wxTreeItemId& item, bool reverse = FALSE) | |
9dfbf520 | 92 | { m_reverseSort = reverse; wxTreeCtrl::SortChildren(item); } |
5f939e78 | 93 | void DoEnsureVisible() { EnsureVisible(m_lastItem); } |
e1ee62bd | 94 | |
9dfbf520 VZ |
95 | void DoToggleIcon(const wxTreeItemId& item); |
96 | ||
e1ee62bd | 97 | protected: |
5f939e78 | 98 | virtual int OnCompareItems(const wxTreeItemId& i1, const wxTreeItemId& i2); |
e1ee62bd | 99 | |
5ea47806 VZ |
100 | // is this the test item which we use in several event handlers? |
101 | bool IsTestItem(const wxTreeItemId& item) | |
102 | { | |
103 | // the test item is the first child folder | |
104 | return GetParent(item) == GetRootItem() && !GetPrevSibling(item); | |
105 | } | |
106 | ||
5a43154b | 107 | private: |
5f939e78 VZ |
108 | void AddItemsRecursively(const wxTreeItemId& idParent, |
109 | size_t nChildren, | |
110 | size_t depth, | |
111 | size_t folder); | |
112 | ||
113 | wxImageList *m_imageListNormal; | |
5888ef1e VZ |
114 | bool m_reverseSort; // flag for OnCompareItems |
115 | wxTreeItemId m_lastItem, // for OnEnsureVisible() | |
116 | m_draggedItem; // item being dragged right now | |
5f939e78 VZ |
117 | |
118 | // NB: due to an ugly wxMSW hack you _must_ use DECLARE_DYNAMIC_CLASS() | |
119 | // if you want your overloaded OnCompareItems() to be called. | |
120 | // OTOH, if you don't want it you may omit the next line - this will | |
121 | // make default (alphabetical) sorting much faster under wxMSW. | |
122 | DECLARE_DYNAMIC_CLASS(MyTreeCtrl) | |
123 | DECLARE_EVENT_TABLE() | |
457814b5 JS |
124 | }; |
125 | ||
126 | // Define a new frame type | |
127 | class MyFrame: public wxFrame | |
019bf128 UU |
128 | { |
129 | public: | |
5f939e78 VZ |
130 | // ctor and dtor |
131 | MyFrame(const wxString& title, int x, int y, int w, int h); | |
132 | virtual ~MyFrame(); | |
f135ff73 | 133 | |
5f939e78 VZ |
134 | // menu callbacks |
135 | void OnQuit(wxCommandEvent& event); | |
136 | void OnAbout(wxCommandEvent& event); | |
f135ff73 | 137 | |
5f939e78 | 138 | void OnDump(wxCommandEvent& event); |
9dfbf520 | 139 | #ifndef NO_MULTIPLE_SELECTION |
91b8de8d | 140 | void OnDumpSelected(wxCommandEvent& event); |
9dfbf520 VZ |
141 | void OnSelect(wxCommandEvent& event); |
142 | void OnUnselect(wxCommandEvent& event); | |
7cc8c988 | 143 | void OnToggleSel(wxCommandEvent& event); |
9dfbf520 | 144 | #endif // NO_MULTIPLE_SELECTION |
5f939e78 VZ |
145 | void OnDelete(wxCommandEvent& event); |
146 | void OnDeleteChildren(wxCommandEvent& event); | |
147 | void OnDeleteAll(wxCommandEvent& event); | |
2c8e4738 | 148 | |
5f939e78 | 149 | void OnRecreate(wxCommandEvent& event); |
2c8e4738 VZ |
150 | void OnToggleImages(wxCommandEvent& event); |
151 | void OnSetImageSize(wxCommandEvent& event); | |
5f939e78 | 152 | void OnCollapseAndReset(wxCommandEvent& event); |
add28c55 | 153 | |
5f939e78 VZ |
154 | void OnSetBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(TRUE); } |
155 | void OnClearBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(FALSE); } | |
f65635b5 | 156 | |
5f939e78 VZ |
157 | void OnEnsureVisible(wxCommandEvent& event); |
158 | ||
5fd11f09 RR |
159 | void OnCount(wxCommandEvent& event); |
160 | void OnCountRec(wxCommandEvent& event); | |
75c74ca0 | 161 | |
5f939e78 VZ |
162 | void OnRename(wxCommandEvent& event); |
163 | void OnSort(wxCommandEvent& event) { DoSort(); } | |
164 | void OnSortRev(wxCommandEvent& event) { DoSort(TRUE); } | |
165 | ||
166 | void OnAddItem(wxCommandEvent& event); | |
75c74ca0 | 167 | void OnInsertItem(wxCommandEvent& event); |
9dfbf520 | 168 | |
cf724bce RR |
169 | void OnIncIndent(wxCommandEvent& event); |
170 | void OnDecIndent(wxCommandEvent& event); | |
171 | ||
172 | void OnIncSpacing(wxCommandEvent& event); | |
173 | void OnDecSpacing(wxCommandEvent& event); | |
e1ee62bd | 174 | |
9dfbf520 VZ |
175 | void OnToggleIcon(wxCommandEvent& event); |
176 | ||
7cc8c988 VZ |
177 | void OnSize(wxSizeEvent& event); |
178 | ||
f135ff73 | 179 | private: |
5f939e78 | 180 | void DoSort(bool reverse = FALSE); |
e1ee62bd | 181 | |
7cc8c988 VZ |
182 | void Resize(const wxSize& size); |
183 | ||
5f939e78 | 184 | MyTreeCtrl *m_treeCtrl; |
7cc8c988 | 185 | wxTextCtrl *m_textCtrl; |
457814b5 | 186 | |
5f939e78 | 187 | void DoSetBold(bool bold = TRUE); |
add28c55 | 188 | |
5f939e78 | 189 | DECLARE_EVENT_TABLE() |
5a43154b | 190 | }; |
457814b5 | 191 | |
5a43154b VZ |
192 | // menu and control ids |
193 | enum | |
194 | { | |
5f939e78 VZ |
195 | TreeTest_Quit, |
196 | TreeTest_About, | |
197 | TreeTest_Dump, | |
9dfbf520 | 198 | TreeTest_DumpSelected, |
5fd11f09 RR |
199 | TreeTest_Count, |
200 | TreeTest_CountRec, | |
5f939e78 VZ |
201 | TreeTest_Sort, |
202 | TreeTest_SortRev, | |
203 | TreeTest_Bold, | |
204 | TreeTest_UnBold, | |
205 | TreeTest_Rename, | |
206 | TreeTest_Delete, | |
207 | TreeTest_DeleteChildren, | |
208 | TreeTest_DeleteAll, | |
209 | TreeTest_Recreate, | |
2c8e4738 VZ |
210 | TreeTest_ToggleImages, |
211 | TreeTest_SetImageSize, | |
7cc8c988 | 212 | TreeTest_ToggleSel, |
5f939e78 VZ |
213 | TreeTest_CollapseAndReset, |
214 | TreeTest_EnsureVisible, | |
215 | TreeTest_AddItem, | |
75c74ca0 | 216 | TreeTest_InsertItem, |
cf724bce RR |
217 | TreeTest_IncIndent, |
218 | TreeTest_DecIndent, | |
219 | TreeTest_IncSpacing, | |
220 | TreeTest_DecSpacing, | |
9dfbf520 VZ |
221 | TreeTest_ToggleIcon, |
222 | TreeTest_Select, | |
223 | TreeTest_Unselect, | |
224 | TreeTest_Ctrl = 1000 | |
f65635b5 | 225 | }; |