]> git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treetest.h
1. Derive wxMessageDialogBase from wxDialog (why did we use multiple
[wxWidgets.git] / samples / treectrl / treetest.h
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_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 SetShowButtons(bool show) { m_showButtons = show; }
34 bool ShowButtons() const { return m_showButtons; }
35
36 private:
37 bool m_showImages, m_showButtons;
38 };
39
40 class MyTreeItemData : public wxTreeItemData
41 {
42 public:
43 MyTreeItemData(const wxString& desc) : m_desc(desc) { }
44
45 void ShowInfo(wxTreeCtrl *tree);
46 const wxChar *GetDesc() const { return m_desc.c_str(); }
47
48 private:
49 wxString m_desc;
50 };
51
52 class MyTreeCtrl : public wxTreeCtrl
53 {
54 public:
55 enum
56 {
57 TreeCtrlIcon_File,
58 TreeCtrlIcon_FileSelected,
59 TreeCtrlIcon_Folder,
60 TreeCtrlIcon_FolderSelected,
61 TreeCtrlIcon_FolderOpened
62 };
63
64 MyTreeCtrl() { m_alternateImages = false; }
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);
72 void OnEndDrag(wxTreeEvent& event);
73 void OnBeginLabelEdit(wxTreeEvent& event);
74 void OnEndLabelEdit(wxTreeEvent& event);
75 void OnDeleteItem(wxTreeEvent& event);
76 void OnContextMenu(wxContextMenuEvent& event);
77 void OnItemMenu(wxTreeEvent& event);
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);
88 void OnItemRClick(wxTreeEvent& event);
89
90 void OnRMouseDown(wxMouseEvent& event);
91 void OnRMouseUp(wxMouseEvent& event);
92 void OnRMouseDClick(wxMouseEvent& event);
93
94 void GetItemsRecursively(const wxTreeItemId& idParent,
95 wxTreeItemIdValue cookie = 0);
96
97 void CreateImageList(int size = 16);
98 void CreateButtonsImageList(int size = 11);
99
100 void AddTestItemsToTree(size_t numChildren, size_t depth);
101
102 void DoSortChildren(const wxTreeItemId& item, bool reverse = false)
103 { m_reverseSort = reverse; wxTreeCtrl::SortChildren(item); }
104 void DoEnsureVisible() { if (m_lastItem.IsOk()) EnsureVisible(m_lastItem); }
105
106 void DoToggleIcon(const wxTreeItemId& item);
107
108 void ShowMenu(wxTreeItemId id, const wxPoint& pt);
109
110 int ImageSize(void) const { return m_imageSize; }
111
112 void SetLastItem(wxTreeItemId id) { m_lastItem = id; }
113
114 void SetAlternateImages(bool show) { m_alternateImages = show; }
115 bool AlternateImages() const { return m_alternateImages; }
116
117 protected:
118 virtual int OnCompareItems(const wxTreeItemId& i1, const wxTreeItemId& i2);
119
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
124 return GetItemParent(item) == GetRootItem() && !GetPrevSibling(item);
125 }
126
127 private:
128 void AddItemsRecursively(const wxTreeItemId& idParent,
129 size_t nChildren,
130 size_t depth,
131 size_t folder);
132
133 void LogEvent(const wxChar *name, const wxTreeEvent& event);
134
135 int m_imageSize; // current size of images
136 bool m_reverseSort; // flag for OnCompareItems
137 wxTreeItemId m_lastItem, // for OnEnsureVisible()
138 m_draggedItem; // item being dragged right now
139 bool m_alternateImages;
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()
147 };
148
149 // Define a new frame type
150 class MyFrame: public wxFrame
151 {
152 public:
153 // ctor and dtor
154 MyFrame(const wxString& title, int x, int y, int w, int h);
155 virtual ~MyFrame();
156
157 // menu callbacks
158 void OnQuit(wxCommandEvent& event);
159 void OnAbout(wxCommandEvent& event);
160 void OnClearLog(wxCommandEvent& event);
161
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(); }
181
182 void OnSetFgColour(wxCommandEvent& event);
183 void OnSetBgColour(wxCommandEvent& event);
184
185 void OnHighlight(wxCommandEvent& event);
186 void OnDump(wxCommandEvent& event);
187 #ifndef NO_MULTIPLE_SELECTION
188 void OnDumpSelected(wxCommandEvent& event);
189 void OnSelect(wxCommandEvent& event);
190 void OnUnselect(wxCommandEvent& event);
191 void OnToggleSel(wxCommandEvent& event);
192 #endif // NO_MULTIPLE_SELECTION
193 void OnSelectRoot(wxCommandEvent& event);
194 void OnDelete(wxCommandEvent& event);
195 void OnDeleteChildren(wxCommandEvent& event);
196 void OnDeleteAll(wxCommandEvent& event);
197
198 void OnRecreate(wxCommandEvent& event);
199 void OnToggleButtons(wxCommandEvent& event);
200 void OnToggleImages(wxCommandEvent& event);
201 void OnToggleAlternateImages(wxCommandEvent& event);
202 void OnSetImageSize(wxCommandEvent& event);
203 void OnCollapseAndReset(wxCommandEvent& event);
204
205 void OnSetBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(true); }
206 void OnClearBold(wxCommandEvent& WXUNUSED(event)) { DoSetBold(false); }
207
208 void OnEnsureVisible(wxCommandEvent& event);
209
210 void OnCount(wxCommandEvent& event);
211 void OnCountRec(wxCommandEvent& event);
212
213 void OnRename(wxCommandEvent& event);
214 void OnSort(wxCommandEvent& WXUNUSED(event)) { DoSort(); }
215 void OnSortRev(wxCommandEvent& WXUNUSED(event)) { DoSort(true); }
216
217 void OnAddItem(wxCommandEvent& event);
218 void OnInsertItem(wxCommandEvent& event);
219
220 void OnIncIndent(wxCommandEvent& event);
221 void OnDecIndent(wxCommandEvent& event);
222
223 void OnIncSpacing(wxCommandEvent& event);
224 void OnDecSpacing(wxCommandEvent& event);
225
226 void OnToggleIcon(wxCommandEvent& event);
227
228 void OnIdle(wxIdleEvent& event);
229 void OnSize(wxSizeEvent& event);
230
231 private:
232 void TogStyle(int id, long flag);
233
234 void DoSort(bool reverse = false);
235
236 void Resize();
237
238 void CreateTreeWithDefStyle();
239 void CreateTree(long style);
240
241 wxPanel *m_panel;
242 MyTreeCtrl *m_treeCtrl;
243 #if wxUSE_LOG
244 wxTextCtrl *m_textCtrl;
245 #endif // wxUSE_LOG
246
247 void DoSetBold(bool bold = true);
248
249 DECLARE_EVENT_TABLE()
250 };
251
252 // menu and control ids
253 enum
254 {
255 TreeTest_Quit = wxID_EXIT,
256 TreeTest_About = wxID_ABOUT,
257 TreeTest_ClearLog = wxID_CLEAR,
258 TreeTest_TogButtons = wxID_HIGHEST,
259 TreeTest_TogTwist,
260 TreeTest_TogLines,
261 TreeTest_TogEdit,
262 TreeTest_TogHideRoot,
263 TreeTest_TogRootLines,
264 TreeTest_TogBorder,
265 TreeTest_TogFullHighlight,
266 TreeTest_SetFgColour,
267 TreeTest_SetBgColour,
268 TreeTest_ResetStyle,
269 TreeTest_Highlight,
270 TreeTest_Dump,
271 TreeTest_DumpSelected,
272 TreeTest_Count,
273 TreeTest_CountRec,
274 TreeTest_Sort,
275 TreeTest_SortRev,
276 TreeTest_SetBold,
277 TreeTest_ClearBold,
278 TreeTest_Rename,
279 TreeTest_Delete,
280 TreeTest_DeleteChildren,
281 TreeTest_DeleteAll,
282 TreeTest_Recreate,
283 TreeTest_ToggleImages,
284 TreeTest_ToggleAlternateImages,
285 TreeTest_ToggleButtons,
286 TreeTest_SetImageSize,
287 TreeTest_ToggleSel,
288 TreeTest_CollapseAndReset,
289 TreeTest_EnsureVisible,
290 TreeTest_AddItem,
291 TreeTest_InsertItem,
292 TreeTest_IncIndent,
293 TreeTest_DecIndent,
294 TreeTest_IncSpacing,
295 TreeTest_DecSpacing,
296 TreeTest_ToggleIcon,
297 TreeTest_Select,
298 TreeTest_Unselect,
299 TreeTest_SelectRoot,
300 TreeTest_Ctrl = 1000
301 };