]>
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 VZ |
28 | #include "wx/log.h" |
29 | ||
91b8de8d | 30 | #include "wx/image.h" |
3a5a2f56 | 31 | #include "wx/imaglist.h" |
457814b5 JS |
32 | #include "wx/treectrl.h" |
33 | ||
cf724bce RR |
34 | #include "math.h" |
35 | ||
457814b5 JS |
36 | #include "treetest.h" |
37 | ||
91b8de8d RR |
38 | #define USE_TR_HAS_VARIABLE_ROW_HIGHT 1 |
39 | ||
40 | // under Windows the icons are in the .rc file | |
41 | #ifndef __WXMSW__ | |
42 | #if !USE_TR_HAS_VARIABLE_ROW_HIGHT | |
43 | #include "icon1.xpm" | |
44 | #endif | |
45 | #include "icon2.xpm" | |
46 | #include "mondrian.xpm" | |
47 | #endif | |
48 | ||
49 | ||
e1ee62bd VZ |
50 | // verify that the item is ok and insult the user if it is not |
51 | #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \ | |
52 | wxMessageBox("Please select some item first!", \ | |
53 | "Tree sample error", \ | |
54 | wxOK | wxICON_EXCLAMATION, \ | |
55 | this); \ | |
56 | return; \ | |
5f939e78 VZ |
57 | } |
58 | ||
457814b5 | 59 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
5f939e78 VZ |
60 | EVT_MENU(TreeTest_Quit, MyFrame::OnQuit) |
61 | EVT_MENU(TreeTest_About, MyFrame::OnAbout) | |
62 | EVT_MENU(TreeTest_Dump, MyFrame::OnDump) | |
91b8de8d | 63 | EVT_MENU(TreeTest_Dump_Selected, MyFrame::OnDumpSelected) |
5f939e78 VZ |
64 | EVT_MENU(TreeTest_Rename, MyFrame::OnRename) |
65 | EVT_MENU(TreeTest_Sort, MyFrame::OnSort) | |
66 | EVT_MENU(TreeTest_SortRev, MyFrame::OnSortRev) | |
67 | EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold) | |
68 | EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold) | |
69 | EVT_MENU(TreeTest_Delete, MyFrame::OnDelete) | |
70 | EVT_MENU(TreeTest_DeleteChildren, MyFrame::OnDeleteChildren) | |
71 | EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll) | |
72 | EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate) | |
73 | EVT_MENU(TreeTest_CollapseAndReset, MyFrame::OnCollapseAndReset) | |
74 | EVT_MENU(TreeTest_EnsureVisible, MyFrame::OnEnsureVisible) | |
75 | EVT_MENU(TreeTest_AddItem, MyFrame::OnAddItem) | |
cf724bce RR |
76 | EVT_MENU(TreeTest_IncIndent, MyFrame::OnIncIndent) |
77 | EVT_MENU(TreeTest_DecIndent, MyFrame::OnDecIndent) | |
78 | EVT_MENU(TreeTest_IncSpacing, MyFrame::OnIncSpacing) | |
79 | EVT_MENU(TreeTest_DecSpacing, MyFrame::OnDecSpacing) | |
457814b5 JS |
80 | END_EVENT_TABLE() |
81 | ||
82 | BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) | |
5f939e78 VZ |
83 | EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag) |
84 | EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag) | |
85 | EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit) | |
86 | EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit) | |
87 | EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem) | |
88 | EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo) | |
89 | EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo) | |
90 | EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded) | |
91 | EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding) | |
92 | EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed) | |
93 | EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing) | |
94 | EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged) | |
95 | EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging) | |
96 | EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown) | |
97 | EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated) | |
98 | EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick) | |
457814b5 JS |
99 | END_EVENT_TABLE() |
100 | ||
101 | IMPLEMENT_APP(MyApp) | |
102 | ||
c89a6106 | 103 | bool MyApp::OnInit() |
457814b5 | 104 | { |
5f939e78 VZ |
105 | // Create the main frame window |
106 | MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340); | |
457814b5 | 107 | |
5f939e78 VZ |
108 | // Show the frame |
109 | frame->Show(TRUE); | |
110 | SetTopWindow(frame); | |
457814b5 | 111 | |
5f939e78 | 112 | return TRUE; |
3a5a2f56 | 113 | } |
457814b5 | 114 | |
457814b5 | 115 | |
3a5a2f56 VZ |
116 | // My frame constructor |
117 | MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) | |
118 | : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)) | |
119 | { | |
5f939e78 VZ |
120 | // This reduces flicker effects - even better would be to define |
121 | // OnEraseBackground to do nothing. When the tree control's scrollbars are | |
122 | // show or hidden, the frame is sent a background erase event. | |
123 | SetBackgroundColour(wxColour(255, 255, 255)); | |
124 | ||
125 | // Give it an icon | |
126 | SetIcon(wxICON(mondrian)); | |
127 | ||
128 | // Make a menubar | |
129 | wxMenu *file_menu = new wxMenu, | |
130 | *tree_menu = new wxMenu, | |
131 | *item_menu = new wxMenu; | |
132 | ||
133 | file_menu->Append(TreeTest_About, "&About..."); | |
134 | file_menu->AppendSeparator(); | |
135 | file_menu->Append(TreeTest_Quit, "E&xit"); | |
136 | ||
137 | tree_menu->Append(TreeTest_Recreate, "&Recreate the tree"); | |
138 | tree_menu->Append(TreeTest_CollapseAndReset, "C&ollapse and reset"); | |
139 | tree_menu->AppendSeparator(); | |
140 | tree_menu->Append(TreeTest_AddItem, "Append a &new item"); | |
141 | tree_menu->Append(TreeTest_Delete, "&Delete this item"); | |
142 | tree_menu->Append(TreeTest_DeleteChildren, "Delete &children"); | |
143 | tree_menu->Append(TreeTest_DeleteAll, "Delete &all items"); | |
144 | tree_menu->AppendSeparator(); | |
145 | tree_menu->Append(TreeTest_Sort, "Sort children of current item"); | |
146 | tree_menu->Append(TreeTest_SortRev, "Sort in reversed order"); | |
147 | tree_menu->AppendSeparator(); | |
148 | tree_menu->Append(TreeTest_EnsureVisible, "Make the last item &visible"); | |
cf724bce RR |
149 | tree_menu->AppendSeparator(); |
150 | tree_menu->Append(TreeTest_IncIndent, "Add 5 points to indentation\tAlt-I"); | |
151 | tree_menu->Append(TreeTest_DecIndent, "Reduce indentation by 5 points\tAlt-R"); | |
152 | tree_menu->AppendSeparator(); | |
153 | tree_menu->Append(TreeTest_IncSpacing, "Add 5 points to spacing\tCtrl-I"); | |
154 | tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R"); | |
5f939e78 | 155 | |
5f939e78 | 156 | item_menu->AppendSeparator(); |
91b8de8d RR |
157 | item_menu->Append(TreeTest_Dump, "&Dump item children"); |
158 | item_menu->Append(TreeTest_Dump_Selected, "Dump selected items\tAlt-S"); | |
5f939e78 | 159 | item_menu->Append(TreeTest_Rename, "&Rename item..."); |
91b8de8d | 160 | |
5f939e78 VZ |
161 | item_menu->AppendSeparator(); |
162 | item_menu->Append(TreeTest_Bold, "Make item &bold"); | |
163 | item_menu->Append(TreeTest_UnBold, "Make item ¬ bold"); | |
164 | ||
165 | wxMenuBar *menu_bar = new wxMenuBar; | |
166 | menu_bar->Append(file_menu, "&File"); | |
167 | menu_bar->Append(tree_menu, "&Tree"); | |
168 | menu_bar->Append(item_menu, "&Item"); | |
169 | SetMenuBar(menu_bar); | |
170 | ||
171 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, | |
172 | wxDefaultPosition, wxDefaultSize, | |
91b8de8d RR |
173 | wxTR_HAS_BUTTONS | |
174 | wxTR_MULTIPLE | | |
175 | #if USE_TR_HAS_VARIABLE_ROW_HIGHT | |
176 | wxTR_HAS_VARIABLE_ROW_HIGHT | | |
177 | #endif | |
178 | wxSUNKEN_BORDER); | |
5f939e78 VZ |
179 | wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "", |
180 | wxDefaultPosition, wxDefaultSize, | |
181 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
182 | ||
183 | wxLayoutConstraints *c = new wxLayoutConstraints; | |
184 | c->top.SameAs(this, wxTop); | |
185 | c->left.SameAs(this, wxLeft); | |
186 | c->right.SameAs(this, wxRight); | |
187 | c->height.PercentOf(this, wxHeight, 66); | |
188 | m_treeCtrl->SetConstraints(c); | |
189 | ||
190 | c = new wxLayoutConstraints; | |
191 | c->top.Below(m_treeCtrl); | |
192 | c->left.SameAs(this, wxLeft); | |
193 | c->right.SameAs(this, wxRight); | |
194 | c->bottom.SameAs(this, wxBottom); | |
195 | textCtrl->SetConstraints(c); | |
196 | SetAutoLayout(TRUE); | |
197 | ||
198 | // create a status bar with 3 panes | |
199 | CreateStatusBar(3); | |
200 | SetStatusText("", 0); | |
3a5a2f56 | 201 | |
a367b9b3 | 202 | #ifdef __WXMOTIF__ |
5f939e78 VZ |
203 | // For some reason, we get a memcpy crash in wxLogStream::DoLogStream |
204 | // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc? | |
205 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
a367b9b3 | 206 | #else |
5f939e78 VZ |
207 | // set our text control as the log target |
208 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl); | |
209 | delete wxLog::SetActiveTarget(logWindow); | |
a367b9b3 | 210 | #endif |
457814b5 JS |
211 | } |
212 | ||
c89a6106 | 213 | MyFrame::~MyFrame() |
457814b5 | 214 | { |
5f939e78 | 215 | delete wxLog::SetActiveTarget(NULL); |
457814b5 JS |
216 | } |
217 | ||
3a5a2f56 | 218 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 219 | { |
5f939e78 | 220 | Close(TRUE); |
457814b5 JS |
221 | } |
222 | ||
3a5a2f56 | 223 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 224 | { |
5f939e78 VZ |
225 | wxMessageBox("Tree test sample\n" |
226 | "Julian Smart (c) 1997,\n" | |
227 | "Vadim Zeitlin (c) 1998", | |
228 | "About tree test", | |
229 | wxOK | wxICON_INFORMATION, this); | |
e1ee62bd VZ |
230 | } |
231 | ||
232 | void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event)) | |
233 | { | |
5f939e78 | 234 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 235 | |
5f939e78 | 236 | CHECK_ITEM( item ); |
e1ee62bd | 237 | |
5f939e78 VZ |
238 | static wxString s_text; |
239 | s_text = wxGetTextFromUser("New name: ", "Tree sample question", | |
240 | s_text, this); | |
241 | if ( !s_text.IsEmpty() ) | |
242 | { | |
243 | m_treeCtrl->SetItemText(item, s_text); | |
244 | } | |
e1ee62bd VZ |
245 | } |
246 | ||
247 | void MyFrame::DoSort(bool reverse) | |
248 | { | |
5f939e78 | 249 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 250 | |
5f939e78 | 251 | CHECK_ITEM( item ); |
457814b5 | 252 | |
5f939e78 | 253 | m_treeCtrl->DoSortChildren(item, reverse); |
457814b5 JS |
254 | } |
255 | ||
4832f7c0 VZ |
256 | void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) |
257 | { | |
5f939e78 | 258 | wxTreeItemId root = m_treeCtrl->GetSelection(); |
e1ee62bd | 259 | |
5f939e78 | 260 | CHECK_ITEM( root ); |
e1ee62bd | 261 | |
5f939e78 | 262 | m_treeCtrl->GetItemsRecursively(root, -1); |
4832f7c0 VZ |
263 | } |
264 | ||
91b8de8d RR |
265 | void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event)) |
266 | { | |
267 | wxArrayTreeItemIds array; | |
268 | ||
269 | m_treeCtrl->GetSelections(array); | |
270 | size_t nos=array.Count(); | |
271 | wxLogMessage(wxString("items selected : ")<< (int)nos); | |
272 | ||
273 | for (size_t n=0; n<nos; ++n) | |
274 | wxLogMessage(m_treeCtrl->GetItemText(array.Item(n))); | |
275 | } | |
276 | ||
add28c55 VZ |
277 | void MyFrame::DoSetBold(bool bold) |
278 | { | |
5f939e78 | 279 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 280 | |
5f939e78 | 281 | CHECK_ITEM( item ); |
e1ee62bd | 282 | |
5f939e78 | 283 | m_treeCtrl->SetItemBold(item, bold); |
add28c55 VZ |
284 | } |
285 | ||
ff5bf259 VZ |
286 | void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) |
287 | { | |
5f939e78 | 288 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 289 | |
5f939e78 | 290 | CHECK_ITEM( item ); |
e1ee62bd | 291 | |
5f939e78 | 292 | m_treeCtrl->Delete(item); |
ff5bf259 VZ |
293 | } |
294 | ||
372edb9d VZ |
295 | void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event)) |
296 | { | |
5f939e78 | 297 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 298 | |
5f939e78 | 299 | CHECK_ITEM( item ); |
e1ee62bd | 300 | |
5f939e78 | 301 | m_treeCtrl->DeleteChildren(item); |
372edb9d VZ |
302 | } |
303 | ||
ff5bf259 VZ |
304 | void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) |
305 | { | |
5f939e78 | 306 | m_treeCtrl->DeleteAllItems(); |
ff5bf259 VZ |
307 | } |
308 | ||
309 | void MyFrame::OnRecreate(wxCommandEvent& event) | |
310 | { | |
5f939e78 VZ |
311 | OnDeleteAll(event); |
312 | m_treeCtrl->AddTestItemsToTree(3, 2); | |
ff5bf259 VZ |
313 | } |
314 | ||
dd3646fd VZ |
315 | void MyFrame::OnCollapseAndReset(wxCommandEvent& event) |
316 | { | |
5f939e78 | 317 | m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem()); |
dd3646fd VZ |
318 | } |
319 | ||
f65635b5 VZ |
320 | void MyFrame::OnEnsureVisible(wxCommandEvent& event) |
321 | { | |
322 | m_treeCtrl->DoEnsureVisible(); | |
323 | } | |
324 | ||
5f939e78 VZ |
325 | void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event)) |
326 | { | |
327 | static int s_num = 0; | |
328 | ||
329 | wxString text; | |
330 | text.Printf("Item #%d", ++s_num); | |
331 | ||
332 | m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(), | |
333 | text, | |
334 | MyTreeCtrl::TreeCtrlIcon_File); | |
335 | } | |
336 | ||
cf724bce RR |
337 | void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event)) |
338 | { | |
339 | unsigned int indent = m_treeCtrl->GetIndent(); | |
340 | if (indent < 100) | |
341 | m_treeCtrl->SetIndent( indent+5 ); | |
342 | } | |
343 | ||
344 | void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event)) | |
345 | { | |
346 | unsigned int indent = m_treeCtrl->GetIndent(); | |
347 | if (indent > 10) | |
348 | m_treeCtrl->SetIndent( indent-5 ); | |
349 | } | |
350 | ||
351 | void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event)) | |
352 | { | |
353 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
354 | if (indent < 100) | |
355 | m_treeCtrl->SetSpacing( indent+5 ); | |
356 | } | |
357 | ||
358 | void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event)) | |
359 | { | |
360 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
361 | if (indent > 10) | |
362 | m_treeCtrl->SetSpacing( indent-5 ); | |
363 | } | |
364 | ||
3a5a2f56 | 365 | // MyTreeCtrl implementation |
23fd5130 VZ |
366 | IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl) |
367 | ||
3a5a2f56 VZ |
368 | MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, |
369 | const wxPoint& pos, const wxSize& size, | |
370 | long style) | |
371 | : wxTreeCtrl(parent, id, pos, size, style) | |
457814b5 | 372 | { |
91b8de8d RR |
373 | #if USE_TR_HAS_VARIABLE_ROW_HIGHT |
374 | wxImage::AddHandler(new wxJPEGHandler); | |
375 | wxImage image; | |
376 | ||
377 | image.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG ); | |
378 | #endif | |
379 | ||
5f939e78 | 380 | m_reverseSort = FALSE; |
e1ee62bd | 381 | |
5f939e78 VZ |
382 | // Make an image list containing small icons |
383 | m_imageListNormal = new wxImageList(16, 16, TRUE); | |
457814b5 | 384 | |
5f939e78 | 385 | // should correspond to TreeCtrlIcon_xxx enum |
f60d0f94 | 386 | #if defined(__WXMSW__) && defined(__WIN16__) |
5f939e78 VZ |
387 | // This is required in 16-bit Windows mode only because we can't load a specific (16x16) |
388 | // icon image, so it comes out stretched | |
91b8de8d RR |
389 | # if USE_TR_HAS_VARIABLE_ROW_HIGHT |
390 | m_imageListNormal->Add(image.ConvertToBitmap()); | |
391 | # else | |
5f939e78 | 392 | m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE)); |
91b8de8d | 393 | # endif |
5f939e78 | 394 | m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE)); |
f60d0f94 | 395 | #else |
91b8de8d RR |
396 | # if USE_TR_HAS_VARIABLE_ROW_HIGHT |
397 | m_imageListNormal->Add(image.ConvertToBitmap()); | |
398 | # else | |
5f939e78 | 399 | m_imageListNormal->Add(wxICON(icon1)); |
91b8de8d | 400 | # endif |
5f939e78 | 401 | m_imageListNormal->Add(wxICON(icon2)); |
f60d0f94 | 402 | #endif |
457814b5 | 403 | |
5f939e78 | 404 | SetImageList(m_imageListNormal); |
457814b5 | 405 | |
5f939e78 VZ |
406 | // Add some items to the tree |
407 | AddTestItemsToTree(3, 2); | |
457814b5 JS |
408 | } |
409 | ||
3a5a2f56 | 410 | MyTreeCtrl::~MyTreeCtrl() |
457814b5 | 411 | { |
5f939e78 | 412 | delete m_imageListNormal; |
457814b5 JS |
413 | } |
414 | ||
e1ee62bd VZ |
415 | int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1, |
416 | const wxTreeItemId& item2) | |
417 | { | |
5f939e78 VZ |
418 | if ( m_reverseSort ) |
419 | { | |
420 | // just exchange 1st and 2nd items | |
421 | return wxTreeCtrl::OnCompareItems(item2, item1); | |
422 | } | |
423 | else | |
424 | { | |
425 | return wxTreeCtrl::OnCompareItems(item1, item2); | |
426 | } | |
e1ee62bd VZ |
427 | } |
428 | ||
3a5a2f56 VZ |
429 | void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent, |
430 | size_t numChildren, | |
4832f7c0 VZ |
431 | size_t depth, |
432 | size_t folder) | |
457814b5 | 433 | { |
f65635b5 | 434 | if ( depth > 0 ) |
3a5a2f56 | 435 | { |
f65635b5 VZ |
436 | wxString str; |
437 | for ( size_t n = 0; n < numChildren; n++ ) | |
438 | { | |
439 | // at depth 1 elements won't have any more children | |
440 | if (depth == 1) | |
441 | str.Printf("%s child %d.%d", "File", folder, n + 1); | |
442 | else | |
443 | str.Printf("%s child %d", "Folder", n + 1); | |
444 | ||
445 | int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder; | |
446 | wxTreeItemId id = AppendItem(idParent, str, image, image, | |
447 | new MyTreeItemData(str)); | |
448 | ||
449 | // remember the last child for OnEnsureVisible() | |
450 | if ( depth == 1 && n == numChildren - 1 ) | |
451 | { | |
452 | m_lastItem = id; | |
453 | } | |
454 | ||
455 | AddItemsRecursively(id, numChildren, depth - 1, n + 1); | |
456 | } | |
3a5a2f56 | 457 | } |
f65635b5 | 458 | //else: done! |
457814b5 JS |
459 | } |
460 | ||
3a5a2f56 VZ |
461 | void MyTreeCtrl::AddTestItemsToTree(size_t numChildren, |
462 | size_t depth) | |
457814b5 | 463 | { |
5f939e78 VZ |
464 | wxTreeItemId rootId = AddRoot("Root", |
465 | TreeCtrlIcon_Folder, TreeCtrlIcon_Folder, | |
466 | new MyTreeItemData("Root item")); | |
457814b5 | 467 | |
5f939e78 | 468 | AddItemsRecursively(rootId, numChildren, depth, 0); |
4832f7c0 VZ |
469 | } |
470 | ||
471 | void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie) | |
472 | { | |
5f939e78 | 473 | wxTreeItemId id; |
4832f7c0 | 474 | |
5f939e78 VZ |
475 | if( cookie == -1 ) |
476 | id = GetFirstChild(idParent, cookie); | |
477 | else | |
478 | id = GetNextChild(idParent, cookie); | |
4832f7c0 | 479 | |
5f939e78 VZ |
480 | if(id <= 0) |
481 | return; | |
4832f7c0 | 482 | |
5f939e78 VZ |
483 | wxString text=GetItemText(id); |
484 | wxLogMessage(text); | |
4832f7c0 | 485 | |
5f939e78 VZ |
486 | if (ItemHasChildren(id)) |
487 | GetItemsRecursively(id,-1); | |
4832f7c0 | 488 | |
5f939e78 | 489 | GetItemsRecursively(idParent, cookie); |
457814b5 JS |
490 | } |
491 | ||
4832f7c0 | 492 | |
3a5a2f56 | 493 | // avoid repetition |
5f939e78 VZ |
494 | #define TREE_EVENT_HANDLER(name) \ |
495 | void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \ | |
496 | { \ | |
497 | wxLogMessage(#name); \ | |
498 | } | |
457814b5 | 499 | |
3a5a2f56 VZ |
500 | TREE_EVENT_HANDLER(OnBeginDrag) |
501 | TREE_EVENT_HANDLER(OnBeginRDrag) | |
502 | TREE_EVENT_HANDLER(OnBeginLabelEdit) | |
503 | TREE_EVENT_HANDLER(OnEndLabelEdit) | |
504 | TREE_EVENT_HANDLER(OnDeleteItem) | |
505 | TREE_EVENT_HANDLER(OnGetInfo) | |
506 | TREE_EVENT_HANDLER(OnSetInfo) | |
507 | TREE_EVENT_HANDLER(OnItemExpanded) | |
508 | TREE_EVENT_HANDLER(OnItemExpanding) | |
509 | TREE_EVENT_HANDLER(OnItemCollapsed) | |
510 | TREE_EVENT_HANDLER(OnSelChanged) | |
511 | TREE_EVENT_HANDLER(OnSelChanging) | |
512 | ||
513 | #undef TREE_EVENT_HANDLER | |
514 | ||
515 | void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event) | |
457814b5 | 516 | { |
5f939e78 | 517 | wxLogMessage("OnItemCollapsing"); |
f135ff73 | 518 | |
5f939e78 VZ |
519 | // for testing, prevent the user from collapsing the first child folder |
520 | wxTreeItemId itemId = event.GetItem(); | |
521 | if ( GetParent(itemId) == GetRootItem() && !GetPrevSibling(itemId) ) | |
522 | { | |
523 | wxMessageBox("You can't collapse this item."); | |
457814b5 | 524 | |
5f939e78 VZ |
525 | event.Veto(); |
526 | } | |
457814b5 JS |
527 | } |
528 | ||
6daa0637 | 529 | void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent&WXUNUSED(event)) |
435fe83e | 530 | { |
5f939e78 | 531 | wxLogMessage("OnTreeKeyDown"); |
435fe83e RR |
532 | } |
533 | ||
534 | void MyTreeCtrl::OnItemActivated(wxTreeEvent&WXUNUSED(event)) | |
457814b5 | 535 | { |
5f939e78 VZ |
536 | // show some info about this item |
537 | wxTreeItemId itemId = GetSelection(); | |
538 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId); | |
457814b5 | 539 | |
5f939e78 VZ |
540 | if ( item != NULL ) |
541 | { | |
542 | item->ShowInfo(this); | |
543 | } | |
457814b5 | 544 | |
5f939e78 VZ |
545 | wxLogMessage("OnItemActivated"); |
546 | } | |
547 | ||
548 | void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event) | |
549 | { | |
550 | wxTreeItemId id = HitTest(event.GetPosition()); | |
551 | if ( !id ) | |
552 | wxLogMessage("No item under mouse"); | |
553 | else | |
554 | { | |
555 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(id); | |
556 | if ( item ) | |
557 | wxLogMessage("Item '%s' under mouse", item->GetDesc()); | |
558 | } | |
457814b5 JS |
559 | } |
560 | ||
3a5a2f56 | 561 | static inline const char *Bool2String(bool b) |
457814b5 | 562 | { |
5f939e78 | 563 | return b ? "" : "not "; |
457814b5 JS |
564 | } |
565 | ||
9769e589 | 566 | void MyTreeItemData::ShowInfo(wxTreeCtrl *tree) |
457814b5 | 567 | { |
5f939e78 VZ |
568 | wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n" |
569 | "%u children (%u immediately under this item).", | |
570 | m_desc.c_str(), | |
571 | Bool2String(tree->IsSelected(GetId())), | |
572 | Bool2String(tree->IsExpanded(GetId())), | |
573 | Bool2String(tree->IsBold(GetId())), | |
574 | tree->GetChildrenCount(GetId()), | |
575 | tree->GetChildrenCount(GetId(), FALSE)); | |
457814b5 | 576 | } |