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)
7 // Copyright: (c) 1998 Robert Roebling and Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // =============================================================================
13 // =============================================================================
15 // -----------------------------------------------------------------------------
17 // -----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/treectrl.h"
31 #include "wx/dcclient.h"
33 #include "wx/settings.h"
34 #include "wx/listbox.h"
35 #include "wx/textctrl.h"
38 #include "wx/generic/treectlg.h"
39 #include "wx/imaglist.h"
41 #include "wx/renderer.h"
44 #include "wx/osx/private.h"
47 // -----------------------------------------------------------------------------
49 // -----------------------------------------------------------------------------
51 class WXDLLIMPEXP_FWD_CORE wxGenericTreeItem
;
53 WX_DEFINE_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 static const int NO_IMAGE
= -1;
61 static const int PIXELS_PER_UNIT
= 10;
63 // the margin between the item state image and the item normal image
64 static const int MARGIN_BETWEEN_STATE_AND_IMAGE
= 2;
66 // the margin between the item image and the item text
67 static const int MARGIN_BETWEEN_IMAGE_AND_TEXT
= 4;
69 // -----------------------------------------------------------------------------
71 // -----------------------------------------------------------------------------
73 // timer used for enabling in-place edit
74 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
77 // start editing the current item after half a second (if the mouse hasn't
78 // been clicked/moved)
81 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
83 virtual void Notify();
86 wxGenericTreeCtrl
*m_owner
;
88 wxDECLARE_NO_COPY_CLASS(wxTreeRenameTimer
);
91 // control used for in-place edit
92 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
95 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
97 void EndEdit( bool discardChanges
);
99 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
102 void OnChar( wxKeyEvent
&event
);
103 void OnKeyUp( wxKeyEvent
&event
);
104 void OnKillFocus( wxFocusEvent
&event
);
106 bool AcceptChanges();
107 void Finish( bool setfocus
);
110 wxGenericTreeCtrl
*m_owner
;
111 wxGenericTreeItem
*m_itemEdited
;
112 wxString m_startValue
;
113 bool m_aboutToFinish
;
115 DECLARE_EVENT_TABLE()
116 wxDECLARE_NO_COPY_CLASS(wxTreeTextCtrl
);
119 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
120 // for a sufficiently long time
121 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
124 // reset the current prefix after half a second of inactivity
125 enum { DELAY
= 500 };
127 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
129 virtual void Notify() { m_owner
->ResetFindState(); }
132 wxGenericTreeCtrl
*m_owner
;
134 wxDECLARE_NO_COPY_CLASS(wxTreeFindTimer
);
138 class WXDLLEXPORT wxGenericTreeItem
149 wxGenericTreeItem( wxGenericTreeItem
*parent
,
150 const wxString
& text
,
153 wxTreeItemData
*data
);
155 ~wxGenericTreeItem();
158 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
160 const wxString
& GetText() const { return m_text
; }
161 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
162 { return m_images
[which
]; }
163 wxTreeItemData
*GetData() const { return m_data
; }
164 int GetState() const { return m_state
; }
166 // returns the current image for the item (depending on its
167 // selected/expanded/whatever state)
168 int GetCurrentImage() const;
170 void SetText(const wxString
& text
)
177 void SetImage(int image
, wxTreeItemIcon which
)
179 m_images
[which
] = image
;
183 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
184 void SetState(int state
) { m_state
= state
; m_width
= 0; }
186 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
188 void SetBold(bool bold
)
195 int GetX() const { return m_x
; }
196 int GetY() const { return m_y
; }
198 void SetX(int x
) { m_x
= x
; }
199 void SetY(int y
) { m_y
= y
; }
201 int GetHeight() const { return m_height
; }
202 int GetWidth() const { return m_width
; }
204 int GetTextHeight() const
206 wxASSERT_MSG( m_heightText
!= -1, "must call CalculateSize() first" );
211 int GetTextWidth() const
213 wxASSERT_MSG( m_widthText
!= -1, "must call CalculateSize() first" );
218 wxGenericTreeItem
*GetParent() const { return m_parent
; }
220 // sets the items font for the specified DC if it uses any special font or
221 // simply returns false otherwise
222 bool SetFont(wxGenericTreeCtrl
*control
, wxDC
& dc
) const
226 wxTreeItemAttr
* const attr
= GetAttributes();
227 if ( attr
&& attr
->HasFont() )
228 font
= attr
->GetFont();
230 font
= control
->m_boldFont
;
240 // deletes all children notifying the treectrl about it
241 void DeleteChildren(wxGenericTreeCtrl
*tree
);
243 // get count of all children (and grand children if 'recursively')
244 size_t GetChildrenCount(bool recursively
= true) const;
246 void Insert(wxGenericTreeItem
*child
, size_t index
)
247 { m_children
.Insert(child
, index
); }
249 // calculate and cache the item size using either the provided DC (which is
250 // supposed to have wxGenericTreeCtrl::m_normalFont selected into it!) or a
251 // wxClientDC on the control window
252 void CalculateSize(wxGenericTreeCtrl
*control
, wxDC
& dc
)
253 { DoCalculateSize(control
, dc
, true /* dc uses normal font */); }
254 void CalculateSize(wxGenericTreeCtrl
*control
);
256 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
258 void ResetSize() { m_width
= 0; }
259 void ResetTextSize() { m_width
= 0; m_widthText
= -1; }
260 void RecursiveResetSize();
261 void RecursiveResetTextSize();
263 // return the item at given position (or NULL if no item), onButton is
264 // true if the point belongs to the item's button, otherwise it lies
265 // on the item's label
266 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
267 const wxGenericTreeCtrl
*,
271 void Expand() { m_isCollapsed
= false; }
272 void Collapse() { m_isCollapsed
= true; }
274 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
277 bool HasChildren() const { return !m_children
.IsEmpty(); }
278 bool IsSelected() const { return m_hasHilight
!= 0; }
279 bool IsExpanded() const { return !m_isCollapsed
; }
280 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
281 bool IsBold() const { return m_isBold
!= 0; }
284 // get them - may be NULL
285 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
286 // get them ensuring that the pointer is not NULL
287 wxTreeItemAttr
& Attr()
291 m_attr
= new wxTreeItemAttr
;
297 void SetAttributes(wxTreeItemAttr
*attr
)
299 if ( m_ownsAttr
) delete m_attr
;
305 // set them and delete when done
306 void AssignAttributes(wxTreeItemAttr
*attr
)
315 // calculate the size of this item, i.e. set m_width, m_height and
316 // m_widthText and m_heightText properly
318 // if dcUsesNormalFont is true, the current dc font must be the normal tree
320 void DoCalculateSize(wxGenericTreeCtrl
*control
,
322 bool dcUsesNormalFont
);
324 // since there can be very many of these, we save size by chosing
325 // the smallest representation for the elements and by ordering
326 // the members to avoid padding.
327 wxString m_text
; // label to be rendered for item
331 wxTreeItemData
*m_data
; // user-provided data
333 int m_state
; // item state
335 wxArrayGenericTreeItems m_children
; // list of children
336 wxGenericTreeItem
*m_parent
; // parent of this item
338 wxTreeItemAttr
*m_attr
; // attributes???
340 // tree ctrl images for the normal, selected, expanded and
341 // expanded+selected states
342 int m_images
[wxTreeItemIcon_Max
];
344 wxCoord m_x
; // (virtual) offset from top
345 wxCoord m_y
; // (virtual) offset from left
346 int m_width
; // width of this item
347 int m_height
; // height of this item
349 // use bitfields to save size
350 unsigned int m_isCollapsed
:1;
351 unsigned int m_hasHilight
:1; // same as focused
352 unsigned int m_hasPlus
:1; // used for item which doesn't have
353 // children but has a [+] button
354 unsigned int m_isBold
:1; // render the label in bold font
355 unsigned int m_ownsAttr
:1; // delete attribute when done
357 wxDECLARE_NO_COPY_CLASS(wxGenericTreeItem
);
360 // =============================================================================
362 // =============================================================================
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 // translate the key or mouse event flags to the type of selection we're
370 static void EventFlagsToSelType(long style
,
374 bool &extended_select
,
375 bool &unselect_others
)
377 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
378 extended_select
= shiftDown
&& is_multiple
;
379 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
382 // check if the given item is under another one
384 IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
388 if ( item
== parent
)
390 // item is a descendant of parent
394 item
= item
->GetParent();
400 // -----------------------------------------------------------------------------
401 // wxTreeRenameTimer (internal)
402 // -----------------------------------------------------------------------------
404 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
409 void wxTreeRenameTimer::Notify()
411 m_owner
->OnRenameTimer();
414 //-----------------------------------------------------------------------------
415 // wxTreeTextCtrl (internal)
416 //-----------------------------------------------------------------------------
418 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
419 EVT_CHAR (wxTreeTextCtrl::OnChar
)
420 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
421 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
424 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
425 wxGenericTreeItem
*itm
)
426 : m_itemEdited(itm
), m_startValue(itm
->GetText())
429 m_aboutToFinish
= false;
432 m_owner
->GetBoundingRect(m_itemEdited
, rect
, true);
434 // corrects position and size for better appearance
438 #elif defined(__WXGTK__)
443 #elif defined(wxOSX_USE_CARBON) && wxOSX_USE_CARBON
444 int bestHeight
= GetBestSize().y
- 8;
445 if ( rect
.height
> bestHeight
)
447 int diff
= rect
.height
- bestHeight
;
453 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
454 rect
.GetPosition(), rect
.GetSize());
459 void wxTreeTextCtrl::EndEdit(bool discardChanges
)
461 m_aboutToFinish
= true;
463 if ( discardChanges
)
465 m_owner
->OnRenameCancelled(m_itemEdited
);
471 // Notify the owner about the changes
474 // Even if vetoed, close the control (consistent with MSW)
479 bool wxTreeTextCtrl::AcceptChanges()
481 const wxString value
= GetValue();
483 if ( value
== m_startValue
)
485 // nothing changed, always accept
486 // when an item remains unchanged, the owner
487 // needs to be notified that the user decided
488 // not to change the tree item label, and that
489 // the edit has been cancelled
491 m_owner
->OnRenameCancelled(m_itemEdited
);
495 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
497 // vetoed by the user
501 // accepted, do rename the item
502 m_owner
->SetItemText(m_itemEdited
, value
);
507 void wxTreeTextCtrl::Finish( bool setfocus
)
509 m_owner
->ResetTextControl();
511 wxPendingDelete
.Append(this);
517 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
519 switch ( event
.m_keyCode
)
534 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
536 if ( !m_aboutToFinish
)
538 // auto-grow the textctrl:
539 wxSize parentSize
= m_owner
->GetSize();
540 wxPoint myPos
= GetPosition();
541 wxSize mySize
= GetSize();
543 GetTextExtent(GetValue() + wxT("M"), &sx
, &sy
);
544 if (myPos
.x
+ sx
> parentSize
.x
)
545 sx
= parentSize
.x
- myPos
.x
;
548 SetSize(sx
, wxDefaultCoord
);
554 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
556 if ( !m_aboutToFinish
)
558 if ( !AcceptChanges() )
559 m_owner
->OnRenameCancelled( m_itemEdited
);
564 // We should let the native text control handle focus, too.
568 // -----------------------------------------------------------------------------
570 // -----------------------------------------------------------------------------
572 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
573 const wxString
& text
,
574 int image
, int selImage
,
575 wxTreeItemData
*data
)
578 m_images
[wxTreeItemIcon_Normal
] = image
;
579 m_images
[wxTreeItemIcon_Selected
] = selImage
;
580 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
581 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
584 m_state
= wxTREE_ITEMSTATE_NONE
;
587 m_isCollapsed
= true;
588 m_hasHilight
= false;
597 // We don't know the height here yet.
605 wxGenericTreeItem::~wxGenericTreeItem()
609 if (m_ownsAttr
) delete m_attr
;
611 wxASSERT_MSG( m_children
.IsEmpty(),
612 "must call DeleteChildren() before deleting the item" );
615 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
617 size_t count
= m_children
.GetCount();
618 for ( size_t n
= 0; n
< count
; n
++ )
620 wxGenericTreeItem
*child
= m_children
[n
];
621 tree
->SendDeleteEvent(child
);
623 child
->DeleteChildren(tree
);
624 if ( child
== tree
->m_select_me
)
625 tree
->m_select_me
= NULL
;
632 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
634 size_t count
= m_children
.GetCount();
638 size_t total
= count
;
639 for (size_t n
= 0; n
< count
; ++n
)
641 total
+= m_children
[n
]->GetChildrenCount();
647 void wxGenericTreeItem::GetSize( int &x
, int &y
,
648 const wxGenericTreeCtrl
*theButton
)
650 int bottomY
=m_y
+theButton
->GetLineHeight(this);
651 if ( y
< bottomY
) y
= bottomY
;
652 int width
= m_x
+ m_width
;
653 if ( x
< width
) x
= width
;
657 size_t count
= m_children
.GetCount();
658 for ( size_t n
= 0; n
< count
; ++n
)
660 m_children
[n
]->GetSize( x
, y
, theButton
);
665 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
666 const wxGenericTreeCtrl
*theCtrl
,
670 // for a hidden root node, don't evaluate it, but do evaluate children
671 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
674 int h
= theCtrl
->GetLineHeight(this);
675 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
677 int y_mid
= m_y
+ h
/2;
678 if (point
.y
< y_mid
)
679 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
681 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
683 int xCross
= m_x
- theCtrl
->GetSpacing();
685 // according to the drawing code the triangels are drawn
686 // at -4 , -4 from the position up to +10/+10 max
687 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
688 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
689 HasPlus() && theCtrl
->HasButtons() )
691 // 5 is the size of the plus sign
692 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
693 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
694 HasPlus() && theCtrl
->HasButtons() )
697 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
701 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
706 // assuming every image (normal and selected) has the same size!
707 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
709 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
716 if ( (GetState() != wxTREE_ITEMSTATE_NONE
) &&
717 theCtrl
->m_imageListState
)
719 theCtrl
->m_imageListState
->GetSize(GetState(),
723 if ((state_w
!= -1) && (point
.x
<= m_x
+ state_w
+ 1))
724 flags
|= wxTREE_HITTEST_ONITEMSTATEICON
;
725 else if ((image_w
!= -1) &&
727 (state_w
!= -1 ? state_w
+
728 MARGIN_BETWEEN_STATE_AND_IMAGE
731 flags
|= wxTREE_HITTEST_ONITEMICON
;
733 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
739 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
740 if (point
.x
> m_x
+m_width
)
741 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
746 // if children are expanded, fall through to evaluate them
747 if (m_isCollapsed
) return NULL
;
751 size_t count
= m_children
.GetCount();
752 for ( size_t n
= 0; n
< count
; n
++ )
754 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
765 int wxGenericTreeItem::GetCurrentImage() const
767 int image
= NO_IMAGE
;
772 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
775 if ( image
== NO_IMAGE
)
777 // we usually fall back to the normal item, but try just the
778 // expanded one (and not selected) first in this case
779 image
= GetImage(wxTreeItemIcon_Expanded
);
785 image
= GetImage(wxTreeItemIcon_Selected
);
788 // maybe it doesn't have the specific image we want,
789 // try the default one instead
790 if ( image
== NO_IMAGE
) image
= GetImage();
795 void wxGenericTreeItem::CalculateSize(wxGenericTreeCtrl
* control
)
797 // check if we need to do anything before creating the DC
801 wxClientDC
dc(control
);
802 DoCalculateSize(control
, dc
, false /* normal font not used */);
806 wxGenericTreeItem::DoCalculateSize(wxGenericTreeCtrl
* control
,
808 bool dcUsesNormalFont
)
810 if ( m_width
!= 0 ) // Size known, nothing to do
813 if ( m_widthText
== -1 )
816 if ( SetFont(control
, dc
) )
820 else // we have no special font
822 if ( !dcUsesNormalFont
)
824 // but we do need to ensure that the normal font is used: notice
825 // that this doesn't count as changing the font as we don't need
827 dc
.SetFont(control
->m_normalFont
);
833 dc
.GetTextExtent( GetText(), &m_widthText
, &m_heightText
);
835 // restore normal font if the DC used it previously and we changed it
837 dc
.SetFont(control
->m_normalFont
);
840 int text_h
= m_heightText
+ 2;
842 int image_h
= 0, image_w
= 0;
843 int image
= GetCurrentImage();
844 if ( image
!= NO_IMAGE
&& control
->m_imageListNormal
)
846 control
->m_imageListNormal
->GetSize(image
, image_w
, image_h
);
847 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
850 int state_h
= 0, state_w
= 0;
851 int state
= GetState();
852 if ( state
!= wxTREE_ITEMSTATE_NONE
&& control
->m_imageListState
)
854 control
->m_imageListState
->GetSize(state
, state_w
, state_h
);
856 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
858 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
861 int img_h
= wxMax(state_h
, image_h
);
862 m_height
= wxMax(img_h
, text_h
);
865 m_height
+= 2; // at least 2 pixels
867 m_height
+= m_height
/ 10; // otherwise 10% extra spacing
869 if (m_height
> control
->m_lineHeight
)
870 control
->m_lineHeight
= m_height
;
872 m_width
= state_w
+ image_w
+ m_widthText
+ 2;
875 void wxGenericTreeItem::RecursiveResetSize()
879 const size_t count
= m_children
.Count();
880 for (size_t i
= 0; i
< count
; i
++ )
881 m_children
[i
]->RecursiveResetSize();
884 void wxGenericTreeItem::RecursiveResetTextSize()
889 const size_t count
= m_children
.Count();
890 for (size_t i
= 0; i
< count
; i
++ )
891 m_children
[i
]->RecursiveResetTextSize();
894 // -----------------------------------------------------------------------------
895 // wxGenericTreeCtrl implementation
896 // -----------------------------------------------------------------------------
898 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
900 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
901 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
902 EVT_SIZE (wxGenericTreeCtrl::OnSize
)
903 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
904 EVT_KEY_DOWN (wxGenericTreeCtrl::OnKeyDown
)
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 // -----------------------------------------------------------------------------
912 // construction/destruction
913 // -----------------------------------------------------------------------------
915 void wxGenericTreeCtrl::Init()
928 m_hilightBrush
= new wxBrush
930 wxSystemSettings::GetColour
932 wxSYS_COLOUR_HIGHLIGHT
937 m_hilightUnfocusedBrush
= new wxBrush
939 wxSystemSettings::GetColour
941 wxSYS_COLOUR_BTNSHADOW
946 m_imageListButtons
= NULL
;
947 m_ownsImageListButtons
= false;
950 m_isDragging
= false;
951 m_dropTarget
= m_oldSelection
= NULL
;
955 m_renameTimer
= NULL
;
958 m_findBell
= 0; // default is to not ring bell at all
960 m_dropEffectAboveItem
= false;
962 m_dndEffect
= NoEffect
;
963 m_dndEffectItem
= NULL
;
965 m_lastOnSame
= false;
967 #if defined( __WXMAC__ )
968 m_normalFont
= wxFont(wxOSX_SYSTEM_FONT_VIEWS
);
970 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
972 m_boldFont
= m_normalFont
.Bold();
975 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
980 const wxValidator
& validator
,
981 const wxString
& name
)
985 wxGetOsVersion(&major
, &minor
);
988 style
|= wxTR_ROW_LINES
;
990 if (style
& wxTR_HAS_BUTTONS
)
991 style
|= wxTR_NO_LINES
;
995 if (style
& wxTR_HAS_BUTTONS
)
996 style
|= wxTR_NO_LINES
;
999 if ( !wxControl::Create( parent
, id
, pos
, size
,
1000 style
|wxHSCROLL
|wxVSCROLL
|wxWANTS_CHARS
,
1005 // If the tree display has no buttons, but does have
1006 // connecting lines, we can use a narrower layout.
1007 // It may not be a good idea to force this...
1008 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
1014 wxVisualAttributes attr
= GetDefaultAttributes();
1015 SetOwnForegroundColour( attr
.colFg
);
1016 SetOwnBackgroundColour( attr
.colBg
);
1018 SetOwnFont(attr
.font
);
1020 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
1021 // style because we apparently get performance problems when using dotted
1022 // pen for drawing in some ports -- but under MSW it seems to work fine
1024 m_dottedPen
= wxPen(*wxLIGHT_GREY
, 0, wxPENSTYLE_DOT
);
1026 m_dottedPen
= *wxGREY_PEN
;
1029 SetInitialSize(size
);
1034 wxGenericTreeCtrl::~wxGenericTreeCtrl()
1036 delete m_hilightBrush
;
1037 delete m_hilightUnfocusedBrush
;
1041 delete m_renameTimer
;
1044 if (m_ownsImageListButtons
)
1045 delete m_imageListButtons
;
1048 void wxGenericTreeCtrl::EnableBellOnNoMatch( bool on
)
1053 // -----------------------------------------------------------------------------
1055 // -----------------------------------------------------------------------------
1057 unsigned int wxGenericTreeCtrl::GetCount() const
1061 // the tree is empty
1065 unsigned int count
= m_anchor
->GetChildrenCount();
1066 if ( !HasFlag(wxTR_HIDE_ROOT
) )
1068 // take the root itself into account
1075 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
1077 m_indent
= (unsigned short) indent
;
1082 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
1083 bool recursively
) const
1085 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
1087 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
1090 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
1092 // Do not try to expand the root node if it hasn't been created yet
1093 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
1095 // if we will hide the root, make sure children are visible
1096 m_anchor
->SetHasPlus();
1098 CalculatePositions();
1101 // right now, just sets the styles. Eventually, we may
1102 // want to update the inherited styles, but right now
1103 // none of the parents has updatable styles
1104 m_windowStyle
= styles
;
1108 // -----------------------------------------------------------------------------
1109 // functions to work with tree items
1110 // -----------------------------------------------------------------------------
1112 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
1114 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
1116 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
1119 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
1120 wxTreeItemIcon which
) const
1122 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1124 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
1127 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1129 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1131 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
1134 int wxGenericTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
1136 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
1138 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1139 return pItem
->GetState();
1142 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1144 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1146 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1147 return pItem
->Attr().GetTextColour();
1151 wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1153 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1155 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1156 return pItem
->Attr().GetBackgroundColour();
1159 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1161 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1163 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1164 return pItem
->Attr().GetFont();
1168 wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
1170 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1172 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1173 pItem
->SetText(text
);
1174 pItem
->CalculateSize(this);
1178 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
1180 wxTreeItemIcon which
)
1182 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1184 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1185 pItem
->SetImage(image
, which
);
1186 pItem
->CalculateSize(this);
1191 wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1193 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1196 data
->SetId( item
);
1198 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1201 void wxGenericTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
1203 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1205 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1206 pItem
->SetState(state
);
1207 pItem
->CalculateSize(this);
1211 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1213 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1215 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1216 pItem
->SetHasPlus(has
);
1220 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1222 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1224 // avoid redrawing the tree if no real change
1225 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1226 if ( pItem
->IsBold() != bold
)
1228 pItem
->SetBold(bold
);
1230 // recalculate the item size as bold and non bold fonts have different
1232 pItem
->CalculateSize(this);
1237 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1240 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1246 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1247 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1250 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1251 pItem
->Attr().SetTextColour(fg
);
1252 pItem
->Attr().SetBackgroundColour(bg
);
1256 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1257 const wxColour
& col
)
1259 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1261 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1262 pItem
->Attr().SetTextColour(col
);
1266 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1267 const wxColour
& col
)
1269 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1271 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1272 pItem
->Attr().SetBackgroundColour(col
);
1277 wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1279 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1281 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1282 pItem
->Attr().SetFont(font
);
1283 pItem
->ResetTextSize();
1284 pItem
->CalculateSize(this);
1288 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1290 wxTreeCtrlBase::SetFont(font
);
1292 m_normalFont
= font
;
1293 m_boldFont
= m_normalFont
.Bold();
1296 m_anchor
->RecursiveResetTextSize();
1302 // -----------------------------------------------------------------------------
1303 // item status inquiries
1304 // -----------------------------------------------------------------------------
1306 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1308 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1310 // An item is only visible if it's not a descendant of a collapsed item
1311 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1312 wxGenericTreeItem
* parent
= pItem
->GetParent();
1315 if (!parent
->IsExpanded())
1317 parent
= parent
->GetParent();
1321 GetViewStart(& startX
, & startY
);
1323 wxSize clientSize
= GetClientSize();
1326 if (!GetBoundingRect(item
, rect
))
1328 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1330 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1332 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1338 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1340 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1342 // consider that the item does have children if it has the "+" button: it
1343 // might not have them (if it had never been expanded yet) but then it
1344 // could have them as well and it's better to err on this side rather than
1345 // disabling some operations which are restricted to the items with
1346 // children for an item which does have them
1347 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1350 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1352 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1354 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1357 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1359 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1361 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1364 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1366 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1368 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1371 // -----------------------------------------------------------------------------
1373 // -----------------------------------------------------------------------------
1375 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1377 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1379 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1382 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1383 wxTreeItemIdValue
& cookie
) const
1385 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1388 return GetNextChild(item
, cookie
);
1391 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1392 wxTreeItemIdValue
& cookie
) const
1394 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1396 wxArrayGenericTreeItems
&
1397 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1399 // it's ok to cast cookie to size_t, we never have indices big enough to
1400 // overflow "void *"
1401 size_t *pIndex
= (size_t *)&cookie
;
1402 if ( *pIndex
< children
.GetCount() )
1404 return children
.Item((*pIndex
)++);
1408 // there are no more of them
1409 return wxTreeItemId();
1413 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1415 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1417 wxArrayGenericTreeItems
&
1418 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1419 return children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last());
1422 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1424 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1426 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1427 wxGenericTreeItem
*parent
= i
->GetParent();
1428 if ( parent
== NULL
)
1430 // root item doesn't have any siblings
1431 return wxTreeItemId();
1434 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1435 int index
= siblings
.Index(i
);
1436 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1438 size_t n
= (size_t)(index
+ 1);
1439 return n
== siblings
.GetCount() ? wxTreeItemId()
1440 : wxTreeItemId(siblings
[n
]);
1443 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1445 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1447 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1448 wxGenericTreeItem
*parent
= i
->GetParent();
1449 if ( parent
== NULL
)
1451 // root item doesn't have any siblings
1452 return wxTreeItemId();
1455 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1456 int index
= siblings
.Index(i
);
1457 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1459 return index
== 0 ? wxTreeItemId()
1460 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1463 // Only for internal use right now, but should probably be public
1464 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1466 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1468 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1470 // First see if there are any children.
1471 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1472 if (children
.GetCount() > 0)
1474 return children
.Item(0);
1478 // Try a sibling of this or ancestor instead
1479 wxTreeItemId p
= item
;
1480 wxTreeItemId toFind
;
1483 toFind
= GetNextSibling(p
);
1484 p
= GetItemParent(p
);
1485 } while (p
.IsOk() && !toFind
.IsOk());
1490 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1492 wxTreeItemId itemid
= GetRootItem();
1498 if (IsVisible(itemid
))
1500 itemid
= GetNext(itemid
);
1501 } while (itemid
.IsOk());
1503 return wxTreeItemId();
1506 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1508 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1509 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1511 wxTreeItemId id
= item
;
1514 while (id
= GetNext(id
), id
.IsOk())
1520 return wxTreeItemId();
1523 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1525 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1526 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1528 // find out the starting point
1529 wxTreeItemId prevItem
= GetPrevSibling(item
);
1530 if ( !prevItem
.IsOk() )
1532 prevItem
= GetItemParent(item
);
1535 // find the first visible item after it
1536 while ( prevItem
.IsOk() && !IsVisible(prevItem
) )
1538 prevItem
= GetNext(prevItem
);
1539 if ( !prevItem
.IsOk() || prevItem
== item
)
1541 // there are no visible items before item
1542 return wxTreeItemId();
1546 // from there we must be able to navigate until this item
1547 while ( prevItem
.IsOk() )
1549 const wxTreeItemId nextItem
= GetNextVisible(prevItem
);
1550 if ( !nextItem
.IsOk() || nextItem
== item
)
1553 prevItem
= nextItem
;
1559 // called by wxTextTreeCtrl when it marks itself for deletion
1560 void wxGenericTreeCtrl::ResetTextControl()
1565 void wxGenericTreeCtrl::ResetFindState()
1567 m_findPrefix
.clear();
1572 // find the first item starting with the given prefix after the given item
1573 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1574 const wxString
& prefixOrig
) const
1576 // match is case insensitive as this is more convenient to the user: having
1577 // to press Shift-letter to go to the item starting with a capital letter
1578 // would be too bothersome
1579 wxString prefix
= prefixOrig
.Lower();
1581 // determine the starting point: we shouldn't take the current item (this
1582 // allows to switch between two items starting with the same letter just by
1583 // pressing it) but we shouldn't jump to the next one if the user is
1584 // continuing to type as otherwise he might easily skip the item he wanted
1585 wxTreeItemId itemid
= idParent
;
1586 if ( prefix
.length() == 1 )
1588 itemid
= GetNext(itemid
);
1591 // look for the item starting with the given prefix after it
1592 while ( itemid
.IsOk() && !GetItemText(itemid
).Lower().StartsWith(prefix
) )
1594 itemid
= GetNext(itemid
);
1597 // if we haven't found anything...
1598 if ( !itemid
.IsOk() )
1600 // ... wrap to the beginning
1601 itemid
= GetRootItem();
1602 if ( HasFlag(wxTR_HIDE_ROOT
) )
1604 // can't select virtual root
1605 itemid
= GetNext(itemid
);
1608 // and try all the items (stop when we get to the one we started from)
1609 while ( itemid
.IsOk() && itemid
!= idParent
&&
1610 !GetItemText(itemid
).Lower().StartsWith(prefix
) )
1612 itemid
= GetNext(itemid
);
1614 // If we haven't found the item but wrapped back to the one we started
1615 // from, id.IsOk() must be false
1616 if ( itemid
== idParent
)
1618 itemid
= wxTreeItemId();
1625 // -----------------------------------------------------------------------------
1627 // -----------------------------------------------------------------------------
1629 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1631 const wxString
& text
,
1634 wxTreeItemData
*data
)
1636 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1639 // should we give a warning here?
1640 return AddRoot(text
, image
, selImage
, data
);
1643 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1645 wxGenericTreeItem
*item
=
1646 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1650 data
->m_pItem
= item
;
1653 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1656 InvalidateBestSize();
1660 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1663 wxTreeItemData
*data
)
1665 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
1667 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1669 m_anchor
= new wxGenericTreeItem(NULL
, text
,
1670 image
, selImage
, data
);
1673 data
->m_pItem
= m_anchor
;
1676 if (HasFlag(wxTR_HIDE_ROOT
))
1678 // if root is hidden, make sure we can navigate
1680 m_anchor
->SetHasPlus();
1682 CalculatePositions();
1685 if (!HasFlag(wxTR_MULTIPLE
))
1687 m_current
= m_key_current
= m_anchor
;
1688 m_current
->SetHilight( true );
1691 InvalidateBestSize();
1695 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1696 const wxTreeItemId
& idPrevious
,
1697 const wxString
& text
,
1698 int image
, int selImage
,
1699 wxTreeItemData
*data
)
1701 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1704 // should we give a warning here?
1705 return AddRoot(text
, image
, selImage
, data
);
1709 if (idPrevious
.IsOk())
1711 index
= parent
->GetChildren().Index(
1712 (wxGenericTreeItem
*) idPrevious
.m_pItem
);
1713 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1714 "previous item in wxGenericTreeCtrl::InsertItem() "
1715 "is not a sibling" );
1718 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1722 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1724 wxTreeEvent
event(wxEVT_TREE_DELETE_ITEM
, this, item
);
1725 GetEventHandler()->ProcessEvent( event
);
1728 // Don't leave edit or selection on a child which is about to disappear
1729 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1731 if ( m_textCtrl
&& item
!= m_textCtrl
->item() &&
1732 IsDescendantOf(item
, m_textCtrl
->item()) )
1734 m_textCtrl
->EndEdit( true );
1737 if ( item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
) )
1739 m_key_current
= NULL
;
1742 if ( IsDescendantOf(item
, m_select_me
) )
1747 if ( item
!= m_current
&& IsDescendantOf(item
, m_current
) )
1749 m_current
->SetHilight( false );
1755 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1757 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1759 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1760 ChildrenClosing(item
);
1761 item
->DeleteChildren(this);
1762 InvalidateBestSize();
1765 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1767 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1769 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1771 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1773 // can't delete the item being edited, cancel editing it first
1774 m_textCtrl
->EndEdit( true );
1777 wxGenericTreeItem
*parent
= item
->GetParent();
1779 // if the selected item will be deleted, select the parent ...
1780 wxGenericTreeItem
*to_be_selected
= parent
;
1783 // .. unless there is a next sibling like wxMSW does it
1784 int pos
= parent
->GetChildren().Index( item
);
1785 if ((int)(parent
->GetChildren().GetCount()) > pos
+1)
1786 to_be_selected
= parent
->GetChildren().Item( pos
+1 );
1789 // don't keep stale pointers around!
1790 if ( IsDescendantOf(item
, m_key_current
) )
1792 // Don't silently change the selection:
1793 // do it properly in idle time, so event
1794 // handlers get called.
1796 // m_key_current = parent;
1797 m_key_current
= NULL
;
1800 // m_select_me records whether we need to select
1801 // a different item, in idle time.
1802 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1804 m_select_me
= to_be_selected
;
1807 if ( IsDescendantOf(item
, m_current
) )
1809 // Don't silently change the selection:
1810 // do it properly in idle time, so event
1811 // handlers get called.
1813 // m_current = parent;
1815 m_select_me
= to_be_selected
;
1818 // remove the item from the tree
1821 parent
->GetChildren().Remove( item
); // remove by value
1823 else // deleting the root
1825 // nothing will be left in the tree
1829 // and delete all of its children and the item itself now
1830 item
->DeleteChildren(this);
1831 SendDeleteEvent(item
);
1833 if (item
== m_select_me
)
1838 InvalidateBestSize();
1841 void wxGenericTreeCtrl::DeleteAllItems()
1849 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1851 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1853 wxCHECK_RET( item
, wxT("invalid item in wxGenericTreeCtrl::Expand") );
1854 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1855 wxT("can't expand hidden root") );
1857 if ( !item
->HasPlus() )
1860 if ( item
->IsExpanded() )
1863 wxTreeEvent
event(wxEVT_TREE_ITEM_EXPANDING
, this, item
);
1865 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1867 // cancelled by program
1874 CalculatePositions();
1876 RefreshSubtree(item
);
1883 event
.SetEventType(wxEVT_TREE_ITEM_EXPANDED
);
1884 GetEventHandler()->ProcessEvent( event
);
1887 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1889 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1890 wxT("can't collapse hidden root") );
1892 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1894 if ( !item
->IsExpanded() )
1897 wxTreeEvent
event(wxEVT_TREE_ITEM_COLLAPSING
, this, item
);
1898 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1900 // cancelled by program
1904 ChildrenClosing(item
);
1907 #if 0 // TODO why should items be collapsed recursively?
1908 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1909 size_t count
= children
.GetCount();
1910 for ( size_t n
= 0; n
< count
; n
++ )
1912 Collapse(children
[n
]);
1916 CalculatePositions();
1918 RefreshSubtree(item
);
1920 event
.SetEventType(wxEVT_TREE_ITEM_COLLAPSED
);
1921 GetEventHandler()->ProcessEvent( event
);
1924 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1927 DeleteChildren(item
);
1930 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1932 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1934 if (item
->IsExpanded())
1940 void wxGenericTreeCtrl::Unselect()
1944 m_current
->SetHilight( false );
1945 RefreshLine( m_current
);
1952 void wxGenericTreeCtrl::ClearFocusedItem()
1954 wxTreeItemId item
= GetFocusedItem();
1956 SelectItem(item
, false);
1959 void wxGenericTreeCtrl::SetFocusedItem(const wxTreeItemId
& item
)
1961 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1963 SelectItem(item
, true);
1966 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1968 if (item
->IsSelected())
1970 item
->SetHilight(false);
1974 if (item
->HasChildren())
1976 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1977 size_t count
= children
.GetCount();
1978 for ( size_t n
= 0; n
< count
; ++n
)
1980 UnselectAllChildren(children
[n
]);
1985 void wxGenericTreeCtrl::UnselectAll()
1987 wxTreeItemId rootItem
= GetRootItem();
1989 // the tree might not have the root item at all
1992 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1996 void wxGenericTreeCtrl::SelectChildren(const wxTreeItemId
& parent
)
1998 wxCHECK_RET( HasFlag(wxTR_MULTIPLE
),
1999 "this only works with multiple selection controls" );
2003 if ( !HasChildren(parent
) )
2007 wxArrayGenericTreeItems
&
2008 children
= ((wxGenericTreeItem
*) parent
.m_pItem
)->GetChildren();
2009 size_t count
= children
.GetCount();
2012 item
= (wxGenericTreeItem
*) ((wxTreeItemId
)children
[0]).m_pItem
;
2013 wxTreeEvent
event(wxEVT_TREE_SEL_CHANGING
, this, item
);
2014 event
.m_itemOld
= m_current
;
2016 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2019 for ( size_t n
= 0; n
< count
; ++n
)
2021 m_current
= m_key_current
= children
[n
];
2022 m_current
->SetHilight(true);
2027 event
.SetEventType(wxEVT_TREE_SEL_CHANGED
);
2028 GetEventHandler()->ProcessEvent( event
);
2032 // Recursive function !
2033 // To stop we must have crt_item<last_item
2035 // Tag all next children, when no more children,
2036 // Move to parent (not to tag)
2037 // Keep going... if we found last_item, we stop.
2039 wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
,
2040 wxGenericTreeItem
*last_item
,
2043 wxGenericTreeItem
*parent
= crt_item
->GetParent();
2045 if (parent
== NULL
) // This is root item
2046 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
2048 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
2049 int index
= children
.Index(crt_item
);
2050 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
2052 size_t count
= children
.GetCount();
2053 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
2055 if ( TagAllChildrenUntilLast(children
[n
], last_item
, select
) )
2059 return TagNextChildren(parent
, last_item
, select
);
2063 wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
,
2064 wxGenericTreeItem
*last_item
,
2067 crt_item
->SetHilight(select
);
2068 RefreshLine(crt_item
);
2070 if (crt_item
==last_item
)
2073 // We should leave the not shown children of collapsed items alone.
2074 if (crt_item
->HasChildren() && crt_item
->IsExpanded())
2076 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
2077 size_t count
= children
.GetCount();
2078 for ( size_t n
= 0; n
< count
; ++n
)
2080 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
2089 wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
,
2090 wxGenericTreeItem
*item2
)
2094 // item2 is not necessary after item1
2095 // choice first' and 'last' between item1 and item2
2096 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
2097 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
2099 bool select
= m_current
->IsSelected();
2101 if ( TagAllChildrenUntilLast(first
,last
,select
) )
2104 TagNextChildren(first
,last
,select
);
2107 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
2108 bool unselect_others
,
2109 bool extended_select
)
2111 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2115 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
2116 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2118 //wxCHECK_RET( ( (!unselect_others) && is_single),
2119 // wxT("this is a single selection tree") );
2121 // to keep going anyhow !!!
2124 if (item
->IsSelected())
2125 return; // nothing to do
2126 unselect_others
= true;
2127 extended_select
= false;
2129 else if ( unselect_others
&& item
->IsSelected() )
2131 // selection change if there is more than one item currently selected
2132 wxArrayTreeItemIds selected_items
;
2133 if ( GetSelections(selected_items
) == 1 )
2137 wxTreeEvent
event(wxEVT_TREE_SEL_CHANGING
, this, item
);
2138 event
.m_itemOld
= m_current
;
2139 // TODO : Here we don't send any selection mode yet !
2141 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2144 wxTreeItemId parent
= GetItemParent( itemId
);
2145 while (parent
.IsOk())
2147 if (!IsExpanded(parent
))
2150 parent
= GetItemParent( parent
);
2154 if (unselect_others
)
2156 if (is_single
) Unselect(); // to speed up thing
2161 if (extended_select
)
2166 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
2169 // don't change the mark (m_current)
2170 SelectItemRange(m_current
, item
);
2174 bool select
= true; // the default
2176 // Check if we need to toggle hilight (ctrl mode)
2177 if (!unselect_others
)
2178 select
=!item
->IsSelected();
2180 m_current
= m_key_current
= item
;
2181 m_current
->SetHilight(select
);
2182 RefreshLine( m_current
);
2185 // This can cause idle processing to select the root
2186 // if no item is selected, so it must be after the
2188 EnsureVisible( itemId
);
2190 event
.SetEventType(wxEVT_TREE_SEL_CHANGED
);
2191 GetEventHandler()->ProcessEvent( event
);
2194 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
2196 wxGenericTreeItem
* const item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2197 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
2201 if ( !item
->IsSelected() )
2202 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
2206 wxTreeEvent
event(wxEVT_TREE_SEL_CHANGING
, this, item
);
2207 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2210 item
->SetHilight(false);
2213 event
.SetEventType(wxEVT_TREE_SEL_CHANGED
);
2214 GetEventHandler()->ProcessEvent( event
);
2218 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
2219 wxArrayTreeItemIds
&array
) const
2221 if ( item
->IsSelected() )
2222 array
.Add(wxTreeItemId(item
));
2224 if ( item
->HasChildren() )
2226 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2227 size_t count
= children
.GetCount();
2228 for ( size_t n
= 0; n
< count
; ++n
)
2229 FillArray(children
[n
], array
);
2233 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
2236 wxTreeItemId idRoot
= GetRootItem();
2237 if ( idRoot
.IsOk() )
2239 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
2241 //else: the tree is empty, so no selections
2243 return array
.GetCount();
2246 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
2248 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2250 if (!item
.IsOk()) return;
2252 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2254 // first expand all parent branches
2255 wxGenericTreeItem
*parent
= gitem
->GetParent();
2257 if ( HasFlag(wxTR_HIDE_ROOT
) )
2259 while ( parent
&& parent
!= m_anchor
)
2262 parent
= parent
->GetParent();
2270 parent
= parent
->GetParent();
2274 //if (parent) CalculatePositions();
2279 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
2284 // update the control before scrolling it
2287 #if defined( __WXMSW__ )
2289 #elif defined(__WXMAC__)
2291 DoDirtyProcessing();
2293 DoDirtyProcessing();
2297 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2299 int itemY
= gitem
->GetY();
2303 GetViewStart( &start_x
, &start_y
);
2305 const int clientHeight
= GetClientSize().y
;
2307 const int itemHeight
= GetLineHeight(gitem
) + 2;
2309 if ( itemY
+ itemHeight
> start_y
*PIXELS_PER_UNIT
+ clientHeight
)
2311 // need to scroll up by enough to show this item fully
2312 itemY
+= itemHeight
- clientHeight
;
2314 // because itemY below will be divided by PIXELS_PER_UNIT it may
2315 // be rounded down, with the result of the item still only being
2316 // partially visible, so make sure we are rounding up
2317 itemY
+= PIXELS_PER_UNIT
-1;
2320 else if ( itemY
> start_y
*PIXELS_PER_UNIT
)
2322 // item is already fully visible, don't do anything
2325 //else: scroll down to make this item the top one displayed
2327 Scroll(-1, itemY
/PIXELS_PER_UNIT
);
2330 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2331 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2333 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2334 wxGenericTreeItem
**item2
)
2336 wxCHECK_MSG( s_treeBeingSorted
, 0,
2337 "bug in wxGenericTreeCtrl::SortChildren()" );
2339 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2342 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2344 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2346 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2348 wxCHECK_RET( !s_treeBeingSorted
,
2349 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2351 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2352 if ( children
.GetCount() > 1 )
2356 s_treeBeingSorted
= this;
2357 children
.Sort(tree_ctrl_compare_func
);
2358 s_treeBeingSorted
= NULL
;
2360 //else: don't make the tree dirty as nothing changed
2363 void wxGenericTreeCtrl::CalculateLineHeight()
2365 wxClientDC
dc(this);
2366 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2368 if ( m_imageListNormal
)
2370 // Calculate a m_lineHeight value from the normal Image sizes.
2371 // May be toggle off. Then wxGenericTreeCtrl will spread when
2372 // necessary (which might look ugly).
2373 int n
= m_imageListNormal
->GetImageCount();
2374 for (int i
= 0; i
< n
; i
++)
2376 int width
= 0, height
= 0;
2377 m_imageListNormal
->GetSize(i
, width
, height
);
2378 if (height
> m_lineHeight
) m_lineHeight
= height
;
2382 if ( m_imageListState
)
2384 // Calculate a m_lineHeight value from the state Image sizes.
2385 // May be toggle off. Then wxGenericTreeCtrl will spread when
2386 // necessary (which might look ugly).
2387 int n
= m_imageListState
->GetImageCount();
2388 for (int i
= 0; i
< n
; i
++)
2390 int width
= 0, height
= 0;
2391 m_imageListState
->GetSize(i
, width
, height
);
2392 if (height
> m_lineHeight
) m_lineHeight
= height
;
2396 if (m_imageListButtons
)
2398 // Calculate a m_lineHeight value from the Button image sizes.
2399 // May be toggle off. Then wxGenericTreeCtrl will spread when
2400 // necessary (which might look ugly).
2401 int n
= m_imageListButtons
->GetImageCount();
2402 for (int i
= 0; i
< n
; i
++)
2404 int width
= 0, height
= 0;
2405 m_imageListButtons
->GetSize(i
, width
, height
);
2406 if (height
> m_lineHeight
) m_lineHeight
= height
;
2410 if (m_lineHeight
< 30)
2411 m_lineHeight
+= 2; // at least 2 pixels
2413 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2416 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2418 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2419 m_imageListNormal
= imageList
;
2420 m_ownsImageListNormal
= false;
2424 m_anchor
->RecursiveResetSize();
2426 // Don't do any drawing if we're setting the list to NULL,
2427 // since we may be in the process of deleting the tree control.
2429 CalculateLineHeight();
2432 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2434 if (m_ownsImageListState
) delete m_imageListState
;
2435 m_imageListState
= imageList
;
2436 m_ownsImageListState
= false;
2440 m_anchor
->RecursiveResetSize();
2442 // Don't do any drawing if we're setting the list to NULL,
2443 // since we may be in the process of deleting the tree control.
2445 CalculateLineHeight();
2448 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2450 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2451 m_imageListButtons
= imageList
;
2452 m_ownsImageListButtons
= false;
2456 m_anchor
->RecursiveResetSize();
2458 CalculateLineHeight();
2461 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2463 SetButtonsImageList(imageList
);
2464 m_ownsImageListButtons
= true;
2467 // -----------------------------------------------------------------------------
2469 // -----------------------------------------------------------------------------
2471 void wxGenericTreeCtrl::AdjustMyScrollbars()
2476 m_anchor
->GetSize( x
, y
, this );
2477 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2478 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2479 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2480 int y_pos
= GetScrollPos( wxVERTICAL
);
2481 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2482 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2487 SetScrollbars( 0, 0, 0, 0 );
2491 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2493 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2494 return item
->GetHeight();
2496 return m_lineHeight
;
2499 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2501 item
->SetFont(this, dc
);
2502 item
->CalculateSize(this, dc
);
2504 wxCoord text_h
= item
->GetTextHeight();
2506 int image_h
= 0, image_w
= 0;
2507 int image
= item
->GetCurrentImage();
2508 if ( image
!= NO_IMAGE
)
2510 if ( m_imageListNormal
)
2512 m_imageListNormal
->GetSize(image
, image_w
, image_h
);
2513 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2521 int state_h
= 0, state_w
= 0;
2522 int state
= item
->GetState();
2523 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2525 if ( m_imageListState
)
2527 m_imageListState
->GetSize(state
, state_w
, state_h
);
2529 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
2531 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2535 state
= wxTREE_ITEMSTATE_NONE
;
2539 int total_h
= GetLineHeight(item
);
2540 bool drawItemBackground
= false,
2541 hasBgColour
= false;
2543 if ( item
->IsSelected() )
2545 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2546 drawItemBackground
= true;
2551 wxTreeItemAttr
* const attr
= item
->GetAttributes();
2552 if ( attr
&& attr
->HasBackgroundColour() )
2554 drawItemBackground
=
2556 colBg
= attr
->GetBackgroundColour();
2560 colBg
= GetBackgroundColour();
2562 dc
.SetBrush(wxBrush(colBg
, wxBRUSHSTYLE_SOLID
));
2565 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2567 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2571 GetVirtualSize(&w
, &h
);
2572 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2573 if (!item
->IsSelected())
2575 dc
.DrawRectangle(rect
);
2579 int flags
= wxCONTROL_SELECTED
;
2581 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2582 && IsControlActive( (ControlRef
)GetHandle() )
2585 flags
|= wxCONTROL_FOCUSED
;
2586 if ((item
== m_current
) && (m_hasFocus
))
2587 flags
|= wxCONTROL_CURRENT
;
2589 wxRendererNative::Get().
2590 DrawItemSelectionRect(this, dc
, rect
, flags
);
2593 else // no full row highlight
2595 if ( item
->IsSelected() &&
2596 (state
!= wxTREE_ITEMSTATE_NONE
|| image
!= NO_IMAGE
) )
2598 // If it's selected, and there's an state image or normal image,
2599 // then we should take care to leave the area under the image
2600 // painted in the background colour.
2601 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2602 item
->GetY() + offset
,
2603 item
->GetWidth() - state_w
- image_w
+ 2,
2605 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2606 dc
.DrawRectangle( rect
);
2611 int flags
= wxCONTROL_SELECTED
;
2613 flags
|= wxCONTROL_FOCUSED
;
2614 if ((item
== m_current
) && (m_hasFocus
))
2615 flags
|= wxCONTROL_CURRENT
;
2616 wxRendererNative::Get().
2617 DrawItemSelectionRect(this, dc
, rect
, flags
);
2620 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2621 // don't allow backgrounds to be customized. Not drawing the background,
2622 // except for custom item backgrounds, works for both kinds of theme.
2623 else if (drawItemBackground
)
2625 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2626 item
->GetY() + offset
,
2627 item
->GetWidth() - state_w
- image_w
+ 2,
2631 dc
.DrawRectangle( rect
);
2633 else // no specific background colour
2638 int flags
= wxCONTROL_SELECTED
;
2640 flags
|= wxCONTROL_FOCUSED
;
2641 if ((item
== m_current
) && (m_hasFocus
))
2642 flags
|= wxCONTROL_CURRENT
;
2643 wxRendererNative::Get().
2644 DrawItemSelectionRect(this, dc
, rect
, flags
);
2649 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2651 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), state_w
, total_h
);
2652 m_imageListState
->Draw( state
, dc
,
2655 (total_h
> state_h
? (total_h
-state_h
)/2
2657 wxIMAGELIST_DRAW_TRANSPARENT
);
2658 dc
.DestroyClippingRegion();
2661 if ( image
!= NO_IMAGE
)
2663 dc
.SetClippingRegion(item
->GetX() + state_w
, item
->GetY(),
2665 m_imageListNormal
->Draw( image
, dc
,
2666 item
->GetX() + state_w
,
2668 (total_h
> image_h
? (total_h
-image_h
)/2
2670 wxIMAGELIST_DRAW_TRANSPARENT
);
2671 dc
.DestroyClippingRegion();
2674 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
2675 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2676 dc
.DrawText( item
->GetText(),
2677 (wxCoord
)(state_w
+ image_w
+ item
->GetX()),
2678 (wxCoord
)(item
->GetY() + extraH
));
2680 // restore normal font
2681 dc
.SetFont( m_normalFont
);
2683 if (item
== m_dndEffectItem
)
2685 dc
.SetPen( *wxBLACK_PEN
);
2686 // DnD visual effects
2687 switch (m_dndEffect
)
2691 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2692 int w
= item
->GetWidth() + 2;
2693 int h
= total_h
+ 2;
2694 dc
.DrawRectangle( item
->GetX() - 1, item
->GetY() - 1, w
, h
);
2699 int x
= item
->GetX(),
2701 dc
.DrawLine( x
, y
, x
+ item
->GetWidth(), y
);
2706 int x
= item
->GetX(),
2709 dc
.DrawLine( x
, y
, x
+ item
->GetWidth(), y
);
2719 wxGenericTreeCtrl::PaintLevel(wxGenericTreeItem
*item
,
2724 int x
= level
*m_indent
;
2725 if (!HasFlag(wxTR_HIDE_ROOT
))
2729 else if (level
== 0)
2731 // always expand hidden root
2733 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2734 int count
= children
.GetCount();
2740 PaintLevel(children
[n
], dc
, 1, y
);
2741 } while (++n
< count
);
2743 if ( !HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
)
2746 // draw line down to last child
2747 origY
+= GetLineHeight(children
[0])>>1;
2748 oldY
+= GetLineHeight(children
[n
-1])>>1;
2749 dc
.DrawLine(3, origY
, 3, oldY
);
2755 item
->SetX(x
+m_spacing
);
2758 int h
= GetLineHeight(item
);
2760 int y_mid
= y_top
+ (h
>>1);
2763 int exposed_x
= dc
.LogicalToDeviceX(0);
2764 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2766 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2770 // don't draw rect outline if we already have the
2771 // background color under Mac
2772 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2773 #endif // !__WXMAC__
2777 if ( item
->IsSelected()
2778 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2779 // On wxMac, if the tree doesn't have the focus we draw an empty
2780 // rectangle, so we want to make sure that the text is visible
2781 // against the normal background, not the highlightbackground, so
2782 // don't use the highlight text colour unless we have the focus.
2783 && m_hasFocus
&& IsControlActive( (ControlRef
)GetHandle() )
2791 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2793 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT
);
2798 wxTreeItemAttr
*attr
= item
->GetAttributes();
2799 if (attr
&& attr
->HasTextColour())
2800 colText
= attr
->GetTextColour();
2802 colText
= GetForegroundColour();
2806 dc
.SetTextForeground(colText
);
2810 PaintItem(item
, dc
);
2812 if (HasFlag(wxTR_ROW_LINES
))
2814 // if the background colour is white, choose a
2815 // contrasting color for the lines
2816 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2817 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2818 dc
.DrawLine(0, y_top
, 10000, y_top
);
2819 dc
.DrawLine(0, y
, 10000, y
);
2822 // restore DC objects
2823 dc
.SetBrush(*wxWHITE_BRUSH
);
2824 dc
.SetPen(m_dottedPen
);
2825 dc
.SetTextForeground(*wxBLACK
);
2827 if ( !HasFlag(wxTR_NO_LINES
) )
2829 // draw the horizontal line here
2831 if (x
> (signed)m_indent
)
2832 x_start
-= m_indent
;
2833 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2835 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2838 // should the item show a button?
2839 if ( item
->HasPlus() && HasButtons() )
2841 if ( m_imageListButtons
)
2843 // draw the image button here
2846 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2847 : wxTreeItemIcon_Normal
;
2848 if ( item
->IsSelected() )
2849 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2851 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2852 int xx
= x
- image_w
/2;
2853 int yy
= y_mid
- image_h
/2;
2855 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2856 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2857 wxIMAGELIST_DRAW_TRANSPARENT
);
2859 else // no custom buttons
2861 static const int wImage
= 9;
2862 static const int hImage
= 9;
2865 if (item
->IsExpanded())
2866 flag
|= wxCONTROL_EXPANDED
;
2867 if (item
== m_underMouse
)
2868 flag
|= wxCONTROL_CURRENT
;
2870 wxRendererNative::Get().DrawTreeItemButton
2874 wxRect(x
- wImage
/2,
2883 if (item
->IsExpanded())
2885 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2886 int count
= children
.GetCount();
2893 PaintLevel(children
[n
], dc
, level
, y
);
2894 } while (++n
< count
);
2896 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2898 // draw line down to last child
2899 oldY
+= GetLineHeight(children
[n
-1])>>1;
2900 if (HasButtons()) y_mid
+= 5;
2902 // Only draw the portion of the line that is visible, in case
2904 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2905 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2906 yOrigin
= abs(yOrigin
);
2907 GetClientSize(&width
, &height
);
2909 // Move end points to the beginning/end of the view?
2910 if (y_mid
< yOrigin
)
2912 if (oldY
> yOrigin
+ height
)
2913 oldY
= yOrigin
+ height
;
2915 // after the adjustments if y_mid is larger than oldY then the
2916 // line isn't visible at all so don't draw anything
2918 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2924 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2928 if ( item
->HasPlus() )
2930 // it's a folder, indicate it by a border
2935 // draw a line under the drop target because the item will be
2937 DrawLine(item
, !m_dropEffectAboveItem
);
2940 SetCursor(*wxSTANDARD_CURSOR
);
2945 SetCursor(wxCURSOR_NO_ENTRY
);
2949 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2951 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2953 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2955 if (m_dndEffect
== NoEffect
)
2957 m_dndEffect
= BorderEffect
;
2958 m_dndEffectItem
= i
;
2962 m_dndEffect
= NoEffect
;
2963 m_dndEffectItem
= NULL
;
2966 wxRect
rect( i
->GetX()-1, i
->GetY()-1, i
->GetWidth()+2, GetLineHeight(i
)+2 );
2967 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2968 RefreshRect( rect
);
2971 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2973 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2975 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2977 if (m_dndEffect
== NoEffect
)
2980 m_dndEffect
= BelowEffect
;
2982 m_dndEffect
= AboveEffect
;
2983 m_dndEffectItem
= i
;
2987 m_dndEffect
= NoEffect
;
2988 m_dndEffectItem
= NULL
;
2991 wxRect
rect( i
->GetX()-1, i
->GetY()-1, i
->GetWidth()+2, GetLineHeight(i
)+2 );
2992 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2993 RefreshRect( rect
);
2996 // -----------------------------------------------------------------------------
2997 // wxWidgets callbacks
2998 // -----------------------------------------------------------------------------
3000 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
3003 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
3004 RefreshLine( m_current
);
3010 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3018 dc
.SetFont( m_normalFont
);
3019 dc
.SetPen( m_dottedPen
);
3021 // this is now done dynamically
3022 //if(GetImageList() == NULL)
3023 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3026 PaintLevel( m_anchor
, dc
, 0, y
);
3029 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
3038 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
3047 void wxGenericTreeCtrl::OnKeyDown( wxKeyEvent
&event
)
3049 // send a tree event
3050 wxTreeEvent
te( wxEVT_TREE_KEY_DOWN
, this);
3051 te
.m_evtKey
= event
;
3052 if ( GetEventHandler()->ProcessEvent( te
) )
3058 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
3060 if ( (m_current
== 0) || (m_key_current
== 0) )
3066 // how should the selection work for this event?
3067 bool is_multiple
, extended_select
, unselect_others
;
3068 EventFlagsToSelType(GetWindowStyleFlag(),
3071 is_multiple
, extended_select
, unselect_others
);
3073 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3075 if (event
.GetKeyCode() == WXK_RIGHT
)
3076 event
.m_keyCode
= WXK_LEFT
;
3077 else if (event
.GetKeyCode() == WXK_LEFT
)
3078 event
.m_keyCode
= WXK_RIGHT
;
3083 // * : Expand all/Collapse all
3084 // ' ' | return : activate
3085 // up : go up (not last children!)
3087 // left : go to parent
3088 // right : open if parent and go next
3089 // home : go to root
3090 // end : go to last item without opening parents
3091 // alnum : start or continue searching for the item with this prefix
3092 int keyCode
= event
.GetKeyCode();
3095 // Make the keys work as they do in the native control:
3097 // left => collapse if current item is expanded
3098 if (keyCode
== WXK_RIGHT
)
3102 else if (keyCode
== WXK_LEFT
&& IsExpanded(m_current
))
3112 if (m_current
->HasPlus() && !IsExpanded(m_current
))
3120 if ( !IsExpanded(m_current
) )
3123 ExpandAllChildren(m_current
);
3126 //else: fall through to Collapse() it
3130 if (IsExpanded(m_current
))
3132 Collapse(m_current
);
3138 // Use the item's bounding rectangle to determine position for
3141 GetBoundingRect(m_current
, ItemRect
, true);
3144 eventMenu(wxEVT_TREE_ITEM_MENU
, this, m_current
);
3145 // Use the left edge, vertical middle
3146 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
3148 ItemRect
.GetHeight() / 2);
3149 GetEventHandler()->ProcessEvent( eventMenu
);
3155 if ( !event
.HasModifiers() )
3158 eventAct(wxEVT_TREE_ITEM_ACTIVATED
, this, m_current
);
3159 GetEventHandler()->ProcessEvent( eventAct
);
3162 // in any case, also generate the normal key event for this key,
3163 // even if we generated the ACTIVATED event above: this is what
3164 // wxMSW does and it makes sense because you might not want to
3165 // process ACTIVATED event at all and handle Space and Return
3166 // directly (and differently) which would be impossible otherwise
3170 // up goes to the previous sibling or to the last
3171 // of its children if it's expanded
3174 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
3177 prev
= GetItemParent( m_key_current
);
3178 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3180 break; // don't go to root if it is hidden
3184 wxTreeItemIdValue cookie
;
3185 wxTreeItemId current
= m_key_current
;
3186 // TODO: Huh? If we get here, we'd better be the first
3187 // child of our parent. How else could it be?
3188 if (current
== GetFirstChild( prev
, cookie
))
3190 // otherwise we return to where we came from
3194 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
3201 while ( IsExpanded(prev
) && HasChildren(prev
) )
3203 wxTreeItemId child
= GetLastChild(prev
);
3210 DoSelectItem( prev
, unselect_others
, extended_select
);
3211 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
3216 // left arrow goes to the parent
3219 wxTreeItemId prev
= GetItemParent( m_current
);
3220 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3222 // don't go to root if it is hidden
3223 prev
= GetPrevSibling( m_current
);
3227 DoSelectItem( prev
, unselect_others
, extended_select
);
3233 // this works the same as the down arrow except that we
3234 // also expand the item if it wasn't expanded yet
3235 if (m_current
!= GetRootItem().m_pItem
|| !HasFlag(wxTR_HIDE_ROOT
))
3237 //else: don't try to expand hidden root item (which can be the
3238 // current one when the tree is empty)
3244 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
3246 wxTreeItemIdValue cookie
;
3247 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
3251 DoSelectItem( child
, unselect_others
, extended_select
);
3252 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
3256 wxTreeItemId next
= GetNextSibling( m_key_current
);
3259 wxTreeItemId current
= m_key_current
;
3260 while (current
.IsOk() && !next
)
3262 current
= GetItemParent( current
);
3263 if (current
) next
= GetNextSibling( current
);
3268 DoSelectItem( next
, unselect_others
, extended_select
);
3269 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
3275 // <End> selects the last visible tree item
3278 wxTreeItemId last
= GetRootItem();
3280 while ( last
.IsOk() && IsExpanded(last
) )
3282 wxTreeItemId lastChild
= GetLastChild(last
);
3284 // it may happen if the item was expanded but then all of
3285 // its children have been deleted - so IsExpanded() returned
3286 // true, but GetLastChild() returned invalid item
3295 DoSelectItem( last
, unselect_others
, extended_select
);
3300 // <Home> selects the root item
3303 wxTreeItemId prev
= GetRootItem();
3307 if ( HasFlag(wxTR_HIDE_ROOT
) )
3309 wxTreeItemIdValue cookie
;
3310 prev
= GetFirstChild(prev
, cookie
);
3315 DoSelectItem( prev
, unselect_others
, extended_select
);
3320 // do not use wxIsalnum() here
3321 if ( !event
.HasModifiers() &&
3322 ((keyCode
>= '0' && keyCode
<= '9') ||
3323 (keyCode
>= 'a' && keyCode
<= 'z') ||
3324 (keyCode
>= 'A' && keyCode
<= 'Z') ||
3327 // find the next item starting with the given prefix
3328 wxChar ch
= (wxChar
)keyCode
;
3331 // if the same character is typed multiple times then go to the
3332 // next entry starting with that character instead of searching
3333 // for an item starting with multiple copies of this character,
3334 // this is more useful and is how it works under Windows.
3335 if ( m_findPrefix
.length() == 1 && m_findPrefix
[0] == ch
)
3337 id
= FindItem(m_current
, ch
);
3341 const wxString
newPrefix(m_findPrefix
+ ch
);
3342 id
= FindItem(m_current
, newPrefix
);
3344 m_findPrefix
= newPrefix
;
3347 // also start the timer to reset the current prefix if the user
3348 // doesn't press any more alnum keys soon -- we wouldn't want
3349 // to use this prefix for a new item search
3352 m_findTimer
= new wxTreeFindTimer(this);
3355 // Notice that we should start the timer even if we didn't find
3356 // anything to make sure we reset the search state later.
3357 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
3363 // Reset the bell flag if it had been temporarily disabled
3368 else // No such item
3370 // Signal it with a bell if enabled.
3371 if ( m_findBell
== 1 )
3375 // Disable it for the next unsuccessful match, we only
3376 // beep once, this is usually enough and continuing to
3377 // do it would be annoying.
3390 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
3395 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
3396 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
3397 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
3398 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
3399 if (flags
) return wxTreeItemId();
3401 if (m_anchor
== NULL
)
3403 flags
= wxTREE_HITTEST_NOWHERE
;
3404 return wxTreeItemId();
3407 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
3411 flags
= wxTREE_HITTEST_NOWHERE
;
3412 return wxTreeItemId();
3417 // get the bounding rectangle of the item (or of its label only)
3418 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
3420 bool textOnly
) const
3422 wxCHECK_MSG( item
.IsOk(), false,
3423 "invalid item in wxGenericTreeCtrl::GetBoundingRect" );
3425 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
3429 int image_h
= 0, image_w
= 0;
3430 int image
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetCurrentImage();
3431 if ( image
!= NO_IMAGE
&& m_imageListNormal
)
3433 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3434 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3437 int state_h
= 0, state_w
= 0;
3438 int state
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetState();
3439 if ( state
!= wxTREE_ITEMSTATE_NONE
&& m_imageListState
)
3441 m_imageListState
->GetSize( state
, state_w
, state_h
);
3443 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
3445 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3448 rect
.x
= i
->GetX() + state_w
+ image_w
;
3449 rect
.width
= i
->GetWidth() - state_w
- image_w
;
3452 else // the entire line
3455 rect
.width
= GetClientSize().x
;
3459 rect
.height
= GetLineHeight(i
);
3461 // we have to return the logical coordinates, not physical ones
3462 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
3467 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
3468 wxClassInfo
* WXUNUSED(textCtrlClass
))
3470 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("can't edit an invalid item") );
3472 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
3474 wxTreeEvent
te(wxEVT_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
3475 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
3481 // We have to call this here because the label in
3482 // question might just have been added and no screen
3483 // update taken place.
3485 DoDirtyProcessing();
3487 // TODO: use textCtrlClass here to create the control of correct class
3488 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3490 m_textCtrl
->SetFocus();
3495 // returns a pointer to the text edit control if the item is being
3496 // edited, NULL otherwise (it's assumed that no more than one item may
3497 // be edited simultaneously)
3498 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3503 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3504 bool discardChanges
)
3506 wxCHECK_RET( m_textCtrl
, wxT("not editing label") );
3508 m_textCtrl
->EndEdit(discardChanges
);
3511 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3512 const wxString
& value
)
3514 wxTreeEvent
le(wxEVT_TREE_END_LABEL_EDIT
, this, item
);
3516 le
.m_editCancelled
= false;
3518 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3521 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3523 // let owner know that the edit was cancelled
3524 wxTreeEvent
le(wxEVT_TREE_END_LABEL_EDIT
, this, item
);
3525 le
.m_label
= wxEmptyString
;
3526 le
.m_editCancelled
= true;
3528 GetEventHandler()->ProcessEvent( le
);
3531 void wxGenericTreeCtrl::OnRenameTimer()
3533 EditLabel( m_current
);
3536 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3538 if ( !m_anchor
)return;
3540 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3542 // Is the mouse over a tree item button?
3544 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3545 wxGenericTreeItem
*underMouse
= thisItem
;
3547 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3548 #endif // wxUSE_TOOLTIPS
3551 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3552 (!event
.LeftIsDown()) &&
3554 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3562 if (underMouse
!= m_underMouse
)
3566 // unhighlight old item
3567 wxGenericTreeItem
*tmp
= m_underMouse
;
3568 m_underMouse
= NULL
;
3572 m_underMouse
= underMouse
;
3574 RefreshLine( m_underMouse
);
3578 // Determines what item we are hovering over and need a tooltip for
3579 wxTreeItemId hoverItem
= thisItem
;
3581 // We do not want a tooltip if we are dragging, or if the rename timer is
3583 if ( underMouseChanged
&&
3586 (!m_renameTimer
|| !m_renameTimer
->IsRunning()) )
3588 // Ask the tree control what tooltip (if any) should be shown
3590 hevent(wxEVT_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3592 // setting a tooltip upon leaving a view is getting the tooltip displayed
3593 // on the neighbouring view ...
3595 if ( event
.Leaving() )
3599 if ( GetEventHandler()->ProcessEvent(hevent
) )
3601 // If the user permitted the tooltip change, update it, otherwise
3602 // remove any old tooltip we might have.
3603 if ( hevent
.IsAllowed() )
3604 SetToolTip(hevent
.m_label
);
3611 // we process left mouse up event (enables in-place edit), middle/right down
3612 // (pass to the user code), left dbl click (activate item) and
3613 // dragging/moving events for items drag-and-drop
3614 if ( !(event
.LeftDown() ||
3616 event
.MiddleDown() ||
3617 event
.RightDown() ||
3618 event
.LeftDClick() ||
3620 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3629 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3631 if ( event
.Dragging() && !m_isDragging
)
3633 if (m_dragCount
== 0)
3638 if (m_dragCount
!= 3)
3640 // wait until user drags a bit further...
3644 wxEventType command
= event
.RightIsDown()
3645 ? wxEVT_TREE_BEGIN_RDRAG
3646 : wxEVT_TREE_BEGIN_DRAG
;
3648 wxTreeEvent
nevent(command
, this, m_current
);
3649 nevent
.SetPoint(CalcScrolledPosition(pt
));
3651 // by default the dragging is not supported, the user code must
3652 // explicitly allow the event for it to take place
3655 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3657 // we're going to drag this item
3658 m_isDragging
= true;
3660 // remember the old cursor because we will change it while
3662 m_oldCursor
= m_cursor
;
3664 // in a single selection control, hide the selection temporarily
3665 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3667 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3669 if ( m_oldSelection
)
3671 m_oldSelection
->SetHilight(false);
3672 RefreshLine(m_oldSelection
);
3679 else if ( event
.Dragging() )
3681 if ( item
!= m_dropTarget
)
3683 // unhighlight the previous drop target
3684 DrawDropEffect(m_dropTarget
);
3686 m_dropTarget
= item
;
3688 // highlight the current drop target if any
3689 DrawDropEffect(m_dropTarget
);
3691 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3694 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3695 // instead (needs to be tested!)
3700 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3704 // erase the highlighting
3705 DrawDropEffect(m_dropTarget
);
3707 if ( m_oldSelection
)
3709 m_oldSelection
->SetHilight(true);
3710 RefreshLine(m_oldSelection
);
3711 m_oldSelection
= NULL
;
3714 // generate the drag end event
3715 wxTreeEvent
eventEndDrag(wxEVT_TREE_END_DRAG
, this, item
);
3717 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3719 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3721 m_isDragging
= false;
3722 m_dropTarget
= NULL
;
3724 SetCursor(m_oldCursor
);
3726 #if defined( __WXMSW__ ) || defined(__WXMAC__) || defined(__WXGTK20__)
3729 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3730 // instead (needs to be tested!)
3736 // If we got to this point, we are not dragging or moving the mouse.
3737 // Because the code in carbon/toplevel.cpp will only set focus to the
3738 // tree if we skip for EVT_LEFT_DOWN, we MUST skip this event here for
3740 // We skip even if we didn't hit an item because we still should
3741 // restore focus to the tree control even if we didn't exactly hit an
3743 if ( event
.LeftDown() )
3748 // here we process only the messages which happen on tree items
3752 if (item
== NULL
) return; /* we hit the blank area */
3754 if ( event
.RightDown() )
3756 // If the item is already selected, do not update the selection.
3757 // Multi-selections should not be cleared if a selected item is
3759 if (!IsSelected(item
))
3761 DoSelectItem(item
, true, false);
3765 nevent(wxEVT_TREE_ITEM_RIGHT_CLICK
, this, item
);
3766 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3767 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3769 // Consistent with MSW (for now), send the ITEM_MENU *after*
3770 // the RIGHT_CLICK event. TODO: This behaviour may change.
3771 wxTreeEvent
nevent2(wxEVT_TREE_ITEM_MENU
, this, item
);
3772 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3773 GetEventHandler()->ProcessEvent(nevent2
);
3775 else if ( event
.MiddleDown() )
3778 nevent(wxEVT_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3779 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3780 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3782 else if ( event
.LeftUp() )
3784 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3787 nevent(wxEVT_TREE_STATE_IMAGE_CLICK
, this, item
);
3788 GetEventHandler()->ProcessEvent(nevent
);
3791 // this facilitates multiple-item drag-and-drop
3793 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3795 wxArrayTreeItemIds selections
;
3796 size_t count
= GetSelections(selections
);
3802 DoSelectItem(item
, true, false);
3808 if ( (item
== m_current
) &&
3809 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3810 HasFlag(wxTR_EDIT_LABELS
) )
3812 if ( m_renameTimer
)
3814 if ( m_renameTimer
->IsRunning() )
3815 m_renameTimer
->Stop();
3819 m_renameTimer
= new wxTreeRenameTimer( this );
3822 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3825 m_lastOnSame
= false;
3828 else // !RightDown() && !MiddleDown() && !LeftUp()
3830 // ==> LeftDown() || LeftDClick()
3831 if ( event
.LeftDown() )
3833 // If we click on an already selected item but do it to return
3834 // the focus to the control, it shouldn't start editing the
3835 // item label because it's too easy to start editing
3836 // accidentally (and also because nobody else does it like
3837 // this). So only set this flag, used to decide whether we
3838 // should start editing the label later, if we already have
3840 m_lastOnSame
= item
== m_current
&& HasFocus();
3843 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3845 // only toggle the item for a single click, double click on
3846 // the button doesn't do anything (it toggles the item twice)
3847 if ( event
.LeftDown() )
3852 // don't select the item if the button was clicked
3857 // clear the previously selected items, if the
3858 // user clicked outside of the present selection.
3859 // otherwise, perform the deselection on mouse-up.
3860 // this allows multiple drag and drop to work.
3861 // but if Cmd is down, toggle selection of the clicked item
3862 if (!IsSelected(item
) || event
.CmdDown())
3864 // how should the selection work for this event?
3865 bool is_multiple
, extended_select
, unselect_others
;
3866 EventFlagsToSelType(GetWindowStyleFlag(),
3873 DoSelectItem(item
, unselect_others
, extended_select
);
3877 // For some reason, Windows isn't recognizing a left double-click,
3878 // so we need to simulate it here. Allow 200 milliseconds for now.
3879 if ( event
.LeftDClick() )
3881 // double clicking should not start editing the item label
3882 if ( m_renameTimer
)
3883 m_renameTimer
->Stop();
3885 m_lastOnSame
= false;
3887 // send activate event first
3889 nevent(wxEVT_TREE_ITEM_ACTIVATED
, this, item
);
3890 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3891 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3893 // if the user code didn't process the activate event,
3894 // handle it ourselves by toggling the item when it is
3896 if ( item
->HasPlus() )
3906 void wxGenericTreeCtrl::OnInternalIdle()
3908 wxWindow::OnInternalIdle();
3910 // Check if we need to select the root item
3911 // because nothing else has been selected.
3912 // Delaying it means that we can invoke event handlers
3913 // as required, when a first item is selected.
3914 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3917 SelectItem(m_select_me
);
3918 else if (GetRootItem().IsOk())
3919 SelectItem(GetRootItem());
3922 // after all changes have been done to the tree control,
3923 // actually redraw the tree when everything is over
3925 DoDirtyProcessing();
3929 wxGenericTreeCtrl::CalculateLevel(wxGenericTreeItem
*item
,
3934 int x
= level
*m_indent
;
3935 if (!HasFlag(wxTR_HIDE_ROOT
))
3939 else if (level
== 0)
3941 // a hidden root is not evaluated, but its
3942 // children are always calculated
3946 item
->CalculateSize(this, dc
);
3949 item
->SetX( x
+m_spacing
);
3951 y
+= GetLineHeight(item
);
3953 if ( !item
->IsExpanded() )
3955 // we don't need to calculate collapsed branches
3960 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3961 size_t n
, count
= children
.GetCount();
3963 for (n
= 0; n
< count
; ++n
)
3964 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3967 void wxGenericTreeCtrl::CalculatePositions()
3969 if ( !m_anchor
) return;
3971 wxClientDC
dc(this);
3974 dc
.SetFont( m_normalFont
);
3976 dc
.SetPen( m_dottedPen
);
3977 //if(GetImageList() == NULL)
3978 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3981 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3984 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3987 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3990 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3992 if (m_dirty
|| IsFrozen() )
3995 wxSize client
= GetClientSize();
3998 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3999 rect
.width
= client
.x
;
4000 rect
.height
= client
.y
;
4002 Refresh(true, &rect
);
4004 AdjustMyScrollbars();
4007 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
4009 if (m_dirty
|| IsFrozen() )
4013 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
4014 rect
.width
= GetClientSize().x
;
4015 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
4017 Refresh(true, &rect
);
4020 void wxGenericTreeCtrl::RefreshSelected()
4025 // TODO: this is awfully inefficient, we should keep the list of all
4026 // selected items internally, should be much faster
4028 RefreshSelectedUnder(m_anchor
);
4031 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
4036 if ( item
->IsSelected() )
4039 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
4040 size_t count
= children
.GetCount();
4041 for ( size_t n
= 0; n
< count
; n
++ )
4043 RefreshSelectedUnder(children
[n
]);
4047 void wxGenericTreeCtrl::DoThaw()
4049 wxTreeCtrlBase::DoThaw();
4052 DoDirtyProcessing();
4057 // ----------------------------------------------------------------------------
4058 // changing colours: we need to refresh the tree control
4059 // ----------------------------------------------------------------------------
4061 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
4063 if ( !wxWindow::SetBackgroundColour(colour
) )
4071 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
4073 if ( !wxWindow::SetForegroundColour(colour
) )
4081 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
4084 wxTreeItemId itemId
= event
.GetItem();
4085 const wxGenericTreeItem
* const pItem
= (wxGenericTreeItem
*)itemId
.m_pItem
;
4087 // Check if the item fits into the client area:
4088 if ( pItem
->GetX() + pItem
->GetWidth() > GetClientSize().x
)
4090 // If it doesn't, show its full text in the tooltip.
4091 event
.SetLabel(pItem
->GetText());
4094 #endif // wxUSE_TOOLTIPS
4096 // veto processing the event, nixing any tooltip
4102 // NOTE: If using the wxListBox visual attributes works everywhere then this can
4103 // be removed, as well as the #else case below.
4104 #define _USE_VISATTR 0
4109 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
4111 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
4115 // Use the same color scheme as wxListBox
4116 return wxListBox::GetClassDefaultAttributes(variant
);
4118 wxVisualAttributes attr
;
4119 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
4120 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
4121 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
4126 void wxGenericTreeCtrl::DoDirtyProcessing()
4133 CalculatePositions();
4135 AdjustMyScrollbars();
4138 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
4140 // make sure all positions are calculated as normally this only done during
4141 // idle time but we need them for base class DoGetBestSize() to return the
4143 wxConstCast(this, wxGenericTreeCtrl
)->CalculatePositions();
4145 wxSize size
= wxTreeCtrlBase::DoGetBestSize();
4147 // there seems to be an implicit extra border around the items, although
4148 // I'm not really sure where does it come from -- but without this, the
4149 // scrollbars appear in a tree with default/best size
4152 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
4153 // scrollbars still appear
4154 const wxSize
& borderSize
= GetWindowBorderSize();
4156 int dx
= (size
.x
- borderSize
.x
) % PIXELS_PER_UNIT
;
4158 size
.x
+= PIXELS_PER_UNIT
- dx
;
4159 int dy
= (size
.y
- borderSize
.y
) % PIXELS_PER_UNIT
;
4161 size
.y
+= PIXELS_PER_UNIT
- dy
;
4163 // we need to update the cache too as the base class cached its own value
4164 CacheBestSize(size
);
4169 #endif // wxUSE_TREECTRL