1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
29 #include "wx/colordlg.h"
32 #include "wx/imaglist.h"
33 #include "wx/treectrl.h"
38 // this is not supported by native control
39 #define NO_VARIABLE_HEIGHT
49 #include "mondrian.xpm"
52 // verify that the item is ok and insult the user if it is not
53 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
54 wxMessageBox(wxT("Please select some item first!"), \
55 wxT("Tree sample error"), \
56 wxOK | wxICON_EXCLAMATION, \
61 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
63 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
64 EVT_SIZE(MyFrame::OnSize
)
73 MENU_LINK(TogHideRoot
)
74 MENU_LINK(TogRootLines
)
76 MENU_LINK(TogFullHighlight
)
77 MENU_LINK(SetFgColour
)
78 MENU_LINK(SetBgColour
)
82 #ifndef NO_MULTIPLE_SELECTION
83 MENU_LINK(DumpSelected
)
87 #endif // NO_MULTIPLE_SELECTION
96 MENU_LINK(DeleteChildren
)
99 MENU_LINK(ToggleImages
)
100 MENU_LINK(ToggleButtons
)
101 MENU_LINK(SetImageSize
)
102 MENU_LINK(CollapseAndReset
)
103 MENU_LINK(EnsureVisible
)
105 MENU_LINK(InsertItem
)
108 MENU_LINK(IncSpacing
)
109 MENU_LINK(DecSpacing
)
110 MENU_LINK(ToggleIcon
)
115 #if USE_GENERIC_TREECTRL
116 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
118 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
120 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
121 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
122 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
123 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
124 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
125 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
126 #if 0 // there are so many of those that logging them causes flicker
127 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
129 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
130 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
131 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
132 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
133 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
134 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRightClick
)
136 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
137 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
138 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
139 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
140 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
141 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
148 // Create the main frame window
149 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
159 // My frame constructor
160 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
161 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)),
162 m_treeCtrl(NULL
), m_textCtrl(NULL
)
164 // This reduces flicker effects - even better would be to define
165 // OnEraseBackground to do nothing. When the tree control's scrollbars are
166 // show or hidden, the frame is sent a background erase event.
167 SetBackgroundColour(wxColour(255, 255, 255));
170 SetIcon(wxICON(mondrian
));
174 wxMenu
*file_menu
= new wxMenu
,
175 *style_menu
= new wxMenu
,
176 *tree_menu
= new wxMenu
,
177 *item_menu
= new wxMenu
;
179 file_menu
->Append(TreeTest_About
, wxT("&About..."));
180 file_menu
->AppendSeparator();
181 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
183 style_menu
->Append(TreeTest_TogButtons
, wxT("Toggle &normal buttons"), wxT(""), TRUE
);
184 style_menu
->Append(TreeTest_TogTwist
, wxT("Toggle &twister buttons"), wxT(""), TRUE
);
185 style_menu
->Append(TreeTest_ToggleButtons
, wxT("Toggle image &buttons"), wxT(""), TRUE
);
186 style_menu
->AppendSeparator();
187 style_menu
->Append(TreeTest_TogLines
, wxT("Toggle &connecting lines"), wxT(""), TRUE
);
188 style_menu
->Append(TreeTest_TogRootLines
, wxT("Toggle &lines at root"), wxT(""), TRUE
);
189 style_menu
->Append(TreeTest_TogHideRoot
, wxT("Toggle &hidden root"), wxT(""), TRUE
);
190 style_menu
->Append(TreeTest_TogBorder
, wxT("Toggle &item border"), wxT(""), TRUE
);
191 style_menu
->Append(TreeTest_TogFullHighlight
, wxT("Toggle &full row highlight"), wxT(""), TRUE
);
192 style_menu
->Append(TreeTest_TogEdit
, wxT("Toggle &edit mode"), wxT(""), TRUE
);
193 #ifndef NO_MULTIPLE_SELECTION
194 style_menu
->Append(TreeTest_ToggleSel
, wxT("Toggle &selection mode"), wxT(""), TRUE
);
195 #endif // NO_MULTIPLE_SELECTION
196 style_menu
->Append(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"), wxT(""), TRUE
);
197 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
198 style_menu
->AppendSeparator();
199 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
200 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
201 style_menu
->AppendSeparator();
202 style_menu
->Append(TreeTest_ResetStyle
, wxT("&Reset to default\tF10"));
204 tree_menu
->Append(TreeTest_Recreate
, wxT("&Recreate the tree"));
205 tree_menu
->Append(TreeTest_CollapseAndReset
, wxT("C&ollapse and reset"));
206 tree_menu
->AppendSeparator();
207 tree_menu
->Append(TreeTest_AddItem
, wxT("Append a &new item"));
208 tree_menu
->Append(TreeTest_InsertItem
, wxT("&Insert a new item"));
209 tree_menu
->Append(TreeTest_Delete
, wxT("&Delete this item"));
210 tree_menu
->Append(TreeTest_DeleteChildren
, wxT("Delete &children"));
211 tree_menu
->Append(TreeTest_DeleteAll
, wxT("Delete &all items"));
212 tree_menu
->AppendSeparator();
213 tree_menu
->Append(TreeTest_Count
, wxT("Count children of current item"));
214 tree_menu
->Append(TreeTest_CountRec
, wxT("Recursively count children of current item"));
215 tree_menu
->AppendSeparator();
216 tree_menu
->Append(TreeTest_Sort
, wxT("Sort children of current item"));
217 tree_menu
->Append(TreeTest_SortRev
, wxT("Sort in reversed order"));
218 tree_menu
->AppendSeparator();
219 tree_menu
->Append(TreeTest_EnsureVisible
, wxT("Make the last item &visible"));
220 tree_menu
->AppendSeparator();
221 tree_menu
->Append(TreeTest_IncIndent
, wxT("Add 5 points to indentation\tAlt-I"));
222 tree_menu
->Append(TreeTest_DecIndent
, wxT("Reduce indentation by 5 points\tAlt-R"));
223 tree_menu
->AppendSeparator();
224 tree_menu
->Append(TreeTest_IncSpacing
, wxT("Add 5 points to spacing\tCtrl-I"));
225 tree_menu
->Append(TreeTest_DecSpacing
, wxT("Reduce spacing by 5 points\tCtrl-R"));
227 item_menu
->Append(TreeTest_Dump
, wxT("&Dump item children"));
228 item_menu
->Append(TreeTest_Rename
, wxT("&Rename item..."));
230 item_menu
->AppendSeparator();
231 item_menu
->Append(TreeTest_SetBold
, wxT("Make item &bold"));
232 item_menu
->Append(TreeTest_ClearBold
, wxT("Make item ¬ bold"));
233 item_menu
->AppendSeparator();
234 item_menu
->Append(TreeTest_ToggleIcon
, wxT("Toggle the item's &icon"));
236 #ifndef NO_MULTIPLE_SELECTION
237 item_menu
->AppendSeparator();
238 item_menu
->Append(TreeTest_DumpSelected
, wxT("Dump selected items\tAlt-D"));
239 item_menu
->Append(TreeTest_Select
, wxT("Select current item\tAlt-S"));
240 item_menu
->Append(TreeTest_Unselect
, wxT("Unselect everything\tAlt-U"));
241 #endif // NO_MULTIPLE_SELECTION
243 wxMenuBar
*menu_bar
= new wxMenuBar
;
244 menu_bar
->Append(file_menu
, wxT("&File"));
245 menu_bar
->Append(style_menu
, wxT("&Style"));
246 menu_bar
->Append(tree_menu
, wxT("&Tree"));
247 menu_bar
->Append(item_menu
, wxT("&Item"));
248 SetMenuBar(menu_bar
);
249 #endif // wxUSE_MENUS
251 // create the controls
252 m_textCtrl
= new wxTextCtrl(this, -1, wxT(""),
253 wxDefaultPosition
, wxDefaultSize
,
254 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
256 CreateTreeWithDefStyle();
258 menu_bar
->Check(TreeTest_ToggleImages
, TRUE
);
260 // create a status bar with 3 panes
262 SetStatusText(wxT(""), 0);
265 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
266 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
267 delete wxLog::SetActiveTarget(new wxLogStderr
);
269 // set our text control as the log target
270 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
271 delete wxLog::SetActiveTarget(logWindow
);
277 delete wxLog::SetActiveTarget(NULL
);
280 void MyFrame::CreateTreeWithDefStyle()
282 long style
= wxTR_DEFAULT_STYLE
|
283 #ifndef NO_VARIABLE_HEIGHT
284 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
288 CreateTree(style
| wxSUNKEN_BORDER
);
290 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
292 wxMenuBar
*mbar
= GetMenuBar();
293 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_HAS_BUTTONS
) != 0);
294 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_TWIST_BUTTONS
) != 0);
295 mbar
->Check(TreeTest_TogLines
, (style
& wxTR_NO_LINES
) == 0);
296 mbar
->Check(TreeTest_TogRootLines
, (style
& wxTR_LINES_AT_ROOT
) != 0);
297 mbar
->Check(TreeTest_TogHideRoot
, (style
& wxTR_HIDE_ROOT
) != 0);
298 mbar
->Check(TreeTest_TogEdit
, (style
& wxTR_EDIT_LABELS
) != 0);
299 mbar
->Check(TreeTest_TogBorder
, (style
& wxTR_ROW_LINES
) != 0);
300 mbar
->Check(TreeTest_TogFullHighlight
, (style
& wxTR_FULL_ROW_HIGHLIGHT
) != 0);
303 void MyFrame::CreateTree(long style
)
305 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
306 wxDefaultPosition
, wxDefaultSize
,
311 void MyFrame::TogStyle(int id
, long flag
)
313 long style
= m_treeCtrl
->GetWindowStyle() ^ flag
;
315 // most treectrl styles can't be changed on the fly using the native
316 // control and the tree must be recreated
318 m_treeCtrl
->SetWindowStyle(style
);
324 GetMenuBar()->Check(id
, (style
& flag
) != 0);
327 void MyFrame::OnSize(wxSizeEvent
& event
)
329 if ( m_treeCtrl
&& m_textCtrl
)
337 void MyFrame::Resize()
339 wxSize size
= GetClientSize();
340 m_treeCtrl
->SetSize(0, 0, size
.x
, 2*size
.y
/3);
341 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3);
344 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
349 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
351 wxMessageBox(wxT("Tree test sample\n")
352 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
353 wxT("About tree test"),
354 wxOK
| wxICON_INFORMATION
, this);
357 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
359 wxTreeItemId item
= m_treeCtrl
->GetSelection();
363 // old code - now we edit in place
365 static wxString s_text
;
366 s_text
= wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
368 if ( !s_text
.IsEmpty() )
370 m_treeCtrl
->SetItemText(item
, s_text
);
374 // TODO demonstrate creating a custom edit control...
375 (void)m_treeCtrl
->EditLabel(item
);
378 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
380 wxTreeItemId item
= m_treeCtrl
->GetSelection();
384 int i
= m_treeCtrl
->GetChildrenCount( item
, FALSE
);
386 wxLogMessage(wxT("%d children"), i
);
389 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
391 wxTreeItemId item
= m_treeCtrl
->GetSelection();
395 int i
= m_treeCtrl
->GetChildrenCount( item
);
397 wxLogMessage(wxT("%d children"), i
);
400 void MyFrame::DoSort(bool reverse
)
402 wxTreeItemId item
= m_treeCtrl
->GetSelection();
406 m_treeCtrl
->DoSortChildren(item
, reverse
);
409 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
411 wxTreeItemId root
= m_treeCtrl
->GetSelection();
415 m_treeCtrl
->GetItemsRecursively(root
, -1);
418 #ifndef NO_MULTIPLE_SELECTION
420 void MyFrame::OnToggleSel(wxCommandEvent
& event
)
422 TogStyle(event
.GetId(), wxTR_MULTIPLE
);
425 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
427 wxArrayTreeItemIds array
;
429 size_t count
= m_treeCtrl
->GetSelections(array
);
430 wxLogMessage(wxT("%u items selected"), count
);
432 for ( size_t n
= 0; n
< count
; n
++ )
434 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
438 void MyFrame::OnSelect(wxCommandEvent
& event
)
440 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
443 void MyFrame::OnUnselect(wxCommandEvent
& event
)
445 m_treeCtrl
->UnselectAll();
448 #endif // NO_MULTIPLE_SELECTION
450 void MyFrame::DoSetBold(bool bold
)
452 wxTreeItemId item
= m_treeCtrl
->GetSelection();
456 m_treeCtrl
->SetItemBold(item
, bold
);
459 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
461 wxTreeItemId item
= m_treeCtrl
->GetSelection();
465 m_treeCtrl
->Delete(item
);
468 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
470 wxTreeItemId item
= m_treeCtrl
->GetSelection();
474 m_treeCtrl
->DeleteChildren(item
);
477 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
479 m_treeCtrl
->DeleteAllItems();
482 void MyFrame::OnRecreate(wxCommandEvent
& event
)
485 m_treeCtrl
->AddTestItemsToTree(5, 2);
488 void MyFrame::OnSetImageSize(wxCommandEvent
& event
)
490 int size
= wxGetNumberFromUser(wxT("Enter the size for the images to use"),
492 wxT("TreeCtrl sample"),
493 m_treeCtrl
->ImageSize());
497 m_treeCtrl
->CreateImageList(size
);
498 wxGetApp().SetShowImages(TRUE
);
501 void MyFrame::OnToggleImages(wxCommandEvent
& event
)
503 if ( wxGetApp().ShowImages() )
505 m_treeCtrl
->CreateImageList(-1);
506 wxGetApp().SetShowImages(FALSE
);
510 m_treeCtrl
->CreateImageList(0);
511 wxGetApp().SetShowImages(TRUE
);
515 void MyFrame::OnToggleButtons(wxCommandEvent
& event
)
517 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
518 if ( wxGetApp().ShowButtons() )
520 m_treeCtrl
->CreateButtonsImageList(-1);
521 wxGetApp().SetShowButtons(FALSE
);
525 m_treeCtrl
->CreateButtonsImageList(15);
526 wxGetApp().SetShowButtons(TRUE
);
531 void MyFrame::OnCollapseAndReset(wxCommandEvent
& event
)
533 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
536 void MyFrame::OnEnsureVisible(wxCommandEvent
& event
)
538 m_treeCtrl
->DoEnsureVisible();
541 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
543 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
544 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, wxT("2nd item"));
547 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
549 static int s_num
= 0;
552 text
.Printf(wxT("Item #%d"), ++s_num
);
554 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
556 MyTreeCtrl::TreeCtrlIcon_File */ );
559 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
561 unsigned int indent
= m_treeCtrl
->GetIndent();
563 m_treeCtrl
->SetIndent( indent
+5 );
566 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
568 unsigned int indent
= m_treeCtrl
->GetIndent();
570 m_treeCtrl
->SetIndent( indent
-5 );
573 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
575 unsigned int indent
= m_treeCtrl
->GetSpacing();
577 m_treeCtrl
->SetSpacing( indent
+5 );
580 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
582 unsigned int indent
= m_treeCtrl
->GetSpacing();
584 m_treeCtrl
->SetSpacing( indent
-5 );
587 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
589 wxTreeItemId item
= m_treeCtrl
->GetSelection();
593 m_treeCtrl
->DoToggleIcon(item
);
596 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
598 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
600 m_treeCtrl
->SetForegroundColour(col
);
603 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
605 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
607 m_treeCtrl
->SetBackgroundColour(col
);
610 // MyTreeCtrl implementation
611 #if USE_GENERIC_TREECTRL
612 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
614 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
617 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
618 const wxPoint
& pos
, const wxSize
& size
,
620 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
622 m_reverseSort
= FALSE
;
626 // Add some items to the tree
627 AddTestItemsToTree(5, 2);
630 void MyTreeCtrl::CreateImageList(int size
)
642 // Make an image list containing small icons
643 wxImageList
*images
= new wxImageList(size
, size
, TRUE
);
645 // should correspond to TreeCtrlIcon_xxx enum
648 icons
[0] = wxIcon(icon1_xpm
);
649 icons
[1] = wxIcon(icon2_xpm
);
650 icons
[2] = wxIcon(icon3_xpm
);
651 icons
[3] = wxIcon(icon4_xpm
);
652 icons
[4] = wxIcon(icon5_xpm
);
654 int sizeOrig
= icons
[0].GetWidth();
655 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
657 if ( size
== sizeOrig
)
659 images
->Add(icons
[i
]);
663 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
667 AssignImageList(images
);
670 void MyTreeCtrl::CreateButtonsImageList(int size
)
672 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
675 SetButtonsImageList(NULL
);
679 // Make an image list containing small icons
680 wxImageList
*images
= new wxImageList(size
, size
, TRUE
);
682 // should correspond to TreeCtrlIcon_xxx enum
685 icons
[0] = wxIcon(icon3_xpm
); // closed
686 icons
[1] = wxIcon(icon3_xpm
); // closed, selected
687 icons
[2] = wxIcon(icon5_xpm
); // open
688 icons
[3] = wxIcon(icon5_xpm
); // open, selected
690 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
692 int sizeOrig
= icons
[i
].GetWidth();
693 if ( size
== sizeOrig
)
695 images
->Add(icons
[i
]);
699 images
->Add(wxBitmap(icons
[i
].ConvertToImage().Rescale(size
, size
)));
703 AssignButtonsImageList(images
);
707 MyTreeCtrl::~MyTreeCtrl()
711 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
712 const wxTreeItemId
& item2
)
716 // just exchange 1st and 2nd items
717 return wxTreeCtrl::OnCompareItems(item2
, item1
);
721 return wxTreeCtrl::OnCompareItems(item1
, item2
);
725 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
732 bool hasChildren
= depth
> 1;
735 for ( size_t n
= 0; n
< numChildren
; n
++ )
737 // at depth 1 elements won't have any more children
739 str
.Printf(wxT("%s child %d"), wxT("Folder"), n
+ 1);
741 str
.Printf(wxT("%s child %d.%d"), wxT("File"), folder
, n
+ 1);
743 // here we pass to AppendItem() normal and selected item images (we
744 // suppose that selected image follows the normal one in the enum)
746 if ( wxGetApp().ShowImages() )
748 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
749 imageSel
= image
+ 1;
753 image
= imageSel
= -1;
755 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
756 new MyTreeItemData(str
));
758 // and now we also set the expanded one (only for the folders)
759 if ( hasChildren
&& wxGetApp().ShowImages() )
761 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
762 wxTreeItemIcon_Expanded
);
765 // remember the last child for OnEnsureVisible()
766 if ( !hasChildren
&& n
== numChildren
- 1 )
771 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
777 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
780 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
781 wxTreeItemId rootId
= AddRoot(wxT("Root"),
783 new MyTreeItemData(wxT("Root item")));
786 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
789 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
791 // set some colours/fonts for testing
792 SetItemFont(rootId
, *wxITALIC_FONT
);
795 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
796 SetItemTextColour(id
, *wxBLUE
);
798 id
= GetNextChild(rootId
, cookie
);
799 id
= GetNextChild(rootId
, cookie
);
800 SetItemTextColour(id
, *wxRED
);
801 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
804 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
809 id
= GetFirstChild(idParent
, cookie
);
811 id
= GetNextChild(idParent
, cookie
);
816 wxString text
= GetItemText(id
);
819 if (ItemHasChildren(id
))
820 GetItemsRecursively(id
,-1);
822 GetItemsRecursively(idParent
, cookie
);
825 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
827 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
828 : TreeCtrlIcon_Folder
;
830 SetItemImage(item
, image
);
835 #define TREE_EVENT_HANDLER(name) \
836 void MyTreeCtrl::name(wxTreeEvent& event) \
838 wxLogMessage(wxT(#name)); \
842 TREE_EVENT_HANDLER(OnBeginRDrag
)
843 TREE_EVENT_HANDLER(OnDeleteItem
)
844 TREE_EVENT_HANDLER(OnGetInfo
)
845 TREE_EVENT_HANDLER(OnSetInfo
)
846 TREE_EVENT_HANDLER(OnItemExpanded
)
847 TREE_EVENT_HANDLER(OnItemExpanding
)
848 TREE_EVENT_HANDLER(OnItemCollapsed
)
849 TREE_EVENT_HANDLER(OnSelChanged
)
850 TREE_EVENT_HANDLER(OnSelChanging
)
852 #undef TREE_EVENT_HANDLER
854 void LogKeyEvent(const wxChar
*name
, const wxKeyEvent
& event
)
857 long keycode
= event
.KeyCode();
861 case WXK_BACK
: key
= wxT("BACK"); break;
862 case WXK_TAB
: key
= wxT("TAB"); break;
863 case WXK_RETURN
: key
= wxT("RETURN"); break;
864 case WXK_ESCAPE
: key
= wxT("ESCAPE"); break;
865 case WXK_SPACE
: key
= wxT("SPACE"); break;
866 case WXK_DELETE
: key
= wxT("DELETE"); break;
867 case WXK_START
: key
= wxT("START"); break;
868 case WXK_LBUTTON
: key
= wxT("LBUTTON"); break;
869 case WXK_RBUTTON
: key
= wxT("RBUTTON"); break;
870 case WXK_CANCEL
: key
= wxT("CANCEL"); break;
871 case WXK_MBUTTON
: key
= wxT("MBUTTON"); break;
872 case WXK_CLEAR
: key
= wxT("CLEAR"); break;
873 case WXK_SHIFT
: key
= wxT("SHIFT"); break;
874 case WXK_ALT
: key
= wxT("ALT"); break;
875 case WXK_CONTROL
: key
= wxT("CONTROL"); break;
876 case WXK_MENU
: key
= wxT("MENU"); break;
877 case WXK_PAUSE
: key
= wxT("PAUSE"); break;
878 case WXK_CAPITAL
: key
= wxT("CAPITAL"); break;
879 case WXK_PRIOR
: key
= wxT("PRIOR"); break;
880 case WXK_NEXT
: key
= wxT("NEXT"); break;
881 case WXK_END
: key
= wxT("END"); break;
882 case WXK_HOME
: key
= wxT("HOME"); break;
883 case WXK_LEFT
: key
= wxT("LEFT"); break;
884 case WXK_UP
: key
= wxT("UP"); break;
885 case WXK_RIGHT
: key
= wxT("RIGHT"); break;
886 case WXK_DOWN
: key
= wxT("DOWN"); break;
887 case WXK_SELECT
: key
= wxT("SELECT"); break;
888 case WXK_PRINT
: key
= wxT("PRINT"); break;
889 case WXK_EXECUTE
: key
= wxT("EXECUTE"); break;
890 case WXK_SNAPSHOT
: key
= wxT("SNAPSHOT"); break;
891 case WXK_INSERT
: key
= wxT("INSERT"); break;
892 case WXK_HELP
: key
= wxT("HELP"); break;
893 case WXK_NUMPAD0
: key
= wxT("NUMPAD0"); break;
894 case WXK_NUMPAD1
: key
= wxT("NUMPAD1"); break;
895 case WXK_NUMPAD2
: key
= wxT("NUMPAD2"); break;
896 case WXK_NUMPAD3
: key
= wxT("NUMPAD3"); break;
897 case WXK_NUMPAD4
: key
= wxT("NUMPAD4"); break;
898 case WXK_NUMPAD5
: key
= wxT("NUMPAD5"); break;
899 case WXK_NUMPAD6
: key
= wxT("NUMPAD6"); break;
900 case WXK_NUMPAD7
: key
= wxT("NUMPAD7"); break;
901 case WXK_NUMPAD8
: key
= wxT("NUMPAD8"); break;
902 case WXK_NUMPAD9
: key
= wxT("NUMPAD9"); break;
903 case WXK_MULTIPLY
: key
= wxT("MULTIPLY"); break;
904 case WXK_ADD
: key
= wxT("ADD"); break;
905 case WXK_SEPARATOR
: key
= wxT("SEPARATOR"); break;
906 case WXK_SUBTRACT
: key
= wxT("SUBTRACT"); break;
907 case WXK_DECIMAL
: key
= wxT("DECIMAL"); break;
908 case WXK_DIVIDE
: key
= wxT("DIVIDE"); break;
909 case WXK_F1
: key
= wxT("F1"); break;
910 case WXK_F2
: key
= wxT("F2"); break;
911 case WXK_F3
: key
= wxT("F3"); break;
912 case WXK_F4
: key
= wxT("F4"); break;
913 case WXK_F5
: key
= wxT("F5"); break;
914 case WXK_F6
: key
= wxT("F6"); break;
915 case WXK_F7
: key
= wxT("F7"); break;
916 case WXK_F8
: key
= wxT("F8"); break;
917 case WXK_F9
: key
= wxT("F9"); break;
918 case WXK_F10
: key
= wxT("F10"); break;
919 case WXK_F11
: key
= wxT("F11"); break;
920 case WXK_F12
: key
= wxT("F12"); break;
921 case WXK_F13
: key
= wxT("F13"); break;
922 case WXK_F14
: key
= wxT("F14"); break;
923 case WXK_F15
: key
= wxT("F15"); break;
924 case WXK_F16
: key
= wxT("F16"); break;
925 case WXK_F17
: key
= wxT("F17"); break;
926 case WXK_F18
: key
= wxT("F18"); break;
927 case WXK_F19
: key
= wxT("F19"); break;
928 case WXK_F20
: key
= wxT("F20"); break;
929 case WXK_F21
: key
= wxT("F21"); break;
930 case WXK_F22
: key
= wxT("F22"); break;
931 case WXK_F23
: key
= wxT("F23"); break;
932 case WXK_F24
: key
= wxT("F24"); break;
933 case WXK_NUMLOCK
: key
= wxT("NUMLOCK"); break;
934 case WXK_SCROLL
: key
= wxT("SCROLL"); break;
935 case WXK_PAGEUP
: key
= wxT("PAGEUP"); break;
936 case WXK_PAGEDOWN
: key
= wxT("PAGEDOWN"); break;
937 case WXK_NUMPAD_SPACE
: key
= wxT("NUMPAD_SPACE"); break;
938 case WXK_NUMPAD_TAB
: key
= wxT("NUMPAD_TAB"); break;
939 case WXK_NUMPAD_ENTER
: key
= wxT("NUMPAD_ENTER"); break;
940 case WXK_NUMPAD_F1
: key
= wxT("NUMPAD_F1"); break;
941 case WXK_NUMPAD_F2
: key
= wxT("NUMPAD_F2"); break;
942 case WXK_NUMPAD_F3
: key
= wxT("NUMPAD_F3"); break;
943 case WXK_NUMPAD_F4
: key
= wxT("NUMPAD_F4"); break;
944 case WXK_NUMPAD_HOME
: key
= wxT("NUMPAD_HOME"); break;
945 case WXK_NUMPAD_LEFT
: key
= wxT("NUMPAD_LEFT"); break;
946 case WXK_NUMPAD_UP
: key
= wxT("NUMPAD_UP"); break;
947 case WXK_NUMPAD_RIGHT
: key
= wxT("NUMPAD_RIGHT"); break;
948 case WXK_NUMPAD_DOWN
: key
= wxT("NUMPAD_DOWN"); break;
949 case WXK_NUMPAD_PRIOR
: key
= wxT("NUMPAD_PRIOR"); break;
950 case WXK_NUMPAD_PAGEUP
: key
= wxT("NUMPAD_PAGEUP"); break;
951 case WXK_NUMPAD_PAGEDOWN
: key
= wxT("NUMPAD_PAGEDOWN"); break;
952 case WXK_NUMPAD_END
: key
= wxT("NUMPAD_END"); break;
953 case WXK_NUMPAD_BEGIN
: key
= wxT("NUMPAD_BEGIN"); break;
954 case WXK_NUMPAD_INSERT
: key
= wxT("NUMPAD_INSERT"); break;
955 case WXK_NUMPAD_DELETE
: key
= wxT("NUMPAD_DELETE"); break;
956 case WXK_NUMPAD_EQUAL
: key
= wxT("NUMPAD_EQUAL"); break;
957 case WXK_NUMPAD_MULTIPLY
: key
= wxT("NUMPAD_MULTIPLY"); break;
958 case WXK_NUMPAD_ADD
: key
= wxT("NUMPAD_ADD"); break;
959 case WXK_NUMPAD_SEPARATOR
: key
= wxT("NUMPAD_SEPARATOR"); break;
960 case WXK_NUMPAD_SUBTRACT
: key
= wxT("NUMPAD_SUBTRACT"); break;
961 case WXK_NUMPAD_DECIMAL
: key
= wxT("NUMPAD_DECIMAL"); break;
965 if ( wxIsprint((int)keycode
) )
966 key
.Printf(wxT("'%c'"), (char)keycode
);
967 else if ( keycode
> 0 && keycode
< 27 )
968 key
.Printf(_("Ctrl-%c"), wxT('A') + keycode
- 1);
970 key
.Printf(wxT("unknown (%ld)"), keycode
);
975 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
978 event
.ControlDown() ? wxT('C') : wxT('-'),
979 event
.AltDown() ? wxT('A') : wxT('-'),
980 event
.ShiftDown() ? wxT('S') : wxT('-'),
981 event
.MetaDown() ? wxT('M') : wxT('-'));
984 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
& event
)
986 LogKeyEvent(wxT("Tree key down "), event
.GetKeyEvent());
991 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
993 // need to explicitly allow drag
994 if ( event
.GetItem() != GetRootItem() )
996 m_draggedItem
= event
.GetItem();
998 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
999 GetItemText(m_draggedItem
).c_str());
1005 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1009 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
1011 wxTreeItemId itemSrc
= m_draggedItem
,
1012 itemDst
= event
.GetItem();
1013 m_draggedItem
= (wxTreeItemId
)0l;
1015 // where to copy the item?
1016 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
1018 // copy to the parent then
1019 itemDst
= GetItemParent(itemDst
);
1022 if ( !itemDst
.IsOk() )
1024 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1029 wxString text
= GetItemText(itemSrc
);
1030 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1031 text
.c_str(), GetItemText(itemDst
).c_str());
1033 // just do append here - we could also insert it just before/after the item
1034 // on which it was dropped, but this requires slightly more work... we also
1035 // completely ignore the client data and icon of the old item but could
1036 // copy them as well.
1038 // Finally, we only copy one item here but we might copy the entire tree if
1039 // we were dragging a folder.
1040 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
1041 AppendItem(itemDst
, text
, image
);
1044 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
1046 wxLogMessage(wxT("OnBeginLabelEdit"));
1048 // for testing, prevent this item's label editing
1049 wxTreeItemId itemId
= event
.GetItem();
1050 if ( IsTestItem(itemId
) )
1052 wxMessageBox(wxT("You can't edit this item."));
1058 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
1060 wxLogMessage(wxT("OnEndLabelEdit"));
1062 // don't allow anything except letters in the labels
1063 if ( !event
.GetLabel().IsWord() )
1065 wxMessageBox(wxT("The new label should be a single word."));
1071 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
1073 wxLogMessage(wxT("OnItemCollapsing"));
1075 // for testing, prevent the user from collapsing the first child folder
1076 wxTreeItemId itemId
= event
.GetItem();
1077 if ( IsTestItem(itemId
) )
1079 wxMessageBox(wxT("You can't collapse this item."));
1085 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
1087 // show some info about this item
1088 wxTreeItemId itemId
= event
.GetItem();
1089 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1093 item
->ShowInfo(this);
1096 wxLogMessage(wxT("OnItemActivated"));
1099 void MyTreeCtrl::OnItemRightClick(wxTreeEvent
& event
)
1101 ShowMenu(event
.GetItem(), event
.GetPoint());
1104 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
1106 wxPoint pt
= event
.GetPosition();
1107 ShowMenu(HitTest(pt
), pt
);
1110 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
1115 title
<< wxT("Menu for ") << GetItemText(id
);
1119 title
= wxT("Menu for no particular item");
1124 menu
.Append(TreeTest_About
, wxT("&About..."));
1125 menu
.Append(TreeTest_Dump
, wxT("&Dump"));
1127 PopupMenu(&menu
, pt
);
1128 #endif // wxUSE_MENUS
1131 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
1133 wxTreeItemId id
= HitTest(event
.GetPosition());
1135 wxLogMessage(wxT("No item under mouse"));
1138 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
1140 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1144 static inline const wxChar
*Bool2String(bool b
)
1146 return b
? wxT("") : wxT("not ");
1149 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1151 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1152 wxT("%u children (%u immediately under this item)."),
1154 Bool2String(tree
->IsSelected(GetId())),
1155 Bool2String(tree
->IsExpanded(GetId())),
1156 Bool2String(tree
->IsBold(GetId())),
1157 tree
->GetChildrenCount(GetId()),
1158 tree
->GetChildrenCount(GetId(), FALSE
));