1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/treectlg.cpp
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/listbox.h"
36 #include "wx/textctrl.h"
39 #include "wx/generic/treectlg.h"
40 #include "wx/imaglist.h"
42 #include "wx/renderer.h"
45 #include "wx/mac/private.h"
49 #include "wx/gtk/private.h"
50 #include "wx/gtk/win_gtk.h"
53 // -----------------------------------------------------------------------------
55 // -----------------------------------------------------------------------------
57 class WXDLLEXPORT wxGenericTreeItem
;
59 WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 static const int NO_IMAGE
= -1;
67 static const int PIXELS_PER_UNIT
= 10;
69 // the margin between the item image and the item text
70 static const int MARGIN_BETWEEN_IMAGE_AND_TEXT
= 4;
72 // -----------------------------------------------------------------------------
74 // -----------------------------------------------------------------------------
76 // timer used for enabling in-place edit
77 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
80 // start editing the current item after half a second (if the mouse hasn't
81 // been clicked/moved)
84 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
86 virtual void Notify();
89 wxGenericTreeCtrl
*m_owner
;
91 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
94 // control used for in-place edit
95 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
98 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
100 void EndEdit(bool discardChanges
= false)
102 if ( discardChanges
)
108 m_aboutToFinish
= true;
110 // Notify the owner about the changes
113 // Even if vetoed, close the control (consistent with MSW)
121 m_owner
->OnRenameCancelled(m_itemEdited
);
123 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
126 void OnChar( wxKeyEvent
&event
);
127 void OnKeyUp( wxKeyEvent
&event
);
128 void OnKillFocus( wxFocusEvent
&event
);
130 bool AcceptChanges();
134 wxGenericTreeCtrl
*m_owner
;
135 wxGenericTreeItem
*m_itemEdited
;
136 wxString m_startValue
;
138 bool m_aboutToFinish
;
140 DECLARE_EVENT_TABLE()
141 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
144 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
145 // for a sufficiently long time
146 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
149 // reset the current prefix after half a second of inactivity
150 enum { DELAY
= 500 };
152 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
154 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
157 wxGenericTreeCtrl
*m_owner
;
159 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
163 class WXDLLEXPORT wxGenericTreeItem
167 wxGenericTreeItem() { m_data
= NULL
; }
168 wxGenericTreeItem( wxGenericTreeItem
*parent
,
169 const wxString
& text
,
172 wxTreeItemData
*data
);
174 ~wxGenericTreeItem();
177 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
179 const wxString
& GetText() const { return m_text
; }
180 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
181 { return m_images
[which
]; }
182 wxTreeItemData
*GetData() const { return m_data
; }
184 // returns the current image for the item (depending on its
185 // selected/expanded/whatever state)
186 int GetCurrentImage() const;
188 void SetText( const wxString
&text
);
189 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
190 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
192 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
194 void SetBold(bool bold
) { m_isBold
= bold
; }
196 int GetX() const { return m_x
; }
197 int GetY() const { return m_y
; }
199 void SetX(int x
) { m_x
= x
; }
200 void SetY(int y
) { m_y
= y
; }
202 int GetHeight() const { return m_height
; }
203 int GetWidth() const { return m_width
; }
205 void SetHeight(int h
) { m_height
= h
; }
206 void SetWidth(int w
) { m_width
= w
; }
208 wxGenericTreeItem
*GetParent() const { return m_parent
; }
212 // deletes all children notifying the treectrl about it
213 void DeleteChildren(wxGenericTreeCtrl
*tree
);
215 // get count of all children (and grand children if 'recursively')
216 size_t GetChildrenCount(bool recursively
= true) const;
218 void Insert(wxGenericTreeItem
*child
, size_t index
)
219 { m_children
.Insert(child
, index
); }
221 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
223 // return the item at given position (or NULL if no item), onButton is
224 // true if the point belongs to the item's button, otherwise it lies
225 // on the item's label
226 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
227 const wxGenericTreeCtrl
*,
231 void Expand() { m_isCollapsed
= false; }
232 void Collapse() { m_isCollapsed
= true; }
234 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
237 bool HasChildren() const { return !m_children
.IsEmpty(); }
238 bool IsSelected() const { return m_hasHilight
!= 0; }
239 bool IsExpanded() const { return !m_isCollapsed
; }
240 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
241 bool IsBold() const { return m_isBold
!= 0; }
244 // get them - may be NULL
245 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
246 // get them ensuring that the pointer is not NULL
247 wxTreeItemAttr
& Attr()
251 m_attr
= new wxTreeItemAttr
;
257 void SetAttributes(wxTreeItemAttr
*attr
)
259 if ( m_ownsAttr
) delete m_attr
;
263 // set them and delete when done
264 void AssignAttributes(wxTreeItemAttr
*attr
)
271 // since there can be very many of these, we save size by chosing
272 // the smallest representation for the elements and by ordering
273 // the members to avoid padding.
274 wxString m_text
; // label to be rendered for item
276 wxTreeItemData
*m_data
; // user-provided data
278 wxArrayGenericTreeItems m_children
; // list of children
279 wxGenericTreeItem
*m_parent
; // parent of this item
281 wxTreeItemAttr
*m_attr
; // attributes???
283 // tree ctrl images for the normal, selected, expanded and
284 // expanded+selected states
285 int m_images
[wxTreeItemIcon_Max
];
287 wxCoord m_x
; // (virtual) offset from top
288 wxCoord m_y
; // (virtual) offset from left
289 int m_width
; // width of this item
290 int m_height
; // height of this item
292 // use bitfields to save size
293 unsigned int m_isCollapsed
:1;
294 unsigned int m_hasHilight
:1; // same as focused
295 unsigned int m_hasPlus
:1; // used for item which doesn't have
296 // children but has a [+] button
297 unsigned int m_isBold
:1; // render the label in bold font
298 unsigned int m_ownsAttr
:1; // delete attribute when done
300 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
303 // =============================================================================
305 // =============================================================================
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 // translate the key or mouse event flags to the type of selection we're
313 static void EventFlagsToSelType(long style
,
317 bool &extended_select
,
318 bool &unselect_others
)
320 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
321 extended_select
= shiftDown
&& is_multiple
;
322 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
325 // check if the given item is under another one
326 static bool IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
330 if ( item
== parent
)
332 // item is a descendant of parent
336 item
= item
->GetParent();
342 // -----------------------------------------------------------------------------
343 // wxTreeRenameTimer (internal)
344 // -----------------------------------------------------------------------------
346 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
351 void wxTreeRenameTimer::Notify()
353 m_owner
->OnRenameTimer();
356 //-----------------------------------------------------------------------------
357 // wxTreeTextCtrl (internal)
358 //-----------------------------------------------------------------------------
360 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
361 EVT_CHAR (wxTreeTextCtrl::OnChar
)
362 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
363 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
366 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
367 wxGenericTreeItem
*item
)
368 : m_itemEdited(item
), m_startValue(item
->GetText())
372 m_aboutToFinish
= false;
374 int w
= m_itemEdited
->GetWidth(),
375 h
= m_itemEdited
->GetHeight();
378 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
383 int image
= item
->GetCurrentImage();
384 if ( image
!= NO_IMAGE
)
386 if ( m_owner
->m_imageListNormal
)
388 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
389 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
393 wxFAIL_MSG(_T("you must create an image list to use images!"));
397 // FIXME: what are all these hardcoded 4, 8 and 11s really?
401 wxSize bs
= DoGetBestSize() ;
402 // edit control height
405 int diff
= h
- ( bs
.y
- 8 ) ;
411 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
412 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
415 bool wxTreeTextCtrl::AcceptChanges()
417 const wxString value
= GetValue();
419 if ( value
== m_startValue
)
421 // nothing changed, always accept
422 // when an item remains unchanged, the owner
423 // needs to be notified that the user decided
424 // not to change the tree item label, and that
425 // the edit has been cancelled
427 m_owner
->OnRenameCancelled(m_itemEdited
);
431 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
433 // vetoed by the user
437 // accepted, do rename the item
438 m_owner
->SetItemText(m_itemEdited
, value
);
443 void wxTreeTextCtrl::Finish()
447 m_owner
->ResetTextControl();
449 wxPendingDelete
.Append(this);
457 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
459 switch ( event
.m_keyCode
)
474 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
478 // auto-grow the textctrl:
479 wxSize parentSize
= m_owner
->GetSize();
480 wxPoint myPos
= GetPosition();
481 wxSize mySize
= GetSize();
483 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
484 if (myPos
.x
+ sx
> parentSize
.x
)
485 sx
= parentSize
.x
- myPos
.x
;
488 SetSize(sx
, wxDefaultCoord
);
494 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
496 if ( !m_finished
&& !m_aboutToFinish
)
498 // We must finish regardless of success, otherwise we'll get
502 if ( !AcceptChanges() )
503 m_owner
->OnRenameCancelled( m_itemEdited
);
506 // We must let the native text control handle focus, too, otherwise
507 // it could have problems with the cursor (e.g., in wxGTK).
511 // -----------------------------------------------------------------------------
513 // -----------------------------------------------------------------------------
515 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
516 const wxString
& text
,
517 int image
, int selImage
,
518 wxTreeItemData
*data
)
521 m_images
[wxTreeItemIcon_Normal
] = image
;
522 m_images
[wxTreeItemIcon_Selected
] = selImage
;
523 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
524 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
529 m_isCollapsed
= true;
530 m_hasHilight
= false;
536 m_attr
= (wxTreeItemAttr
*)NULL
;
539 // We don't know the height here yet.
544 wxGenericTreeItem::~wxGenericTreeItem()
548 if (m_ownsAttr
) delete m_attr
;
550 wxASSERT_MSG( m_children
.IsEmpty(),
551 wxT("please call DeleteChildren() before deleting the item") );
554 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
556 size_t count
= m_children
.Count();
557 for ( size_t n
= 0; n
< count
; n
++ )
559 wxGenericTreeItem
*child
= m_children
[n
];
560 tree
->SendDeleteEvent(child
);
562 child
->DeleteChildren(tree
);
563 if ( child
== tree
->m_select_me
)
564 tree
->m_select_me
= NULL
;
571 void wxGenericTreeItem::SetText( const wxString
&text
)
576 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
578 size_t count
= m_children
.Count();
582 size_t total
= count
;
583 for (size_t n
= 0; n
< count
; ++n
)
585 total
+= m_children
[n
]->GetChildrenCount();
591 void wxGenericTreeItem::GetSize( int &x
, int &y
,
592 const wxGenericTreeCtrl
*theButton
)
594 int bottomY
=m_y
+theButton
->GetLineHeight(this);
595 if ( y
< bottomY
) y
= bottomY
;
596 int width
= m_x
+ m_width
;
597 if ( x
< width
) x
= width
;
601 size_t count
= m_children
.Count();
602 for ( size_t n
= 0; n
< count
; ++n
)
604 m_children
[n
]->GetSize( x
, y
, theButton
);
609 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
610 const wxGenericTreeCtrl
*theCtrl
,
614 // for a hidden root node, don't evaluate it, but do evaluate children
615 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
618 int h
= theCtrl
->GetLineHeight(this);
619 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
621 int y_mid
= m_y
+ h
/2;
622 if (point
.y
< y_mid
)
623 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
625 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
627 int xCross
= m_x
- theCtrl
->GetSpacing();
629 // according to the drawing code the triangels are drawn
630 // at -4 , -4 from the position up to +10/+10 max
631 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
632 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
633 HasPlus() && theCtrl
->HasButtons() )
635 // 5 is the size of the plus sign
636 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
637 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
638 HasPlus() && theCtrl
->HasButtons() )
641 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
645 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
650 // assuming every image (normal and selected) has the same size!
651 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
652 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
655 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
656 flags
|= wxTREE_HITTEST_ONITEMICON
;
658 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
664 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
665 if (point
.x
> m_x
+m_width
)
666 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
671 // if children are expanded, fall through to evaluate them
672 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
676 size_t count
= m_children
.Count();
677 for ( size_t n
= 0; n
< count
; n
++ )
679 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
687 return (wxGenericTreeItem
*) NULL
;
690 int wxGenericTreeItem::GetCurrentImage() const
692 int image
= NO_IMAGE
;
697 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
700 if ( image
== NO_IMAGE
)
702 // we usually fall back to the normal item, but try just the
703 // expanded one (and not selected) first in this case
704 image
= GetImage(wxTreeItemIcon_Expanded
);
710 image
= GetImage(wxTreeItemIcon_Selected
);
713 // maybe it doesn't have the specific image we want,
714 // try the default one instead
715 if ( image
== NO_IMAGE
) image
= GetImage();
720 // -----------------------------------------------------------------------------
721 // wxGenericTreeCtrl implementation
722 // -----------------------------------------------------------------------------
724 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
726 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
727 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
728 EVT_SIZE (wxGenericTreeCtrl::OnSize
)
729 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
730 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
731 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
732 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
733 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
736 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
738 * wxTreeCtrl has to be a real class or we have problems with
739 * the run-time information.
742 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
745 // -----------------------------------------------------------------------------
746 // construction/destruction
747 // -----------------------------------------------------------------------------
749 void wxGenericTreeCtrl::Init()
754 m_select_me
= (wxGenericTreeItem
*) NULL
;
763 // OS X sel item highlight color differs from text highlight color, which is
764 // what wxSYS_COLOUR_HIGHLIGHT returns.
766 GetThemeBrushAsColor(kThemeBrushAlternatePrimaryHighlightColor
, 32, true, &hilight
);
767 m_hilightBrush
= new wxBrush( wxColour(hilight
.red
, hilight
.green
, hilight
.blue
), wxSOLID
);
769 m_hilightBrush
= new wxBrush
771 wxSystemSettings::GetColour
773 wxSYS_COLOUR_HIGHLIGHT
780 // on Mac, this color also differs from the wxSYS_COLOUR_BTNSHADOW, enough to be noticable.
781 // I don't know if BTNSHADOW is appropriate in other contexts, so I'm just changing it here.
782 GetThemeBrushAsColor(kThemeBrushSecondaryHighlightColor
, 32, true, &hilight
);
783 m_hilightUnfocusedBrush
= new wxBrush( wxColour(hilight
.red
, hilight
.green
, hilight
.blue
), wxSOLID
);
785 m_hilightUnfocusedBrush
= new wxBrush
787 wxSystemSettings::GetColour
789 wxSYS_COLOUR_BTNSHADOW
794 m_imageListButtons
= NULL
;
795 m_ownsImageListButtons
= false;
798 m_isDragging
= false;
799 m_dropTarget
= m_oldSelection
= NULL
;
803 m_renameTimer
= NULL
;
808 m_dropEffectAboveItem
= false;
810 m_lastOnSame
= false;
812 #ifdef __WXMAC_CARBON__
813 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
815 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
817 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
818 m_normalFont
.GetFamily(),
819 m_normalFont
.GetStyle(),
821 m_normalFont
.GetUnderlined(),
822 m_normalFont
.GetFaceName(),
823 m_normalFont
.GetEncoding());
826 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
831 const wxValidator
& validator
,
832 const wxString
& name
)
836 wxGetOsVersion( &major
, &minor
);
838 style
&= ~wxTR_LINES_AT_ROOT
;
839 style
|= wxTR_NO_LINES
;
841 style
|= wxTR_ROW_LINES
;
843 if (style
== 0 || style
& wxTR_DEFAULT_STYLE
)
844 style
|= wxTR_FULL_ROW_HIGHLIGHT
;
848 style
|= wxTR_NO_LINES
;
851 if ( !wxControl::Create( parent
, id
, pos
, size
,
852 style
|wxHSCROLL
|wxVSCROLL
,
857 // If the tree display has no buttons, but does have
858 // connecting lines, we can use a narrower layout.
859 // It may not be a good idea to force this...
860 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
866 wxVisualAttributes attr
= GetDefaultAttributes();
867 SetOwnForegroundColour( attr
.colFg
);
868 SetOwnBackgroundColour( attr
.colBg
);
870 SetOwnFont(attr
.font
);
872 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
874 SetInitialSize(size
);
879 wxGenericTreeCtrl::~wxGenericTreeCtrl()
881 delete m_hilightBrush
;
882 delete m_hilightUnfocusedBrush
;
886 delete m_renameTimer
;
889 if (m_ownsImageListButtons
)
890 delete m_imageListButtons
;
893 // -----------------------------------------------------------------------------
895 // -----------------------------------------------------------------------------
897 unsigned int wxGenericTreeCtrl::GetCount() const
905 unsigned int count
= m_anchor
->GetChildrenCount();
906 if ( !HasFlag(wxTR_HIDE_ROOT
) )
908 // take the root itself into account
915 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
917 m_indent
= (unsigned short) indent
;
922 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
923 bool recursively
) const
925 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
927 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
930 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
932 // Do not try to expand the root node if it hasn't been created yet
933 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
935 // if we will hide the root, make sure children are visible
936 m_anchor
->SetHasPlus();
938 CalculatePositions();
941 // right now, just sets the styles. Eventually, we may
942 // want to update the inherited styles, but right now
943 // none of the parents has updatable styles
944 m_windowStyle
= styles
;
948 // -----------------------------------------------------------------------------
949 // functions to work with tree items
950 // -----------------------------------------------------------------------------
952 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
954 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
956 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
959 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
960 wxTreeItemIcon which
) const
962 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
964 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
967 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
969 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
971 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
974 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
976 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
978 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
979 return pItem
->Attr().GetTextColour();
982 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
984 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
986 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
987 return pItem
->Attr().GetBackgroundColour();
990 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
992 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
994 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
995 return pItem
->Attr().GetFont();
998 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
1000 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1002 wxClientDC
dc(this);
1003 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1004 pItem
->SetText(text
);
1005 CalculateSize(pItem
, dc
);
1009 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
1011 wxTreeItemIcon which
)
1013 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1015 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1016 pItem
->SetImage(image
, which
);
1018 wxClientDC
dc(this);
1019 CalculateSize(pItem
, dc
);
1023 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1025 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1028 data
->SetId( item
);
1030 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1033 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1035 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1037 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1038 pItem
->SetHasPlus(has
);
1042 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1044 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1046 // avoid redrawing the tree if no real change
1047 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1048 if ( pItem
->IsBold() != bold
)
1050 pItem
->SetBold(bold
);
1052 // recalculate the item size as bold and non bold fonts have different
1054 wxClientDC
dc(this);
1055 CalculateSize(pItem
, dc
);
1061 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1064 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1070 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1071 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1074 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1075 pItem
->Attr().SetTextColour(fg
);
1076 pItem
->Attr().SetBackgroundColour(bg
);
1080 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1081 const wxColour
& col
)
1083 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1085 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1086 pItem
->Attr().SetTextColour(col
);
1090 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1091 const wxColour
& col
)
1093 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1095 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1096 pItem
->Attr().SetBackgroundColour(col
);
1100 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1102 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1104 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1105 pItem
->Attr().SetFont(font
);
1109 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1111 wxTreeCtrlBase::SetFont(font
);
1113 m_normalFont
= font
;
1114 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1115 m_normalFont
.GetFamily(),
1116 m_normalFont
.GetStyle(),
1118 m_normalFont
.GetUnderlined(),
1119 m_normalFont
.GetFaceName(),
1120 m_normalFont
.GetEncoding());
1126 // -----------------------------------------------------------------------------
1127 // item status inquiries
1128 // -----------------------------------------------------------------------------
1130 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1132 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1134 // An item is only visible if it's not a descendant of a collapsed item
1135 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1136 wxGenericTreeItem
* parent
= pItem
->GetParent();
1139 if (!parent
->IsExpanded())
1141 parent
= parent
->GetParent();
1145 GetViewStart(& startX
, & startY
);
1147 wxSize clientSize
= GetClientSize();
1150 if (!GetBoundingRect(item
, rect
))
1152 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1154 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1156 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1162 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1164 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1166 // consider that the item does have children if it has the "+" button: it
1167 // might not have them (if it had never been expanded yet) but then it
1168 // could have them as well and it's better to err on this side rather than
1169 // disabling some operations which are restricted to the items with
1170 // children for an item which does have them
1171 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1174 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1176 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1178 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1181 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1183 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1185 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1188 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1190 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1192 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1195 // -----------------------------------------------------------------------------
1197 // -----------------------------------------------------------------------------
1199 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1201 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1203 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1206 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1207 wxTreeItemIdValue
& cookie
) const
1209 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1212 return GetNextChild(item
, cookie
);
1215 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1216 wxTreeItemIdValue
& cookie
) const
1218 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1220 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1222 // it's ok to cast cookie to size_t, we never have indices big enough to
1223 // overflow "void *"
1224 size_t *pIndex
= (size_t *)&cookie
;
1225 if ( *pIndex
< children
.Count() )
1227 return children
.Item((*pIndex
)++);
1231 // there are no more of them
1232 return wxTreeItemId();
1236 #if WXWIN_COMPATIBILITY_2_4
1238 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1241 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1244 return GetNextChild(item
, cookie
);
1247 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1250 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1252 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1253 if ( (size_t)cookie
< children
.Count() )
1255 return children
.Item((size_t)cookie
++);
1259 // there are no more of them
1260 return wxTreeItemId();
1264 #endif // WXWIN_COMPATIBILITY_2_4
1266 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1268 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1270 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1271 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1274 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1276 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1278 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1279 wxGenericTreeItem
*parent
= i
->GetParent();
1280 if ( parent
== NULL
)
1282 // root item doesn't have any siblings
1283 return wxTreeItemId();
1286 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1287 int index
= siblings
.Index(i
);
1288 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1290 size_t n
= (size_t)(index
+ 1);
1291 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1294 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1296 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1298 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1299 wxGenericTreeItem
*parent
= i
->GetParent();
1300 if ( parent
== NULL
)
1302 // root item doesn't have any siblings
1303 return wxTreeItemId();
1306 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1307 int index
= siblings
.Index(i
);
1308 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1310 return index
== 0 ? wxTreeItemId()
1311 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1314 // Only for internal use right now, but should probably be public
1315 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1317 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1319 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1321 // First see if there are any children.
1322 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1323 if (children
.GetCount() > 0)
1325 return children
.Item(0);
1329 // Try a sibling of this or ancestor instead
1330 wxTreeItemId p
= item
;
1331 wxTreeItemId toFind
;
1334 toFind
= GetNextSibling(p
);
1335 p
= GetItemParent(p
);
1336 } while (p
.IsOk() && !toFind
.IsOk());
1341 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1343 wxTreeItemId id
= GetRootItem();
1352 } while (id
.IsOk());
1354 return wxTreeItemId();
1357 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1359 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1361 wxTreeItemId id
= item
;
1364 while (id
= GetNext(id
), id
.IsOk())
1370 return wxTreeItemId();
1373 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1375 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1377 wxFAIL_MSG(wxT("not implemented"));
1379 return wxTreeItemId();
1382 // called by wxTextTreeCtrl when it marks itself for deletion
1383 void wxGenericTreeCtrl::ResetTextControl()
1388 // find the first item starting with the given prefix after the given item
1389 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1390 const wxString
& prefixOrig
) const
1392 // match is case insensitive as this is more convenient to the user: having
1393 // to press Shift-letter to go to the item starting with a capital letter
1394 // would be too bothersome
1395 wxString prefix
= prefixOrig
.Lower();
1397 // determine the starting point: we shouldn't take the current item (this
1398 // allows to switch between two items starting with the same letter just by
1399 // pressing it) but we shouldn't jump to the next one if the user is
1400 // continuing to type as otherwise he might easily skip the item he wanted
1401 wxTreeItemId id
= idParent
;
1402 if ( prefix
.length() == 1 )
1407 // look for the item starting with the given prefix after it
1408 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1413 // if we haven't found anything...
1416 // ... wrap to the beginning
1418 if ( HasFlag(wxTR_HIDE_ROOT
) )
1420 // can't select virtual root
1424 // and try all the items (stop when we get to the one we started from)
1425 while (id
.IsOk() && id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1429 // If we haven't found the item, id.IsOk() will be false, as per
1436 // -----------------------------------------------------------------------------
1438 // -----------------------------------------------------------------------------
1440 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1442 const wxString
& text
,
1445 wxTreeItemData
*data
)
1447 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1450 // should we give a warning here?
1451 return AddRoot(text
, image
, selImage
, data
);
1454 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1456 wxGenericTreeItem
*item
=
1457 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1461 data
->m_pItem
= item
;
1464 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1470 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1473 wxTreeItemData
*data
)
1475 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1477 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1479 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1480 image
, selImage
, data
);
1483 data
->m_pItem
= m_anchor
;
1486 if (HasFlag(wxTR_HIDE_ROOT
))
1488 // if root is hidden, make sure we can navigate
1490 m_anchor
->SetHasPlus();
1492 CalculatePositions();
1495 if (!HasFlag(wxTR_MULTIPLE
))
1497 m_current
= m_key_current
= m_anchor
;
1498 m_current
->SetHilight( true );
1504 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1505 const wxTreeItemId
& idPrevious
,
1506 const wxString
& text
,
1507 int image
, int selImage
,
1508 wxTreeItemData
*data
)
1510 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1513 // should we give a warning here?
1514 return AddRoot(text
, image
, selImage
, data
);
1518 if (idPrevious
.IsOk())
1520 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1521 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1522 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1525 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1529 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1531 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1532 ProcessEvent( event
);
1535 // Don't leave edit or selection on a child which is about to disappear
1536 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1538 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1539 m_textCtrl
->StopEditing();
1541 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1542 m_key_current
= NULL
;
1544 if (IsDescendantOf(item
, m_select_me
)) {
1547 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1548 m_current
->SetHilight( false );
1554 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1556 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1558 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1559 ChildrenClosing(item
);
1560 item
->DeleteChildren(this);
1563 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1565 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1567 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1569 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1571 // can't delete the item being edited, cancel editing it first
1572 m_textCtrl
->StopEditing();
1575 wxGenericTreeItem
*parent
= item
->GetParent();
1577 // don't keep stale pointers around!
1578 if ( IsDescendantOf(item
, m_key_current
) )
1580 // Don't silently change the selection:
1581 // do it properly in idle time, so event
1582 // handlers get called.
1584 // m_key_current = parent;
1585 m_key_current
= NULL
;
1588 // m_select_me records whether we need to select
1589 // a different item, in idle time.
1590 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1592 m_select_me
= parent
;
1595 if ( IsDescendantOf(item
, m_current
) )
1597 // Don't silently change the selection:
1598 // do it properly in idle time, so event
1599 // handlers get called.
1601 // m_current = parent;
1603 m_select_me
= parent
;
1606 // remove the item from the tree
1609 parent
->GetChildren().Remove( item
); // remove by value
1611 else // deleting the root
1613 // nothing will be left in the tree
1617 // and delete all of its children and the item itself now
1618 item
->DeleteChildren(this);
1619 SendDeleteEvent(item
);
1621 if (item
== m_select_me
)
1627 void wxGenericTreeCtrl::DeleteAllItems()
1635 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1637 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1639 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1640 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1641 _T("can't expand hidden root") );
1643 if ( !item
->HasPlus() )
1646 if ( item
->IsExpanded() )
1649 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1651 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1653 // cancelled by program
1658 CalculatePositions();
1660 RefreshSubtree(item
);
1662 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1663 ProcessEvent( event
);
1666 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1668 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1669 _T("can't collapse hidden root") );
1671 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1673 if ( !item
->IsExpanded() )
1676 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1677 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1679 // cancelled by program
1683 ChildrenClosing(item
);
1686 #if 0 // TODO why should items be collapsed recursively?
1687 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1688 size_t count
= children
.Count();
1689 for ( size_t n
= 0; n
< count
; n
++ )
1691 Collapse(children
[n
]);
1695 CalculatePositions();
1697 RefreshSubtree(item
);
1699 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1700 ProcessEvent( event
);
1703 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1706 DeleteChildren(item
);
1709 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1711 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1713 if (item
->IsExpanded())
1719 void wxGenericTreeCtrl::Unselect()
1723 m_current
->SetHilight( false );
1724 RefreshLine( m_current
);
1731 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1733 if (item
->IsSelected())
1735 item
->SetHilight(false);
1739 if (item
->HasChildren())
1741 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1742 size_t count
= children
.Count();
1743 for ( size_t n
= 0; n
< count
; ++n
)
1745 UnselectAllChildren(children
[n
]);
1750 void wxGenericTreeCtrl::UnselectAll()
1752 wxTreeItemId rootItem
= GetRootItem();
1754 // the tree might not have the root item at all
1757 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1761 // Recursive function !
1762 // To stop we must have crt_item<last_item
1764 // Tag all next children, when no more children,
1765 // Move to parent (not to tag)
1766 // Keep going... if we found last_item, we stop.
1767 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1769 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1771 if (parent
== NULL
) // This is root item
1772 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1774 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1775 int index
= children
.Index(crt_item
);
1776 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1778 size_t count
= children
.Count();
1779 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1781 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1784 return TagNextChildren(parent
, last_item
, select
);
1787 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1789 crt_item
->SetHilight(select
);
1790 RefreshLine(crt_item
);
1792 if (crt_item
==last_item
)
1795 if (crt_item
->HasChildren())
1797 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1798 size_t count
= children
.Count();
1799 for ( size_t n
= 0; n
< count
; ++n
)
1801 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1809 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1813 // item2 is not necessary after item1
1814 // choice first' and 'last' between item1 and item2
1815 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1816 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1818 bool select
= m_current
->IsSelected();
1820 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1823 TagNextChildren(first
,last
,select
);
1826 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1827 bool unselect_others
,
1828 bool extended_select
)
1830 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1834 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1835 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1837 //wxCHECK_RET( ( (!unselect_others) && is_single),
1838 // wxT("this is a single selection tree") );
1840 // to keep going anyhow !!!
1843 if (item
->IsSelected())
1844 return; // nothing to do
1845 unselect_others
= true;
1846 extended_select
= false;
1848 else if ( unselect_others
&& item
->IsSelected() )
1850 // selection change if there is more than one item currently selected
1851 wxArrayTreeItemIds selected_items
;
1852 if ( GetSelections(selected_items
) == 1 )
1856 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1857 event
.m_itemOld
= m_current
;
1858 // TODO : Here we don't send any selection mode yet !
1860 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1863 wxTreeItemId parent
= GetItemParent( itemId
);
1864 while (parent
.IsOk())
1866 if (!IsExpanded(parent
))
1869 parent
= GetItemParent( parent
);
1873 if (unselect_others
)
1875 if (is_single
) Unselect(); // to speed up thing
1880 if (extended_select
)
1884 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1887 // don't change the mark (m_current)
1888 SelectItemRange(m_current
, item
);
1892 bool select
= true; // the default
1894 // Check if we need to toggle hilight (ctrl mode)
1895 if (!unselect_others
)
1896 select
=!item
->IsSelected();
1898 m_current
= m_key_current
= item
;
1899 m_current
->SetHilight(select
);
1900 RefreshLine( m_current
);
1903 // This can cause idle processing to select the root
1904 // if no item is selected, so it must be after the
1906 EnsureVisible( itemId
);
1908 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1909 GetEventHandler()->ProcessEvent( event
);
1912 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1916 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1920 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1921 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1923 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1924 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1927 item
->SetHilight(false);
1930 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1931 GetEventHandler()->ProcessEvent( event
);
1935 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1936 wxArrayTreeItemIds
&array
) const
1938 if ( item
->IsSelected() )
1939 array
.Add(wxTreeItemId(item
));
1941 if ( item
->HasChildren() )
1943 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1944 size_t count
= children
.GetCount();
1945 for ( size_t n
= 0; n
< count
; ++n
)
1946 FillArray(children
[n
], array
);
1950 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1953 wxTreeItemId idRoot
= GetRootItem();
1954 if ( idRoot
.IsOk() )
1956 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1958 //else: the tree is empty, so no selections
1960 return array
.Count();
1963 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1965 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1967 if (!item
.IsOk()) return;
1969 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1971 // first expand all parent branches
1972 wxGenericTreeItem
*parent
= gitem
->GetParent();
1974 if ( HasFlag(wxTR_HIDE_ROOT
) )
1976 while ( parent
&& parent
!= m_anchor
)
1979 parent
= parent
->GetParent();
1987 parent
= parent
->GetParent();
1991 //if (parent) CalculatePositions();
1996 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1998 if (!item
.IsOk()) return;
2000 // We have to call this here because the label in
2001 // question might just have been added and no screen
2002 // update taken place.
2004 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2007 DoDirtyProcessing();
2009 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2011 // now scroll to the item
2012 int item_y
= gitem
->GetY();
2016 GetViewStart( &start_x
, &start_y
);
2017 start_y
*= PIXELS_PER_UNIT
;
2021 GetClientSize( &client_w
, &client_h
);
2023 if (item_y
< start_y
+3)
2028 m_anchor
->GetSize( x
, y
, this );
2029 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2030 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2031 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2032 // Item should appear at top
2033 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2035 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2040 m_anchor
->GetSize( x
, y
, this );
2041 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2042 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2043 item_y
+= PIXELS_PER_UNIT
+2;
2044 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2045 // Item should appear at bottom
2046 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, (item_y
+GetLineHeight(gitem
)-client_h
)/PIXELS_PER_UNIT
);
2050 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2051 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2053 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2054 wxGenericTreeItem
**item2
)
2056 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2058 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2061 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2063 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2065 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2067 wxCHECK_RET( !s_treeBeingSorted
,
2068 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2070 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2071 if ( children
.Count() > 1 )
2075 s_treeBeingSorted
= this;
2076 children
.Sort(tree_ctrl_compare_func
);
2077 s_treeBeingSorted
= NULL
;
2079 //else: don't make the tree dirty as nothing changed
2082 void wxGenericTreeCtrl::CalculateLineHeight()
2084 wxClientDC
dc(this);
2085 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2087 if ( m_imageListNormal
)
2089 // Calculate a m_lineHeight value from the normal Image sizes.
2090 // May be toggle off. Then wxGenericTreeCtrl will spread when
2091 // necessary (which might look ugly).
2092 int n
= m_imageListNormal
->GetImageCount();
2093 for (int i
= 0; i
< n
; i
++)
2095 int width
= 0, height
= 0;
2096 m_imageListNormal
->GetSize(i
, width
, height
);
2097 if (height
> m_lineHeight
) m_lineHeight
= height
;
2101 if (m_imageListButtons
)
2103 // Calculate a m_lineHeight value from the Button image sizes.
2104 // May be toggle off. Then wxGenericTreeCtrl will spread when
2105 // necessary (which might look ugly).
2106 int n
= m_imageListButtons
->GetImageCount();
2107 for (int i
= 0; i
< n
; i
++)
2109 int width
= 0, height
= 0;
2110 m_imageListButtons
->GetSize(i
, width
, height
);
2111 if (height
> m_lineHeight
) m_lineHeight
= height
;
2115 if (m_lineHeight
< 30)
2116 m_lineHeight
+= 2; // at least 2 pixels
2118 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2121 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2123 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2124 m_imageListNormal
= imageList
;
2125 m_ownsImageListNormal
= false;
2127 // Don't do any drawing if we're setting the list to NULL,
2128 // since we may be in the process of deleting the tree control.
2130 CalculateLineHeight();
2133 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2135 if (m_ownsImageListState
) delete m_imageListState
;
2136 m_imageListState
= imageList
;
2137 m_ownsImageListState
= false;
2140 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2142 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2143 m_imageListButtons
= imageList
;
2144 m_ownsImageListButtons
= false;
2146 CalculateLineHeight();
2149 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2151 SetButtonsImageList(imageList
);
2152 m_ownsImageListButtons
= true;
2155 // -----------------------------------------------------------------------------
2157 // -----------------------------------------------------------------------------
2159 void wxGenericTreeCtrl::AdjustMyScrollbars()
2164 m_anchor
->GetSize( x
, y
, this );
2165 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2166 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2167 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2168 int y_pos
= GetScrollPos( wxVERTICAL
);
2169 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2173 SetScrollbars( 0, 0, 0, 0 );
2177 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2179 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2180 return item
->GetHeight();
2182 return m_lineHeight
;
2185 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2187 // TODO implement "state" icon on items
2189 wxTreeItemAttr
*attr
= item
->GetAttributes();
2190 if ( attr
&& attr
->HasFont() )
2191 dc
.SetFont(attr
->GetFont());
2192 else if (item
->IsBold())
2193 dc
.SetFont(m_boldFont
);
2195 long text_w
= 0, text_h
= 0;
2196 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2198 int image_h
= 0, image_w
= 0;
2199 int image
= item
->GetCurrentImage();
2200 if ( image
!= NO_IMAGE
)
2202 if ( m_imageListNormal
)
2204 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2205 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2213 int total_h
= GetLineHeight(item
);
2214 bool drawItemBackground
= false;
2216 if ( item
->IsSelected() )
2218 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2219 drawItemBackground
= true;
2224 if ( attr
&& attr
->HasBackgroundColour() )
2226 drawItemBackground
= true;
2227 colBg
= attr
->GetBackgroundColour();
2231 colBg
= GetBackgroundColour();
2233 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2236 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2238 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2242 GetVirtualSize(&w
, &h
);
2243 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2245 dc
.DrawRectangle(rect
);
2247 if (!item
->IsSelected())
2249 dc
.DrawRectangle(rect
);
2253 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2255 gtk_paint_flat_box( m_widget
->style
,
2256 GTK_PIZZA(m_wxwindow
)->bin_window
,
2262 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2264 if ((item
== m_current
) && (m_hasFocus
))
2265 gtk_paint_focus( m_widget
->style
,
2266 GTK_PIZZA(m_wxwindow
)->bin_window
,
2271 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2277 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2279 // If it's selected, and there's an image, then we should
2280 // take care to leave the area under the image painted in the
2281 // background colour.
2282 wxRect
rect( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2283 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2285 dc
.DrawRectangle( rect
);
2287 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2291 gtk_paint_flat_box( m_widget
->style
,
2292 GTK_PIZZA(m_wxwindow
)->bin_window
,
2298 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2300 if ((item
== m_current
) && (m_hasFocus
))
2301 gtk_paint_focus( m_widget
->style
,
2302 GTK_PIZZA(m_wxwindow
)->bin_window
,
2307 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2311 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2312 // don't allow backgrounds to be customized. Not drawing the background,
2313 // except for custom item backgrounds, works for both kinds of theme.
2314 else if (drawItemBackground
)
2316 wxRect
rect( item
->GetX()-2, item
->GetY()+offset
,
2317 item
->GetWidth()+2, total_h
-offset
);
2319 dc
.DrawRectangle( rect
);
2321 if ( attr
&& attr
->HasBackgroundColour() )
2323 dc
.DrawRectangle( rect
);
2327 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2331 gtk_paint_flat_box( m_widget
->style
,
2332 GTK_PIZZA(m_wxwindow
)->bin_window
,
2338 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2340 if ((item
== m_current
) && (m_hasFocus
))
2341 gtk_paint_focus( m_widget
->style
,
2342 GTK_PIZZA(m_wxwindow
)->bin_window
,
2347 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2353 if ( image
!= NO_IMAGE
)
2355 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2356 m_imageListNormal
->Draw( image
, dc
,
2358 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2359 wxIMAGELIST_DRAW_TRANSPARENT
);
2360 dc
.DestroyClippingRegion();
2363 dc
.SetBackgroundMode(wxTRANSPARENT
);
2364 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2365 dc
.DrawText( item
->GetText(),
2366 (wxCoord
)(image_w
+ item
->GetX()),
2367 (wxCoord
)(item
->GetY() + extraH
));
2369 // restore normal font
2370 dc
.SetFont( m_normalFont
);
2373 // Now y stands for the top of the item, whereas it used to stand for middle !
2374 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2376 int x
= level
*m_indent
;
2377 if (!HasFlag(wxTR_HIDE_ROOT
))
2381 else if (level
== 0)
2383 // always expand hidden root
2385 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2386 int count
= children
.Count();
2392 PaintLevel(children
[n
], dc
, 1, y
);
2393 } while (++n
< count
);
2395 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2397 // draw line down to last child
2398 origY
+= GetLineHeight(children
[0])>>1;
2399 oldY
+= GetLineHeight(children
[n
-1])>>1;
2400 dc
.DrawLine(3, origY
, 3, oldY
);
2406 item
->SetX(x
+m_spacing
);
2409 int h
= GetLineHeight(item
);
2411 int y_mid
= y_top
+ (h
>>1);
2414 int exposed_x
= dc
.LogicalToDeviceX(0);
2415 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2417 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2421 // don't draw rect outline if we already have the
2422 // background color under Mac
2423 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2424 #endif // !__WXMAC__
2428 if ( item
->IsSelected()
2430 // On wxMac, if the tree doesn't have the focus we draw an empty
2431 // rectangle, so we want to make sure that the text is visible
2432 // against the normal background, not the highlightbackground, so
2433 // don't use the highlight text colour unless we have the focus.
2441 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2446 wxTreeItemAttr
*attr
= item
->GetAttributes();
2447 if (attr
&& attr
->HasTextColour())
2448 colText
= attr
->GetTextColour();
2450 colText
= GetForegroundColour();
2454 dc
.SetTextForeground(colText
);
2458 PaintItem(item
, dc
);
2460 if (HasFlag(wxTR_ROW_LINES
))
2462 // if the background colour is white, choose a
2463 // contrasting color for the lines
2464 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2465 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2466 dc
.DrawLine(0, y_top
, 10000, y_top
);
2467 dc
.DrawLine(0, y
, 10000, y
);
2470 // restore DC objects
2471 dc
.SetBrush(*wxWHITE_BRUSH
);
2472 dc
.SetPen(m_dottedPen
);
2473 dc
.SetTextForeground(*wxBLACK
);
2475 if ( !HasFlag(wxTR_NO_LINES
) )
2477 // draw the horizontal line here
2479 if (x
> (signed)m_indent
)
2480 x_start
-= m_indent
;
2481 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2483 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2486 // should the item show a button?
2487 if ( item
->HasPlus() && HasButtons() )
2489 if ( m_imageListButtons
)
2491 // draw the image button here
2494 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2495 : wxTreeItemIcon_Normal
;
2496 if ( item
->IsSelected() )
2497 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2499 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2500 int xx
= x
- image_w
/2;
2501 int yy
= y_mid
- image_h
/2;
2503 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2504 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2505 wxIMAGELIST_DRAW_TRANSPARENT
);
2507 else // no custom buttons
2509 static const int wImage
= 9;
2510 static const int hImage
= 9;
2513 if (item
->IsExpanded())
2514 flag
|= wxCONTROL_EXPANDED
;
2515 if (item
== m_underMouse
)
2516 flag
|= wxCONTROL_CURRENT
;
2518 wxRendererNative::Get().DrawTreeItemButton
2522 wxRect(x
- wImage
/2,
2531 if (item
->IsExpanded())
2533 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2534 int count
= children
.Count();
2541 PaintLevel(children
[n
], dc
, level
, y
);
2542 } while (++n
< count
);
2544 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2546 // draw line down to last child
2547 oldY
+= GetLineHeight(children
[n
-1])>>1;
2548 if (HasButtons()) y_mid
+= 5;
2550 // Only draw the portion of the line that is visible, in case it is huge
2551 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2552 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2553 yOrigin
= abs(yOrigin
);
2554 GetClientSize(&width
, &height
);
2556 // Move end points to the begining/end of the view?
2557 if (y_mid
< yOrigin
)
2559 if (oldY
> yOrigin
+ height
)
2560 oldY
= yOrigin
+ height
;
2562 // after the adjustments if y_mid is larger than oldY then the line
2563 // isn't visible at all so don't draw anything
2565 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2571 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2575 if ( item
->HasPlus() )
2577 // it's a folder, indicate it by a border
2582 // draw a line under the drop target because the item will be
2584 DrawLine(item
, !m_dropEffectAboveItem
);
2587 SetCursor(wxCURSOR_BULLSEYE
);
2592 SetCursor(wxCURSOR_NO_ENTRY
);
2596 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2598 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2600 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2602 wxClientDC
dc(this);
2604 dc
.SetLogicalFunction(wxINVERT
);
2605 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2607 int w
= i
->GetWidth() + 2;
2608 int h
= GetLineHeight(i
) + 2;
2610 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2613 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2615 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2617 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2619 wxClientDC
dc(this);
2621 dc
.SetLogicalFunction(wxINVERT
);
2627 y
+= GetLineHeight(i
) - 1;
2630 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2633 // -----------------------------------------------------------------------------
2634 // wxWidgets callbacks
2635 // -----------------------------------------------------------------------------
2637 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2640 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2641 RefreshLine( m_current
);
2647 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2655 dc
.SetFont( m_normalFont
);
2656 dc
.SetPen( m_dottedPen
);
2658 // this is now done dynamically
2659 //if(GetImageList() == NULL)
2660 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2663 PaintLevel( m_anchor
, dc
, 0, y
);
2666 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2675 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2684 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2686 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2687 te
.m_evtKey
= event
;
2688 if ( GetEventHandler()->ProcessEvent( te
) )
2690 // intercepted by the user code
2694 if ( (m_current
== 0) || (m_key_current
== 0) )
2700 // how should the selection work for this event?
2701 bool is_multiple
, extended_select
, unselect_others
;
2702 EventFlagsToSelType(GetWindowStyleFlag(),
2705 is_multiple
, extended_select
, unselect_others
);
2707 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2709 if (event
.GetKeyCode() == WXK_RIGHT
)
2710 event
.m_keyCode
= WXK_LEFT
;
2711 else if (event
.GetKeyCode() == WXK_LEFT
)
2712 event
.m_keyCode
= WXK_RIGHT
;
2717 // * : Expand all/Collapse all
2718 // ' ' | return : activate
2719 // up : go up (not last children!)
2721 // left : go to parent
2722 // right : open if parent and go next
2723 // home : go to root
2724 // end : go to last item without opening parents
2725 // alnum : start or continue searching for the item with this prefix
2726 int keyCode
= event
.GetKeyCode();
2731 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2739 if ( !IsExpanded(m_current
) )
2742 ExpandAllChildren(m_current
);
2745 //else: fall through to Collapse() it
2749 if (IsExpanded(m_current
))
2751 Collapse(m_current
);
2757 // Use the item's bounding rectangle to determine position for the event
2759 GetBoundingRect(m_current
, ItemRect
, true);
2761 wxTreeEvent
eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
2762 // Use the left edge, vertical middle
2763 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2764 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2765 GetEventHandler()->ProcessEvent( eventMenu
);
2771 if ( !event
.HasModifiers() )
2773 wxTreeEvent
eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
2774 GetEventHandler()->ProcessEvent( eventAct
);
2777 // in any case, also generate the normal key event for this key,
2778 // even if we generated the ACTIVATED event above: this is what
2779 // wxMSW does and it makes sense because you might not want to
2780 // process ACTIVATED event at all and handle Space and Return
2781 // directly (and differently) which would be impossible otherwise
2785 // up goes to the previous sibling or to the last
2786 // of its children if it's expanded
2789 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2792 prev
= GetItemParent( m_key_current
);
2793 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2795 break; // don't go to root if it is hidden
2799 wxTreeItemIdValue cookie
;
2800 wxTreeItemId current
= m_key_current
;
2801 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2802 if (current
== GetFirstChild( prev
, cookie
))
2804 // otherwise we return to where we came from
2805 DoSelectItem( prev
, unselect_others
, extended_select
);
2806 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2813 while ( IsExpanded(prev
) && HasChildren(prev
) )
2815 wxTreeItemId child
= GetLastChild(prev
);
2822 DoSelectItem( prev
, unselect_others
, extended_select
);
2823 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2828 // left arrow goes to the parent
2831 wxTreeItemId prev
= GetItemParent( m_current
);
2832 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2834 // don't go to root if it is hidden
2835 prev
= GetPrevSibling( m_current
);
2839 DoSelectItem( prev
, unselect_others
, extended_select
);
2845 // this works the same as the down arrow except that we
2846 // also expand the item if it wasn't expanded yet
2852 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2854 wxTreeItemIdValue cookie
;
2855 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2856 DoSelectItem( child
, unselect_others
, extended_select
);
2857 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2861 wxTreeItemId next
= GetNextSibling( m_key_current
);
2864 wxTreeItemId current
= m_key_current
;
2865 while (current
.IsOk() && !next
)
2867 current
= GetItemParent( current
);
2868 if (current
) next
= GetNextSibling( current
);
2873 DoSelectItem( next
, unselect_others
, extended_select
);
2874 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2880 // <End> selects the last visible tree item
2883 wxTreeItemId last
= GetRootItem();
2885 while ( last
.IsOk() && IsExpanded(last
) )
2887 wxTreeItemId lastChild
= GetLastChild(last
);
2889 // it may happen if the item was expanded but then all of
2890 // its children have been deleted - so IsExpanded() returned
2891 // true, but GetLastChild() returned invalid item
2900 DoSelectItem( last
, unselect_others
, extended_select
);
2905 // <Home> selects the root item
2908 wxTreeItemId prev
= GetRootItem();
2912 if ( HasFlag(wxTR_HIDE_ROOT
) )
2914 wxTreeItemIdValue cookie
;
2915 prev
= GetFirstChild(prev
, cookie
);
2920 DoSelectItem( prev
, unselect_others
, extended_select
);
2925 // do not use wxIsalnum() here
2926 if ( !event
.HasModifiers() &&
2927 ((keyCode
>= '0' && keyCode
<= '9') ||
2928 (keyCode
>= 'a' && keyCode
<= 'z') ||
2929 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2931 // find the next item starting with the given prefix
2932 wxChar ch
= (wxChar
)keyCode
;
2934 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2945 // also start the timer to reset the current prefix if the user
2946 // doesn't press any more alnum keys soon -- we wouldn't want
2947 // to use this prefix for a new item search
2950 m_findTimer
= new wxTreeFindTimer(this);
2953 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2963 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2968 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2969 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2970 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2971 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2972 if (flags
) return wxTreeItemId();
2974 if (m_anchor
== NULL
)
2976 flags
= wxTREE_HITTEST_NOWHERE
;
2977 return wxTreeItemId();
2980 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2984 flags
= wxTREE_HITTEST_NOWHERE
;
2985 return wxTreeItemId();
2990 // get the bounding rectangle of the item (or of its label only)
2991 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2993 bool textOnly
) const
2995 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2997 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
3002 rect
.width
= i
->GetWidth();
3004 if ( m_imageListNormal
)
3006 int image_w
, image_h
;
3007 m_imageListNormal
->GetSize( 0, image_w
, image_h
);
3008 rect
.width
+= image_w
+ MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3011 else // the entire line
3014 rect
.width
= GetClientSize().x
;
3018 rect
.height
= GetLineHeight(i
);
3020 // we have to return the logical coordinates, not physical ones
3021 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
3026 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
3027 wxClassInfo
* WXUNUSED(textCtrlClass
))
3029 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
3031 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
3033 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
3034 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
3040 // We have to call this here because the label in
3041 // question might just have been added and no screen
3042 // update taken place.
3044 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3047 DoDirtyProcessing();
3050 // TODO: use textCtrlClass here to create the control of correct class
3051 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3053 m_textCtrl
->SetFocus();
3058 // returns a pointer to the text edit control if the item is being
3059 // edited, NULL otherwise (it's assumed that no more than one item may
3060 // be edited simultaneously)
3061 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3066 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3067 bool discardChanges
)
3069 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
3071 m_textCtrl
->EndEdit(discardChanges
);
3074 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3075 const wxString
& value
)
3077 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3079 le
.m_editCancelled
= false;
3081 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3084 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3086 // let owner know that the edit was cancelled
3087 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3088 le
.m_label
= wxEmptyString
;
3089 le
.m_editCancelled
= true;
3091 GetEventHandler()->ProcessEvent( le
);
3094 void wxGenericTreeCtrl::OnRenameTimer()
3096 EditLabel( m_current
);
3099 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3101 if ( !m_anchor
)return;
3103 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3105 // Is the mouse over a tree item button?
3107 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3108 wxGenericTreeItem
*underMouse
= thisItem
;
3110 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3111 #endif // wxUSE_TOOLTIPS
3114 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3115 (!event
.LeftIsDown()) &&
3117 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3125 if (underMouse
!= m_underMouse
)
3129 // unhighlight old item
3130 wxGenericTreeItem
*tmp
= m_underMouse
;
3131 m_underMouse
= NULL
;
3135 m_underMouse
= underMouse
;
3137 RefreshLine( m_underMouse
);
3141 // Determines what item we are hovering over and need a tooltip for
3142 wxTreeItemId hoverItem
= thisItem
;
3144 // We do not want a tooltip if we are dragging, or if the rename timer is running
3145 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3147 // Ask the tree control what tooltip (if any) should be shown
3148 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3150 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3152 SetToolTip(hevent
.m_label
);
3157 // we process left mouse up event (enables in-place edit), middle/right down
3158 // (pass to the user code), left dbl click (activate item) and
3159 // dragging/moving events for items drag-and-drop
3160 if ( !(event
.LeftDown() ||
3162 event
.MiddleDown() ||
3163 event
.RightDown() ||
3164 event
.LeftDClick() ||
3166 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3175 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3177 if ( event
.Dragging() && !m_isDragging
)
3179 if (m_dragCount
== 0)
3184 if (m_dragCount
!= 3)
3186 // wait until user drags a bit further...
3190 wxEventType command
= event
.RightIsDown()
3191 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3192 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3194 wxTreeEvent
nevent(command
, this, m_current
);
3195 nevent
.SetPoint(CalcScrolledPosition(pt
));
3197 // by default the dragging is not supported, the user code must
3198 // explicitly allow the event for it to take place
3201 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3203 // we're going to drag this item
3204 m_isDragging
= true;
3206 // remember the old cursor because we will change it while
3208 m_oldCursor
= m_cursor
;
3210 // in a single selection control, hide the selection temporarily
3211 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3213 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3215 if ( m_oldSelection
)
3217 m_oldSelection
->SetHilight(false);
3218 RefreshLine(m_oldSelection
);
3225 else if ( event
.Dragging() )
3227 if ( item
!= m_dropTarget
)
3229 // unhighlight the previous drop target
3230 DrawDropEffect(m_dropTarget
);
3232 m_dropTarget
= item
;
3234 // highlight the current drop target if any
3235 DrawDropEffect(m_dropTarget
);
3237 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3244 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3248 // erase the highlighting
3249 DrawDropEffect(m_dropTarget
);
3251 if ( m_oldSelection
)
3253 m_oldSelection
->SetHilight(true);
3254 RefreshLine(m_oldSelection
);
3255 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3258 // generate the drag end event
3259 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3261 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3263 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3265 m_isDragging
= false;
3266 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3268 SetCursor(m_oldCursor
);
3270 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3278 // If we got to this point, we are not dragging or moving the mouse.
3279 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3280 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3281 // We skip even if we didn't hit an item because we still should
3282 // restore focus to the tree control even if we didn't exactly hit an item.
3283 if ( event
.LeftDown() )
3288 // here we process only the messages which happen on tree items
3292 if (item
== NULL
) return; /* we hit the blank area */
3294 if ( event
.RightDown() )
3296 // If the item is already selected, do not update the selection.
3297 // Multi-selections should not be cleared if a selected item is clicked.
3298 if (!IsSelected(item
))
3300 DoSelectItem(item
, true, false);
3303 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3304 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3305 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3307 // Consistent with MSW (for now), send the ITEM_MENU *after*
3308 // the RIGHT_CLICK event. TODO: This behavior may change.
3309 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3310 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3311 GetEventHandler()->ProcessEvent(nevent2
);
3313 else if ( event
.MiddleDown() )
3315 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3316 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3317 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3319 else if ( event
.LeftUp() )
3321 // this facilitates multiple-item drag-and-drop
3323 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3325 wxArrayTreeItemIds selections
;
3326 size_t count
= GetSelections(selections
);
3332 DoSelectItem(item
, true, false);
3338 if ( (item
== m_current
) &&
3339 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3340 HasFlag(wxTR_EDIT_LABELS
) )
3342 if ( m_renameTimer
)
3344 if ( m_renameTimer
->IsRunning() )
3345 m_renameTimer
->Stop();
3349 m_renameTimer
= new wxTreeRenameTimer( this );
3352 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3355 m_lastOnSame
= false;
3358 else // !RightDown() && !MiddleDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3360 if ( event
.LeftDown() )
3362 m_lastOnSame
= item
== m_current
;
3365 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3367 // only toggle the item for a single click, double click on
3368 // the button doesn't do anything (it toggles the item twice)
3369 if ( event
.LeftDown() )
3374 // don't select the item if the button was clicked
3379 // clear the previously selected items, if the
3380 // user clicked outside of the present selection.
3381 // otherwise, perform the deselection on mouse-up.
3382 // this allows multiple drag and drop to work.
3383 // but if Cmd is down, toggle selection of the clicked item
3384 if (!IsSelected(item
) || event
.CmdDown())
3386 // how should the selection work for this event?
3387 bool is_multiple
, extended_select
, unselect_others
;
3388 EventFlagsToSelType(GetWindowStyleFlag(),
3391 is_multiple
, extended_select
, unselect_others
);
3393 DoSelectItem(item
, unselect_others
, extended_select
);
3397 // For some reason, Windows isn't recognizing a left double-click,
3398 // so we need to simulate it here. Allow 200 milliseconds for now.
3399 if ( event
.LeftDClick() )
3401 // double clicking should not start editing the item label
3402 if ( m_renameTimer
)
3403 m_renameTimer
->Stop();
3405 m_lastOnSame
= false;
3407 // send activate event first
3408 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3409 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3410 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3412 // if the user code didn't process the activate event,
3413 // handle it ourselves by toggling the item when it is
3415 if ( item
->HasPlus() )
3425 void wxGenericTreeCtrl::OnInternalIdle()
3427 wxWindow::OnInternalIdle();
3429 // Check if we need to select the root item
3430 // because nothing else has been selected.
3431 // Delaying it means that we can invoke event handlers
3432 // as required, when a first item is selected.
3433 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3436 SelectItem(m_select_me
);
3437 else if (GetRootItem().IsOk())
3438 SelectItem(GetRootItem());
3441 // after all changes have been done to the tree control,
3442 // actually redraw the tree when everything is over
3444 DoDirtyProcessing();
3447 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3452 wxTreeItemAttr
*attr
= item
->GetAttributes();
3453 if ( attr
&& attr
->HasFont() )
3454 dc
.SetFont(attr
->GetFont());
3455 else if ( item
->IsBold() )
3456 dc
.SetFont(m_boldFont
);
3458 dc
.SetFont(m_normalFont
);
3460 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3463 // restore normal font
3464 dc
.SetFont( m_normalFont
);
3468 int image
= item
->GetCurrentImage();
3469 if ( image
!= NO_IMAGE
)
3471 if ( m_imageListNormal
)
3473 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3474 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3478 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3481 total_h
+= 2; // at least 2 pixels
3483 total_h
+= total_h
/10; // otherwise 10% extra spacing
3485 item
->SetHeight(total_h
);
3486 if (total_h
>m_lineHeight
)
3487 m_lineHeight
=total_h
;
3489 item
->SetWidth(image_w
+text_w
+2);
3492 // -----------------------------------------------------------------------------
3493 // for developper : y is now the top of the level
3494 // not the middle of it !
3495 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3497 int x
= level
*m_indent
;
3498 if (!HasFlag(wxTR_HIDE_ROOT
))
3502 else if (level
== 0)
3504 // a hidden root is not evaluated, but its
3505 // children are always calculated
3509 CalculateSize( item
, dc
);
3512 item
->SetX( x
+m_spacing
);
3514 y
+= GetLineHeight(item
);
3516 if ( !item
->IsExpanded() )
3518 // we don't need to calculate collapsed branches
3523 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3524 size_t n
, count
= children
.Count();
3526 for (n
= 0; n
< count
; ++n
)
3527 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3530 void wxGenericTreeCtrl::CalculatePositions()
3532 if ( !m_anchor
) return;
3534 wxClientDC
dc(this);
3537 dc
.SetFont( m_normalFont
);
3539 dc
.SetPen( m_dottedPen
);
3540 //if(GetImageList() == NULL)
3541 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3544 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3547 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3549 if ( !m_freezeCount
)
3550 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3553 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3555 if (m_dirty
|| m_freezeCount
)
3558 wxSize client
= GetClientSize();
3561 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3562 rect
.width
= client
.x
;
3563 rect
.height
= client
.y
;
3565 Refresh(true, &rect
);
3567 AdjustMyScrollbars();
3570 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3572 if (m_dirty
|| m_freezeCount
)
3576 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3577 rect
.width
= GetClientSize().x
;
3578 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3580 Refresh(true, &rect
);
3583 void wxGenericTreeCtrl::RefreshSelected()
3588 // TODO: this is awfully inefficient, we should keep the list of all
3589 // selected items internally, should be much faster
3591 RefreshSelectedUnder(m_anchor
);
3594 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3599 if ( item
->IsSelected() )
3602 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3603 size_t count
= children
.GetCount();
3604 for ( size_t n
= 0; n
< count
; n
++ )
3606 RefreshSelectedUnder(children
[n
]);
3610 void wxGenericTreeCtrl::Freeze()
3615 void wxGenericTreeCtrl::Thaw()
3617 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen tree control?") );
3619 if ( --m_freezeCount
== 0 )
3625 // ----------------------------------------------------------------------------
3626 // changing colours: we need to refresh the tree control
3627 // ----------------------------------------------------------------------------
3629 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3631 if ( !wxWindow::SetBackgroundColour(colour
) )
3639 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3641 if ( !wxWindow::SetForegroundColour(colour
) )
3649 // Process the tooltip event, to speed up event processing.
3650 // Doesn't actually get a tooltip.
3651 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3657 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3658 // be removed, as well as the #else case below.
3659 #define _USE_VISATTR 0
3664 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3666 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3670 // Use the same color scheme as wxListBox
3671 return wxListBox::GetClassDefaultAttributes(variant
);
3673 wxVisualAttributes attr
;
3674 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3675 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3676 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3681 #if WXWIN_COMPATIBILITY_2_4
3683 int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
3685 return GetItemImage(item
, wxTreeItemIcon_Selected
);
3688 void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
3690 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
3693 #endif // WXWIN_COMPATIBILITY_2_4
3695 void wxGenericTreeCtrl::DoDirtyProcessing()
3702 CalculatePositions();
3704 AdjustMyScrollbars();
3707 #endif // wxUSE_TREECTRL