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"
33 #include "wx/wupdlock.h"
36 // this is not supported by native control
37 #define NO_VARIABLE_HEIGHT
54 #include "unchecked.xpm"
55 #include "checked.xpm"
57 #ifndef wxHAS_IMAGES_IN_RESOURCES
58 #include "../sample.xpm"
61 static const int NUM_CHILDREN_PER_LEVEL
= 5;
62 static const int NUM_LEVELS
= 2;
64 // verify that the item is ok and insult the user if it is not
65 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
66 wxMessageBox(wxT("Please select some item first!"), \
67 wxT("Tree sample error"), \
68 wxOK | wxICON_EXCLAMATION, \
73 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
75 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
76 EVT_IDLE(MyFrame::OnIdle
)
77 EVT_SIZE(MyFrame::OnSize
)
87 MENU_LINK(TogHideRoot
)
88 MENU_LINK(TogRootLines
)
90 MENU_LINK(TogFullHighlight
)
91 MENU_LINK(SetFgColour
)
92 MENU_LINK(SetBgColour
)
97 #ifndef NO_MULTIPLE_SELECTION
98 MENU_LINK(DumpSelected
)
102 MENU_LINK(SelectChildren
)
103 #endif // NO_MULTIPLE_SELECTION
112 MENU_LINK(DeleteChildren
)
115 MENU_LINK(ToggleImages
)
116 MENU_LINK(ToggleStates
)
117 MENU_LINK(ToggleBell
)
118 MENU_LINK(ToggleAlternateImages
)
119 MENU_LINK(ToggleAlternateStates
)
120 MENU_LINK(ToggleButtons
)
121 MENU_LINK(SetImageSize
)
122 MENU_LINK(CollapseAndReset
)
123 MENU_LINK(EnsureVisible
)
126 MENU_LINK(AddManyItems
)
127 MENU_LINK(InsertItem
)
130 MENU_LINK(IncSpacing
)
131 MENU_LINK(DecSpacing
)
132 MENU_LINK(ToggleIcon
)
133 MENU_LINK(ToggleState
)
134 MENU_LINK(SelectRoot
)
135 MENU_LINK(SetFocusedRoot
)
136 MENU_LINK(ClearFocused
)
138 MENU_LINK(ShowFirstVisible
)
139 #ifdef wxHAS_LAST_VISIBLE
140 MENU_LINK(ShowLastVisible
)
141 #endif // wxHAS_LAST_VISIBLE
142 MENU_LINK(ShowNextVisible
)
143 MENU_LINK(ShowPrevVisible
)
144 MENU_LINK(ShowParent
)
145 MENU_LINK(ShowPrevSibling
)
146 MENU_LINK(ShowNextSibling
)
148 MENU_LINK(SelectLast
)
153 #if USE_GENERIC_TREECTRL
154 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
156 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
158 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
159 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
160 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
161 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
162 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
163 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
164 #if 0 // there are so many of those that logging them causes flicker
165 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
167 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
168 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
169 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
170 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
171 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
173 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
174 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
175 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
176 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
177 EVT_TREE_STATE_IMAGE_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemStateClick
)
179 // so many different ways to handle right mouse button clicks...
180 EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu
)
181 // EVT_TREE_ITEM_MENU is the preferred event for creating context menus
182 // on a tree control, because it includes the point of the click or item,
183 // meaning that no additional placement calculations are required.
184 EVT_TREE_ITEM_MENU(TreeTest_Ctrl
, MyTreeCtrl::OnItemMenu
)
185 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRClick
)
187 EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown
)
188 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
189 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
196 if ( !wxApp::OnInit() )
199 // Create the main frame window
200 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
209 // My frame constructor
210 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
211 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
217 // This reduces flicker effects - even better would be to define
218 // OnEraseBackground to do nothing. When the tree control's scrollbars are
219 // show or hidden, the frame is sent a background erase event.
220 SetBackgroundColour(*wxWHITE
);
223 SetIcon(wxICON(sample
));
227 wxMenu
*file_menu
= new wxMenu
,
228 *style_menu
= new wxMenu
,
229 *tree_menu
= new wxMenu
,
230 *item_menu
= new wxMenu
;
233 file_menu
->Append(TreeTest_ClearLog
, wxT("&Clear log\tCtrl-L"));
234 file_menu
->AppendSeparator();
236 file_menu
->Append(TreeTest_About
, wxT("&About"));
237 file_menu
->AppendSeparator();
238 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
240 style_menu
->AppendCheckItem(TreeTest_TogButtons
, wxT("Toggle &normal buttons"));
241 style_menu
->AppendCheckItem(TreeTest_TogTwist
, wxT("Toggle &twister buttons"));
242 style_menu
->AppendCheckItem(TreeTest_ToggleButtons
, wxT("Toggle image &buttons"));
243 style_menu
->AppendSeparator();
244 style_menu
->AppendCheckItem(TreeTest_TogLines
, wxT("Toggle &connecting lines"));
245 style_menu
->AppendCheckItem(TreeTest_TogRootLines
, wxT("Toggle &lines at root"));
246 style_menu
->AppendCheckItem(TreeTest_TogHideRoot
, wxT("Toggle &hidden root"));
247 style_menu
->AppendCheckItem(TreeTest_TogBorder
, wxT("Toggle &item border"));
248 style_menu
->AppendCheckItem(TreeTest_TogFullHighlight
, wxT("Toggle &full row highlight"));
249 style_menu
->AppendCheckItem(TreeTest_TogEdit
, wxT("Toggle &edit mode"));
250 #ifndef NO_MULTIPLE_SELECTION
251 style_menu
->AppendCheckItem(TreeTest_ToggleSel
, wxT("Toggle &selection mode\tCtrl-S"));
252 #endif // NO_MULTIPLE_SELECTION
253 style_menu
->AppendCheckItem(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
254 style_menu
->AppendCheckItem(TreeTest_ToggleStates
, wxT("Toggle show st&ates"));
255 style_menu
->AppendCheckItem(TreeTest_ToggleBell
, wxT("Toggle &bell on no match"));
256 style_menu
->AppendCheckItem(TreeTest_ToggleAlternateImages
, wxT("Toggle alternate images"));
257 style_menu
->AppendCheckItem(TreeTest_ToggleAlternateStates
, wxT("Toggle alternate state images"));
258 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
259 style_menu
->AppendSeparator();
260 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
261 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
262 style_menu
->AppendSeparator();
263 style_menu
->Append(TreeTest_ResetStyle
, wxT("&Reset to default\tF10"));
265 tree_menu
->Append(TreeTest_Recreate
, wxT("&Recreate the tree"));
266 tree_menu
->Append(TreeTest_CollapseAndReset
, wxT("C&ollapse and reset"));
267 tree_menu
->AppendSeparator();
268 tree_menu
->Append(TreeTest_AddItem
, wxT("Append a &new item"));
269 tree_menu
->Append(TreeTest_AddManyItems
, wxT("Appends &many items"));
270 tree_menu
->Append(TreeTest_InsertItem
, wxT("&Insert a new item"));
271 tree_menu
->Append(TreeTest_Delete
, wxT("&Delete this item"));
272 tree_menu
->Append(TreeTest_DeleteChildren
, wxT("Delete &children"));
273 tree_menu
->Append(TreeTest_DeleteAll
, wxT("Delete &all items"));
274 tree_menu
->Append(TreeTest_SelectRoot
, wxT("Select root item"));
275 tree_menu
->AppendSeparator();
276 tree_menu
->Append(TreeTest_SetFocusedRoot
, wxT("Set focus to root item"));
277 tree_menu
->Append(TreeTest_ClearFocused
, wxT("Reset focus"));
279 tree_menu
->AppendSeparator();
280 tree_menu
->Append(TreeTest_Count
, wxT("Count children of current item"));
281 tree_menu
->Append(TreeTest_CountRec
, wxT("Recursively count children of current item"));
282 tree_menu
->AppendSeparator();
283 tree_menu
->Append(TreeTest_Sort
, wxT("Sort children of current item"));
284 tree_menu
->Append(TreeTest_SortRev
, wxT("Sort in reversed order"));
285 tree_menu
->AppendSeparator();
286 tree_menu
->Append(TreeTest_EnsureVisible
, wxT("Make the last item &visible"));
287 tree_menu
->Append(TreeTest_SetFocus
, wxT("Set &focus to the tree"));
288 tree_menu
->AppendSeparator();
289 tree_menu
->Append(TreeTest_IncIndent
, wxT("Add 5 points to indentation\tAlt-I"));
290 tree_menu
->Append(TreeTest_DecIndent
, wxT("Reduce indentation by 5 points\tAlt-R"));
291 tree_menu
->AppendSeparator();
292 tree_menu
->Append(TreeTest_IncSpacing
, wxT("Add 5 points to spacing\tCtrl-I"));
293 tree_menu
->Append(TreeTest_DecSpacing
, wxT("Reduce spacing by 5 points\tCtrl-R"));
295 item_menu
->Append(TreeTest_Dump
, wxT("&Dump item children"));
296 item_menu
->Append(TreeTest_Rename
, wxT("&Rename item..."));
298 item_menu
->AppendSeparator();
299 item_menu
->Append(TreeTest_SetBold
, wxT("Make item &bold"));
300 item_menu
->Append(TreeTest_ClearBold
, wxT("Make item ¬ bold"));
301 item_menu
->AppendSeparator();
302 item_menu
->Append(TreeTest_ToggleIcon
, wxT("Toggle the item's &icon"));
303 item_menu
->Append(TreeTest_ToggleState
, wxT("Toggle the item's &state"));
304 item_menu
->AppendSeparator();
305 item_menu
->Append(TreeTest_ShowFirstVisible
, wxT("Show &first visible"));
306 #ifdef wxHAS_LAST_VISIBLE
307 item_menu
->Append(TreeTest_ShowLastVisible
, wxT("Show &last visible"));
308 #endif // wxHAS_LAST_VISIBLE
309 item_menu
->Append(TreeTest_ShowNextVisible
, wxT("Show &next visible"));
310 item_menu
->Append(TreeTest_ShowPrevVisible
, wxT("Show &previous visible"));
311 item_menu
->AppendSeparator();
312 item_menu
->Append(TreeTest_ShowParent
, "Show pa&rent");
313 item_menu
->Append(TreeTest_ShowPrevSibling
, "Show &previous sibling");
314 item_menu
->Append(TreeTest_ShowNextSibling
, "Show &next sibling");
315 item_menu
->AppendSeparator();
316 item_menu
->Append(TreeTest_ScrollTo
, "Scroll &to item",
317 "Scroll to the last by one top level child");
318 item_menu
->Append(TreeTest_SelectLast
, "Select &last item",
319 "Select the last item");
321 #ifndef NO_MULTIPLE_SELECTION
322 item_menu
->AppendSeparator();
323 item_menu
->Append(TreeTest_DumpSelected
, wxT("Dump selected items\tAlt-D"));
324 item_menu
->Append(TreeTest_Select
, wxT("Select current item\tAlt-S"));
325 item_menu
->Append(TreeTest_Unselect
, wxT("Unselect everything\tAlt-U"));
326 item_menu
->Append(TreeTest_SelectChildren
, wxT("Select all children\tCtrl-A"));
327 #endif // NO_MULTIPLE_SELECTION
329 wxMenuBar
*menu_bar
= new wxMenuBar
;
330 menu_bar
->Append(file_menu
, wxT("&File"));
331 menu_bar
->Append(style_menu
, wxT("&Style"));
332 menu_bar
->Append(tree_menu
, wxT("&Tree"));
333 menu_bar
->Append(item_menu
, wxT("&Item"));
334 SetMenuBar(menu_bar
);
335 #endif // wxUSE_MENUS
337 m_panel
= new wxPanel(this);
340 // create the controls
341 m_textCtrl
= new wxTextCtrl(m_panel
, wxID_ANY
, wxT(""),
342 wxDefaultPosition
, wxDefaultSize
,
343 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
346 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
347 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
348 delete wxLog::SetActiveTarget(new wxLogStderr
);
350 // set our text control as the log target
351 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
352 delete wxLog::SetActiveTarget(logWindow
);
356 CreateTreeWithDefStyle();
358 menu_bar
->Check(TreeTest_ToggleImages
, true);
359 menu_bar
->Check(TreeTest_ToggleStates
, true);
360 menu_bar
->Check(TreeTest_ToggleAlternateImages
, false);
361 menu_bar
->Check(TreeTest_ToggleAlternateStates
, false);
364 // create a status bar
366 #endif // wxUSE_STATUSBAR
372 delete wxLog::SetActiveTarget(NULL
);
376 void MyFrame::CreateTreeWithDefStyle()
378 long style
= wxTR_DEFAULT_STYLE
|
379 #ifndef NO_VARIABLE_HEIGHT
380 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
384 CreateTree(style
| wxSUNKEN_BORDER
);
386 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
388 wxMenuBar
*mbar
= GetMenuBar();
389 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_HAS_BUTTONS
) != 0);
390 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_TWIST_BUTTONS
) != 0);
391 mbar
->Check(TreeTest_TogLines
, (style
& wxTR_NO_LINES
) == 0);
392 mbar
->Check(TreeTest_TogRootLines
, (style
& wxTR_LINES_AT_ROOT
) != 0);
393 mbar
->Check(TreeTest_TogHideRoot
, (style
& wxTR_HIDE_ROOT
) != 0);
394 mbar
->Check(TreeTest_TogEdit
, (style
& wxTR_EDIT_LABELS
) != 0);
395 mbar
->Check(TreeTest_TogBorder
, (style
& wxTR_ROW_LINES
) != 0);
396 mbar
->Check(TreeTest_TogFullHighlight
, (style
& wxTR_FULL_ROW_HIGHLIGHT
) != 0);
399 void MyFrame::CreateTree(long style
)
401 m_treeCtrl
= new MyTreeCtrl(m_panel
, TreeTest_Ctrl
,
402 wxDefaultPosition
, wxDefaultSize
,
405 GetMenuBar()->Enable(TreeTest_SelectRoot
, !(style
& wxTR_HIDE_ROOT
));
410 void MyFrame::TogStyle(int id
, long flag
)
412 long style
= m_treeCtrl
->GetWindowStyle() ^ flag
;
414 // most treectrl styles can't be changed on the fly using the native
415 // control and the tree must be recreated
417 m_treeCtrl
->SetWindowStyle(style
);
423 GetMenuBar()->Check(id
, (style
& flag
) != 0);
426 void MyFrame::OnIdle(wxIdleEvent
& event
)
431 wxTreeItemId idRoot
= m_treeCtrl
->GetRootItem();
435 wxTreeItemId idLast
= m_treeCtrl
->GetLastChild(idRoot
);
436 status
= wxString::Format(
437 wxT("Root/last item is %svisible/%svisible"),
438 m_treeCtrl
->IsVisible(idRoot
) ? wxT("") : wxT("not "),
439 idLast
.IsOk() && m_treeCtrl
->IsVisible(idLast
)
440 ? wxT("") : wxT("not "));
443 status
= wxT("No root item");
445 SetStatusText(status
, 1);
447 #endif // wxUSE_STATUSBAR
452 void MyFrame::OnSize(wxSizeEvent
& event
)
466 void MyFrame::Resize()
468 wxSize size
= GetClientSize();
469 m_treeCtrl
->SetSize(0, 0, size
.x
, size
.y
472 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3
477 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
482 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
484 wxMessageBox(wxT("Tree test sample\n")
485 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
486 wxT("About tree test"),
487 wxOK
| wxICON_INFORMATION
, this);
490 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
495 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
497 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
501 // old code - now we edit in place
503 static wxString s_text
;
504 s_text
= wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
506 if ( !s_text
.empty() )
508 m_treeCtrl
->SetItemText(item
, s_text
);
512 // TODO demonstrate creating a custom edit control...
513 (void)m_treeCtrl
->EditLabel(item
);
516 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
518 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
522 int i
= m_treeCtrl
->GetChildrenCount( item
, false );
524 wxLogMessage(wxT("%d children"), i
);
527 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
529 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
533 int i
= m_treeCtrl
->GetChildrenCount( item
);
535 wxLogMessage(wxT("%d children"), i
);
538 void MyFrame::DoSort(bool reverse
)
540 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
544 m_treeCtrl
->DoSortChildren(item
, reverse
);
547 void MyFrame::OnHighlight(wxCommandEvent
& WXUNUSED(event
))
549 wxTreeItemId id
= m_treeCtrl
->GetFocusedItem();
554 if ( !m_treeCtrl
->GetBoundingRect(id
, r
, true /* text, not full row */) )
556 wxLogMessage(wxT("Failed to get bounding item rect"));
560 wxClientDC
dc(m_treeCtrl
);
562 dc
.SetPen(*wxTRANSPARENT_PEN
);
564 m_treeCtrl
->Update();
567 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
569 wxTreeItemId root
= m_treeCtrl
->GetFocusedItem();
573 m_treeCtrl
->GetItemsRecursively(root
);
576 #ifndef NO_MULTIPLE_SELECTION
578 void MyFrame::OnToggleSel(wxCommandEvent
& event
)
580 TogStyle(event
.GetId(), wxTR_MULTIPLE
);
583 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
585 wxArrayTreeItemIds array
;
587 size_t count
= m_treeCtrl
->GetSelections(array
);
588 wxLogMessage(wxT("%u items selected"), unsigned(count
));
590 for ( size_t n
= 0; n
< count
; n
++ )
592 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
596 void MyFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
598 m_treeCtrl
->SelectItem(m_treeCtrl
->GetFocusedItem());
601 void MyFrame::OnSelectRoot(wxCommandEvent
& WXUNUSED(event
))
603 if ( !m_treeCtrl
->HasFlag(wxTR_HIDE_ROOT
) )
604 m_treeCtrl
->SelectItem(m_treeCtrl
->GetRootItem());
607 void MyFrame::OnSetFocusedRoot(wxCommandEvent
& WXUNUSED(event
))
609 if ( !m_treeCtrl
->HasFlag(wxTR_HIDE_ROOT
) )
610 m_treeCtrl
->SetFocusedItem(m_treeCtrl
->GetRootItem());
613 void MyFrame::OnClearFocused(wxCommandEvent
& WXUNUSED(event
))
615 m_treeCtrl
->ClearFocusedItem();
618 void MyFrame::OnUnselect(wxCommandEvent
& WXUNUSED(event
))
620 m_treeCtrl
->UnselectAll();
623 void MyFrame::OnSelectChildren(wxCommandEvent
& WXUNUSED(event
))
625 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
627 item
= m_treeCtrl
->GetRootItem();
629 m_treeCtrl
->SelectChildren(item
);
632 #endif // NO_MULTIPLE_SELECTION
634 void MyFrame::DoSetBold(bool bold
)
636 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
640 m_treeCtrl
->SetItemBold(item
, bold
);
643 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
645 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
649 m_treeCtrl
->Delete(item
);
652 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
654 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
658 m_treeCtrl
->DeleteChildren(item
);
661 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
663 m_treeCtrl
->DeleteAllItems();
666 void MyFrame::OnRecreate(wxCommandEvent
& event
)
669 m_treeCtrl
->AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL
, NUM_LEVELS
);
672 void MyFrame::OnSetImageSize(wxCommandEvent
& WXUNUSED(event
))
674 int size
= wxGetNumberFromUser(wxT("Enter the size for the images to use"),
676 wxT("TreeCtrl sample"),
677 m_treeCtrl
->ImageSize());
681 m_treeCtrl
->CreateImageList(size
);
682 wxGetApp().SetShowImages(true);
685 void MyFrame::OnToggleImages(wxCommandEvent
& WXUNUSED(event
))
687 if ( wxGetApp().ShowImages() )
689 m_treeCtrl
->CreateImageList(-1);
690 wxGetApp().SetShowImages(false);
694 m_treeCtrl
->CreateImageList(0);
695 wxGetApp().SetShowImages(true);
699 void MyFrame::OnToggleStates(wxCommandEvent
& WXUNUSED(event
))
701 if ( wxGetApp().ShowStates() )
703 m_treeCtrl
->CreateStateImageList(true);
704 wxGetApp().SetShowStates(false);
708 m_treeCtrl
->CreateStateImageList(false);
709 wxGetApp().SetShowStates(true);
713 void MyFrame::OnToggleBell(wxCommandEvent
& event
)
715 m_treeCtrl
->EnableBellOnNoMatch(event
.IsChecked());
718 void MyFrame::OnToggleAlternateImages(wxCommandEvent
& WXUNUSED(event
))
720 bool alternateImages
= m_treeCtrl
->AlternateImages();
722 m_treeCtrl
->SetAlternateImages(!alternateImages
);
723 m_treeCtrl
->CreateImageList(0);
726 void MyFrame::OnToggleAlternateStates(wxCommandEvent
& WXUNUSED(event
))
728 bool alternateStates
= m_treeCtrl
->AlternateStates();
730 m_treeCtrl
->SetAlternateStates(!alternateStates
);
731 m_treeCtrl
->CreateStateImageList();
733 // normal states < alternate states
734 // so we must reset broken states
735 if ( alternateStates
)
736 m_treeCtrl
->ResetBrokenStateImages();
739 void MyFrame::OnToggleButtons(wxCommandEvent
& WXUNUSED(event
))
741 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
742 if ( wxGetApp().ShowButtons() )
744 m_treeCtrl
->CreateButtonsImageList(-1);
745 wxGetApp().SetShowButtons(false);
749 m_treeCtrl
->CreateButtonsImageList(15);
750 wxGetApp().SetShowButtons(true);
755 void MyFrame::OnCollapseAndReset(wxCommandEvent
& WXUNUSED(event
))
757 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
760 void MyFrame::OnEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
762 m_treeCtrl
->DoEnsureVisible();
765 void MyFrame::OnSetFocus(wxCommandEvent
& WXUNUSED(event
))
767 m_treeCtrl
->SetFocus();
770 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
772 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
773 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, wxT("2nd item"));
776 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
778 static int s_num
= 0;
781 text
.Printf(wxT("Item #%d"), ++s_num
);
783 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
785 MyTreeCtrl::TreeCtrlIcon_File */ );
788 void MyFrame::OnAddManyItems(wxCommandEvent
& WXUNUSED(event
))
790 wxWindowUpdateLocker
lockUpdates(this);
792 const wxTreeItemId root
= m_treeCtrl
->GetRootItem();
793 for ( int n
= 0; n
< 1000; n
++ )
795 m_treeCtrl
->AppendItem(root
, wxString::Format("Item #%03d", n
));
799 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
801 unsigned int indent
= m_treeCtrl
->GetIndent();
803 m_treeCtrl
->SetIndent( indent
+5 );
806 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
808 unsigned int indent
= m_treeCtrl
->GetIndent();
810 m_treeCtrl
->SetIndent( indent
-5 );
813 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
815 unsigned int indent
= m_treeCtrl
->GetSpacing();
818 m_treeCtrl
->SetSpacing( indent
+5 );
819 m_treeCtrl
->Refresh();
823 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
825 unsigned int indent
= m_treeCtrl
->GetSpacing();
828 m_treeCtrl
->SetSpacing( indent
-5 );
829 m_treeCtrl
->Refresh();
833 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
835 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
839 m_treeCtrl
->DoToggleIcon(item
);
842 void MyFrame::OnToggleState(wxCommandEvent
& WXUNUSED(event
))
844 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
848 m_treeCtrl
->DoToggleState(item
);
851 void MyFrame::DoShowFirstOrLast(TreeFunc0_t pfn
, const wxString
& label
)
853 const wxTreeItemId item
= (m_treeCtrl
->*pfn
)();
857 wxLogMessage("There is no %s item", label
);
861 wxLogMessage("The %s item is \"%s\"",
862 label
, m_treeCtrl
->GetItemText(item
));
866 void MyFrame::DoShowRelativeItem(TreeFunc1_t pfn
, const wxString
& label
)
868 wxTreeItemId item
= m_treeCtrl
->GetFocusedItem();
872 if ((pfn
== (TreeFunc1_t
) &wxTreeCtrl::GetPrevVisible
873 || pfn
== (TreeFunc1_t
) &wxTreeCtrl::GetNextVisible
)
874 && !m_treeCtrl
->IsVisible(item
))
876 wxLogMessage("The selected item must be visible.");
880 wxTreeItemId new_item
= (m_treeCtrl
->*pfn
)(item
);
882 if ( !new_item
.IsOk() )
884 wxLogMessage("There is no %s item", label
);
888 wxLogMessage("The %s item is \"%s\"",
889 label
, m_treeCtrl
->GetItemText(new_item
));
893 void MyFrame::OnScrollTo(wxCommandEvent
& WXUNUSED(event
))
895 // scroll to the last but one top level child
896 wxTreeItemId item
= m_treeCtrl
->GetPrevSibling(
897 m_treeCtrl
->GetLastChild(
898 m_treeCtrl
->GetRootItem()));
901 m_treeCtrl
->ScrollTo(item
);
904 void MyFrame::OnSelectLast(wxCommandEvent
& WXUNUSED(event
))
906 // select the very last item of the tree
907 wxTreeItemId item
= m_treeCtrl
->GetRootItem();
910 wxTreeItemId itemChild
= m_treeCtrl
->GetLastChild(item
);
911 if ( !itemChild
.IsOk() )
919 m_treeCtrl
->SelectItem(item
);
922 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
924 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
926 m_treeCtrl
->SetForegroundColour(col
);
929 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
931 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
933 m_treeCtrl
->SetBackgroundColour(col
);
936 // MyTreeCtrl implementation
937 #if USE_GENERIC_TREECTRL
938 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
940 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
943 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
944 const wxPoint
& pos
, const wxSize
& size
,
946 : wxTreeCtrl(parent
, id
, pos
, size
, style
),
947 m_alternateImages(false),
948 m_alternateStates(false)
950 m_reverseSort
= false;
953 CreateStateImageList();
955 // Add some items to the tree
956 AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL
, NUM_LEVELS
);
959 void MyTreeCtrl::CreateImageList(int size
)
971 // Make an image list containing small icons
972 wxImageList
*images
= new wxImageList(size
, size
, true);
974 // should correspond to TreeCtrlIcon_xxx enum
978 if (m_alternateImages
)
980 icons
[TreeCtrlIcon_File
] = wxIcon(icon1_xpm
);
981 icons
[TreeCtrlIcon_FileSelected
] = wxIcon(icon2_xpm
);
982 icons
[TreeCtrlIcon_Folder
] = wxIcon(icon3_xpm
);
983 icons
[TreeCtrlIcon_FolderSelected
] = wxIcon(icon4_xpm
);
984 icons
[TreeCtrlIcon_FolderOpened
] = wxIcon(icon5_xpm
);
988 wxSize
iconSize(size
, size
);
990 icons
[TreeCtrlIcon_File
] =
991 icons
[TreeCtrlIcon_FileSelected
] = wxArtProvider::GetIcon(wxART_NORMAL_FILE
, wxART_LIST
, iconSize
);
992 icons
[TreeCtrlIcon_Folder
] =
993 icons
[TreeCtrlIcon_FolderSelected
] =
994 icons
[TreeCtrlIcon_FolderOpened
] = wxArtProvider::GetIcon(wxART_FOLDER
, wxART_LIST
, iconSize
);
997 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
999 int sizeOrig
= icons
[0].GetWidth();
1000 if ( size
== sizeOrig
)
1002 images
->Add(icons
[i
]);
1006 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
1010 AssignImageList(images
);
1013 void MyTreeCtrl::CreateStateImageList(bool del
)
1017 SetStateImageList(NULL
);
1021 wxImageList
*states
;
1024 if (m_alternateStates
)
1027 icons
[0] = wxIcon(state1_xpm
); // yellow
1028 icons
[1] = wxIcon(state2_xpm
); // green
1029 icons
[2] = wxIcon(state3_xpm
); // red
1030 icons
[3] = wxIcon(state4_xpm
); // blue
1031 icons
[4] = wxIcon(state5_xpm
); // black
1033 int width
= icons
[0].GetWidth(),
1034 height
= icons
[0].GetHeight();
1036 // Make an state image list containing small icons
1037 states
= new wxImageList(width
, height
, true);
1039 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
1040 states
->Add(icons
[i
]);
1045 icons
[0] = wxIcon(unchecked_xpm
);
1046 icons
[1] = wxIcon(checked_xpm
);
1048 int width
= icons
[0].GetWidth(),
1049 height
= icons
[0].GetHeight();
1051 // Make an state image list containing small icons
1052 states
= new wxImageList(width
, height
, true);
1054 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
1055 states
->Add(icons
[i
]);
1058 AssignStateImageList(states
);
1061 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
1062 void MyTreeCtrl::CreateButtonsImageList(int size
)
1066 SetButtonsImageList(NULL
);
1070 // Make an image list containing small icons
1071 wxImageList
*images
= new wxImageList(size
, size
, true);
1073 // should correspond to TreeCtrlIcon_xxx enum
1077 if (m_alternateImages
)
1079 icons
[0] = wxIcon(icon3_xpm
); // closed
1080 icons
[1] = wxIcon(icon3_xpm
); // closed, selected
1081 icons
[2] = wxIcon(icon5_xpm
); // open
1082 icons
[3] = wxIcon(icon5_xpm
); // open, selected
1086 wxSize
iconSize(size
, size
);
1088 icons
[0] = // closed
1089 icons
[1] = wxArtProvider::GetIcon(wxART_FOLDER
, wxART_LIST
, iconSize
); // closed, selected
1091 icons
[3] = wxArtProvider::GetIcon(wxART_FOLDER_OPEN
, wxART_LIST
, iconSize
);// open, selected
1094 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
1096 int sizeOrig
= icons
[i
].GetWidth();
1097 if ( size
== sizeOrig
)
1099 images
->Add(icons
[i
]);
1103 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
1107 AssignButtonsImageList(images
);
1109 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size
))
1114 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1115 const wxTreeItemId
& item2
)
1117 if ( m_reverseSort
)
1119 // just exchange 1st and 2nd items
1120 return wxTreeCtrl::OnCompareItems(item2
, item1
);
1124 return wxTreeCtrl::OnCompareItems(item1
, item2
);
1128 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
1135 bool hasChildren
= depth
> 1;
1138 for ( size_t n
= 0; n
< numChildren
; n
++ )
1140 // at depth 1 elements won't have any more children
1142 str
.Printf(wxT("%s child %u"), wxT("Folder"), unsigned(n
+ 1));
1144 str
.Printf(wxT("%s child %u.%u"), wxT("File"), unsigned(folder
), unsigned(n
+ 1));
1146 // here we pass to AppendItem() normal and selected item images (we
1147 // suppose that selected image follows the normal one in the enum)
1148 int image
, imageSel
;
1149 if ( wxGetApp().ShowImages() )
1151 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
1152 imageSel
= image
+ 1;
1156 image
= imageSel
= -1;
1158 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
1159 new MyTreeItemData(str
));
1161 if ( wxGetApp().ShowStates() )
1162 SetItemState(id
, 0);
1164 // and now we also set the expanded one (only for the folders)
1165 if ( hasChildren
&& wxGetApp().ShowImages() )
1167 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
1168 wxTreeItemIcon_Expanded
);
1171 // remember the last child for OnEnsureVisible()
1172 if ( !hasChildren
&& n
== numChildren
- 1 )
1177 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
1183 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
1186 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
1187 wxTreeItemId rootId
= AddRoot(wxT("Root"),
1189 new MyTreeItemData(wxT("Root item")));
1190 if ( !HasFlag(wxTR_HIDE_ROOT
) && image
!= -1 )
1192 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
1195 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
1197 // set some colours/fonts for testing
1198 if ( !HasFlag(wxTR_HIDE_ROOT
) )
1199 SetItemFont(rootId
, *wxITALIC_FONT
);
1201 wxTreeItemIdValue cookie
;
1202 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
1203 SetItemTextColour(id
, *wxBLUE
);
1205 id
= GetNextChild(rootId
, cookie
);
1207 id
= GetNextChild(rootId
, cookie
);
1210 SetItemTextColour(id
, *wxRED
);
1211 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
1215 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
,
1216 wxTreeItemIdValue cookie
)
1221 id
= GetFirstChild(idParent
, cookie
);
1223 id
= GetNextChild(idParent
, cookie
);
1228 wxString text
= GetItemText(id
);
1231 if (ItemHasChildren(id
))
1232 GetItemsRecursively(id
);
1234 GetItemsRecursively(idParent
, cookie
);
1237 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
1239 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
1241 : TreeCtrlIcon_Folder
;
1242 SetItemImage(item
, image
, wxTreeItemIcon_Normal
);
1244 image
= GetItemImage(item
, wxTreeItemIcon_Selected
) == TreeCtrlIcon_FolderSelected
1245 ? TreeCtrlIcon_FileSelected
1246 : TreeCtrlIcon_FolderSelected
;
1247 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1250 void MyTreeCtrl::DoToggleState(const wxTreeItemId
& item
)
1252 if ( m_alternateStates
)
1254 // sets random state unlike current
1255 int state
= GetItemState(item
);
1260 nState
= rand() % GetStateImageList()->GetImageCount();
1261 } while (nState
== state
);
1263 SetItemState(item
, nState
);
1267 // we have only 2 checkbox states, so next state will be reversed
1268 SetItemState(item
, wxTREE_ITEMSTATE_NEXT
);
1272 void MyTreeCtrl::DoResetBrokenStateImages(const wxTreeItemId
& idParent
,
1273 wxTreeItemIdValue cookie
, int state
)
1278 id
= GetFirstChild(idParent
, cookie
);
1280 id
= GetNextChild(idParent
, cookie
);
1285 int curState
= GetItemState(id
);
1286 if ( curState
!= wxTREE_ITEMSTATE_NONE
&& curState
> state
)
1287 SetItemState(id
, state
);
1289 if (ItemHasChildren(id
))
1290 DoResetBrokenStateImages(id
, 0, state
);
1292 DoResetBrokenStateImages(idParent
, cookie
, state
);
1295 void MyTreeCtrl::LogEvent(const wxChar
*name
, const wxTreeEvent
& event
)
1297 wxTreeItemId item
= event
.GetItem();
1300 text
<< wxT('"') << GetItemText(item
).c_str() << wxT('"');
1302 text
= wxT("invalid item");
1303 wxLogMessage(wxT("%s(%s)"), name
, text
.c_str());
1307 #define TREE_EVENT_HANDLER(name) \
1308 void MyTreeCtrl::name(wxTreeEvent& event) \
1310 LogEvent(wxT(#name), event); \
1311 SetLastItem(wxTreeItemId()); \
1315 TREE_EVENT_HANDLER(OnBeginRDrag
)
1316 TREE_EVENT_HANDLER(OnDeleteItem
)
1317 TREE_EVENT_HANDLER(OnGetInfo
)
1318 TREE_EVENT_HANDLER(OnSetInfo
)
1319 TREE_EVENT_HANDLER(OnItemExpanded
)
1320 TREE_EVENT_HANDLER(OnItemExpanding
)
1321 TREE_EVENT_HANDLER(OnItemCollapsed
)
1322 TREE_EVENT_HANDLER(OnSelChanged
)
1323 TREE_EVENT_HANDLER(OnSelChanging
)
1325 #undef TREE_EVENT_HANDLER
1327 void LogKeyEvent(const wxChar
*name
, const wxKeyEvent
& event
)
1330 long keycode
= event
.GetKeyCode();
1334 case WXK_BACK
: key
= wxT("BACK"); break;
1335 case WXK_TAB
: key
= wxT("TAB"); break;
1336 case WXK_RETURN
: key
= wxT("RETURN"); break;
1337 case WXK_ESCAPE
: key
= wxT("ESCAPE"); break;
1338 case WXK_SPACE
: key
= wxT("SPACE"); break;
1339 case WXK_DELETE
: key
= wxT("DELETE"); break;
1340 case WXK_START
: key
= wxT("START"); break;
1341 case WXK_LBUTTON
: key
= wxT("LBUTTON"); break;
1342 case WXK_RBUTTON
: key
= wxT("RBUTTON"); break;
1343 case WXK_CANCEL
: key
= wxT("CANCEL"); break;
1344 case WXK_MBUTTON
: key
= wxT("MBUTTON"); break;
1345 case WXK_CLEAR
: key
= wxT("CLEAR"); break;
1346 case WXK_SHIFT
: key
= wxT("SHIFT"); break;
1347 case WXK_ALT
: key
= wxT("ALT"); break;
1348 case WXK_CONTROL
: key
= wxT("CONTROL"); break;
1349 case WXK_MENU
: key
= wxT("MENU"); break;
1350 case WXK_PAUSE
: key
= wxT("PAUSE"); break;
1351 case WXK_CAPITAL
: key
= wxT("CAPITAL"); break;
1352 case WXK_END
: key
= wxT("END"); break;
1353 case WXK_HOME
: key
= wxT("HOME"); break;
1354 case WXK_LEFT
: key
= wxT("LEFT"); break;
1355 case WXK_UP
: key
= wxT("UP"); break;
1356 case WXK_RIGHT
: key
= wxT("RIGHT"); break;
1357 case WXK_DOWN
: key
= wxT("DOWN"); break;
1358 case WXK_SELECT
: key
= wxT("SELECT"); break;
1359 case WXK_PRINT
: key
= wxT("PRINT"); break;
1360 case WXK_EXECUTE
: key
= wxT("EXECUTE"); break;
1361 case WXK_SNAPSHOT
: key
= wxT("SNAPSHOT"); break;
1362 case WXK_INSERT
: key
= wxT("INSERT"); break;
1363 case WXK_HELP
: key
= wxT("HELP"); break;
1364 case WXK_NUMPAD0
: key
= wxT("NUMPAD0"); break;
1365 case WXK_NUMPAD1
: key
= wxT("NUMPAD1"); break;
1366 case WXK_NUMPAD2
: key
= wxT("NUMPAD2"); break;
1367 case WXK_NUMPAD3
: key
= wxT("NUMPAD3"); break;
1368 case WXK_NUMPAD4
: key
= wxT("NUMPAD4"); break;
1369 case WXK_NUMPAD5
: key
= wxT("NUMPAD5"); break;
1370 case WXK_NUMPAD6
: key
= wxT("NUMPAD6"); break;
1371 case WXK_NUMPAD7
: key
= wxT("NUMPAD7"); break;
1372 case WXK_NUMPAD8
: key
= wxT("NUMPAD8"); break;
1373 case WXK_NUMPAD9
: key
= wxT("NUMPAD9"); break;
1374 case WXK_MULTIPLY
: key
= wxT("MULTIPLY"); break;
1375 case WXK_ADD
: key
= wxT("ADD"); break;
1376 case WXK_SEPARATOR
: key
= wxT("SEPARATOR"); break;
1377 case WXK_SUBTRACT
: key
= wxT("SUBTRACT"); break;
1378 case WXK_DECIMAL
: key
= wxT("DECIMAL"); break;
1379 case WXK_DIVIDE
: key
= wxT("DIVIDE"); break;
1380 case WXK_F1
: key
= wxT("F1"); break;
1381 case WXK_F2
: key
= wxT("F2"); break;
1382 case WXK_F3
: key
= wxT("F3"); break;
1383 case WXK_F4
: key
= wxT("F4"); break;
1384 case WXK_F5
: key
= wxT("F5"); break;
1385 case WXK_F6
: key
= wxT("F6"); break;
1386 case WXK_F7
: key
= wxT("F7"); break;
1387 case WXK_F8
: key
= wxT("F8"); break;
1388 case WXK_F9
: key
= wxT("F9"); break;
1389 case WXK_F10
: key
= wxT("F10"); break;
1390 case WXK_F11
: key
= wxT("F11"); break;
1391 case WXK_F12
: key
= wxT("F12"); break;
1392 case WXK_F13
: key
= wxT("F13"); break;
1393 case WXK_F14
: key
= wxT("F14"); break;
1394 case WXK_F15
: key
= wxT("F15"); break;
1395 case WXK_F16
: key
= wxT("F16"); break;
1396 case WXK_F17
: key
= wxT("F17"); break;
1397 case WXK_F18
: key
= wxT("F18"); break;
1398 case WXK_F19
: key
= wxT("F19"); break;
1399 case WXK_F20
: key
= wxT("F20"); break;
1400 case WXK_F21
: key
= wxT("F21"); break;
1401 case WXK_F22
: key
= wxT("F22"); break;
1402 case WXK_F23
: key
= wxT("F23"); break;
1403 case WXK_F24
: key
= wxT("F24"); break;
1404 case WXK_NUMLOCK
: key
= wxT("NUMLOCK"); break;
1405 case WXK_SCROLL
: key
= wxT("SCROLL"); break;
1406 case WXK_PAGEUP
: key
= wxT("PAGEUP"); break;
1407 case WXK_PAGEDOWN
: key
= wxT("PAGEDOWN"); break;
1408 case WXK_NUMPAD_SPACE
: key
= wxT("NUMPAD_SPACE"); break;
1409 case WXK_NUMPAD_TAB
: key
= wxT("NUMPAD_TAB"); break;
1410 case WXK_NUMPAD_ENTER
: key
= wxT("NUMPAD_ENTER"); break;
1411 case WXK_NUMPAD_F1
: key
= wxT("NUMPAD_F1"); break;
1412 case WXK_NUMPAD_F2
: key
= wxT("NUMPAD_F2"); break;
1413 case WXK_NUMPAD_F3
: key
= wxT("NUMPAD_F3"); break;
1414 case WXK_NUMPAD_F4
: key
= wxT("NUMPAD_F4"); break;
1415 case WXK_NUMPAD_HOME
: key
= wxT("NUMPAD_HOME"); break;
1416 case WXK_NUMPAD_LEFT
: key
= wxT("NUMPAD_LEFT"); break;
1417 case WXK_NUMPAD_UP
: key
= wxT("NUMPAD_UP"); break;
1418 case WXK_NUMPAD_RIGHT
: key
= wxT("NUMPAD_RIGHT"); break;
1419 case WXK_NUMPAD_DOWN
: key
= wxT("NUMPAD_DOWN"); break;
1420 case WXK_NUMPAD_PAGEUP
: key
= wxT("NUMPAD_PAGEUP"); break;
1421 case WXK_NUMPAD_PAGEDOWN
: key
= wxT("NUMPAD_PAGEDOWN"); break;
1422 case WXK_NUMPAD_END
: key
= wxT("NUMPAD_END"); break;
1423 case WXK_NUMPAD_BEGIN
: key
= wxT("NUMPAD_BEGIN"); break;
1424 case WXK_NUMPAD_INSERT
: key
= wxT("NUMPAD_INSERT"); break;
1425 case WXK_NUMPAD_DELETE
: key
= wxT("NUMPAD_DELETE"); break;
1426 case WXK_NUMPAD_EQUAL
: key
= wxT("NUMPAD_EQUAL"); break;
1427 case WXK_NUMPAD_MULTIPLY
: key
= wxT("NUMPAD_MULTIPLY"); break;
1428 case WXK_NUMPAD_ADD
: key
= wxT("NUMPAD_ADD"); break;
1429 case WXK_NUMPAD_SEPARATOR
: key
= wxT("NUMPAD_SEPARATOR"); break;
1430 case WXK_NUMPAD_SUBTRACT
: key
= wxT("NUMPAD_SUBTRACT"); break;
1431 case WXK_NUMPAD_DECIMAL
: key
= wxT("NUMPAD_DECIMAL"); break;
1435 if ( keycode
< 128 && wxIsprint((int)keycode
) )
1436 key
.Printf(wxT("'%c'"), (char)keycode
);
1437 else if ( keycode
> 0 && keycode
< 27 )
1438 key
.Printf(_("Ctrl-%c"), wxT('A') + keycode
- 1);
1440 key
.Printf(wxT("unknown (%ld)"), keycode
);
1445 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1448 event
.ControlDown() ? wxT('C') : wxT('-'),
1449 event
.AltDown() ? wxT('A') : wxT('-'),
1450 event
.ShiftDown() ? wxT('S') : wxT('-'),
1451 event
.MetaDown() ? wxT('M') : wxT('-'));
1454 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
& event
)
1456 LogKeyEvent(wxT("Tree key down "), event
.GetKeyEvent());
1461 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
1463 // need to explicitly allow drag
1464 if ( event
.GetItem() != GetRootItem() )
1466 m_draggedItem
= event
.GetItem();
1468 wxPoint clientpt
= event
.GetPoint();
1469 wxPoint screenpt
= ClientToScreen(clientpt
);
1471 wxLogMessage(wxT("OnBeginDrag: started dragging %s at screen coords (%i,%i)"),
1472 GetItemText(m_draggedItem
).c_str(),
1473 screenpt
.x
, screenpt
.y
);
1479 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1483 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
1485 wxTreeItemId itemSrc
= m_draggedItem
,
1486 itemDst
= event
.GetItem();
1487 m_draggedItem
= (wxTreeItemId
)0l;
1489 // where to copy the item?
1490 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
1492 // copy to the parent then
1493 itemDst
= GetItemParent(itemDst
);
1496 if ( !itemDst
.IsOk() )
1498 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1503 wxString text
= GetItemText(itemSrc
);
1504 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1505 text
.c_str(), GetItemText(itemDst
).c_str());
1507 // just do append here - we could also insert it just before/after the item
1508 // on which it was dropped, but this requires slightly more work... we also
1509 // completely ignore the client data and icon of the old item but could
1510 // copy them as well.
1512 // Finally, we only copy one item here but we might copy the entire tree if
1513 // we were dragging a folder.
1514 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
1515 wxTreeItemId id
= AppendItem(itemDst
, text
, image
);
1517 if ( wxGetApp().ShowStates() )
1518 SetItemState(id
, GetItemState(itemSrc
));
1521 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
1523 wxLogMessage(wxT("OnBeginLabelEdit"));
1525 // for testing, prevent this item's label editing
1526 wxTreeItemId itemId
= event
.GetItem();
1527 if ( IsTestItem(itemId
) )
1529 wxMessageBox(wxT("You can't edit this item."));
1533 else if ( itemId
== GetRootItem() )
1535 // test that it is possible to change the text of the item being edited
1536 SetItemText(itemId
, wxT("Editing root item"));
1540 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
1542 wxLogMessage(wxT("OnEndLabelEdit"));
1544 // don't allow anything except letters in the labels
1545 if ( !event
.GetLabel().IsWord() )
1547 wxMessageBox(wxT("The new label should be a single word."));
1553 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
1555 wxLogMessage(wxT("OnItemCollapsing"));
1557 // for testing, prevent the user from collapsing the first child folder
1558 wxTreeItemId itemId
= event
.GetItem();
1559 if ( IsTestItem(itemId
) )
1561 wxMessageBox(wxT("You can't collapse this item."));
1567 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
1569 // show some info about this item
1570 wxTreeItemId itemId
= event
.GetItem();
1571 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1575 item
->ShowInfo(this);
1578 wxLogMessage(wxT("OnItemActivated"));
1581 void MyTreeCtrl::OnItemStateClick(wxTreeEvent
& event
)
1583 // toggle item state
1584 wxTreeItemId itemId
= event
.GetItem();
1585 DoToggleState(itemId
);
1587 wxLogMessage(wxT("Item \"%s\" state changed to %d"),
1588 GetItemText(itemId
), GetItemState(itemId
));
1591 void MyTreeCtrl::OnItemMenu(wxTreeEvent
& event
)
1593 wxTreeItemId itemId
= event
.GetItem();
1594 wxCHECK_RET( itemId
.IsOk(), "should have a valid item" );
1596 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1597 wxPoint clientpt
= event
.GetPoint();
1598 wxPoint screenpt
= ClientToScreen(clientpt
);
1600 wxLogMessage(wxT("OnItemMenu for item \"%s\" at screen coords (%i, %i)"),
1601 item
->GetDesc(), screenpt
.x
, screenpt
.y
);
1603 ShowMenu(itemId
, clientpt
);
1607 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1609 wxPoint pt
= event
.GetPosition();
1611 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt
.x
, pt
.y
);
1616 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
1621 title
<< wxT("Menu for ") << GetItemText(id
);
1625 title
= wxT("Menu for no particular item");
1630 menu
.Append(TreeTest_About
, wxT("&About"));
1631 menu
.AppendSeparator();
1632 menu
.Append(TreeTest_Highlight
, wxT("&Highlight item"));
1633 menu
.Append(TreeTest_Dump
, wxT("&Dump"));
1635 PopupMenu(&menu
, pt
);
1636 #endif // wxUSE_MENUS
1639 void MyTreeCtrl::OnItemRClick(wxTreeEvent
& event
)
1641 wxTreeItemId itemId
= event
.GetItem();
1642 wxCHECK_RET( itemId
.IsOk(), "should have a valid item" );
1644 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1646 wxLogMessage(wxT("Item \"%s\" right clicked"), item
->GetDesc());
1651 void MyTreeCtrl::OnRMouseDown(wxMouseEvent
& event
)
1653 wxLogMessage(wxT("Right mouse button down"));
1658 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
1660 wxLogMessage(wxT("Right mouse button up"));
1665 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
1667 wxTreeItemId id
= HitTest(event
.GetPosition());
1670 wxLogMessage(wxT("No item under mouse"));
1674 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
1677 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1684 static inline const wxChar
*Bool2String(bool b
)
1686 return b
? wxT("") : wxT("not ");
1689 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1691 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1692 wxT("%u children (%u immediately under this item)."),
1694 Bool2String(tree
->IsSelected(GetId())),
1695 Bool2String(tree
->IsExpanded(GetId())),
1696 Bool2String(tree
->IsBold(GetId())),
1697 unsigned(tree
->GetChildrenCount(GetId())),
1698 unsigned(tree
->GetChildrenCount(GetId(), false)));