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