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
53 #include "unchecked.xpm"
54 #include "checked.xpm"
57 #include "../sample.xpm"
60 // verify that the item is ok and insult the user if it is not
61 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
62 wxMessageBox(wxT("Please select some item first!"), \
63 wxT("Tree sample error"), \
64 wxOK | wxICON_EXCLAMATION, \
69 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
71 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
72 EVT_IDLE(MyFrame::OnIdle
)
73 EVT_SIZE(MyFrame::OnSize
)
83 MENU_LINK(TogHideRoot
)
84 MENU_LINK(TogRootLines
)
86 MENU_LINK(TogFullHighlight
)
87 MENU_LINK(SetFgColour
)
88 MENU_LINK(SetBgColour
)
93 #ifndef NO_MULTIPLE_SELECTION
94 MENU_LINK(DumpSelected
)
98 #endif // NO_MULTIPLE_SELECTION
107 MENU_LINK(DeleteChildren
)
110 MENU_LINK(ToggleImages
)
111 MENU_LINK(ToggleStates
)
112 MENU_LINK(ToggleAlternateImages
)
113 MENU_LINK(ToggleAlternateStates
)
114 MENU_LINK(ToggleButtons
)
115 MENU_LINK(SetImageSize
)
116 MENU_LINK(CollapseAndReset
)
117 MENU_LINK(EnsureVisible
)
120 MENU_LINK(InsertItem
)
123 MENU_LINK(IncSpacing
)
124 MENU_LINK(DecSpacing
)
125 MENU_LINK(ToggleIcon
)
126 MENU_LINK(ToggleState
)
127 MENU_LINK(SelectRoot
)
129 MENU_LINK(ShowFirstVisible
)
130 #ifdef wxHAS_LAST_VISIBLE
131 MENU_LINK(ShowLastVisible
)
132 #endif // wxHAS_LAST_VISIBLE
133 MENU_LINK(ShowNextVisible
)
134 MENU_LINK(ShowPrevVisible
)
135 MENU_LINK(ShowParent
)
136 MENU_LINK(ShowPrevSibling
)
137 MENU_LINK(ShowNextSibling
)
142 #if USE_GENERIC_TREECTRL
143 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
145 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
147 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
148 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
149 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
150 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
151 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
152 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
153 #if 0 // there are so many of those that logging them causes flicker
154 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
156 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
157 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
158 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
159 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
160 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
162 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
163 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
164 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
165 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
166 EVT_TREE_STATE_IMAGE_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemStateClick
)
168 // so many different ways to handle right mouse button clicks...
169 EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu
)
170 // EVT_TREE_ITEM_MENU is the preferred event for creating context menus
171 // on a tree control, because it includes the point of the click or item,
172 // meaning that no additional placement calculations are required.
173 EVT_TREE_ITEM_MENU(TreeTest_Ctrl
, MyTreeCtrl::OnItemMenu
)
174 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRClick
)
176 EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown
)
177 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
178 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
185 if ( !wxApp::OnInit() )
188 // Create the main frame window
189 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
199 // My frame constructor
200 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
201 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
207 // This reduces flicker effects - even better would be to define
208 // OnEraseBackground to do nothing. When the tree control's scrollbars are
209 // show or hidden, the frame is sent a background erase event.
210 SetBackgroundColour(wxColour(255, 255, 255));
213 SetIcon(wxICON(sample
));
217 wxMenu
*file_menu
= new wxMenu
,
218 *style_menu
= new wxMenu
,
219 *tree_menu
= new wxMenu
,
220 *item_menu
= new wxMenu
;
222 file_menu
->Append(TreeTest_ClearLog
, wxT("&Clear log\tCtrl-L"));
223 file_menu
->AppendSeparator();
224 file_menu
->Append(TreeTest_About
, wxT("&About..."));
225 file_menu
->AppendSeparator();
226 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
228 style_menu
->AppendCheckItem(TreeTest_TogButtons
, wxT("Toggle &normal buttons"));
229 style_menu
->AppendCheckItem(TreeTest_TogTwist
, wxT("Toggle &twister buttons"));
230 style_menu
->AppendCheckItem(TreeTest_ToggleButtons
, wxT("Toggle image &buttons"));
231 style_menu
->AppendSeparator();
232 style_menu
->AppendCheckItem(TreeTest_TogLines
, wxT("Toggle &connecting lines"));
233 style_menu
->AppendCheckItem(TreeTest_TogRootLines
, wxT("Toggle &lines at root"));
234 style_menu
->AppendCheckItem(TreeTest_TogHideRoot
, wxT("Toggle &hidden root"));
235 style_menu
->AppendCheckItem(TreeTest_TogBorder
, wxT("Toggle &item border"));
236 style_menu
->AppendCheckItem(TreeTest_TogFullHighlight
, wxT("Toggle &full row highlight"));
237 style_menu
->AppendCheckItem(TreeTest_TogEdit
, wxT("Toggle &edit mode"));
238 #ifndef NO_MULTIPLE_SELECTION
239 style_menu
->AppendCheckItem(TreeTest_ToggleSel
, wxT("Toggle &selection mode\tCtrl-S"));
240 #endif // NO_MULTIPLE_SELECTION
241 style_menu
->AppendCheckItem(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
242 style_menu
->AppendCheckItem(TreeTest_ToggleStates
, wxT("Toggle show st&ates"));
243 style_menu
->AppendCheckItem(TreeTest_ToggleAlternateImages
, wxT("Toggle alternate images"));
244 style_menu
->AppendCheckItem(TreeTest_ToggleAlternateStates
, wxT("Toggle alternate state images"));
245 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
246 style_menu
->AppendSeparator();
247 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
248 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
249 style_menu
->AppendSeparator();
250 style_menu
->Append(TreeTest_ResetStyle
, wxT("&Reset to default\tF10"));
252 tree_menu
->Append(TreeTest_Recreate
, wxT("&Recreate the tree"));
253 tree_menu
->Append(TreeTest_CollapseAndReset
, wxT("C&ollapse and reset"));
254 tree_menu
->AppendSeparator();
255 tree_menu
->Append(TreeTest_AddItem
, wxT("Append a &new item"));
256 tree_menu
->Append(TreeTest_InsertItem
, wxT("&Insert a new item"));
257 tree_menu
->Append(TreeTest_Delete
, wxT("&Delete this item"));
258 tree_menu
->Append(TreeTest_DeleteChildren
, wxT("Delete &children"));
259 tree_menu
->Append(TreeTest_DeleteAll
, wxT("Delete &all items"));
260 tree_menu
->Append(TreeTest_SelectRoot
, wxT("Select root item"));
261 tree_menu
->AppendSeparator();
262 tree_menu
->Append(TreeTest_Count
, wxT("Count children of current item"));
263 tree_menu
->Append(TreeTest_CountRec
, wxT("Recursively count children of current item"));
264 tree_menu
->AppendSeparator();
265 tree_menu
->Append(TreeTest_Sort
, wxT("Sort children of current item"));
266 tree_menu
->Append(TreeTest_SortRev
, wxT("Sort in reversed order"));
267 tree_menu
->AppendSeparator();
268 tree_menu
->Append(TreeTest_EnsureVisible
, wxT("Make the last item &visible"));
269 tree_menu
->Append(TreeTest_SetFocus
, wxT("Set &focus to the tree"));
270 tree_menu
->AppendSeparator();
271 tree_menu
->Append(TreeTest_IncIndent
, wxT("Add 5 points to indentation\tAlt-I"));
272 tree_menu
->Append(TreeTest_DecIndent
, wxT("Reduce indentation by 5 points\tAlt-R"));
273 tree_menu
->AppendSeparator();
274 tree_menu
->Append(TreeTest_IncSpacing
, wxT("Add 5 points to spacing\tCtrl-I"));
275 tree_menu
->Append(TreeTest_DecSpacing
, wxT("Reduce spacing by 5 points\tCtrl-R"));
277 item_menu
->Append(TreeTest_Dump
, wxT("&Dump item children"));
278 item_menu
->Append(TreeTest_Rename
, wxT("&Rename item..."));
280 item_menu
->AppendSeparator();
281 item_menu
->Append(TreeTest_SetBold
, wxT("Make item &bold"));
282 item_menu
->Append(TreeTest_ClearBold
, wxT("Make item ¬ bold"));
283 item_menu
->AppendSeparator();
284 item_menu
->Append(TreeTest_ToggleIcon
, wxT("Toggle the item's &icon"));
285 item_menu
->Append(TreeTest_ToggleState
, wxT("Toggle the item's &state"));
286 item_menu
->AppendSeparator();
287 item_menu
->Append(TreeTest_ShowFirstVisible
, wxT("Show &first visible"));
288 #ifdef wxHAS_LAST_VISIBLE
289 item_menu
->Append(TreeTest_ShowLastVisible
, wxT("Show &last visible"));
290 #endif // wxHAS_LAST_VISIBLE
291 item_menu
->Append(TreeTest_ShowNextVisible
, wxT("Show &next visible"));
292 item_menu
->Append(TreeTest_ShowPrevVisible
, wxT("Show &previous visible"));
293 item_menu
->AppendSeparator();
294 item_menu
->Append(TreeTest_ShowParent
, "Show pa&rent");
295 item_menu
->Append(TreeTest_ShowPrevSibling
, "Show &previous sibling");
296 item_menu
->Append(TreeTest_ShowNextSibling
, "Show &next sibling");
298 #ifndef NO_MULTIPLE_SELECTION
299 item_menu
->AppendSeparator();
300 item_menu
->Append(TreeTest_DumpSelected
, wxT("Dump selected items\tAlt-D"));
301 item_menu
->Append(TreeTest_Select
, wxT("Select current item\tAlt-S"));
302 item_menu
->Append(TreeTest_Unselect
, wxT("Unselect everything\tAlt-U"));
303 #endif // NO_MULTIPLE_SELECTION
305 wxMenuBar
*menu_bar
= new wxMenuBar
;
306 menu_bar
->Append(file_menu
, wxT("&File"));
307 menu_bar
->Append(style_menu
, wxT("&Style"));
308 menu_bar
->Append(tree_menu
, wxT("&Tree"));
309 menu_bar
->Append(item_menu
, wxT("&Item"));
310 SetMenuBar(menu_bar
);
311 #endif // wxUSE_MENUS
313 m_panel
= new wxPanel(this);
316 // create the controls
317 m_textCtrl
= new wxTextCtrl(m_panel
, wxID_ANY
, wxT(""),
318 wxDefaultPosition
, wxDefaultSize
,
319 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
322 CreateTreeWithDefStyle();
324 menu_bar
->Check(TreeTest_ToggleImages
, true);
325 menu_bar
->Check(TreeTest_ToggleStates
, true);
326 menu_bar
->Check(TreeTest_ToggleAlternateImages
, false);
327 menu_bar
->Check(TreeTest_ToggleAlternateStates
, false);
330 // create a status bar
332 #endif // wxUSE_STATUSBAR
336 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
337 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
338 delete wxLog::SetActiveTarget(new wxLogStderr
);
340 // set our text control as the log target
341 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
342 delete wxLog::SetActiveTarget(logWindow
);
350 delete wxLog::SetActiveTarget(NULL
);
354 void MyFrame::CreateTreeWithDefStyle()
356 long style
= wxTR_DEFAULT_STYLE
|
357 #ifndef NO_VARIABLE_HEIGHT
358 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
362 CreateTree(style
| wxSUNKEN_BORDER
);
364 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
366 wxMenuBar
*mbar
= GetMenuBar();
367 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_HAS_BUTTONS
) != 0);
368 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_TWIST_BUTTONS
) != 0);
369 mbar
->Check(TreeTest_TogLines
, (style
& wxTR_NO_LINES
) == 0);
370 mbar
->Check(TreeTest_TogRootLines
, (style
& wxTR_LINES_AT_ROOT
) != 0);
371 mbar
->Check(TreeTest_TogHideRoot
, (style
& wxTR_HIDE_ROOT
) != 0);
372 mbar
->Check(TreeTest_TogEdit
, (style
& wxTR_EDIT_LABELS
) != 0);
373 mbar
->Check(TreeTest_TogBorder
, (style
& wxTR_ROW_LINES
) != 0);
374 mbar
->Check(TreeTest_TogFullHighlight
, (style
& wxTR_FULL_ROW_HIGHLIGHT
) != 0);
377 void MyFrame::CreateTree(long style
)
379 m_treeCtrl
= new MyTreeCtrl(m_panel
, TreeTest_Ctrl
,
380 wxDefaultPosition
, wxDefaultSize
,
383 GetMenuBar()->Enable(TreeTest_SelectRoot
, !(style
& wxTR_HIDE_ROOT
));
388 void MyFrame::TogStyle(int id
, long flag
)
390 long style
= m_treeCtrl
->GetWindowStyle() ^ flag
;
392 // most treectrl styles can't be changed on the fly using the native
393 // control and the tree must be recreated
395 m_treeCtrl
->SetWindowStyle(style
);
401 GetMenuBar()->Check(id
, (style
& flag
) != 0);
404 void MyFrame::OnIdle(wxIdleEvent
& event
)
409 wxTreeItemId idRoot
= m_treeCtrl
->GetRootItem();
413 wxTreeItemId idLast
= m_treeCtrl
->GetLastChild(idRoot
);
414 status
= wxString::Format(
415 _T("Root/last item is %svisible/%svisible"),
416 m_treeCtrl
->IsVisible(idRoot
) ? _T("") : _T("not "),
417 idLast
.IsOk() && m_treeCtrl
->IsVisible(idLast
)
418 ? _T("") : _T("not "));
421 status
= _T("No root item");
423 SetStatusText(status
, 1);
425 #endif // wxUSE_STATUSBAR
430 void MyFrame::OnSize(wxSizeEvent
& event
)
444 void MyFrame::Resize()
446 wxSize size
= GetClientSize();
447 m_treeCtrl
->SetSize(0, 0, size
.x
, size
.y
450 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3
455 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
460 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
462 wxMessageBox(wxT("Tree test sample\n")
463 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
464 wxT("About tree test"),
465 wxOK
| wxICON_INFORMATION
, this);
468 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
473 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
475 wxTreeItemId item
= m_treeCtrl
->GetSelection();
479 // old code - now we edit in place
481 static wxString s_text
;
482 s_text
= wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
484 if ( !s_text
.empty() )
486 m_treeCtrl
->SetItemText(item
, s_text
);
490 // TODO demonstrate creating a custom edit control...
491 (void)m_treeCtrl
->EditLabel(item
);
494 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
496 wxTreeItemId item
= m_treeCtrl
->GetSelection();
500 int i
= m_treeCtrl
->GetChildrenCount( item
, false );
502 wxLogMessage(wxT("%d children"), i
);
505 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
507 wxTreeItemId item
= m_treeCtrl
->GetSelection();
511 int i
= m_treeCtrl
->GetChildrenCount( item
);
513 wxLogMessage(wxT("%d children"), i
);
516 void MyFrame::DoSort(bool reverse
)
518 wxTreeItemId item
= m_treeCtrl
->GetSelection();
522 m_treeCtrl
->DoSortChildren(item
, reverse
);
525 void MyFrame::OnHighlight(wxCommandEvent
& WXUNUSED(event
))
527 wxTreeItemId id
= m_treeCtrl
->GetSelection();
532 if ( !m_treeCtrl
->GetBoundingRect(id
, r
, true /* text, not full row */) )
534 wxLogMessage(_T("Failed to get bounding item rect"));
538 wxClientDC
dc(m_treeCtrl
);
540 dc
.SetPen(*wxTRANSPARENT_PEN
);
542 m_treeCtrl
->Update();
545 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
547 wxTreeItemId root
= m_treeCtrl
->GetSelection();
551 m_treeCtrl
->GetItemsRecursively(root
);
554 #ifndef NO_MULTIPLE_SELECTION
556 void MyFrame::OnToggleSel(wxCommandEvent
& event
)
558 TogStyle(event
.GetId(), wxTR_MULTIPLE
);
561 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
563 wxArrayTreeItemIds array
;
565 size_t count
= m_treeCtrl
->GetSelections(array
);
566 wxLogMessage(wxT("%u items selected"), unsigned(count
));
568 for ( size_t n
= 0; n
< count
; n
++ )
570 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
574 void MyFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
576 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
579 void MyFrame::OnSelectRoot(wxCommandEvent
& WXUNUSED(event
))
581 if ( !m_treeCtrl
->HasFlag(wxTR_HIDE_ROOT
) )
582 m_treeCtrl
->SelectItem(m_treeCtrl
->GetRootItem());
585 void MyFrame::OnUnselect(wxCommandEvent
& WXUNUSED(event
))
587 m_treeCtrl
->UnselectAll();
590 #endif // NO_MULTIPLE_SELECTION
592 void MyFrame::DoSetBold(bool bold
)
594 wxTreeItemId item
= m_treeCtrl
->GetSelection();
598 m_treeCtrl
->SetItemBold(item
, bold
);
601 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
603 wxTreeItemId item
= m_treeCtrl
->GetSelection();
607 m_treeCtrl
->Delete(item
);
610 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
612 wxTreeItemId item
= m_treeCtrl
->GetSelection();
616 m_treeCtrl
->DeleteChildren(item
);
619 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
621 m_treeCtrl
->DeleteAllItems();
624 void MyFrame::OnRecreate(wxCommandEvent
& event
)
627 m_treeCtrl
->AddTestItemsToTree(5, 2);
630 void MyFrame::OnSetImageSize(wxCommandEvent
& WXUNUSED(event
))
632 int size
= wxGetNumberFromUser(wxT("Enter the size for the images to use"),
634 wxT("TreeCtrl sample"),
635 m_treeCtrl
->ImageSize());
639 m_treeCtrl
->CreateImageList(size
);
640 wxGetApp().SetShowImages(true);
643 void MyFrame::OnToggleImages(wxCommandEvent
& WXUNUSED(event
))
645 if ( wxGetApp().ShowImages() )
647 m_treeCtrl
->CreateImageList(-1);
648 wxGetApp().SetShowImages(false);
652 m_treeCtrl
->CreateImageList(0);
653 wxGetApp().SetShowImages(true);
657 void MyFrame::OnToggleStates(wxCommandEvent
& WXUNUSED(event
))
659 if ( wxGetApp().ShowStates() )
661 m_treeCtrl
->CreateStateImageList(true);
662 wxGetApp().SetShowStates(false);
666 m_treeCtrl
->CreateStateImageList(false);
667 wxGetApp().SetShowStates(true);
671 void MyFrame::OnToggleAlternateImages(wxCommandEvent
& WXUNUSED(event
))
673 bool alternateImages
= m_treeCtrl
->AlternateImages();
675 m_treeCtrl
->SetAlternateImages(!alternateImages
);
676 m_treeCtrl
->CreateImageList(0);
679 void MyFrame::OnToggleAlternateStates(wxCommandEvent
& WXUNUSED(event
))
681 bool alternateStates
= m_treeCtrl
->AlternateStates();
683 m_treeCtrl
->SetAlternateStates(!alternateStates
);
684 m_treeCtrl
->CreateStateImageList();
686 // normal states < alternate states
687 // so we must reset broken states
688 if ( alternateStates
)
689 m_treeCtrl
->ResetBrokenStateImages();
692 void MyFrame::OnToggleButtons(wxCommandEvent
& WXUNUSED(event
))
694 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
695 if ( wxGetApp().ShowButtons() )
697 m_treeCtrl
->CreateButtonsImageList(-1);
698 wxGetApp().SetShowButtons(false);
702 m_treeCtrl
->CreateButtonsImageList(15);
703 wxGetApp().SetShowButtons(true);
708 void MyFrame::OnCollapseAndReset(wxCommandEvent
& WXUNUSED(event
))
710 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
713 void MyFrame::OnEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
715 m_treeCtrl
->DoEnsureVisible();
718 void MyFrame::OnSetFocus(wxCommandEvent
& WXUNUSED(event
))
720 m_treeCtrl
->SetFocus();
723 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
725 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
726 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, wxT("2nd item"));
729 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
731 static int s_num
= 0;
734 text
.Printf(wxT("Item #%d"), ++s_num
);
736 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
738 MyTreeCtrl::TreeCtrlIcon_File */ );
741 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
743 unsigned int indent
= m_treeCtrl
->GetIndent();
745 m_treeCtrl
->SetIndent( indent
+5 );
748 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
750 unsigned int indent
= m_treeCtrl
->GetIndent();
752 m_treeCtrl
->SetIndent( indent
-5 );
755 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
757 unsigned int indent
= m_treeCtrl
->GetSpacing();
759 m_treeCtrl
->SetSpacing( indent
+5 );
762 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
764 unsigned int indent
= m_treeCtrl
->GetSpacing();
766 m_treeCtrl
->SetSpacing( indent
-5 );
769 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
771 wxTreeItemId item
= m_treeCtrl
->GetSelection();
775 m_treeCtrl
->DoToggleIcon(item
);
778 void MyFrame::OnToggleState(wxCommandEvent
& WXUNUSED(event
))
780 wxTreeItemId item
= m_treeCtrl
->GetSelection();
784 m_treeCtrl
->DoToggleState(item
);
787 void MyFrame::DoShowFirstOrLast(TreeFunc0_t pfn
, const wxString
& label
)
789 const wxTreeItemId item
= (m_treeCtrl
->*pfn
)();
792 wxLogMessage("There is no %s item", label
);
794 wxLogMessage("The %s item is \"%s\"",
795 label
, m_treeCtrl
->GetItemText(item
));
798 void MyFrame::DoShowRelativeItem(TreeFunc1_t pfn
, const wxString
& label
)
800 wxTreeItemId item
= m_treeCtrl
->GetSelection();
804 if ((pfn
== (TreeFunc1_t
) &wxTreeCtrl::GetPrevVisible
805 || pfn
== (TreeFunc1_t
) &wxTreeCtrl::GetNextVisible
)
806 && !m_treeCtrl
->IsVisible(item
))
808 wxLogMessage("The selected item must be visible.");
812 wxTreeItemId new_item
= (m_treeCtrl
->*pfn
)(item
);
814 if ( !new_item
.IsOk() )
815 wxLogMessage("There is no %s item", label
);
817 wxLogMessage("The %s item is \"%s\"",
818 label
, m_treeCtrl
->GetItemText(new_item
));
821 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
823 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
825 m_treeCtrl
->SetForegroundColour(col
);
828 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
830 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
832 m_treeCtrl
->SetBackgroundColour(col
);
835 // MyTreeCtrl implementation
836 #if USE_GENERIC_TREECTRL
837 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
839 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
842 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
843 const wxPoint
& pos
, const wxSize
& size
,
845 : wxTreeCtrl(parent
, id
, pos
, size
, style
),
846 m_alternateImages(false),
847 m_alternateStates(false)
849 m_reverseSort
= false;
852 CreateStateImageList();
854 // Add some items to the tree
855 AddTestItemsToTree(5, 2);
858 void MyTreeCtrl::CreateImageList(int size
)
870 // Make an image list containing small icons
871 wxImageList
*images
= new wxImageList(size
, size
, true);
873 // should correspond to TreeCtrlIcon_xxx enum
877 if (m_alternateImages
)
879 icons
[TreeCtrlIcon_File
] = wxIcon(icon1_xpm
);
880 icons
[TreeCtrlIcon_FileSelected
] = wxIcon(icon2_xpm
);
881 icons
[TreeCtrlIcon_Folder
] = wxIcon(icon3_xpm
);
882 icons
[TreeCtrlIcon_FolderSelected
] = wxIcon(icon4_xpm
);
883 icons
[TreeCtrlIcon_FolderOpened
] = wxIcon(icon5_xpm
);
887 wxSize
iconSize(size
, size
);
889 icons
[TreeCtrlIcon_File
] =
890 icons
[TreeCtrlIcon_FileSelected
] = wxArtProvider::GetIcon(wxART_NORMAL_FILE
, wxART_LIST
, iconSize
);
891 icons
[TreeCtrlIcon_Folder
] =
892 icons
[TreeCtrlIcon_FolderSelected
] =
893 icons
[TreeCtrlIcon_FolderOpened
] = wxArtProvider::GetIcon(wxART_FOLDER
, wxART_LIST
, iconSize
);
896 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
898 int sizeOrig
= icons
[0].GetWidth();
899 if ( size
== sizeOrig
)
901 images
->Add(icons
[i
]);
905 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
909 AssignImageList(images
);
912 void MyTreeCtrl::CreateStateImageList(bool del
)
916 SetStateImageList(NULL
);
923 if (m_alternateStates
)
926 icons
[0] = wxIcon(state1_xpm
); // yellow
927 icons
[1] = wxIcon(state2_xpm
); // green
928 icons
[2] = wxIcon(state3_xpm
); // red
929 icons
[3] = wxIcon(state4_xpm
); // blue
930 icons
[4] = wxIcon(state5_xpm
); // black
932 int width
= icons
[0].GetWidth(),
933 height
= icons
[0].GetHeight();
935 // Make an state image list containing small icons
936 states
= new wxImageList(width
, height
, true);
938 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
939 states
->Add(icons
[i
]);
944 icons
[0] = wxIcon(unchecked_xpm
);
945 icons
[1] = wxIcon(checked_xpm
);
947 int width
= icons
[0].GetWidth(),
948 height
= icons
[0].GetHeight();
950 // Make an state image list containing small icons
951 states
= new wxImageList(width
, height
, true);
953 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
954 states
->Add(icons
[i
]);
957 AssignStateImageList(states
);
960 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
961 void MyTreeCtrl::CreateButtonsImageList(int size
)
965 SetButtonsImageList(NULL
);
969 // Make an image list containing small icons
970 wxImageList
*images
= new wxImageList(size
, size
, true);
972 // should correspond to TreeCtrlIcon_xxx enum
976 if (m_alternateImages
)
978 icons
[0] = wxIcon(icon3_xpm
); // closed
979 icons
[1] = wxIcon(icon3_xpm
); // closed, selected
980 icons
[2] = wxIcon(icon5_xpm
); // open
981 icons
[3] = wxIcon(icon5_xpm
); // open, selected
985 wxSize
iconSize(size
, size
);
988 icons
[1] = wxArtProvider::GetIcon(wxART_FOLDER
, wxART_LIST
, iconSize
); // closed, selected
990 icons
[3] = wxArtProvider::GetIcon(wxART_FOLDER_OPEN
, wxART_LIST
, iconSize
);// open, selected
993 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
995 int sizeOrig
= icons
[i
].GetWidth();
996 if ( size
== sizeOrig
)
998 images
->Add(icons
[i
]);
1002 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
1006 AssignButtonsImageList(images
);
1008 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size
))
1013 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1014 const wxTreeItemId
& item2
)
1016 if ( m_reverseSort
)
1018 // just exchange 1st and 2nd items
1019 return wxTreeCtrl::OnCompareItems(item2
, item1
);
1023 return wxTreeCtrl::OnCompareItems(item1
, item2
);
1027 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
1034 bool hasChildren
= depth
> 1;
1037 for ( size_t n
= 0; n
< numChildren
; n
++ )
1039 // at depth 1 elements won't have any more children
1041 str
.Printf(wxT("%s child %u"), wxT("Folder"), unsigned(n
+ 1));
1043 str
.Printf(wxT("%s child %u.%u"), wxT("File"), unsigned(folder
), unsigned(n
+ 1));
1045 // here we pass to AppendItem() normal and selected item images (we
1046 // suppose that selected image follows the normal one in the enum)
1047 int image
, imageSel
;
1048 if ( wxGetApp().ShowImages() )
1050 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
1051 imageSel
= image
+ 1;
1055 image
= imageSel
= -1;
1057 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
1058 new MyTreeItemData(str
));
1060 if ( wxGetApp().ShowStates() )
1061 SetItemState(id
, 0);
1063 // and now we also set the expanded one (only for the folders)
1064 if ( hasChildren
&& wxGetApp().ShowImages() )
1066 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
1067 wxTreeItemIcon_Expanded
);
1070 // remember the last child for OnEnsureVisible()
1071 if ( !hasChildren
&& n
== numChildren
- 1 )
1076 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
1082 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
1085 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
1086 wxTreeItemId rootId
= AddRoot(wxT("Root"),
1088 new MyTreeItemData(wxT("Root item")));
1089 if ( !HasFlag(wxTR_HIDE_ROOT
) && image
!= -1 )
1091 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
1094 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
1096 // set some colours/fonts for testing
1097 if ( !HasFlag(wxTR_HIDE_ROOT
) )
1098 SetItemFont(rootId
, *wxITALIC_FONT
);
1100 wxTreeItemIdValue cookie
;
1101 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
1102 SetItemTextColour(id
, *wxBLUE
);
1104 id
= GetNextChild(rootId
, cookie
);
1105 id
= GetNextChild(rootId
, cookie
);
1106 SetItemTextColour(id
, *wxRED
);
1107 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
1110 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
,
1111 wxTreeItemIdValue cookie
)
1116 id
= GetFirstChild(idParent
, cookie
);
1118 id
= GetNextChild(idParent
, cookie
);
1123 wxString text
= GetItemText(id
);
1126 if (ItemHasChildren(id
))
1127 GetItemsRecursively(id
);
1129 GetItemsRecursively(idParent
, cookie
);
1132 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
1134 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
1136 : TreeCtrlIcon_Folder
;
1137 SetItemImage(item
, image
, wxTreeItemIcon_Normal
);
1139 image
= GetItemImage(item
, wxTreeItemIcon_Selected
) == TreeCtrlIcon_FolderSelected
1140 ? TreeCtrlIcon_FileSelected
1141 : TreeCtrlIcon_FolderSelected
;
1142 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1145 void MyTreeCtrl::DoToggleState(const wxTreeItemId
& item
)
1147 if ( m_alternateStates
)
1149 // sets random state unlike current
1150 int state
= GetItemState(item
);
1155 nState
= rand() % GetStateImageList()->GetImageCount();
1156 } while (nState
== state
);
1158 SetItemState(item
, nState
);
1162 // we have only 2 checkbox states, so next state will be reversed
1163 SetItemState(item
, wxTREE_ITEMSTATE_NEXT
);
1167 void MyTreeCtrl::DoResetBrokenStateImages(const wxTreeItemId
& idParent
,
1168 wxTreeItemIdValue cookie
, int state
)
1173 id
= GetFirstChild(idParent
, cookie
);
1175 id
= GetNextChild(idParent
, cookie
);
1180 int curState
= GetItemState(id
);
1181 if ( curState
!= wxTREE_ITEMSTATE_NONE
&& curState
> state
)
1182 SetItemState(id
, state
);
1184 if (ItemHasChildren(id
))
1185 DoResetBrokenStateImages(id
, 0, state
);
1187 DoResetBrokenStateImages(idParent
, cookie
, state
);
1190 void MyTreeCtrl::LogEvent(const wxChar
*name
, const wxTreeEvent
& event
)
1192 wxTreeItemId item
= event
.GetItem();
1195 text
<< _T('"') << GetItemText(item
).c_str() << _T('"');
1197 text
= _T("invalid item");
1198 wxLogMessage(wxT("%s(%s)"), name
, text
.c_str());
1202 #define TREE_EVENT_HANDLER(name) \
1203 void MyTreeCtrl::name(wxTreeEvent& event) \
1205 LogEvent(_T(#name), event); \
1206 SetLastItem(wxTreeItemId()); \
1210 TREE_EVENT_HANDLER(OnBeginRDrag
)
1211 TREE_EVENT_HANDLER(OnDeleteItem
)
1212 TREE_EVENT_HANDLER(OnGetInfo
)
1213 TREE_EVENT_HANDLER(OnSetInfo
)
1214 TREE_EVENT_HANDLER(OnItemExpanded
)
1215 TREE_EVENT_HANDLER(OnItemExpanding
)
1216 TREE_EVENT_HANDLER(OnItemCollapsed
)
1217 TREE_EVENT_HANDLER(OnSelChanged
)
1218 TREE_EVENT_HANDLER(OnSelChanging
)
1220 #undef TREE_EVENT_HANDLER
1222 void LogKeyEvent(const wxChar
*name
, const wxKeyEvent
& event
)
1225 long keycode
= event
.GetKeyCode();
1229 case WXK_BACK
: key
= wxT("BACK"); break;
1230 case WXK_TAB
: key
= wxT("TAB"); break;
1231 case WXK_RETURN
: key
= wxT("RETURN"); break;
1232 case WXK_ESCAPE
: key
= wxT("ESCAPE"); break;
1233 case WXK_SPACE
: key
= wxT("SPACE"); break;
1234 case WXK_DELETE
: key
= wxT("DELETE"); break;
1235 case WXK_START
: key
= wxT("START"); break;
1236 case WXK_LBUTTON
: key
= wxT("LBUTTON"); break;
1237 case WXK_RBUTTON
: key
= wxT("RBUTTON"); break;
1238 case WXK_CANCEL
: key
= wxT("CANCEL"); break;
1239 case WXK_MBUTTON
: key
= wxT("MBUTTON"); break;
1240 case WXK_CLEAR
: key
= wxT("CLEAR"); break;
1241 case WXK_SHIFT
: key
= wxT("SHIFT"); break;
1242 case WXK_ALT
: key
= wxT("ALT"); break;
1243 case WXK_CONTROL
: key
= wxT("CONTROL"); break;
1244 case WXK_MENU
: key
= wxT("MENU"); break;
1245 case WXK_PAUSE
: key
= wxT("PAUSE"); break;
1246 case WXK_CAPITAL
: key
= wxT("CAPITAL"); break;
1247 case WXK_END
: key
= wxT("END"); break;
1248 case WXK_HOME
: key
= wxT("HOME"); break;
1249 case WXK_LEFT
: key
= wxT("LEFT"); break;
1250 case WXK_UP
: key
= wxT("UP"); break;
1251 case WXK_RIGHT
: key
= wxT("RIGHT"); break;
1252 case WXK_DOWN
: key
= wxT("DOWN"); break;
1253 case WXK_SELECT
: key
= wxT("SELECT"); break;
1254 case WXK_PRINT
: key
= wxT("PRINT"); break;
1255 case WXK_EXECUTE
: key
= wxT("EXECUTE"); break;
1256 case WXK_SNAPSHOT
: key
= wxT("SNAPSHOT"); break;
1257 case WXK_INSERT
: key
= wxT("INSERT"); break;
1258 case WXK_HELP
: key
= wxT("HELP"); break;
1259 case WXK_NUMPAD0
: key
= wxT("NUMPAD0"); break;
1260 case WXK_NUMPAD1
: key
= wxT("NUMPAD1"); break;
1261 case WXK_NUMPAD2
: key
= wxT("NUMPAD2"); break;
1262 case WXK_NUMPAD3
: key
= wxT("NUMPAD3"); break;
1263 case WXK_NUMPAD4
: key
= wxT("NUMPAD4"); break;
1264 case WXK_NUMPAD5
: key
= wxT("NUMPAD5"); break;
1265 case WXK_NUMPAD6
: key
= wxT("NUMPAD6"); break;
1266 case WXK_NUMPAD7
: key
= wxT("NUMPAD7"); break;
1267 case WXK_NUMPAD8
: key
= wxT("NUMPAD8"); break;
1268 case WXK_NUMPAD9
: key
= wxT("NUMPAD9"); break;
1269 case WXK_MULTIPLY
: key
= wxT("MULTIPLY"); break;
1270 case WXK_ADD
: key
= wxT("ADD"); break;
1271 case WXK_SEPARATOR
: key
= wxT("SEPARATOR"); break;
1272 case WXK_SUBTRACT
: key
= wxT("SUBTRACT"); break;
1273 case WXK_DECIMAL
: key
= wxT("DECIMAL"); break;
1274 case WXK_DIVIDE
: key
= wxT("DIVIDE"); break;
1275 case WXK_F1
: key
= wxT("F1"); break;
1276 case WXK_F2
: key
= wxT("F2"); break;
1277 case WXK_F3
: key
= wxT("F3"); break;
1278 case WXK_F4
: key
= wxT("F4"); break;
1279 case WXK_F5
: key
= wxT("F5"); break;
1280 case WXK_F6
: key
= wxT("F6"); break;
1281 case WXK_F7
: key
= wxT("F7"); break;
1282 case WXK_F8
: key
= wxT("F8"); break;
1283 case WXK_F9
: key
= wxT("F9"); break;
1284 case WXK_F10
: key
= wxT("F10"); break;
1285 case WXK_F11
: key
= wxT("F11"); break;
1286 case WXK_F12
: key
= wxT("F12"); break;
1287 case WXK_F13
: key
= wxT("F13"); break;
1288 case WXK_F14
: key
= wxT("F14"); break;
1289 case WXK_F15
: key
= wxT("F15"); break;
1290 case WXK_F16
: key
= wxT("F16"); break;
1291 case WXK_F17
: key
= wxT("F17"); break;
1292 case WXK_F18
: key
= wxT("F18"); break;
1293 case WXK_F19
: key
= wxT("F19"); break;
1294 case WXK_F20
: key
= wxT("F20"); break;
1295 case WXK_F21
: key
= wxT("F21"); break;
1296 case WXK_F22
: key
= wxT("F22"); break;
1297 case WXK_F23
: key
= wxT("F23"); break;
1298 case WXK_F24
: key
= wxT("F24"); break;
1299 case WXK_NUMLOCK
: key
= wxT("NUMLOCK"); break;
1300 case WXK_SCROLL
: key
= wxT("SCROLL"); break;
1301 case WXK_PAGEUP
: key
= wxT("PAGEUP"); break;
1302 case WXK_PAGEDOWN
: key
= wxT("PAGEDOWN"); break;
1303 case WXK_NUMPAD_SPACE
: key
= wxT("NUMPAD_SPACE"); break;
1304 case WXK_NUMPAD_TAB
: key
= wxT("NUMPAD_TAB"); break;
1305 case WXK_NUMPAD_ENTER
: key
= wxT("NUMPAD_ENTER"); break;
1306 case WXK_NUMPAD_F1
: key
= wxT("NUMPAD_F1"); break;
1307 case WXK_NUMPAD_F2
: key
= wxT("NUMPAD_F2"); break;
1308 case WXK_NUMPAD_F3
: key
= wxT("NUMPAD_F3"); break;
1309 case WXK_NUMPAD_F4
: key
= wxT("NUMPAD_F4"); break;
1310 case WXK_NUMPAD_HOME
: key
= wxT("NUMPAD_HOME"); break;
1311 case WXK_NUMPAD_LEFT
: key
= wxT("NUMPAD_LEFT"); break;
1312 case WXK_NUMPAD_UP
: key
= wxT("NUMPAD_UP"); break;
1313 case WXK_NUMPAD_RIGHT
: key
= wxT("NUMPAD_RIGHT"); break;
1314 case WXK_NUMPAD_DOWN
: key
= wxT("NUMPAD_DOWN"); break;
1315 case WXK_NUMPAD_PAGEUP
: key
= wxT("NUMPAD_PAGEUP"); break;
1316 case WXK_NUMPAD_PAGEDOWN
: key
= wxT("NUMPAD_PAGEDOWN"); break;
1317 case WXK_NUMPAD_END
: key
= wxT("NUMPAD_END"); break;
1318 case WXK_NUMPAD_BEGIN
: key
= wxT("NUMPAD_BEGIN"); break;
1319 case WXK_NUMPAD_INSERT
: key
= wxT("NUMPAD_INSERT"); break;
1320 case WXK_NUMPAD_DELETE
: key
= wxT("NUMPAD_DELETE"); break;
1321 case WXK_NUMPAD_EQUAL
: key
= wxT("NUMPAD_EQUAL"); break;
1322 case WXK_NUMPAD_MULTIPLY
: key
= wxT("NUMPAD_MULTIPLY"); break;
1323 case WXK_NUMPAD_ADD
: key
= wxT("NUMPAD_ADD"); break;
1324 case WXK_NUMPAD_SEPARATOR
: key
= wxT("NUMPAD_SEPARATOR"); break;
1325 case WXK_NUMPAD_SUBTRACT
: key
= wxT("NUMPAD_SUBTRACT"); break;
1326 case WXK_NUMPAD_DECIMAL
: key
= wxT("NUMPAD_DECIMAL"); break;
1330 if ( keycode
< 128 && wxIsprint((int)keycode
) )
1331 key
.Printf(wxT("'%c'"), (char)keycode
);
1332 else if ( keycode
> 0 && keycode
< 27 )
1333 key
.Printf(_("Ctrl-%c"), wxT('A') + keycode
- 1);
1335 key
.Printf(wxT("unknown (%ld)"), keycode
);
1340 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1343 event
.ControlDown() ? wxT('C') : wxT('-'),
1344 event
.AltDown() ? wxT('A') : wxT('-'),
1345 event
.ShiftDown() ? wxT('S') : wxT('-'),
1346 event
.MetaDown() ? wxT('M') : wxT('-'));
1349 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
& event
)
1351 LogKeyEvent(wxT("Tree key down "), event
.GetKeyEvent());
1356 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
1358 // need to explicitly allow drag
1359 if ( event
.GetItem() != GetRootItem() )
1361 m_draggedItem
= event
.GetItem();
1363 wxPoint clientpt
= event
.GetPoint();
1364 wxPoint screenpt
= ClientToScreen(clientpt
);
1366 wxLogMessage(wxT("OnBeginDrag: started dragging %s at screen coords (%i,%i)"),
1367 GetItemText(m_draggedItem
).c_str(),
1368 screenpt
.x
, screenpt
.y
);
1374 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1378 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
1380 wxTreeItemId itemSrc
= m_draggedItem
,
1381 itemDst
= event
.GetItem();
1382 m_draggedItem
= (wxTreeItemId
)0l;
1384 // where to copy the item?
1385 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
1387 // copy to the parent then
1388 itemDst
= GetItemParent(itemDst
);
1391 if ( !itemDst
.IsOk() )
1393 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1398 wxString text
= GetItemText(itemSrc
);
1399 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1400 text
.c_str(), GetItemText(itemDst
).c_str());
1402 // just do append here - we could also insert it just before/after the item
1403 // on which it was dropped, but this requires slightly more work... we also
1404 // completely ignore the client data and icon of the old item but could
1405 // copy them as well.
1407 // Finally, we only copy one item here but we might copy the entire tree if
1408 // we were dragging a folder.
1409 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
1410 wxTreeItemId id
= AppendItem(itemDst
, text
, image
);
1412 if ( wxGetApp().ShowStates() )
1413 SetItemState(id
, GetItemState(itemSrc
));
1416 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
1418 wxLogMessage(wxT("OnBeginLabelEdit"));
1420 // for testing, prevent this item's label editing
1421 wxTreeItemId itemId
= event
.GetItem();
1422 if ( IsTestItem(itemId
) )
1424 wxMessageBox(wxT("You can't edit this item."));
1428 else if ( itemId
== GetRootItem() )
1430 // test that it is possible to change the text of the item being edited
1431 SetItemText(itemId
, _T("Editing root item"));
1435 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
1437 wxLogMessage(wxT("OnEndLabelEdit"));
1439 // don't allow anything except letters in the labels
1440 if ( !event
.GetLabel().IsWord() )
1442 wxMessageBox(wxT("The new label should be a single word."));
1448 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
1450 wxLogMessage(wxT("OnItemCollapsing"));
1452 // for testing, prevent the user from collapsing the first child folder
1453 wxTreeItemId itemId
= event
.GetItem();
1454 if ( IsTestItem(itemId
) )
1456 wxMessageBox(wxT("You can't collapse this item."));
1462 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
1464 // show some info about this item
1465 wxTreeItemId itemId
= event
.GetItem();
1466 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1470 item
->ShowInfo(this);
1473 wxLogMessage(wxT("OnItemActivated"));
1476 void MyTreeCtrl::OnItemStateClick(wxTreeEvent
& event
)
1478 // toggle item state
1479 wxTreeItemId itemId
= event
.GetItem();
1480 DoToggleState(itemId
);
1482 wxLogMessage(wxT("Item \"%s\" state changed to %d"),
1483 GetItemText(itemId
), GetItemState(itemId
));
1486 void MyTreeCtrl::OnItemMenu(wxTreeEvent
& event
)
1488 wxTreeItemId itemId
= event
.GetItem();
1489 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1491 wxPoint clientpt
= event
.GetPoint();
1492 wxPoint screenpt
= ClientToScreen(clientpt
);
1494 wxLogMessage(wxT("OnItemMenu for item \"%s\" at screen coords (%i, %i)"),
1495 item
? item
->GetDesc() : _T(""), screenpt
.x
, screenpt
.y
);
1497 ShowMenu(itemId
, clientpt
);
1501 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1503 wxPoint pt
= event
.GetPosition();
1505 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt
.x
, pt
.y
);
1510 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
1515 title
<< wxT("Menu for ") << GetItemText(id
);
1519 title
= wxT("Menu for no particular item");
1524 menu
.Append(TreeTest_About
, wxT("&About..."));
1525 menu
.AppendSeparator();
1526 menu
.Append(TreeTest_Highlight
, wxT("&Highlight item"));
1527 menu
.Append(TreeTest_Dump
, wxT("&Dump"));
1529 PopupMenu(&menu
, pt
);
1530 #endif // wxUSE_MENUS
1533 void MyTreeCtrl::OnItemRClick(wxTreeEvent
& event
)
1535 wxTreeItemId itemId
= event
.GetItem();
1536 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1539 wxLogMessage(wxT("Item \"%s\" right clicked"), item
? item
->GetDesc()
1545 void MyTreeCtrl::OnRMouseDown(wxMouseEvent
& event
)
1547 wxLogMessage(wxT("Right mouse button down"));
1552 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
1554 wxLogMessage(wxT("Right mouse button up"));
1559 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
1561 wxTreeItemId id
= HitTest(event
.GetPosition());
1563 wxLogMessage(wxT("No item under mouse"));
1566 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
1568 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1574 static inline const wxChar
*Bool2String(bool b
)
1576 return b
? wxT("") : wxT("not ");
1579 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1581 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1582 wxT("%u children (%u immediately under this item)."),
1584 Bool2String(tree
->IsSelected(GetId())),
1585 Bool2String(tree
->IsExpanded(GetId())),
1586 Bool2String(tree
->IsBold(GetId())),
1587 unsigned(tree
->GetChildrenCount(GetId())),
1588 unsigned(tree
->GetChildrenCount(GetId(), false)));