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