1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/treectrl.cpp
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
7 // Copyright: (c) 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/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/msw/missing.h"
33 #include "wx/dynarray.h"
36 #include "wx/settings.h"
39 #include "wx/dynlib.h"
40 #include "wx/msw/private.h"
42 #include "wx/imaglist.h"
43 #include "wx/msw/dragimag.h"
44 #include "wx/msw/uxtheme.h"
46 // macros to hide the cast ugliness
47 // --------------------------------
49 // get HTREEITEM from wxTreeItemId
50 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
53 // older SDKs are missing these
54 #ifndef TVN_ITEMCHANGINGA
56 #define TVN_ITEMCHANGINGA (TVN_FIRST-16)
57 #define TVN_ITEMCHANGINGW (TVN_FIRST-17)
59 typedef struct tagNMTVITEMCHANGE
72 // this helper class is used on vista systems for preventing unwanted
73 // item state changes in the vista tree control. It is only effective in
74 // multi-select mode on vista systems.
76 // The vista tree control includes some new code that originally broke the
77 // multi-selection tree, causing seemingly spurious item selection state changes
78 // during Shift or Ctrl-click item selection. (To witness the original broken
79 // behaviour, simply make IsLocked() below always return false). This problem was
80 // solved by using the following class to 'unlock' an item's selection state.
82 class TreeItemUnlocker
85 // unlock a single item
86 TreeItemUnlocker(HTREEITEM item
)
88 m_oldUnlockedItem
= ms_unlockedItem
;
89 ms_unlockedItem
= item
;
92 // unlock all items, don't use unless absolutely necessary
95 m_oldUnlockedItem
= ms_unlockedItem
;
96 ms_unlockedItem
= (HTREEITEM
)-1;
99 // lock everything back
100 ~TreeItemUnlocker() { ms_unlockedItem
= m_oldUnlockedItem
; }
103 // check if the item state is currently locked
104 static bool IsLocked(HTREEITEM item
)
105 { return ms_unlockedItem
!= (HTREEITEM
)-1 && item
!= ms_unlockedItem
; }
108 static HTREEITEM ms_unlockedItem
;
109 HTREEITEM m_oldUnlockedItem
;
111 wxDECLARE_NO_COPY_CLASS(TreeItemUnlocker
);
114 HTREEITEM
TreeItemUnlocker::ms_unlockedItem
= NULL
;
116 // another helper class: set the variable to true during its lifetime and reset
117 // it to false when it is destroyed
119 // it is currently always used with wxTreeCtrl::m_changingSelection
123 TempSetter(bool& var
) : m_var(var
)
125 wxASSERT_MSG( !m_var
, "variable shouldn't be already set" );
137 wxDECLARE_NO_COPY_CLASS(TempSetter
);
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
147 // Work around a problem with TreeView_GetItemRect() when using MinGW/Cygwin:
148 // it results in warnings about breaking strict aliasing rules because HITEM is
149 // passed via a RECT pointer, so use a union to avoid them and define our own
150 // version of the standard macro using it.
151 union TVGetItemRectParam
158 wxTreeView_GetItemRect(HWND hwnd
,
160 TVGetItemRectParam
& param
,
164 return ::SendMessage(hwnd
, TVM_GETITEMRECT
, fItemRect
,
165 (LPARAM
)¶m
) == TRUE
;
168 } // anonymous namespace
170 // wrappers for TreeView_GetItem/TreeView_SetItem
171 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
174 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
175 tvi
.stateMask
= TVIS_SELECTED
;
178 TreeItemUnlocker
unlocker(hItem
);
180 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
182 wxLogLastError(wxT("TreeView_GetItem"));
185 return (tvi
.state
& TVIS_SELECTED
) != 0;
188 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
191 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
192 tvi
.stateMask
= TVIS_SELECTED
;
193 tvi
.state
= select
? TVIS_SELECTED
: 0;
196 TreeItemUnlocker
unlocker(hItem
);
198 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
200 wxLogLastError(wxT("TreeView_SetItem"));
207 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
209 SelectItem(hwndTV
, htItem
, false);
212 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
214 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
217 // helper function which selects all items in a range and, optionally,
218 // deselects all the other ones
220 // returns true if the selection changed at all or false if nothing changed
222 // flags for SelectRange()
225 SR_SIMULATE
= 1, // don't do anything, just return true or false
226 SR_UNSELECT_OTHERS
= 2 // deselect the items not in range
229 static bool SelectRange(HWND hwndTV
,
234 // find the first (or last) item and select it
235 bool changed
= false;
237 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
239 while ( htItem
&& cont
)
241 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
243 if ( !IsItemSelected(hwndTV
, htItem
) )
245 if ( !(flags
& SR_SIMULATE
) )
247 SelectItem(hwndTV
, htItem
);
255 else // not first or last
257 if ( flags
& SR_UNSELECT_OTHERS
)
259 if ( IsItemSelected(hwndTV
, htItem
) )
261 if ( !(flags
& SR_SIMULATE
) )
262 UnselectItem(hwndTV
, htItem
);
269 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
272 // select the items in range
273 cont
= htFirst
!= htLast
;
274 while ( htItem
&& cont
)
276 if ( !IsItemSelected(hwndTV
, htItem
) )
278 if ( !(flags
& SR_SIMULATE
) )
280 SelectItem(hwndTV
, htItem
);
286 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
288 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
291 // optionally deselect the rest
292 if ( flags
& SR_UNSELECT_OTHERS
)
296 if ( IsItemSelected(hwndTV
, htItem
) )
298 if ( !(flags
& SR_SIMULATE
) )
300 UnselectItem(hwndTV
, htItem
);
306 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
310 // seems to be necessary - otherwise the just selected items don't always
311 // appear as selected
312 if ( !(flags
& SR_SIMULATE
) )
314 UpdateWindow(hwndTV
);
320 // helper function which tricks the standard control into changing the focused
321 // item without changing anything else (if someone knows why Microsoft doesn't
322 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
324 // returns true if the focus was changed, false if the given item was already
326 static bool SetFocus(HWND hwndTV
, HTREEITEM htItem
)
329 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
331 if ( htItem
== htFocus
)
336 // remember the selection state of the item
337 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
339 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
341 // prevent the tree from unselecting the old focus which it
342 // would do by default (TreeView_SelectItem unselects the
344 TreeView_SelectItem(hwndTV
, 0);
345 SelectItem(hwndTV
, htFocus
);
348 TreeView_SelectItem(hwndTV
, htItem
);
352 // need to clear the selection which TreeView_SelectItem() gave
354 UnselectItem(hwndTV
, htItem
);
356 //else: was selected, still selected - ok
360 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
362 // just clear the focus
363 TreeView_SelectItem(hwndTV
, 0);
365 if ( wasFocusSelected
)
367 // restore the selection state
368 SelectItem(hwndTV
, htFocus
);
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 // a convenient wrapper around TV_ITEM struct which adds a ctor
381 #pragma warning( disable : 4097 ) // inheriting from typedef
384 struct wxTreeViewItem
: public TV_ITEM
386 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
387 UINT mask_
, // fields which are valid
388 UINT stateMask_
= 0) // for TVIF_STATE only
392 // hItem member is always valid
393 mask
= mask_
| TVIF_HANDLE
;
394 stateMask
= stateMask_
;
399 // ----------------------------------------------------------------------------
400 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
402 // We need this for a couple of reasons:
404 // 1) This class is needed for support of different images: the Win32 common
405 // control natively supports only 2 images (the normal one and another for the
406 // selected state). We wish to provide support for 2 more of them for folder
407 // items (i.e. those which have children): for expanded state and for expanded
408 // selected state. For this we use this structure to store the additional items
411 // 2) This class is also needed to hold the HITEM so that we can sort
412 // it correctly in the MSW sort callback.
414 // In addition it makes other workarounds such as this easier and helps
415 // simplify the code.
416 // ----------------------------------------------------------------------------
418 class wxTreeItemParam
425 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
431 // dtor deletes the associated data as well
432 virtual ~wxTreeItemParam() { delete m_data
; }
435 // get the real data associated with the item
436 wxTreeItemData
*GetData() const { return m_data
; }
438 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
440 // do we have such image?
441 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
442 // get image, falling back to the other images if this one is not
444 int GetImage(wxTreeItemIcon which
) const
446 int image
= m_images
[which
];
451 case wxTreeItemIcon_SelectedExpanded
:
452 // We consider that expanded icon is more important than
453 // selected so test for it first.
454 image
= m_images
[wxTreeItemIcon_Expanded
];
456 image
= m_images
[wxTreeItemIcon_Selected
];
461 case wxTreeItemIcon_Selected
:
462 case wxTreeItemIcon_Expanded
:
463 image
= m_images
[wxTreeItemIcon_Normal
];
466 case wxTreeItemIcon_Normal
:
471 wxFAIL_MSG( wxT("unsupported wxTreeItemIcon value") );
477 // change the given image
478 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
481 const wxTreeItemId
& GetItem() const { return m_item
; }
483 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
486 // all the images associated with the item
487 int m_images
[wxTreeItemIcon_Max
];
489 // item for sort callbacks
492 // the real client data
493 wxTreeItemData
*m_data
;
495 wxDECLARE_NO_COPY_CLASS(wxTreeItemParam
);
498 // wxVirutalNode is used in place of a single root when 'hidden' root is
500 class wxVirtualNode
: public wxTreeViewItem
503 wxVirtualNode(wxTreeItemParam
*param
)
504 : wxTreeViewItem(TVI_ROOT
, 0)
514 wxTreeItemParam
*GetParam() const { return m_param
; }
515 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
518 wxTreeItemParam
*m_param
;
520 wxDECLARE_NO_COPY_CLASS(wxVirtualNode
);
524 #pragma warning( default : 4097 )
527 // a macro to get the virtual root, returns NULL if none
528 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
530 // returns true if the item is the virtual root
531 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
533 // a class which encapsulates the tree traversal logic: it vists all (unless
534 // OnVisit() returns false) items under the given one
535 class wxTreeTraversal
538 wxTreeTraversal(const wxTreeCtrl
*tree
)
543 // give it a virtual dtor: not really needed as the class is never used
544 // polymorphically and not even allocated on heap at all, but this is safer
545 // (in case it ever is) and silences the compiler warnings for now
546 virtual ~wxTreeTraversal() { }
548 // do traverse the tree: visit all items (recursively by default) under the
549 // given one; return true if all items were traversed or false if the
550 // traversal was aborted because OnVisit returned false
551 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
553 // override this function to do whatever is needed for each item, return
554 // false to stop traversing
555 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
558 const wxTreeCtrl
*GetTree() const { return m_tree
; }
561 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
563 const wxTreeCtrl
*m_tree
;
565 wxDECLARE_NO_COPY_CLASS(wxTreeTraversal
);
568 // internal class for getting the selected items
569 class TraverseSelections
: public wxTreeTraversal
572 TraverseSelections(const wxTreeCtrl
*tree
,
573 wxArrayTreeItemIds
& selections
)
574 : wxTreeTraversal(tree
), m_selections(selections
)
576 m_selections
.Empty();
578 if (tree
->GetCount() > 0)
579 DoTraverse(tree
->GetRootItem());
582 virtual bool OnVisit(const wxTreeItemId
& item
)
584 const wxTreeCtrl
* const tree
= GetTree();
586 // can't visit a virtual node.
587 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
592 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
594 m_selections
.Add(item
);
600 size_t GetCount() const { return m_selections
.GetCount(); }
603 wxArrayTreeItemIds
& m_selections
;
605 wxDECLARE_NO_COPY_CLASS(TraverseSelections
);
608 // internal class for counting tree items
609 class TraverseCounter
: public wxTreeTraversal
612 TraverseCounter(const wxTreeCtrl
*tree
,
613 const wxTreeItemId
& root
,
615 : wxTreeTraversal(tree
)
619 DoTraverse(root
, recursively
);
622 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
629 size_t GetCount() const { return m_count
; }
634 wxDECLARE_NO_COPY_CLASS(TraverseCounter
);
637 // ----------------------------------------------------------------------------
639 // ----------------------------------------------------------------------------
641 // ----------------------------------------------------------------------------
643 // ----------------------------------------------------------------------------
645 // indices in gs_expandEvents table below
660 // handy table for sending events - it has to be initialized during run-time
661 // now so can't be const any more
662 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
665 but logically it's a const table with the following entries:
668 { wxEVT_TREE_ITEM_COLLAPSED, wxEVT_TREE_ITEM_COLLAPSING },
669 { wxEVT_TREE_ITEM_EXPANDED, wxEVT_TREE_ITEM_EXPANDING }
673 // ============================================================================
675 // ============================================================================
677 // ----------------------------------------------------------------------------
679 // ----------------------------------------------------------------------------
681 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
683 if ( !OnVisit(root
) )
686 return Traverse(root
, recursively
);
689 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
691 wxTreeItemIdValue cookie
;
692 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
693 while ( child
.IsOk() )
695 // depth first traversal
696 if ( recursively
&& !Traverse(child
, true) )
699 if ( !OnVisit(child
) )
702 child
= m_tree
->GetNextChild(root
, cookie
);
708 // ----------------------------------------------------------------------------
709 // construction and destruction
710 // ----------------------------------------------------------------------------
712 void wxTreeCtrl::Init()
715 m_hasAnyAttr
= false;
719 m_pVirtualRoot
= NULL
;
720 m_dragStarted
= false;
722 m_changingSelection
= false;
723 m_triggerStateImageClick
= false;
724 m_mouseUpDeselect
= false;
726 // initialize the global array of events now as it can't be done statically
727 // with the wxEVT_XXX values being allocated during run-time only
728 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_TREE_ITEM_COLLAPSED
;
729 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_TREE_ITEM_COLLAPSING
;
730 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_TREE_ITEM_EXPANDED
;
731 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_TREE_ITEM_EXPANDING
;
734 bool wxTreeCtrl::Create(wxWindow
*parent
,
739 const wxValidator
& validator
,
740 const wxString
& name
)
744 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
745 style
|= wxBORDER_SUNKEN
;
747 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
751 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
752 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
754 if ( !(m_windowStyle
& wxTR_NO_LINES
) )
755 wstyle
|= TVS_HASLINES
;
756 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
757 wstyle
|= TVS_HASBUTTONS
;
759 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
760 wstyle
|= TVS_EDITLABELS
;
762 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
763 wstyle
|= TVS_LINESATROOT
;
765 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
767 if ( wxApp::GetComCtl32Version() >= 471 )
768 wstyle
|= TVS_FULLROWSELECT
;
771 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
772 // Need so that TVN_GETINFOTIP messages will be sent
773 wstyle
|= TVS_INFOTIP
;
776 // Create the tree control.
777 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
780 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
781 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
783 wxSetCCUnicodeFormat(GetHwnd());
785 if ( m_windowStyle
& wxTR_TWIST_BUTTONS
)
787 // Under Vista and later Explorer uses rotating ("twist") buttons
788 // instead of the default "+/-" ones so apply its theme to the tree
789 // control to implement this style.
790 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
792 if ( wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive() )
794 theme
->SetWindowTheme(GetHwnd(), L
"EXPLORER", NULL
);
802 wxTreeCtrl::~wxTreeCtrl()
804 m_isBeingDeleted
= true;
806 // delete any attributes
809 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
811 // prevent TVN_DELETEITEM handler from deleting the attributes again!
812 m_hasAnyAttr
= false;
817 // delete user data to prevent memory leaks
818 // also deletes hidden root node storage.
822 // ----------------------------------------------------------------------------
824 // ----------------------------------------------------------------------------
826 /* static */ wxVisualAttributes
827 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
829 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
831 // common controls have their own default font
832 attrs
.font
= wxGetCCDefaultFont();
838 // simple wrappers which add error checking in debug mode
840 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
842 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
843 wxT("can't retrieve virtual root item") );
845 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
847 wxLogLastError(wxT("TreeView_GetItem"));
855 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
857 TreeItemUnlocker
unlocker(tvItem
->hItem
);
859 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
861 wxLogLastError(wxT("TreeView_SetItem"));
865 unsigned int wxTreeCtrl::GetCount() const
867 return (unsigned int)TreeView_GetCount(GetHwnd());
870 unsigned int wxTreeCtrl::GetIndent() const
872 return TreeView_GetIndent(GetHwnd());
875 void wxTreeCtrl::SetIndent(unsigned int indent
)
877 TreeView_SetIndent(GetHwnd(), indent
);
880 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
883 (void) TreeView_SetImageList(GetHwnd(),
884 imageList
? imageList
->GetHIMAGELIST() : 0,
888 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
890 if (m_ownsImageListNormal
)
891 delete m_imageListNormal
;
893 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
894 m_ownsImageListNormal
= false;
897 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
899 if (m_ownsImageListState
) delete m_imageListState
;
900 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
901 m_ownsImageListState
= false;
904 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
905 bool recursively
) const
907 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
909 TraverseCounter
counter(this, item
, recursively
);
910 return counter
.GetCount() - 1;
913 // ----------------------------------------------------------------------------
915 // ----------------------------------------------------------------------------
917 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
919 if ( !wxWindowBase::SetBackgroundColour(colour
) )
922 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
927 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
929 if ( !wxWindowBase::SetForegroundColour(colour
) )
932 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
937 // ----------------------------------------------------------------------------
939 // ----------------------------------------------------------------------------
941 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
943 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
946 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
948 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
950 wxChar buf
[512]; // the size is arbitrary...
952 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
953 tvItem
.pszText
= buf
;
954 tvItem
.cchTextMax
= WXSIZEOF(buf
);
955 if ( !DoGetItem(&tvItem
) )
957 // don't return some garbage which was on stack, but an empty string
961 return wxString(buf
);
964 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
966 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
968 if ( IS_VIRTUAL_ROOT(item
) )
971 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
972 tvItem
.pszText
= wxMSW_CONV_LPTSTR(text
);
975 // when setting the text of the item being edited, the text control should
976 // be updated to reflect the new text as well, otherwise calling
977 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
979 // don't use GetEditControl() here because m_textCtrl is not set yet
980 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
983 if ( item
== m_idEdited
)
985 ::SetWindowText(hwndEdit
, text
.t_str());
990 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
991 wxTreeItemIcon which
) const
993 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
995 if ( IsHiddenRoot(item
) )
997 // no images for hidden root item
1001 wxTreeItemParam
*param
= GetItemParam(item
);
1003 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
1006 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1007 wxTreeItemIcon which
)
1009 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1010 wxCHECK_RET( which
>= 0 &&
1011 which
< wxTreeItemIcon_Max
,
1012 wxT("invalid image index"));
1015 if ( IsHiddenRoot(item
) )
1017 // no images for hidden root item
1021 wxTreeItemParam
*data
= GetItemParam(item
);
1025 data
->SetImage(image
, which
);
1030 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
1032 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1034 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1036 // hidden root may still have data.
1037 if ( IS_VIRTUAL_ROOT(item
) )
1039 return GET_VIRTUAL_ROOT()->GetParam();
1043 if ( !DoGetItem(&tvItem
) )
1048 return (wxTreeItemParam
*)tvItem
.lParam
;
1051 bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent
& event
) const
1053 if ( event
.m_item
.IsOk() )
1055 event
.SetClientObject(GetItemData(event
.m_item
));
1058 return HandleWindowEvent(event
);
1061 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1063 wxTreeItemParam
*data
= GetItemParam(item
);
1065 return data
? data
->GetData() : NULL
;
1068 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1070 // first, associate this piece of data with this item
1076 wxTreeItemParam
*param
= GetItemParam(item
);
1078 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1080 param
->SetData(data
);
1083 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1085 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1087 if ( IS_VIRTUAL_ROOT(item
) )
1090 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1091 tvItem
.cChildren
= (int)has
;
1095 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1097 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1099 if ( IS_VIRTUAL_ROOT(item
) )
1102 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1103 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1107 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1109 if ( IS_VIRTUAL_ROOT(item
) )
1112 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1113 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1117 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1119 if ( IS_VIRTUAL_ROOT(item
) )
1123 if ( GetBoundingRect(item
, rect
) )
1129 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1131 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1133 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1134 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1137 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1139 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1141 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1142 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1145 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1147 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1149 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1150 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1153 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1154 const wxColour
& col
)
1156 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1158 wxTreeItemAttr
*attr
;
1159 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1160 if ( it
== m_attrs
.end() )
1162 m_hasAnyAttr
= true;
1164 m_attrs
[item
.m_pItem
] =
1165 attr
= new wxTreeItemAttr
;
1172 attr
->SetTextColour(col
);
1177 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1178 const wxColour
& col
)
1180 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1182 wxTreeItemAttr
*attr
;
1183 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1184 if ( it
== m_attrs
.end() )
1186 m_hasAnyAttr
= true;
1188 m_attrs
[item
.m_pItem
] =
1189 attr
= new wxTreeItemAttr
;
1191 else // already in the hash
1196 attr
->SetBackgroundColour(col
);
1201 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1203 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1205 wxTreeItemAttr
*attr
;
1206 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1207 if ( it
== m_attrs
.end() )
1209 m_hasAnyAttr
= true;
1211 m_attrs
[item
.m_pItem
] =
1212 attr
= new wxTreeItemAttr
;
1214 else // already in the hash
1219 attr
->SetFont(font
);
1221 // Reset the item's text to ensure that the bounding rect will be adjusted
1222 // for the new font.
1223 SetItemText(item
, GetItemText(item
));
1228 // ----------------------------------------------------------------------------
1230 // ----------------------------------------------------------------------------
1232 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1234 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1236 if ( item
== wxTreeItemId(TVI_ROOT
) )
1238 // virtual (hidden) root is never visible
1242 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1243 TVGetItemRectParam param
;
1245 // true means to get rect for just the text, not the whole line
1246 if ( !wxTreeView_GetItemRect(GetHwnd(), HITEM(item
), param
, TRUE
) )
1248 // if TVM_GETITEMRECT returned false, then the item is definitely not
1249 // visible (because its parent is not expanded)
1253 // however if it returned true, the item might still be outside the
1254 // currently visible part of the tree, test for it (notice that partly
1255 // visible means visible here)
1256 return param
.rect
.bottom
> 0 && param
.rect
.top
< GetClientSize().y
;
1259 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1261 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1263 if ( IS_VIRTUAL_ROOT(item
) )
1265 wxTreeItemIdValue cookie
;
1266 return GetFirstChild(item
, cookie
).IsOk();
1269 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1272 return tvItem
.cChildren
!= 0;
1275 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1277 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1279 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1282 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1285 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1287 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1289 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1292 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1295 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1297 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1299 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1302 return (tvItem
.state
& TVIS_BOLD
) != 0;
1305 // ----------------------------------------------------------------------------
1307 // ----------------------------------------------------------------------------
1309 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1311 // Root may be real (visible) or virtual (hidden).
1312 if ( GET_VIRTUAL_ROOT() )
1315 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1318 wxTreeItemId
wxTreeCtrl::GetSelection() const
1320 wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE
), wxTreeItemId(),
1321 wxT("this only works with single selection controls") );
1323 return GetFocusedItem();
1326 wxTreeItemId
wxTreeCtrl::GetFocusedItem() const
1328 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1331 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1333 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1337 if ( IS_VIRTUAL_ROOT(item
) )
1339 // no parent for the virtual root
1344 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1345 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1347 // the top level items should have the virtual root as their parent
1352 return wxTreeItemId(hItem
);
1355 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1356 wxTreeItemIdValue
& cookie
) const
1358 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1360 // remember the last child returned in 'cookie'
1361 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1363 return wxTreeItemId(cookie
);
1366 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1367 wxTreeItemIdValue
& cookie
) const
1369 wxTreeItemId
fromCookie(cookie
);
1371 HTREEITEM hitem
= HITEM(fromCookie
);
1373 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1375 wxTreeItemId
item(hitem
);
1377 cookie
= item
.m_pItem
;
1382 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1384 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1386 // can this be done more efficiently?
1387 wxTreeItemIdValue cookie
;
1389 wxTreeItemId childLast
,
1390 child
= GetFirstChild(item
, cookie
);
1391 while ( child
.IsOk() )
1394 child
= GetNextChild(item
, cookie
);
1400 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1402 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1403 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1406 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1408 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1409 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1412 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1414 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1417 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1419 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1420 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1422 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1423 if ( next
.IsOk() && !IsVisible(next
) )
1425 // Win32 considers that any non-collapsed item is visible while we want
1426 // to return only really visible items
1433 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1435 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1436 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1438 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1439 if ( prev
.IsOk() && !IsVisible(prev
) )
1441 // just as above, Win32 function will happily return the previous item
1442 // in the tree for the first visible item too
1449 // ----------------------------------------------------------------------------
1450 // multiple selections emulation
1451 // ----------------------------------------------------------------------------
1453 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1455 TraverseSelections
selector(this, selections
);
1457 return selector
.GetCount();
1460 // ----------------------------------------------------------------------------
1462 // ----------------------------------------------------------------------------
1464 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1465 const wxTreeItemId
& hInsertAfter
,
1466 const wxString
& text
,
1467 int image
, int selectedImage
,
1468 wxTreeItemData
*data
)
1470 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1472 wxT("can't have more than one root in the tree") );
1474 TV_INSERTSTRUCT tvIns
;
1475 tvIns
.hParent
= HITEM(parent
);
1476 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1478 // this is how we insert the item as the first child: supply a NULL
1480 if ( !tvIns
.hInsertAfter
)
1482 tvIns
.hInsertAfter
= TVI_FIRST
;
1486 if ( !text
.empty() )
1489 tvIns
.item
.pszText
= wxMSW_CONV_LPTSTR(text
);
1493 tvIns
.item
.pszText
= NULL
;
1494 tvIns
.item
.cchTextMax
= 0;
1497 // create the param which will store the other item parameters
1498 wxTreeItemParam
*param
= new wxTreeItemParam
;
1500 // we return the images on demand as they depend on whether the item is
1501 // expanded or collapsed too in our case
1502 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1503 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1504 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1506 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1507 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1510 tvIns
.item
.lParam
= (LPARAM
)param
;
1511 tvIns
.item
.mask
= mask
;
1513 // don't use the hack below for the children of hidden root: this results
1514 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1515 const bool firstChild
= !IsHiddenRoot(parent
) &&
1516 !TreeView_GetChild(GetHwnd(), HITEM(parent
));
1518 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1521 wxLogLastError(wxT("TreeView_InsertItem"));
1524 // apparently some Windows versions (2000 and XP are reported to do this)
1525 // sometimes don't refresh the tree after adding the first child and so we
1526 // need this to make the "[+]" appear
1529 TVGetItemRectParam param
;
1531 wxTreeView_GetItemRect(GetHwnd(), HITEM(parent
), param
, FALSE
);
1532 ::InvalidateRect(GetHwnd(), ¶m
.rect
, FALSE
);
1535 // associate the application tree item with Win32 tree item handle
1538 // setup wxTreeItemData
1541 param
->SetData(data
);
1545 return wxTreeItemId(id
);
1548 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1549 int image
, int selectedImage
,
1550 wxTreeItemData
*data
)
1552 if ( HasFlag(wxTR_HIDE_ROOT
) )
1554 wxASSERT_MSG( !m_pVirtualRoot
, wxT("tree can have only a single root") );
1556 // create a virtual root item, the parent for all the others
1557 wxTreeItemParam
*param
= new wxTreeItemParam
;
1558 param
->SetData(data
);
1560 m_pVirtualRoot
= new wxVirtualNode(param
);
1565 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1566 text
, image
, selectedImage
, data
);
1569 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1571 const wxString
& text
,
1572 int image
, int selectedImage
,
1573 wxTreeItemData
*data
)
1575 wxTreeItemId idPrev
;
1576 if ( index
== (size_t)-1 )
1578 // special value: append to the end
1581 else // find the item from index
1583 wxTreeItemIdValue cookie
;
1584 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1585 while ( index
!= 0 && idCur
.IsOk() )
1590 idCur
= GetNextChild(parent
, cookie
);
1593 // assert, not check: if the index is invalid, we will append the item
1595 wxASSERT_MSG( index
== 0, wxT("bad index in wxTreeCtrl::InsertItem") );
1598 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1601 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1603 // unlock tree selections on vista, without this the
1604 // tree ctrl will eventually crash after item deletion
1605 TreeItemUnlocker unlock_all
;
1607 if ( HasFlag(wxTR_MULTIPLE
) )
1609 bool selected
= IsSelected(item
);
1614 next
= TreeView_GetNextVisible(GetHwnd(), HITEM(item
));
1618 next
= TreeView_GetPrevVisible(GetHwnd(), HITEM(item
));
1623 TempSetter
set(m_changingSelection
);
1624 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1626 wxLogLastError(wxT("TreeView_DeleteItem"));
1636 if ( item
== m_htSelStart
)
1637 m_htSelStart
.Unset();
1639 if ( item
== m_htClickedItem
)
1640 m_htClickedItem
.Unset();
1644 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
, this, next
);
1646 if ( IsTreeEventAllowed(changingEvent
) )
1648 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
, this, next
);
1649 (void)HandleTreeEvent(changedEvent
);
1653 DoUnselectItem(next
);
1660 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1662 wxLogLastError(wxT("TreeView_DeleteItem"));
1667 // delete all children (but don't delete the item itself)
1668 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1670 // unlock tree selections on vista for the duration of this call
1671 TreeItemUnlocker unlock_all
;
1673 wxTreeItemIdValue cookie
;
1675 wxArrayTreeItemIds children
;
1676 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1677 while ( child
.IsOk() )
1679 children
.Add(child
);
1681 child
= GetNextChild(item
, cookie
);
1684 size_t nCount
= children
.Count();
1685 for ( size_t n
= 0; n
< nCount
; n
++ )
1687 Delete(children
[n
]);
1691 void wxTreeCtrl::DeleteAllItems()
1693 // unlock tree selections on vista for the duration of this call
1694 TreeItemUnlocker unlock_all
;
1696 // invalidate all the items we store as they're going to become invalid
1698 m_htClickedItem
= wxTreeItemId();
1700 // delete the "virtual" root item.
1701 if ( GET_VIRTUAL_ROOT() )
1703 delete GET_VIRTUAL_ROOT();
1704 m_pVirtualRoot
= NULL
;
1707 // and all the real items
1709 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1711 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1715 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1717 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1718 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1719 flag
== TVE_EXPAND
||
1721 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1723 // A hidden root can be neither expanded nor collapsed.
1724 wxCHECK_RET( !IsHiddenRoot(item
),
1725 wxT("Can't expand/collapse hidden root node!") );
1727 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1728 // emulate them. This behaviour has changed slightly with comctl32.dll
1729 // v 4.70 - now it does send them but only the first time. To maintain
1730 // compatible behaviour and also in order to not have surprises with the
1731 // future versions, don't rely on this and still do everything ourselves.
1732 // To avoid that the messages be sent twice when the item is expanded for
1733 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1735 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1739 if ( IsExpanded(item
) )
1741 wxTreeEvent
event(wxEVT_TREE_ITEM_COLLAPSING
,
1742 this, wxTreeItemId(item
));
1744 if ( !IsTreeEventAllowed(event
) )
1748 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) )
1750 if ( IsExpanded(item
) )
1753 wxTreeEvent
event(wxEVT_TREE_ITEM_COLLAPSED
, this, item
);
1754 (void)HandleTreeEvent(event
);
1756 //else: change didn't took place, so do nothing at all
1759 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1761 DoExpand(item
, TVE_EXPAND
);
1764 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1766 DoExpand(item
, TVE_COLLAPSE
);
1769 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1771 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1774 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1776 DoExpand(item
, TVE_TOGGLE
);
1779 void wxTreeCtrl::Unselect()
1781 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE
),
1782 wxT("doesn't make sense, may be you want UnselectAll()?") );
1784 // the current focus
1785 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1792 if ( HasFlag(wxTR_MULTIPLE
) )
1794 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
1795 this, wxTreeItemId());
1796 changingEvent
.m_itemOld
= htFocus
;
1798 if ( IsTreeEventAllowed(changingEvent
) )
1802 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
1803 this, wxTreeItemId());
1804 changedEvent
.m_itemOld
= htFocus
;
1805 (void)HandleTreeEvent(changedEvent
);
1814 void wxTreeCtrl::DoUnselectAll()
1816 wxArrayTreeItemIds selections
;
1817 size_t count
= GetSelections(selections
);
1819 for ( size_t n
= 0; n
< count
; n
++ )
1821 DoUnselectItem(selections
[n
]);
1824 m_htSelStart
.Unset();
1827 void wxTreeCtrl::UnselectAll()
1829 if ( HasFlag(wxTR_MULTIPLE
) )
1831 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1832 if ( !htFocus
) return;
1834 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
, this);
1835 changingEvent
.m_itemOld
= htFocus
;
1837 if ( IsTreeEventAllowed(changingEvent
) )
1841 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
, this);
1842 changedEvent
.m_itemOld
= htFocus
;
1843 (void)HandleTreeEvent(changedEvent
);
1852 void wxTreeCtrl::DoSelectChildren(const wxTreeItemId
& parent
)
1856 wxTreeItemIdValue cookie
;
1857 wxTreeItemId child
= GetFirstChild(parent
, cookie
);
1858 while ( child
.IsOk() )
1860 DoSelectItem(child
, true);
1861 child
= GetNextChild(child
, cookie
);
1865 void wxTreeCtrl::SelectChildren(const wxTreeItemId
& parent
)
1867 wxCHECK_RET( HasFlag(wxTR_MULTIPLE
),
1868 "this only works with multiple selection controls" );
1870 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1872 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
, this);
1873 changingEvent
.m_itemOld
= htFocus
;
1875 if ( IsTreeEventAllowed(changingEvent
) )
1877 DoSelectChildren(parent
);
1879 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
, this);
1880 changedEvent
.m_itemOld
= htFocus
;
1881 (void)HandleTreeEvent(changedEvent
);
1885 void wxTreeCtrl::DoSelectItem(const wxTreeItemId
& item
, bool select
)
1887 TempSetter
set(m_changingSelection
);
1889 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1892 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1894 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't select hidden root item") );
1896 if ( select
== IsSelected(item
) )
1898 // nothing to do, the item is already in the requested state
1902 if ( HasFlag(wxTR_MULTIPLE
) )
1904 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
, this, item
);
1906 if ( IsTreeEventAllowed(changingEvent
) )
1908 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1909 DoSelectItem(item
, select
);
1913 SetFocusedItem(item
);
1916 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
1918 (void)HandleTreeEvent(changedEvent
);
1921 else // single selection
1923 wxTreeItemId itemOld
, itemNew
;
1926 itemOld
= GetSelection();
1929 else // deselecting the currently selected item
1932 // leave itemNew invalid
1935 // Recent versions of comctl32.dll send TVN_SELCHANG{ED,ING} events
1936 // when we call TreeView_SelectItem() but apparently some old ones did
1937 // not so send the events ourselves and ignore those generated by
1938 // TreeView_SelectItem() if m_changingSelection is set.
1940 changingEvent(wxEVT_TREE_SEL_CHANGING
, this, itemNew
);
1941 changingEvent
.SetOldItem(itemOld
);
1943 if ( IsTreeEventAllowed(changingEvent
) )
1945 TempSetter
set(m_changingSelection
);
1947 if ( !TreeView_SelectItem(GetHwnd(), HITEM(itemNew
)) )
1949 wxLogLastError(wxT("TreeView_SelectItem"));
1953 ::SetFocus(GetHwnd(), HITEM(item
));
1955 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
1957 changedEvent
.SetOldItem(itemOld
);
1958 (void)HandleTreeEvent(changedEvent
);
1961 //else: program vetoed the change
1965 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1967 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't show hidden root item") );
1970 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1973 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1975 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1977 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1981 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1986 void wxTreeCtrl::DeleteTextCtrl()
1990 // the HWND corresponding to this control is deleted by the tree
1991 // control itself and we don't know when exactly this happens, so check
1992 // if the window still exists before calling UnsubclassWin()
1993 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1995 m_textCtrl
->SetHWND(0);
1998 m_textCtrl
->UnsubclassWin();
1999 m_textCtrl
->SetHWND(0);
2000 wxDELETE(m_textCtrl
);
2006 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2007 wxClassInfo
*textControlClass
)
2009 wxASSERT( textControlClass
->IsKindOf(wxCLASSINFO(wxTextCtrl
)) );
2014 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
2015 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
2017 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2021 wxDELETE(m_textCtrl
);
2025 // textctrl is subclassed in MSWOnNotify
2029 // End label editing, optionally cancelling the edit
2030 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
2032 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
2037 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2039 TV_HITTESTINFO hitTestInfo
;
2040 hitTestInfo
.pt
.x
= (int)point
.x
;
2041 hitTestInfo
.pt
.y
= (int)point
.y
;
2043 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2048 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2049 flags |= wxTREE_HITTEST_##flag
2051 TRANSLATE_FLAG(ABOVE
);
2052 TRANSLATE_FLAG(BELOW
);
2053 TRANSLATE_FLAG(NOWHERE
);
2054 TRANSLATE_FLAG(ONITEMBUTTON
);
2055 TRANSLATE_FLAG(ONITEMICON
);
2056 TRANSLATE_FLAG(ONITEMINDENT
);
2057 TRANSLATE_FLAG(ONITEMLABEL
);
2058 TRANSLATE_FLAG(ONITEMRIGHT
);
2059 TRANSLATE_FLAG(ONITEMSTATEICON
);
2060 TRANSLATE_FLAG(TOLEFT
);
2061 TRANSLATE_FLAG(TORIGHT
);
2063 #undef TRANSLATE_FLAG
2065 return wxTreeItemId(hitTestInfo
.hItem
);
2068 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2070 bool textOnly
) const
2072 // Virtual root items have no bounding rectangle
2073 if ( IS_VIRTUAL_ROOT(item
) )
2078 TVGetItemRectParam param
;
2080 if ( wxTreeView_GetItemRect(GetHwnd(), HITEM(item
), param
, textOnly
) )
2082 rect
= wxRect(wxPoint(param
.rect
.left
, param
.rect
.top
),
2083 wxPoint(param
.rect
.right
, param
.rect
.bottom
));
2089 // couldn't retrieve rect: for example, item isn't visible
2094 void wxTreeCtrl::ClearFocusedItem()
2096 TempSetter
set(m_changingSelection
);
2098 if ( !TreeView_SelectItem(GetHwnd(), 0) )
2100 wxLogLastError(wxT("TreeView_SelectItem"));
2104 void wxTreeCtrl::SetFocusedItem(const wxTreeItemId
& item
)
2106 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2108 TempSetter
set(m_changingSelection
);
2110 ::SetFocus(GetHwnd(), HITEM(item
));
2113 void wxTreeCtrl::DoUnselectItem(const wxTreeItemId
& item
)
2115 TempSetter
set(m_changingSelection
);
2117 ::UnselectItem(GetHwnd(), HITEM(item
));
2120 void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId
& item
)
2122 TempSetter
set(m_changingSelection
);
2124 ::ToggleItemSelection(GetHwnd(), HITEM(item
));
2127 // ----------------------------------------------------------------------------
2129 // ----------------------------------------------------------------------------
2131 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2132 // functions such as IsDataIndirect()
2133 class wxTreeSortHelper
2136 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2139 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
2141 return ((wxTreeItemParam
*)lParam
)->GetItem();
2145 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2149 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2150 wxT("sorting tree without data doesn't make sense") );
2152 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2154 return tree
->OnCompareItems(GetIdFromData(pItem1
),
2155 GetIdFromData(pItem2
));
2158 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2160 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2162 // rely on the fact that TreeView_SortChildren does the same thing as our
2163 // default behaviour, i.e. sorts items alphabetically and so call it
2164 // directly if we're not in derived class (much more efficient!)
2165 // RN: Note that if you find you're code doesn't sort as expected this
2166 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
2167 // combo for your derived wxTreeCtrl if will sort without
2169 if ( GetClassInfo() == wxCLASSINFO(wxTreeCtrl
) )
2171 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2176 tvSort
.hParent
= HITEM(item
);
2177 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2178 tvSort
.lParam
= (LPARAM
)this;
2179 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2183 // ----------------------------------------------------------------------------
2185 // ----------------------------------------------------------------------------
2187 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
2189 if ( msg
->message
== WM_KEYDOWN
)
2191 // Only eat VK_RETURN if not being used by the application in
2192 // conjunction with modifiers
2193 if ( (msg
->wParam
== VK_RETURN
) && !wxIsAnyModifierDown() )
2195 // we need VK_RETURN to generate wxEVT_TREE_ITEM_ACTIVATED
2200 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
2203 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
2205 const int id
= (signed short)id_
;
2207 if ( cmd
== EN_UPDATE
)
2209 wxCommandEvent
event(wxEVT_TEXT
, id
);
2210 event
.SetEventObject( this );
2211 ProcessCommand(event
);
2213 else if ( cmd
== EN_KILLFOCUS
)
2215 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2216 event
.SetEventObject( this );
2217 ProcessCommand(event
);
2225 // command processed
2229 bool wxTreeCtrl::MSWIsOnItem(unsigned flags
) const
2231 unsigned mask
= TVHT_ONITEM
;
2232 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2233 mask
|= TVHT_ONITEMINDENT
| TVHT_ONITEMRIGHT
;
2235 return (flags
& mask
) != 0;
2238 bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey
)
2240 const bool bCtrl
= wxIsCtrlDown();
2241 const bool bShift
= wxIsShiftDown();
2242 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2251 if ( vkey
!= VK_RETURN
&& bCtrl
)
2253 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
2255 changingEvent
.m_itemOld
= htSel
;
2257 if ( IsTreeEventAllowed(changingEvent
) )
2259 DoToggleItemSelection(wxTreeItemId(htSel
));
2261 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
2263 changedEvent
.m_itemOld
= htSel
;
2264 (void)HandleTreeEvent(changedEvent
);
2269 wxArrayTreeItemIds selections
;
2270 size_t count
= GetSelections(selections
);
2272 if ( count
!= 1 || HITEM(selections
[0]) != htSel
)
2274 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
2276 changingEvent
.m_itemOld
= htSel
;
2278 if ( IsTreeEventAllowed(changingEvent
) )
2281 DoSelectItem(wxTreeItemId(htSel
));
2283 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
2285 changedEvent
.m_itemOld
= htSel
;
2286 (void)HandleTreeEvent(changedEvent
);
2294 if ( !bCtrl
&& !bShift
)
2296 wxArrayTreeItemIds selections
;
2301 next
= vkey
== VK_UP
2302 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2303 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2307 next
= GetRootItem();
2309 if ( IsHiddenRoot(next
) )
2310 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2318 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
2320 changingEvent
.m_itemOld
= htSel
;
2322 if ( IsTreeEventAllowed(changingEvent
) )
2326 SetFocusedItem(next
);
2328 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
2330 changedEvent
.m_itemOld
= htSel
;
2331 (void)HandleTreeEvent(changedEvent
);
2336 wxTreeItemId next
= vkey
== VK_UP
2337 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2338 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2345 if ( !m_htSelStart
)
2347 m_htSelStart
= htSel
;
2350 if ( bShift
&& SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2351 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2353 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
, this, next
);
2354 changingEvent
.m_itemOld
= htSel
;
2356 if ( IsTreeEventAllowed(changingEvent
) )
2358 SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2359 SR_UNSELECT_OTHERS
);
2361 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
, this, next
);
2362 changedEvent
.m_itemOld
= htSel
;
2363 (void)HandleTreeEvent(changedEvent
);
2367 SetFocusedItem(next
);
2372 if ( HasChildren(htSel
) && IsExpanded(htSel
) )
2378 wxTreeItemId next
= GetItemParent(htSel
);
2380 if ( next
.IsOk() && !IsHiddenRoot(next
) )
2382 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
2384 changingEvent
.m_itemOld
= htSel
;
2386 if ( IsTreeEventAllowed(changingEvent
) )
2390 SetFocusedItem(next
);
2392 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
2394 changedEvent
.m_itemOld
= htSel
;
2395 (void)HandleTreeEvent(changedEvent
);
2402 if ( !IsVisible(htSel
) )
2404 EnsureVisible(htSel
);
2407 if ( !HasChildren(htSel
) )
2410 if ( !IsExpanded(htSel
) )
2416 wxTreeItemId next
= TreeView_GetChild(GetHwnd(), htSel
);
2418 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
, this, next
);
2419 changingEvent
.m_itemOld
= htSel
;
2421 if ( IsTreeEventAllowed(changingEvent
) )
2425 SetFocusedItem(next
);
2427 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
, this, next
);
2428 changedEvent
.m_itemOld
= htSel
;
2429 (void)HandleTreeEvent(changedEvent
);
2437 wxTreeItemId next
= GetRootItem();
2439 if ( IsHiddenRoot(next
) )
2441 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2447 if ( vkey
== VK_END
)
2451 wxTreeItemId nextTemp
= TreeView_GetNextVisible(
2452 GetHwnd(), HITEM(next
));
2454 if ( !nextTemp
.IsOk() )
2461 if ( htSel
== HITEM(next
) )
2466 if ( !m_htSelStart
)
2468 m_htSelStart
= htSel
;
2471 if ( SelectRange(GetHwnd(),
2472 HITEM(m_htSelStart
), HITEM(next
),
2473 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2475 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
2477 changingEvent
.m_itemOld
= htSel
;
2479 if ( IsTreeEventAllowed(changingEvent
) )
2481 SelectRange(GetHwnd(),
2482 HITEM(m_htSelStart
), HITEM(next
),
2483 SR_UNSELECT_OTHERS
);
2484 SetFocusedItem(next
);
2486 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
2488 changedEvent
.m_itemOld
= htSel
;
2489 (void)HandleTreeEvent(changedEvent
);
2495 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
2497 changingEvent
.m_itemOld
= htSel
;
2499 if ( IsTreeEventAllowed(changingEvent
) )
2503 SetFocusedItem(next
);
2505 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
2507 changedEvent
.m_itemOld
= htSel
;
2508 (void)HandleTreeEvent(changedEvent
);
2518 wxTreeItemId firstVisible
= GetFirstVisibleItem();
2519 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2520 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2521 TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible
)) :
2522 TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible
));
2524 if ( !nextAdjacent
)
2529 wxTreeItemId nextStart
= firstVisible
;
2531 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2533 wxTreeItemId nextTemp
= (vkey
== VK_PRIOR
) ?
2534 TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart
)) :
2535 TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart
));
2537 if ( nextTemp
.IsOk() )
2539 nextStart
= nextTemp
;
2547 EnsureVisible(nextStart
);
2549 if ( vkey
== VK_NEXT
)
2551 wxTreeItemId nextEnd
= nextStart
;
2553 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2555 wxTreeItemId nextTemp
=
2556 TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd
));
2558 if ( nextTemp
.IsOk() )
2568 EnsureVisible(nextEnd
);
2573 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2574 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2575 TreeView_GetPrevVisible(GetHwnd(), htSel
) :
2576 TreeView_GetNextVisible(GetHwnd(), htSel
);
2578 if ( !nextAdjacent
)
2583 wxTreeItemId
next(htSel
);
2585 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2587 wxTreeItemId nextTemp
= vkey
== VK_PRIOR
?
2588 TreeView_GetPrevVisible(GetHwnd(), HITEM(next
)) :
2589 TreeView_GetNextVisible(GetHwnd(), HITEM(next
));
2591 if ( !nextTemp
.IsOk() )
2597 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
2599 changingEvent
.m_itemOld
= htSel
;
2601 if ( IsTreeEventAllowed(changingEvent
) )
2604 m_htSelStart
.Unset();
2606 SetFocusedItem(next
);
2608 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
2610 changedEvent
.m_itemOld
= htSel
;
2611 (void)HandleTreeEvent(changedEvent
);
2623 bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam
, WXLPARAM lParam
)
2625 wxTreeEvent
keyEvent(wxEVT_TREE_KEY_DOWN
, this);
2626 keyEvent
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
, wParam
, lParam
);
2628 bool processed
= HandleTreeEvent(keyEvent
);
2630 // generate a separate event for Space/Return
2631 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
2632 ((wParam
== VK_SPACE
) || (wParam
== VK_RETURN
)) )
2634 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2637 wxTreeEvent
activatedEvent(wxEVT_TREE_ITEM_ACTIVATED
,
2639 (void)HandleTreeEvent(activatedEvent
);
2646 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2647 // only do it during dragging, minimize wxWin overhead (this is important for
2648 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2649 // instead of passing by wxWin events
2651 wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2653 bool processed
= false;
2655 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2657 if ( nMsg
== WM_CONTEXTMENU
)
2659 int x
= GET_X_LPARAM(lParam
),
2660 y
= GET_Y_LPARAM(lParam
);
2662 // the item for which the menu should be shown
2665 // the position where the menu should be shown in client coordinates
2666 // (so that it can be passed directly to PopupMenu())
2669 if ( x
== -1 || y
== -1 )
2671 // this means that the event was generated from keyboard (e.g. with
2672 // Shift-F10 or special Windows menu key)
2674 // use the Explorer standard of putting the menu at the left edge
2675 // of the text, in the vertical middle of the text
2676 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2679 // Use the bounding rectangle of only the text part
2681 GetBoundingRect(item
, rect
, true);
2682 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
2685 else // event from mouse, use mouse position
2687 pt
= ScreenToClient(wxPoint(x
, y
));
2689 TV_HITTESTINFO tvhti
;
2693 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2694 item
= wxTreeItemId(tvhti
.hItem
);
2700 wxTreeEvent
event(wxEVT_TREE_ITEM_MENU
, this, item
);
2702 event
.m_pointDrag
= pt
;
2704 if ( HandleTreeEvent(event
) )
2706 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2709 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2711 // we only process mouse messages here and these parameters have the
2712 // same meaning for all of them
2713 int x
= GET_X_LPARAM(lParam
),
2714 y
= GET_Y_LPARAM(lParam
);
2716 TV_HITTESTINFO tvht
;
2720 HTREEITEM htOldItem
= TreeView_GetSelection(GetHwnd());
2721 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2725 case WM_LBUTTONDOWN
:
2729 m_htClickedItem
.Unset();
2731 if ( !MSWIsOnItem(tvht
.flags
) )
2733 if ( tvht
.flags
& TVHT_ONITEMBUTTON
)
2735 // either it's going to be handled by user code or
2736 // we're going to use it ourselves to toggle the
2737 // branch, in either case don't pass it to the base
2738 // class which would generate another mouse click event
2739 // for it even though it's already handled here
2743 if ( !HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2745 if ( !IsExpanded(htItem
) )
2756 m_focusLost
= false;
2762 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2763 m_ptClick
= wxPoint(x
, y
);
2765 if ( wParam
& MK_CONTROL
)
2767 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2769 m_htClickedItem
.Unset();
2773 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
2775 changingEvent
.m_itemOld
= htOldItem
;
2777 if ( IsTreeEventAllowed(changingEvent
) )
2779 // toggle selected state
2780 DoToggleItemSelection(wxTreeItemId(htItem
));
2782 SetFocusedItem(wxTreeItemId(htItem
));
2784 // reset on any click without Shift
2785 m_htSelStart
.Unset();
2787 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
2789 changedEvent
.m_itemOld
= htOldItem
;
2790 (void)HandleTreeEvent(changedEvent
);
2793 else if ( wParam
& MK_SHIFT
)
2795 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2797 m_htClickedItem
.Unset();
2802 bool willChange
= true;
2804 if ( !(wParam
& MK_CONTROL
) )
2806 srFlags
|= SR_UNSELECT_OTHERS
;
2809 if ( !m_htSelStart
)
2811 // take the focused item
2812 m_htSelStart
= htOldItem
;
2816 willChange
= SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2817 htItem
, srFlags
| SR_SIMULATE
);
2822 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
2824 changingEvent
.m_itemOld
= htOldItem
;
2826 if ( IsTreeEventAllowed(changingEvent
) )
2828 // this selects all items between the starting one
2832 SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2837 DoSelectItem(wxTreeItemId(htItem
));
2840 SetFocusedItem(wxTreeItemId(htItem
));
2842 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
2844 changedEvent
.m_itemOld
= htOldItem
;
2845 (void)HandleTreeEvent(changedEvent
);
2849 else // normal click
2851 // avoid doing anything if we click on the only
2852 // currently selected item
2854 wxArrayTreeItemIds selections
;
2855 size_t count
= GetSelections(selections
);
2859 HITEM(selections
[0]) != htItem
)
2861 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2863 m_htClickedItem
.Unset();
2867 // clear the previously selected items, if the user
2868 // clicked outside of the present selection, otherwise,
2869 // perform the deselection on mouse-up, this allows
2870 // multiple drag and drop to work.
2871 if ( !IsItemSelected(GetHwnd(), htItem
))
2873 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
2875 changingEvent
.m_itemOld
= htOldItem
;
2877 if ( IsTreeEventAllowed(changingEvent
) )
2880 DoSelectItem(wxTreeItemId(htItem
));
2881 SetFocusedItem(wxTreeItemId(htItem
));
2883 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
2885 changedEvent
.m_itemOld
= htOldItem
;
2886 (void)HandleTreeEvent(changedEvent
);
2891 SetFocusedItem(wxTreeItemId(htItem
));
2892 m_mouseUpDeselect
= true;
2895 else // click on a single selected item
2897 // don't interfere with the default processing in
2898 // WM_MOUSEMOVE handler below as the default window
2899 // proc will start the drag itself if we let have
2901 m_htClickedItem
.Unset();
2903 // prevent in-place editing from starting if focus lost
2904 // since previous click
2908 DoSelectItem(wxTreeItemId(htItem
));
2909 SetFocusedItem(wxTreeItemId(htItem
));
2917 // reset on any click without Shift
2918 m_htSelStart
.Unset();
2921 m_focusLost
= false;
2923 // we consumed the event so we need to trigger state image
2927 if ( tvht
.flags
& TVHT_ONITEMSTATEICON
)
2929 m_triggerStateImageClick
= true;
2934 case WM_RBUTTONDOWN
:
2941 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) || !htItem
)
2946 // default handler removes the highlight from the currently
2947 // focused item when right mouse button is pressed on another
2948 // one but keeps the remaining items highlighted, which is
2949 // confusing, so override this default behaviour
2950 if ( !IsItemSelected(GetHwnd(), htItem
) )
2952 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
2954 changingEvent
.m_itemOld
= htOldItem
;
2956 if ( IsTreeEventAllowed(changingEvent
) )
2959 DoSelectItem(wxTreeItemId(htItem
));
2960 SetFocusedItem(wxTreeItemId(htItem
));
2962 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
2964 changedEvent
.m_itemOld
= htOldItem
;
2965 (void)HandleTreeEvent(changedEvent
);
2973 if ( m_htClickedItem
)
2975 int cx
= abs(m_ptClick
.x
- x
);
2976 int cy
= abs(m_ptClick
.y
- y
);
2978 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2979 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2984 tv
.hdr
.hwndFrom
= GetHwnd();
2985 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2986 tv
.hdr
.code
= TVN_BEGINDRAG
;
2988 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2992 wxZeroMemory(tviAux
);
2994 tviAux
.hItem
= HITEM(m_htClickedItem
);
2995 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2996 tviAux
.stateMask
= 0xffffffff;
2997 TreeView_GetItem(GetHwnd(), &tviAux
);
2999 tv
.itemNew
.state
= tviAux
.state
;
3000 tv
.itemNew
.lParam
= tviAux
.lParam
;
3005 // do it before SendMessage() call below to avoid
3006 // reentrancies here if there is another WM_MOUSEMOVE
3007 // in the queue already
3008 m_htClickedItem
.Unset();
3010 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
3011 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
3013 // don't pass it to the default window proc, it would
3014 // start dragging again
3018 #endif // __WXWINCE__
3023 m_dragImage
->Move(wxPoint(x
, y
));
3026 // highlight the item as target (hiding drag image is
3027 // necessary - otherwise the display will be corrupted)
3028 m_dragImage
->Hide();
3029 TreeView_SelectDropTarget(GetHwnd(), htItem
);
3030 m_dragImage
->Show();
3033 #endif // wxUSE_DRAGIMAGE
3039 // deselect other items if needed
3042 if ( m_mouseUpDeselect
)
3044 m_mouseUpDeselect
= false;
3046 wxTreeEvent
changingEvent(wxEVT_TREE_SEL_CHANGING
,
3048 changingEvent
.m_itemOld
= htOldItem
;
3050 if ( IsTreeEventAllowed(changingEvent
) )
3053 DoSelectItem(wxTreeItemId(htItem
));
3054 SetFocusedItem(wxTreeItemId(htItem
));
3056 wxTreeEvent
changedEvent(wxEVT_TREE_SEL_CHANGED
,
3058 changedEvent
.m_itemOld
= htOldItem
;
3059 (void)HandleTreeEvent(changedEvent
);
3064 m_htClickedItem
.Unset();
3066 if ( m_triggerStateImageClick
)
3068 if ( tvht
.flags
& TVHT_ONITEMSTATEICON
)
3070 wxTreeEvent
event(wxEVT_TREE_STATE_IMAGE_CLICK
,
3072 (void)HandleTreeEvent(event
);
3074 m_triggerStateImageClick
= false;
3079 if ( !m_dragStarted
&& MSWIsOnItem(tvht
.flags
) )
3091 m_dragImage
->EndDrag();
3092 wxDELETE(m_dragImage
);
3094 // generate the drag end event
3095 wxTreeEvent
event(wxEVT_TREE_END_DRAG
,
3097 event
.m_pointDrag
= wxPoint(x
, y
);
3098 (void)HandleTreeEvent(event
);
3100 // if we don't do it, the tree seems to think that 2 items
3101 // are selected simultaneously which is quite weird
3102 TreeView_SelectDropTarget(GetHwnd(), 0);
3104 #endif // wxUSE_DRAGIMAGE
3106 if ( isMultiple
&& nMsg
== WM_RBUTTONUP
)
3110 nmhdr
.hwndFrom
= GetHwnd();
3111 nmhdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
3112 nmhdr
.code
= NM_RCLICK
;
3113 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY
,
3114 nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
3118 m_dragStarted
= false;
3123 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) )
3127 // the tree control greys out the selected item when it loses focus
3128 // and paints it as selected again when it regains it, but it won't
3129 // do it for the other items itself - help it
3130 wxArrayTreeItemIds selections
;
3131 size_t count
= GetSelections(selections
);
3132 TVGetItemRectParam param
;
3134 for ( size_t n
= 0; n
< count
; n
++ )
3136 // TreeView_GetItemRect() will return false if item is not
3137 // visible, which may happen perfectly well
3138 if ( wxTreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
3141 ::InvalidateRect(GetHwnd(), ¶m
.rect
, FALSE
);
3146 if ( nMsg
== WM_KILLFOCUS
)
3151 else if ( (nMsg
== WM_KEYDOWN
|| nMsg
== WM_SYSKEYDOWN
) && isMultiple
)
3153 // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
3154 // notification but for the keys which can be used to change selection
3155 // we need to do it from here so as to not apply the default behaviour
3156 // if the events are handled by the user code
3169 if ( !HandleKeyDown(wParam
, lParam
) &&
3170 !MSWHandleTreeKeyDownEvent(wParam
, lParam
) )
3172 // use the key to update the selection if it was left
3174 MSWHandleSelectionKey(wParam
);
3177 // pretend that we did process it in any case as we already
3178 // generated an event for it
3181 //default: for all the other keys leave processed as false so that
3182 // the tree control generates a TVN_KEYDOWN for us
3186 else if ( nMsg
== WM_COMMAND
)
3188 // if we receive a EN_KILLFOCUS command from the in-place edit control
3189 // used for label editing, make sure to end editing
3192 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
3194 if ( cmd
== EN_KILLFOCUS
)
3196 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
3206 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
3212 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
3214 if ( nMsg
== WM_CHAR
)
3216 // don't let the control process Space and Return keys because it
3217 // doesn't do anything useful with them anyhow but always beeps
3218 // annoyingly when it receives them and there is no way to turn it off
3219 // simply if you just process TREEITEM_ACTIVATED event to which Space
3220 // and Enter presses are mapped in your code
3221 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
3225 else if ( nMsg
== WM_KEYDOWN
)
3227 if ( wParam
== VK_ESCAPE
)
3231 m_dragImage
->EndDrag();
3232 wxDELETE(m_dragImage
);
3234 // if we don't do it, the tree seems to think that 2 items
3235 // are selected simultaneously which is quite weird
3236 TreeView_SelectDropTarget(GetHwnd(), 0);
3240 #endif // wxUSE_DRAGIMAGE
3242 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
3245 // process WM_NOTIFY Windows message
3246 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
3248 wxTreeEvent
event(wxEVT_NULL
, this);
3249 wxEventType eventType
= wxEVT_NULL
;
3250 NMHDR
*hdr
= (NMHDR
*)lParam
;
3252 switch ( hdr
->code
)
3255 eventType
= wxEVT_TREE_BEGIN_DRAG
;
3258 case TVN_BEGINRDRAG
:
3260 if ( eventType
== wxEVT_NULL
)
3261 eventType
= wxEVT_TREE_BEGIN_RDRAG
;
3262 //else: left drag, already set above
3264 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3266 event
.m_item
= tv
->itemNew
.hItem
;
3267 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
3269 // don't allow dragging by default: the user code must
3270 // explicitly say that it wants to allow it to avoid breaking
3276 case TVN_BEGINLABELEDIT
:
3278 eventType
= wxEVT_TREE_BEGIN_LABEL_EDIT
;
3279 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3281 // although the user event handler may still veto it, it is
3282 // important to set it now so that calls to SetItemText() from
3283 // the event handler would change the text controls contents
3285 event
.m_item
= info
->item
.hItem
;
3286 event
.m_label
= info
->item
.pszText
;
3287 event
.m_editCancelled
= false;
3291 case TVN_DELETEITEM
:
3293 eventType
= wxEVT_TREE_DELETE_ITEM
;
3294 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3296 event
.m_item
= tv
->itemOld
.hItem
;
3300 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
3301 if ( it
!= m_attrs
.end() )
3310 case TVN_ENDLABELEDIT
:
3312 eventType
= wxEVT_TREE_END_LABEL_EDIT
;
3313 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3315 event
.m_item
= info
->item
.hItem
;
3316 event
.m_label
= info
->item
.pszText
;
3317 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
3322 // These *must* not be removed or TVN_GETINFOTIP will
3323 // not be processed each time the mouse is moved
3324 // and the tooltip will only ever update once.
3333 #ifdef TVN_GETINFOTIP
3334 case TVN_GETINFOTIP
:
3336 eventType
= wxEVT_TREE_ITEM_GETTOOLTIP
;
3337 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
3339 // Which item are we trying to get a tooltip for?
3340 event
.m_item
= info
->hItem
;
3344 #endif // TVN_GETINFOTIP
3345 #endif // !__WXWINCE__
3347 case TVN_GETDISPINFO
:
3348 eventType
= wxEVT_TREE_GET_INFO
;
3351 case TVN_SETDISPINFO
:
3353 if ( eventType
== wxEVT_NULL
)
3354 eventType
= wxEVT_TREE_SET_INFO
;
3355 //else: get, already set above
3357 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3359 event
.m_item
= info
->item
.hItem
;
3363 case TVN_ITEMEXPANDING
:
3364 case TVN_ITEMEXPANDED
:
3366 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3369 switch ( tv
->action
)
3372 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
3380 what
= IDX_COLLAPSE
;
3384 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
3387 eventType
= gs_expandEvents
[what
][how
];
3389 event
.m_item
= tv
->itemNew
.hItem
;
3395 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
3397 // fabricate the lParam and wParam parameters sufficiently
3398 // similar to the ones from a "real" WM_KEYDOWN so that
3399 // CreateKeyEvent() works correctly
3400 return MSWHandleTreeKeyDownEvent(
3401 info
->wVKey
, (wxIsAltDown() ? KF_ALTDOWN
: 0) << 16);
3405 // Vista's tree control has introduced some problems with our
3406 // multi-selection tree. When TreeView_SelectItem() is called,
3407 // the wrong items are deselected.
3409 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3410 // that can be used to regulate this incorrect behaviour. The
3411 // following messages will allow only the unlocked item's selection
3414 case TVN_ITEMCHANGINGA
:
3415 case TVN_ITEMCHANGINGW
:
3417 // we only need to handles these in multi-select trees
3418 if ( HasFlag(wxTR_MULTIPLE
) )
3420 // get info about the item about to be changed
3421 NMTVITEMCHANGE
* info
= (NMTVITEMCHANGE
*)lParam
;
3422 if (TreeItemUnlocker::IsLocked(info
->hItem
))
3424 // item's state is locked, don't allow the change
3425 // returning 1 will disallow the change
3431 // allow the state change
3435 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
3436 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3437 // we have to handle both messages:
3438 case TVN_SELCHANGEDA
:
3439 case TVN_SELCHANGEDW
:
3440 if ( !m_changingSelection
)
3442 eventType
= wxEVT_TREE_SEL_CHANGED
;
3446 case TVN_SELCHANGINGA
:
3447 case TVN_SELCHANGINGW
:
3448 if ( !m_changingSelection
)
3450 if ( eventType
== wxEVT_NULL
)
3451 eventType
= wxEVT_TREE_SEL_CHANGING
;
3452 //else: already set above
3454 if (hdr
->code
== TVN_SELCHANGINGW
||
3455 hdr
->code
== TVN_SELCHANGEDW
)
3457 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
3458 event
.m_item
= tv
->itemNew
.hItem
;
3459 event
.m_itemOld
= tv
->itemOld
.hItem
;
3463 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
3464 event
.m_item
= tv
->itemNew
.hItem
;
3465 event
.m_itemOld
= tv
->itemOld
.hItem
;
3469 // we receive this message from WM_LBUTTONDOWN handler inside
3470 // comctl32.dll and so before the click is passed to
3471 // DefWindowProc() which sets the focus to the window which was
3472 // clicked and this can lead to unexpected event sequences: for
3473 // example, we may get a "selection change" event from the tree
3474 // before getting a "kill focus" event for the text control which
3475 // had the focus previously, thus breaking user code doing input
3478 // to avoid such surprises, we force the generation of focus events
3479 // now, before we generate the selection change ones
3480 if ( !m_changingSelection
&& !m_isBeingDeleted
)
3484 // instead of explicitly checking for _WIN32_IE, check if the
3485 // required symbols are available in the headers
3486 #if defined(CDDS_PREPAINT)
3489 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
3490 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
3491 switch ( nmcd
.dwDrawStage
)
3494 // if we've got any items with non standard attributes,
3495 // notify us before painting each item
3496 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
3499 // windows in TreeCtrl use one-based index for item state images,
3500 // 0 indexed image is not being used, we're using zero-based index,
3501 // so we have to add temp image (of zero index) to state image list
3502 // before we draw any item, then after items are drawn we have to
3503 // delete it (in POSTPAINT notify)
3504 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3506 typedef BOOL (wxSTDCALL
*ImageList_Copy_t
)
3507 (HIMAGELIST
, int, HIMAGELIST
, int, UINT
);
3508 static ImageList_Copy_t s_pfnImageList_Copy
= NULL
;
3509 static bool loaded
= false;
3513 wxLoadedDLL
dllComCtl32(wxT("comctl32.dll"));
3514 if ( dllComCtl32
.IsLoaded() )
3516 wxDL_INIT_FUNC(s_pfn
, ImageList_Copy
, dllComCtl32
);
3521 if ( !s_pfnImageList_Copy
)
3523 // this code is broken with ImageList_Copy()
3524 // but I don't care enough about Win95 support
3525 // to write it now -- if anybody does, please
3527 wxFAIL_MSG("TODO: implement this for Win95");
3532 hImageList
= GetHimagelistOf(m_imageListState
);
3534 // add temporary image
3536 m_imageListState
->GetSize(0, width
, height
);
3538 HBITMAP hbmpTemp
= ::CreateBitmap(width
, height
, 1, 1, NULL
);
3539 int index
= ::ImageList_Add(hImageList
, hbmpTemp
, hbmpTemp
);
3540 ::DeleteObject(hbmpTemp
);
3544 // move images to right
3545 for ( int i
= index
; i
> 0; i
-- )
3547 (*s_pfnImageList_Copy
)(hImageList
, i
,
3552 // we must remove the image in POSTPAINT notify
3553 *result
|= CDRF_NOTIFYPOSTPAINT
;
3558 case CDDS_POSTPAINT
:
3559 // we are deleting temp image of 0 index, which was
3560 // added before items were drawn (in PREPAINT notify)
3561 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3562 m_imageListState
->Remove(0);
3565 case CDDS_ITEMPREPAINT
:
3567 wxMapTreeAttr::iterator
3568 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
3570 if ( it
== m_attrs
.end() )
3572 // nothing to do for this item
3573 *result
= CDRF_DODEFAULT
;
3577 wxTreeItemAttr
* const attr
= it
->second
;
3579 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
3580 TVIF_STATE
, TVIS_DROPHILITED
);
3582 const UINT tvItemState
= tvItem
.state
;
3584 // selection colours should override ours,
3585 // otherwise it is too confusing to the user
3586 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
3587 !(tvItemState
& TVIS_DROPHILITED
) )
3590 if ( attr
->HasBackgroundColour() )
3592 colBack
= attr
->GetBackgroundColour();
3593 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
3597 // but we still want to keep the special foreground
3598 // colour when we don't have focus (we can't keep
3599 // it when we do, it would usually be unreadable on
3600 // the almost inverted bg colour...)
3601 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
3602 FindFocus() != this ) &&
3603 !(tvItemState
& TVIS_DROPHILITED
) )
3606 if ( attr
->HasTextColour() )
3608 colText
= attr
->GetTextColour();
3609 lptvcd
->clrText
= wxColourToRGB(colText
);
3613 if ( attr
->HasFont() )
3615 HFONT hFont
= GetHfontOf(attr
->GetFont());
3617 ::SelectObject(nmcd
.hdc
, hFont
);
3619 *result
= CDRF_NEWFONT
;
3621 else // no specific font
3623 *result
= CDRF_DODEFAULT
;
3629 *result
= CDRF_DODEFAULT
;
3633 // we always process it
3635 #endif // have owner drawn support in headers
3639 DWORD pos
= GetMessagePos();
3641 point
.x
= LOWORD(pos
);
3642 point
.y
= HIWORD(pos
);
3643 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
3645 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), htFlags
);
3647 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3649 event
.m_item
= item
;
3650 eventType
= wxEVT_TREE_STATE_IMAGE_CLICK
;
3659 TV_HITTESTINFO tvhti
;
3660 wxGetCursorPosMSW(&tvhti
.pt
);
3661 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
3662 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
3664 if ( MSWIsOnItem(tvhti
.flags
) )
3666 event
.m_item
= tvhti
.hItem
;
3667 eventType
= (int)hdr
->code
== NM_DBLCLK
3668 ? wxEVT_TREE_ITEM_ACTIVATED
3669 : wxEVT_TREE_ITEM_RIGHT_CLICK
;
3671 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
3672 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
3681 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
3684 event
.SetEventType(eventType
);
3686 bool processed
= HandleTreeEvent(event
);
3689 switch ( hdr
->code
)
3692 // we translate NM_DBLCLK into ACTIVATED event and if the user
3693 // handled the activation of the item we shouldn't proceed with
3694 // also using the same double click for toggling the item expanded
3695 // state -- but OTOH do let the user to expand/collapse the item by
3696 // double clicking on it if the activation is not handled specially
3697 *result
= processed
;
3701 // prevent tree control from sending WM_CONTEXTMENU to our parent
3702 // (which it does if NM_RCLICK is not handled) because we want to
3703 // send it to the control itself
3707 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
3708 (WPARAM
)GetHwnd(), ::GetMessagePos());
3712 case TVN_BEGINRDRAG
:
3714 if ( event
.IsAllowed() )
3716 // normally this is impossible because the m_dragImage is
3717 // deleted once the drag operation is over
3718 wxASSERT_MSG( !m_dragImage
, wxT("starting to drag once again?") );
3720 m_dragImage
= new wxDragImage(*this, event
.m_item
);
3721 m_dragImage
->BeginDrag(wxPoint(0,0), this);
3722 m_dragImage
->Show();
3724 m_dragStarted
= true;
3726 #endif // wxUSE_DRAGIMAGE
3729 case TVN_DELETEITEM
:
3731 // NB: we might process this message using wxWidgets event
3732 // tables, but due to overhead of wxWin event system we
3733 // prefer to do it here ourself (otherwise deleting a tree
3734 // with many items is just too slow)
3735 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3737 wxTreeItemParam
*param
=
3738 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
3741 processed
= true; // Make sure we don't get called twice
3745 case TVN_BEGINLABELEDIT
:
3746 // return true to cancel label editing
3747 *result
= !event
.IsAllowed();
3749 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3750 if ( event
.IsAllowed() )
3752 HWND hText
= TreeView_GetEditControl(GetHwnd());
3755 // MBN: if m_textCtrl already has an HWND, it is a stale
3756 // pointer from a previous edit (because the user
3757 // didn't modify the label before dismissing the control,
3758 // and TVN_ENDLABELEDIT was not sent), so delete it
3759 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
3762 m_textCtrl
= new wxTextCtrl();
3763 m_textCtrl
->SetParent(this);
3764 m_textCtrl
->SetHWND((WXHWND
)hText
);
3765 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3767 // set wxTE_PROCESS_ENTER style for the text control to
3768 // force it to process the Enter presses itself, otherwise
3769 // they could be stolen from it by the dialog
3771 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3772 | wxTE_PROCESS_ENTER
);
3775 else // we had set m_idEdited before
3781 case TVN_ENDLABELEDIT
:
3782 // return true to set the label to the new string: note that we
3783 // also must pretend that we did process the message or it is going
3784 // to be passed to DefWindowProc() which will happily return false
3785 // cancelling the label change
3786 *result
= event
.IsAllowed();
3789 // ensure that we don't have the text ctrl which is going to be
3795 #ifdef TVN_GETINFOTIP
3796 case TVN_GETINFOTIP
:
3798 // If the user permitted a tooltip change, change it
3799 if (event
.IsAllowed())
3801 SetToolTip(event
.m_label
);
3808 case TVN_SELCHANGING
:
3809 case TVN_ITEMEXPANDING
:
3810 // return true to prevent the action from happening
3811 *result
= !event
.IsAllowed();
3814 case TVN_ITEMEXPANDED
:
3816 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3817 const wxTreeItemId
id(tv
->itemNew
.hItem
);
3819 if ( tv
->action
== TVE_COLLAPSE
)
3821 if ( wxApp::GetComCtl32Version() >= 600 )
3823 // for some reason the item selection rectangle depends
3824 // on whether it is expanded or collapsed (at least
3825 // with comctl32.dll v6): it is wider (by 3 pixels) in
3826 // the expanded state, so when the item collapses and
3827 // then is deselected the rightmost 3 pixels of the
3828 // previously drawn selection are left on the screen
3830 // it's not clear if it's a bug in comctl32.dll or in
3831 // our code (because it does not happen in Explorer but
3832 // OTOH we don't do anything which could result in this
3833 // AFAICS) but we do need to work around it to avoid
3840 // the item is also not refreshed properly after expansion when
3841 // it has an image depending on the expanded/collapsed state:
3842 // again, it's not clear if the bug is in comctl32.dll or our
3844 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3853 case TVN_GETDISPINFO
:
3854 // NB: so far the user can't set the image himself anyhow, so do it
3855 // anyway - but this may change later
3856 //if ( /* !processed && */ )
3858 wxTreeItemId item
= event
.m_item
;
3859 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3861 const wxTreeItemParam
* const param
= GetItemParam(item
);
3865 if ( info
->item
.mask
& TVIF_IMAGE
)
3870 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3871 : wxTreeItemIcon_Normal
3874 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3876 info
->item
.iSelectedImage
=
3879 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3880 : wxTreeItemIcon_Selected
3887 // for the other messages the return value is ignored and there is
3888 // nothing special to do
3893 // ----------------------------------------------------------------------------
3895 // ----------------------------------------------------------------------------
3897 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3898 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3900 int wxTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
3902 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
3904 // receive the desired information
3905 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3908 // state images are one-based
3909 return STATEIMAGEMASKTOINDEX(tvItem
.state
) - 1;
3912 void wxTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
3914 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
3916 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3918 // state images are one-based
3919 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3920 tvItem
.state
= INDEXTOSTATEIMAGEMASK(state
+ 1);
3925 // ----------------------------------------------------------------------------
3927 // ----------------------------------------------------------------------------
3929 // Using WM_SETREDRAW with the native control is a bad idea as it's broken in
3930 // some Windows versions (see http://support.microsoft.com/kb/130611) and
3931 // doesn't seem to do anything in other ones (e.g. under Windows 7 the tree
3932 // control keeps updating its scrollbars while the items are added to it,
3933 // resulting in horrible flicker when adding even a couple of dozen items).
3934 // So we resize it to the smallest possible size instead of freezing -- this
3935 // still flickers, but actually not as badly as it would if we didn't do it.
3937 void wxTreeCtrl::DoFreeze()
3942 ::GetWindowRect(GetHwnd(), &rc
);
3943 m_thawnSize
= wxRectFromRECT(rc
).GetSize();
3945 ::SetWindowPos(GetHwnd(), 0, 0, 0, 1, 1,
3946 SWP_NOMOVE
| SWP_NOZORDER
| SWP_NOREDRAW
| SWP_NOACTIVATE
);
3950 void wxTreeCtrl::DoThaw()
3954 if ( m_thawnSize
!= wxDefaultSize
)
3956 ::SetWindowPos(GetHwnd(), 0, 0, 0, m_thawnSize
.x
, m_thawnSize
.y
,
3957 SWP_NOMOVE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
3962 // We also need to override DoSetSize() to ensure that m_thawnSize is reset if
3963 // the window is resized while being frozen -- in this case, we need to avoid
3964 // resizing it back to its original, pre-freeze, size when it's thawed.
3965 void wxTreeCtrl::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
3967 m_thawnSize
= wxDefaultSize
;
3969 wxTreeCtrlBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
3972 #endif // wxUSE_TREECTRL