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"
46 // macros to hide the cast ugliness
47 // --------------------------------
49 // get HTREEITEM from wxTreeItemId
50 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
53 // older SDKs are missing these
54 #ifndef TVN_ITEMCHANGINGA
56 #define TVN_ITEMCHANGINGA (TVN_FIRST-16)
57 #define TVN_ITEMCHANGINGW (TVN_FIRST-17)
59 typedef struct tagNMTVITEMCHANGE
72 // this helper class is used on vista systems for preventing unwanted
73 // item state changes in the vista tree control. It is only effective in
74 // multi-select mode on vista systems.
76 // The vista tree control includes some new code that originally broke the
77 // multi-selection tree, causing seemingly spurious item selection state changes
78 // during Shift or Ctrl-click item selection. (To witness the original broken
79 // behavior, simply make IsLocked() below always return false). This problem was
80 // solved by using the following class to 'unlock' an item's selection state.
82 class TreeItemUnlocker
85 // unlock a single item
86 TreeItemUnlocker(HTREEITEM item
) { ms_unlockedItem
= item
; }
88 // unlock all items, don't use unless absolutely necessary
89 TreeItemUnlocker() { ms_unlockedItem
= (HTREEITEM
)-1; }
91 // lock everything back
92 ~TreeItemUnlocker() { ms_unlockedItem
= NULL
; }
95 // check if the item state is currently locked
96 static bool IsLocked(HTREEITEM item
)
97 { return ms_unlockedItem
!= (HTREEITEM
)-1 && item
!= ms_unlockedItem
; }
100 static HTREEITEM ms_unlockedItem
;
103 HTREEITEM TreeItemUnlocker
::ms_unlockedItem
= NULL
;
105 // another helper class: set the variable to true during its lifetime and reset
106 // it to false when it is destroyed
108 // it is currently always used with wxTreeCtrl::m_changingSelection
112 TempSetter(bool& var
) : m_var(var
)
114 wxASSERT_MSG( !m_var
, "variable shouldn't be already set" );
126 wxDECLARE_NO_COPY_CLASS(TempSetter
);
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 // wrappers for TreeView_GetItem/TreeView_SetItem
134 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
137 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
138 tvi
.stateMask
= TVIS_SELECTED
;
141 TreeItemUnlocker
unlocker(hItem
);
143 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
145 wxLogLastError(wxT("TreeView_GetItem"));
148 return (tvi
.state
& TVIS_SELECTED
) != 0;
151 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
154 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
155 tvi
.stateMask
= TVIS_SELECTED
;
156 tvi
.state
= select ? TVIS_SELECTED
: 0;
159 TreeItemUnlocker
unlocker(hItem
);
161 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
163 wxLogLastError(wxT("TreeView_SetItem"));
170 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
172 SelectItem(hwndTV
, htItem
, false);
175 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
177 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
180 // helper function which selects all items in a range and, optionally,
181 // deselects all the other ones
183 // returns true if the selection changed at all or false if nothing changed
185 // flags for SelectRange()
188 SR_SIMULATE
= 1, // don't do anything, just return true or false
189 SR_UNSELECT_OTHERS
= 2 // deselect the items not in range
192 static bool SelectRange(HWND hwndTV
,
197 // find the first (or last) item and select it
198 bool changed
= false;
200 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
202 while ( htItem
&& cont
)
204 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
206 if ( !IsItemSelected(hwndTV
, htItem
) )
208 if ( !(flags
& SR_SIMULATE
) )
210 SelectItem(hwndTV
, htItem
);
218 else // not first or last
220 if ( flags
& SR_UNSELECT_OTHERS
)
222 if ( IsItemSelected(hwndTV
, htItem
) )
224 if ( !(flags
& SR_SIMULATE
) )
225 UnselectItem(hwndTV
, htItem
);
232 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
235 // select the items in range
236 cont
= htFirst
!= htLast
;
237 while ( htItem
&& cont
)
239 if ( !IsItemSelected(hwndTV
, htItem
) )
241 if ( !(flags
& SR_SIMULATE
) )
243 SelectItem(hwndTV
, htItem
);
249 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
251 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
254 // optionally deselect the rest
255 if ( flags
& SR_UNSELECT_OTHERS
)
259 if ( IsItemSelected(hwndTV
, htItem
) )
261 if ( !(flags
& SR_SIMULATE
) )
263 UnselectItem(hwndTV
, htItem
);
269 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
273 // seems to be necessary - otherwise the just selected items don't always
274 // appear as selected
275 if ( !(flags
& SR_SIMULATE
) )
277 UpdateWindow(hwndTV
);
283 // helper function which tricks the standard control into changing the focused
284 // item without changing anything else (if someone knows why Microsoft doesn't
285 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
287 // returns true if the focus was changed, false if the given item was already
289 static bool SetFocus(HWND hwndTV
, HTREEITEM htItem
)
292 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
294 if ( htItem
== htFocus
)
299 // remember the selection state of the item
300 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
302 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
304 // prevent the tree from unselecting the old focus which it
305 // would do by default (TreeView_SelectItem unselects the
307 TreeView_SelectItem(hwndTV
, 0);
308 SelectItem(hwndTV
, htFocus
);
311 TreeView_SelectItem(hwndTV
, htItem
);
315 // need to clear the selection which TreeView_SelectItem() gave
317 UnselectItem(hwndTV
, htItem
);
319 //else: was selected, still selected - ok
323 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
325 // just clear the focus
326 TreeView_SelectItem(hwndTV
, 0);
328 if ( wasFocusSelected
)
330 // restore the selection state
331 SelectItem(hwndTV
, htFocus
);
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
342 // a convenient wrapper around TV_ITEM struct which adds a ctor
344 #pragma warning( disable : 4097 ) // inheriting from typedef
347 struct wxTreeViewItem
: public TV_ITEM
349 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
350 UINT mask_
, // fields which are valid
351 UINT stateMask_
= 0) // for TVIF_STATE only
355 // hItem member is always valid
356 mask
= mask_
| TVIF_HANDLE
;
357 stateMask
= stateMask_
;
362 // ----------------------------------------------------------------------------
363 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
365 // We need this for a couple of reasons:
367 // 1) This class is needed for support of different images: the Win32 common
368 // control natively supports only 2 images (the normal one and another for the
369 // selected state). We wish to provide support for 2 more of them for folder
370 // items (i.e. those which have children): for expanded state and for expanded
371 // selected state. For this we use this structure to store the additional items
374 // 2) This class is also needed to hold the HITEM so that we can sort
375 // it correctly in the MSW sort callback.
377 // In addition it makes other workarounds such as this easier and helps
378 // simplify the code.
379 // ----------------------------------------------------------------------------
381 class wxTreeItemParam
388 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
394 // dtor deletes the associated data as well
395 virtual ~wxTreeItemParam() { delete m_data
; }
398 // get the real data associated with the item
399 wxTreeItemData
*GetData() const { return m_data
; }
401 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
403 // do we have such image?
404 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
405 // get image, falling back to the other images if this one is not
407 int GetImage(wxTreeItemIcon which
) const
409 int image
= m_images
[which
];
414 case wxTreeItemIcon_SelectedExpanded
:
415 image
= GetImage(wxTreeItemIcon_Expanded
);
420 case wxTreeItemIcon_Selected
:
421 case wxTreeItemIcon_Expanded
:
422 image
= GetImage(wxTreeItemIcon_Normal
);
425 case wxTreeItemIcon_Normal
:
430 wxFAIL_MSG( wxT("unsupported wxTreeItemIcon value") );
436 // change the given image
437 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
440 const wxTreeItemId
& GetItem() const { return m_item
; }
442 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
445 // all the images associated with the item
446 int m_images
[wxTreeItemIcon_Max
];
448 // item for sort callbacks
451 // the real client data
452 wxTreeItemData
*m_data
;
454 wxDECLARE_NO_COPY_CLASS(wxTreeItemParam
);
457 // wxVirutalNode is used in place of a single root when 'hidden' root is
459 class wxVirtualNode
: public wxTreeViewItem
462 wxVirtualNode(wxTreeItemParam
*param
)
463 : wxTreeViewItem(TVI_ROOT
, 0)
473 wxTreeItemParam
*GetParam() const { return m_param
; }
474 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
477 wxTreeItemParam
*m_param
;
479 wxDECLARE_NO_COPY_CLASS(wxVirtualNode
);
483 #pragma warning( default : 4097 )
486 // a macro to get the virtual root, returns NULL if none
487 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
489 // returns true if the item is the virtual root
490 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
492 // a class which encapsulates the tree traversal logic: it vists all (unless
493 // OnVisit() returns false) items under the given one
494 class wxTreeTraversal
497 wxTreeTraversal(const wxTreeCtrl
*tree
)
502 // give it a virtual dtor: not really needed as the class is never used
503 // polymorphically and not even allocated on heap at all, but this is safer
504 // (in case it ever is) and silences the compiler warnings for now
505 virtual ~wxTreeTraversal() { }
507 // do traverse the tree: visit all items (recursively by default) under the
508 // given one; return true if all items were traversed or false if the
509 // traversal was aborted because OnVisit returned false
510 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
512 // override this function to do whatever is needed for each item, return
513 // false to stop traversing
514 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
517 const wxTreeCtrl
*GetTree() const { return m_tree
; }
520 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
522 const wxTreeCtrl
*m_tree
;
524 wxDECLARE_NO_COPY_CLASS(wxTreeTraversal
);
527 // internal class for getting the selected items
528 class TraverseSelections
: public wxTreeTraversal
531 TraverseSelections(const wxTreeCtrl
*tree
,
532 wxArrayTreeItemIds
& selections
)
533 : wxTreeTraversal(tree
), m_selections(selections
)
535 m_selections
.Empty();
537 if (tree
->GetCount() > 0)
538 DoTraverse(tree
->GetRootItem());
541 virtual bool OnVisit(const wxTreeItemId
& item
)
543 const wxTreeCtrl
* const tree
= GetTree();
545 // can't visit a virtual node.
546 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
551 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
553 m_selections
.Add(item
);
559 size_t GetCount() const { return m_selections
.GetCount(); }
562 wxArrayTreeItemIds
& m_selections
;
564 wxDECLARE_NO_COPY_CLASS(TraverseSelections
);
567 // internal class for counting tree items
568 class TraverseCounter
: public wxTreeTraversal
571 TraverseCounter(const wxTreeCtrl
*tree
,
572 const wxTreeItemId
& root
,
574 : wxTreeTraversal(tree
)
578 DoTraverse(root
, recursively
);
581 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
588 size_t GetCount() const { return m_count
; }
593 wxDECLARE_NO_COPY_CLASS(TraverseCounter
);
596 // ----------------------------------------------------------------------------
598 // ----------------------------------------------------------------------------
600 // ----------------------------------------------------------------------------
602 // ----------------------------------------------------------------------------
604 // indices in gs_expandEvents table below
619 // handy table for sending events - it has to be initialized during run-time
620 // now so can't be const any more
621 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
624 but logically it's a const table with the following entries:
627 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
628 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
632 // ============================================================================
634 // ============================================================================
636 // ----------------------------------------------------------------------------
638 // ----------------------------------------------------------------------------
640 bool wxTreeTraversal
::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
642 if ( !OnVisit(root
) )
645 return Traverse(root
, recursively
);
648 bool wxTreeTraversal
::Traverse(const wxTreeItemId
& root
, bool recursively
)
650 wxTreeItemIdValue cookie
;
651 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
652 while ( child
.IsOk() )
654 // depth first traversal
655 if ( recursively
&& !Traverse(child
, true) )
658 if ( !OnVisit(child
) )
661 child
= m_tree
->GetNextChild(root
, cookie
);
667 // ----------------------------------------------------------------------------
668 // construction and destruction
669 // ----------------------------------------------------------------------------
671 void wxTreeCtrl
::Init()
674 m_hasAnyAttr
= false;
678 m_pVirtualRoot
= NULL
;
679 m_dragStarted
= false;
681 m_changingSelection
= false;
682 m_triggerStateImageClick
= false;
683 m_mouseUpDeselect
= false;
685 // initialize the global array of events now as it can't be done statically
686 // with the wxEVT_XXX values being allocated during run-time only
687 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
688 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
689 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
690 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
693 bool wxTreeCtrl
::Create(wxWindow
*parent
,
698 const wxValidator
& validator
,
699 const wxString
& name
)
703 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
704 style
|= wxBORDER_SUNKEN
;
706 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
710 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
711 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
713 if ( !(m_windowStyle
& wxTR_NO_LINES
) )
714 wstyle
|= TVS_HASLINES
;
715 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
716 wstyle
|= TVS_HASBUTTONS
;
718 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
719 wstyle
|= TVS_EDITLABELS
;
721 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
722 wstyle
|= TVS_LINESATROOT
;
724 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
726 if ( wxApp
::GetComCtl32Version() >= 471 )
727 wstyle
|= TVS_FULLROWSELECT
;
730 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
731 // Need so that TVN_GETINFOTIP messages will be sent
732 wstyle
|= TVS_INFOTIP
;
735 // Create the tree control.
736 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
739 SetBackgroundColour(wxSystemSettings
::GetColour(wxSYS_COLOUR_WINDOW
));
740 SetForegroundColour(wxWindow
::GetParent()->GetForegroundColour());
742 wxSetCCUnicodeFormat(GetHwnd());
747 wxTreeCtrl
::~wxTreeCtrl()
749 // delete any attributes
752 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
754 // prevent TVN_DELETEITEM handler from deleting the attributes again!
755 m_hasAnyAttr
= false;
760 // delete user data to prevent memory leaks
761 // also deletes hidden root node storage.
765 // ----------------------------------------------------------------------------
767 // ----------------------------------------------------------------------------
769 /* static */ wxVisualAttributes
770 wxTreeCtrl
::GetClassDefaultAttributes(wxWindowVariant variant
)
772 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
774 // common controls have their own default font
775 attrs
.font
= wxGetCCDefaultFont();
781 // simple wrappers which add error checking in debug mode
783 bool wxTreeCtrl
::DoGetItem(wxTreeViewItem
*tvItem
) const
785 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
786 wxT("can't retrieve virtual root item") );
788 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
790 wxLogLastError(wxT("TreeView_GetItem"));
798 void wxTreeCtrl
::DoSetItem(wxTreeViewItem
*tvItem
)
800 TreeItemUnlocker
unlocker(tvItem
->hItem
);
802 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
804 wxLogLastError(wxT("TreeView_SetItem"));
808 unsigned int wxTreeCtrl
::GetCount() const
810 return (unsigned int)TreeView_GetCount(GetHwnd());
813 unsigned int wxTreeCtrl
::GetIndent() const
815 return TreeView_GetIndent(GetHwnd());
818 void wxTreeCtrl
::SetIndent(unsigned int indent
)
820 TreeView_SetIndent(GetHwnd(), indent
);
823 void wxTreeCtrl
::SetAnyImageList(wxImageList
*imageList
, int which
)
826 (void) TreeView_SetImageList(GetHwnd(),
827 imageList ? imageList
->GetHIMAGELIST() : 0,
831 void wxTreeCtrl
::SetImageList(wxImageList
*imageList
)
833 if (m_ownsImageListNormal
)
834 delete m_imageListNormal
;
836 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
837 m_ownsImageListNormal
= false;
840 void wxTreeCtrl
::SetStateImageList(wxImageList
*imageList
)
842 if (m_ownsImageListState
) delete m_imageListState
;
843 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
844 m_ownsImageListState
= false;
847 size_t wxTreeCtrl
::GetChildrenCount(const wxTreeItemId
& item
,
848 bool recursively
) const
850 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
852 TraverseCounter
counter(this, item
, recursively
);
853 return counter
.GetCount() - 1;
856 // ----------------------------------------------------------------------------
858 // ----------------------------------------------------------------------------
860 bool wxTreeCtrl
::SetBackgroundColour(const wxColour
&colour
)
862 if ( !wxWindowBase
::SetBackgroundColour(colour
) )
865 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
870 bool wxTreeCtrl
::SetForegroundColour(const wxColour
&colour
)
872 if ( !wxWindowBase
::SetForegroundColour(colour
) )
875 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
880 // ----------------------------------------------------------------------------
882 // ----------------------------------------------------------------------------
884 bool wxTreeCtrl
::IsHiddenRoot(const wxTreeItemId
& item
) const
886 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
889 wxString wxTreeCtrl
::GetItemText(const wxTreeItemId
& item
) const
891 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
893 wxChar buf
[512]; // the size is arbitrary...
895 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
896 tvItem
.pszText
= buf
;
897 tvItem
.cchTextMax
= WXSIZEOF(buf
);
898 if ( !DoGetItem(&tvItem
) )
900 // don't return some garbage which was on stack, but an empty string
904 return wxString(buf
);
907 void wxTreeCtrl
::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
909 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
911 if ( IS_VIRTUAL_ROOT(item
) )
914 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
915 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
918 // when setting the text of the item being edited, the text control should
919 // be updated to reflect the new text as well, otherwise calling
920 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
922 // don't use GetEditControl() here because m_textCtrl is not set yet
923 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
926 if ( item
== m_idEdited
)
928 ::SetWindowText(hwndEdit
, text
.wx_str());
933 int wxTreeCtrl
::GetItemImage(const wxTreeItemId
& item
,
934 wxTreeItemIcon which
) const
936 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
938 if ( IsHiddenRoot(item
) )
940 // no images for hidden root item
944 wxTreeItemParam
*param
= GetItemParam(item
);
946 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
949 void wxTreeCtrl
::SetItemImage(const wxTreeItemId
& item
, int image
,
950 wxTreeItemIcon which
)
952 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
953 wxCHECK_RET( which
>= 0 &&
954 which
< wxTreeItemIcon_Max
,
955 wxT("invalid image index"));
958 if ( IsHiddenRoot(item
) )
960 // no images for hidden root item
964 wxTreeItemParam
*data
= GetItemParam(item
);
968 data
->SetImage(image
, which
);
973 wxTreeItemParam
*wxTreeCtrl
::GetItemParam(const wxTreeItemId
& item
) const
975 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
977 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
979 // hidden root may still have data.
980 if ( IS_VIRTUAL_ROOT(item
) )
982 return GET_VIRTUAL_ROOT()->GetParam();
986 if ( !DoGetItem(&tvItem
) )
991 return (wxTreeItemParam
*)tvItem
.lParam
;
994 bool wxTreeCtrl
::HandleTreeEvent(wxTreeEvent
& event
) const
996 if ( event
.m_item
.IsOk() )
998 event
.SetClientObject(GetItemData(event
.m_item
));
1001 return HandleWindowEvent(event
);
1004 wxTreeItemData
*wxTreeCtrl
::GetItemData(const wxTreeItemId
& item
) const
1006 wxTreeItemParam
*data
= GetItemParam(item
);
1008 return data ? data
->GetData() : NULL
;
1011 void wxTreeCtrl
::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1013 // first, associate this piece of data with this item
1019 wxTreeItemParam
*param
= GetItemParam(item
);
1021 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1023 param
->SetData(data
);
1026 void wxTreeCtrl
::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1028 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1030 if ( IS_VIRTUAL_ROOT(item
) )
1033 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1034 tvItem
.cChildren
= (int)has
;
1038 void wxTreeCtrl
::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1040 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1042 if ( IS_VIRTUAL_ROOT(item
) )
1045 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1046 tvItem
.state
= bold ? TVIS_BOLD
: 0;
1050 void wxTreeCtrl
::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1052 if ( IS_VIRTUAL_ROOT(item
) )
1055 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1056 tvItem
.state
= highlight ? TVIS_DROPHILITED
: 0;
1060 void wxTreeCtrl
::RefreshItem(const wxTreeItemId
& item
)
1062 if ( IS_VIRTUAL_ROOT(item
) )
1066 if ( GetBoundingRect(item
, rect
) )
1072 wxColour wxTreeCtrl
::GetItemTextColour(const wxTreeItemId
& item
) const
1074 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1076 wxMapTreeAttr
::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1077 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1080 wxColour wxTreeCtrl
::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1082 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1084 wxMapTreeAttr
::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1085 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1088 wxFont wxTreeCtrl
::GetItemFont(const wxTreeItemId
& item
) const
1090 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1092 wxMapTreeAttr
::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1093 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1096 void wxTreeCtrl
::SetItemTextColour(const wxTreeItemId
& item
,
1097 const wxColour
& col
)
1099 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1101 wxTreeItemAttr
*attr
;
1102 wxMapTreeAttr
::iterator it
= m_attrs
.find(item
.m_pItem
);
1103 if ( it
== m_attrs
.end() )
1105 m_hasAnyAttr
= true;
1107 m_attrs
[item
.m_pItem
] =
1108 attr
= new wxTreeItemAttr
;
1115 attr
->SetTextColour(col
);
1120 void wxTreeCtrl
::SetItemBackgroundColour(const wxTreeItemId
& item
,
1121 const wxColour
& col
)
1123 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1125 wxTreeItemAttr
*attr
;
1126 wxMapTreeAttr
::iterator it
= m_attrs
.find(item
.m_pItem
);
1127 if ( it
== m_attrs
.end() )
1129 m_hasAnyAttr
= true;
1131 m_attrs
[item
.m_pItem
] =
1132 attr
= new wxTreeItemAttr
;
1134 else // already in the hash
1139 attr
->SetBackgroundColour(col
);
1144 void wxTreeCtrl
::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
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
;
1157 else // already in the hash
1162 attr
->SetFont(font
);
1164 // Reset the item's text to ensure that the bounding rect will be adjusted
1165 // for the new font.
1166 SetItemText(item
, GetItemText(item
));
1171 // ----------------------------------------------------------------------------
1173 // ----------------------------------------------------------------------------
1175 bool wxTreeCtrl
::IsVisible(const wxTreeItemId
& item
) const
1177 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1179 if ( item
== wxTreeItemId(TVI_ROOT
) )
1181 // virtual (hidden) root is never visible
1185 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1188 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1189 // the HTREEITEM with TVM_GETITEMRECT
1190 *(HTREEITEM
*)&rect
= HITEM(item
);
1192 // true means to get rect for just the text, not the whole line
1193 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1195 // if TVM_GETITEMRECT returned false, then the item is definitely not
1196 // visible (because its parent is not expanded)
1200 // however if it returned true, the item might still be outside the
1201 // currently visible part of the tree, test for it (notice that partly
1202 // visible means visible here)
1203 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1206 bool wxTreeCtrl
::ItemHasChildren(const wxTreeItemId
& item
) const
1208 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1210 if ( IS_VIRTUAL_ROOT(item
) )
1212 wxTreeItemIdValue cookie
;
1213 return GetFirstChild(item
, cookie
).IsOk();
1216 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1219 return tvItem
.cChildren
!= 0;
1222 bool wxTreeCtrl
::IsExpanded(const wxTreeItemId
& item
) const
1224 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1226 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1229 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1232 bool wxTreeCtrl
::IsSelected(const wxTreeItemId
& item
) const
1234 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1236 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1239 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1242 bool wxTreeCtrl
::IsBold(const wxTreeItemId
& item
) const
1244 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1246 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1249 return (tvItem
.state
& TVIS_BOLD
) != 0;
1252 // ----------------------------------------------------------------------------
1254 // ----------------------------------------------------------------------------
1256 wxTreeItemId wxTreeCtrl
::GetRootItem() const
1258 // Root may be real (visible) or virtual (hidden).
1259 if ( GET_VIRTUAL_ROOT() )
1262 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1265 wxTreeItemId wxTreeCtrl
::GetSelection() const
1267 wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE
), wxTreeItemId(),
1268 wxT("this only works with single selection controls") );
1270 return GetFocusedItem();
1273 wxTreeItemId wxTreeCtrl
::GetFocusedItem() const
1275 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1278 wxTreeItemId wxTreeCtrl
::GetItemParent(const wxTreeItemId
& item
) const
1280 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1284 if ( IS_VIRTUAL_ROOT(item
) )
1286 // no parent for the virtual root
1291 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1292 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1294 // the top level items should have the virtual root as their parent
1299 return wxTreeItemId(hItem
);
1302 wxTreeItemId wxTreeCtrl
::GetFirstChild(const wxTreeItemId
& item
,
1303 wxTreeItemIdValue
& cookie
) const
1305 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1307 // remember the last child returned in 'cookie'
1308 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1310 return wxTreeItemId(cookie
);
1313 wxTreeItemId wxTreeCtrl
::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1314 wxTreeItemIdValue
& cookie
) const
1316 wxTreeItemId
fromCookie(cookie
);
1318 HTREEITEM hitem
= HITEM(fromCookie
);
1320 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1322 wxTreeItemId
item(hitem
);
1324 cookie
= item
.m_pItem
;
1329 wxTreeItemId wxTreeCtrl
::GetLastChild(const wxTreeItemId
& item
) const
1331 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1333 // can this be done more efficiently?
1334 wxTreeItemIdValue cookie
;
1336 wxTreeItemId childLast
,
1337 child
= GetFirstChild(item
, cookie
);
1338 while ( child
.IsOk() )
1341 child
= GetNextChild(item
, cookie
);
1347 wxTreeItemId wxTreeCtrl
::GetNextSibling(const wxTreeItemId
& item
) const
1349 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1350 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1353 wxTreeItemId wxTreeCtrl
::GetPrevSibling(const wxTreeItemId
& item
) const
1355 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1356 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1359 wxTreeItemId wxTreeCtrl
::GetFirstVisibleItem() const
1361 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1364 wxTreeItemId wxTreeCtrl
::GetNextVisible(const wxTreeItemId
& item
) const
1366 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1367 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1369 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1370 if ( next
.IsOk() && !IsVisible(next
) )
1372 // Win32 considers that any non-collapsed item is visible while we want
1373 // to return only really visible items
1380 wxTreeItemId wxTreeCtrl
::GetPrevVisible(const wxTreeItemId
& item
) const
1382 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1383 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1385 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1386 if ( prev
.IsOk() && !IsVisible(prev
) )
1388 // just as above, Win32 function will happily return the previous item
1389 // in the tree for the first visible item too
1396 // ----------------------------------------------------------------------------
1397 // multiple selections emulation
1398 // ----------------------------------------------------------------------------
1400 size_t wxTreeCtrl
::GetSelections(wxArrayTreeItemIds
& selections
) const
1402 TraverseSelections
selector(this, selections
);
1404 return selector
.GetCount();
1407 // ----------------------------------------------------------------------------
1409 // ----------------------------------------------------------------------------
1411 wxTreeItemId wxTreeCtrl
::DoInsertAfter(const wxTreeItemId
& parent
,
1412 const wxTreeItemId
& hInsertAfter
,
1413 const wxString
& text
,
1414 int image
, int selectedImage
,
1415 wxTreeItemData
*data
)
1417 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1419 wxT("can't have more than one root in the tree") );
1421 TV_INSERTSTRUCT tvIns
;
1422 tvIns
.hParent
= HITEM(parent
);
1423 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1425 // this is how we insert the item as the first child: supply a NULL
1427 if ( !tvIns
.hInsertAfter
)
1429 tvIns
.hInsertAfter
= TVI_FIRST
;
1433 if ( !text
.empty() )
1436 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1440 tvIns
.item
.pszText
= NULL
;
1441 tvIns
.item
.cchTextMax
= 0;
1444 // create the param which will store the other item parameters
1445 wxTreeItemParam
*param
= new wxTreeItemParam
;
1447 // we return the images on demand as they depend on whether the item is
1448 // expanded or collapsed too in our case
1449 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1450 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1451 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1453 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1454 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1457 tvIns
.item
.lParam
= (LPARAM
)param
;
1458 tvIns
.item
.mask
= mask
;
1460 // don't use the hack below for the children of hidden root: this results
1461 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1462 const bool firstChild
= !IsHiddenRoot(parent
) &&
1463 !TreeView_GetChild(GetHwnd(), HITEM(parent
));
1465 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1468 wxLogLastError(wxT("TreeView_InsertItem"));
1471 // apparently some Windows versions (2000 and XP are reported to do this)
1472 // sometimes don't refresh the tree after adding the first child and so we
1473 // need this to make the "[+]" appear
1477 TreeView_GetItemRect(GetHwnd(), HITEM(parent
), &rect
, FALSE
);
1478 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1481 // associate the application tree item with Win32 tree item handle
1484 // setup wxTreeItemData
1487 param
->SetData(data
);
1491 return wxTreeItemId(id
);
1494 wxTreeItemId wxTreeCtrl
::AddRoot(const wxString
& text
,
1495 int image
, int selectedImage
,
1496 wxTreeItemData
*data
)
1498 if ( HasFlag(wxTR_HIDE_ROOT
) )
1500 wxASSERT_MSG( !m_pVirtualRoot
, wxT("tree can have only a single root") );
1502 // create a virtual root item, the parent for all the others
1503 wxTreeItemParam
*param
= new wxTreeItemParam
;
1504 param
->SetData(data
);
1506 m_pVirtualRoot
= new wxVirtualNode(param
);
1511 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1512 text
, image
, selectedImage
, data
);
1515 wxTreeItemId wxTreeCtrl
::DoInsertItem(const wxTreeItemId
& parent
,
1517 const wxString
& text
,
1518 int image
, int selectedImage
,
1519 wxTreeItemData
*data
)
1521 wxTreeItemId idPrev
;
1522 if ( index
== (size_t)-1 )
1524 // special value: append to the end
1527 else // find the item from index
1529 wxTreeItemIdValue cookie
;
1530 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1531 while ( index
!= 0 && idCur
.IsOk() )
1536 idCur
= GetNextChild(parent
, cookie
);
1539 // assert, not check: if the index is invalid, we will append the item
1541 wxASSERT_MSG( index
== 0, wxT("bad index in wxTreeCtrl::InsertItem") );
1544 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1547 void wxTreeCtrl
::Delete(const wxTreeItemId
& item
)
1549 // unlock tree selections on vista, without this the
1550 // tree ctrl will eventually crash after item deletion
1551 TreeItemUnlocker unlock_all
;
1553 if ( HasFlag(wxTR_MULTIPLE
) )
1555 bool selected
= IsSelected(item
);
1560 next
= TreeView_GetNextVisible(GetHwnd(), HITEM(item
));
1564 next
= TreeView_GetPrevVisible(GetHwnd(), HITEM(item
));
1569 TempSetter
set(m_changingSelection
);
1570 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1572 wxLogLastError(wxT("TreeView_DeleteItem"));
1582 if ( item
== m_htSelStart
)
1583 m_htSelStart
.Unset();
1585 if ( item
== m_htClickedItem
)
1586 m_htClickedItem
.Unset();
1590 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
1592 if ( IsTreeEventAllowed(changingEvent
) )
1594 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
1595 (void)HandleTreeEvent(changedEvent
);
1599 DoUnselectItem(next
);
1606 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1608 wxLogLastError(wxT("TreeView_DeleteItem"));
1613 // delete all children (but don't delete the item itself)
1614 void wxTreeCtrl
::DeleteChildren(const wxTreeItemId
& item
)
1616 // unlock tree selections on vista for the duration of this call
1617 TreeItemUnlocker unlock_all
;
1619 wxTreeItemIdValue cookie
;
1621 wxArrayTreeItemIds children
;
1622 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1623 while ( child
.IsOk() )
1625 children
.Add(child
);
1627 child
= GetNextChild(item
, cookie
);
1630 size_t nCount
= children
.Count();
1631 for ( size_t n
= 0; n
< nCount
; n
++ )
1633 Delete(children
[n
]);
1637 void wxTreeCtrl
::DeleteAllItems()
1639 // unlock tree selections on vista for the duration of this call
1640 TreeItemUnlocker unlock_all
;
1642 // invalidate all the items we store as they're going to become invalid
1644 m_htClickedItem
= wxTreeItemId();
1646 // delete the "virtual" root item.
1647 if ( GET_VIRTUAL_ROOT() )
1649 delete GET_VIRTUAL_ROOT();
1650 m_pVirtualRoot
= NULL
;
1653 // and all the real items
1655 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1657 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1661 void wxTreeCtrl
::DoExpand(const wxTreeItemId
& item
, int flag
)
1663 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1664 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1665 flag
== TVE_EXPAND
||
1667 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1669 // A hidden root can be neither expanded nor collapsed.
1670 wxCHECK_RET( !IsHiddenRoot(item
),
1671 wxT("Can't expand/collapse hidden root node!") );
1673 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1674 // emulate them. This behaviour has changed slightly with comctl32.dll
1675 // v 4.70 - now it does send them but only the first time. To maintain
1676 // compatible behaviour and also in order to not have surprises with the
1677 // future versions, don't rely on this and still do everything ourselves.
1678 // To avoid that the messages be sent twice when the item is expanded for
1679 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1681 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1685 if ( IsExpanded(item
) )
1687 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
,
1688 this, wxTreeItemId(item
));
1690 if ( !IsTreeEventAllowed(event
) )
1694 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) )
1696 if ( IsExpanded(item
) )
1699 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, this, item
);
1700 (void)HandleTreeEvent(event
);
1702 //else: change didn't took place, so do nothing at all
1705 void wxTreeCtrl
::Expand(const wxTreeItemId
& item
)
1707 DoExpand(item
, TVE_EXPAND
);
1710 void wxTreeCtrl
::Collapse(const wxTreeItemId
& item
)
1712 DoExpand(item
, TVE_COLLAPSE
);
1715 void wxTreeCtrl
::CollapseAndReset(const wxTreeItemId
& item
)
1717 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1720 void wxTreeCtrl
::Toggle(const wxTreeItemId
& item
)
1722 DoExpand(item
, TVE_TOGGLE
);
1725 void wxTreeCtrl
::Unselect()
1727 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE
),
1728 wxT("doesn't make sense, may be you want UnselectAll()?") );
1730 // the current focus
1731 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1738 if ( HasFlag(wxTR_MULTIPLE
) )
1740 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
1741 this, wxTreeItemId());
1742 changingEvent
.m_itemOld
= htFocus
;
1744 if ( IsTreeEventAllowed(changingEvent
) )
1748 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1749 this, wxTreeItemId());
1750 changedEvent
.m_itemOld
= htFocus
;
1751 (void)HandleTreeEvent(changedEvent
);
1760 void wxTreeCtrl
::DoUnselectAll()
1762 wxArrayTreeItemIds selections
;
1763 size_t count
= GetSelections(selections
);
1765 for ( size_t n
= 0; n
< count
; n
++ )
1767 DoUnselectItem(selections
[n
]);
1770 m_htSelStart
.Unset();
1773 void wxTreeCtrl
::UnselectAll()
1775 if ( HasFlag(wxTR_MULTIPLE
) )
1777 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1778 if ( !htFocus
) return;
1780 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1781 changingEvent
.m_itemOld
= htFocus
;
1783 if ( IsTreeEventAllowed(changingEvent
) )
1787 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1788 changedEvent
.m_itemOld
= htFocus
;
1789 (void)HandleTreeEvent(changedEvent
);
1798 void wxTreeCtrl
::DoSelectChildren(const wxTreeItemId
& parent
)
1802 wxTreeItemIdValue cookie
;
1803 wxTreeItemId child
= GetFirstChild(parent
, cookie
);
1804 while ( child
.IsOk() )
1806 DoSelectItem(child
, true);
1807 child
= GetNextChild(child
, cookie
);
1811 void wxTreeCtrl
::SelectChildren(const wxTreeItemId
& parent
)
1813 wxCHECK_RET( HasFlag(wxTR_MULTIPLE
),
1814 "this only works with multiple selection controls" );
1816 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1818 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1819 changingEvent
.m_itemOld
= htFocus
;
1821 if ( IsTreeEventAllowed(changingEvent
) )
1823 DoSelectChildren(parent
);
1825 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1826 changedEvent
.m_itemOld
= htFocus
;
1827 (void)HandleTreeEvent(changedEvent
);
1831 void wxTreeCtrl
::DoSelectItem(const wxTreeItemId
& item
, bool select
)
1833 TempSetter
set(m_changingSelection
);
1835 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1838 void wxTreeCtrl
::SelectItem(const wxTreeItemId
& item
, bool select
)
1840 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't select hidden root item") );
1842 if ( select
== IsSelected(item
) )
1844 // nothing to do, the item is already in the requested state
1848 if ( HasFlag(wxTR_MULTIPLE
) )
1850 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1852 if ( IsTreeEventAllowed(changingEvent
) )
1854 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1855 DoSelectItem(item
, select
);
1859 SetFocusedItem(item
);
1862 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1864 (void)HandleTreeEvent(changedEvent
);
1867 else // single selection
1869 wxTreeItemId itemOld
, itemNew
;
1872 itemOld
= GetSelection();
1875 else // deselecting the currently selected item
1878 // leave itemNew invalid
1881 // Recent versions of comctl32.dll send TVN_SELCHANG{ED,ING} events
1882 // when we call TreeView_SelectItem() but apparently some old ones did
1883 // not so send the events ourselves and ignore those generated by
1884 // TreeView_SelectItem() if m_changingSelection is set.
1886 changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, itemNew
);
1887 changingEvent
.SetOldItem(itemOld
);
1889 if ( IsTreeEventAllowed(changingEvent
) )
1891 TempSetter
set(m_changingSelection
);
1893 if ( !TreeView_SelectItem(GetHwnd(), HITEM(itemNew
)) )
1895 wxLogLastError(wxT("TreeView_SelectItem"));
1899 ::SetFocus(GetHwnd(), HITEM(item
));
1901 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1903 changedEvent
.SetOldItem(itemOld
);
1904 (void)HandleTreeEvent(changedEvent
);
1907 //else: program vetoed the change
1911 void wxTreeCtrl
::EnsureVisible(const wxTreeItemId
& item
)
1913 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't show hidden root item") );
1916 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1919 void wxTreeCtrl
::ScrollTo(const wxTreeItemId
& item
)
1921 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1923 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1927 wxTextCtrl
*wxTreeCtrl
::GetEditControl() const
1932 void wxTreeCtrl
::DeleteTextCtrl()
1936 // the HWND corresponding to this control is deleted by the tree
1937 // control itself and we don't know when exactly this happens, so check
1938 // if the window still exists before calling UnsubclassWin()
1939 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1941 m_textCtrl
->SetHWND(0);
1944 m_textCtrl
->UnsubclassWin();
1945 m_textCtrl
->SetHWND(0);
1946 wxDELETE(m_textCtrl
);
1952 wxTextCtrl
*wxTreeCtrl
::EditLabel(const wxTreeItemId
& item
,
1953 wxClassInfo
*textControlClass
)
1955 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1960 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1961 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1963 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1967 wxDELETE(m_textCtrl
);
1971 // textctrl is subclassed in MSWOnNotify
1975 // End label editing, optionally cancelling the edit
1976 void wxTreeCtrl
::DoEndEditLabel(bool discardChanges
)
1978 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1983 wxTreeItemId wxTreeCtrl
::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1985 TV_HITTESTINFO hitTestInfo
;
1986 hitTestInfo
.pt
.x
= (int)point
.x
;
1987 hitTestInfo
.pt
.y
= (int)point
.y
;
1989 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1994 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1995 flags |= wxTREE_HITTEST_##flag
1997 TRANSLATE_FLAG(ABOVE
);
1998 TRANSLATE_FLAG(BELOW
);
1999 TRANSLATE_FLAG(NOWHERE
);
2000 TRANSLATE_FLAG(ONITEMBUTTON
);
2001 TRANSLATE_FLAG(ONITEMICON
);
2002 TRANSLATE_FLAG(ONITEMINDENT
);
2003 TRANSLATE_FLAG(ONITEMLABEL
);
2004 TRANSLATE_FLAG(ONITEMRIGHT
);
2005 TRANSLATE_FLAG(ONITEMSTATEICON
);
2006 TRANSLATE_FLAG(TOLEFT
);
2007 TRANSLATE_FLAG(TORIGHT
);
2009 #undef TRANSLATE_FLAG
2011 return wxTreeItemId(hitTestInfo
.hItem
);
2014 bool wxTreeCtrl
::GetBoundingRect(const wxTreeItemId
& item
,
2016 bool textOnly
) const
2020 // Virtual root items have no bounding rectangle
2021 if ( IS_VIRTUAL_ROOT(item
) )
2026 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2029 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2035 // couldn't retrieve rect: for example, item isn't visible
2040 void wxTreeCtrl
::ClearFocusedItem()
2042 TempSetter
set(m_changingSelection
);
2044 if ( !TreeView_SelectItem(GetHwnd(), 0) )
2046 wxLogLastError(wxT("TreeView_SelectItem"));
2050 void wxTreeCtrl
::SetFocusedItem(const wxTreeItemId
& item
)
2052 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2054 TempSetter
set(m_changingSelection
);
2056 ::SetFocus(GetHwnd(), HITEM(item
));
2059 void wxTreeCtrl
::DoUnselectItem(const wxTreeItemId
& item
)
2061 TempSetter
set(m_changingSelection
);
2063 ::UnselectItem(GetHwnd(), HITEM(item
));
2066 void wxTreeCtrl
::DoToggleItemSelection(const wxTreeItemId
& item
)
2068 TempSetter
set(m_changingSelection
);
2070 ::ToggleItemSelection(GetHwnd(), HITEM(item
));
2073 // ----------------------------------------------------------------------------
2075 // ----------------------------------------------------------------------------
2077 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2078 // functions such as IsDataIndirect()
2079 class wxTreeSortHelper
2082 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2085 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
2087 return ((wxTreeItemParam
*)lParam
)->GetItem();
2091 int CALLBACK wxTreeSortHelper
::Compare(LPARAM pItem1
,
2095 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2096 wxT("sorting tree without data doesn't make sense") );
2098 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2100 return tree
->OnCompareItems(GetIdFromData(pItem1
),
2101 GetIdFromData(pItem2
));
2104 void wxTreeCtrl
::SortChildren(const wxTreeItemId
& item
)
2106 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2108 // rely on the fact that TreeView_SortChildren does the same thing as our
2109 // default behaviour, i.e. sorts items alphabetically and so call it
2110 // directly if we're not in derived class (much more efficient!)
2111 // RN: Note that if you find you're code doesn't sort as expected this
2112 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
2113 // combo for your derived wxTreeCtrl if will sort without
2115 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2117 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2122 tvSort
.hParent
= HITEM(item
);
2123 tvSort
.lpfnCompare
= wxTreeSortHelper
::Compare
;
2124 tvSort
.lParam
= (LPARAM
)this;
2125 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2129 // ----------------------------------------------------------------------------
2131 // ----------------------------------------------------------------------------
2133 bool wxTreeCtrl
::MSWShouldPreProcessMessage(WXMSG
* msg
)
2135 if ( msg
->message
== WM_KEYDOWN
)
2137 // Only eat VK_RETURN if not being used by the application in
2138 // conjunction with modifiers
2139 if ( (msg
->wParam
== VK_RETURN
) && !wxIsAnyModifierDown() )
2141 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2146 return wxTreeCtrlBase
::MSWShouldPreProcessMessage(msg
);
2149 bool wxTreeCtrl
::MSWCommand(WXUINT cmd
, WXWORD id_
)
2151 const int id
= (signed short)id_
;
2153 if ( cmd
== EN_UPDATE
)
2155 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2156 event
.SetEventObject( this );
2157 ProcessCommand(event
);
2159 else if ( cmd
== EN_KILLFOCUS
)
2161 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2162 event
.SetEventObject( this );
2163 ProcessCommand(event
);
2171 // command processed
2175 bool wxTreeCtrl
::MSWIsOnItem(unsigned flags
) const
2177 unsigned mask
= TVHT_ONITEM
;
2178 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2179 mask
|= TVHT_ONITEMINDENT
| TVHT_ONITEMRIGHT
;
2181 return (flags
& mask
) != 0;
2184 bool wxTreeCtrl
::MSWHandleSelectionKey(unsigned vkey
)
2186 const bool bCtrl
= wxIsCtrlDown();
2187 const bool bShift
= wxIsShiftDown();
2188 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2197 if ( vkey
!= VK_RETURN
&& bCtrl
)
2199 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2201 changingEvent
.m_itemOld
= htSel
;
2203 if ( IsTreeEventAllowed(changingEvent
) )
2205 DoToggleItemSelection(wxTreeItemId(htSel
));
2207 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2209 changedEvent
.m_itemOld
= htSel
;
2210 (void)HandleTreeEvent(changedEvent
);
2215 wxArrayTreeItemIds selections
;
2216 size_t count
= GetSelections(selections
);
2218 if ( count
!= 1 || HITEM(selections
[0]) != htSel
)
2220 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2222 changingEvent
.m_itemOld
= htSel
;
2224 if ( IsTreeEventAllowed(changingEvent
) )
2227 DoSelectItem(wxTreeItemId(htSel
));
2229 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2231 changedEvent
.m_itemOld
= htSel
;
2232 (void)HandleTreeEvent(changedEvent
);
2240 if ( !bCtrl
&& !bShift
)
2242 wxArrayTreeItemIds selections
;
2247 next
= vkey
== VK_UP
2248 ?
TreeView_GetPrevVisible(GetHwnd(), htSel
)
2249 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2253 next
= GetRootItem();
2255 if ( IsHiddenRoot(next
) )
2256 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2264 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2266 changingEvent
.m_itemOld
= htSel
;
2268 if ( IsTreeEventAllowed(changingEvent
) )
2272 SetFocusedItem(next
);
2274 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2276 changedEvent
.m_itemOld
= htSel
;
2277 (void)HandleTreeEvent(changedEvent
);
2282 wxTreeItemId next
= vkey
== VK_UP
2283 ?
TreeView_GetPrevVisible(GetHwnd(), htSel
)
2284 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2291 if ( !m_htSelStart
)
2293 m_htSelStart
= htSel
;
2296 if ( bShift
&& SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2297 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2299 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2300 changingEvent
.m_itemOld
= htSel
;
2302 if ( IsTreeEventAllowed(changingEvent
) )
2304 SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2305 SR_UNSELECT_OTHERS
);
2307 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2308 changedEvent
.m_itemOld
= htSel
;
2309 (void)HandleTreeEvent(changedEvent
);
2313 SetFocusedItem(next
);
2318 if ( HasChildren(htSel
) && IsExpanded(htSel
) )
2324 wxTreeItemId next
= GetItemParent(htSel
);
2326 if ( next
.IsOk() && !IsHiddenRoot(next
) )
2328 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2330 changingEvent
.m_itemOld
= htSel
;
2332 if ( IsTreeEventAllowed(changingEvent
) )
2336 SetFocusedItem(next
);
2338 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2340 changedEvent
.m_itemOld
= htSel
;
2341 (void)HandleTreeEvent(changedEvent
);
2348 if ( !IsVisible(htSel
) )
2350 EnsureVisible(htSel
);
2353 if ( !HasChildren(htSel
) )
2356 if ( !IsExpanded(htSel
) )
2362 wxTreeItemId next
= TreeView_GetChild(GetHwnd(), htSel
);
2364 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2365 changingEvent
.m_itemOld
= htSel
;
2367 if ( IsTreeEventAllowed(changingEvent
) )
2371 SetFocusedItem(next
);
2373 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2374 changedEvent
.m_itemOld
= htSel
;
2375 (void)HandleTreeEvent(changedEvent
);
2383 wxTreeItemId next
= GetRootItem();
2385 if ( IsHiddenRoot(next
) )
2387 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2393 if ( vkey
== VK_END
)
2397 wxTreeItemId nextTemp
= TreeView_GetNextVisible(
2398 GetHwnd(), HITEM(next
));
2400 if ( !nextTemp
.IsOk() )
2407 if ( htSel
== HITEM(next
) )
2412 if ( !m_htSelStart
)
2414 m_htSelStart
= htSel
;
2417 if ( SelectRange(GetHwnd(),
2418 HITEM(m_htSelStart
), HITEM(next
),
2419 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2421 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2423 changingEvent
.m_itemOld
= htSel
;
2425 if ( IsTreeEventAllowed(changingEvent
) )
2427 SelectRange(GetHwnd(),
2428 HITEM(m_htSelStart
), HITEM(next
),
2429 SR_UNSELECT_OTHERS
);
2430 SetFocusedItem(next
);
2432 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2434 changedEvent
.m_itemOld
= htSel
;
2435 (void)HandleTreeEvent(changedEvent
);
2441 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2443 changingEvent
.m_itemOld
= htSel
;
2445 if ( IsTreeEventAllowed(changingEvent
) )
2449 SetFocusedItem(next
);
2451 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2453 changedEvent
.m_itemOld
= htSel
;
2454 (void)HandleTreeEvent(changedEvent
);
2464 wxTreeItemId firstVisible
= GetFirstVisibleItem();
2465 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2466 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2467 TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible
)) :
2468 TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible
));
2470 if ( !nextAdjacent
)
2475 wxTreeItemId nextStart
= firstVisible
;
2477 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2479 wxTreeItemId nextTemp
= (vkey
== VK_PRIOR
) ?
2480 TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart
)) :
2481 TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart
));
2483 if ( nextTemp
.IsOk() )
2485 nextStart
= nextTemp
;
2493 EnsureVisible(nextStart
);
2495 if ( vkey
== VK_NEXT
)
2497 wxTreeItemId nextEnd
= nextStart
;
2499 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2501 wxTreeItemId nextTemp
=
2502 TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd
));
2504 if ( nextTemp
.IsOk() )
2514 EnsureVisible(nextEnd
);
2519 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2520 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2521 TreeView_GetPrevVisible(GetHwnd(), htSel
) :
2522 TreeView_GetNextVisible(GetHwnd(), htSel
);
2524 if ( !nextAdjacent
)
2529 wxTreeItemId
next(htSel
);
2531 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2533 wxTreeItemId nextTemp
= vkey
== VK_PRIOR ?
2534 TreeView_GetPrevVisible(GetHwnd(), HITEM(next
)) :
2535 TreeView_GetNextVisible(GetHwnd(), HITEM(next
));
2537 if ( !nextTemp
.IsOk() )
2543 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2545 changingEvent
.m_itemOld
= htSel
;
2547 if ( IsTreeEventAllowed(changingEvent
) )
2550 m_htSelStart
.Unset();
2552 SetFocusedItem(next
);
2554 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2556 changedEvent
.m_itemOld
= htSel
;
2557 (void)HandleTreeEvent(changedEvent
);
2569 bool wxTreeCtrl
::MSWHandleTreeKeyDownEvent(WXWPARAM wParam
, WXLPARAM lParam
)
2571 wxTreeEvent
keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2572 keyEvent
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
, wParam
, lParam
);
2574 bool processed
= HandleTreeEvent(keyEvent
);
2576 // generate a separate event for Space/Return
2577 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
2578 ((wParam
== VK_SPACE
) || (wParam
== VK_RETURN
)) )
2580 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2583 wxTreeEvent
activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2585 (void)HandleTreeEvent(activatedEvent
);
2592 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2593 // only do it during dragging, minimize wxWin overhead (this is important for
2594 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2595 // instead of passing by wxWin events
2597 wxTreeCtrl
::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2599 bool processed
= false;
2601 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2603 if ( nMsg
== WM_CONTEXTMENU
)
2605 int x
= GET_X_LPARAM(lParam
),
2606 y
= GET_Y_LPARAM(lParam
);
2608 // the item for which the menu should be shown
2611 // the position where the menu should be shown in client coordinates
2612 // (so that it can be passed directly to PopupMenu())
2615 if ( x
== -1 || y
== -1 )
2617 // this means that the event was generated from keyboard (e.g. with
2618 // Shift-F10 or special Windows menu key)
2620 // use the Explorer standard of putting the menu at the left edge
2621 // of the text, in the vertical middle of the text
2622 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2625 // Use the bounding rectangle of only the text part
2627 GetBoundingRect(item
, rect
, true);
2628 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
2631 else // event from mouse, use mouse position
2633 pt
= ScreenToClient(wxPoint(x
, y
));
2635 TV_HITTESTINFO tvhti
;
2639 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2640 item
= wxTreeItemId(tvhti
.hItem
);
2646 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2648 event
.m_pointDrag
= pt
;
2650 if ( HandleTreeEvent(event
) )
2652 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2655 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2657 // we only process mouse messages here and these parameters have the
2658 // same meaning for all of them
2659 int x
= GET_X_LPARAM(lParam
),
2660 y
= GET_Y_LPARAM(lParam
);
2662 TV_HITTESTINFO tvht
;
2666 HTREEITEM htOldItem
= TreeView_GetSelection(GetHwnd());
2667 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2671 case WM_LBUTTONDOWN
:
2675 m_htClickedItem
.Unset();
2677 if ( !MSWIsOnItem(tvht
.flags
) )
2679 if ( tvht
.flags
& TVHT_ONITEMBUTTON
)
2681 // either it's going to be handled by user code or
2682 // we're going to use it ourselves to toggle the
2683 // branch, in either case don't pass it to the base
2684 // class which would generate another mouse click event
2685 // for it even though it's already handled here
2689 if ( !HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2691 if ( !IsExpanded(htItem
) )
2702 m_focusLost
= false;
2708 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2709 m_ptClick
= wxPoint(x
, y
);
2711 if ( wParam
& MK_CONTROL
)
2713 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2715 m_htClickedItem
.Unset();
2719 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2721 changingEvent
.m_itemOld
= htOldItem
;
2723 if ( IsTreeEventAllowed(changingEvent
) )
2725 // toggle selected state
2726 DoToggleItemSelection(wxTreeItemId(htItem
));
2728 SetFocusedItem(wxTreeItemId(htItem
));
2730 // reset on any click without Shift
2731 m_htSelStart
.Unset();
2733 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2735 changedEvent
.m_itemOld
= htOldItem
;
2736 (void)HandleTreeEvent(changedEvent
);
2739 else if ( wParam
& MK_SHIFT
)
2741 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2743 m_htClickedItem
.Unset();
2748 bool willChange
= true;
2750 if ( !(wParam
& MK_CONTROL
) )
2752 srFlags
|= SR_UNSELECT_OTHERS
;
2755 if ( !m_htSelStart
)
2757 // take the focused item
2758 m_htSelStart
= htOldItem
;
2762 willChange
= SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2763 htItem
, srFlags
| SR_SIMULATE
);
2768 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2770 changingEvent
.m_itemOld
= htOldItem
;
2772 if ( IsTreeEventAllowed(changingEvent
) )
2774 // this selects all items between the starting one
2778 SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2783 DoSelectItem(wxTreeItemId(htItem
));
2786 SetFocusedItem(wxTreeItemId(htItem
));
2788 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2790 changedEvent
.m_itemOld
= htOldItem
;
2791 (void)HandleTreeEvent(changedEvent
);
2795 else // normal click
2797 // avoid doing anything if we click on the only
2798 // currently selected item
2800 wxArrayTreeItemIds selections
;
2801 size_t count
= GetSelections(selections
);
2805 HITEM(selections
[0]) != htItem
)
2807 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2809 m_htClickedItem
.Unset();
2813 // clear the previously selected items, if the user
2814 // clicked outside of the present selection, otherwise,
2815 // perform the deselection on mouse-up, this allows
2816 // multiple drag and drop to work.
2817 if ( !IsItemSelected(GetHwnd(), htItem
))
2819 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2821 changingEvent
.m_itemOld
= htOldItem
;
2823 if ( IsTreeEventAllowed(changingEvent
) )
2826 DoSelectItem(wxTreeItemId(htItem
));
2827 SetFocusedItem(wxTreeItemId(htItem
));
2829 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2831 changedEvent
.m_itemOld
= htOldItem
;
2832 (void)HandleTreeEvent(changedEvent
);
2837 SetFocusedItem(wxTreeItemId(htItem
));
2838 m_mouseUpDeselect
= true;
2841 else // click on a single selected item
2843 // don't interfere with the default processing in
2844 // WM_MOUSEMOVE handler below as the default window
2845 // proc will start the drag itself if we let have
2847 m_htClickedItem
.Unset();
2849 // prevent in-place editing from starting if focus lost
2850 // since previous click
2854 DoSelectItem(wxTreeItemId(htItem
));
2855 SetFocusedItem(wxTreeItemId(htItem
));
2863 // reset on any click without Shift
2864 m_htSelStart
.Unset();
2867 m_focusLost
= false;
2869 // we consumed the event so we need to trigger state image
2874 wxTreeItemId item
= HitTest(wxPoint(x
, y
), htFlags
);
2876 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2878 m_triggerStateImageClick
= true;
2883 case WM_RBUTTONDOWN
:
2890 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) || !htItem
)
2895 // default handler removes the highlight from the currently
2896 // focused item when right mouse button is pressed on another
2897 // one but keeps the remaining items highlighted, which is
2898 // confusing, so override this default behaviour
2899 if ( !IsItemSelected(GetHwnd(), htItem
) )
2901 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2903 changingEvent
.m_itemOld
= htOldItem
;
2905 if ( IsTreeEventAllowed(changingEvent
) )
2908 DoSelectItem(wxTreeItemId(htItem
));
2909 SetFocusedItem(wxTreeItemId(htItem
));
2911 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2913 changedEvent
.m_itemOld
= htOldItem
;
2914 (void)HandleTreeEvent(changedEvent
);
2922 if ( m_htClickedItem
)
2924 int cx
= abs(m_ptClick
.x
- x
);
2925 int cy
= abs(m_ptClick
.y
- y
);
2927 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2928 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2933 tv
.hdr
.hwndFrom
= GetHwnd();
2934 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2935 tv
.hdr
.code
= TVN_BEGINDRAG
;
2937 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2941 wxZeroMemory(tviAux
);
2943 tviAux
.hItem
= HITEM(m_htClickedItem
);
2944 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2945 tviAux
.stateMask
= 0xffffffff;
2946 TreeView_GetItem(GetHwnd(), &tviAux
);
2948 tv
.itemNew
.state
= tviAux
.state
;
2949 tv
.itemNew
.lParam
= tviAux
.lParam
;
2954 // do it before SendMessage() call below to avoid
2955 // reentrancies here if there is another WM_MOUSEMOVE
2956 // in the queue already
2957 m_htClickedItem
.Unset();
2959 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
2960 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2962 // don't pass it to the default window proc, it would
2963 // start dragging again
2967 #endif // __WXWINCE__
2972 m_dragImage
->Move(wxPoint(x
, y
));
2975 // highlight the item as target (hiding drag image is
2976 // necessary - otherwise the display will be corrupted)
2977 m_dragImage
->Hide();
2978 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2979 m_dragImage
->Show();
2982 #endif // wxUSE_DRAGIMAGE
2988 // deselect other items if needed
2991 if ( m_mouseUpDeselect
)
2993 m_mouseUpDeselect
= false;
2995 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2997 changingEvent
.m_itemOld
= htOldItem
;
2999 if ( IsTreeEventAllowed(changingEvent
) )
3002 DoSelectItem(wxTreeItemId(htItem
));
3003 SetFocusedItem(wxTreeItemId(htItem
));
3005 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
3007 changedEvent
.m_itemOld
= htOldItem
;
3008 (void)HandleTreeEvent(changedEvent
);
3013 m_htClickedItem
.Unset();
3015 if ( m_triggerStateImageClick
)
3017 if ( tvht
.flags
& TVHT_ONITEMSTATEICON
)
3019 wxTreeEvent
event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
,
3021 (void)HandleTreeEvent(event
);
3023 m_triggerStateImageClick
= false;
3028 if ( !m_dragStarted
&& MSWIsOnItem(tvht
.flags
) )
3040 m_dragImage
->EndDrag();
3041 wxDELETE(m_dragImage
);
3043 // generate the drag end event
3044 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
,
3046 event
.m_pointDrag
= wxPoint(x
, y
);
3047 (void)HandleTreeEvent(event
);
3049 // if we don't do it, the tree seems to think that 2 items
3050 // are selected simultaneously which is quite weird
3051 TreeView_SelectDropTarget(GetHwnd(), 0);
3053 #endif // wxUSE_DRAGIMAGE
3055 if ( isMultiple
&& nMsg
== WM_RBUTTONUP
)
3059 nmhdr
.hwndFrom
= GetHwnd();
3060 nmhdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
3061 nmhdr
.code
= NM_RCLICK
;
3062 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY
,
3063 nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
3067 m_dragStarted
= false;
3072 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) )
3076 // the tree control greys out the selected item when it loses focus
3077 // and paints it as selected again when it regains it, but it won't
3078 // do it for the other items itself - help it
3079 wxArrayTreeItemIds selections
;
3080 size_t count
= GetSelections(selections
);
3083 for ( size_t n
= 0; n
< count
; n
++ )
3085 // TreeView_GetItemRect() will return false if item is not
3086 // visible, which may happen perfectly well
3087 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
3090 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
3095 if ( nMsg
== WM_KILLFOCUS
)
3100 else if ( (nMsg
== WM_KEYDOWN
|| nMsg
== WM_SYSKEYDOWN
) && isMultiple
)
3102 // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
3103 // notification but for the keys which can be used to change selection
3104 // we need to do it from here so as to not apply the default behaviour
3105 // if the events are handled by the user code
3118 if ( !HandleKeyDown(wParam
, lParam
) &&
3119 !MSWHandleTreeKeyDownEvent(wParam
, lParam
) )
3121 // use the key to update the selection if it was left
3123 MSWHandleSelectionKey(wParam
);
3126 // pretend that we did process it in any case as we already
3127 // generated an event for it
3130 //default: for all the other keys leave processed as false so that
3131 // the tree control generates a TVN_KEYDOWN for us
3135 else if ( nMsg
== WM_COMMAND
)
3137 // if we receive a EN_KILLFOCUS command from the in-place edit control
3138 // used for label editing, make sure to end editing
3141 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
3143 if ( cmd
== EN_KILLFOCUS
)
3145 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
3155 rc
= wxControl
::MSWWindowProc(nMsg
, wParam
, lParam
);
3161 wxTreeCtrl
::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
3163 if ( nMsg
== WM_CHAR
)
3165 // don't let the control process Space and Return keys because it
3166 // doesn't do anything useful with them anyhow but always beeps
3167 // annoyingly when it receives them and there is no way to turn it off
3168 // simply if you just process TREEITEM_ACTIVATED event to which Space
3169 // and Enter presses are mapped in your code
3170 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
3174 else if ( nMsg
== WM_KEYDOWN
)
3176 if ( wParam
== VK_ESCAPE
)
3180 m_dragImage
->EndDrag();
3181 wxDELETE(m_dragImage
);
3183 // if we don't do it, the tree seems to think that 2 items
3184 // are selected simultaneously which is quite weird
3185 TreeView_SelectDropTarget(GetHwnd(), 0);
3189 #endif // wxUSE_DRAGIMAGE
3191 return wxControl
::MSWDefWindowProc(nMsg
, wParam
, lParam
);
3194 // process WM_NOTIFY Windows message
3195 bool wxTreeCtrl
::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
3197 wxTreeEvent
event(wxEVT_NULL
, this);
3198 wxEventType eventType
= wxEVT_NULL
;
3199 NMHDR
*hdr
= (NMHDR
*)lParam
;
3201 switch ( hdr
->code
)
3204 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3207 case TVN_BEGINRDRAG
:
3209 if ( eventType
== wxEVT_NULL
)
3210 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
3211 //else: left drag, already set above
3213 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3215 event
.m_item
= tv
->itemNew
.hItem
;
3216 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
3218 // don't allow dragging by default: the user code must
3219 // explicitly say that it wants to allow it to avoid breaking
3225 case TVN_BEGINLABELEDIT
:
3227 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
3228 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3230 // although the user event handler may still veto it, it is
3231 // important to set it now so that calls to SetItemText() from
3232 // the event handler would change the text controls contents
3234 event
.m_item
= info
->item
.hItem
;
3235 event
.m_label
= info
->item
.pszText
;
3236 event
.m_editCancelled
= false;
3240 case TVN_DELETEITEM
:
3242 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
3243 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3245 event
.m_item
= tv
->itemOld
.hItem
;
3249 wxMapTreeAttr
::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
3250 if ( it
!= m_attrs
.end() )
3259 case TVN_ENDLABELEDIT
:
3261 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
3262 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3264 event
.m_item
= info
->item
.hItem
;
3265 event
.m_label
= info
->item
.pszText
;
3266 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
3271 // These *must* not be removed or TVN_GETINFOTIP will
3272 // not be processed each time the mouse is moved
3273 // and the tooltip will only ever update once.
3282 #ifdef TVN_GETINFOTIP
3283 case TVN_GETINFOTIP
:
3285 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
3286 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
3288 // Which item are we trying to get a tooltip for?
3289 event
.m_item
= info
->hItem
;
3293 #endif // TVN_GETINFOTIP
3294 #endif // !__WXWINCE__
3296 case TVN_GETDISPINFO
:
3297 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
3300 case TVN_SETDISPINFO
:
3302 if ( eventType
== wxEVT_NULL
)
3303 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
3304 //else: get, already set above
3306 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3308 event
.m_item
= info
->item
.hItem
;
3312 case TVN_ITEMEXPANDING
:
3313 case TVN_ITEMEXPANDED
:
3315 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3318 switch ( tv
->action
)
3321 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
3329 what
= IDX_COLLAPSE
;
3333 int how
= hdr
->code
== TVN_ITEMEXPANDING ? IDX_DOING
3336 eventType
= gs_expandEvents
[what
][how
];
3338 event
.m_item
= tv
->itemNew
.hItem
;
3344 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
3346 // fabricate the lParam and wParam parameters sufficiently
3347 // similar to the ones from a "real" WM_KEYDOWN so that
3348 // CreateKeyEvent() works correctly
3349 return MSWHandleTreeKeyDownEvent(
3350 info
->wVKey
, (wxIsAltDown() ? KF_ALTDOWN
: 0) << 16);
3354 // Vista's tree control has introduced some problems with our
3355 // multi-selection tree. When TreeView_SelectItem() is called,
3356 // the wrong items are deselected.
3358 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3359 // that can be used to regulate this incorrect behavior. The
3360 // following messages will allow only the unlocked item's selection
3363 case TVN_ITEMCHANGINGA
:
3364 case TVN_ITEMCHANGINGW
:
3366 // we only need to handles these in multi-select trees
3367 if ( HasFlag(wxTR_MULTIPLE
) )
3369 // get info about the item about to be changed
3370 NMTVITEMCHANGE
* info
= (NMTVITEMCHANGE
*)lParam
;
3371 if (TreeItemUnlocker
::IsLocked(info
->hItem
))
3373 // item's state is locked, don't allow the change
3374 // returning 1 will disallow the change
3380 // allow the state change
3384 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
3385 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3386 // we have to handle both messages:
3387 case TVN_SELCHANGEDA
:
3388 case TVN_SELCHANGEDW
:
3389 if ( !m_changingSelection
)
3391 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
3395 case TVN_SELCHANGINGA
:
3396 case TVN_SELCHANGINGW
:
3397 if ( !m_changingSelection
)
3399 if ( eventType
== wxEVT_NULL
)
3400 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
3401 //else: already set above
3403 if (hdr
->code
== TVN_SELCHANGINGW
||
3404 hdr
->code
== TVN_SELCHANGEDW
)
3406 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
3407 event
.m_item
= tv
->itemNew
.hItem
;
3408 event
.m_itemOld
= tv
->itemOld
.hItem
;
3412 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
3413 event
.m_item
= tv
->itemNew
.hItem
;
3414 event
.m_itemOld
= tv
->itemOld
.hItem
;
3418 // we receive this message from WM_LBUTTONDOWN handler inside
3419 // comctl32.dll and so before the click is passed to
3420 // DefWindowProc() which sets the focus to the window which was
3421 // clicked and this can lead to unexpected event sequences: for
3422 // example, we may get a "selection change" event from the tree
3423 // before getting a "kill focus" event for the text control which
3424 // had the focus previously, thus breaking user code doing input
3427 // to avoid such surprises, we force the generation of focus events
3428 // now, before we generate the selection change ones
3429 if ( !m_changingSelection
)
3433 // instead of explicitly checking for _WIN32_IE, check if the
3434 // required symbols are available in the headers
3435 #if defined(CDDS_PREPAINT)
3438 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
3439 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
3440 switch ( nmcd
.dwDrawStage
)
3443 // if we've got any items with non standard attributes,
3444 // notify us before painting each item
3445 *result
= m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
3448 // windows in TreeCtrl use one-based index for item state images,
3449 // 0 indexed image is not being used, we're using zero-based index,
3450 // so we have to add temp image (of zero index) to state image list
3451 // before we draw any item, then after items are drawn we have to
3452 // delete it (in POSTPAINT notify)
3453 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3455 typedef BOOL (wxSTDCALL
*ImageList_Copy_t
)
3456 (HIMAGELIST
, int, HIMAGELIST
, int, UINT
);
3457 static ImageList_Copy_t s_pfnImageList_Copy
= NULL
;
3458 static bool loaded
= false;
3462 wxLoadedDLL
dllComCtl32(wxT("comctl32.dll"));
3463 if ( dllComCtl32
.IsLoaded() )
3464 wxDL_INIT_FUNC(s_pfn
, ImageList_Copy
, dllComCtl32
);
3467 if ( !s_pfnImageList_Copy
)
3469 // this code is broken with ImageList_Copy()
3470 // but I don't care enough about Win95 support
3471 // to write it now -- if anybody does, please
3473 wxFAIL_MSG("TODO: implement this for Win95");
3478 hImageList
= GetHimagelistOf(m_imageListState
);
3480 // add temporary image
3482 m_imageListState
->GetSize(0, width
, height
);
3484 HBITMAP hbmpTemp
= ::CreateBitmap(width
, height
, 1, 1, NULL
);
3485 int index
= ::ImageList_Add(hImageList
, hbmpTemp
, hbmpTemp
);
3486 ::DeleteObject(hbmpTemp
);
3490 // move images to right
3491 for ( int i
= index
; i
> 0; i
-- )
3493 (*s_pfnImageList_Copy
)(hImageList
, i
,
3498 // we must remove the image in POSTPAINT notify
3499 *result
|= CDRF_NOTIFYPOSTPAINT
;
3504 case CDDS_POSTPAINT
:
3505 // we are deleting temp image of 0 index, which was
3506 // added before items were drawn (in PREPAINT notify)
3507 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3508 m_imageListState
->Remove(0);
3511 case CDDS_ITEMPREPAINT
:
3513 wxMapTreeAttr
::iterator
3514 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
3516 if ( it
== m_attrs
.end() )
3518 // nothing to do for this item
3519 *result
= CDRF_DODEFAULT
;
3523 wxTreeItemAttr
* const attr
= it
->second
;
3525 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
3526 TVIF_STATE
, TVIS_DROPHILITED
);
3528 const UINT tvItemState
= tvItem
.state
;
3530 // selection colours should override ours,
3531 // otherwise it is too confusing to the user
3532 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
3533 !(tvItemState
& TVIS_DROPHILITED
) )
3536 if ( attr
->HasBackgroundColour() )
3538 colBack
= attr
->GetBackgroundColour();
3539 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
3543 // but we still want to keep the special foreground
3544 // colour when we don't have focus (we can't keep
3545 // it when we do, it would usually be unreadable on
3546 // the almost inverted bg colour...)
3547 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
3548 FindFocus() != this ) &&
3549 !(tvItemState
& TVIS_DROPHILITED
) )
3552 if ( attr
->HasTextColour() )
3554 colText
= attr
->GetTextColour();
3555 lptvcd
->clrText
= wxColourToRGB(colText
);
3559 if ( attr
->HasFont() )
3561 HFONT hFont
= GetHfontOf(attr
->GetFont());
3563 ::SelectObject(nmcd
.hdc
, hFont
);
3565 *result
= CDRF_NEWFONT
;
3567 else // no specific font
3569 *result
= CDRF_DODEFAULT
;
3575 *result
= CDRF_DODEFAULT
;
3579 // we always process it
3581 #endif // have owner drawn support in headers
3585 DWORD pos
= GetMessagePos();
3587 point
.x
= LOWORD(pos
);
3588 point
.y
= HIWORD(pos
);
3589 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
3591 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), htFlags
);
3593 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3595 event
.m_item
= item
;
3596 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
3605 TV_HITTESTINFO tvhti
;
3606 ::GetCursorPos(&tvhti
.pt
);
3607 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
3608 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
3610 if ( MSWIsOnItem(tvhti
.flags
) )
3612 event
.m_item
= tvhti
.hItem
;
3613 eventType
= (int)hdr
->code
== NM_DBLCLK
3614 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
3615 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
3617 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
3618 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
3627 return wxControl
::MSWOnNotify(idCtrl
, lParam
, result
);
3630 event
.SetEventType(eventType
);
3632 bool processed
= HandleTreeEvent(event
);
3635 switch ( hdr
->code
)
3638 // we translate NM_DBLCLK into ACTIVATED event and if the user
3639 // handled the activation of the item we shouldn't proceed with
3640 // also using the same double click for toggling the item expanded
3641 // state -- but OTOH do let the user to expand/collapse the item by
3642 // double clicking on it if the activation is not handled specially
3643 *result
= processed
;
3647 // prevent tree control from sending WM_CONTEXTMENU to our parent
3648 // (which it does if NM_RCLICK is not handled) because we want to
3649 // send it to the control itself
3653 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
3654 (WPARAM
)GetHwnd(), ::GetMessagePos());
3658 case TVN_BEGINRDRAG
:
3660 if ( event
.IsAllowed() )
3662 // normally this is impossible because the m_dragImage is
3663 // deleted once the drag operation is over
3664 wxASSERT_MSG( !m_dragImage
, wxT("starting to drag once again?") );
3666 m_dragImage
= new wxDragImage(*this, event
.m_item
);
3667 m_dragImage
->BeginDrag(wxPoint(0,0), this);
3668 m_dragImage
->Show();
3670 m_dragStarted
= true;
3672 #endif // wxUSE_DRAGIMAGE
3675 case TVN_DELETEITEM
:
3677 // NB: we might process this message using wxWidgets event
3678 // tables, but due to overhead of wxWin event system we
3679 // prefer to do it here ourself (otherwise deleting a tree
3680 // with many items is just too slow)
3681 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3683 wxTreeItemParam
*param
=
3684 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
3687 processed
= true; // Make sure we don't get called twice
3691 case TVN_BEGINLABELEDIT
:
3692 // return true to cancel label editing
3693 *result
= !event
.IsAllowed();
3695 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3696 if ( event
.IsAllowed() )
3698 HWND hText
= TreeView_GetEditControl(GetHwnd());
3701 // MBN: if m_textCtrl already has an HWND, it is a stale
3702 // pointer from a previous edit (because the user
3703 // didn't modify the label before dismissing the control,
3704 // and TVN_ENDLABELEDIT was not sent), so delete it
3705 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
3708 m_textCtrl
= new wxTextCtrl();
3709 m_textCtrl
->SetParent(this);
3710 m_textCtrl
->SetHWND((WXHWND
)hText
);
3711 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3713 // set wxTE_PROCESS_ENTER style for the text control to
3714 // force it to process the Enter presses itself, otherwise
3715 // they could be stolen from it by the dialog
3717 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3718 | wxTE_PROCESS_ENTER
);
3721 else // we had set m_idEdited before
3727 case TVN_ENDLABELEDIT
:
3728 // return true to set the label to the new string: note that we
3729 // also must pretend that we did process the message or it is going
3730 // to be passed to DefWindowProc() which will happily return false
3731 // cancelling the label change
3732 *result
= event
.IsAllowed();
3735 // ensure that we don't have the text ctrl which is going to be
3741 #ifdef TVN_GETINFOTIP
3742 case TVN_GETINFOTIP
:
3744 // If the user permitted a tooltip change, change it
3745 if (event
.IsAllowed())
3747 SetToolTip(event
.m_label
);
3754 case TVN_SELCHANGING
:
3755 case TVN_ITEMEXPANDING
:
3756 // return true to prevent the action from happening
3757 *result
= !event
.IsAllowed();
3760 case TVN_ITEMEXPANDED
:
3762 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3763 const wxTreeItemId
id(tv
->itemNew
.hItem
);
3765 if ( tv
->action
== TVE_COLLAPSE
)
3767 if ( wxApp
::GetComCtl32Version() >= 600 )
3769 // for some reason the item selection rectangle depends
3770 // on whether it is expanded or collapsed (at least
3771 // with comctl32.dll v6): it is wider (by 3 pixels) in
3772 // the expanded state, so when the item collapses and
3773 // then is deselected the rightmost 3 pixels of the
3774 // previously drawn selection are left on the screen
3776 // it's not clear if it's a bug in comctl32.dll or in
3777 // our code (because it does not happen in Explorer but
3778 // OTOH we don't do anything which could result in this
3779 // AFAICS) but we do need to work around it to avoid
3786 // the item is also not refreshed properly after expansion when
3787 // it has an image depending on the expanded/collapsed state:
3788 // again, it's not clear if the bug is in comctl32.dll or our
3790 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3799 case TVN_GETDISPINFO
:
3800 // NB: so far the user can't set the image himself anyhow, so do it
3801 // anyway - but this may change later
3802 //if ( /* !processed && */ )
3804 wxTreeItemId item
= event
.m_item
;
3805 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3807 const wxTreeItemParam
* const param
= GetItemParam(item
);
3811 if ( info
->item
.mask
& TVIF_IMAGE
)
3816 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3817 : wxTreeItemIcon_Normal
3820 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3822 info
->item
.iSelectedImage
=
3825 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3826 : wxTreeItemIcon_Selected
3833 // for the other messages the return value is ignored and there is
3834 // nothing special to do
3839 // ----------------------------------------------------------------------------
3841 // ----------------------------------------------------------------------------
3843 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3844 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3846 int wxTreeCtrl
::DoGetItemState(const wxTreeItemId
& item
) const
3848 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
3850 // receive the desired information
3851 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3854 // state images are one-based
3855 return STATEIMAGEMASKTOINDEX(tvItem
.state
) - 1;
3858 void wxTreeCtrl
::DoSetItemState(const wxTreeItemId
& item
, int state
)
3860 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
3862 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3864 // state images are one-based
3865 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3866 tvItem
.state
= INDEXTOSTATEIMAGEMASK(state
+ 1);
3871 #endif // wxUSE_TREECTRL