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