1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // Define a new application type
13 class MyApp
: public wxApp
16 MyApp() { m_showImages
= TRUE
; }
20 void SetShowImages(bool show
) { m_showImages
= show
; }
21 bool ShowImages() const { return m_showImages
; }
27 class MyTreeItemData
: public wxTreeItemData
30 MyTreeItemData(const wxString
& desc
) : m_desc(desc
) { }
32 void ShowInfo(wxTreeCtrl
*tree
);
33 const char *GetDesc() const { return m_desc
.c_str(); }
39 class MyTreeCtrl
: public wxTreeCtrl
45 TreeCtrlIcon_FileSelected
,
47 TreeCtrlIcon_FolderSelected
,
48 TreeCtrlIcon_FolderOpened
52 MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
53 const wxPoint
& pos
, const wxSize
& size
,
55 virtual ~MyTreeCtrl();
57 void OnBeginDrag(wxTreeEvent
& event
);
58 void OnBeginRDrag(wxTreeEvent
& event
);
59 void OnEndDrag(wxTreeEvent
& event
);
60 void OnBeginLabelEdit(wxTreeEvent
& event
);
61 void OnEndLabelEdit(wxTreeEvent
& event
);
62 void OnDeleteItem(wxTreeEvent
& event
);
63 void OnGetInfo(wxTreeEvent
& event
);
64 void OnSetInfo(wxTreeEvent
& event
);
65 void OnItemExpanded(wxTreeEvent
& event
);
66 void OnItemExpanding(wxTreeEvent
& event
);
67 void OnItemCollapsed(wxTreeEvent
& event
);
68 void OnItemCollapsing(wxTreeEvent
& event
);
69 void OnSelChanged(wxTreeEvent
& event
);
70 void OnSelChanging(wxTreeEvent
& event
);
71 void OnTreeKeyDown(wxTreeEvent
& event
);
72 void OnItemActivated(wxTreeEvent
& event
);
73 void OnRMouseDClick(wxMouseEvent
& event
);
75 void GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
);
77 void CreateImageList(int size
= 32);
79 void AddTestItemsToTree(size_t numChildren
, size_t depth
);
81 void DoSortChildren(const wxTreeItemId
& item
, bool reverse
= FALSE
)
82 { m_reverseSort
= reverse
; wxTreeCtrl::SortChildren(item
); }
83 void DoEnsureVisible() { EnsureVisible(m_lastItem
); }
85 void DoToggleIcon(const wxTreeItemId
& item
);
88 virtual int OnCompareItems(const wxTreeItemId
& i1
, const wxTreeItemId
& i2
);
90 // is this the test item which we use in several event handlers?
91 bool IsTestItem(const wxTreeItemId
& item
)
93 // the test item is the first child folder
94 return GetParent(item
) == GetRootItem() && !GetPrevSibling(item
);
98 void AddItemsRecursively(const wxTreeItemId
& idParent
,
103 wxImageList
*m_imageListNormal
;
104 bool m_reverseSort
; // flag for OnCompareItems
105 wxTreeItemId m_lastItem
, // for OnEnsureVisible()
106 m_draggedItem
; // item being dragged right now
108 // NB: due to an ugly wxMSW hack you _must_ use DECLARE_DYNAMIC_CLASS()
109 // if you want your overloaded OnCompareItems() to be called.
110 // OTOH, if you don't want it you may omit the next line - this will
111 // make default (alphabetical) sorting much faster under wxMSW.
112 DECLARE_DYNAMIC_CLASS(MyTreeCtrl
)
113 DECLARE_EVENT_TABLE()
116 // Define a new frame type
117 class MyFrame
: public wxFrame
121 MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
);
125 void OnQuit(wxCommandEvent
& event
);
126 void OnAbout(wxCommandEvent
& event
);
128 void OnDump(wxCommandEvent
& event
);
129 #ifndef NO_MULTIPLE_SELECTION
130 void OnDumpSelected(wxCommandEvent
& event
);
131 void OnSelect(wxCommandEvent
& event
);
132 void OnUnselect(wxCommandEvent
& event
);
133 void OnToggleSel(wxCommandEvent
& event
);
134 #endif // NO_MULTIPLE_SELECTION
135 void OnDelete(wxCommandEvent
& event
);
136 void OnDeleteChildren(wxCommandEvent
& event
);
137 void OnDeleteAll(wxCommandEvent
& event
);
139 void OnRecreate(wxCommandEvent
& event
);
140 void OnToggleImages(wxCommandEvent
& event
);
141 void OnSetImageSize(wxCommandEvent
& event
);
142 void OnCollapseAndReset(wxCommandEvent
& event
);
144 void OnSetBold(wxCommandEvent
& WXUNUSED(event
)) { DoSetBold(TRUE
); }
145 void OnClearBold(wxCommandEvent
& WXUNUSED(event
)) { DoSetBold(FALSE
); }
147 void OnEnsureVisible(wxCommandEvent
& event
);
149 void OnCount(wxCommandEvent
& event
);
150 void OnCountRec(wxCommandEvent
& event
);
152 void OnRename(wxCommandEvent
& event
);
153 void OnSort(wxCommandEvent
& event
) { DoSort(); }
154 void OnSortRev(wxCommandEvent
& event
) { DoSort(TRUE
); }
156 void OnAddItem(wxCommandEvent
& event
);
157 void OnInsertItem(wxCommandEvent
& event
);
159 void OnIncIndent(wxCommandEvent
& event
);
160 void OnDecIndent(wxCommandEvent
& event
);
162 void OnIncSpacing(wxCommandEvent
& event
);
163 void OnDecSpacing(wxCommandEvent
& event
);
165 void OnToggleIcon(wxCommandEvent
& event
);
167 void OnSize(wxSizeEvent
& event
);
170 void DoSort(bool reverse
= FALSE
);
172 void Resize(const wxSize
& size
);
174 MyTreeCtrl
*m_treeCtrl
;
175 wxTextCtrl
*m_textCtrl
;
177 void DoSetBold(bool bold
= TRUE
);
179 DECLARE_EVENT_TABLE()
182 // menu and control ids
188 TreeTest_DumpSelected
,
197 TreeTest_DeleteChildren
,
200 TreeTest_ToggleImages
,
201 TreeTest_SetImageSize
,
203 TreeTest_CollapseAndReset
,
204 TreeTest_EnsureVisible
,