1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
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"
30 #include "wx/numdlg.h"
33 #include "wx/imaglist.h"
34 #include "wx/treectrl.h"
38 // this is not supported by native control
39 #define NO_VARIABLE_HEIGHT
50 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
51 #include "mondrian.xpm"
55 // verify that the item is ok and insult the user if it is not
56 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
57 wxMessageBox(wxT("Please select some item first!"), \
58 wxT("Tree sample error"), \
59 wxOK | wxICON_EXCLAMATION, \
64 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
66 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
67 EVT_IDLE(MyFrame::OnIdle
)
68 EVT_SIZE(MyFrame::OnSize
)
77 MENU_LINK(TogHideRoot
)
78 MENU_LINK(TogRootLines
)
80 MENU_LINK(TogFullHighlight
)
81 MENU_LINK(SetFgColour
)
82 MENU_LINK(SetBgColour
)
86 #ifndef NO_MULTIPLE_SELECTION
87 MENU_LINK(DumpSelected
)
91 #endif // NO_MULTIPLE_SELECTION
100 MENU_LINK(DeleteChildren
)
103 MENU_LINK(ToggleImages
)
104 MENU_LINK(ToggleButtons
)
105 MENU_LINK(SetImageSize
)
106 MENU_LINK(CollapseAndReset
)
107 MENU_LINK(EnsureVisible
)
109 MENU_LINK(InsertItem
)
112 MENU_LINK(IncSpacing
)
113 MENU_LINK(DecSpacing
)
114 MENU_LINK(ToggleIcon
)
115 MENU_LINK(SelectRoot
)
120 #if USE_GENERIC_TREECTRL
121 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
123 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
125 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
126 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
127 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
128 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
129 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
130 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
131 #if 0 // there are so many of those that logging them causes flicker
132 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
134 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
135 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
136 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
137 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
138 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
139 //EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRightClick)
141 EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu
)
142 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
143 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
144 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
145 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
146 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
153 // Create the main frame window
154 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
164 // My frame constructor
165 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
166 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
172 // This reduces flicker effects - even better would be to define
173 // OnEraseBackground to do nothing. When the tree control's scrollbars are
174 // show or hidden, the frame is sent a background erase event.
175 SetBackgroundColour(wxColour(255, 255, 255));
178 SetIcon(wxICON(mondrian
));
182 wxMenu
*file_menu
= new wxMenu
,
183 *style_menu
= new wxMenu
,
184 *tree_menu
= new wxMenu
,
185 *item_menu
= new wxMenu
;
187 file_menu
->Append(TreeTest_About
, wxT("&About..."));
188 file_menu
->AppendSeparator();
189 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
191 style_menu
->AppendCheckItem(TreeTest_TogButtons
, wxT("Toggle &normal buttons"));
192 style_menu
->AppendCheckItem(TreeTest_TogTwist
, wxT("Toggle &twister buttons"));
193 style_menu
->AppendCheckItem(TreeTest_ToggleButtons
, wxT("Toggle image &buttons"));
194 style_menu
->AppendSeparator();
195 style_menu
->AppendCheckItem(TreeTest_TogLines
, wxT("Toggle &connecting lines"));
196 style_menu
->AppendCheckItem(TreeTest_TogRootLines
, wxT("Toggle &lines at root"));
197 style_menu
->AppendCheckItem(TreeTest_TogHideRoot
, wxT("Toggle &hidden root"));
198 style_menu
->AppendCheckItem(TreeTest_TogBorder
, wxT("Toggle &item border"));
199 style_menu
->AppendCheckItem(TreeTest_TogFullHighlight
, wxT("Toggle &full row highlight"));
200 style_menu
->AppendCheckItem(TreeTest_TogEdit
, wxT("Toggle &edit mode"));
201 #ifndef NO_MULTIPLE_SELECTION
202 style_menu
->AppendCheckItem(TreeTest_ToggleSel
, wxT("Toggle &selection mode"));
203 #endif // NO_MULTIPLE_SELECTION
204 style_menu
->AppendCheckItem(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
205 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
206 style_menu
->AppendSeparator();
207 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
208 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
209 style_menu
->AppendSeparator();
210 style_menu
->Append(TreeTest_ResetStyle
, wxT("&Reset to default\tF10"));
212 tree_menu
->Append(TreeTest_Recreate
, wxT("&Recreate the tree"));
213 tree_menu
->Append(TreeTest_CollapseAndReset
, wxT("C&ollapse and reset"));
214 tree_menu
->AppendSeparator();
215 tree_menu
->Append(TreeTest_AddItem
, wxT("Append a &new item"));
216 tree_menu
->Append(TreeTest_InsertItem
, wxT("&Insert a new item"));
217 tree_menu
->Append(TreeTest_Delete
, wxT("&Delete this item"));
218 tree_menu
->Append(TreeTest_DeleteChildren
, wxT("Delete &children"));
219 tree_menu
->Append(TreeTest_DeleteAll
, wxT("Delete &all items"));
220 tree_menu
->Append(TreeTest_SelectRoot
, wxT("Select root item"));
221 tree_menu
->AppendSeparator();
222 tree_menu
->Append(TreeTest_Count
, wxT("Count children of current item"));
223 tree_menu
->Append(TreeTest_CountRec
, wxT("Recursively count children of current item"));
224 tree_menu
->AppendSeparator();
225 tree_menu
->Append(TreeTest_Sort
, wxT("Sort children of current item"));
226 tree_menu
->Append(TreeTest_SortRev
, wxT("Sort in reversed order"));
227 tree_menu
->AppendSeparator();
228 tree_menu
->Append(TreeTest_EnsureVisible
, wxT("Make the last item &visible"));
229 tree_menu
->AppendSeparator();
230 tree_menu
->Append(TreeTest_IncIndent
, wxT("Add 5 points to indentation\tAlt-I"));
231 tree_menu
->Append(TreeTest_DecIndent
, wxT("Reduce indentation by 5 points\tAlt-R"));
232 tree_menu
->AppendSeparator();
233 tree_menu
->Append(TreeTest_IncSpacing
, wxT("Add 5 points to spacing\tCtrl-I"));
234 tree_menu
->Append(TreeTest_DecSpacing
, wxT("Reduce spacing by 5 points\tCtrl-R"));
236 item_menu
->Append(TreeTest_Dump
, wxT("&Dump item children"));
237 item_menu
->Append(TreeTest_Rename
, wxT("&Rename item..."));
239 item_menu
->AppendSeparator();
240 item_menu
->Append(TreeTest_SetBold
, wxT("Make item &bold"));
241 item_menu
->Append(TreeTest_ClearBold
, wxT("Make item ¬ bold"));
242 item_menu
->AppendSeparator();
243 item_menu
->Append(TreeTest_ToggleIcon
, wxT("Toggle the item's &icon"));
245 #ifndef NO_MULTIPLE_SELECTION
246 item_menu
->AppendSeparator();
247 item_menu
->Append(TreeTest_DumpSelected
, wxT("Dump selected items\tAlt-D"));
248 item_menu
->Append(TreeTest_Select
, wxT("Select current item\tAlt-S"));
249 item_menu
->Append(TreeTest_Unselect
, wxT("Unselect everything\tAlt-U"));
250 #endif // NO_MULTIPLE_SELECTION
252 wxMenuBar
*menu_bar
= new wxMenuBar
;
253 menu_bar
->Append(file_menu
, wxT("&File"));
254 menu_bar
->Append(style_menu
, wxT("&Style"));
255 menu_bar
->Append(tree_menu
, wxT("&Tree"));
256 menu_bar
->Append(item_menu
, wxT("&Item"));
257 SetMenuBar(menu_bar
);
258 #endif // wxUSE_MENUS
261 // create the controls
262 m_textCtrl
= new wxTextCtrl(this, wxID_ANY
, wxT(""),
263 wxDefaultPosition
, wxDefaultSize
,
264 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
267 CreateTreeWithDefStyle();
269 menu_bar
->Check(TreeTest_ToggleImages
, true);
272 // create a status bar
274 #endif // wxUSE_STATUSBAR
278 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
279 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
280 delete wxLog::SetActiveTarget(new wxLogStderr
);
282 // set our text control as the log target
283 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
284 delete wxLog::SetActiveTarget(logWindow
);
292 delete wxLog::SetActiveTarget(NULL
);
296 void MyFrame::CreateTreeWithDefStyle()
298 long style
= wxTR_DEFAULT_STYLE
|
299 #ifndef NO_VARIABLE_HEIGHT
300 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
304 CreateTree(style
| wxSUNKEN_BORDER
);
306 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
308 wxMenuBar
*mbar
= GetMenuBar();
309 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_HAS_BUTTONS
) != 0);
310 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_TWIST_BUTTONS
) != 0);
311 mbar
->Check(TreeTest_TogLines
, (style
& wxTR_NO_LINES
) == 0);
312 mbar
->Check(TreeTest_TogRootLines
, (style
& wxTR_LINES_AT_ROOT
) != 0);
313 mbar
->Check(TreeTest_TogHideRoot
, (style
& wxTR_HIDE_ROOT
) != 0);
314 mbar
->Check(TreeTest_TogEdit
, (style
& wxTR_EDIT_LABELS
) != 0);
315 mbar
->Check(TreeTest_TogBorder
, (style
& wxTR_ROW_LINES
) != 0);
316 mbar
->Check(TreeTest_TogFullHighlight
, (style
& wxTR_FULL_ROW_HIGHLIGHT
) != 0);
319 void MyFrame::CreateTree(long style
)
321 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
322 wxDefaultPosition
, wxDefaultSize
,
327 void MyFrame::TogStyle(int id
, long flag
)
329 long style
= m_treeCtrl
->GetWindowStyle() ^ flag
;
331 // most treectrl styles can't be changed on the fly using the native
332 // control and the tree must be recreated
334 m_treeCtrl
->SetWindowStyle(style
);
340 GetMenuBar()->Check(id
, (style
& flag
) != 0);
343 void MyFrame::OnIdle(wxIdleEvent
& event
)
348 wxTreeItemId idRoot
= m_treeCtrl
->GetRootItem();
350 SetStatusText(wxString::Format
352 _T("Root/last item is %svisible/%svisible"),
353 m_treeCtrl
->IsVisible(idRoot
) ? _T("") : _T("not "),
354 m_treeCtrl
->IsVisible(m_treeCtrl
->GetLastChild(idRoot
))
355 ? _T("") : _T("not ")
358 #endif // wxUSE_STATUSBAR
363 void MyFrame::OnSize(wxSizeEvent
& event
)
377 void MyFrame::Resize()
379 wxSize size
= GetClientSize();
380 m_treeCtrl
->SetSize(0, 0, size
.x
, size
.y
385 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3);
389 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
394 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
396 wxMessageBox(wxT("Tree test sample\n")
397 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
398 wxT("About tree test"),
399 wxOK
| wxICON_INFORMATION
, this);
402 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
404 wxTreeItemId item
= m_treeCtrl
->GetSelection();
408 // old code - now we edit in place
410 static wxString s_text
;
411 s_text
= wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
413 if ( !s_text
.empty() )
415 m_treeCtrl
->SetItemText(item
, s_text
);
419 // TODO demonstrate creating a custom edit control...
420 (void)m_treeCtrl
->EditLabel(item
);
423 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
425 wxTreeItemId item
= m_treeCtrl
->GetSelection();
429 int i
= m_treeCtrl
->GetChildrenCount( item
, false );
431 wxLogMessage(wxT("%d children"), i
);
434 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
436 wxTreeItemId item
= m_treeCtrl
->GetSelection();
440 int i
= m_treeCtrl
->GetChildrenCount( item
);
442 wxLogMessage(wxT("%d children"), i
);
445 void MyFrame::DoSort(bool reverse
)
447 wxTreeItemId item
= m_treeCtrl
->GetSelection();
451 m_treeCtrl
->DoSortChildren(item
, reverse
);
454 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
456 wxTreeItemId root
= m_treeCtrl
->GetSelection();
460 m_treeCtrl
->GetItemsRecursively(root
);
463 #ifndef NO_MULTIPLE_SELECTION
465 void MyFrame::OnToggleSel(wxCommandEvent
& event
)
467 TogStyle(event
.GetId(), wxTR_MULTIPLE
);
470 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
472 wxArrayTreeItemIds array
;
474 size_t count
= m_treeCtrl
->GetSelections(array
);
475 wxLogMessage(wxT("%u items selected"), count
);
477 for ( size_t n
= 0; n
< count
; n
++ )
479 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
483 void MyFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
485 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
488 void MyFrame::OnSelectRoot(wxCommandEvent
& WXUNUSED(event
))
490 m_treeCtrl
->SelectItem(m_treeCtrl
->GetRootItem());
493 void MyFrame::OnUnselect(wxCommandEvent
& WXUNUSED(event
))
495 m_treeCtrl
->UnselectAll();
498 #endif // NO_MULTIPLE_SELECTION
500 void MyFrame::DoSetBold(bool bold
)
502 wxTreeItemId item
= m_treeCtrl
->GetSelection();
506 m_treeCtrl
->SetItemBold(item
, bold
);
509 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
511 wxTreeItemId item
= m_treeCtrl
->GetSelection();
515 m_treeCtrl
->Delete(item
);
518 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
520 wxTreeItemId item
= m_treeCtrl
->GetSelection();
524 m_treeCtrl
->DeleteChildren(item
);
527 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
529 m_treeCtrl
->DeleteAllItems();
532 void MyFrame::OnRecreate(wxCommandEvent
& event
)
535 m_treeCtrl
->AddTestItemsToTree(5, 2);
538 void MyFrame::OnSetImageSize(wxCommandEvent
& WXUNUSED(event
))
540 int size
= wxGetNumberFromUser(wxT("Enter the size for the images to use"),
542 wxT("TreeCtrl sample"),
543 m_treeCtrl
->ImageSize());
547 m_treeCtrl
->CreateImageList(size
);
548 wxGetApp().SetShowImages(true);
551 void MyFrame::OnToggleImages(wxCommandEvent
& WXUNUSED(event
))
553 if ( wxGetApp().ShowImages() )
555 m_treeCtrl
->CreateImageList(-1);
556 wxGetApp().SetShowImages(false);
560 m_treeCtrl
->CreateImageList(0);
561 wxGetApp().SetShowImages(true);
565 void MyFrame::OnToggleButtons(wxCommandEvent
& WXUNUSED(event
))
567 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
568 if ( wxGetApp().ShowButtons() )
570 m_treeCtrl
->CreateButtonsImageList(-1);
571 wxGetApp().SetShowButtons(false);
575 m_treeCtrl
->CreateButtonsImageList(15);
576 wxGetApp().SetShowButtons(true);
581 void MyFrame::OnCollapseAndReset(wxCommandEvent
& WXUNUSED(event
))
583 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
586 void MyFrame::OnEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
588 m_treeCtrl
->DoEnsureVisible();
591 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
593 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
594 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, wxT("2nd item"));
597 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
599 static int s_num
= 0;
602 text
.Printf(wxT("Item #%d"), ++s_num
);
604 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
606 MyTreeCtrl::TreeCtrlIcon_File */ );
609 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
611 unsigned int indent
= m_treeCtrl
->GetIndent();
613 m_treeCtrl
->SetIndent( indent
+5 );
616 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
618 unsigned int indent
= m_treeCtrl
->GetIndent();
620 m_treeCtrl
->SetIndent( indent
-5 );
623 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
625 unsigned int indent
= m_treeCtrl
->GetSpacing();
627 m_treeCtrl
->SetSpacing( indent
+5 );
630 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
632 unsigned int indent
= m_treeCtrl
->GetSpacing();
634 m_treeCtrl
->SetSpacing( indent
-5 );
637 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
639 wxTreeItemId item
= m_treeCtrl
->GetSelection();
643 m_treeCtrl
->DoToggleIcon(item
);
646 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
648 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
650 m_treeCtrl
->SetForegroundColour(col
);
653 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
655 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
657 m_treeCtrl
->SetBackgroundColour(col
);
660 // MyTreeCtrl implementation
661 #if USE_GENERIC_TREECTRL
662 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
664 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
667 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
668 const wxPoint
& pos
, const wxSize
& size
,
670 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
672 m_reverseSort
= false;
676 // Add some items to the tree
677 AddTestItemsToTree(5, 2);
680 void MyTreeCtrl::CreateImageList(int size
)
692 // Make an image list containing small icons
693 wxImageList
*images
= new wxImageList(size
, size
, true);
695 // should correspond to TreeCtrlIcon_xxx enum
698 icons
[0] = wxIcon(icon1_xpm
);
699 icons
[1] = wxIcon(icon2_xpm
);
700 icons
[2] = wxIcon(icon3_xpm
);
701 icons
[3] = wxIcon(icon4_xpm
);
702 icons
[4] = wxIcon(icon5_xpm
);
704 int sizeOrig
= icons
[0].GetWidth();
705 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
707 if ( size
== sizeOrig
)
709 images
->Add(icons
[i
]);
713 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
717 AssignImageList(images
);
720 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
721 void MyTreeCtrl::CreateButtonsImageList(int size
)
725 SetButtonsImageList(NULL
);
729 // Make an image list containing small icons
730 wxImageList
*images
= new wxImageList(size
, size
, true);
732 // should correspond to TreeCtrlIcon_xxx enum
735 icons
[0] = wxIcon(icon3_xpm
); // closed
736 icons
[1] = wxIcon(icon3_xpm
); // closed, selected
737 icons
[2] = wxIcon(icon5_xpm
); // open
738 icons
[3] = wxIcon(icon5_xpm
); // open, selected
740 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
742 int sizeOrig
= icons
[i
].GetWidth();
743 if ( size
== sizeOrig
)
745 images
->Add(icons
[i
]);
749 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
753 AssignButtonsImageList(images
);
755 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size
))
760 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
761 const wxTreeItemId
& item2
)
765 // just exchange 1st and 2nd items
766 return wxTreeCtrl::OnCompareItems(item2
, item1
);
770 return wxTreeCtrl::OnCompareItems(item1
, item2
);
774 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
781 bool hasChildren
= depth
> 1;
784 for ( size_t n
= 0; n
< numChildren
; n
++ )
786 // at depth 1 elements won't have any more children
788 str
.Printf(wxT("%s child %d"), wxT("Folder"), n
+ 1);
790 str
.Printf(wxT("%s child %d.%d"), wxT("File"), folder
, n
+ 1);
792 // here we pass to AppendItem() normal and selected item images (we
793 // suppose that selected image follows the normal one in the enum)
795 if ( wxGetApp().ShowImages() )
797 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
798 imageSel
= image
+ 1;
802 image
= imageSel
= -1;
804 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
805 new MyTreeItemData(str
));
807 // and now we also set the expanded one (only for the folders)
808 if ( hasChildren
&& wxGetApp().ShowImages() )
810 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
811 wxTreeItemIcon_Expanded
);
814 // remember the last child for OnEnsureVisible()
815 if ( !hasChildren
&& n
== numChildren
- 1 )
820 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
826 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
829 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
830 wxTreeItemId rootId
= AddRoot(wxT("Root"),
832 new MyTreeItemData(wxT("Root item")));
835 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
838 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
840 // set some colours/fonts for testing
841 SetItemFont(rootId
, *wxITALIC_FONT
);
843 wxTreeItemIdValue cookie
;
844 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
845 SetItemTextColour(id
, *wxBLUE
);
847 id
= GetNextChild(rootId
, cookie
);
848 id
= GetNextChild(rootId
, cookie
);
849 SetItemTextColour(id
, *wxRED
);
850 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
853 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
,
854 wxTreeItemIdValue cookie
)
859 id
= GetFirstChild(idParent
, cookie
);
861 id
= GetNextChild(idParent
, cookie
);
866 wxString text
= GetItemText(id
);
869 if (ItemHasChildren(id
))
870 GetItemsRecursively(id
);
872 GetItemsRecursively(idParent
, cookie
);
875 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
877 int image
= GetItemImage(item
) == TreeCtrlIcon_Folder
? TreeCtrlIcon_File
878 : TreeCtrlIcon_Folder
;
880 SetItemImage(item
, image
);
885 #define TREE_EVENT_HANDLER(name) \
886 void MyTreeCtrl::name(wxTreeEvent& event) \
888 wxLogMessage(wxT(#name)); \
889 SetLastItem(wxTreeItemId()); \
893 TREE_EVENT_HANDLER(OnBeginRDrag
)
894 TREE_EVENT_HANDLER(OnDeleteItem
)
895 TREE_EVENT_HANDLER(OnGetInfo
)
896 TREE_EVENT_HANDLER(OnSetInfo
)
897 TREE_EVENT_HANDLER(OnItemExpanded
)
898 TREE_EVENT_HANDLER(OnItemExpanding
)
899 TREE_EVENT_HANDLER(OnItemCollapsed
)
900 TREE_EVENT_HANDLER(OnSelChanged
)
901 TREE_EVENT_HANDLER(OnSelChanging
)
903 #undef TREE_EVENT_HANDLER
905 void LogKeyEvent(const wxChar
*name
, const wxKeyEvent
& event
)
908 long keycode
= event
.GetKeyCode();
912 case WXK_BACK
: key
= wxT("BACK"); break;
913 case WXK_TAB
: key
= wxT("TAB"); break;
914 case WXK_RETURN
: key
= wxT("RETURN"); break;
915 case WXK_ESCAPE
: key
= wxT("ESCAPE"); break;
916 case WXK_SPACE
: key
= wxT("SPACE"); break;
917 case WXK_DELETE
: key
= wxT("DELETE"); break;
918 case WXK_START
: key
= wxT("START"); break;
919 case WXK_LBUTTON
: key
= wxT("LBUTTON"); break;
920 case WXK_RBUTTON
: key
= wxT("RBUTTON"); break;
921 case WXK_CANCEL
: key
= wxT("CANCEL"); break;
922 case WXK_MBUTTON
: key
= wxT("MBUTTON"); break;
923 case WXK_CLEAR
: key
= wxT("CLEAR"); break;
924 case WXK_SHIFT
: key
= wxT("SHIFT"); break;
925 case WXK_ALT
: key
= wxT("ALT"); break;
926 case WXK_CONTROL
: key
= wxT("CONTROL"); break;
927 case WXK_MENU
: key
= wxT("MENU"); break;
928 case WXK_PAUSE
: key
= wxT("PAUSE"); break;
929 case WXK_CAPITAL
: key
= wxT("CAPITAL"); break;
930 case WXK_PRIOR
: key
= wxT("PRIOR"); break;
931 case WXK_NEXT
: key
= wxT("NEXT"); break;
932 case WXK_END
: key
= wxT("END"); break;
933 case WXK_HOME
: key
= wxT("HOME"); break;
934 case WXK_LEFT
: key
= wxT("LEFT"); break;
935 case WXK_UP
: key
= wxT("UP"); break;
936 case WXK_RIGHT
: key
= wxT("RIGHT"); break;
937 case WXK_DOWN
: key
= wxT("DOWN"); break;
938 case WXK_SELECT
: key
= wxT("SELECT"); break;
939 case WXK_PRINT
: key
= wxT("PRINT"); break;
940 case WXK_EXECUTE
: key
= wxT("EXECUTE"); break;
941 case WXK_SNAPSHOT
: key
= wxT("SNAPSHOT"); break;
942 case WXK_INSERT
: key
= wxT("INSERT"); break;
943 case WXK_HELP
: key
= wxT("HELP"); break;
944 case WXK_NUMPAD0
: key
= wxT("NUMPAD0"); break;
945 case WXK_NUMPAD1
: key
= wxT("NUMPAD1"); break;
946 case WXK_NUMPAD2
: key
= wxT("NUMPAD2"); break;
947 case WXK_NUMPAD3
: key
= wxT("NUMPAD3"); break;
948 case WXK_NUMPAD4
: key
= wxT("NUMPAD4"); break;
949 case WXK_NUMPAD5
: key
= wxT("NUMPAD5"); break;
950 case WXK_NUMPAD6
: key
= wxT("NUMPAD6"); break;
951 case WXK_NUMPAD7
: key
= wxT("NUMPAD7"); break;
952 case WXK_NUMPAD8
: key
= wxT("NUMPAD8"); break;
953 case WXK_NUMPAD9
: key
= wxT("NUMPAD9"); break;
954 case WXK_MULTIPLY
: key
= wxT("MULTIPLY"); break;
955 case WXK_ADD
: key
= wxT("ADD"); break;
956 case WXK_SEPARATOR
: key
= wxT("SEPARATOR"); break;
957 case WXK_SUBTRACT
: key
= wxT("SUBTRACT"); break;
958 case WXK_DECIMAL
: key
= wxT("DECIMAL"); break;
959 case WXK_DIVIDE
: key
= wxT("DIVIDE"); break;
960 case WXK_F1
: key
= wxT("F1"); break;
961 case WXK_F2
: key
= wxT("F2"); break;
962 case WXK_F3
: key
= wxT("F3"); break;
963 case WXK_F4
: key
= wxT("F4"); break;
964 case WXK_F5
: key
= wxT("F5"); break;
965 case WXK_F6
: key
= wxT("F6"); break;
966 case WXK_F7
: key
= wxT("F7"); break;
967 case WXK_F8
: key
= wxT("F8"); break;
968 case WXK_F9
: key
= wxT("F9"); break;
969 case WXK_F10
: key
= wxT("F10"); break;
970 case WXK_F11
: key
= wxT("F11"); break;
971 case WXK_F12
: key
= wxT("F12"); break;
972 case WXK_F13
: key
= wxT("F13"); break;
973 case WXK_F14
: key
= wxT("F14"); break;
974 case WXK_F15
: key
= wxT("F15"); break;
975 case WXK_F16
: key
= wxT("F16"); break;
976 case WXK_F17
: key
= wxT("F17"); break;
977 case WXK_F18
: key
= wxT("F18"); break;
978 case WXK_F19
: key
= wxT("F19"); break;
979 case WXK_F20
: key
= wxT("F20"); break;
980 case WXK_F21
: key
= wxT("F21"); break;
981 case WXK_F22
: key
= wxT("F22"); break;
982 case WXK_F23
: key
= wxT("F23"); break;
983 case WXK_F24
: key
= wxT("F24"); break;
984 case WXK_NUMLOCK
: key
= wxT("NUMLOCK"); break;
985 case WXK_SCROLL
: key
= wxT("SCROLL"); break;
986 case WXK_PAGEUP
: key
= wxT("PAGEUP"); break;
987 case WXK_PAGEDOWN
: key
= wxT("PAGEDOWN"); break;
988 case WXK_NUMPAD_SPACE
: key
= wxT("NUMPAD_SPACE"); break;
989 case WXK_NUMPAD_TAB
: key
= wxT("NUMPAD_TAB"); break;
990 case WXK_NUMPAD_ENTER
: key
= wxT("NUMPAD_ENTER"); break;
991 case WXK_NUMPAD_F1
: key
= wxT("NUMPAD_F1"); break;
992 case WXK_NUMPAD_F2
: key
= wxT("NUMPAD_F2"); break;
993 case WXK_NUMPAD_F3
: key
= wxT("NUMPAD_F3"); break;
994 case WXK_NUMPAD_F4
: key
= wxT("NUMPAD_F4"); break;
995 case WXK_NUMPAD_HOME
: key
= wxT("NUMPAD_HOME"); break;
996 case WXK_NUMPAD_LEFT
: key
= wxT("NUMPAD_LEFT"); break;
997 case WXK_NUMPAD_UP
: key
= wxT("NUMPAD_UP"); break;
998 case WXK_NUMPAD_RIGHT
: key
= wxT("NUMPAD_RIGHT"); break;
999 case WXK_NUMPAD_DOWN
: key
= wxT("NUMPAD_DOWN"); break;
1000 case WXK_NUMPAD_PRIOR
: key
= wxT("NUMPAD_PRIOR"); break;
1001 case WXK_NUMPAD_PAGEUP
: key
= wxT("NUMPAD_PAGEUP"); break;
1002 case WXK_NUMPAD_PAGEDOWN
: key
= wxT("NUMPAD_PAGEDOWN"); break;
1003 case WXK_NUMPAD_END
: key
= wxT("NUMPAD_END"); break;
1004 case WXK_NUMPAD_BEGIN
: key
= wxT("NUMPAD_BEGIN"); break;
1005 case WXK_NUMPAD_INSERT
: key
= wxT("NUMPAD_INSERT"); break;
1006 case WXK_NUMPAD_DELETE
: key
= wxT("NUMPAD_DELETE"); break;
1007 case WXK_NUMPAD_EQUAL
: key
= wxT("NUMPAD_EQUAL"); break;
1008 case WXK_NUMPAD_MULTIPLY
: key
= wxT("NUMPAD_MULTIPLY"); break;
1009 case WXK_NUMPAD_ADD
: key
= wxT("NUMPAD_ADD"); break;
1010 case WXK_NUMPAD_SEPARATOR
: key
= wxT("NUMPAD_SEPARATOR"); break;
1011 case WXK_NUMPAD_SUBTRACT
: key
= wxT("NUMPAD_SUBTRACT"); break;
1012 case WXK_NUMPAD_DECIMAL
: key
= wxT("NUMPAD_DECIMAL"); break;
1016 if ( wxIsprint((int)keycode
) )
1017 key
.Printf(wxT("'%c'"), (char)keycode
);
1018 else if ( keycode
> 0 && keycode
< 27 )
1019 key
.Printf(_("Ctrl-%c"), wxT('A') + keycode
- 1);
1021 key
.Printf(wxT("unknown (%ld)"), keycode
);
1026 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1029 event
.ControlDown() ? wxT('C') : wxT('-'),
1030 event
.AltDown() ? wxT('A') : wxT('-'),
1031 event
.ShiftDown() ? wxT('S') : wxT('-'),
1032 event
.MetaDown() ? wxT('M') : wxT('-'));
1035 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
& event
)
1037 LogKeyEvent(wxT("Tree key down "), event
.GetKeyEvent());
1042 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
1044 // need to explicitly allow drag
1045 if ( event
.GetItem() != GetRootItem() )
1047 m_draggedItem
= event
.GetItem();
1049 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
1050 GetItemText(m_draggedItem
).c_str());
1056 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1060 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
1062 wxTreeItemId itemSrc
= m_draggedItem
,
1063 itemDst
= event
.GetItem();
1064 m_draggedItem
= (wxTreeItemId
)0l;
1066 // where to copy the item?
1067 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
1069 // copy to the parent then
1070 itemDst
= GetItemParent(itemDst
);
1073 if ( !itemDst
.IsOk() )
1075 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1080 wxString text
= GetItemText(itemSrc
);
1081 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1082 text
.c_str(), GetItemText(itemDst
).c_str());
1084 // just do append here - we could also insert it just before/after the item
1085 // on which it was dropped, but this requires slightly more work... we also
1086 // completely ignore the client data and icon of the old item but could
1087 // copy them as well.
1089 // Finally, we only copy one item here but we might copy the entire tree if
1090 // we were dragging a folder.
1091 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
1092 AppendItem(itemDst
, text
, image
);
1095 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
1097 wxLogMessage(wxT("OnBeginLabelEdit"));
1099 // for testing, prevent this item's label editing
1100 wxTreeItemId itemId
= event
.GetItem();
1101 if ( IsTestItem(itemId
) )
1103 wxMessageBox(wxT("You can't edit this item."));
1109 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
1111 wxLogMessage(wxT("OnEndLabelEdit"));
1113 // don't allow anything except letters in the labels
1114 if ( !event
.GetLabel().IsWord() )
1116 wxMessageBox(wxT("The new label should be a single word."));
1122 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
1124 wxLogMessage(wxT("OnItemCollapsing"));
1126 // for testing, prevent the user from collapsing the first child folder
1127 wxTreeItemId itemId
= event
.GetItem();
1128 if ( IsTestItem(itemId
) )
1130 wxMessageBox(wxT("You can't collapse this item."));
1136 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
1138 // show some info about this item
1139 wxTreeItemId itemId
= event
.GetItem();
1140 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1144 item
->ShowInfo(this);
1147 wxLogMessage(wxT("OnItemActivated"));
1150 void MyTreeCtrl::OnItemRightClick(wxTreeEvent
& event
)
1152 ShowMenu(event
.GetItem(), event
.GetPoint());
1155 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1157 wxPoint pt
= event
.GetPosition();
1158 wxTreeItemId item
= GetSelection();
1159 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt
.x
, pt
.y
);
1161 //check if event was generated by keyboard (MSW-specific?)
1162 if (pt
.x
==-1 && pt
.y
==-1) //(this is how MSW indicates it)
1164 //attempt to guess where to show the menu
1167 //if an item was clicked, show menu to the right of it
1169 GetBoundingRect(item
, rect
, true); //true = only the label
1170 pt
= wxPoint(rect
.GetRight(), rect
.GetTop());
1179 //event was generated by mouse, use supplied coords
1180 pt
= ScreenToClient(pt
);
1186 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
1191 title
<< wxT("Menu for ") << GetItemText(id
);
1195 title
= wxT("Menu for no particular item");
1200 menu
.Append(TreeTest_About
, wxT("&About..."));
1201 menu
.Append(TreeTest_Dump
, wxT("&Dump"));
1203 PopupMenu(&menu
, pt
);
1204 #endif // wxUSE_MENUS
1207 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
1209 wxTreeItemId id
= HitTest(event
.GetPosition());
1211 wxLogMessage(wxT("No item under mouse"));
1214 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
1216 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1220 static inline const wxChar
*Bool2String(bool b
)
1222 return b
? wxT("") : wxT("not ");
1225 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1227 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1228 wxT("%u children (%u immediately under this item)."),
1230 Bool2String(tree
->IsSelected(GetId())),
1231 Bool2String(tree
->IsExpanded(GetId())),
1232 Bool2String(tree
->IsBold(GetId())),
1233 tree
->GetChildrenCount(GetId()),
1234 tree
->GetChildrenCount(GetId(), false));