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 // Set this to 1 to be _absolutely_ sure that repainting will work for all
44 // comctl32.dll versions
45 #define wxUSE_COMCTL32_SAFELY 0
47 #include "wx/imaglist.h"
48 #include "wx/msw/dragimag.h"
50 // macros to hide the cast ugliness
51 // --------------------------------
53 // get HTREEITEM from wxTreeItemId
54 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
57 // older SDKs are missing these
58 #ifndef TVN_ITEMCHANGINGA
60 #define TVN_ITEMCHANGINGA (TVN_FIRST-16)
61 #define TVN_ITEMCHANGINGW (TVN_FIRST-17)
63 typedef struct tagNMTVITEMCHANGE
76 // this helper class is used on vista systems for preventing unwanted
77 // item state changes in the vista tree control. It is only effective in
78 // multi-select mode on vista systems.
80 // The vista tree control includes some new code that originally broke the
81 // multi-selection tree, causing seemingly spurious item selection state changes
82 // during Shift or Ctrl-click item selection. (To witness the original broken
83 // behavior, simply make IsLocked() below always return false). This problem was
84 // solved by using the following class to 'unlock' an item's selection state.
86 class TreeItemUnlocker
89 // unlock a single item
90 TreeItemUnlocker(HTREEITEM item
) { ms_unlockedItem
= item
; }
92 // unlock all items, don't use unless absolutely necessary
93 TreeItemUnlocker() { ms_unlockedItem
= (HTREEITEM
)-1; }
95 // lock everything back
96 ~TreeItemUnlocker() { ms_unlockedItem
= NULL
; }
99 // check if the item state is currently locked
100 static bool IsLocked(HTREEITEM item
)
101 { return ms_unlockedItem
!= (HTREEITEM
)-1 && item
!= ms_unlockedItem
; }
104 static HTREEITEM ms_unlockedItem
;
107 HTREEITEM
TreeItemUnlocker::ms_unlockedItem
= NULL
;
109 // another helper class: set the variable to true during its lifetime and reset
110 // it to false when it is destroyed
112 // it is currently always used with wxTreeCtrl::m_changingSelection
116 TempSetter(bool& var
) : m_var(var
)
118 wxASSERT_MSG( !m_var
, "variable shouldn't be already set" );
130 wxDECLARE_NO_COPY_CLASS(TempSetter
);
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 // wrappers for TreeView_GetItem/TreeView_SetItem
138 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
141 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
142 tvi
.stateMask
= TVIS_SELECTED
;
145 TreeItemUnlocker
unlocker(hItem
);
147 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
149 wxLogLastError(wxT("TreeView_GetItem"));
152 return (tvi
.state
& TVIS_SELECTED
) != 0;
155 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
158 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
159 tvi
.stateMask
= TVIS_SELECTED
;
160 tvi
.state
= select
? TVIS_SELECTED
: 0;
163 TreeItemUnlocker
unlocker(hItem
);
165 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
167 wxLogLastError(wxT("TreeView_SetItem"));
174 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
176 SelectItem(hwndTV
, htItem
, false);
179 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
181 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
184 // helper function which selects all items in a range and, optionally,
185 // deselects all the other ones
187 // returns true if the selection changed at all or false if nothing changed
189 // flags for SelectRange()
192 SR_SIMULATE
= 1, // don't do anything, just return true or false
193 SR_UNSELECT_OTHERS
= 2 // deselect the items not in range
196 static bool SelectRange(HWND hwndTV
,
201 // find the first (or last) item and select it
202 bool changed
= false;
204 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
206 while ( htItem
&& cont
)
208 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
210 if ( !IsItemSelected(hwndTV
, htItem
) )
212 if ( !(flags
& SR_SIMULATE
) )
214 SelectItem(hwndTV
, htItem
);
222 else // not first or last
224 if ( flags
& SR_UNSELECT_OTHERS
)
226 if ( IsItemSelected(hwndTV
, htItem
) )
228 if ( !(flags
& SR_SIMULATE
) )
229 UnselectItem(hwndTV
, htItem
);
236 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
239 // select the items in range
240 cont
= htFirst
!= htLast
;
241 while ( htItem
&& cont
)
243 if ( !IsItemSelected(hwndTV
, htItem
) )
245 if ( !(flags
& SR_SIMULATE
) )
247 SelectItem(hwndTV
, htItem
);
253 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
255 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
258 // optionally deselect the rest
259 if ( flags
& SR_UNSELECT_OTHERS
)
263 if ( IsItemSelected(hwndTV
, htItem
) )
265 if ( !(flags
& SR_SIMULATE
) )
267 UnselectItem(hwndTV
, htItem
);
273 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
277 // seems to be necessary - otherwise the just selected items don't always
278 // appear as selected
279 if ( !(flags
& SR_SIMULATE
) )
281 UpdateWindow(hwndTV
);
287 // helper function which tricks the standard control into changing the focused
288 // item without changing anything else (if someone knows why Microsoft doesn't
289 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
291 // returns true if the focus was changed, false if the given item was already
293 static bool SetFocus(HWND hwndTV
, HTREEITEM htItem
)
296 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
298 if ( htItem
== htFocus
)
303 // remember the selection state of the item
304 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
306 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
308 // prevent the tree from unselecting the old focus which it
309 // would do by default (TreeView_SelectItem unselects the
311 TreeView_SelectItem(hwndTV
, 0);
312 SelectItem(hwndTV
, htFocus
);
315 TreeView_SelectItem(hwndTV
, htItem
);
319 // need to clear the selection which TreeView_SelectItem() gave
321 UnselectItem(hwndTV
, htItem
);
323 //else: was selected, still selected - ok
327 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
329 // just clear the focus
330 TreeView_SelectItem(hwndTV
, 0);
332 if ( wasFocusSelected
)
334 // restore the selection state
335 SelectItem(hwndTV
, htFocus
);
342 // ----------------------------------------------------------------------------
344 // ----------------------------------------------------------------------------
346 // a convenient wrapper around TV_ITEM struct which adds a ctor
348 #pragma warning( disable : 4097 ) // inheriting from typedef
351 struct wxTreeViewItem
: public TV_ITEM
353 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
354 UINT mask_
, // fields which are valid
355 UINT stateMask_
= 0) // for TVIF_STATE only
359 // hItem member is always valid
360 mask
= mask_
| TVIF_HANDLE
;
361 stateMask
= stateMask_
;
366 // ----------------------------------------------------------------------------
367 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
369 // We need this for a couple of reasons:
371 // 1) This class is needed for support of different images: the Win32 common
372 // control natively supports only 2 images (the normal one and another for the
373 // selected state). We wish to provide support for 2 more of them for folder
374 // items (i.e. those which have children): for expanded state and for expanded
375 // selected state. For this we use this structure to store the additional items
378 // 2) This class is also needed to hold the HITEM so that we can sort
379 // it correctly in the MSW sort callback.
381 // In addition it makes other workarounds such as this easier and helps
382 // simplify the code.
383 // ----------------------------------------------------------------------------
385 class wxTreeItemParam
392 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
398 // dtor deletes the associated data as well
399 virtual ~wxTreeItemParam() { delete m_data
; }
402 // get the real data associated with the item
403 wxTreeItemData
*GetData() const { return m_data
; }
405 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
407 // do we have such image?
408 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
409 // get image, falling back to the other images if this one is not
411 int GetImage(wxTreeItemIcon which
) const
413 int image
= m_images
[which
];
418 case wxTreeItemIcon_SelectedExpanded
:
419 image
= GetImage(wxTreeItemIcon_Expanded
);
424 case wxTreeItemIcon_Selected
:
425 case wxTreeItemIcon_Expanded
:
426 image
= GetImage(wxTreeItemIcon_Normal
);
429 case wxTreeItemIcon_Normal
:
434 wxFAIL_MSG( wxT("unsupported wxTreeItemIcon value") );
440 // change the given image
441 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
444 const wxTreeItemId
& GetItem() const { return m_item
; }
446 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
449 // all the images associated with the item
450 int m_images
[wxTreeItemIcon_Max
];
452 // item for sort callbacks
455 // the real client data
456 wxTreeItemData
*m_data
;
458 wxDECLARE_NO_COPY_CLASS(wxTreeItemParam
);
461 // wxVirutalNode is used in place of a single root when 'hidden' root is
463 class wxVirtualNode
: public wxTreeViewItem
466 wxVirtualNode(wxTreeItemParam
*param
)
467 : wxTreeViewItem(TVI_ROOT
, 0)
477 wxTreeItemParam
*GetParam() const { return m_param
; }
478 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
481 wxTreeItemParam
*m_param
;
483 wxDECLARE_NO_COPY_CLASS(wxVirtualNode
);
487 #pragma warning( default : 4097 )
490 // a macro to get the virtual root, returns NULL if none
491 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
493 // returns true if the item is the virtual root
494 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
496 // a class which encapsulates the tree traversal logic: it vists all (unless
497 // OnVisit() returns false) items under the given one
498 class wxTreeTraversal
501 wxTreeTraversal(const wxTreeCtrl
*tree
)
506 // give it a virtual dtor: not really needed as the class is never used
507 // polymorphically and not even allocated on heap at all, but this is safer
508 // (in case it ever is) and silences the compiler warnings for now
509 virtual ~wxTreeTraversal() { }
511 // do traverse the tree: visit all items (recursively by default) under the
512 // given one; return true if all items were traversed or false if the
513 // traversal was aborted because OnVisit returned false
514 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
516 // override this function to do whatever is needed for each item, return
517 // false to stop traversing
518 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
521 const wxTreeCtrl
*GetTree() const { return m_tree
; }
524 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
526 const wxTreeCtrl
*m_tree
;
528 wxDECLARE_NO_COPY_CLASS(wxTreeTraversal
);
531 // internal class for getting the selected items
532 class TraverseSelections
: public wxTreeTraversal
535 TraverseSelections(const wxTreeCtrl
*tree
,
536 wxArrayTreeItemIds
& selections
)
537 : wxTreeTraversal(tree
), m_selections(selections
)
539 m_selections
.Empty();
541 if (tree
->GetCount() > 0)
542 DoTraverse(tree
->GetRootItem());
545 virtual bool OnVisit(const wxTreeItemId
& item
)
547 const wxTreeCtrl
* const tree
= GetTree();
549 // can't visit a virtual node.
550 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
555 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
557 m_selections
.Add(item
);
563 size_t GetCount() const { return m_selections
.GetCount(); }
566 wxArrayTreeItemIds
& m_selections
;
568 wxDECLARE_NO_COPY_CLASS(TraverseSelections
);
571 // internal class for counting tree items
572 class TraverseCounter
: public wxTreeTraversal
575 TraverseCounter(const wxTreeCtrl
*tree
,
576 const wxTreeItemId
& root
,
578 : wxTreeTraversal(tree
)
582 DoTraverse(root
, recursively
);
585 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
592 size_t GetCount() const { return m_count
; }
597 wxDECLARE_NO_COPY_CLASS(TraverseCounter
);
600 // ----------------------------------------------------------------------------
602 // ----------------------------------------------------------------------------
604 // ----------------------------------------------------------------------------
606 // ----------------------------------------------------------------------------
608 // indices in gs_expandEvents table below
623 // handy table for sending events - it has to be initialized during run-time
624 // now so can't be const any more
625 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
628 but logically it's a const table with the following entries:
631 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
632 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
636 // ============================================================================
638 // ============================================================================
640 // ----------------------------------------------------------------------------
642 // ----------------------------------------------------------------------------
644 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
646 if ( !OnVisit(root
) )
649 return Traverse(root
, recursively
);
652 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
654 wxTreeItemIdValue cookie
;
655 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
656 while ( child
.IsOk() )
658 // depth first traversal
659 if ( recursively
&& !Traverse(child
, true) )
662 if ( !OnVisit(child
) )
665 child
= m_tree
->GetNextChild(root
, cookie
);
671 // ----------------------------------------------------------------------------
672 // construction and destruction
673 // ----------------------------------------------------------------------------
675 void wxTreeCtrl::Init()
678 m_hasAnyAttr
= false;
682 m_pVirtualRoot
= NULL
;
683 m_dragStarted
= false;
685 m_changingSelection
= false;
686 m_triggerStateImageClick
= false;
687 m_mouseUpDeselect
= false;
689 // initialize the global array of events now as it can't be done statically
690 // with the wxEVT_XXX values being allocated during run-time only
691 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
692 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
693 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
694 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
697 bool wxTreeCtrl::Create(wxWindow
*parent
,
702 const wxValidator
& validator
,
703 const wxString
& name
)
707 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
708 style
|= wxBORDER_SUNKEN
;
710 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
714 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
715 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
717 if ( !(m_windowStyle
& wxTR_NO_LINES
) )
718 wstyle
|= TVS_HASLINES
;
719 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
720 wstyle
|= TVS_HASBUTTONS
;
722 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
723 wstyle
|= TVS_EDITLABELS
;
725 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
726 wstyle
|= TVS_LINESATROOT
;
728 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
730 if ( wxApp::GetComCtl32Version() >= 471 )
731 wstyle
|= TVS_FULLROWSELECT
;
734 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
735 // Need so that TVN_GETINFOTIP messages will be sent
736 wstyle
|= TVS_INFOTIP
;
739 // Create the tree control.
740 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
743 #if wxUSE_COMCTL32_SAFELY
744 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
745 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
747 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
748 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
750 // This works around a bug in the Windows tree control whereby for some versions
751 // of comctrl32, setting any colour actually draws the background in black.
752 // This will initialise the background to the system colour.
753 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
754 // Assume the user has an updated comctl32.dll.
755 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
756 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
757 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
760 wxSetCCUnicodeFormat(GetHwnd());
765 wxTreeCtrl::~wxTreeCtrl()
767 // delete any attributes
770 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
772 // prevent TVN_DELETEITEM handler from deleting the attributes again!
773 m_hasAnyAttr
= false;
778 // delete user data to prevent memory leaks
779 // also deletes hidden root node storage.
783 // ----------------------------------------------------------------------------
785 // ----------------------------------------------------------------------------
787 /* static */ wxVisualAttributes
788 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
790 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
792 // common controls have their own default font
793 attrs
.font
= wxGetCCDefaultFont();
799 // simple wrappers which add error checking in debug mode
801 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
803 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
804 wxT("can't retrieve virtual root item") );
806 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
808 wxLogLastError(wxT("TreeView_GetItem"));
816 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
818 TreeItemUnlocker
unlocker(tvItem
->hItem
);
820 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
822 wxLogLastError(wxT("TreeView_SetItem"));
826 unsigned int wxTreeCtrl::GetCount() const
828 return (unsigned int)TreeView_GetCount(GetHwnd());
831 unsigned int wxTreeCtrl::GetIndent() const
833 return TreeView_GetIndent(GetHwnd());
836 void wxTreeCtrl::SetIndent(unsigned int indent
)
838 TreeView_SetIndent(GetHwnd(), indent
);
841 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
844 (void) TreeView_SetImageList(GetHwnd(),
845 imageList
? imageList
->GetHIMAGELIST() : 0,
849 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
851 if (m_ownsImageListNormal
)
852 delete m_imageListNormal
;
854 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
855 m_ownsImageListNormal
= false;
858 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
860 if (m_ownsImageListState
) delete m_imageListState
;
861 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
862 m_ownsImageListState
= false;
865 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
866 bool recursively
) const
868 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
870 TraverseCounter
counter(this, item
, recursively
);
871 return counter
.GetCount() - 1;
874 // ----------------------------------------------------------------------------
876 // ----------------------------------------------------------------------------
878 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
880 #if !wxUSE_COMCTL32_SAFELY
881 if ( !wxWindowBase::SetBackgroundColour(colour
) )
884 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
890 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
892 #if !wxUSE_COMCTL32_SAFELY
893 if ( !wxWindowBase::SetForegroundColour(colour
) )
896 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
902 // ----------------------------------------------------------------------------
904 // ----------------------------------------------------------------------------
906 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
908 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
911 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
913 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
915 wxChar buf
[512]; // the size is arbitrary...
917 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
918 tvItem
.pszText
= buf
;
919 tvItem
.cchTextMax
= WXSIZEOF(buf
);
920 if ( !DoGetItem(&tvItem
) )
922 // don't return some garbage which was on stack, but an empty string
926 return wxString(buf
);
929 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
931 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
933 if ( IS_VIRTUAL_ROOT(item
) )
936 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
937 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
940 // when setting the text of the item being edited, the text control should
941 // be updated to reflect the new text as well, otherwise calling
942 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
944 // don't use GetEditControl() here because m_textCtrl is not set yet
945 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
948 if ( item
== m_idEdited
)
950 ::SetWindowText(hwndEdit
, text
.wx_str());
955 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
956 wxTreeItemIcon which
) const
958 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
960 if ( IsHiddenRoot(item
) )
962 // no images for hidden root item
966 wxTreeItemParam
*param
= GetItemParam(item
);
968 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
971 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
972 wxTreeItemIcon which
)
974 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
975 wxCHECK_RET( which
>= 0 &&
976 which
< wxTreeItemIcon_Max
,
977 wxT("invalid image index"));
980 if ( IsHiddenRoot(item
) )
982 // no images for hidden root item
986 wxTreeItemParam
*data
= GetItemParam(item
);
990 data
->SetImage(image
, which
);
995 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
997 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
999 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1001 // hidden root may still have data.
1002 if ( IS_VIRTUAL_ROOT(item
) )
1004 return GET_VIRTUAL_ROOT()->GetParam();
1008 if ( !DoGetItem(&tvItem
) )
1013 return (wxTreeItemParam
*)tvItem
.lParam
;
1016 bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent
& event
) const
1018 if ( event
.m_item
.IsOk() )
1020 event
.SetClientObject(GetItemData(event
.m_item
));
1023 return HandleWindowEvent(event
);
1026 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1028 wxTreeItemParam
*data
= GetItemParam(item
);
1030 return data
? data
->GetData() : NULL
;
1033 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1035 // first, associate this piece of data with this item
1041 wxTreeItemParam
*param
= GetItemParam(item
);
1043 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1045 param
->SetData(data
);
1048 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1050 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1052 if ( IS_VIRTUAL_ROOT(item
) )
1055 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1056 tvItem
.cChildren
= (int)has
;
1060 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1062 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1064 if ( IS_VIRTUAL_ROOT(item
) )
1067 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1068 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1072 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1074 if ( IS_VIRTUAL_ROOT(item
) )
1077 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1078 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1082 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1084 if ( IS_VIRTUAL_ROOT(item
) )
1088 if ( GetBoundingRect(item
, rect
) )
1094 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1096 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1098 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1099 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1102 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1104 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1106 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1107 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1110 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1112 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1114 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1115 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1118 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1119 const wxColour
& col
)
1121 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1123 wxTreeItemAttr
*attr
;
1124 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1125 if ( it
== m_attrs
.end() )
1127 m_hasAnyAttr
= true;
1129 m_attrs
[item
.m_pItem
] =
1130 attr
= new wxTreeItemAttr
;
1137 attr
->SetTextColour(col
);
1142 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1143 const wxColour
& col
)
1145 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1147 wxTreeItemAttr
*attr
;
1148 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1149 if ( it
== m_attrs
.end() )
1151 m_hasAnyAttr
= true;
1153 m_attrs
[item
.m_pItem
] =
1154 attr
= new wxTreeItemAttr
;
1156 else // already in the hash
1161 attr
->SetBackgroundColour(col
);
1166 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1168 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1170 wxTreeItemAttr
*attr
;
1171 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1172 if ( it
== m_attrs
.end() )
1174 m_hasAnyAttr
= true;
1176 m_attrs
[item
.m_pItem
] =
1177 attr
= new wxTreeItemAttr
;
1179 else // already in the hash
1184 attr
->SetFont(font
);
1186 // Reset the item's text to ensure that the bounding rect will be adjusted
1187 // for the new font.
1188 SetItemText(item
, GetItemText(item
));
1193 // ----------------------------------------------------------------------------
1195 // ----------------------------------------------------------------------------
1197 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1199 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1201 if ( item
== wxTreeItemId(TVI_ROOT
) )
1203 // virtual (hidden) root is never visible
1207 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1210 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1211 // the HTREEITEM with TVM_GETITEMRECT
1212 *(HTREEITEM
*)&rect
= HITEM(item
);
1214 // true means to get rect for just the text, not the whole line
1215 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1217 // if TVM_GETITEMRECT returned false, then the item is definitely not
1218 // visible (because its parent is not expanded)
1222 // however if it returned true, the item might still be outside the
1223 // currently visible part of the tree, test for it (notice that partly
1224 // visible means visible here)
1225 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1228 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1230 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1232 if ( IS_VIRTUAL_ROOT(item
) )
1234 wxTreeItemIdValue cookie
;
1235 return GetFirstChild(item
, cookie
).IsOk();
1238 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1241 return tvItem
.cChildren
!= 0;
1244 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1246 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1248 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1251 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1254 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1256 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1258 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1261 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1264 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1266 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1268 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1271 return (tvItem
.state
& TVIS_BOLD
) != 0;
1274 // ----------------------------------------------------------------------------
1276 // ----------------------------------------------------------------------------
1278 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1280 // Root may be real (visible) or virtual (hidden).
1281 if ( GET_VIRTUAL_ROOT() )
1284 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1287 wxTreeItemId
wxTreeCtrl::GetSelection() const
1289 wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE
), wxTreeItemId(),
1290 wxT("this only works with single selection controls") );
1292 return GetFocusedItem();
1295 wxTreeItemId
wxTreeCtrl::GetFocusedItem() const
1297 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1300 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1302 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1306 if ( IS_VIRTUAL_ROOT(item
) )
1308 // no parent for the virtual root
1313 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1314 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1316 // the top level items should have the virtual root as their parent
1321 return wxTreeItemId(hItem
);
1324 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1325 wxTreeItemIdValue
& cookie
) const
1327 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1329 // remember the last child returned in 'cookie'
1330 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1332 return wxTreeItemId(cookie
);
1335 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1336 wxTreeItemIdValue
& cookie
) const
1338 wxTreeItemId
fromCookie(cookie
);
1340 HTREEITEM hitem
= HITEM(fromCookie
);
1342 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1344 wxTreeItemId
item(hitem
);
1346 cookie
= item
.m_pItem
;
1351 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1353 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1355 // can this be done more efficiently?
1356 wxTreeItemIdValue cookie
;
1358 wxTreeItemId childLast
,
1359 child
= GetFirstChild(item
, cookie
);
1360 while ( child
.IsOk() )
1363 child
= GetNextChild(item
, cookie
);
1369 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1371 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1372 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1375 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1377 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1378 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1381 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1383 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1386 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1388 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1389 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1391 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1392 if ( next
.IsOk() && !IsVisible(next
) )
1394 // Win32 considers that any non-collapsed item is visible while we want
1395 // to return only really visible items
1402 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1404 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1405 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1407 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1408 if ( prev
.IsOk() && !IsVisible(prev
) )
1410 // just as above, Win32 function will happily return the previous item
1411 // in the tree for the first visible item too
1418 // ----------------------------------------------------------------------------
1419 // multiple selections emulation
1420 // ----------------------------------------------------------------------------
1422 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1424 TraverseSelections
selector(this, selections
);
1426 return selector
.GetCount();
1429 // ----------------------------------------------------------------------------
1431 // ----------------------------------------------------------------------------
1433 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1434 const wxTreeItemId
& hInsertAfter
,
1435 const wxString
& text
,
1436 int image
, int selectedImage
,
1437 wxTreeItemData
*data
)
1439 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1441 wxT("can't have more than one root in the tree") );
1443 TV_INSERTSTRUCT tvIns
;
1444 tvIns
.hParent
= HITEM(parent
);
1445 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1447 // this is how we insert the item as the first child: supply a NULL
1449 if ( !tvIns
.hInsertAfter
)
1451 tvIns
.hInsertAfter
= TVI_FIRST
;
1455 if ( !text
.empty() )
1458 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1462 tvIns
.item
.pszText
= NULL
;
1463 tvIns
.item
.cchTextMax
= 0;
1466 // create the param which will store the other item parameters
1467 wxTreeItemParam
*param
= new wxTreeItemParam
;
1469 // we return the images on demand as they depend on whether the item is
1470 // expanded or collapsed too in our case
1471 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1472 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1473 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1475 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1476 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1479 tvIns
.item
.lParam
= (LPARAM
)param
;
1480 tvIns
.item
.mask
= mask
;
1482 // don't use the hack below for the children of hidden root: this results
1483 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1484 const bool firstChild
= !IsHiddenRoot(parent
) &&
1485 !TreeView_GetChild(GetHwnd(), HITEM(parent
));
1487 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1490 wxLogLastError(wxT("TreeView_InsertItem"));
1493 // apparently some Windows versions (2000 and XP are reported to do this)
1494 // sometimes don't refresh the tree after adding the first child and so we
1495 // need this to make the "[+]" appear
1499 TreeView_GetItemRect(GetHwnd(), HITEM(parent
), &rect
, FALSE
);
1500 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1503 // associate the application tree item with Win32 tree item handle
1506 // setup wxTreeItemData
1509 param
->SetData(data
);
1513 return wxTreeItemId(id
);
1516 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1517 int image
, int selectedImage
,
1518 wxTreeItemData
*data
)
1520 if ( HasFlag(wxTR_HIDE_ROOT
) )
1522 wxASSERT_MSG( !m_pVirtualRoot
, wxT("tree can have only a single root") );
1524 // create a virtual root item, the parent for all the others
1525 wxTreeItemParam
*param
= new wxTreeItemParam
;
1526 param
->SetData(data
);
1528 m_pVirtualRoot
= new wxVirtualNode(param
);
1533 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1534 text
, image
, selectedImage
, data
);
1537 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1539 const wxString
& text
,
1540 int image
, int selectedImage
,
1541 wxTreeItemData
*data
)
1543 wxTreeItemId idPrev
;
1544 if ( index
== (size_t)-1 )
1546 // special value: append to the end
1549 else // find the item from index
1551 wxTreeItemIdValue cookie
;
1552 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1553 while ( index
!= 0 && idCur
.IsOk() )
1558 idCur
= GetNextChild(parent
, cookie
);
1561 // assert, not check: if the index is invalid, we will append the item
1563 wxASSERT_MSG( index
== 0, wxT("bad index in wxTreeCtrl::InsertItem") );
1566 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1569 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1571 // unlock tree selections on vista, without this the
1572 // tree ctrl will eventually crash after item deletion
1573 TreeItemUnlocker unlock_all
;
1575 if ( HasFlag(wxTR_MULTIPLE
) )
1577 bool selected
= IsSelected(item
);
1582 next
= TreeView_GetNextVisible(GetHwnd(), HITEM(item
));
1586 next
= TreeView_GetPrevVisible(GetHwnd(), HITEM(item
));
1591 TempSetter
set(m_changingSelection
);
1592 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1594 wxLogLastError(wxT("TreeView_DeleteItem"));
1604 if ( item
== m_htSelStart
)
1605 m_htSelStart
.Unset();
1607 if ( item
== m_htClickedItem
)
1608 m_htClickedItem
.Unset();
1612 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
1614 if ( IsTreeEventAllowed(changingEvent
) )
1616 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
1617 (void)HandleTreeEvent(changedEvent
);
1621 DoUnselectItem(next
);
1628 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1630 wxLogLastError(wxT("TreeView_DeleteItem"));
1635 // delete all children (but don't delete the item itself)
1636 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1638 // unlock tree selections on vista for the duration of this call
1639 TreeItemUnlocker unlock_all
;
1641 wxTreeItemIdValue cookie
;
1643 wxArrayTreeItemIds children
;
1644 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1645 while ( child
.IsOk() )
1647 children
.Add(child
);
1649 child
= GetNextChild(item
, cookie
);
1652 size_t nCount
= children
.Count();
1653 for ( size_t n
= 0; n
< nCount
; n
++ )
1655 Delete(children
[n
]);
1659 void wxTreeCtrl::DeleteAllItems()
1661 // unlock tree selections on vista for the duration of this call
1662 TreeItemUnlocker unlock_all
;
1664 // invalidate all the items we store as they're going to become invalid
1666 m_htClickedItem
= wxTreeItemId();
1668 // delete the "virtual" root item.
1669 if ( GET_VIRTUAL_ROOT() )
1671 delete GET_VIRTUAL_ROOT();
1672 m_pVirtualRoot
= NULL
;
1675 // and all the real items
1677 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1679 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1683 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1685 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1686 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1687 flag
== TVE_EXPAND
||
1689 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1691 // A hidden root can be neither expanded nor collapsed.
1692 wxCHECK_RET( !IsHiddenRoot(item
),
1693 wxT("Can't expand/collapse hidden root node!") );
1695 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1696 // emulate them. This behaviour has changed slightly with comctl32.dll
1697 // v 4.70 - now it does send them but only the first time. To maintain
1698 // compatible behaviour and also in order to not have surprises with the
1699 // future versions, don't rely on this and still do everything ourselves.
1700 // To avoid that the messages be sent twice when the item is expanded for
1701 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1703 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1707 if ( IsExpanded(item
) )
1709 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
,
1710 this, wxTreeItemId(item
));
1712 if ( !IsTreeEventAllowed(event
) )
1716 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) )
1718 if ( IsExpanded(item
) )
1721 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, this, item
);
1722 (void)HandleTreeEvent(event
);
1724 //else: change didn't took place, so do nothing at all
1727 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1729 DoExpand(item
, TVE_EXPAND
);
1732 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1734 DoExpand(item
, TVE_COLLAPSE
);
1737 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1739 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1742 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1744 DoExpand(item
, TVE_TOGGLE
);
1747 void wxTreeCtrl::Unselect()
1749 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE
),
1750 wxT("doesn't make sense, may be you want UnselectAll()?") );
1752 // the current focus
1753 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1760 if ( HasFlag(wxTR_MULTIPLE
) )
1762 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
1763 this, wxTreeItemId());
1764 changingEvent
.m_itemOld
= htFocus
;
1766 if ( IsTreeEventAllowed(changingEvent
) )
1770 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1771 this, wxTreeItemId());
1772 changedEvent
.m_itemOld
= htFocus
;
1773 (void)HandleTreeEvent(changedEvent
);
1782 void wxTreeCtrl::DoUnselectAll()
1784 wxArrayTreeItemIds selections
;
1785 size_t count
= GetSelections(selections
);
1787 for ( size_t n
= 0; n
< count
; n
++ )
1789 DoUnselectItem(selections
[n
]);
1792 m_htSelStart
.Unset();
1795 void wxTreeCtrl::UnselectAll()
1797 if ( HasFlag(wxTR_MULTIPLE
) )
1799 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1800 if ( !htFocus
) return;
1802 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1803 changingEvent
.m_itemOld
= htFocus
;
1805 if ( IsTreeEventAllowed(changingEvent
) )
1809 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1810 changedEvent
.m_itemOld
= htFocus
;
1811 (void)HandleTreeEvent(changedEvent
);
1820 void wxTreeCtrl::DoSelectChildren(const wxTreeItemId
& parent
)
1824 wxTreeItemIdValue cookie
;
1825 wxTreeItemId child
= GetFirstChild(parent
, cookie
);
1826 while ( child
.IsOk() )
1828 DoSelectItem(child
, true);
1829 child
= GetNextChild(child
, cookie
);
1833 void wxTreeCtrl::SelectChildren(const wxTreeItemId
& parent
)
1835 wxCHECK_RET( HasFlag(wxTR_MULTIPLE
),
1836 "this only works with multiple selection controls" );
1838 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1840 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1841 changingEvent
.m_itemOld
= htFocus
;
1843 if ( IsTreeEventAllowed(changingEvent
) )
1845 DoSelectChildren(parent
);
1847 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1848 changedEvent
.m_itemOld
= htFocus
;
1849 (void)HandleTreeEvent(changedEvent
);
1853 void wxTreeCtrl::DoSelectItem(const wxTreeItemId
& item
, bool select
)
1855 TempSetter
set(m_changingSelection
);
1857 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1860 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1862 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't select hidden root item") );
1864 if ( select
== IsSelected(item
) )
1866 // nothing to do, the item is already in the requested state
1870 if ( HasFlag(wxTR_MULTIPLE
) )
1872 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1874 if ( IsTreeEventAllowed(changingEvent
) )
1876 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1877 DoSelectItem(item
, select
);
1881 SetFocusedItem(item
);
1884 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1886 (void)HandleTreeEvent(changedEvent
);
1889 else // single selection
1891 wxTreeItemId itemOld
, itemNew
;
1894 itemOld
= GetSelection();
1897 else // deselecting the currently selected item
1900 // leave itemNew invalid
1903 // Recent versions of comctl32.dll send TVN_SELCHANG{ED,ING} events
1904 // when we call TreeView_SelectItem() but apparently some old ones did
1905 // not so send the events ourselves and ignore those generated by
1906 // TreeView_SelectItem() if m_changingSelection is set.
1908 changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, itemNew
);
1909 changingEvent
.SetOldItem(itemOld
);
1911 if ( IsTreeEventAllowed(changingEvent
) )
1913 TempSetter
set(m_changingSelection
);
1915 if ( !TreeView_SelectItem(GetHwnd(), HITEM(itemNew
)) )
1917 wxLogLastError(wxT("TreeView_SelectItem"));
1921 ::SetFocus(GetHwnd(), HITEM(item
));
1923 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1925 changedEvent
.SetOldItem(itemOld
);
1926 (void)HandleTreeEvent(changedEvent
);
1929 //else: program vetoed the change
1933 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1935 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't show hidden root item") );
1938 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1941 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1943 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1945 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1949 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1954 void wxTreeCtrl::DeleteTextCtrl()
1958 // the HWND corresponding to this control is deleted by the tree
1959 // control itself and we don't know when exactly this happens, so check
1960 // if the window still exists before calling UnsubclassWin()
1961 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1963 m_textCtrl
->SetHWND(0);
1966 m_textCtrl
->UnsubclassWin();
1967 m_textCtrl
->SetHWND(0);
1968 wxDELETE(m_textCtrl
);
1974 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1975 wxClassInfo
*textControlClass
)
1977 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1982 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1983 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1985 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1989 wxDELETE(m_textCtrl
);
1993 // textctrl is subclassed in MSWOnNotify
1997 // End label editing, optionally cancelling the edit
1998 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
2000 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
2005 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2007 TV_HITTESTINFO hitTestInfo
;
2008 hitTestInfo
.pt
.x
= (int)point
.x
;
2009 hitTestInfo
.pt
.y
= (int)point
.y
;
2011 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2016 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2017 flags |= wxTREE_HITTEST_##flag
2019 TRANSLATE_FLAG(ABOVE
);
2020 TRANSLATE_FLAG(BELOW
);
2021 TRANSLATE_FLAG(NOWHERE
);
2022 TRANSLATE_FLAG(ONITEMBUTTON
);
2023 TRANSLATE_FLAG(ONITEMICON
);
2024 TRANSLATE_FLAG(ONITEMINDENT
);
2025 TRANSLATE_FLAG(ONITEMLABEL
);
2026 TRANSLATE_FLAG(ONITEMRIGHT
);
2027 TRANSLATE_FLAG(ONITEMSTATEICON
);
2028 TRANSLATE_FLAG(TOLEFT
);
2029 TRANSLATE_FLAG(TORIGHT
);
2031 #undef TRANSLATE_FLAG
2033 return wxTreeItemId(hitTestInfo
.hItem
);
2036 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2038 bool textOnly
) const
2042 // Virtual root items have no bounding rectangle
2043 if ( IS_VIRTUAL_ROOT(item
) )
2048 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2051 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2057 // couldn't retrieve rect: for example, item isn't visible
2062 void wxTreeCtrl::ClearFocusedItem()
2064 TempSetter
set(m_changingSelection
);
2066 if ( !TreeView_SelectItem(GetHwnd(), 0) )
2068 wxLogLastError(wxT("TreeView_SelectItem"));
2072 void wxTreeCtrl::SetFocusedItem(const wxTreeItemId
& item
)
2074 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2076 TempSetter
set(m_changingSelection
);
2078 ::SetFocus(GetHwnd(), HITEM(item
));
2081 void wxTreeCtrl::DoUnselectItem(const wxTreeItemId
& item
)
2083 TempSetter
set(m_changingSelection
);
2085 ::UnselectItem(GetHwnd(), HITEM(item
));
2088 void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId
& item
)
2090 TempSetter
set(m_changingSelection
);
2092 ::ToggleItemSelection(GetHwnd(), HITEM(item
));
2095 // ----------------------------------------------------------------------------
2097 // ----------------------------------------------------------------------------
2099 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2100 // functions such as IsDataIndirect()
2101 class wxTreeSortHelper
2104 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2107 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
2109 return ((wxTreeItemParam
*)lParam
)->GetItem();
2113 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2117 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2118 wxT("sorting tree without data doesn't make sense") );
2120 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2122 return tree
->OnCompareItems(GetIdFromData(pItem1
),
2123 GetIdFromData(pItem2
));
2126 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2128 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2130 // rely on the fact that TreeView_SortChildren does the same thing as our
2131 // default behaviour, i.e. sorts items alphabetically and so call it
2132 // directly if we're not in derived class (much more efficient!)
2133 // RN: Note that if you find you're code doesn't sort as expected this
2134 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
2135 // combo for your derived wxTreeCtrl if will sort without
2137 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2139 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2144 tvSort
.hParent
= HITEM(item
);
2145 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2146 tvSort
.lParam
= (LPARAM
)this;
2147 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2151 // ----------------------------------------------------------------------------
2153 // ----------------------------------------------------------------------------
2155 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
2157 if ( msg
->message
== WM_KEYDOWN
)
2159 // Only eat VK_RETURN if not being used by the application in
2160 // conjunction with modifiers
2161 if ( (msg
->wParam
== VK_RETURN
) && !wxIsAnyModifierDown() )
2163 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2168 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
2171 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
2173 const int id
= (signed short)id_
;
2175 if ( cmd
== EN_UPDATE
)
2177 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2178 event
.SetEventObject( this );
2179 ProcessCommand(event
);
2181 else if ( cmd
== EN_KILLFOCUS
)
2183 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2184 event
.SetEventObject( this );
2185 ProcessCommand(event
);
2193 // command processed
2197 bool wxTreeCtrl::MSWIsOnItem(unsigned flags
) const
2199 unsigned mask
= TVHT_ONITEM
;
2200 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2201 mask
|= TVHT_ONITEMINDENT
| TVHT_ONITEMRIGHT
;
2203 return (flags
& mask
) != 0;
2206 bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey
)
2208 const bool bCtrl
= wxIsCtrlDown();
2209 const bool bShift
= wxIsShiftDown();
2210 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2219 if ( vkey
!= VK_RETURN
&& bCtrl
)
2221 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2223 changingEvent
.m_itemOld
= htSel
;
2225 if ( IsTreeEventAllowed(changingEvent
) )
2227 DoToggleItemSelection(wxTreeItemId(htSel
));
2229 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2231 changedEvent
.m_itemOld
= htSel
;
2232 (void)HandleTreeEvent(changedEvent
);
2237 wxArrayTreeItemIds selections
;
2238 size_t count
= GetSelections(selections
);
2240 if ( count
!= 1 || HITEM(selections
[0]) != htSel
)
2242 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2244 changingEvent
.m_itemOld
= htSel
;
2246 if ( IsTreeEventAllowed(changingEvent
) )
2249 DoSelectItem(wxTreeItemId(htSel
));
2251 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2253 changedEvent
.m_itemOld
= htSel
;
2254 (void)HandleTreeEvent(changedEvent
);
2262 if ( !bCtrl
&& !bShift
)
2264 wxArrayTreeItemIds selections
;
2269 next
= vkey
== VK_UP
2270 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2271 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2275 next
= GetRootItem();
2277 if ( IsHiddenRoot(next
) )
2278 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2286 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2288 changingEvent
.m_itemOld
= htSel
;
2290 if ( IsTreeEventAllowed(changingEvent
) )
2294 SetFocusedItem(next
);
2296 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2298 changedEvent
.m_itemOld
= htSel
;
2299 (void)HandleTreeEvent(changedEvent
);
2304 wxTreeItemId next
= vkey
== VK_UP
2305 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2306 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2313 if ( !m_htSelStart
)
2315 m_htSelStart
= htSel
;
2318 if ( bShift
&& SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2319 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2321 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2322 changingEvent
.m_itemOld
= htSel
;
2324 if ( IsTreeEventAllowed(changingEvent
) )
2326 SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2327 SR_UNSELECT_OTHERS
);
2329 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2330 changedEvent
.m_itemOld
= htSel
;
2331 (void)HandleTreeEvent(changedEvent
);
2335 SetFocusedItem(next
);
2340 if ( HasChildren(htSel
) && IsExpanded(htSel
) )
2346 wxTreeItemId next
= GetItemParent(htSel
);
2348 if ( next
.IsOk() && !IsHiddenRoot(next
) )
2350 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2352 changingEvent
.m_itemOld
= htSel
;
2354 if ( IsTreeEventAllowed(changingEvent
) )
2358 SetFocusedItem(next
);
2360 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2362 changedEvent
.m_itemOld
= htSel
;
2363 (void)HandleTreeEvent(changedEvent
);
2370 if ( !IsVisible(htSel
) )
2372 EnsureVisible(htSel
);
2375 if ( !HasChildren(htSel
) )
2378 if ( !IsExpanded(htSel
) )
2384 wxTreeItemId next
= TreeView_GetChild(GetHwnd(), htSel
);
2386 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2387 changingEvent
.m_itemOld
= htSel
;
2389 if ( IsTreeEventAllowed(changingEvent
) )
2393 SetFocusedItem(next
);
2395 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2396 changedEvent
.m_itemOld
= htSel
;
2397 (void)HandleTreeEvent(changedEvent
);
2405 wxTreeItemId next
= GetRootItem();
2407 if ( IsHiddenRoot(next
) )
2409 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2415 if ( vkey
== VK_END
)
2419 wxTreeItemId nextTemp
= TreeView_GetNextVisible(
2420 GetHwnd(), HITEM(next
));
2422 if ( !nextTemp
.IsOk() )
2429 if ( htSel
== HITEM(next
) )
2434 if ( !m_htSelStart
)
2436 m_htSelStart
= htSel
;
2439 if ( SelectRange(GetHwnd(),
2440 HITEM(m_htSelStart
), HITEM(next
),
2441 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2443 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2445 changingEvent
.m_itemOld
= htSel
;
2447 if ( IsTreeEventAllowed(changingEvent
) )
2449 SelectRange(GetHwnd(),
2450 HITEM(m_htSelStart
), HITEM(next
),
2451 SR_UNSELECT_OTHERS
);
2452 SetFocusedItem(next
);
2454 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2456 changedEvent
.m_itemOld
= htSel
;
2457 (void)HandleTreeEvent(changedEvent
);
2463 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2465 changingEvent
.m_itemOld
= htSel
;
2467 if ( IsTreeEventAllowed(changingEvent
) )
2471 SetFocusedItem(next
);
2473 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2475 changedEvent
.m_itemOld
= htSel
;
2476 (void)HandleTreeEvent(changedEvent
);
2486 wxTreeItemId firstVisible
= GetFirstVisibleItem();
2487 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2488 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2489 TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible
)) :
2490 TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible
));
2492 if ( !nextAdjacent
)
2497 wxTreeItemId nextStart
= firstVisible
;
2499 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2501 wxTreeItemId nextTemp
= (vkey
== VK_PRIOR
) ?
2502 TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart
)) :
2503 TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart
));
2505 if ( nextTemp
.IsOk() )
2507 nextStart
= nextTemp
;
2515 EnsureVisible(nextStart
);
2517 if ( vkey
== VK_NEXT
)
2519 wxTreeItemId nextEnd
= nextStart
;
2521 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2523 wxTreeItemId nextTemp
=
2524 TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd
));
2526 if ( nextTemp
.IsOk() )
2536 EnsureVisible(nextEnd
);
2541 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2542 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2543 TreeView_GetPrevVisible(GetHwnd(), htSel
) :
2544 TreeView_GetNextVisible(GetHwnd(), htSel
);
2546 if ( !nextAdjacent
)
2551 wxTreeItemId
next(htSel
);
2553 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2555 wxTreeItemId nextTemp
= vkey
== VK_PRIOR
?
2556 TreeView_GetPrevVisible(GetHwnd(), HITEM(next
)) :
2557 TreeView_GetNextVisible(GetHwnd(), HITEM(next
));
2559 if ( !nextTemp
.IsOk() )
2565 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2567 changingEvent
.m_itemOld
= htSel
;
2569 if ( IsTreeEventAllowed(changingEvent
) )
2572 m_htSelStart
.Unset();
2574 SetFocusedItem(next
);
2576 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2578 changedEvent
.m_itemOld
= htSel
;
2579 (void)HandleTreeEvent(changedEvent
);
2591 bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam
, WXLPARAM lParam
)
2593 wxTreeEvent
keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2594 keyEvent
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
, wParam
, lParam
);
2596 bool processed
= HandleTreeEvent(keyEvent
);
2598 // generate a separate event for Space/Return
2599 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
2600 ((wParam
== VK_SPACE
) || (wParam
== VK_RETURN
)) )
2602 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2605 wxTreeEvent
activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2607 (void)HandleTreeEvent(activatedEvent
);
2614 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2615 // only do it during dragging, minimize wxWin overhead (this is important for
2616 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2617 // instead of passing by wxWin events
2619 wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2621 bool processed
= false;
2623 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2625 if ( nMsg
== WM_CONTEXTMENU
)
2627 int x
= GET_X_LPARAM(lParam
),
2628 y
= GET_Y_LPARAM(lParam
);
2630 // the item for which the menu should be shown
2633 // the position where the menu should be shown in client coordinates
2634 // (so that it can be passed directly to PopupMenu())
2637 if ( x
== -1 || y
== -1 )
2639 // this means that the event was generated from keyboard (e.g. with
2640 // Shift-F10 or special Windows menu key)
2642 // use the Explorer standard of putting the menu at the left edge
2643 // of the text, in the vertical middle of the text
2644 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2647 // Use the bounding rectangle of only the text part
2649 GetBoundingRect(item
, rect
, true);
2650 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
2653 else // event from mouse, use mouse position
2655 pt
= ScreenToClient(wxPoint(x
, y
));
2657 TV_HITTESTINFO tvhti
;
2661 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2662 item
= wxTreeItemId(tvhti
.hItem
);
2668 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2670 event
.m_pointDrag
= pt
;
2672 if ( HandleTreeEvent(event
) )
2674 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2677 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2679 // we only process mouse messages here and these parameters have the
2680 // same meaning for all of them
2681 int x
= GET_X_LPARAM(lParam
),
2682 y
= GET_Y_LPARAM(lParam
);
2684 TV_HITTESTINFO tvht
;
2688 HTREEITEM htOldItem
= TreeView_GetSelection(GetHwnd());
2689 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2693 case WM_LBUTTONDOWN
:
2697 m_htClickedItem
.Unset();
2699 if ( !MSWIsOnItem(tvht
.flags
) )
2701 if ( tvht
.flags
& TVHT_ONITEMBUTTON
)
2703 // either it's going to be handled by user code or
2704 // we're going to use it ourselves to toggle the
2705 // branch, in either case don't pass it to the base
2706 // class which would generate another mouse click event
2707 // for it even though it's already handled here
2711 if ( !HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2713 if ( !IsExpanded(htItem
) )
2724 m_focusLost
= false;
2730 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2731 m_ptClick
= wxPoint(x
, y
);
2733 if ( wParam
& MK_CONTROL
)
2735 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2737 m_htClickedItem
.Unset();
2741 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2743 changingEvent
.m_itemOld
= htOldItem
;
2745 if ( IsTreeEventAllowed(changingEvent
) )
2747 // toggle selected state
2748 DoToggleItemSelection(wxTreeItemId(htItem
));
2750 SetFocusedItem(wxTreeItemId(htItem
));
2752 // reset on any click without Shift
2753 m_htSelStart
.Unset();
2755 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2757 changedEvent
.m_itemOld
= htOldItem
;
2758 (void)HandleTreeEvent(changedEvent
);
2761 else if ( wParam
& MK_SHIFT
)
2763 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2765 m_htClickedItem
.Unset();
2770 bool willChange
= true;
2772 if ( !(wParam
& MK_CONTROL
) )
2774 srFlags
|= SR_UNSELECT_OTHERS
;
2777 if ( !m_htSelStart
)
2779 // take the focused item
2780 m_htSelStart
= htOldItem
;
2784 willChange
= SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2785 htItem
, srFlags
| SR_SIMULATE
);
2790 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2792 changingEvent
.m_itemOld
= htOldItem
;
2794 if ( IsTreeEventAllowed(changingEvent
) )
2796 // this selects all items between the starting one
2800 SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2805 DoSelectItem(wxTreeItemId(htItem
));
2808 SetFocusedItem(wxTreeItemId(htItem
));
2810 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2812 changedEvent
.m_itemOld
= htOldItem
;
2813 (void)HandleTreeEvent(changedEvent
);
2817 else // normal click
2819 // avoid doing anything if we click on the only
2820 // currently selected item
2822 wxArrayTreeItemIds selections
;
2823 size_t count
= GetSelections(selections
);
2827 HITEM(selections
[0]) != htItem
)
2829 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2831 m_htClickedItem
.Unset();
2835 // clear the previously selected items, if the user
2836 // clicked outside of the present selection, otherwise,
2837 // perform the deselection on mouse-up, this allows
2838 // multiple drag and drop to work.
2839 if ( !IsItemSelected(GetHwnd(), htItem
))
2841 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2843 changingEvent
.m_itemOld
= htOldItem
;
2845 if ( IsTreeEventAllowed(changingEvent
) )
2848 DoSelectItem(wxTreeItemId(htItem
));
2849 SetFocusedItem(wxTreeItemId(htItem
));
2851 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2853 changedEvent
.m_itemOld
= htOldItem
;
2854 (void)HandleTreeEvent(changedEvent
);
2859 SetFocusedItem(wxTreeItemId(htItem
));
2860 m_mouseUpDeselect
= true;
2863 else // click on a single selected item
2865 // don't interfere with the default processing in
2866 // WM_MOUSEMOVE handler below as the default window
2867 // proc will start the drag itself if we let have
2869 m_htClickedItem
.Unset();
2871 // prevent in-place editing from starting if focus lost
2872 // since previous click
2876 DoSelectItem(wxTreeItemId(htItem
));
2877 SetFocusedItem(wxTreeItemId(htItem
));
2885 // reset on any click without Shift
2886 m_htSelStart
.Unset();
2889 m_focusLost
= false;
2891 // we consumed the event so we need to trigger state image
2896 wxTreeItemId item
= HitTest(wxPoint(x
, y
), htFlags
);
2898 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2900 m_triggerStateImageClick
= true;
2905 case WM_RBUTTONDOWN
:
2912 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) || !htItem
)
2917 // default handler removes the highlight from the currently
2918 // focused item when right mouse button is pressed on another
2919 // one but keeps the remaining items highlighted, which is
2920 // confusing, so override this default behaviour
2921 if ( !IsItemSelected(GetHwnd(), htItem
) )
2923 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2925 changingEvent
.m_itemOld
= htOldItem
;
2927 if ( IsTreeEventAllowed(changingEvent
) )
2930 DoSelectItem(wxTreeItemId(htItem
));
2931 SetFocusedItem(wxTreeItemId(htItem
));
2933 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2935 changedEvent
.m_itemOld
= htOldItem
;
2936 (void)HandleTreeEvent(changedEvent
);
2944 if ( m_htClickedItem
)
2946 int cx
= abs(m_ptClick
.x
- x
);
2947 int cy
= abs(m_ptClick
.y
- y
);
2949 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2950 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2955 tv
.hdr
.hwndFrom
= GetHwnd();
2956 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2957 tv
.hdr
.code
= TVN_BEGINDRAG
;
2959 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2963 wxZeroMemory(tviAux
);
2965 tviAux
.hItem
= HITEM(m_htClickedItem
);
2966 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2967 tviAux
.stateMask
= 0xffffffff;
2968 TreeView_GetItem(GetHwnd(), &tviAux
);
2970 tv
.itemNew
.state
= tviAux
.state
;
2971 tv
.itemNew
.lParam
= tviAux
.lParam
;
2976 // do it before SendMessage() call below to avoid
2977 // reentrancies here if there is another WM_MOUSEMOVE
2978 // in the queue already
2979 m_htClickedItem
.Unset();
2981 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
2982 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2984 // don't pass it to the default window proc, it would
2985 // start dragging again
2989 #endif // __WXWINCE__
2994 m_dragImage
->Move(wxPoint(x
, y
));
2997 // highlight the item as target (hiding drag image is
2998 // necessary - otherwise the display will be corrupted)
2999 m_dragImage
->Hide();
3000 TreeView_SelectDropTarget(GetHwnd(), htItem
);
3001 m_dragImage
->Show();
3004 #endif // wxUSE_DRAGIMAGE
3010 // deselect other items if needed
3013 if ( m_mouseUpDeselect
)
3015 m_mouseUpDeselect
= false;
3017 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
3019 changingEvent
.m_itemOld
= htOldItem
;
3021 if ( IsTreeEventAllowed(changingEvent
) )
3024 DoSelectItem(wxTreeItemId(htItem
));
3025 SetFocusedItem(wxTreeItemId(htItem
));
3027 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
3029 changedEvent
.m_itemOld
= htOldItem
;
3030 (void)HandleTreeEvent(changedEvent
);
3035 m_htClickedItem
.Unset();
3037 if ( m_triggerStateImageClick
)
3039 if ( tvht
.flags
& TVHT_ONITEMSTATEICON
)
3041 wxTreeEvent
event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
,
3043 (void)HandleTreeEvent(event
);
3045 m_triggerStateImageClick
= false;
3050 if ( !m_dragStarted
&& MSWIsOnItem(tvht
.flags
) )
3062 m_dragImage
->EndDrag();
3063 wxDELETE(m_dragImage
);
3065 // generate the drag end event
3066 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
,
3068 event
.m_pointDrag
= wxPoint(x
, y
);
3069 (void)HandleTreeEvent(event
);
3071 // if we don't do it, the tree seems to think that 2 items
3072 // are selected simultaneously which is quite weird
3073 TreeView_SelectDropTarget(GetHwnd(), 0);
3075 #endif // wxUSE_DRAGIMAGE
3077 if ( isMultiple
&& nMsg
== WM_RBUTTONUP
)
3081 nmhdr
.hwndFrom
= GetHwnd();
3082 nmhdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
3083 nmhdr
.code
= NM_RCLICK
;
3084 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY
,
3085 nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
3089 m_dragStarted
= false;
3094 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) )
3098 // the tree control greys out the selected item when it loses focus
3099 // and paints it as selected again when it regains it, but it won't
3100 // do it for the other items itself - help it
3101 wxArrayTreeItemIds selections
;
3102 size_t count
= GetSelections(selections
);
3105 for ( size_t n
= 0; n
< count
; n
++ )
3107 // TreeView_GetItemRect() will return false if item is not
3108 // visible, which may happen perfectly well
3109 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
3112 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
3117 if ( nMsg
== WM_KILLFOCUS
)
3122 else if ( (nMsg
== WM_KEYDOWN
|| nMsg
== WM_SYSKEYDOWN
) && isMultiple
)
3124 // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
3125 // notification but for the keys which can be used to change selection
3126 // we need to do it from here so as to not apply the default behaviour
3127 // if the events are handled by the user code
3140 if ( !HandleKeyDown(wParam
, lParam
) &&
3141 !MSWHandleTreeKeyDownEvent(wParam
, lParam
) )
3143 // use the key to update the selection if it was left
3145 MSWHandleSelectionKey(wParam
);
3148 // pretend that we did process it in any case as we already
3149 // generated an event for it
3152 //default: for all the other keys leave processed as false so that
3153 // the tree control generates a TVN_KEYDOWN for us
3157 else if ( nMsg
== WM_COMMAND
)
3159 // if we receive a EN_KILLFOCUS command from the in-place edit control
3160 // used for label editing, make sure to end editing
3163 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
3165 if ( cmd
== EN_KILLFOCUS
)
3167 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
3177 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
3183 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
3185 if ( nMsg
== WM_CHAR
)
3187 // don't let the control process Space and Return keys because it
3188 // doesn't do anything useful with them anyhow but always beeps
3189 // annoyingly when it receives them and there is no way to turn it off
3190 // simply if you just process TREEITEM_ACTIVATED event to which Space
3191 // and Enter presses are mapped in your code
3192 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
3196 else if ( nMsg
== WM_KEYDOWN
)
3198 if ( wParam
== VK_ESCAPE
)
3202 m_dragImage
->EndDrag();
3203 wxDELETE(m_dragImage
);
3205 // if we don't do it, the tree seems to think that 2 items
3206 // are selected simultaneously which is quite weird
3207 TreeView_SelectDropTarget(GetHwnd(), 0);
3211 #endif // wxUSE_DRAGIMAGE
3213 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
3216 // process WM_NOTIFY Windows message
3217 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
3219 wxTreeEvent
event(wxEVT_NULL
, this);
3220 wxEventType eventType
= wxEVT_NULL
;
3221 NMHDR
*hdr
= (NMHDR
*)lParam
;
3223 switch ( hdr
->code
)
3226 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3229 case TVN_BEGINRDRAG
:
3231 if ( eventType
== wxEVT_NULL
)
3232 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
3233 //else: left drag, already set above
3235 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3237 event
.m_item
= tv
->itemNew
.hItem
;
3238 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
3240 // don't allow dragging by default: the user code must
3241 // explicitly say that it wants to allow it to avoid breaking
3247 case TVN_BEGINLABELEDIT
:
3249 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
3250 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3252 // although the user event handler may still veto it, it is
3253 // important to set it now so that calls to SetItemText() from
3254 // the event handler would change the text controls contents
3256 event
.m_item
= info
->item
.hItem
;
3257 event
.m_label
= info
->item
.pszText
;
3258 event
.m_editCancelled
= false;
3262 case TVN_DELETEITEM
:
3264 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
3265 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3267 event
.m_item
= tv
->itemOld
.hItem
;
3271 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
3272 if ( it
!= m_attrs
.end() )
3281 case TVN_ENDLABELEDIT
:
3283 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
3284 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3286 event
.m_item
= info
->item
.hItem
;
3287 event
.m_label
= info
->item
.pszText
;
3288 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
3293 // These *must* not be removed or TVN_GETINFOTIP will
3294 // not be processed each time the mouse is moved
3295 // and the tooltip will only ever update once.
3304 #ifdef TVN_GETINFOTIP
3305 case TVN_GETINFOTIP
:
3307 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
3308 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
3310 // Which item are we trying to get a tooltip for?
3311 event
.m_item
= info
->hItem
;
3315 #endif // TVN_GETINFOTIP
3316 #endif // !__WXWINCE__
3318 case TVN_GETDISPINFO
:
3319 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
3322 case TVN_SETDISPINFO
:
3324 if ( eventType
== wxEVT_NULL
)
3325 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
3326 //else: get, already set above
3328 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3330 event
.m_item
= info
->item
.hItem
;
3334 case TVN_ITEMEXPANDING
:
3335 case TVN_ITEMEXPANDED
:
3337 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3340 switch ( tv
->action
)
3343 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
3351 what
= IDX_COLLAPSE
;
3355 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
3358 eventType
= gs_expandEvents
[what
][how
];
3360 event
.m_item
= tv
->itemNew
.hItem
;
3366 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
3368 // fabricate the lParam and wParam parameters sufficiently
3369 // similar to the ones from a "real" WM_KEYDOWN so that
3370 // CreateKeyEvent() works correctly
3371 return MSWHandleTreeKeyDownEvent(
3372 info
->wVKey
, (wxIsAltDown() ? KF_ALTDOWN
: 0) << 16);
3376 // Vista's tree control has introduced some problems with our
3377 // multi-selection tree. When TreeView_SelectItem() is called,
3378 // the wrong items are deselected.
3380 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3381 // that can be used to regulate this incorrect behavior. The
3382 // following messages will allow only the unlocked item's selection
3385 case TVN_ITEMCHANGINGA
:
3386 case TVN_ITEMCHANGINGW
:
3388 // we only need to handles these in multi-select trees
3389 if ( HasFlag(wxTR_MULTIPLE
) )
3391 // get info about the item about to be changed
3392 NMTVITEMCHANGE
* info
= (NMTVITEMCHANGE
*)lParam
;
3393 if (TreeItemUnlocker::IsLocked(info
->hItem
))
3395 // item's state is locked, don't allow the change
3396 // returning 1 will disallow the change
3402 // allow the state change
3406 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
3407 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3408 // we have to handle both messages:
3409 case TVN_SELCHANGEDA
:
3410 case TVN_SELCHANGEDW
:
3411 if ( !m_changingSelection
)
3413 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
3417 case TVN_SELCHANGINGA
:
3418 case TVN_SELCHANGINGW
:
3419 if ( !m_changingSelection
)
3421 if ( eventType
== wxEVT_NULL
)
3422 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
3423 //else: already set above
3425 if (hdr
->code
== TVN_SELCHANGINGW
||
3426 hdr
->code
== TVN_SELCHANGEDW
)
3428 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
3429 event
.m_item
= tv
->itemNew
.hItem
;
3430 event
.m_itemOld
= tv
->itemOld
.hItem
;
3434 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
3435 event
.m_item
= tv
->itemNew
.hItem
;
3436 event
.m_itemOld
= tv
->itemOld
.hItem
;
3440 // we receive this message from WM_LBUTTONDOWN handler inside
3441 // comctl32.dll and so before the click is passed to
3442 // DefWindowProc() which sets the focus to the window which was
3443 // clicked and this can lead to unexpected event sequences: for
3444 // example, we may get a "selection change" event from the tree
3445 // before getting a "kill focus" event for the text control which
3446 // had the focus previously, thus breaking user code doing input
3449 // to avoid such surprises, we force the generation of focus events
3450 // now, before we generate the selection change ones
3451 if ( !m_changingSelection
)
3455 // instead of explicitly checking for _WIN32_IE, check if the
3456 // required symbols are available in the headers
3457 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
3460 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
3461 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
3462 switch ( nmcd
.dwDrawStage
)
3465 // if we've got any items with non standard attributes,
3466 // notify us before painting each item
3467 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
3470 // windows in TreeCtrl use one-based index for item state images,
3471 // 0 indexed image is not being used, we're using zero-based index,
3472 // so we have to add temp image (of zero index) to state image list
3473 // before we draw any item, then after items are drawn we have to
3474 // delete it (in POSTPAINT notify)
3475 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3477 typedef BOOL (wxSTDCALL
*ImageList_Copy_t
)
3478 (HIMAGELIST
, int, HIMAGELIST
, int, UINT
);
3479 static ImageList_Copy_t s_pfnImageList_Copy
= NULL
;
3480 static bool loaded
= false;
3484 wxLoadedDLL
dllComCtl32(wxT("comctl32.dll"));
3485 if ( dllComCtl32
.IsLoaded() )
3486 wxDL_INIT_FUNC(s_pfn
, ImageList_Copy
, dllComCtl32
);
3489 if ( !s_pfnImageList_Copy
)
3491 // this code is broken with ImageList_Copy()
3492 // but I don't care enough about Win95 support
3493 // to write it now -- if anybody does, please
3495 wxFAIL_MSG("TODO: implement this for Win95");
3500 hImageList
= GetHimagelistOf(m_imageListState
);
3502 // add temporary image
3504 m_imageListState
->GetSize(0, width
, height
);
3506 HBITMAP hbmpTemp
= ::CreateBitmap(width
, height
, 1, 1, NULL
);
3507 int index
= ::ImageList_Add(hImageList
, hbmpTemp
, hbmpTemp
);
3508 ::DeleteObject(hbmpTemp
);
3512 // move images to right
3513 for ( int i
= index
; i
> 0; i
-- )
3515 (*s_pfnImageList_Copy
)(hImageList
, i
,
3520 // we must remove the image in POSTPAINT notify
3521 *result
|= CDRF_NOTIFYPOSTPAINT
;
3526 case CDDS_POSTPAINT
:
3527 // we are deleting temp image of 0 index, which was
3528 // added before items were drawn (in PREPAINT notify)
3529 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3530 m_imageListState
->Remove(0);
3533 case CDDS_ITEMPREPAINT
:
3535 wxMapTreeAttr::iterator
3536 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
3538 if ( it
== m_attrs
.end() )
3540 // nothing to do for this item
3541 *result
= CDRF_DODEFAULT
;
3545 wxTreeItemAttr
* const attr
= it
->second
;
3547 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
3548 TVIF_STATE
, TVIS_DROPHILITED
);
3550 const UINT tvItemState
= tvItem
.state
;
3552 // selection colours should override ours,
3553 // otherwise it is too confusing to the user
3554 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
3555 !(tvItemState
& TVIS_DROPHILITED
) )
3558 if ( attr
->HasBackgroundColour() )
3560 colBack
= attr
->GetBackgroundColour();
3561 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
3565 // but we still want to keep the special foreground
3566 // colour when we don't have focus (we can't keep
3567 // it when we do, it would usually be unreadable on
3568 // the almost inverted bg colour...)
3569 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
3570 FindFocus() != this ) &&
3571 !(tvItemState
& TVIS_DROPHILITED
) )
3574 if ( attr
->HasTextColour() )
3576 colText
= attr
->GetTextColour();
3577 lptvcd
->clrText
= wxColourToRGB(colText
);
3581 if ( attr
->HasFont() )
3583 HFONT hFont
= GetHfontOf(attr
->GetFont());
3585 ::SelectObject(nmcd
.hdc
, hFont
);
3587 *result
= CDRF_NEWFONT
;
3589 else // no specific font
3591 *result
= CDRF_DODEFAULT
;
3597 *result
= CDRF_DODEFAULT
;
3601 // we always process it
3603 #endif // have owner drawn support in headers
3607 DWORD pos
= GetMessagePos();
3609 point
.x
= LOWORD(pos
);
3610 point
.y
= HIWORD(pos
);
3611 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
3613 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), htFlags
);
3615 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3617 event
.m_item
= item
;
3618 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
3627 TV_HITTESTINFO tvhti
;
3628 ::GetCursorPos(&tvhti
.pt
);
3629 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
3630 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
3632 if ( MSWIsOnItem(tvhti
.flags
) )
3634 event
.m_item
= tvhti
.hItem
;
3635 eventType
= (int)hdr
->code
== NM_DBLCLK
3636 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
3637 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
3639 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
3640 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
3649 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
3652 event
.SetEventType(eventType
);
3654 bool processed
= HandleTreeEvent(event
);
3657 switch ( hdr
->code
)
3660 // we translate NM_DBLCLK into ACTIVATED event and if the user
3661 // handled the activation of the item we shouldn't proceed with
3662 // also using the same double click for toggling the item expanded
3663 // state -- but OTOH do let the user to expand/collapse the item by
3664 // double clicking on it if the activation is not handled specially
3665 *result
= processed
;
3669 // prevent tree control from sending WM_CONTEXTMENU to our parent
3670 // (which it does if NM_RCLICK is not handled) because we want to
3671 // send it to the control itself
3675 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
3676 (WPARAM
)GetHwnd(), ::GetMessagePos());
3680 case TVN_BEGINRDRAG
:
3682 if ( event
.IsAllowed() )
3684 // normally this is impossible because the m_dragImage is
3685 // deleted once the drag operation is over
3686 wxASSERT_MSG( !m_dragImage
, wxT("starting to drag once again?") );
3688 m_dragImage
= new wxDragImage(*this, event
.m_item
);
3689 m_dragImage
->BeginDrag(wxPoint(0,0), this);
3690 m_dragImage
->Show();
3692 m_dragStarted
= true;
3694 #endif // wxUSE_DRAGIMAGE
3697 case TVN_DELETEITEM
:
3699 // NB: we might process this message using wxWidgets event
3700 // tables, but due to overhead of wxWin event system we
3701 // prefer to do it here ourself (otherwise deleting a tree
3702 // with many items is just too slow)
3703 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3705 wxTreeItemParam
*param
=
3706 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
3709 processed
= true; // Make sure we don't get called twice
3713 case TVN_BEGINLABELEDIT
:
3714 // return true to cancel label editing
3715 *result
= !event
.IsAllowed();
3717 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3718 if ( event
.IsAllowed() )
3720 HWND hText
= TreeView_GetEditControl(GetHwnd());
3723 // MBN: if m_textCtrl already has an HWND, it is a stale
3724 // pointer from a previous edit (because the user
3725 // didn't modify the label before dismissing the control,
3726 // and TVN_ENDLABELEDIT was not sent), so delete it
3727 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
3730 m_textCtrl
= new wxTextCtrl();
3731 m_textCtrl
->SetParent(this);
3732 m_textCtrl
->SetHWND((WXHWND
)hText
);
3733 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3735 // set wxTE_PROCESS_ENTER style for the text control to
3736 // force it to process the Enter presses itself, otherwise
3737 // they could be stolen from it by the dialog
3739 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3740 | wxTE_PROCESS_ENTER
);
3743 else // we had set m_idEdited before
3749 case TVN_ENDLABELEDIT
:
3750 // return true to set the label to the new string: note that we
3751 // also must pretend that we did process the message or it is going
3752 // to be passed to DefWindowProc() which will happily return false
3753 // cancelling the label change
3754 *result
= event
.IsAllowed();
3757 // ensure that we don't have the text ctrl which is going to be
3763 #ifdef TVN_GETINFOTIP
3764 case TVN_GETINFOTIP
:
3766 // If the user permitted a tooltip change, change it
3767 if (event
.IsAllowed())
3769 SetToolTip(event
.m_label
);
3776 case TVN_SELCHANGING
:
3777 case TVN_ITEMEXPANDING
:
3778 // return true to prevent the action from happening
3779 *result
= !event
.IsAllowed();
3782 case TVN_ITEMEXPANDED
:
3784 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3785 const wxTreeItemId
id(tv
->itemNew
.hItem
);
3787 if ( tv
->action
== TVE_COLLAPSE
)
3789 if ( wxApp::GetComCtl32Version() >= 600 )
3791 // for some reason the item selection rectangle depends
3792 // on whether it is expanded or collapsed (at least
3793 // with comctl32.dll v6): it is wider (by 3 pixels) in
3794 // the expanded state, so when the item collapses and
3795 // then is deselected the rightmost 3 pixels of the
3796 // previously drawn selection are left on the screen
3798 // it's not clear if it's a bug in comctl32.dll or in
3799 // our code (because it does not happen in Explorer but
3800 // OTOH we don't do anything which could result in this
3801 // AFAICS) but we do need to work around it to avoid
3808 // the item is also not refreshed properly after expansion when
3809 // it has an image depending on the expanded/collapsed state:
3810 // again, it's not clear if the bug is in comctl32.dll or our
3812 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3821 case TVN_GETDISPINFO
:
3822 // NB: so far the user can't set the image himself anyhow, so do it
3823 // anyway - but this may change later
3824 //if ( /* !processed && */ )
3826 wxTreeItemId item
= event
.m_item
;
3827 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3829 const wxTreeItemParam
* const param
= GetItemParam(item
);
3833 if ( info
->item
.mask
& TVIF_IMAGE
)
3838 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3839 : wxTreeItemIcon_Normal
3842 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3844 info
->item
.iSelectedImage
=
3847 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3848 : wxTreeItemIcon_Selected
3855 // for the other messages the return value is ignored and there is
3856 // nothing special to do
3861 // ----------------------------------------------------------------------------
3863 // ----------------------------------------------------------------------------
3865 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3866 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3868 int wxTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
3870 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
3872 // receive the desired information
3873 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3876 // state images are one-based
3877 return STATEIMAGEMASKTOINDEX(tvItem
.state
) - 1;
3880 void wxTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
3882 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
3884 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3886 // state images are one-based
3887 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3888 tvItem
.state
= INDEXTOSTATEIMAGEMASK(state
+ 1);
3893 #endif // wxUSE_TREECTRL