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 // behavior, 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 // ----------------------------------------------------------------------------
134 // wrappers for TreeView_GetItem/TreeView_SetItem
135 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
138 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
139 tvi
.stateMask
= TVIS_SELECTED
;
142 TreeItemUnlocker
unlocker(hItem
);
144 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
146 wxLogLastError(wxT("TreeView_GetItem"));
149 return (tvi
.state
& TVIS_SELECTED
) != 0;
152 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
155 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
156 tvi
.stateMask
= TVIS_SELECTED
;
157 tvi
.state
= select
? TVIS_SELECTED
: 0;
160 TreeItemUnlocker
unlocker(hItem
);
162 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
164 wxLogLastError(wxT("TreeView_SetItem"));
171 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
173 SelectItem(hwndTV
, htItem
, false);
176 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
178 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
181 // helper function which selects all items in a range and, optionally,
182 // deselects all the other ones
184 // returns true if the selection changed at all or false if nothing changed
186 // flags for SelectRange()
189 SR_SIMULATE
= 1, // don't do anything, just return true or false
190 SR_UNSELECT_OTHERS
= 2 // deselect the items not in range
193 static bool SelectRange(HWND hwndTV
,
198 // find the first (or last) item and select it
199 bool changed
= false;
201 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
203 while ( htItem
&& cont
)
205 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
207 if ( !IsItemSelected(hwndTV
, htItem
) )
209 if ( !(flags
& SR_SIMULATE
) )
211 SelectItem(hwndTV
, htItem
);
219 else // not first or last
221 if ( flags
& SR_UNSELECT_OTHERS
)
223 if ( IsItemSelected(hwndTV
, htItem
) )
225 if ( !(flags
& SR_SIMULATE
) )
226 UnselectItem(hwndTV
, htItem
);
233 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
236 // select the items in range
237 cont
= htFirst
!= htLast
;
238 while ( htItem
&& cont
)
240 if ( !IsItemSelected(hwndTV
, htItem
) )
242 if ( !(flags
& SR_SIMULATE
) )
244 SelectItem(hwndTV
, htItem
);
250 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
252 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
255 // optionally deselect the rest
256 if ( flags
& SR_UNSELECT_OTHERS
)
260 if ( IsItemSelected(hwndTV
, htItem
) )
262 if ( !(flags
& SR_SIMULATE
) )
264 UnselectItem(hwndTV
, htItem
);
270 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
274 // seems to be necessary - otherwise the just selected items don't always
275 // appear as selected
276 if ( !(flags
& SR_SIMULATE
) )
278 UpdateWindow(hwndTV
);
284 // helper function which tricks the standard control into changing the focused
285 // item without changing anything else (if someone knows why Microsoft doesn't
286 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
288 // returns true if the focus was changed, false if the given item was already
290 static bool SetFocus(HWND hwndTV
, HTREEITEM htItem
)
293 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
295 if ( htItem
== htFocus
)
300 // remember the selection state of the item
301 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
303 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
305 // prevent the tree from unselecting the old focus which it
306 // would do by default (TreeView_SelectItem unselects the
308 TreeView_SelectItem(hwndTV
, 0);
309 SelectItem(hwndTV
, htFocus
);
312 TreeView_SelectItem(hwndTV
, htItem
);
316 // need to clear the selection which TreeView_SelectItem() gave
318 UnselectItem(hwndTV
, htItem
);
320 //else: was selected, still selected - ok
324 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
326 // just clear the focus
327 TreeView_SelectItem(hwndTV
, 0);
329 if ( wasFocusSelected
)
331 // restore the selection state
332 SelectItem(hwndTV
, htFocus
);
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
343 // a convenient wrapper around TV_ITEM struct which adds a ctor
345 #pragma warning( disable : 4097 ) // inheriting from typedef
348 struct wxTreeViewItem
: public TV_ITEM
350 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
351 UINT mask_
, // fields which are valid
352 UINT stateMask_
= 0) // for TVIF_STATE only
356 // hItem member is always valid
357 mask
= mask_
| TVIF_HANDLE
;
358 stateMask
= stateMask_
;
363 // ----------------------------------------------------------------------------
364 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
366 // We need this for a couple of reasons:
368 // 1) This class is needed for support of different images: the Win32 common
369 // control natively supports only 2 images (the normal one and another for the
370 // selected state). We wish to provide support for 2 more of them for folder
371 // items (i.e. those which have children): for expanded state and for expanded
372 // selected state. For this we use this structure to store the additional items
375 // 2) This class is also needed to hold the HITEM so that we can sort
376 // it correctly in the MSW sort callback.
378 // In addition it makes other workarounds such as this easier and helps
379 // simplify the code.
380 // ----------------------------------------------------------------------------
382 class wxTreeItemParam
389 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
395 // dtor deletes the associated data as well
396 virtual ~wxTreeItemParam() { delete m_data
; }
399 // get the real data associated with the item
400 wxTreeItemData
*GetData() const { return m_data
; }
402 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
404 // do we have such image?
405 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
406 // get image, falling back to the other images if this one is not
408 int GetImage(wxTreeItemIcon which
) const
410 int image
= m_images
[which
];
415 case wxTreeItemIcon_SelectedExpanded
:
416 image
= GetImage(wxTreeItemIcon_Expanded
);
421 case wxTreeItemIcon_Selected
:
422 case wxTreeItemIcon_Expanded
:
423 image
= GetImage(wxTreeItemIcon_Normal
);
426 case wxTreeItemIcon_Normal
:
431 wxFAIL_MSG( wxT("unsupported wxTreeItemIcon value") );
437 // change the given image
438 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
441 const wxTreeItemId
& GetItem() const { return m_item
; }
443 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
446 // all the images associated with the item
447 int m_images
[wxTreeItemIcon_Max
];
449 // item for sort callbacks
452 // the real client data
453 wxTreeItemData
*m_data
;
455 wxDECLARE_NO_COPY_CLASS(wxTreeItemParam
);
458 // wxVirutalNode is used in place of a single root when 'hidden' root is
460 class wxVirtualNode
: public wxTreeViewItem
463 wxVirtualNode(wxTreeItemParam
*param
)
464 : wxTreeViewItem(TVI_ROOT
, 0)
474 wxTreeItemParam
*GetParam() const { return m_param
; }
475 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
478 wxTreeItemParam
*m_param
;
480 wxDECLARE_NO_COPY_CLASS(wxVirtualNode
);
484 #pragma warning( default : 4097 )
487 // a macro to get the virtual root, returns NULL if none
488 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
490 // returns true if the item is the virtual root
491 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
493 // a class which encapsulates the tree traversal logic: it vists all (unless
494 // OnVisit() returns false) items under the given one
495 class wxTreeTraversal
498 wxTreeTraversal(const wxTreeCtrl
*tree
)
503 // give it a virtual dtor: not really needed as the class is never used
504 // polymorphically and not even allocated on heap at all, but this is safer
505 // (in case it ever is) and silences the compiler warnings for now
506 virtual ~wxTreeTraversal() { }
508 // do traverse the tree: visit all items (recursively by default) under the
509 // given one; return true if all items were traversed or false if the
510 // traversal was aborted because OnVisit returned false
511 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
513 // override this function to do whatever is needed for each item, return
514 // false to stop traversing
515 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
518 const wxTreeCtrl
*GetTree() const { return m_tree
; }
521 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
523 const wxTreeCtrl
*m_tree
;
525 wxDECLARE_NO_COPY_CLASS(wxTreeTraversal
);
528 // internal class for getting the selected items
529 class TraverseSelections
: public wxTreeTraversal
532 TraverseSelections(const wxTreeCtrl
*tree
,
533 wxArrayTreeItemIds
& selections
)
534 : wxTreeTraversal(tree
), m_selections(selections
)
536 m_selections
.Empty();
538 if (tree
->GetCount() > 0)
539 DoTraverse(tree
->GetRootItem());
542 virtual bool OnVisit(const wxTreeItemId
& item
)
544 const wxTreeCtrl
* const tree
= GetTree();
546 // can't visit a virtual node.
547 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
552 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
554 m_selections
.Add(item
);
560 size_t GetCount() const { return m_selections
.GetCount(); }
563 wxArrayTreeItemIds
& m_selections
;
565 wxDECLARE_NO_COPY_CLASS(TraverseSelections
);
568 // internal class for counting tree items
569 class TraverseCounter
: public wxTreeTraversal
572 TraverseCounter(const wxTreeCtrl
*tree
,
573 const wxTreeItemId
& root
,
575 : wxTreeTraversal(tree
)
579 DoTraverse(root
, recursively
);
582 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
589 size_t GetCount() const { return m_count
; }
594 wxDECLARE_NO_COPY_CLASS(TraverseCounter
);
597 // ----------------------------------------------------------------------------
599 // ----------------------------------------------------------------------------
601 // ----------------------------------------------------------------------------
603 // ----------------------------------------------------------------------------
605 // indices in gs_expandEvents table below
620 // handy table for sending events - it has to be initialized during run-time
621 // now so can't be const any more
622 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
625 but logically it's a const table with the following entries:
628 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
629 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
633 // ============================================================================
635 // ============================================================================
637 // ----------------------------------------------------------------------------
639 // ----------------------------------------------------------------------------
641 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
643 if ( !OnVisit(root
) )
646 return Traverse(root
, recursively
);
649 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
651 wxTreeItemIdValue cookie
;
652 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
653 while ( child
.IsOk() )
655 // depth first traversal
656 if ( recursively
&& !Traverse(child
, true) )
659 if ( !OnVisit(child
) )
662 child
= m_tree
->GetNextChild(root
, cookie
);
668 // ----------------------------------------------------------------------------
669 // construction and destruction
670 // ----------------------------------------------------------------------------
672 void wxTreeCtrl::Init()
675 m_hasAnyAttr
= false;
679 m_pVirtualRoot
= NULL
;
680 m_dragStarted
= false;
682 m_changingSelection
= false;
683 m_triggerStateImageClick
= false;
684 m_mouseUpDeselect
= false;
686 // initialize the global array of events now as it can't be done statically
687 // with the wxEVT_XXX values being allocated during run-time only
688 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
689 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
690 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
691 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
694 bool wxTreeCtrl::Create(wxWindow
*parent
,
699 const wxValidator
& validator
,
700 const wxString
& name
)
704 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
705 style
|= wxBORDER_SUNKEN
;
707 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
711 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
712 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
714 if ( !(m_windowStyle
& wxTR_NO_LINES
) )
715 wstyle
|= TVS_HASLINES
;
716 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
717 wstyle
|= TVS_HASBUTTONS
;
719 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
720 wstyle
|= TVS_EDITLABELS
;
722 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
723 wstyle
|= TVS_LINESATROOT
;
725 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
727 if ( wxApp::GetComCtl32Version() >= 471 )
728 wstyle
|= TVS_FULLROWSELECT
;
731 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
732 // Need so that TVN_GETINFOTIP messages will be sent
733 wstyle
|= TVS_INFOTIP
;
736 // Create the tree control.
737 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
740 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
741 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
743 wxSetCCUnicodeFormat(GetHwnd());
745 if ( m_windowStyle
& wxTR_TWIST_BUTTONS
)
747 // Under Vista and later Explorer uses rotating ("twist") buttons
748 // instead of the default "+/-" ones so apply its theme to the tree
749 // control to implement this style.
750 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
752 if ( wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive() )
754 theme
->SetWindowTheme(GetHwnd(), L
"EXPLORER", NULL
);
762 wxTreeCtrl::~wxTreeCtrl()
764 // delete any attributes
767 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
769 // prevent TVN_DELETEITEM handler from deleting the attributes again!
770 m_hasAnyAttr
= false;
775 // delete user data to prevent memory leaks
776 // also deletes hidden root node storage.
780 // ----------------------------------------------------------------------------
782 // ----------------------------------------------------------------------------
784 /* static */ wxVisualAttributes
785 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
787 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
789 // common controls have their own default font
790 attrs
.font
= wxGetCCDefaultFont();
796 // simple wrappers which add error checking in debug mode
798 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
800 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
801 wxT("can't retrieve virtual root item") );
803 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
805 wxLogLastError(wxT("TreeView_GetItem"));
813 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
815 TreeItemUnlocker
unlocker(tvItem
->hItem
);
817 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
819 wxLogLastError(wxT("TreeView_SetItem"));
823 unsigned int wxTreeCtrl::GetCount() const
825 return (unsigned int)TreeView_GetCount(GetHwnd());
828 unsigned int wxTreeCtrl::GetIndent() const
830 return TreeView_GetIndent(GetHwnd());
833 void wxTreeCtrl::SetIndent(unsigned int indent
)
835 TreeView_SetIndent(GetHwnd(), indent
);
838 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
841 (void) TreeView_SetImageList(GetHwnd(),
842 imageList
? imageList
->GetHIMAGELIST() : 0,
846 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
848 if (m_ownsImageListNormal
)
849 delete m_imageListNormal
;
851 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
852 m_ownsImageListNormal
= false;
855 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
857 if (m_ownsImageListState
) delete m_imageListState
;
858 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
859 m_ownsImageListState
= false;
862 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
863 bool recursively
) const
865 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
867 TraverseCounter
counter(this, item
, recursively
);
868 return counter
.GetCount() - 1;
871 // ----------------------------------------------------------------------------
873 // ----------------------------------------------------------------------------
875 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
877 if ( !wxWindowBase::SetBackgroundColour(colour
) )
880 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
885 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
887 if ( !wxWindowBase::SetForegroundColour(colour
) )
890 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
895 // ----------------------------------------------------------------------------
897 // ----------------------------------------------------------------------------
899 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
901 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
904 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
906 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
908 wxChar buf
[512]; // the size is arbitrary...
910 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
911 tvItem
.pszText
= buf
;
912 tvItem
.cchTextMax
= WXSIZEOF(buf
);
913 if ( !DoGetItem(&tvItem
) )
915 // don't return some garbage which was on stack, but an empty string
919 return wxString(buf
);
922 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
924 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
926 if ( IS_VIRTUAL_ROOT(item
) )
929 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
930 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
933 // when setting the text of the item being edited, the text control should
934 // be updated to reflect the new text as well, otherwise calling
935 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
937 // don't use GetEditControl() here because m_textCtrl is not set yet
938 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
941 if ( item
== m_idEdited
)
943 ::SetWindowText(hwndEdit
, text
.wx_str());
948 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
949 wxTreeItemIcon which
) const
951 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
953 if ( IsHiddenRoot(item
) )
955 // no images for hidden root item
959 wxTreeItemParam
*param
= GetItemParam(item
);
961 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
964 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
965 wxTreeItemIcon which
)
967 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
968 wxCHECK_RET( which
>= 0 &&
969 which
< wxTreeItemIcon_Max
,
970 wxT("invalid image index"));
973 if ( IsHiddenRoot(item
) )
975 // no images for hidden root item
979 wxTreeItemParam
*data
= GetItemParam(item
);
983 data
->SetImage(image
, which
);
988 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
990 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
992 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
994 // hidden root may still have data.
995 if ( IS_VIRTUAL_ROOT(item
) )
997 return GET_VIRTUAL_ROOT()->GetParam();
1001 if ( !DoGetItem(&tvItem
) )
1006 return (wxTreeItemParam
*)tvItem
.lParam
;
1009 bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent
& event
) const
1011 if ( event
.m_item
.IsOk() )
1013 event
.SetClientObject(GetItemData(event
.m_item
));
1016 return HandleWindowEvent(event
);
1019 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1021 wxTreeItemParam
*data
= GetItemParam(item
);
1023 return data
? data
->GetData() : NULL
;
1026 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1028 // first, associate this piece of data with this item
1034 wxTreeItemParam
*param
= GetItemParam(item
);
1036 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1038 param
->SetData(data
);
1041 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1043 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1045 if ( IS_VIRTUAL_ROOT(item
) )
1048 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1049 tvItem
.cChildren
= (int)has
;
1053 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1055 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1057 if ( IS_VIRTUAL_ROOT(item
) )
1060 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1061 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1065 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1067 if ( IS_VIRTUAL_ROOT(item
) )
1070 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1071 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1075 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1077 if ( IS_VIRTUAL_ROOT(item
) )
1081 if ( GetBoundingRect(item
, rect
) )
1087 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1089 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1091 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1092 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1095 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1097 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1099 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1100 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1103 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1105 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1107 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1108 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1111 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1112 const wxColour
& col
)
1114 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1116 wxTreeItemAttr
*attr
;
1117 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1118 if ( it
== m_attrs
.end() )
1120 m_hasAnyAttr
= true;
1122 m_attrs
[item
.m_pItem
] =
1123 attr
= new wxTreeItemAttr
;
1130 attr
->SetTextColour(col
);
1135 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1136 const wxColour
& col
)
1138 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1140 wxTreeItemAttr
*attr
;
1141 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1142 if ( it
== m_attrs
.end() )
1144 m_hasAnyAttr
= true;
1146 m_attrs
[item
.m_pItem
] =
1147 attr
= new wxTreeItemAttr
;
1149 else // already in the hash
1154 attr
->SetBackgroundColour(col
);
1159 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1161 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1163 wxTreeItemAttr
*attr
;
1164 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1165 if ( it
== m_attrs
.end() )
1167 m_hasAnyAttr
= true;
1169 m_attrs
[item
.m_pItem
] =
1170 attr
= new wxTreeItemAttr
;
1172 else // already in the hash
1177 attr
->SetFont(font
);
1179 // Reset the item's text to ensure that the bounding rect will be adjusted
1180 // for the new font.
1181 SetItemText(item
, GetItemText(item
));
1186 // ----------------------------------------------------------------------------
1188 // ----------------------------------------------------------------------------
1190 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1192 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1194 if ( item
== wxTreeItemId(TVI_ROOT
) )
1196 // virtual (hidden) root is never visible
1200 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1203 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1204 // the HTREEITEM with TVM_GETITEMRECT
1205 *(HTREEITEM
*)&rect
= HITEM(item
);
1207 // true means to get rect for just the text, not the whole line
1208 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1210 // if TVM_GETITEMRECT returned false, then the item is definitely not
1211 // visible (because its parent is not expanded)
1215 // however if it returned true, the item might still be outside the
1216 // currently visible part of the tree, test for it (notice that partly
1217 // visible means visible here)
1218 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1221 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1223 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1225 if ( IS_VIRTUAL_ROOT(item
) )
1227 wxTreeItemIdValue cookie
;
1228 return GetFirstChild(item
, cookie
).IsOk();
1231 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1234 return tvItem
.cChildren
!= 0;
1237 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1239 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1241 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1244 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1247 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1249 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1251 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1254 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1257 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1259 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1261 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1264 return (tvItem
.state
& TVIS_BOLD
) != 0;
1267 // ----------------------------------------------------------------------------
1269 // ----------------------------------------------------------------------------
1271 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1273 // Root may be real (visible) or virtual (hidden).
1274 if ( GET_VIRTUAL_ROOT() )
1277 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1280 wxTreeItemId
wxTreeCtrl::GetSelection() const
1282 wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE
), wxTreeItemId(),
1283 wxT("this only works with single selection controls") );
1285 return GetFocusedItem();
1288 wxTreeItemId
wxTreeCtrl::GetFocusedItem() const
1290 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1293 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1295 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1299 if ( IS_VIRTUAL_ROOT(item
) )
1301 // no parent for the virtual root
1306 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1307 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1309 // the top level items should have the virtual root as their parent
1314 return wxTreeItemId(hItem
);
1317 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1318 wxTreeItemIdValue
& cookie
) const
1320 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1322 // remember the last child returned in 'cookie'
1323 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1325 return wxTreeItemId(cookie
);
1328 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1329 wxTreeItemIdValue
& cookie
) const
1331 wxTreeItemId
fromCookie(cookie
);
1333 HTREEITEM hitem
= HITEM(fromCookie
);
1335 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1337 wxTreeItemId
item(hitem
);
1339 cookie
= item
.m_pItem
;
1344 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1346 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1348 // can this be done more efficiently?
1349 wxTreeItemIdValue cookie
;
1351 wxTreeItemId childLast
,
1352 child
= GetFirstChild(item
, cookie
);
1353 while ( child
.IsOk() )
1356 child
= GetNextChild(item
, cookie
);
1362 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1364 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1365 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1368 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1370 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1371 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1374 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1376 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1379 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1381 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1382 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1384 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1385 if ( next
.IsOk() && !IsVisible(next
) )
1387 // Win32 considers that any non-collapsed item is visible while we want
1388 // to return only really visible items
1395 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1397 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1398 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1400 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1401 if ( prev
.IsOk() && !IsVisible(prev
) )
1403 // just as above, Win32 function will happily return the previous item
1404 // in the tree for the first visible item too
1411 // ----------------------------------------------------------------------------
1412 // multiple selections emulation
1413 // ----------------------------------------------------------------------------
1415 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1417 TraverseSelections
selector(this, selections
);
1419 return selector
.GetCount();
1422 // ----------------------------------------------------------------------------
1424 // ----------------------------------------------------------------------------
1426 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1427 const wxTreeItemId
& hInsertAfter
,
1428 const wxString
& text
,
1429 int image
, int selectedImage
,
1430 wxTreeItemData
*data
)
1432 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1434 wxT("can't have more than one root in the tree") );
1436 TV_INSERTSTRUCT tvIns
;
1437 tvIns
.hParent
= HITEM(parent
);
1438 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1440 // this is how we insert the item as the first child: supply a NULL
1442 if ( !tvIns
.hInsertAfter
)
1444 tvIns
.hInsertAfter
= TVI_FIRST
;
1448 if ( !text
.empty() )
1451 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1455 tvIns
.item
.pszText
= NULL
;
1456 tvIns
.item
.cchTextMax
= 0;
1459 // create the param which will store the other item parameters
1460 wxTreeItemParam
*param
= new wxTreeItemParam
;
1462 // we return the images on demand as they depend on whether the item is
1463 // expanded or collapsed too in our case
1464 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1465 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1466 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1468 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1469 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1472 tvIns
.item
.lParam
= (LPARAM
)param
;
1473 tvIns
.item
.mask
= mask
;
1475 // don't use the hack below for the children of hidden root: this results
1476 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1477 const bool firstChild
= !IsHiddenRoot(parent
) &&
1478 !TreeView_GetChild(GetHwnd(), HITEM(parent
));
1480 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1483 wxLogLastError(wxT("TreeView_InsertItem"));
1486 // apparently some Windows versions (2000 and XP are reported to do this)
1487 // sometimes don't refresh the tree after adding the first child and so we
1488 // need this to make the "[+]" appear
1492 TreeView_GetItemRect(GetHwnd(), HITEM(parent
), &rect
, FALSE
);
1493 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1496 // associate the application tree item with Win32 tree item handle
1499 // setup wxTreeItemData
1502 param
->SetData(data
);
1506 return wxTreeItemId(id
);
1509 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1510 int image
, int selectedImage
,
1511 wxTreeItemData
*data
)
1513 if ( HasFlag(wxTR_HIDE_ROOT
) )
1515 wxASSERT_MSG( !m_pVirtualRoot
, wxT("tree can have only a single root") );
1517 // create a virtual root item, the parent for all the others
1518 wxTreeItemParam
*param
= new wxTreeItemParam
;
1519 param
->SetData(data
);
1521 m_pVirtualRoot
= new wxVirtualNode(param
);
1526 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1527 text
, image
, selectedImage
, data
);
1530 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1532 const wxString
& text
,
1533 int image
, int selectedImage
,
1534 wxTreeItemData
*data
)
1536 wxTreeItemId idPrev
;
1537 if ( index
== (size_t)-1 )
1539 // special value: append to the end
1542 else // find the item from index
1544 wxTreeItemIdValue cookie
;
1545 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1546 while ( index
!= 0 && idCur
.IsOk() )
1551 idCur
= GetNextChild(parent
, cookie
);
1554 // assert, not check: if the index is invalid, we will append the item
1556 wxASSERT_MSG( index
== 0, wxT("bad index in wxTreeCtrl::InsertItem") );
1559 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1562 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1564 // unlock tree selections on vista, without this the
1565 // tree ctrl will eventually crash after item deletion
1566 TreeItemUnlocker unlock_all
;
1568 if ( HasFlag(wxTR_MULTIPLE
) )
1570 bool selected
= IsSelected(item
);
1575 next
= TreeView_GetNextVisible(GetHwnd(), HITEM(item
));
1579 next
= TreeView_GetPrevVisible(GetHwnd(), HITEM(item
));
1584 TempSetter
set(m_changingSelection
);
1585 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1587 wxLogLastError(wxT("TreeView_DeleteItem"));
1597 if ( item
== m_htSelStart
)
1598 m_htSelStart
.Unset();
1600 if ( item
== m_htClickedItem
)
1601 m_htClickedItem
.Unset();
1605 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
1607 if ( IsTreeEventAllowed(changingEvent
) )
1609 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
1610 (void)HandleTreeEvent(changedEvent
);
1614 DoUnselectItem(next
);
1621 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1623 wxLogLastError(wxT("TreeView_DeleteItem"));
1628 // delete all children (but don't delete the item itself)
1629 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1631 // unlock tree selections on vista for the duration of this call
1632 TreeItemUnlocker unlock_all
;
1634 wxTreeItemIdValue cookie
;
1636 wxArrayTreeItemIds children
;
1637 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1638 while ( child
.IsOk() )
1640 children
.Add(child
);
1642 child
= GetNextChild(item
, cookie
);
1645 size_t nCount
= children
.Count();
1646 for ( size_t n
= 0; n
< nCount
; n
++ )
1648 Delete(children
[n
]);
1652 void wxTreeCtrl::DeleteAllItems()
1654 // unlock tree selections on vista for the duration of this call
1655 TreeItemUnlocker unlock_all
;
1657 // invalidate all the items we store as they're going to become invalid
1659 m_htClickedItem
= wxTreeItemId();
1661 // delete the "virtual" root item.
1662 if ( GET_VIRTUAL_ROOT() )
1664 delete GET_VIRTUAL_ROOT();
1665 m_pVirtualRoot
= NULL
;
1668 // and all the real items
1670 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1672 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1676 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1678 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1679 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1680 flag
== TVE_EXPAND
||
1682 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1684 // A hidden root can be neither expanded nor collapsed.
1685 wxCHECK_RET( !IsHiddenRoot(item
),
1686 wxT("Can't expand/collapse hidden root node!") );
1688 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1689 // emulate them. This behaviour has changed slightly with comctl32.dll
1690 // v 4.70 - now it does send them but only the first time. To maintain
1691 // compatible behaviour and also in order to not have surprises with the
1692 // future versions, don't rely on this and still do everything ourselves.
1693 // To avoid that the messages be sent twice when the item is expanded for
1694 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1696 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1700 if ( IsExpanded(item
) )
1702 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
,
1703 this, wxTreeItemId(item
));
1705 if ( !IsTreeEventAllowed(event
) )
1709 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) )
1711 if ( IsExpanded(item
) )
1714 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, this, item
);
1715 (void)HandleTreeEvent(event
);
1717 //else: change didn't took place, so do nothing at all
1720 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1722 DoExpand(item
, TVE_EXPAND
);
1725 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1727 DoExpand(item
, TVE_COLLAPSE
);
1730 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1732 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1735 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1737 DoExpand(item
, TVE_TOGGLE
);
1740 void wxTreeCtrl::Unselect()
1742 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE
),
1743 wxT("doesn't make sense, may be you want UnselectAll()?") );
1745 // the current focus
1746 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1753 if ( HasFlag(wxTR_MULTIPLE
) )
1755 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
1756 this, wxTreeItemId());
1757 changingEvent
.m_itemOld
= htFocus
;
1759 if ( IsTreeEventAllowed(changingEvent
) )
1763 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1764 this, wxTreeItemId());
1765 changedEvent
.m_itemOld
= htFocus
;
1766 (void)HandleTreeEvent(changedEvent
);
1775 void wxTreeCtrl::DoUnselectAll()
1777 wxArrayTreeItemIds selections
;
1778 size_t count
= GetSelections(selections
);
1780 for ( size_t n
= 0; n
< count
; n
++ )
1782 DoUnselectItem(selections
[n
]);
1785 m_htSelStart
.Unset();
1788 void wxTreeCtrl::UnselectAll()
1790 if ( HasFlag(wxTR_MULTIPLE
) )
1792 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1793 if ( !htFocus
) return;
1795 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1796 changingEvent
.m_itemOld
= htFocus
;
1798 if ( IsTreeEventAllowed(changingEvent
) )
1802 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1803 changedEvent
.m_itemOld
= htFocus
;
1804 (void)HandleTreeEvent(changedEvent
);
1813 void wxTreeCtrl::DoSelectChildren(const wxTreeItemId
& parent
)
1817 wxTreeItemIdValue cookie
;
1818 wxTreeItemId child
= GetFirstChild(parent
, cookie
);
1819 while ( child
.IsOk() )
1821 DoSelectItem(child
, true);
1822 child
= GetNextChild(child
, cookie
);
1826 void wxTreeCtrl::SelectChildren(const wxTreeItemId
& parent
)
1828 wxCHECK_RET( HasFlag(wxTR_MULTIPLE
),
1829 "this only works with multiple selection controls" );
1831 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1833 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1834 changingEvent
.m_itemOld
= htFocus
;
1836 if ( IsTreeEventAllowed(changingEvent
) )
1838 DoSelectChildren(parent
);
1840 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1841 changedEvent
.m_itemOld
= htFocus
;
1842 (void)HandleTreeEvent(changedEvent
);
1846 void wxTreeCtrl::DoSelectItem(const wxTreeItemId
& item
, bool select
)
1848 TempSetter
set(m_changingSelection
);
1850 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1853 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1855 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't select hidden root item") );
1857 if ( select
== IsSelected(item
) )
1859 // nothing to do, the item is already in the requested state
1863 if ( HasFlag(wxTR_MULTIPLE
) )
1865 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1867 if ( IsTreeEventAllowed(changingEvent
) )
1869 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1870 DoSelectItem(item
, select
);
1874 SetFocusedItem(item
);
1877 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1879 (void)HandleTreeEvent(changedEvent
);
1882 else // single selection
1884 wxTreeItemId itemOld
, itemNew
;
1887 itemOld
= GetSelection();
1890 else // deselecting the currently selected item
1893 // leave itemNew invalid
1896 // Recent versions of comctl32.dll send TVN_SELCHANG{ED,ING} events
1897 // when we call TreeView_SelectItem() but apparently some old ones did
1898 // not so send the events ourselves and ignore those generated by
1899 // TreeView_SelectItem() if m_changingSelection is set.
1901 changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, itemNew
);
1902 changingEvent
.SetOldItem(itemOld
);
1904 if ( IsTreeEventAllowed(changingEvent
) )
1906 TempSetter
set(m_changingSelection
);
1908 if ( !TreeView_SelectItem(GetHwnd(), HITEM(itemNew
)) )
1910 wxLogLastError(wxT("TreeView_SelectItem"));
1914 ::SetFocus(GetHwnd(), HITEM(item
));
1916 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1918 changedEvent
.SetOldItem(itemOld
);
1919 (void)HandleTreeEvent(changedEvent
);
1922 //else: program vetoed the change
1926 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1928 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't show hidden root item") );
1931 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1934 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1936 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1938 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1942 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1947 void wxTreeCtrl::DeleteTextCtrl()
1951 // the HWND corresponding to this control is deleted by the tree
1952 // control itself and we don't know when exactly this happens, so check
1953 // if the window still exists before calling UnsubclassWin()
1954 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1956 m_textCtrl
->SetHWND(0);
1959 m_textCtrl
->UnsubclassWin();
1960 m_textCtrl
->SetHWND(0);
1961 wxDELETE(m_textCtrl
);
1967 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1968 wxClassInfo
*textControlClass
)
1970 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1975 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1976 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1978 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1982 wxDELETE(m_textCtrl
);
1986 // textctrl is subclassed in MSWOnNotify
1990 // End label editing, optionally cancelling the edit
1991 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1993 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1998 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2000 TV_HITTESTINFO hitTestInfo
;
2001 hitTestInfo
.pt
.x
= (int)point
.x
;
2002 hitTestInfo
.pt
.y
= (int)point
.y
;
2004 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2009 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2010 flags |= wxTREE_HITTEST_##flag
2012 TRANSLATE_FLAG(ABOVE
);
2013 TRANSLATE_FLAG(BELOW
);
2014 TRANSLATE_FLAG(NOWHERE
);
2015 TRANSLATE_FLAG(ONITEMBUTTON
);
2016 TRANSLATE_FLAG(ONITEMICON
);
2017 TRANSLATE_FLAG(ONITEMINDENT
);
2018 TRANSLATE_FLAG(ONITEMLABEL
);
2019 TRANSLATE_FLAG(ONITEMRIGHT
);
2020 TRANSLATE_FLAG(ONITEMSTATEICON
);
2021 TRANSLATE_FLAG(TOLEFT
);
2022 TRANSLATE_FLAG(TORIGHT
);
2024 #undef TRANSLATE_FLAG
2026 return wxTreeItemId(hitTestInfo
.hItem
);
2029 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2031 bool textOnly
) const
2035 // Virtual root items have no bounding rectangle
2036 if ( IS_VIRTUAL_ROOT(item
) )
2041 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2044 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2050 // couldn't retrieve rect: for example, item isn't visible
2055 void wxTreeCtrl::ClearFocusedItem()
2057 TempSetter
set(m_changingSelection
);
2059 if ( !TreeView_SelectItem(GetHwnd(), 0) )
2061 wxLogLastError(wxT("TreeView_SelectItem"));
2065 void wxTreeCtrl::SetFocusedItem(const wxTreeItemId
& item
)
2067 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2069 TempSetter
set(m_changingSelection
);
2071 ::SetFocus(GetHwnd(), HITEM(item
));
2074 void wxTreeCtrl::DoUnselectItem(const wxTreeItemId
& item
)
2076 TempSetter
set(m_changingSelection
);
2078 ::UnselectItem(GetHwnd(), HITEM(item
));
2081 void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId
& item
)
2083 TempSetter
set(m_changingSelection
);
2085 ::ToggleItemSelection(GetHwnd(), HITEM(item
));
2088 // ----------------------------------------------------------------------------
2090 // ----------------------------------------------------------------------------
2092 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2093 // functions such as IsDataIndirect()
2094 class wxTreeSortHelper
2097 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2100 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
2102 return ((wxTreeItemParam
*)lParam
)->GetItem();
2106 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2110 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2111 wxT("sorting tree without data doesn't make sense") );
2113 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2115 return tree
->OnCompareItems(GetIdFromData(pItem1
),
2116 GetIdFromData(pItem2
));
2119 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2121 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2123 // rely on the fact that TreeView_SortChildren does the same thing as our
2124 // default behaviour, i.e. sorts items alphabetically and so call it
2125 // directly if we're not in derived class (much more efficient!)
2126 // RN: Note that if you find you're code doesn't sort as expected this
2127 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
2128 // combo for your derived wxTreeCtrl if will sort without
2130 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2132 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2137 tvSort
.hParent
= HITEM(item
);
2138 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2139 tvSort
.lParam
= (LPARAM
)this;
2140 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2144 // ----------------------------------------------------------------------------
2146 // ----------------------------------------------------------------------------
2148 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
2150 if ( msg
->message
== WM_KEYDOWN
)
2152 // Only eat VK_RETURN if not being used by the application in
2153 // conjunction with modifiers
2154 if ( (msg
->wParam
== VK_RETURN
) && !wxIsAnyModifierDown() )
2156 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2161 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
2164 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
2166 const int id
= (signed short)id_
;
2168 if ( cmd
== EN_UPDATE
)
2170 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2171 event
.SetEventObject( this );
2172 ProcessCommand(event
);
2174 else if ( cmd
== EN_KILLFOCUS
)
2176 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2177 event
.SetEventObject( this );
2178 ProcessCommand(event
);
2186 // command processed
2190 bool wxTreeCtrl::MSWIsOnItem(unsigned flags
) const
2192 unsigned mask
= TVHT_ONITEM
;
2193 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2194 mask
|= TVHT_ONITEMINDENT
| TVHT_ONITEMRIGHT
;
2196 return (flags
& mask
) != 0;
2199 bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey
)
2201 const bool bCtrl
= wxIsCtrlDown();
2202 const bool bShift
= wxIsShiftDown();
2203 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2212 if ( vkey
!= VK_RETURN
&& bCtrl
)
2214 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2216 changingEvent
.m_itemOld
= htSel
;
2218 if ( IsTreeEventAllowed(changingEvent
) )
2220 DoToggleItemSelection(wxTreeItemId(htSel
));
2222 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2224 changedEvent
.m_itemOld
= htSel
;
2225 (void)HandleTreeEvent(changedEvent
);
2230 wxArrayTreeItemIds selections
;
2231 size_t count
= GetSelections(selections
);
2233 if ( count
!= 1 || HITEM(selections
[0]) != htSel
)
2235 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2237 changingEvent
.m_itemOld
= htSel
;
2239 if ( IsTreeEventAllowed(changingEvent
) )
2242 DoSelectItem(wxTreeItemId(htSel
));
2244 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2246 changedEvent
.m_itemOld
= htSel
;
2247 (void)HandleTreeEvent(changedEvent
);
2255 if ( !bCtrl
&& !bShift
)
2257 wxArrayTreeItemIds selections
;
2262 next
= vkey
== VK_UP
2263 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2264 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2268 next
= GetRootItem();
2270 if ( IsHiddenRoot(next
) )
2271 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2279 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2281 changingEvent
.m_itemOld
= htSel
;
2283 if ( IsTreeEventAllowed(changingEvent
) )
2287 SetFocusedItem(next
);
2289 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2291 changedEvent
.m_itemOld
= htSel
;
2292 (void)HandleTreeEvent(changedEvent
);
2297 wxTreeItemId next
= vkey
== VK_UP
2298 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2299 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2306 if ( !m_htSelStart
)
2308 m_htSelStart
= htSel
;
2311 if ( bShift
&& SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2312 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2314 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2315 changingEvent
.m_itemOld
= htSel
;
2317 if ( IsTreeEventAllowed(changingEvent
) )
2319 SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2320 SR_UNSELECT_OTHERS
);
2322 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2323 changedEvent
.m_itemOld
= htSel
;
2324 (void)HandleTreeEvent(changedEvent
);
2328 SetFocusedItem(next
);
2333 if ( HasChildren(htSel
) && IsExpanded(htSel
) )
2339 wxTreeItemId next
= GetItemParent(htSel
);
2341 if ( next
.IsOk() && !IsHiddenRoot(next
) )
2343 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2345 changingEvent
.m_itemOld
= htSel
;
2347 if ( IsTreeEventAllowed(changingEvent
) )
2351 SetFocusedItem(next
);
2353 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2355 changedEvent
.m_itemOld
= htSel
;
2356 (void)HandleTreeEvent(changedEvent
);
2363 if ( !IsVisible(htSel
) )
2365 EnsureVisible(htSel
);
2368 if ( !HasChildren(htSel
) )
2371 if ( !IsExpanded(htSel
) )
2377 wxTreeItemId next
= TreeView_GetChild(GetHwnd(), htSel
);
2379 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2380 changingEvent
.m_itemOld
= htSel
;
2382 if ( IsTreeEventAllowed(changingEvent
) )
2386 SetFocusedItem(next
);
2388 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2389 changedEvent
.m_itemOld
= htSel
;
2390 (void)HandleTreeEvent(changedEvent
);
2398 wxTreeItemId next
= GetRootItem();
2400 if ( IsHiddenRoot(next
) )
2402 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2408 if ( vkey
== VK_END
)
2412 wxTreeItemId nextTemp
= TreeView_GetNextVisible(
2413 GetHwnd(), HITEM(next
));
2415 if ( !nextTemp
.IsOk() )
2422 if ( htSel
== HITEM(next
) )
2427 if ( !m_htSelStart
)
2429 m_htSelStart
= htSel
;
2432 if ( SelectRange(GetHwnd(),
2433 HITEM(m_htSelStart
), HITEM(next
),
2434 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2436 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2438 changingEvent
.m_itemOld
= htSel
;
2440 if ( IsTreeEventAllowed(changingEvent
) )
2442 SelectRange(GetHwnd(),
2443 HITEM(m_htSelStart
), HITEM(next
),
2444 SR_UNSELECT_OTHERS
);
2445 SetFocusedItem(next
);
2447 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2449 changedEvent
.m_itemOld
= htSel
;
2450 (void)HandleTreeEvent(changedEvent
);
2456 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2458 changingEvent
.m_itemOld
= htSel
;
2460 if ( IsTreeEventAllowed(changingEvent
) )
2464 SetFocusedItem(next
);
2466 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2468 changedEvent
.m_itemOld
= htSel
;
2469 (void)HandleTreeEvent(changedEvent
);
2479 wxTreeItemId firstVisible
= GetFirstVisibleItem();
2480 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2481 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2482 TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible
)) :
2483 TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible
));
2485 if ( !nextAdjacent
)
2490 wxTreeItemId nextStart
= firstVisible
;
2492 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2494 wxTreeItemId nextTemp
= (vkey
== VK_PRIOR
) ?
2495 TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart
)) :
2496 TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart
));
2498 if ( nextTemp
.IsOk() )
2500 nextStart
= nextTemp
;
2508 EnsureVisible(nextStart
);
2510 if ( vkey
== VK_NEXT
)
2512 wxTreeItemId nextEnd
= nextStart
;
2514 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2516 wxTreeItemId nextTemp
=
2517 TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd
));
2519 if ( nextTemp
.IsOk() )
2529 EnsureVisible(nextEnd
);
2534 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2535 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2536 TreeView_GetPrevVisible(GetHwnd(), htSel
) :
2537 TreeView_GetNextVisible(GetHwnd(), htSel
);
2539 if ( !nextAdjacent
)
2544 wxTreeItemId
next(htSel
);
2546 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2548 wxTreeItemId nextTemp
= vkey
== VK_PRIOR
?
2549 TreeView_GetPrevVisible(GetHwnd(), HITEM(next
)) :
2550 TreeView_GetNextVisible(GetHwnd(), HITEM(next
));
2552 if ( !nextTemp
.IsOk() )
2558 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2560 changingEvent
.m_itemOld
= htSel
;
2562 if ( IsTreeEventAllowed(changingEvent
) )
2565 m_htSelStart
.Unset();
2567 SetFocusedItem(next
);
2569 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2571 changedEvent
.m_itemOld
= htSel
;
2572 (void)HandleTreeEvent(changedEvent
);
2584 bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam
, WXLPARAM lParam
)
2586 wxTreeEvent
keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2587 keyEvent
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
, wParam
, lParam
);
2589 bool processed
= HandleTreeEvent(keyEvent
);
2591 // generate a separate event for Space/Return
2592 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
2593 ((wParam
== VK_SPACE
) || (wParam
== VK_RETURN
)) )
2595 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2598 wxTreeEvent
activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2600 (void)HandleTreeEvent(activatedEvent
);
2607 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2608 // only do it during dragging, minimize wxWin overhead (this is important for
2609 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2610 // instead of passing by wxWin events
2612 wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2614 bool processed
= false;
2616 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2618 if ( nMsg
== WM_CONTEXTMENU
)
2620 int x
= GET_X_LPARAM(lParam
),
2621 y
= GET_Y_LPARAM(lParam
);
2623 // the item for which the menu should be shown
2626 // the position where the menu should be shown in client coordinates
2627 // (so that it can be passed directly to PopupMenu())
2630 if ( x
== -1 || y
== -1 )
2632 // this means that the event was generated from keyboard (e.g. with
2633 // Shift-F10 or special Windows menu key)
2635 // use the Explorer standard of putting the menu at the left edge
2636 // of the text, in the vertical middle of the text
2637 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2640 // Use the bounding rectangle of only the text part
2642 GetBoundingRect(item
, rect
, true);
2643 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
2646 else // event from mouse, use mouse position
2648 pt
= ScreenToClient(wxPoint(x
, y
));
2650 TV_HITTESTINFO tvhti
;
2654 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2655 item
= wxTreeItemId(tvhti
.hItem
);
2661 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2663 event
.m_pointDrag
= pt
;
2665 if ( HandleTreeEvent(event
) )
2667 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2670 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2672 // we only process mouse messages here and these parameters have the
2673 // same meaning for all of them
2674 int x
= GET_X_LPARAM(lParam
),
2675 y
= GET_Y_LPARAM(lParam
);
2677 TV_HITTESTINFO tvht
;
2681 HTREEITEM htOldItem
= TreeView_GetSelection(GetHwnd());
2682 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2686 case WM_LBUTTONDOWN
:
2690 m_htClickedItem
.Unset();
2692 if ( !MSWIsOnItem(tvht
.flags
) )
2694 if ( tvht
.flags
& TVHT_ONITEMBUTTON
)
2696 // either it's going to be handled by user code or
2697 // we're going to use it ourselves to toggle the
2698 // branch, in either case don't pass it to the base
2699 // class which would generate another mouse click event
2700 // for it even though it's already handled here
2704 if ( !HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2706 if ( !IsExpanded(htItem
) )
2717 m_focusLost
= false;
2723 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2724 m_ptClick
= wxPoint(x
, y
);
2726 if ( wParam
& MK_CONTROL
)
2728 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2730 m_htClickedItem
.Unset();
2734 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2736 changingEvent
.m_itemOld
= htOldItem
;
2738 if ( IsTreeEventAllowed(changingEvent
) )
2740 // toggle selected state
2741 DoToggleItemSelection(wxTreeItemId(htItem
));
2743 SetFocusedItem(wxTreeItemId(htItem
));
2745 // reset on any click without Shift
2746 m_htSelStart
.Unset();
2748 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2750 changedEvent
.m_itemOld
= htOldItem
;
2751 (void)HandleTreeEvent(changedEvent
);
2754 else if ( wParam
& MK_SHIFT
)
2756 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2758 m_htClickedItem
.Unset();
2763 bool willChange
= true;
2765 if ( !(wParam
& MK_CONTROL
) )
2767 srFlags
|= SR_UNSELECT_OTHERS
;
2770 if ( !m_htSelStart
)
2772 // take the focused item
2773 m_htSelStart
= htOldItem
;
2777 willChange
= SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2778 htItem
, srFlags
| SR_SIMULATE
);
2783 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2785 changingEvent
.m_itemOld
= htOldItem
;
2787 if ( IsTreeEventAllowed(changingEvent
) )
2789 // this selects all items between the starting one
2793 SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2798 DoSelectItem(wxTreeItemId(htItem
));
2801 SetFocusedItem(wxTreeItemId(htItem
));
2803 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2805 changedEvent
.m_itemOld
= htOldItem
;
2806 (void)HandleTreeEvent(changedEvent
);
2810 else // normal click
2812 // avoid doing anything if we click on the only
2813 // currently selected item
2815 wxArrayTreeItemIds selections
;
2816 size_t count
= GetSelections(selections
);
2820 HITEM(selections
[0]) != htItem
)
2822 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2824 m_htClickedItem
.Unset();
2828 // clear the previously selected items, if the user
2829 // clicked outside of the present selection, otherwise,
2830 // perform the deselection on mouse-up, this allows
2831 // multiple drag and drop to work.
2832 if ( !IsItemSelected(GetHwnd(), htItem
))
2834 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2836 changingEvent
.m_itemOld
= htOldItem
;
2838 if ( IsTreeEventAllowed(changingEvent
) )
2841 DoSelectItem(wxTreeItemId(htItem
));
2842 SetFocusedItem(wxTreeItemId(htItem
));
2844 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2846 changedEvent
.m_itemOld
= htOldItem
;
2847 (void)HandleTreeEvent(changedEvent
);
2852 SetFocusedItem(wxTreeItemId(htItem
));
2853 m_mouseUpDeselect
= true;
2856 else // click on a single selected item
2858 // don't interfere with the default processing in
2859 // WM_MOUSEMOVE handler below as the default window
2860 // proc will start the drag itself if we let have
2862 m_htClickedItem
.Unset();
2864 // prevent in-place editing from starting if focus lost
2865 // since previous click
2869 DoSelectItem(wxTreeItemId(htItem
));
2870 SetFocusedItem(wxTreeItemId(htItem
));
2878 // reset on any click without Shift
2879 m_htSelStart
.Unset();
2882 m_focusLost
= false;
2884 // we consumed the event so we need to trigger state image
2889 wxTreeItemId item
= HitTest(wxPoint(x
, y
), htFlags
);
2891 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2893 m_triggerStateImageClick
= true;
2898 case WM_RBUTTONDOWN
:
2905 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) || !htItem
)
2910 // default handler removes the highlight from the currently
2911 // focused item when right mouse button is pressed on another
2912 // one but keeps the remaining items highlighted, which is
2913 // confusing, so override this default behaviour
2914 if ( !IsItemSelected(GetHwnd(), htItem
) )
2916 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2918 changingEvent
.m_itemOld
= htOldItem
;
2920 if ( IsTreeEventAllowed(changingEvent
) )
2923 DoSelectItem(wxTreeItemId(htItem
));
2924 SetFocusedItem(wxTreeItemId(htItem
));
2926 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2928 changedEvent
.m_itemOld
= htOldItem
;
2929 (void)HandleTreeEvent(changedEvent
);
2937 if ( m_htClickedItem
)
2939 int cx
= abs(m_ptClick
.x
- x
);
2940 int cy
= abs(m_ptClick
.y
- y
);
2942 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2943 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2948 tv
.hdr
.hwndFrom
= GetHwnd();
2949 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2950 tv
.hdr
.code
= TVN_BEGINDRAG
;
2952 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2956 wxZeroMemory(tviAux
);
2958 tviAux
.hItem
= HITEM(m_htClickedItem
);
2959 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2960 tviAux
.stateMask
= 0xffffffff;
2961 TreeView_GetItem(GetHwnd(), &tviAux
);
2963 tv
.itemNew
.state
= tviAux
.state
;
2964 tv
.itemNew
.lParam
= tviAux
.lParam
;
2969 // do it before SendMessage() call below to avoid
2970 // reentrancies here if there is another WM_MOUSEMOVE
2971 // in the queue already
2972 m_htClickedItem
.Unset();
2974 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
2975 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2977 // don't pass it to the default window proc, it would
2978 // start dragging again
2982 #endif // __WXWINCE__
2987 m_dragImage
->Move(wxPoint(x
, y
));
2990 // highlight the item as target (hiding drag image is
2991 // necessary - otherwise the display will be corrupted)
2992 m_dragImage
->Hide();
2993 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2994 m_dragImage
->Show();
2997 #endif // wxUSE_DRAGIMAGE
3003 // deselect other items if needed
3006 if ( m_mouseUpDeselect
)
3008 m_mouseUpDeselect
= false;
3010 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
3012 changingEvent
.m_itemOld
= htOldItem
;
3014 if ( IsTreeEventAllowed(changingEvent
) )
3017 DoSelectItem(wxTreeItemId(htItem
));
3018 SetFocusedItem(wxTreeItemId(htItem
));
3020 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
3022 changedEvent
.m_itemOld
= htOldItem
;
3023 (void)HandleTreeEvent(changedEvent
);
3028 m_htClickedItem
.Unset();
3030 if ( m_triggerStateImageClick
)
3032 if ( tvht
.flags
& TVHT_ONITEMSTATEICON
)
3034 wxTreeEvent
event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
,
3036 (void)HandleTreeEvent(event
);
3038 m_triggerStateImageClick
= false;
3043 if ( !m_dragStarted
&& MSWIsOnItem(tvht
.flags
) )
3055 m_dragImage
->EndDrag();
3056 wxDELETE(m_dragImage
);
3058 // generate the drag end event
3059 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
,
3061 event
.m_pointDrag
= wxPoint(x
, y
);
3062 (void)HandleTreeEvent(event
);
3064 // if we don't do it, the tree seems to think that 2 items
3065 // are selected simultaneously which is quite weird
3066 TreeView_SelectDropTarget(GetHwnd(), 0);
3068 #endif // wxUSE_DRAGIMAGE
3070 if ( isMultiple
&& nMsg
== WM_RBUTTONUP
)
3074 nmhdr
.hwndFrom
= GetHwnd();
3075 nmhdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
3076 nmhdr
.code
= NM_RCLICK
;
3077 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY
,
3078 nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
3082 m_dragStarted
= false;
3087 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) )
3091 // the tree control greys out the selected item when it loses focus
3092 // and paints it as selected again when it regains it, but it won't
3093 // do it for the other items itself - help it
3094 wxArrayTreeItemIds selections
;
3095 size_t count
= GetSelections(selections
);
3098 for ( size_t n
= 0; n
< count
; n
++ )
3100 // TreeView_GetItemRect() will return false if item is not
3101 // visible, which may happen perfectly well
3102 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
3105 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
3110 if ( nMsg
== WM_KILLFOCUS
)
3115 else if ( (nMsg
== WM_KEYDOWN
|| nMsg
== WM_SYSKEYDOWN
) && isMultiple
)
3117 // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
3118 // notification but for the keys which can be used to change selection
3119 // we need to do it from here so as to not apply the default behaviour
3120 // if the events are handled by the user code
3133 if ( !HandleKeyDown(wParam
, lParam
) &&
3134 !MSWHandleTreeKeyDownEvent(wParam
, lParam
) )
3136 // use the key to update the selection if it was left
3138 MSWHandleSelectionKey(wParam
);
3141 // pretend that we did process it in any case as we already
3142 // generated an event for it
3145 //default: for all the other keys leave processed as false so that
3146 // the tree control generates a TVN_KEYDOWN for us
3150 else if ( nMsg
== WM_COMMAND
)
3152 // if we receive a EN_KILLFOCUS command from the in-place edit control
3153 // used for label editing, make sure to end editing
3156 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
3158 if ( cmd
== EN_KILLFOCUS
)
3160 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
3170 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
3176 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
3178 if ( nMsg
== WM_CHAR
)
3180 // don't let the control process Space and Return keys because it
3181 // doesn't do anything useful with them anyhow but always beeps
3182 // annoyingly when it receives them and there is no way to turn it off
3183 // simply if you just process TREEITEM_ACTIVATED event to which Space
3184 // and Enter presses are mapped in your code
3185 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
3189 else if ( nMsg
== WM_KEYDOWN
)
3191 if ( wParam
== VK_ESCAPE
)
3195 m_dragImage
->EndDrag();
3196 wxDELETE(m_dragImage
);
3198 // if we don't do it, the tree seems to think that 2 items
3199 // are selected simultaneously which is quite weird
3200 TreeView_SelectDropTarget(GetHwnd(), 0);
3204 #endif // wxUSE_DRAGIMAGE
3206 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
3209 // process WM_NOTIFY Windows message
3210 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
3212 wxTreeEvent
event(wxEVT_NULL
, this);
3213 wxEventType eventType
= wxEVT_NULL
;
3214 NMHDR
*hdr
= (NMHDR
*)lParam
;
3216 switch ( hdr
->code
)
3219 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3222 case TVN_BEGINRDRAG
:
3224 if ( eventType
== wxEVT_NULL
)
3225 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
3226 //else: left drag, already set above
3228 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3230 event
.m_item
= tv
->itemNew
.hItem
;
3231 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
3233 // don't allow dragging by default: the user code must
3234 // explicitly say that it wants to allow it to avoid breaking
3240 case TVN_BEGINLABELEDIT
:
3242 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
3243 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3245 // although the user event handler may still veto it, it is
3246 // important to set it now so that calls to SetItemText() from
3247 // the event handler would change the text controls contents
3249 event
.m_item
= info
->item
.hItem
;
3250 event
.m_label
= info
->item
.pszText
;
3251 event
.m_editCancelled
= false;
3255 case TVN_DELETEITEM
:
3257 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
3258 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3260 event
.m_item
= tv
->itemOld
.hItem
;
3264 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
3265 if ( it
!= m_attrs
.end() )
3274 case TVN_ENDLABELEDIT
:
3276 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
3277 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3279 event
.m_item
= info
->item
.hItem
;
3280 event
.m_label
= info
->item
.pszText
;
3281 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
3286 // These *must* not be removed or TVN_GETINFOTIP will
3287 // not be processed each time the mouse is moved
3288 // and the tooltip will only ever update once.
3297 #ifdef TVN_GETINFOTIP
3298 case TVN_GETINFOTIP
:
3300 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
3301 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
3303 // Which item are we trying to get a tooltip for?
3304 event
.m_item
= info
->hItem
;
3308 #endif // TVN_GETINFOTIP
3309 #endif // !__WXWINCE__
3311 case TVN_GETDISPINFO
:
3312 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
3315 case TVN_SETDISPINFO
:
3317 if ( eventType
== wxEVT_NULL
)
3318 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
3319 //else: get, already set above
3321 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3323 event
.m_item
= info
->item
.hItem
;
3327 case TVN_ITEMEXPANDING
:
3328 case TVN_ITEMEXPANDED
:
3330 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3333 switch ( tv
->action
)
3336 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
3344 what
= IDX_COLLAPSE
;
3348 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
3351 eventType
= gs_expandEvents
[what
][how
];
3353 event
.m_item
= tv
->itemNew
.hItem
;
3359 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
3361 // fabricate the lParam and wParam parameters sufficiently
3362 // similar to the ones from a "real" WM_KEYDOWN so that
3363 // CreateKeyEvent() works correctly
3364 return MSWHandleTreeKeyDownEvent(
3365 info
->wVKey
, (wxIsAltDown() ? KF_ALTDOWN
: 0) << 16);
3369 // Vista's tree control has introduced some problems with our
3370 // multi-selection tree. When TreeView_SelectItem() is called,
3371 // the wrong items are deselected.
3373 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3374 // that can be used to regulate this incorrect behavior. The
3375 // following messages will allow only the unlocked item's selection
3378 case TVN_ITEMCHANGINGA
:
3379 case TVN_ITEMCHANGINGW
:
3381 // we only need to handles these in multi-select trees
3382 if ( HasFlag(wxTR_MULTIPLE
) )
3384 // get info about the item about to be changed
3385 NMTVITEMCHANGE
* info
= (NMTVITEMCHANGE
*)lParam
;
3386 if (TreeItemUnlocker::IsLocked(info
->hItem
))
3388 // item's state is locked, don't allow the change
3389 // returning 1 will disallow the change
3395 // allow the state change
3399 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
3400 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3401 // we have to handle both messages:
3402 case TVN_SELCHANGEDA
:
3403 case TVN_SELCHANGEDW
:
3404 if ( !m_changingSelection
)
3406 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
3410 case TVN_SELCHANGINGA
:
3411 case TVN_SELCHANGINGW
:
3412 if ( !m_changingSelection
)
3414 if ( eventType
== wxEVT_NULL
)
3415 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
3416 //else: already set above
3418 if (hdr
->code
== TVN_SELCHANGINGW
||
3419 hdr
->code
== TVN_SELCHANGEDW
)
3421 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
3422 event
.m_item
= tv
->itemNew
.hItem
;
3423 event
.m_itemOld
= tv
->itemOld
.hItem
;
3427 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
3428 event
.m_item
= tv
->itemNew
.hItem
;
3429 event
.m_itemOld
= tv
->itemOld
.hItem
;
3433 // we receive this message from WM_LBUTTONDOWN handler inside
3434 // comctl32.dll and so before the click is passed to
3435 // DefWindowProc() which sets the focus to the window which was
3436 // clicked and this can lead to unexpected event sequences: for
3437 // example, we may get a "selection change" event from the tree
3438 // before getting a "kill focus" event for the text control which
3439 // had the focus previously, thus breaking user code doing input
3442 // to avoid such surprises, we force the generation of focus events
3443 // now, before we generate the selection change ones
3444 if ( !m_changingSelection
)
3448 // instead of explicitly checking for _WIN32_IE, check if the
3449 // required symbols are available in the headers
3450 #if defined(CDDS_PREPAINT)
3453 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
3454 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
3455 switch ( nmcd
.dwDrawStage
)
3458 // if we've got any items with non standard attributes,
3459 // notify us before painting each item
3460 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
3463 // windows in TreeCtrl use one-based index for item state images,
3464 // 0 indexed image is not being used, we're using zero-based index,
3465 // so we have to add temp image (of zero index) to state image list
3466 // before we draw any item, then after items are drawn we have to
3467 // delete it (in POSTPAINT notify)
3468 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3470 typedef BOOL (wxSTDCALL
*ImageList_Copy_t
)
3471 (HIMAGELIST
, int, HIMAGELIST
, int, UINT
);
3472 static ImageList_Copy_t s_pfnImageList_Copy
= NULL
;
3473 static bool loaded
= false;
3477 wxLoadedDLL
dllComCtl32(wxT("comctl32.dll"));
3478 if ( dllComCtl32
.IsLoaded() )
3479 wxDL_INIT_FUNC(s_pfn
, ImageList_Copy
, dllComCtl32
);
3482 if ( !s_pfnImageList_Copy
)
3484 // this code is broken with ImageList_Copy()
3485 // but I don't care enough about Win95 support
3486 // to write it now -- if anybody does, please
3488 wxFAIL_MSG("TODO: implement this for Win95");
3493 hImageList
= GetHimagelistOf(m_imageListState
);
3495 // add temporary image
3497 m_imageListState
->GetSize(0, width
, height
);
3499 HBITMAP hbmpTemp
= ::CreateBitmap(width
, height
, 1, 1, NULL
);
3500 int index
= ::ImageList_Add(hImageList
, hbmpTemp
, hbmpTemp
);
3501 ::DeleteObject(hbmpTemp
);
3505 // move images to right
3506 for ( int i
= index
; i
> 0; i
-- )
3508 (*s_pfnImageList_Copy
)(hImageList
, i
,
3513 // we must remove the image in POSTPAINT notify
3514 *result
|= CDRF_NOTIFYPOSTPAINT
;
3519 case CDDS_POSTPAINT
:
3520 // we are deleting temp image of 0 index, which was
3521 // added before items were drawn (in PREPAINT notify)
3522 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3523 m_imageListState
->Remove(0);
3526 case CDDS_ITEMPREPAINT
:
3528 wxMapTreeAttr::iterator
3529 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
3531 if ( it
== m_attrs
.end() )
3533 // nothing to do for this item
3534 *result
= CDRF_DODEFAULT
;
3538 wxTreeItemAttr
* const attr
= it
->second
;
3540 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
3541 TVIF_STATE
, TVIS_DROPHILITED
);
3543 const UINT tvItemState
= tvItem
.state
;
3545 // selection colours should override ours,
3546 // otherwise it is too confusing to the user
3547 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
3548 !(tvItemState
& TVIS_DROPHILITED
) )
3551 if ( attr
->HasBackgroundColour() )
3553 colBack
= attr
->GetBackgroundColour();
3554 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
3558 // but we still want to keep the special foreground
3559 // colour when we don't have focus (we can't keep
3560 // it when we do, it would usually be unreadable on
3561 // the almost inverted bg colour...)
3562 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
3563 FindFocus() != this ) &&
3564 !(tvItemState
& TVIS_DROPHILITED
) )
3567 if ( attr
->HasTextColour() )
3569 colText
= attr
->GetTextColour();
3570 lptvcd
->clrText
= wxColourToRGB(colText
);
3574 if ( attr
->HasFont() )
3576 HFONT hFont
= GetHfontOf(attr
->GetFont());
3578 ::SelectObject(nmcd
.hdc
, hFont
);
3580 *result
= CDRF_NEWFONT
;
3582 else // no specific font
3584 *result
= CDRF_DODEFAULT
;
3590 *result
= CDRF_DODEFAULT
;
3594 // we always process it
3596 #endif // have owner drawn support in headers
3600 DWORD pos
= GetMessagePos();
3602 point
.x
= LOWORD(pos
);
3603 point
.y
= HIWORD(pos
);
3604 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
3606 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), htFlags
);
3608 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3610 event
.m_item
= item
;
3611 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
3620 TV_HITTESTINFO tvhti
;
3621 ::GetCursorPos(&tvhti
.pt
);
3622 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
3623 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
3625 if ( MSWIsOnItem(tvhti
.flags
) )
3627 event
.m_item
= tvhti
.hItem
;
3628 eventType
= (int)hdr
->code
== NM_DBLCLK
3629 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
3630 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
3632 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
3633 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
3642 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
3645 event
.SetEventType(eventType
);
3647 bool processed
= HandleTreeEvent(event
);
3650 switch ( hdr
->code
)
3653 // we translate NM_DBLCLK into ACTIVATED event and if the user
3654 // handled the activation of the item we shouldn't proceed with
3655 // also using the same double click for toggling the item expanded
3656 // state -- but OTOH do let the user to expand/collapse the item by
3657 // double clicking on it if the activation is not handled specially
3658 *result
= processed
;
3662 // prevent tree control from sending WM_CONTEXTMENU to our parent
3663 // (which it does if NM_RCLICK is not handled) because we want to
3664 // send it to the control itself
3668 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
3669 (WPARAM
)GetHwnd(), ::GetMessagePos());
3673 case TVN_BEGINRDRAG
:
3675 if ( event
.IsAllowed() )
3677 // normally this is impossible because the m_dragImage is
3678 // deleted once the drag operation is over
3679 wxASSERT_MSG( !m_dragImage
, wxT("starting to drag once again?") );
3681 m_dragImage
= new wxDragImage(*this, event
.m_item
);
3682 m_dragImage
->BeginDrag(wxPoint(0,0), this);
3683 m_dragImage
->Show();
3685 m_dragStarted
= true;
3687 #endif // wxUSE_DRAGIMAGE
3690 case TVN_DELETEITEM
:
3692 // NB: we might process this message using wxWidgets event
3693 // tables, but due to overhead of wxWin event system we
3694 // prefer to do it here ourself (otherwise deleting a tree
3695 // with many items is just too slow)
3696 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3698 wxTreeItemParam
*param
=
3699 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
3702 processed
= true; // Make sure we don't get called twice
3706 case TVN_BEGINLABELEDIT
:
3707 // return true to cancel label editing
3708 *result
= !event
.IsAllowed();
3710 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3711 if ( event
.IsAllowed() )
3713 HWND hText
= TreeView_GetEditControl(GetHwnd());
3716 // MBN: if m_textCtrl already has an HWND, it is a stale
3717 // pointer from a previous edit (because the user
3718 // didn't modify the label before dismissing the control,
3719 // and TVN_ENDLABELEDIT was not sent), so delete it
3720 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
3723 m_textCtrl
= new wxTextCtrl();
3724 m_textCtrl
->SetParent(this);
3725 m_textCtrl
->SetHWND((WXHWND
)hText
);
3726 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3728 // set wxTE_PROCESS_ENTER style for the text control to
3729 // force it to process the Enter presses itself, otherwise
3730 // they could be stolen from it by the dialog
3732 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3733 | wxTE_PROCESS_ENTER
);
3736 else // we had set m_idEdited before
3742 case TVN_ENDLABELEDIT
:
3743 // return true to set the label to the new string: note that we
3744 // also must pretend that we did process the message or it is going
3745 // to be passed to DefWindowProc() which will happily return false
3746 // cancelling the label change
3747 *result
= event
.IsAllowed();
3750 // ensure that we don't have the text ctrl which is going to be
3756 #ifdef TVN_GETINFOTIP
3757 case TVN_GETINFOTIP
:
3759 // If the user permitted a tooltip change, change it
3760 if (event
.IsAllowed())
3762 SetToolTip(event
.m_label
);
3769 case TVN_SELCHANGING
:
3770 case TVN_ITEMEXPANDING
:
3771 // return true to prevent the action from happening
3772 *result
= !event
.IsAllowed();
3775 case TVN_ITEMEXPANDED
:
3777 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3778 const wxTreeItemId
id(tv
->itemNew
.hItem
);
3780 if ( tv
->action
== TVE_COLLAPSE
)
3782 if ( wxApp::GetComCtl32Version() >= 600 )
3784 // for some reason the item selection rectangle depends
3785 // on whether it is expanded or collapsed (at least
3786 // with comctl32.dll v6): it is wider (by 3 pixels) in
3787 // the expanded state, so when the item collapses and
3788 // then is deselected the rightmost 3 pixels of the
3789 // previously drawn selection are left on the screen
3791 // it's not clear if it's a bug in comctl32.dll or in
3792 // our code (because it does not happen in Explorer but
3793 // OTOH we don't do anything which could result in this
3794 // AFAICS) but we do need to work around it to avoid
3801 // the item is also not refreshed properly after expansion when
3802 // it has an image depending on the expanded/collapsed state:
3803 // again, it's not clear if the bug is in comctl32.dll or our
3805 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3814 case TVN_GETDISPINFO
:
3815 // NB: so far the user can't set the image himself anyhow, so do it
3816 // anyway - but this may change later
3817 //if ( /* !processed && */ )
3819 wxTreeItemId item
= event
.m_item
;
3820 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3822 const wxTreeItemParam
* const param
= GetItemParam(item
);
3826 if ( info
->item
.mask
& TVIF_IMAGE
)
3831 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3832 : wxTreeItemIcon_Normal
3835 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3837 info
->item
.iSelectedImage
=
3840 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3841 : wxTreeItemIcon_Selected
3848 // for the other messages the return value is ignored and there is
3849 // nothing special to do
3854 // ----------------------------------------------------------------------------
3856 // ----------------------------------------------------------------------------
3858 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3859 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3861 int wxTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
3863 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
3865 // receive the desired information
3866 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3869 // state images are one-based
3870 return STATEIMAGEMASKTOINDEX(tvItem
.state
) - 1;
3873 void wxTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
3875 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
3877 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3879 // state images are one-based
3880 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3881 tvItem
.state
= INDEXTOSTATEIMAGEMASK(state
+ 1);
3886 #endif // wxUSE_TREECTRL