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 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(ToggleAlternateImages
)
117 MENU_LINK(ToggleAlternateStates
)
118 MENU_LINK(ToggleButtons
)
119 MENU_LINK(SetImageSize
)
120 MENU_LINK(CollapseAndReset
)
121 MENU_LINK(EnsureVisible
)
124 MENU_LINK(InsertItem
)
127 MENU_LINK(IncSpacing
)
128 MENU_LINK(DecSpacing
)
129 MENU_LINK(ToggleIcon
)
130 MENU_LINK(ToggleState
)
131 MENU_LINK(SelectRoot
)
132 MENU_LINK(SetFocusedRoot
)
133 MENU_LINK(ClearFocused
)
135 MENU_LINK(ShowFirstVisible
)
136 #ifdef wxHAS_LAST_VISIBLE
137 MENU_LINK(ShowLastVisible
)
138 #endif // wxHAS_LAST_VISIBLE
139 MENU_LINK(ShowNextVisible
)
140 MENU_LINK(ShowPrevVisible
)
141 MENU_LINK(ShowParent
)
142 MENU_LINK(ShowPrevSibling
)
143 MENU_LINK(ShowNextSibling
)
149 #if USE_GENERIC_TREECTRL
150 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
152 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
154 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
155 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
156 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
157 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
158 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
159 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
160 #if 0 // there are so many of those that logging them causes flicker
161 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
163 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
164 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
165 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
166 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
167 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
169 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
170 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
171 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
172 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
173 EVT_TREE_STATE_IMAGE_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemStateClick
)
175 // so many different ways to handle right mouse button clicks...
176 EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu
)
177 // EVT_TREE_ITEM_MENU is the preferred event for creating context menus
178 // on a tree control, because it includes the point of the click or item,
179 // meaning that no additional placement calculations are required.
180 EVT_TREE_ITEM_MENU(TreeTest_Ctrl
, MyTreeCtrl::OnItemMenu
)
181 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRClick
)
183 EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown
)
184 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
185 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
192 if ( !wxApp::OnInit() )
195 // Create the main frame window
196 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
206 // My frame constructor
207 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
208 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
214 // This reduces flicker effects - even better would be to define
215 // OnEraseBackground to do nothing. When the tree control's scrollbars are
216 // show or hidden, the frame is sent a background erase event.
217 SetBackgroundColour(wxColour(255, 255, 255));
220 SetIcon(wxICON(sample
));
224 wxMenu
*file_menu
= new wxMenu
,
225 *style_menu
= new wxMenu
,
226 *tree_menu
= new wxMenu
,
227 *item_menu
= new wxMenu
;
229 file_menu
->Append(TreeTest_ClearLog
, wxT("&Clear log\tCtrl-L"));
230 file_menu
->AppendSeparator();
231 file_menu
->Append(TreeTest_About
, wxT("&About..."));
232 file_menu
->AppendSeparator();
233 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
235 style_menu
->AppendCheckItem(TreeTest_TogButtons
, wxT("Toggle &normal buttons"));
236 style_menu
->AppendCheckItem(TreeTest_TogTwist
, wxT("Toggle &twister buttons"));
237 style_menu
->AppendCheckItem(TreeTest_ToggleButtons
, wxT("Toggle image &buttons"));
238 style_menu
->AppendSeparator();
239 style_menu
->AppendCheckItem(TreeTest_TogLines
, wxT("Toggle &connecting lines"));
240 style_menu
->AppendCheckItem(TreeTest_TogRootLines
, wxT("Toggle &lines at root"));
241 style_menu
->AppendCheckItem(TreeTest_TogHideRoot
, wxT("Toggle &hidden root"));
242 style_menu
->AppendCheckItem(TreeTest_TogBorder
, wxT("Toggle &item border"));
243 style_menu
->AppendCheckItem(TreeTest_TogFullHighlight
, wxT("Toggle &full row highlight"));
244 style_menu
->AppendCheckItem(TreeTest_TogEdit
, wxT("Toggle &edit mode"));
245 #ifndef NO_MULTIPLE_SELECTION
246 style_menu
->AppendCheckItem(TreeTest_ToggleSel
, wxT("Toggle &selection mode\tCtrl-S"));
247 #endif // NO_MULTIPLE_SELECTION
248 style_menu
->AppendCheckItem(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
249 style_menu
->AppendCheckItem(TreeTest_ToggleStates
, wxT("Toggle show st&ates"));
250 style_menu
->AppendCheckItem(TreeTest_ToggleAlternateImages
, wxT("Toggle alternate images"));
251 style_menu
->AppendCheckItem(TreeTest_ToggleAlternateStates
, wxT("Toggle alternate state images"));
252 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
253 style_menu
->AppendSeparator();
254 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
255 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
256 style_menu
->AppendSeparator();
257 style_menu
->Append(TreeTest_ResetStyle
, wxT("&Reset to default\tF10"));
259 tree_menu
->Append(TreeTest_Recreate
, wxT("&Recreate the tree"));
260 tree_menu
->Append(TreeTest_CollapseAndReset
, wxT("C&ollapse and reset"));
261 tree_menu
->AppendSeparator();
262 tree_menu
->Append(TreeTest_AddItem
, wxT("Append a &new item"));
263 tree_menu
->Append(TreeTest_InsertItem
, wxT("&Insert a new item"));
264 tree_menu
->Append(TreeTest_Delete
, wxT("&Delete this item"));
265 tree_menu
->Append(TreeTest_DeleteChildren
, wxT("Delete &children"));
266 tree_menu
->Append(TreeTest_DeleteAll
, wxT("Delete &all items"));
267 tree_menu
->Append(TreeTest_SelectRoot
, wxT("Select root item"));
268 tree_menu
->AppendSeparator();
269 tree_menu
->Append(TreeTest_SetFocusedRoot
, wxT("Set focus to root item"));
270 tree_menu
->Append(TreeTest_ClearFocused
, wxT("Reset focus"));
272 tree_menu
->AppendSeparator();
273 tree_menu
->Append(TreeTest_Count
, wxT("Count children of current item"));
274 tree_menu
->Append(TreeTest_CountRec
, wxT("Recursively count children of current item"));
275 tree_menu
->AppendSeparator();
276 tree_menu
->Append(TreeTest_Sort
, wxT("Sort children of current item"));
277 tree_menu
->Append(TreeTest_SortRev
, wxT("Sort in reversed order"));
278 tree_menu
->AppendSeparator();
279 tree_menu
->Append(TreeTest_EnsureVisible
, wxT("Make the last item &visible"));
280 tree_menu
->Append(TreeTest_SetFocus
, wxT("Set &focus to the tree"));
281 tree_menu
->AppendSeparator();
282 tree_menu
->Append(TreeTest_IncIndent
, wxT("Add 5 points to indentation\tAlt-I"));
283 tree_menu
->Append(TreeTest_DecIndent
, wxT("Reduce indentation by 5 points\tAlt-R"));
284 tree_menu
->AppendSeparator();
285 tree_menu
->Append(TreeTest_IncSpacing
, wxT("Add 5 points to spacing\tCtrl-I"));
286 tree_menu
->Append(TreeTest_DecSpacing
, wxT("Reduce spacing by 5 points\tCtrl-R"));
288 item_menu
->Append(TreeTest_Dump
, wxT("&Dump item children"));
289 item_menu
->Append(TreeTest_Rename
, wxT("&Rename item..."));
291 item_menu
->AppendSeparator();
292 item_menu
->Append(TreeTest_SetBold
, wxT("Make item &bold"));
293 item_menu
->Append(TreeTest_ClearBold
, wxT("Make item ¬ bold"));
294 item_menu
->AppendSeparator();
295 item_menu
->Append(TreeTest_ToggleIcon
, wxT("Toggle the item's &icon"));
296 item_menu
->Append(TreeTest_ToggleState
, wxT("Toggle the item's &state"));
297 item_menu
->AppendSeparator();
298 item_menu
->Append(TreeTest_ShowFirstVisible
, wxT("Show &first visible"));
299 #ifdef wxHAS_LAST_VISIBLE
300 item_menu
->Append(TreeTest_ShowLastVisible
, wxT("Show &last visible"));
301 #endif // wxHAS_LAST_VISIBLE
302 item_menu
->Append(TreeTest_ShowNextVisible
, wxT("Show &next visible"));
303 item_menu
->Append(TreeTest_ShowPrevVisible
, wxT("Show &previous visible"));
304 item_menu
->AppendSeparator();
305 item_menu
->Append(TreeTest_ShowParent
, "Show pa&rent");
306 item_menu
->Append(TreeTest_ShowPrevSibling
, "Show &previous sibling");
307 item_menu
->Append(TreeTest_ShowNextSibling
, "Show &next sibling");
308 item_menu
->AppendSeparator();
309 item_menu
->Append(TreeTest_ScrollTo
, "Scroll &to item",
310 "Scroll to the last by one top level child");
312 #ifndef NO_MULTIPLE_SELECTION
313 item_menu
->AppendSeparator();
314 item_menu
->Append(TreeTest_DumpSelected
, wxT("Dump selected items\tAlt-D"));
315 item_menu
->Append(TreeTest_Select
, wxT("Select current item\tAlt-S"));
316 item_menu
->Append(TreeTest_Unselect
, wxT("Unselect everything\tAlt-U"));
317 item_menu
->Append(TreeTest_SelectChildren
, wxT("Select all children\tCtrl-A"));
318 #endif // NO_MULTIPLE_SELECTION
320 wxMenuBar
*menu_bar
= new wxMenuBar
;
321 menu_bar
->Append(file_menu
, wxT("&File"));
322 menu_bar
->Append(style_menu
, wxT("&Style"));
323 menu_bar
->Append(tree_menu
, wxT("&Tree"));
324 menu_bar
->Append(item_menu
, wxT("&Item"));
325 SetMenuBar(menu_bar
);
326 #endif // wxUSE_MENUS
328 m_panel
= new wxPanel(this);
331 // create the controls
332 m_textCtrl
= new wxTextCtrl(m_panel
, wxID_ANY
, wxT(""),
333 wxDefaultPosition
, wxDefaultSize
,
334 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
337 CreateTreeWithDefStyle();
339 menu_bar
->Check(TreeTest_ToggleImages
, true);
340 menu_bar
->Check(TreeTest_ToggleStates
, true);
341 menu_bar
->Check(TreeTest_ToggleAlternateImages
, false);
342 menu_bar
->Check(TreeTest_ToggleAlternateStates
, false);
345 // create a status bar
347 #endif // wxUSE_STATUSBAR
351 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
352 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
353 delete wxLog::SetActiveTarget(new wxLogStderr
);
355 // set our text control as the log target
356 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
357 delete wxLog::SetActiveTarget(logWindow
);
365 delete wxLog::SetActiveTarget(NULL
);
369 void MyFrame::CreateTreeWithDefStyle()
371 long style
= wxTR_DEFAULT_STYLE
|
372 #ifndef NO_VARIABLE_HEIGHT
373 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
377 CreateTree(style
| wxSUNKEN_BORDER
);
379 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
381 wxMenuBar
*mbar
= GetMenuBar();
382 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_HAS_BUTTONS
) != 0);
383 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_TWIST_BUTTONS
) != 0);
384 mbar
->Check(TreeTest_TogLines
, (style
& wxTR_NO_LINES
) == 0);
385 mbar
->Check(TreeTest_TogRootLines
, (style
& wxTR_LINES_AT_ROOT
) != 0);
386 mbar
->Check(TreeTest_TogHideRoot
, (style
& wxTR_HIDE_ROOT
) != 0);
387 mbar
->Check(TreeTest_TogEdit
, (style
& wxTR_EDIT_LABELS
) != 0);
388 mbar
->Check(TreeTest_TogBorder
, (style
& wxTR_ROW_LINES
) != 0);
389 mbar
->Check(TreeTest_TogFullHighlight
, (style
& wxTR_FULL_ROW_HIGHLIGHT
) != 0);
392 void MyFrame::CreateTree(long style
)
394 m_treeCtrl
= new MyTreeCtrl(m_panel
, TreeTest_Ctrl
,
395 wxDefaultPosition
, wxDefaultSize
,
398 GetMenuBar()->Enable(TreeTest_SelectRoot
, !(style
& wxTR_HIDE_ROOT
));
403 void MyFrame::TogStyle(int id
, long flag
)
405 long style
= m_treeCtrl
->GetWindowStyle() ^ flag
;
407 // most treectrl styles can't be changed on the fly using the native
408 // control and the tree must be recreated
410 m_treeCtrl
->SetWindowStyle(style
);
416 GetMenuBar()->Check(id
, (style
& flag
) != 0);
419 void MyFrame::OnIdle(wxIdleEvent
& event
)
424 wxTreeItemId idRoot
= m_treeCtrl
->GetRootItem();
428 wxTreeItemId idLast
= m_treeCtrl
->GetLastChild(idRoot
);
429 status
= wxString::Format(
430 wxT("Root/last item is %svisible/%svisible"),
431 m_treeCtrl
->IsVisible(idRoot
) ? wxT("") : wxT("not "),
432 idLast
.IsOk() && m_treeCtrl
->IsVisible(idLast
)
433 ? wxT("") : wxT("not "));
436 status
= wxT("No root item");
438 SetStatusText(status
, 1);
440 #endif // wxUSE_STATUSBAR
445 void MyFrame::OnSize(wxSizeEvent
& event
)
459 void MyFrame::Resize()
461 wxSize size
= GetClientSize();
462 m_treeCtrl
->SetSize(0, 0, size
.x
, size
.y
465 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3
470 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
475 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
477 wxMessageBox(wxT("Tree test sample\n")
478 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
479 wxT("About tree test"),
480 wxOK
| wxICON_INFORMATION
, this);
483 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
488 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
490 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
494 // old code - now we edit in place
496 static wxString s_text
;
497 s_text
= wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
499 if ( !s_text
.empty() )
501 m_treeCtrl
->SetItemText(item
, s_text
);
505 // TODO demonstrate creating a custom edit control...
506 (void)m_treeCtrl
->EditLabel(item
);
509 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
511 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
515 int i
= m_treeCtrl
->GetChildrenCount( item
, false );
517 wxLogMessage(wxT("%d children"), i
);
520 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
522 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
526 int i
= m_treeCtrl
->GetChildrenCount( item
);
528 wxLogMessage(wxT("%d children"), i
);
531 void MyFrame::DoSort(bool reverse
)
533 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
537 m_treeCtrl
->DoSortChildren(item
, reverse
);
540 void MyFrame::OnHighlight(wxCommandEvent
& WXUNUSED(event
))
542 wxTreeItemId id
= m_treeCtrl
->GetFocusedItem();
547 if ( !m_treeCtrl
->GetBoundingRect(id
, r
, true /* text, not full row */) )
549 wxLogMessage(wxT("Failed to get bounding item rect"));
553 wxClientDC
dc(m_treeCtrl
);
555 dc
.SetPen(*wxTRANSPARENT_PEN
);
557 m_treeCtrl
->Update();
560 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
562 wxTreeItemId root
= m_treeCtrl
->GetFocusedItem();
566 m_treeCtrl
->GetItemsRecursively(root
);
569 #ifndef NO_MULTIPLE_SELECTION
571 void MyFrame::OnToggleSel(wxCommandEvent
& event
)
573 TogStyle(event
.GetId(), wxTR_MULTIPLE
);
576 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
578 wxArrayTreeItemIds array
;
580 size_t count
= m_treeCtrl
->GetSelections(array
);
581 wxLogMessage(wxT("%u items selected"), unsigned(count
));
583 for ( size_t n
= 0; n
< count
; n
++ )
585 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
589 void MyFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
591 m_treeCtrl
->SelectItem(m_treeCtrl
->GetFocusedItem());
594 void MyFrame::OnSelectRoot(wxCommandEvent
& WXUNUSED(event
))
596 if ( !m_treeCtrl
->HasFlag(wxTR_HIDE_ROOT
) )
597 m_treeCtrl
->SelectItem(m_treeCtrl
->GetRootItem());
600 void MyFrame::OnSetFocusedRoot(wxCommandEvent
& WXUNUSED(event
))
602 if ( !m_treeCtrl
->HasFlag(wxTR_HIDE_ROOT
) )
603 m_treeCtrl
->SetFocusedItem(m_treeCtrl
->GetRootItem());
606 void MyFrame::OnClearFocused(wxCommandEvent
& WXUNUSED(event
))
608 m_treeCtrl
->ClearFocusedItem();
611 void MyFrame::OnUnselect(wxCommandEvent
& WXUNUSED(event
))
613 m_treeCtrl
->UnselectAll();
616 void MyFrame::OnSelectChildren(wxCommandEvent
& WXUNUSED(event
))
618 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
620 item
= m_treeCtrl
->GetRootItem();
622 m_treeCtrl
->SelectChildren(item
);
625 #endif // NO_MULTIPLE_SELECTION
627 void MyFrame::DoSetBold(bool bold
)
629 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
633 m_treeCtrl
->SetItemBold(item
, bold
);
636 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
638 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
642 m_treeCtrl
->Delete(item
);
645 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
647 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
651 m_treeCtrl
->DeleteChildren(item
);
654 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
656 m_treeCtrl
->DeleteAllItems();
659 void MyFrame::OnRecreate(wxCommandEvent
& event
)
662 m_treeCtrl
->AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL
, NUM_LEVELS
);
665 void MyFrame::OnSetImageSize(wxCommandEvent
& WXUNUSED(event
))
667 int size
= wxGetNumberFromUser(wxT("Enter the size for the images to use"),
669 wxT("TreeCtrl sample"),
670 m_treeCtrl
->ImageSize());
674 m_treeCtrl
->CreateImageList(size
);
675 wxGetApp().SetShowImages(true);
678 void MyFrame::OnToggleImages(wxCommandEvent
& WXUNUSED(event
))
680 if ( wxGetApp().ShowImages() )
682 m_treeCtrl
->CreateImageList(-1);
683 wxGetApp().SetShowImages(false);
687 m_treeCtrl
->CreateImageList(0);
688 wxGetApp().SetShowImages(true);
692 void MyFrame::OnToggleStates(wxCommandEvent
& WXUNUSED(event
))
694 if ( wxGetApp().ShowStates() )
696 m_treeCtrl
->CreateStateImageList(true);
697 wxGetApp().SetShowStates(false);
701 m_treeCtrl
->CreateStateImageList(false);
702 wxGetApp().SetShowStates(true);
706 void MyFrame::OnToggleAlternateImages(wxCommandEvent
& WXUNUSED(event
))
708 bool alternateImages
= m_treeCtrl
->AlternateImages();
710 m_treeCtrl
->SetAlternateImages(!alternateImages
);
711 m_treeCtrl
->CreateImageList(0);
714 void MyFrame::OnToggleAlternateStates(wxCommandEvent
& WXUNUSED(event
))
716 bool alternateStates
= m_treeCtrl
->AlternateStates();
718 m_treeCtrl
->SetAlternateStates(!alternateStates
);
719 m_treeCtrl
->CreateStateImageList();
721 // normal states < alternate states
722 // so we must reset broken states
723 if ( alternateStates
)
724 m_treeCtrl
->ResetBrokenStateImages();
727 void MyFrame::OnToggleButtons(wxCommandEvent
& WXUNUSED(event
))
729 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
730 if ( wxGetApp().ShowButtons() )
732 m_treeCtrl
->CreateButtonsImageList(-1);
733 wxGetApp().SetShowButtons(false);
737 m_treeCtrl
->CreateButtonsImageList(15);
738 wxGetApp().SetShowButtons(true);
743 void MyFrame::OnCollapseAndReset(wxCommandEvent
& WXUNUSED(event
))
745 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
748 void MyFrame::OnEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
750 m_treeCtrl
->DoEnsureVisible();
753 void MyFrame::OnSetFocus(wxCommandEvent
& WXUNUSED(event
))
755 m_treeCtrl
->SetFocus();
758 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
760 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
761 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, wxT("2nd item"));
764 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
766 static int s_num
= 0;
769 text
.Printf(wxT("Item #%d"), ++s_num
);
771 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
773 MyTreeCtrl::TreeCtrlIcon_File */ );
776 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
778 unsigned int indent
= m_treeCtrl
->GetIndent();
780 m_treeCtrl
->SetIndent( indent
+5 );
783 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
785 unsigned int indent
= m_treeCtrl
->GetIndent();
787 m_treeCtrl
->SetIndent( indent
-5 );
790 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
792 unsigned int indent
= m_treeCtrl
->GetSpacing();
795 m_treeCtrl
->SetSpacing( indent
+5 );
796 m_treeCtrl
->Refresh();
800 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
802 unsigned int indent
= m_treeCtrl
->GetSpacing();
805 m_treeCtrl
->SetSpacing( indent
-5 );
806 m_treeCtrl
->Refresh();
810 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
812 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
816 m_treeCtrl
->DoToggleIcon(item
);
819 void MyFrame::OnToggleState(wxCommandEvent
& WXUNUSED(event
))
821 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
825 m_treeCtrl
->DoToggleState(item
);
828 void MyFrame::DoShowFirstOrLast(TreeFunc0_t pfn
, const wxString
& label
)
830 const wxTreeItemId item
= (m_treeCtrl
->*pfn
)();
834 wxLogMessage("There is no %s item", label
);
838 wxLogMessage("The %s item is \"%s\"",
839 label
, m_treeCtrl
->GetItemText(item
));
843 void MyFrame::DoShowRelativeItem(TreeFunc1_t pfn
, const wxString
& label
)
845 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
849 if ((pfn
== (TreeFunc1_t
) &wxTreeCtrl::GetPrevVisible
850 || pfn
== (TreeFunc1_t
) &wxTreeCtrl::GetNextVisible
)
851 && !m_treeCtrl
->IsVisible(item
))
853 wxLogMessage("The selected item must be visible.");
857 wxTreeItemId new_item
= (m_treeCtrl
->*pfn
)(item
);
859 if ( !new_item
.IsOk() )
861 wxLogMessage("There is no %s item", label
);
865 wxLogMessage("The %s item is \"%s\"",
866 label
, m_treeCtrl
->GetItemText(new_item
));
870 void MyFrame::OnScrollTo(wxCommandEvent
& WXUNUSED(event
))
872 // scroll to the last but one top level child
873 wxTreeItemId item
= m_treeCtrl
->GetPrevSibling(
874 m_treeCtrl
->GetLastChild(
875 m_treeCtrl
->GetRootItem()));
878 m_treeCtrl
->ScrollTo(item
);
881 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
883 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
885 m_treeCtrl
->SetForegroundColour(col
);
888 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
890 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
892 m_treeCtrl
->SetBackgroundColour(col
);
895 // MyTreeCtrl implementation
896 #if USE_GENERIC_TREECTRL
897 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
899 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
902 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
903 const wxPoint
& pos
, const wxSize
& size
,
905 : wxTreeCtrl(parent
, id
, pos
, size
, style
),
906 m_alternateImages(false),
907 m_alternateStates(false)
909 m_reverseSort
= false;
912 CreateStateImageList();
914 // Add some items to the tree
915 AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL
, NUM_LEVELS
);
918 void MyTreeCtrl::CreateImageList(int size
)
930 // Make an image list containing small icons
931 wxImageList
*images
= new wxImageList(size
, size
, true);
933 // should correspond to TreeCtrlIcon_xxx enum
937 if (m_alternateImages
)
939 icons
[TreeCtrlIcon_File
] = wxIcon(icon1_xpm
);
940 icons
[TreeCtrlIcon_FileSelected
] = wxIcon(icon2_xpm
);
941 icons
[TreeCtrlIcon_Folder
] = wxIcon(icon3_xpm
);
942 icons
[TreeCtrlIcon_FolderSelected
] = wxIcon(icon4_xpm
);
943 icons
[TreeCtrlIcon_FolderOpened
] = wxIcon(icon5_xpm
);
947 wxSize
iconSize(size
, size
);
949 icons
[TreeCtrlIcon_File
] =
950 icons
[TreeCtrlIcon_FileSelected
] = wxArtProvider::GetIcon(wxART_NORMAL_FILE
, wxART_LIST
, iconSize
);
951 icons
[TreeCtrlIcon_Folder
] =
952 icons
[TreeCtrlIcon_FolderSelected
] =
953 icons
[TreeCtrlIcon_FolderOpened
] = wxArtProvider::GetIcon(wxART_FOLDER
, wxART_LIST
, iconSize
);
956 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
958 int sizeOrig
= icons
[0].GetWidth();
959 if ( size
== sizeOrig
)
961 images
->Add(icons
[i
]);
965 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
969 AssignImageList(images
);
972 void MyTreeCtrl::CreateStateImageList(bool del
)
976 SetStateImageList(NULL
);
983 if (m_alternateStates
)
986 icons
[0] = wxIcon(state1_xpm
); // yellow
987 icons
[1] = wxIcon(state2_xpm
); // green
988 icons
[2] = wxIcon(state3_xpm
); // red
989 icons
[3] = wxIcon(state4_xpm
); // blue
990 icons
[4] = wxIcon(state5_xpm
); // black
992 int width
= icons
[0].GetWidth(),
993 height
= icons
[0].GetHeight();
995 // Make an state image list containing small icons
996 states
= new wxImageList(width
, height
, true);
998 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
999 states
->Add(icons
[i
]);
1004 icons
[0] = wxIcon(unchecked_xpm
);
1005 icons
[1] = wxIcon(checked_xpm
);
1007 int width
= icons
[0].GetWidth(),
1008 height
= icons
[0].GetHeight();
1010 // Make an state image list containing small icons
1011 states
= new wxImageList(width
, height
, true);
1013 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
1014 states
->Add(icons
[i
]);
1017 AssignStateImageList(states
);
1020 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
1021 void MyTreeCtrl::CreateButtonsImageList(int size
)
1025 SetButtonsImageList(NULL
);
1029 // Make an image list containing small icons
1030 wxImageList
*images
= new wxImageList(size
, size
, true);
1032 // should correspond to TreeCtrlIcon_xxx enum
1036 if (m_alternateImages
)
1038 icons
[0] = wxIcon(icon3_xpm
); // closed
1039 icons
[1] = wxIcon(icon3_xpm
); // closed, selected
1040 icons
[2] = wxIcon(icon5_xpm
); // open
1041 icons
[3] = wxIcon(icon5_xpm
); // open, selected
1045 wxSize
iconSize(size
, size
);
1047 icons
[0] = // closed
1048 icons
[1] = wxArtProvider::GetIcon(wxART_FOLDER
, wxART_LIST
, iconSize
); // closed, selected
1050 icons
[3] = wxArtProvider::GetIcon(wxART_FOLDER_OPEN
, wxART_LIST
, iconSize
);// open, selected
1053 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
1055 int sizeOrig
= icons
[i
].GetWidth();
1056 if ( size
== sizeOrig
)
1058 images
->Add(icons
[i
]);
1062 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
1066 AssignButtonsImageList(images
);
1068 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size
))
1073 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1074 const wxTreeItemId
& item2
)
1076 if ( m_reverseSort
)
1078 // just exchange 1st and 2nd items
1079 return wxTreeCtrl::OnCompareItems(item2
, item1
);
1083 return wxTreeCtrl::OnCompareItems(item1
, item2
);
1087 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
1094 bool hasChildren
= depth
> 1;
1097 for ( size_t n
= 0; n
< numChildren
; n
++ )
1099 // at depth 1 elements won't have any more children
1101 str
.Printf(wxT("%s child %u"), wxT("Folder"), unsigned(n
+ 1));
1103 str
.Printf(wxT("%s child %u.%u"), wxT("File"), unsigned(folder
), unsigned(n
+ 1));
1105 // here we pass to AppendItem() normal and selected item images (we
1106 // suppose that selected image follows the normal one in the enum)
1107 int image
, imageSel
;
1108 if ( wxGetApp().ShowImages() )
1110 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
1111 imageSel
= image
+ 1;
1115 image
= imageSel
= -1;
1117 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
1118 new MyTreeItemData(str
));
1120 if ( wxGetApp().ShowStates() )
1121 SetItemState(id
, 0);
1123 // and now we also set the expanded one (only for the folders)
1124 if ( hasChildren
&& wxGetApp().ShowImages() )
1126 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
1127 wxTreeItemIcon_Expanded
);
1130 // remember the last child for OnEnsureVisible()
1131 if ( !hasChildren
&& n
== numChildren
- 1 )
1136 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
1142 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
1145 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
1146 wxTreeItemId rootId
= AddRoot(wxT("Root"),
1148 new MyTreeItemData(wxT("Root item")));
1149 if ( !HasFlag(wxTR_HIDE_ROOT
) && image
!= -1 )
1151 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
1154 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
1156 // set some colours/fonts for testing
1157 if ( !HasFlag(wxTR_HIDE_ROOT
) )
1158 SetItemFont(rootId
, *wxITALIC_FONT
);
1160 wxTreeItemIdValue cookie
;
1161 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
1162 SetItemTextColour(id
, *wxBLUE
);
1164 id
= GetNextChild(rootId
, cookie
);
1166 id
= GetNextChild(rootId
, cookie
);
1169 SetItemTextColour(id
, *wxRED
);
1170 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
1174 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
,
1175 wxTreeItemIdValue cookie
)
1180 id
= GetFirstChild(idParent
, cookie
);
1182 id
= GetNextChild(idParent
, cookie
);
1187 wxString text
= GetItemText(id
);
1190 if (ItemHasChildren(id
))
1191 GetItemsRecursively(id
);
1193 GetItemsRecursively(idParent
, cookie
);
1196 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
1198 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
1200 : TreeCtrlIcon_Folder
;
1201 SetItemImage(item
, image
, wxTreeItemIcon_Normal
);
1203 image
= GetItemImage(item
, wxTreeItemIcon_Selected
) == TreeCtrlIcon_FolderSelected
1204 ? TreeCtrlIcon_FileSelected
1205 : TreeCtrlIcon_FolderSelected
;
1206 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1209 void MyTreeCtrl::DoToggleState(const wxTreeItemId
& item
)
1211 if ( m_alternateStates
)
1213 // sets random state unlike current
1214 int state
= GetItemState(item
);
1219 nState
= rand() % GetStateImageList()->GetImageCount();
1220 } while (nState
== state
);
1222 SetItemState(item
, nState
);
1226 // we have only 2 checkbox states, so next state will be reversed
1227 SetItemState(item
, wxTREE_ITEMSTATE_NEXT
);
1231 void MyTreeCtrl::DoResetBrokenStateImages(const wxTreeItemId
& idParent
,
1232 wxTreeItemIdValue cookie
, int state
)
1237 id
= GetFirstChild(idParent
, cookie
);
1239 id
= GetNextChild(idParent
, cookie
);
1244 int curState
= GetItemState(id
);
1245 if ( curState
!= wxTREE_ITEMSTATE_NONE
&& curState
> state
)
1246 SetItemState(id
, state
);
1248 if (ItemHasChildren(id
))
1249 DoResetBrokenStateImages(id
, 0, state
);
1251 DoResetBrokenStateImages(idParent
, cookie
, state
);
1254 void MyTreeCtrl::LogEvent(const wxChar
*name
, const wxTreeEvent
& event
)
1256 wxTreeItemId item
= event
.GetItem();
1259 text
<< wxT('"') << GetItemText(item
).c_str() << wxT('"');
1261 text
= wxT("invalid item");
1262 wxLogMessage(wxT("%s(%s)"), name
, text
.c_str());
1266 #define TREE_EVENT_HANDLER(name) \
1267 void MyTreeCtrl::name(wxTreeEvent& event) \
1269 LogEvent(wxT(#name), event); \
1270 SetLastItem(wxTreeItemId()); \
1274 TREE_EVENT_HANDLER(OnBeginRDrag
)
1275 TREE_EVENT_HANDLER(OnDeleteItem
)
1276 TREE_EVENT_HANDLER(OnGetInfo
)
1277 TREE_EVENT_HANDLER(OnSetInfo
)
1278 TREE_EVENT_HANDLER(OnItemExpanded
)
1279 TREE_EVENT_HANDLER(OnItemExpanding
)
1280 TREE_EVENT_HANDLER(OnItemCollapsed
)
1281 TREE_EVENT_HANDLER(OnSelChanged
)
1282 TREE_EVENT_HANDLER(OnSelChanging
)
1284 #undef TREE_EVENT_HANDLER
1286 void LogKeyEvent(const wxChar
*name
, const wxKeyEvent
& event
)
1289 long keycode
= event
.GetKeyCode();
1293 case WXK_BACK
: key
= wxT("BACK"); break;
1294 case WXK_TAB
: key
= wxT("TAB"); break;
1295 case WXK_RETURN
: key
= wxT("RETURN"); break;
1296 case WXK_ESCAPE
: key
= wxT("ESCAPE"); break;
1297 case WXK_SPACE
: key
= wxT("SPACE"); break;
1298 case WXK_DELETE
: key
= wxT("DELETE"); break;
1299 case WXK_START
: key
= wxT("START"); break;
1300 case WXK_LBUTTON
: key
= wxT("LBUTTON"); break;
1301 case WXK_RBUTTON
: key
= wxT("RBUTTON"); break;
1302 case WXK_CANCEL
: key
= wxT("CANCEL"); break;
1303 case WXK_MBUTTON
: key
= wxT("MBUTTON"); break;
1304 case WXK_CLEAR
: key
= wxT("CLEAR"); break;
1305 case WXK_SHIFT
: key
= wxT("SHIFT"); break;
1306 case WXK_ALT
: key
= wxT("ALT"); break;
1307 case WXK_CONTROL
: key
= wxT("CONTROL"); break;
1308 case WXK_MENU
: key
= wxT("MENU"); break;
1309 case WXK_PAUSE
: key
= wxT("PAUSE"); break;
1310 case WXK_CAPITAL
: key
= wxT("CAPITAL"); break;
1311 case WXK_END
: key
= wxT("END"); break;
1312 case WXK_HOME
: key
= wxT("HOME"); break;
1313 case WXK_LEFT
: key
= wxT("LEFT"); break;
1314 case WXK_UP
: key
= wxT("UP"); break;
1315 case WXK_RIGHT
: key
= wxT("RIGHT"); break;
1316 case WXK_DOWN
: key
= wxT("DOWN"); break;
1317 case WXK_SELECT
: key
= wxT("SELECT"); break;
1318 case WXK_PRINT
: key
= wxT("PRINT"); break;
1319 case WXK_EXECUTE
: key
= wxT("EXECUTE"); break;
1320 case WXK_SNAPSHOT
: key
= wxT("SNAPSHOT"); break;
1321 case WXK_INSERT
: key
= wxT("INSERT"); break;
1322 case WXK_HELP
: key
= wxT("HELP"); break;
1323 case WXK_NUMPAD0
: key
= wxT("NUMPAD0"); break;
1324 case WXK_NUMPAD1
: key
= wxT("NUMPAD1"); break;
1325 case WXK_NUMPAD2
: key
= wxT("NUMPAD2"); break;
1326 case WXK_NUMPAD3
: key
= wxT("NUMPAD3"); break;
1327 case WXK_NUMPAD4
: key
= wxT("NUMPAD4"); break;
1328 case WXK_NUMPAD5
: key
= wxT("NUMPAD5"); break;
1329 case WXK_NUMPAD6
: key
= wxT("NUMPAD6"); break;
1330 case WXK_NUMPAD7
: key
= wxT("NUMPAD7"); break;
1331 case WXK_NUMPAD8
: key
= wxT("NUMPAD8"); break;
1332 case WXK_NUMPAD9
: key
= wxT("NUMPAD9"); break;
1333 case WXK_MULTIPLY
: key
= wxT("MULTIPLY"); break;
1334 case WXK_ADD
: key
= wxT("ADD"); break;
1335 case WXK_SEPARATOR
: key
= wxT("SEPARATOR"); break;
1336 case WXK_SUBTRACT
: key
= wxT("SUBTRACT"); break;
1337 case WXK_DECIMAL
: key
= wxT("DECIMAL"); break;
1338 case WXK_DIVIDE
: key
= wxT("DIVIDE"); break;
1339 case WXK_F1
: key
= wxT("F1"); break;
1340 case WXK_F2
: key
= wxT("F2"); break;
1341 case WXK_F3
: key
= wxT("F3"); break;
1342 case WXK_F4
: key
= wxT("F4"); break;
1343 case WXK_F5
: key
= wxT("F5"); break;
1344 case WXK_F6
: key
= wxT("F6"); break;
1345 case WXK_F7
: key
= wxT("F7"); break;
1346 case WXK_F8
: key
= wxT("F8"); break;
1347 case WXK_F9
: key
= wxT("F9"); break;
1348 case WXK_F10
: key
= wxT("F10"); break;
1349 case WXK_F11
: key
= wxT("F11"); break;
1350 case WXK_F12
: key
= wxT("F12"); break;
1351 case WXK_F13
: key
= wxT("F13"); break;
1352 case WXK_F14
: key
= wxT("F14"); break;
1353 case WXK_F15
: key
= wxT("F15"); break;
1354 case WXK_F16
: key
= wxT("F16"); break;
1355 case WXK_F17
: key
= wxT("F17"); break;
1356 case WXK_F18
: key
= wxT("F18"); break;
1357 case WXK_F19
: key
= wxT("F19"); break;
1358 case WXK_F20
: key
= wxT("F20"); break;
1359 case WXK_F21
: key
= wxT("F21"); break;
1360 case WXK_F22
: key
= wxT("F22"); break;
1361 case WXK_F23
: key
= wxT("F23"); break;
1362 case WXK_F24
: key
= wxT("F24"); break;
1363 case WXK_NUMLOCK
: key
= wxT("NUMLOCK"); break;
1364 case WXK_SCROLL
: key
= wxT("SCROLL"); break;
1365 case WXK_PAGEUP
: key
= wxT("PAGEUP"); break;
1366 case WXK_PAGEDOWN
: key
= wxT("PAGEDOWN"); break;
1367 case WXK_NUMPAD_SPACE
: key
= wxT("NUMPAD_SPACE"); break;
1368 case WXK_NUMPAD_TAB
: key
= wxT("NUMPAD_TAB"); break;
1369 case WXK_NUMPAD_ENTER
: key
= wxT("NUMPAD_ENTER"); break;
1370 case WXK_NUMPAD_F1
: key
= wxT("NUMPAD_F1"); break;
1371 case WXK_NUMPAD_F2
: key
= wxT("NUMPAD_F2"); break;
1372 case WXK_NUMPAD_F3
: key
= wxT("NUMPAD_F3"); break;
1373 case WXK_NUMPAD_F4
: key
= wxT("NUMPAD_F4"); break;
1374 case WXK_NUMPAD_HOME
: key
= wxT("NUMPAD_HOME"); break;
1375 case WXK_NUMPAD_LEFT
: key
= wxT("NUMPAD_LEFT"); break;
1376 case WXK_NUMPAD_UP
: key
= wxT("NUMPAD_UP"); break;
1377 case WXK_NUMPAD_RIGHT
: key
= wxT("NUMPAD_RIGHT"); break;
1378 case WXK_NUMPAD_DOWN
: key
= wxT("NUMPAD_DOWN"); break;
1379 case WXK_NUMPAD_PAGEUP
: key
= wxT("NUMPAD_PAGEUP"); break;
1380 case WXK_NUMPAD_PAGEDOWN
: key
= wxT("NUMPAD_PAGEDOWN"); break;
1381 case WXK_NUMPAD_END
: key
= wxT("NUMPAD_END"); break;
1382 case WXK_NUMPAD_BEGIN
: key
= wxT("NUMPAD_BEGIN"); break;
1383 case WXK_NUMPAD_INSERT
: key
= wxT("NUMPAD_INSERT"); break;
1384 case WXK_NUMPAD_DELETE
: key
= wxT("NUMPAD_DELETE"); break;
1385 case WXK_NUMPAD_EQUAL
: key
= wxT("NUMPAD_EQUAL"); break;
1386 case WXK_NUMPAD_MULTIPLY
: key
= wxT("NUMPAD_MULTIPLY"); break;
1387 case WXK_NUMPAD_ADD
: key
= wxT("NUMPAD_ADD"); break;
1388 case WXK_NUMPAD_SEPARATOR
: key
= wxT("NUMPAD_SEPARATOR"); break;
1389 case WXK_NUMPAD_SUBTRACT
: key
= wxT("NUMPAD_SUBTRACT"); break;
1390 case WXK_NUMPAD_DECIMAL
: key
= wxT("NUMPAD_DECIMAL"); break;
1394 if ( keycode
< 128 && wxIsprint((int)keycode
) )
1395 key
.Printf(wxT("'%c'"), (char)keycode
);
1396 else if ( keycode
> 0 && keycode
< 27 )
1397 key
.Printf(_("Ctrl-%c"), wxT('A') + keycode
- 1);
1399 key
.Printf(wxT("unknown (%ld)"), keycode
);
1404 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1407 event
.ControlDown() ? wxT('C') : wxT('-'),
1408 event
.AltDown() ? wxT('A') : wxT('-'),
1409 event
.ShiftDown() ? wxT('S') : wxT('-'),
1410 event
.MetaDown() ? wxT('M') : wxT('-'));
1413 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
& event
)
1415 LogKeyEvent(wxT("Tree key down "), event
.GetKeyEvent());
1420 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
1422 // need to explicitly allow drag
1423 if ( event
.GetItem() != GetRootItem() )
1425 m_draggedItem
= event
.GetItem();
1427 wxPoint clientpt
= event
.GetPoint();
1428 wxPoint screenpt
= ClientToScreen(clientpt
);
1430 wxLogMessage(wxT("OnBeginDrag: started dragging %s at screen coords (%i,%i)"),
1431 GetItemText(m_draggedItem
).c_str(),
1432 screenpt
.x
, screenpt
.y
);
1438 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1442 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
1444 wxTreeItemId itemSrc
= m_draggedItem
,
1445 itemDst
= event
.GetItem();
1446 m_draggedItem
= (wxTreeItemId
)0l;
1448 // where to copy the item?
1449 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
1451 // copy to the parent then
1452 itemDst
= GetItemParent(itemDst
);
1455 if ( !itemDst
.IsOk() )
1457 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1462 wxString text
= GetItemText(itemSrc
);
1463 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1464 text
.c_str(), GetItemText(itemDst
).c_str());
1466 // just do append here - we could also insert it just before/after the item
1467 // on which it was dropped, but this requires slightly more work... we also
1468 // completely ignore the client data and icon of the old item but could
1469 // copy them as well.
1471 // Finally, we only copy one item here but we might copy the entire tree if
1472 // we were dragging a folder.
1473 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
1474 wxTreeItemId id
= AppendItem(itemDst
, text
, image
);
1476 if ( wxGetApp().ShowStates() )
1477 SetItemState(id
, GetItemState(itemSrc
));
1480 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
1482 wxLogMessage(wxT("OnBeginLabelEdit"));
1484 // for testing, prevent this item's label editing
1485 wxTreeItemId itemId
= event
.GetItem();
1486 if ( IsTestItem(itemId
) )
1488 wxMessageBox(wxT("You can't edit this item."));
1492 else if ( itemId
== GetRootItem() )
1494 // test that it is possible to change the text of the item being edited
1495 SetItemText(itemId
, wxT("Editing root item"));
1499 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
1501 wxLogMessage(wxT("OnEndLabelEdit"));
1503 // don't allow anything except letters in the labels
1504 if ( !event
.GetLabel().IsWord() )
1506 wxMessageBox(wxT("The new label should be a single word."));
1512 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
1514 wxLogMessage(wxT("OnItemCollapsing"));
1516 // for testing, prevent the user from collapsing the first child folder
1517 wxTreeItemId itemId
= event
.GetItem();
1518 if ( IsTestItem(itemId
) )
1520 wxMessageBox(wxT("You can't collapse this item."));
1526 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
1528 // show some info about this item
1529 wxTreeItemId itemId
= event
.GetItem();
1530 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1534 item
->ShowInfo(this);
1537 wxLogMessage(wxT("OnItemActivated"));
1540 void MyTreeCtrl::OnItemStateClick(wxTreeEvent
& event
)
1542 // toggle item state
1543 wxTreeItemId itemId
= event
.GetItem();
1544 DoToggleState(itemId
);
1546 wxLogMessage(wxT("Item \"%s\" state changed to %d"),
1547 GetItemText(itemId
), GetItemState(itemId
));
1550 void MyTreeCtrl::OnItemMenu(wxTreeEvent
& event
)
1552 wxTreeItemId itemId
= event
.GetItem();
1553 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1555 wxPoint clientpt
= event
.GetPoint();
1556 wxPoint screenpt
= ClientToScreen(clientpt
);
1558 wxLogMessage(wxT("OnItemMenu for item \"%s\" at screen coords (%i, %i)"),
1559 item
? item
->GetDesc() : wxT(""), screenpt
.x
, screenpt
.y
);
1561 ShowMenu(itemId
, clientpt
);
1565 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1567 wxPoint pt
= event
.GetPosition();
1569 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt
.x
, pt
.y
);
1574 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
1579 title
<< wxT("Menu for ") << GetItemText(id
);
1583 title
= wxT("Menu for no particular item");
1588 menu
.Append(TreeTest_About
, wxT("&About..."));
1589 menu
.AppendSeparator();
1590 menu
.Append(TreeTest_Highlight
, wxT("&Highlight item"));
1591 menu
.Append(TreeTest_Dump
, wxT("&Dump"));
1593 PopupMenu(&menu
, pt
);
1594 #endif // wxUSE_MENUS
1597 void MyTreeCtrl::OnItemRClick(wxTreeEvent
& event
)
1599 wxTreeItemId itemId
= event
.GetItem();
1600 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1603 wxLogMessage(wxT("Item \"%s\" right clicked"), item
? item
->GetDesc()
1609 void MyTreeCtrl::OnRMouseDown(wxMouseEvent
& event
)
1611 wxLogMessage(wxT("Right mouse button down"));
1616 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
1618 wxLogMessage(wxT("Right mouse button up"));
1623 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
1625 wxTreeItemId id
= HitTest(event
.GetPosition());
1628 wxLogMessage(wxT("No item under mouse"));
1632 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
1635 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1642 static inline const wxChar
*Bool2String(bool b
)
1644 return b
? wxT("") : wxT("not ");
1647 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1649 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1650 wxT("%u children (%u immediately under this item)."),
1652 Bool2String(tree
->IsSelected(GetId())),
1653 Bool2String(tree
->IsExpanded(GetId())),
1654 Bool2String(tree
->IsBold(GetId())),
1655 unsigned(tree
->GetChildrenCount(GetId())),
1656 unsigned(tree
->GetChildrenCount(GetId(), false)));