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
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/msw/missing.h"
34 #include "wx/dynarray.h"
37 #include "wx/settings.h"
40 #include "wx/dynlib.h"
41 #include "wx/msw/private.h"
43 #include "wx/imaglist.h"
44 #include "wx/msw/dragimag.h"
45 #include "wx/msw/uxtheme.h"
47 // macros to hide the cast ugliness
48 // --------------------------------
50 // get HTREEITEM from wxTreeItemId
51 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
54 // older SDKs are missing these
55 #ifndef TVN_ITEMCHANGINGA
57 #define TVN_ITEMCHANGINGA (TVN_FIRST-16)
58 #define TVN_ITEMCHANGINGW (TVN_FIRST-17)
60 typedef struct tagNMTVITEMCHANGE
73 // this helper class is used on vista systems for preventing unwanted
74 // item state changes in the vista tree control. It is only effective in
75 // multi-select mode on vista systems.
77 // The vista tree control includes some new code that originally broke the
78 // multi-selection tree, causing seemingly spurious item selection state changes
79 // during Shift or Ctrl-click item selection. (To witness the original broken
80 // behaviour, simply make IsLocked() below always return false). This problem was
81 // solved by using the following class to 'unlock' an item's selection state.
83 class TreeItemUnlocker
86 // unlock a single item
87 TreeItemUnlocker(HTREEITEM item
) { ms_unlockedItem
= item
; }
89 // unlock all items, don't use unless absolutely necessary
90 TreeItemUnlocker() { ms_unlockedItem
= (HTREEITEM
)-1; }
92 // lock everything back
93 ~TreeItemUnlocker() { ms_unlockedItem
= NULL
; }
96 // check if the item state is currently locked
97 static bool IsLocked(HTREEITEM item
)
98 { return ms_unlockedItem
!= (HTREEITEM
)-1 && item
!= ms_unlockedItem
; }
101 static HTREEITEM ms_unlockedItem
;
104 HTREEITEM
TreeItemUnlocker::ms_unlockedItem
= NULL
;
106 // another helper class: set the variable to true during its lifetime and reset
107 // it to false when it is destroyed
109 // it is currently always used with wxTreeCtrl::m_changingSelection
113 TempSetter(bool& var
) : m_var(var
)
115 wxASSERT_MSG( !m_var
, "variable shouldn't be already set" );
127 wxDECLARE_NO_COPY_CLASS(TempSetter
);
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
137 // Work around a problem with TreeView_GetItemRect() when using MinGW/Cygwin:
138 // it results in warnings about breaking strict aliasing rules because HITEM is
139 // passed via a RECT pointer, so use a union to avoid them and define our own
140 // version of the standard macro using it.
141 union TVGetItemRectParam
148 wxTreeView_GetItemRect(HWND hwnd
,
150 TVGetItemRectParam
& param
,
154 return ::SendMessage(hwnd
, TVM_GETITEMRECT
, fItemRect
,
155 (LPARAM
)¶m
) == TRUE
;
158 } // anonymous namespace
160 // wrappers for TreeView_GetItem/TreeView_SetItem
161 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
164 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
165 tvi
.stateMask
= TVIS_SELECTED
;
168 TreeItemUnlocker
unlocker(hItem
);
170 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
172 wxLogLastError(wxT("TreeView_GetItem"));
175 return (tvi
.state
& TVIS_SELECTED
) != 0;
178 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
181 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
182 tvi
.stateMask
= TVIS_SELECTED
;
183 tvi
.state
= select
? TVIS_SELECTED
: 0;
186 TreeItemUnlocker
unlocker(hItem
);
188 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
190 wxLogLastError(wxT("TreeView_SetItem"));
197 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
199 SelectItem(hwndTV
, htItem
, false);
202 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
204 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
207 // helper function which selects all items in a range and, optionally,
208 // deselects all the other ones
210 // returns true if the selection changed at all or false if nothing changed
212 // flags for SelectRange()
215 SR_SIMULATE
= 1, // don't do anything, just return true or false
216 SR_UNSELECT_OTHERS
= 2 // deselect the items not in range
219 static bool SelectRange(HWND hwndTV
,
224 // find the first (or last) item and select it
225 bool changed
= false;
227 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
229 while ( htItem
&& cont
)
231 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
233 if ( !IsItemSelected(hwndTV
, htItem
) )
235 if ( !(flags
& SR_SIMULATE
) )
237 SelectItem(hwndTV
, htItem
);
245 else // not first or last
247 if ( flags
& SR_UNSELECT_OTHERS
)
249 if ( IsItemSelected(hwndTV
, htItem
) )
251 if ( !(flags
& SR_SIMULATE
) )
252 UnselectItem(hwndTV
, htItem
);
259 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
262 // select the items in range
263 cont
= htFirst
!= htLast
;
264 while ( htItem
&& cont
)
266 if ( !IsItemSelected(hwndTV
, htItem
) )
268 if ( !(flags
& SR_SIMULATE
) )
270 SelectItem(hwndTV
, htItem
);
276 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
278 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
281 // optionally deselect the rest
282 if ( flags
& SR_UNSELECT_OTHERS
)
286 if ( IsItemSelected(hwndTV
, htItem
) )
288 if ( !(flags
& SR_SIMULATE
) )
290 UnselectItem(hwndTV
, htItem
);
296 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
300 // seems to be necessary - otherwise the just selected items don't always
301 // appear as selected
302 if ( !(flags
& SR_SIMULATE
) )
304 UpdateWindow(hwndTV
);
310 // helper function which tricks the standard control into changing the focused
311 // item without changing anything else (if someone knows why Microsoft doesn't
312 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
314 // returns true if the focus was changed, false if the given item was already
316 static bool SetFocus(HWND hwndTV
, HTREEITEM htItem
)
319 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
321 if ( htItem
== htFocus
)
326 // remember the selection state of the item
327 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
329 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
331 // prevent the tree from unselecting the old focus which it
332 // would do by default (TreeView_SelectItem unselects the
334 TreeView_SelectItem(hwndTV
, 0);
335 SelectItem(hwndTV
, htFocus
);
338 TreeView_SelectItem(hwndTV
, htItem
);
342 // need to clear the selection which TreeView_SelectItem() gave
344 UnselectItem(hwndTV
, htItem
);
346 //else: was selected, still selected - ok
350 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
352 // just clear the focus
353 TreeView_SelectItem(hwndTV
, 0);
355 if ( wasFocusSelected
)
357 // restore the selection state
358 SelectItem(hwndTV
, htFocus
);
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 // a convenient wrapper around TV_ITEM struct which adds a ctor
371 #pragma warning( disable : 4097 ) // inheriting from typedef
374 struct wxTreeViewItem
: public TV_ITEM
376 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
377 UINT mask_
, // fields which are valid
378 UINT stateMask_
= 0) // for TVIF_STATE only
382 // hItem member is always valid
383 mask
= mask_
| TVIF_HANDLE
;
384 stateMask
= stateMask_
;
389 // ----------------------------------------------------------------------------
390 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
392 // We need this for a couple of reasons:
394 // 1) This class is needed for support of different images: the Win32 common
395 // control natively supports only 2 images (the normal one and another for the
396 // selected state). We wish to provide support for 2 more of them for folder
397 // items (i.e. those which have children): for expanded state and for expanded
398 // selected state. For this we use this structure to store the additional items
401 // 2) This class is also needed to hold the HITEM so that we can sort
402 // it correctly in the MSW sort callback.
404 // In addition it makes other workarounds such as this easier and helps
405 // simplify the code.
406 // ----------------------------------------------------------------------------
408 class wxTreeItemParam
415 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
421 // dtor deletes the associated data as well
422 virtual ~wxTreeItemParam() { delete m_data
; }
425 // get the real data associated with the item
426 wxTreeItemData
*GetData() const { return m_data
; }
428 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
430 // do we have such image?
431 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
432 // get image, falling back to the other images if this one is not
434 int GetImage(wxTreeItemIcon which
) const
436 int image
= m_images
[which
];
441 case wxTreeItemIcon_SelectedExpanded
:
442 // We consider that expanded icon is more important than
443 // selected so test for it first.
444 image
= m_images
[wxTreeItemIcon_Expanded
];
446 image
= m_images
[wxTreeItemIcon_Selected
];
451 case wxTreeItemIcon_Selected
:
452 case wxTreeItemIcon_Expanded
:
453 image
= m_images
[wxTreeItemIcon_Normal
];
456 case wxTreeItemIcon_Normal
:
461 wxFAIL_MSG( wxT("unsupported wxTreeItemIcon value") );
467 // change the given image
468 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
471 const wxTreeItemId
& GetItem() const { return m_item
; }
473 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
476 // all the images associated with the item
477 int m_images
[wxTreeItemIcon_Max
];
479 // item for sort callbacks
482 // the real client data
483 wxTreeItemData
*m_data
;
485 wxDECLARE_NO_COPY_CLASS(wxTreeItemParam
);
488 // wxVirutalNode is used in place of a single root when 'hidden' root is
490 class wxVirtualNode
: public wxTreeViewItem
493 wxVirtualNode(wxTreeItemParam
*param
)
494 : wxTreeViewItem(TVI_ROOT
, 0)
504 wxTreeItemParam
*GetParam() const { return m_param
; }
505 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
508 wxTreeItemParam
*m_param
;
510 wxDECLARE_NO_COPY_CLASS(wxVirtualNode
);
514 #pragma warning( default : 4097 )
517 // a macro to get the virtual root, returns NULL if none
518 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
520 // returns true if the item is the virtual root
521 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
523 // a class which encapsulates the tree traversal logic: it vists all (unless
524 // OnVisit() returns false) items under the given one
525 class wxTreeTraversal
528 wxTreeTraversal(const wxTreeCtrl
*tree
)
533 // give it a virtual dtor: not really needed as the class is never used
534 // polymorphically and not even allocated on heap at all, but this is safer
535 // (in case it ever is) and silences the compiler warnings for now
536 virtual ~wxTreeTraversal() { }
538 // do traverse the tree: visit all items (recursively by default) under the
539 // given one; return true if all items were traversed or false if the
540 // traversal was aborted because OnVisit returned false
541 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
543 // override this function to do whatever is needed for each item, return
544 // false to stop traversing
545 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
548 const wxTreeCtrl
*GetTree() const { return m_tree
; }
551 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
553 const wxTreeCtrl
*m_tree
;
555 wxDECLARE_NO_COPY_CLASS(wxTreeTraversal
);
558 // internal class for getting the selected items
559 class TraverseSelections
: public wxTreeTraversal
562 TraverseSelections(const wxTreeCtrl
*tree
,
563 wxArrayTreeItemIds
& selections
)
564 : wxTreeTraversal(tree
), m_selections(selections
)
566 m_selections
.Empty();
568 if (tree
->GetCount() > 0)
569 DoTraverse(tree
->GetRootItem());
572 virtual bool OnVisit(const wxTreeItemId
& item
)
574 const wxTreeCtrl
* const tree
= GetTree();
576 // can't visit a virtual node.
577 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
582 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
584 m_selections
.Add(item
);
590 size_t GetCount() const { return m_selections
.GetCount(); }
593 wxArrayTreeItemIds
& m_selections
;
595 wxDECLARE_NO_COPY_CLASS(TraverseSelections
);
598 // internal class for counting tree items
599 class TraverseCounter
: public wxTreeTraversal
602 TraverseCounter(const wxTreeCtrl
*tree
,
603 const wxTreeItemId
& root
,
605 : wxTreeTraversal(tree
)
609 DoTraverse(root
, recursively
);
612 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
619 size_t GetCount() const { return m_count
; }
624 wxDECLARE_NO_COPY_CLASS(TraverseCounter
);
627 // ----------------------------------------------------------------------------
629 // ----------------------------------------------------------------------------
631 // ----------------------------------------------------------------------------
633 // ----------------------------------------------------------------------------
635 // indices in gs_expandEvents table below
650 // handy table for sending events - it has to be initialized during run-time
651 // now so can't be const any more
652 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
655 but logically it's a const table with the following entries:
658 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
659 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
663 // ============================================================================
665 // ============================================================================
667 // ----------------------------------------------------------------------------
669 // ----------------------------------------------------------------------------
671 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
673 if ( !OnVisit(root
) )
676 return Traverse(root
, recursively
);
679 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
681 wxTreeItemIdValue cookie
;
682 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
683 while ( child
.IsOk() )
685 // depth first traversal
686 if ( recursively
&& !Traverse(child
, true) )
689 if ( !OnVisit(child
) )
692 child
= m_tree
->GetNextChild(root
, cookie
);
698 // ----------------------------------------------------------------------------
699 // construction and destruction
700 // ----------------------------------------------------------------------------
702 void wxTreeCtrl::Init()
705 m_hasAnyAttr
= false;
709 m_pVirtualRoot
= NULL
;
710 m_dragStarted
= false;
712 m_changingSelection
= false;
713 m_triggerStateImageClick
= false;
714 m_mouseUpDeselect
= false;
716 // initialize the global array of events now as it can't be done statically
717 // with the wxEVT_XXX values being allocated during run-time only
718 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
719 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
720 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
721 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
724 bool wxTreeCtrl::Create(wxWindow
*parent
,
729 const wxValidator
& validator
,
730 const wxString
& name
)
734 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
735 style
|= wxBORDER_SUNKEN
;
737 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
741 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
742 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
744 if ( !(m_windowStyle
& wxTR_NO_LINES
) )
745 wstyle
|= TVS_HASLINES
;
746 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
747 wstyle
|= TVS_HASBUTTONS
;
749 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
750 wstyle
|= TVS_EDITLABELS
;
752 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
753 wstyle
|= TVS_LINESATROOT
;
755 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
757 if ( wxApp::GetComCtl32Version() >= 471 )
758 wstyle
|= TVS_FULLROWSELECT
;
761 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
762 // Need so that TVN_GETINFOTIP messages will be sent
763 wstyle
|= TVS_INFOTIP
;
766 // Create the tree control.
767 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
770 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
771 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
773 wxSetCCUnicodeFormat(GetHwnd());
775 if ( m_windowStyle
& wxTR_TWIST_BUTTONS
)
777 // Under Vista and later Explorer uses rotating ("twist") buttons
778 // instead of the default "+/-" ones so apply its theme to the tree
779 // control to implement this style.
780 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
782 if ( wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive() )
784 theme
->SetWindowTheme(GetHwnd(), L
"EXPLORER", NULL
);
792 wxTreeCtrl::~wxTreeCtrl()
794 // delete any attributes
797 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
799 // prevent TVN_DELETEITEM handler from deleting the attributes again!
800 m_hasAnyAttr
= false;
805 // delete user data to prevent memory leaks
806 // also deletes hidden root node storage.
810 // ----------------------------------------------------------------------------
812 // ----------------------------------------------------------------------------
814 /* static */ wxVisualAttributes
815 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
817 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
819 // common controls have their own default font
820 attrs
.font
= wxGetCCDefaultFont();
826 // simple wrappers which add error checking in debug mode
828 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
830 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
831 wxT("can't retrieve virtual root item") );
833 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
835 wxLogLastError(wxT("TreeView_GetItem"));
843 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
845 TreeItemUnlocker
unlocker(tvItem
->hItem
);
847 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
849 wxLogLastError(wxT("TreeView_SetItem"));
853 unsigned int wxTreeCtrl::GetCount() const
855 return (unsigned int)TreeView_GetCount(GetHwnd());
858 unsigned int wxTreeCtrl::GetIndent() const
860 return TreeView_GetIndent(GetHwnd());
863 void wxTreeCtrl::SetIndent(unsigned int indent
)
865 TreeView_SetIndent(GetHwnd(), indent
);
868 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
871 (void) TreeView_SetImageList(GetHwnd(),
872 imageList
? imageList
->GetHIMAGELIST() : 0,
876 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
878 if (m_ownsImageListNormal
)
879 delete m_imageListNormal
;
881 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
882 m_ownsImageListNormal
= false;
885 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
887 if (m_ownsImageListState
) delete m_imageListState
;
888 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
889 m_ownsImageListState
= false;
892 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
893 bool recursively
) const
895 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
897 TraverseCounter
counter(this, item
, recursively
);
898 return counter
.GetCount() - 1;
901 // ----------------------------------------------------------------------------
903 // ----------------------------------------------------------------------------
905 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
907 if ( !wxWindowBase::SetBackgroundColour(colour
) )
910 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
915 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
917 if ( !wxWindowBase::SetForegroundColour(colour
) )
920 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
925 // ----------------------------------------------------------------------------
927 // ----------------------------------------------------------------------------
929 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
931 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
934 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
936 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
938 wxChar buf
[512]; // the size is arbitrary...
940 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
941 tvItem
.pszText
= buf
;
942 tvItem
.cchTextMax
= WXSIZEOF(buf
);
943 if ( !DoGetItem(&tvItem
) )
945 // don't return some garbage which was on stack, but an empty string
949 return wxString(buf
);
952 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
954 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
956 if ( IS_VIRTUAL_ROOT(item
) )
959 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
960 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
963 // when setting the text of the item being edited, the text control should
964 // be updated to reflect the new text as well, otherwise calling
965 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
967 // don't use GetEditControl() here because m_textCtrl is not set yet
968 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
971 if ( item
== m_idEdited
)
973 ::SetWindowText(hwndEdit
, text
.wx_str());
978 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
979 wxTreeItemIcon which
) const
981 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
983 if ( IsHiddenRoot(item
) )
985 // no images for hidden root item
989 wxTreeItemParam
*param
= GetItemParam(item
);
991 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
994 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
995 wxTreeItemIcon which
)
997 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
998 wxCHECK_RET( which
>= 0 &&
999 which
< wxTreeItemIcon_Max
,
1000 wxT("invalid image index"));
1003 if ( IsHiddenRoot(item
) )
1005 // no images for hidden root item
1009 wxTreeItemParam
*data
= GetItemParam(item
);
1013 data
->SetImage(image
, which
);
1018 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
1020 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1022 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1024 // hidden root may still have data.
1025 if ( IS_VIRTUAL_ROOT(item
) )
1027 return GET_VIRTUAL_ROOT()->GetParam();
1031 if ( !DoGetItem(&tvItem
) )
1036 return (wxTreeItemParam
*)tvItem
.lParam
;
1039 bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent
& event
) const
1041 if ( event
.m_item
.IsOk() )
1043 event
.SetClientObject(GetItemData(event
.m_item
));
1046 return HandleWindowEvent(event
);
1049 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1051 wxTreeItemParam
*data
= GetItemParam(item
);
1053 return data
? data
->GetData() : NULL
;
1056 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1058 // first, associate this piece of data with this item
1064 wxTreeItemParam
*param
= GetItemParam(item
);
1066 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1068 param
->SetData(data
);
1071 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1073 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1075 if ( IS_VIRTUAL_ROOT(item
) )
1078 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1079 tvItem
.cChildren
= (int)has
;
1083 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1085 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1087 if ( IS_VIRTUAL_ROOT(item
) )
1090 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1091 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1095 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1097 if ( IS_VIRTUAL_ROOT(item
) )
1100 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1101 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1105 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1107 if ( IS_VIRTUAL_ROOT(item
) )
1111 if ( GetBoundingRect(item
, rect
) )
1117 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1119 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1121 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1122 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1125 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1127 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1129 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1130 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1133 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1135 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1137 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1138 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1141 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1142 const wxColour
& col
)
1144 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1146 wxTreeItemAttr
*attr
;
1147 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1148 if ( it
== m_attrs
.end() )
1150 m_hasAnyAttr
= true;
1152 m_attrs
[item
.m_pItem
] =
1153 attr
= new wxTreeItemAttr
;
1160 attr
->SetTextColour(col
);
1165 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1166 const wxColour
& col
)
1168 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1170 wxTreeItemAttr
*attr
;
1171 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1172 if ( it
== m_attrs
.end() )
1174 m_hasAnyAttr
= true;
1176 m_attrs
[item
.m_pItem
] =
1177 attr
= new wxTreeItemAttr
;
1179 else // already in the hash
1184 attr
->SetBackgroundColour(col
);
1189 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1191 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1193 wxTreeItemAttr
*attr
;
1194 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1195 if ( it
== m_attrs
.end() )
1197 m_hasAnyAttr
= true;
1199 m_attrs
[item
.m_pItem
] =
1200 attr
= new wxTreeItemAttr
;
1202 else // already in the hash
1207 attr
->SetFont(font
);
1209 // Reset the item's text to ensure that the bounding rect will be adjusted
1210 // for the new font.
1211 SetItemText(item
, GetItemText(item
));
1216 // ----------------------------------------------------------------------------
1218 // ----------------------------------------------------------------------------
1220 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1222 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1224 if ( item
== wxTreeItemId(TVI_ROOT
) )
1226 // virtual (hidden) root is never visible
1230 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1231 TVGetItemRectParam param
;
1233 // true means to get rect for just the text, not the whole line
1234 if ( !wxTreeView_GetItemRect(GetHwnd(), HITEM(item
), param
, TRUE
) )
1236 // if TVM_GETITEMRECT returned false, then the item is definitely not
1237 // visible (because its parent is not expanded)
1241 // however if it returned true, the item might still be outside the
1242 // currently visible part of the tree, test for it (notice that partly
1243 // visible means visible here)
1244 return param
.rect
.bottom
> 0 && param
.rect
.top
< GetClientSize().y
;
1247 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1249 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1251 if ( IS_VIRTUAL_ROOT(item
) )
1253 wxTreeItemIdValue cookie
;
1254 return GetFirstChild(item
, cookie
).IsOk();
1257 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1260 return tvItem
.cChildren
!= 0;
1263 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1265 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1267 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1270 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1273 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1275 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1277 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1280 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1283 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1285 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1287 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1290 return (tvItem
.state
& TVIS_BOLD
) != 0;
1293 // ----------------------------------------------------------------------------
1295 // ----------------------------------------------------------------------------
1297 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1299 // Root may be real (visible) or virtual (hidden).
1300 if ( GET_VIRTUAL_ROOT() )
1303 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1306 wxTreeItemId
wxTreeCtrl::GetSelection() const
1308 wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE
), wxTreeItemId(),
1309 wxT("this only works with single selection controls") );
1311 return GetFocusedItem();
1314 wxTreeItemId
wxTreeCtrl::GetFocusedItem() const
1316 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1319 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1321 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1325 if ( IS_VIRTUAL_ROOT(item
) )
1327 // no parent for the virtual root
1332 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1333 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1335 // the top level items should have the virtual root as their parent
1340 return wxTreeItemId(hItem
);
1343 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1344 wxTreeItemIdValue
& cookie
) const
1346 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1348 // remember the last child returned in 'cookie'
1349 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1351 return wxTreeItemId(cookie
);
1354 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1355 wxTreeItemIdValue
& cookie
) const
1357 wxTreeItemId
fromCookie(cookie
);
1359 HTREEITEM hitem
= HITEM(fromCookie
);
1361 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1363 wxTreeItemId
item(hitem
);
1365 cookie
= item
.m_pItem
;
1370 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1372 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1374 // can this be done more efficiently?
1375 wxTreeItemIdValue cookie
;
1377 wxTreeItemId childLast
,
1378 child
= GetFirstChild(item
, cookie
);
1379 while ( child
.IsOk() )
1382 child
= GetNextChild(item
, cookie
);
1388 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1390 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1391 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1394 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1396 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1397 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1400 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1402 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1405 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1407 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1408 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1410 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1411 if ( next
.IsOk() && !IsVisible(next
) )
1413 // Win32 considers that any non-collapsed item is visible while we want
1414 // to return only really visible items
1421 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1423 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1424 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1426 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1427 if ( prev
.IsOk() && !IsVisible(prev
) )
1429 // just as above, Win32 function will happily return the previous item
1430 // in the tree for the first visible item too
1437 // ----------------------------------------------------------------------------
1438 // multiple selections emulation
1439 // ----------------------------------------------------------------------------
1441 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1443 TraverseSelections
selector(this, selections
);
1445 return selector
.GetCount();
1448 // ----------------------------------------------------------------------------
1450 // ----------------------------------------------------------------------------
1452 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1453 const wxTreeItemId
& hInsertAfter
,
1454 const wxString
& text
,
1455 int image
, int selectedImage
,
1456 wxTreeItemData
*data
)
1458 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1460 wxT("can't have more than one root in the tree") );
1462 TV_INSERTSTRUCT tvIns
;
1463 tvIns
.hParent
= HITEM(parent
);
1464 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1466 // this is how we insert the item as the first child: supply a NULL
1468 if ( !tvIns
.hInsertAfter
)
1470 tvIns
.hInsertAfter
= TVI_FIRST
;
1474 if ( !text
.empty() )
1477 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1481 tvIns
.item
.pszText
= NULL
;
1482 tvIns
.item
.cchTextMax
= 0;
1485 // create the param which will store the other item parameters
1486 wxTreeItemParam
*param
= new wxTreeItemParam
;
1488 // we return the images on demand as they depend on whether the item is
1489 // expanded or collapsed too in our case
1490 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1491 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1492 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1494 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1495 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1498 tvIns
.item
.lParam
= (LPARAM
)param
;
1499 tvIns
.item
.mask
= mask
;
1501 // don't use the hack below for the children of hidden root: this results
1502 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1503 const bool firstChild
= !IsHiddenRoot(parent
) &&
1504 !TreeView_GetChild(GetHwnd(), HITEM(parent
));
1506 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1509 wxLogLastError(wxT("TreeView_InsertItem"));
1512 // apparently some Windows versions (2000 and XP are reported to do this)
1513 // sometimes don't refresh the tree after adding the first child and so we
1514 // need this to make the "[+]" appear
1517 TVGetItemRectParam param
;
1519 wxTreeView_GetItemRect(GetHwnd(), HITEM(parent
), param
, FALSE
);
1520 ::InvalidateRect(GetHwnd(), ¶m
.rect
, FALSE
);
1523 // associate the application tree item with Win32 tree item handle
1526 // setup wxTreeItemData
1529 param
->SetData(data
);
1533 return wxTreeItemId(id
);
1536 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1537 int image
, int selectedImage
,
1538 wxTreeItemData
*data
)
1540 if ( HasFlag(wxTR_HIDE_ROOT
) )
1542 wxASSERT_MSG( !m_pVirtualRoot
, wxT("tree can have only a single root") );
1544 // create a virtual root item, the parent for all the others
1545 wxTreeItemParam
*param
= new wxTreeItemParam
;
1546 param
->SetData(data
);
1548 m_pVirtualRoot
= new wxVirtualNode(param
);
1553 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1554 text
, image
, selectedImage
, data
);
1557 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1559 const wxString
& text
,
1560 int image
, int selectedImage
,
1561 wxTreeItemData
*data
)
1563 wxTreeItemId idPrev
;
1564 if ( index
== (size_t)-1 )
1566 // special value: append to the end
1569 else // find the item from index
1571 wxTreeItemIdValue cookie
;
1572 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1573 while ( index
!= 0 && idCur
.IsOk() )
1578 idCur
= GetNextChild(parent
, cookie
);
1581 // assert, not check: if the index is invalid, we will append the item
1583 wxASSERT_MSG( index
== 0, wxT("bad index in wxTreeCtrl::InsertItem") );
1586 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1589 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1591 // unlock tree selections on vista, without this the
1592 // tree ctrl will eventually crash after item deletion
1593 TreeItemUnlocker unlock_all
;
1595 if ( HasFlag(wxTR_MULTIPLE
) )
1597 bool selected
= IsSelected(item
);
1602 next
= TreeView_GetNextVisible(GetHwnd(), HITEM(item
));
1606 next
= TreeView_GetPrevVisible(GetHwnd(), HITEM(item
));
1611 TempSetter
set(m_changingSelection
);
1612 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1614 wxLogLastError(wxT("TreeView_DeleteItem"));
1624 if ( item
== m_htSelStart
)
1625 m_htSelStart
.Unset();
1627 if ( item
== m_htClickedItem
)
1628 m_htClickedItem
.Unset();
1632 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
1634 if ( IsTreeEventAllowed(changingEvent
) )
1636 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
1637 (void)HandleTreeEvent(changedEvent
);
1641 DoUnselectItem(next
);
1648 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1650 wxLogLastError(wxT("TreeView_DeleteItem"));
1655 // delete all children (but don't delete the item itself)
1656 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1658 // unlock tree selections on vista for the duration of this call
1659 TreeItemUnlocker unlock_all
;
1661 wxTreeItemIdValue cookie
;
1663 wxArrayTreeItemIds children
;
1664 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1665 while ( child
.IsOk() )
1667 children
.Add(child
);
1669 child
= GetNextChild(item
, cookie
);
1672 size_t nCount
= children
.Count();
1673 for ( size_t n
= 0; n
< nCount
; n
++ )
1675 Delete(children
[n
]);
1679 void wxTreeCtrl::DeleteAllItems()
1681 // unlock tree selections on vista for the duration of this call
1682 TreeItemUnlocker unlock_all
;
1684 // invalidate all the items we store as they're going to become invalid
1686 m_htClickedItem
= wxTreeItemId();
1688 // delete the "virtual" root item.
1689 if ( GET_VIRTUAL_ROOT() )
1691 delete GET_VIRTUAL_ROOT();
1692 m_pVirtualRoot
= NULL
;
1695 // and all the real items
1697 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1699 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1703 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1705 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1706 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1707 flag
== TVE_EXPAND
||
1709 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1711 // A hidden root can be neither expanded nor collapsed.
1712 wxCHECK_RET( !IsHiddenRoot(item
),
1713 wxT("Can't expand/collapse hidden root node!") );
1715 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1716 // emulate them. This behaviour has changed slightly with comctl32.dll
1717 // v 4.70 - now it does send them but only the first time. To maintain
1718 // compatible behaviour and also in order to not have surprises with the
1719 // future versions, don't rely on this and still do everything ourselves.
1720 // To avoid that the messages be sent twice when the item is expanded for
1721 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1723 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1727 if ( IsExpanded(item
) )
1729 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
,
1730 this, wxTreeItemId(item
));
1732 if ( !IsTreeEventAllowed(event
) )
1736 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) )
1738 if ( IsExpanded(item
) )
1741 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, this, item
);
1742 (void)HandleTreeEvent(event
);
1744 //else: change didn't took place, so do nothing at all
1747 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1749 DoExpand(item
, TVE_EXPAND
);
1752 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1754 DoExpand(item
, TVE_COLLAPSE
);
1757 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1759 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1762 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1764 DoExpand(item
, TVE_TOGGLE
);
1767 void wxTreeCtrl::Unselect()
1769 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE
),
1770 wxT("doesn't make sense, may be you want UnselectAll()?") );
1772 // the current focus
1773 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1780 if ( HasFlag(wxTR_MULTIPLE
) )
1782 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
1783 this, wxTreeItemId());
1784 changingEvent
.m_itemOld
= htFocus
;
1786 if ( IsTreeEventAllowed(changingEvent
) )
1790 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1791 this, wxTreeItemId());
1792 changedEvent
.m_itemOld
= htFocus
;
1793 (void)HandleTreeEvent(changedEvent
);
1802 void wxTreeCtrl::DoUnselectAll()
1804 wxArrayTreeItemIds selections
;
1805 size_t count
= GetSelections(selections
);
1807 for ( size_t n
= 0; n
< count
; n
++ )
1809 DoUnselectItem(selections
[n
]);
1812 m_htSelStart
.Unset();
1815 void wxTreeCtrl::UnselectAll()
1817 if ( HasFlag(wxTR_MULTIPLE
) )
1819 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1820 if ( !htFocus
) return;
1822 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1823 changingEvent
.m_itemOld
= htFocus
;
1825 if ( IsTreeEventAllowed(changingEvent
) )
1829 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1830 changedEvent
.m_itemOld
= htFocus
;
1831 (void)HandleTreeEvent(changedEvent
);
1840 void wxTreeCtrl::DoSelectChildren(const wxTreeItemId
& parent
)
1844 wxTreeItemIdValue cookie
;
1845 wxTreeItemId child
= GetFirstChild(parent
, cookie
);
1846 while ( child
.IsOk() )
1848 DoSelectItem(child
, true);
1849 child
= GetNextChild(child
, cookie
);
1853 void wxTreeCtrl::SelectChildren(const wxTreeItemId
& parent
)
1855 wxCHECK_RET( HasFlag(wxTR_MULTIPLE
),
1856 "this only works with multiple selection controls" );
1858 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1860 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1861 changingEvent
.m_itemOld
= htFocus
;
1863 if ( IsTreeEventAllowed(changingEvent
) )
1865 DoSelectChildren(parent
);
1867 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1868 changedEvent
.m_itemOld
= htFocus
;
1869 (void)HandleTreeEvent(changedEvent
);
1873 void wxTreeCtrl::DoSelectItem(const wxTreeItemId
& item
, bool select
)
1875 TempSetter
set(m_changingSelection
);
1877 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1880 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1882 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't select hidden root item") );
1884 if ( select
== IsSelected(item
) )
1886 // nothing to do, the item is already in the requested state
1890 if ( HasFlag(wxTR_MULTIPLE
) )
1892 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1894 if ( IsTreeEventAllowed(changingEvent
) )
1896 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1897 DoSelectItem(item
, select
);
1901 SetFocusedItem(item
);
1904 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1906 (void)HandleTreeEvent(changedEvent
);
1909 else // single selection
1911 wxTreeItemId itemOld
, itemNew
;
1914 itemOld
= GetSelection();
1917 else // deselecting the currently selected item
1920 // leave itemNew invalid
1923 // Recent versions of comctl32.dll send TVN_SELCHANG{ED,ING} events
1924 // when we call TreeView_SelectItem() but apparently some old ones did
1925 // not so send the events ourselves and ignore those generated by
1926 // TreeView_SelectItem() if m_changingSelection is set.
1928 changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, itemNew
);
1929 changingEvent
.SetOldItem(itemOld
);
1931 if ( IsTreeEventAllowed(changingEvent
) )
1933 TempSetter
set(m_changingSelection
);
1935 if ( !TreeView_SelectItem(GetHwnd(), HITEM(itemNew
)) )
1937 wxLogLastError(wxT("TreeView_SelectItem"));
1941 ::SetFocus(GetHwnd(), HITEM(item
));
1943 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1945 changedEvent
.SetOldItem(itemOld
);
1946 (void)HandleTreeEvent(changedEvent
);
1949 //else: program vetoed the change
1953 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1955 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't show hidden root item") );
1958 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1961 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1963 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1965 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1969 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1974 void wxTreeCtrl::DeleteTextCtrl()
1978 // the HWND corresponding to this control is deleted by the tree
1979 // control itself and we don't know when exactly this happens, so check
1980 // if the window still exists before calling UnsubclassWin()
1981 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1983 m_textCtrl
->SetHWND(0);
1986 m_textCtrl
->UnsubclassWin();
1987 m_textCtrl
->SetHWND(0);
1988 wxDELETE(m_textCtrl
);
1994 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1995 wxClassInfo
*textControlClass
)
1997 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
2002 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
2003 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
2005 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2009 wxDELETE(m_textCtrl
);
2013 // textctrl is subclassed in MSWOnNotify
2017 // End label editing, optionally cancelling the edit
2018 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
2020 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
2025 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2027 TV_HITTESTINFO hitTestInfo
;
2028 hitTestInfo
.pt
.x
= (int)point
.x
;
2029 hitTestInfo
.pt
.y
= (int)point
.y
;
2031 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2036 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2037 flags |= wxTREE_HITTEST_##flag
2039 TRANSLATE_FLAG(ABOVE
);
2040 TRANSLATE_FLAG(BELOW
);
2041 TRANSLATE_FLAG(NOWHERE
);
2042 TRANSLATE_FLAG(ONITEMBUTTON
);
2043 TRANSLATE_FLAG(ONITEMICON
);
2044 TRANSLATE_FLAG(ONITEMINDENT
);
2045 TRANSLATE_FLAG(ONITEMLABEL
);
2046 TRANSLATE_FLAG(ONITEMRIGHT
);
2047 TRANSLATE_FLAG(ONITEMSTATEICON
);
2048 TRANSLATE_FLAG(TOLEFT
);
2049 TRANSLATE_FLAG(TORIGHT
);
2051 #undef TRANSLATE_FLAG
2053 return wxTreeItemId(hitTestInfo
.hItem
);
2056 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2058 bool textOnly
) const
2060 // Virtual root items have no bounding rectangle
2061 if ( IS_VIRTUAL_ROOT(item
) )
2066 TVGetItemRectParam param
;
2068 if ( wxTreeView_GetItemRect(GetHwnd(), HITEM(item
), param
, textOnly
) )
2070 rect
= wxRect(wxPoint(param
.rect
.left
, param
.rect
.top
),
2071 wxPoint(param
.rect
.right
, param
.rect
.bottom
));
2077 // couldn't retrieve rect: for example, item isn't visible
2082 void wxTreeCtrl::ClearFocusedItem()
2084 TempSetter
set(m_changingSelection
);
2086 if ( !TreeView_SelectItem(GetHwnd(), 0) )
2088 wxLogLastError(wxT("TreeView_SelectItem"));
2092 void wxTreeCtrl::SetFocusedItem(const wxTreeItemId
& item
)
2094 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2096 TempSetter
set(m_changingSelection
);
2098 ::SetFocus(GetHwnd(), HITEM(item
));
2101 void wxTreeCtrl::DoUnselectItem(const wxTreeItemId
& item
)
2103 TempSetter
set(m_changingSelection
);
2105 ::UnselectItem(GetHwnd(), HITEM(item
));
2108 void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId
& item
)
2110 TempSetter
set(m_changingSelection
);
2112 ::ToggleItemSelection(GetHwnd(), HITEM(item
));
2115 // ----------------------------------------------------------------------------
2117 // ----------------------------------------------------------------------------
2119 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2120 // functions such as IsDataIndirect()
2121 class wxTreeSortHelper
2124 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2127 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
2129 return ((wxTreeItemParam
*)lParam
)->GetItem();
2133 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2137 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2138 wxT("sorting tree without data doesn't make sense") );
2140 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2142 return tree
->OnCompareItems(GetIdFromData(pItem1
),
2143 GetIdFromData(pItem2
));
2146 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2148 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2150 // rely on the fact that TreeView_SortChildren does the same thing as our
2151 // default behaviour, i.e. sorts items alphabetically and so call it
2152 // directly if we're not in derived class (much more efficient!)
2153 // RN: Note that if you find you're code doesn't sort as expected this
2154 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
2155 // combo for your derived wxTreeCtrl if will sort without
2157 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2159 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2164 tvSort
.hParent
= HITEM(item
);
2165 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2166 tvSort
.lParam
= (LPARAM
)this;
2167 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2171 // ----------------------------------------------------------------------------
2173 // ----------------------------------------------------------------------------
2175 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
2177 if ( msg
->message
== WM_KEYDOWN
)
2179 // Only eat VK_RETURN if not being used by the application in
2180 // conjunction with modifiers
2181 if ( (msg
->wParam
== VK_RETURN
) && !wxIsAnyModifierDown() )
2183 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2188 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
2191 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
2193 const int id
= (signed short)id_
;
2195 if ( cmd
== EN_UPDATE
)
2197 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2198 event
.SetEventObject( this );
2199 ProcessCommand(event
);
2201 else if ( cmd
== EN_KILLFOCUS
)
2203 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2204 event
.SetEventObject( this );
2205 ProcessCommand(event
);
2213 // command processed
2217 bool wxTreeCtrl::MSWIsOnItem(unsigned flags
) const
2219 unsigned mask
= TVHT_ONITEM
;
2220 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2221 mask
|= TVHT_ONITEMINDENT
| TVHT_ONITEMRIGHT
;
2223 return (flags
& mask
) != 0;
2226 bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey
)
2228 const bool bCtrl
= wxIsCtrlDown();
2229 const bool bShift
= wxIsShiftDown();
2230 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2239 if ( vkey
!= VK_RETURN
&& bCtrl
)
2241 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2243 changingEvent
.m_itemOld
= htSel
;
2245 if ( IsTreeEventAllowed(changingEvent
) )
2247 DoToggleItemSelection(wxTreeItemId(htSel
));
2249 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2251 changedEvent
.m_itemOld
= htSel
;
2252 (void)HandleTreeEvent(changedEvent
);
2257 wxArrayTreeItemIds selections
;
2258 size_t count
= GetSelections(selections
);
2260 if ( count
!= 1 || HITEM(selections
[0]) != htSel
)
2262 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2264 changingEvent
.m_itemOld
= htSel
;
2266 if ( IsTreeEventAllowed(changingEvent
) )
2269 DoSelectItem(wxTreeItemId(htSel
));
2271 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2273 changedEvent
.m_itemOld
= htSel
;
2274 (void)HandleTreeEvent(changedEvent
);
2282 if ( !bCtrl
&& !bShift
)
2284 wxArrayTreeItemIds selections
;
2289 next
= vkey
== VK_UP
2290 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2291 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2295 next
= GetRootItem();
2297 if ( IsHiddenRoot(next
) )
2298 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2306 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2308 changingEvent
.m_itemOld
= htSel
;
2310 if ( IsTreeEventAllowed(changingEvent
) )
2314 SetFocusedItem(next
);
2316 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2318 changedEvent
.m_itemOld
= htSel
;
2319 (void)HandleTreeEvent(changedEvent
);
2324 wxTreeItemId next
= vkey
== VK_UP
2325 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2326 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2333 if ( !m_htSelStart
)
2335 m_htSelStart
= htSel
;
2338 if ( bShift
&& SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2339 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2341 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2342 changingEvent
.m_itemOld
= htSel
;
2344 if ( IsTreeEventAllowed(changingEvent
) )
2346 SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2347 SR_UNSELECT_OTHERS
);
2349 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2350 changedEvent
.m_itemOld
= htSel
;
2351 (void)HandleTreeEvent(changedEvent
);
2355 SetFocusedItem(next
);
2360 if ( HasChildren(htSel
) && IsExpanded(htSel
) )
2366 wxTreeItemId next
= GetItemParent(htSel
);
2368 if ( next
.IsOk() && !IsHiddenRoot(next
) )
2370 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2372 changingEvent
.m_itemOld
= htSel
;
2374 if ( IsTreeEventAllowed(changingEvent
) )
2378 SetFocusedItem(next
);
2380 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2382 changedEvent
.m_itemOld
= htSel
;
2383 (void)HandleTreeEvent(changedEvent
);
2390 if ( !IsVisible(htSel
) )
2392 EnsureVisible(htSel
);
2395 if ( !HasChildren(htSel
) )
2398 if ( !IsExpanded(htSel
) )
2404 wxTreeItemId next
= TreeView_GetChild(GetHwnd(), htSel
);
2406 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2407 changingEvent
.m_itemOld
= htSel
;
2409 if ( IsTreeEventAllowed(changingEvent
) )
2413 SetFocusedItem(next
);
2415 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2416 changedEvent
.m_itemOld
= htSel
;
2417 (void)HandleTreeEvent(changedEvent
);
2425 wxTreeItemId next
= GetRootItem();
2427 if ( IsHiddenRoot(next
) )
2429 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2435 if ( vkey
== VK_END
)
2439 wxTreeItemId nextTemp
= TreeView_GetNextVisible(
2440 GetHwnd(), HITEM(next
));
2442 if ( !nextTemp
.IsOk() )
2449 if ( htSel
== HITEM(next
) )
2454 if ( !m_htSelStart
)
2456 m_htSelStart
= htSel
;
2459 if ( SelectRange(GetHwnd(),
2460 HITEM(m_htSelStart
), HITEM(next
),
2461 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2463 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2465 changingEvent
.m_itemOld
= htSel
;
2467 if ( IsTreeEventAllowed(changingEvent
) )
2469 SelectRange(GetHwnd(),
2470 HITEM(m_htSelStart
), HITEM(next
),
2471 SR_UNSELECT_OTHERS
);
2472 SetFocusedItem(next
);
2474 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2476 changedEvent
.m_itemOld
= htSel
;
2477 (void)HandleTreeEvent(changedEvent
);
2483 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2485 changingEvent
.m_itemOld
= htSel
;
2487 if ( IsTreeEventAllowed(changingEvent
) )
2491 SetFocusedItem(next
);
2493 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2495 changedEvent
.m_itemOld
= htSel
;
2496 (void)HandleTreeEvent(changedEvent
);
2506 wxTreeItemId firstVisible
= GetFirstVisibleItem();
2507 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2508 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2509 TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible
)) :
2510 TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible
));
2512 if ( !nextAdjacent
)
2517 wxTreeItemId nextStart
= firstVisible
;
2519 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2521 wxTreeItemId nextTemp
= (vkey
== VK_PRIOR
) ?
2522 TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart
)) :
2523 TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart
));
2525 if ( nextTemp
.IsOk() )
2527 nextStart
= nextTemp
;
2535 EnsureVisible(nextStart
);
2537 if ( vkey
== VK_NEXT
)
2539 wxTreeItemId nextEnd
= nextStart
;
2541 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2543 wxTreeItemId nextTemp
=
2544 TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd
));
2546 if ( nextTemp
.IsOk() )
2556 EnsureVisible(nextEnd
);
2561 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2562 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2563 TreeView_GetPrevVisible(GetHwnd(), htSel
) :
2564 TreeView_GetNextVisible(GetHwnd(), htSel
);
2566 if ( !nextAdjacent
)
2571 wxTreeItemId
next(htSel
);
2573 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2575 wxTreeItemId nextTemp
= vkey
== VK_PRIOR
?
2576 TreeView_GetPrevVisible(GetHwnd(), HITEM(next
)) :
2577 TreeView_GetNextVisible(GetHwnd(), HITEM(next
));
2579 if ( !nextTemp
.IsOk() )
2585 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2587 changingEvent
.m_itemOld
= htSel
;
2589 if ( IsTreeEventAllowed(changingEvent
) )
2592 m_htSelStart
.Unset();
2594 SetFocusedItem(next
);
2596 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2598 changedEvent
.m_itemOld
= htSel
;
2599 (void)HandleTreeEvent(changedEvent
);
2611 bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam
, WXLPARAM lParam
)
2613 wxTreeEvent
keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2614 keyEvent
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
, wParam
, lParam
);
2616 bool processed
= HandleTreeEvent(keyEvent
);
2618 // generate a separate event for Space/Return
2619 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
2620 ((wParam
== VK_SPACE
) || (wParam
== VK_RETURN
)) )
2622 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2625 wxTreeEvent
activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2627 (void)HandleTreeEvent(activatedEvent
);
2634 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2635 // only do it during dragging, minimize wxWin overhead (this is important for
2636 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2637 // instead of passing by wxWin events
2639 wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2641 bool processed
= false;
2643 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2645 if ( nMsg
== WM_CONTEXTMENU
)
2647 int x
= GET_X_LPARAM(lParam
),
2648 y
= GET_Y_LPARAM(lParam
);
2650 // the item for which the menu should be shown
2653 // the position where the menu should be shown in client coordinates
2654 // (so that it can be passed directly to PopupMenu())
2657 if ( x
== -1 || y
== -1 )
2659 // this means that the event was generated from keyboard (e.g. with
2660 // Shift-F10 or special Windows menu key)
2662 // use the Explorer standard of putting the menu at the left edge
2663 // of the text, in the vertical middle of the text
2664 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2667 // Use the bounding rectangle of only the text part
2669 GetBoundingRect(item
, rect
, true);
2670 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
2673 else // event from mouse, use mouse position
2675 pt
= ScreenToClient(wxPoint(x
, y
));
2677 TV_HITTESTINFO tvhti
;
2681 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2682 item
= wxTreeItemId(tvhti
.hItem
);
2688 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2690 event
.m_pointDrag
= pt
;
2692 if ( HandleTreeEvent(event
) )
2694 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2697 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2699 // we only process mouse messages here and these parameters have the
2700 // same meaning for all of them
2701 int x
= GET_X_LPARAM(lParam
),
2702 y
= GET_Y_LPARAM(lParam
);
2704 TV_HITTESTINFO tvht
;
2708 HTREEITEM htOldItem
= TreeView_GetSelection(GetHwnd());
2709 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2713 case WM_LBUTTONDOWN
:
2717 m_htClickedItem
.Unset();
2719 if ( !MSWIsOnItem(tvht
.flags
) )
2721 if ( tvht
.flags
& TVHT_ONITEMBUTTON
)
2723 // either it's going to be handled by user code or
2724 // we're going to use it ourselves to toggle the
2725 // branch, in either case don't pass it to the base
2726 // class which would generate another mouse click event
2727 // for it even though it's already handled here
2731 if ( !HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2733 if ( !IsExpanded(htItem
) )
2744 m_focusLost
= false;
2750 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2751 m_ptClick
= wxPoint(x
, y
);
2753 if ( wParam
& MK_CONTROL
)
2755 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2757 m_htClickedItem
.Unset();
2761 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2763 changingEvent
.m_itemOld
= htOldItem
;
2765 if ( IsTreeEventAllowed(changingEvent
) )
2767 // toggle selected state
2768 DoToggleItemSelection(wxTreeItemId(htItem
));
2770 SetFocusedItem(wxTreeItemId(htItem
));
2772 // reset on any click without Shift
2773 m_htSelStart
.Unset();
2775 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2777 changedEvent
.m_itemOld
= htOldItem
;
2778 (void)HandleTreeEvent(changedEvent
);
2781 else if ( wParam
& MK_SHIFT
)
2783 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2785 m_htClickedItem
.Unset();
2790 bool willChange
= true;
2792 if ( !(wParam
& MK_CONTROL
) )
2794 srFlags
|= SR_UNSELECT_OTHERS
;
2797 if ( !m_htSelStart
)
2799 // take the focused item
2800 m_htSelStart
= htOldItem
;
2804 willChange
= SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2805 htItem
, srFlags
| SR_SIMULATE
);
2810 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2812 changingEvent
.m_itemOld
= htOldItem
;
2814 if ( IsTreeEventAllowed(changingEvent
) )
2816 // this selects all items between the starting one
2820 SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2825 DoSelectItem(wxTreeItemId(htItem
));
2828 SetFocusedItem(wxTreeItemId(htItem
));
2830 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2832 changedEvent
.m_itemOld
= htOldItem
;
2833 (void)HandleTreeEvent(changedEvent
);
2837 else // normal click
2839 // avoid doing anything if we click on the only
2840 // currently selected item
2842 wxArrayTreeItemIds selections
;
2843 size_t count
= GetSelections(selections
);
2847 HITEM(selections
[0]) != htItem
)
2849 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2851 m_htClickedItem
.Unset();
2855 // clear the previously selected items, if the user
2856 // clicked outside of the present selection, otherwise,
2857 // perform the deselection on mouse-up, this allows
2858 // multiple drag and drop to work.
2859 if ( !IsItemSelected(GetHwnd(), htItem
))
2861 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2863 changingEvent
.m_itemOld
= htOldItem
;
2865 if ( IsTreeEventAllowed(changingEvent
) )
2868 DoSelectItem(wxTreeItemId(htItem
));
2869 SetFocusedItem(wxTreeItemId(htItem
));
2871 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2873 changedEvent
.m_itemOld
= htOldItem
;
2874 (void)HandleTreeEvent(changedEvent
);
2879 SetFocusedItem(wxTreeItemId(htItem
));
2880 m_mouseUpDeselect
= true;
2883 else // click on a single selected item
2885 // don't interfere with the default processing in
2886 // WM_MOUSEMOVE handler below as the default window
2887 // proc will start the drag itself if we let have
2889 m_htClickedItem
.Unset();
2891 // prevent in-place editing from starting if focus lost
2892 // since previous click
2896 DoSelectItem(wxTreeItemId(htItem
));
2897 SetFocusedItem(wxTreeItemId(htItem
));
2905 // reset on any click without Shift
2906 m_htSelStart
.Unset();
2909 m_focusLost
= false;
2911 // we consumed the event so we need to trigger state image
2916 wxTreeItemId item
= HitTest(wxPoint(x
, y
), htFlags
);
2918 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2920 m_triggerStateImageClick
= true;
2925 case WM_RBUTTONDOWN
:
2932 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) || !htItem
)
2937 // default handler removes the highlight from the currently
2938 // focused item when right mouse button is pressed on another
2939 // one but keeps the remaining items highlighted, which is
2940 // confusing, so override this default behaviour
2941 if ( !IsItemSelected(GetHwnd(), htItem
) )
2943 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2945 changingEvent
.m_itemOld
= htOldItem
;
2947 if ( IsTreeEventAllowed(changingEvent
) )
2950 DoSelectItem(wxTreeItemId(htItem
));
2951 SetFocusedItem(wxTreeItemId(htItem
));
2953 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2955 changedEvent
.m_itemOld
= htOldItem
;
2956 (void)HandleTreeEvent(changedEvent
);
2964 if ( m_htClickedItem
)
2966 int cx
= abs(m_ptClick
.x
- x
);
2967 int cy
= abs(m_ptClick
.y
- y
);
2969 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2970 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2975 tv
.hdr
.hwndFrom
= GetHwnd();
2976 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2977 tv
.hdr
.code
= TVN_BEGINDRAG
;
2979 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2983 wxZeroMemory(tviAux
);
2985 tviAux
.hItem
= HITEM(m_htClickedItem
);
2986 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2987 tviAux
.stateMask
= 0xffffffff;
2988 TreeView_GetItem(GetHwnd(), &tviAux
);
2990 tv
.itemNew
.state
= tviAux
.state
;
2991 tv
.itemNew
.lParam
= tviAux
.lParam
;
2996 // do it before SendMessage() call below to avoid
2997 // reentrancies here if there is another WM_MOUSEMOVE
2998 // in the queue already
2999 m_htClickedItem
.Unset();
3001 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
3002 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
3004 // don't pass it to the default window proc, it would
3005 // start dragging again
3009 #endif // __WXWINCE__
3014 m_dragImage
->Move(wxPoint(x
, y
));
3017 // highlight the item as target (hiding drag image is
3018 // necessary - otherwise the display will be corrupted)
3019 m_dragImage
->Hide();
3020 TreeView_SelectDropTarget(GetHwnd(), htItem
);
3021 m_dragImage
->Show();
3024 #endif // wxUSE_DRAGIMAGE
3030 // deselect other items if needed
3033 if ( m_mouseUpDeselect
)
3035 m_mouseUpDeselect
= false;
3037 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
3039 changingEvent
.m_itemOld
= htOldItem
;
3041 if ( IsTreeEventAllowed(changingEvent
) )
3044 DoSelectItem(wxTreeItemId(htItem
));
3045 SetFocusedItem(wxTreeItemId(htItem
));
3047 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
3049 changedEvent
.m_itemOld
= htOldItem
;
3050 (void)HandleTreeEvent(changedEvent
);
3055 m_htClickedItem
.Unset();
3057 if ( m_triggerStateImageClick
)
3059 if ( tvht
.flags
& TVHT_ONITEMSTATEICON
)
3061 wxTreeEvent
event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
,
3063 (void)HandleTreeEvent(event
);
3065 m_triggerStateImageClick
= false;
3070 if ( !m_dragStarted
&& MSWIsOnItem(tvht
.flags
) )
3082 m_dragImage
->EndDrag();
3083 wxDELETE(m_dragImage
);
3085 // generate the drag end event
3086 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
,
3088 event
.m_pointDrag
= wxPoint(x
, y
);
3089 (void)HandleTreeEvent(event
);
3091 // if we don't do it, the tree seems to think that 2 items
3092 // are selected simultaneously which is quite weird
3093 TreeView_SelectDropTarget(GetHwnd(), 0);
3095 #endif // wxUSE_DRAGIMAGE
3097 if ( isMultiple
&& nMsg
== WM_RBUTTONUP
)
3101 nmhdr
.hwndFrom
= GetHwnd();
3102 nmhdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
3103 nmhdr
.code
= NM_RCLICK
;
3104 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY
,
3105 nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
3109 m_dragStarted
= false;
3114 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) )
3118 // the tree control greys out the selected item when it loses focus
3119 // and paints it as selected again when it regains it, but it won't
3120 // do it for the other items itself - help it
3121 wxArrayTreeItemIds selections
;
3122 size_t count
= GetSelections(selections
);
3123 TVGetItemRectParam param
;
3125 for ( size_t n
= 0; n
< count
; n
++ )
3127 // TreeView_GetItemRect() will return false if item is not
3128 // visible, which may happen perfectly well
3129 if ( wxTreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
3132 ::InvalidateRect(GetHwnd(), ¶m
.rect
, FALSE
);
3137 if ( nMsg
== WM_KILLFOCUS
)
3142 else if ( (nMsg
== WM_KEYDOWN
|| nMsg
== WM_SYSKEYDOWN
) && isMultiple
)
3144 // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
3145 // notification but for the keys which can be used to change selection
3146 // we need to do it from here so as to not apply the default behaviour
3147 // if the events are handled by the user code
3160 if ( !HandleKeyDown(wParam
, lParam
) &&
3161 !MSWHandleTreeKeyDownEvent(wParam
, lParam
) )
3163 // use the key to update the selection if it was left
3165 MSWHandleSelectionKey(wParam
);
3168 // pretend that we did process it in any case as we already
3169 // generated an event for it
3172 //default: for all the other keys leave processed as false so that
3173 // the tree control generates a TVN_KEYDOWN for us
3177 else if ( nMsg
== WM_COMMAND
)
3179 // if we receive a EN_KILLFOCUS command from the in-place edit control
3180 // used for label editing, make sure to end editing
3183 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
3185 if ( cmd
== EN_KILLFOCUS
)
3187 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
3197 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
3203 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
3205 if ( nMsg
== WM_CHAR
)
3207 // don't let the control process Space and Return keys because it
3208 // doesn't do anything useful with them anyhow but always beeps
3209 // annoyingly when it receives them and there is no way to turn it off
3210 // simply if you just process TREEITEM_ACTIVATED event to which Space
3211 // and Enter presses are mapped in your code
3212 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
3216 else if ( nMsg
== WM_KEYDOWN
)
3218 if ( wParam
== VK_ESCAPE
)
3222 m_dragImage
->EndDrag();
3223 wxDELETE(m_dragImage
);
3225 // if we don't do it, the tree seems to think that 2 items
3226 // are selected simultaneously which is quite weird
3227 TreeView_SelectDropTarget(GetHwnd(), 0);
3231 #endif // wxUSE_DRAGIMAGE
3233 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
3236 // process WM_NOTIFY Windows message
3237 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
3239 wxTreeEvent
event(wxEVT_NULL
, this);
3240 wxEventType eventType
= wxEVT_NULL
;
3241 NMHDR
*hdr
= (NMHDR
*)lParam
;
3243 switch ( hdr
->code
)
3246 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3249 case TVN_BEGINRDRAG
:
3251 if ( eventType
== wxEVT_NULL
)
3252 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
3253 //else: left drag, already set above
3255 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3257 event
.m_item
= tv
->itemNew
.hItem
;
3258 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
3260 // don't allow dragging by default: the user code must
3261 // explicitly say that it wants to allow it to avoid breaking
3267 case TVN_BEGINLABELEDIT
:
3269 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
3270 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3272 // although the user event handler may still veto it, it is
3273 // important to set it now so that calls to SetItemText() from
3274 // the event handler would change the text controls contents
3276 event
.m_item
= info
->item
.hItem
;
3277 event
.m_label
= info
->item
.pszText
;
3278 event
.m_editCancelled
= false;
3282 case TVN_DELETEITEM
:
3284 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
3285 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3287 event
.m_item
= tv
->itemOld
.hItem
;
3291 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
3292 if ( it
!= m_attrs
.end() )
3301 case TVN_ENDLABELEDIT
:
3303 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
3304 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3306 event
.m_item
= info
->item
.hItem
;
3307 event
.m_label
= info
->item
.pszText
;
3308 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
3313 // These *must* not be removed or TVN_GETINFOTIP will
3314 // not be processed each time the mouse is moved
3315 // and the tooltip will only ever update once.
3324 #ifdef TVN_GETINFOTIP
3325 case TVN_GETINFOTIP
:
3327 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
3328 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
3330 // Which item are we trying to get a tooltip for?
3331 event
.m_item
= info
->hItem
;
3335 #endif // TVN_GETINFOTIP
3336 #endif // !__WXWINCE__
3338 case TVN_GETDISPINFO
:
3339 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
3342 case TVN_SETDISPINFO
:
3344 if ( eventType
== wxEVT_NULL
)
3345 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
3346 //else: get, already set above
3348 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3350 event
.m_item
= info
->item
.hItem
;
3354 case TVN_ITEMEXPANDING
:
3355 case TVN_ITEMEXPANDED
:
3357 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3360 switch ( tv
->action
)
3363 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
3371 what
= IDX_COLLAPSE
;
3375 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
3378 eventType
= gs_expandEvents
[what
][how
];
3380 event
.m_item
= tv
->itemNew
.hItem
;
3386 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
3388 // fabricate the lParam and wParam parameters sufficiently
3389 // similar to the ones from a "real" WM_KEYDOWN so that
3390 // CreateKeyEvent() works correctly
3391 return MSWHandleTreeKeyDownEvent(
3392 info
->wVKey
, (wxIsAltDown() ? KF_ALTDOWN
: 0) << 16);
3396 // Vista's tree control has introduced some problems with our
3397 // multi-selection tree. When TreeView_SelectItem() is called,
3398 // the wrong items are deselected.
3400 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3401 // that can be used to regulate this incorrect behaviour. The
3402 // following messages will allow only the unlocked item's selection
3405 case TVN_ITEMCHANGINGA
:
3406 case TVN_ITEMCHANGINGW
:
3408 // we only need to handles these in multi-select trees
3409 if ( HasFlag(wxTR_MULTIPLE
) )
3411 // get info about the item about to be changed
3412 NMTVITEMCHANGE
* info
= (NMTVITEMCHANGE
*)lParam
;
3413 if (TreeItemUnlocker::IsLocked(info
->hItem
))
3415 // item's state is locked, don't allow the change
3416 // returning 1 will disallow the change
3422 // allow the state change
3426 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
3427 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3428 // we have to handle both messages:
3429 case TVN_SELCHANGEDA
:
3430 case TVN_SELCHANGEDW
:
3431 if ( !m_changingSelection
)
3433 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
3437 case TVN_SELCHANGINGA
:
3438 case TVN_SELCHANGINGW
:
3439 if ( !m_changingSelection
)
3441 if ( eventType
== wxEVT_NULL
)
3442 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
3443 //else: already set above
3445 if (hdr
->code
== TVN_SELCHANGINGW
||
3446 hdr
->code
== TVN_SELCHANGEDW
)
3448 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
3449 event
.m_item
= tv
->itemNew
.hItem
;
3450 event
.m_itemOld
= tv
->itemOld
.hItem
;
3454 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
3455 event
.m_item
= tv
->itemNew
.hItem
;
3456 event
.m_itemOld
= tv
->itemOld
.hItem
;
3460 // we receive this message from WM_LBUTTONDOWN handler inside
3461 // comctl32.dll and so before the click is passed to
3462 // DefWindowProc() which sets the focus to the window which was
3463 // clicked and this can lead to unexpected event sequences: for
3464 // example, we may get a "selection change" event from the tree
3465 // before getting a "kill focus" event for the text control which
3466 // had the focus previously, thus breaking user code doing input
3469 // to avoid such surprises, we force the generation of focus events
3470 // now, before we generate the selection change ones
3471 if ( !m_changingSelection
)
3475 // instead of explicitly checking for _WIN32_IE, check if the
3476 // required symbols are available in the headers
3477 #if defined(CDDS_PREPAINT)
3480 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
3481 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
3482 switch ( nmcd
.dwDrawStage
)
3485 // if we've got any items with non standard attributes,
3486 // notify us before painting each item
3487 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
3490 // windows in TreeCtrl use one-based index for item state images,
3491 // 0 indexed image is not being used, we're using zero-based index,
3492 // so we have to add temp image (of zero index) to state image list
3493 // before we draw any item, then after items are drawn we have to
3494 // delete it (in POSTPAINT notify)
3495 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3497 typedef BOOL (wxSTDCALL
*ImageList_Copy_t
)
3498 (HIMAGELIST
, int, HIMAGELIST
, int, UINT
);
3499 static ImageList_Copy_t s_pfnImageList_Copy
= NULL
;
3500 static bool loaded
= false;
3504 wxLoadedDLL
dllComCtl32(wxT("comctl32.dll"));
3505 if ( dllComCtl32
.IsLoaded() )
3506 wxDL_INIT_FUNC(s_pfn
, ImageList_Copy
, dllComCtl32
);
3509 if ( !s_pfnImageList_Copy
)
3511 // this code is broken with ImageList_Copy()
3512 // but I don't care enough about Win95 support
3513 // to write it now -- if anybody does, please
3515 wxFAIL_MSG("TODO: implement this for Win95");
3520 hImageList
= GetHimagelistOf(m_imageListState
);
3522 // add temporary image
3524 m_imageListState
->GetSize(0, width
, height
);
3526 HBITMAP hbmpTemp
= ::CreateBitmap(width
, height
, 1, 1, NULL
);
3527 int index
= ::ImageList_Add(hImageList
, hbmpTemp
, hbmpTemp
);
3528 ::DeleteObject(hbmpTemp
);
3532 // move images to right
3533 for ( int i
= index
; i
> 0; i
-- )
3535 (*s_pfnImageList_Copy
)(hImageList
, i
,
3540 // we must remove the image in POSTPAINT notify
3541 *result
|= CDRF_NOTIFYPOSTPAINT
;
3546 case CDDS_POSTPAINT
:
3547 // we are deleting temp image of 0 index, which was
3548 // added before items were drawn (in PREPAINT notify)
3549 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3550 m_imageListState
->Remove(0);
3553 case CDDS_ITEMPREPAINT
:
3555 wxMapTreeAttr::iterator
3556 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
3558 if ( it
== m_attrs
.end() )
3560 // nothing to do for this item
3561 *result
= CDRF_DODEFAULT
;
3565 wxTreeItemAttr
* const attr
= it
->second
;
3567 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
3568 TVIF_STATE
, TVIS_DROPHILITED
);
3570 const UINT tvItemState
= tvItem
.state
;
3572 // selection colours should override ours,
3573 // otherwise it is too confusing to the user
3574 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
3575 !(tvItemState
& TVIS_DROPHILITED
) )
3578 if ( attr
->HasBackgroundColour() )
3580 colBack
= attr
->GetBackgroundColour();
3581 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
3585 // but we still want to keep the special foreground
3586 // colour when we don't have focus (we can't keep
3587 // it when we do, it would usually be unreadable on
3588 // the almost inverted bg colour...)
3589 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
3590 FindFocus() != this ) &&
3591 !(tvItemState
& TVIS_DROPHILITED
) )
3594 if ( attr
->HasTextColour() )
3596 colText
= attr
->GetTextColour();
3597 lptvcd
->clrText
= wxColourToRGB(colText
);
3601 if ( attr
->HasFont() )
3603 HFONT hFont
= GetHfontOf(attr
->GetFont());
3605 ::SelectObject(nmcd
.hdc
, hFont
);
3607 *result
= CDRF_NEWFONT
;
3609 else // no specific font
3611 *result
= CDRF_DODEFAULT
;
3617 *result
= CDRF_DODEFAULT
;
3621 // we always process it
3623 #endif // have owner drawn support in headers
3627 DWORD pos
= GetMessagePos();
3629 point
.x
= LOWORD(pos
);
3630 point
.y
= HIWORD(pos
);
3631 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
3633 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), htFlags
);
3635 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3637 event
.m_item
= item
;
3638 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
3647 TV_HITTESTINFO tvhti
;
3648 wxGetCursorPosMSW(&tvhti
.pt
);
3649 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
3650 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
3652 if ( MSWIsOnItem(tvhti
.flags
) )
3654 event
.m_item
= tvhti
.hItem
;
3655 eventType
= (int)hdr
->code
== NM_DBLCLK
3656 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
3657 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
3659 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
3660 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
3669 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
3672 event
.SetEventType(eventType
);
3674 bool processed
= HandleTreeEvent(event
);
3677 switch ( hdr
->code
)
3680 // we translate NM_DBLCLK into ACTIVATED event and if the user
3681 // handled the activation of the item we shouldn't proceed with
3682 // also using the same double click for toggling the item expanded
3683 // state -- but OTOH do let the user to expand/collapse the item by
3684 // double clicking on it if the activation is not handled specially
3685 *result
= processed
;
3689 // prevent tree control from sending WM_CONTEXTMENU to our parent
3690 // (which it does if NM_RCLICK is not handled) because we want to
3691 // send it to the control itself
3695 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
3696 (WPARAM
)GetHwnd(), ::GetMessagePos());
3700 case TVN_BEGINRDRAG
:
3702 if ( event
.IsAllowed() )
3704 // normally this is impossible because the m_dragImage is
3705 // deleted once the drag operation is over
3706 wxASSERT_MSG( !m_dragImage
, wxT("starting to drag once again?") );
3708 m_dragImage
= new wxDragImage(*this, event
.m_item
);
3709 m_dragImage
->BeginDrag(wxPoint(0,0), this);
3710 m_dragImage
->Show();
3712 m_dragStarted
= true;
3714 #endif // wxUSE_DRAGIMAGE
3717 case TVN_DELETEITEM
:
3719 // NB: we might process this message using wxWidgets event
3720 // tables, but due to overhead of wxWin event system we
3721 // prefer to do it here ourself (otherwise deleting a tree
3722 // with many items is just too slow)
3723 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3725 wxTreeItemParam
*param
=
3726 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
3729 processed
= true; // Make sure we don't get called twice
3733 case TVN_BEGINLABELEDIT
:
3734 // return true to cancel label editing
3735 *result
= !event
.IsAllowed();
3737 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3738 if ( event
.IsAllowed() )
3740 HWND hText
= TreeView_GetEditControl(GetHwnd());
3743 // MBN: if m_textCtrl already has an HWND, it is a stale
3744 // pointer from a previous edit (because the user
3745 // didn't modify the label before dismissing the control,
3746 // and TVN_ENDLABELEDIT was not sent), so delete it
3747 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
3750 m_textCtrl
= new wxTextCtrl();
3751 m_textCtrl
->SetParent(this);
3752 m_textCtrl
->SetHWND((WXHWND
)hText
);
3753 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3755 // set wxTE_PROCESS_ENTER style for the text control to
3756 // force it to process the Enter presses itself, otherwise
3757 // they could be stolen from it by the dialog
3759 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3760 | wxTE_PROCESS_ENTER
);
3763 else // we had set m_idEdited before
3769 case TVN_ENDLABELEDIT
:
3770 // return true to set the label to the new string: note that we
3771 // also must pretend that we did process the message or it is going
3772 // to be passed to DefWindowProc() which will happily return false
3773 // cancelling the label change
3774 *result
= event
.IsAllowed();
3777 // ensure that we don't have the text ctrl which is going to be
3783 #ifdef TVN_GETINFOTIP
3784 case TVN_GETINFOTIP
:
3786 // If the user permitted a tooltip change, change it
3787 if (event
.IsAllowed())
3789 SetToolTip(event
.m_label
);
3796 case TVN_SELCHANGING
:
3797 case TVN_ITEMEXPANDING
:
3798 // return true to prevent the action from happening
3799 *result
= !event
.IsAllowed();
3802 case TVN_ITEMEXPANDED
:
3804 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3805 const wxTreeItemId
id(tv
->itemNew
.hItem
);
3807 if ( tv
->action
== TVE_COLLAPSE
)
3809 if ( wxApp::GetComCtl32Version() >= 600 )
3811 // for some reason the item selection rectangle depends
3812 // on whether it is expanded or collapsed (at least
3813 // with comctl32.dll v6): it is wider (by 3 pixels) in
3814 // the expanded state, so when the item collapses and
3815 // then is deselected the rightmost 3 pixels of the
3816 // previously drawn selection are left on the screen
3818 // it's not clear if it's a bug in comctl32.dll or in
3819 // our code (because it does not happen in Explorer but
3820 // OTOH we don't do anything which could result in this
3821 // AFAICS) but we do need to work around it to avoid
3828 // the item is also not refreshed properly after expansion when
3829 // it has an image depending on the expanded/collapsed state:
3830 // again, it's not clear if the bug is in comctl32.dll or our
3832 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3841 case TVN_GETDISPINFO
:
3842 // NB: so far the user can't set the image himself anyhow, so do it
3843 // anyway - but this may change later
3844 //if ( /* !processed && */ )
3846 wxTreeItemId item
= event
.m_item
;
3847 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3849 const wxTreeItemParam
* const param
= GetItemParam(item
);
3853 if ( info
->item
.mask
& TVIF_IMAGE
)
3858 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3859 : wxTreeItemIcon_Normal
3862 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3864 info
->item
.iSelectedImage
=
3867 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3868 : wxTreeItemIcon_Selected
3875 // for the other messages the return value is ignored and there is
3876 // nothing special to do
3881 // ----------------------------------------------------------------------------
3883 // ----------------------------------------------------------------------------
3885 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3886 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3888 int wxTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
3890 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
3892 // receive the desired information
3893 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3896 // state images are one-based
3897 return STATEIMAGEMASKTOINDEX(tvItem
.state
) - 1;
3900 void wxTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
3902 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
3904 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3906 // state images are one-based
3907 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3908 tvItem
.state
= INDEXTOSTATEIMAGEMASK(state
+ 1);
3913 #endif // wxUSE_TREECTRL