1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/colordlg.h"
25 #include "wx/numdlg.h"
28 #include "wx/imaglist.h"
29 #include "wx/treectrl.h"
33 // this is not supported by native control
34 #define NO_VARIABLE_HEIGHT
46 #include "../sample.xpm"
49 // verify that the item is ok and insult the user if it is not
50 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
51 wxMessageBox(wxT("Please select some item first!"), \
52 wxT("Tree sample error"), \
53 wxOK | wxICON_EXCLAMATION, \
58 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
60 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
61 EVT_IDLE(MyFrame::OnIdle
)
62 EVT_SIZE(MyFrame::OnSize
)
71 MENU_LINK(TogHideRoot
)
72 MENU_LINK(TogRootLines
)
74 MENU_LINK(TogFullHighlight
)
75 MENU_LINK(SetFgColour
)
76 MENU_LINK(SetBgColour
)
80 #ifndef NO_MULTIPLE_SELECTION
81 MENU_LINK(DumpSelected
)
85 #endif // NO_MULTIPLE_SELECTION
94 MENU_LINK(DeleteChildren
)
97 MENU_LINK(ToggleImages
)
98 MENU_LINK(ToggleButtons
)
99 MENU_LINK(SetImageSize
)
100 MENU_LINK(CollapseAndReset
)
101 MENU_LINK(EnsureVisible
)
103 MENU_LINK(InsertItem
)
106 MENU_LINK(IncSpacing
)
107 MENU_LINK(DecSpacing
)
108 MENU_LINK(ToggleIcon
)
109 MENU_LINK(SelectRoot
)
114 #if USE_GENERIC_TREECTRL
115 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxGenericTreeCtrl
)
117 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
119 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
120 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
121 EVT_TREE_END_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnEndDrag
)
122 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
123 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
124 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
125 #if 0 // there are so many of those that logging them causes flicker
126 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
128 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
129 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
130 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
131 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
132 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
134 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
135 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
136 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
137 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl
, MyTreeCtrl::OnItemActivated
)
139 // so many differents ways to handle right mouse button clicks...
140 EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu
)
141 // EVT_TREE_ITEM_MENU is the preferred event for creating context menus
142 // on a tree control, because it includes the point of the click or item,
143 // meaning that no additional placement calculations are required.
144 EVT_TREE_ITEM_MENU(TreeTest_Ctrl
, MyTreeCtrl::OnItemMenu
)
145 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl
, MyTreeCtrl::OnItemRClick
)
147 EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown
)
148 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp
)
149 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick
)
156 // Create the main frame window
157 MyFrame
*frame
= new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
167 // My frame constructor
168 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
169 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
175 // This reduces flicker effects - even better would be to define
176 // OnEraseBackground to do nothing. When the tree control's scrollbars are
177 // show or hidden, the frame is sent a background erase event.
178 SetBackgroundColour(wxColour(255, 255, 255));
181 SetIcon(wxICON(sample
));
185 wxMenu
*file_menu
= new wxMenu
,
186 *style_menu
= new wxMenu
,
187 *tree_menu
= new wxMenu
,
188 *item_menu
= new wxMenu
;
190 file_menu
->Append(TreeTest_About
, wxT("&About..."));
191 file_menu
->AppendSeparator();
192 file_menu
->Append(TreeTest_Quit
, wxT("E&xit\tAlt-X"));
194 style_menu
->AppendCheckItem(TreeTest_TogButtons
, wxT("Toggle &normal buttons"));
195 style_menu
->AppendCheckItem(TreeTest_TogTwist
, wxT("Toggle &twister buttons"));
196 style_menu
->AppendCheckItem(TreeTest_ToggleButtons
, wxT("Toggle image &buttons"));
197 style_menu
->AppendSeparator();
198 style_menu
->AppendCheckItem(TreeTest_TogLines
, wxT("Toggle &connecting lines"));
199 style_menu
->AppendCheckItem(TreeTest_TogRootLines
, wxT("Toggle &lines at root"));
200 style_menu
->AppendCheckItem(TreeTest_TogHideRoot
, wxT("Toggle &hidden root"));
201 style_menu
->AppendCheckItem(TreeTest_TogBorder
, wxT("Toggle &item border"));
202 style_menu
->AppendCheckItem(TreeTest_TogFullHighlight
, wxT("Toggle &full row highlight"));
203 style_menu
->AppendCheckItem(TreeTest_TogEdit
, wxT("Toggle &edit mode"));
204 #ifndef NO_MULTIPLE_SELECTION
205 style_menu
->AppendCheckItem(TreeTest_ToggleSel
, wxT("Toggle &selection mode"));
206 #endif // NO_MULTIPLE_SELECTION
207 style_menu
->AppendCheckItem(TreeTest_ToggleImages
, wxT("Toggle show ima&ges"));
208 style_menu
->Append(TreeTest_SetImageSize
, wxT("Set image si&ze..."));
209 style_menu
->AppendSeparator();
210 style_menu
->Append(TreeTest_SetFgColour
, wxT("Set &foreground colour..."));
211 style_menu
->Append(TreeTest_SetBgColour
, wxT("Set &background colour..."));
212 style_menu
->AppendSeparator();
213 style_menu
->Append(TreeTest_ResetStyle
, wxT("&Reset to default\tF10"));
215 tree_menu
->Append(TreeTest_Recreate
, wxT("&Recreate the tree"));
216 tree_menu
->Append(TreeTest_CollapseAndReset
, wxT("C&ollapse and reset"));
217 tree_menu
->AppendSeparator();
218 tree_menu
->Append(TreeTest_AddItem
, wxT("Append a &new item"));
219 tree_menu
->Append(TreeTest_InsertItem
, wxT("&Insert a new item"));
220 tree_menu
->Append(TreeTest_Delete
, wxT("&Delete this item"));
221 tree_menu
->Append(TreeTest_DeleteChildren
, wxT("Delete &children"));
222 tree_menu
->Append(TreeTest_DeleteAll
, wxT("Delete &all items"));
223 tree_menu
->Append(TreeTest_SelectRoot
, wxT("Select root item"));
224 tree_menu
->AppendSeparator();
225 tree_menu
->Append(TreeTest_Count
, wxT("Count children of current item"));
226 tree_menu
->Append(TreeTest_CountRec
, wxT("Recursively count children of current item"));
227 tree_menu
->AppendSeparator();
228 tree_menu
->Append(TreeTest_Sort
, wxT("Sort children of current item"));
229 tree_menu
->Append(TreeTest_SortRev
, wxT("Sort in reversed order"));
230 tree_menu
->AppendSeparator();
231 tree_menu
->Append(TreeTest_EnsureVisible
, wxT("Make the last item &visible"));
232 tree_menu
->AppendSeparator();
233 tree_menu
->Append(TreeTest_IncIndent
, wxT("Add 5 points to indentation\tAlt-I"));
234 tree_menu
->Append(TreeTest_DecIndent
, wxT("Reduce indentation by 5 points\tAlt-R"));
235 tree_menu
->AppendSeparator();
236 tree_menu
->Append(TreeTest_IncSpacing
, wxT("Add 5 points to spacing\tCtrl-I"));
237 tree_menu
->Append(TreeTest_DecSpacing
, wxT("Reduce spacing by 5 points\tCtrl-R"));
239 item_menu
->Append(TreeTest_Dump
, wxT("&Dump item children"));
240 item_menu
->Append(TreeTest_Rename
, wxT("&Rename item..."));
242 item_menu
->AppendSeparator();
243 item_menu
->Append(TreeTest_SetBold
, wxT("Make item &bold"));
244 item_menu
->Append(TreeTest_ClearBold
, wxT("Make item ¬ bold"));
245 item_menu
->AppendSeparator();
246 item_menu
->Append(TreeTest_ToggleIcon
, wxT("Toggle the item's &icon"));
248 #ifndef NO_MULTIPLE_SELECTION
249 item_menu
->AppendSeparator();
250 item_menu
->Append(TreeTest_DumpSelected
, wxT("Dump selected items\tAlt-D"));
251 item_menu
->Append(TreeTest_Select
, wxT("Select current item\tAlt-S"));
252 item_menu
->Append(TreeTest_Unselect
, wxT("Unselect everything\tAlt-U"));
253 #endif // NO_MULTIPLE_SELECTION
255 wxMenuBar
*menu_bar
= new wxMenuBar
;
256 menu_bar
->Append(file_menu
, wxT("&File"));
257 menu_bar
->Append(style_menu
, wxT("&Style"));
258 menu_bar
->Append(tree_menu
, wxT("&Tree"));
259 menu_bar
->Append(item_menu
, wxT("&Item"));
260 SetMenuBar(menu_bar
);
261 #endif // wxUSE_MENUS
263 m_panel
= new wxPanel(this);
266 // create the controls
267 m_textCtrl
= new wxTextCtrl(m_panel
, wxID_ANY
, wxT(""),
268 wxDefaultPosition
, wxDefaultSize
,
269 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
272 CreateTreeWithDefStyle();
274 menu_bar
->Check(TreeTest_ToggleImages
, true);
277 // create a status bar
279 #endif // wxUSE_STATUSBAR
283 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
284 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
285 delete wxLog::SetActiveTarget(new wxLogStderr
);
287 // set our text control as the log target
288 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(m_textCtrl
);
289 delete wxLog::SetActiveTarget(logWindow
);
297 delete wxLog::SetActiveTarget(NULL
);
301 void MyFrame::CreateTreeWithDefStyle()
303 long style
= wxTR_DEFAULT_STYLE
|
304 #ifndef NO_VARIABLE_HEIGHT
305 wxTR_HAS_VARIABLE_ROW_HEIGHT
|
309 CreateTree(style
| wxSUNKEN_BORDER
);
311 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
313 wxMenuBar
*mbar
= GetMenuBar();
314 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_HAS_BUTTONS
) != 0);
315 mbar
->Check(TreeTest_TogButtons
, (style
& wxTR_TWIST_BUTTONS
) != 0);
316 mbar
->Check(TreeTest_TogLines
, (style
& wxTR_NO_LINES
) == 0);
317 mbar
->Check(TreeTest_TogRootLines
, (style
& wxTR_LINES_AT_ROOT
) != 0);
318 mbar
->Check(TreeTest_TogHideRoot
, (style
& wxTR_HIDE_ROOT
) != 0);
319 mbar
->Check(TreeTest_TogEdit
, (style
& wxTR_EDIT_LABELS
) != 0);
320 mbar
->Check(TreeTest_TogBorder
, (style
& wxTR_ROW_LINES
) != 0);
321 mbar
->Check(TreeTest_TogFullHighlight
, (style
& wxTR_FULL_ROW_HIGHLIGHT
) != 0);
324 void MyFrame::CreateTree(long style
)
326 m_treeCtrl
= new MyTreeCtrl(m_panel
, TreeTest_Ctrl
,
327 wxDefaultPosition
, wxDefaultSize
,
332 void MyFrame::TogStyle(int id
, long flag
)
334 long style
= m_treeCtrl
->GetWindowStyle() ^ flag
;
336 // most treectrl styles can't be changed on the fly using the native
337 // control and the tree must be recreated
339 m_treeCtrl
->SetWindowStyle(style
);
345 GetMenuBar()->Check(id
, (style
& flag
) != 0);
348 void MyFrame::OnIdle(wxIdleEvent
& event
)
353 wxTreeItemId idRoot
= m_treeCtrl
->GetRootItem();
355 SetStatusText(wxString::Format
357 _T("Root/last item is %svisible/%svisible"),
358 m_treeCtrl
->IsVisible(idRoot
) ? _T("") : _T("not "),
359 m_treeCtrl
->IsVisible(m_treeCtrl
->GetLastChild(idRoot
))
360 ? _T("") : _T("not ")
363 #endif // wxUSE_STATUSBAR
368 void MyFrame::OnSize(wxSizeEvent
& event
)
382 void MyFrame::Resize()
384 wxSize size
= GetClientSize();
385 m_treeCtrl
->SetSize(0, 0, size
.x
, size
.y
390 m_textCtrl
->SetSize(0, 2*size
.y
/3, size
.x
, size
.y
/3);
394 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
399 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
401 wxMessageBox(wxT("Tree test sample\n")
402 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
403 wxT("About tree test"),
404 wxOK
| wxICON_INFORMATION
, this);
407 void MyFrame::OnRename(wxCommandEvent
& WXUNUSED(event
))
409 wxTreeItemId item
= m_treeCtrl
->GetSelection();
413 // old code - now we edit in place
415 static wxString s_text
;
416 s_text
= wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
418 if ( !s_text
.empty() )
420 m_treeCtrl
->SetItemText(item
, s_text
);
424 // TODO demonstrate creating a custom edit control...
425 (void)m_treeCtrl
->EditLabel(item
);
428 void MyFrame::OnCount(wxCommandEvent
& WXUNUSED(event
))
430 wxTreeItemId item
= m_treeCtrl
->GetSelection();
434 int i
= m_treeCtrl
->GetChildrenCount( item
, false );
436 wxLogMessage(wxT("%d children"), i
);
439 void MyFrame::OnCountRec(wxCommandEvent
& WXUNUSED(event
))
441 wxTreeItemId item
= m_treeCtrl
->GetSelection();
445 int i
= m_treeCtrl
->GetChildrenCount( item
);
447 wxLogMessage(wxT("%d children"), i
);
450 void MyFrame::DoSort(bool reverse
)
452 wxTreeItemId item
= m_treeCtrl
->GetSelection();
456 m_treeCtrl
->DoSortChildren(item
, reverse
);
459 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
461 wxTreeItemId root
= m_treeCtrl
->GetSelection();
465 m_treeCtrl
->GetItemsRecursively(root
);
468 #ifndef NO_MULTIPLE_SELECTION
470 void MyFrame::OnToggleSel(wxCommandEvent
& event
)
472 TogStyle(event
.GetId(), wxTR_MULTIPLE
);
475 void MyFrame::OnDumpSelected(wxCommandEvent
& WXUNUSED(event
))
477 wxArrayTreeItemIds array
;
479 size_t count
= m_treeCtrl
->GetSelections(array
);
480 wxLogMessage(wxT("%u items selected"), count
);
482 for ( size_t n
= 0; n
< count
; n
++ )
484 wxLogMessage(wxT("\t%s"), m_treeCtrl
->GetItemText(array
.Item(n
)).c_str());
488 void MyFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
490 m_treeCtrl
->SelectItem(m_treeCtrl
->GetSelection());
493 void MyFrame::OnSelectRoot(wxCommandEvent
& WXUNUSED(event
))
495 m_treeCtrl
->SelectItem(m_treeCtrl
->GetRootItem());
498 void MyFrame::OnUnselect(wxCommandEvent
& WXUNUSED(event
))
500 m_treeCtrl
->UnselectAll();
503 #endif // NO_MULTIPLE_SELECTION
505 void MyFrame::DoSetBold(bool bold
)
507 wxTreeItemId item
= m_treeCtrl
->GetSelection();
511 m_treeCtrl
->SetItemBold(item
, bold
);
514 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
516 wxTreeItemId item
= m_treeCtrl
->GetSelection();
520 m_treeCtrl
->Delete(item
);
523 void MyFrame::OnDeleteChildren(wxCommandEvent
& WXUNUSED(event
))
525 wxTreeItemId item
= m_treeCtrl
->GetSelection();
529 m_treeCtrl
->DeleteChildren(item
);
532 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
534 m_treeCtrl
->DeleteAllItems();
537 void MyFrame::OnRecreate(wxCommandEvent
& event
)
540 m_treeCtrl
->AddTestItemsToTree(5, 2);
543 void MyFrame::OnSetImageSize(wxCommandEvent
& WXUNUSED(event
))
545 int size
= wxGetNumberFromUser(wxT("Enter the size for the images to use"),
547 wxT("TreeCtrl sample"),
548 m_treeCtrl
->ImageSize());
552 m_treeCtrl
->CreateImageList(size
);
553 wxGetApp().SetShowImages(true);
556 void MyFrame::OnToggleImages(wxCommandEvent
& WXUNUSED(event
))
558 if ( wxGetApp().ShowImages() )
560 m_treeCtrl
->CreateImageList(-1);
561 wxGetApp().SetShowImages(false);
565 m_treeCtrl
->CreateImageList(0);
566 wxGetApp().SetShowImages(true);
570 void MyFrame::OnToggleButtons(wxCommandEvent
& WXUNUSED(event
))
572 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
573 if ( wxGetApp().ShowButtons() )
575 m_treeCtrl
->CreateButtonsImageList(-1);
576 wxGetApp().SetShowButtons(false);
580 m_treeCtrl
->CreateButtonsImageList(15);
581 wxGetApp().SetShowButtons(true);
586 void MyFrame::OnCollapseAndReset(wxCommandEvent
& WXUNUSED(event
))
588 m_treeCtrl
->CollapseAndReset(m_treeCtrl
->GetRootItem());
591 void MyFrame::OnEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
593 m_treeCtrl
->DoEnsureVisible();
596 void MyFrame::OnInsertItem(wxCommandEvent
& WXUNUSED(event
))
598 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File
: -1;
599 m_treeCtrl
->InsertItem(m_treeCtrl
->GetRootItem(), image
, wxT("2nd item"));
602 void MyFrame::OnAddItem(wxCommandEvent
& WXUNUSED(event
))
604 static int s_num
= 0;
607 text
.Printf(wxT("Item #%d"), ++s_num
);
609 m_treeCtrl
->AppendItem(m_treeCtrl
->GetRootItem(),
611 MyTreeCtrl::TreeCtrlIcon_File */ );
614 void MyFrame::OnIncIndent(wxCommandEvent
& WXUNUSED(event
))
616 unsigned int indent
= m_treeCtrl
->GetIndent();
618 m_treeCtrl
->SetIndent( indent
+5 );
621 void MyFrame::OnDecIndent(wxCommandEvent
& WXUNUSED(event
))
623 unsigned int indent
= m_treeCtrl
->GetIndent();
625 m_treeCtrl
->SetIndent( indent
-5 );
628 void MyFrame::OnIncSpacing(wxCommandEvent
& WXUNUSED(event
))
630 unsigned int indent
= m_treeCtrl
->GetSpacing();
632 m_treeCtrl
->SetSpacing( indent
+5 );
635 void MyFrame::OnDecSpacing(wxCommandEvent
& WXUNUSED(event
))
637 unsigned int indent
= m_treeCtrl
->GetSpacing();
639 m_treeCtrl
->SetSpacing( indent
-5 );
642 void MyFrame::OnToggleIcon(wxCommandEvent
& WXUNUSED(event
))
644 wxTreeItemId item
= m_treeCtrl
->GetSelection();
648 m_treeCtrl
->DoToggleIcon(item
);
651 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
653 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetForegroundColour());
655 m_treeCtrl
->SetForegroundColour(col
);
658 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
660 wxColour col
= wxGetColourFromUser(this, m_treeCtrl
->GetBackgroundColour());
662 m_treeCtrl
->SetBackgroundColour(col
);
665 // MyTreeCtrl implementation
666 #if USE_GENERIC_TREECTRL
667 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxGenericTreeCtrl
)
669 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl
, wxTreeCtrl
)
672 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
673 const wxPoint
& pos
, const wxSize
& size
,
675 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
677 m_reverseSort
= false;
681 // Add some items to the tree
682 AddTestItemsToTree(5, 2);
685 void MyTreeCtrl::CreateImageList(int size
)
697 // Make an image list containing small icons
698 wxImageList
*images
= new wxImageList(size
, size
, true);
700 // should correspond to TreeCtrlIcon_xxx enum
703 icons
[0] = wxIcon(icon1_xpm
);
704 icons
[1] = wxIcon(icon2_xpm
);
705 icons
[2] = wxIcon(icon3_xpm
);
706 icons
[3] = wxIcon(icon4_xpm
);
707 icons
[4] = wxIcon(icon5_xpm
);
709 int sizeOrig
= icons
[0].GetWidth();
710 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
712 if ( size
== sizeOrig
)
714 images
->Add(icons
[i
]);
718 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
722 AssignImageList(images
);
725 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
726 void MyTreeCtrl::CreateButtonsImageList(int size
)
730 SetButtonsImageList(NULL
);
734 // Make an image list containing small icons
735 wxImageList
*images
= new wxImageList(size
, size
, true);
737 // should correspond to TreeCtrlIcon_xxx enum
740 icons
[0] = wxIcon(icon3_xpm
); // closed
741 icons
[1] = wxIcon(icon3_xpm
); // closed, selected
742 icons
[2] = wxIcon(icon5_xpm
); // open
743 icons
[3] = wxIcon(icon5_xpm
); // open, selected
745 for ( size_t i
= 0; i
< WXSIZEOF(icons
); i
++ )
747 int sizeOrig
= icons
[i
].GetWidth();
748 if ( size
== sizeOrig
)
750 images
->Add(icons
[i
]);
754 images
->Add(wxBitmap(wxBitmap(icons
[i
]).ConvertToImage().Rescale(size
, size
)));
758 AssignButtonsImageList(images
);
760 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size
))
765 int MyTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
766 const wxTreeItemId
& item2
)
770 // just exchange 1st and 2nd items
771 return wxTreeCtrl::OnCompareItems(item2
, item1
);
775 return wxTreeCtrl::OnCompareItems(item1
, item2
);
779 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
786 bool hasChildren
= depth
> 1;
789 for ( size_t n
= 0; n
< numChildren
; n
++ )
791 // at depth 1 elements won't have any more children
793 str
.Printf(wxT("%s child %d"), wxT("Folder"), n
+ 1);
795 str
.Printf(wxT("%s child %d.%d"), wxT("File"), folder
, n
+ 1);
797 // here we pass to AppendItem() normal and selected item images (we
798 // suppose that selected image follows the normal one in the enum)
800 if ( wxGetApp().ShowImages() )
802 image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
803 imageSel
= image
+ 1;
807 image
= imageSel
= -1;
809 wxTreeItemId id
= AppendItem(idParent
, str
, image
, imageSel
,
810 new MyTreeItemData(str
));
812 // and now we also set the expanded one (only for the folders)
813 if ( hasChildren
&& wxGetApp().ShowImages() )
815 SetItemImage(id
, TreeCtrlIcon_FolderOpened
,
816 wxTreeItemIcon_Expanded
);
819 // remember the last child for OnEnsureVisible()
820 if ( !hasChildren
&& n
== numChildren
- 1 )
825 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
831 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
834 int image
= wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder
: -1;
835 wxTreeItemId rootId
= AddRoot(wxT("Root"),
837 new MyTreeItemData(wxT("Root item")));
840 SetItemImage(rootId
, TreeCtrlIcon_FolderOpened
, wxTreeItemIcon_Expanded
);
843 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
845 // set some colours/fonts for testing
846 SetItemFont(rootId
, *wxITALIC_FONT
);
848 wxTreeItemIdValue cookie
;
849 wxTreeItemId id
= GetFirstChild(rootId
, cookie
);
850 SetItemTextColour(id
, *wxBLUE
);
852 id
= GetNextChild(rootId
, cookie
);
853 id
= GetNextChild(rootId
, cookie
);
854 SetItemTextColour(id
, *wxRED
);
855 SetItemBackgroundColour(id
, *wxLIGHT_GREY
);
858 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
,
859 wxTreeItemIdValue cookie
)
864 id
= GetFirstChild(idParent
, cookie
);
866 id
= GetNextChild(idParent
, cookie
);
871 wxString text
= GetItemText(id
);
874 if (ItemHasChildren(id
))
875 GetItemsRecursively(id
);
877 GetItemsRecursively(idParent
, cookie
);
880 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId
& item
)
882 int image
= (GetItemImage(item
) == TreeCtrlIcon_Folder
)
884 : TreeCtrlIcon_Folder
;
885 SetItemImage(item
, image
, wxTreeItemIcon_Normal
);
887 image
= (GetItemImage(item
) == TreeCtrlIcon_FolderSelected
)
888 ? TreeCtrlIcon_FileSelected
889 : TreeCtrlIcon_FolderSelected
;
890 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
895 #define TREE_EVENT_HANDLER(name) \
896 void MyTreeCtrl::name(wxTreeEvent& event) \
898 wxLogMessage(wxT(#name)); \
899 SetLastItem(wxTreeItemId()); \
903 TREE_EVENT_HANDLER(OnBeginRDrag
)
904 TREE_EVENT_HANDLER(OnDeleteItem
)
905 TREE_EVENT_HANDLER(OnGetInfo
)
906 TREE_EVENT_HANDLER(OnSetInfo
)
907 TREE_EVENT_HANDLER(OnItemExpanded
)
908 TREE_EVENT_HANDLER(OnItemExpanding
)
909 TREE_EVENT_HANDLER(OnItemCollapsed
)
910 TREE_EVENT_HANDLER(OnSelChanged
)
911 TREE_EVENT_HANDLER(OnSelChanging
)
913 #undef TREE_EVENT_HANDLER
915 void LogKeyEvent(const wxChar
*name
, const wxKeyEvent
& event
)
918 long keycode
= event
.GetKeyCode();
922 case WXK_BACK
: key
= wxT("BACK"); break;
923 case WXK_TAB
: key
= wxT("TAB"); break;
924 case WXK_RETURN
: key
= wxT("RETURN"); break;
925 case WXK_ESCAPE
: key
= wxT("ESCAPE"); break;
926 case WXK_SPACE
: key
= wxT("SPACE"); break;
927 case WXK_DELETE
: key
= wxT("DELETE"); break;
928 case WXK_START
: key
= wxT("START"); break;
929 case WXK_LBUTTON
: key
= wxT("LBUTTON"); break;
930 case WXK_RBUTTON
: key
= wxT("RBUTTON"); break;
931 case WXK_CANCEL
: key
= wxT("CANCEL"); break;
932 case WXK_MBUTTON
: key
= wxT("MBUTTON"); break;
933 case WXK_CLEAR
: key
= wxT("CLEAR"); break;
934 case WXK_SHIFT
: key
= wxT("SHIFT"); break;
935 case WXK_ALT
: key
= wxT("ALT"); break;
936 case WXK_CONTROL
: key
= wxT("CONTROL"); break;
937 case WXK_MENU
: key
= wxT("MENU"); break;
938 case WXK_PAUSE
: key
= wxT("PAUSE"); break;
939 case WXK_CAPITAL
: key
= wxT("CAPITAL"); break;
940 case WXK_END
: key
= wxT("END"); break;
941 case WXK_HOME
: key
= wxT("HOME"); break;
942 case WXK_LEFT
: key
= wxT("LEFT"); break;
943 case WXK_UP
: key
= wxT("UP"); break;
944 case WXK_RIGHT
: key
= wxT("RIGHT"); break;
945 case WXK_DOWN
: key
= wxT("DOWN"); break;
946 case WXK_SELECT
: key
= wxT("SELECT"); break;
947 case WXK_PRINT
: key
= wxT("PRINT"); break;
948 case WXK_EXECUTE
: key
= wxT("EXECUTE"); break;
949 case WXK_SNAPSHOT
: key
= wxT("SNAPSHOT"); break;
950 case WXK_INSERT
: key
= wxT("INSERT"); break;
951 case WXK_HELP
: key
= wxT("HELP"); break;
952 case WXK_NUMPAD0
: key
= wxT("NUMPAD0"); break;
953 case WXK_NUMPAD1
: key
= wxT("NUMPAD1"); break;
954 case WXK_NUMPAD2
: key
= wxT("NUMPAD2"); break;
955 case WXK_NUMPAD3
: key
= wxT("NUMPAD3"); break;
956 case WXK_NUMPAD4
: key
= wxT("NUMPAD4"); break;
957 case WXK_NUMPAD5
: key
= wxT("NUMPAD5"); break;
958 case WXK_NUMPAD6
: key
= wxT("NUMPAD6"); break;
959 case WXK_NUMPAD7
: key
= wxT("NUMPAD7"); break;
960 case WXK_NUMPAD8
: key
= wxT("NUMPAD8"); break;
961 case WXK_NUMPAD9
: key
= wxT("NUMPAD9"); break;
962 case WXK_MULTIPLY
: key
= wxT("MULTIPLY"); break;
963 case WXK_ADD
: key
= wxT("ADD"); break;
964 case WXK_SEPARATOR
: key
= wxT("SEPARATOR"); break;
965 case WXK_SUBTRACT
: key
= wxT("SUBTRACT"); break;
966 case WXK_DECIMAL
: key
= wxT("DECIMAL"); break;
967 case WXK_DIVIDE
: key
= wxT("DIVIDE"); break;
968 case WXK_F1
: key
= wxT("F1"); break;
969 case WXK_F2
: key
= wxT("F2"); break;
970 case WXK_F3
: key
= wxT("F3"); break;
971 case WXK_F4
: key
= wxT("F4"); break;
972 case WXK_F5
: key
= wxT("F5"); break;
973 case WXK_F6
: key
= wxT("F6"); break;
974 case WXK_F7
: key
= wxT("F7"); break;
975 case WXK_F8
: key
= wxT("F8"); break;
976 case WXK_F9
: key
= wxT("F9"); break;
977 case WXK_F10
: key
= wxT("F10"); break;
978 case WXK_F11
: key
= wxT("F11"); break;
979 case WXK_F12
: key
= wxT("F12"); break;
980 case WXK_F13
: key
= wxT("F13"); break;
981 case WXK_F14
: key
= wxT("F14"); break;
982 case WXK_F15
: key
= wxT("F15"); break;
983 case WXK_F16
: key
= wxT("F16"); break;
984 case WXK_F17
: key
= wxT("F17"); break;
985 case WXK_F18
: key
= wxT("F18"); break;
986 case WXK_F19
: key
= wxT("F19"); break;
987 case WXK_F20
: key
= wxT("F20"); break;
988 case WXK_F21
: key
= wxT("F21"); break;
989 case WXK_F22
: key
= wxT("F22"); break;
990 case WXK_F23
: key
= wxT("F23"); break;
991 case WXK_F24
: key
= wxT("F24"); break;
992 case WXK_NUMLOCK
: key
= wxT("NUMLOCK"); break;
993 case WXK_SCROLL
: key
= wxT("SCROLL"); break;
994 case WXK_PAGEUP
: key
= wxT("PAGEUP"); break;
995 case WXK_PAGEDOWN
: key
= wxT("PAGEDOWN"); break;
996 case WXK_NUMPAD_SPACE
: key
= wxT("NUMPAD_SPACE"); break;
997 case WXK_NUMPAD_TAB
: key
= wxT("NUMPAD_TAB"); break;
998 case WXK_NUMPAD_ENTER
: key
= wxT("NUMPAD_ENTER"); break;
999 case WXK_NUMPAD_F1
: key
= wxT("NUMPAD_F1"); break;
1000 case WXK_NUMPAD_F2
: key
= wxT("NUMPAD_F2"); break;
1001 case WXK_NUMPAD_F3
: key
= wxT("NUMPAD_F3"); break;
1002 case WXK_NUMPAD_F4
: key
= wxT("NUMPAD_F4"); break;
1003 case WXK_NUMPAD_HOME
: key
= wxT("NUMPAD_HOME"); break;
1004 case WXK_NUMPAD_LEFT
: key
= wxT("NUMPAD_LEFT"); break;
1005 case WXK_NUMPAD_UP
: key
= wxT("NUMPAD_UP"); break;
1006 case WXK_NUMPAD_RIGHT
: key
= wxT("NUMPAD_RIGHT"); break;
1007 case WXK_NUMPAD_DOWN
: key
= wxT("NUMPAD_DOWN"); break;
1008 case WXK_NUMPAD_PAGEUP
: key
= wxT("NUMPAD_PAGEUP"); break;
1009 case WXK_NUMPAD_PAGEDOWN
: key
= wxT("NUMPAD_PAGEDOWN"); break;
1010 case WXK_NUMPAD_END
: key
= wxT("NUMPAD_END"); break;
1011 case WXK_NUMPAD_BEGIN
: key
= wxT("NUMPAD_BEGIN"); break;
1012 case WXK_NUMPAD_INSERT
: key
= wxT("NUMPAD_INSERT"); break;
1013 case WXK_NUMPAD_DELETE
: key
= wxT("NUMPAD_DELETE"); break;
1014 case WXK_NUMPAD_EQUAL
: key
= wxT("NUMPAD_EQUAL"); break;
1015 case WXK_NUMPAD_MULTIPLY
: key
= wxT("NUMPAD_MULTIPLY"); break;
1016 case WXK_NUMPAD_ADD
: key
= wxT("NUMPAD_ADD"); break;
1017 case WXK_NUMPAD_SEPARATOR
: key
= wxT("NUMPAD_SEPARATOR"); break;
1018 case WXK_NUMPAD_SUBTRACT
: key
= wxT("NUMPAD_SUBTRACT"); break;
1019 case WXK_NUMPAD_DECIMAL
: key
= wxT("NUMPAD_DECIMAL"); break;
1023 if ( keycode
< 128 && wxIsprint((int)keycode
) )
1024 key
.Printf(wxT("'%c'"), (char)keycode
);
1025 else if ( keycode
> 0 && keycode
< 27 )
1026 key
.Printf(_("Ctrl-%c"), wxT('A') + keycode
- 1);
1028 key
.Printf(wxT("unknown (%ld)"), keycode
);
1033 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1036 event
.ControlDown() ? wxT('C') : wxT('-'),
1037 event
.AltDown() ? wxT('A') : wxT('-'),
1038 event
.ShiftDown() ? wxT('S') : wxT('-'),
1039 event
.MetaDown() ? wxT('M') : wxT('-'));
1042 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
& event
)
1044 LogKeyEvent(wxT("Tree key down "), event
.GetKeyEvent());
1049 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
1051 // need to explicitly allow drag
1052 if ( event
.GetItem() != GetRootItem() )
1054 m_draggedItem
= event
.GetItem();
1056 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
1057 GetItemText(m_draggedItem
).c_str());
1063 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1067 void MyTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
1069 wxTreeItemId itemSrc
= m_draggedItem
,
1070 itemDst
= event
.GetItem();
1071 m_draggedItem
= (wxTreeItemId
)0l;
1073 // where to copy the item?
1074 if ( itemDst
.IsOk() && !ItemHasChildren(itemDst
) )
1076 // copy to the parent then
1077 itemDst
= GetItemParent(itemDst
);
1080 if ( !itemDst
.IsOk() )
1082 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1087 wxString text
= GetItemText(itemSrc
);
1088 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1089 text
.c_str(), GetItemText(itemDst
).c_str());
1091 // just do append here - we could also insert it just before/after the item
1092 // on which it was dropped, but this requires slightly more work... we also
1093 // completely ignore the client data and icon of the old item but could
1094 // copy them as well.
1096 // Finally, we only copy one item here but we might copy the entire tree if
1097 // we were dragging a folder.
1098 int image
= wxGetApp().ShowImages() ? TreeCtrlIcon_File
: -1;
1099 AppendItem(itemDst
, text
, image
);
1102 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
1104 wxLogMessage(wxT("OnBeginLabelEdit"));
1106 // for testing, prevent this item's label editing
1107 wxTreeItemId itemId
= event
.GetItem();
1108 if ( IsTestItem(itemId
) )
1110 wxMessageBox(wxT("You can't edit this item."));
1114 else if ( itemId
== GetRootItem() )
1116 // test that it is possible to change the text of the item being edited
1117 SetItemText(itemId
, _T("Editing root item"));
1121 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
1123 wxLogMessage(wxT("OnEndLabelEdit"));
1125 // don't allow anything except letters in the labels
1126 if ( !event
.GetLabel().IsWord() )
1128 wxMessageBox(wxT("The new label should be a single word."));
1134 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
1136 wxLogMessage(wxT("OnItemCollapsing"));
1138 // for testing, prevent the user from collapsing the first child folder
1139 wxTreeItemId itemId
= event
.GetItem();
1140 if ( IsTestItem(itemId
) )
1142 wxMessageBox(wxT("You can't collapse this item."));
1148 void MyTreeCtrl::OnItemActivated(wxTreeEvent
& event
)
1150 // show some info about this item
1151 wxTreeItemId itemId
= event
.GetItem();
1152 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
1156 item
->ShowInfo(this);
1159 wxLogMessage(wxT("OnItemActivated"));
1162 void MyTreeCtrl::OnItemMenu(wxTreeEvent
& event
)
1164 wxTreeItemId itemId
= event
.GetItem();
1165 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1168 wxLogMessage(wxT("OnItemMenu for item \"%s\""), item
? item
->GetDesc()
1174 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1176 wxPoint pt
= event
.GetPosition();
1178 if ( !HasFlag(wxTR_MULTIPLE
) )
1179 item
= GetSelection();
1180 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt
.x
, pt
.y
);
1182 // check if event was generated by keyboard (MSW-specific?)
1183 if ( pt
.x
== -1 && pt
.y
== -1 ) //(this is how MSW indicates it)
1185 // attempt to guess where to show the menu
1188 // if an item was clicked, show menu to the right of it
1190 GetBoundingRect(item
, rect
, true /* only the label */);
1191 pt
= wxPoint(rect
.GetRight(), rect
.GetTop());
1198 else // event was generated by mouse, use supplied coords
1200 pt
= ScreenToClient(pt
);
1206 void MyTreeCtrl::ShowMenu(wxTreeItemId id
, const wxPoint
& pt
)
1211 title
<< wxT("Menu for ") << GetItemText(id
);
1215 title
= wxT("Menu for no particular item");
1220 menu
.Append(TreeTest_About
, wxT("&About..."));
1221 menu
.Append(TreeTest_Dump
, wxT("&Dump"));
1223 PopupMenu(&menu
, pt
);
1224 #endif // wxUSE_MENUS
1227 void MyTreeCtrl::OnItemRClick(wxTreeEvent
& event
)
1229 wxTreeItemId itemId
= event
.GetItem();
1230 MyTreeItemData
*item
= itemId
.IsOk() ? (MyTreeItemData
*)GetItemData(itemId
)
1233 wxLogMessage(wxT("Item \"%s\" right clicked"), item
? item
->GetDesc()
1239 void MyTreeCtrl::OnRMouseDown(wxMouseEvent
& event
)
1241 wxLogMessage(wxT("Right mouse button down"));
1246 void MyTreeCtrl::OnRMouseUp(wxMouseEvent
& event
)
1248 wxLogMessage(wxT("Right mouse button up"));
1253 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent
& event
)
1255 wxTreeItemId id
= HitTest(event
.GetPosition());
1257 wxLogMessage(wxT("No item under mouse"));
1260 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(id
);
1262 wxLogMessage(wxT("Item '%s' under mouse"), item
->GetDesc());
1268 static inline const wxChar
*Bool2String(bool b
)
1270 return b
? wxT("") : wxT("not ");
1273 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
1275 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1276 wxT("%u children (%u immediately under this item)."),
1278 Bool2String(tree
->IsSelected(GetId())),
1279 Bool2String(tree
->IsExpanded(GetId())),
1280 Bool2String(tree
->IsBold(GetId())),
1281 tree
->GetChildrenCount(GetId()),
1282 tree
->GetChildrenCount(GetId(), false));