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