]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectrl.cpp
wxTextFile::Type changed to ::wxTextFileType (portability)
[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/settings.h"
33 #include "wx/log.h"
34 #include "wx/intl.h"
35 #include "wx/dynarray.h"
36 #include "wx/dcclient.h"
37 #include "wx/imaglist.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() : 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() : siblings[(size_t)(index - 1)];
603 }
604
605 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
606 {
607 wxFAIL_MSG("not implemented");
608
609 return wxTreeItemId();
610 }
611
612 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
613 {
614 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
615
616 wxFAIL_MSG("not implemented");
617
618 return wxTreeItemId();
619 }
620
621 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
622 {
623 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
624
625 wxFAIL_MSG("not implemented");
626
627 return wxTreeItemId();
628 }
629
630 // -----------------------------------------------------------------------------
631 // operations
632 // -----------------------------------------------------------------------------
633
634 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
635 size_t previous,
636 const wxString& text,
637 int image, int selImage,
638 wxTreeItemData *data)
639 {
640 wxGenericTreeItem *parent = parentId.m_pItem;
641 if ( !parent )
642 {
643 // should we give a warning here?
644 return AddRoot(text, image, selImage, data);
645 }
646
647 wxClientDC dc(this);
648 wxGenericTreeItem *item = new wxGenericTreeItem(parent,
649 text, dc,
650 image, selImage,
651 data);
652
653 if ( data != NULL )
654 {
655 data->m_pItem = item;
656 }
657
658 parent->Insert( item, previous );
659
660 m_dirty = TRUE;
661
662 return item;
663 }
664
665 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
666 int image, int selImage,
667 wxTreeItemData *data)
668 {
669 wxCHECK_MSG( !m_anchor, wxTreeItemId(), "tree can have only one root" );
670
671 wxClientDC dc(this);
672 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
673 image, selImage, data);
674 if ( data != NULL )
675 {
676 data->m_pItem = m_anchor;
677 }
678
679 AdjustMyScrollbars();
680 Refresh();
681
682 return m_anchor;
683 }
684
685 wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
686 const wxString& text,
687 int image, int selImage,
688 wxTreeItemData *data)
689 {
690 return DoInsertItem(parent, 0u, text, image, selImage, data);
691 }
692
693 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
694 const wxTreeItemId& idPrevious,
695 const wxString& text,
696 int image, int selImage,
697 wxTreeItemData *data)
698 {
699 wxGenericTreeItem *parent = parentId.m_pItem;
700 if ( !parent )
701 {
702 // should we give a warning here?
703 return AddRoot(text, image, selImage, data);
704 }
705
706 int index = parent->GetChildren().Index(idPrevious.m_pItem);
707 wxASSERT_MSG( index != NOT_FOUND,
708 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
709 return DoInsertItem(parentId, (size_t)index, text, image, selImage, data);
710 }
711
712 wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
713 const wxString& text,
714 int image, int selImage,
715 wxTreeItemData *data)
716 {
717 wxGenericTreeItem *parent = parentId.m_pItem;
718 if ( !parent )
719 {
720 // should we give a warning here?
721 return AddRoot(text, image, selImage, data);
722 }
723
724 return DoInsertItem(parent, parent->GetChildren().Count(), text,
725 image, selImage, data);
726 }
727
728 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
729 {
730 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
731 event.m_item = item;
732 event.SetEventObject( this );
733 ProcessEvent( event );
734 }
735
736 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
737 {
738 wxGenericTreeItem *item = itemId.m_pItem;
739 item->DeleteChildren(this);
740
741 m_dirty = TRUE;
742 }
743
744 void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
745 {
746 wxGenericTreeItem *item = itemId.m_pItem;
747 wxGenericTreeItem *parent = item->GetParent();
748
749 if ( parent )
750 {
751 parent->GetChildren().Remove(item);
752 }
753
754 item->DeleteChildren(this);
755 SendDeleteEvent(item);
756 delete item;
757
758 m_dirty = TRUE;
759 }
760
761 void wxTreeCtrl::DeleteAllItems()
762 {
763 if ( m_anchor )
764 {
765 m_anchor->DeleteChildren(this);
766 delete m_anchor;
767
768 m_anchor = NULL;
769
770 m_dirty = TRUE;
771 }
772 }
773
774 void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
775 {
776 wxGenericTreeItem *item = itemId.m_pItem;
777
778 if ( !item->HasPlus() )
779 return;
780
781 if ( item->IsExpanded() )
782 return;
783
784 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
785 event.m_item = item;
786 event.SetEventObject( this );
787 if ( ProcessEvent( event ) && event.m_code )
788 {
789 // cancelled by program
790 return;
791 }
792
793 item->Expand();
794 CalculatePositions();
795
796 RefreshSubtree(item);
797
798 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
799 ProcessEvent( event );
800 }
801
802 void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
803 {
804 wxGenericTreeItem *item = itemId.m_pItem;
805
806 if ( !item->IsExpanded() )
807 return;
808
809 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
810 event.m_item = item;
811 event.SetEventObject( this );
812 if ( ProcessEvent( event ) && event.m_code )
813 {
814 // cancelled by program
815 return;
816 }
817
818 item->Collapse();
819
820 wxArrayTreeItems& children = item->GetChildren();
821 size_t count = children.Count();
822 for ( size_t n = 0; n < count; n++ )
823 {
824 Collapse(children[n]);
825 }
826
827 CalculatePositions();
828
829 RefreshSubtree(item);
830
831 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
832 ProcessEvent( event );
833 }
834
835 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
836 {
837 Collapse(item);
838 DeleteChildren(item);
839 }
840
841 void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
842 {
843 wxGenericTreeItem *item = itemId.m_pItem;
844
845 if ( item->IsExpanded() )
846 Collapse(itemId);
847 else
848 Expand(itemId);
849 }
850
851 void wxTreeCtrl::Unselect()
852 {
853 if ( m_current )
854 {
855 m_current->SetHilight( FALSE );
856 RefreshLine( m_current );
857 }
858 }
859
860 void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId)
861 {
862 wxGenericTreeItem *item = itemId.m_pItem;
863
864 if ( m_current != item )
865 {
866 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
867 event.m_item = item;
868 event.m_itemOld = m_current;
869 event.SetEventObject( this );
870 if ( GetEventHandler()->ProcessEvent( event ) && event.WasVetoed() )
871 return;
872
873 if ( m_current )
874 {
875 m_current->SetHilight( FALSE );
876 RefreshLine( m_current );
877 }
878
879 m_current = item;
880 m_current->SetHilight( TRUE );
881 RefreshLine( m_current );
882
883 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
884 GetEventHandler()->ProcessEvent( event );
885 }
886 }
887
888 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
889 {
890 wxGenericTreeItem *gitem = item.m_pItem;
891
892 int item_y = gitem->GetY();
893
894 int start_x = 0;
895 int start_y = 0;
896 ViewStart( &start_x, &start_y );
897 start_y *= 10;
898
899 int client_h = 0;
900 int client_w = 0;
901 GetClientSize( &client_w, &client_h );
902
903 if (item_y < start_y+3)
904 {
905 int x = 0;
906 int y = 0;
907 m_anchor->GetSize( x, y );
908 y += 2*m_lineHeight;
909 int x_pos = GetScrollPos( wxHORIZONTAL );
910 SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 );
911 return;
912 }
913
914 if (item_y > start_y+client_h-16)
915 {
916 int x = 0;
917 int y = 0;
918 m_anchor->GetSize( x, y );
919 y += 2*m_lineHeight;
920 int x_pos = GetScrollPos( wxHORIZONTAL );
921 SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 );
922 return;
923 }
924 }
925
926 void wxTreeCtrl::ScrollTo(const wxTreeItemId& WXUNUSED(item))
927 {
928 wxFAIL_MSG("not implemented");
929 }
930
931 wxTextCtrl *wxTreeCtrl::EditLabel( const wxTreeItemId& WXUNUSED(item),
932 wxClassInfo* WXUNUSED(textCtrlClass) )
933 {
934 wxFAIL_MSG("not implemented");
935
936 return (wxTextCtrl*)NULL;
937 }
938
939 wxTextCtrl *wxTreeCtrl::GetEditControl() const
940 {
941 wxFAIL_MSG("not implemented");
942
943 return (wxTextCtrl*)NULL;
944 }
945
946 void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool WXUNUSED(discardChanges))
947 {
948 wxFAIL_MSG("not implemented");
949 }
950
951 // FIXME: tree sorting functions are not reentrant and not MT-safe!
952 static wxTreeCtrl *s_treeBeingSorted = NULL;
953
954 static int tree_ctrl_compare_func(wxGenericTreeItem **item1,
955 wxGenericTreeItem **item2)
956 {
957 wxCHECK_MSG( s_treeBeingSorted, 0, "bug in wxTreeCtrl::SortChildren()" );
958
959 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
960 }
961
962 int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
963 const wxTreeItemId& item2)
964 {
965 return strcmp(GetItemText(item1), GetItemText(item2));
966 }
967
968 void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
969 {
970 wxCHECK_RET( itemId.IsOk(), "invalid tree item" );
971
972 wxGenericTreeItem *item = itemId.m_pItem;
973
974 wxCHECK_RET( !s_treeBeingSorted,
975 "wxTreeCtrl::SortChildren is not reentrant" );
976
977 wxArrayTreeItems& children = item->GetChildren();
978 if ( children.Count() > 1 )
979 {
980 s_treeBeingSorted = this;
981 children.Sort(tree_ctrl_compare_func);
982 s_treeBeingSorted = NULL;
983
984 m_dirty = TRUE;
985 }
986 //else: don't make the tree dirty as nothing changed
987 }
988
989 wxImageList *wxTreeCtrl::GetImageList() const
990 {
991 return m_imageListNormal;
992 }
993
994 wxImageList *wxTreeCtrl::GetStateImageList() const
995 {
996 return m_imageListState;
997 }
998
999 void wxTreeCtrl::SetImageList(wxImageList *imageList)
1000 {
1001 m_imageListNormal = imageList;
1002 }
1003
1004 void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
1005 {
1006 m_imageListState = imageList;
1007 }
1008
1009 // -----------------------------------------------------------------------------
1010 // helpers
1011 // -----------------------------------------------------------------------------
1012
1013 void wxTreeCtrl::AdjustMyScrollbars()
1014 {
1015 if (m_anchor)
1016 {
1017 int x = 0;
1018 int y = 0;
1019 m_anchor->GetSize( x, y );
1020 y += 2*m_lineHeight;
1021 int x_pos = GetScrollPos( wxHORIZONTAL );
1022 int y_pos = GetScrollPos( wxVERTICAL );
1023 SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos );
1024 }
1025 else
1026 {
1027 SetScrollbars( 0, 0, 0, 0 );
1028 }
1029 }
1030
1031 void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1032 {
1033 // render bold items in bold
1034 wxFont fontOld;
1035 wxFont fontNew;
1036
1037 if ( item->IsBold() )
1038 {
1039 fontOld = dc.GetFont();
1040 if (fontOld.Ok())
1041 {
1042 // @@ is there any better way to make a bold variant of old font?
1043 fontNew = wxFont( fontOld.GetPointSize(),
1044 fontOld.GetFamily(),
1045 fontOld.GetStyle(),
1046 wxBOLD,
1047 fontOld.GetUnderlined());
1048 dc.SetFont(fontNew);
1049 }
1050 else
1051 {
1052 wxFAIL_MSG("wxDC::GetFont() failed!");
1053 }
1054 }
1055
1056 long text_w = 0;
1057 long text_h = 0;
1058 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
1059
1060 int image_h = 0;
1061 int image_w = 0;
1062 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1063 {
1064 m_imageListNormal->GetSize( item->GetSelectedImage(), image_w, image_h );
1065 image_w += 4;
1066 }
1067 else if (item->GetImage() != -1)
1068 {
1069 m_imageListNormal->GetSize( item->GetImage(), image_w, image_h );
1070 image_w += 4;
1071 }
1072
1073 dc.DrawRectangle( item->GetX()-2, item->GetY()-2, image_w+text_w+4, text_h+4 );
1074
1075 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1076 {
1077 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, text_h );
1078 m_imageListNormal->Draw( item->GetSelectedImage(), dc,
1079 item->GetX(), item->GetY()-1,
1080 wxIMAGELIST_DRAW_TRANSPARENT );
1081 dc.DestroyClippingRegion();
1082 }
1083 else if (item->GetImage() != -1)
1084 {
1085 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, text_h );
1086 m_imageListNormal->Draw( item->GetImage(), dc,
1087 item->GetX(), item->GetY()-1,
1088 wxIMAGELIST_DRAW_TRANSPARENT );
1089 dc.DestroyClippingRegion();
1090 }
1091
1092 dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY() );
1093
1094 // restore normal font for bold items
1095 if (fontOld.Ok())
1096 {
1097 dc.SetFont( fontOld);
1098 }
1099 }
1100
1101 void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
1102 {
1103 int horizX = level*m_indent;
1104
1105 item->SetX( horizX+33 );
1106 item->SetY( y-m_lineHeight/3 );
1107 item->SetHeight( m_lineHeight );
1108
1109 item->SetCross( horizX+15, y );
1110
1111 int oldY = y;
1112
1113 int exposed_x = dc.LogicalToDeviceX( 0 );
1114 int exposed_y = dc.LogicalToDeviceY( item->GetY()-2 );
1115
1116 if (IsExposed( exposed_x, exposed_y, 10000, m_lineHeight+4 )) // 10000 = very much
1117 {
1118 int startX = horizX;
1119 int endX = horizX + 10;
1120
1121 if (!item->HasChildren()) endX += 20;
1122
1123 dc.DrawLine( startX, y, endX, y );
1124
1125 if (item->HasPlus())
1126 {
1127 dc.DrawLine( horizX+20, y, horizX+30, y );
1128 dc.SetPen( *wxGREY_PEN );
1129 dc.SetBrush( *wxWHITE_BRUSH );
1130 dc.DrawRectangle( horizX+10, y-4, 11, 9 );
1131 dc.SetPen( *wxBLACK_PEN );
1132 dc.DrawLine( horizX+13, y, horizX+18, y );
1133
1134 if (!item->IsExpanded())
1135 dc.DrawLine( horizX+15, y-2, horizX+15, y+3 );
1136 }
1137
1138 if (item->HasHilight())
1139 {
1140 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
1141
1142 dc.SetBrush( *m_hilightBrush );
1143
1144 if (m_hasFocus)
1145 dc.SetPen( *wxBLACK_PEN );
1146 else
1147 dc.SetPen( *wxTRANSPARENT_PEN );
1148
1149 PaintItem(item, dc);
1150
1151 dc.SetPen( *wxBLACK_PEN );
1152 dc.SetTextForeground( *wxBLACK );
1153 dc.SetBrush( *wxWHITE_BRUSH );
1154 }
1155 else
1156 {
1157 dc.SetBrush( *wxWHITE_BRUSH );
1158 dc.SetPen( *wxTRANSPARENT_PEN );
1159
1160 PaintItem(item, dc);
1161
1162 dc.SetPen( *wxBLACK_PEN );
1163 }
1164 }
1165
1166 if ( item->IsExpanded() )
1167 {
1168 int semiOldY = y;
1169
1170 wxArrayTreeItems& children = item->GetChildren();
1171 size_t count = children.Count();
1172 for ( size_t n = 0; n < count; n++ )
1173 {
1174 y += m_lineHeight;
1175 semiOldY = y;
1176
1177 PaintLevel( children[n], dc, level+1, y );
1178 }
1179
1180 // it may happen that the item is expanded but has no items (when you
1181 // delete all its children for example) - don't draw the vertical line
1182 // in this case
1183 if ( count > 0 )
1184 dc.DrawLine( horizX+15, oldY+5, horizX+15, semiOldY );
1185 }
1186 }
1187
1188 // -----------------------------------------------------------------------------
1189 // wxWindows callbacks
1190 // -----------------------------------------------------------------------------
1191
1192 void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
1193 {
1194 if ( !m_anchor )
1195 return;
1196
1197 wxPaintDC dc(this);
1198 PrepareDC( dc );
1199
1200 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
1201
1202 dc.SetPen( m_dottedPen );
1203 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1204
1205 int y = m_lineHeight / 2 + 2;
1206 PaintLevel( m_anchor, dc, 0, y );
1207 }
1208
1209 void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1210 {
1211 m_hasFocus = TRUE;
1212 if ( m_current )
1213 RefreshLine( m_current );
1214 }
1215
1216 void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1217 {
1218 m_hasFocus = FALSE;
1219 if ( m_current )
1220 RefreshLine( m_current );
1221 }
1222
1223 void wxTreeCtrl::OnChar( wxKeyEvent &event )
1224 {
1225 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
1226 te.m_code = event.KeyCode();
1227 te.SetEventObject( this );
1228 GetEventHandler()->ProcessEvent( te );
1229
1230 if (m_current == 0)
1231 {
1232 event.Skip();
1233 return;
1234 }
1235
1236 switch (event.KeyCode())
1237 {
1238 case '+':
1239 case WXK_ADD:
1240 if (m_current->HasPlus() && !IsExpanded(m_current))
1241 {
1242 Expand(m_current);
1243 }
1244 break;
1245
1246 case '-':
1247 case WXK_SUBTRACT:
1248 if (IsExpanded(m_current))
1249 {
1250 Collapse(m_current);
1251 }
1252 break;
1253
1254 case '*':
1255 case WXK_MULTIPLY:
1256 Toggle(m_current);
1257 break;
1258
1259 case ' ':
1260 case WXK_RETURN:
1261 {
1262 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1263 event.m_item = m_current;
1264 event.m_code = 0;
1265 event.SetEventObject( this );
1266 GetEventHandler()->ProcessEvent( event );
1267 }
1268 break;
1269
1270 case WXK_UP:
1271 {
1272 wxTreeItemId prev = GetPrevSibling( m_current );
1273 if (!prev)
1274 {
1275 prev = GetParent( m_current );
1276 long cockie = 0;
1277 wxTreeItemId current = m_current;
1278 if (current == GetFirstChild( prev, cockie ))
1279 {
1280 // otherwise we return to where we came from
1281 SelectItem( prev );
1282 EnsureVisible( prev );
1283 break;
1284 }
1285 }
1286 if (prev)
1287 {
1288 while (IsExpanded(prev))
1289 {
1290 int c = (int)GetChildrenCount( prev, FALSE );
1291 long cockie = 0;
1292 prev = GetFirstChild( prev, cockie );
1293 for (int i = 0; i < c-1; i++)
1294 prev = GetNextSibling( prev );
1295 }
1296 SelectItem( prev );
1297 EnsureVisible( prev );
1298 }
1299 }
1300 break;
1301 case WXK_LEFT:
1302 {
1303 wxTreeItemId prev = GetPrevSibling( m_current );
1304 if (prev != 0)
1305 {
1306 SelectItem( prev );
1307 EnsureVisible( prev );
1308 }
1309 else
1310 {
1311 prev = GetParent( m_current );
1312 if (prev)
1313 {
1314 EnsureVisible( prev );
1315 SelectItem( prev );
1316 }
1317 }
1318 }
1319 break;
1320
1321 case WXK_RIGHT:
1322 // this works the same as the down arrow except that we also expand the
1323 // item if it wasn't expanded yet
1324 Expand(m_current);
1325 // fall through
1326
1327 case WXK_DOWN:
1328 {
1329 if (IsExpanded(m_current))
1330 {
1331 long cookie = 0;
1332 wxTreeItemId child = GetFirstChild( m_current, cookie );
1333 SelectItem( child );
1334 EnsureVisible( child );
1335 }
1336 else
1337 {
1338 wxTreeItemId next = GetNextSibling( m_current );
1339 if (next == 0)
1340 {
1341 wxTreeItemId current = m_current;
1342 while (current && !next)
1343 {
1344 current = GetParent( current );
1345 if (current) next = GetNextSibling( current );
1346 }
1347 }
1348 if (next != 0)
1349 {
1350 SelectItem( next );
1351 EnsureVisible( next );
1352 }
1353 }
1354 }
1355 break;
1356
1357 default:
1358 event.Skip();
1359 }
1360 }
1361
1362 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& WXUNUSED(flags))
1363 {
1364 bool onButton = FALSE;
1365 return m_anchor->HitTest( point, onButton );
1366 }
1367
1368 void wxTreeCtrl::OnMouse( wxMouseEvent &event )
1369 {
1370 if ( !(event.LeftDown() || event.LeftDClick()) )
1371 return;
1372
1373 if ( !m_anchor )
1374 return;
1375
1376 wxClientDC dc(this);
1377 PrepareDC(dc);
1378 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1379 long y = dc.DeviceToLogicalY( (long)event.GetY() );
1380
1381 bool onButton = FALSE;
1382 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), onButton );
1383 if ( item == NULL )
1384 return;
1385
1386 if (!IsSelected(item)) SelectItem(item);
1387
1388 if ( event.LeftDClick() )
1389 {
1390 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1391 event.m_item = item;
1392 event.m_code = 0;
1393 event.SetEventObject( this );
1394 GetEventHandler()->ProcessEvent( event );
1395 }
1396
1397 if ( onButton )
1398 {
1399 Toggle( item );
1400 }
1401 }
1402
1403 void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
1404 {
1405 if (!m_dirty) return;
1406
1407 m_dirty = FALSE;
1408
1409 CalculatePositions();
1410
1411 AdjustMyScrollbars();
1412 }
1413
1414 // -----------------------------------------------------------------------------
1415 // -----------------------------------------------------------------------------
1416 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item,
1417 wxDC &dc,
1418 int level,
1419 int &y )
1420 {
1421 int horizX = level*m_indent;
1422
1423 item->SetX( horizX+33 );
1424 item->SetY( y-m_lineHeight/3-2 );
1425 item->SetHeight( m_lineHeight );
1426
1427 // if ( item->IsExpanded() )
1428 // return;
1429 if ( !item->IsExpanded() ) // Surely this is correct? JACS
1430 return;
1431
1432 wxArrayTreeItems& children = item->GetChildren();
1433 size_t count = children.Count();
1434 for ( size_t n = 0; n < count; n++ )
1435 {
1436 y += m_lineHeight;
1437 CalculateLevel( children[n], dc, level+1, y );
1438 }
1439 }
1440
1441 void wxTreeCtrl::CalculatePositions()
1442 {
1443 if ( !m_anchor )
1444 return;
1445
1446 wxClientDC dc(this);
1447 PrepareDC( dc );
1448
1449 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
1450
1451 dc.SetPen( m_dottedPen );
1452 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1453
1454 int y = m_lineHeight / 2 + 2;
1455 CalculateLevel( m_anchor, dc, 0, y );
1456 }
1457
1458 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
1459 {
1460 wxClientDC dc(this);
1461 PrepareDC(dc);
1462
1463 int cw = 0;
1464 int ch = 0;
1465 GetClientSize( &cw, &ch );
1466
1467 wxRect rect;
1468 rect.x = dc.LogicalToDeviceX( 0 );
1469 rect.width = cw;
1470 rect.y = dc.LogicalToDeviceY( item->GetY() );
1471 rect.height = ch;
1472
1473 Refresh( TRUE, &rect );
1474
1475 AdjustMyScrollbars();
1476 }
1477
1478 void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
1479 {
1480 wxClientDC dc(this);
1481 PrepareDC( dc );
1482
1483 wxRect rect;
1484 rect.x = dc.LogicalToDeviceX( item->GetX() - 2 );
1485 rect.y = dc.LogicalToDeviceY( item->GetY() - 2 );
1486 rect.width = 1000;
1487 rect.height = dc.GetCharHeight() + 6;
1488 Refresh( TRUE, &rect );
1489 }
1490