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