]> git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treetest.cpp
Fix build with wxUSE_FFILE=0.
[wxWidgets.git] / samples / treectrl / treetest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: treetest.cpp
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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #include "wx/log.h"
22 #endif
23
24 #include "wx/colordlg.h"
25 #include "wx/numdlg.h"
26
27 #include "wx/artprov.h"
28 #include "wx/image.h"
29 #include "wx/imaglist.h"
30 #include "wx/treectrl.h"
31 #include "wx/math.h"
32 #include "wx/renderer.h"
33 #include "wx/wupdlock.h"
34
35 #ifdef __WIN32__
36 // this is not supported by native control
37 #define NO_VARIABLE_HEIGHT
38 #endif
39
40 #include "treetest.h"
41
42 #include "icon1.xpm"
43 #include "icon2.xpm"
44 #include "icon3.xpm"
45 #include "icon4.xpm"
46 #include "icon5.xpm"
47
48 #include "state1.xpm"
49 #include "state2.xpm"
50 #include "state3.xpm"
51 #include "state4.xpm"
52 #include "state5.xpm"
53
54 #include "unchecked.xpm"
55 #include "checked.xpm"
56
57 #ifndef wxHAS_IMAGES_IN_RESOURCES
58 #include "../sample.xpm"
59 #endif
60
61 static const int NUM_CHILDREN_PER_LEVEL = 5;
62 static const int NUM_LEVELS = 2;
63
64 // verify that the item is ok and insult the user if it is not
65 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
66 wxMessageBox(wxT("Please select some item first!"), \
67 wxT("Tree sample error"), \
68 wxOK | wxICON_EXCLAMATION, \
69 this); \
70 return; \
71 }
72
73 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
74
75 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
76 EVT_IDLE(MyFrame::OnIdle)
77 EVT_SIZE(MyFrame::OnSize)
78
79 MENU_LINK(Quit)
80 MENU_LINK(About)
81 MENU_LINK(ClearLog)
82
83 MENU_LINK(TogButtons)
84 MENU_LINK(TogTwist)
85 MENU_LINK(TogLines)
86 MENU_LINK(TogEdit)
87 MENU_LINK(TogHideRoot)
88 MENU_LINK(TogRootLines)
89 MENU_LINK(TogBorder)
90 MENU_LINK(TogFullHighlight)
91 MENU_LINK(SetFgColour)
92 MENU_LINK(SetBgColour)
93 MENU_LINK(ResetStyle)
94
95 MENU_LINK(Highlight)
96 MENU_LINK(Dump)
97 #ifndef NO_MULTIPLE_SELECTION
98 MENU_LINK(DumpSelected)
99 MENU_LINK(Select)
100 MENU_LINK(Unselect)
101 MENU_LINK(ToggleSel)
102 MENU_LINK(SelectChildren)
103 #endif // NO_MULTIPLE_SELECTION
104 MENU_LINK(Rename)
105 MENU_LINK(Count)
106 MENU_LINK(CountRec)
107 MENU_LINK(Sort)
108 MENU_LINK(SortRev)
109 MENU_LINK(SetBold)
110 MENU_LINK(ClearBold)
111 MENU_LINK(Delete)
112 MENU_LINK(DeleteChildren)
113 MENU_LINK(DeleteAll)
114 MENU_LINK(Recreate)
115 MENU_LINK(ToggleImages)
116 MENU_LINK(ToggleStates)
117 MENU_LINK(ToggleBell)
118 MENU_LINK(ToggleAlternateImages)
119 MENU_LINK(ToggleAlternateStates)
120 MENU_LINK(ToggleButtons)
121 MENU_LINK(SetImageSize)
122 MENU_LINK(CollapseAndReset)
123 MENU_LINK(EnsureVisible)
124 MENU_LINK(SetFocus)
125 MENU_LINK(AddItem)
126 MENU_LINK(AddManyItems)
127 MENU_LINK(InsertItem)
128 MENU_LINK(IncIndent)
129 MENU_LINK(DecIndent)
130 MENU_LINK(IncSpacing)
131 MENU_LINK(DecSpacing)
132 MENU_LINK(ToggleIcon)
133 MENU_LINK(ToggleState)
134 MENU_LINK(SelectRoot)
135 MENU_LINK(SetFocusedRoot)
136 MENU_LINK(ClearFocused)
137
138 MENU_LINK(ShowFirstVisible)
139 #ifdef wxHAS_LAST_VISIBLE
140 MENU_LINK(ShowLastVisible)
141 #endif // wxHAS_LAST_VISIBLE
142 MENU_LINK(ShowNextVisible)
143 MENU_LINK(ShowPrevVisible)
144 MENU_LINK(ShowParent)
145 MENU_LINK(ShowPrevSibling)
146 MENU_LINK(ShowNextSibling)
147 MENU_LINK(ScrollTo)
148 MENU_LINK(SelectLast)
149 #undef MENU_LINK
150
151 END_EVENT_TABLE()
152
153 #if USE_GENERIC_TREECTRL
154 BEGIN_EVENT_TABLE(MyTreeCtrl, wxGenericTreeCtrl)
155 #else
156 BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
157 #endif
158 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
159 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
160 EVT_TREE_END_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnEndDrag)
161 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit)
162 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit)
163 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem)
164 #if 0 // there are so many of those that logging them causes flicker
165 EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo)
166 #endif
167 EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo)
168 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded)
169 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding)
170 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed)
171 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing)
172
173 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged)
174 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging)
175 EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown)
176 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated)
177 EVT_TREE_STATE_IMAGE_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemStateClick)
178
179 // so many different ways to handle right mouse button clicks...
180 EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu)
181 // EVT_TREE_ITEM_MENU is the preferred event for creating context menus
182 // on a tree control, because it includes the point of the click or item,
183 // meaning that no additional placement calculations are required.
184 EVT_TREE_ITEM_MENU(TreeTest_Ctrl, MyTreeCtrl::OnItemMenu)
185 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRClick)
186
187 EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown)
188 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp)
189 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick)
190 END_EVENT_TABLE()
191
192 IMPLEMENT_APP(MyApp)
193
194 bool MyApp::OnInit()
195 {
196 if ( !wxApp::OnInit() )
197 return false;
198
199 // Create the main frame window
200 MyFrame *frame = new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
201
202 // Show the frame
203 frame->Show(true);
204
205 return true;
206 }
207
208
209 // My frame constructor
210 MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
211 : wxFrame((wxFrame *)NULL, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)),
212 m_treeCtrl(NULL)
213 #if wxUSE_LOG
214 , m_textCtrl(NULL)
215 #endif // wxUSE_LOG
216 {
217 // This reduces flicker effects - even better would be to define
218 // OnEraseBackground to do nothing. When the tree control's scrollbars are
219 // show or hidden, the frame is sent a background erase event.
220 SetBackgroundColour(*wxWHITE);
221
222 // Give it an icon
223 SetIcon(wxICON(sample));
224
225 #if wxUSE_MENUS
226 // Make a menubar
227 wxMenu *file_menu = new wxMenu,
228 *style_menu = new wxMenu,
229 *tree_menu = new wxMenu,
230 *item_menu = new wxMenu;
231
232 #if wxUSE_LOG
233 file_menu->Append(TreeTest_ClearLog, wxT("&Clear log\tCtrl-L"));
234 file_menu->AppendSeparator();
235 #endif // wxUSE_LOG
236 file_menu->Append(TreeTest_About, wxT("&About"));
237 file_menu->AppendSeparator();
238 file_menu->Append(TreeTest_Quit, wxT("E&xit\tAlt-X"));
239
240 style_menu->AppendCheckItem(TreeTest_TogButtons, wxT("Toggle &normal buttons"));
241 style_menu->AppendCheckItem(TreeTest_TogTwist, wxT("Toggle &twister buttons"));
242 style_menu->AppendCheckItem(TreeTest_ToggleButtons, wxT("Toggle image &buttons"));
243 style_menu->AppendSeparator();
244 style_menu->AppendCheckItem(TreeTest_TogLines, wxT("Toggle &connecting lines"));
245 style_menu->AppendCheckItem(TreeTest_TogRootLines, wxT("Toggle &lines at root"));
246 style_menu->AppendCheckItem(TreeTest_TogHideRoot, wxT("Toggle &hidden root"));
247 style_menu->AppendCheckItem(TreeTest_TogBorder, wxT("Toggle &item border"));
248 style_menu->AppendCheckItem(TreeTest_TogFullHighlight, wxT("Toggle &full row highlight"));
249 style_menu->AppendCheckItem(TreeTest_TogEdit, wxT("Toggle &edit mode"));
250 #ifndef NO_MULTIPLE_SELECTION
251 style_menu->AppendCheckItem(TreeTest_ToggleSel, wxT("Toggle &selection mode\tCtrl-S"));
252 #endif // NO_MULTIPLE_SELECTION
253 style_menu->AppendCheckItem(TreeTest_ToggleImages, wxT("Toggle show ima&ges"));
254 style_menu->AppendCheckItem(TreeTest_ToggleStates, wxT("Toggle show st&ates"));
255 style_menu->AppendCheckItem(TreeTest_ToggleBell, wxT("Toggle &bell on no match"));
256 style_menu->AppendCheckItem(TreeTest_ToggleAlternateImages, wxT("Toggle alternate images"));
257 style_menu->AppendCheckItem(TreeTest_ToggleAlternateStates, wxT("Toggle alternate state images"));
258 style_menu->Append(TreeTest_SetImageSize, wxT("Set image si&ze..."));
259 style_menu->AppendSeparator();
260 style_menu->Append(TreeTest_SetFgColour, wxT("Set &foreground colour..."));
261 style_menu->Append(TreeTest_SetBgColour, wxT("Set &background colour..."));
262 style_menu->AppendSeparator();
263 style_menu->Append(TreeTest_ResetStyle, wxT("&Reset to default\tF10"));
264
265 tree_menu->Append(TreeTest_Recreate, wxT("&Recreate the tree"));
266 tree_menu->Append(TreeTest_CollapseAndReset, wxT("C&ollapse and reset"));
267 tree_menu->AppendSeparator();
268 tree_menu->Append(TreeTest_AddItem, wxT("Append a &new item"));
269 tree_menu->Append(TreeTest_AddManyItems, wxT("Appends &many items"));
270 tree_menu->Append(TreeTest_InsertItem, wxT("&Insert a new item"));
271 tree_menu->Append(TreeTest_Delete, wxT("&Delete this item"));
272 tree_menu->Append(TreeTest_DeleteChildren, wxT("Delete &children"));
273 tree_menu->Append(TreeTest_DeleteAll, wxT("Delete &all items"));
274 tree_menu->Append(TreeTest_SelectRoot, wxT("Select root item"));
275 tree_menu->AppendSeparator();
276 tree_menu->Append(TreeTest_SetFocusedRoot, wxT("Set focus to root item"));
277 tree_menu->Append(TreeTest_ClearFocused, wxT("Reset focus"));
278
279 tree_menu->AppendSeparator();
280 tree_menu->Append(TreeTest_Count, wxT("Count children of current item"));
281 tree_menu->Append(TreeTest_CountRec, wxT("Recursively count children of current item"));
282 tree_menu->AppendSeparator();
283 tree_menu->Append(TreeTest_Sort, wxT("Sort children of current item"));
284 tree_menu->Append(TreeTest_SortRev, wxT("Sort in reversed order"));
285 tree_menu->AppendSeparator();
286 tree_menu->Append(TreeTest_EnsureVisible, wxT("Make the last item &visible"));
287 tree_menu->Append(TreeTest_SetFocus, wxT("Set &focus to the tree"));
288 tree_menu->AppendSeparator();
289 tree_menu->Append(TreeTest_IncIndent, wxT("Add 5 points to indentation\tAlt-I"));
290 tree_menu->Append(TreeTest_DecIndent, wxT("Reduce indentation by 5 points\tAlt-R"));
291 tree_menu->AppendSeparator();
292 tree_menu->Append(TreeTest_IncSpacing, wxT("Add 5 points to spacing\tCtrl-I"));
293 tree_menu->Append(TreeTest_DecSpacing, wxT("Reduce spacing by 5 points\tCtrl-R"));
294
295 item_menu->Append(TreeTest_Dump, wxT("&Dump item children"));
296 item_menu->Append(TreeTest_Rename, wxT("&Rename item..."));
297
298 item_menu->AppendSeparator();
299 item_menu->Append(TreeTest_SetBold, wxT("Make item &bold"));
300 item_menu->Append(TreeTest_ClearBold, wxT("Make item &not bold"));
301 item_menu->AppendSeparator();
302 item_menu->Append(TreeTest_ToggleIcon, wxT("Toggle the item's &icon"));
303 item_menu->Append(TreeTest_ToggleState, wxT("Toggle the item's &state"));
304 item_menu->AppendSeparator();
305 item_menu->Append(TreeTest_ShowFirstVisible, wxT("Show &first visible"));
306 #ifdef wxHAS_LAST_VISIBLE
307 item_menu->Append(TreeTest_ShowLastVisible, wxT("Show &last visible"));
308 #endif // wxHAS_LAST_VISIBLE
309 item_menu->Append(TreeTest_ShowNextVisible, wxT("Show &next visible"));
310 item_menu->Append(TreeTest_ShowPrevVisible, wxT("Show &previous visible"));
311 item_menu->AppendSeparator();
312 item_menu->Append(TreeTest_ShowParent, "Show pa&rent");
313 item_menu->Append(TreeTest_ShowPrevSibling, "Show &previous sibling");
314 item_menu->Append(TreeTest_ShowNextSibling, "Show &next sibling");
315 item_menu->AppendSeparator();
316 item_menu->Append(TreeTest_ScrollTo, "Scroll &to item",
317 "Scroll to the last by one top level child");
318 item_menu->Append(TreeTest_SelectLast, "Select &last item",
319 "Select the last item");
320
321 #ifndef NO_MULTIPLE_SELECTION
322 item_menu->AppendSeparator();
323 item_menu->Append(TreeTest_DumpSelected, wxT("Dump selected items\tAlt-D"));
324 item_menu->Append(TreeTest_Select, wxT("Select current item\tAlt-S"));
325 item_menu->Append(TreeTest_Unselect, wxT("Unselect everything\tAlt-U"));
326 item_menu->Append(TreeTest_SelectChildren, wxT("Select all children\tCtrl-A"));
327 #endif // NO_MULTIPLE_SELECTION
328
329 wxMenuBar *menu_bar = new wxMenuBar;
330 menu_bar->Append(file_menu, wxT("&File"));
331 menu_bar->Append(style_menu, wxT("&Style"));
332 menu_bar->Append(tree_menu, wxT("&Tree"));
333 menu_bar->Append(item_menu, wxT("&Item"));
334 SetMenuBar(menu_bar);
335 #endif // wxUSE_MENUS
336
337 m_panel = new wxPanel(this);
338
339 #if wxUSE_LOG
340 // create the controls
341 m_textCtrl = new wxTextCtrl(m_panel, wxID_ANY, wxT(""),
342 wxDefaultPosition, wxDefaultSize,
343 wxTE_MULTILINE | wxSUNKEN_BORDER);
344
345 #ifdef __WXMOTIF__
346 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
347 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
348 delete wxLog::SetActiveTarget(new wxLogStderr);
349 #else
350 // set our text control as the log target
351 wxLogTextCtrl *logWindow = new wxLogTextCtrl(m_textCtrl);
352 delete wxLog::SetActiveTarget(logWindow);
353 #endif
354 #endif // wxUSE_LOG
355
356 CreateTreeWithDefStyle();
357
358 menu_bar->Check(TreeTest_ToggleImages, true);
359 menu_bar->Check(TreeTest_ToggleStates, true);
360 menu_bar->Check(TreeTest_ToggleAlternateImages, false);
361 menu_bar->Check(TreeTest_ToggleAlternateStates, false);
362
363 #if wxUSE_STATUSBAR
364 // create a status bar
365 CreateStatusBar(2);
366 #endif // wxUSE_STATUSBAR
367 }
368
369 MyFrame::~MyFrame()
370 {
371 #if wxUSE_LOG
372 delete wxLog::SetActiveTarget(NULL);
373 #endif // wxUSE_LOG
374 }
375
376 void MyFrame::CreateTreeWithDefStyle()
377 {
378 long style = wxTR_DEFAULT_STYLE |
379 #ifndef NO_VARIABLE_HEIGHT
380 wxTR_HAS_VARIABLE_ROW_HEIGHT |
381 #endif
382 wxTR_EDIT_LABELS;
383
384 CreateTree(style | wxSUNKEN_BORDER);
385
386 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
387 // everything
388 wxMenuBar *mbar = GetMenuBar();
389 mbar->Check(TreeTest_TogButtons, (style & wxTR_HAS_BUTTONS) != 0);
390 mbar->Check(TreeTest_TogButtons, (style & wxTR_TWIST_BUTTONS) != 0);
391 mbar->Check(TreeTest_TogLines, (style & wxTR_NO_LINES) == 0);
392 mbar->Check(TreeTest_TogRootLines, (style & wxTR_LINES_AT_ROOT) != 0);
393 mbar->Check(TreeTest_TogHideRoot, (style & wxTR_HIDE_ROOT) != 0);
394 mbar->Check(TreeTest_TogEdit, (style & wxTR_EDIT_LABELS) != 0);
395 mbar->Check(TreeTest_TogBorder, (style & wxTR_ROW_LINES) != 0);
396 mbar->Check(TreeTest_TogFullHighlight, (style & wxTR_FULL_ROW_HIGHLIGHT) != 0);
397 }
398
399 void MyFrame::CreateTree(long style)
400 {
401 m_treeCtrl = new MyTreeCtrl(m_panel, TreeTest_Ctrl,
402 wxDefaultPosition, wxDefaultSize,
403 style);
404
405 GetMenuBar()->Enable(TreeTest_SelectRoot, !(style & wxTR_HIDE_ROOT));
406
407 Resize();
408 }
409
410 void MyFrame::TogStyle(int id, long flag)
411 {
412 long style = m_treeCtrl->GetWindowStyle() ^ flag;
413
414 // most treectrl styles can't be changed on the fly using the native
415 // control and the tree must be recreated
416 #ifndef __WXMSW__
417 m_treeCtrl->SetWindowStyle(style);
418 #else // MSW
419 delete m_treeCtrl;
420 CreateTree(style);
421 #endif // !MSW/MSW
422
423 GetMenuBar()->Check(id, (style & flag) != 0);
424 }
425
426 void MyFrame::OnIdle(wxIdleEvent& event)
427 {
428 #if wxUSE_STATUSBAR
429 if ( m_treeCtrl )
430 {
431 wxTreeItemId idRoot = m_treeCtrl->GetRootItem();
432 wxString status;
433 if (idRoot.IsOk())
434 {
435 wxTreeItemId idLast = m_treeCtrl->GetLastChild(idRoot);
436 status = wxString::Format(
437 wxT("Root/last item is %svisible/%svisible"),
438 m_treeCtrl->IsVisible(idRoot) ? wxT("") : wxT("not "),
439 idLast.IsOk() && m_treeCtrl->IsVisible(idLast)
440 ? wxT("") : wxT("not "));
441 }
442 else
443 status = wxT("No root item");
444
445 SetStatusText(status, 1);
446 }
447 #endif // wxUSE_STATUSBAR
448
449 event.Skip();
450 }
451
452 void MyFrame::OnSize(wxSizeEvent& event)
453 {
454 if ( m_treeCtrl
455 #if wxUSE_LOG
456 && m_textCtrl
457 #endif // wxUSE_LOG
458 )
459 {
460 Resize();
461 }
462
463 event.Skip();
464 }
465
466 void MyFrame::Resize()
467 {
468 wxSize size = GetClientSize();
469 m_treeCtrl->SetSize(0, 0, size.x, size.y
470 #if wxUSE_LOG
471 *2/3);
472 m_textCtrl->SetSize(0, 2*size.y/3, size.x, size.y/3
473 #endif
474 );
475 }
476
477 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
478 {
479 Close(true);
480 }
481
482 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
483 {
484 wxMessageBox(wxT("Tree test sample\n")
485 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
486 wxT("About tree test"),
487 wxOK | wxICON_INFORMATION, this);
488 }
489
490 void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
491 {
492 m_textCtrl->Clear();
493 }
494
495 void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
496 {
497 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
498
499 CHECK_ITEM( item );
500
501 // old code - now we edit in place
502 #if 0
503 static wxString s_text;
504 s_text = wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
505 s_text, this);
506 if ( !s_text.empty() )
507 {
508 m_treeCtrl->SetItemText(item, s_text);
509 }
510 #endif // 0
511
512 // TODO demonstrate creating a custom edit control...
513 (void)m_treeCtrl->EditLabel(item);
514 }
515
516 void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event))
517 {
518 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
519
520 CHECK_ITEM( item );
521
522 int i = m_treeCtrl->GetChildrenCount( item, false );
523
524 wxLogMessage(wxT("%d children"), i);
525 }
526
527 void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event))
528 {
529 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
530
531 CHECK_ITEM( item );
532
533 int i = m_treeCtrl->GetChildrenCount( item );
534
535 wxLogMessage(wxT("%d children"), i);
536 }
537
538 void MyFrame::DoSort(bool reverse)
539 {
540 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
541
542 CHECK_ITEM( item );
543
544 m_treeCtrl->DoSortChildren(item, reverse);
545 }
546
547 void MyFrame::OnHighlight(wxCommandEvent& WXUNUSED(event))
548 {
549 wxTreeItemId id = m_treeCtrl->GetFocusedItem();
550
551 CHECK_ITEM( id );
552
553 wxRect r;
554 if ( !m_treeCtrl->GetBoundingRect(id, r, true /* text, not full row */) )
555 {
556 wxLogMessage(wxT("Failed to get bounding item rect"));
557 return;
558 }
559
560 wxClientDC dc(m_treeCtrl);
561 dc.SetBrush(*wxRED);
562 dc.SetPen(*wxTRANSPARENT_PEN);
563 dc.DrawRectangle(r);
564 m_treeCtrl->Update();
565 }
566
567 void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
568 {
569 wxTreeItemId root = m_treeCtrl->GetFocusedItem();
570
571 CHECK_ITEM( root );
572
573 m_treeCtrl->GetItemsRecursively(root);
574 }
575
576 #ifndef NO_MULTIPLE_SELECTION
577
578 void MyFrame::OnToggleSel(wxCommandEvent& event)
579 {
580 TogStyle(event.GetId(), wxTR_MULTIPLE);
581 }
582
583 void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event))
584 {
585 wxArrayTreeItemIds array;
586
587 size_t count = m_treeCtrl->GetSelections(array);
588 wxLogMessage(wxT("%u items selected"), unsigned(count));
589
590 for ( size_t n = 0; n < count; n++ )
591 {
592 wxLogMessage(wxT("\t%s"), m_treeCtrl->GetItemText(array.Item(n)).c_str());
593 }
594 }
595
596 void MyFrame::OnSelect(wxCommandEvent& WXUNUSED(event))
597 {
598 m_treeCtrl->SelectItem(m_treeCtrl->GetFocusedItem());
599 }
600
601 void MyFrame::OnSelectRoot(wxCommandEvent& WXUNUSED(event))
602 {
603 if ( !m_treeCtrl->HasFlag(wxTR_HIDE_ROOT) )
604 m_treeCtrl->SelectItem(m_treeCtrl->GetRootItem());
605 }
606
607 void MyFrame::OnSetFocusedRoot(wxCommandEvent& WXUNUSED(event))
608 {
609 if ( !m_treeCtrl->HasFlag(wxTR_HIDE_ROOT) )
610 m_treeCtrl->SetFocusedItem(m_treeCtrl->GetRootItem());
611 }
612
613 void MyFrame::OnClearFocused(wxCommandEvent& WXUNUSED(event))
614 {
615 m_treeCtrl->ClearFocusedItem();
616 }
617
618 void MyFrame::OnUnselect(wxCommandEvent& WXUNUSED(event))
619 {
620 m_treeCtrl->UnselectAll();
621 }
622
623 void MyFrame::OnSelectChildren(wxCommandEvent& WXUNUSED(event))
624 {
625 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
626 if ( !item.IsOk() )
627 item = m_treeCtrl->GetRootItem();
628
629 m_treeCtrl->SelectChildren(item);
630 }
631
632 #endif // NO_MULTIPLE_SELECTION
633
634 void MyFrame::DoSetBold(bool bold)
635 {
636 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
637
638 CHECK_ITEM( item );
639
640 m_treeCtrl->SetItemBold(item, bold);
641 }
642
643 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
644 {
645 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
646
647 CHECK_ITEM( item );
648
649 m_treeCtrl->Delete(item);
650 }
651
652 void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event))
653 {
654 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
655
656 CHECK_ITEM( item );
657
658 m_treeCtrl->DeleteChildren(item);
659 }
660
661 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
662 {
663 m_treeCtrl->DeleteAllItems();
664 }
665
666 void MyFrame::OnRecreate(wxCommandEvent& event)
667 {
668 OnDeleteAll(event);
669 m_treeCtrl->AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL, NUM_LEVELS);
670 }
671
672 void MyFrame::OnSetImageSize(wxCommandEvent& WXUNUSED(event))
673 {
674 int size = wxGetNumberFromUser(wxT("Enter the size for the images to use"),
675 wxT("Size: "),
676 wxT("TreeCtrl sample"),
677 m_treeCtrl->ImageSize());
678 if ( size == -1 )
679 return;
680
681 m_treeCtrl->CreateImageList(size);
682 wxGetApp().SetShowImages(true);
683 }
684
685 void MyFrame::OnToggleImages(wxCommandEvent& WXUNUSED(event))
686 {
687 if ( wxGetApp().ShowImages() )
688 {
689 m_treeCtrl->CreateImageList(-1);
690 wxGetApp().SetShowImages(false);
691 }
692 else
693 {
694 m_treeCtrl->CreateImageList(0);
695 wxGetApp().SetShowImages(true);
696 }
697 }
698
699 void MyFrame::OnToggleStates(wxCommandEvent& WXUNUSED(event))
700 {
701 if ( wxGetApp().ShowStates() )
702 {
703 m_treeCtrl->CreateStateImageList(true);
704 wxGetApp().SetShowStates(false);
705 }
706 else
707 {
708 m_treeCtrl->CreateStateImageList(false);
709 wxGetApp().SetShowStates(true);
710 }
711 }
712
713 void MyFrame::OnToggleBell(wxCommandEvent& event)
714 {
715 m_treeCtrl->EnableBellOnNoMatch(event.IsChecked());
716 }
717
718 void MyFrame::OnToggleAlternateImages(wxCommandEvent& WXUNUSED(event))
719 {
720 bool alternateImages = m_treeCtrl->AlternateImages();
721
722 m_treeCtrl->SetAlternateImages(!alternateImages);
723 m_treeCtrl->CreateImageList(0);
724 }
725
726 void MyFrame::OnToggleAlternateStates(wxCommandEvent& WXUNUSED(event))
727 {
728 bool alternateStates = m_treeCtrl->AlternateStates();
729
730 m_treeCtrl->SetAlternateStates(!alternateStates);
731 m_treeCtrl->CreateStateImageList();
732
733 // normal states < alternate states
734 // so we must reset broken states
735 if ( alternateStates )
736 m_treeCtrl->ResetBrokenStateImages();
737 }
738
739 void MyFrame::OnToggleButtons(wxCommandEvent& WXUNUSED(event))
740 {
741 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
742 if ( wxGetApp().ShowButtons() )
743 {
744 m_treeCtrl->CreateButtonsImageList(-1);
745 wxGetApp().SetShowButtons(false);
746 }
747 else
748 {
749 m_treeCtrl->CreateButtonsImageList(15);
750 wxGetApp().SetShowButtons(true);
751 }
752 #endif
753 }
754
755 void MyFrame::OnCollapseAndReset(wxCommandEvent& WXUNUSED(event))
756 {
757 m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem());
758 }
759
760 void MyFrame::OnEnsureVisible(wxCommandEvent& WXUNUSED(event))
761 {
762 m_treeCtrl->DoEnsureVisible();
763 }
764
765 void MyFrame::OnSetFocus(wxCommandEvent& WXUNUSED(event))
766 {
767 m_treeCtrl->SetFocus();
768 }
769
770 void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event))
771 {
772 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File : -1;
773 m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), image, wxT("2nd item"));
774 }
775
776 void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event))
777 {
778 static int s_num = 0;
779
780 wxString text;
781 text.Printf(wxT("Item #%d"), ++s_num);
782
783 m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(),
784 text /*,
785 MyTreeCtrl::TreeCtrlIcon_File */ );
786 }
787
788 void MyFrame::OnAddManyItems(wxCommandEvent& WXUNUSED(event))
789 {
790 wxWindowUpdateLocker lockUpdates(this);
791
792 const wxTreeItemId root = m_treeCtrl->GetRootItem();
793 for ( int n = 0; n < 1000; n++ )
794 {
795 m_treeCtrl->AppendItem(root, wxString::Format("Item #%03d", n));
796 }
797 }
798
799 void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event))
800 {
801 unsigned int indent = m_treeCtrl->GetIndent();
802 if (indent < 100)
803 m_treeCtrl->SetIndent( indent+5 );
804 }
805
806 void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event))
807 {
808 unsigned int indent = m_treeCtrl->GetIndent();
809 if (indent > 10)
810 m_treeCtrl->SetIndent( indent-5 );
811 }
812
813 void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event))
814 {
815 unsigned int indent = m_treeCtrl->GetSpacing();
816 if (indent < 100)
817 {
818 m_treeCtrl->SetSpacing( indent+5 );
819 m_treeCtrl->Refresh();
820 }
821 }
822
823 void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event))
824 {
825 unsigned int indent = m_treeCtrl->GetSpacing();
826 if (indent > 10)
827 {
828 m_treeCtrl->SetSpacing( indent-5 );
829 m_treeCtrl->Refresh();
830 }
831 }
832
833 void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event))
834 {
835 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
836
837 CHECK_ITEM( item );
838
839 m_treeCtrl->DoToggleIcon(item);
840 }
841
842 void MyFrame::OnToggleState(wxCommandEvent& WXUNUSED(event))
843 {
844 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
845
846 CHECK_ITEM( item );
847
848 m_treeCtrl->DoToggleState(item);
849 }
850
851 void MyFrame::DoShowFirstOrLast(TreeFunc0_t pfn, const wxString& label)
852 {
853 const wxTreeItemId item = (m_treeCtrl->*pfn)();
854
855 if ( !item.IsOk() )
856 {
857 wxLogMessage("There is no %s item", label);
858 }
859 else
860 {
861 wxLogMessage("The %s item is \"%s\"",
862 label, m_treeCtrl->GetItemText(item));
863 }
864 }
865
866 void MyFrame::DoShowRelativeItem(TreeFunc1_t pfn, const wxString& label)
867 {
868 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
869
870 CHECK_ITEM( item );
871
872 if ((pfn == (TreeFunc1_t) &wxTreeCtrl::GetPrevVisible
873 || pfn == (TreeFunc1_t) &wxTreeCtrl::GetNextVisible)
874 && !m_treeCtrl->IsVisible(item))
875 {
876 wxLogMessage("The selected item must be visible.");
877 return;
878 }
879
880 wxTreeItemId new_item = (m_treeCtrl->*pfn)(item);
881
882 if ( !new_item.IsOk() )
883 {
884 wxLogMessage("There is no %s item", label);
885 }
886 else
887 {
888 wxLogMessage("The %s item is \"%s\"",
889 label, m_treeCtrl->GetItemText(new_item));
890 }
891 }
892
893 void MyFrame::OnScrollTo(wxCommandEvent& WXUNUSED(event))
894 {
895 // scroll to the last but one top level child
896 wxTreeItemId item = m_treeCtrl->GetPrevSibling(
897 m_treeCtrl->GetLastChild(
898 m_treeCtrl->GetRootItem()));
899 CHECK_ITEM( item );
900
901 m_treeCtrl->ScrollTo(item);
902 }
903
904 void MyFrame::OnSelectLast(wxCommandEvent& WXUNUSED(event))
905 {
906 // select the very last item of the tree
907 wxTreeItemId item = m_treeCtrl->GetRootItem();
908 for ( ;; )
909 {
910 wxTreeItemId itemChild = m_treeCtrl->GetLastChild(item);
911 if ( !itemChild.IsOk() )
912 break;
913
914 item = itemChild;
915 }
916
917 CHECK_ITEM( item );
918
919 m_treeCtrl->SelectItem(item);
920 }
921
922 void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
923 {
924 wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetForegroundColour());
925 if ( col.IsOk() )
926 m_treeCtrl->SetForegroundColour(col);
927 }
928
929 void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
930 {
931 wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetBackgroundColour());
932 if ( col.IsOk() )
933 m_treeCtrl->SetBackgroundColour(col);
934 }
935
936 // MyTreeCtrl implementation
937 #if USE_GENERIC_TREECTRL
938 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxGenericTreeCtrl)
939 #else
940 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
941 #endif
942
943 MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
944 const wxPoint& pos, const wxSize& size,
945 long style)
946 : wxTreeCtrl(parent, id, pos, size, style),
947 m_alternateImages(false),
948 m_alternateStates(false)
949 {
950 m_reverseSort = false;
951
952 CreateImageList();
953 CreateStateImageList();
954
955 // Add some items to the tree
956 AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL, NUM_LEVELS);
957 }
958
959 void MyTreeCtrl::CreateImageList(int size)
960 {
961 if ( size == -1 )
962 {
963 SetImageList(NULL);
964 return;
965 }
966 if ( size == 0 )
967 size = m_imageSize;
968 else
969 m_imageSize = size;
970
971 // Make an image list containing small icons
972 wxImageList *images = new wxImageList(size, size, true);
973
974 // should correspond to TreeCtrlIcon_xxx enum
975 wxBusyCursor wait;
976 wxIcon icons[5];
977
978 if (m_alternateImages)
979 {
980 icons[TreeCtrlIcon_File] = wxIcon(icon1_xpm);
981 icons[TreeCtrlIcon_FileSelected] = wxIcon(icon2_xpm);
982 icons[TreeCtrlIcon_Folder] = wxIcon(icon3_xpm);
983 icons[TreeCtrlIcon_FolderSelected] = wxIcon(icon4_xpm);
984 icons[TreeCtrlIcon_FolderOpened] = wxIcon(icon5_xpm);
985 }
986 else
987 {
988 wxSize iconSize(size, size);
989
990 icons[TreeCtrlIcon_File] =
991 icons[TreeCtrlIcon_FileSelected] = wxArtProvider::GetIcon(wxART_NORMAL_FILE, wxART_LIST, iconSize);
992 icons[TreeCtrlIcon_Folder] =
993 icons[TreeCtrlIcon_FolderSelected] =
994 icons[TreeCtrlIcon_FolderOpened] = wxArtProvider::GetIcon(wxART_FOLDER, wxART_LIST, iconSize);
995 }
996
997 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
998 {
999 int sizeOrig = icons[0].GetWidth();
1000 if ( size == sizeOrig )
1001 {
1002 images->Add(icons[i]);
1003 }
1004 else
1005 {
1006 images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
1007 }
1008 }
1009
1010 AssignImageList(images);
1011 }
1012
1013 void MyTreeCtrl::CreateStateImageList(bool del)
1014 {
1015 if ( del )
1016 {
1017 SetStateImageList(NULL);
1018 return;
1019 }
1020
1021 wxImageList *states;
1022 wxBusyCursor wait;
1023
1024 if (m_alternateStates)
1025 {
1026 wxIcon icons[5];
1027 icons[0] = wxIcon(state1_xpm); // yellow
1028 icons[1] = wxIcon(state2_xpm); // green
1029 icons[2] = wxIcon(state3_xpm); // red
1030 icons[3] = wxIcon(state4_xpm); // blue
1031 icons[4] = wxIcon(state5_xpm); // black
1032
1033 int width = icons[0].GetWidth(),
1034 height = icons[0].GetHeight();
1035
1036 // Make an state image list containing small icons
1037 states = new wxImageList(width, height, true);
1038
1039 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
1040 states->Add(icons[i]);
1041 }
1042 else
1043 {
1044 wxIcon icons[2];
1045 icons[0] = wxIcon(unchecked_xpm);
1046 icons[1] = wxIcon(checked_xpm);
1047
1048 int width = icons[0].GetWidth(),
1049 height = icons[0].GetHeight();
1050
1051 // Make an state image list containing small icons
1052 states = new wxImageList(width, height, true);
1053
1054 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
1055 states->Add(icons[i]);
1056 }
1057
1058 AssignStateImageList(states);
1059 }
1060
1061 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
1062 void MyTreeCtrl::CreateButtonsImageList(int size)
1063 {
1064 if ( size == -1 )
1065 {
1066 SetButtonsImageList(NULL);
1067 return;
1068 }
1069
1070 // Make an image list containing small icons
1071 wxImageList *images = new wxImageList(size, size, true);
1072
1073 // should correspond to TreeCtrlIcon_xxx enum
1074 wxBusyCursor wait;
1075 wxIcon icons[4];
1076
1077 if (m_alternateImages)
1078 {
1079 icons[0] = wxIcon(icon3_xpm); // closed
1080 icons[1] = wxIcon(icon3_xpm); // closed, selected
1081 icons[2] = wxIcon(icon5_xpm); // open
1082 icons[3] = wxIcon(icon5_xpm); // open, selected
1083 }
1084 else
1085 {
1086 wxSize iconSize(size, size);
1087
1088 icons[0] = // closed
1089 icons[1] = wxArtProvider::GetIcon(wxART_FOLDER, wxART_LIST, iconSize); // closed, selected
1090 icons[2] = // open
1091 icons[3] = wxArtProvider::GetIcon(wxART_FOLDER_OPEN, wxART_LIST, iconSize);// open, selected
1092 }
1093
1094 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
1095 {
1096 int sizeOrig = icons[i].GetWidth();
1097 if ( size == sizeOrig )
1098 {
1099 images->Add(icons[i]);
1100 }
1101 else
1102 {
1103 images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
1104 }
1105 }
1106
1107 AssignButtonsImageList(images);
1108 #else
1109 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size))
1110 {
1111 #endif
1112 }
1113
1114 int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1115 const wxTreeItemId& item2)
1116 {
1117 if ( m_reverseSort )
1118 {
1119 // just exchange 1st and 2nd items
1120 return wxTreeCtrl::OnCompareItems(item2, item1);
1121 }
1122 else
1123 {
1124 return wxTreeCtrl::OnCompareItems(item1, item2);
1125 }
1126 }
1127
1128 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
1129 size_t numChildren,
1130 size_t depth,
1131 size_t folder)
1132 {
1133 if ( depth > 0 )
1134 {
1135 bool hasChildren = depth > 1;
1136
1137 wxString str;
1138 for ( size_t n = 0; n < numChildren; n++ )
1139 {
1140 // at depth 1 elements won't have any more children
1141 if ( hasChildren )
1142 str.Printf(wxT("%s child %u"), wxT("Folder"), unsigned(n + 1));
1143 else
1144 str.Printf(wxT("%s child %u.%u"), wxT("File"), unsigned(folder), unsigned(n + 1));
1145
1146 // here we pass to AppendItem() normal and selected item images (we
1147 // suppose that selected image follows the normal one in the enum)
1148 int image, imageSel;
1149 if ( wxGetApp().ShowImages() )
1150 {
1151 image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
1152 imageSel = image + 1;
1153 }
1154 else
1155 {
1156 image = imageSel = -1;
1157 }
1158 wxTreeItemId id = AppendItem(idParent, str, image, imageSel,
1159 new MyTreeItemData(str));
1160
1161 if ( wxGetApp().ShowStates() )
1162 SetItemState(id, 0);
1163
1164 // and now we also set the expanded one (only for the folders)
1165 if ( hasChildren && wxGetApp().ShowImages() )
1166 {
1167 SetItemImage(id, TreeCtrlIcon_FolderOpened,
1168 wxTreeItemIcon_Expanded);
1169 }
1170
1171 // remember the last child for OnEnsureVisible()
1172 if ( !hasChildren && n == numChildren - 1 )
1173 {
1174 m_lastItem = id;
1175 }
1176
1177 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
1178 }
1179 }
1180 //else: done!
1181 }
1182
1183 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
1184 size_t depth)
1185 {
1186 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder : -1;
1187 wxTreeItemId rootId = AddRoot(wxT("Root"),
1188 image, image,
1189 new MyTreeItemData(wxT("Root item")));
1190 if ( !HasFlag(wxTR_HIDE_ROOT) && image != -1 )
1191 {
1192 SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded);
1193 }
1194
1195 AddItemsRecursively(rootId, numChildren, depth, 0);
1196
1197 // set some colours/fonts for testing
1198 if ( !HasFlag(wxTR_HIDE_ROOT) )
1199 SetItemFont(rootId, *wxITALIC_FONT);
1200
1201 wxTreeItemIdValue cookie;
1202 wxTreeItemId id = GetFirstChild(rootId, cookie);
1203 SetItemTextColour(id, *wxBLUE);
1204
1205 id = GetNextChild(rootId, cookie);
1206 if ( id )
1207 id = GetNextChild(rootId, cookie);
1208 if ( id )
1209 {
1210 SetItemTextColour(id, *wxRED);
1211 SetItemBackgroundColour(id, *wxLIGHT_GREY);
1212 }
1213 }
1214
1215 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent,
1216 wxTreeItemIdValue cookie)
1217 {
1218 wxTreeItemId id;
1219
1220 if ( !cookie )
1221 id = GetFirstChild(idParent, cookie);
1222 else
1223 id = GetNextChild(idParent, cookie);
1224
1225 if ( !id.IsOk() )
1226 return;
1227
1228 wxString text = GetItemText(id);
1229 wxLogMessage(text);
1230
1231 if (ItemHasChildren(id))
1232 GetItemsRecursively(id);
1233
1234 GetItemsRecursively(idParent, cookie);
1235 }
1236
1237 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item)
1238 {
1239 int image = GetItemImage(item) == TreeCtrlIcon_Folder
1240 ? TreeCtrlIcon_File
1241 : TreeCtrlIcon_Folder;
1242 SetItemImage(item, image, wxTreeItemIcon_Normal);
1243
1244 image = GetItemImage(item, wxTreeItemIcon_Selected) == TreeCtrlIcon_FolderSelected
1245 ? TreeCtrlIcon_FileSelected
1246 : TreeCtrlIcon_FolderSelected;
1247 SetItemImage(item, image, wxTreeItemIcon_Selected);
1248 }
1249
1250 void MyTreeCtrl::DoToggleState(const wxTreeItemId& item)
1251 {
1252 if ( m_alternateStates )
1253 {
1254 // sets random state unlike current
1255 int state = GetItemState(item);
1256 int nState;
1257
1258 srand (time(NULL));
1259 do {
1260 nState = rand() % GetStateImageList()->GetImageCount();
1261 } while (nState == state);
1262
1263 SetItemState(item, nState);
1264 }
1265 else
1266 {
1267 // we have only 2 checkbox states, so next state will be reversed
1268 SetItemState(item, wxTREE_ITEMSTATE_NEXT);
1269 }
1270 }
1271
1272 void MyTreeCtrl::DoResetBrokenStateImages(const wxTreeItemId& idParent,
1273 wxTreeItemIdValue cookie, int state)
1274 {
1275 wxTreeItemId id;
1276
1277 if ( !cookie )
1278 id = GetFirstChild(idParent, cookie);
1279 else
1280 id = GetNextChild(idParent, cookie);
1281
1282 if ( !id.IsOk() )
1283 return;
1284
1285 int curState = GetItemState(id);
1286 if ( curState != wxTREE_ITEMSTATE_NONE && curState > state )
1287 SetItemState(id, state);
1288
1289 if (ItemHasChildren(id))
1290 DoResetBrokenStateImages(id, 0, state);
1291
1292 DoResetBrokenStateImages(idParent, cookie, state);
1293 }
1294
1295 void MyTreeCtrl::LogEvent(const wxChar *name, const wxTreeEvent& event)
1296 {
1297 wxTreeItemId item = event.GetItem();
1298 wxString text;
1299 if ( item.IsOk() )
1300 text << wxT('"') << GetItemText(item).c_str() << wxT('"');
1301 else
1302 text = wxT("invalid item");
1303 wxLogMessage(wxT("%s(%s)"), name, text.c_str());
1304 }
1305
1306 // avoid repetition
1307 #define TREE_EVENT_HANDLER(name) \
1308 void MyTreeCtrl::name(wxTreeEvent& event) \
1309 { \
1310 LogEvent(wxT(#name), event); \
1311 SetLastItem(wxTreeItemId()); \
1312 event.Skip(); \
1313 }
1314
1315 TREE_EVENT_HANDLER(OnBeginRDrag)
1316 TREE_EVENT_HANDLER(OnDeleteItem)
1317 TREE_EVENT_HANDLER(OnGetInfo)
1318 TREE_EVENT_HANDLER(OnSetInfo)
1319 TREE_EVENT_HANDLER(OnItemExpanded)
1320 TREE_EVENT_HANDLER(OnItemExpanding)
1321 TREE_EVENT_HANDLER(OnItemCollapsed)
1322 TREE_EVENT_HANDLER(OnSelChanged)
1323 TREE_EVENT_HANDLER(OnSelChanging)
1324
1325 #undef TREE_EVENT_HANDLER
1326
1327 void LogKeyEvent(const wxChar *name, const wxKeyEvent& event)
1328 {
1329 wxString key;
1330 long keycode = event.GetKeyCode();
1331 {
1332 switch ( keycode )
1333 {
1334 case WXK_BACK: key = wxT("BACK"); break;
1335 case WXK_TAB: key = wxT("TAB"); break;
1336 case WXK_RETURN: key = wxT("RETURN"); break;
1337 case WXK_ESCAPE: key = wxT("ESCAPE"); break;
1338 case WXK_SPACE: key = wxT("SPACE"); break;
1339 case WXK_DELETE: key = wxT("DELETE"); break;
1340 case WXK_START: key = wxT("START"); break;
1341 case WXK_LBUTTON: key = wxT("LBUTTON"); break;
1342 case WXK_RBUTTON: key = wxT("RBUTTON"); break;
1343 case WXK_CANCEL: key = wxT("CANCEL"); break;
1344 case WXK_MBUTTON: key = wxT("MBUTTON"); break;
1345 case WXK_CLEAR: key = wxT("CLEAR"); break;
1346 case WXK_SHIFT: key = wxT("SHIFT"); break;
1347 case WXK_ALT: key = wxT("ALT"); break;
1348 case WXK_CONTROL: key = wxT("CONTROL"); break;
1349 case WXK_MENU: key = wxT("MENU"); break;
1350 case WXK_PAUSE: key = wxT("PAUSE"); break;
1351 case WXK_CAPITAL: key = wxT("CAPITAL"); break;
1352 case WXK_END: key = wxT("END"); break;
1353 case WXK_HOME: key = wxT("HOME"); break;
1354 case WXK_LEFT: key = wxT("LEFT"); break;
1355 case WXK_UP: key = wxT("UP"); break;
1356 case WXK_RIGHT: key = wxT("RIGHT"); break;
1357 case WXK_DOWN: key = wxT("DOWN"); break;
1358 case WXK_SELECT: key = wxT("SELECT"); break;
1359 case WXK_PRINT: key = wxT("PRINT"); break;
1360 case WXK_EXECUTE: key = wxT("EXECUTE"); break;
1361 case WXK_SNAPSHOT: key = wxT("SNAPSHOT"); break;
1362 case WXK_INSERT: key = wxT("INSERT"); break;
1363 case WXK_HELP: key = wxT("HELP"); break;
1364 case WXK_NUMPAD0: key = wxT("NUMPAD0"); break;
1365 case WXK_NUMPAD1: key = wxT("NUMPAD1"); break;
1366 case WXK_NUMPAD2: key = wxT("NUMPAD2"); break;
1367 case WXK_NUMPAD3: key = wxT("NUMPAD3"); break;
1368 case WXK_NUMPAD4: key = wxT("NUMPAD4"); break;
1369 case WXK_NUMPAD5: key = wxT("NUMPAD5"); break;
1370 case WXK_NUMPAD6: key = wxT("NUMPAD6"); break;
1371 case WXK_NUMPAD7: key = wxT("NUMPAD7"); break;
1372 case WXK_NUMPAD8: key = wxT("NUMPAD8"); break;
1373 case WXK_NUMPAD9: key = wxT("NUMPAD9"); break;
1374 case WXK_MULTIPLY: key = wxT("MULTIPLY"); break;
1375 case WXK_ADD: key = wxT("ADD"); break;
1376 case WXK_SEPARATOR: key = wxT("SEPARATOR"); break;
1377 case WXK_SUBTRACT: key = wxT("SUBTRACT"); break;
1378 case WXK_DECIMAL: key = wxT("DECIMAL"); break;
1379 case WXK_DIVIDE: key = wxT("DIVIDE"); break;
1380 case WXK_F1: key = wxT("F1"); break;
1381 case WXK_F2: key = wxT("F2"); break;
1382 case WXK_F3: key = wxT("F3"); break;
1383 case WXK_F4: key = wxT("F4"); break;
1384 case WXK_F5: key = wxT("F5"); break;
1385 case WXK_F6: key = wxT("F6"); break;
1386 case WXK_F7: key = wxT("F7"); break;
1387 case WXK_F8: key = wxT("F8"); break;
1388 case WXK_F9: key = wxT("F9"); break;
1389 case WXK_F10: key = wxT("F10"); break;
1390 case WXK_F11: key = wxT("F11"); break;
1391 case WXK_F12: key = wxT("F12"); break;
1392 case WXK_F13: key = wxT("F13"); break;
1393 case WXK_F14: key = wxT("F14"); break;
1394 case WXK_F15: key = wxT("F15"); break;
1395 case WXK_F16: key = wxT("F16"); break;
1396 case WXK_F17: key = wxT("F17"); break;
1397 case WXK_F18: key = wxT("F18"); break;
1398 case WXK_F19: key = wxT("F19"); break;
1399 case WXK_F20: key = wxT("F20"); break;
1400 case WXK_F21: key = wxT("F21"); break;
1401 case WXK_F22: key = wxT("F22"); break;
1402 case WXK_F23: key = wxT("F23"); break;
1403 case WXK_F24: key = wxT("F24"); break;
1404 case WXK_NUMLOCK: key = wxT("NUMLOCK"); break;
1405 case WXK_SCROLL: key = wxT("SCROLL"); break;
1406 case WXK_PAGEUP: key = wxT("PAGEUP"); break;
1407 case WXK_PAGEDOWN: key = wxT("PAGEDOWN"); break;
1408 case WXK_NUMPAD_SPACE: key = wxT("NUMPAD_SPACE"); break;
1409 case WXK_NUMPAD_TAB: key = wxT("NUMPAD_TAB"); break;
1410 case WXK_NUMPAD_ENTER: key = wxT("NUMPAD_ENTER"); break;
1411 case WXK_NUMPAD_F1: key = wxT("NUMPAD_F1"); break;
1412 case WXK_NUMPAD_F2: key = wxT("NUMPAD_F2"); break;
1413 case WXK_NUMPAD_F3: key = wxT("NUMPAD_F3"); break;
1414 case WXK_NUMPAD_F4: key = wxT("NUMPAD_F4"); break;
1415 case WXK_NUMPAD_HOME: key = wxT("NUMPAD_HOME"); break;
1416 case WXK_NUMPAD_LEFT: key = wxT("NUMPAD_LEFT"); break;
1417 case WXK_NUMPAD_UP: key = wxT("NUMPAD_UP"); break;
1418 case WXK_NUMPAD_RIGHT: key = wxT("NUMPAD_RIGHT"); break;
1419 case WXK_NUMPAD_DOWN: key = wxT("NUMPAD_DOWN"); break;
1420 case WXK_NUMPAD_PAGEUP: key = wxT("NUMPAD_PAGEUP"); break;
1421 case WXK_NUMPAD_PAGEDOWN: key = wxT("NUMPAD_PAGEDOWN"); break;
1422 case WXK_NUMPAD_END: key = wxT("NUMPAD_END"); break;
1423 case WXK_NUMPAD_BEGIN: key = wxT("NUMPAD_BEGIN"); break;
1424 case WXK_NUMPAD_INSERT: key = wxT("NUMPAD_INSERT"); break;
1425 case WXK_NUMPAD_DELETE: key = wxT("NUMPAD_DELETE"); break;
1426 case WXK_NUMPAD_EQUAL: key = wxT("NUMPAD_EQUAL"); break;
1427 case WXK_NUMPAD_MULTIPLY: key = wxT("NUMPAD_MULTIPLY"); break;
1428 case WXK_NUMPAD_ADD: key = wxT("NUMPAD_ADD"); break;
1429 case WXK_NUMPAD_SEPARATOR: key = wxT("NUMPAD_SEPARATOR"); break;
1430 case WXK_NUMPAD_SUBTRACT: key = wxT("NUMPAD_SUBTRACT"); break;
1431 case WXK_NUMPAD_DECIMAL: key = wxT("NUMPAD_DECIMAL"); break;
1432
1433 default:
1434 {
1435 if ( keycode < 128 && wxIsprint((int)keycode) )
1436 key.Printf(wxT("'%c'"), (char)keycode);
1437 else if ( keycode > 0 && keycode < 27 )
1438 key.Printf(_("Ctrl-%c"), wxT('A') + keycode - 1);
1439 else
1440 key.Printf(wxT("unknown (%ld)"), keycode);
1441 }
1442 }
1443 }
1444
1445 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1446 name,
1447 key.c_str(),
1448 event.ControlDown() ? wxT('C') : wxT('-'),
1449 event.AltDown() ? wxT('A') : wxT('-'),
1450 event.ShiftDown() ? wxT('S') : wxT('-'),
1451 event.MetaDown() ? wxT('M') : wxT('-'));
1452 }
1453
1454 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent& event)
1455 {
1456 LogKeyEvent(wxT("Tree key down "), event.GetKeyEvent());
1457
1458 event.Skip();
1459 }
1460
1461 void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
1462 {
1463 // need to explicitly allow drag
1464 if ( event.GetItem() != GetRootItem() )
1465 {
1466 m_draggedItem = event.GetItem();
1467
1468 wxPoint clientpt = event.GetPoint();
1469 wxPoint screenpt = ClientToScreen(clientpt);
1470
1471 wxLogMessage(wxT("OnBeginDrag: started dragging %s at screen coords (%i,%i)"),
1472 GetItemText(m_draggedItem).c_str(),
1473 screenpt.x, screenpt.y);
1474
1475 event.Allow();
1476 }
1477 else
1478 {
1479 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1480 }
1481 }
1482
1483 void MyTreeCtrl::OnEndDrag(wxTreeEvent& event)
1484 {
1485 wxTreeItemId itemSrc = m_draggedItem,
1486 itemDst = event.GetItem();
1487 m_draggedItem = (wxTreeItemId)0l;
1488
1489 // where to copy the item?
1490 if ( itemDst.IsOk() && !ItemHasChildren(itemDst) )
1491 {
1492 // copy to the parent then
1493 itemDst = GetItemParent(itemDst);
1494 }
1495
1496 if ( !itemDst.IsOk() )
1497 {
1498 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1499
1500 return;
1501 }
1502
1503 wxString text = GetItemText(itemSrc);
1504 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1505 text.c_str(), GetItemText(itemDst).c_str());
1506
1507 // just do append here - we could also insert it just before/after the item
1508 // on which it was dropped, but this requires slightly more work... we also
1509 // completely ignore the client data and icon of the old item but could
1510 // copy them as well.
1511 //
1512 // Finally, we only copy one item here but we might copy the entire tree if
1513 // we were dragging a folder.
1514 int image = wxGetApp().ShowImages() ? TreeCtrlIcon_File : -1;
1515 wxTreeItemId id = AppendItem(itemDst, text, image);
1516
1517 if ( wxGetApp().ShowStates() )
1518 SetItemState(id, GetItemState(itemSrc));
1519 }
1520
1521 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
1522 {
1523 wxLogMessage(wxT("OnBeginLabelEdit"));
1524
1525 // for testing, prevent this item's label editing
1526 wxTreeItemId itemId = event.GetItem();
1527 if ( IsTestItem(itemId) )
1528 {
1529 wxMessageBox(wxT("You can't edit this item."));
1530
1531 event.Veto();
1532 }
1533 else if ( itemId == GetRootItem() )
1534 {
1535 // test that it is possible to change the text of the item being edited
1536 SetItemText(itemId, wxT("Editing root item"));
1537 }
1538 }
1539
1540 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
1541 {
1542 wxLogMessage(wxT("OnEndLabelEdit"));
1543
1544 // don't allow anything except letters in the labels
1545 if ( !event.GetLabel().IsWord() )
1546 {
1547 wxMessageBox(wxT("The new label should be a single word."));
1548
1549 event.Veto();
1550 }
1551 }
1552
1553 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
1554 {
1555 wxLogMessage(wxT("OnItemCollapsing"));
1556
1557 // for testing, prevent the user from collapsing the first child folder
1558 wxTreeItemId itemId = event.GetItem();
1559 if ( IsTestItem(itemId) )
1560 {
1561 wxMessageBox(wxT("You can't collapse this item."));
1562
1563 event.Veto();
1564 }
1565 }
1566
1567 void MyTreeCtrl::OnItemActivated(wxTreeEvent& event)
1568 {
1569 // show some info about this item
1570 wxTreeItemId itemId = event.GetItem();
1571 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
1572
1573 if ( item != NULL )
1574 {
1575 item->ShowInfo(this);
1576 }
1577
1578 wxLogMessage(wxT("OnItemActivated"));
1579 }
1580
1581 void MyTreeCtrl::OnItemStateClick(wxTreeEvent& event)
1582 {
1583 // toggle item state
1584 wxTreeItemId itemId = event.GetItem();
1585 DoToggleState(itemId);
1586
1587 wxLogMessage(wxT("Item \"%s\" state changed to %d"),
1588 GetItemText(itemId), GetItemState(itemId));
1589 }
1590
1591 void MyTreeCtrl::OnItemMenu(wxTreeEvent& event)
1592 {
1593 wxTreeItemId itemId = event.GetItem();
1594 wxCHECK_RET( itemId.IsOk(), "should have a valid item" );
1595
1596 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
1597 wxPoint clientpt = event.GetPoint();
1598 wxPoint screenpt = ClientToScreen(clientpt);
1599
1600 wxLogMessage(wxT("OnItemMenu for item \"%s\" at screen coords (%i, %i)"),
1601 item->GetDesc(), screenpt.x, screenpt.y);
1602
1603 ShowMenu(itemId, clientpt);
1604 event.Skip();
1605 }
1606
1607 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent& event)
1608 {
1609 wxPoint pt = event.GetPosition();
1610
1611 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt.x, pt.y);
1612
1613 event.Skip();
1614 }
1615
1616 void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt)
1617 {
1618 wxString title;
1619 if ( id.IsOk() )
1620 {
1621 title << wxT("Menu for ") << GetItemText(id);
1622 }
1623 else
1624 {
1625 title = wxT("Menu for no particular item");
1626 }
1627
1628 #if wxUSE_MENUS
1629 wxMenu menu(title);
1630 menu.Append(TreeTest_About, wxT("&About"));
1631 menu.AppendSeparator();
1632 menu.Append(TreeTest_Highlight, wxT("&Highlight item"));
1633 menu.Append(TreeTest_Dump, wxT("&Dump"));
1634
1635 PopupMenu(&menu, pt);
1636 #endif // wxUSE_MENUS
1637 }
1638
1639 void MyTreeCtrl::OnItemRClick(wxTreeEvent& event)
1640 {
1641 wxTreeItemId itemId = event.GetItem();
1642 wxCHECK_RET( itemId.IsOk(), "should have a valid item" );
1643
1644 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
1645
1646 wxLogMessage(wxT("Item \"%s\" right clicked"), item->GetDesc());
1647
1648 event.Skip();
1649 }
1650
1651 void MyTreeCtrl::OnRMouseDown(wxMouseEvent& event)
1652 {
1653 wxLogMessage(wxT("Right mouse button down"));
1654
1655 event.Skip();
1656 }
1657
1658 void MyTreeCtrl::OnRMouseUp(wxMouseEvent& event)
1659 {
1660 wxLogMessage(wxT("Right mouse button up"));
1661
1662 event.Skip();
1663 }
1664
1665 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
1666 {
1667 wxTreeItemId id = HitTest(event.GetPosition());
1668 if ( !id )
1669 {
1670 wxLogMessage(wxT("No item under mouse"));
1671 }
1672 else
1673 {
1674 MyTreeItemData *item = (MyTreeItemData *)GetItemData(id);
1675 if ( item )
1676 {
1677 wxLogMessage(wxT("Item '%s' under mouse"), item->GetDesc());
1678 }
1679 }
1680
1681 event.Skip();
1682 }
1683
1684 static inline const wxChar *Bool2String(bool b)
1685 {
1686 return b ? wxT("") : wxT("not ");
1687 }
1688
1689 void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
1690 {
1691 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1692 wxT("%u children (%u immediately under this item)."),
1693 m_desc.c_str(),
1694 Bool2String(tree->IsSelected(GetId())),
1695 Bool2String(tree->IsExpanded(GetId())),
1696 Bool2String(tree->IsBold(GetId())),
1697 unsigned(tree->GetChildrenCount(GetId())),
1698 unsigned(tree->GetChildrenCount(GetId(), false)));
1699 }