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"
32 #include "wx/renderer.h"
35 // this is not supported by native control
36 #define NO_VARIABLE_HEIGHT
54 #include "../sample.xpm"
57 // verify that the item is ok and insult the user if it is not
58 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
59 wxMessageBox(wxT("Please select some item first!"), \
60 wxT("Tree sample error"), \
61 wxOK | wxICON_EXCLAMATION, \
66 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
68 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
69 EVT_IDLE(MyFrame::OnIdle
)
70 EVT_SIZE(MyFrame::OnSize
)
80 MENU_LINK(TogHideRoot
)
81 MENU_LINK(TogRootLines
)
83 MENU_LINK(TogFullHighlight
)
84 MENU_LINK(SetFgColour
)
85 MENU_LINK(SetBgColour
)
90 #ifndef NO_MULTIPLE_SELECTION
91 MENU_LINK(DumpSelected
)
95 #endif // NO_MULTIPLE_SELECTION
104 MENU_LINK(DeleteChildren
)
107 MENU_LINK(ToggleImages
)
108 MENU_LINK(ToggleStates
)
109 MENU_LINK(ToggleAlternateImages
)
110 MENU_LINK(ToggleAlternateStates
)
111 MENU_LINK(ToggleButtons
)
112 MENU_LINK(SetImageSize
)
113 MENU_LINK(CollapseAndReset
)
114 MENU_LINK(EnsureVisible
)
116 MENU_LINK(InsertItem
)
119 MENU_LINK(IncSpacing
)
120 MENU_LINK(DecSpacing
)
121 MENU_LINK(ToggleIcon
)
122 MENU_LINK(ToggleState
)
123 MENU_LINK(SelectRoot
)
125 MENU_LINK(ShowFirstVisible
)
126 #ifdef wxHAS_LAST_VISIBLE
127 MENU_LINK(ShowLastVisible
)
128 #endif // wxHAS_LAST_VISIBLE
129 MENU_LINK(ShowNextVisible
)
130 MENU_LINK(ShowPrevVisible
)
135 #if USE_GENERIC_TREECTRL
136 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
138 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
140 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
141 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
142 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
143 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
144 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
145 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
146 #if 0 // there are so many of those that logging them causes flicker
147 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
149 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
150 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
151 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
152 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
153 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
155 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
156 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
157 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
158 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
159 EVT_TREE_STATE_IMAGE_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemStateClick
)
161 // so many different ways to handle right mouse button clicks...
162 EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu
)
163 // EVT_TREE_ITEM_MENU is the preferred event for creating context menus
164 // on a tree control, because it includes the point of the click or item,
165 // meaning that no additional placement calculations are required.
166 EVT_TREE_ITEM_MENU(TreeTest_Ctrl
, MyTreeCtrl::OnItemMenu
)
167 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRClick
)
169 EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown
)
170 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
171 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
178 if ( !wxApp::OnInit() )
181 // Create the main frame window
182 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
192 // My frame constructor
193 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
194 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
200 // This reduces flicker effects - even better would be to define
201 // OnEraseBackground to do nothing. When the tree control's scrollbars are
202 // show or hidden, the frame is sent a background erase event.
203 SetBackgroundColour(wxColour(255, 255, 255));
206 SetIcon(wxICON(sample
));
210 wxMenu
*file_menu
= new wxMenu
,
211 *style_menu
= new wxMenu
,
212 *tree_menu
= new wxMenu
,
213 *item_menu
= new wxMenu
;
215 file_menu
->Append(TreeTest_ClearLog
, wxT("&Clear log\tCtrl-L"));
216 file_menu
->AppendSeparator();
217 file_menu
->Append(TreeTest_About
, wxT("&About..."));
218 file_menu
->AppendSeparator();
219 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
221 style_menu
->AppendCheckItem(TreeTest_TogButtons
, wxT("Toggle &normal buttons"));
222 style_menu
->AppendCheckItem(TreeTest_TogTwist
, wxT("Toggle &twister buttons"));
223 style_menu
->AppendCheckItem(TreeTest_ToggleButtons
, wxT("Toggle image &buttons"));
224 style_menu
->AppendSeparator();
225 style_menu
->AppendCheckItem(TreeTest_TogLines
, wxT("Toggle &connecting lines"));
226 style_menu
->AppendCheckItem(TreeTest_TogRootLines
, wxT("Toggle &lines at root"));
227 style_menu
->AppendCheckItem(TreeTest_TogHideRoot
, wxT("Toggle &hidden root"));
228 style_menu
->AppendCheckItem(TreeTest_TogBorder
, wxT("Toggle &item border"));
229 style_menu
->AppendCheckItem(TreeTest_TogFullHighlight
, wxT("Toggle &full row highlight"));
230 style_menu
->AppendCheckItem(TreeTest_TogEdit
, wxT("Toggle &edit mode"));
231 #ifndef NO_MULTIPLE_SELECTION
232 style_menu
->AppendCheckItem(TreeTest_ToggleSel
, wxT("Toggle &selection mode\tCtrl-S"));
233 #endif // NO_MULTIPLE_SELECTION
234 style_menu
->AppendCheckItem(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
235 style_menu
->AppendCheckItem(TreeTest_ToggleStates
, wxT("Toggle show st&ates"));
236 style_menu
->AppendCheckItem(TreeTest_ToggleAlternateImages
, wxT("Toggle alternate images"));
237 style_menu
->AppendCheckItem(TreeTest_ToggleAlternateStates
, wxT("Toggle alternate state images"));
238 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
239 style_menu
->AppendSeparator();
240 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
241 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
242 style_menu
->AppendSeparator();
243 style_menu
->Append(TreeTest_ResetStyle
, wxT("&Reset to default\tF10"));
245 tree_menu
->Append(TreeTest_Recreate
, wxT("&Recreate the tree"));
246 tree_menu
->Append(TreeTest_CollapseAndReset
, wxT("C&ollapse and reset"));
247 tree_menu
->AppendSeparator();
248 tree_menu
->Append(TreeTest_AddItem
, wxT("Append a &new item"));
249 tree_menu
->Append(TreeTest_InsertItem
, wxT("&Insert a new item"));
250 tree_menu
->Append(TreeTest_Delete
, wxT("&Delete this item"));
251 tree_menu
->Append(TreeTest_DeleteChildren
, wxT("Delete &children"));
252 tree_menu
->Append(TreeTest_DeleteAll
, wxT("Delete &all items"));
253 tree_menu
->Append(TreeTest_SelectRoot
, wxT("Select root item"));
254 tree_menu
->AppendSeparator();
255 tree_menu
->Append(TreeTest_Count
, wxT("Count children of current item"));
256 tree_menu
->Append(TreeTest_CountRec
, wxT("Recursively count children of current item"));
257 tree_menu
->AppendSeparator();
258 tree_menu
->Append(TreeTest_Sort
, wxT("Sort children of current item"));
259 tree_menu
->Append(TreeTest_SortRev
, wxT("Sort in reversed order"));
260 tree_menu
->AppendSeparator();
261 tree_menu
->Append(TreeTest_EnsureVisible
, wxT("Make the last item &visible"));
262 tree_menu
->AppendSeparator();
263 tree_menu
->Append(TreeTest_IncIndent
, wxT("Add 5 points to indentation\tAlt-I"));
264 tree_menu
->Append(TreeTest_DecIndent
, wxT("Reduce indentation by 5 points\tAlt-R"));
265 tree_menu
->AppendSeparator();
266 tree_menu
->Append(TreeTest_IncSpacing
, wxT("Add 5 points to spacing\tCtrl-I"));
267 tree_menu
->Append(TreeTest_DecSpacing
, wxT("Reduce spacing by 5 points\tCtrl-R"));
269 item_menu
->Append(TreeTest_Dump
, wxT("&Dump item children"));
270 item_menu
->Append(TreeTest_Rename
, wxT("&Rename item..."));
272 item_menu
->AppendSeparator();
273 item_menu
->Append(TreeTest_SetBold
, wxT("Make item &bold"));
274 item_menu
->Append(TreeTest_ClearBold
, wxT("Make item ¬ bold"));
275 item_menu
->AppendSeparator();
276 item_menu
->Append(TreeTest_ToggleIcon
, wxT("Toggle the item's &icon"));
277 item_menu
->Append(TreeTest_ToggleState
, wxT("Toggle the item's &state"));
278 item_menu
->AppendSeparator();
279 item_menu
->Append(TreeTest_ShowFirstVisible
, wxT("Show &first visible"));
280 #ifdef wxHAS_LAST_VISIBLE
281 item_menu
->Append(TreeTest_ShowLastVisible
, wxT("Show &last visible"));
282 #endif // wxHAS_LAST_VISIBLE
283 item_menu
->Append(TreeTest_ShowNextVisible
, wxT("Show &next visible"));
284 item_menu
->Append(TreeTest_ShowPrevVisible
, wxT("Show &previous visible"));
286 #ifndef NO_MULTIPLE_SELECTION
287 item_menu
->AppendSeparator();
288 item_menu
->Append(TreeTest_DumpSelected
, wxT("Dump selected items\tAlt-D"));
289 item_menu
->Append(TreeTest_Select
, wxT("Select current item\tAlt-S"));
290 item_menu
->Append(TreeTest_Unselect
, wxT("Unselect everything\tAlt-U"));
291 #endif // NO_MULTIPLE_SELECTION
293 wxMenuBar
*menu_bar
= new wxMenuBar
;
294 menu_bar
->Append(file_menu
, wxT("&File"));
295 menu_bar
->Append(style_menu
, wxT("&Style"));
296 menu_bar
->Append(tree_menu
, wxT("&Tree"));
297 menu_bar
->Append(item_menu
, wxT("&Item"));
298 SetMenuBar(menu_bar
);
299 #endif // wxUSE_MENUS
301 m_panel
= new wxPanel(this);
304 // create the controls
305 m_textCtrl
= new wxTextCtrl(m_panel
, wxID_ANY
, wxT(""),
306 wxDefaultPosition
, wxDefaultSize
,
307 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
310 CreateTreeWithDefStyle();
312 menu_bar
->Check(TreeTest_ToggleImages
, true);
313 menu_bar
->Check(TreeTest_ToggleStates
, true);
314 menu_bar
->Check(TreeTest_ToggleAlternateImages
, false);
315 menu_bar
->Check(TreeTest_ToggleAlternateStates
, false);
318 // create a status bar
320 #endif // wxUSE_STATUSBAR
324 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
325 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
326 delete wxLog::SetActiveTarget(new wxLogStderr
);
328 // set our text control as the log target
329 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
330 delete wxLog::SetActiveTarget(logWindow
);
338 delete wxLog::SetActiveTarget(NULL
);
342 void MyFrame::CreateTreeWithDefStyle()
344 long style
= wxTR_DEFAULT_STYLE
|
345 #ifndef NO_VARIABLE_HEIGHT
346 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
350 CreateTree(style
| wxSUNKEN_BORDER
);
352 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
354 wxMenuBar
*mbar
= GetMenuBar();
355 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_HAS_BUTTONS
) != 0);
356 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_TWIST_BUTTONS
) != 0);
357 mbar
->Check(TreeTest_TogLines
, (style
& wxTR_NO_LINES
) == 0);
358 mbar
->Check(TreeTest_TogRootLines
, (style
& wxTR_LINES_AT_ROOT
) != 0);
359 mbar
->Check(TreeTest_TogHideRoot
, (style
& wxTR_HIDE_ROOT
) != 0);
360 mbar
->Check(TreeTest_TogEdit
, (style
& wxTR_EDIT_LABELS
) != 0);
361 mbar
->Check(TreeTest_TogBorder
, (style
& wxTR_ROW_LINES
) != 0);
362 mbar
->Check(TreeTest_TogFullHighlight
, (style
& wxTR_FULL_ROW_HIGHLIGHT
) != 0);
365 void MyFrame::CreateTree(long style
)
367 m_treeCtrl
= new MyTreeCtrl(m_panel
, TreeTest_Ctrl
,
368 wxDefaultPosition
, wxDefaultSize
,
371 GetMenuBar()->Enable(TreeTest_SelectRoot
, !(style
& wxTR_HIDE_ROOT
));
376 void MyFrame::TogStyle(int id
, long flag
)
378 long style
= m_treeCtrl
->GetWindowStyle() ^ flag
;
380 // most treectrl styles can't be changed on the fly using the native
381 // control and the tree must be recreated
383 m_treeCtrl
->SetWindowStyle(style
);
389 GetMenuBar()->Check(id
, (style
& flag
) != 0);
392 void MyFrame::OnIdle(wxIdleEvent
& event
)
397 wxTreeItemId idRoot
= m_treeCtrl
->GetRootItem();
401 wxTreeItemId idLast
= m_treeCtrl
->GetLastChild(idRoot
);
402 status
= wxString::Format(
403 _T("Root/last item is %svisible/%svisible"),
404 m_treeCtrl
->IsVisible(idRoot
) ? _T("") : _T("not "),
405 idLast
.IsOk() && m_treeCtrl
->IsVisible(idLast
)
406 ? _T("") : _T("not "));
409 status
= _T("No root item");
411 SetStatusText(status
, 1);
413 #endif // wxUSE_STATUSBAR
418 void MyFrame::OnSize(wxSizeEvent
& event
)
432 void MyFrame::Resize()
434 wxSize size
= GetClientSize();
435 m_treeCtrl
->SetSize(0, 0, size
.x
, size
.y
438 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3
443 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
448 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
450 wxMessageBox(wxT("Tree test sample\n")
451 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
452 wxT("About tree test"),
453 wxOK
| wxICON_INFORMATION
, this);
456 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
461 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
463 wxTreeItemId item
= m_treeCtrl
->GetSelection();
467 // old code - now we edit in place
469 static wxString s_text
;
470 s_text
= wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
472 if ( !s_text
.empty() )
474 m_treeCtrl
->SetItemText(item
, s_text
);
478 // TODO demonstrate creating a custom edit control...
479 (void)m_treeCtrl
->EditLabel(item
);
482 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
484 wxTreeItemId item
= m_treeCtrl
->GetSelection();
488 int i
= m_treeCtrl
->GetChildrenCount( item
, false );
490 wxLogMessage(wxT("%d children"), i
);
493 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
495 wxTreeItemId item
= m_treeCtrl
->GetSelection();
499 int i
= m_treeCtrl
->GetChildrenCount( item
);
501 wxLogMessage(wxT("%d children"), i
);
504 void MyFrame::DoSort(bool reverse
)
506 wxTreeItemId item
= m_treeCtrl
->GetSelection();
510 m_treeCtrl
->DoSortChildren(item
, reverse
);
513 void MyFrame::OnHighlight(wxCommandEvent
& WXUNUSED(event
))
515 wxTreeItemId id
= m_treeCtrl
->GetSelection();
520 if ( !m_treeCtrl
->GetBoundingRect(id
, r
, true /* text, not full row */) )
522 wxLogMessage(_T("Failed to get bounding item rect"));
526 wxClientDC
dc(m_treeCtrl
);
528 dc
.SetPen(*wxTRANSPARENT_PEN
);
530 m_treeCtrl
->Update();
533 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
535 wxTreeItemId root
= m_treeCtrl
->GetSelection();
539 m_treeCtrl
->GetItemsRecursively(root
);
542 #ifndef NO_MULTIPLE_SELECTION
544 void MyFrame::OnToggleSel(wxCommandEvent
& event
)
546 TogStyle(event
.GetId(), wxTR_MULTIPLE
);
549 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
551 wxArrayTreeItemIds array
;
553 size_t count
= m_treeCtrl
->GetSelections(array
);
554 wxLogMessage(wxT("%u items selected"), unsigned(count
));
556 for ( size_t n
= 0; n
< count
; n
++ )
558 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
562 void MyFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
564 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
567 void MyFrame::OnSelectRoot(wxCommandEvent
& WXUNUSED(event
))
569 if ( !m_treeCtrl
->HasFlag(wxTR_HIDE_ROOT
) )
570 m_treeCtrl
->SelectItem(m_treeCtrl
->GetRootItem());
573 void MyFrame::OnUnselect(wxCommandEvent
& WXUNUSED(event
))
575 m_treeCtrl
->UnselectAll();
578 #endif // NO_MULTIPLE_SELECTION
580 void MyFrame::DoSetBold(bool bold
)
582 wxTreeItemId item
= m_treeCtrl
->GetSelection();
586 m_treeCtrl
->SetItemBold(item
, bold
);
589 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
591 wxTreeItemId item
= m_treeCtrl
->GetSelection();
595 m_treeCtrl
->Delete(item
);
598 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
600 wxTreeItemId item
= m_treeCtrl
->GetSelection();
604 m_treeCtrl
->DeleteChildren(item
);
607 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
609 m_treeCtrl
->DeleteAllItems();
612 void MyFrame::OnRecreate(wxCommandEvent
& event
)
615 m_treeCtrl
->AddTestItemsToTree(5, 2);
618 void MyFrame::OnSetImageSize(wxCommandEvent
& WXUNUSED(event
))
620 int size
= wxGetNumberFromUser(wxT("Enter the size for the images to use"),
622 wxT("TreeCtrl sample"),
623 m_treeCtrl
->ImageSize());
627 m_treeCtrl
->CreateImageList(size
);
628 wxGetApp().SetShowImages(true);
631 void MyFrame::OnToggleImages(wxCommandEvent
& WXUNUSED(event
))
633 if ( wxGetApp().ShowImages() )
635 m_treeCtrl
->CreateImageList(-1);
636 wxGetApp().SetShowImages(false);
640 m_treeCtrl
->CreateImageList(0);
641 wxGetApp().SetShowImages(true);
645 void MyFrame::OnToggleStates(wxCommandEvent
& WXUNUSED(event
))
647 if ( wxGetApp().ShowStates() )
649 m_treeCtrl
->CreateStateImageList(true);
650 wxGetApp().SetShowStates(false);
654 m_treeCtrl
->CreateStateImageList(false);
655 wxGetApp().SetShowStates(true);
659 void MyFrame::OnToggleAlternateImages(wxCommandEvent
& WXUNUSED(event
))
661 bool alternateImages
= m_treeCtrl
->AlternateImages();
663 m_treeCtrl
->SetAlternateImages(!alternateImages
);
664 m_treeCtrl
->CreateImageList(0);
667 void MyFrame::OnToggleAlternateStates(wxCommandEvent
& WXUNUSED(event
))
669 bool alternateStates
= m_treeCtrl
->AlternateStates();
671 m_treeCtrl
->SetAlternateStates(!alternateStates
);
672 m_treeCtrl
->CreateStateImageList();
675 void MyFrame::OnToggleButtons(wxCommandEvent
& WXUNUSED(event
))
677 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
678 if ( wxGetApp().ShowButtons() )
680 m_treeCtrl
->CreateButtonsImageList(-1);
681 wxGetApp().SetShowButtons(false);
685 m_treeCtrl
->CreateButtonsImageList(15);
686 wxGetApp().SetShowButtons(true);
691 void MyFrame::OnCollapseAndReset(wxCommandEvent
& WXUNUSED(event
))
693 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
696 void MyFrame::OnEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
698 m_treeCtrl
->DoEnsureVisible();
701 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
703 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
704 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, wxT("2nd item"));
707 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
709 static int s_num
= 0;
712 text
.Printf(wxT("Item #%d"), ++s_num
);
714 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
716 MyTreeCtrl::TreeCtrlIcon_File */ );
719 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
721 unsigned int indent
= m_treeCtrl
->GetIndent();
723 m_treeCtrl
->SetIndent( indent
+5 );
726 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
728 unsigned int indent
= m_treeCtrl
->GetIndent();
730 m_treeCtrl
->SetIndent( indent
-5 );
733 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
735 unsigned int indent
= m_treeCtrl
->GetSpacing();
737 m_treeCtrl
->SetSpacing( indent
+5 );
740 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
742 unsigned int indent
= m_treeCtrl
->GetSpacing();
744 m_treeCtrl
->SetSpacing( indent
-5 );
747 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
749 wxTreeItemId item
= m_treeCtrl
->GetSelection();
753 m_treeCtrl
->DoToggleIcon(item
);
756 void MyFrame::OnToggleState(wxCommandEvent
& WXUNUSED(event
))
758 wxTreeItemId item
= m_treeCtrl
->GetSelection();
762 m_treeCtrl
->DoToggleState(item
);
765 void MyFrame::DoShowFirstOrLast(TreeFunc0_t pfn
, const wxString
& label
)
767 const wxTreeItemId item
= (m_treeCtrl
->*pfn
)();
770 wxLogMessage("There is no %s item", label
);
772 wxLogMessage("The %s item is \"%s\"",
773 label
, m_treeCtrl
->GetItemText(item
));
776 void MyFrame::DoShowNextOrPrev(TreeFunc1_t pfn
, const wxString
& label
)
778 wxTreeItemId item
= m_treeCtrl
->GetSelection();
782 if ( !m_treeCtrl
->IsVisible(item
) )
784 wxLogMessage("The selected item must be visible.");
788 item
= (m_treeCtrl
->*pfn
)(item
);
791 wxLogMessage("There is no %s item", label
);
793 wxLogMessage("The %s item is \"%s\"",
794 label
, m_treeCtrl
->GetItemText(item
));
797 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
799 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
801 m_treeCtrl
->SetForegroundColour(col
);
804 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
806 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
808 m_treeCtrl
->SetBackgroundColour(col
);
811 // MyTreeCtrl implementation
812 #if USE_GENERIC_TREECTRL
813 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
815 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
818 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
819 const wxPoint
& pos
, const wxSize
& size
,
821 : wxTreeCtrl(parent
, id
, pos
, size
, style
),
822 m_alternateImages(false),
823 m_alternateStates(false)
825 m_reverseSort
= false;
828 CreateStateImageList();
830 // Add some items to the tree
831 AddTestItemsToTree(5, 2);
834 void MyTreeCtrl::CreateImageList(int size
)
846 // Make an image list containing small icons
847 wxImageList
*images
= new wxImageList(size
, size
, true);
849 // should correspond to TreeCtrlIcon_xxx enum
853 if (m_alternateImages
)
855 icons
[TreeCtrlIcon_File
] = wxIcon(icon1_xpm
);
856 icons
[TreeCtrlIcon_FileSelected
] = wxIcon(icon2_xpm
);
857 icons
[TreeCtrlIcon_Folder
] = wxIcon(icon3_xpm
);
858 icons
[TreeCtrlIcon_FolderSelected
] = wxIcon(icon4_xpm
);
859 icons
[TreeCtrlIcon_FolderOpened
] = wxIcon(icon5_xpm
);
863 wxSize
iconSize(size
, size
);
865 icons
[TreeCtrlIcon_File
] =
866 icons
[TreeCtrlIcon_FileSelected
] = wxArtProvider::GetIcon(wxART_NORMAL_FILE
, wxART_LIST
, iconSize
);
867 icons
[TreeCtrlIcon_Folder
] =
868 icons
[TreeCtrlIcon_FolderSelected
] =
869 icons
[TreeCtrlIcon_FolderOpened
] = wxArtProvider::GetIcon(wxART_FOLDER
, wxART_LIST
, iconSize
);
872 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
874 int sizeOrig
= icons
[0].GetWidth();
875 if ( size
== sizeOrig
)
877 images
->Add(icons
[i
]);
881 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
885 AssignImageList(images
);
888 void MyTreeCtrl::CreateStateImageList(bool del
)
892 SetStateImageList(NULL
);
899 if (m_alternateStates
)
902 icons
[0] = wxIcon(state1_xpm
); // yellow
903 icons
[1] = wxIcon(state2_xpm
); // green
904 icons
[2] = wxIcon(state3_xpm
); // red
905 icons
[3] = wxIcon(state4_xpm
); // blue
906 icons
[4] = wxIcon(state5_xpm
); // black
908 int width
= icons
[0].GetWidth(),
909 height
= icons
[0].GetHeight();
911 // Make an state image list containing small icons
912 states
= new wxImageList(width
, height
, true);
914 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
915 states
->Add(icons
[i
]);
920 int width
= ::GetSystemMetrics(SM_CXMENUCHECK
),
921 height
= ::GetSystemMetrics(SM_CYMENUCHECK
);
927 // make an state checkbox image list
928 states
= new wxImageList(width
, height
, true);
930 wxBitmap
checkBmp(width
, height
);
931 wxRect
rect (0, 0, width
, height
);
933 wxRendererNative
& renderer
= wxRendererNative::Get();
935 // create no checked image
937 // first create bitmap in a memory DC
938 wxMemoryDC
memDC(checkBmp
);
940 // then draw a check mark into it
941 renderer
.DrawCheckBox(this, memDC
, rect
, 0);
942 } // select checkBmp out of memDC
944 states
->Add(checkBmp
);
946 // create checked image
948 wxMemoryDC
memDC(checkBmp
);
949 renderer
.DrawCheckBox(this, memDC
, rect
, wxCONTROL_CHECKED
);
952 states
->Add(checkBmp
);
955 AssignStateImageList(states
);
958 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
959 void MyTreeCtrl::CreateButtonsImageList(int size
)
963 SetButtonsImageList(NULL
);
967 // Make an image list containing small icons
968 wxImageList
*images
= new wxImageList(size
, size
, true);
970 // should correspond to TreeCtrlIcon_xxx enum
974 if (m_alternateImages
)
976 icons
[0] = wxIcon(icon3_xpm
); // closed
977 icons
[1] = wxIcon(icon3_xpm
); // closed, selected
978 icons
[2] = wxIcon(icon5_xpm
); // open
979 icons
[3] = wxIcon(icon5_xpm
); // open, selected
983 wxSize
iconSize(size
, size
);
986 icons
[1] = wxArtProvider::GetIcon(wxART_FOLDER
, wxART_LIST
, iconSize
); // closed, selected
988 icons
[3] = wxArtProvider::GetIcon(wxART_FOLDER_OPEN
, wxART_LIST
, iconSize
);// open, selected
991 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
993 int sizeOrig
= icons
[i
].GetWidth();
994 if ( size
== sizeOrig
)
996 images
->Add(icons
[i
]);
1000 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
1004 AssignButtonsImageList(images
);
1006 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size
))
1011 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1012 const wxTreeItemId
& item2
)
1014 if ( m_reverseSort
)
1016 // just exchange 1st and 2nd items
1017 return wxTreeCtrl::OnCompareItems(item2
, item1
);
1021 return wxTreeCtrl::OnCompareItems(item1
, item2
);
1025 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
1032 bool hasChildren
= depth
> 1;
1035 for ( size_t n
= 0; n
< numChildren
; n
++ )
1037 // at depth 1 elements won't have any more children
1039 str
.Printf(wxT("%s child %u"), wxT("Folder"), unsigned(n
+ 1));
1041 str
.Printf(wxT("%s child %u.%u"), wxT("File"), unsigned(folder
), unsigned(n
+ 1));
1043 // here we pass to AppendItem() normal and selected item images (we
1044 // suppose that selected image follows the normal one in the enum)
1045 int image
, imageSel
;
1046 if ( wxGetApp().ShowImages() )
1048 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
1049 imageSel
= image
+ 1;
1053 image
= imageSel
= -1;
1055 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
1056 new MyTreeItemData(str
));
1058 if ( wxGetApp().ShowStates() )
1059 SetItemState(id
, 0);
1061 // and now we also set the expanded one (only for the folders)
1062 if ( hasChildren
&& wxGetApp().ShowImages() )
1064 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
1065 wxTreeItemIcon_Expanded
);
1068 // remember the last child for OnEnsureVisible()
1069 if ( !hasChildren
&& n
== numChildren
- 1 )
1074 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
1080 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
1083 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
1084 wxTreeItemId rootId
= AddRoot(wxT("Root"),
1086 new MyTreeItemData(wxT("Root item")));
1087 if ( !HasFlag(wxTR_HIDE_ROOT
) && image
!= -1 )
1089 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
1092 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
1094 // set some colours/fonts for testing
1095 if ( !HasFlag(wxTR_HIDE_ROOT
) )
1096 SetItemFont(rootId
, *wxITALIC_FONT
);
1098 wxTreeItemIdValue cookie
;
1099 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
1100 SetItemTextColour(id
, *wxBLUE
);
1102 id
= GetNextChild(rootId
, cookie
);
1103 id
= GetNextChild(rootId
, cookie
);
1104 SetItemTextColour(id
, *wxRED
);
1105 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
1108 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
,
1109 wxTreeItemIdValue cookie
)
1114 id
= GetFirstChild(idParent
, cookie
);
1116 id
= GetNextChild(idParent
, cookie
);
1121 wxString text
= GetItemText(id
);
1124 if (ItemHasChildren(id
))
1125 GetItemsRecursively(id
);
1127 GetItemsRecursively(idParent
, cookie
);
1130 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
1132 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
1134 : TreeCtrlIcon_Folder
;
1135 SetItemImage(item
, image
, wxTreeItemIcon_Normal
);
1137 image
= GetItemImage(item
, wxTreeItemIcon_Selected
) == TreeCtrlIcon_FolderSelected
1138 ? TreeCtrlIcon_FileSelected
1139 : TreeCtrlIcon_FolderSelected
;
1140 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1143 void MyTreeCtrl::DoToggleState(const wxTreeItemId
& item
)
1145 if ( m_alternateStates
)
1147 // sets random state unlike current
1148 int state
= GetItemState(item
);
1153 nState
= rand() % GetStateImageList()->GetImageCount();
1154 } while (nState
== state
);
1156 SetItemState(item
, nState
);
1160 // we have only 2 checkbox states, so next state will be reversed
1161 SetItemState(item
, wxTREE_ITEMSTATE_NEXT
);
1165 void MyTreeCtrl::LogEvent(const wxChar
*name
, const wxTreeEvent
& event
)
1167 wxTreeItemId item
= event
.GetItem();
1170 text
<< _T('"') << GetItemText(item
).c_str() << _T('"');
1172 text
= _T("invalid item");
1173 wxLogMessage(wxT("%s(%s)"), name
, text
.c_str());
1177 #define TREE_EVENT_HANDLER(name) \
1178 void MyTreeCtrl::name(wxTreeEvent& event) \
1180 LogEvent(_T(#name), event); \
1181 SetLastItem(wxTreeItemId()); \
1185 TREE_EVENT_HANDLER(OnBeginRDrag
)
1186 TREE_EVENT_HANDLER(OnDeleteItem
)
1187 TREE_EVENT_HANDLER(OnGetInfo
)
1188 TREE_EVENT_HANDLER(OnSetInfo
)
1189 TREE_EVENT_HANDLER(OnItemExpanded
)
1190 TREE_EVENT_HANDLER(OnItemExpanding
)
1191 TREE_EVENT_HANDLER(OnItemCollapsed
)
1192 TREE_EVENT_HANDLER(OnSelChanged
)
1193 TREE_EVENT_HANDLER(OnSelChanging
)
1195 #undef TREE_EVENT_HANDLER
1197 void LogKeyEvent(const wxChar
*name
, const wxKeyEvent
& event
)
1200 long keycode
= event
.GetKeyCode();
1204 case WXK_BACK
: key
= wxT("BACK"); break;
1205 case WXK_TAB
: key
= wxT("TAB"); break;
1206 case WXK_RETURN
: key
= wxT("RETURN"); break;
1207 case WXK_ESCAPE
: key
= wxT("ESCAPE"); break;
1208 case WXK_SPACE
: key
= wxT("SPACE"); break;
1209 case WXK_DELETE
: key
= wxT("DELETE"); break;
1210 case WXK_START
: key
= wxT("START"); break;
1211 case WXK_LBUTTON
: key
= wxT("LBUTTON"); break;
1212 case WXK_RBUTTON
: key
= wxT("RBUTTON"); break;
1213 case WXK_CANCEL
: key
= wxT("CANCEL"); break;
1214 case WXK_MBUTTON
: key
= wxT("MBUTTON"); break;
1215 case WXK_CLEAR
: key
= wxT("CLEAR"); break;
1216 case WXK_SHIFT
: key
= wxT("SHIFT"); break;
1217 case WXK_ALT
: key
= wxT("ALT"); break;
1218 case WXK_CONTROL
: key
= wxT("CONTROL"); break;
1219 case WXK_MENU
: key
= wxT("MENU"); break;
1220 case WXK_PAUSE
: key
= wxT("PAUSE"); break;
1221 case WXK_CAPITAL
: key
= wxT("CAPITAL"); break;
1222 case WXK_END
: key
= wxT("END"); break;
1223 case WXK_HOME
: key
= wxT("HOME"); break;
1224 case WXK_LEFT
: key
= wxT("LEFT"); break;
1225 case WXK_UP
: key
= wxT("UP"); break;
1226 case WXK_RIGHT
: key
= wxT("RIGHT"); break;
1227 case WXK_DOWN
: key
= wxT("DOWN"); break;
1228 case WXK_SELECT
: key
= wxT("SELECT"); break;
1229 case WXK_PRINT
: key
= wxT("PRINT"); break;
1230 case WXK_EXECUTE
: key
= wxT("EXECUTE"); break;
1231 case WXK_SNAPSHOT
: key
= wxT("SNAPSHOT"); break;
1232 case WXK_INSERT
: key
= wxT("INSERT"); break;
1233 case WXK_HELP
: key
= wxT("HELP"); break;
1234 case WXK_NUMPAD0
: key
= wxT("NUMPAD0"); break;
1235 case WXK_NUMPAD1
: key
= wxT("NUMPAD1"); break;
1236 case WXK_NUMPAD2
: key
= wxT("NUMPAD2"); break;
1237 case WXK_NUMPAD3
: key
= wxT("NUMPAD3"); break;
1238 case WXK_NUMPAD4
: key
= wxT("NUMPAD4"); break;
1239 case WXK_NUMPAD5
: key
= wxT("NUMPAD5"); break;
1240 case WXK_NUMPAD6
: key
= wxT("NUMPAD6"); break;
1241 case WXK_NUMPAD7
: key
= wxT("NUMPAD7"); break;
1242 case WXK_NUMPAD8
: key
= wxT("NUMPAD8"); break;
1243 case WXK_NUMPAD9
: key
= wxT("NUMPAD9"); break;
1244 case WXK_MULTIPLY
: key
= wxT("MULTIPLY"); break;
1245 case WXK_ADD
: key
= wxT("ADD"); break;
1246 case WXK_SEPARATOR
: key
= wxT("SEPARATOR"); break;
1247 case WXK_SUBTRACT
: key
= wxT("SUBTRACT"); break;
1248 case WXK_DECIMAL
: key
= wxT("DECIMAL"); break;
1249 case WXK_DIVIDE
: key
= wxT("DIVIDE"); break;
1250 case WXK_F1
: key
= wxT("F1"); break;
1251 case WXK_F2
: key
= wxT("F2"); break;
1252 case WXK_F3
: key
= wxT("F3"); break;
1253 case WXK_F4
: key
= wxT("F4"); break;
1254 case WXK_F5
: key
= wxT("F5"); break;
1255 case WXK_F6
: key
= wxT("F6"); break;
1256 case WXK_F7
: key
= wxT("F7"); break;
1257 case WXK_F8
: key
= wxT("F8"); break;
1258 case WXK_F9
: key
= wxT("F9"); break;
1259 case WXK_F10
: key
= wxT("F10"); break;
1260 case WXK_F11
: key
= wxT("F11"); break;
1261 case WXK_F12
: key
= wxT("F12"); break;
1262 case WXK_F13
: key
= wxT("F13"); break;
1263 case WXK_F14
: key
= wxT("F14"); break;
1264 case WXK_F15
: key
= wxT("F15"); break;
1265 case WXK_F16
: key
= wxT("F16"); break;
1266 case WXK_F17
: key
= wxT("F17"); break;
1267 case WXK_F18
: key
= wxT("F18"); break;
1268 case WXK_F19
: key
= wxT("F19"); break;
1269 case WXK_F20
: key
= wxT("F20"); break;
1270 case WXK_F21
: key
= wxT("F21"); break;
1271 case WXK_F22
: key
= wxT("F22"); break;
1272 case WXK_F23
: key
= wxT("F23"); break;
1273 case WXK_F24
: key
= wxT("F24"); break;
1274 case WXK_NUMLOCK
: key
= wxT("NUMLOCK"); break;
1275 case WXK_SCROLL
: key
= wxT("SCROLL"); break;
1276 case WXK_PAGEUP
: key
= wxT("PAGEUP"); break;
1277 case WXK_PAGEDOWN
: key
= wxT("PAGEDOWN"); break;
1278 case WXK_NUMPAD_SPACE
: key
= wxT("NUMPAD_SPACE"); break;
1279 case WXK_NUMPAD_TAB
: key
= wxT("NUMPAD_TAB"); break;
1280 case WXK_NUMPAD_ENTER
: key
= wxT("NUMPAD_ENTER"); break;
1281 case WXK_NUMPAD_F1
: key
= wxT("NUMPAD_F1"); break;
1282 case WXK_NUMPAD_F2
: key
= wxT("NUMPAD_F2"); break;
1283 case WXK_NUMPAD_F3
: key
= wxT("NUMPAD_F3"); break;
1284 case WXK_NUMPAD_F4
: key
= wxT("NUMPAD_F4"); break;
1285 case WXK_NUMPAD_HOME
: key
= wxT("NUMPAD_HOME"); break;
1286 case WXK_NUMPAD_LEFT
: key
= wxT("NUMPAD_LEFT"); break;
1287 case WXK_NUMPAD_UP
: key
= wxT("NUMPAD_UP"); break;
1288 case WXK_NUMPAD_RIGHT
: key
= wxT("NUMPAD_RIGHT"); break;
1289 case WXK_NUMPAD_DOWN
: key
= wxT("NUMPAD_DOWN"); break;
1290 case WXK_NUMPAD_PAGEUP
: key
= wxT("NUMPAD_PAGEUP"); break;
1291 case WXK_NUMPAD_PAGEDOWN
: key
= wxT("NUMPAD_PAGEDOWN"); break;
1292 case WXK_NUMPAD_END
: key
= wxT("NUMPAD_END"); break;
1293 case WXK_NUMPAD_BEGIN
: key
= wxT("NUMPAD_BEGIN"); break;
1294 case WXK_NUMPAD_INSERT
: key
= wxT("NUMPAD_INSERT"); break;
1295 case WXK_NUMPAD_DELETE
: key
= wxT("NUMPAD_DELETE"); break;
1296 case WXK_NUMPAD_EQUAL
: key
= wxT("NUMPAD_EQUAL"); break;
1297 case WXK_NUMPAD_MULTIPLY
: key
= wxT("NUMPAD_MULTIPLY"); break;
1298 case WXK_NUMPAD_ADD
: key
= wxT("NUMPAD_ADD"); break;
1299 case WXK_NUMPAD_SEPARATOR
: key
= wxT("NUMPAD_SEPARATOR"); break;
1300 case WXK_NUMPAD_SUBTRACT
: key
= wxT("NUMPAD_SUBTRACT"); break;
1301 case WXK_NUMPAD_DECIMAL
: key
= wxT("NUMPAD_DECIMAL"); break;
1305 if ( keycode
< 128 && wxIsprint((int)keycode
) )
1306 key
.Printf(wxT("'%c'"), (char)keycode
);
1307 else if ( keycode
> 0 && keycode
< 27 )
1308 key
.Printf(_("Ctrl-%c"), wxT('A') + keycode
- 1);
1310 key
.Printf(wxT("unknown (%ld)"), keycode
);
1315 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1318 event
.ControlDown() ? wxT('C') : wxT('-'),
1319 event
.AltDown() ? wxT('A') : wxT('-'),
1320 event
.ShiftDown() ? wxT('S') : wxT('-'),
1321 event
.MetaDown() ? wxT('M') : wxT('-'));
1324 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
& event
)
1326 LogKeyEvent(wxT("Tree key down "), event
.GetKeyEvent());
1331 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
1333 // need to explicitly allow drag
1334 if ( event
.GetItem() != GetRootItem() )
1336 m_draggedItem
= event
.GetItem();
1338 wxPoint clientpt
= event
.GetPoint();
1339 wxPoint screenpt
= ClientToScreen(clientpt
);
1341 wxLogMessage(wxT("OnBeginDrag: started dragging %s at screen coords (%i,%i)"),
1342 GetItemText(m_draggedItem
).c_str(),
1343 screenpt
.x
, screenpt
.y
);
1349 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1353 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
1355 wxTreeItemId itemSrc
= m_draggedItem
,
1356 itemDst
= event
.GetItem();
1357 m_draggedItem
= (wxTreeItemId
)0l;
1359 // where to copy the item?
1360 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
1362 // copy to the parent then
1363 itemDst
= GetItemParent(itemDst
);
1366 if ( !itemDst
.IsOk() )
1368 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1373 wxString text
= GetItemText(itemSrc
);
1374 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1375 text
.c_str(), GetItemText(itemDst
).c_str());
1377 // just do append here - we could also insert it just before/after the item
1378 // on which it was dropped, but this requires slightly more work... we also
1379 // completely ignore the client data and icon of the old item but could
1380 // copy them as well.
1382 // Finally, we only copy one item here but we might copy the entire tree if
1383 // we were dragging a folder.
1384 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
1385 wxTreeItemId id
= AppendItem(itemDst
, text
, image
);
1387 if ( wxGetApp().ShowStates() )
1388 SetItemState(id
, GetItemState(itemSrc
));
1391 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
1393 wxLogMessage(wxT("OnBeginLabelEdit"));
1395 // for testing, prevent this item's label editing
1396 wxTreeItemId itemId
= event
.GetItem();
1397 if ( IsTestItem(itemId
) )
1399 wxMessageBox(wxT("You can't edit this item."));
1403 else if ( itemId
== GetRootItem() )
1405 // test that it is possible to change the text of the item being edited
1406 SetItemText(itemId
, _T("Editing root item"));
1410 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
1412 wxLogMessage(wxT("OnEndLabelEdit"));
1414 // don't allow anything except letters in the labels
1415 if ( !event
.GetLabel().IsWord() )
1417 wxMessageBox(wxT("The new label should be a single word."));
1423 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
1425 wxLogMessage(wxT("OnItemCollapsing"));
1427 // for testing, prevent the user from collapsing the first child folder
1428 wxTreeItemId itemId
= event
.GetItem();
1429 if ( IsTestItem(itemId
) )
1431 wxMessageBox(wxT("You can't collapse this item."));
1437 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
1439 // show some info about this item
1440 wxTreeItemId itemId
= event
.GetItem();
1441 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1445 item
->ShowInfo(this);
1448 wxLogMessage(wxT("OnItemActivated"));
1451 void MyTreeCtrl::OnItemStateClick(wxTreeEvent
& event
)
1453 // toggle item state
1454 wxTreeItemId itemId
= event
.GetItem();
1455 DoToggleState(itemId
);
1457 wxLogMessage(wxT("Item \"%s\" state changed to %d"),
1458 GetItemText(itemId
), GetItemState(itemId
));
1461 void MyTreeCtrl::OnItemMenu(wxTreeEvent
& event
)
1463 wxTreeItemId itemId
= event
.GetItem();
1464 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1466 wxPoint clientpt
= event
.GetPoint();
1467 wxPoint screenpt
= ClientToScreen(clientpt
);
1469 wxLogMessage(wxT("OnItemMenu for item \"%s\" at screen coords (%i, %i)"),
1470 item
? item
->GetDesc() : _T(""), screenpt
.x
, screenpt
.y
);
1472 ShowMenu(itemId
, clientpt
);
1476 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1478 wxPoint pt
= event
.GetPosition();
1480 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt
.x
, pt
.y
);
1483 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
1488 title
<< wxT("Menu for ") << GetItemText(id
);
1492 title
= wxT("Menu for no particular item");
1497 menu
.Append(TreeTest_About
, wxT("&About..."));
1498 menu
.AppendSeparator();
1499 menu
.Append(TreeTest_Highlight
, wxT("&Highlight item"));
1500 menu
.Append(TreeTest_Dump
, wxT("&Dump"));
1502 PopupMenu(&menu
, pt
);
1503 #endif // wxUSE_MENUS
1506 void MyTreeCtrl::OnItemRClick(wxTreeEvent
& event
)
1508 wxTreeItemId itemId
= event
.GetItem();
1509 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1512 wxLogMessage(wxT("Item \"%s\" right clicked"), item
? item
->GetDesc()
1518 void MyTreeCtrl::OnRMouseDown(wxMouseEvent
& event
)
1520 wxLogMessage(wxT("Right mouse button down"));
1525 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
1527 wxLogMessage(wxT("Right mouse button up"));
1532 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
1534 wxTreeItemId id
= HitTest(event
.GetPosition());
1536 wxLogMessage(wxT("No item under mouse"));
1539 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
1541 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1547 static inline const wxChar
*Bool2String(bool b
)
1549 return b
? wxT("") : wxT("not ");
1552 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1554 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1555 wxT("%u children (%u immediately under this item)."),
1557 Bool2String(tree
->IsSelected(GetId())),
1558 Bool2String(tree
->IsExpanded(GetId())),
1559 Bool2String(tree
->IsBold(GetId())),
1560 unsigned(tree
->GetChildrenCount(GetId())),
1561 unsigned(tree
->GetChildrenCount(GetId(), false)));