1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
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"
56 #ifndef wxHAS_IMAGES_IN_RESOURCES
57 #include "../sample.xpm"
60 static const int NUM_CHILDREN_PER_LEVEL
= 5;
61 static const int NUM_LEVELS
= 2;
63 // verify that the item is ok and insult the user if it is not
64 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
65 wxMessageBox(wxT("Please select some item first!"), \
66 wxT("Tree sample error"), \
67 wxOK | wxICON_EXCLAMATION, \
72 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
74 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
75 EVT_IDLE(MyFrame::OnIdle
)
76 EVT_SIZE(MyFrame::OnSize
)
86 MENU_LINK(TogHideRoot
)
87 MENU_LINK(TogRootLines
)
89 MENU_LINK(TogFullHighlight
)
90 MENU_LINK(SetFgColour
)
91 MENU_LINK(SetBgColour
)
96 #ifndef NO_MULTIPLE_SELECTION
97 MENU_LINK(DumpSelected
)
101 MENU_LINK(SelectChildren
)
102 #endif // NO_MULTIPLE_SELECTION
111 MENU_LINK(DeleteChildren
)
114 MENU_LINK(ToggleImages
)
115 MENU_LINK(ToggleStates
)
116 MENU_LINK(ToggleBell
)
117 MENU_LINK(ToggleAlternateImages
)
118 MENU_LINK(ToggleAlternateStates
)
119 MENU_LINK(ToggleButtons
)
120 MENU_LINK(SetImageSize
)
121 MENU_LINK(CollapseAndReset
)
122 MENU_LINK(EnsureVisible
)
125 MENU_LINK(InsertItem
)
128 MENU_LINK(IncSpacing
)
129 MENU_LINK(DecSpacing
)
130 MENU_LINK(ToggleIcon
)
131 MENU_LINK(ToggleState
)
132 MENU_LINK(SelectRoot
)
133 MENU_LINK(SetFocusedRoot
)
134 MENU_LINK(ClearFocused
)
136 MENU_LINK(ShowFirstVisible
)
137 #ifdef wxHAS_LAST_VISIBLE
138 MENU_LINK(ShowLastVisible
)
139 #endif // wxHAS_LAST_VISIBLE
140 MENU_LINK(ShowNextVisible
)
141 MENU_LINK(ShowPrevVisible
)
142 MENU_LINK(ShowParent
)
143 MENU_LINK(ShowPrevSibling
)
144 MENU_LINK(ShowNextSibling
)
146 MENU_LINK(SelectLast
)
151 #if USE_GENERIC_TREECTRL
152 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
154 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
156 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
157 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
158 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
159 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
160 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
161 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
162 #if 0 // there are so many of those that logging them causes flicker
163 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
165 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
166 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
167 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
168 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
169 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
171 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
172 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
173 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
174 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
175 EVT_TREE_STATE_IMAGE_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemStateClick
)
177 // so many different ways to handle right mouse button clicks...
178 EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu
)
179 // EVT_TREE_ITEM_MENU is the preferred event for creating context menus
180 // on a tree control, because it includes the point of the click or item,
181 // meaning that no additional placement calculations are required.
182 EVT_TREE_ITEM_MENU(TreeTest_Ctrl
, MyTreeCtrl::OnItemMenu
)
183 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRClick
)
185 EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown
)
186 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
187 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
194 if ( !wxApp::OnInit() )
197 // Create the main frame window
198 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
207 // My frame constructor
208 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
209 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
215 // This reduces flicker effects - even better would be to define
216 // OnEraseBackground to do nothing. When the tree control's scrollbars are
217 // show or hidden, the frame is sent a background erase event.
218 SetBackgroundColour(*wxWHITE
);
221 SetIcon(wxICON(sample
));
225 wxMenu
*file_menu
= new wxMenu
,
226 *style_menu
= new wxMenu
,
227 *tree_menu
= new wxMenu
,
228 *item_menu
= new wxMenu
;
230 file_menu
->Append(TreeTest_ClearLog
, wxT("&Clear log\tCtrl-L"));
231 file_menu
->AppendSeparator();
232 file_menu
->Append(TreeTest_About
, wxT("&About"));
233 file_menu
->AppendSeparator();
234 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
236 style_menu
->AppendCheckItem(TreeTest_TogButtons
, wxT("Toggle &normal buttons"));
237 style_menu
->AppendCheckItem(TreeTest_TogTwist
, wxT("Toggle &twister buttons"));
238 style_menu
->AppendCheckItem(TreeTest_ToggleButtons
, wxT("Toggle image &buttons"));
239 style_menu
->AppendSeparator();
240 style_menu
->AppendCheckItem(TreeTest_TogLines
, wxT("Toggle &connecting lines"));
241 style_menu
->AppendCheckItem(TreeTest_TogRootLines
, wxT("Toggle &lines at root"));
242 style_menu
->AppendCheckItem(TreeTest_TogHideRoot
, wxT("Toggle &hidden root"));
243 style_menu
->AppendCheckItem(TreeTest_TogBorder
, wxT("Toggle &item border"));
244 style_menu
->AppendCheckItem(TreeTest_TogFullHighlight
, wxT("Toggle &full row highlight"));
245 style_menu
->AppendCheckItem(TreeTest_TogEdit
, wxT("Toggle &edit mode"));
246 #ifndef NO_MULTIPLE_SELECTION
247 style_menu
->AppendCheckItem(TreeTest_ToggleSel
, wxT("Toggle &selection mode\tCtrl-S"));
248 #endif // NO_MULTIPLE_SELECTION
249 style_menu
->AppendCheckItem(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
250 style_menu
->AppendCheckItem(TreeTest_ToggleStates
, wxT("Toggle show st&ates"));
251 style_menu
->AppendCheckItem(TreeTest_ToggleBell
, wxT("Toggle &bell on no match"));
252 style_menu
->AppendCheckItem(TreeTest_ToggleAlternateImages
, wxT("Toggle alternate images"));
253 style_menu
->AppendCheckItem(TreeTest_ToggleAlternateStates
, wxT("Toggle alternate state images"));
254 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
255 style_menu
->AppendSeparator();
256 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
257 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
258 style_menu
->AppendSeparator();
259 style_menu
->Append(TreeTest_ResetStyle
, wxT("&Reset to default\tF10"));
261 tree_menu
->Append(TreeTest_Recreate
, wxT("&Recreate the tree"));
262 tree_menu
->Append(TreeTest_CollapseAndReset
, wxT("C&ollapse and reset"));
263 tree_menu
->AppendSeparator();
264 tree_menu
->Append(TreeTest_AddItem
, wxT("Append a &new item"));
265 tree_menu
->Append(TreeTest_InsertItem
, wxT("&Insert a new item"));
266 tree_menu
->Append(TreeTest_Delete
, wxT("&Delete this item"));
267 tree_menu
->Append(TreeTest_DeleteChildren
, wxT("Delete &children"));
268 tree_menu
->Append(TreeTest_DeleteAll
, wxT("Delete &all items"));
269 tree_menu
->Append(TreeTest_SelectRoot
, wxT("Select root item"));
270 tree_menu
->AppendSeparator();
271 tree_menu
->Append(TreeTest_SetFocusedRoot
, wxT("Set focus to root item"));
272 tree_menu
->Append(TreeTest_ClearFocused
, wxT("Reset focus"));
274 tree_menu
->AppendSeparator();
275 tree_menu
->Append(TreeTest_Count
, wxT("Count children of current item"));
276 tree_menu
->Append(TreeTest_CountRec
, wxT("Recursively count children of current item"));
277 tree_menu
->AppendSeparator();
278 tree_menu
->Append(TreeTest_Sort
, wxT("Sort children of current item"));
279 tree_menu
->Append(TreeTest_SortRev
, wxT("Sort in reversed order"));
280 tree_menu
->AppendSeparator();
281 tree_menu
->Append(TreeTest_EnsureVisible
, wxT("Make the last item &visible"));
282 tree_menu
->Append(TreeTest_SetFocus
, wxT("Set &focus to the tree"));
283 tree_menu
->AppendSeparator();
284 tree_menu
->Append(TreeTest_IncIndent
, wxT("Add 5 points to indentation\tAlt-I"));
285 tree_menu
->Append(TreeTest_DecIndent
, wxT("Reduce indentation by 5 points\tAlt-R"));
286 tree_menu
->AppendSeparator();
287 tree_menu
->Append(TreeTest_IncSpacing
, wxT("Add 5 points to spacing\tCtrl-I"));
288 tree_menu
->Append(TreeTest_DecSpacing
, wxT("Reduce spacing by 5 points\tCtrl-R"));
290 item_menu
->Append(TreeTest_Dump
, wxT("&Dump item children"));
291 item_menu
->Append(TreeTest_Rename
, wxT("&Rename item..."));
293 item_menu
->AppendSeparator();
294 item_menu
->Append(TreeTest_SetBold
, wxT("Make item &bold"));
295 item_menu
->Append(TreeTest_ClearBold
, wxT("Make item ¬ bold"));
296 item_menu
->AppendSeparator();
297 item_menu
->Append(TreeTest_ToggleIcon
, wxT("Toggle the item's &icon"));
298 item_menu
->Append(TreeTest_ToggleState
, wxT("Toggle the item's &state"));
299 item_menu
->AppendSeparator();
300 item_menu
->Append(TreeTest_ShowFirstVisible
, wxT("Show &first visible"));
301 #ifdef wxHAS_LAST_VISIBLE
302 item_menu
->Append(TreeTest_ShowLastVisible
, wxT("Show &last visible"));
303 #endif // wxHAS_LAST_VISIBLE
304 item_menu
->Append(TreeTest_ShowNextVisible
, wxT("Show &next visible"));
305 item_menu
->Append(TreeTest_ShowPrevVisible
, wxT("Show &previous visible"));
306 item_menu
->AppendSeparator();
307 item_menu
->Append(TreeTest_ShowParent
, "Show pa&rent");
308 item_menu
->Append(TreeTest_ShowPrevSibling
, "Show &previous sibling");
309 item_menu
->Append(TreeTest_ShowNextSibling
, "Show &next sibling");
310 item_menu
->AppendSeparator();
311 item_menu
->Append(TreeTest_ScrollTo
, "Scroll &to item",
312 "Scroll to the last by one top level child");
313 item_menu
->Append(TreeTest_SelectLast
, "Select &last item",
314 "Select the last item");
316 #ifndef NO_MULTIPLE_SELECTION
317 item_menu
->AppendSeparator();
318 item_menu
->Append(TreeTest_DumpSelected
, wxT("Dump selected items\tAlt-D"));
319 item_menu
->Append(TreeTest_Select
, wxT("Select current item\tAlt-S"));
320 item_menu
->Append(TreeTest_Unselect
, wxT("Unselect everything\tAlt-U"));
321 item_menu
->Append(TreeTest_SelectChildren
, wxT("Select all children\tCtrl-A"));
322 #endif // NO_MULTIPLE_SELECTION
324 wxMenuBar
*menu_bar
= new wxMenuBar
;
325 menu_bar
->Append(file_menu
, wxT("&File"));
326 menu_bar
->Append(style_menu
, wxT("&Style"));
327 menu_bar
->Append(tree_menu
, wxT("&Tree"));
328 menu_bar
->Append(item_menu
, wxT("&Item"));
329 SetMenuBar(menu_bar
);
330 #endif // wxUSE_MENUS
332 m_panel
= new wxPanel(this);
335 // create the controls
336 m_textCtrl
= new wxTextCtrl(m_panel
, wxID_ANY
, wxT(""),
337 wxDefaultPosition
, wxDefaultSize
,
338 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
341 CreateTreeWithDefStyle();
343 menu_bar
->Check(TreeTest_ToggleImages
, true);
344 menu_bar
->Check(TreeTest_ToggleStates
, true);
345 menu_bar
->Check(TreeTest_ToggleAlternateImages
, false);
346 menu_bar
->Check(TreeTest_ToggleAlternateStates
, false);
349 // create a status bar
351 #endif // wxUSE_STATUSBAR
355 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
356 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
357 delete wxLog::SetActiveTarget(new wxLogStderr
);
359 // set our text control as the log target
360 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
361 delete wxLog::SetActiveTarget(logWindow
);
369 delete wxLog::SetActiveTarget(NULL
);
373 void MyFrame::CreateTreeWithDefStyle()
375 long style
= wxTR_DEFAULT_STYLE
|
376 #ifndef NO_VARIABLE_HEIGHT
377 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
381 CreateTree(style
| wxSUNKEN_BORDER
);
383 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
385 wxMenuBar
*mbar
= GetMenuBar();
386 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_HAS_BUTTONS
) != 0);
387 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_TWIST_BUTTONS
) != 0);
388 mbar
->Check(TreeTest_TogLines
, (style
& wxTR_NO_LINES
) == 0);
389 mbar
->Check(TreeTest_TogRootLines
, (style
& wxTR_LINES_AT_ROOT
) != 0);
390 mbar
->Check(TreeTest_TogHideRoot
, (style
& wxTR_HIDE_ROOT
) != 0);
391 mbar
->Check(TreeTest_TogEdit
, (style
& wxTR_EDIT_LABELS
) != 0);
392 mbar
->Check(TreeTest_TogBorder
, (style
& wxTR_ROW_LINES
) != 0);
393 mbar
->Check(TreeTest_TogFullHighlight
, (style
& wxTR_FULL_ROW_HIGHLIGHT
) != 0);
396 void MyFrame::CreateTree(long style
)
398 m_treeCtrl
= new MyTreeCtrl(m_panel
, TreeTest_Ctrl
,
399 wxDefaultPosition
, wxDefaultSize
,
402 GetMenuBar()->Enable(TreeTest_SelectRoot
, !(style
& wxTR_HIDE_ROOT
));
407 void MyFrame::TogStyle(int id
, long flag
)
409 long style
= m_treeCtrl
->GetWindowStyle() ^ flag
;
411 // most treectrl styles can't be changed on the fly using the native
412 // control and the tree must be recreated
414 m_treeCtrl
->SetWindowStyle(style
);
420 GetMenuBar()->Check(id
, (style
& flag
) != 0);
423 void MyFrame::OnIdle(wxIdleEvent
& event
)
428 wxTreeItemId idRoot
= m_treeCtrl
->GetRootItem();
432 wxTreeItemId idLast
= m_treeCtrl
->GetLastChild(idRoot
);
433 status
= wxString::Format(
434 wxT("Root/last item is %svisible/%svisible"),
435 m_treeCtrl
->IsVisible(idRoot
) ? wxT("") : wxT("not "),
436 idLast
.IsOk() && m_treeCtrl
->IsVisible(idLast
)
437 ? wxT("") : wxT("not "));
440 status
= wxT("No root item");
442 SetStatusText(status
, 1);
444 #endif // wxUSE_STATUSBAR
449 void MyFrame::OnSize(wxSizeEvent
& event
)
463 void MyFrame::Resize()
465 wxSize size
= GetClientSize();
466 m_treeCtrl
->SetSize(0, 0, size
.x
, size
.y
469 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3
474 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
479 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
481 wxMessageBox(wxT("Tree test sample\n")
482 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
483 wxT("About tree test"),
484 wxOK
| wxICON_INFORMATION
, this);
487 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
492 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
494 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
498 // old code - now we edit in place
500 static wxString s_text
;
501 s_text
= wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
503 if ( !s_text
.empty() )
505 m_treeCtrl
->SetItemText(item
, s_text
);
509 // TODO demonstrate creating a custom edit control...
510 (void)m_treeCtrl
->EditLabel(item
);
513 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
515 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
519 int i
= m_treeCtrl
->GetChildrenCount( item
, false );
521 wxLogMessage(wxT("%d children"), i
);
524 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
526 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
530 int i
= m_treeCtrl
->GetChildrenCount( item
);
532 wxLogMessage(wxT("%d children"), i
);
535 void MyFrame::DoSort(bool reverse
)
537 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
541 m_treeCtrl
->DoSortChildren(item
, reverse
);
544 void MyFrame::OnHighlight(wxCommandEvent
& WXUNUSED(event
))
546 wxTreeItemId id
= m_treeCtrl
->GetFocusedItem();
551 if ( !m_treeCtrl
->GetBoundingRect(id
, r
, true /* text, not full row */) )
553 wxLogMessage(wxT("Failed to get bounding item rect"));
557 wxClientDC
dc(m_treeCtrl
);
559 dc
.SetPen(*wxTRANSPARENT_PEN
);
561 m_treeCtrl
->Update();
564 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
566 wxTreeItemId root
= m_treeCtrl
->GetFocusedItem();
570 m_treeCtrl
->GetItemsRecursively(root
);
573 #ifndef NO_MULTIPLE_SELECTION
575 void MyFrame::OnToggleSel(wxCommandEvent
& event
)
577 TogStyle(event
.GetId(), wxTR_MULTIPLE
);
580 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
582 wxArrayTreeItemIds array
;
584 size_t count
= m_treeCtrl
->GetSelections(array
);
585 wxLogMessage(wxT("%u items selected"), unsigned(count
));
587 for ( size_t n
= 0; n
< count
; n
++ )
589 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
593 void MyFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
595 m_treeCtrl
->SelectItem(m_treeCtrl
->GetFocusedItem());
598 void MyFrame::OnSelectRoot(wxCommandEvent
& WXUNUSED(event
))
600 if ( !m_treeCtrl
->HasFlag(wxTR_HIDE_ROOT
) )
601 m_treeCtrl
->SelectItem(m_treeCtrl
->GetRootItem());
604 void MyFrame::OnSetFocusedRoot(wxCommandEvent
& WXUNUSED(event
))
606 if ( !m_treeCtrl
->HasFlag(wxTR_HIDE_ROOT
) )
607 m_treeCtrl
->SetFocusedItem(m_treeCtrl
->GetRootItem());
610 void MyFrame::OnClearFocused(wxCommandEvent
& WXUNUSED(event
))
612 m_treeCtrl
->ClearFocusedItem();
615 void MyFrame::OnUnselect(wxCommandEvent
& WXUNUSED(event
))
617 m_treeCtrl
->UnselectAll();
620 void MyFrame::OnSelectChildren(wxCommandEvent
& WXUNUSED(event
))
622 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
624 item
= m_treeCtrl
->GetRootItem();
626 m_treeCtrl
->SelectChildren(item
);
629 #endif // NO_MULTIPLE_SELECTION
631 void MyFrame::DoSetBold(bool bold
)
633 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
637 m_treeCtrl
->SetItemBold(item
, bold
);
640 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
642 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
646 m_treeCtrl
->Delete(item
);
649 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
651 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
655 m_treeCtrl
->DeleteChildren(item
);
658 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
660 m_treeCtrl
->DeleteAllItems();
663 void MyFrame::OnRecreate(wxCommandEvent
& event
)
666 m_treeCtrl
->AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL
, NUM_LEVELS
);
669 void MyFrame::OnSetImageSize(wxCommandEvent
& WXUNUSED(event
))
671 int size
= wxGetNumberFromUser(wxT("Enter the size for the images to use"),
673 wxT("TreeCtrl sample"),
674 m_treeCtrl
->ImageSize());
678 m_treeCtrl
->CreateImageList(size
);
679 wxGetApp().SetShowImages(true);
682 void MyFrame::OnToggleImages(wxCommandEvent
& WXUNUSED(event
))
684 if ( wxGetApp().ShowImages() )
686 m_treeCtrl
->CreateImageList(-1);
687 wxGetApp().SetShowImages(false);
691 m_treeCtrl
->CreateImageList(0);
692 wxGetApp().SetShowImages(true);
696 void MyFrame::OnToggleStates(wxCommandEvent
& WXUNUSED(event
))
698 if ( wxGetApp().ShowStates() )
700 m_treeCtrl
->CreateStateImageList(true);
701 wxGetApp().SetShowStates(false);
705 m_treeCtrl
->CreateStateImageList(false);
706 wxGetApp().SetShowStates(true);
710 void MyFrame::OnToggleBell(wxCommandEvent
& event
)
712 m_treeCtrl
->EnableBellOnNoMatch(event
.IsChecked());
715 void MyFrame::OnToggleAlternateImages(wxCommandEvent
& WXUNUSED(event
))
717 bool alternateImages
= m_treeCtrl
->AlternateImages();
719 m_treeCtrl
->SetAlternateImages(!alternateImages
);
720 m_treeCtrl
->CreateImageList(0);
723 void MyFrame::OnToggleAlternateStates(wxCommandEvent
& WXUNUSED(event
))
725 bool alternateStates
= m_treeCtrl
->AlternateStates();
727 m_treeCtrl
->SetAlternateStates(!alternateStates
);
728 m_treeCtrl
->CreateStateImageList();
730 // normal states < alternate states
731 // so we must reset broken states
732 if ( alternateStates
)
733 m_treeCtrl
->ResetBrokenStateImages();
736 void MyFrame::OnToggleButtons(wxCommandEvent
& WXUNUSED(event
))
738 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
739 if ( wxGetApp().ShowButtons() )
741 m_treeCtrl
->CreateButtonsImageList(-1);
742 wxGetApp().SetShowButtons(false);
746 m_treeCtrl
->CreateButtonsImageList(15);
747 wxGetApp().SetShowButtons(true);
752 void MyFrame::OnCollapseAndReset(wxCommandEvent
& WXUNUSED(event
))
754 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
757 void MyFrame::OnEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
759 m_treeCtrl
->DoEnsureVisible();
762 void MyFrame::OnSetFocus(wxCommandEvent
& WXUNUSED(event
))
764 m_treeCtrl
->SetFocus();
767 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
769 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
770 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, wxT("2nd item"));
773 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
775 static int s_num
= 0;
778 text
.Printf(wxT("Item #%d"), ++s_num
);
780 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
782 MyTreeCtrl::TreeCtrlIcon_File */ );
785 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
787 unsigned int indent
= m_treeCtrl
->GetIndent();
789 m_treeCtrl
->SetIndent( indent
+5 );
792 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
794 unsigned int indent
= m_treeCtrl
->GetIndent();
796 m_treeCtrl
->SetIndent( indent
-5 );
799 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
801 unsigned int indent
= m_treeCtrl
->GetSpacing();
804 m_treeCtrl
->SetSpacing( indent
+5 );
805 m_treeCtrl
->Refresh();
809 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
811 unsigned int indent
= m_treeCtrl
->GetSpacing();
814 m_treeCtrl
->SetSpacing( indent
-5 );
815 m_treeCtrl
->Refresh();
819 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
821 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
825 m_treeCtrl
->DoToggleIcon(item
);
828 void MyFrame::OnToggleState(wxCommandEvent
& WXUNUSED(event
))
830 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
834 m_treeCtrl
->DoToggleState(item
);
837 void MyFrame::DoShowFirstOrLast(TreeFunc0_t pfn
, const wxString
& label
)
839 const wxTreeItemId item
= (m_treeCtrl
->*pfn
)();
843 wxLogMessage("There is no %s item", label
);
847 wxLogMessage("The %s item is \"%s\"",
848 label
, m_treeCtrl
->GetItemText(item
));
852 void MyFrame::DoShowRelativeItem(TreeFunc1_t pfn
, const wxString
& label
)
854 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
858 if ((pfn
== (TreeFunc1_t
) &wxTreeCtrl::GetPrevVisible
859 || pfn
== (TreeFunc1_t
) &wxTreeCtrl::GetNextVisible
)
860 && !m_treeCtrl
->IsVisible(item
))
862 wxLogMessage("The selected item must be visible.");
866 wxTreeItemId new_item
= (m_treeCtrl
->*pfn
)(item
);
868 if ( !new_item
.IsOk() )
870 wxLogMessage("There is no %s item", label
);
874 wxLogMessage("The %s item is \"%s\"",
875 label
, m_treeCtrl
->GetItemText(new_item
));
879 void MyFrame::OnScrollTo(wxCommandEvent
& WXUNUSED(event
))
881 // scroll to the last but one top level child
882 wxTreeItemId item
= m_treeCtrl
->GetPrevSibling(
883 m_treeCtrl
->GetLastChild(
884 m_treeCtrl
->GetRootItem()));
887 m_treeCtrl
->ScrollTo(item
);
890 void MyFrame::OnSelectLast(wxCommandEvent
& WXUNUSED(event
))
892 // select the very last item of the tree
893 wxTreeItemId item
= m_treeCtrl
->GetRootItem();
896 wxTreeItemId itemChild
= m_treeCtrl
->GetLastChild(item
);
897 if ( !itemChild
.IsOk() )
905 m_treeCtrl
->SelectItem(item
);
908 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
910 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
912 m_treeCtrl
->SetForegroundColour(col
);
915 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
917 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
919 m_treeCtrl
->SetBackgroundColour(col
);
922 // MyTreeCtrl implementation
923 #if USE_GENERIC_TREECTRL
924 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
926 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
929 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
930 const wxPoint
& pos
, const wxSize
& size
,
932 : wxTreeCtrl(parent
, id
, pos
, size
, style
),
933 m_alternateImages(false),
934 m_alternateStates(false)
936 m_reverseSort
= false;
939 CreateStateImageList();
941 // Add some items to the tree
942 AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL
, NUM_LEVELS
);
945 void MyTreeCtrl::CreateImageList(int size
)
957 // Make an image list containing small icons
958 wxImageList
*images
= new wxImageList(size
, size
, true);
960 // should correspond to TreeCtrlIcon_xxx enum
964 if (m_alternateImages
)
966 icons
[TreeCtrlIcon_File
] = wxIcon(icon1_xpm
);
967 icons
[TreeCtrlIcon_FileSelected
] = wxIcon(icon2_xpm
);
968 icons
[TreeCtrlIcon_Folder
] = wxIcon(icon3_xpm
);
969 icons
[TreeCtrlIcon_FolderSelected
] = wxIcon(icon4_xpm
);
970 icons
[TreeCtrlIcon_FolderOpened
] = wxIcon(icon5_xpm
);
974 wxSize
iconSize(size
, size
);
976 icons
[TreeCtrlIcon_File
] =
977 icons
[TreeCtrlIcon_FileSelected
] = wxArtProvider::GetIcon(wxART_NORMAL_FILE
, wxART_LIST
, iconSize
);
978 icons
[TreeCtrlIcon_Folder
] =
979 icons
[TreeCtrlIcon_FolderSelected
] =
980 icons
[TreeCtrlIcon_FolderOpened
] = wxArtProvider::GetIcon(wxART_FOLDER
, wxART_LIST
, iconSize
);
983 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
985 int sizeOrig
= icons
[0].GetWidth();
986 if ( size
== sizeOrig
)
988 images
->Add(icons
[i
]);
992 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
996 AssignImageList(images
);
999 void MyTreeCtrl::CreateStateImageList(bool del
)
1003 SetStateImageList(NULL
);
1007 wxImageList
*states
;
1010 if (m_alternateStates
)
1013 icons
[0] = wxIcon(state1_xpm
); // yellow
1014 icons
[1] = wxIcon(state2_xpm
); // green
1015 icons
[2] = wxIcon(state3_xpm
); // red
1016 icons
[3] = wxIcon(state4_xpm
); // blue
1017 icons
[4] = wxIcon(state5_xpm
); // black
1019 int width
= icons
[0].GetWidth(),
1020 height
= icons
[0].GetHeight();
1022 // Make an state image list containing small icons
1023 states
= new wxImageList(width
, height
, true);
1025 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
1026 states
->Add(icons
[i
]);
1031 icons
[0] = wxIcon(unchecked_xpm
);
1032 icons
[1] = wxIcon(checked_xpm
);
1034 int width
= icons
[0].GetWidth(),
1035 height
= icons
[0].GetHeight();
1037 // Make an state image list containing small icons
1038 states
= new wxImageList(width
, height
, true);
1040 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
1041 states
->Add(icons
[i
]);
1044 AssignStateImageList(states
);
1047 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
1048 void MyTreeCtrl::CreateButtonsImageList(int size
)
1052 SetButtonsImageList(NULL
);
1056 // Make an image list containing small icons
1057 wxImageList
*images
= new wxImageList(size
, size
, true);
1059 // should correspond to TreeCtrlIcon_xxx enum
1063 if (m_alternateImages
)
1065 icons
[0] = wxIcon(icon3_xpm
); // closed
1066 icons
[1] = wxIcon(icon3_xpm
); // closed, selected
1067 icons
[2] = wxIcon(icon5_xpm
); // open
1068 icons
[3] = wxIcon(icon5_xpm
); // open, selected
1072 wxSize
iconSize(size
, size
);
1074 icons
[0] = // closed
1075 icons
[1] = wxArtProvider::GetIcon(wxART_FOLDER
, wxART_LIST
, iconSize
); // closed, selected
1077 icons
[3] = wxArtProvider::GetIcon(wxART_FOLDER_OPEN
, wxART_LIST
, iconSize
);// open, selected
1080 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
1082 int sizeOrig
= icons
[i
].GetWidth();
1083 if ( size
== sizeOrig
)
1085 images
->Add(icons
[i
]);
1089 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
1093 AssignButtonsImageList(images
);
1095 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size
))
1100 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1101 const wxTreeItemId
& item2
)
1103 if ( m_reverseSort
)
1105 // just exchange 1st and 2nd items
1106 return wxTreeCtrl::OnCompareItems(item2
, item1
);
1110 return wxTreeCtrl::OnCompareItems(item1
, item2
);
1114 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
1121 bool hasChildren
= depth
> 1;
1124 for ( size_t n
= 0; n
< numChildren
; n
++ )
1126 // at depth 1 elements won't have any more children
1128 str
.Printf(wxT("%s child %u"), wxT("Folder"), unsigned(n
+ 1));
1130 str
.Printf(wxT("%s child %u.%u"), wxT("File"), unsigned(folder
), unsigned(n
+ 1));
1132 // here we pass to AppendItem() normal and selected item images (we
1133 // suppose that selected image follows the normal one in the enum)
1134 int image
, imageSel
;
1135 if ( wxGetApp().ShowImages() )
1137 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
1138 imageSel
= image
+ 1;
1142 image
= imageSel
= -1;
1144 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
1145 new MyTreeItemData(str
));
1147 if ( wxGetApp().ShowStates() )
1148 SetItemState(id
, 0);
1150 // and now we also set the expanded one (only for the folders)
1151 if ( hasChildren
&& wxGetApp().ShowImages() )
1153 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
1154 wxTreeItemIcon_Expanded
);
1157 // remember the last child for OnEnsureVisible()
1158 if ( !hasChildren
&& n
== numChildren
- 1 )
1163 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
1169 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
1172 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
1173 wxTreeItemId rootId
= AddRoot(wxT("Root"),
1175 new MyTreeItemData(wxT("Root item")));
1176 if ( !HasFlag(wxTR_HIDE_ROOT
) && image
!= -1 )
1178 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
1181 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
1183 // set some colours/fonts for testing
1184 if ( !HasFlag(wxTR_HIDE_ROOT
) )
1185 SetItemFont(rootId
, *wxITALIC_FONT
);
1187 wxTreeItemIdValue cookie
;
1188 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
1189 SetItemTextColour(id
, *wxBLUE
);
1191 id
= GetNextChild(rootId
, cookie
);
1193 id
= GetNextChild(rootId
, cookie
);
1196 SetItemTextColour(id
, *wxRED
);
1197 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
1201 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
,
1202 wxTreeItemIdValue cookie
)
1207 id
= GetFirstChild(idParent
, cookie
);
1209 id
= GetNextChild(idParent
, cookie
);
1214 wxString text
= GetItemText(id
);
1217 if (ItemHasChildren(id
))
1218 GetItemsRecursively(id
);
1220 GetItemsRecursively(idParent
, cookie
);
1223 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
1225 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
1227 : TreeCtrlIcon_Folder
;
1228 SetItemImage(item
, image
, wxTreeItemIcon_Normal
);
1230 image
= GetItemImage(item
, wxTreeItemIcon_Selected
) == TreeCtrlIcon_FolderSelected
1231 ? TreeCtrlIcon_FileSelected
1232 : TreeCtrlIcon_FolderSelected
;
1233 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1236 void MyTreeCtrl::DoToggleState(const wxTreeItemId
& item
)
1238 if ( m_alternateStates
)
1240 // sets random state unlike current
1241 int state
= GetItemState(item
);
1246 nState
= rand() % GetStateImageList()->GetImageCount();
1247 } while (nState
== state
);
1249 SetItemState(item
, nState
);
1253 // we have only 2 checkbox states, so next state will be reversed
1254 SetItemState(item
, wxTREE_ITEMSTATE_NEXT
);
1258 void MyTreeCtrl::DoResetBrokenStateImages(const wxTreeItemId
& idParent
,
1259 wxTreeItemIdValue cookie
, int state
)
1264 id
= GetFirstChild(idParent
, cookie
);
1266 id
= GetNextChild(idParent
, cookie
);
1271 int curState
= GetItemState(id
);
1272 if ( curState
!= wxTREE_ITEMSTATE_NONE
&& curState
> state
)
1273 SetItemState(id
, state
);
1275 if (ItemHasChildren(id
))
1276 DoResetBrokenStateImages(id
, 0, state
);
1278 DoResetBrokenStateImages(idParent
, cookie
, state
);
1281 void MyTreeCtrl::LogEvent(const wxChar
*name
, const wxTreeEvent
& event
)
1283 wxTreeItemId item
= event
.GetItem();
1286 text
<< wxT('"') << GetItemText(item
).c_str() << wxT('"');
1288 text
= wxT("invalid item");
1289 wxLogMessage(wxT("%s(%s)"), name
, text
.c_str());
1293 #define TREE_EVENT_HANDLER(name) \
1294 void MyTreeCtrl::name(wxTreeEvent& event) \
1296 LogEvent(wxT(#name), event); \
1297 SetLastItem(wxTreeItemId()); \
1301 TREE_EVENT_HANDLER(OnBeginRDrag
)
1302 TREE_EVENT_HANDLER(OnDeleteItem
)
1303 TREE_EVENT_HANDLER(OnGetInfo
)
1304 TREE_EVENT_HANDLER(OnSetInfo
)
1305 TREE_EVENT_HANDLER(OnItemExpanded
)
1306 TREE_EVENT_HANDLER(OnItemExpanding
)
1307 TREE_EVENT_HANDLER(OnItemCollapsed
)
1308 TREE_EVENT_HANDLER(OnSelChanged
)
1309 TREE_EVENT_HANDLER(OnSelChanging
)
1311 #undef TREE_EVENT_HANDLER
1313 void LogKeyEvent(const wxChar
*name
, const wxKeyEvent
& event
)
1316 long keycode
= event
.GetKeyCode();
1320 case WXK_BACK
: key
= wxT("BACK"); break;
1321 case WXK_TAB
: key
= wxT("TAB"); break;
1322 case WXK_RETURN
: key
= wxT("RETURN"); break;
1323 case WXK_ESCAPE
: key
= wxT("ESCAPE"); break;
1324 case WXK_SPACE
: key
= wxT("SPACE"); break;
1325 case WXK_DELETE
: key
= wxT("DELETE"); break;
1326 case WXK_START
: key
= wxT("START"); break;
1327 case WXK_LBUTTON
: key
= wxT("LBUTTON"); break;
1328 case WXK_RBUTTON
: key
= wxT("RBUTTON"); break;
1329 case WXK_CANCEL
: key
= wxT("CANCEL"); break;
1330 case WXK_MBUTTON
: key
= wxT("MBUTTON"); break;
1331 case WXK_CLEAR
: key
= wxT("CLEAR"); break;
1332 case WXK_SHIFT
: key
= wxT("SHIFT"); break;
1333 case WXK_ALT
: key
= wxT("ALT"); break;
1334 case WXK_CONTROL
: key
= wxT("CONTROL"); break;
1335 case WXK_MENU
: key
= wxT("MENU"); break;
1336 case WXK_PAUSE
: key
= wxT("PAUSE"); break;
1337 case WXK_CAPITAL
: key
= wxT("CAPITAL"); break;
1338 case WXK_END
: key
= wxT("END"); break;
1339 case WXK_HOME
: key
= wxT("HOME"); break;
1340 case WXK_LEFT
: key
= wxT("LEFT"); break;
1341 case WXK_UP
: key
= wxT("UP"); break;
1342 case WXK_RIGHT
: key
= wxT("RIGHT"); break;
1343 case WXK_DOWN
: key
= wxT("DOWN"); break;
1344 case WXK_SELECT
: key
= wxT("SELECT"); break;
1345 case WXK_PRINT
: key
= wxT("PRINT"); break;
1346 case WXK_EXECUTE
: key
= wxT("EXECUTE"); break;
1347 case WXK_SNAPSHOT
: key
= wxT("SNAPSHOT"); break;
1348 case WXK_INSERT
: key
= wxT("INSERT"); break;
1349 case WXK_HELP
: key
= wxT("HELP"); break;
1350 case WXK_NUMPAD0
: key
= wxT("NUMPAD0"); break;
1351 case WXK_NUMPAD1
: key
= wxT("NUMPAD1"); break;
1352 case WXK_NUMPAD2
: key
= wxT("NUMPAD2"); break;
1353 case WXK_NUMPAD3
: key
= wxT("NUMPAD3"); break;
1354 case WXK_NUMPAD4
: key
= wxT("NUMPAD4"); break;
1355 case WXK_NUMPAD5
: key
= wxT("NUMPAD5"); break;
1356 case WXK_NUMPAD6
: key
= wxT("NUMPAD6"); break;
1357 case WXK_NUMPAD7
: key
= wxT("NUMPAD7"); break;
1358 case WXK_NUMPAD8
: key
= wxT("NUMPAD8"); break;
1359 case WXK_NUMPAD9
: key
= wxT("NUMPAD9"); break;
1360 case WXK_MULTIPLY
: key
= wxT("MULTIPLY"); break;
1361 case WXK_ADD
: key
= wxT("ADD"); break;
1362 case WXK_SEPARATOR
: key
= wxT("SEPARATOR"); break;
1363 case WXK_SUBTRACT
: key
= wxT("SUBTRACT"); break;
1364 case WXK_DECIMAL
: key
= wxT("DECIMAL"); break;
1365 case WXK_DIVIDE
: key
= wxT("DIVIDE"); break;
1366 case WXK_F1
: key
= wxT("F1"); break;
1367 case WXK_F2
: key
= wxT("F2"); break;
1368 case WXK_F3
: key
= wxT("F3"); break;
1369 case WXK_F4
: key
= wxT("F4"); break;
1370 case WXK_F5
: key
= wxT("F5"); break;
1371 case WXK_F6
: key
= wxT("F6"); break;
1372 case WXK_F7
: key
= wxT("F7"); break;
1373 case WXK_F8
: key
= wxT("F8"); break;
1374 case WXK_F9
: key
= wxT("F9"); break;
1375 case WXK_F10
: key
= wxT("F10"); break;
1376 case WXK_F11
: key
= wxT("F11"); break;
1377 case WXK_F12
: key
= wxT("F12"); break;
1378 case WXK_F13
: key
= wxT("F13"); break;
1379 case WXK_F14
: key
= wxT("F14"); break;
1380 case WXK_F15
: key
= wxT("F15"); break;
1381 case WXK_F16
: key
= wxT("F16"); break;
1382 case WXK_F17
: key
= wxT("F17"); break;
1383 case WXK_F18
: key
= wxT("F18"); break;
1384 case WXK_F19
: key
= wxT("F19"); break;
1385 case WXK_F20
: key
= wxT("F20"); break;
1386 case WXK_F21
: key
= wxT("F21"); break;
1387 case WXK_F22
: key
= wxT("F22"); break;
1388 case WXK_F23
: key
= wxT("F23"); break;
1389 case WXK_F24
: key
= wxT("F24"); break;
1390 case WXK_NUMLOCK
: key
= wxT("NUMLOCK"); break;
1391 case WXK_SCROLL
: key
= wxT("SCROLL"); break;
1392 case WXK_PAGEUP
: key
= wxT("PAGEUP"); break;
1393 case WXK_PAGEDOWN
: key
= wxT("PAGEDOWN"); break;
1394 case WXK_NUMPAD_SPACE
: key
= wxT("NUMPAD_SPACE"); break;
1395 case WXK_NUMPAD_TAB
: key
= wxT("NUMPAD_TAB"); break;
1396 case WXK_NUMPAD_ENTER
: key
= wxT("NUMPAD_ENTER"); break;
1397 case WXK_NUMPAD_F1
: key
= wxT("NUMPAD_F1"); break;
1398 case WXK_NUMPAD_F2
: key
= wxT("NUMPAD_F2"); break;
1399 case WXK_NUMPAD_F3
: key
= wxT("NUMPAD_F3"); break;
1400 case WXK_NUMPAD_F4
: key
= wxT("NUMPAD_F4"); break;
1401 case WXK_NUMPAD_HOME
: key
= wxT("NUMPAD_HOME"); break;
1402 case WXK_NUMPAD_LEFT
: key
= wxT("NUMPAD_LEFT"); break;
1403 case WXK_NUMPAD_UP
: key
= wxT("NUMPAD_UP"); break;
1404 case WXK_NUMPAD_RIGHT
: key
= wxT("NUMPAD_RIGHT"); break;
1405 case WXK_NUMPAD_DOWN
: key
= wxT("NUMPAD_DOWN"); break;
1406 case WXK_NUMPAD_PAGEUP
: key
= wxT("NUMPAD_PAGEUP"); break;
1407 case WXK_NUMPAD_PAGEDOWN
: key
= wxT("NUMPAD_PAGEDOWN"); break;
1408 case WXK_NUMPAD_END
: key
= wxT("NUMPAD_END"); break;
1409 case WXK_NUMPAD_BEGIN
: key
= wxT("NUMPAD_BEGIN"); break;
1410 case WXK_NUMPAD_INSERT
: key
= wxT("NUMPAD_INSERT"); break;
1411 case WXK_NUMPAD_DELETE
: key
= wxT("NUMPAD_DELETE"); break;
1412 case WXK_NUMPAD_EQUAL
: key
= wxT("NUMPAD_EQUAL"); break;
1413 case WXK_NUMPAD_MULTIPLY
: key
= wxT("NUMPAD_MULTIPLY"); break;
1414 case WXK_NUMPAD_ADD
: key
= wxT("NUMPAD_ADD"); break;
1415 case WXK_NUMPAD_SEPARATOR
: key
= wxT("NUMPAD_SEPARATOR"); break;
1416 case WXK_NUMPAD_SUBTRACT
: key
= wxT("NUMPAD_SUBTRACT"); break;
1417 case WXK_NUMPAD_DECIMAL
: key
= wxT("NUMPAD_DECIMAL"); break;
1421 if ( keycode
< 128 && wxIsprint((int)keycode
) )
1422 key
.Printf(wxT("'%c'"), (char)keycode
);
1423 else if ( keycode
> 0 && keycode
< 27 )
1424 key
.Printf(_("Ctrl-%c"), wxT('A') + keycode
- 1);
1426 key
.Printf(wxT("unknown (%ld)"), keycode
);
1431 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1434 event
.ControlDown() ? wxT('C') : wxT('-'),
1435 event
.AltDown() ? wxT('A') : wxT('-'),
1436 event
.ShiftDown() ? wxT('S') : wxT('-'),
1437 event
.MetaDown() ? wxT('M') : wxT('-'));
1440 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
& event
)
1442 LogKeyEvent(wxT("Tree key down "), event
.GetKeyEvent());
1447 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
1449 // need to explicitly allow drag
1450 if ( event
.GetItem() != GetRootItem() )
1452 m_draggedItem
= event
.GetItem();
1454 wxPoint clientpt
= event
.GetPoint();
1455 wxPoint screenpt
= ClientToScreen(clientpt
);
1457 wxLogMessage(wxT("OnBeginDrag: started dragging %s at screen coords (%i,%i)"),
1458 GetItemText(m_draggedItem
).c_str(),
1459 screenpt
.x
, screenpt
.y
);
1465 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1469 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
1471 wxTreeItemId itemSrc
= m_draggedItem
,
1472 itemDst
= event
.GetItem();
1473 m_draggedItem
= (wxTreeItemId
)0l;
1475 // where to copy the item?
1476 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
1478 // copy to the parent then
1479 itemDst
= GetItemParent(itemDst
);
1482 if ( !itemDst
.IsOk() )
1484 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1489 wxString text
= GetItemText(itemSrc
);
1490 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1491 text
.c_str(), GetItemText(itemDst
).c_str());
1493 // just do append here - we could also insert it just before/after the item
1494 // on which it was dropped, but this requires slightly more work... we also
1495 // completely ignore the client data and icon of the old item but could
1496 // copy them as well.
1498 // Finally, we only copy one item here but we might copy the entire tree if
1499 // we were dragging a folder.
1500 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
1501 wxTreeItemId id
= AppendItem(itemDst
, text
, image
);
1503 if ( wxGetApp().ShowStates() )
1504 SetItemState(id
, GetItemState(itemSrc
));
1507 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
1509 wxLogMessage(wxT("OnBeginLabelEdit"));
1511 // for testing, prevent this item's label editing
1512 wxTreeItemId itemId
= event
.GetItem();
1513 if ( IsTestItem(itemId
) )
1515 wxMessageBox(wxT("You can't edit this item."));
1519 else if ( itemId
== GetRootItem() )
1521 // test that it is possible to change the text of the item being edited
1522 SetItemText(itemId
, wxT("Editing root item"));
1526 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
1528 wxLogMessage(wxT("OnEndLabelEdit"));
1530 // don't allow anything except letters in the labels
1531 if ( !event
.GetLabel().IsWord() )
1533 wxMessageBox(wxT("The new label should be a single word."));
1539 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
1541 wxLogMessage(wxT("OnItemCollapsing"));
1543 // for testing, prevent the user from collapsing the first child folder
1544 wxTreeItemId itemId
= event
.GetItem();
1545 if ( IsTestItem(itemId
) )
1547 wxMessageBox(wxT("You can't collapse this item."));
1553 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
1555 // show some info about this item
1556 wxTreeItemId itemId
= event
.GetItem();
1557 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1561 item
->ShowInfo(this);
1564 wxLogMessage(wxT("OnItemActivated"));
1567 void MyTreeCtrl::OnItemStateClick(wxTreeEvent
& event
)
1569 // toggle item state
1570 wxTreeItemId itemId
= event
.GetItem();
1571 DoToggleState(itemId
);
1573 wxLogMessage(wxT("Item \"%s\" state changed to %d"),
1574 GetItemText(itemId
), GetItemState(itemId
));
1577 void MyTreeCtrl::OnItemMenu(wxTreeEvent
& event
)
1579 wxTreeItemId itemId
= event
.GetItem();
1580 wxCHECK_RET( itemId
.IsOk(), "should have a valid item" );
1582 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1583 wxPoint clientpt
= event
.GetPoint();
1584 wxPoint screenpt
= ClientToScreen(clientpt
);
1586 wxLogMessage(wxT("OnItemMenu for item \"%s\" at screen coords (%i, %i)"),
1587 item
->GetDesc(), screenpt
.x
, screenpt
.y
);
1589 ShowMenu(itemId
, clientpt
);
1593 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1595 wxPoint pt
= event
.GetPosition();
1597 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt
.x
, pt
.y
);
1602 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
1607 title
<< wxT("Menu for ") << GetItemText(id
);
1611 title
= wxT("Menu for no particular item");
1616 menu
.Append(TreeTest_About
, wxT("&About"));
1617 menu
.AppendSeparator();
1618 menu
.Append(TreeTest_Highlight
, wxT("&Highlight item"));
1619 menu
.Append(TreeTest_Dump
, wxT("&Dump"));
1621 PopupMenu(&menu
, pt
);
1622 #endif // wxUSE_MENUS
1625 void MyTreeCtrl::OnItemRClick(wxTreeEvent
& event
)
1627 wxTreeItemId itemId
= event
.GetItem();
1628 wxCHECK_RET( itemId
.IsOk(), "should have a valid item" );
1630 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1632 wxLogMessage(wxT("Item \"%s\" right clicked"), item
->GetDesc());
1637 void MyTreeCtrl::OnRMouseDown(wxMouseEvent
& event
)
1639 wxLogMessage(wxT("Right mouse button down"));
1644 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
1646 wxLogMessage(wxT("Right mouse button up"));
1651 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
1653 wxTreeItemId id
= HitTest(event
.GetPosition());
1656 wxLogMessage(wxT("No item under mouse"));
1660 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
1663 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1670 static inline const wxChar
*Bool2String(bool b
)
1672 return b
? wxT("") : wxT("not ");
1675 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1677 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1678 wxT("%u children (%u immediately under this item)."),
1680 Bool2String(tree
->IsSelected(GetId())),
1681 Bool2String(tree
->IsExpanded(GetId())),
1682 Bool2String(tree
->IsBold(GetId())),
1683 unsigned(tree
->GetChildrenCount(GetId())),
1684 unsigned(tree
->GetChildrenCount(GetId(), false)));