]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectrl.cpp
1. added wxTreeCtrl::DeleteChildren()
[wxWidgets.git] / src / generic / treectrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: treectrl.cpp
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
7 // Id: $Id$
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // =============================================================================
13 // declarations
14 // =============================================================================
15
16 // -----------------------------------------------------------------------------
17 // headers
18 // -----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "treectrl.h"
22 #endif
23
24 #include "wx/treectrl.h"
25 #include "wx/settings.h"
26 #include "wx/log.h"
27 #include "wx/intl.h"
28 #include "wx/dynarray.h"
29 #include "wx/dcclient.h"
30 #include "wx/imaglist.h"
31
32 // -----------------------------------------------------------------------------
33 // array types
34 // -----------------------------------------------------------------------------
35
36 WX_DEFINE_ARRAY(wxGenericTreeItem *, wxArrayTreeItems);
37
38 // -----------------------------------------------------------------------------
39 // private classes
40 // -----------------------------------------------------------------------------
41
42 // a tree item
43 class WXDLLEXPORT wxGenericTreeItem
44 {
45 public:
46 // ctors & dtor
47 wxGenericTreeItem() { m_data = NULL; }
48 wxGenericTreeItem( wxGenericTreeItem *parent,
49 const wxString& text,
50 wxDC& dc,
51 int image, int selImage,
52 wxTreeItemData *data );
53
54 ~wxGenericTreeItem();
55
56 // trivial accessors
57 wxArrayTreeItems& GetChildren() { return m_children; }
58
59 const wxString& GetText() const { return m_text; }
60 int GetImage() const { return m_image; }
61 int GetSelectedImage() const { return m_selImage; }
62 wxTreeItemData *GetData() const { return m_data; }
63
64 void SetText( const wxString &text, wxDC& dc );
65 void SetImage(int image) { m_image = image; }
66 void SetSelectedImage(int image) { m_selImage = image; }
67 void SetData(wxTreeItemData *data) { m_data = data; }
68
69 void SetHasPlus(bool has = TRUE) { m_hasPlus = has; }
70
71 void SetBold(bool bold) { m_isBold = bold; }
72
73 int GetX() const { return m_x; }
74 int GetY() const { return m_y; }
75
76 void SetHeight(int h) { m_height = h; }
77
78 void SetX(int x) { m_x = x; }
79 void SetY(int y) { m_y = y; }
80
81 wxGenericTreeItem *GetParent() const { return m_parent; }
82
83 // operations
84 void DeleteChildren();
85 void Reset();
86
87 // get count of all children (and grand children if 'recursively')
88 size_t GetChildrenCount(bool recursively = TRUE) const;
89
90 void Insert(wxGenericTreeItem *child, size_t index)
91 { m_children.Insert(child, index); }
92
93 void SetCross( int x, int y );
94 void GetSize( int &x, int &y );
95
96 // return the item at given position (or NULL if no item), onButton is TRUE
97 // if the point belongs to the item's button, otherwise it lies on the
98 // button's label
99 wxGenericTreeItem *HitTest( const wxPoint& point, bool &onButton );
100
101 void Expand() { m_isCollapsed = FALSE; }
102 void Collapse() { m_isCollapsed = TRUE; }
103
104 void SetHilight( bool set = TRUE ) { m_hasHilight = set; }
105
106 // status inquiries
107 bool HasChildren() const { return !m_children.IsEmpty(); }
108 bool HasHilight() const { return m_hasHilight; }
109 bool IsExpanded() const { return !m_isCollapsed; }
110 bool HasPlus() const { return m_hasPlus || HasChildren(); }
111 bool IsBold() const { return m_isBold; }
112
113 private:
114 wxString m_text;
115
116 int m_image,
117 m_selImage;
118
119 wxTreeItemData *m_data;
120
121 // use bitfields to save size
122 int m_isCollapsed :1;
123 int m_hasHilight :1; // same as focused
124 int m_hasPlus :1; // used for item which doesn't have
125 // children but still has a [+] button
126 int m_isBold :1; // render the label in bold font
127
128 int m_x, m_y;
129 long m_height, m_width;
130 int m_xCross, m_yCross;
131 int m_level;
132 wxArrayTreeItems m_children;
133 wxGenericTreeItem *m_parent;
134 };
135
136 // =============================================================================
137 // implementation
138 // =============================================================================
139
140 // -----------------------------------------------------------------------------
141 // wxTreeEvent
142 // -----------------------------------------------------------------------------
143
144 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
145
146 wxTreeEvent::wxTreeEvent( wxEventType commandType, int id )
147 : wxNotifyEvent( commandType, id )
148 {
149 m_code = 0;
150 m_itemOld = (wxGenericTreeItem *)NULL;
151 }
152
153 // -----------------------------------------------------------------------------
154 // wxGenericTreeItem
155 // -----------------------------------------------------------------------------
156
157 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
158 const wxString& text,
159 wxDC& dc,
160 int image, int selImage,
161 wxTreeItemData *data)
162 : m_text(text)
163 {
164 m_image = image;
165 m_selImage = selImage;
166 m_data = data;
167 m_x = m_y = 0;
168 m_xCross = m_yCross = 0;
169
170 m_level = 0;
171
172 m_isCollapsed = TRUE;
173 m_hasHilight = FALSE;
174 m_hasPlus = FALSE;
175 m_isBold = FALSE;
176
177 m_parent = parent;
178
179 dc.GetTextExtent( m_text, &m_width, &m_height );
180 }
181
182 wxGenericTreeItem::~wxGenericTreeItem()
183 {
184 delete m_data;
185
186 DeleteChildren();
187 }
188
189 void wxGenericTreeItem::DeleteChildren()
190 {
191 size_t count = m_children.Count();
192 for ( size_t n = 0; n < count; n++ )
193 delete m_children[n];
194
195 m_children.Empty();
196 }
197
198 void wxGenericTreeItem::SetText( const wxString &text, wxDC& dc )
199 {
200 m_text = text;
201
202 dc.GetTextExtent( m_text, &m_width, &m_height );
203 }
204
205 void wxGenericTreeItem::Reset()
206 {
207 m_text.Empty();
208 m_image =
209 m_selImage = -1;
210 m_data = NULL;
211 m_x = m_y =
212 m_height = m_width = 0;
213 m_xCross =
214 m_yCross = 0;
215
216 m_level = 0;
217
218 DeleteChildren();
219 m_isCollapsed = TRUE;
220
221 m_parent = (wxGenericTreeItem *)NULL;
222 }
223
224 size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
225 {
226 size_t count = m_children.Count();
227 if ( !recursively )
228 return count;
229
230 size_t total = count;
231 for ( size_t n = 0; n < count; n++ )
232 {
233 total += m_children[n]->GetChildrenCount();
234 }
235
236 return total;
237 }
238
239 void wxGenericTreeItem::SetCross( int x, int y )
240 {
241 m_xCross = x;
242 m_yCross = y;
243 }
244
245 void wxGenericTreeItem::GetSize( int &x, int &y )
246 {
247 if ( y < m_y ) y = m_y;
248 int width = m_x + m_width;
249 if (width > x) x = width;
250
251 if (IsExpanded())
252 {
253 size_t count = m_children.Count();
254 for ( size_t n = 0; n < count; n++ )
255 {
256 m_children[n]->GetSize( x, y );
257 }
258 }
259 }
260
261 wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point,
262 bool &onButton )
263 {
264 if ((point.y > m_y) && (point.y < m_y + m_height))
265 {
266 // FIXME why +5?
267 if ((point.x > m_xCross-5) && (point.x < m_xCross+5) &&
268 (point.y > m_yCross-5) && (point.y < m_yCross+5) &&
269 (IsExpanded() || HasPlus()))
270 {
271 onButton = TRUE;
272 return this;
273 }
274
275 if ((point.x > m_x) && (point.x < m_x+m_width))
276 {
277 onButton = FALSE;
278 return this;
279 }
280 }
281 else
282 {
283 if (!m_isCollapsed)
284 {
285 size_t count = m_children.Count();
286 for ( size_t n = 0; n < count; n++ )
287 {
288 wxGenericTreeItem *res = m_children[n]->HitTest( point, onButton );
289 if ( res != NULL )
290 return res;
291 }
292 }
293 }
294
295 return NULL;
296 }
297
298 // -----------------------------------------------------------------------------
299 // wxTreeCtrl implementation
300 // -----------------------------------------------------------------------------
301
302 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxScrolledWindow)
303
304 BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow)
305 EVT_PAINT (wxTreeCtrl::OnPaint)
306 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse)
307 EVT_CHAR (wxTreeCtrl::OnChar)
308 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus)
309 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus)
310 EVT_IDLE (wxTreeCtrl::OnIdle)
311 END_EVENT_TABLE()
312
313 // -----------------------------------------------------------------------------
314 // construction/destruction
315 // -----------------------------------------------------------------------------
316 void wxTreeCtrl::Init()
317 {
318 m_current =
319 m_anchor = (wxGenericTreeItem *) NULL;
320 m_hasFocus = FALSE;
321 m_dirty = FALSE;
322
323 m_xScroll = 0;
324 m_yScroll = 0;
325 m_lineHeight = 10;
326 m_indent = 15;
327
328 m_hilightBrush = new wxBrush
329 (
330 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT),
331 wxSOLID
332 );
333
334 m_imageListNormal =
335 m_imageListState = (wxImageList *) NULL;
336 }
337
338 bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
339 const wxPoint& pos, const wxSize& size,
340 long style,
341 const wxValidator &validator,
342 const wxString& name )
343 {
344 Init();
345
346 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
347
348 SetValidator( validator );
349
350 SetBackgroundColour( *wxWHITE );
351 m_dottedPen = wxPen( *wxBLACK, 0, 0 );
352
353 return TRUE;
354 }
355
356 wxTreeCtrl::~wxTreeCtrl()
357 {
358 wxDELETE( m_hilightBrush );
359 wxDELETE( m_anchor );
360 }
361
362 // -----------------------------------------------------------------------------
363 // accessors
364 // -----------------------------------------------------------------------------
365
366 size_t wxTreeCtrl::GetCount() const
367 {
368 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
369 }
370
371 void wxTreeCtrl::SetIndent(unsigned int indent)
372 {
373 m_indent = indent;
374 Refresh();
375 }
376
377 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
378 {
379 wxCHECK_MSG( item.IsOk(), 0u, "invalid tree item" );
380
381 return item.m_pItem->GetChildrenCount(recursively);
382 }
383
384 // -----------------------------------------------------------------------------
385 // functions to work with tree items
386 // -----------------------------------------------------------------------------
387
388 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
389 {
390 wxCHECK_MSG( item.IsOk(), "", "invalid tree item" );
391
392 return item.m_pItem->GetText();
393 }
394
395 int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const
396 {
397 wxCHECK_MSG( item.IsOk(), -1, "invalid tree item" );
398
399 return item.m_pItem->GetImage();
400 }
401
402 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
403 {
404 wxCHECK_MSG( item.IsOk(), -1, "invalid tree item" );
405
406 return item.m_pItem->GetSelectedImage();
407 }
408
409 wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
410 {
411 wxCHECK_MSG( item.IsOk(), NULL, "invalid tree item" );
412
413 return item.m_pItem->GetData();
414 }
415
416 void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
417 {
418 wxCHECK_RET( item.IsOk(), "invalid tree item" );
419
420 wxClientDC dc(this);
421 item.m_pItem->SetText(text, dc);
422 }
423
424 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image)
425 {
426 wxCHECK_RET( item.IsOk(), "invalid tree item" );
427
428 item.m_pItem->SetImage(image);
429 }
430
431 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
432 {
433 wxCHECK_RET( item.IsOk(), "invalid tree item" );
434
435 item.m_pItem->SetSelectedImage(image);
436 }
437
438 void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
439 {
440 wxCHECK_RET( item.IsOk(), "invalid tree item" );
441
442 item.m_pItem->SetData(data);
443 }
444
445 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
446 {
447 wxCHECK_RET( item.IsOk(), "invalid tree item" );
448
449 item.m_pItem->SetHasPlus(has);
450 }
451
452 void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
453 {
454 wxCHECK_RET( item.IsOk(), "invalid tree item" );
455
456 // avoid redrawing the tree if no real change
457 wxGenericTreeItem *pItem = item.m_pItem;
458 if ( pItem->IsBold() != bold )
459 {
460 pItem->SetBold(bold);
461 RefreshLine(pItem);
462 }
463 }
464
465 // -----------------------------------------------------------------------------
466 // item status inquiries
467 // -----------------------------------------------------------------------------
468
469 bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const
470 {
471 wxFAIL_MSG("not implemented");
472
473 return TRUE;
474 }
475
476 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
477 {
478 wxCHECK_MSG( item.IsOk(), FALSE, "invalid tree item" );
479
480 return !item.m_pItem->GetChildren().IsEmpty();
481 }
482
483 bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
484 {
485 wxCHECK_MSG( item.IsOk(), FALSE, "invalid tree item" );
486
487 return item.m_pItem->IsExpanded();
488 }
489
490 bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
491 {
492 wxCHECK_MSG( item.IsOk(), FALSE, "invalid tree item" );
493
494 return item.m_pItem->HasHilight();
495 }
496
497 bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
498 {
499 wxCHECK_MSG( item.IsOk(), FALSE, "invalid tree item" );
500
501 return item.m_pItem->IsBold();
502 }
503
504 // -----------------------------------------------------------------------------
505 // navigation
506 // -----------------------------------------------------------------------------
507
508 wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
509 {
510 wxCHECK_MSG( item.IsOk(), NULL, "invalid tree item" );
511
512 return item.m_pItem->GetParent();
513 }
514
515 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
516 {
517 wxCHECK_MSG( item.IsOk(), NULL, "invalid tree item" );
518
519 cookie = 0;
520 return GetNextChild(item, cookie);
521 }
522
523 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
524 {
525 wxCHECK_MSG( item.IsOk(), NULL, "invalid tree item" );
526
527 wxArrayTreeItems& children = item.m_pItem->GetChildren();
528 if ( (size_t)cookie < children.Count() )
529 {
530 return item.m_pItem->GetChildren().Item(cookie++);
531 }
532 else
533 {
534 // there are no more of them
535 return NULL;
536 }
537 }
538
539 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
540 {
541 wxCHECK_MSG( item.IsOk(), NULL, "invalid tree item" );
542
543 wxGenericTreeItem *i = item.m_pItem;
544 wxGenericTreeItem *parent = i->GetParent();
545 if ( parent == NULL )
546 {
547 // root item doesn't have any siblings
548 return NULL;
549 }
550
551 wxArrayTreeItems& siblings = parent->GetChildren();
552 int index = siblings.Index(i);
553 wxASSERT( index != NOT_FOUND ); // I'm not a child of my parent?
554
555 size_t n = (size_t)(index + 1);
556 return n == siblings.Count() ? (wxGenericTreeItem*)NULL : siblings[n];
557 }
558
559 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
560 {
561 wxCHECK_MSG( item.IsOk(), NULL, "invalid tree item" );
562
563 wxGenericTreeItem *i = item.m_pItem;
564 wxGenericTreeItem *parent = i->GetParent();
565 if ( parent == NULL )
566 {
567 // root item doesn't have any siblings
568 return NULL;
569 }
570
571 wxArrayTreeItems& siblings = parent->GetChildren();
572 int index = siblings.Index(i);
573 wxASSERT( index != NOT_FOUND ); // I'm not a child of my parent?
574
575 return index == 0 ? (wxGenericTreeItem*)NULL : siblings[(size_t)(index - 1)];
576 }
577
578 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
579 {
580 wxFAIL_MSG("not implemented");
581
582 return NULL;
583 }
584
585 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
586 {
587 wxCHECK_MSG( item.IsOk(), NULL, "invalid tree item" );
588
589 wxFAIL_MSG("not implemented");
590
591 return NULL;
592 }
593
594 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
595 {
596 wxCHECK_MSG( item.IsOk(), NULL, "invalid tree item" );
597
598 wxFAIL_MSG("not implemented");
599
600 return NULL;
601 }
602
603 // -----------------------------------------------------------------------------
604 // operations
605 // -----------------------------------------------------------------------------
606
607 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
608 size_t previous,
609 const wxString& text,
610 int image, int selImage,
611 wxTreeItemData *data)
612 {
613 wxGenericTreeItem *parent = parentId.m_pItem;
614 if ( !parent )
615 {
616 // should we give a warning here?
617 return AddRoot(text, image, selImage, data);
618 }
619
620 wxClientDC dc(this);
621 wxGenericTreeItem *item = new wxGenericTreeItem(parent,
622 text, dc,
623 image, selImage,
624 data);
625
626 if ( data != NULL )
627 {
628 data->m_pItem = item;
629 }
630
631 parent->Insert( item, previous );
632
633 m_dirty = TRUE;
634
635 return item;
636 }
637
638 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
639 int image, int selImage,
640 wxTreeItemData *data)
641 {
642 wxCHECK_MSG( !m_anchor, NULL, "tree can have only one root" );
643
644 wxClientDC dc(this);
645 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
646 image, selImage, data);
647 if ( data != NULL )
648 {
649 data->m_pItem = m_anchor;
650 }
651
652 AdjustMyScrollbars();
653 Refresh();
654
655 return m_anchor;
656 }
657
658 wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
659 const wxString& text,
660 int image, int selImage,
661 wxTreeItemData *data)
662 {
663 return DoInsertItem(parent, 0u, text, image, selImage, data);
664 }
665
666 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
667 const wxTreeItemId& idPrevious,
668 const wxString& text,
669 int image, int selImage,
670 wxTreeItemData *data)
671 {
672 wxGenericTreeItem *parent = parentId.m_pItem;
673 if ( !parent )
674 {
675 // should we give a warning here?
676 return AddRoot(text, image, selImage, data);
677 }
678
679 int index = parent->GetChildren().Index(idPrevious.m_pItem);
680 wxASSERT_MSG( index != NOT_FOUND,
681 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
682 return DoInsertItem(parentId, (size_t)index, text, image, selImage, data);
683 }
684
685 wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
686 const wxString& text,
687 int image, int selImage,
688 wxTreeItemData *data)
689 {
690 wxGenericTreeItem *parent = parentId.m_pItem;
691 if ( !parent )
692 {
693 // should we give a warning here?
694 return AddRoot(text, image, selImage, data);
695 }
696
697 return DoInsertItem(parent, parent->GetChildren().Count(), text,
698 image, selImage, data);
699 }
700
701 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
702 {
703 wxGenericTreeItem *item = itemId.m_pItem;
704 item->DeleteChildren();
705
706 m_dirty = TRUE;
707 }
708
709 void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
710 {
711 wxGenericTreeItem *item = itemId.m_pItem;
712 wxGenericTreeItem *parent = item->GetParent();
713
714 // notify the parent...
715 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
716 event.m_item = item;
717 event.SetEventObject( this );
718 ProcessEvent( event );
719
720 if ( parent )
721 {
722 parent->GetChildren().Remove(item);
723 }
724
725 delete item;
726
727 m_dirty = TRUE;
728 }
729
730 void wxTreeCtrl::DeleteAllItems()
731 {
732 if ( m_anchor )
733 {
734 delete m_anchor;
735 m_anchor = NULL;
736
737 m_dirty = TRUE;
738 }
739 }
740
741 void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
742 {
743 wxGenericTreeItem *item = itemId.m_pItem;
744
745 if ( !item->HasPlus() )
746 return;
747
748 if ( item->IsExpanded() )
749 return;
750
751 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
752 event.m_item = item;
753 event.SetEventObject( this );
754 if ( ProcessEvent( event ) && event.m_code )
755 {
756 // cancelled by program
757 return;
758 }
759
760 item->Expand();
761
762 RefreshSubtree(item);
763
764 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
765 ProcessEvent( event );
766 }
767
768 void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
769 {
770 wxGenericTreeItem *item = itemId.m_pItem;
771
772 if ( !item->IsExpanded() )
773 return;
774
775 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
776 event.m_item = item;
777 event.SetEventObject( this );
778 if ( ProcessEvent( event ) && event.m_code )
779 {
780 // cancelled by program
781 return;
782 }
783
784 item->Collapse();
785
786 wxArrayTreeItems& children = item->GetChildren();
787 size_t count = children.Count();
788 for ( size_t n = 0; n < count; n++ )
789 {
790 Collapse(children[n]);
791 }
792
793 CalculatePositions();
794
795 RefreshSubtree(item);
796
797 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
798 ProcessEvent( event );
799 }
800
801 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
802 {
803 Collapse(item);
804 DeleteChildren(item);
805 }
806
807 void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
808 {
809 wxGenericTreeItem *item = itemId.m_pItem;
810
811 if ( item->IsExpanded() )
812 Collapse(itemId);
813 else
814 Expand(itemId);
815 }
816
817 void wxTreeCtrl::Unselect()
818 {
819 if ( m_current )
820 {
821 m_current->SetHilight( FALSE );
822 RefreshLine( m_current );
823 }
824 }
825
826 void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId)
827 {
828 wxGenericTreeItem *item = itemId.m_pItem;
829
830 if ( m_current != item )
831 {
832 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
833 event.m_item = item;
834 event.m_itemOld = m_current;
835 event.SetEventObject( this );
836 if ( GetEventHandler()->ProcessEvent( event ) && event.WasVetoed() )
837 return;
838
839 if ( m_current )
840 {
841 m_current->SetHilight( FALSE );
842 RefreshLine( m_current );
843 }
844
845 m_current = item;
846 m_current->SetHilight( TRUE );
847 RefreshLine( m_current );
848
849 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
850 GetEventHandler()->ProcessEvent( event );
851 }
852 }
853
854 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
855 {
856 wxGenericTreeItem *gitem = item.m_pItem;
857
858 int item_y = gitem->GetY();
859
860 int start_x = 0;
861 int start_y = 0;
862 ViewStart( &start_x, &start_y );
863 start_y *= 10;
864
865 if (item_y < start_y+3)
866 {
867 int x = 0;
868 int y = 0;
869 m_anchor->GetSize( x, y );
870 y += 2*m_lineHeight;
871 int x_pos = GetScrollPos( wxHORIZONTAL );
872 SetScrollbars( 10, 10, x/10, y/10, x_pos, item_y/10 );
873 return;
874 }
875
876 int w = 0;
877 int h = 0;
878 GetClientSize( &w, &h );
879
880 if (item_y > start_y+h-26)
881 {
882 int x = 0;
883 int y = 0;
884 m_anchor->GetSize( x, y );
885 y += 2*m_lineHeight;
886 int x_pos = GetScrollPos( wxHORIZONTAL );
887 SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-h+30)/10 );
888 return;
889 }
890 }
891
892 void wxTreeCtrl::ScrollTo(const wxTreeItemId& WXUNUSED(item))
893 {
894 wxFAIL_MSG("not implemented");
895 }
896
897 wxTextCtrl *wxTreeCtrl::EditLabel( const wxTreeItemId& WXUNUSED(item),
898 wxClassInfo* WXUNUSED(textCtrlClass) )
899 {
900 wxFAIL_MSG("not implemented");
901
902 return NULL;
903 }
904
905 wxTextCtrl *wxTreeCtrl::GetEditControl() const
906 {
907 wxFAIL_MSG("not implemented");
908
909 return NULL;
910 }
911
912 void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool WXUNUSED(discardChanges))
913 {
914 wxFAIL_MSG("not implemented");
915 }
916
917 void wxTreeCtrl::SortChildren( const wxTreeItemId& WXUNUSED(item),
918 wxTreeItemCmpFunc *WXUNUSED(cmpFunction))
919 {
920 wxFAIL_MSG("not implemented");
921 }
922
923 wxImageList *wxTreeCtrl::GetImageList() const
924 {
925 return m_imageListNormal;
926 }
927
928 wxImageList *wxTreeCtrl::GetStateImageList() const
929 {
930 return m_imageListState;
931 }
932
933 void wxTreeCtrl::SetImageList(wxImageList *imageList)
934 {
935 m_imageListNormal = imageList;
936 }
937
938 void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
939 {
940 m_imageListState = imageList;
941 }
942
943 // -----------------------------------------------------------------------------
944 // helpers
945 // -----------------------------------------------------------------------------
946 void wxTreeCtrl::AdjustMyScrollbars()
947 {
948 if (m_anchor)
949 {
950 int x = 0;
951 int y = 0;
952 m_anchor->GetSize( x, y );
953 y += 2*m_lineHeight;
954 int x_pos = GetScrollPos( wxHORIZONTAL );
955 int y_pos = GetScrollPos( wxVERTICAL );
956 SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos );
957 }
958 else
959 {
960 SetScrollbars( 0, 0, 0, 0 );
961 }
962 }
963
964 void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
965 {
966 // render bold items in bold
967 wxFont fontOld;
968 wxFont fontNew;
969
970 if ( item->IsBold() )
971 {
972 fontOld = dc.GetFont();
973 if (fontOld.Ok())
974 {
975 // @@ is there any better way to make a bold variant of old font?
976 fontNew = wxFont( fontOld.GetPointSize(),
977 fontOld.GetFamily(),
978 fontOld.GetStyle(),
979 wxBOLD,
980 fontOld.GetUnderlined());
981 dc.SetFont(fontNew);
982 }
983 else
984 {
985 wxFAIL_MSG("wxDC::GetFont() failed!");
986 }
987 }
988
989 long text_w = 0;
990 long text_h = 0;
991 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
992
993 int image_h = 0;
994 int image_w = 0;
995 if (item->GetImage() != -1)
996 {
997 m_imageListNormal->GetSize( item->GetImage(), image_w, image_h );
998 image_w += 4;
999 }
1000
1001 dc.DrawRectangle( item->GetX()-2, item->GetY()-2, image_w+text_w+4, text_h+4 );
1002
1003 if (item->GetImage() != -1)
1004 {
1005 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, text_h );
1006 m_imageListNormal->Draw( item->GetImage(), dc,
1007 item->GetX(), item->GetY()-1,
1008 wxIMAGELIST_DRAW_TRANSPARENT );
1009 dc.DestroyClippingRegion();
1010 }
1011
1012 dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY() );
1013
1014 // restore normal font for bold items
1015 if (fontOld.Ok())
1016 {
1017 dc.SetFont( fontOld);
1018 }
1019 }
1020
1021 void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
1022 {
1023 int horizX = level*m_indent;
1024
1025 item->SetX( horizX+33 );
1026 item->SetY( y-m_lineHeight/3 );
1027 item->SetHeight( m_lineHeight );
1028
1029 item->SetCross( horizX+15, y );
1030
1031 int oldY = y;
1032
1033 int exposed_x = dc.LogicalToDeviceX( 0 );
1034 int exposed_y = dc.LogicalToDeviceY( item->GetY()-2 );
1035
1036 if (IsExposed( exposed_x, exposed_y, 10000, m_lineHeight+4 )) // 10000 = very much
1037 {
1038 int startX = horizX;
1039 int endX = horizX + 10;
1040
1041 if (!item->HasChildren()) endX += 20;
1042
1043 dc.DrawLine( startX, y, endX, y );
1044
1045 if (item->HasPlus())
1046 {
1047 dc.DrawLine( horizX+20, y, horizX+30, y );
1048 dc.SetPen( *wxGREY_PEN );
1049 dc.SetBrush( *wxWHITE_BRUSH );
1050 dc.DrawRectangle( horizX+10, y-4, 11, 9 );
1051 dc.SetPen( *wxBLACK_PEN );
1052 dc.DrawLine( horizX+13, y, horizX+18, y );
1053
1054 if (!item->IsExpanded())
1055 dc.DrawLine( horizX+15, y-2, horizX+15, y+3 );
1056 }
1057
1058 if (item->HasHilight())
1059 {
1060 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
1061
1062 dc.SetBrush( *m_hilightBrush );
1063
1064 if (m_hasFocus)
1065 dc.SetPen( *wxBLACK_PEN );
1066 else
1067 dc.SetPen( *wxTRANSPARENT_PEN );
1068
1069 PaintItem(item, dc);
1070
1071 dc.SetPen( *wxBLACK_PEN );
1072 dc.SetTextForeground( *wxBLACK );
1073 dc.SetBrush( *wxWHITE_BRUSH );
1074 }
1075 else
1076 {
1077 dc.SetBrush( *wxWHITE_BRUSH );
1078 dc.SetPen( *wxTRANSPARENT_PEN );
1079
1080 PaintItem(item, dc);
1081
1082 dc.SetPen( *wxBLACK_PEN );
1083 }
1084 }
1085
1086 if ( item->IsExpanded() )
1087 {
1088 int semiOldY = y;
1089
1090 wxArrayTreeItems& children = item->GetChildren();
1091 size_t count = children.Count();
1092 for ( size_t n = 0; n < count; n++ )
1093 {
1094 y += m_lineHeight;
1095 semiOldY = y;
1096
1097 PaintLevel( children[n], dc, level+1, y );
1098 }
1099
1100 // it may happen that the item is expanded but has no items (when you
1101 // delete all its children for example) - don't draw the vertical line
1102 // in this case
1103 if ( count > 0 )
1104 dc.DrawLine( horizX+15, oldY+5, horizX+15, semiOldY );
1105 }
1106 }
1107
1108 // -----------------------------------------------------------------------------
1109 // wxWindows callbacks
1110 // -----------------------------------------------------------------------------
1111
1112 void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
1113 {
1114 if ( !m_anchor )
1115 return;
1116
1117 wxPaintDC dc(this);
1118 PrepareDC( dc );
1119
1120 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
1121
1122 dc.SetPen( m_dottedPen );
1123 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1124
1125 int y = m_lineHeight / 2 + 2;
1126 PaintLevel( m_anchor, dc, 0, y );
1127 }
1128
1129 void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1130 {
1131 m_hasFocus = TRUE;
1132 if ( m_current )
1133 RefreshLine( m_current );
1134 }
1135
1136 void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1137 {
1138 m_hasFocus = FALSE;
1139 if ( m_current )
1140 RefreshLine( m_current );
1141 }
1142
1143 void wxTreeCtrl::OnChar( wxKeyEvent &event )
1144 {
1145 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
1146 te.m_code = event.KeyCode();
1147 te.SetEventObject( this );
1148 GetEventHandler()->ProcessEvent( te );
1149
1150 if (m_current == 0)
1151 {
1152 event.Skip();
1153 return;
1154 }
1155
1156 switch (event.KeyCode())
1157 {
1158 case '+':
1159 case WXK_ADD:
1160 if (m_current->HasPlus() && !IsExpanded(m_current))
1161 {
1162 Expand(m_current);
1163 }
1164 break;
1165
1166 case '-':
1167 case WXK_SUBTRACT:
1168 if (IsExpanded(m_current))
1169 {
1170 Collapse(m_current);
1171 }
1172 break;
1173
1174 case '*':
1175 case WXK_MULTIPLY:
1176 Toggle(m_current);
1177 break;
1178
1179 case ' ':
1180 case WXK_RETURN:
1181 {
1182 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1183 event.m_item = m_current;
1184 event.m_code = 0;
1185 event.SetEventObject( this );
1186 GetEventHandler()->ProcessEvent( event );
1187 }
1188 break;
1189
1190 case WXK_LEFT:
1191 case WXK_UP:
1192 {
1193 wxTreeItemId prev = GetPrevSibling( m_current );
1194 if (prev != 0)
1195 {
1196 SelectItem( prev );
1197 EnsureVisible( prev );
1198 }
1199 else
1200 {
1201 prev = GetParent( m_current );
1202 if (prev)
1203 {
1204 EnsureVisible( prev );
1205 SelectItem( prev );
1206 }
1207 }
1208 }
1209 break;
1210
1211 case WXK_RIGHT:
1212 // this works the same as the down arrow except that we also expand the
1213 // item if it wasn't expanded yet
1214 Expand(m_current);
1215 // fall through
1216
1217 case WXK_DOWN:
1218 {
1219 if (IsExpanded(m_current))
1220 {
1221 long cookie = 0;
1222 wxTreeItemId child = GetFirstChild( m_current, cookie );
1223 SelectItem( child );
1224 EnsureVisible( child );
1225 }
1226 else
1227 {
1228 wxTreeItemId next = GetNextSibling( m_current );
1229 if (next == 0)
1230 {
1231 wxTreeItemId current = m_current;
1232 while (current && !next)
1233 {
1234 current = GetParent( current );
1235 if (current) next = GetNextSibling( current );
1236 }
1237 }
1238 if (next != 0)
1239 {
1240 SelectItem( next );
1241 EnsureVisible( next );
1242 }
1243 }
1244 }
1245 break;
1246
1247 default:
1248 event.Skip();
1249 }
1250 }
1251
1252 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& WXUNUSED(flags))
1253 {
1254 bool onButton = FALSE;
1255 return m_anchor->HitTest( point, onButton );
1256 }
1257
1258 void wxTreeCtrl::OnMouse( wxMouseEvent &event )
1259 {
1260 if ( !(event.LeftDown() || event.LeftDClick()) )
1261 return;
1262
1263 if ( !m_anchor )
1264 return;
1265
1266 wxClientDC dc(this);
1267 PrepareDC(dc);
1268 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1269 long y = dc.DeviceToLogicalY( (long)event.GetY() );
1270
1271 bool onButton = FALSE;
1272 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), onButton );
1273 if ( item == NULL )
1274 return;
1275
1276 if (!IsSelected(item)) SelectItem(item);
1277
1278 if ( event.LeftDClick() )
1279 {
1280 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1281 event.m_item = item;
1282 event.m_code = 0;
1283 event.SetEventObject( this );
1284 GetEventHandler()->ProcessEvent( event );
1285 }
1286
1287 if ( onButton )
1288 {
1289 Toggle( item );
1290 }
1291 }
1292
1293 void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
1294 {
1295 if (!m_dirty) return;
1296
1297 m_dirty = FALSE;
1298
1299 CalculatePositions();
1300
1301 AdjustMyScrollbars();
1302 }
1303
1304 // -----------------------------------------------------------------------------
1305 // -----------------------------------------------------------------------------
1306 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item,
1307 wxDC &dc,
1308 int level,
1309 int &y )
1310 {
1311 int horizX = level*m_indent;
1312
1313 item->SetX( horizX+33 );
1314 item->SetY( y-m_lineHeight/3-2 );
1315 item->SetHeight( m_lineHeight );
1316
1317 if ( item->IsExpanded() )
1318 return;
1319
1320 wxArrayTreeItems& children = item->GetChildren();
1321 size_t count = children.Count();
1322 for ( size_t n = 0; n < count; n++ )
1323 {
1324 y += m_lineHeight;
1325 CalculateLevel( children[n], dc, level+1, y );
1326 }
1327 }
1328
1329 void wxTreeCtrl::CalculatePositions()
1330 {
1331 if ( !m_anchor )
1332 return;
1333
1334 wxClientDC dc(this);
1335 PrepareDC( dc );
1336
1337 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
1338
1339 dc.SetPen( m_dottedPen );
1340 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1341
1342 int y = m_lineHeight / 2 + 2;
1343 CalculateLevel( m_anchor, dc, 0, y );
1344 }
1345
1346 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
1347 {
1348 wxClientDC dc(this);
1349 PrepareDC(dc);
1350
1351 int cw = 0;
1352 int ch = 0;
1353 GetClientSize( &cw, &ch );
1354
1355 wxRect rect;
1356 rect.x = dc.LogicalToDeviceX( 0 );
1357 rect.width = cw;
1358 rect.y = dc.LogicalToDeviceY( item->GetY() );
1359 rect.height = ch;
1360
1361 Refresh( TRUE, &rect );
1362
1363 AdjustMyScrollbars();
1364 }
1365
1366 void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
1367 {
1368 wxClientDC dc(this);
1369 PrepareDC( dc );
1370
1371 wxRect rect;
1372 rect.x = dc.LogicalToDeviceX( item->GetX() - 2 );
1373 rect.y = dc.LogicalToDeviceY( item->GetY() - 2 );
1374 rect.width = 1000;
1375 rect.height = dc.GetCharHeight() + 6;
1376 Refresh( TRUE, &rect );
1377 }
1378