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/osx/private.h"
48 // -----------------------------------------------------------------------------
50 // -----------------------------------------------------------------------------
52 class WXDLLIMPEXP_FWD_CORE wxGenericTreeItem
;
54 WX_DEFINE_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 static const int NO_IMAGE
= -1;
62 static const int PIXELS_PER_UNIT
= 10;
64 // the margin between the item state image and the item normal image
65 static const int MARGIN_BETWEEN_STATE_AND_IMAGE
= 2;
67 // the margin between the item image and the item text
68 static const int MARGIN_BETWEEN_IMAGE_AND_TEXT
= 4;
70 // -----------------------------------------------------------------------------
72 // -----------------------------------------------------------------------------
74 // timer used for enabling in-place edit
75 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
78 // start editing the current item after half a second (if the mouse hasn't
79 // been clicked/moved)
82 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
84 virtual void Notify();
87 wxGenericTreeCtrl
*m_owner
;
89 wxDECLARE_NO_COPY_CLASS(wxTreeRenameTimer
);
92 // control used for in-place edit
93 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
96 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
98 void EndEdit( bool discardChanges
);
100 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
103 void OnChar( wxKeyEvent
&event
);
104 void OnKeyUp( wxKeyEvent
&event
);
105 void OnKillFocus( wxFocusEvent
&event
);
107 bool AcceptChanges();
108 void Finish( bool setfocus
);
111 wxGenericTreeCtrl
*m_owner
;
112 wxGenericTreeItem
*m_itemEdited
;
113 wxString m_startValue
;
114 bool m_aboutToFinish
;
116 DECLARE_EVENT_TABLE()
117 wxDECLARE_NO_COPY_CLASS(wxTreeTextCtrl
);
120 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
121 // for a sufficiently long time
122 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
125 // reset the current prefix after half a second of inactivity
126 enum { DELAY
= 500 };
128 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
130 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
133 wxGenericTreeCtrl
*m_owner
;
135 wxDECLARE_NO_COPY_CLASS(wxTreeFindTimer
);
139 class WXDLLEXPORT wxGenericTreeItem
150 wxGenericTreeItem( wxGenericTreeItem
*parent
,
151 const wxString
& text
,
154 wxTreeItemData
*data
);
156 ~wxGenericTreeItem();
159 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
161 const wxString
& GetText() const { return m_text
; }
162 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
163 { return m_images
[which
]; }
164 wxTreeItemData
*GetData() const { return m_data
; }
165 int GetState() const { return m_state
; }
167 // returns the current image for the item (depending on its
168 // selected/expanded/whatever state)
169 int GetCurrentImage() const;
171 void SetText(const wxString
& text
)
178 void SetImage(int image
, wxTreeItemIcon which
)
180 m_images
[which
] = image
;
184 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
185 void SetState(int state
) { m_state
= state
; m_width
= 0; }
187 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
189 void SetBold(bool 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 int GetTextHeight() const
207 wxASSERT_MSG( m_heightText
!= -1, "must call CalculateSize() first" );
212 int GetTextWidth() const
214 wxASSERT_MSG( m_widthText
!= -1, "must call CalculateSize() first" );
219 wxGenericTreeItem
*GetParent() const { return m_parent
; }
221 // sets the items font for the specified DC if it uses any special font or
222 // simply returns false otherwise
223 bool SetFont(wxGenericTreeCtrl
*control
, wxDC
& dc
) const
227 wxTreeItemAttr
* const attr
= GetAttributes();
228 if ( attr
&& attr
->HasFont() )
229 font
= attr
->GetFont();
231 font
= control
->m_boldFont
;
241 // deletes all children notifying the treectrl about it
242 void DeleteChildren(wxGenericTreeCtrl
*tree
);
244 // get count of all children (and grand children if 'recursively')
245 size_t GetChildrenCount(bool recursively
= true) const;
247 void Insert(wxGenericTreeItem
*child
, size_t index
)
248 { m_children
.Insert(child
, index
); }
250 // calculate and cache the item size using either the provided DC (which is
251 // supposed to have wxGenericTreeCtrl::m_normalFont selected into it!) or a
252 // wxClientDC on the control window
253 void CalculateSize(wxGenericTreeCtrl
*control
, wxDC
& dc
)
254 { DoCalculateSize(control
, dc
, true /* dc uses normal font */); }
255 void CalculateSize(wxGenericTreeCtrl
*control
);
257 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
259 void ResetSize() { m_width
= 0; }
260 void ResetTextSize() { m_width
= 0; m_widthText
= -1; }
261 void RecursiveResetSize();
262 void RecursiveResetTextSize();
264 // return the item at given position (or NULL if no item), onButton is
265 // true if the point belongs to the item's button, otherwise it lies
266 // on the item's label
267 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
268 const wxGenericTreeCtrl
*,
272 void Expand() { m_isCollapsed
= false; }
273 void Collapse() { m_isCollapsed
= true; }
275 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
278 bool HasChildren() const { return !m_children
.IsEmpty(); }
279 bool IsSelected() const { return m_hasHilight
!= 0; }
280 bool IsExpanded() const { return !m_isCollapsed
; }
281 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
282 bool IsBold() const { return m_isBold
!= 0; }
285 // get them - may be NULL
286 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
287 // get them ensuring that the pointer is not NULL
288 wxTreeItemAttr
& Attr()
292 m_attr
= new wxTreeItemAttr
;
298 void SetAttributes(wxTreeItemAttr
*attr
)
300 if ( m_ownsAttr
) delete m_attr
;
306 // set them and delete when done
307 void AssignAttributes(wxTreeItemAttr
*attr
)
316 // calculate the size of this item, i.e. set m_width, m_height and
317 // m_widthText and m_heightText properly
319 // if dcUsesNormalFont is true, the current dc font must be the normal tree
321 void DoCalculateSize(wxGenericTreeCtrl
*control
,
323 bool dcUsesNormalFont
);
325 // since there can be very many of these, we save size by chosing
326 // the smallest representation for the elements and by ordering
327 // the members to avoid padding.
328 wxString m_text
; // label to be rendered for item
332 wxTreeItemData
*m_data
; // user-provided data
334 int m_state
; // item state
336 wxArrayGenericTreeItems m_children
; // list of children
337 wxGenericTreeItem
*m_parent
; // parent of this item
339 wxTreeItemAttr
*m_attr
; // attributes???
341 // tree ctrl images for the normal, selected, expanded and
342 // expanded+selected states
343 int m_images
[wxTreeItemIcon_Max
];
345 wxCoord m_x
; // (virtual) offset from top
346 wxCoord m_y
; // (virtual) offset from left
347 int m_width
; // width of this item
348 int m_height
; // height of this item
350 // use bitfields to save size
351 unsigned int m_isCollapsed
:1;
352 unsigned int m_hasHilight
:1; // same as focused
353 unsigned int m_hasPlus
:1; // used for item which doesn't have
354 // children but has a [+] button
355 unsigned int m_isBold
:1; // render the label in bold font
356 unsigned int m_ownsAttr
:1; // delete attribute when done
358 wxDECLARE_NO_COPY_CLASS(wxGenericTreeItem
);
361 // =============================================================================
363 // =============================================================================
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 // translate the key or mouse event flags to the type of selection we're
371 static void EventFlagsToSelType(long style
,
375 bool &extended_select
,
376 bool &unselect_others
)
378 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
379 extended_select
= shiftDown
&& is_multiple
;
380 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
383 // check if the given item is under another one
385 IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
389 if ( item
== parent
)
391 // item is a descendant of parent
395 item
= item
->GetParent();
401 // -----------------------------------------------------------------------------
402 // wxTreeRenameTimer (internal)
403 // -----------------------------------------------------------------------------
405 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
410 void wxTreeRenameTimer::Notify()
412 m_owner
->OnRenameTimer();
415 //-----------------------------------------------------------------------------
416 // wxTreeTextCtrl (internal)
417 //-----------------------------------------------------------------------------
419 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
420 EVT_CHAR (wxTreeTextCtrl::OnChar
)
421 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
422 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
425 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
426 wxGenericTreeItem
*item
)
427 : m_itemEdited(item
), m_startValue(item
->GetText())
430 m_aboutToFinish
= false;
433 m_owner
->GetBoundingRect(m_itemEdited
, rect
, true);
435 // corrects position and size for better appearance
439 #elif defined(__WXGTK__)
444 #elif defined(__WXMAC__)
445 int bestHeight
= GetBestSize().y
- 8;
446 if ( rect
.height
> bestHeight
)
448 int diff
= rect
.height
- bestHeight
;
454 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
455 rect
.GetPosition(), rect
.GetSize());
457 SetSelection(-1, -1);
460 void wxTreeTextCtrl::EndEdit(bool discardChanges
)
462 m_aboutToFinish
= true;
464 if ( discardChanges
)
466 m_owner
->OnRenameCancelled(m_itemEdited
);
472 // Notify the owner about the changes
475 // Even if vetoed, close the control (consistent with MSW)
480 bool wxTreeTextCtrl::AcceptChanges()
482 const wxString value
= GetValue();
484 if ( value
== m_startValue
)
486 // nothing changed, always accept
487 // when an item remains unchanged, the owner
488 // needs to be notified that the user decided
489 // not to change the tree item label, and that
490 // the edit has been cancelled
492 m_owner
->OnRenameCancelled(m_itemEdited
);
496 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
498 // vetoed by the user
502 // accepted, do rename the item
503 m_owner
->SetItemText(m_itemEdited
, value
);
508 void wxTreeTextCtrl::Finish( bool setfocus
)
510 m_owner
->ResetTextControl();
512 wxPendingDelete
.Append(this);
518 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
520 switch ( event
.m_keyCode
)
535 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
537 if ( !m_aboutToFinish
)
539 // auto-grow the textctrl:
540 wxSize parentSize
= m_owner
->GetSize();
541 wxPoint myPos
= GetPosition();
542 wxSize mySize
= GetSize();
544 GetTextExtent(GetValue() + wxT("M"), &sx
, &sy
);
545 if (myPos
.x
+ sx
> parentSize
.x
)
546 sx
= parentSize
.x
- myPos
.x
;
549 SetSize(sx
, wxDefaultCoord
);
555 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
557 if ( !m_aboutToFinish
)
559 if ( !AcceptChanges() )
560 m_owner
->OnRenameCancelled( m_itemEdited
);
565 // We should let the native text control handle focus, too.
569 // -----------------------------------------------------------------------------
571 // -----------------------------------------------------------------------------
573 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
574 const wxString
& text
,
575 int image
, int selImage
,
576 wxTreeItemData
*data
)
579 m_images
[wxTreeItemIcon_Normal
] = image
;
580 m_images
[wxTreeItemIcon_Selected
] = selImage
;
581 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
582 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
585 m_state
= wxTREE_ITEMSTATE_NONE
;
588 m_isCollapsed
= true;
589 m_hasHilight
= false;
598 // We don't know the height here yet.
606 wxGenericTreeItem::~wxGenericTreeItem()
610 if (m_ownsAttr
) delete m_attr
;
612 wxASSERT_MSG( m_children
.IsEmpty(),
613 "must call DeleteChildren() before deleting the item" );
616 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
618 size_t count
= m_children
.GetCount();
619 for ( size_t n
= 0; n
< count
; n
++ )
621 wxGenericTreeItem
*child
= m_children
[n
];
622 tree
->SendDeleteEvent(child
);
624 child
->DeleteChildren(tree
);
625 if ( child
== tree
->m_select_me
)
626 tree
->m_select_me
= NULL
;
633 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
635 size_t count
= m_children
.GetCount();
639 size_t total
= count
;
640 for (size_t n
= 0; n
< count
; ++n
)
642 total
+= m_children
[n
]->GetChildrenCount();
648 void wxGenericTreeItem::GetSize( int &x
, int &y
,
649 const wxGenericTreeCtrl
*theButton
)
651 int bottomY
=m_y
+theButton
->GetLineHeight(this);
652 if ( y
< bottomY
) y
= bottomY
;
653 int width
= m_x
+ m_width
;
654 if ( x
< width
) x
= width
;
658 size_t count
= m_children
.GetCount();
659 for ( size_t n
= 0; n
< count
; ++n
)
661 m_children
[n
]->GetSize( x
, y
, theButton
);
666 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
667 const wxGenericTreeCtrl
*theCtrl
,
671 // for a hidden root node, don't evaluate it, but do evaluate children
672 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
675 int h
= theCtrl
->GetLineHeight(this);
676 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
678 int y_mid
= m_y
+ h
/2;
679 if (point
.y
< y_mid
)
680 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
682 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
684 int xCross
= m_x
- theCtrl
->GetSpacing();
686 // according to the drawing code the triangels are drawn
687 // at -4 , -4 from the position up to +10/+10 max
688 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
689 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
690 HasPlus() && theCtrl
->HasButtons() )
692 // 5 is the size of the plus sign
693 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
694 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
695 HasPlus() && theCtrl
->HasButtons() )
698 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
702 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
707 // assuming every image (normal and selected) has the same size!
708 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
710 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
717 if ( (GetState() != wxTREE_ITEMSTATE_NONE
) &&
718 theCtrl
->m_imageListState
)
720 theCtrl
->m_imageListState
->GetSize(GetState(),
724 if ((state_w
!= -1) && (point
.x
<= m_x
+ state_w
+ 1))
725 flags
|= wxTREE_HITTEST_ONITEMSTATEICON
;
726 else if ((image_w
!= -1) &&
728 (state_w
!= -1 ? state_w
+
729 MARGIN_BETWEEN_STATE_AND_IMAGE
732 flags
|= wxTREE_HITTEST_ONITEMICON
;
734 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
740 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
741 if (point
.x
> m_x
+m_width
)
742 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
747 // if children are expanded, fall through to evaluate them
748 if (m_isCollapsed
) return NULL
;
752 size_t count
= m_children
.GetCount();
753 for ( size_t n
= 0; n
< count
; n
++ )
755 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
766 int wxGenericTreeItem::GetCurrentImage() const
768 int image
= NO_IMAGE
;
773 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
776 if ( image
== NO_IMAGE
)
778 // we usually fall back to the normal item, but try just the
779 // expanded one (and not selected) first in this case
780 image
= GetImage(wxTreeItemIcon_Expanded
);
786 image
= GetImage(wxTreeItemIcon_Selected
);
789 // maybe it doesn't have the specific image we want,
790 // try the default one instead
791 if ( image
== NO_IMAGE
) image
= GetImage();
796 void wxGenericTreeItem::CalculateSize(wxGenericTreeCtrl
* control
)
798 // check if we need to do anything before creating the DC
802 wxClientDC
dc(control
);
803 DoCalculateSize(control
, dc
, false /* normal font not used */);
807 wxGenericTreeItem::DoCalculateSize(wxGenericTreeCtrl
* control
,
809 bool dcUsesNormalFont
)
811 if ( m_width
!= 0 ) // Size known, nothing to do
814 if ( m_widthText
== -1 )
817 if ( SetFont(control
, dc
) )
821 else // we have no special font
823 if ( !dcUsesNormalFont
)
825 // but we do need to ensure that the normal font is used: notice
826 // that this doesn't count as changing the font as we don't need
828 dc
.SetFont(control
->m_normalFont
);
834 dc
.GetTextExtent( GetText(), &m_widthText
, &m_heightText
);
836 // restore normal font if the DC used it previously and we changed it
838 dc
.SetFont(control
->m_normalFont
);
841 int text_h
= m_heightText
+ 2;
843 int image_h
= 0, image_w
= 0;
844 int image
= GetCurrentImage();
845 if ( image
!= NO_IMAGE
&& control
->m_imageListNormal
)
847 control
->m_imageListNormal
->GetSize(image
, image_w
, image_h
);
848 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
851 int state_h
= 0, state_w
= 0;
852 int state
= GetState();
853 if ( state
!= wxTREE_ITEMSTATE_NONE
&& control
->m_imageListState
)
855 control
->m_imageListState
->GetSize(state
, state_w
, state_h
);
857 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
859 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
862 int img_h
= wxMax(state_h
, image_h
);
863 m_height
= wxMax(img_h
, text_h
);
866 m_height
+= 2; // at least 2 pixels
868 m_height
+= m_height
/ 10; // otherwise 10% extra spacing
870 if (m_height
> control
->m_lineHeight
)
871 control
->m_lineHeight
= m_height
;
873 m_width
= state_w
+ image_w
+ m_widthText
+ 2;
876 void wxGenericTreeItem::RecursiveResetSize()
880 const size_t count
= m_children
.Count();
881 for (size_t i
= 0; i
< count
; i
++ )
882 m_children
[i
]->RecursiveResetSize();
885 void wxGenericTreeItem::RecursiveResetTextSize()
890 const size_t count
= m_children
.Count();
891 for (size_t i
= 0; i
< count
; i
++ )
892 m_children
[i
]->RecursiveResetTextSize();
895 // -----------------------------------------------------------------------------
896 // wxGenericTreeCtrl implementation
897 // -----------------------------------------------------------------------------
899 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
901 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
902 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
903 EVT_SIZE (wxGenericTreeCtrl::OnSize
)
904 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
905 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
906 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
907 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
908 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
911 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
913 * wxTreeCtrl has to be a real class or we have problems with
914 * the run-time information.
917 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
920 // -----------------------------------------------------------------------------
921 // construction/destruction
922 // -----------------------------------------------------------------------------
924 void wxGenericTreeCtrl::Init()
937 m_hilightBrush
= new wxBrush
939 wxSystemSettings::GetColour
941 wxSYS_COLOUR_HIGHLIGHT
946 m_hilightUnfocusedBrush
= new wxBrush
948 wxSystemSettings::GetColour
950 wxSYS_COLOUR_BTNSHADOW
955 m_imageListButtons
= NULL
;
956 m_ownsImageListButtons
= false;
959 m_isDragging
= false;
960 m_dropTarget
= m_oldSelection
= NULL
;
964 m_renameTimer
= NULL
;
968 m_dropEffectAboveItem
= false;
970 m_dndEffect
= NoEffect
;
971 m_dndEffectItem
= NULL
;
973 m_lastOnSame
= false;
975 #if defined( __WXMAC__ )
976 m_normalFont
= wxFont(wxOSX_SYSTEM_FONT_VIEWS
);
978 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
980 m_boldFont
= m_normalFont
.Bold();
983 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
988 const wxValidator
& validator
,
989 const wxString
& name
)
993 wxGetOsVersion(&major
, &minor
);
996 style
|= wxTR_ROW_LINES
;
998 if (style
& wxTR_HAS_BUTTONS
)
999 style
|= wxTR_NO_LINES
;
1003 if (style
& wxTR_HAS_BUTTONS
)
1004 style
|= wxTR_NO_LINES
;
1007 if ( !wxControl::Create( parent
, id
, pos
, size
,
1008 style
|wxHSCROLL
|wxVSCROLL
,
1013 // If the tree display has no buttons, but does have
1014 // connecting lines, we can use a narrower layout.
1015 // It may not be a good idea to force this...
1016 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
1022 wxVisualAttributes attr
= GetDefaultAttributes();
1023 SetOwnForegroundColour( attr
.colFg
);
1024 SetOwnBackgroundColour( attr
.colBg
);
1026 SetOwnFont(attr
.font
);
1028 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
1029 // style because we apparently get performance problems when using dotted
1030 // pen for drawing in some ports -- but under MSW it seems to work fine
1032 m_dottedPen
= wxPen(*wxLIGHT_GREY
, 0, wxPENSTYLE_DOT
);
1034 m_dottedPen
= *wxGREY_PEN
;
1037 SetInitialSize(size
);
1042 wxGenericTreeCtrl::~wxGenericTreeCtrl()
1044 delete m_hilightBrush
;
1045 delete m_hilightUnfocusedBrush
;
1049 delete m_renameTimer
;
1052 if (m_ownsImageListButtons
)
1053 delete m_imageListButtons
;
1056 // -----------------------------------------------------------------------------
1058 // -----------------------------------------------------------------------------
1060 unsigned int wxGenericTreeCtrl::GetCount() const
1064 // the tree is empty
1068 unsigned int count
= m_anchor
->GetChildrenCount();
1069 if ( !HasFlag(wxTR_HIDE_ROOT
) )
1071 // take the root itself into account
1078 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
1080 m_indent
= (unsigned short) indent
;
1085 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
1086 bool recursively
) const
1088 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
1090 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
1093 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
1095 // Do not try to expand the root node if it hasn't been created yet
1096 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
1098 // if we will hide the root, make sure children are visible
1099 m_anchor
->SetHasPlus();
1101 CalculatePositions();
1104 // right now, just sets the styles. Eventually, we may
1105 // want to update the inherited styles, but right now
1106 // none of the parents has updatable styles
1107 m_windowStyle
= styles
;
1111 // -----------------------------------------------------------------------------
1112 // functions to work with tree items
1113 // -----------------------------------------------------------------------------
1115 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
1117 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
1119 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
1122 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
1123 wxTreeItemIcon which
) const
1125 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1127 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
1130 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1132 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1134 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
1137 int wxGenericTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
1139 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
1141 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1142 return pItem
->GetState();
1145 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1147 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1149 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1150 return pItem
->Attr().GetTextColour();
1154 wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1156 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1158 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1159 return pItem
->Attr().GetBackgroundColour();
1162 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1164 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1166 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1167 return pItem
->Attr().GetFont();
1171 wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
1173 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1175 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1176 pItem
->SetText(text
);
1177 pItem
->CalculateSize(this);
1181 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
1183 wxTreeItemIcon which
)
1185 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1187 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1188 pItem
->SetImage(image
, which
);
1189 pItem
->CalculateSize(this);
1194 wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1196 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1199 data
->SetId( item
);
1201 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1204 void wxGenericTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
1206 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1208 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1209 pItem
->SetState(state
);
1210 pItem
->CalculateSize(this);
1214 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1216 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1218 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1219 pItem
->SetHasPlus(has
);
1223 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1225 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1227 // avoid redrawing the tree if no real change
1228 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1229 if ( pItem
->IsBold() != bold
)
1231 pItem
->SetBold(bold
);
1233 // recalculate the item size as bold and non bold fonts have different
1235 pItem
->CalculateSize(this);
1240 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1243 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1249 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1250 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1253 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1254 pItem
->Attr().SetTextColour(fg
);
1255 pItem
->Attr().SetBackgroundColour(bg
);
1259 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1260 const wxColour
& col
)
1262 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1264 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1265 pItem
->Attr().SetTextColour(col
);
1269 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1270 const wxColour
& col
)
1272 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1274 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1275 pItem
->Attr().SetBackgroundColour(col
);
1280 wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1282 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1284 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1285 pItem
->Attr().SetFont(font
);
1286 pItem
->ResetTextSize();
1287 pItem
->CalculateSize(this);
1291 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1293 wxTreeCtrlBase::SetFont(font
);
1295 m_normalFont
= font
;
1296 m_boldFont
= m_normalFont
.Bold();
1299 m_anchor
->RecursiveResetTextSize();
1305 // -----------------------------------------------------------------------------
1306 // item status inquiries
1307 // -----------------------------------------------------------------------------
1309 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1311 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1313 // An item is only visible if it's not a descendant of a collapsed item
1314 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1315 wxGenericTreeItem
* parent
= pItem
->GetParent();
1318 if (!parent
->IsExpanded())
1320 parent
= parent
->GetParent();
1324 GetViewStart(& startX
, & startY
);
1326 wxSize clientSize
= GetClientSize();
1329 if (!GetBoundingRect(item
, rect
))
1331 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1333 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1335 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1341 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1343 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1345 // consider that the item does have children if it has the "+" button: it
1346 // might not have them (if it had never been expanded yet) but then it
1347 // could have them as well and it's better to err on this side rather than
1348 // disabling some operations which are restricted to the items with
1349 // children for an item which does have them
1350 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1353 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1355 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1357 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1360 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1362 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1364 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1367 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1369 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1371 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1374 // -----------------------------------------------------------------------------
1376 // -----------------------------------------------------------------------------
1378 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1380 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1382 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1385 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1386 wxTreeItemIdValue
& cookie
) const
1388 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1391 return GetNextChild(item
, cookie
);
1394 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1395 wxTreeItemIdValue
& cookie
) const
1397 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1399 wxArrayGenericTreeItems
&
1400 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1402 // it's ok to cast cookie to size_t, we never have indices big enough to
1403 // overflow "void *"
1404 size_t *pIndex
= (size_t *)&cookie
;
1405 if ( *pIndex
< children
.GetCount() )
1407 return children
.Item((*pIndex
)++);
1411 // there are no more of them
1412 return wxTreeItemId();
1416 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1418 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1420 wxArrayGenericTreeItems
&
1421 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1422 return children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last());
1425 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1427 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1429 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1430 wxGenericTreeItem
*parent
= i
->GetParent();
1431 if ( parent
== NULL
)
1433 // root item doesn't have any siblings
1434 return wxTreeItemId();
1437 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1438 int index
= siblings
.Index(i
);
1439 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1441 size_t n
= (size_t)(index
+ 1);
1442 return n
== siblings
.GetCount() ? wxTreeItemId()
1443 : wxTreeItemId(siblings
[n
]);
1446 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1448 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1450 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1451 wxGenericTreeItem
*parent
= i
->GetParent();
1452 if ( parent
== NULL
)
1454 // root item doesn't have any siblings
1455 return wxTreeItemId();
1458 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1459 int index
= siblings
.Index(i
);
1460 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1462 return index
== 0 ? wxTreeItemId()
1463 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1466 // Only for internal use right now, but should probably be public
1467 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1469 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1471 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1473 // First see if there are any children.
1474 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1475 if (children
.GetCount() > 0)
1477 return children
.Item(0);
1481 // Try a sibling of this or ancestor instead
1482 wxTreeItemId p
= item
;
1483 wxTreeItemId toFind
;
1486 toFind
= GetNextSibling(p
);
1487 p
= GetItemParent(p
);
1488 } while (p
.IsOk() && !toFind
.IsOk());
1493 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1495 wxTreeItemId id
= GetRootItem();
1504 } while (id
.IsOk());
1506 return wxTreeItemId();
1509 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1511 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1512 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1514 wxTreeItemId id
= item
;
1517 while (id
= GetNext(id
), id
.IsOk())
1523 return wxTreeItemId();
1526 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1528 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1529 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1531 // find out the starting point
1532 wxTreeItemId prevItem
= GetPrevSibling(item
);
1533 if ( !prevItem
.IsOk() )
1535 prevItem
= GetItemParent(item
);
1538 // find the first visible item after it
1539 while ( prevItem
.IsOk() && !IsVisible(prevItem
) )
1541 prevItem
= GetNext(prevItem
);
1542 if ( !prevItem
.IsOk() || prevItem
== item
)
1544 // there are no visible items before item
1545 return wxTreeItemId();
1549 // from there we must be able to navigate until this item
1550 while ( prevItem
.IsOk() )
1552 const wxTreeItemId nextItem
= GetNextVisible(prevItem
);
1553 if ( !nextItem
.IsOk() || nextItem
== item
)
1556 prevItem
= nextItem
;
1562 // called by wxTextTreeCtrl when it marks itself for deletion
1563 void wxGenericTreeCtrl::ResetTextControl()
1568 // find the first item starting with the given prefix after the given item
1569 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1570 const wxString
& prefixOrig
) const
1572 // match is case insensitive as this is more convenient to the user: having
1573 // to press Shift-letter to go to the item starting with a capital letter
1574 // would be too bothersome
1575 wxString prefix
= prefixOrig
.Lower();
1577 // determine the starting point: we shouldn't take the current item (this
1578 // allows to switch between two items starting with the same letter just by
1579 // pressing it) but we shouldn't jump to the next one if the user is
1580 // continuing to type as otherwise he might easily skip the item he wanted
1581 wxTreeItemId id
= idParent
;
1582 if ( prefix
.length() == 1 )
1587 // look for the item starting with the given prefix after it
1588 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1593 // if we haven't found anything...
1596 // ... wrap to the beginning
1598 if ( HasFlag(wxTR_HIDE_ROOT
) )
1600 // can't select virtual root
1604 // and try all the items (stop when we get to the one we started from)
1605 while ( id
.IsOk() && id
!= idParent
&&
1606 !GetItemText(id
).Lower().StartsWith(prefix
) )
1610 // If we haven't found the item, id.IsOk() will be false, as per
1617 // -----------------------------------------------------------------------------
1619 // -----------------------------------------------------------------------------
1621 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1623 const wxString
& text
,
1626 wxTreeItemData
*data
)
1628 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1631 // should we give a warning here?
1632 return AddRoot(text
, image
, selImage
, data
);
1635 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1637 wxGenericTreeItem
*item
=
1638 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1642 data
->m_pItem
= item
;
1645 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1648 InvalidateBestSize();
1652 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1655 wxTreeItemData
*data
)
1657 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
1659 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1661 m_anchor
= new wxGenericTreeItem(NULL
, text
,
1662 image
, selImage
, data
);
1665 data
->m_pItem
= m_anchor
;
1668 if (HasFlag(wxTR_HIDE_ROOT
))
1670 // if root is hidden, make sure we can navigate
1672 m_anchor
->SetHasPlus();
1674 CalculatePositions();
1677 if (!HasFlag(wxTR_MULTIPLE
))
1679 m_current
= m_key_current
= m_anchor
;
1680 m_current
->SetHilight( true );
1683 InvalidateBestSize();
1687 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1688 const wxTreeItemId
& idPrevious
,
1689 const wxString
& text
,
1690 int image
, int selImage
,
1691 wxTreeItemData
*data
)
1693 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1696 // should we give a warning here?
1697 return AddRoot(text
, image
, selImage
, data
);
1701 if (idPrevious
.IsOk())
1703 index
= parent
->GetChildren().Index(
1704 (wxGenericTreeItem
*) idPrevious
.m_pItem
);
1705 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1706 "previous item in wxGenericTreeCtrl::InsertItem() "
1707 "is not a sibling" );
1710 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1714 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1716 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1717 GetEventHandler()->ProcessEvent( event
);
1720 // Don't leave edit or selection on a child which is about to disappear
1721 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1723 if ( m_textCtrl
&& item
!= m_textCtrl
->item() &&
1724 IsDescendantOf(item
, m_textCtrl
->item()) )
1726 m_textCtrl
->EndEdit( true );
1729 if ( item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
) )
1731 m_key_current
= NULL
;
1734 if ( IsDescendantOf(item
, m_select_me
) )
1739 if ( item
!= m_current
&& IsDescendantOf(item
, m_current
) )
1741 m_current
->SetHilight( false );
1747 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1749 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1751 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1752 ChildrenClosing(item
);
1753 item
->DeleteChildren(this);
1754 InvalidateBestSize();
1757 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1759 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1761 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1763 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1765 // can't delete the item being edited, cancel editing it first
1766 m_textCtrl
->EndEdit( true );
1769 wxGenericTreeItem
*parent
= item
->GetParent();
1771 // if the selected item will be deleted, select the parent ...
1772 wxGenericTreeItem
*to_be_selected
= parent
;
1775 // .. unless there is a next sibling like wxMSW does it
1776 int pos
= parent
->GetChildren().Index( item
);
1777 if ((int)(parent
->GetChildren().GetCount()) > pos
+1)
1778 to_be_selected
= parent
->GetChildren().Item( pos
+1 );
1781 // don't keep stale pointers around!
1782 if ( IsDescendantOf(item
, m_key_current
) )
1784 // Don't silently change the selection:
1785 // do it properly in idle time, so event
1786 // handlers get called.
1788 // m_key_current = parent;
1789 m_key_current
= NULL
;
1792 // m_select_me records whether we need to select
1793 // a different item, in idle time.
1794 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1796 m_select_me
= to_be_selected
;
1799 if ( IsDescendantOf(item
, m_current
) )
1801 // Don't silently change the selection:
1802 // do it properly in idle time, so event
1803 // handlers get called.
1805 // m_current = parent;
1807 m_select_me
= to_be_selected
;
1810 // remove the item from the tree
1813 parent
->GetChildren().Remove( item
); // remove by value
1815 else // deleting the root
1817 // nothing will be left in the tree
1821 // and delete all of its children and the item itself now
1822 item
->DeleteChildren(this);
1823 SendDeleteEvent(item
);
1825 if (item
== m_select_me
)
1830 InvalidateBestSize();
1833 void wxGenericTreeCtrl::DeleteAllItems()
1841 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1843 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1845 wxCHECK_RET( item
, wxT("invalid item in wxGenericTreeCtrl::Expand") );
1846 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1847 wxT("can't expand hidden root") );
1849 if ( !item
->HasPlus() )
1852 if ( item
->IsExpanded() )
1855 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1857 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1859 // cancelled by program
1866 CalculatePositions();
1868 RefreshSubtree(item
);
1875 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1876 GetEventHandler()->ProcessEvent( event
);
1879 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1881 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1882 wxT("can't collapse hidden root") );
1884 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1886 if ( !item
->IsExpanded() )
1889 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1890 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1892 // cancelled by program
1896 ChildrenClosing(item
);
1899 #if 0 // TODO why should items be collapsed recursively?
1900 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1901 size_t count
= children
.GetCount();
1902 for ( size_t n
= 0; n
< count
; n
++ )
1904 Collapse(children
[n
]);
1908 CalculatePositions();
1910 RefreshSubtree(item
);
1912 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1913 GetEventHandler()->ProcessEvent( event
);
1916 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1919 DeleteChildren(item
);
1922 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1924 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1926 if (item
->IsExpanded())
1932 void wxGenericTreeCtrl::Unselect()
1936 m_current
->SetHilight( false );
1937 RefreshLine( m_current
);
1944 void wxGenericTreeCtrl::ClearFocusedItem()
1946 wxTreeItemId item
= GetFocusedItem();
1948 SelectItem(item
, false);
1951 void wxGenericTreeCtrl::SetFocusedItem(const wxTreeItemId
& item
)
1953 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1955 SelectItem(item
, true);
1958 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1960 if (item
->IsSelected())
1962 item
->SetHilight(false);
1966 if (item
->HasChildren())
1968 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1969 size_t count
= children
.GetCount();
1970 for ( size_t n
= 0; n
< count
; ++n
)
1972 UnselectAllChildren(children
[n
]);
1977 void wxGenericTreeCtrl::UnselectAll()
1979 wxTreeItemId rootItem
= GetRootItem();
1981 // the tree might not have the root item at all
1984 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1988 void wxGenericTreeCtrl::SelectChildren(const wxTreeItemId
& parent
)
1990 wxCHECK_RET( HasFlag(wxTR_MULTIPLE
),
1991 "this only works with multiple selection controls" );
1995 if ( !HasChildren(parent
) )
1999 wxArrayGenericTreeItems
&
2000 children
= ((wxGenericTreeItem
*) parent
.m_pItem
)->GetChildren();
2001 size_t count
= children
.GetCount();
2004 item
= (wxGenericTreeItem
*) ((wxTreeItemId
)children
[0]).m_pItem
;
2005 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2006 event
.m_itemOld
= m_current
;
2008 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2011 for ( size_t n
= 0; n
< count
; ++n
)
2013 m_current
= m_key_current
= children
[n
];
2014 m_current
->SetHilight(true);
2019 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2020 GetEventHandler()->ProcessEvent( event
);
2024 // Recursive function !
2025 // To stop we must have crt_item<last_item
2027 // Tag all next children, when no more children,
2028 // Move to parent (not to tag)
2029 // Keep going... if we found last_item, we stop.
2031 wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
,
2032 wxGenericTreeItem
*last_item
,
2035 wxGenericTreeItem
*parent
= crt_item
->GetParent();
2037 if (parent
== NULL
) // This is root item
2038 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
2040 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
2041 int index
= children
.Index(crt_item
);
2042 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
2044 size_t count
= children
.GetCount();
2045 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
2047 if ( TagAllChildrenUntilLast(children
[n
], last_item
, select
) )
2051 return TagNextChildren(parent
, last_item
, select
);
2055 wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
,
2056 wxGenericTreeItem
*last_item
,
2059 crt_item
->SetHilight(select
);
2060 RefreshLine(crt_item
);
2062 if (crt_item
==last_item
)
2065 if (crt_item
->HasChildren())
2067 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
2068 size_t count
= children
.GetCount();
2069 for ( size_t n
= 0; n
< count
; ++n
)
2071 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
2080 wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
,
2081 wxGenericTreeItem
*item2
)
2085 // item2 is not necessary after item1
2086 // choice first' and 'last' between item1 and item2
2087 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
2088 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
2090 bool select
= m_current
->IsSelected();
2092 if ( TagAllChildrenUntilLast(first
,last
,select
) )
2095 TagNextChildren(first
,last
,select
);
2098 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
2099 bool unselect_others
,
2100 bool extended_select
)
2102 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2106 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
2107 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2109 //wxCHECK_RET( ( (!unselect_others) && is_single),
2110 // wxT("this is a single selection tree") );
2112 // to keep going anyhow !!!
2115 if (item
->IsSelected())
2116 return; // nothing to do
2117 unselect_others
= true;
2118 extended_select
= false;
2120 else if ( unselect_others
&& item
->IsSelected() )
2122 // selection change if there is more than one item currently selected
2123 wxArrayTreeItemIds selected_items
;
2124 if ( GetSelections(selected_items
) == 1 )
2128 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2129 event
.m_itemOld
= m_current
;
2130 // TODO : Here we don't send any selection mode yet !
2132 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2135 wxTreeItemId parent
= GetItemParent( itemId
);
2136 while (parent
.IsOk())
2138 if (!IsExpanded(parent
))
2141 parent
= GetItemParent( parent
);
2145 if (unselect_others
)
2147 if (is_single
) Unselect(); // to speed up thing
2152 if (extended_select
)
2157 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
2160 // don't change the mark (m_current)
2161 SelectItemRange(m_current
, item
);
2165 bool select
= true; // the default
2167 // Check if we need to toggle hilight (ctrl mode)
2168 if (!unselect_others
)
2169 select
=!item
->IsSelected();
2171 m_current
= m_key_current
= item
;
2172 m_current
->SetHilight(select
);
2173 RefreshLine( m_current
);
2176 // This can cause idle processing to select the root
2177 // if no item is selected, so it must be after the
2179 EnsureVisible( itemId
);
2181 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2182 GetEventHandler()->ProcessEvent( event
);
2185 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
2187 wxGenericTreeItem
* const item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2188 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
2192 if ( !item
->IsSelected() )
2193 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
2197 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2198 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2201 item
->SetHilight(false);
2204 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2205 GetEventHandler()->ProcessEvent( event
);
2209 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
2210 wxArrayTreeItemIds
&array
) const
2212 if ( item
->IsSelected() )
2213 array
.Add(wxTreeItemId(item
));
2215 if ( item
->HasChildren() )
2217 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2218 size_t count
= children
.GetCount();
2219 for ( size_t n
= 0; n
< count
; ++n
)
2220 FillArray(children
[n
], array
);
2224 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
2227 wxTreeItemId idRoot
= GetRootItem();
2228 if ( idRoot
.IsOk() )
2230 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
2232 //else: the tree is empty, so no selections
2234 return array
.GetCount();
2237 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
2239 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2241 if (!item
.IsOk()) return;
2243 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2245 // first expand all parent branches
2246 wxGenericTreeItem
*parent
= gitem
->GetParent();
2248 if ( HasFlag(wxTR_HIDE_ROOT
) )
2250 while ( parent
&& parent
!= m_anchor
)
2253 parent
= parent
->GetParent();
2261 parent
= parent
->GetParent();
2265 //if (parent) CalculatePositions();
2270 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
2275 // update the control before scrolling it
2277 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2280 DoDirtyProcessing();
2283 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2285 int itemY
= gitem
->GetY();
2289 GetViewStart( &start_x
, &start_y
);
2291 const int clientHeight
= GetClientSize().y
;
2293 const int itemHeight
= GetLineHeight(gitem
) + 2;
2295 if ( itemY
+ itemHeight
> start_y
*PIXELS_PER_UNIT
+ clientHeight
)
2297 // need to scroll up by enough to show this item fully
2298 itemY
+= itemHeight
- clientHeight
;
2300 else if ( itemY
> start_y
*PIXELS_PER_UNIT
)
2302 // item is already fully visible, don't do anything
2305 //else: scroll down to make this item the top one displayed
2307 Scroll(-1, itemY
/PIXELS_PER_UNIT
);
2310 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2311 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2313 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2314 wxGenericTreeItem
**item2
)
2316 wxCHECK_MSG( s_treeBeingSorted
, 0,
2317 "bug in wxGenericTreeCtrl::SortChildren()" );
2319 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2322 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2324 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2326 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2328 wxCHECK_RET( !s_treeBeingSorted
,
2329 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2331 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2332 if ( children
.GetCount() > 1 )
2336 s_treeBeingSorted
= this;
2337 children
.Sort(tree_ctrl_compare_func
);
2338 s_treeBeingSorted
= NULL
;
2340 //else: don't make the tree dirty as nothing changed
2343 void wxGenericTreeCtrl::CalculateLineHeight()
2345 wxClientDC
dc(this);
2346 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2348 if ( m_imageListNormal
)
2350 // Calculate a m_lineHeight value from the normal Image sizes.
2351 // May be toggle off. Then wxGenericTreeCtrl will spread when
2352 // necessary (which might look ugly).
2353 int n
= m_imageListNormal
->GetImageCount();
2354 for (int i
= 0; i
< n
; i
++)
2356 int width
= 0, height
= 0;
2357 m_imageListNormal
->GetSize(i
, width
, height
);
2358 if (height
> m_lineHeight
) m_lineHeight
= height
;
2362 if ( m_imageListState
)
2364 // Calculate a m_lineHeight value from the state Image sizes.
2365 // May be toggle off. Then wxGenericTreeCtrl will spread when
2366 // necessary (which might look ugly).
2367 int n
= m_imageListState
->GetImageCount();
2368 for (int i
= 0; i
< n
; i
++)
2370 int width
= 0, height
= 0;
2371 m_imageListState
->GetSize(i
, width
, height
);
2372 if (height
> m_lineHeight
) m_lineHeight
= height
;
2376 if (m_imageListButtons
)
2378 // Calculate a m_lineHeight value from the Button image sizes.
2379 // May be toggle off. Then wxGenericTreeCtrl will spread when
2380 // necessary (which might look ugly).
2381 int n
= m_imageListButtons
->GetImageCount();
2382 for (int i
= 0; i
< n
; i
++)
2384 int width
= 0, height
= 0;
2385 m_imageListButtons
->GetSize(i
, width
, height
);
2386 if (height
> m_lineHeight
) m_lineHeight
= height
;
2390 if (m_lineHeight
< 30)
2391 m_lineHeight
+= 2; // at least 2 pixels
2393 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2396 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2398 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2399 m_imageListNormal
= imageList
;
2400 m_ownsImageListNormal
= false;
2404 m_anchor
->RecursiveResetSize();
2406 // Don't do any drawing if we're setting the list to NULL,
2407 // since we may be in the process of deleting the tree control.
2409 CalculateLineHeight();
2412 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2414 if (m_ownsImageListState
) delete m_imageListState
;
2415 m_imageListState
= imageList
;
2416 m_ownsImageListState
= false;
2420 m_anchor
->RecursiveResetSize();
2422 // Don't do any drawing if we're setting the list to NULL,
2423 // since we may be in the process of deleting the tree control.
2425 CalculateLineHeight();
2428 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2430 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2431 m_imageListButtons
= imageList
;
2432 m_ownsImageListButtons
= false;
2436 m_anchor
->RecursiveResetSize();
2438 CalculateLineHeight();
2441 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2443 SetButtonsImageList(imageList
);
2444 m_ownsImageListButtons
= true;
2447 // -----------------------------------------------------------------------------
2449 // -----------------------------------------------------------------------------
2451 void wxGenericTreeCtrl::AdjustMyScrollbars()
2456 m_anchor
->GetSize( x
, y
, this );
2457 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2458 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2459 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2460 int y_pos
= GetScrollPos( wxVERTICAL
);
2461 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2462 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2467 SetScrollbars( 0, 0, 0, 0 );
2471 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2473 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2474 return item
->GetHeight();
2476 return m_lineHeight
;
2479 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2481 item
->SetFont(this, dc
);
2482 item
->CalculateSize(this, dc
);
2484 wxCoord text_h
= item
->GetTextHeight();
2486 int image_h
= 0, image_w
= 0;
2487 int image
= item
->GetCurrentImage();
2488 if ( image
!= NO_IMAGE
)
2490 if ( m_imageListNormal
)
2492 m_imageListNormal
->GetSize(image
, image_w
, image_h
);
2493 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2501 int state_h
= 0, state_w
= 0;
2502 int state
= item
->GetState();
2503 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2505 if ( m_imageListState
)
2507 m_imageListState
->GetSize(state
, state_w
, state_h
);
2509 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
2511 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2515 state
= wxTREE_ITEMSTATE_NONE
;
2519 int total_h
= GetLineHeight(item
);
2520 bool drawItemBackground
= false,
2521 hasBgColour
= false;
2523 if ( item
->IsSelected() )
2525 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2526 drawItemBackground
= true;
2531 wxTreeItemAttr
* const attr
= item
->GetAttributes();
2532 if ( attr
&& attr
->HasBackgroundColour() )
2534 drawItemBackground
=
2536 colBg
= attr
->GetBackgroundColour();
2540 colBg
= GetBackgroundColour();
2542 dc
.SetBrush(wxBrush(colBg
, wxBRUSHSTYLE_SOLID
));
2545 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2547 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2551 GetVirtualSize(&w
, &h
);
2552 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2553 if (!item
->IsSelected())
2555 dc
.DrawRectangle(rect
);
2559 int flags
= wxCONTROL_SELECTED
;
2561 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2562 && IsControlActive( (ControlRef
)GetHandle() )
2565 flags
|= wxCONTROL_FOCUSED
;
2566 if ((item
== m_current
) && (m_hasFocus
))
2567 flags
|= wxCONTROL_CURRENT
;
2569 wxRendererNative::Get().
2570 DrawItemSelectionRect(this, dc
, rect
, flags
);
2573 else // no full row highlight
2575 if ( item
->IsSelected() &&
2576 (state
!= wxTREE_ITEMSTATE_NONE
|| image
!= NO_IMAGE
) )
2578 // If it's selected, and there's an state image or normal image,
2579 // then we should take care to leave the area under the image
2580 // painted in the background colour.
2581 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2582 item
->GetY() + offset
,
2583 item
->GetWidth() - state_w
- image_w
+ 2,
2585 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2586 dc
.DrawRectangle( rect
);
2591 int flags
= wxCONTROL_SELECTED
;
2593 flags
|= wxCONTROL_FOCUSED
;
2594 if ((item
== m_current
) && (m_hasFocus
))
2595 flags
|= wxCONTROL_CURRENT
;
2596 wxRendererNative::Get().
2597 DrawItemSelectionRect(this, dc
, rect
, flags
);
2600 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2601 // don't allow backgrounds to be customized. Not drawing the background,
2602 // except for custom item backgrounds, works for both kinds of theme.
2603 else if (drawItemBackground
)
2605 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2606 item
->GetY() + offset
,
2607 item
->GetWidth() - state_w
- image_w
+ 2,
2611 dc
.DrawRectangle( rect
);
2613 else // no specific background colour
2618 int flags
= wxCONTROL_SELECTED
;
2620 flags
|= wxCONTROL_FOCUSED
;
2621 if ((item
== m_current
) && (m_hasFocus
))
2622 flags
|= wxCONTROL_CURRENT
;
2623 wxRendererNative::Get().
2624 DrawItemSelectionRect(this, dc
, rect
, flags
);
2629 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2631 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), state_w
, total_h
);
2632 m_imageListState
->Draw( state
, dc
,
2635 (total_h
> state_h
? (total_h
-state_h
)/2
2637 wxIMAGELIST_DRAW_TRANSPARENT
);
2638 dc
.DestroyClippingRegion();
2641 if ( image
!= NO_IMAGE
)
2643 dc
.SetClippingRegion(item
->GetX() + state_w
, item
->GetY(),
2645 m_imageListNormal
->Draw( image
, dc
,
2646 item
->GetX() + state_w
,
2648 (total_h
> image_h
? (total_h
-image_h
)/2
2650 wxIMAGELIST_DRAW_TRANSPARENT
);
2651 dc
.DestroyClippingRegion();
2654 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
2655 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2656 dc
.DrawText( item
->GetText(),
2657 (wxCoord
)(state_w
+ image_w
+ item
->GetX()),
2658 (wxCoord
)(item
->GetY() + extraH
));
2660 // restore normal font
2661 dc
.SetFont( m_normalFont
);
2663 if (item
== m_dndEffectItem
)
2665 dc
.SetPen( *wxBLACK_PEN
);
2666 // DnD visual effects
2667 switch (m_dndEffect
)
2671 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2672 int w
= item
->GetWidth() + 2;
2673 int h
= total_h
+ 2;
2674 dc
.DrawRectangle( item
->GetX() - 1, item
->GetY() - 1, w
, h
);
2679 int x
= item
->GetX(),
2681 dc
.DrawLine( x
, y
, x
+ item
->GetWidth(), y
);
2686 int x
= item
->GetX(),
2689 dc
.DrawLine( x
, y
, x
+ item
->GetWidth(), y
);
2699 wxGenericTreeCtrl::PaintLevel(wxGenericTreeItem
*item
,
2704 int x
= level
*m_indent
;
2705 if (!HasFlag(wxTR_HIDE_ROOT
))
2709 else if (level
== 0)
2711 // always expand hidden root
2713 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2714 int count
= children
.GetCount();
2720 PaintLevel(children
[n
], dc
, 1, y
);
2721 } while (++n
< count
);
2723 if ( !HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
)
2726 // draw line down to last child
2727 origY
+= GetLineHeight(children
[0])>>1;
2728 oldY
+= GetLineHeight(children
[n
-1])>>1;
2729 dc
.DrawLine(3, origY
, 3, oldY
);
2735 item
->SetX(x
+m_spacing
);
2738 int h
= GetLineHeight(item
);
2740 int y_mid
= y_top
+ (h
>>1);
2743 int exposed_x
= dc
.LogicalToDeviceX(0);
2744 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2746 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2750 // don't draw rect outline if we already have the
2751 // background color under Mac
2752 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2753 #endif // !__WXMAC__
2757 if ( item
->IsSelected()
2758 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2759 // On wxMac, if the tree doesn't have the focus we draw an empty
2760 // rectangle, so we want to make sure that the text is visible
2761 // against the normal background, not the highlightbackground, so
2762 // don't use the highlight text colour unless we have the focus.
2763 && m_hasFocus
&& IsControlActive( (ControlRef
)GetHandle() )
2770 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2775 wxTreeItemAttr
*attr
= item
->GetAttributes();
2776 if (attr
&& attr
->HasTextColour())
2777 colText
= attr
->GetTextColour();
2779 colText
= GetForegroundColour();
2783 dc
.SetTextForeground(colText
);
2787 PaintItem(item
, dc
);
2789 if (HasFlag(wxTR_ROW_LINES
))
2791 // if the background colour is white, choose a
2792 // contrasting color for the lines
2793 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2794 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2795 dc
.DrawLine(0, y_top
, 10000, y_top
);
2796 dc
.DrawLine(0, y
, 10000, y
);
2799 // restore DC objects
2800 dc
.SetBrush(*wxWHITE_BRUSH
);
2801 dc
.SetPen(m_dottedPen
);
2802 dc
.SetTextForeground(*wxBLACK
);
2804 if ( !HasFlag(wxTR_NO_LINES
) )
2806 // draw the horizontal line here
2808 if (x
> (signed)m_indent
)
2809 x_start
-= m_indent
;
2810 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2812 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2815 // should the item show a button?
2816 if ( item
->HasPlus() && HasButtons() )
2818 if ( m_imageListButtons
)
2820 // draw the image button here
2823 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2824 : wxTreeItemIcon_Normal
;
2825 if ( item
->IsSelected() )
2826 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2828 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2829 int xx
= x
- image_w
/2;
2830 int yy
= y_mid
- image_h
/2;
2832 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2833 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2834 wxIMAGELIST_DRAW_TRANSPARENT
);
2836 else // no custom buttons
2838 static const int wImage
= 9;
2839 static const int hImage
= 9;
2842 if (item
->IsExpanded())
2843 flag
|= wxCONTROL_EXPANDED
;
2844 if (item
== m_underMouse
)
2845 flag
|= wxCONTROL_CURRENT
;
2847 wxRendererNative::Get().DrawTreeItemButton
2851 wxRect(x
- wImage
/2,
2860 if (item
->IsExpanded())
2862 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2863 int count
= children
.GetCount();
2870 PaintLevel(children
[n
], dc
, level
, y
);
2871 } while (++n
< count
);
2873 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2875 // draw line down to last child
2876 oldY
+= GetLineHeight(children
[n
-1])>>1;
2877 if (HasButtons()) y_mid
+= 5;
2879 // Only draw the portion of the line that is visible, in case
2881 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2882 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2883 yOrigin
= abs(yOrigin
);
2884 GetClientSize(&width
, &height
);
2886 // Move end points to the begining/end of the view?
2887 if (y_mid
< yOrigin
)
2889 if (oldY
> yOrigin
+ height
)
2890 oldY
= yOrigin
+ height
;
2892 // after the adjustments if y_mid is larger than oldY then the
2893 // line isn't visible at all so don't draw anything
2895 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2901 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2905 if ( item
->HasPlus() )
2907 // it's a folder, indicate it by a border
2912 // draw a line under the drop target because the item will be
2914 DrawLine(item
, !m_dropEffectAboveItem
);
2917 SetCursor(*wxSTANDARD_CURSOR
);
2922 SetCursor(wxCURSOR_NO_ENTRY
);
2926 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2928 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2930 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2932 if (m_dndEffect
== NoEffect
)
2934 m_dndEffect
= BorderEffect
;
2935 m_dndEffectItem
= i
;
2939 m_dndEffect
= NoEffect
;
2940 m_dndEffectItem
= NULL
;
2943 wxRect
rect( i
->GetX()-1, i
->GetY()-1, i
->GetWidth()+2, GetLineHeight(i
)+2 );
2944 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2945 RefreshRect( rect
);
2948 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2950 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2952 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2954 if (m_dndEffect
== NoEffect
)
2957 m_dndEffect
= BelowEffect
;
2959 m_dndEffect
= AboveEffect
;
2960 m_dndEffectItem
= i
;
2964 m_dndEffect
= NoEffect
;
2965 m_dndEffectItem
= NULL
;
2968 wxRect
rect( i
->GetX()-1, i
->GetY()-1, i
->GetWidth()+2, GetLineHeight(i
)+2 );
2969 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2970 RefreshRect( rect
);
2973 // -----------------------------------------------------------------------------
2974 // wxWidgets callbacks
2975 // -----------------------------------------------------------------------------
2977 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2980 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2981 RefreshLine( m_current
);
2987 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2995 dc
.SetFont( m_normalFont
);
2996 dc
.SetPen( m_dottedPen
);
2998 // this is now done dynamically
2999 //if(GetImageList() == NULL)
3000 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3003 PaintLevel( m_anchor
, dc
, 0, y
);
3006 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
3015 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
3024 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
3026 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
3027 te
.m_evtKey
= event
;
3028 if ( GetEventHandler()->ProcessEvent( te
) )
3030 // intercepted by the user code
3034 if ( (m_current
== 0) || (m_key_current
== 0) )
3040 // how should the selection work for this event?
3041 bool is_multiple
, extended_select
, unselect_others
;
3042 EventFlagsToSelType(GetWindowStyleFlag(),
3045 is_multiple
, extended_select
, unselect_others
);
3047 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3049 if (event
.GetKeyCode() == WXK_RIGHT
)
3050 event
.m_keyCode
= WXK_LEFT
;
3051 else if (event
.GetKeyCode() == WXK_LEFT
)
3052 event
.m_keyCode
= WXK_RIGHT
;
3057 // * : Expand all/Collapse all
3058 // ' ' | return : activate
3059 // up : go up (not last children!)
3061 // left : go to parent
3062 // right : open if parent and go next
3063 // home : go to root
3064 // end : go to last item without opening parents
3065 // alnum : start or continue searching for the item with this prefix
3066 int keyCode
= event
.GetKeyCode();
3069 // Make the keys work as they do in the native control:
3071 // left => collapse if current item is expanded
3072 if (keyCode
== WXK_RIGHT
)
3076 else if (keyCode
== WXK_LEFT
&& IsExpanded(m_current
))
3086 if (m_current
->HasPlus() && !IsExpanded(m_current
))
3094 if ( !IsExpanded(m_current
) )
3097 ExpandAllChildren(m_current
);
3100 //else: fall through to Collapse() it
3104 if (IsExpanded(m_current
))
3106 Collapse(m_current
);
3112 // Use the item's bounding rectangle to determine position for
3115 GetBoundingRect(m_current
, ItemRect
, true);
3118 eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
3119 // Use the left edge, vertical middle
3120 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
3122 ItemRect
.GetHeight() / 2);
3123 GetEventHandler()->ProcessEvent( eventMenu
);
3129 if ( !event
.HasModifiers() )
3132 eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
3133 GetEventHandler()->ProcessEvent( eventAct
);
3136 // in any case, also generate the normal key event for this key,
3137 // even if we generated the ACTIVATED event above: this is what
3138 // wxMSW does and it makes sense because you might not want to
3139 // process ACTIVATED event at all and handle Space and Return
3140 // directly (and differently) which would be impossible otherwise
3144 // up goes to the previous sibling or to the last
3145 // of its children if it's expanded
3148 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
3151 prev
= GetItemParent( m_key_current
);
3152 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3154 break; // don't go to root if it is hidden
3158 wxTreeItemIdValue cookie
;
3159 wxTreeItemId current
= m_key_current
;
3160 // TODO: Huh? If we get here, we'd better be the first
3161 // child of our parent. How else could it be?
3162 if (current
== GetFirstChild( prev
, cookie
))
3164 // otherwise we return to where we came from
3168 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
3175 while ( IsExpanded(prev
) && HasChildren(prev
) )
3177 wxTreeItemId child
= GetLastChild(prev
);
3184 DoSelectItem( prev
, unselect_others
, extended_select
);
3185 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
3190 // left arrow goes to the parent
3193 wxTreeItemId prev
= GetItemParent( m_current
);
3194 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3196 // don't go to root if it is hidden
3197 prev
= GetPrevSibling( m_current
);
3201 DoSelectItem( prev
, unselect_others
, extended_select
);
3207 // this works the same as the down arrow except that we
3208 // also expand the item if it wasn't expanded yet
3209 if (m_current
!= GetRootItem().m_pItem
|| !HasFlag(wxTR_HIDE_ROOT
))
3211 //else: don't try to expand hidden root item (which can be the
3212 // current one when the tree is empty)
3218 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
3220 wxTreeItemIdValue cookie
;
3221 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
3225 DoSelectItem( child
, unselect_others
, extended_select
);
3226 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
3230 wxTreeItemId next
= GetNextSibling( m_key_current
);
3233 wxTreeItemId current
= m_key_current
;
3234 while (current
.IsOk() && !next
)
3236 current
= GetItemParent( current
);
3237 if (current
) next
= GetNextSibling( current
);
3242 DoSelectItem( next
, unselect_others
, extended_select
);
3243 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
3249 // <End> selects the last visible tree item
3252 wxTreeItemId last
= GetRootItem();
3254 while ( last
.IsOk() && IsExpanded(last
) )
3256 wxTreeItemId lastChild
= GetLastChild(last
);
3258 // it may happen if the item was expanded but then all of
3259 // its children have been deleted - so IsExpanded() returned
3260 // true, but GetLastChild() returned invalid item
3269 DoSelectItem( last
, unselect_others
, extended_select
);
3274 // <Home> selects the root item
3277 wxTreeItemId prev
= GetRootItem();
3281 if ( HasFlag(wxTR_HIDE_ROOT
) )
3283 wxTreeItemIdValue cookie
;
3284 prev
= GetFirstChild(prev
, cookie
);
3289 DoSelectItem( prev
, unselect_others
, extended_select
);
3294 // do not use wxIsalnum() here
3295 if ( !event
.HasModifiers() &&
3296 ((keyCode
>= '0' && keyCode
<= '9') ||
3297 (keyCode
>= 'a' && keyCode
<= 'z') ||
3298 (keyCode
>= 'A' && keyCode
<= 'Z' )))
3300 // find the next item starting with the given prefix
3301 wxChar ch
= (wxChar
)keyCode
;
3303 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
3314 // also start the timer to reset the current prefix if the user
3315 // doesn't press any more alnum keys soon -- we wouldn't want
3316 // to use this prefix for a new item search
3319 m_findTimer
= new wxTreeFindTimer(this);
3322 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
3332 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
3337 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
3338 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
3339 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
3340 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
3341 if (flags
) return wxTreeItemId();
3343 if (m_anchor
== NULL
)
3345 flags
= wxTREE_HITTEST_NOWHERE
;
3346 return wxTreeItemId();
3349 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
3353 flags
= wxTREE_HITTEST_NOWHERE
;
3354 return wxTreeItemId();
3359 // get the bounding rectangle of the item (or of its label only)
3360 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
3362 bool textOnly
) const
3364 wxCHECK_MSG( item
.IsOk(), false,
3365 "invalid item in wxGenericTreeCtrl::GetBoundingRect" );
3367 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
3371 int image_h
= 0, image_w
= 0;
3372 int image
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetCurrentImage();
3373 if ( image
!= NO_IMAGE
&& m_imageListNormal
)
3375 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3376 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3379 int state_h
= 0, state_w
= 0;
3380 int state
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetState();
3381 if ( state
!= wxTREE_ITEMSTATE_NONE
&& m_imageListState
)
3383 m_imageListState
->GetSize( state
, state_w
, state_h
);
3385 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
3387 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3390 rect
.x
= i
->GetX() + state_w
+ image_w
;
3391 rect
.width
= i
->GetWidth() - state_w
- image_w
;
3394 else // the entire line
3397 rect
.width
= GetClientSize().x
;
3401 rect
.height
= GetLineHeight(i
);
3403 // we have to return the logical coordinates, not physical ones
3404 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
3409 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
3410 wxClassInfo
* WXUNUSED(textCtrlClass
))
3412 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("can't edit an invalid item") );
3414 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
3416 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
3417 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
3423 // We have to call this here because the label in
3424 // question might just have been added and no screen
3425 // update taken place.
3427 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3430 DoDirtyProcessing();
3433 // TODO: use textCtrlClass here to create the control of correct class
3434 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3436 m_textCtrl
->SetFocus();
3441 // returns a pointer to the text edit control if the item is being
3442 // edited, NULL otherwise (it's assumed that no more than one item may
3443 // be edited simultaneously)
3444 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3449 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3450 bool discardChanges
)
3452 wxCHECK_RET( m_textCtrl
, wxT("not editing label") );
3454 m_textCtrl
->EndEdit(discardChanges
);
3457 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3458 const wxString
& value
)
3460 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3462 le
.m_editCancelled
= false;
3464 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3467 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3469 // let owner know that the edit was cancelled
3470 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3471 le
.m_label
= wxEmptyString
;
3472 le
.m_editCancelled
= true;
3474 GetEventHandler()->ProcessEvent( le
);
3477 void wxGenericTreeCtrl::OnRenameTimer()
3479 EditLabel( m_current
);
3482 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3484 if ( !m_anchor
)return;
3486 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3488 // Is the mouse over a tree item button?
3490 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3491 wxGenericTreeItem
*underMouse
= thisItem
;
3493 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3494 #endif // wxUSE_TOOLTIPS
3497 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3498 (!event
.LeftIsDown()) &&
3500 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3508 if (underMouse
!= m_underMouse
)
3512 // unhighlight old item
3513 wxGenericTreeItem
*tmp
= m_underMouse
;
3514 m_underMouse
= NULL
;
3518 m_underMouse
= underMouse
;
3520 RefreshLine( m_underMouse
);
3524 // Determines what item we are hovering over and need a tooltip for
3525 wxTreeItemId hoverItem
= thisItem
;
3527 // We do not want a tooltip if we are dragging, or if the rename timer is
3529 if ( underMouseChanged
&&
3532 (!m_renameTimer
|| !m_renameTimer
->IsRunning()) )
3534 // Ask the tree control what tooltip (if any) should be shown
3536 hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3538 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3540 SetToolTip(hevent
.m_label
);
3545 // we process left mouse up event (enables in-place edit), middle/right down
3546 // (pass to the user code), left dbl click (activate item) and
3547 // dragging/moving events for items drag-and-drop
3548 if ( !(event
.LeftDown() ||
3550 event
.MiddleDown() ||
3551 event
.RightDown() ||
3552 event
.LeftDClick() ||
3554 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3563 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3565 if ( event
.Dragging() && !m_isDragging
)
3567 if (m_dragCount
== 0)
3572 if (m_dragCount
!= 3)
3574 // wait until user drags a bit further...
3578 wxEventType command
= event
.RightIsDown()
3579 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3580 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3582 wxTreeEvent
nevent(command
, this, m_current
);
3583 nevent
.SetPoint(CalcScrolledPosition(pt
));
3585 // by default the dragging is not supported, the user code must
3586 // explicitly allow the event for it to take place
3589 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3591 // we're going to drag this item
3592 m_isDragging
= true;
3594 // remember the old cursor because we will change it while
3596 m_oldCursor
= m_cursor
;
3598 // in a single selection control, hide the selection temporarily
3599 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3601 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3603 if ( m_oldSelection
)
3605 m_oldSelection
->SetHilight(false);
3606 RefreshLine(m_oldSelection
);
3613 else if ( event
.Dragging() )
3615 if ( item
!= m_dropTarget
)
3617 // unhighlight the previous drop target
3618 DrawDropEffect(m_dropTarget
);
3620 m_dropTarget
= item
;
3622 // highlight the current drop target if any
3623 DrawDropEffect(m_dropTarget
);
3625 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3628 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3629 // instead (needs to be tested!)
3634 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3638 // erase the highlighting
3639 DrawDropEffect(m_dropTarget
);
3641 if ( m_oldSelection
)
3643 m_oldSelection
->SetHilight(true);
3644 RefreshLine(m_oldSelection
);
3645 m_oldSelection
= NULL
;
3648 // generate the drag end event
3649 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3651 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3653 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3655 m_isDragging
= false;
3656 m_dropTarget
= NULL
;
3658 SetCursor(m_oldCursor
);
3660 #if defined( __WXMSW__ ) || defined(__WXMAC__) || defined(__WXGTK20__)
3663 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3664 // instead (needs to be tested!)
3670 // If we got to this point, we are not dragging or moving the mouse.
3671 // Because the code in carbon/toplevel.cpp will only set focus to the
3672 // tree if we skip for EVT_LEFT_DOWN, we MUST skip this event here for
3674 // We skip even if we didn't hit an item because we still should
3675 // restore focus to the tree control even if we didn't exactly hit an
3677 if ( event
.LeftDown() )
3682 // here we process only the messages which happen on tree items
3686 if (item
== NULL
) return; /* we hit the blank area */
3688 if ( event
.RightDown() )
3690 // If the item is already selected, do not update the selection.
3691 // Multi-selections should not be cleared if a selected item is
3693 if (!IsSelected(item
))
3695 DoSelectItem(item
, true, false);
3699 nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3700 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3701 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3703 // Consistent with MSW (for now), send the ITEM_MENU *after*
3704 // the RIGHT_CLICK event. TODO: This behavior may change.
3705 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3706 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3707 GetEventHandler()->ProcessEvent(nevent2
);
3709 else if ( event
.MiddleDown() )
3712 nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3713 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3714 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3716 else if ( event
.LeftUp() )
3718 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3721 nevent(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, this, item
);
3722 GetEventHandler()->ProcessEvent(nevent
);
3725 // this facilitates multiple-item drag-and-drop
3727 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3729 wxArrayTreeItemIds selections
;
3730 size_t count
= GetSelections(selections
);
3736 DoSelectItem(item
, true, false);
3742 if ( (item
== m_current
) &&
3743 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3744 HasFlag(wxTR_EDIT_LABELS
) )
3746 if ( m_renameTimer
)
3748 if ( m_renameTimer
->IsRunning() )
3749 m_renameTimer
->Stop();
3753 m_renameTimer
= new wxTreeRenameTimer( this );
3756 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3759 m_lastOnSame
= false;
3762 else // !RightDown() && !MiddleDown() && !LeftUp()
3764 // ==> LeftDown() || LeftDClick()
3765 if ( event
.LeftDown() )
3767 m_lastOnSame
= item
== m_current
;
3770 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3772 // only toggle the item for a single click, double click on
3773 // the button doesn't do anything (it toggles the item twice)
3774 if ( event
.LeftDown() )
3779 // don't select the item if the button was clicked
3784 // clear the previously selected items, if the
3785 // user clicked outside of the present selection.
3786 // otherwise, perform the deselection on mouse-up.
3787 // this allows multiple drag and drop to work.
3788 // but if Cmd is down, toggle selection of the clicked item
3789 if (!IsSelected(item
) || event
.CmdDown())
3791 // how should the selection work for this event?
3792 bool is_multiple
, extended_select
, unselect_others
;
3793 EventFlagsToSelType(GetWindowStyleFlag(),
3800 DoSelectItem(item
, unselect_others
, extended_select
);
3804 // For some reason, Windows isn't recognizing a left double-click,
3805 // so we need to simulate it here. Allow 200 milliseconds for now.
3806 if ( event
.LeftDClick() )
3808 // double clicking should not start editing the item label
3809 if ( m_renameTimer
)
3810 m_renameTimer
->Stop();
3812 m_lastOnSame
= false;
3814 // send activate event first
3816 nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3817 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3818 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3820 // if the user code didn't process the activate event,
3821 // handle it ourselves by toggling the item when it is
3823 if ( item
->HasPlus() )
3833 void wxGenericTreeCtrl::OnInternalIdle()
3835 wxWindow::OnInternalIdle();
3837 // Check if we need to select the root item
3838 // because nothing else has been selected.
3839 // Delaying it means that we can invoke event handlers
3840 // as required, when a first item is selected.
3841 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3844 SelectItem(m_select_me
);
3845 else if (GetRootItem().IsOk())
3846 SelectItem(GetRootItem());
3849 // after all changes have been done to the tree control,
3850 // actually redraw the tree when everything is over
3852 DoDirtyProcessing();
3856 wxGenericTreeCtrl::CalculateLevel(wxGenericTreeItem
*item
,
3861 int x
= level
*m_indent
;
3862 if (!HasFlag(wxTR_HIDE_ROOT
))
3866 else if (level
== 0)
3868 // a hidden root is not evaluated, but its
3869 // children are always calculated
3873 item
->CalculateSize(this, dc
);
3876 item
->SetX( x
+m_spacing
);
3878 y
+= GetLineHeight(item
);
3880 if ( !item
->IsExpanded() )
3882 // we don't need to calculate collapsed branches
3887 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3888 size_t n
, count
= children
.GetCount();
3890 for (n
= 0; n
< count
; ++n
)
3891 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3894 void wxGenericTreeCtrl::CalculatePositions()
3896 if ( !m_anchor
) return;
3898 wxClientDC
dc(this);
3901 dc
.SetFont( m_normalFont
);
3903 dc
.SetPen( m_dottedPen
);
3904 //if(GetImageList() == NULL)
3905 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3908 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3911 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3914 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3917 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3919 if (m_dirty
|| IsFrozen() )
3922 wxSize client
= GetClientSize();
3925 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3926 rect
.width
= client
.x
;
3927 rect
.height
= client
.y
;
3929 Refresh(true, &rect
);
3931 AdjustMyScrollbars();
3934 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3936 if (m_dirty
|| IsFrozen() )
3940 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3941 rect
.width
= GetClientSize().x
;
3942 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3944 Refresh(true, &rect
);
3947 void wxGenericTreeCtrl::RefreshSelected()
3952 // TODO: this is awfully inefficient, we should keep the list of all
3953 // selected items internally, should be much faster
3955 RefreshSelectedUnder(m_anchor
);
3958 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3963 if ( item
->IsSelected() )
3966 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3967 size_t count
= children
.GetCount();
3968 for ( size_t n
= 0; n
< count
; n
++ )
3970 RefreshSelectedUnder(children
[n
]);
3974 void wxGenericTreeCtrl::DoThaw()
3976 wxTreeCtrlBase::DoThaw();
3979 DoDirtyProcessing();
3984 // ----------------------------------------------------------------------------
3985 // changing colours: we need to refresh the tree control
3986 // ----------------------------------------------------------------------------
3988 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3990 if ( !wxWindow::SetBackgroundColour(colour
) )
3998 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
4000 if ( !wxWindow::SetForegroundColour(colour
) )
4008 // Process the tooltip event, to speed up event processing.
4009 // Doesn't actually get a tooltip.
4010 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
4016 // NOTE: If using the wxListBox visual attributes works everywhere then this can
4017 // be removed, as well as the #else case below.
4018 #define _USE_VISATTR 0
4023 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
4025 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
4029 // Use the same color scheme as wxListBox
4030 return wxListBox::GetClassDefaultAttributes(variant
);
4032 wxVisualAttributes attr
;
4033 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
4034 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
4035 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
4040 void wxGenericTreeCtrl::DoDirtyProcessing()
4047 CalculatePositions();
4049 AdjustMyScrollbars();
4052 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
4054 // make sure all positions are calculated as normally this only done during
4055 // idle time but we need them for base class DoGetBestSize() to return the
4057 wxConstCast(this, wxGenericTreeCtrl
)->CalculatePositions();
4059 wxSize size
= wxTreeCtrlBase::DoGetBestSize();
4061 // there seems to be an implicit extra border around the items, although
4062 // I'm not really sure where does it come from -- but without this, the
4063 // scrollbars appear in a tree with default/best size
4066 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
4067 // scrollbars still appear
4068 const wxSize
& borderSize
= GetWindowBorderSize();
4070 int dx
= (size
.x
- borderSize
.x
) % PIXELS_PER_UNIT
;
4072 size
.x
+= PIXELS_PER_UNIT
- dx
;
4073 int dy
= (size
.y
- borderSize
.y
) % PIXELS_PER_UNIT
;
4075 size
.y
+= PIXELS_PER_UNIT
- dy
;
4077 // we need to update the cache too as the base class cached its own value
4078 CacheBestSize(size
);
4083 #endif // wxUSE_TREECTRL