]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectrl.cpp
Removed wxProp source files
[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
364 bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
365 const wxPoint& pos, const wxSize& size,
366 long style,
367 const wxValidator &validator,
368 const wxString& name )
369 {
370 Init();
371
372 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
373
374 SetValidator( validator );
375
376 SetBackgroundColour( *wxWHITE );
377 m_dottedPen = wxPen( *wxBLACK, 0, 0 );
378
379 return TRUE;
380 }
381
382 wxTreeCtrl::~wxTreeCtrl()
383 {
384 wxDELETE( m_hilightBrush );
385
386 DeleteAllItems();
387 }
388
389 // -----------------------------------------------------------------------------
390 // accessors
391 // -----------------------------------------------------------------------------
392
393 size_t wxTreeCtrl::GetCount() const
394 {
395 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
396 }
397
398 void wxTreeCtrl::SetIndent(unsigned int indent)
399 {
400 m_indent = indent;
401 Refresh();
402 }
403
404 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
405 {
406 wxCHECK_MSG( item.IsOk(), 0u, "invalid tree item" );
407
408 return item.m_pItem->GetChildrenCount(recursively);
409 }
410
411 // -----------------------------------------------------------------------------
412 // functions to work with tree items
413 // -----------------------------------------------------------------------------
414
415 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
416 {
417 wxCHECK_MSG( item.IsOk(), "", "invalid tree item" );
418
419 return item.m_pItem->GetText();
420 }
421
422 int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const
423 {
424 wxCHECK_MSG( item.IsOk(), -1, "invalid tree item" );
425
426 return item.m_pItem->GetImage();
427 }
428
429 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
430 {
431 wxCHECK_MSG( item.IsOk(), -1, "invalid tree item" );
432
433 return item.m_pItem->GetSelectedImage();
434 }
435
436 wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
437 {
438 wxCHECK_MSG( item.IsOk(), NULL, "invalid tree item" );
439
440 return item.m_pItem->GetData();
441 }
442
443 void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
444 {
445 wxCHECK_RET( item.IsOk(), "invalid tree item" );
446
447 wxClientDC dc(this);
448 item.m_pItem->SetText(text, dc);
449 }
450
451 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image)
452 {
453 wxCHECK_RET( item.IsOk(), "invalid tree item" );
454
455 item.m_pItem->SetImage(image);
456 }
457
458 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
459 {
460 wxCHECK_RET( item.IsOk(), "invalid tree item" );
461
462 item.m_pItem->SetSelectedImage(image);
463 }
464
465 void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
466 {
467 wxCHECK_RET( item.IsOk(), "invalid tree item" );
468
469 item.m_pItem->SetData(data);
470 }
471
472 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
473 {
474 wxCHECK_RET( item.IsOk(), "invalid tree item" );
475
476 item.m_pItem->SetHasPlus(has);
477 }
478
479 void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
480 {
481 wxCHECK_RET( item.IsOk(), "invalid tree item" );
482
483 // avoid redrawing the tree if no real change
484 wxGenericTreeItem *pItem = item.m_pItem;
485 if ( pItem->IsBold() != bold )
486 {
487 pItem->SetBold(bold);
488 RefreshLine(pItem);
489 }
490 }
491
492 // -----------------------------------------------------------------------------
493 // item status inquiries
494 // -----------------------------------------------------------------------------
495
496 bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const
497 {
498 wxFAIL_MSG("not implemented");
499
500 return TRUE;
501 }
502
503 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
504 {
505 wxCHECK_MSG( item.IsOk(), FALSE, "invalid tree item" );
506
507 return !item.m_pItem->GetChildren().IsEmpty();
508 }
509
510 bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
511 {
512 wxCHECK_MSG( item.IsOk(), FALSE, "invalid tree item" );
513
514 return item.m_pItem->IsExpanded();
515 }
516
517 bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
518 {
519 wxCHECK_MSG( item.IsOk(), FALSE, "invalid tree item" );
520
521 return item.m_pItem->HasHilight();
522 }
523
524 bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
525 {
526 wxCHECK_MSG( item.IsOk(), FALSE, "invalid tree item" );
527
528 return item.m_pItem->IsBold();
529 }
530
531 // -----------------------------------------------------------------------------
532 // navigation
533 // -----------------------------------------------------------------------------
534
535 wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
536 {
537 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
538
539 return item.m_pItem->GetParent();
540 }
541
542 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
543 {
544 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
545
546 cookie = 0;
547 return GetNextChild(item, cookie);
548 }
549
550 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
551 {
552 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
553
554 wxArrayTreeItems& children = item.m_pItem->GetChildren();
555 if ( (size_t)cookie < children.Count() )
556 {
557 return item.m_pItem->GetChildren().Item(cookie++);
558 }
559 else
560 {
561 // there are no more of them
562 return wxTreeItemId();
563 }
564 }
565
566 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
567 {
568 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
569
570 wxGenericTreeItem *i = item.m_pItem;
571 wxGenericTreeItem *parent = i->GetParent();
572 if ( parent == NULL )
573 {
574 // root item doesn't have any siblings
575 return wxTreeItemId();
576 }
577
578 wxArrayTreeItems& siblings = parent->GetChildren();
579 int index = siblings.Index(i);
580 wxASSERT( index != NOT_FOUND ); // I'm not a child of my parent?
581
582 size_t n = (size_t)(index + 1);
583 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
584 }
585
586 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
587 {
588 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
589
590 wxGenericTreeItem *i = item.m_pItem;
591 wxGenericTreeItem *parent = i->GetParent();
592 if ( parent == NULL )
593 {
594 // root item doesn't have any siblings
595 return wxTreeItemId();
596 }
597
598 wxArrayTreeItems& siblings = parent->GetChildren();
599 int index = siblings.Index(i);
600 wxASSERT( index != NOT_FOUND ); // I'm not a child of my parent?
601
602 return index == 0 ? wxTreeItemId()
603 : wxTreeItemId(siblings[(size_t)(index - 1)]);
604 }
605
606 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
607 {
608 wxFAIL_MSG("not implemented");
609
610 return wxTreeItemId();
611 }
612
613 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
614 {
615 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
616
617 wxFAIL_MSG("not implemented");
618
619 return wxTreeItemId();
620 }
621
622 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
623 {
624 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
625
626 wxFAIL_MSG("not implemented");
627
628 return wxTreeItemId();
629 }
630
631 // -----------------------------------------------------------------------------
632 // operations
633 // -----------------------------------------------------------------------------
634
635 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
636 size_t previous,
637 const wxString& text,
638 int image, int selImage,
639 wxTreeItemData *data)
640 {
641 wxGenericTreeItem *parent = parentId.m_pItem;
642 if ( !parent )
643 {
644 // should we give a warning here?
645 return AddRoot(text, image, selImage, data);
646 }
647
648 wxClientDC dc(this);
649 wxGenericTreeItem *item = new wxGenericTreeItem(parent,
650 text, dc,
651 image, selImage,
652 data);
653
654 if ( data != NULL )
655 {
656 data->m_pItem = item;
657 }
658
659 parent->Insert( item, previous );
660
661 m_dirty = TRUE;
662
663 return item;
664 }
665
666 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
667 int image, int selImage,
668 wxTreeItemData *data)
669 {
670 wxCHECK_MSG( !m_anchor, wxTreeItemId(), "tree can have only one root" );
671
672 wxClientDC dc(this);
673 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
674 image, selImage, data);
675 if ( data != NULL )
676 {
677 data->m_pItem = m_anchor;
678 }
679
680 AdjustMyScrollbars();
681 Refresh();
682
683 return m_anchor;
684 }
685
686 wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
687 const wxString& text,
688 int image, int selImage,
689 wxTreeItemData *data)
690 {
691 return DoInsertItem(parent, 0u, text, image, selImage, data);
692 }
693
694 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
695 const wxTreeItemId& idPrevious,
696 const wxString& text,
697 int image, int selImage,
698 wxTreeItemData *data)
699 {
700 wxGenericTreeItem *parent = parentId.m_pItem;
701 if ( !parent )
702 {
703 // should we give a warning here?
704 return AddRoot(text, image, selImage, data);
705 }
706
707 int index = parent->GetChildren().Index(idPrevious.m_pItem);
708 wxASSERT_MSG( index != NOT_FOUND,
709 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
710 return DoInsertItem(parentId, (size_t)index, text, image, selImage, data);
711 }
712
713 wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
714 const wxString& text,
715 int image, int selImage,
716 wxTreeItemData *data)
717 {
718 wxGenericTreeItem *parent = parentId.m_pItem;
719 if ( !parent )
720 {
721 // should we give a warning here?
722 return AddRoot(text, image, selImage, data);
723 }
724
725 return DoInsertItem(parent, parent->GetChildren().Count(), text,
726 image, selImage, data);
727 }
728
729 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
730 {
731 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
732 event.m_item = item;
733 event.SetEventObject( this );
734 ProcessEvent( event );
735 }
736
737 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
738 {
739 wxGenericTreeItem *item = itemId.m_pItem;
740 item->DeleteChildren(this);
741
742 m_dirty = TRUE;
743 }
744
745 void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
746 {
747 wxGenericTreeItem *item = itemId.m_pItem;
748 wxGenericTreeItem *parent = item->GetParent();
749
750 if ( parent )
751 {
752 parent->GetChildren().Remove(item);
753 }
754
755 item->DeleteChildren(this);
756 SendDeleteEvent(item);
757 delete item;
758
759 m_dirty = TRUE;
760 }
761
762 void wxTreeCtrl::DeleteAllItems()
763 {
764 if ( m_anchor )
765 {
766 m_anchor->DeleteChildren(this);
767 delete m_anchor;
768
769 m_anchor = NULL;
770
771 m_dirty = TRUE;
772 }
773 }
774
775 void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
776 {
777 wxGenericTreeItem *item = itemId.m_pItem;
778
779 if ( !item->HasPlus() )
780 return;
781
782 if ( item->IsExpanded() )
783 return;
784
785 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
786 event.m_item = item;
787 event.SetEventObject( this );
788 if ( ProcessEvent( event ) && event.m_code )
789 {
790 // cancelled by program
791 return;
792 }
793
794 item->Expand();
795 CalculatePositions();
796
797 RefreshSubtree(item);
798
799 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
800 ProcessEvent( event );
801 }
802
803 void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
804 {
805 wxGenericTreeItem *item = itemId.m_pItem;
806
807 if ( !item->IsExpanded() )
808 return;
809
810 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
811 event.m_item = item;
812 event.SetEventObject( this );
813 if ( ProcessEvent( event ) && event.m_code )
814 {
815 // cancelled by program
816 return;
817 }
818
819 item->Collapse();
820
821 wxArrayTreeItems& children = item->GetChildren();
822 size_t count = children.Count();
823 for ( size_t n = 0; n < count; n++ )
824 {
825 Collapse(children[n]);
826 }
827
828 CalculatePositions();
829
830 RefreshSubtree(item);
831
832 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
833 ProcessEvent( event );
834 }
835
836 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
837 {
838 Collapse(item);
839 DeleteChildren(item);
840 }
841
842 void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
843 {
844 wxGenericTreeItem *item = itemId.m_pItem;
845
846 if ( item->IsExpanded() )
847 Collapse(itemId);
848 else
849 Expand(itemId);
850 }
851
852 void wxTreeCtrl::Unselect()
853 {
854 if ( m_current )
855 {
856 m_current->SetHilight( FALSE );
857 RefreshLine( m_current );
858 }
859 }
860
861 void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId)
862 {
863 wxGenericTreeItem *item = itemId.m_pItem;
864
865 if ( m_current != item )
866 {
867 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
868 event.m_item = item;
869 event.m_itemOld = m_current;
870 event.SetEventObject( this );
871 if ( GetEventHandler()->ProcessEvent( event ) && event.WasVetoed() )
872 return;
873
874 if ( m_current )
875 {
876 m_current->SetHilight( FALSE );
877 RefreshLine( m_current );
878 }
879
880 m_current = item;
881 m_current->SetHilight( TRUE );
882 RefreshLine( m_current );
883
884 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
885 GetEventHandler()->ProcessEvent( event );
886 }
887 }
888
889 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
890 {
891 wxGenericTreeItem *gitem = item.m_pItem;
892
893 int item_y = gitem->GetY();
894
895 int start_x = 0;
896 int start_y = 0;
897 ViewStart( &start_x, &start_y );
898 start_y *= 10;
899
900 int client_h = 0;
901 int client_w = 0;
902 GetClientSize( &client_w, &client_h );
903
904 if (item_y < start_y+3)
905 {
906 int x = 0;
907 int y = 0;
908 m_anchor->GetSize( x, y );
909 y += 2*m_lineHeight;
910 int x_pos = GetScrollPos( wxHORIZONTAL );
911 SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 );
912 return;
913 }
914
915 if (item_y > start_y+client_h-16)
916 {
917 int x = 0;
918 int y = 0;
919 m_anchor->GetSize( x, y );
920 y += 2*m_lineHeight;
921 int x_pos = GetScrollPos( wxHORIZONTAL );
922 SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 );
923 return;
924 }
925 }
926
927 void wxTreeCtrl::ScrollTo(const wxTreeItemId& WXUNUSED(item))
928 {
929 wxFAIL_MSG("not implemented");
930 }
931
932 wxTextCtrl *wxTreeCtrl::EditLabel( const wxTreeItemId& WXUNUSED(item),
933 wxClassInfo* WXUNUSED(textCtrlClass) )
934 {
935 wxFAIL_MSG("not implemented");
936
937 return (wxTextCtrl*)NULL;
938 }
939
940 wxTextCtrl *wxTreeCtrl::GetEditControl() const
941 {
942 wxFAIL_MSG("not implemented");
943
944 return (wxTextCtrl*)NULL;
945 }
946
947 void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool WXUNUSED(discardChanges))
948 {
949 wxFAIL_MSG("not implemented");
950 }
951
952 // FIXME: tree sorting functions are not reentrant and not MT-safe!
953 static wxTreeCtrl *s_treeBeingSorted = NULL;
954
955 static int tree_ctrl_compare_func(wxGenericTreeItem **item1,
956 wxGenericTreeItem **item2)
957 {
958 wxCHECK_MSG( s_treeBeingSorted, 0, "bug in wxTreeCtrl::SortChildren()" );
959
960 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
961 }
962
963 int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
964 const wxTreeItemId& item2)
965 {
966 return strcmp(GetItemText(item1), GetItemText(item2));
967 }
968
969 void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
970 {
971 wxCHECK_RET( itemId.IsOk(), "invalid tree item" );
972
973 wxGenericTreeItem *item = itemId.m_pItem;
974
975 wxCHECK_RET( !s_treeBeingSorted,
976 "wxTreeCtrl::SortChildren is not reentrant" );
977
978 wxArrayTreeItems& children = item->GetChildren();
979 if ( children.Count() > 1 )
980 {
981 s_treeBeingSorted = this;
982 children.Sort(tree_ctrl_compare_func);
983 s_treeBeingSorted = NULL;
984
985 m_dirty = TRUE;
986 }
987 //else: don't make the tree dirty as nothing changed
988 }
989
990 wxImageList *wxTreeCtrl::GetImageList() const
991 {
992 return m_imageListNormal;
993 }
994
995 wxImageList *wxTreeCtrl::GetStateImageList() const
996 {
997 return m_imageListState;
998 }
999
1000 void wxTreeCtrl::SetImageList(wxImageList *imageList)
1001 {
1002 m_imageListNormal = imageList;
1003 }
1004
1005 void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
1006 {
1007 m_imageListState = imageList;
1008 }
1009
1010 // -----------------------------------------------------------------------------
1011 // helpers
1012 // -----------------------------------------------------------------------------
1013
1014 void wxTreeCtrl::AdjustMyScrollbars()
1015 {
1016 if (m_anchor)
1017 {
1018 int x = 0;
1019 int y = 0;
1020 m_anchor->GetSize( x, y );
1021 y += 2*m_lineHeight;
1022 int x_pos = GetScrollPos( wxHORIZONTAL );
1023 int y_pos = GetScrollPos( wxVERTICAL );
1024 SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos );
1025 }
1026 else
1027 {
1028 SetScrollbars( 0, 0, 0, 0 );
1029 }
1030 }
1031
1032 void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1033 {
1034 // render bold items in bold
1035 wxFont fontOld;
1036 wxFont fontNew;
1037
1038 if ( item->IsBold() )
1039 {
1040 fontOld = dc.GetFont();
1041 if (fontOld.Ok())
1042 {
1043 // @@ is there any better way to make a bold variant of old font?
1044 fontNew = wxFont( fontOld.GetPointSize(),
1045 fontOld.GetFamily(),
1046 fontOld.GetStyle(),
1047 wxBOLD,
1048 fontOld.GetUnderlined());
1049 dc.SetFont(fontNew);
1050 }
1051 else
1052 {
1053 wxFAIL_MSG("wxDC::GetFont() failed!");
1054 }
1055 }
1056
1057 long text_w = 0;
1058 long text_h = 0;
1059 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
1060
1061 int image_h = 0;
1062 int image_w = 0;
1063 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1064 {
1065 m_imageListNormal->GetSize( item->GetSelectedImage(), image_w, image_h );
1066 image_w += 4;
1067 }
1068 else if (item->GetImage() != -1)
1069 {
1070 m_imageListNormal->GetSize( item->GetImage(), image_w, image_h );
1071 image_w += 4;
1072 }
1073
1074 dc.DrawRectangle( item->GetX()-2, item->GetY()-2, image_w+text_w+4, text_h+4 );
1075
1076 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1077 {
1078 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, text_h );
1079 m_imageListNormal->Draw( item->GetSelectedImage(), dc,
1080 item->GetX(), item->GetY()-1,
1081 wxIMAGELIST_DRAW_TRANSPARENT );
1082 dc.DestroyClippingRegion();
1083 }
1084 else if (item->GetImage() != -1)
1085 {
1086 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, text_h );
1087 m_imageListNormal->Draw( item->GetImage(), dc,
1088 item->GetX(), item->GetY()-1,
1089 wxIMAGELIST_DRAW_TRANSPARENT );
1090 dc.DestroyClippingRegion();
1091 }
1092
1093 dc.SetBackgroundMode(wxTRANSPARENT);
1094 dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY() );
1095
1096 // restore normal font for bold items
1097 if (fontOld.Ok())
1098 {
1099 dc.SetFont( fontOld);
1100 }
1101 }
1102
1103 void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
1104 {
1105 int horizX = level*m_indent;
1106
1107 item->SetX( horizX+33 );
1108 item->SetY( y-m_lineHeight/3 );
1109 item->SetHeight( m_lineHeight );
1110
1111 item->SetCross( horizX+15, y );
1112
1113 int oldY = y;
1114
1115 int exposed_x = dc.LogicalToDeviceX( 0 );
1116 int exposed_y = dc.LogicalToDeviceY( item->GetY()-2 );
1117
1118 if (IsExposed( exposed_x, exposed_y, 10000, m_lineHeight+4 )) // 10000 = very much
1119 {
1120 int startX = horizX;
1121 int endX = horizX + 10;
1122
1123 if (!item->HasChildren()) endX += 20;
1124
1125 dc.DrawLine( startX, y, endX, y );
1126
1127 if (item->HasPlus())
1128 {
1129 dc.DrawLine( horizX+20, y, horizX+30, y );
1130 dc.SetPen( *wxGREY_PEN );
1131 dc.SetBrush( *wxWHITE_BRUSH );
1132 dc.DrawRectangle( horizX+10, y-4, 11, 9 );
1133 dc.SetPen( *wxBLACK_PEN );
1134 dc.DrawLine( horizX+13, y, horizX+18, y );
1135
1136 if (!item->IsExpanded())
1137 dc.DrawLine( horizX+15, y-2, horizX+15, y+3 );
1138 }
1139
1140 if (item->HasHilight())
1141 {
1142 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
1143
1144 dc.SetBrush( *m_hilightBrush );
1145
1146 if (m_hasFocus)
1147 dc.SetPen( *wxBLACK_PEN );
1148 else
1149 dc.SetPen( *wxTRANSPARENT_PEN );
1150
1151 PaintItem(item, dc);
1152
1153 dc.SetPen( *wxBLACK_PEN );
1154 dc.SetTextForeground( *wxBLACK );
1155 dc.SetBrush( *wxWHITE_BRUSH );
1156 }
1157 else
1158 {
1159 dc.SetBrush( *wxWHITE_BRUSH );
1160 dc.SetPen( *wxTRANSPARENT_PEN );
1161
1162 PaintItem(item, dc);
1163
1164 dc.SetPen( *wxBLACK_PEN );
1165 }
1166 }
1167
1168 if ( item->IsExpanded() )
1169 {
1170 int semiOldY = y;
1171
1172 wxArrayTreeItems& children = item->GetChildren();
1173 size_t count = children.Count();
1174 for ( size_t n = 0; n < count; n++ )
1175 {
1176 y += m_lineHeight;
1177 semiOldY = y;
1178
1179 PaintLevel( children[n], dc, level+1, y );
1180 }
1181
1182 // it may happen that the item is expanded but has no items (when you
1183 // delete all its children for example) - don't draw the vertical line
1184 // in this case
1185 if ( count > 0 )
1186 dc.DrawLine( horizX+15, oldY+5, horizX+15, semiOldY );
1187 }
1188 }
1189
1190 // -----------------------------------------------------------------------------
1191 // wxWindows callbacks
1192 // -----------------------------------------------------------------------------
1193
1194 void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
1195 {
1196 if ( !m_anchor )
1197 return;
1198
1199 wxPaintDC dc(this);
1200 PrepareDC( dc );
1201
1202 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
1203
1204 dc.SetPen( m_dottedPen );
1205 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1206
1207 int y = m_lineHeight / 2 + 2;
1208 PaintLevel( m_anchor, dc, 0, y );
1209 }
1210
1211 void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1212 {
1213 m_hasFocus = TRUE;
1214 if ( m_current )
1215 RefreshLine( m_current );
1216 }
1217
1218 void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1219 {
1220 m_hasFocus = FALSE;
1221 if ( m_current )
1222 RefreshLine( m_current );
1223 }
1224
1225 void wxTreeCtrl::OnChar( wxKeyEvent &event )
1226 {
1227 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
1228 te.m_code = event.KeyCode();
1229 te.SetEventObject( this );
1230 GetEventHandler()->ProcessEvent( te );
1231
1232 if (m_current == 0)
1233 {
1234 event.Skip();
1235 return;
1236 }
1237
1238 switch (event.KeyCode())
1239 {
1240 case '+':
1241 case WXK_ADD:
1242 if (m_current->HasPlus() && !IsExpanded(m_current))
1243 {
1244 Expand(m_current);
1245 }
1246 break;
1247
1248 case '-':
1249 case WXK_SUBTRACT:
1250 if (IsExpanded(m_current))
1251 {
1252 Collapse(m_current);
1253 }
1254 break;
1255
1256 case '*':
1257 case WXK_MULTIPLY:
1258 Toggle(m_current);
1259 break;
1260
1261 case ' ':
1262 case WXK_RETURN:
1263 {
1264 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1265 event.m_item = m_current;
1266 event.m_code = 0;
1267 event.SetEventObject( this );
1268 GetEventHandler()->ProcessEvent( event );
1269 }
1270 break;
1271
1272 case WXK_UP:
1273 {
1274 wxTreeItemId prev = GetPrevSibling( m_current );
1275 if (!prev)
1276 {
1277 prev = GetParent( m_current );
1278 long cockie = 0;
1279 wxTreeItemId current = m_current;
1280 if (current == GetFirstChild( prev, cockie ))
1281 {
1282 // otherwise we return to where we came from
1283 SelectItem( prev );
1284 EnsureVisible( prev );
1285 break;
1286 }
1287 }
1288 if (prev)
1289 {
1290 while (IsExpanded(prev))
1291 {
1292 int c = (int)GetChildrenCount( prev, FALSE );
1293 long cockie = 0;
1294 prev = GetFirstChild( prev, cockie );
1295 for (int i = 0; i < c-1; i++)
1296 prev = GetNextSibling( prev );
1297 }
1298 SelectItem( prev );
1299 EnsureVisible( prev );
1300 }
1301 }
1302 break;
1303 case WXK_LEFT:
1304 {
1305 wxTreeItemId prev = GetPrevSibling( m_current );
1306 if (prev != 0)
1307 {
1308 SelectItem( prev );
1309 EnsureVisible( prev );
1310 }
1311 else
1312 {
1313 prev = GetParent( m_current );
1314 if (prev)
1315 {
1316 EnsureVisible( prev );
1317 SelectItem( prev );
1318 }
1319 }
1320 }
1321 break;
1322
1323 case WXK_RIGHT:
1324 // this works the same as the down arrow except that we also expand the
1325 // item if it wasn't expanded yet
1326 Expand(m_current);
1327 // fall through
1328
1329 case WXK_DOWN:
1330 {
1331 if (IsExpanded(m_current))
1332 {
1333 long cookie = 0;
1334 wxTreeItemId child = GetFirstChild( m_current, cookie );
1335 SelectItem( child );
1336 EnsureVisible( child );
1337 }
1338 else
1339 {
1340 wxTreeItemId next = GetNextSibling( m_current );
1341 if (next == 0)
1342 {
1343 wxTreeItemId current = m_current;
1344 while (current && !next)
1345 {
1346 current = GetParent( current );
1347 if (current) next = GetNextSibling( current );
1348 }
1349 }
1350 if (next != 0)
1351 {
1352 SelectItem( next );
1353 EnsureVisible( next );
1354 }
1355 }
1356 }
1357 break;
1358
1359 default:
1360 event.Skip();
1361 }
1362 }
1363
1364 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& WXUNUSED(flags))
1365 {
1366 bool onButton = FALSE;
1367 return m_anchor->HitTest( point, onButton );
1368 }
1369
1370 void wxTreeCtrl::OnMouse( wxMouseEvent &event )
1371 {
1372 if ( !(event.LeftDown() || event.LeftDClick()) )
1373 return;
1374
1375 if ( !m_anchor )
1376 return;
1377
1378 wxClientDC dc(this);
1379 PrepareDC(dc);
1380 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1381 long y = dc.DeviceToLogicalY( (long)event.GetY() );
1382
1383 bool onButton = FALSE;
1384 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), onButton );
1385 if ( item == NULL )
1386 return;
1387
1388 if (!IsSelected(item)) SelectItem(item);
1389
1390 if ( event.LeftDClick() )
1391 {
1392 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1393 event.m_item = item;
1394 event.m_code = 0;
1395 event.SetEventObject( this );
1396 GetEventHandler()->ProcessEvent( event );
1397 }
1398
1399 if ( onButton )
1400 {
1401 Toggle( item );
1402 }
1403 }
1404
1405 void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
1406 {
1407 if (!m_dirty) return;
1408
1409 m_dirty = FALSE;
1410
1411 CalculatePositions();
1412
1413 AdjustMyScrollbars();
1414 }
1415
1416 // -----------------------------------------------------------------------------
1417 // -----------------------------------------------------------------------------
1418 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item,
1419 wxDC &dc,
1420 int level,
1421 int &y )
1422 {
1423 int horizX = level*m_indent;
1424
1425 item->SetX( horizX+33 );
1426 item->SetY( y-m_lineHeight/3-2 );
1427 item->SetHeight( m_lineHeight );
1428
1429 // if ( item->IsExpanded() )
1430 // return;
1431 if ( !item->IsExpanded() ) // Surely this is correct? JACS
1432 return;
1433
1434 wxArrayTreeItems& children = item->GetChildren();
1435 size_t count = children.Count();
1436 for ( size_t n = 0; n < count; n++ )
1437 {
1438 y += m_lineHeight;
1439 CalculateLevel( children[n], dc, level+1, y );
1440 }
1441 }
1442
1443 void wxTreeCtrl::CalculatePositions()
1444 {
1445 if ( !m_anchor )
1446 return;
1447
1448 wxClientDC dc(this);
1449 PrepareDC( dc );
1450
1451 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
1452
1453 dc.SetPen( m_dottedPen );
1454 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1455
1456 int y = m_lineHeight / 2 + 2;
1457 CalculateLevel( m_anchor, dc, 0, y );
1458 }
1459
1460 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
1461 {
1462 wxClientDC dc(this);
1463 PrepareDC(dc);
1464
1465 int cw = 0;
1466 int ch = 0;
1467 GetClientSize( &cw, &ch );
1468
1469 wxRect rect;
1470 rect.x = dc.LogicalToDeviceX( 0 );
1471 rect.width = cw;
1472 rect.y = dc.LogicalToDeviceY( item->GetY() );
1473 rect.height = ch;
1474
1475 Refresh( TRUE, &rect );
1476
1477 AdjustMyScrollbars();
1478 }
1479
1480 void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
1481 {
1482 wxClientDC dc(this);
1483 PrepareDC( dc );
1484
1485 wxRect rect;
1486 rect.x = dc.LogicalToDeviceX( item->GetX() - 2 );
1487 rect.y = dc.LogicalToDeviceY( item->GetY() - 2 );
1488 rect.width = 1000;
1489 rect.height = dc.GetCharHeight() + 6;
1490 Refresh( TRUE, &rect );
1491 }
1492