1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/colordlg.h"
25 #include "wx/numdlg.h"
27 #include "wx/artprov.h"
29 #include "wx/imaglist.h"
30 #include "wx/treectrl.h"
34 // this is not supported by native control
35 #define NO_VARIABLE_HEIGHT
47 #include "../sample.xpm"
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, \
59 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
61 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
62 EVT_IDLE(MyFrame::OnIdle
)
63 EVT_SIZE(MyFrame::OnSize
)
73 MENU_LINK(TogHideRoot
)
74 MENU_LINK(TogRootLines
)
76 MENU_LINK(TogFullHighlight
)
77 MENU_LINK(SetFgColour
)
78 MENU_LINK(SetBgColour
)
83 #ifndef NO_MULTIPLE_SELECTION
84 MENU_LINK(DumpSelected
)
88 #endif // NO_MULTIPLE_SELECTION
97 MENU_LINK(DeleteChildren
)
100 MENU_LINK(ToggleImages
)
101 MENU_LINK(ToggleAlternateImages
)
102 MENU_LINK(ToggleButtons
)
103 MENU_LINK(SetImageSize
)
104 MENU_LINK(CollapseAndReset
)
105 MENU_LINK(EnsureVisible
)
107 MENU_LINK(InsertItem
)
110 MENU_LINK(IncSpacing
)
111 MENU_LINK(DecSpacing
)
112 MENU_LINK(ToggleIcon
)
113 MENU_LINK(SelectRoot
)
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
)
125 #if USE_GENERIC_TREECTRL
126 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
128 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
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
)
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
)
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
)
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
)
158 EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown
)
159 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
160 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
167 if ( !wxApp::OnInit() )
170 // Create the main frame window
171 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
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
)),
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));
195 SetIcon(wxICON(sample
));
199 wxMenu
*file_menu
= new wxMenu
,
200 *style_menu
= new wxMenu
,
201 *tree_menu
= new wxMenu
,
202 *item_menu
= new wxMenu
;
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"));
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"));
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"));
256 item_menu
->Append(TreeTest_Dump
, wxT("&Dump item children"));
257 item_menu
->Append(TreeTest_Rename
, wxT("&Rename item..."));
259 item_menu
->AppendSeparator();
260 item_menu
->Append(TreeTest_SetBold
, wxT("Make item &bold"));
261 item_menu
->Append(TreeTest_ClearBold
, wxT("Make item ¬ 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"));
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
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
287 m_panel
= new wxPanel(this);
290 // create the controls
291 m_textCtrl
= new wxTextCtrl(m_panel
, wxID_ANY
, wxT(""),
292 wxDefaultPosition
, wxDefaultSize
,
293 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
296 CreateTreeWithDefStyle();
298 menu_bar
->Check(TreeTest_ToggleImages
, true);
299 menu_bar
->Check(TreeTest_ToggleAlternateImages
, false);
302 // create a status bar
304 #endif // wxUSE_STATUSBAR
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
);
312 // set our text control as the log target
313 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
314 delete wxLog::SetActiveTarget(logWindow
);
322 delete wxLog::SetActiveTarget(NULL
);
326 void MyFrame::CreateTreeWithDefStyle()
328 long style
= wxTR_DEFAULT_STYLE
|
329 #ifndef NO_VARIABLE_HEIGHT
330 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
334 CreateTree(style
| wxSUNKEN_BORDER
);
336 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
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);
349 void MyFrame::CreateTree(long style
)
351 m_treeCtrl
= new MyTreeCtrl(m_panel
, TreeTest_Ctrl
,
352 wxDefaultPosition
, wxDefaultSize
,
355 GetMenuBar()->Enable(TreeTest_SelectRoot
, !(style
& wxTR_HIDE_ROOT
));
360 void MyFrame::TogStyle(int id
, long flag
)
362 long style
= m_treeCtrl
->GetWindowStyle() ^ flag
;
364 // most treectrl styles can't be changed on the fly using the native
365 // control and the tree must be recreated
367 m_treeCtrl
->SetWindowStyle(style
);
373 GetMenuBar()->Check(id
, (style
& flag
) != 0);
376 void MyFrame::OnIdle(wxIdleEvent
& event
)
381 wxTreeItemId idRoot
= m_treeCtrl
->GetRootItem();
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 "));
393 status
= _T("No root item");
395 SetStatusText(status
, 1);
397 #endif // wxUSE_STATUSBAR
402 void MyFrame::OnSize(wxSizeEvent
& event
)
416 void MyFrame::Resize()
418 wxSize size
= GetClientSize();
419 m_treeCtrl
->SetSize(0, 0, size
.x
, size
.y
422 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3
427 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
432 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
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);
440 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
445 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
447 wxTreeItemId item
= m_treeCtrl
->GetSelection();
451 // old code - now we edit in place
453 static wxString s_text
;
454 s_text
= wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
456 if ( !s_text
.empty() )
458 m_treeCtrl
->SetItemText(item
, s_text
);
462 // TODO demonstrate creating a custom edit control...
463 (void)m_treeCtrl
->EditLabel(item
);
466 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
468 wxTreeItemId item
= m_treeCtrl
->GetSelection();
472 int i
= m_treeCtrl
->GetChildrenCount( item
, false );
474 wxLogMessage(wxT("%d children"), i
);
477 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
479 wxTreeItemId item
= m_treeCtrl
->GetSelection();
483 int i
= m_treeCtrl
->GetChildrenCount( item
);
485 wxLogMessage(wxT("%d children"), i
);
488 void MyFrame::DoSort(bool reverse
)
490 wxTreeItemId item
= m_treeCtrl
->GetSelection();
494 m_treeCtrl
->DoSortChildren(item
, reverse
);
497 void MyFrame::OnHighlight(wxCommandEvent
& WXUNUSED(event
))
499 wxTreeItemId id
= m_treeCtrl
->GetSelection();
504 if ( !m_treeCtrl
->GetBoundingRect(id
, r
, true /* text, not full row */) )
506 wxLogMessage(_T("Failed to get bounding item rect"));
510 wxClientDC
dc(m_treeCtrl
);
512 dc
.SetPen(*wxTRANSPARENT_PEN
);
514 m_treeCtrl
->Update();
517 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
519 wxTreeItemId root
= m_treeCtrl
->GetSelection();
523 m_treeCtrl
->GetItemsRecursively(root
);
526 #ifndef NO_MULTIPLE_SELECTION
528 void MyFrame::OnToggleSel(wxCommandEvent
& event
)
530 TogStyle(event
.GetId(), wxTR_MULTIPLE
);
533 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
535 wxArrayTreeItemIds array
;
537 size_t count
= m_treeCtrl
->GetSelections(array
);
538 wxLogMessage(wxT("%u items selected"), unsigned(count
));
540 for ( size_t n
= 0; n
< count
; n
++ )
542 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
546 void MyFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
548 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
551 void MyFrame::OnSelectRoot(wxCommandEvent
& WXUNUSED(event
))
553 if ( !m_treeCtrl
->HasFlag(wxTR_HIDE_ROOT
) )
554 m_treeCtrl
->SelectItem(m_treeCtrl
->GetRootItem());
557 void MyFrame::OnUnselect(wxCommandEvent
& WXUNUSED(event
))
559 m_treeCtrl
->UnselectAll();
562 #endif // NO_MULTIPLE_SELECTION
564 void MyFrame::DoSetBold(bool bold
)
566 wxTreeItemId item
= m_treeCtrl
->GetSelection();
570 m_treeCtrl
->SetItemBold(item
, bold
);
573 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
575 wxTreeItemId item
= m_treeCtrl
->GetSelection();
579 m_treeCtrl
->Delete(item
);
582 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
584 wxTreeItemId item
= m_treeCtrl
->GetSelection();
588 m_treeCtrl
->DeleteChildren(item
);
591 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
593 m_treeCtrl
->DeleteAllItems();
596 void MyFrame::OnRecreate(wxCommandEvent
& event
)
599 m_treeCtrl
->AddTestItemsToTree(5, 2);
602 void MyFrame::OnSetImageSize(wxCommandEvent
& WXUNUSED(event
))
604 int size
= wxGetNumberFromUser(wxT("Enter the size for the images to use"),
606 wxT("TreeCtrl sample"),
607 m_treeCtrl
->ImageSize());
611 m_treeCtrl
->CreateImageList(size
);
612 wxGetApp().SetShowImages(true);
615 void MyFrame::OnToggleImages(wxCommandEvent
& WXUNUSED(event
))
617 if ( wxGetApp().ShowImages() )
619 m_treeCtrl
->CreateImageList(-1);
620 wxGetApp().SetShowImages(false);
624 m_treeCtrl
->CreateImageList(0);
625 wxGetApp().SetShowImages(true);
629 void MyFrame::OnToggleAlternateImages(wxCommandEvent
& WXUNUSED(event
))
631 bool alternateImages
= m_treeCtrl
->AlternateImages();
633 m_treeCtrl
->SetAlternateImages(!alternateImages
);
634 m_treeCtrl
->CreateImageList(0);
637 void MyFrame::OnToggleButtons(wxCommandEvent
& WXUNUSED(event
))
639 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
640 if ( wxGetApp().ShowButtons() )
642 m_treeCtrl
->CreateButtonsImageList(-1);
643 wxGetApp().SetShowButtons(false);
647 m_treeCtrl
->CreateButtonsImageList(15);
648 wxGetApp().SetShowButtons(true);
653 void MyFrame::OnCollapseAndReset(wxCommandEvent
& WXUNUSED(event
))
655 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
658 void MyFrame::OnEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
660 m_treeCtrl
->DoEnsureVisible();
663 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
665 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
666 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, wxT("2nd item"));
669 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
671 static int s_num
= 0;
674 text
.Printf(wxT("Item #%d"), ++s_num
);
676 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
678 MyTreeCtrl::TreeCtrlIcon_File */ );
681 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
683 unsigned int indent
= m_treeCtrl
->GetIndent();
685 m_treeCtrl
->SetIndent( indent
+5 );
688 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
690 unsigned int indent
= m_treeCtrl
->GetIndent();
692 m_treeCtrl
->SetIndent( indent
-5 );
695 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
697 unsigned int indent
= m_treeCtrl
->GetSpacing();
699 m_treeCtrl
->SetSpacing( indent
+5 );
702 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
704 unsigned int indent
= m_treeCtrl
->GetSpacing();
706 m_treeCtrl
->SetSpacing( indent
-5 );
709 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
711 wxTreeItemId item
= m_treeCtrl
->GetSelection();
715 m_treeCtrl
->DoToggleIcon(item
);
718 void MyFrame::DoShowFirstOrLast(TreeFunc0_t pfn
, const wxString
& label
)
720 const wxTreeItemId item
= (m_treeCtrl
->*pfn
)();
723 wxLogMessage("There is no %s item", label
);
725 wxLogMessage("The %s item is \"%s\"",
726 label
, m_treeCtrl
->GetItemText(item
));
729 void MyFrame::DoShowNextOrPrev(TreeFunc1_t pfn
, const wxString
& label
)
731 wxTreeItemId item
= m_treeCtrl
->GetSelection();
735 if ( !m_treeCtrl
->IsVisible(item
) )
737 wxLogMessage("The selected item must be visible.");
741 item
= (m_treeCtrl
->*pfn
)(item
);
744 wxLogMessage("There is no %s item", label
);
746 wxLogMessage("The %s item is \"%s\"",
747 label
, m_treeCtrl
->GetItemText(item
));
750 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
752 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
754 m_treeCtrl
->SetForegroundColour(col
);
757 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
759 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
761 m_treeCtrl
->SetBackgroundColour(col
);
764 // MyTreeCtrl implementation
765 #if USE_GENERIC_TREECTRL
766 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
768 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
771 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
772 const wxPoint
& pos
, const wxSize
& size
,
774 : wxTreeCtrl(parent
, id
, pos
, size
, style
),
775 m_alternateImages(false)
777 m_reverseSort
= false;
781 // Add some items to the tree
782 AddTestItemsToTree(5, 2);
785 void MyTreeCtrl::CreateImageList(int size
)
797 // Make an image list containing small icons
798 wxImageList
*images
= new wxImageList(size
, size
, true);
800 // should correspond to TreeCtrlIcon_xxx enum
804 if (m_alternateImages
)
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
);
814 wxSize
iconSize(size
, size
);
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
);
823 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
825 int sizeOrig
= icons
[0].GetWidth();
826 if ( size
== sizeOrig
)
828 images
->Add(icons
[i
]);
832 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
836 AssignImageList(images
);
839 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
840 void MyTreeCtrl::CreateButtonsImageList(int size
)
844 SetButtonsImageList(NULL
);
848 // Make an image list containing small icons
849 wxImageList
*images
= new wxImageList(size
, size
, true);
851 // should correspond to TreeCtrlIcon_xxx enum
855 if (m_alternateImages
)
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
864 wxSize
iconSize(size
, size
);
867 icons
[1] = wxArtProvider::GetIcon(wxART_FOLDER
, wxART_LIST
, iconSize
); // closed, selected
869 icons
[3] = wxArtProvider::GetIcon(wxART_FOLDER_OPEN
, wxART_LIST
, iconSize
);// open, selected
872 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
874 int sizeOrig
= icons
[i
].GetWidth();
875 if ( size
== sizeOrig
)
877 images
->Add(icons
[i
]);
881 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
885 AssignButtonsImageList(images
);
887 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size
))
892 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
893 const wxTreeItemId
& item2
)
897 // just exchange 1st and 2nd items
898 return wxTreeCtrl::OnCompareItems(item2
, item1
);
902 return wxTreeCtrl::OnCompareItems(item1
, item2
);
906 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
913 bool hasChildren
= depth
> 1;
916 for ( size_t n
= 0; n
< numChildren
; n
++ )
918 // at depth 1 elements won't have any more children
920 str
.Printf(wxT("%s child %u"), wxT("Folder"), unsigned(n
+ 1));
922 str
.Printf(wxT("%s child %u.%u"), wxT("File"), unsigned(folder
), unsigned(n
+ 1));
924 // here we pass to AppendItem() normal and selected item images (we
925 // suppose that selected image follows the normal one in the enum)
927 if ( wxGetApp().ShowImages() )
929 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
930 imageSel
= image
+ 1;
934 image
= imageSel
= -1;
936 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
937 new MyTreeItemData(str
));
939 // and now we also set the expanded one (only for the folders)
940 if ( hasChildren
&& wxGetApp().ShowImages() )
942 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
943 wxTreeItemIcon_Expanded
);
946 // remember the last child for OnEnsureVisible()
947 if ( !hasChildren
&& n
== numChildren
- 1 )
952 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
958 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
961 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
962 wxTreeItemId rootId
= AddRoot(wxT("Root"),
964 new MyTreeItemData(wxT("Root item")));
965 if ( !HasFlag(wxTR_HIDE_ROOT
) && image
!= -1 )
967 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
970 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
972 // set some colours/fonts for testing
973 if ( !HasFlag(wxTR_HIDE_ROOT
) )
974 SetItemFont(rootId
, *wxITALIC_FONT
);
976 wxTreeItemIdValue cookie
;
977 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
978 SetItemTextColour(id
, *wxBLUE
);
980 id
= GetNextChild(rootId
, cookie
);
981 id
= GetNextChild(rootId
, cookie
);
982 SetItemTextColour(id
, *wxRED
);
983 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
986 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
,
987 wxTreeItemIdValue cookie
)
992 id
= GetFirstChild(idParent
, cookie
);
994 id
= GetNextChild(idParent
, cookie
);
999 wxString text
= GetItemText(id
);
1002 if (ItemHasChildren(id
))
1003 GetItemsRecursively(id
);
1005 GetItemsRecursively(idParent
, cookie
);
1008 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
1010 int image
= (GetItemImage(item
) == TreeCtrlIcon_Folder
)
1012 : TreeCtrlIcon_Folder
;
1013 SetItemImage(item
, image
, wxTreeItemIcon_Normal
);
1015 image
= (GetItemImage(item
) == TreeCtrlIcon_FolderSelected
)
1016 ? TreeCtrlIcon_FileSelected
1017 : TreeCtrlIcon_FolderSelected
;
1018 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1021 void MyTreeCtrl::LogEvent(const wxChar
*name
, const wxTreeEvent
& event
)
1023 wxTreeItemId item
= event
.GetItem();
1026 text
<< _T('"') << GetItemText(item
).c_str() << _T('"');
1028 text
= _T("invalid item");
1029 wxLogMessage(wxT("%s(%s)"), name
, text
.c_str());
1033 #define TREE_EVENT_HANDLER(name) \
1034 void MyTreeCtrl::name(wxTreeEvent& event) \
1036 LogEvent(_T(#name), event); \
1037 SetLastItem(wxTreeItemId()); \
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
)
1051 #undef TREE_EVENT_HANDLER
1053 void LogKeyEvent(const wxChar
*name
, const wxKeyEvent
& event
)
1056 long keycode
= event
.GetKeyCode();
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;
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);
1166 key
.Printf(wxT("unknown (%ld)"), keycode
);
1171 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1174 event
.ControlDown() ? wxT('C') : wxT('-'),
1175 event
.AltDown() ? wxT('A') : wxT('-'),
1176 event
.ShiftDown() ? wxT('S') : wxT('-'),
1177 event
.MetaDown() ? wxT('M') : wxT('-'));
1180 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
& event
)
1182 LogKeyEvent(wxT("Tree key down "), event
.GetKeyEvent());
1187 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
1189 // need to explicitly allow drag
1190 if ( event
.GetItem() != GetRootItem() )
1192 m_draggedItem
= event
.GetItem();
1194 wxPoint clientpt
= event
.GetPoint();
1195 wxPoint screenpt
= ClientToScreen(clientpt
);
1197 wxLogMessage(wxT("OnBeginDrag: started dragging %s at screen coords (%i,%i)"),
1198 GetItemText(m_draggedItem
).c_str(),
1199 screenpt
.x
, screenpt
.y
);
1205 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1209 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
1211 wxTreeItemId itemSrc
= m_draggedItem
,
1212 itemDst
= event
.GetItem();
1213 m_draggedItem
= (wxTreeItemId
)0l;
1215 // where to copy the item?
1216 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
1218 // copy to the parent then
1219 itemDst
= GetItemParent(itemDst
);
1222 if ( !itemDst
.IsOk() )
1224 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1229 wxString text
= GetItemText(itemSrc
);
1230 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1231 text
.c_str(), GetItemText(itemDst
).c_str());
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.
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
);
1244 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
1246 wxLogMessage(wxT("OnBeginLabelEdit"));
1248 // for testing, prevent this item's label editing
1249 wxTreeItemId itemId
= event
.GetItem();
1250 if ( IsTestItem(itemId
) )
1252 wxMessageBox(wxT("You can't edit this item."));
1256 else if ( itemId
== GetRootItem() )
1258 // test that it is possible to change the text of the item being edited
1259 SetItemText(itemId
, _T("Editing root item"));
1263 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
1265 wxLogMessage(wxT("OnEndLabelEdit"));
1267 // don't allow anything except letters in the labels
1268 if ( !event
.GetLabel().IsWord() )
1270 wxMessageBox(wxT("The new label should be a single word."));
1276 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
1278 wxLogMessage(wxT("OnItemCollapsing"));
1280 // for testing, prevent the user from collapsing the first child folder
1281 wxTreeItemId itemId
= event
.GetItem();
1282 if ( IsTestItem(itemId
) )
1284 wxMessageBox(wxT("You can't collapse this item."));
1290 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
1292 // show some info about this item
1293 wxTreeItemId itemId
= event
.GetItem();
1294 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1298 item
->ShowInfo(this);
1301 wxLogMessage(wxT("OnItemActivated"));
1304 void MyTreeCtrl::OnItemMenu(wxTreeEvent
& event
)
1306 wxTreeItemId itemId
= event
.GetItem();
1307 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1309 wxPoint clientpt
= event
.GetPoint();
1310 wxPoint screenpt
= ClientToScreen(clientpt
);
1312 wxLogMessage(wxT("OnItemMenu for item \"%s\" at screen coords (%i, %i)"),
1313 item
? item
->GetDesc() : _T(""), screenpt
.x
, screenpt
.y
);
1315 ShowMenu(itemId
, clientpt
);
1319 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1321 wxPoint pt
= event
.GetPosition();
1323 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt
.x
, pt
.y
);
1326 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
1331 title
<< wxT("Menu for ") << GetItemText(id
);
1335 title
= wxT("Menu for no particular item");
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"));
1345 PopupMenu(&menu
, pt
);
1346 #endif // wxUSE_MENUS
1349 void MyTreeCtrl::OnItemRClick(wxTreeEvent
& event
)
1351 wxTreeItemId itemId
= event
.GetItem();
1352 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1355 wxLogMessage(wxT("Item \"%s\" right clicked"), item
? item
->GetDesc()
1361 void MyTreeCtrl::OnRMouseDown(wxMouseEvent
& event
)
1363 wxLogMessage(wxT("Right mouse button down"));
1368 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
1370 wxLogMessage(wxT("Right mouse button up"));
1375 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
1377 wxTreeItemId id
= HitTest(event
.GetPosition());
1379 wxLogMessage(wxT("No item under mouse"));
1382 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
1384 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1390 static inline const wxChar
*Bool2String(bool b
)
1392 return b
? wxT("") : wxT("not ");
1395 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1397 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1398 wxT("%u children (%u immediately under this item)."),
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)));