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