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