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