]>
Commit | Line | Data |
---|---|---|
457814b5 | 1 | ///////////////////////////////////////////////////////////////////////////// |
5301d933 | 2 | // Name: treectrl.cpp |
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 | |
3a5a2f56 | 9 | // Licence: wxWindows license |
457814b5 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
f6bcfd97 BP |
12 | // important: the #pragma argument must be different from treectrl.cpp, |
13 | // otherwise gcc gets confused (as there is also treectrl.cpp in the library | |
14 | // which has identical #pragma) and the sample crashes on startup! | |
457814b5 | 15 | #ifdef __GNUG__ |
f6bcfd97 BP |
16 | #pragma interface "treetest.cpp" |
17 | #pragma implementation "treetest.cpp" | |
457814b5 JS |
18 | #endif |
19 | ||
20 | // For compilers that support precompilation, includes "wx/wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
3a5a2f56 | 24 | #pragma hdrstop |
457814b5 JS |
25 | #endif |
26 | ||
27 | #ifndef WX_PRECOMP | |
3a5a2f56 | 28 | #include "wx/wx.h" |
457814b5 JS |
29 | #endif |
30 | ||
3a5a2f56 VZ |
31 | #include "wx/log.h" |
32 | ||
91b8de8d | 33 | #include "wx/image.h" |
3a5a2f56 | 34 | #include "wx/imaglist.h" |
457814b5 JS |
35 | #include "wx/treectrl.h" |
36 | ||
cf724bce RR |
37 | #include "math.h" |
38 | ||
2c8e4738 VZ |
39 | #ifdef __WIN32__ |
40 | // this is not supported by native control | |
7cc8c988 | 41 | #define NO_VARIABLE_HEIGHT |
74b31181 | 42 | #endif |
91b8de8d | 43 | |
5301d933 | 44 | #include "treectrl.h" |
9dfbf520 | 45 | |
91b8de8d RR |
46 | // under Windows the icons are in the .rc file |
47 | #ifndef __WXMSW__ | |
2c8e4738 VZ |
48 | #include "icon1.xpm" |
49 | #include "icon2.xpm" | |
50 | #include "icon3.xpm" | |
51 | #include "icon4.xpm" | |
52 | #include "icon5.xpm" | |
53 | #include "mondrian.xpm" | |
91b8de8d RR |
54 | #endif |
55 | ||
e1ee62bd VZ |
56 | // verify that the item is ok and insult the user if it is not |
57 | #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \ | |
58 | wxMessageBox("Please select some item first!", \ | |
59 | "Tree sample error", \ | |
60 | wxOK | wxICON_EXCLAMATION, \ | |
61 | this); \ | |
62 | return; \ | |
5f939e78 VZ |
63 | } |
64 | ||
457814b5 | 65 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
7cc8c988 VZ |
66 | EVT_SIZE(MyFrame::OnSize) |
67 | ||
5f939e78 VZ |
68 | EVT_MENU(TreeTest_Quit, MyFrame::OnQuit) |
69 | EVT_MENU(TreeTest_About, MyFrame::OnAbout) | |
70 | EVT_MENU(TreeTest_Dump, MyFrame::OnDump) | |
9dfbf520 VZ |
71 | #ifndef NO_MULTIPLE_SELECTION |
72 | EVT_MENU(TreeTest_DumpSelected, MyFrame::OnDumpSelected) | |
73 | EVT_MENU(TreeTest_Select, MyFrame::OnSelect) | |
74 | EVT_MENU(TreeTest_Unselect, MyFrame::OnUnselect) | |
7cc8c988 | 75 | EVT_MENU(TreeTest_ToggleSel, MyFrame::OnToggleSel) |
9dfbf520 | 76 | #endif // NO_MULTIPLE_SELECTION |
5f939e78 | 77 | EVT_MENU(TreeTest_Rename, MyFrame::OnRename) |
5fd11f09 RR |
78 | EVT_MENU(TreeTest_Count, MyFrame::OnCount) |
79 | EVT_MENU(TreeTest_CountRec, MyFrame::OnCountRec) | |
5f939e78 VZ |
80 | EVT_MENU(TreeTest_Sort, MyFrame::OnSort) |
81 | EVT_MENU(TreeTest_SortRev, MyFrame::OnSortRev) | |
82 | EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold) | |
83 | EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold) | |
84 | EVT_MENU(TreeTest_Delete, MyFrame::OnDelete) | |
85 | EVT_MENU(TreeTest_DeleteChildren, MyFrame::OnDeleteChildren) | |
86 | EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll) | |
87 | EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate) | |
2c8e4738 VZ |
88 | EVT_MENU(TreeTest_ToggleImages, MyFrame::OnToggleImages) |
89 | EVT_MENU(TreeTest_SetImageSize, MyFrame::OnSetImageSize) | |
5f939e78 VZ |
90 | EVT_MENU(TreeTest_CollapseAndReset, MyFrame::OnCollapseAndReset) |
91 | EVT_MENU(TreeTest_EnsureVisible, MyFrame::OnEnsureVisible) | |
92 | EVT_MENU(TreeTest_AddItem, MyFrame::OnAddItem) | |
75c74ca0 | 93 | EVT_MENU(TreeTest_InsertItem, MyFrame::OnInsertItem) |
cf724bce RR |
94 | EVT_MENU(TreeTest_IncIndent, MyFrame::OnIncIndent) |
95 | EVT_MENU(TreeTest_DecIndent, MyFrame::OnDecIndent) | |
96 | EVT_MENU(TreeTest_IncSpacing, MyFrame::OnIncSpacing) | |
97 | EVT_MENU(TreeTest_DecSpacing, MyFrame::OnDecSpacing) | |
9dfbf520 | 98 | EVT_MENU(TreeTest_ToggleIcon, MyFrame::OnToggleIcon) |
457814b5 JS |
99 | END_EVENT_TABLE() |
100 | ||
101 | BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) | |
5f939e78 VZ |
102 | EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag) |
103 | EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag) | |
5888ef1e | 104 | EVT_TREE_END_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnEndDrag) |
5f939e78 VZ |
105 | EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit) |
106 | EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit) | |
107 | EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem) | |
74b31181 | 108 | #if 0 // there are so many of those that logging them causes flicker |
5f939e78 | 109 | EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo) |
74b31181 | 110 | #endif |
5f939e78 VZ |
111 | EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo) |
112 | EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded) | |
113 | EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding) | |
114 | EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed) | |
115 | EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing) | |
116 | EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged) | |
117 | EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging) | |
118 | EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown) | |
119 | EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated) | |
120 | EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick) | |
457814b5 JS |
121 | END_EVENT_TABLE() |
122 | ||
123 | IMPLEMENT_APP(MyApp) | |
124 | ||
c89a6106 | 125 | bool MyApp::OnInit() |
457814b5 | 126 | { |
5f939e78 | 127 | // Create the main frame window |
477350ce | 128 | MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 600); |
457814b5 | 129 | |
5f939e78 VZ |
130 | // Show the frame |
131 | frame->Show(TRUE); | |
132 | SetTopWindow(frame); | |
457814b5 | 133 | |
5f939e78 | 134 | return TRUE; |
3a5a2f56 | 135 | } |
457814b5 | 136 | |
457814b5 | 137 | |
3a5a2f56 VZ |
138 | // My frame constructor |
139 | MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) | |
7cc8c988 VZ |
140 | : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)), |
141 | m_treeCtrl(NULL), m_textCtrl(NULL) | |
3a5a2f56 | 142 | { |
5f939e78 VZ |
143 | // This reduces flicker effects - even better would be to define |
144 | // OnEraseBackground to do nothing. When the tree control's scrollbars are | |
145 | // show or hidden, the frame is sent a background erase event. | |
146 | SetBackgroundColour(wxColour(255, 255, 255)); | |
147 | ||
148 | // Give it an icon | |
149 | SetIcon(wxICON(mondrian)); | |
150 | ||
151 | // Make a menubar | |
152 | wxMenu *file_menu = new wxMenu, | |
153 | *tree_menu = new wxMenu, | |
154 | *item_menu = new wxMenu; | |
155 | ||
156 | file_menu->Append(TreeTest_About, "&About..."); | |
157 | file_menu->AppendSeparator(); | |
3dbeaa52 | 158 | file_menu->Append(TreeTest_Quit, "E&xit\tAlt-X"); |
5f939e78 | 159 | |
7cc8c988 VZ |
160 | #ifndef NO_MULTIPLE_SELECTION |
161 | tree_menu->Append(TreeTest_ToggleSel, "&Toggle selection mode"); | |
162 | #endif // NO_MULTIPLE_SELECTION | |
2c8e4738 VZ |
163 | tree_menu->Append(TreeTest_ToggleImages, "&Show images", "", TRUE); |
164 | tree_menu->Append(TreeTest_SetImageSize, "Set image si&ze..."); | |
5f939e78 VZ |
165 | tree_menu->Append(TreeTest_Recreate, "&Recreate the tree"); |
166 | tree_menu->Append(TreeTest_CollapseAndReset, "C&ollapse and reset"); | |
167 | tree_menu->AppendSeparator(); | |
168 | tree_menu->Append(TreeTest_AddItem, "Append a &new item"); | |
75c74ca0 | 169 | tree_menu->Append(TreeTest_InsertItem, "&Insert a new item"); |
5f939e78 VZ |
170 | tree_menu->Append(TreeTest_Delete, "&Delete this item"); |
171 | tree_menu->Append(TreeTest_DeleteChildren, "Delete &children"); | |
172 | tree_menu->Append(TreeTest_DeleteAll, "Delete &all items"); | |
173 | tree_menu->AppendSeparator(); | |
5fd11f09 RR |
174 | tree_menu->Append(TreeTest_Count, "Count children of current item"); |
175 | tree_menu->Append(TreeTest_CountRec, "Recursively count children of current item"); | |
176 | tree_menu->AppendSeparator(); | |
5f939e78 VZ |
177 | tree_menu->Append(TreeTest_Sort, "Sort children of current item"); |
178 | tree_menu->Append(TreeTest_SortRev, "Sort in reversed order"); | |
179 | tree_menu->AppendSeparator(); | |
180 | tree_menu->Append(TreeTest_EnsureVisible, "Make the last item &visible"); | |
cf724bce RR |
181 | tree_menu->AppendSeparator(); |
182 | tree_menu->Append(TreeTest_IncIndent, "Add 5 points to indentation\tAlt-I"); | |
183 | tree_menu->Append(TreeTest_DecIndent, "Reduce indentation by 5 points\tAlt-R"); | |
184 | tree_menu->AppendSeparator(); | |
185 | tree_menu->Append(TreeTest_IncSpacing, "Add 5 points to spacing\tCtrl-I"); | |
186 | tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R"); | |
5f939e78 | 187 | |
91b8de8d | 188 | item_menu->Append(TreeTest_Dump, "&Dump item children"); |
5f939e78 | 189 | item_menu->Append(TreeTest_Rename, "&Rename item..."); |
91b8de8d | 190 | |
5f939e78 VZ |
191 | item_menu->AppendSeparator(); |
192 | item_menu->Append(TreeTest_Bold, "Make item &bold"); | |
193 | item_menu->Append(TreeTest_UnBold, "Make item ¬ bold"); | |
9dfbf520 VZ |
194 | item_menu->AppendSeparator(); |
195 | item_menu->Append(TreeTest_ToggleIcon, "Toggle the items &icon"); | |
196 | ||
197 | #ifndef NO_MULTIPLE_SELECTION | |
198 | item_menu->AppendSeparator(); | |
199 | item_menu->Append(TreeTest_DumpSelected, "Dump selected items\tAlt-D"); | |
200 | item_menu->Append(TreeTest_Select, "Select current item\tAlt-S"); | |
201 | item_menu->Append(TreeTest_Unselect, "Unselect everything\tAlt-U"); | |
7cc8c988 | 202 | #endif // NO_MULTIPLE_SELECTION |
5f939e78 VZ |
203 | |
204 | wxMenuBar *menu_bar = new wxMenuBar; | |
205 | menu_bar->Append(file_menu, "&File"); | |
206 | menu_bar->Append(tree_menu, "&Tree"); | |
207 | menu_bar->Append(item_menu, "&Item"); | |
208 | SetMenuBar(menu_bar); | |
209 | ||
2c8e4738 VZ |
210 | menu_bar->Check(TreeTest_ToggleImages, TRUE); |
211 | ||
5f939e78 VZ |
212 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, |
213 | wxDefaultPosition, wxDefaultSize, | |
5ea47806 VZ |
214 | wxTR_HAS_BUTTONS | |
215 | wxTR_EDIT_LABELS | | |
89124b1a | 216 | #ifndef NO_VARIABLE_HEIGHT |
74b31181 | 217 | wxTR_HAS_VARIABLE_ROW_HEIGHT | |
89124b1a | 218 | #endif |
74b31181 | 219 | wxSUNKEN_BORDER); |
31405c01 VZ |
220 | |
221 | m_treeCtrl->SetBackgroundColour(wxColour(204, 205, 79)); | |
222 | ||
7cc8c988 | 223 | m_textCtrl = new wxTextCtrl(this, -1, "", |
5f939e78 VZ |
224 | wxDefaultPosition, wxDefaultSize, |
225 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
226 | ||
5f939e78 VZ |
227 | // create a status bar with 3 panes |
228 | CreateStatusBar(3); | |
229 | SetStatusText("", 0); | |
3a5a2f56 | 230 | |
a367b9b3 | 231 | #ifdef __WXMOTIF__ |
5f939e78 VZ |
232 | // For some reason, we get a memcpy crash in wxLogStream::DoLogStream |
233 | // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc? | |
234 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
a367b9b3 | 235 | #else |
5f939e78 | 236 | // set our text control as the log target |
7cc8c988 | 237 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(m_textCtrl); |
5f939e78 | 238 | delete wxLog::SetActiveTarget(logWindow); |
a367b9b3 | 239 | #endif |
457814b5 JS |
240 | } |
241 | ||
c89a6106 | 242 | MyFrame::~MyFrame() |
457814b5 | 243 | { |
5f939e78 | 244 | delete wxLog::SetActiveTarget(NULL); |
457814b5 JS |
245 | } |
246 | ||
7cc8c988 VZ |
247 | void MyFrame::OnSize(wxSizeEvent& event) |
248 | { | |
249 | if ( m_treeCtrl && m_textCtrl ) | |
250 | { | |
251 | Resize(GetClientSize()); | |
252 | } | |
253 | ||
254 | event.Skip(); | |
255 | } | |
256 | ||
257 | void MyFrame::Resize(const wxSize& size) | |
258 | { | |
259 | m_treeCtrl->SetSize(0, 0, size.x, 2*size.y/3); | |
260 | m_textCtrl->SetSize(0, 2*size.y/3, size.x, size.y/3); | |
261 | } | |
262 | ||
3a5a2f56 | 263 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 264 | { |
5f939e78 | 265 | Close(TRUE); |
457814b5 JS |
266 | } |
267 | ||
3a5a2f56 | 268 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 269 | { |
5f939e78 | 270 | wxMessageBox("Tree test sample\n" |
5ea47806 | 271 | "(c) Julian Smart 1997, Vadim Zeitlin 1998", |
5f939e78 VZ |
272 | "About tree test", |
273 | wxOK | wxICON_INFORMATION, this); | |
e1ee62bd VZ |
274 | } |
275 | ||
276 | void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event)) | |
277 | { | |
5f939e78 | 278 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 279 | |
5f939e78 | 280 | CHECK_ITEM( item ); |
e1ee62bd | 281 | |
5ea47806 VZ |
282 | // old code - now we edit in place |
283 | #if 0 | |
5f939e78 VZ |
284 | static wxString s_text; |
285 | s_text = wxGetTextFromUser("New name: ", "Tree sample question", | |
286 | s_text, this); | |
287 | if ( !s_text.IsEmpty() ) | |
288 | { | |
289 | m_treeCtrl->SetItemText(item, s_text); | |
290 | } | |
5ea47806 VZ |
291 | #endif // 0 |
292 | ||
293 | // TODO demonstrate creating a custom edit control... | |
294 | (void)m_treeCtrl->EditLabel(item); | |
e1ee62bd VZ |
295 | } |
296 | ||
5fd11f09 RR |
297 | void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event)) |
298 | { | |
299 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
300 | ||
301 | CHECK_ITEM( item ); | |
302 | ||
303 | int i = m_treeCtrl->GetChildrenCount( item, FALSE ); | |
75c74ca0 | 304 | |
247e5b16 | 305 | wxLogMessage(wxT("%d children"), i); |
5fd11f09 RR |
306 | } |
307 | ||
308 | void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event)) | |
309 | { | |
310 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
311 | ||
312 | CHECK_ITEM( item ); | |
313 | ||
314 | int i = m_treeCtrl->GetChildrenCount( item ); | |
75c74ca0 | 315 | |
247e5b16 | 316 | wxLogMessage(wxT("%d children"), i); |
5fd11f09 RR |
317 | } |
318 | ||
e1ee62bd VZ |
319 | void MyFrame::DoSort(bool reverse) |
320 | { | |
5f939e78 | 321 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 322 | |
5f939e78 | 323 | CHECK_ITEM( item ); |
457814b5 | 324 | |
5f939e78 | 325 | m_treeCtrl->DoSortChildren(item, reverse); |
457814b5 JS |
326 | } |
327 | ||
4832f7c0 VZ |
328 | void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) |
329 | { | |
5f939e78 | 330 | wxTreeItemId root = m_treeCtrl->GetSelection(); |
e1ee62bd | 331 | |
5f939e78 | 332 | CHECK_ITEM( root ); |
e1ee62bd | 333 | |
5f939e78 | 334 | m_treeCtrl->GetItemsRecursively(root, -1); |
4832f7c0 VZ |
335 | } |
336 | ||
9dfbf520 VZ |
337 | #ifndef NO_MULTIPLE_SELECTION |
338 | ||
7cc8c988 VZ |
339 | void MyFrame::OnToggleSel(wxCommandEvent& WXUNUSED(event)) |
340 | { | |
341 | long style = m_treeCtrl->GetWindowStyle(); | |
342 | if ( style & wxTR_MULTIPLE ) | |
343 | style &= ~wxTR_MULTIPLE; | |
344 | else | |
345 | style |= wxTR_MULTIPLE; | |
346 | ||
347 | delete m_treeCtrl; | |
348 | ||
349 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, | |
350 | wxDefaultPosition, wxDefaultSize, | |
351 | style); | |
352 | Resize(GetClientSize()); | |
353 | } | |
354 | ||
91b8de8d RR |
355 | void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event)) |
356 | { | |
9dfbf520 | 357 | wxArrayTreeItemIds array; |
91b8de8d | 358 | |
9dfbf520 | 359 | size_t count = m_treeCtrl->GetSelections(array); |
247e5b16 | 360 | wxLogMessage(wxT("%u items selected"), count); |
91b8de8d | 361 | |
9dfbf520 VZ |
362 | for ( size_t n = 0; n < count; n++ ) |
363 | { | |
364 | wxLogMessage("\t%s", m_treeCtrl->GetItemText(array.Item(n)).c_str()); | |
365 | } | |
366 | } | |
367 | ||
368 | void MyFrame::OnSelect(wxCommandEvent& event) | |
369 | { | |
370 | m_treeCtrl->SelectItem(m_treeCtrl->GetSelection()); | |
371 | } | |
372 | ||
373 | void MyFrame::OnUnselect(wxCommandEvent& event) | |
374 | { | |
375 | m_treeCtrl->UnselectAll(); | |
91b8de8d RR |
376 | } |
377 | ||
9dfbf520 VZ |
378 | #endif // NO_MULTIPLE_SELECTION |
379 | ||
add28c55 VZ |
380 | void MyFrame::DoSetBold(bool bold) |
381 | { | |
5f939e78 | 382 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 383 | |
5f939e78 | 384 | CHECK_ITEM( item ); |
e1ee62bd | 385 | |
5f939e78 | 386 | m_treeCtrl->SetItemBold(item, bold); |
add28c55 VZ |
387 | } |
388 | ||
ff5bf259 VZ |
389 | void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) |
390 | { | |
5f939e78 | 391 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 392 | |
5f939e78 | 393 | CHECK_ITEM( item ); |
e1ee62bd | 394 | |
5f939e78 | 395 | m_treeCtrl->Delete(item); |
ff5bf259 VZ |
396 | } |
397 | ||
372edb9d VZ |
398 | void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event)) |
399 | { | |
5f939e78 | 400 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 401 | |
5f939e78 | 402 | CHECK_ITEM( item ); |
e1ee62bd | 403 | |
5f939e78 | 404 | m_treeCtrl->DeleteChildren(item); |
372edb9d VZ |
405 | } |
406 | ||
ff5bf259 VZ |
407 | void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) |
408 | { | |
5f939e78 | 409 | m_treeCtrl->DeleteAllItems(); |
ff5bf259 VZ |
410 | } |
411 | ||
412 | void MyFrame::OnRecreate(wxCommandEvent& event) | |
413 | { | |
5f939e78 VZ |
414 | OnDeleteAll(event); |
415 | m_treeCtrl->AddTestItemsToTree(3, 2); | |
ff5bf259 VZ |
416 | } |
417 | ||
2c8e4738 VZ |
418 | void MyFrame::OnSetImageSize(wxCommandEvent& event) |
419 | { | |
420 | long size = wxGetNumberFromUser("Enter the size for the images to use", | |
421 | "Size: ", | |
422 | "TreeCtrl sample", | |
dee1b66f | 423 | 16); |
2c8e4738 VZ |
424 | if ( size == -1 ) |
425 | return; | |
426 | ||
427 | m_treeCtrl->CreateImageList((int)size); | |
428 | ||
429 | OnRecreate(event); | |
430 | } | |
431 | ||
432 | void MyFrame::OnToggleImages(wxCommandEvent& event) | |
433 | { | |
f6bcfd97 BP |
434 | if ( wxGetApp().ShowImages() ) |
435 | { | |
436 | m_treeCtrl->CreateImageList(-1); | |
437 | ||
438 | wxGetApp().SetShowImages(FALSE); | |
439 | } | |
440 | else | |
441 | { | |
442 | m_treeCtrl->CreateImageList(); | |
443 | ||
444 | wxGetApp().SetShowImages(TRUE); | |
445 | } | |
2c8e4738 VZ |
446 | |
447 | OnRecreate(event); | |
448 | } | |
449 | ||
dd3646fd VZ |
450 | void MyFrame::OnCollapseAndReset(wxCommandEvent& event) |
451 | { | |
5f939e78 | 452 | m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem()); |
dd3646fd VZ |
453 | } |
454 | ||
f65635b5 VZ |
455 | void MyFrame::OnEnsureVisible(wxCommandEvent& event) |
456 | { | |
457 | m_treeCtrl->DoEnsureVisible(); | |
458 | } | |
459 | ||
75c74ca0 VZ |
460 | void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event)) |
461 | { | |
2c8e4738 VZ |
462 | int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File : -1; |
463 | m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), image, "2nd item"); | |
75c74ca0 VZ |
464 | } |
465 | ||
5f939e78 VZ |
466 | void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event)) |
467 | { | |
468 | static int s_num = 0; | |
469 | ||
470 | wxString text; | |
471 | text.Printf("Item #%d", ++s_num); | |
472 | ||
473 | m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(), | |
112c5086 RR |
474 | text /*, |
475 | MyTreeCtrl::TreeCtrlIcon_File */ ); | |
5f939e78 VZ |
476 | } |
477 | ||
cf724bce RR |
478 | void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event)) |
479 | { | |
480 | unsigned int indent = m_treeCtrl->GetIndent(); | |
481 | if (indent < 100) | |
482 | m_treeCtrl->SetIndent( indent+5 ); | |
483 | } | |
484 | ||
485 | void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event)) | |
486 | { | |
487 | unsigned int indent = m_treeCtrl->GetIndent(); | |
488 | if (indent > 10) | |
489 | m_treeCtrl->SetIndent( indent-5 ); | |
490 | } | |
491 | ||
492 | void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event)) | |
493 | { | |
494 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
495 | if (indent < 100) | |
496 | m_treeCtrl->SetSpacing( indent+5 ); | |
497 | } | |
498 | ||
499 | void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event)) | |
500 | { | |
501 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
502 | if (indent > 10) | |
503 | m_treeCtrl->SetSpacing( indent-5 ); | |
504 | } | |
505 | ||
9dfbf520 VZ |
506 | void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event)) |
507 | { | |
508 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
509 | ||
510 | CHECK_ITEM( item ); | |
511 | ||
512 | m_treeCtrl->DoToggleIcon(item); | |
513 | } | |
514 | ||
3a5a2f56 | 515 | // MyTreeCtrl implementation |
23fd5130 VZ |
516 | IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl) |
517 | ||
3a5a2f56 VZ |
518 | MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, |
519 | const wxPoint& pos, const wxSize& size, | |
520 | long style) | |
521 | : wxTreeCtrl(parent, id, pos, size, style) | |
457814b5 | 522 | { |
2c8e4738 | 523 | m_reverseSort = FALSE; |
47678105 | 524 | m_imageListNormal = NULL; |
91b8de8d | 525 | |
2c8e4738 | 526 | CreateImageList(); |
91b8de8d | 527 | |
2c8e4738 VZ |
528 | // Add some items to the tree |
529 | AddTestItemsToTree(3, 2); | |
530 | } | |
531 | ||
532 | void MyTreeCtrl::CreateImageList(int size) | |
533 | { | |
534 | delete m_imageListNormal; | |
535 | ||
536 | if ( size == -1 ) | |
537 | { | |
538 | m_imageListNormal = NULL; | |
2c8e4738 | 539 | } |
f6bcfd97 BP |
540 | else |
541 | { | |
542 | // Make an image list containing small icons | |
543 | m_imageListNormal = new wxImageList(size, size, TRUE); | |
e1ee62bd | 544 | |
f6bcfd97 | 545 | // should correspond to TreeCtrlIcon_xxx enum |
f60d0f94 | 546 | #if defined(__WXMSW__) && defined(__WIN16__) |
f6bcfd97 BP |
547 | m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE)); |
548 | m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE)); | |
549 | m_imageListNormal->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE)); | |
550 | m_imageListNormal->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE)); | |
551 | m_imageListNormal->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE)); | |
552 | #else // !MSW | |
553 | wxIcon icons[5]; | |
554 | icons[0] = wxICON(icon1); | |
555 | icons[1] = wxICON(icon2); | |
556 | icons[2] = wxICON(icon3); | |
557 | icons[3] = wxICON(icon4); | |
558 | icons[4] = wxICON(icon5); | |
559 | ||
560 | int sizeOrig = icons[0].GetWidth(); | |
561 | for ( size_t i = 0; i < WXSIZEOF(icons); i++ ) | |
2c8e4738 | 562 | { |
f6bcfd97 BP |
563 | if ( size == sizeOrig ) |
564 | { | |
565 | m_imageListNormal->Add(icons[i]); | |
566 | } | |
567 | else | |
568 | { | |
569 | m_imageListNormal->Add(wxImage(icons[i]).Rescale(size, size). | |
570 | ConvertToBitmap()); | |
571 | } | |
2c8e4738 | 572 | } |
f6bcfd97 | 573 | #endif // MSW/!MSW |
2c8e4738 | 574 | } |
457814b5 | 575 | |
5f939e78 | 576 | SetImageList(m_imageListNormal); |
457814b5 JS |
577 | } |
578 | ||
3a5a2f56 | 579 | MyTreeCtrl::~MyTreeCtrl() |
457814b5 | 580 | { |
5f939e78 | 581 | delete m_imageListNormal; |
457814b5 JS |
582 | } |
583 | ||
e1ee62bd VZ |
584 | int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1, |
585 | const wxTreeItemId& item2) | |
586 | { | |
5f939e78 VZ |
587 | if ( m_reverseSort ) |
588 | { | |
589 | // just exchange 1st and 2nd items | |
590 | return wxTreeCtrl::OnCompareItems(item2, item1); | |
591 | } | |
592 | else | |
593 | { | |
594 | return wxTreeCtrl::OnCompareItems(item1, item2); | |
595 | } | |
e1ee62bd VZ |
596 | } |
597 | ||
3a5a2f56 VZ |
598 | void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent, |
599 | size_t numChildren, | |
4832f7c0 VZ |
600 | size_t depth, |
601 | size_t folder) | |
457814b5 | 602 | { |
f65635b5 | 603 | if ( depth > 0 ) |
3a5a2f56 | 604 | { |
74b31181 VZ |
605 | bool hasChildren = depth > 1; |
606 | ||
f65635b5 VZ |
607 | wxString str; |
608 | for ( size_t n = 0; n < numChildren; n++ ) | |
609 | { | |
610 | // at depth 1 elements won't have any more children | |
74b31181 | 611 | if ( hasChildren ) |
f65635b5 | 612 | str.Printf("%s child %d", "Folder", n + 1); |
74b31181 VZ |
613 | else |
614 | str.Printf("%s child %d.%d", "File", folder, n + 1); | |
f65635b5 | 615 | |
74b31181 VZ |
616 | // here we pass to AppendItem() normal and selected item images (we |
617 | // suppose that selected image follows the normal one in the enum) | |
2c8e4738 VZ |
618 | int image, imageSel; |
619 | if ( wxGetApp().ShowImages() ) | |
620 | { | |
621 | image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder; | |
622 | imageSel = image + 1; | |
623 | } | |
624 | else | |
625 | { | |
626 | image = imageSel = -1; | |
627 | } | |
628 | wxTreeItemId id = AppendItem(idParent, str, image, imageSel, | |
f65635b5 VZ |
629 | new MyTreeItemData(str)); |
630 | ||
74b31181 | 631 | // and now we also set the expanded one (only for the folders) |
2c8e4738 | 632 | if ( hasChildren && wxGetApp().ShowImages() ) |
74b31181 VZ |
633 | { |
634 | SetItemImage(id, TreeCtrlIcon_FolderOpened, | |
635 | wxTreeItemIcon_Expanded); | |
636 | } | |
637 | ||
f65635b5 | 638 | // remember the last child for OnEnsureVisible() |
74b31181 | 639 | if ( !hasChildren && n == numChildren - 1 ) |
f65635b5 VZ |
640 | { |
641 | m_lastItem = id; | |
642 | } | |
643 | ||
644 | AddItemsRecursively(id, numChildren, depth - 1, n + 1); | |
645 | } | |
3a5a2f56 | 646 | } |
f65635b5 | 647 | //else: done! |
457814b5 JS |
648 | } |
649 | ||
3a5a2f56 VZ |
650 | void MyTreeCtrl::AddTestItemsToTree(size_t numChildren, |
651 | size_t depth) | |
457814b5 | 652 | { |
2c8e4738 | 653 | int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder : -1; |
5f939e78 | 654 | wxTreeItemId rootId = AddRoot("Root", |
2c8e4738 | 655 | image, image, |
5f939e78 | 656 | new MyTreeItemData("Root item")); |
2c8e4738 VZ |
657 | if ( image != -1 ) |
658 | { | |
659 | SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded); | |
660 | } | |
457814b5 | 661 | |
5f939e78 | 662 | AddItemsRecursively(rootId, numChildren, depth, 0); |
9ec64fa7 VZ |
663 | |
664 | // set some colours/fonts for testing | |
665 | SetItemFont(rootId, *wxITALIC_FONT); | |
666 | ||
667 | long cookie; | |
668 | wxTreeItemId id = GetFirstChild(rootId, cookie); | |
669 | SetItemTextColour(id, *wxBLUE); | |
670 | ||
671 | id = GetNextChild(rootId, cookie); | |
672 | id = GetNextChild(rootId, cookie); | |
673 | SetItemTextColour(id, *wxRED); | |
674 | SetItemBackgroundColour(id, *wxLIGHT_GREY); | |
4832f7c0 VZ |
675 | } |
676 | ||
677 | void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie) | |
678 | { | |
5f939e78 | 679 | wxTreeItemId id; |
4832f7c0 | 680 | |
5f939e78 VZ |
681 | if( cookie == -1 ) |
682 | id = GetFirstChild(idParent, cookie); | |
683 | else | |
684 | id = GetNextChild(idParent, cookie); | |
4832f7c0 | 685 | |
5f939e78 VZ |
686 | if(id <= 0) |
687 | return; | |
4832f7c0 | 688 | |
9dfbf520 | 689 | wxString text = GetItemText(id); |
5f939e78 | 690 | wxLogMessage(text); |
4832f7c0 | 691 | |
5f939e78 VZ |
692 | if (ItemHasChildren(id)) |
693 | GetItemsRecursively(id,-1); | |
4832f7c0 | 694 | |
5f939e78 | 695 | GetItemsRecursively(idParent, cookie); |
457814b5 JS |
696 | } |
697 | ||
9dfbf520 VZ |
698 | void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item) |
699 | { | |
700 | int image = GetItemImage(item) == TreeCtrlIcon_Folder ? TreeCtrlIcon_File | |
701 | : TreeCtrlIcon_Folder; | |
702 | ||
703 | SetItemImage(item, image); | |
704 | } | |
705 | ||
4832f7c0 | 706 | |
3a5a2f56 | 707 | // avoid repetition |
5f939e78 | 708 | #define TREE_EVENT_HANDLER(name) \ |
3dbeaa52 | 709 | void MyTreeCtrl::name(wxTreeEvent& event) \ |
5f939e78 VZ |
710 | { \ |
711 | wxLogMessage(#name); \ | |
3dbeaa52 | 712 | event.Skip(); \ |
5f939e78 | 713 | } |
457814b5 | 714 | |
3a5a2f56 | 715 | TREE_EVENT_HANDLER(OnBeginRDrag) |
3a5a2f56 VZ |
716 | TREE_EVENT_HANDLER(OnDeleteItem) |
717 | TREE_EVENT_HANDLER(OnGetInfo) | |
718 | TREE_EVENT_HANDLER(OnSetInfo) | |
719 | TREE_EVENT_HANDLER(OnItemExpanded) | |
720 | TREE_EVENT_HANDLER(OnItemExpanding) | |
721 | TREE_EVENT_HANDLER(OnItemCollapsed) | |
722 | TREE_EVENT_HANDLER(OnSelChanged) | |
723 | TREE_EVENT_HANDLER(OnSelChanging) | |
3dbeaa52 | 724 | TREE_EVENT_HANDLER(OnTreeKeyDown) |
3a5a2f56 VZ |
725 | |
726 | #undef TREE_EVENT_HANDLER | |
727 | ||
5888ef1e VZ |
728 | void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event) |
729 | { | |
730 | // need to explicitly allow drag | |
731 | if ( event.GetItem() != GetRootItem() ) | |
732 | { | |
733 | m_draggedItem = event.GetItem(); | |
734 | ||
735 | wxLogMessage("OnBeginDrag: started dragging %s", | |
736 | GetItemText(m_draggedItem).c_str()); | |
737 | ||
738 | event.Allow(); | |
739 | } | |
740 | else | |
741 | { | |
742 | wxLogMessage("OnBeginDrag: this item can't be dragged."); | |
743 | } | |
744 | } | |
745 | ||
746 | void MyTreeCtrl::OnEndDrag(wxTreeEvent& event) | |
747 | { | |
748 | wxTreeItemId itemSrc = m_draggedItem, | |
749 | itemDst = event.GetItem(); | |
e97320d4 | 750 | m_draggedItem = (wxTreeItemId)0l; |
5888ef1e VZ |
751 | |
752 | // where to copy the item? | |
753 | if ( itemDst.IsOk() && !ItemHasChildren(itemDst) ) | |
754 | { | |
755 | // copy to the parent then | |
756 | itemDst = GetParent(itemDst); | |
757 | } | |
758 | ||
759 | if ( !itemDst.IsOk() ) | |
760 | { | |
761 | wxLogMessage("OnEndDrag: can't drop here."); | |
762 | ||
763 | return; | |
764 | } | |
765 | ||
766 | wxString text = GetItemText(itemSrc); | |
767 | wxLogMessage("OnEndDrag: '%s' copied to '%s'.", | |
768 | text.c_str(), GetItemText(itemDst).c_str()); | |
769 | ||
770 | // just do append here - we could also insert it just before/after the item | |
771 | // on which it was dropped, but this requires slightly more work... we also | |
772 | // completely ignore the client data and icon of the old item but could | |
773 | // copy them as well. | |
774 | // | |
775 | // Finally, we only copy one item here but we might copy the entire tree if | |
776 | // we were dragging a folder. | |
2c8e4738 VZ |
777 | int image = wxGetApp().ShowImages() ? TreeCtrlIcon_File : -1; |
778 | AppendItem(itemDst, text, image); | |
5888ef1e VZ |
779 | } |
780 | ||
5ea47806 VZ |
781 | void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event) |
782 | { | |
783 | wxLogMessage("OnBeginLabelEdit"); | |
784 | ||
785 | // for testing, prevent this items label editing | |
786 | wxTreeItemId itemId = event.GetItem(); | |
787 | if ( IsTestItem(itemId) ) | |
788 | { | |
789 | wxMessageBox("You can't edit this item."); | |
790 | ||
791 | event.Veto(); | |
792 | } | |
793 | } | |
794 | ||
795 | void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event) | |
796 | { | |
797 | wxLogMessage("OnEndLabelEdit"); | |
798 | ||
799 | // don't allow anything except letters in the labels | |
800 | if ( !event.GetLabel().IsWord() ) | |
801 | { | |
802 | wxMessageBox("The label should contain only letters."); | |
803 | ||
804 | event.Veto(); | |
805 | } | |
806 | } | |
807 | ||
3a5a2f56 | 808 | void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event) |
457814b5 | 809 | { |
5f939e78 | 810 | wxLogMessage("OnItemCollapsing"); |
f135ff73 | 811 | |
5f939e78 VZ |
812 | // for testing, prevent the user from collapsing the first child folder |
813 | wxTreeItemId itemId = event.GetItem(); | |
5ea47806 | 814 | if ( IsTestItem(itemId) ) |
5f939e78 VZ |
815 | { |
816 | wxMessageBox("You can't collapse this item."); | |
457814b5 | 817 | |
5f939e78 VZ |
818 | event.Veto(); |
819 | } | |
457814b5 JS |
820 | } |
821 | ||
477350ce | 822 | void MyTreeCtrl::OnItemActivated(wxTreeEvent& event) |
457814b5 | 823 | { |
5f939e78 | 824 | // show some info about this item |
477350ce | 825 | wxTreeItemId itemId = event.GetItem(); |
5f939e78 | 826 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId); |
457814b5 | 827 | |
5f939e78 VZ |
828 | if ( item != NULL ) |
829 | { | |
830 | item->ShowInfo(this); | |
831 | } | |
457814b5 | 832 | |
5f939e78 VZ |
833 | wxLogMessage("OnItemActivated"); |
834 | } | |
835 | ||
836 | void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event) | |
837 | { | |
838 | wxTreeItemId id = HitTest(event.GetPosition()); | |
839 | if ( !id ) | |
840 | wxLogMessage("No item under mouse"); | |
841 | else | |
842 | { | |
843 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(id); | |
844 | if ( item ) | |
845 | wxLogMessage("Item '%s' under mouse", item->GetDesc()); | |
846 | } | |
457814b5 JS |
847 | } |
848 | ||
3a5a2f56 | 849 | static inline const char *Bool2String(bool b) |
457814b5 | 850 | { |
5f939e78 | 851 | return b ? "" : "not "; |
457814b5 JS |
852 | } |
853 | ||
9769e589 | 854 | void MyTreeItemData::ShowInfo(wxTreeCtrl *tree) |
457814b5 | 855 | { |
5f939e78 VZ |
856 | wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n" |
857 | "%u children (%u immediately under this item).", | |
858 | m_desc.c_str(), | |
859 | Bool2String(tree->IsSelected(GetId())), | |
860 | Bool2String(tree->IsExpanded(GetId())), | |
861 | Bool2String(tree->IsBold(GetId())), | |
862 | tree->GetChildrenCount(GetId()), | |
863 | tree->GetChildrenCount(GetId(), FALSE)); | |
457814b5 | 864 | } |