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