]> git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treetest.cpp
added MacOpenURL() (patch 1235957)
[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
334 GetMenuBar()->Enable(TreeTest_SelectRoot, !(style & wxTR_HIDE_ROOT));
335
336 Resize();
337 }
338
339 void MyFrame::TogStyle(int id, long flag)
340 {
341 long style = m_treeCtrl->GetWindowStyle() ^ flag;
342
343 // most treectrl styles can't be changed on the fly using the native
344 // control and the tree must be recreated
345 #ifndef __WXMSW__
346 m_treeCtrl->SetWindowStyle(style);
347 #else // MSW
348 delete m_treeCtrl;
349 CreateTree(style);
350 #endif // !MSW/MSW
351
352 GetMenuBar()->Check(id, (style & flag) != 0);
353 }
354
355 void MyFrame::OnIdle(wxIdleEvent& event)
356 {
357 #if wxUSE_STATUSBAR
358 if ( m_treeCtrl )
359 {
360 wxTreeItemId idRoot = m_treeCtrl->GetRootItem();
361 wxString status;
362 if (idRoot.IsOk())
363 {
364 wxTreeItemId idLast = m_treeCtrl->GetLastChild(idRoot);
365 status = wxString::Format(
366 _T("Root/last item is %svisible/%svisible"),
367 m_treeCtrl->IsVisible(idRoot) ? _T("") : _T("not "),
368 idLast.IsOk() && m_treeCtrl->IsVisible(idLast)
369 ? _T("") : _T("not "));
370 }
371 else
372 status = _T("No root item");
373
374 SetStatusText(status, 1);
375 }
376 #endif // wxUSE_STATUSBAR
377
378 event.Skip();
379 }
380
381 void MyFrame::OnSize(wxSizeEvent& event)
382 {
383 if ( m_treeCtrl
384 #if wxUSE_LOG
385 && m_textCtrl
386 #endif // wxUSE_LOG
387 )
388 {
389 Resize();
390 }
391
392 event.Skip();
393 }
394
395 void MyFrame::Resize()
396 {
397 wxSize size = GetClientSize();
398 m_treeCtrl->SetSize(0, 0, size.x, size.y
399 #if !wxUSE_LOG
400 );
401 #else
402 *2/3);
403 m_textCtrl->SetSize(0, 2*size.y/3, size.x, size.y/3);
404 #endif
405 }
406
407 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
408 {
409 Close(true);
410 }
411
412 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
413 {
414 wxMessageBox(wxT("Tree test sample\n")
415 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
416 wxT("About tree test"),
417 wxOK | wxICON_INFORMATION, this);
418 }
419
420 void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
421 {
422 wxTreeItemId item = m_treeCtrl->GetSelection();
423
424 CHECK_ITEM( item );
425
426 // old code - now we edit in place
427 #if 0
428 static wxString s_text;
429 s_text = wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
430 s_text, this);
431 if ( !s_text.empty() )
432 {
433 m_treeCtrl->SetItemText(item, s_text);
434 }
435 #endif // 0
436
437 // TODO demonstrate creating a custom edit control...
438 (void)m_treeCtrl->EditLabel(item);
439 }
440
441 void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event))
442 {
443 wxTreeItemId item = m_treeCtrl->GetSelection();
444
445 CHECK_ITEM( item );
446
447 int i = m_treeCtrl->GetChildrenCount( item, false );
448
449 wxLogMessage(wxT("%d children"), i);
450 }
451
452 void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event))
453 {
454 wxTreeItemId item = m_treeCtrl->GetSelection();
455
456 CHECK_ITEM( item );
457
458 int i = m_treeCtrl->GetChildrenCount( item );
459
460 wxLogMessage(wxT("%d children"), i);
461 }
462
463 void MyFrame::DoSort(bool reverse)
464 {
465 wxTreeItemId item = m_treeCtrl->GetSelection();
466
467 CHECK_ITEM( item );
468
469 m_treeCtrl->DoSortChildren(item, reverse);
470 }
471
472 void MyFrame::OnHighlight(wxCommandEvent& WXUNUSED(event))
473 {
474 wxTreeItemId id = m_treeCtrl->GetSelection();
475
476 CHECK_ITEM( id );
477
478 wxRect r;
479 if ( !m_treeCtrl->GetBoundingRect(id, r, true /* text, not full row */) )
480 {
481 wxLogMessage(_T("Failed to get bounding item rect"));
482 return;
483 }
484
485 wxClientDC dc(m_treeCtrl);
486 dc.SetBrush(*wxRED);
487 dc.SetPen(*wxTRANSPARENT_PEN);
488 dc.DrawRectangle(r);
489 m_treeCtrl->Update();
490 }
491
492 void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
493 {
494 wxTreeItemId root = m_treeCtrl->GetSelection();
495
496 CHECK_ITEM( root );
497
498 m_treeCtrl->GetItemsRecursively(root);
499 }
500
501 #ifndef NO_MULTIPLE_SELECTION
502
503 void MyFrame::OnToggleSel(wxCommandEvent& event)
504 {
505 TogStyle(event.GetId(), wxTR_MULTIPLE);
506 }
507
508 void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event))
509 {
510 wxArrayTreeItemIds array;
511
512 size_t count = m_treeCtrl->GetSelections(array);
513 wxLogMessage(wxT("%u items selected"), unsigned(count));
514
515 for ( size_t n = 0; n < count; n++ )
516 {
517 wxLogMessage(wxT("\t%s"), m_treeCtrl->GetItemText(array.Item(n)).c_str());
518 }
519 }
520
521 void MyFrame::OnSelect(wxCommandEvent& WXUNUSED(event))
522 {
523 m_treeCtrl->SelectItem(m_treeCtrl->GetSelection());
524 }
525
526 void MyFrame::OnSelectRoot(wxCommandEvent& WXUNUSED(event))
527 {
528 if ( !m_treeCtrl->HasFlag(wxTR_HIDE_ROOT) )
529 m_treeCtrl->SelectItem(m_treeCtrl->GetRootItem());
530 }
531
532 void MyFrame::OnUnselect(wxCommandEvent& WXUNUSED(event))
533 {
534 m_treeCtrl->UnselectAll();
535 }
536
537 #endif // NO_MULTIPLE_SELECTION
538
539 void MyFrame::DoSetBold(bool bold)
540 {
541 wxTreeItemId item = m_treeCtrl->GetSelection();
542
543 CHECK_ITEM( item );
544
545 m_treeCtrl->SetItemBold(item, bold);
546 }
547
548 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
549 {
550 wxTreeItemId item = m_treeCtrl->GetSelection();
551
552 CHECK_ITEM( item );
553
554 m_treeCtrl->Delete(item);
555 }
556
557 void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event))
558 {
559 wxTreeItemId item = m_treeCtrl->GetSelection();
560
561 CHECK_ITEM( item );
562
563 m_treeCtrl->DeleteChildren(item);
564 }
565
566 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
567 {
568 m_treeCtrl->DeleteAllItems();
569 }
570
571 void MyFrame::OnRecreate(wxCommandEvent& event)
572 {
573 OnDeleteAll(event);
574 m_treeCtrl->AddTestItemsToTree(5, 2);
575 }
576
577 void MyFrame::OnSetImageSize(wxCommandEvent& WXUNUSED(event))
578 {
579 int size = wxGetNumberFromUser(wxT("Enter the size for the images to use"),
580 wxT("Size: "),
581 wxT("TreeCtrl sample"),
582 m_treeCtrl->ImageSize());
583 if ( size == -1 )
584 return;
585
586 m_treeCtrl->CreateImageList(size);
587 wxGetApp().SetShowImages(true);
588 }
589
590 void MyFrame::OnToggleImages(wxCommandEvent& WXUNUSED(event))
591 {
592 if ( wxGetApp().ShowImages() )
593 {
594 m_treeCtrl->CreateImageList(-1);
595 wxGetApp().SetShowImages(false);
596 }
597 else
598 {
599 m_treeCtrl->CreateImageList(0);
600 wxGetApp().SetShowImages(true);
601 }
602 }
603
604 void MyFrame::OnToggleButtons(wxCommandEvent& WXUNUSED(event))
605 {
606 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
607 if ( wxGetApp().ShowButtons() )
608 {
609 m_treeCtrl->CreateButtonsImageList(-1);
610 wxGetApp().SetShowButtons(false);
611 }
612 else
613 {
614 m_treeCtrl->CreateButtonsImageList(15);
615 wxGetApp().SetShowButtons(true);
616 }
617 #endif
618 }
619
620 void MyFrame::OnCollapseAndReset(wxCommandEvent& WXUNUSED(event))
621 {
622 m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem());
623 }
624
625 void MyFrame::OnEnsureVisible(wxCommandEvent& WXUNUSED(event))
626 {
627 m_treeCtrl->DoEnsureVisible();
628 }
629
630 void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event))
631 {
632 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File : -1;
633 m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), image, wxT("2nd item"));
634 }
635
636 void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event))
637 {
638 static int s_num = 0;
639
640 wxString text;
641 text.Printf(wxT("Item #%d"), ++s_num);
642
643 m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(),
644 text /*,
645 MyTreeCtrl::TreeCtrlIcon_File */ );
646 }
647
648 void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event))
649 {
650 unsigned int indent = m_treeCtrl->GetIndent();
651 if (indent < 100)
652 m_treeCtrl->SetIndent( indent+5 );
653 }
654
655 void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event))
656 {
657 unsigned int indent = m_treeCtrl->GetIndent();
658 if (indent > 10)
659 m_treeCtrl->SetIndent( indent-5 );
660 }
661
662 void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event))
663 {
664 unsigned int indent = m_treeCtrl->GetSpacing();
665 if (indent < 100)
666 m_treeCtrl->SetSpacing( indent+5 );
667 }
668
669 void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event))
670 {
671 unsigned int indent = m_treeCtrl->GetSpacing();
672 if (indent > 10)
673 m_treeCtrl->SetSpacing( indent-5 );
674 }
675
676 void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event))
677 {
678 wxTreeItemId item = m_treeCtrl->GetSelection();
679
680 CHECK_ITEM( item );
681
682 m_treeCtrl->DoToggleIcon(item);
683 }
684
685 void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
686 {
687 wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetForegroundColour());
688 if ( col.Ok() )
689 m_treeCtrl->SetForegroundColour(col);
690 }
691
692 void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
693 {
694 wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetBackgroundColour());
695 if ( col.Ok() )
696 m_treeCtrl->SetBackgroundColour(col);
697 }
698
699 // MyTreeCtrl implementation
700 #if USE_GENERIC_TREECTRL
701 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxGenericTreeCtrl)
702 #else
703 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
704 #endif
705
706 MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
707 const wxPoint& pos, const wxSize& size,
708 long style)
709 : wxTreeCtrl(parent, id, pos, size, style)
710 {
711 m_reverseSort = false;
712
713 CreateImageList();
714
715 // Add some items to the tree
716 AddTestItemsToTree(5, 2);
717 }
718
719 void MyTreeCtrl::CreateImageList(int size)
720 {
721 if ( size == -1 )
722 {
723 SetImageList(NULL);
724 return;
725 }
726 if ( size == 0 )
727 size = m_imageSize;
728 else
729 m_imageSize = size;
730
731 // Make an image list containing small icons
732 wxImageList *images = new wxImageList(size, size, true);
733
734 // should correspond to TreeCtrlIcon_xxx enum
735 wxBusyCursor wait;
736 wxIcon icons[5];
737 icons[0] = wxIcon(icon1_xpm);
738 icons[1] = wxIcon(icon2_xpm);
739 icons[2] = wxIcon(icon3_xpm);
740 icons[3] = wxIcon(icon4_xpm);
741 icons[4] = wxIcon(icon5_xpm);
742
743 int sizeOrig = icons[0].GetWidth();
744 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
745 {
746 if ( size == sizeOrig )
747 {
748 images->Add(icons[i]);
749 }
750 else
751 {
752 images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
753 }
754 }
755
756 AssignImageList(images);
757 }
758
759 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
760 void MyTreeCtrl::CreateButtonsImageList(int size)
761 {
762 if ( size == -1 )
763 {
764 SetButtonsImageList(NULL);
765 return;
766 }
767
768 // Make an image list containing small icons
769 wxImageList *images = new wxImageList(size, size, true);
770
771 // should correspond to TreeCtrlIcon_xxx enum
772 wxBusyCursor wait;
773 wxIcon icons[4];
774 icons[0] = wxIcon(icon3_xpm); // closed
775 icons[1] = wxIcon(icon3_xpm); // closed, selected
776 icons[2] = wxIcon(icon5_xpm); // open
777 icons[3] = wxIcon(icon5_xpm); // open, selected
778
779 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
780 {
781 int sizeOrig = icons[i].GetWidth();
782 if ( size == sizeOrig )
783 {
784 images->Add(icons[i]);
785 }
786 else
787 {
788 images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
789 }
790 }
791
792 AssignButtonsImageList(images);
793 #else
794 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size))
795 {
796 #endif
797 }
798
799 int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
800 const wxTreeItemId& item2)
801 {
802 if ( m_reverseSort )
803 {
804 // just exchange 1st and 2nd items
805 return wxTreeCtrl::OnCompareItems(item2, item1);
806 }
807 else
808 {
809 return wxTreeCtrl::OnCompareItems(item1, item2);
810 }
811 }
812
813 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
814 size_t numChildren,
815 size_t depth,
816 size_t folder)
817 {
818 if ( depth > 0 )
819 {
820 bool hasChildren = depth > 1;
821
822 wxString str;
823 for ( size_t n = 0; n < numChildren; n++ )
824 {
825 // at depth 1 elements won't have any more children
826 if ( hasChildren )
827 str.Printf(wxT("%s child %u"), wxT("Folder"), unsigned(n + 1));
828 else
829 str.Printf(wxT("%s child %u.%u"), wxT("File"), unsigned(folder), unsigned(n + 1));
830
831 // here we pass to AppendItem() normal and selected item images (we
832 // suppose that selected image follows the normal one in the enum)
833 int image, imageSel;
834 if ( wxGetApp().ShowImages() )
835 {
836 image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
837 imageSel = image + 1;
838 }
839 else
840 {
841 image = imageSel = -1;
842 }
843 wxTreeItemId id = AppendItem(idParent, str, image, imageSel,
844 new MyTreeItemData(str));
845
846 // and now we also set the expanded one (only for the folders)
847 if ( hasChildren && wxGetApp().ShowImages() )
848 {
849 SetItemImage(id, TreeCtrlIcon_FolderOpened,
850 wxTreeItemIcon_Expanded);
851 }
852
853 // remember the last child for OnEnsureVisible()
854 if ( !hasChildren && n == numChildren - 1 )
855 {
856 m_lastItem = id;
857 }
858
859 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
860 }
861 }
862 //else: done!
863 }
864
865 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
866 size_t depth)
867 {
868 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder : -1;
869 wxTreeItemId rootId = AddRoot(wxT("Root"),
870 image, image,
871 new MyTreeItemData(wxT("Root item")));
872 if ( !HasFlag(wxTR_HIDE_ROOT) && image != -1 )
873 {
874 SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded);
875 }
876
877 AddItemsRecursively(rootId, numChildren, depth, 0);
878
879 // set some colours/fonts for testing
880 if ( !HasFlag(wxTR_HIDE_ROOT) )
881 SetItemFont(rootId, *wxITALIC_FONT);
882
883 wxTreeItemIdValue cookie;
884 wxTreeItemId id = GetFirstChild(rootId, cookie);
885 SetItemTextColour(id, *wxBLUE);
886
887 id = GetNextChild(rootId, cookie);
888 id = GetNextChild(rootId, cookie);
889 SetItemTextColour(id, *wxRED);
890 SetItemBackgroundColour(id, *wxLIGHT_GREY);
891 }
892
893 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent,
894 wxTreeItemIdValue cookie)
895 {
896 wxTreeItemId id;
897
898 if ( !cookie )
899 id = GetFirstChild(idParent, cookie);
900 else
901 id = GetNextChild(idParent, cookie);
902
903 if ( !id.IsOk() )
904 return;
905
906 wxString text = GetItemText(id);
907 wxLogMessage(text);
908
909 if (ItemHasChildren(id))
910 GetItemsRecursively(id);
911
912 GetItemsRecursively(idParent, cookie);
913 }
914
915 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item)
916 {
917 int image = (GetItemImage(item) == TreeCtrlIcon_Folder)
918 ? TreeCtrlIcon_File
919 : TreeCtrlIcon_Folder;
920 SetItemImage(item, image, wxTreeItemIcon_Normal);
921
922 image = (GetItemImage(item) == TreeCtrlIcon_FolderSelected)
923 ? TreeCtrlIcon_FileSelected
924 : TreeCtrlIcon_FolderSelected;
925 SetItemImage(item, image, wxTreeItemIcon_Selected);
926 }
927
928 void MyTreeCtrl::LogEvent(const wxChar *name, const wxTreeEvent& event)
929 {
930 wxTreeItemId item = event.GetItem();
931 wxString text;
932 if ( item.IsOk() )
933 text << _T('"') << GetItemText(item).c_str() << _T('"');
934 else
935 text = _T("invalid item");
936 wxLogMessage(wxT("%s(%s)"), name, text.c_str());
937 }
938
939 // avoid repetition
940 #define TREE_EVENT_HANDLER(name) \
941 void MyTreeCtrl::name(wxTreeEvent& event) \
942 { \
943 LogEvent(_T(#name), event); \
944 SetLastItem(wxTreeItemId()); \
945 event.Skip(); \
946 }
947
948 TREE_EVENT_HANDLER(OnBeginRDrag)
949 TREE_EVENT_HANDLER(OnDeleteItem)
950 TREE_EVENT_HANDLER(OnGetInfo)
951 TREE_EVENT_HANDLER(OnSetInfo)
952 TREE_EVENT_HANDLER(OnItemExpanded)
953 TREE_EVENT_HANDLER(OnItemExpanding)
954 TREE_EVENT_HANDLER(OnItemCollapsed)
955 TREE_EVENT_HANDLER(OnSelChanged)
956 TREE_EVENT_HANDLER(OnSelChanging)
957
958 #undef TREE_EVENT_HANDLER
959
960 void LogKeyEvent(const wxChar *name, const wxKeyEvent& event)
961 {
962 wxString key;
963 long keycode = event.GetKeyCode();
964 {
965 switch ( keycode )
966 {
967 case WXK_BACK: key = wxT("BACK"); break;
968 case WXK_TAB: key = wxT("TAB"); break;
969 case WXK_RETURN: key = wxT("RETURN"); break;
970 case WXK_ESCAPE: key = wxT("ESCAPE"); break;
971 case WXK_SPACE: key = wxT("SPACE"); break;
972 case WXK_DELETE: key = wxT("DELETE"); break;
973 case WXK_START: key = wxT("START"); break;
974 case WXK_LBUTTON: key = wxT("LBUTTON"); break;
975 case WXK_RBUTTON: key = wxT("RBUTTON"); break;
976 case WXK_CANCEL: key = wxT("CANCEL"); break;
977 case WXK_MBUTTON: key = wxT("MBUTTON"); break;
978 case WXK_CLEAR: key = wxT("CLEAR"); break;
979 case WXK_SHIFT: key = wxT("SHIFT"); break;
980 case WXK_ALT: key = wxT("ALT"); break;
981 case WXK_CONTROL: key = wxT("CONTROL"); break;
982 case WXK_MENU: key = wxT("MENU"); break;
983 case WXK_PAUSE: key = wxT("PAUSE"); break;
984 case WXK_CAPITAL: key = wxT("CAPITAL"); break;
985 case WXK_END: key = wxT("END"); break;
986 case WXK_HOME: key = wxT("HOME"); break;
987 case WXK_LEFT: key = wxT("LEFT"); break;
988 case WXK_UP: key = wxT("UP"); break;
989 case WXK_RIGHT: key = wxT("RIGHT"); break;
990 case WXK_DOWN: key = wxT("DOWN"); break;
991 case WXK_SELECT: key = wxT("SELECT"); break;
992 case WXK_PRINT: key = wxT("PRINT"); break;
993 case WXK_EXECUTE: key = wxT("EXECUTE"); break;
994 case WXK_SNAPSHOT: key = wxT("SNAPSHOT"); break;
995 case WXK_INSERT: key = wxT("INSERT"); break;
996 case WXK_HELP: key = wxT("HELP"); break;
997 case WXK_NUMPAD0: key = wxT("NUMPAD0"); break;
998 case WXK_NUMPAD1: key = wxT("NUMPAD1"); break;
999 case WXK_NUMPAD2: key = wxT("NUMPAD2"); break;
1000 case WXK_NUMPAD3: key = wxT("NUMPAD3"); break;
1001 case WXK_NUMPAD4: key = wxT("NUMPAD4"); break;
1002 case WXK_NUMPAD5: key = wxT("NUMPAD5"); break;
1003 case WXK_NUMPAD6: key = wxT("NUMPAD6"); break;
1004 case WXK_NUMPAD7: key = wxT("NUMPAD7"); break;
1005 case WXK_NUMPAD8: key = wxT("NUMPAD8"); break;
1006 case WXK_NUMPAD9: key = wxT("NUMPAD9"); break;
1007 case WXK_MULTIPLY: key = wxT("MULTIPLY"); break;
1008 case WXK_ADD: key = wxT("ADD"); break;
1009 case WXK_SEPARATOR: key = wxT("SEPARATOR"); break;
1010 case WXK_SUBTRACT: key = wxT("SUBTRACT"); break;
1011 case WXK_DECIMAL: key = wxT("DECIMAL"); break;
1012 case WXK_DIVIDE: key = wxT("DIVIDE"); break;
1013 case WXK_F1: key = wxT("F1"); break;
1014 case WXK_F2: key = wxT("F2"); break;
1015 case WXK_F3: key = wxT("F3"); break;
1016 case WXK_F4: key = wxT("F4"); break;
1017 case WXK_F5: key = wxT("F5"); break;
1018 case WXK_F6: key = wxT("F6"); break;
1019 case WXK_F7: key = wxT("F7"); break;
1020 case WXK_F8: key = wxT("F8"); break;
1021 case WXK_F9: key = wxT("F9"); break;
1022 case WXK_F10: key = wxT("F10"); break;
1023 case WXK_F11: key = wxT("F11"); break;
1024 case WXK_F12: key = wxT("F12"); break;
1025 case WXK_F13: key = wxT("F13"); break;
1026 case WXK_F14: key = wxT("F14"); break;
1027 case WXK_F15: key = wxT("F15"); break;
1028 case WXK_F16: key = wxT("F16"); break;
1029 case WXK_F17: key = wxT("F17"); break;
1030 case WXK_F18: key = wxT("F18"); break;
1031 case WXK_F19: key = wxT("F19"); break;
1032 case WXK_F20: key = wxT("F20"); break;
1033 case WXK_F21: key = wxT("F21"); break;
1034 case WXK_F22: key = wxT("F22"); break;
1035 case WXK_F23: key = wxT("F23"); break;
1036 case WXK_F24: key = wxT("F24"); break;
1037 case WXK_NUMLOCK: key = wxT("NUMLOCK"); break;
1038 case WXK_SCROLL: key = wxT("SCROLL"); break;
1039 case WXK_PAGEUP: key = wxT("PAGEUP"); break;
1040 case WXK_PAGEDOWN: key = wxT("PAGEDOWN"); break;
1041 case WXK_NUMPAD_SPACE: key = wxT("NUMPAD_SPACE"); break;
1042 case WXK_NUMPAD_TAB: key = wxT("NUMPAD_TAB"); break;
1043 case WXK_NUMPAD_ENTER: key = wxT("NUMPAD_ENTER"); break;
1044 case WXK_NUMPAD_F1: key = wxT("NUMPAD_F1"); break;
1045 case WXK_NUMPAD_F2: key = wxT("NUMPAD_F2"); break;
1046 case WXK_NUMPAD_F3: key = wxT("NUMPAD_F3"); break;
1047 case WXK_NUMPAD_F4: key = wxT("NUMPAD_F4"); break;
1048 case WXK_NUMPAD_HOME: key = wxT("NUMPAD_HOME"); break;
1049 case WXK_NUMPAD_LEFT: key = wxT("NUMPAD_LEFT"); break;
1050 case WXK_NUMPAD_UP: key = wxT("NUMPAD_UP"); break;
1051 case WXK_NUMPAD_RIGHT: key = wxT("NUMPAD_RIGHT"); break;
1052 case WXK_NUMPAD_DOWN: key = wxT("NUMPAD_DOWN"); break;
1053 case WXK_NUMPAD_PAGEUP: key = wxT("NUMPAD_PAGEUP"); break;
1054 case WXK_NUMPAD_PAGEDOWN: key = wxT("NUMPAD_PAGEDOWN"); break;
1055 case WXK_NUMPAD_END: key = wxT("NUMPAD_END"); break;
1056 case WXK_NUMPAD_BEGIN: key = wxT("NUMPAD_BEGIN"); break;
1057 case WXK_NUMPAD_INSERT: key = wxT("NUMPAD_INSERT"); break;
1058 case WXK_NUMPAD_DELETE: key = wxT("NUMPAD_DELETE"); break;
1059 case WXK_NUMPAD_EQUAL: key = wxT("NUMPAD_EQUAL"); break;
1060 case WXK_NUMPAD_MULTIPLY: key = wxT("NUMPAD_MULTIPLY"); break;
1061 case WXK_NUMPAD_ADD: key = wxT("NUMPAD_ADD"); break;
1062 case WXK_NUMPAD_SEPARATOR: key = wxT("NUMPAD_SEPARATOR"); break;
1063 case WXK_NUMPAD_SUBTRACT: key = wxT("NUMPAD_SUBTRACT"); break;
1064 case WXK_NUMPAD_DECIMAL: key = wxT("NUMPAD_DECIMAL"); break;
1065
1066 default:
1067 {
1068 if ( keycode < 128 && wxIsprint((int)keycode) )
1069 key.Printf(wxT("'%c'"), (char)keycode);
1070 else if ( keycode > 0 && keycode < 27 )
1071 key.Printf(_("Ctrl-%c"), wxT('A') + keycode - 1);
1072 else
1073 key.Printf(wxT("unknown (%ld)"), keycode);
1074 }
1075 }
1076 }
1077
1078 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1079 name,
1080 key.c_str(),
1081 event.ControlDown() ? wxT('C') : wxT('-'),
1082 event.AltDown() ? wxT('A') : wxT('-'),
1083 event.ShiftDown() ? wxT('S') : wxT('-'),
1084 event.MetaDown() ? wxT('M') : wxT('-'));
1085 }
1086
1087 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent& event)
1088 {
1089 LogKeyEvent(wxT("Tree key down "), event.GetKeyEvent());
1090
1091 event.Skip();
1092 }
1093
1094 void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
1095 {
1096 // need to explicitly allow drag
1097 if ( event.GetItem() != GetRootItem() )
1098 {
1099 m_draggedItem = event.GetItem();
1100
1101 wxPoint clientpt = event.GetPoint();
1102 wxPoint screenpt = ClientToScreen(clientpt);
1103
1104 wxLogMessage(wxT("OnBeginDrag: started dragging %s at screen coords (%i,%i)"),
1105 GetItemText(m_draggedItem).c_str(),
1106 screenpt.x, screenpt.y);
1107
1108 event.Allow();
1109 }
1110 else
1111 {
1112 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1113 }
1114 }
1115
1116 void MyTreeCtrl::OnEndDrag(wxTreeEvent& event)
1117 {
1118 wxTreeItemId itemSrc = m_draggedItem,
1119 itemDst = event.GetItem();
1120 m_draggedItem = (wxTreeItemId)0l;
1121
1122 // where to copy the item?
1123 if ( itemDst.IsOk() && !ItemHasChildren(itemDst) )
1124 {
1125 // copy to the parent then
1126 itemDst = GetItemParent(itemDst);
1127 }
1128
1129 if ( !itemDst.IsOk() )
1130 {
1131 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1132
1133 return;
1134 }
1135
1136 wxString text = GetItemText(itemSrc);
1137 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1138 text.c_str(), GetItemText(itemDst).c_str());
1139
1140 // just do append here - we could also insert it just before/after the item
1141 // on which it was dropped, but this requires slightly more work... we also
1142 // completely ignore the client data and icon of the old item but could
1143 // copy them as well.
1144 //
1145 // Finally, we only copy one item here but we might copy the entire tree if
1146 // we were dragging a folder.
1147 int image = wxGetApp().ShowImages() ? TreeCtrlIcon_File : -1;
1148 AppendItem(itemDst, text, image);
1149 }
1150
1151 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
1152 {
1153 wxLogMessage(wxT("OnBeginLabelEdit"));
1154
1155 // for testing, prevent this item's label editing
1156 wxTreeItemId itemId = event.GetItem();
1157 if ( IsTestItem(itemId) )
1158 {
1159 wxMessageBox(wxT("You can't edit this item."));
1160
1161 event.Veto();
1162 }
1163 else if ( itemId == GetRootItem() )
1164 {
1165 // test that it is possible to change the text of the item being edited
1166 SetItemText(itemId, _T("Editing root item"));
1167 }
1168 }
1169
1170 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
1171 {
1172 wxLogMessage(wxT("OnEndLabelEdit"));
1173
1174 // don't allow anything except letters in the labels
1175 if ( !event.GetLabel().IsWord() )
1176 {
1177 wxMessageBox(wxT("The new label should be a single word."));
1178
1179 event.Veto();
1180 }
1181 }
1182
1183 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
1184 {
1185 wxLogMessage(wxT("OnItemCollapsing"));
1186
1187 // for testing, prevent the user from collapsing the first child folder
1188 wxTreeItemId itemId = event.GetItem();
1189 if ( IsTestItem(itemId) )
1190 {
1191 wxMessageBox(wxT("You can't collapse this item."));
1192
1193 event.Veto();
1194 }
1195 }
1196
1197 void MyTreeCtrl::OnItemActivated(wxTreeEvent& event)
1198 {
1199 // show some info about this item
1200 wxTreeItemId itemId = event.GetItem();
1201 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
1202
1203 if ( item != NULL )
1204 {
1205 item->ShowInfo(this);
1206 }
1207
1208 wxLogMessage(wxT("OnItemActivated"));
1209 }
1210
1211 void MyTreeCtrl::OnItemMenu(wxTreeEvent& event)
1212 {
1213 wxTreeItemId itemId = event.GetItem();
1214 MyTreeItemData *item = itemId.IsOk() ? (MyTreeItemData *)GetItemData(itemId)
1215 : NULL;
1216 wxPoint clientpt = event.GetPoint();
1217 wxPoint screenpt = ClientToScreen(clientpt);
1218
1219 wxLogMessage(wxT("OnItemMenu for item \"%s\" at screen coords (%i, %i)"),
1220 item ? item->GetDesc() : _T(""), screenpt.x, screenpt.y);
1221
1222 ShowMenu(itemId, clientpt);
1223 event.Skip();
1224 }
1225
1226 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent& event)
1227 {
1228 wxPoint pt = event.GetPosition();
1229
1230 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt.x, pt.y);
1231 }
1232
1233 void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt)
1234 {
1235 wxString title;
1236 if ( id.IsOk() )
1237 {
1238 title << wxT("Menu for ") << GetItemText(id);
1239 }
1240 else
1241 {
1242 title = wxT("Menu for no particular item");
1243 }
1244
1245 #if wxUSE_MENUS
1246 wxMenu menu(title);
1247 menu.Append(TreeTest_About, wxT("&About..."));
1248 menu.AppendSeparator();
1249 menu.Append(TreeTest_Highlight, wxT("&Highlight item"));
1250 menu.Append(TreeTest_Dump, wxT("&Dump"));
1251
1252 PopupMenu(&menu, pt);
1253 #endif // wxUSE_MENUS
1254 }
1255
1256 void MyTreeCtrl::OnItemRClick(wxTreeEvent& event)
1257 {
1258 wxTreeItemId itemId = event.GetItem();
1259 MyTreeItemData *item = itemId.IsOk() ? (MyTreeItemData *)GetItemData(itemId)
1260 : NULL;
1261
1262 wxLogMessage(wxT("Item \"%s\" right clicked"), item ? item->GetDesc()
1263 : _T(""));
1264
1265 event.Skip();
1266 }
1267
1268 void MyTreeCtrl::OnRMouseDown(wxMouseEvent& event)
1269 {
1270 wxLogMessage(wxT("Right mouse button down"));
1271
1272 event.Skip();
1273 }
1274
1275 void MyTreeCtrl::OnRMouseUp(wxMouseEvent& event)
1276 {
1277 wxLogMessage(wxT("Right mouse button up"));
1278
1279 event.Skip();
1280 }
1281
1282 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
1283 {
1284 wxTreeItemId id = HitTest(event.GetPosition());
1285 if ( !id )
1286 wxLogMessage(wxT("No item under mouse"));
1287 else
1288 {
1289 MyTreeItemData *item = (MyTreeItemData *)GetItemData(id);
1290 if ( item )
1291 wxLogMessage(wxT("Item '%s' under mouse"), item->GetDesc());
1292 }
1293
1294 event.Skip();
1295 }
1296
1297 static inline const wxChar *Bool2String(bool b)
1298 {
1299 return b ? wxT("") : wxT("not ");
1300 }
1301
1302 void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
1303 {
1304 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1305 wxT("%u children (%u immediately under this item)."),
1306 m_desc.c_str(),
1307 Bool2String(tree->IsSelected(GetId())),
1308 Bool2String(tree->IsExpanded(GetId())),
1309 Bool2String(tree->IsBold(GetId())),
1310 unsigned(tree->GetChildrenCount(GetId())),
1311 unsigned(tree->GetChildrenCount(GetId(), false)));
1312 }