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 m_isBeingDeleted
= true;
796 // delete any attributes
799 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
801 // prevent TVN_DELETEITEM handler from deleting the attributes again!
802 m_hasAnyAttr
= false;
807 // delete user data to prevent memory leaks
808 // also deletes hidden root node storage.
812 // ----------------------------------------------------------------------------
814 // ----------------------------------------------------------------------------
816 /* static */ wxVisualAttributes
817 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
819 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
821 // common controls have their own default font
822 attrs
.font
= wxGetCCDefaultFont();
828 // simple wrappers which add error checking in debug mode
830 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
832 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
833 wxT("can't retrieve virtual root item") );
835 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
837 wxLogLastError(wxT("TreeView_GetItem"));
845 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
847 TreeItemUnlocker
unlocker(tvItem
->hItem
);
849 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
851 wxLogLastError(wxT("TreeView_SetItem"));
855 unsigned int wxTreeCtrl::GetCount() const
857 return (unsigned int)TreeView_GetCount(GetHwnd());
860 unsigned int wxTreeCtrl::GetIndent() const
862 return TreeView_GetIndent(GetHwnd());
865 void wxTreeCtrl::SetIndent(unsigned int indent
)
867 TreeView_SetIndent(GetHwnd(), indent
);
870 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
873 (void) TreeView_SetImageList(GetHwnd(),
874 imageList
? imageList
->GetHIMAGELIST() : 0,
878 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
880 if (m_ownsImageListNormal
)
881 delete m_imageListNormal
;
883 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
884 m_ownsImageListNormal
= false;
887 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
889 if (m_ownsImageListState
) delete m_imageListState
;
890 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
891 m_ownsImageListState
= false;
894 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
895 bool recursively
) const
897 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
899 TraverseCounter
counter(this, item
, recursively
);
900 return counter
.GetCount() - 1;
903 // ----------------------------------------------------------------------------
905 // ----------------------------------------------------------------------------
907 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
909 if ( !wxWindowBase::SetBackgroundColour(colour
) )
912 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
917 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
919 if ( !wxWindowBase::SetForegroundColour(colour
) )
922 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
927 // ----------------------------------------------------------------------------
929 // ----------------------------------------------------------------------------
931 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
933 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
936 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
938 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
940 wxChar buf
[512]; // the size is arbitrary...
942 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
943 tvItem
.pszText
= buf
;
944 tvItem
.cchTextMax
= WXSIZEOF(buf
);
945 if ( !DoGetItem(&tvItem
) )
947 // don't return some garbage which was on stack, but an empty string
951 return wxString(buf
);
954 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
956 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
958 if ( IS_VIRTUAL_ROOT(item
) )
961 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
962 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
965 // when setting the text of the item being edited, the text control should
966 // be updated to reflect the new text as well, otherwise calling
967 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
969 // don't use GetEditControl() here because m_textCtrl is not set yet
970 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
973 if ( item
== m_idEdited
)
975 ::SetWindowText(hwndEdit
, text
.wx_str());
980 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
981 wxTreeItemIcon which
) const
983 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
985 if ( IsHiddenRoot(item
) )
987 // no images for hidden root item
991 wxTreeItemParam
*param
= GetItemParam(item
);
993 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
996 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
997 wxTreeItemIcon which
)
999 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1000 wxCHECK_RET( which
>= 0 &&
1001 which
< wxTreeItemIcon_Max
,
1002 wxT("invalid image index"));
1005 if ( IsHiddenRoot(item
) )
1007 // no images for hidden root item
1011 wxTreeItemParam
*data
= GetItemParam(item
);
1015 data
->SetImage(image
, which
);
1020 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
1022 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1024 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1026 // hidden root may still have data.
1027 if ( IS_VIRTUAL_ROOT(item
) )
1029 return GET_VIRTUAL_ROOT()->GetParam();
1033 if ( !DoGetItem(&tvItem
) )
1038 return (wxTreeItemParam
*)tvItem
.lParam
;
1041 bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent
& event
) const
1043 if ( event
.m_item
.IsOk() )
1045 event
.SetClientObject(GetItemData(event
.m_item
));
1048 return HandleWindowEvent(event
);
1051 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1053 wxTreeItemParam
*data
= GetItemParam(item
);
1055 return data
? data
->GetData() : NULL
;
1058 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1060 // first, associate this piece of data with this item
1066 wxTreeItemParam
*param
= GetItemParam(item
);
1068 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1070 param
->SetData(data
);
1073 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1075 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1077 if ( IS_VIRTUAL_ROOT(item
) )
1080 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1081 tvItem
.cChildren
= (int)has
;
1085 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1087 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1089 if ( IS_VIRTUAL_ROOT(item
) )
1092 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1093 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1097 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1099 if ( IS_VIRTUAL_ROOT(item
) )
1102 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1103 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1107 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1109 if ( IS_VIRTUAL_ROOT(item
) )
1113 if ( GetBoundingRect(item
, rect
) )
1119 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1121 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1123 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1124 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1127 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1129 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1131 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1132 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1135 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1137 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1139 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1140 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1143 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1144 const wxColour
& col
)
1146 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1148 wxTreeItemAttr
*attr
;
1149 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1150 if ( it
== m_attrs
.end() )
1152 m_hasAnyAttr
= true;
1154 m_attrs
[item
.m_pItem
] =
1155 attr
= new wxTreeItemAttr
;
1162 attr
->SetTextColour(col
);
1167 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1168 const wxColour
& col
)
1170 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1172 wxTreeItemAttr
*attr
;
1173 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1174 if ( it
== m_attrs
.end() )
1176 m_hasAnyAttr
= true;
1178 m_attrs
[item
.m_pItem
] =
1179 attr
= new wxTreeItemAttr
;
1181 else // already in the hash
1186 attr
->SetBackgroundColour(col
);
1191 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1193 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1195 wxTreeItemAttr
*attr
;
1196 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1197 if ( it
== m_attrs
.end() )
1199 m_hasAnyAttr
= true;
1201 m_attrs
[item
.m_pItem
] =
1202 attr
= new wxTreeItemAttr
;
1204 else // already in the hash
1209 attr
->SetFont(font
);
1211 // Reset the item's text to ensure that the bounding rect will be adjusted
1212 // for the new font.
1213 SetItemText(item
, GetItemText(item
));
1218 // ----------------------------------------------------------------------------
1220 // ----------------------------------------------------------------------------
1222 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1224 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1226 if ( item
== wxTreeItemId(TVI_ROOT
) )
1228 // virtual (hidden) root is never visible
1232 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1233 TVGetItemRectParam param
;
1235 // true means to get rect for just the text, not the whole line
1236 if ( !wxTreeView_GetItemRect(GetHwnd(), HITEM(item
), param
, TRUE
) )
1238 // if TVM_GETITEMRECT returned false, then the item is definitely not
1239 // visible (because its parent is not expanded)
1243 // however if it returned true, the item might still be outside the
1244 // currently visible part of the tree, test for it (notice that partly
1245 // visible means visible here)
1246 return param
.rect
.bottom
> 0 && param
.rect
.top
< GetClientSize().y
;
1249 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1251 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1253 if ( IS_VIRTUAL_ROOT(item
) )
1255 wxTreeItemIdValue cookie
;
1256 return GetFirstChild(item
, cookie
).IsOk();
1259 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1262 return tvItem
.cChildren
!= 0;
1265 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1267 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1269 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1272 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1275 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1277 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1279 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1282 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1285 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1287 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1289 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1292 return (tvItem
.state
& TVIS_BOLD
) != 0;
1295 // ----------------------------------------------------------------------------
1297 // ----------------------------------------------------------------------------
1299 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1301 // Root may be real (visible) or virtual (hidden).
1302 if ( GET_VIRTUAL_ROOT() )
1305 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1308 wxTreeItemId
wxTreeCtrl::GetSelection() const
1310 wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE
), wxTreeItemId(),
1311 wxT("this only works with single selection controls") );
1313 return GetFocusedItem();
1316 wxTreeItemId
wxTreeCtrl::GetFocusedItem() const
1318 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1321 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1323 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1327 if ( IS_VIRTUAL_ROOT(item
) )
1329 // no parent for the virtual root
1334 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1335 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1337 // the top level items should have the virtual root as their parent
1342 return wxTreeItemId(hItem
);
1345 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1346 wxTreeItemIdValue
& cookie
) const
1348 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1350 // remember the last child returned in 'cookie'
1351 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1353 return wxTreeItemId(cookie
);
1356 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1357 wxTreeItemIdValue
& cookie
) const
1359 wxTreeItemId
fromCookie(cookie
);
1361 HTREEITEM hitem
= HITEM(fromCookie
);
1363 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1365 wxTreeItemId
item(hitem
);
1367 cookie
= item
.m_pItem
;
1372 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1374 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1376 // can this be done more efficiently?
1377 wxTreeItemIdValue cookie
;
1379 wxTreeItemId childLast
,
1380 child
= GetFirstChild(item
, cookie
);
1381 while ( child
.IsOk() )
1384 child
= GetNextChild(item
, cookie
);
1390 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1392 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1393 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1396 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1398 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1399 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1402 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1404 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1407 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1409 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1410 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1412 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1413 if ( next
.IsOk() && !IsVisible(next
) )
1415 // Win32 considers that any non-collapsed item is visible while we want
1416 // to return only really visible items
1423 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1425 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1426 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1428 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1429 if ( prev
.IsOk() && !IsVisible(prev
) )
1431 // just as above, Win32 function will happily return the previous item
1432 // in the tree for the first visible item too
1439 // ----------------------------------------------------------------------------
1440 // multiple selections emulation
1441 // ----------------------------------------------------------------------------
1443 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1445 TraverseSelections
selector(this, selections
);
1447 return selector
.GetCount();
1450 // ----------------------------------------------------------------------------
1452 // ----------------------------------------------------------------------------
1454 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1455 const wxTreeItemId
& hInsertAfter
,
1456 const wxString
& text
,
1457 int image
, int selectedImage
,
1458 wxTreeItemData
*data
)
1460 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1462 wxT("can't have more than one root in the tree") );
1464 TV_INSERTSTRUCT tvIns
;
1465 tvIns
.hParent
= HITEM(parent
);
1466 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1468 // this is how we insert the item as the first child: supply a NULL
1470 if ( !tvIns
.hInsertAfter
)
1472 tvIns
.hInsertAfter
= TVI_FIRST
;
1476 if ( !text
.empty() )
1479 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1483 tvIns
.item
.pszText
= NULL
;
1484 tvIns
.item
.cchTextMax
= 0;
1487 // create the param which will store the other item parameters
1488 wxTreeItemParam
*param
= new wxTreeItemParam
;
1490 // we return the images on demand as they depend on whether the item is
1491 // expanded or collapsed too in our case
1492 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1493 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1494 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1496 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1497 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1500 tvIns
.item
.lParam
= (LPARAM
)param
;
1501 tvIns
.item
.mask
= mask
;
1503 // don't use the hack below for the children of hidden root: this results
1504 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1505 const bool firstChild
= !IsHiddenRoot(parent
) &&
1506 !TreeView_GetChild(GetHwnd(), HITEM(parent
));
1508 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1511 wxLogLastError(wxT("TreeView_InsertItem"));
1514 // apparently some Windows versions (2000 and XP are reported to do this)
1515 // sometimes don't refresh the tree after adding the first child and so we
1516 // need this to make the "[+]" appear
1519 TVGetItemRectParam param
;
1521 wxTreeView_GetItemRect(GetHwnd(), HITEM(parent
), param
, FALSE
);
1522 ::InvalidateRect(GetHwnd(), ¶m
.rect
, FALSE
);
1525 // associate the application tree item with Win32 tree item handle
1528 // setup wxTreeItemData
1531 param
->SetData(data
);
1535 return wxTreeItemId(id
);
1538 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1539 int image
, int selectedImage
,
1540 wxTreeItemData
*data
)
1542 if ( HasFlag(wxTR_HIDE_ROOT
) )
1544 wxASSERT_MSG( !m_pVirtualRoot
, wxT("tree can have only a single root") );
1546 // create a virtual root item, the parent for all the others
1547 wxTreeItemParam
*param
= new wxTreeItemParam
;
1548 param
->SetData(data
);
1550 m_pVirtualRoot
= new wxVirtualNode(param
);
1555 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1556 text
, image
, selectedImage
, data
);
1559 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1561 const wxString
& text
,
1562 int image
, int selectedImage
,
1563 wxTreeItemData
*data
)
1565 wxTreeItemId idPrev
;
1566 if ( index
== (size_t)-1 )
1568 // special value: append to the end
1571 else // find the item from index
1573 wxTreeItemIdValue cookie
;
1574 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1575 while ( index
!= 0 && idCur
.IsOk() )
1580 idCur
= GetNextChild(parent
, cookie
);
1583 // assert, not check: if the index is invalid, we will append the item
1585 wxASSERT_MSG( index
== 0, wxT("bad index in wxTreeCtrl::InsertItem") );
1588 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1591 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1593 // unlock tree selections on vista, without this the
1594 // tree ctrl will eventually crash after item deletion
1595 TreeItemUnlocker unlock_all
;
1597 if ( HasFlag(wxTR_MULTIPLE
) )
1599 bool selected
= IsSelected(item
);
1604 next
= TreeView_GetNextVisible(GetHwnd(), HITEM(item
));
1608 next
= TreeView_GetPrevVisible(GetHwnd(), HITEM(item
));
1613 TempSetter
set(m_changingSelection
);
1614 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1616 wxLogLastError(wxT("TreeView_DeleteItem"));
1626 if ( item
== m_htSelStart
)
1627 m_htSelStart
.Unset();
1629 if ( item
== m_htClickedItem
)
1630 m_htClickedItem
.Unset();
1634 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
1636 if ( IsTreeEventAllowed(changingEvent
) )
1638 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
1639 (void)HandleTreeEvent(changedEvent
);
1643 DoUnselectItem(next
);
1650 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1652 wxLogLastError(wxT("TreeView_DeleteItem"));
1657 // delete all children (but don't delete the item itself)
1658 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1660 // unlock tree selections on vista for the duration of this call
1661 TreeItemUnlocker unlock_all
;
1663 wxTreeItemIdValue cookie
;
1665 wxArrayTreeItemIds children
;
1666 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1667 while ( child
.IsOk() )
1669 children
.Add(child
);
1671 child
= GetNextChild(item
, cookie
);
1674 size_t nCount
= children
.Count();
1675 for ( size_t n
= 0; n
< nCount
; n
++ )
1677 Delete(children
[n
]);
1681 void wxTreeCtrl::DeleteAllItems()
1683 // unlock tree selections on vista for the duration of this call
1684 TreeItemUnlocker unlock_all
;
1686 // invalidate all the items we store as they're going to become invalid
1688 m_htClickedItem
= wxTreeItemId();
1690 // delete the "virtual" root item.
1691 if ( GET_VIRTUAL_ROOT() )
1693 delete GET_VIRTUAL_ROOT();
1694 m_pVirtualRoot
= NULL
;
1697 // and all the real items
1699 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1701 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1705 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1707 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1708 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1709 flag
== TVE_EXPAND
||
1711 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1713 // A hidden root can be neither expanded nor collapsed.
1714 wxCHECK_RET( !IsHiddenRoot(item
),
1715 wxT("Can't expand/collapse hidden root node!") );
1717 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1718 // emulate them. This behaviour has changed slightly with comctl32.dll
1719 // v 4.70 - now it does send them but only the first time. To maintain
1720 // compatible behaviour and also in order to not have surprises with the
1721 // future versions, don't rely on this and still do everything ourselves.
1722 // To avoid that the messages be sent twice when the item is expanded for
1723 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1725 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1729 if ( IsExpanded(item
) )
1731 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
,
1732 this, wxTreeItemId(item
));
1734 if ( !IsTreeEventAllowed(event
) )
1738 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) )
1740 if ( IsExpanded(item
) )
1743 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, this, item
);
1744 (void)HandleTreeEvent(event
);
1746 //else: change didn't took place, so do nothing at all
1749 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1751 DoExpand(item
, TVE_EXPAND
);
1754 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1756 DoExpand(item
, TVE_COLLAPSE
);
1759 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1761 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1764 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1766 DoExpand(item
, TVE_TOGGLE
);
1769 void wxTreeCtrl::Unselect()
1771 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE
),
1772 wxT("doesn't make sense, may be you want UnselectAll()?") );
1774 // the current focus
1775 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1782 if ( HasFlag(wxTR_MULTIPLE
) )
1784 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
1785 this, wxTreeItemId());
1786 changingEvent
.m_itemOld
= htFocus
;
1788 if ( IsTreeEventAllowed(changingEvent
) )
1792 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1793 this, wxTreeItemId());
1794 changedEvent
.m_itemOld
= htFocus
;
1795 (void)HandleTreeEvent(changedEvent
);
1804 void wxTreeCtrl::DoUnselectAll()
1806 wxArrayTreeItemIds selections
;
1807 size_t count
= GetSelections(selections
);
1809 for ( size_t n
= 0; n
< count
; n
++ )
1811 DoUnselectItem(selections
[n
]);
1814 m_htSelStart
.Unset();
1817 void wxTreeCtrl::UnselectAll()
1819 if ( HasFlag(wxTR_MULTIPLE
) )
1821 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1822 if ( !htFocus
) return;
1824 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1825 changingEvent
.m_itemOld
= htFocus
;
1827 if ( IsTreeEventAllowed(changingEvent
) )
1831 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1832 changedEvent
.m_itemOld
= htFocus
;
1833 (void)HandleTreeEvent(changedEvent
);
1842 void wxTreeCtrl::DoSelectChildren(const wxTreeItemId
& parent
)
1846 wxTreeItemIdValue cookie
;
1847 wxTreeItemId child
= GetFirstChild(parent
, cookie
);
1848 while ( child
.IsOk() )
1850 DoSelectItem(child
, true);
1851 child
= GetNextChild(child
, cookie
);
1855 void wxTreeCtrl::SelectChildren(const wxTreeItemId
& parent
)
1857 wxCHECK_RET( HasFlag(wxTR_MULTIPLE
),
1858 "this only works with multiple selection controls" );
1860 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1862 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1863 changingEvent
.m_itemOld
= htFocus
;
1865 if ( IsTreeEventAllowed(changingEvent
) )
1867 DoSelectChildren(parent
);
1869 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1870 changedEvent
.m_itemOld
= htFocus
;
1871 (void)HandleTreeEvent(changedEvent
);
1875 void wxTreeCtrl::DoSelectItem(const wxTreeItemId
& item
, bool select
)
1877 TempSetter
set(m_changingSelection
);
1879 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1882 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1884 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't select hidden root item") );
1886 if ( select
== IsSelected(item
) )
1888 // nothing to do, the item is already in the requested state
1892 if ( HasFlag(wxTR_MULTIPLE
) )
1894 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1896 if ( IsTreeEventAllowed(changingEvent
) )
1898 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1899 DoSelectItem(item
, select
);
1903 SetFocusedItem(item
);
1906 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1908 (void)HandleTreeEvent(changedEvent
);
1911 else // single selection
1913 wxTreeItemId itemOld
, itemNew
;
1916 itemOld
= GetSelection();
1919 else // deselecting the currently selected item
1922 // leave itemNew invalid
1925 // Recent versions of comctl32.dll send TVN_SELCHANG{ED,ING} events
1926 // when we call TreeView_SelectItem() but apparently some old ones did
1927 // not so send the events ourselves and ignore those generated by
1928 // TreeView_SelectItem() if m_changingSelection is set.
1930 changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, itemNew
);
1931 changingEvent
.SetOldItem(itemOld
);
1933 if ( IsTreeEventAllowed(changingEvent
) )
1935 TempSetter
set(m_changingSelection
);
1937 if ( !TreeView_SelectItem(GetHwnd(), HITEM(itemNew
)) )
1939 wxLogLastError(wxT("TreeView_SelectItem"));
1943 ::SetFocus(GetHwnd(), HITEM(item
));
1945 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1947 changedEvent
.SetOldItem(itemOld
);
1948 (void)HandleTreeEvent(changedEvent
);
1951 //else: program vetoed the change
1955 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1957 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't show hidden root item") );
1960 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1963 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1965 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1967 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1971 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1976 void wxTreeCtrl::DeleteTextCtrl()
1980 // the HWND corresponding to this control is deleted by the tree
1981 // control itself and we don't know when exactly this happens, so check
1982 // if the window still exists before calling UnsubclassWin()
1983 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1985 m_textCtrl
->SetHWND(0);
1988 m_textCtrl
->UnsubclassWin();
1989 m_textCtrl
->SetHWND(0);
1990 wxDELETE(m_textCtrl
);
1996 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1997 wxClassInfo
*textControlClass
)
1999 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
2004 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
2005 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
2007 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2011 wxDELETE(m_textCtrl
);
2015 // textctrl is subclassed in MSWOnNotify
2019 // End label editing, optionally cancelling the edit
2020 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
2022 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
2027 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2029 TV_HITTESTINFO hitTestInfo
;
2030 hitTestInfo
.pt
.x
= (int)point
.x
;
2031 hitTestInfo
.pt
.y
= (int)point
.y
;
2033 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2038 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2039 flags |= wxTREE_HITTEST_##flag
2041 TRANSLATE_FLAG(ABOVE
);
2042 TRANSLATE_FLAG(BELOW
);
2043 TRANSLATE_FLAG(NOWHERE
);
2044 TRANSLATE_FLAG(ONITEMBUTTON
);
2045 TRANSLATE_FLAG(ONITEMICON
);
2046 TRANSLATE_FLAG(ONITEMINDENT
);
2047 TRANSLATE_FLAG(ONITEMLABEL
);
2048 TRANSLATE_FLAG(ONITEMRIGHT
);
2049 TRANSLATE_FLAG(ONITEMSTATEICON
);
2050 TRANSLATE_FLAG(TOLEFT
);
2051 TRANSLATE_FLAG(TORIGHT
);
2053 #undef TRANSLATE_FLAG
2055 return wxTreeItemId(hitTestInfo
.hItem
);
2058 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2060 bool textOnly
) const
2062 // Virtual root items have no bounding rectangle
2063 if ( IS_VIRTUAL_ROOT(item
) )
2068 TVGetItemRectParam param
;
2070 if ( wxTreeView_GetItemRect(GetHwnd(), HITEM(item
), param
, textOnly
) )
2072 rect
= wxRect(wxPoint(param
.rect
.left
, param
.rect
.top
),
2073 wxPoint(param
.rect
.right
, param
.rect
.bottom
));
2079 // couldn't retrieve rect: for example, item isn't visible
2084 void wxTreeCtrl::ClearFocusedItem()
2086 TempSetter
set(m_changingSelection
);
2088 if ( !TreeView_SelectItem(GetHwnd(), 0) )
2090 wxLogLastError(wxT("TreeView_SelectItem"));
2094 void wxTreeCtrl::SetFocusedItem(const wxTreeItemId
& item
)
2096 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2098 TempSetter
set(m_changingSelection
);
2100 ::SetFocus(GetHwnd(), HITEM(item
));
2103 void wxTreeCtrl::DoUnselectItem(const wxTreeItemId
& item
)
2105 TempSetter
set(m_changingSelection
);
2107 ::UnselectItem(GetHwnd(), HITEM(item
));
2110 void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId
& item
)
2112 TempSetter
set(m_changingSelection
);
2114 ::ToggleItemSelection(GetHwnd(), HITEM(item
));
2117 // ----------------------------------------------------------------------------
2119 // ----------------------------------------------------------------------------
2121 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2122 // functions such as IsDataIndirect()
2123 class wxTreeSortHelper
2126 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2129 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
2131 return ((wxTreeItemParam
*)lParam
)->GetItem();
2135 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2139 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2140 wxT("sorting tree without data doesn't make sense") );
2142 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2144 return tree
->OnCompareItems(GetIdFromData(pItem1
),
2145 GetIdFromData(pItem2
));
2148 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2150 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2152 // rely on the fact that TreeView_SortChildren does the same thing as our
2153 // default behaviour, i.e. sorts items alphabetically and so call it
2154 // directly if we're not in derived class (much more efficient!)
2155 // RN: Note that if you find you're code doesn't sort as expected this
2156 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
2157 // combo for your derived wxTreeCtrl if will sort without
2159 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2161 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2166 tvSort
.hParent
= HITEM(item
);
2167 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2168 tvSort
.lParam
= (LPARAM
)this;
2169 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2173 // ----------------------------------------------------------------------------
2175 // ----------------------------------------------------------------------------
2177 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
2179 if ( msg
->message
== WM_KEYDOWN
)
2181 // Only eat VK_RETURN if not being used by the application in
2182 // conjunction with modifiers
2183 if ( (msg
->wParam
== VK_RETURN
) && !wxIsAnyModifierDown() )
2185 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2190 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
2193 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
2195 const int id
= (signed short)id_
;
2197 if ( cmd
== EN_UPDATE
)
2199 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2200 event
.SetEventObject( this );
2201 ProcessCommand(event
);
2203 else if ( cmd
== EN_KILLFOCUS
)
2205 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2206 event
.SetEventObject( this );
2207 ProcessCommand(event
);
2215 // command processed
2219 bool wxTreeCtrl::MSWIsOnItem(unsigned flags
) const
2221 unsigned mask
= TVHT_ONITEM
;
2222 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2223 mask
|= TVHT_ONITEMINDENT
| TVHT_ONITEMRIGHT
;
2225 return (flags
& mask
) != 0;
2228 bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey
)
2230 const bool bCtrl
= wxIsCtrlDown();
2231 const bool bShift
= wxIsShiftDown();
2232 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2241 if ( vkey
!= VK_RETURN
&& bCtrl
)
2243 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2245 changingEvent
.m_itemOld
= htSel
;
2247 if ( IsTreeEventAllowed(changingEvent
) )
2249 DoToggleItemSelection(wxTreeItemId(htSel
));
2251 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2253 changedEvent
.m_itemOld
= htSel
;
2254 (void)HandleTreeEvent(changedEvent
);
2259 wxArrayTreeItemIds selections
;
2260 size_t count
= GetSelections(selections
);
2262 if ( count
!= 1 || HITEM(selections
[0]) != htSel
)
2264 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2266 changingEvent
.m_itemOld
= htSel
;
2268 if ( IsTreeEventAllowed(changingEvent
) )
2271 DoSelectItem(wxTreeItemId(htSel
));
2273 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2275 changedEvent
.m_itemOld
= htSel
;
2276 (void)HandleTreeEvent(changedEvent
);
2284 if ( !bCtrl
&& !bShift
)
2286 wxArrayTreeItemIds selections
;
2291 next
= vkey
== VK_UP
2292 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2293 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2297 next
= GetRootItem();
2299 if ( IsHiddenRoot(next
) )
2300 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2308 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2310 changingEvent
.m_itemOld
= htSel
;
2312 if ( IsTreeEventAllowed(changingEvent
) )
2316 SetFocusedItem(next
);
2318 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2320 changedEvent
.m_itemOld
= htSel
;
2321 (void)HandleTreeEvent(changedEvent
);
2326 wxTreeItemId next
= vkey
== VK_UP
2327 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2328 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2335 if ( !m_htSelStart
)
2337 m_htSelStart
= htSel
;
2340 if ( bShift
&& SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2341 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2343 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2344 changingEvent
.m_itemOld
= htSel
;
2346 if ( IsTreeEventAllowed(changingEvent
) )
2348 SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2349 SR_UNSELECT_OTHERS
);
2351 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2352 changedEvent
.m_itemOld
= htSel
;
2353 (void)HandleTreeEvent(changedEvent
);
2357 SetFocusedItem(next
);
2362 if ( HasChildren(htSel
) && IsExpanded(htSel
) )
2368 wxTreeItemId next
= GetItemParent(htSel
);
2370 if ( next
.IsOk() && !IsHiddenRoot(next
) )
2372 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2374 changingEvent
.m_itemOld
= htSel
;
2376 if ( IsTreeEventAllowed(changingEvent
) )
2380 SetFocusedItem(next
);
2382 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2384 changedEvent
.m_itemOld
= htSel
;
2385 (void)HandleTreeEvent(changedEvent
);
2392 if ( !IsVisible(htSel
) )
2394 EnsureVisible(htSel
);
2397 if ( !HasChildren(htSel
) )
2400 if ( !IsExpanded(htSel
) )
2406 wxTreeItemId next
= TreeView_GetChild(GetHwnd(), htSel
);
2408 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2409 changingEvent
.m_itemOld
= htSel
;
2411 if ( IsTreeEventAllowed(changingEvent
) )
2415 SetFocusedItem(next
);
2417 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2418 changedEvent
.m_itemOld
= htSel
;
2419 (void)HandleTreeEvent(changedEvent
);
2427 wxTreeItemId next
= GetRootItem();
2429 if ( IsHiddenRoot(next
) )
2431 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2437 if ( vkey
== VK_END
)
2441 wxTreeItemId nextTemp
= TreeView_GetNextVisible(
2442 GetHwnd(), HITEM(next
));
2444 if ( !nextTemp
.IsOk() )
2451 if ( htSel
== HITEM(next
) )
2456 if ( !m_htSelStart
)
2458 m_htSelStart
= htSel
;
2461 if ( SelectRange(GetHwnd(),
2462 HITEM(m_htSelStart
), HITEM(next
),
2463 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2465 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2467 changingEvent
.m_itemOld
= htSel
;
2469 if ( IsTreeEventAllowed(changingEvent
) )
2471 SelectRange(GetHwnd(),
2472 HITEM(m_htSelStart
), HITEM(next
),
2473 SR_UNSELECT_OTHERS
);
2474 SetFocusedItem(next
);
2476 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2478 changedEvent
.m_itemOld
= htSel
;
2479 (void)HandleTreeEvent(changedEvent
);
2485 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2487 changingEvent
.m_itemOld
= htSel
;
2489 if ( IsTreeEventAllowed(changingEvent
) )
2493 SetFocusedItem(next
);
2495 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2497 changedEvent
.m_itemOld
= htSel
;
2498 (void)HandleTreeEvent(changedEvent
);
2508 wxTreeItemId firstVisible
= GetFirstVisibleItem();
2509 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2510 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2511 TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible
)) :
2512 TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible
));
2514 if ( !nextAdjacent
)
2519 wxTreeItemId nextStart
= firstVisible
;
2521 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2523 wxTreeItemId nextTemp
= (vkey
== VK_PRIOR
) ?
2524 TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart
)) :
2525 TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart
));
2527 if ( nextTemp
.IsOk() )
2529 nextStart
= nextTemp
;
2537 EnsureVisible(nextStart
);
2539 if ( vkey
== VK_NEXT
)
2541 wxTreeItemId nextEnd
= nextStart
;
2543 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2545 wxTreeItemId nextTemp
=
2546 TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd
));
2548 if ( nextTemp
.IsOk() )
2558 EnsureVisible(nextEnd
);
2563 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2564 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2565 TreeView_GetPrevVisible(GetHwnd(), htSel
) :
2566 TreeView_GetNextVisible(GetHwnd(), htSel
);
2568 if ( !nextAdjacent
)
2573 wxTreeItemId
next(htSel
);
2575 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2577 wxTreeItemId nextTemp
= vkey
== VK_PRIOR
?
2578 TreeView_GetPrevVisible(GetHwnd(), HITEM(next
)) :
2579 TreeView_GetNextVisible(GetHwnd(), HITEM(next
));
2581 if ( !nextTemp
.IsOk() )
2587 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2589 changingEvent
.m_itemOld
= htSel
;
2591 if ( IsTreeEventAllowed(changingEvent
) )
2594 m_htSelStart
.Unset();
2596 SetFocusedItem(next
);
2598 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2600 changedEvent
.m_itemOld
= htSel
;
2601 (void)HandleTreeEvent(changedEvent
);
2613 bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam
, WXLPARAM lParam
)
2615 wxTreeEvent
keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2616 keyEvent
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
, wParam
, lParam
);
2618 bool processed
= HandleTreeEvent(keyEvent
);
2620 // generate a separate event for Space/Return
2621 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
2622 ((wParam
== VK_SPACE
) || (wParam
== VK_RETURN
)) )
2624 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2627 wxTreeEvent
activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2629 (void)HandleTreeEvent(activatedEvent
);
2636 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2637 // only do it during dragging, minimize wxWin overhead (this is important for
2638 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2639 // instead of passing by wxWin events
2641 wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2643 bool processed
= false;
2645 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2647 if ( nMsg
== WM_CONTEXTMENU
)
2649 int x
= GET_X_LPARAM(lParam
),
2650 y
= GET_Y_LPARAM(lParam
);
2652 // the item for which the menu should be shown
2655 // the position where the menu should be shown in client coordinates
2656 // (so that it can be passed directly to PopupMenu())
2659 if ( x
== -1 || y
== -1 )
2661 // this means that the event was generated from keyboard (e.g. with
2662 // Shift-F10 or special Windows menu key)
2664 // use the Explorer standard of putting the menu at the left edge
2665 // of the text, in the vertical middle of the text
2666 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2669 // Use the bounding rectangle of only the text part
2671 GetBoundingRect(item
, rect
, true);
2672 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
2675 else // event from mouse, use mouse position
2677 pt
= ScreenToClient(wxPoint(x
, y
));
2679 TV_HITTESTINFO tvhti
;
2683 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2684 item
= wxTreeItemId(tvhti
.hItem
);
2690 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2692 event
.m_pointDrag
= pt
;
2694 if ( HandleTreeEvent(event
) )
2696 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2699 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2701 // we only process mouse messages here and these parameters have the
2702 // same meaning for all of them
2703 int x
= GET_X_LPARAM(lParam
),
2704 y
= GET_Y_LPARAM(lParam
);
2706 TV_HITTESTINFO tvht
;
2710 HTREEITEM htOldItem
= TreeView_GetSelection(GetHwnd());
2711 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2715 case WM_LBUTTONDOWN
:
2719 m_htClickedItem
.Unset();
2721 if ( !MSWIsOnItem(tvht
.flags
) )
2723 if ( tvht
.flags
& TVHT_ONITEMBUTTON
)
2725 // either it's going to be handled by user code or
2726 // we're going to use it ourselves to toggle the
2727 // branch, in either case don't pass it to the base
2728 // class which would generate another mouse click event
2729 // for it even though it's already handled here
2733 if ( !HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2735 if ( !IsExpanded(htItem
) )
2746 m_focusLost
= false;
2752 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2753 m_ptClick
= wxPoint(x
, y
);
2755 if ( wParam
& MK_CONTROL
)
2757 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2759 m_htClickedItem
.Unset();
2763 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2765 changingEvent
.m_itemOld
= htOldItem
;
2767 if ( IsTreeEventAllowed(changingEvent
) )
2769 // toggle selected state
2770 DoToggleItemSelection(wxTreeItemId(htItem
));
2772 SetFocusedItem(wxTreeItemId(htItem
));
2774 // reset on any click without Shift
2775 m_htSelStart
.Unset();
2777 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2779 changedEvent
.m_itemOld
= htOldItem
;
2780 (void)HandleTreeEvent(changedEvent
);
2783 else if ( wParam
& MK_SHIFT
)
2785 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2787 m_htClickedItem
.Unset();
2792 bool willChange
= true;
2794 if ( !(wParam
& MK_CONTROL
) )
2796 srFlags
|= SR_UNSELECT_OTHERS
;
2799 if ( !m_htSelStart
)
2801 // take the focused item
2802 m_htSelStart
= htOldItem
;
2806 willChange
= SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2807 htItem
, srFlags
| SR_SIMULATE
);
2812 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2814 changingEvent
.m_itemOld
= htOldItem
;
2816 if ( IsTreeEventAllowed(changingEvent
) )
2818 // this selects all items between the starting one
2822 SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2827 DoSelectItem(wxTreeItemId(htItem
));
2830 SetFocusedItem(wxTreeItemId(htItem
));
2832 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2834 changedEvent
.m_itemOld
= htOldItem
;
2835 (void)HandleTreeEvent(changedEvent
);
2839 else // normal click
2841 // avoid doing anything if we click on the only
2842 // currently selected item
2844 wxArrayTreeItemIds selections
;
2845 size_t count
= GetSelections(selections
);
2849 HITEM(selections
[0]) != htItem
)
2851 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2853 m_htClickedItem
.Unset();
2857 // clear the previously selected items, if the user
2858 // clicked outside of the present selection, otherwise,
2859 // perform the deselection on mouse-up, this allows
2860 // multiple drag and drop to work.
2861 if ( !IsItemSelected(GetHwnd(), htItem
))
2863 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2865 changingEvent
.m_itemOld
= htOldItem
;
2867 if ( IsTreeEventAllowed(changingEvent
) )
2870 DoSelectItem(wxTreeItemId(htItem
));
2871 SetFocusedItem(wxTreeItemId(htItem
));
2873 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2875 changedEvent
.m_itemOld
= htOldItem
;
2876 (void)HandleTreeEvent(changedEvent
);
2881 SetFocusedItem(wxTreeItemId(htItem
));
2882 m_mouseUpDeselect
= true;
2885 else // click on a single selected item
2887 // don't interfere with the default processing in
2888 // WM_MOUSEMOVE handler below as the default window
2889 // proc will start the drag itself if we let have
2891 m_htClickedItem
.Unset();
2893 // prevent in-place editing from starting if focus lost
2894 // since previous click
2898 DoSelectItem(wxTreeItemId(htItem
));
2899 SetFocusedItem(wxTreeItemId(htItem
));
2907 // reset on any click without Shift
2908 m_htSelStart
.Unset();
2911 m_focusLost
= false;
2913 // we consumed the event so we need to trigger state image
2918 wxTreeItemId item
= HitTest(wxPoint(x
, y
), htFlags
);
2920 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2922 m_triggerStateImageClick
= true;
2927 case WM_RBUTTONDOWN
:
2934 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) || !htItem
)
2939 // default handler removes the highlight from the currently
2940 // focused item when right mouse button is pressed on another
2941 // one but keeps the remaining items highlighted, which is
2942 // confusing, so override this default behaviour
2943 if ( !IsItemSelected(GetHwnd(), htItem
) )
2945 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2947 changingEvent
.m_itemOld
= htOldItem
;
2949 if ( IsTreeEventAllowed(changingEvent
) )
2952 DoSelectItem(wxTreeItemId(htItem
));
2953 SetFocusedItem(wxTreeItemId(htItem
));
2955 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2957 changedEvent
.m_itemOld
= htOldItem
;
2958 (void)HandleTreeEvent(changedEvent
);
2966 if ( m_htClickedItem
)
2968 int cx
= abs(m_ptClick
.x
- x
);
2969 int cy
= abs(m_ptClick
.y
- y
);
2971 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2972 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2977 tv
.hdr
.hwndFrom
= GetHwnd();
2978 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2979 tv
.hdr
.code
= TVN_BEGINDRAG
;
2981 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2985 wxZeroMemory(tviAux
);
2987 tviAux
.hItem
= HITEM(m_htClickedItem
);
2988 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2989 tviAux
.stateMask
= 0xffffffff;
2990 TreeView_GetItem(GetHwnd(), &tviAux
);
2992 tv
.itemNew
.state
= tviAux
.state
;
2993 tv
.itemNew
.lParam
= tviAux
.lParam
;
2998 // do it before SendMessage() call below to avoid
2999 // reentrancies here if there is another WM_MOUSEMOVE
3000 // in the queue already
3001 m_htClickedItem
.Unset();
3003 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
3004 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
3006 // don't pass it to the default window proc, it would
3007 // start dragging again
3011 #endif // __WXWINCE__
3016 m_dragImage
->Move(wxPoint(x
, y
));
3019 // highlight the item as target (hiding drag image is
3020 // necessary - otherwise the display will be corrupted)
3021 m_dragImage
->Hide();
3022 TreeView_SelectDropTarget(GetHwnd(), htItem
);
3023 m_dragImage
->Show();
3026 #endif // wxUSE_DRAGIMAGE
3032 // deselect other items if needed
3035 if ( m_mouseUpDeselect
)
3037 m_mouseUpDeselect
= false;
3039 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
3041 changingEvent
.m_itemOld
= htOldItem
;
3043 if ( IsTreeEventAllowed(changingEvent
) )
3046 DoSelectItem(wxTreeItemId(htItem
));
3047 SetFocusedItem(wxTreeItemId(htItem
));
3049 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
3051 changedEvent
.m_itemOld
= htOldItem
;
3052 (void)HandleTreeEvent(changedEvent
);
3057 m_htClickedItem
.Unset();
3059 if ( m_triggerStateImageClick
)
3061 if ( tvht
.flags
& TVHT_ONITEMSTATEICON
)
3063 wxTreeEvent
event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
,
3065 (void)HandleTreeEvent(event
);
3067 m_triggerStateImageClick
= false;
3072 if ( !m_dragStarted
&& MSWIsOnItem(tvht
.flags
) )
3084 m_dragImage
->EndDrag();
3085 wxDELETE(m_dragImage
);
3087 // generate the drag end event
3088 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
,
3090 event
.m_pointDrag
= wxPoint(x
, y
);
3091 (void)HandleTreeEvent(event
);
3093 // if we don't do it, the tree seems to think that 2 items
3094 // are selected simultaneously which is quite weird
3095 TreeView_SelectDropTarget(GetHwnd(), 0);
3097 #endif // wxUSE_DRAGIMAGE
3099 if ( isMultiple
&& nMsg
== WM_RBUTTONUP
)
3103 nmhdr
.hwndFrom
= GetHwnd();
3104 nmhdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
3105 nmhdr
.code
= NM_RCLICK
;
3106 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY
,
3107 nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
3111 m_dragStarted
= false;
3116 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) )
3120 // the tree control greys out the selected item when it loses focus
3121 // and paints it as selected again when it regains it, but it won't
3122 // do it for the other items itself - help it
3123 wxArrayTreeItemIds selections
;
3124 size_t count
= GetSelections(selections
);
3125 TVGetItemRectParam param
;
3127 for ( size_t n
= 0; n
< count
; n
++ )
3129 // TreeView_GetItemRect() will return false if item is not
3130 // visible, which may happen perfectly well
3131 if ( wxTreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
3134 ::InvalidateRect(GetHwnd(), ¶m
.rect
, FALSE
);
3139 if ( nMsg
== WM_KILLFOCUS
)
3144 else if ( (nMsg
== WM_KEYDOWN
|| nMsg
== WM_SYSKEYDOWN
) && isMultiple
)
3146 // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
3147 // notification but for the keys which can be used to change selection
3148 // we need to do it from here so as to not apply the default behaviour
3149 // if the events are handled by the user code
3162 if ( !HandleKeyDown(wParam
, lParam
) &&
3163 !MSWHandleTreeKeyDownEvent(wParam
, lParam
) )
3165 // use the key to update the selection if it was left
3167 MSWHandleSelectionKey(wParam
);
3170 // pretend that we did process it in any case as we already
3171 // generated an event for it
3174 //default: for all the other keys leave processed as false so that
3175 // the tree control generates a TVN_KEYDOWN for us
3179 else if ( nMsg
== WM_COMMAND
)
3181 // if we receive a EN_KILLFOCUS command from the in-place edit control
3182 // used for label editing, make sure to end editing
3185 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
3187 if ( cmd
== EN_KILLFOCUS
)
3189 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
3199 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
3205 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
3207 if ( nMsg
== WM_CHAR
)
3209 // don't let the control process Space and Return keys because it
3210 // doesn't do anything useful with them anyhow but always beeps
3211 // annoyingly when it receives them and there is no way to turn it off
3212 // simply if you just process TREEITEM_ACTIVATED event to which Space
3213 // and Enter presses are mapped in your code
3214 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
3218 else if ( nMsg
== WM_KEYDOWN
)
3220 if ( wParam
== VK_ESCAPE
)
3224 m_dragImage
->EndDrag();
3225 wxDELETE(m_dragImage
);
3227 // if we don't do it, the tree seems to think that 2 items
3228 // are selected simultaneously which is quite weird
3229 TreeView_SelectDropTarget(GetHwnd(), 0);
3233 #endif // wxUSE_DRAGIMAGE
3235 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
3238 // process WM_NOTIFY Windows message
3239 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
3241 wxTreeEvent
event(wxEVT_NULL
, this);
3242 wxEventType eventType
= wxEVT_NULL
;
3243 NMHDR
*hdr
= (NMHDR
*)lParam
;
3245 switch ( hdr
->code
)
3248 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3251 case TVN_BEGINRDRAG
:
3253 if ( eventType
== wxEVT_NULL
)
3254 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
3255 //else: left drag, already set above
3257 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3259 event
.m_item
= tv
->itemNew
.hItem
;
3260 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
3262 // don't allow dragging by default: the user code must
3263 // explicitly say that it wants to allow it to avoid breaking
3269 case TVN_BEGINLABELEDIT
:
3271 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
3272 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3274 // although the user event handler may still veto it, it is
3275 // important to set it now so that calls to SetItemText() from
3276 // the event handler would change the text controls contents
3278 event
.m_item
= info
->item
.hItem
;
3279 event
.m_label
= info
->item
.pszText
;
3280 event
.m_editCancelled
= false;
3284 case TVN_DELETEITEM
:
3286 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
3287 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3289 event
.m_item
= tv
->itemOld
.hItem
;
3293 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
3294 if ( it
!= m_attrs
.end() )
3303 case TVN_ENDLABELEDIT
:
3305 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
3306 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3308 event
.m_item
= info
->item
.hItem
;
3309 event
.m_label
= info
->item
.pszText
;
3310 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
3315 // These *must* not be removed or TVN_GETINFOTIP will
3316 // not be processed each time the mouse is moved
3317 // and the tooltip will only ever update once.
3326 #ifdef TVN_GETINFOTIP
3327 case TVN_GETINFOTIP
:
3329 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
3330 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
3332 // Which item are we trying to get a tooltip for?
3333 event
.m_item
= info
->hItem
;
3337 #endif // TVN_GETINFOTIP
3338 #endif // !__WXWINCE__
3340 case TVN_GETDISPINFO
:
3341 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
3344 case TVN_SETDISPINFO
:
3346 if ( eventType
== wxEVT_NULL
)
3347 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
3348 //else: get, already set above
3350 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3352 event
.m_item
= info
->item
.hItem
;
3356 case TVN_ITEMEXPANDING
:
3357 case TVN_ITEMEXPANDED
:
3359 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3362 switch ( tv
->action
)
3365 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
3373 what
= IDX_COLLAPSE
;
3377 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
3380 eventType
= gs_expandEvents
[what
][how
];
3382 event
.m_item
= tv
->itemNew
.hItem
;
3388 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
3390 // fabricate the lParam and wParam parameters sufficiently
3391 // similar to the ones from a "real" WM_KEYDOWN so that
3392 // CreateKeyEvent() works correctly
3393 return MSWHandleTreeKeyDownEvent(
3394 info
->wVKey
, (wxIsAltDown() ? KF_ALTDOWN
: 0) << 16);
3398 // Vista's tree control has introduced some problems with our
3399 // multi-selection tree. When TreeView_SelectItem() is called,
3400 // the wrong items are deselected.
3402 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3403 // that can be used to regulate this incorrect behaviour. The
3404 // following messages will allow only the unlocked item's selection
3407 case TVN_ITEMCHANGINGA
:
3408 case TVN_ITEMCHANGINGW
:
3410 // we only need to handles these in multi-select trees
3411 if ( HasFlag(wxTR_MULTIPLE
) )
3413 // get info about the item about to be changed
3414 NMTVITEMCHANGE
* info
= (NMTVITEMCHANGE
*)lParam
;
3415 if (TreeItemUnlocker::IsLocked(info
->hItem
))
3417 // item's state is locked, don't allow the change
3418 // returning 1 will disallow the change
3424 // allow the state change
3428 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
3429 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3430 // we have to handle both messages:
3431 case TVN_SELCHANGEDA
:
3432 case TVN_SELCHANGEDW
:
3433 if ( !m_changingSelection
)
3435 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
3439 case TVN_SELCHANGINGA
:
3440 case TVN_SELCHANGINGW
:
3441 if ( !m_changingSelection
)
3443 if ( eventType
== wxEVT_NULL
)
3444 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
3445 //else: already set above
3447 if (hdr
->code
== TVN_SELCHANGINGW
||
3448 hdr
->code
== TVN_SELCHANGEDW
)
3450 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
3451 event
.m_item
= tv
->itemNew
.hItem
;
3452 event
.m_itemOld
= tv
->itemOld
.hItem
;
3456 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
3457 event
.m_item
= tv
->itemNew
.hItem
;
3458 event
.m_itemOld
= tv
->itemOld
.hItem
;
3462 // we receive this message from WM_LBUTTONDOWN handler inside
3463 // comctl32.dll and so before the click is passed to
3464 // DefWindowProc() which sets the focus to the window which was
3465 // clicked and this can lead to unexpected event sequences: for
3466 // example, we may get a "selection change" event from the tree
3467 // before getting a "kill focus" event for the text control which
3468 // had the focus previously, thus breaking user code doing input
3471 // to avoid such surprises, we force the generation of focus events
3472 // now, before we generate the selection change ones
3473 if ( !m_changingSelection
&& !m_isBeingDeleted
)
3477 // instead of explicitly checking for _WIN32_IE, check if the
3478 // required symbols are available in the headers
3479 #if defined(CDDS_PREPAINT)
3482 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
3483 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
3484 switch ( nmcd
.dwDrawStage
)
3487 // if we've got any items with non standard attributes,
3488 // notify us before painting each item
3489 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
3492 // windows in TreeCtrl use one-based index for item state images,
3493 // 0 indexed image is not being used, we're using zero-based index,
3494 // so we have to add temp image (of zero index) to state image list
3495 // before we draw any item, then after items are drawn we have to
3496 // delete it (in POSTPAINT notify)
3497 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3499 typedef BOOL (wxSTDCALL
*ImageList_Copy_t
)
3500 (HIMAGELIST
, int, HIMAGELIST
, int, UINT
);
3501 static ImageList_Copy_t s_pfnImageList_Copy
= NULL
;
3502 static bool loaded
= false;
3506 wxLoadedDLL
dllComCtl32(wxT("comctl32.dll"));
3507 if ( dllComCtl32
.IsLoaded() )
3508 wxDL_INIT_FUNC(s_pfn
, ImageList_Copy
, dllComCtl32
);
3511 if ( !s_pfnImageList_Copy
)
3513 // this code is broken with ImageList_Copy()
3514 // but I don't care enough about Win95 support
3515 // to write it now -- if anybody does, please
3517 wxFAIL_MSG("TODO: implement this for Win95");
3522 hImageList
= GetHimagelistOf(m_imageListState
);
3524 // add temporary image
3526 m_imageListState
->GetSize(0, width
, height
);
3528 HBITMAP hbmpTemp
= ::CreateBitmap(width
, height
, 1, 1, NULL
);
3529 int index
= ::ImageList_Add(hImageList
, hbmpTemp
, hbmpTemp
);
3530 ::DeleteObject(hbmpTemp
);
3534 // move images to right
3535 for ( int i
= index
; i
> 0; i
-- )
3537 (*s_pfnImageList_Copy
)(hImageList
, i
,
3542 // we must remove the image in POSTPAINT notify
3543 *result
|= CDRF_NOTIFYPOSTPAINT
;
3548 case CDDS_POSTPAINT
:
3549 // we are deleting temp image of 0 index, which was
3550 // added before items were drawn (in PREPAINT notify)
3551 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3552 m_imageListState
->Remove(0);
3555 case CDDS_ITEMPREPAINT
:
3557 wxMapTreeAttr::iterator
3558 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
3560 if ( it
== m_attrs
.end() )
3562 // nothing to do for this item
3563 *result
= CDRF_DODEFAULT
;
3567 wxTreeItemAttr
* const attr
= it
->second
;
3569 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
3570 TVIF_STATE
, TVIS_DROPHILITED
);
3572 const UINT tvItemState
= tvItem
.state
;
3574 // selection colours should override ours,
3575 // otherwise it is too confusing to the user
3576 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
3577 !(tvItemState
& TVIS_DROPHILITED
) )
3580 if ( attr
->HasBackgroundColour() )
3582 colBack
= attr
->GetBackgroundColour();
3583 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
3587 // but we still want to keep the special foreground
3588 // colour when we don't have focus (we can't keep
3589 // it when we do, it would usually be unreadable on
3590 // the almost inverted bg colour...)
3591 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
3592 FindFocus() != this ) &&
3593 !(tvItemState
& TVIS_DROPHILITED
) )
3596 if ( attr
->HasTextColour() )
3598 colText
= attr
->GetTextColour();
3599 lptvcd
->clrText
= wxColourToRGB(colText
);
3603 if ( attr
->HasFont() )
3605 HFONT hFont
= GetHfontOf(attr
->GetFont());
3607 ::SelectObject(nmcd
.hdc
, hFont
);
3609 *result
= CDRF_NEWFONT
;
3611 else // no specific font
3613 *result
= CDRF_DODEFAULT
;
3619 *result
= CDRF_DODEFAULT
;
3623 // we always process it
3625 #endif // have owner drawn support in headers
3629 DWORD pos
= GetMessagePos();
3631 point
.x
= LOWORD(pos
);
3632 point
.y
= HIWORD(pos
);
3633 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
3635 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), htFlags
);
3637 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3639 event
.m_item
= item
;
3640 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
3649 TV_HITTESTINFO tvhti
;
3650 wxGetCursorPosMSW(&tvhti
.pt
);
3651 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
3652 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
3654 if ( MSWIsOnItem(tvhti
.flags
) )
3656 event
.m_item
= tvhti
.hItem
;
3657 eventType
= (int)hdr
->code
== NM_DBLCLK
3658 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
3659 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
3661 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
3662 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
3671 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
3674 event
.SetEventType(eventType
);
3676 bool processed
= HandleTreeEvent(event
);
3679 switch ( hdr
->code
)
3682 // we translate NM_DBLCLK into ACTIVATED event and if the user
3683 // handled the activation of the item we shouldn't proceed with
3684 // also using the same double click for toggling the item expanded
3685 // state -- but OTOH do let the user to expand/collapse the item by
3686 // double clicking on it if the activation is not handled specially
3687 *result
= processed
;
3691 // prevent tree control from sending WM_CONTEXTMENU to our parent
3692 // (which it does if NM_RCLICK is not handled) because we want to
3693 // send it to the control itself
3697 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
3698 (WPARAM
)GetHwnd(), ::GetMessagePos());
3702 case TVN_BEGINRDRAG
:
3704 if ( event
.IsAllowed() )
3706 // normally this is impossible because the m_dragImage is
3707 // deleted once the drag operation is over
3708 wxASSERT_MSG( !m_dragImage
, wxT("starting to drag once again?") );
3710 m_dragImage
= new wxDragImage(*this, event
.m_item
);
3711 m_dragImage
->BeginDrag(wxPoint(0,0), this);
3712 m_dragImage
->Show();
3714 m_dragStarted
= true;
3716 #endif // wxUSE_DRAGIMAGE
3719 case TVN_DELETEITEM
:
3721 // NB: we might process this message using wxWidgets event
3722 // tables, but due to overhead of wxWin event system we
3723 // prefer to do it here ourself (otherwise deleting a tree
3724 // with many items is just too slow)
3725 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3727 wxTreeItemParam
*param
=
3728 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
3731 processed
= true; // Make sure we don't get called twice
3735 case TVN_BEGINLABELEDIT
:
3736 // return true to cancel label editing
3737 *result
= !event
.IsAllowed();
3739 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3740 if ( event
.IsAllowed() )
3742 HWND hText
= TreeView_GetEditControl(GetHwnd());
3745 // MBN: if m_textCtrl already has an HWND, it is a stale
3746 // pointer from a previous edit (because the user
3747 // didn't modify the label before dismissing the control,
3748 // and TVN_ENDLABELEDIT was not sent), so delete it
3749 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
3752 m_textCtrl
= new wxTextCtrl();
3753 m_textCtrl
->SetParent(this);
3754 m_textCtrl
->SetHWND((WXHWND
)hText
);
3755 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3757 // set wxTE_PROCESS_ENTER style for the text control to
3758 // force it to process the Enter presses itself, otherwise
3759 // they could be stolen from it by the dialog
3761 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3762 | wxTE_PROCESS_ENTER
);
3765 else // we had set m_idEdited before
3771 case TVN_ENDLABELEDIT
:
3772 // return true to set the label to the new string: note that we
3773 // also must pretend that we did process the message or it is going
3774 // to be passed to DefWindowProc() which will happily return false
3775 // cancelling the label change
3776 *result
= event
.IsAllowed();
3779 // ensure that we don't have the text ctrl which is going to be
3785 #ifdef TVN_GETINFOTIP
3786 case TVN_GETINFOTIP
:
3788 // If the user permitted a tooltip change, change it
3789 if (event
.IsAllowed())
3791 SetToolTip(event
.m_label
);
3798 case TVN_SELCHANGING
:
3799 case TVN_ITEMEXPANDING
:
3800 // return true to prevent the action from happening
3801 *result
= !event
.IsAllowed();
3804 case TVN_ITEMEXPANDED
:
3806 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3807 const wxTreeItemId
id(tv
->itemNew
.hItem
);
3809 if ( tv
->action
== TVE_COLLAPSE
)
3811 if ( wxApp::GetComCtl32Version() >= 600 )
3813 // for some reason the item selection rectangle depends
3814 // on whether it is expanded or collapsed (at least
3815 // with comctl32.dll v6): it is wider (by 3 pixels) in
3816 // the expanded state, so when the item collapses and
3817 // then is deselected the rightmost 3 pixels of the
3818 // previously drawn selection are left on the screen
3820 // it's not clear if it's a bug in comctl32.dll or in
3821 // our code (because it does not happen in Explorer but
3822 // OTOH we don't do anything which could result in this
3823 // AFAICS) but we do need to work around it to avoid
3830 // the item is also not refreshed properly after expansion when
3831 // it has an image depending on the expanded/collapsed state:
3832 // again, it's not clear if the bug is in comctl32.dll or our
3834 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3843 case TVN_GETDISPINFO
:
3844 // NB: so far the user can't set the image himself anyhow, so do it
3845 // anyway - but this may change later
3846 //if ( /* !processed && */ )
3848 wxTreeItemId item
= event
.m_item
;
3849 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3851 const wxTreeItemParam
* const param
= GetItemParam(item
);
3855 if ( info
->item
.mask
& TVIF_IMAGE
)
3860 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3861 : wxTreeItemIcon_Normal
3864 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3866 info
->item
.iSelectedImage
=
3869 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3870 : wxTreeItemIcon_Selected
3877 // for the other messages the return value is ignored and there is
3878 // nothing special to do
3883 // ----------------------------------------------------------------------------
3885 // ----------------------------------------------------------------------------
3887 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3888 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3890 int wxTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
3892 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
3894 // receive the desired information
3895 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3898 // state images are one-based
3899 return STATEIMAGEMASKTOINDEX(tvItem
.state
) - 1;
3902 void wxTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
3904 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
3906 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3908 // state images are one-based
3909 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3910 tvItem
.state
= INDEXTOSTATEIMAGEMASK(state
+ 1);
3915 #endif // wxUSE_TREECTRL