]>
Commit | Line | Data |
---|---|---|
457814b5 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: treetest.cpp | |
3 | // Purpose: wxTreeCtrl sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
3a5a2f56 | 9 | // Licence: wxWindows license |
457814b5 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
3a5a2f56 VZ |
13 | #pragma implementation |
14 | #pragma interface | |
457814b5 JS |
15 | #endif |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
3a5a2f56 | 21 | #pragma hdrstop |
457814b5 JS |
22 | #endif |
23 | ||
24 | #ifndef WX_PRECOMP | |
3a5a2f56 | 25 | #include "wx/wx.h" |
457814b5 JS |
26 | #endif |
27 | ||
3a5a2f56 | 28 | // under Windows the icons are in the .rc file |
c89a6106 VZ |
29 | #ifndef __WXMSW__ |
30 | #include "icon1.xpm" | |
31 | #include "icon2.xpm" | |
32 | #include "mondrian.xpm" | |
47908e25 RR |
33 | #endif |
34 | ||
3a5a2f56 VZ |
35 | #include "wx/log.h" |
36 | ||
37 | #include "wx/imaglist.h" | |
457814b5 JS |
38 | #include "wx/treectrl.h" |
39 | ||
40 | #include "treetest.h" | |
41 | ||
42 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
5a43154b VZ |
43 | EVT_MENU(TreeTest_Quit, MyFrame::OnQuit) |
44 | EVT_MENU(TreeTest_About, MyFrame::OnAbout) | |
4832f7c0 | 45 | EVT_MENU(TreeTest_Dump, MyFrame::OnDump) |
457814b5 JS |
46 | END_EVENT_TABLE() |
47 | ||
48 | BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) | |
5a43154b VZ |
49 | EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag) |
50 | EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag) | |
51 | EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit) | |
52 | EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit) | |
53 | EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem) | |
54 | EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo) | |
55 | EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo) | |
56 | EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded) | |
57 | EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding) | |
58 | EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed) | |
59 | EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing) | |
60 | EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged) | |
61 | EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging) | |
4832f7c0 | 62 | EVT_CHAR(MyTreeCtrl::OnKeyDown) |
457814b5 JS |
63 | END_EVENT_TABLE() |
64 | ||
65 | IMPLEMENT_APP(MyApp) | |
66 | ||
67 | // `Main program' equivalent, creating windows and returning main app frame | |
c89a6106 | 68 | bool MyApp::OnInit() |
457814b5 JS |
69 | { |
70 | // Create the main frame window | |
3a5a2f56 | 71 | MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340); |
457814b5 | 72 | |
3a5a2f56 VZ |
73 | // Show the frame |
74 | frame->Show(TRUE); | |
75 | SetTopWindow(frame); | |
457814b5 | 76 | |
3a5a2f56 VZ |
77 | return TRUE; |
78 | } | |
457814b5 | 79 | |
457814b5 | 80 | |
3a5a2f56 VZ |
81 | // My frame constructor |
82 | MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) | |
83 | : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)) | |
84 | { | |
85 | // This reduces flicker effects - even better would be to define | |
86 | // OnEraseBackground to do nothing. When the tree control's scrollbars are | |
87 | // show or hidden, the frame is sent a background erase event. | |
88 | SetBackgroundColour(wxColour(255, 255, 255)); | |
89 | ||
90 | // Give it an icon | |
91 | SetIcon(wxICON(mondrian)); | |
457814b5 JS |
92 | |
93 | // Make a menubar | |
94 | wxMenu *file_menu = new wxMenu; | |
95 | ||
4832f7c0 VZ |
96 | file_menu->Append(TreeTest_Dump, "&Dump tree items"); |
97 | file_menu->AppendSeparator(); | |
5a43154b | 98 | file_menu->Append(TreeTest_About, "&About..."); |
4832f7c0 | 99 | file_menu->AppendSeparator(); |
5a43154b | 100 | file_menu->Append(TreeTest_Quit, "E&xit"); |
4832f7c0 | 101 | |
457814b5 JS |
102 | wxMenuBar *menu_bar = new wxMenuBar; |
103 | menu_bar->Append(file_menu, "&File"); | |
3a5a2f56 | 104 | SetMenuBar(menu_bar); |
457814b5 JS |
105 | |
106 | // Make a panel with a message | |
5a43154b | 107 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, |
3a5a2f56 VZ |
108 | wxDefaultPosition, wxDefaultSize, |
109 | wxTR_HAS_BUTTONS | wxSUNKEN_BORDER); | |
110 | wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "", | |
111 | wxDefaultPosition, wxDefaultSize, | |
112 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
457814b5 JS |
113 | |
114 | wxLayoutConstraints *c = new wxLayoutConstraints; | |
3a5a2f56 VZ |
115 | c->top.SameAs(this, wxTop); |
116 | c->left.SameAs(this, wxLeft); | |
117 | c->right.SameAs(this, wxRight); | |
118 | c->height.PercentOf(this, wxHeight, 66); | |
119 | m_treeCtrl->SetConstraints(c); | |
457814b5 JS |
120 | |
121 | c = new wxLayoutConstraints; | |
3a5a2f56 VZ |
122 | c->top.Below(m_treeCtrl); |
123 | c->left.SameAs(this, wxLeft); | |
124 | c->right.SameAs(this, wxRight); | |
125 | c->bottom.SameAs(this, wxBottom); | |
126 | textCtrl->SetConstraints(c); | |
127 | SetAutoLayout(TRUE); | |
128 | ||
129 | // create a status bar with 3 panes | |
130 | CreateStatusBar(3); | |
131 | SetStatusText("", 0); | |
132 | ||
133 | // set our text control as the log target | |
134 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl); | |
135 | delete wxLog::SetActiveTarget(logWindow); | |
457814b5 JS |
136 | } |
137 | ||
c89a6106 | 138 | MyFrame::~MyFrame() |
457814b5 | 139 | { |
3a5a2f56 | 140 | delete wxLog::SetActiveTarget(NULL); |
457814b5 JS |
141 | } |
142 | ||
3a5a2f56 | 143 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
457814b5 JS |
144 | { |
145 | Close(TRUE); | |
146 | } | |
147 | ||
3a5a2f56 | 148 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 149 | { |
4832f7c0 VZ |
150 | wxMessageDialog dialog(this, |
151 | "Tree test sample\n" | |
152 | "Julian Smart (c) 1997", | |
3a5a2f56 | 153 | "About tree test", wxOK); |
457814b5 JS |
154 | |
155 | dialog.ShowModal(); | |
156 | } | |
157 | ||
4832f7c0 VZ |
158 | void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) |
159 | { | |
160 | wxTreeItemId root=m_treeCtrl->GetSelection(); | |
161 | m_treeCtrl->GetItemsRecursively(root, -1); | |
162 | } | |
163 | ||
3a5a2f56 VZ |
164 | // MyTreeCtrl implementation |
165 | MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, | |
166 | const wxPoint& pos, const wxSize& size, | |
167 | long style) | |
168 | : wxTreeCtrl(parent, id, pos, size, style) | |
457814b5 | 169 | { |
3a5a2f56 VZ |
170 | // Make an image list containing small icons |
171 | m_imageListNormal = new wxImageList(16, 16, TRUE); | |
457814b5 | 172 | |
3a5a2f56 | 173 | // should correspond to TreeCtrlIcon_xxx enum |
3a5a2f56 | 174 | m_imageListNormal->Add(wxICON(icon2)); |
df875e59 | 175 | m_imageListNormal->Add(wxICON(icon1)); |
457814b5 | 176 | |
3a5a2f56 | 177 | SetImageList(m_imageListNormal); |
457814b5 | 178 | |
3a5a2f56 | 179 | // Add some items to the tree |
4832f7c0 | 180 | AddTestItemsToTree(3, 2); |
457814b5 JS |
181 | } |
182 | ||
3a5a2f56 | 183 | MyTreeCtrl::~MyTreeCtrl() |
457814b5 | 184 | { |
3a5a2f56 | 185 | delete m_imageListNormal; |
457814b5 JS |
186 | } |
187 | ||
3a5a2f56 VZ |
188 | void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent, |
189 | size_t numChildren, | |
4832f7c0 VZ |
190 | size_t depth, |
191 | size_t folder) | |
457814b5 | 192 | { |
3a5a2f56 VZ |
193 | if ( depth > 0 ) |
194 | { | |
195 | wxString str; | |
196 | for ( size_t n = 0; n < numChildren; n++ ) | |
197 | { | |
198 | // at depth 1 elements won't have any more children | |
4832f7c0 VZ |
199 | if (depth == 1) |
200 | str.Printf("%s child %d.%d", "File", folder, n + 1); | |
201 | else | |
202 | str.Printf("%s child %d","Folder", n + 1); | |
203 | ||
3a5a2f56 VZ |
204 | int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder; |
205 | wxTreeItemId id = AppendItem(idParent, str, image, image, | |
206 | new MyTreeItemData(str)); | |
4832f7c0 | 207 | AddItemsRecursively(id, numChildren, depth - 1,n+1); |
3a5a2f56 VZ |
208 | } |
209 | } | |
210 | //else: done! | |
457814b5 JS |
211 | } |
212 | ||
3a5a2f56 VZ |
213 | void MyTreeCtrl::AddTestItemsToTree(size_t numChildren, |
214 | size_t depth) | |
457814b5 | 215 | { |
3efb01df | 216 | wxTreeItemId rootId = AddRoot("Root", |
3a5a2f56 VZ |
217 | TreeCtrlIcon_Folder, TreeCtrlIcon_Folder, |
218 | new MyTreeItemData("Root item")); | |
457814b5 | 219 | |
4832f7c0 VZ |
220 | AddItemsRecursively(rootId, numChildren, depth,0); |
221 | } | |
222 | ||
223 | void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie) | |
224 | { | |
225 | wxTreeItemId id; | |
226 | ||
227 | if( cookie == -1 ) | |
228 | id = GetFirstChild(idParent, cookie); | |
229 | else | |
230 | id = GetNextChild(idParent, cookie); | |
231 | ||
232 | if(id <= 0) | |
233 | return; | |
234 | ||
235 | wxString text=GetItemText(id); | |
236 | wxLogMessage(text); | |
237 | ||
238 | if (ItemHasChildren(id)) | |
239 | GetItemsRecursively(id,-1); | |
240 | ||
241 | GetItemsRecursively(idParent, cookie); | |
457814b5 JS |
242 | } |
243 | ||
4832f7c0 | 244 | |
3a5a2f56 VZ |
245 | // avoid repetition |
246 | #define TREE_EVENT_HANDLER(name) \ | |
247 | void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \ | |
248 | { \ | |
249 | wxLogMessage(#name); \ | |
250 | } | |
457814b5 | 251 | |
3a5a2f56 VZ |
252 | TREE_EVENT_HANDLER(OnBeginDrag) |
253 | TREE_EVENT_HANDLER(OnBeginRDrag) | |
254 | TREE_EVENT_HANDLER(OnBeginLabelEdit) | |
255 | TREE_EVENT_HANDLER(OnEndLabelEdit) | |
256 | TREE_EVENT_HANDLER(OnDeleteItem) | |
257 | TREE_EVENT_HANDLER(OnGetInfo) | |
258 | TREE_EVENT_HANDLER(OnSetInfo) | |
259 | TREE_EVENT_HANDLER(OnItemExpanded) | |
260 | TREE_EVENT_HANDLER(OnItemExpanding) | |
261 | TREE_EVENT_HANDLER(OnItemCollapsed) | |
262 | TREE_EVENT_HANDLER(OnSelChanged) | |
263 | TREE_EVENT_HANDLER(OnSelChanging) | |
264 | ||
265 | #undef TREE_EVENT_HANDLER | |
266 | ||
267 | void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event) | |
457814b5 | 268 | { |
f135ff73 VZ |
269 | wxLogMessage("OnItemCollapsing"); |
270 | ||
3a5a2f56 VZ |
271 | // for testing, prevent the user from collapsing the root item |
272 | wxTreeItemId itemId = event.GetItem(); | |
273 | if ( !GetParent(itemId).IsOk() ) | |
274 | { | |
f135ff73 | 275 | wxMessageBox("You can't collapse the root item."); |
457814b5 | 276 | |
3a5a2f56 VZ |
277 | event.Veto(); |
278 | } | |
457814b5 JS |
279 | } |
280 | ||
d18ed59a | 281 | void MyTreeCtrl::OnKeyDown(wxTreeEvent& WXUNUSED(event)) |
457814b5 | 282 | { |
3a5a2f56 VZ |
283 | // show some info about this item |
284 | wxTreeItemId itemId = GetSelection(); | |
285 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId); | |
457814b5 | 286 | |
3a5a2f56 VZ |
287 | if ( item != NULL ) |
288 | { | |
289 | item->ShowInfo(this); | |
290 | } | |
457814b5 | 291 | |
3a5a2f56 | 292 | wxLogMessage("OnKeyDown"); |
457814b5 JS |
293 | } |
294 | ||
3a5a2f56 | 295 | static inline const char *Bool2String(bool b) |
457814b5 | 296 | { |
3a5a2f56 | 297 | return b ? "" : "not "; |
457814b5 JS |
298 | } |
299 | ||
3a5a2f56 | 300 | void MyTreeItemData::ShowInfo(wxTreeCtrl *tree) |
457814b5 | 301 | { |
4832f7c0 VZ |
302 | wxLogMessage("Item '%s': %sselected, %sexpanded, " |
303 | "%u children (%u immediately under this item).", | |
3a5a2f56 | 304 | m_desc.c_str(), |
f135ff73 | 305 | Bool2String(tree->IsSelected(GetId())), |
4832f7c0 VZ |
306 | Bool2String(tree->IsExpanded(GetId())), |
307 | tree->GetChildrenCount(GetId()), | |
308 | tree->GetChildrenCount(GetId(), FALSE)); | |
457814b5 | 309 | } |