]> git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treectrl.cpp
don't treat changing focus to popup's child as loosing it
[wxWidgets.git] / samples / treectrl / treectrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: treectrl.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 // important: the #pragma argument must be different from treectrl.cpp,
13 // otherwise gcc gets confused (as there is also treectrl.cpp in the library
14 // which has identical #pragma) and the sample crashes on startup!
15 #ifdef __GNUG__
16 #pragma interface "treetest.cpp"
17 #pragma implementation "treetest.cpp"
18 #endif
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 #include "wx/log.h"
32
33 #include "wx/image.h"
34 #include "wx/imaglist.h"
35 #include "wx/treectrl.h"
36
37 #include "math.h"
38
39 #ifdef __WIN32__
40 // this is not supported by native control
41 #define NO_VARIABLE_HEIGHT
42 #endif
43
44 #include "treectrl.h"
45
46 // under Windows the icons are in the .rc file
47 #ifndef __WXMSW__
48 #include "icon1.xpm"
49 #include "icon2.xpm"
50 #include "icon3.xpm"
51 #include "icon4.xpm"
52 #include "icon5.xpm"
53 #include "mondrian.xpm"
54 #endif
55
56 // verify that the item is ok and insult the user if it is not
57 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
58 wxMessageBox(wxT("Please select some item first!"), \
59 wxT("Tree sample error"), \
60 wxOK | wxICON_EXCLAMATION, \
61 this); \
62 return; \
63 }
64
65 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
66 EVT_SIZE(MyFrame::OnSize)
67
68 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
69 MENU_LINK(Quit)
70 MENU_LINK(About)
71 MENU_LINK(TogButtons)
72 MENU_LINK(TogTwist)
73 MENU_LINK(TogLines)
74 MENU_LINK(TogEdit)
75 MENU_LINK(TogHideRoot)
76 MENU_LINK(TogRootLines)
77 MENU_LINK(TogBorder)
78 MENU_LINK(TogFullHighlight)
79 MENU_LINK(Dump)
80 #ifndef NO_MULTIPLE_SELECTION
81 MENU_LINK(DumpSelected)
82 MENU_LINK(Select)
83 MENU_LINK(Unselect)
84 MENU_LINK(ToggleSel)
85 #endif // NO_MULTIPLE_SELECTION
86 MENU_LINK(Rename)
87 MENU_LINK(Count)
88 MENU_LINK(CountRec)
89 MENU_LINK(Sort)
90 MENU_LINK(SortRev)
91 MENU_LINK(SetBold)
92 MENU_LINK(ClearBold)
93 MENU_LINK(Delete)
94 MENU_LINK(DeleteChildren)
95 MENU_LINK(DeleteAll)
96 MENU_LINK(Recreate)
97 MENU_LINK(ToggleImages)
98 MENU_LINK(ToggleButtons)
99 MENU_LINK(SetImageSize)
100 MENU_LINK(CollapseAndReset)
101 MENU_LINK(EnsureVisible)
102 MENU_LINK(AddItem)
103 MENU_LINK(InsertItem)
104 MENU_LINK(IncIndent)
105 MENU_LINK(DecIndent)
106 MENU_LINK(IncSpacing)
107 MENU_LINK(DecSpacing)
108 MENU_LINK(ToggleIcon)
109 #undef MENU_LINK
110
111 END_EVENT_TABLE()
112
113 #if USE_GENERIC_TREECTRL
114 BEGIN_EVENT_TABLE(MyTreeCtrl, wxGenericTreeCtrl)
115 #else
116 BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
117 #endif
118 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
119 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
120 EVT_TREE_END_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnEndDrag)
121 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit)
122 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit)
123 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem)
124 #if 0 // there are so many of those that logging them causes flicker
125 EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo)
126 #endif
127 EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo)
128 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded)
129 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding)
130 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed)
131 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing)
132 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRightClick)
133
134 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp)
135 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged)
136 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging)
137 EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown)
138 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated)
139 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick)
140 END_EVENT_TABLE()
141
142 IMPLEMENT_APP(MyApp)
143
144 bool MyApp::OnInit()
145 {
146 // Create the main frame window
147 MyFrame *frame = new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
148
149 // Show the frame
150 frame->Show(TRUE);
151 SetTopWindow(frame);
152
153 return TRUE;
154 }
155
156
157 // My frame constructor
158 MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
159 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)),
160 m_treeCtrl(NULL), m_textCtrl(NULL)
161 {
162 // This reduces flicker effects - even better would be to define
163 // OnEraseBackground to do nothing. When the tree control's scrollbars are
164 // show or hidden, the frame is sent a background erase event.
165 SetBackgroundColour(wxColour(255, 255, 255));
166
167 // Give it an icon
168 SetIcon(wxICON(mondrian));
169
170 #if wxUSE_MENUS
171 // Make a menubar
172 wxMenu *file_menu = new wxMenu,
173 *style_menu = new wxMenu,
174 *tree_menu = new wxMenu,
175 *item_menu = new wxMenu;
176
177 file_menu->Append(TreeTest_About, wxT("&About..."));
178 file_menu->AppendSeparator();
179 file_menu->Append(TreeTest_Quit, wxT("E&xit\tAlt-X"));
180
181 style_menu->Append(TreeTest_TogButtons, "Toggle &normal buttons");
182 style_menu->Append(TreeTest_TogTwist, "Toggle &twister buttons");
183 style_menu->Append(TreeTest_ToggleButtons, "Toggle image &buttons");
184 style_menu->AppendSeparator();
185 style_menu->Append(TreeTest_TogLines, "Toggle &connecting lines");
186 style_menu->Append(TreeTest_TogRootLines, "Toggle &lines at root");
187 style_menu->Append(TreeTest_TogHideRoot, "Toggle &hidden root");
188 style_menu->Append(TreeTest_TogBorder, "Toggle &item border");
189 style_menu->Append(TreeTest_TogFullHighlight, "Toggle &full row highlight");
190 style_menu->Append(TreeTest_TogEdit, "Toggle &edit mode");
191 #ifndef NO_MULTIPLE_SELECTION
192 style_menu->Append(TreeTest_ToggleSel, wxT("Toggle &selection mode"));
193 #endif // NO_MULTIPLE_SELECTION
194 style_menu->Append(TreeTest_ToggleImages, wxT("Toggle show ima&ges"));
195 style_menu->Append(TreeTest_SetImageSize, wxT("Set image si&ze..."));
196
197 tree_menu->Append(TreeTest_Recreate, "&Recreate the tree");
198 tree_menu->Append(TreeTest_CollapseAndReset, "C&ollapse and reset");
199 tree_menu->AppendSeparator();
200 tree_menu->Append(TreeTest_AddItem, "Append a &new item");
201 tree_menu->Append(TreeTest_InsertItem, "&Insert a new item");
202 tree_menu->Append(TreeTest_Delete, "&Delete this item");
203 tree_menu->Append(TreeTest_DeleteChildren, "Delete &children");
204 tree_menu->Append(TreeTest_DeleteAll, "Delete &all items");
205 tree_menu->AppendSeparator();
206 tree_menu->Append(TreeTest_Count, "Count children of current item");
207 tree_menu->Append(TreeTest_CountRec, "Recursively count children of current item");
208 tree_menu->AppendSeparator();
209 tree_menu->Append(TreeTest_Sort, "Sort children of current item");
210 tree_menu->Append(TreeTest_SortRev, "Sort in reversed order");
211 tree_menu->AppendSeparator();
212 tree_menu->Append(TreeTest_EnsureVisible, "Make the last item &visible");
213 tree_menu->AppendSeparator();
214 tree_menu->Append(TreeTest_IncIndent, "Add 5 points to indentation\tAlt-I");
215 tree_menu->Append(TreeTest_DecIndent, "Reduce indentation by 5 points\tAlt-R");
216 tree_menu->AppendSeparator();
217 tree_menu->Append(TreeTest_IncSpacing, "Add 5 points to spacing\tCtrl-I");
218 tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R");
219
220 item_menu->Append(TreeTest_Dump, "&Dump item children");
221 item_menu->Append(TreeTest_Rename, "&Rename item...");
222
223 item_menu->AppendSeparator();
224 item_menu->Append(TreeTest_SetBold, "Make item &bold");
225 item_menu->Append(TreeTest_ClearBold, "Make item &not bold");
226 item_menu->AppendSeparator();
227 item_menu->Append(TreeTest_ToggleIcon, "Toggle the item's &icon");
228
229 #ifndef NO_MULTIPLE_SELECTION
230 item_menu->AppendSeparator();
231 item_menu->Append(TreeTest_DumpSelected, "Dump selected items\tAlt-D");
232 item_menu->Append(TreeTest_Select, "Select current item\tAlt-S");
233 item_menu->Append(TreeTest_Unselect, "Unselect everything\tAlt-U");
234 #endif // NO_MULTIPLE_SELECTION
235
236 wxMenuBar *menu_bar = new wxMenuBar;
237 menu_bar->Append(file_menu, "&File");
238 menu_bar->Append(style_menu, "&Style");
239 menu_bar->Append(tree_menu, "&Tree");
240 menu_bar->Append(item_menu, "&Item");
241 SetMenuBar(menu_bar);
242
243 //menu_bar->Check(TreeTest_ToggleImages, TRUE);
244 #endif // wxUSE_MENUS
245
246 m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl,
247 wxDefaultPosition, wxDefaultSize,
248 wxTR_DEFAULT_STYLE | wxTR_EDIT_LABELS |
249 #ifndef NO_VARIABLE_HEIGHT
250 wxTR_HAS_VARIABLE_ROW_HEIGHT |
251 #endif
252 wxSUNKEN_BORDER);
253
254 // m_treeCtrl->SetBackgroundColour( *wxLIGHT_GREY );
255
256 m_textCtrl = new wxTextCtrl(this, -1, "",
257 wxDefaultPosition, wxDefaultSize,
258 wxTE_MULTILINE | wxSUNKEN_BORDER);
259
260 // create a status bar with 3 panes
261 CreateStatusBar(3);
262 SetStatusText("", 0);
263
264 #ifdef __WXMOTIF__
265 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
266 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
267 delete wxLog::SetActiveTarget(new wxLogStderr);
268 #else
269 // set our text control as the log target
270 wxLogTextCtrl *logWindow = new wxLogTextCtrl(m_textCtrl);
271 delete wxLog::SetActiveTarget(logWindow);
272 #endif
273 }
274
275 MyFrame::~MyFrame()
276 {
277 delete wxLog::SetActiveTarget(NULL);
278 }
279
280 void MyFrame::TogStyle(long flag)
281 {
282 m_treeCtrl->SetWindowStyle(m_treeCtrl->GetWindowStyle() ^ flag);
283 }
284
285 void MyFrame::OnSize(wxSizeEvent& event)
286 {
287 if ( m_treeCtrl && m_textCtrl )
288 {
289 Resize(GetClientSize());
290 }
291
292 event.Skip();
293 }
294
295 void MyFrame::Resize(const wxSize& size)
296 {
297 m_treeCtrl->SetSize(0, 0, size.x, 2*size.y/3);
298 m_textCtrl->SetSize(0, 2*size.y/3, size.x, size.y/3);
299 }
300
301 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
302 {
303 Close(TRUE);
304 }
305
306 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
307 {
308 wxMessageBox("Tree test sample\n"
309 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
310 "About tree test",
311 wxOK | wxICON_INFORMATION, this);
312 }
313
314 void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
315 {
316 wxTreeItemId item = m_treeCtrl->GetSelection();
317
318 CHECK_ITEM( item );
319
320 // old code - now we edit in place
321 #if 0
322 static wxString s_text;
323 s_text = wxGetTextFromUser("New name: ", "Tree sample question",
324 s_text, this);
325 if ( !s_text.IsEmpty() )
326 {
327 m_treeCtrl->SetItemText(item, s_text);
328 }
329 #endif // 0
330
331 // TODO demonstrate creating a custom edit control...
332 (void)m_treeCtrl->EditLabel(item);
333 }
334
335 void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event))
336 {
337 wxTreeItemId item = m_treeCtrl->GetSelection();
338
339 CHECK_ITEM( item );
340
341 int i = m_treeCtrl->GetChildrenCount( item, FALSE );
342
343 wxLogMessage(wxT("%d children"), i);
344 }
345
346 void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event))
347 {
348 wxTreeItemId item = m_treeCtrl->GetSelection();
349
350 CHECK_ITEM( item );
351
352 int i = m_treeCtrl->GetChildrenCount( item );
353
354 wxLogMessage(wxT("%d children"), i);
355 }
356
357 void MyFrame::DoSort(bool reverse)
358 {
359 wxTreeItemId item = m_treeCtrl->GetSelection();
360
361 CHECK_ITEM( item );
362
363 m_treeCtrl->DoSortChildren(item, reverse);
364 }
365
366 void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
367 {
368 wxTreeItemId root = m_treeCtrl->GetSelection();
369
370 CHECK_ITEM( root );
371
372 m_treeCtrl->GetItemsRecursively(root, -1);
373 }
374
375 #ifndef NO_MULTIPLE_SELECTION
376
377 void MyFrame::OnToggleSel(wxCommandEvent& WXUNUSED(event))
378 {
379 TogStyle(wxTR_MULTIPLE);
380 #if 0
381 long style = m_treeCtrl->GetWindowStyle();
382 if ( style & wxTR_MULTIPLE )
383 style &= ~wxTR_MULTIPLE;
384 else
385 style |= wxTR_MULTIPLE;
386
387 delete m_treeCtrl;
388
389 m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl,
390 wxDefaultPosition, wxDefaultSize,
391 style);
392 Resize(GetClientSize());
393 #endif
394 }
395
396 void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event))
397 {
398 wxArrayTreeItemIds array;
399
400 size_t count = m_treeCtrl->GetSelections(array);
401 wxLogMessage(wxT("%u items selected"), count);
402
403 for ( size_t n = 0; n < count; n++ )
404 {
405 wxLogMessage(wxT("\t%s"), m_treeCtrl->GetItemText(array.Item(n)).c_str());
406 }
407 }
408
409 void MyFrame::OnSelect(wxCommandEvent& event)
410 {
411 m_treeCtrl->SelectItem(m_treeCtrl->GetSelection());
412 }
413
414 void MyFrame::OnUnselect(wxCommandEvent& event)
415 {
416 m_treeCtrl->UnselectAll();
417 }
418
419 #endif // NO_MULTIPLE_SELECTION
420
421 void MyFrame::DoSetBold(bool bold)
422 {
423 wxTreeItemId item = m_treeCtrl->GetSelection();
424
425 CHECK_ITEM( item );
426
427 m_treeCtrl->SetItemBold(item, bold);
428 }
429
430 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
431 {
432 wxTreeItemId item = m_treeCtrl->GetSelection();
433
434 CHECK_ITEM( item );
435
436 m_treeCtrl->Delete(item);
437 }
438
439 void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event))
440 {
441 wxTreeItemId item = m_treeCtrl->GetSelection();
442
443 CHECK_ITEM( item );
444
445 m_treeCtrl->DeleteChildren(item);
446 }
447
448 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
449 {
450 m_treeCtrl->DeleteAllItems();
451 }
452
453 void MyFrame::OnRecreate(wxCommandEvent& event)
454 {
455 OnDeleteAll(event);
456 m_treeCtrl->AddTestItemsToTree(5, 2);
457 }
458
459 void MyFrame::OnSetImageSize(wxCommandEvent& event)
460 {
461 int size = wxGetNumberFromUser("Enter the size for the images to use",
462 "Size: ",
463 "TreeCtrl sample",
464 m_treeCtrl->ImageSize());
465 if ( size == -1 )
466 return;
467
468 m_treeCtrl->CreateImageList(size);
469 wxGetApp().SetShowImages(TRUE);
470 }
471
472 void MyFrame::OnToggleImages(wxCommandEvent& event)
473 {
474 if ( wxGetApp().ShowImages() )
475 {
476 m_treeCtrl->CreateImageList(-1);
477 wxGetApp().SetShowImages(FALSE);
478 }
479 else
480 {
481 m_treeCtrl->CreateImageList(0);
482 wxGetApp().SetShowImages(TRUE);
483 }
484 }
485
486 void MyFrame::OnToggleButtons(wxCommandEvent& event)
487 {
488 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
489 if ( wxGetApp().ShowButtons() )
490 {
491 m_treeCtrl->CreateButtonsImageList(-1);
492 wxGetApp().SetShowButtons(FALSE);
493 }
494 else
495 {
496 m_treeCtrl->CreateButtonsImageList(15);
497 wxGetApp().SetShowButtons(TRUE);
498 }
499 #endif
500 }
501
502 void MyFrame::OnCollapseAndReset(wxCommandEvent& event)
503 {
504 m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem());
505 }
506
507 void MyFrame::OnEnsureVisible(wxCommandEvent& event)
508 {
509 m_treeCtrl->DoEnsureVisible();
510 }
511
512 void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event))
513 {
514 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File : -1;
515 m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), image, "2nd item");
516 }
517
518 void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event))
519 {
520 static int s_num = 0;
521
522 wxString text;
523 text.Printf(wxT("Item #%d"), ++s_num);
524
525 m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(),
526 text /*,
527 MyTreeCtrl::TreeCtrlIcon_File */ );
528 }
529
530 void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event))
531 {
532 unsigned int indent = m_treeCtrl->GetIndent();
533 if (indent < 100)
534 m_treeCtrl->SetIndent( indent+5 );
535 }
536
537 void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event))
538 {
539 unsigned int indent = m_treeCtrl->GetIndent();
540 if (indent > 10)
541 m_treeCtrl->SetIndent( indent-5 );
542 }
543
544 void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event))
545 {
546 unsigned int indent = m_treeCtrl->GetSpacing();
547 if (indent < 100)
548 m_treeCtrl->SetSpacing( indent+5 );
549 }
550
551 void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event))
552 {
553 unsigned int indent = m_treeCtrl->GetSpacing();
554 if (indent > 10)
555 m_treeCtrl->SetSpacing( indent-5 );
556 }
557
558 void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event))
559 {
560 wxTreeItemId item = m_treeCtrl->GetSelection();
561
562 CHECK_ITEM( item );
563
564 m_treeCtrl->DoToggleIcon(item);
565 }
566
567 // MyTreeCtrl implementation
568 #if USE_GENERIC_TREECTRL
569 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxGenericTreeCtrl)
570 #else
571 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
572 #endif
573
574 MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
575 const wxPoint& pos, const wxSize& size,
576 long style)
577 : wxTreeCtrl(parent, id, pos, size, style)
578 {
579 m_reverseSort = FALSE;
580
581 CreateImageList();
582
583 // Add some items to the tree
584 AddTestItemsToTree(5, 2);
585 }
586
587 void MyTreeCtrl::CreateImageList(int size)
588 {
589 if ( size == -1 )
590 {
591 SetImageList(NULL);
592 return;
593 }
594 if ( size == 0 )
595 size = m_imageSize;
596 else
597 m_imageSize = size;
598
599 // Make an image list containing small icons
600 wxImageList *images = new wxImageList(size, size, TRUE);
601
602 // should correspond to TreeCtrlIcon_xxx enum
603 #if defined(__WXMSW__) && defined(__WIN16__)
604 images->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE));
605 images->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE));
606 images->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE));
607 images->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE));
608 images->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE));
609 #else // !MSW
610 wxBusyCursor wait;
611 wxIcon icons[5];
612 icons[0] = wxICON(icon1);
613 icons[1] = wxICON(icon2);
614 icons[2] = wxICON(icon3);
615 icons[3] = wxICON(icon4);
616 icons[4] = wxICON(icon5);
617
618 int sizeOrig = icons[0].GetWidth();
619 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
620 {
621 if ( size == sizeOrig )
622 {
623 images->Add(icons[i]);
624 }
625 else
626 {
627 images->Add(wxImage(icons[i]).Rescale(size, size).
628 ConvertToBitmap());
629 }
630 }
631 #endif // MSW/!MSW
632
633 AssignImageList(images);
634 }
635
636 void MyTreeCtrl::CreateButtonsImageList(int size)
637 {
638 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
639 if ( size == -1 )
640 {
641 SetButtonsImageList(NULL);
642 return;
643 }
644
645 // Make an image list containing small icons
646 wxImageList *images = new wxImageList(size, size, TRUE);
647
648 // should correspond to TreeCtrlIcon_xxx enum
649 #if defined(__WXMSW__) && defined(__WIN16__)
650 images->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE));
651 images->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE));
652 images->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE));
653 images->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE));
654 images->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE));
655 #else // !MSW
656 wxBusyCursor wait;
657 wxIcon icons[4];
658 icons[0] = wxICON(icon3); // closed
659 icons[1] = wxICON(icon3); // closed, selected
660 icons[2] = wxICON(icon5); // open
661 icons[3] = wxICON(icon5); // open, selected
662
663 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
664 {
665 int sizeOrig = icons[i].GetWidth();
666 if ( size == sizeOrig )
667 {
668 images->Add(icons[i]);
669 }
670 else
671 {
672 images->Add(wxImage(icons[i]).Rescale(size, size).
673 ConvertToBitmap());
674 }
675 }
676 #endif // MSW/!MSW
677
678 AssignButtonsImageList(images);
679 #endif
680 }
681
682 MyTreeCtrl::~MyTreeCtrl()
683 {
684 }
685
686 int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
687 const wxTreeItemId& item2)
688 {
689 if ( m_reverseSort )
690 {
691 // just exchange 1st and 2nd items
692 return wxTreeCtrl::OnCompareItems(item2, item1);
693 }
694 else
695 {
696 return wxTreeCtrl::OnCompareItems(item1, item2);
697 }
698 }
699
700 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
701 size_t numChildren,
702 size_t depth,
703 size_t folder)
704 {
705 if ( depth > 0 )
706 {
707 bool hasChildren = depth > 1;
708
709 wxString str;
710 for ( size_t n = 0; n < numChildren; n++ )
711 {
712 // at depth 1 elements won't have any more children
713 if ( hasChildren )
714 str.Printf(wxT("%s child %d"), wxT("Folder"), n + 1);
715 else
716 str.Printf(wxT("%s child %d.%d"), wxT("File"), folder, n + 1);
717
718 // here we pass to AppendItem() normal and selected item images (we
719 // suppose that selected image follows the normal one in the enum)
720 int image, imageSel;
721 if ( wxGetApp().ShowImages() )
722 {
723 image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
724 imageSel = image + 1;
725 }
726 else
727 {
728 image = imageSel = -1;
729 }
730 wxTreeItemId id = AppendItem(idParent, str, image, imageSel,
731 new MyTreeItemData(str));
732
733 // and now we also set the expanded one (only for the folders)
734 if ( hasChildren && wxGetApp().ShowImages() )
735 {
736 SetItemImage(id, TreeCtrlIcon_FolderOpened,
737 wxTreeItemIcon_Expanded);
738 }
739
740 // remember the last child for OnEnsureVisible()
741 if ( !hasChildren && n == numChildren - 1 )
742 {
743 m_lastItem = id;
744 }
745
746 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
747 }
748 }
749 //else: done!
750 }
751
752 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
753 size_t depth)
754 {
755 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder : -1;
756 wxTreeItemId rootId = AddRoot("Root",
757 image, image,
758 new MyTreeItemData("Root item"));
759 if ( image != -1 )
760 {
761 SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded);
762 }
763
764 AddItemsRecursively(rootId, numChildren, depth, 0);
765
766 // set some colours/fonts for testing
767 SetItemFont(rootId, *wxITALIC_FONT);
768
769 long cookie;
770 wxTreeItemId id = GetFirstChild(rootId, cookie);
771 SetItemTextColour(id, *wxBLUE);
772
773 id = GetNextChild(rootId, cookie);
774 id = GetNextChild(rootId, cookie);
775 SetItemTextColour(id, *wxRED);
776 SetItemBackgroundColour(id, *wxLIGHT_GREY);
777 }
778
779 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie)
780 {
781 wxTreeItemId id;
782
783 if( cookie == -1 )
784 id = GetFirstChild(idParent, cookie);
785 else
786 id = GetNextChild(idParent, cookie);
787
788 if(id <= 0)
789 return;
790
791 wxString text = GetItemText(id);
792 wxLogMessage(text);
793
794 if (ItemHasChildren(id))
795 GetItemsRecursively(id,-1);
796
797 GetItemsRecursively(idParent, cookie);
798 }
799
800 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item)
801 {
802 int image = GetItemImage(item) == TreeCtrlIcon_Folder ? TreeCtrlIcon_File
803 : TreeCtrlIcon_Folder;
804
805 SetItemImage(item, image);
806 }
807
808
809 // avoid repetition
810 #define TREE_EVENT_HANDLER(name) \
811 void MyTreeCtrl::name(wxTreeEvent& event) \
812 { \
813 wxLogMessage(wxT(#name)); \
814 event.Skip(); \
815 }
816
817 TREE_EVENT_HANDLER(OnBeginRDrag)
818 TREE_EVENT_HANDLER(OnDeleteItem)
819 TREE_EVENT_HANDLER(OnGetInfo)
820 TREE_EVENT_HANDLER(OnSetInfo)
821 TREE_EVENT_HANDLER(OnItemExpanded)
822 TREE_EVENT_HANDLER(OnItemExpanding)
823 TREE_EVENT_HANDLER(OnItemCollapsed)
824 TREE_EVENT_HANDLER(OnSelChanged)
825 TREE_EVENT_HANDLER(OnSelChanging)
826 TREE_EVENT_HANDLER(OnTreeKeyDown)
827
828 #undef TREE_EVENT_HANDLER
829
830 void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
831 {
832 // need to explicitly allow drag
833 if ( event.GetItem() != GetRootItem() )
834 {
835 m_draggedItem = event.GetItem();
836
837 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
838 GetItemText(m_draggedItem).c_str());
839
840 event.Allow();
841 }
842 else
843 {
844 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
845 }
846 }
847
848 void MyTreeCtrl::OnEndDrag(wxTreeEvent& event)
849 {
850 wxTreeItemId itemSrc = m_draggedItem,
851 itemDst = event.GetItem();
852 m_draggedItem = (wxTreeItemId)0l;
853
854 // where to copy the item?
855 if ( itemDst.IsOk() && !ItemHasChildren(itemDst) )
856 {
857 // copy to the parent then
858 itemDst = GetParent(itemDst);
859 }
860
861 if ( !itemDst.IsOk() )
862 {
863 wxLogMessage(wxT("OnEndDrag: can't drop here."));
864
865 return;
866 }
867
868 wxString text = GetItemText(itemSrc);
869 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
870 text.c_str(), GetItemText(itemDst).c_str());
871
872 // just do append here - we could also insert it just before/after the item
873 // on which it was dropped, but this requires slightly more work... we also
874 // completely ignore the client data and icon of the old item but could
875 // copy them as well.
876 //
877 // Finally, we only copy one item here but we might copy the entire tree if
878 // we were dragging a folder.
879 int image = wxGetApp().ShowImages() ? TreeCtrlIcon_File : -1;
880 AppendItem(itemDst, text, image);
881 }
882
883 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
884 {
885 wxLogMessage(wxT("OnBeginLabelEdit"));
886
887 // for testing, prevent this item's label editing
888 wxTreeItemId itemId = event.GetItem();
889 if ( IsTestItem(itemId) )
890 {
891 wxMessageBox(wxT("You can't edit this item."));
892
893 event.Veto();
894 }
895 }
896
897 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
898 {
899 wxLogMessage(wxT("OnEndLabelEdit"));
900
901 // don't allow anything except letters in the labels
902 if ( !event.GetLabel().IsWord() )
903 {
904 wxMessageBox(wxT("The label should contain only letters."));
905
906 event.Veto();
907 }
908 }
909
910 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
911 {
912 wxLogMessage(wxT("OnItemCollapsing"));
913
914 // for testing, prevent the user from collapsing the first child folder
915 wxTreeItemId itemId = event.GetItem();
916 if ( IsTestItem(itemId) )
917 {
918 wxMessageBox(wxT("You can't collapse this item."));
919
920 event.Veto();
921 }
922 }
923
924 void MyTreeCtrl::OnItemActivated(wxTreeEvent& event)
925 {
926 // show some info about this item
927 wxTreeItemId itemId = event.GetItem();
928 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
929
930 if ( item != NULL )
931 {
932 item->ShowInfo(this);
933 }
934
935 wxLogMessage(wxT("OnItemActivated"));
936 }
937
938 void MyTreeCtrl::OnItemRightClick(wxTreeEvent& event)
939 {
940 ShowMenu(event.GetItem(), event.GetPoint());
941 }
942
943 void MyTreeCtrl::OnRMouseUp(wxMouseEvent& event)
944 {
945 wxPoint pt = event.GetPosition();
946 ShowMenu(HitTest(pt), pt);
947 }
948
949 void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt)
950 {
951 wxString title;
952 if ( id.IsOk() )
953 {
954 title << _T("Menu for ") << GetItemText(id);
955 }
956 else
957 {
958 title = _T("Menu for no particular item");
959 }
960
961 #if wxUSE_MENUS
962 wxMenu menu(title);
963 menu.Append(TreeTest_About, _T("&About..."));
964 menu.Append(TreeTest_Dump, _T("&Dump"));
965
966 PopupMenu(&menu, pt);
967 #endif // wxUSE_MENUS
968 }
969
970 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
971 {
972 wxTreeItemId id = HitTest(event.GetPosition());
973 if ( !id )
974 wxLogMessage(wxT("No item under mouse"));
975 else
976 {
977 MyTreeItemData *item = (MyTreeItemData *)GetItemData(id);
978 if ( item )
979 wxLogMessage(wxT("Item '%s' under mouse"), item->GetDesc());
980 }
981 }
982
983 static inline const wxChar *Bool2String(bool b)
984 {
985 return b ? wxT("") : wxT("not ");
986 }
987
988 void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
989 {
990 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
991 wxT("%u children (%u immediately under this item)."),
992 m_desc.c_str(),
993 Bool2String(tree->IsSelected(GetId())),
994 Bool2String(tree->IsExpanded(GetId())),
995 Bool2String(tree->IsBold(GetId())),
996 tree->GetChildrenCount(GetId()),
997 tree->GetChildrenCount(GetId(), FALSE));
998 }