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