1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/treectrl.cpp
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/msw/missing.h"
34 #include "wx/dynarray.h"
37 #include "wx/settings.h"
40 #include "wx/dynlib.h"
41 #include "wx/msw/private.h"
43 #include "wx/imaglist.h"
44 #include "wx/msw/dragimag.h"
45 #include "wx/msw/uxtheme.h"
47 // macros to hide the cast ugliness
48 // --------------------------------
50 // get HTREEITEM from wxTreeItemId
51 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
54 // older SDKs are missing these
55 #ifndef TVN_ITEMCHANGINGA
57 #define TVN_ITEMCHANGINGA (TVN_FIRST-16)
58 #define TVN_ITEMCHANGINGW (TVN_FIRST-17)
60 typedef struct tagNMTVITEMCHANGE
73 // this helper class is used on vista systems for preventing unwanted
74 // item state changes in the vista tree control. It is only effective in
75 // multi-select mode on vista systems.
77 // The vista tree control includes some new code that originally broke the
78 // multi-selection tree, causing seemingly spurious item selection state changes
79 // during Shift or Ctrl-click item selection. (To witness the original broken
80 // behaviour, simply make IsLocked() below always return false). This problem was
81 // solved by using the following class to 'unlock' an item's selection state.
83 class TreeItemUnlocker
86 // unlock a single item
87 TreeItemUnlocker(HTREEITEM item
)
89 m_oldUnlockedItem
= ms_unlockedItem
;
90 ms_unlockedItem
= item
;
93 // unlock all items, don't use unless absolutely necessary
96 m_oldUnlockedItem
= ms_unlockedItem
;
97 ms_unlockedItem
= (HTREEITEM
)-1;
100 // lock everything back
101 ~TreeItemUnlocker() { ms_unlockedItem
= m_oldUnlockedItem
; }
104 // check if the item state is currently locked
105 static bool IsLocked(HTREEITEM item
)
106 { return ms_unlockedItem
!= (HTREEITEM
)-1 && item
!= ms_unlockedItem
; }
109 static HTREEITEM ms_unlockedItem
;
110 HTREEITEM m_oldUnlockedItem
;
112 wxDECLARE_NO_COPY_CLASS(TreeItemUnlocker
);
115 HTREEITEM
TreeItemUnlocker::ms_unlockedItem
= NULL
;
117 // another helper class: set the variable to true during its lifetime and reset
118 // it to false when it is destroyed
120 // it is currently always used with wxTreeCtrl::m_changingSelection
124 TempSetter(bool& var
) : m_var(var
)
126 wxASSERT_MSG( !m_var
, "variable shouldn't be already set" );
138 wxDECLARE_NO_COPY_CLASS(TempSetter
);
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
148 // Work around a problem with TreeView_GetItemRect() when using MinGW/Cygwin:
149 // it results in warnings about breaking strict aliasing rules because HITEM is
150 // passed via a RECT pointer, so use a union to avoid them and define our own
151 // version of the standard macro using it.
152 union TVGetItemRectParam
159 wxTreeView_GetItemRect(HWND hwnd
,
161 TVGetItemRectParam
& param
,
165 return ::SendMessage(hwnd
, TVM_GETITEMRECT
, fItemRect
,
166 (LPARAM
)¶m
) == TRUE
;
169 } // anonymous namespace
171 // wrappers for TreeView_GetItem/TreeView_SetItem
172 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
175 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
176 tvi
.stateMask
= TVIS_SELECTED
;
179 TreeItemUnlocker
unlocker(hItem
);
181 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
183 wxLogLastError(wxT("TreeView_GetItem"));
186 return (tvi
.state
& TVIS_SELECTED
) != 0;
189 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
192 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
193 tvi
.stateMask
= TVIS_SELECTED
;
194 tvi
.state
= select
? TVIS_SELECTED
: 0;
197 TreeItemUnlocker
unlocker(hItem
);
199 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
201 wxLogLastError(wxT("TreeView_SetItem"));
208 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
210 SelectItem(hwndTV
, htItem
, false);
213 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
215 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
218 // helper function which selects all items in a range and, optionally,
219 // deselects all the other ones
221 // returns true if the selection changed at all or false if nothing changed
223 // flags for SelectRange()
226 SR_SIMULATE
= 1, // don't do anything, just return true or false
227 SR_UNSELECT_OTHERS
= 2 // deselect the items not in range
230 static bool SelectRange(HWND hwndTV
,
235 // find the first (or last) item and select it
236 bool changed
= false;
238 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
240 while ( htItem
&& cont
)
242 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
244 if ( !IsItemSelected(hwndTV
, htItem
) )
246 if ( !(flags
& SR_SIMULATE
) )
248 SelectItem(hwndTV
, htItem
);
256 else // not first or last
258 if ( flags
& SR_UNSELECT_OTHERS
)
260 if ( IsItemSelected(hwndTV
, htItem
) )
262 if ( !(flags
& SR_SIMULATE
) )
263 UnselectItem(hwndTV
, htItem
);
270 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
273 // select the items in range
274 cont
= htFirst
!= htLast
;
275 while ( htItem
&& cont
)
277 if ( !IsItemSelected(hwndTV
, htItem
) )
279 if ( !(flags
& SR_SIMULATE
) )
281 SelectItem(hwndTV
, htItem
);
287 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
289 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
292 // optionally deselect the rest
293 if ( flags
& SR_UNSELECT_OTHERS
)
297 if ( IsItemSelected(hwndTV
, htItem
) )
299 if ( !(flags
& SR_SIMULATE
) )
301 UnselectItem(hwndTV
, htItem
);
307 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
311 // seems to be necessary - otherwise the just selected items don't always
312 // appear as selected
313 if ( !(flags
& SR_SIMULATE
) )
315 UpdateWindow(hwndTV
);
321 // helper function which tricks the standard control into changing the focused
322 // item without changing anything else (if someone knows why Microsoft doesn't
323 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
325 // returns true if the focus was changed, false if the given item was already
327 static bool SetFocus(HWND hwndTV
, HTREEITEM htItem
)
330 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
332 if ( htItem
== htFocus
)
337 // remember the selection state of the item
338 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
340 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
342 // prevent the tree from unselecting the old focus which it
343 // would do by default (TreeView_SelectItem unselects the
345 TreeView_SelectItem(hwndTV
, 0);
346 SelectItem(hwndTV
, htFocus
);
349 TreeView_SelectItem(hwndTV
, htItem
);
353 // need to clear the selection which TreeView_SelectItem() gave
355 UnselectItem(hwndTV
, htItem
);
357 //else: was selected, still selected - ok
361 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
363 // just clear the focus
364 TreeView_SelectItem(hwndTV
, 0);
366 if ( wasFocusSelected
)
368 // restore the selection state
369 SelectItem(hwndTV
, htFocus
);
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 // a convenient wrapper around TV_ITEM struct which adds a ctor
382 #pragma warning( disable : 4097 ) // inheriting from typedef
385 struct wxTreeViewItem
: public TV_ITEM
387 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
388 UINT mask_
, // fields which are valid
389 UINT stateMask_
= 0) // for TVIF_STATE only
393 // hItem member is always valid
394 mask
= mask_
| TVIF_HANDLE
;
395 stateMask
= stateMask_
;
400 // ----------------------------------------------------------------------------
401 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
403 // We need this for a couple of reasons:
405 // 1) This class is needed for support of different images: the Win32 common
406 // control natively supports only 2 images (the normal one and another for the
407 // selected state). We wish to provide support for 2 more of them for folder
408 // items (i.e. those which have children): for expanded state and for expanded
409 // selected state. For this we use this structure to store the additional items
412 // 2) This class is also needed to hold the HITEM so that we can sort
413 // it correctly in the MSW sort callback.
415 // In addition it makes other workarounds such as this easier and helps
416 // simplify the code.
417 // ----------------------------------------------------------------------------
419 class wxTreeItemParam
426 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
432 // dtor deletes the associated data as well
433 virtual ~wxTreeItemParam() { delete m_data
; }
436 // get the real data associated with the item
437 wxTreeItemData
*GetData() const { return m_data
; }
439 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
441 // do we have such image?
442 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
443 // get image, falling back to the other images if this one is not
445 int GetImage(wxTreeItemIcon which
) const
447 int image
= m_images
[which
];
452 case wxTreeItemIcon_SelectedExpanded
:
453 // We consider that expanded icon is more important than
454 // selected so test for it first.
455 image
= m_images
[wxTreeItemIcon_Expanded
];
457 image
= m_images
[wxTreeItemIcon_Selected
];
462 case wxTreeItemIcon_Selected
:
463 case wxTreeItemIcon_Expanded
:
464 image
= m_images
[wxTreeItemIcon_Normal
];
467 case wxTreeItemIcon_Normal
:
472 wxFAIL_MSG( wxT("unsupported wxTreeItemIcon value") );
478 // change the given image
479 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
482 const wxTreeItemId
& GetItem() const { return m_item
; }
484 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
487 // all the images associated with the item
488 int m_images
[wxTreeItemIcon_Max
];
490 // item for sort callbacks
493 // the real client data
494 wxTreeItemData
*m_data
;
496 wxDECLARE_NO_COPY_CLASS(wxTreeItemParam
);
499 // wxVirutalNode is used in place of a single root when 'hidden' root is
501 class wxVirtualNode
: public wxTreeViewItem
504 wxVirtualNode(wxTreeItemParam
*param
)
505 : wxTreeViewItem(TVI_ROOT
, 0)
515 wxTreeItemParam
*GetParam() const { return m_param
; }
516 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
519 wxTreeItemParam
*m_param
;
521 wxDECLARE_NO_COPY_CLASS(wxVirtualNode
);
525 #pragma warning( default : 4097 )
528 // a macro to get the virtual root, returns NULL if none
529 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
531 // returns true if the item is the virtual root
532 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
534 // a class which encapsulates the tree traversal logic: it vists all (unless
535 // OnVisit() returns false) items under the given one
536 class wxTreeTraversal
539 wxTreeTraversal(const wxTreeCtrl
*tree
)
544 // give it a virtual dtor: not really needed as the class is never used
545 // polymorphically and not even allocated on heap at all, but this is safer
546 // (in case it ever is) and silences the compiler warnings for now
547 virtual ~wxTreeTraversal() { }
549 // do traverse the tree: visit all items (recursively by default) under the
550 // given one; return true if all items were traversed or false if the
551 // traversal was aborted because OnVisit returned false
552 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
554 // override this function to do whatever is needed for each item, return
555 // false to stop traversing
556 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
559 const wxTreeCtrl
*GetTree() const { return m_tree
; }
562 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
564 const wxTreeCtrl
*m_tree
;
566 wxDECLARE_NO_COPY_CLASS(wxTreeTraversal
);
569 // internal class for getting the selected items
570 class TraverseSelections
: public wxTreeTraversal
573 TraverseSelections(const wxTreeCtrl
*tree
,
574 wxArrayTreeItemIds
& selections
)
575 : wxTreeTraversal(tree
), m_selections(selections
)
577 m_selections
.Empty();
579 if (tree
->GetCount() > 0)
580 DoTraverse(tree
->GetRootItem());
583 virtual bool OnVisit(const wxTreeItemId
& item
)
585 const wxTreeCtrl
* const tree
= GetTree();
587 // can't visit a virtual node.
588 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
593 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
595 m_selections
.Add(item
);
601 size_t GetCount() const { return m_selections
.GetCount(); }
604 wxArrayTreeItemIds
& m_selections
;
606 wxDECLARE_NO_COPY_CLASS(TraverseSelections
);
609 // internal class for counting tree items
610 class TraverseCounter
: public wxTreeTraversal
613 TraverseCounter(const wxTreeCtrl
*tree
,
614 const wxTreeItemId
& root
,
616 : wxTreeTraversal(tree
)
620 DoTraverse(root
, recursively
);
623 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
630 size_t GetCount() const { return m_count
; }
635 wxDECLARE_NO_COPY_CLASS(TraverseCounter
);
638 // ----------------------------------------------------------------------------
640 // ----------------------------------------------------------------------------
642 // ----------------------------------------------------------------------------
644 // ----------------------------------------------------------------------------
646 // indices in gs_expandEvents table below
661 // handy table for sending events - it has to be initialized during run-time
662 // now so can't be const any more
663 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
666 but logically it's a const table with the following entries:
669 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
670 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
674 // ============================================================================
676 // ============================================================================
678 // ----------------------------------------------------------------------------
680 // ----------------------------------------------------------------------------
682 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
684 if ( !OnVisit(root
) )
687 return Traverse(root
, recursively
);
690 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
692 wxTreeItemIdValue cookie
;
693 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
694 while ( child
.IsOk() )
696 // depth first traversal
697 if ( recursively
&& !Traverse(child
, true) )
700 if ( !OnVisit(child
) )
703 child
= m_tree
->GetNextChild(root
, cookie
);
709 // ----------------------------------------------------------------------------
710 // construction and destruction
711 // ----------------------------------------------------------------------------
713 void wxTreeCtrl::Init()
716 m_hasAnyAttr
= false;
720 m_pVirtualRoot
= NULL
;
721 m_dragStarted
= false;
723 m_changingSelection
= false;
724 m_triggerStateImageClick
= false;
725 m_mouseUpDeselect
= false;
727 // initialize the global array of events now as it can't be done statically
728 // with the wxEVT_XXX values being allocated during run-time only
729 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
730 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
731 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
732 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
735 bool wxTreeCtrl::Create(wxWindow
*parent
,
740 const wxValidator
& validator
,
741 const wxString
& name
)
745 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
746 style
|= wxBORDER_SUNKEN
;
748 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
752 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
753 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
755 if ( !(m_windowStyle
& wxTR_NO_LINES
) )
756 wstyle
|= TVS_HASLINES
;
757 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
758 wstyle
|= TVS_HASBUTTONS
;
760 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
761 wstyle
|= TVS_EDITLABELS
;
763 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
764 wstyle
|= TVS_LINESATROOT
;
766 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
768 if ( wxApp::GetComCtl32Version() >= 471 )
769 wstyle
|= TVS_FULLROWSELECT
;
772 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
773 // Need so that TVN_GETINFOTIP messages will be sent
774 wstyle
|= TVS_INFOTIP
;
777 // Create the tree control.
778 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
781 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
782 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
784 wxSetCCUnicodeFormat(GetHwnd());
786 if ( m_windowStyle
& wxTR_TWIST_BUTTONS
)
788 // Under Vista and later Explorer uses rotating ("twist") buttons
789 // instead of the default "+/-" ones so apply its theme to the tree
790 // control to implement this style.
791 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
793 if ( wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive() )
795 theme
->SetWindowTheme(GetHwnd(), L
"EXPLORER", NULL
);
803 wxTreeCtrl::~wxTreeCtrl()
805 m_isBeingDeleted
= true;
807 // delete any attributes
810 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
812 // prevent TVN_DELETEITEM handler from deleting the attributes again!
813 m_hasAnyAttr
= false;
818 // delete user data to prevent memory leaks
819 // also deletes hidden root node storage.
823 // ----------------------------------------------------------------------------
825 // ----------------------------------------------------------------------------
827 /* static */ wxVisualAttributes
828 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
830 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
832 // common controls have their own default font
833 attrs
.font
= wxGetCCDefaultFont();
839 // simple wrappers which add error checking in debug mode
841 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
843 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
844 wxT("can't retrieve virtual root item") );
846 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
848 wxLogLastError(wxT("TreeView_GetItem"));
856 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
858 TreeItemUnlocker
unlocker(tvItem
->hItem
);
860 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
862 wxLogLastError(wxT("TreeView_SetItem"));
866 unsigned int wxTreeCtrl::GetCount() const
868 return (unsigned int)TreeView_GetCount(GetHwnd());
871 unsigned int wxTreeCtrl::GetIndent() const
873 return TreeView_GetIndent(GetHwnd());
876 void wxTreeCtrl::SetIndent(unsigned int indent
)
878 TreeView_SetIndent(GetHwnd(), indent
);
881 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
884 (void) TreeView_SetImageList(GetHwnd(),
885 imageList
? imageList
->GetHIMAGELIST() : 0,
889 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
891 if (m_ownsImageListNormal
)
892 delete m_imageListNormal
;
894 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
895 m_ownsImageListNormal
= false;
898 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
900 if (m_ownsImageListState
) delete m_imageListState
;
901 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
902 m_ownsImageListState
= false;
905 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
906 bool recursively
) const
908 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
910 TraverseCounter
counter(this, item
, recursively
);
911 return counter
.GetCount() - 1;
914 // ----------------------------------------------------------------------------
916 // ----------------------------------------------------------------------------
918 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
920 if ( !wxWindowBase::SetBackgroundColour(colour
) )
923 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
928 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
930 if ( !wxWindowBase::SetForegroundColour(colour
) )
933 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
938 // ----------------------------------------------------------------------------
940 // ----------------------------------------------------------------------------
942 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
944 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
947 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
949 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
951 wxChar buf
[512]; // the size is arbitrary...
953 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
954 tvItem
.pszText
= buf
;
955 tvItem
.cchTextMax
= WXSIZEOF(buf
);
956 if ( !DoGetItem(&tvItem
) )
958 // don't return some garbage which was on stack, but an empty string
962 return wxString(buf
);
965 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
967 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
969 if ( IS_VIRTUAL_ROOT(item
) )
972 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
973 tvItem
.pszText
= wxMSW_CONV_LPTSTR(text
);
976 // when setting the text of the item being edited, the text control should
977 // be updated to reflect the new text as well, otherwise calling
978 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
980 // don't use GetEditControl() here because m_textCtrl is not set yet
981 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
984 if ( item
== m_idEdited
)
986 ::SetWindowText(hwndEdit
, text
.t_str());
991 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
992 wxTreeItemIcon which
) const
994 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
996 if ( IsHiddenRoot(item
) )
998 // no images for hidden root item
1002 wxTreeItemParam
*param
= GetItemParam(item
);
1004 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
1007 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1008 wxTreeItemIcon which
)
1010 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1011 wxCHECK_RET( which
>= 0 &&
1012 which
< wxTreeItemIcon_Max
,
1013 wxT("invalid image index"));
1016 if ( IsHiddenRoot(item
) )
1018 // no images for hidden root item
1022 wxTreeItemParam
*data
= GetItemParam(item
);
1026 data
->SetImage(image
, which
);
1031 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
1033 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1035 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1037 // hidden root may still have data.
1038 if ( IS_VIRTUAL_ROOT(item
) )
1040 return GET_VIRTUAL_ROOT()->GetParam();
1044 if ( !DoGetItem(&tvItem
) )
1049 return (wxTreeItemParam
*)tvItem
.lParam
;
1052 bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent
& event
) const
1054 if ( event
.m_item
.IsOk() )
1056 event
.SetClientObject(GetItemData(event
.m_item
));
1059 return HandleWindowEvent(event
);
1062 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1064 wxTreeItemParam
*data
= GetItemParam(item
);
1066 return data
? data
->GetData() : NULL
;
1069 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1071 // first, associate this piece of data with this item
1077 wxTreeItemParam
*param
= GetItemParam(item
);
1079 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1081 param
->SetData(data
);
1084 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1086 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1088 if ( IS_VIRTUAL_ROOT(item
) )
1091 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1092 tvItem
.cChildren
= (int)has
;
1096 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1098 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1100 if ( IS_VIRTUAL_ROOT(item
) )
1103 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1104 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1108 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1110 if ( IS_VIRTUAL_ROOT(item
) )
1113 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1114 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1118 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1120 if ( IS_VIRTUAL_ROOT(item
) )
1124 if ( GetBoundingRect(item
, rect
) )
1130 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1132 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1134 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1135 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1138 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1140 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1142 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1143 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1146 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1148 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1150 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1151 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1154 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1155 const wxColour
& col
)
1157 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1159 wxTreeItemAttr
*attr
;
1160 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1161 if ( it
== m_attrs
.end() )
1163 m_hasAnyAttr
= true;
1165 m_attrs
[item
.m_pItem
] =
1166 attr
= new wxTreeItemAttr
;
1173 attr
->SetTextColour(col
);
1178 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1179 const wxColour
& col
)
1181 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1183 wxTreeItemAttr
*attr
;
1184 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1185 if ( it
== m_attrs
.end() )
1187 m_hasAnyAttr
= true;
1189 m_attrs
[item
.m_pItem
] =
1190 attr
= new wxTreeItemAttr
;
1192 else // already in the hash
1197 attr
->SetBackgroundColour(col
);
1202 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1204 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1206 wxTreeItemAttr
*attr
;
1207 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1208 if ( it
== m_attrs
.end() )
1210 m_hasAnyAttr
= true;
1212 m_attrs
[item
.m_pItem
] =
1213 attr
= new wxTreeItemAttr
;
1215 else // already in the hash
1220 attr
->SetFont(font
);
1222 // Reset the item's text to ensure that the bounding rect will be adjusted
1223 // for the new font.
1224 SetItemText(item
, GetItemText(item
));
1229 // ----------------------------------------------------------------------------
1231 // ----------------------------------------------------------------------------
1233 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1235 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1237 if ( item
== wxTreeItemId(TVI_ROOT
) )
1239 // virtual (hidden) root is never visible
1243 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1244 TVGetItemRectParam param
;
1246 // true means to get rect for just the text, not the whole line
1247 if ( !wxTreeView_GetItemRect(GetHwnd(), HITEM(item
), param
, TRUE
) )
1249 // if TVM_GETITEMRECT returned false, then the item is definitely not
1250 // visible (because its parent is not expanded)
1254 // however if it returned true, the item might still be outside the
1255 // currently visible part of the tree, test for it (notice that partly
1256 // visible means visible here)
1257 return param
.rect
.bottom
> 0 && param
.rect
.top
< GetClientSize().y
;
1260 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1262 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1264 if ( IS_VIRTUAL_ROOT(item
) )
1266 wxTreeItemIdValue cookie
;
1267 return GetFirstChild(item
, cookie
).IsOk();
1270 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1273 return tvItem
.cChildren
!= 0;
1276 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1278 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1280 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1283 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1286 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1288 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1290 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1293 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1296 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1298 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1300 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1303 return (tvItem
.state
& TVIS_BOLD
) != 0;
1306 // ----------------------------------------------------------------------------
1308 // ----------------------------------------------------------------------------
1310 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1312 // Root may be real (visible) or virtual (hidden).
1313 if ( GET_VIRTUAL_ROOT() )
1316 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1319 wxTreeItemId
wxTreeCtrl::GetSelection() const
1321 wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE
), wxTreeItemId(),
1322 wxT("this only works with single selection controls") );
1324 return GetFocusedItem();
1327 wxTreeItemId
wxTreeCtrl::GetFocusedItem() const
1329 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1332 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1334 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1338 if ( IS_VIRTUAL_ROOT(item
) )
1340 // no parent for the virtual root
1345 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1346 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1348 // the top level items should have the virtual root as their parent
1353 return wxTreeItemId(hItem
);
1356 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1357 wxTreeItemIdValue
& cookie
) const
1359 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1361 // remember the last child returned in 'cookie'
1362 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1364 return wxTreeItemId(cookie
);
1367 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1368 wxTreeItemIdValue
& cookie
) const
1370 wxTreeItemId
fromCookie(cookie
);
1372 HTREEITEM hitem
= HITEM(fromCookie
);
1374 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1376 wxTreeItemId
item(hitem
);
1378 cookie
= item
.m_pItem
;
1383 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1385 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1387 // can this be done more efficiently?
1388 wxTreeItemIdValue cookie
;
1390 wxTreeItemId childLast
,
1391 child
= GetFirstChild(item
, cookie
);
1392 while ( child
.IsOk() )
1395 child
= GetNextChild(item
, cookie
);
1401 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1403 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1404 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1407 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1409 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1410 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1413 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1415 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1418 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1420 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1421 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1423 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1424 if ( next
.IsOk() && !IsVisible(next
) )
1426 // Win32 considers that any non-collapsed item is visible while we want
1427 // to return only really visible items
1434 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1436 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1437 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1439 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1440 if ( prev
.IsOk() && !IsVisible(prev
) )
1442 // just as above, Win32 function will happily return the previous item
1443 // in the tree for the first visible item too
1450 // ----------------------------------------------------------------------------
1451 // multiple selections emulation
1452 // ----------------------------------------------------------------------------
1454 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1456 TraverseSelections
selector(this, selections
);
1458 return selector
.GetCount();
1461 // ----------------------------------------------------------------------------
1463 // ----------------------------------------------------------------------------
1465 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1466 const wxTreeItemId
& hInsertAfter
,
1467 const wxString
& text
,
1468 int image
, int selectedImage
,
1469 wxTreeItemData
*data
)
1471 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1473 wxT("can't have more than one root in the tree") );
1475 TV_INSERTSTRUCT tvIns
;
1476 tvIns
.hParent
= HITEM(parent
);
1477 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1479 // this is how we insert the item as the first child: supply a NULL
1481 if ( !tvIns
.hInsertAfter
)
1483 tvIns
.hInsertAfter
= TVI_FIRST
;
1487 if ( !text
.empty() )
1490 tvIns
.item
.pszText
= wxMSW_CONV_LPTSTR(text
);
1494 tvIns
.item
.pszText
= NULL
;
1495 tvIns
.item
.cchTextMax
= 0;
1498 // create the param which will store the other item parameters
1499 wxTreeItemParam
*param
= new wxTreeItemParam
;
1501 // we return the images on demand as they depend on whether the item is
1502 // expanded or collapsed too in our case
1503 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1504 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1505 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1507 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1508 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1511 tvIns
.item
.lParam
= (LPARAM
)param
;
1512 tvIns
.item
.mask
= mask
;
1514 // don't use the hack below for the children of hidden root: this results
1515 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1516 const bool firstChild
= !IsHiddenRoot(parent
) &&
1517 !TreeView_GetChild(GetHwnd(), HITEM(parent
));
1519 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1522 wxLogLastError(wxT("TreeView_InsertItem"));
1525 // apparently some Windows versions (2000 and XP are reported to do this)
1526 // sometimes don't refresh the tree after adding the first child and so we
1527 // need this to make the "[+]" appear
1530 TVGetItemRectParam param
;
1532 wxTreeView_GetItemRect(GetHwnd(), HITEM(parent
), param
, FALSE
);
1533 ::InvalidateRect(GetHwnd(), ¶m
.rect
, FALSE
);
1536 // associate the application tree item with Win32 tree item handle
1539 // setup wxTreeItemData
1542 param
->SetData(data
);
1546 return wxTreeItemId(id
);
1549 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1550 int image
, int selectedImage
,
1551 wxTreeItemData
*data
)
1553 if ( HasFlag(wxTR_HIDE_ROOT
) )
1555 wxASSERT_MSG( !m_pVirtualRoot
, wxT("tree can have only a single root") );
1557 // create a virtual root item, the parent for all the others
1558 wxTreeItemParam
*param
= new wxTreeItemParam
;
1559 param
->SetData(data
);
1561 m_pVirtualRoot
= new wxVirtualNode(param
);
1566 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1567 text
, image
, selectedImage
, data
);
1570 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1572 const wxString
& text
,
1573 int image
, int selectedImage
,
1574 wxTreeItemData
*data
)
1576 wxTreeItemId idPrev
;
1577 if ( index
== (size_t)-1 )
1579 // special value: append to the end
1582 else // find the item from index
1584 wxTreeItemIdValue cookie
;
1585 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1586 while ( index
!= 0 && idCur
.IsOk() )
1591 idCur
= GetNextChild(parent
, cookie
);
1594 // assert, not check: if the index is invalid, we will append the item
1596 wxASSERT_MSG( index
== 0, wxT("bad index in wxTreeCtrl::InsertItem") );
1599 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1602 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1604 // unlock tree selections on vista, without this the
1605 // tree ctrl will eventually crash after item deletion
1606 TreeItemUnlocker unlock_all
;
1608 if ( HasFlag(wxTR_MULTIPLE
) )
1610 bool selected
= IsSelected(item
);
1615 next
= TreeView_GetNextVisible(GetHwnd(), HITEM(item
));
1619 next
= TreeView_GetPrevVisible(GetHwnd(), HITEM(item
));
1624 TempSetter
set(m_changingSelection
);
1625 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1627 wxLogLastError(wxT("TreeView_DeleteItem"));
1637 if ( item
== m_htSelStart
)
1638 m_htSelStart
.Unset();
1640 if ( item
== m_htClickedItem
)
1641 m_htClickedItem
.Unset();
1645 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
1647 if ( IsTreeEventAllowed(changingEvent
) )
1649 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
1650 (void)HandleTreeEvent(changedEvent
);
1654 DoUnselectItem(next
);
1661 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1663 wxLogLastError(wxT("TreeView_DeleteItem"));
1668 // delete all children (but don't delete the item itself)
1669 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1671 // unlock tree selections on vista for the duration of this call
1672 TreeItemUnlocker unlock_all
;
1674 wxTreeItemIdValue cookie
;
1676 wxArrayTreeItemIds children
;
1677 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1678 while ( child
.IsOk() )
1680 children
.Add(child
);
1682 child
= GetNextChild(item
, cookie
);
1685 size_t nCount
= children
.Count();
1686 for ( size_t n
= 0; n
< nCount
; n
++ )
1688 Delete(children
[n
]);
1692 void wxTreeCtrl::DeleteAllItems()
1694 // unlock tree selections on vista for the duration of this call
1695 TreeItemUnlocker unlock_all
;
1697 // invalidate all the items we store as they're going to become invalid
1699 m_htClickedItem
= wxTreeItemId();
1701 // delete the "virtual" root item.
1702 if ( GET_VIRTUAL_ROOT() )
1704 delete GET_VIRTUAL_ROOT();
1705 m_pVirtualRoot
= NULL
;
1708 // and all the real items
1710 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1712 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1716 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1718 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1719 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1720 flag
== TVE_EXPAND
||
1722 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1724 // A hidden root can be neither expanded nor collapsed.
1725 wxCHECK_RET( !IsHiddenRoot(item
),
1726 wxT("Can't expand/collapse hidden root node!") );
1728 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1729 // emulate them. This behaviour has changed slightly with comctl32.dll
1730 // v 4.70 - now it does send them but only the first time. To maintain
1731 // compatible behaviour and also in order to not have surprises with the
1732 // future versions, don't rely on this and still do everything ourselves.
1733 // To avoid that the messages be sent twice when the item is expanded for
1734 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1736 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1740 if ( IsExpanded(item
) )
1742 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
,
1743 this, wxTreeItemId(item
));
1745 if ( !IsTreeEventAllowed(event
) )
1749 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) )
1751 if ( IsExpanded(item
) )
1754 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, this, item
);
1755 (void)HandleTreeEvent(event
);
1757 //else: change didn't took place, so do nothing at all
1760 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1762 DoExpand(item
, TVE_EXPAND
);
1765 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1767 DoExpand(item
, TVE_COLLAPSE
);
1770 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1772 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1775 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1777 DoExpand(item
, TVE_TOGGLE
);
1780 void wxTreeCtrl::Unselect()
1782 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE
),
1783 wxT("doesn't make sense, may be you want UnselectAll()?") );
1785 // the current focus
1786 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1793 if ( HasFlag(wxTR_MULTIPLE
) )
1795 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
1796 this, wxTreeItemId());
1797 changingEvent
.m_itemOld
= htFocus
;
1799 if ( IsTreeEventAllowed(changingEvent
) )
1803 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1804 this, wxTreeItemId());
1805 changedEvent
.m_itemOld
= htFocus
;
1806 (void)HandleTreeEvent(changedEvent
);
1815 void wxTreeCtrl::DoUnselectAll()
1817 wxArrayTreeItemIds selections
;
1818 size_t count
= GetSelections(selections
);
1820 for ( size_t n
= 0; n
< count
; n
++ )
1822 DoUnselectItem(selections
[n
]);
1825 m_htSelStart
.Unset();
1828 void wxTreeCtrl::UnselectAll()
1830 if ( HasFlag(wxTR_MULTIPLE
) )
1832 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1833 if ( !htFocus
) return;
1835 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1836 changingEvent
.m_itemOld
= htFocus
;
1838 if ( IsTreeEventAllowed(changingEvent
) )
1842 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1843 changedEvent
.m_itemOld
= htFocus
;
1844 (void)HandleTreeEvent(changedEvent
);
1853 void wxTreeCtrl::DoSelectChildren(const wxTreeItemId
& parent
)
1857 wxTreeItemIdValue cookie
;
1858 wxTreeItemId child
= GetFirstChild(parent
, cookie
);
1859 while ( child
.IsOk() )
1861 DoSelectItem(child
, true);
1862 child
= GetNextChild(child
, cookie
);
1866 void wxTreeCtrl::SelectChildren(const wxTreeItemId
& parent
)
1868 wxCHECK_RET( HasFlag(wxTR_MULTIPLE
),
1869 "this only works with multiple selection controls" );
1871 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1873 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1874 changingEvent
.m_itemOld
= htFocus
;
1876 if ( IsTreeEventAllowed(changingEvent
) )
1878 DoSelectChildren(parent
);
1880 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1881 changedEvent
.m_itemOld
= htFocus
;
1882 (void)HandleTreeEvent(changedEvent
);
1886 void wxTreeCtrl::DoSelectItem(const wxTreeItemId
& item
, bool select
)
1888 TempSetter
set(m_changingSelection
);
1890 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1893 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1895 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't select hidden root item") );
1897 if ( select
== IsSelected(item
) )
1899 // nothing to do, the item is already in the requested state
1903 if ( HasFlag(wxTR_MULTIPLE
) )
1905 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1907 if ( IsTreeEventAllowed(changingEvent
) )
1909 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1910 DoSelectItem(item
, select
);
1914 SetFocusedItem(item
);
1917 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1919 (void)HandleTreeEvent(changedEvent
);
1922 else // single selection
1924 wxTreeItemId itemOld
, itemNew
;
1927 itemOld
= GetSelection();
1930 else // deselecting the currently selected item
1933 // leave itemNew invalid
1936 // Recent versions of comctl32.dll send TVN_SELCHANG{ED,ING} events
1937 // when we call TreeView_SelectItem() but apparently some old ones did
1938 // not so send the events ourselves and ignore those generated by
1939 // TreeView_SelectItem() if m_changingSelection is set.
1941 changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, itemNew
);
1942 changingEvent
.SetOldItem(itemOld
);
1944 if ( IsTreeEventAllowed(changingEvent
) )
1946 TempSetter
set(m_changingSelection
);
1948 if ( !TreeView_SelectItem(GetHwnd(), HITEM(itemNew
)) )
1950 wxLogLastError(wxT("TreeView_SelectItem"));
1954 ::SetFocus(GetHwnd(), HITEM(item
));
1956 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1958 changedEvent
.SetOldItem(itemOld
);
1959 (void)HandleTreeEvent(changedEvent
);
1962 //else: program vetoed the change
1966 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1968 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't show hidden root item") );
1971 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1974 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1976 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1978 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1982 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1987 void wxTreeCtrl::DeleteTextCtrl()
1991 // the HWND corresponding to this control is deleted by the tree
1992 // control itself and we don't know when exactly this happens, so check
1993 // if the window still exists before calling UnsubclassWin()
1994 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1996 m_textCtrl
->SetHWND(0);
1999 m_textCtrl
->UnsubclassWin();
2000 m_textCtrl
->SetHWND(0);
2001 wxDELETE(m_textCtrl
);
2007 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2008 wxClassInfo
*textControlClass
)
2010 wxASSERT( textControlClass
->IsKindOf(wxCLASSINFO(wxTextCtrl
)) );
2015 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
2016 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
2018 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2022 wxDELETE(m_textCtrl
);
2026 // textctrl is subclassed in MSWOnNotify
2030 // End label editing, optionally cancelling the edit
2031 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
2033 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
2038 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2040 TV_HITTESTINFO hitTestInfo
;
2041 hitTestInfo
.pt
.x
= (int)point
.x
;
2042 hitTestInfo
.pt
.y
= (int)point
.y
;
2044 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2049 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2050 flags |= wxTREE_HITTEST_##flag
2052 TRANSLATE_FLAG(ABOVE
);
2053 TRANSLATE_FLAG(BELOW
);
2054 TRANSLATE_FLAG(NOWHERE
);
2055 TRANSLATE_FLAG(ONITEMBUTTON
);
2056 TRANSLATE_FLAG(ONITEMICON
);
2057 TRANSLATE_FLAG(ONITEMINDENT
);
2058 TRANSLATE_FLAG(ONITEMLABEL
);
2059 TRANSLATE_FLAG(ONITEMRIGHT
);
2060 TRANSLATE_FLAG(ONITEMSTATEICON
);
2061 TRANSLATE_FLAG(TOLEFT
);
2062 TRANSLATE_FLAG(TORIGHT
);
2064 #undef TRANSLATE_FLAG
2066 return wxTreeItemId(hitTestInfo
.hItem
);
2069 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2071 bool textOnly
) const
2073 // Virtual root items have no bounding rectangle
2074 if ( IS_VIRTUAL_ROOT(item
) )
2079 TVGetItemRectParam param
;
2081 if ( wxTreeView_GetItemRect(GetHwnd(), HITEM(item
), param
, textOnly
) )
2083 rect
= wxRect(wxPoint(param
.rect
.left
, param
.rect
.top
),
2084 wxPoint(param
.rect
.right
, param
.rect
.bottom
));
2090 // couldn't retrieve rect: for example, item isn't visible
2095 void wxTreeCtrl::ClearFocusedItem()
2097 TempSetter
set(m_changingSelection
);
2099 if ( !TreeView_SelectItem(GetHwnd(), 0) )
2101 wxLogLastError(wxT("TreeView_SelectItem"));
2105 void wxTreeCtrl::SetFocusedItem(const wxTreeItemId
& item
)
2107 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2109 TempSetter
set(m_changingSelection
);
2111 ::SetFocus(GetHwnd(), HITEM(item
));
2114 void wxTreeCtrl::DoUnselectItem(const wxTreeItemId
& item
)
2116 TempSetter
set(m_changingSelection
);
2118 ::UnselectItem(GetHwnd(), HITEM(item
));
2121 void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId
& item
)
2123 TempSetter
set(m_changingSelection
);
2125 ::ToggleItemSelection(GetHwnd(), HITEM(item
));
2128 // ----------------------------------------------------------------------------
2130 // ----------------------------------------------------------------------------
2132 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2133 // functions such as IsDataIndirect()
2134 class wxTreeSortHelper
2137 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2140 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
2142 return ((wxTreeItemParam
*)lParam
)->GetItem();
2146 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2150 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2151 wxT("sorting tree without data doesn't make sense") );
2153 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2155 return tree
->OnCompareItems(GetIdFromData(pItem1
),
2156 GetIdFromData(pItem2
));
2159 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2161 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2163 // rely on the fact that TreeView_SortChildren does the same thing as our
2164 // default behaviour, i.e. sorts items alphabetically and so call it
2165 // directly if we're not in derived class (much more efficient!)
2166 // RN: Note that if you find you're code doesn't sort as expected this
2167 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
2168 // combo for your derived wxTreeCtrl if will sort without
2170 if ( GetClassInfo() == wxCLASSINFO(wxTreeCtrl
) )
2172 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2177 tvSort
.hParent
= HITEM(item
);
2178 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2179 tvSort
.lParam
= (LPARAM
)this;
2180 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2184 // ----------------------------------------------------------------------------
2186 // ----------------------------------------------------------------------------
2188 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
2190 if ( msg
->message
== WM_KEYDOWN
)
2192 // Only eat VK_RETURN if not being used by the application in
2193 // conjunction with modifiers
2194 if ( (msg
->wParam
== VK_RETURN
) && !wxIsAnyModifierDown() )
2196 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2201 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
2204 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
2206 const int id
= (signed short)id_
;
2208 if ( cmd
== EN_UPDATE
)
2210 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2211 event
.SetEventObject( this );
2212 ProcessCommand(event
);
2214 else if ( cmd
== EN_KILLFOCUS
)
2216 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2217 event
.SetEventObject( this );
2218 ProcessCommand(event
);
2226 // command processed
2230 bool wxTreeCtrl::MSWIsOnItem(unsigned flags
) const
2232 unsigned mask
= TVHT_ONITEM
;
2233 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2234 mask
|= TVHT_ONITEMINDENT
| TVHT_ONITEMRIGHT
;
2236 return (flags
& mask
) != 0;
2239 bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey
)
2241 const bool bCtrl
= wxIsCtrlDown();
2242 const bool bShift
= wxIsShiftDown();
2243 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2252 if ( vkey
!= VK_RETURN
&& bCtrl
)
2254 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2256 changingEvent
.m_itemOld
= htSel
;
2258 if ( IsTreeEventAllowed(changingEvent
) )
2260 DoToggleItemSelection(wxTreeItemId(htSel
));
2262 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2264 changedEvent
.m_itemOld
= htSel
;
2265 (void)HandleTreeEvent(changedEvent
);
2270 wxArrayTreeItemIds selections
;
2271 size_t count
= GetSelections(selections
);
2273 if ( count
!= 1 || HITEM(selections
[0]) != htSel
)
2275 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2277 changingEvent
.m_itemOld
= htSel
;
2279 if ( IsTreeEventAllowed(changingEvent
) )
2282 DoSelectItem(wxTreeItemId(htSel
));
2284 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2286 changedEvent
.m_itemOld
= htSel
;
2287 (void)HandleTreeEvent(changedEvent
);
2295 if ( !bCtrl
&& !bShift
)
2297 wxArrayTreeItemIds selections
;
2302 next
= vkey
== VK_UP
2303 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2304 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2308 next
= GetRootItem();
2310 if ( IsHiddenRoot(next
) )
2311 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2319 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2321 changingEvent
.m_itemOld
= htSel
;
2323 if ( IsTreeEventAllowed(changingEvent
) )
2327 SetFocusedItem(next
);
2329 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2331 changedEvent
.m_itemOld
= htSel
;
2332 (void)HandleTreeEvent(changedEvent
);
2337 wxTreeItemId next
= vkey
== VK_UP
2338 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2339 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2346 if ( !m_htSelStart
)
2348 m_htSelStart
= htSel
;
2351 if ( bShift
&& SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2352 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2354 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2355 changingEvent
.m_itemOld
= htSel
;
2357 if ( IsTreeEventAllowed(changingEvent
) )
2359 SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2360 SR_UNSELECT_OTHERS
);
2362 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2363 changedEvent
.m_itemOld
= htSel
;
2364 (void)HandleTreeEvent(changedEvent
);
2368 SetFocusedItem(next
);
2373 if ( HasChildren(htSel
) && IsExpanded(htSel
) )
2379 wxTreeItemId next
= GetItemParent(htSel
);
2381 if ( next
.IsOk() && !IsHiddenRoot(next
) )
2383 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2385 changingEvent
.m_itemOld
= htSel
;
2387 if ( IsTreeEventAllowed(changingEvent
) )
2391 SetFocusedItem(next
);
2393 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2395 changedEvent
.m_itemOld
= htSel
;
2396 (void)HandleTreeEvent(changedEvent
);
2403 if ( !IsVisible(htSel
) )
2405 EnsureVisible(htSel
);
2408 if ( !HasChildren(htSel
) )
2411 if ( !IsExpanded(htSel
) )
2417 wxTreeItemId next
= TreeView_GetChild(GetHwnd(), htSel
);
2419 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2420 changingEvent
.m_itemOld
= htSel
;
2422 if ( IsTreeEventAllowed(changingEvent
) )
2426 SetFocusedItem(next
);
2428 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2429 changedEvent
.m_itemOld
= htSel
;
2430 (void)HandleTreeEvent(changedEvent
);
2438 wxTreeItemId next
= GetRootItem();
2440 if ( IsHiddenRoot(next
) )
2442 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2448 if ( vkey
== VK_END
)
2452 wxTreeItemId nextTemp
= TreeView_GetNextVisible(
2453 GetHwnd(), HITEM(next
));
2455 if ( !nextTemp
.IsOk() )
2462 if ( htSel
== HITEM(next
) )
2467 if ( !m_htSelStart
)
2469 m_htSelStart
= htSel
;
2472 if ( SelectRange(GetHwnd(),
2473 HITEM(m_htSelStart
), HITEM(next
),
2474 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2476 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2478 changingEvent
.m_itemOld
= htSel
;
2480 if ( IsTreeEventAllowed(changingEvent
) )
2482 SelectRange(GetHwnd(),
2483 HITEM(m_htSelStart
), HITEM(next
),
2484 SR_UNSELECT_OTHERS
);
2485 SetFocusedItem(next
);
2487 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2489 changedEvent
.m_itemOld
= htSel
;
2490 (void)HandleTreeEvent(changedEvent
);
2496 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2498 changingEvent
.m_itemOld
= htSel
;
2500 if ( IsTreeEventAllowed(changingEvent
) )
2504 SetFocusedItem(next
);
2506 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2508 changedEvent
.m_itemOld
= htSel
;
2509 (void)HandleTreeEvent(changedEvent
);
2519 wxTreeItemId firstVisible
= GetFirstVisibleItem();
2520 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2521 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2522 TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible
)) :
2523 TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible
));
2525 if ( !nextAdjacent
)
2530 wxTreeItemId nextStart
= firstVisible
;
2532 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2534 wxTreeItemId nextTemp
= (vkey
== VK_PRIOR
) ?
2535 TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart
)) :
2536 TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart
));
2538 if ( nextTemp
.IsOk() )
2540 nextStart
= nextTemp
;
2548 EnsureVisible(nextStart
);
2550 if ( vkey
== VK_NEXT
)
2552 wxTreeItemId nextEnd
= nextStart
;
2554 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2556 wxTreeItemId nextTemp
=
2557 TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd
));
2559 if ( nextTemp
.IsOk() )
2569 EnsureVisible(nextEnd
);
2574 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2575 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2576 TreeView_GetPrevVisible(GetHwnd(), htSel
) :
2577 TreeView_GetNextVisible(GetHwnd(), htSel
);
2579 if ( !nextAdjacent
)
2584 wxTreeItemId
next(htSel
);
2586 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2588 wxTreeItemId nextTemp
= vkey
== VK_PRIOR
?
2589 TreeView_GetPrevVisible(GetHwnd(), HITEM(next
)) :
2590 TreeView_GetNextVisible(GetHwnd(), HITEM(next
));
2592 if ( !nextTemp
.IsOk() )
2598 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2600 changingEvent
.m_itemOld
= htSel
;
2602 if ( IsTreeEventAllowed(changingEvent
) )
2605 m_htSelStart
.Unset();
2607 SetFocusedItem(next
);
2609 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2611 changedEvent
.m_itemOld
= htSel
;
2612 (void)HandleTreeEvent(changedEvent
);
2624 bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam
, WXLPARAM lParam
)
2626 wxTreeEvent
keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2627 keyEvent
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
, wParam
, lParam
);
2629 bool processed
= HandleTreeEvent(keyEvent
);
2631 // generate a separate event for Space/Return
2632 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
2633 ((wParam
== VK_SPACE
) || (wParam
== VK_RETURN
)) )
2635 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2638 wxTreeEvent
activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2640 (void)HandleTreeEvent(activatedEvent
);
2647 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2648 // only do it during dragging, minimize wxWin overhead (this is important for
2649 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2650 // instead of passing by wxWin events
2652 wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2654 bool processed
= false;
2656 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2658 if ( nMsg
== WM_CONTEXTMENU
)
2660 int x
= GET_X_LPARAM(lParam
),
2661 y
= GET_Y_LPARAM(lParam
);
2663 // the item for which the menu should be shown
2666 // the position where the menu should be shown in client coordinates
2667 // (so that it can be passed directly to PopupMenu())
2670 if ( x
== -1 || y
== -1 )
2672 // this means that the event was generated from keyboard (e.g. with
2673 // Shift-F10 or special Windows menu key)
2675 // use the Explorer standard of putting the menu at the left edge
2676 // of the text, in the vertical middle of the text
2677 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2680 // Use the bounding rectangle of only the text part
2682 GetBoundingRect(item
, rect
, true);
2683 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
2686 else // event from mouse, use mouse position
2688 pt
= ScreenToClient(wxPoint(x
, y
));
2690 TV_HITTESTINFO tvhti
;
2694 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2695 item
= wxTreeItemId(tvhti
.hItem
);
2701 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2703 event
.m_pointDrag
= pt
;
2705 if ( HandleTreeEvent(event
) )
2707 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2710 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2712 // we only process mouse messages here and these parameters have the
2713 // same meaning for all of them
2714 int x
= GET_X_LPARAM(lParam
),
2715 y
= GET_Y_LPARAM(lParam
);
2717 TV_HITTESTINFO tvht
;
2721 HTREEITEM htOldItem
= TreeView_GetSelection(GetHwnd());
2722 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2726 case WM_LBUTTONDOWN
:
2730 m_htClickedItem
.Unset();
2732 if ( !MSWIsOnItem(tvht
.flags
) )
2734 if ( tvht
.flags
& TVHT_ONITEMBUTTON
)
2736 // either it's going to be handled by user code or
2737 // we're going to use it ourselves to toggle the
2738 // branch, in either case don't pass it to the base
2739 // class which would generate another mouse click event
2740 // for it even though it's already handled here
2744 if ( !HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2746 if ( !IsExpanded(htItem
) )
2757 m_focusLost
= false;
2763 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2764 m_ptClick
= wxPoint(x
, y
);
2766 if ( wParam
& MK_CONTROL
)
2768 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2770 m_htClickedItem
.Unset();
2774 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2776 changingEvent
.m_itemOld
= htOldItem
;
2778 if ( IsTreeEventAllowed(changingEvent
) )
2780 // toggle selected state
2781 DoToggleItemSelection(wxTreeItemId(htItem
));
2783 SetFocusedItem(wxTreeItemId(htItem
));
2785 // reset on any click without Shift
2786 m_htSelStart
.Unset();
2788 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2790 changedEvent
.m_itemOld
= htOldItem
;
2791 (void)HandleTreeEvent(changedEvent
);
2794 else if ( wParam
& MK_SHIFT
)
2796 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2798 m_htClickedItem
.Unset();
2803 bool willChange
= true;
2805 if ( !(wParam
& MK_CONTROL
) )
2807 srFlags
|= SR_UNSELECT_OTHERS
;
2810 if ( !m_htSelStart
)
2812 // take the focused item
2813 m_htSelStart
= htOldItem
;
2817 willChange
= SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2818 htItem
, srFlags
| SR_SIMULATE
);
2823 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2825 changingEvent
.m_itemOld
= htOldItem
;
2827 if ( IsTreeEventAllowed(changingEvent
) )
2829 // this selects all items between the starting one
2833 SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2838 DoSelectItem(wxTreeItemId(htItem
));
2841 SetFocusedItem(wxTreeItemId(htItem
));
2843 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2845 changedEvent
.m_itemOld
= htOldItem
;
2846 (void)HandleTreeEvent(changedEvent
);
2850 else // normal click
2852 // avoid doing anything if we click on the only
2853 // currently selected item
2855 wxArrayTreeItemIds selections
;
2856 size_t count
= GetSelections(selections
);
2860 HITEM(selections
[0]) != htItem
)
2862 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2864 m_htClickedItem
.Unset();
2868 // clear the previously selected items, if the user
2869 // clicked outside of the present selection, otherwise,
2870 // perform the deselection on mouse-up, this allows
2871 // multiple drag and drop to work.
2872 if ( !IsItemSelected(GetHwnd(), htItem
))
2874 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2876 changingEvent
.m_itemOld
= htOldItem
;
2878 if ( IsTreeEventAllowed(changingEvent
) )
2881 DoSelectItem(wxTreeItemId(htItem
));
2882 SetFocusedItem(wxTreeItemId(htItem
));
2884 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2886 changedEvent
.m_itemOld
= htOldItem
;
2887 (void)HandleTreeEvent(changedEvent
);
2892 SetFocusedItem(wxTreeItemId(htItem
));
2893 m_mouseUpDeselect
= true;
2896 else // click on a single selected item
2898 // don't interfere with the default processing in
2899 // WM_MOUSEMOVE handler below as the default window
2900 // proc will start the drag itself if we let have
2902 m_htClickedItem
.Unset();
2904 // prevent in-place editing from starting if focus lost
2905 // since previous click
2909 DoSelectItem(wxTreeItemId(htItem
));
2910 SetFocusedItem(wxTreeItemId(htItem
));
2918 // reset on any click without Shift
2919 m_htSelStart
.Unset();
2922 m_focusLost
= false;
2924 // we consumed the event so we need to trigger state image
2928 if ( tvht
.flags
& TVHT_ONITEMSTATEICON
)
2930 m_triggerStateImageClick
= true;
2935 case WM_RBUTTONDOWN
:
2942 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) || !htItem
)
2947 // default handler removes the highlight from the currently
2948 // focused item when right mouse button is pressed on another
2949 // one but keeps the remaining items highlighted, which is
2950 // confusing, so override this default behaviour
2951 if ( !IsItemSelected(GetHwnd(), htItem
) )
2953 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2955 changingEvent
.m_itemOld
= htOldItem
;
2957 if ( IsTreeEventAllowed(changingEvent
) )
2960 DoSelectItem(wxTreeItemId(htItem
));
2961 SetFocusedItem(wxTreeItemId(htItem
));
2963 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2965 changedEvent
.m_itemOld
= htOldItem
;
2966 (void)HandleTreeEvent(changedEvent
);
2974 if ( m_htClickedItem
)
2976 int cx
= abs(m_ptClick
.x
- x
);
2977 int cy
= abs(m_ptClick
.y
- y
);
2979 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2980 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2985 tv
.hdr
.hwndFrom
= GetHwnd();
2986 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2987 tv
.hdr
.code
= TVN_BEGINDRAG
;
2989 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2993 wxZeroMemory(tviAux
);
2995 tviAux
.hItem
= HITEM(m_htClickedItem
);
2996 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2997 tviAux
.stateMask
= 0xffffffff;
2998 TreeView_GetItem(GetHwnd(), &tviAux
);
3000 tv
.itemNew
.state
= tviAux
.state
;
3001 tv
.itemNew
.lParam
= tviAux
.lParam
;
3006 // do it before SendMessage() call below to avoid
3007 // reentrancies here if there is another WM_MOUSEMOVE
3008 // in the queue already
3009 m_htClickedItem
.Unset();
3011 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
3012 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
3014 // don't pass it to the default window proc, it would
3015 // start dragging again
3019 #endif // __WXWINCE__
3024 m_dragImage
->Move(wxPoint(x
, y
));
3027 // highlight the item as target (hiding drag image is
3028 // necessary - otherwise the display will be corrupted)
3029 m_dragImage
->Hide();
3030 TreeView_SelectDropTarget(GetHwnd(), htItem
);
3031 m_dragImage
->Show();
3034 #endif // wxUSE_DRAGIMAGE
3040 // deselect other items if needed
3043 if ( m_mouseUpDeselect
)
3045 m_mouseUpDeselect
= false;
3047 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
3049 changingEvent
.m_itemOld
= htOldItem
;
3051 if ( IsTreeEventAllowed(changingEvent
) )
3054 DoSelectItem(wxTreeItemId(htItem
));
3055 SetFocusedItem(wxTreeItemId(htItem
));
3057 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
3059 changedEvent
.m_itemOld
= htOldItem
;
3060 (void)HandleTreeEvent(changedEvent
);
3065 m_htClickedItem
.Unset();
3067 if ( m_triggerStateImageClick
)
3069 if ( tvht
.flags
& TVHT_ONITEMSTATEICON
)
3071 wxTreeEvent
event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
,
3073 (void)HandleTreeEvent(event
);
3075 m_triggerStateImageClick
= false;
3080 if ( !m_dragStarted
&& MSWIsOnItem(tvht
.flags
) )
3092 m_dragImage
->EndDrag();
3093 wxDELETE(m_dragImage
);
3095 // generate the drag end event
3096 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
,
3098 event
.m_pointDrag
= wxPoint(x
, y
);
3099 (void)HandleTreeEvent(event
);
3101 // if we don't do it, the tree seems to think that 2 items
3102 // are selected simultaneously which is quite weird
3103 TreeView_SelectDropTarget(GetHwnd(), 0);
3105 #endif // wxUSE_DRAGIMAGE
3107 if ( isMultiple
&& nMsg
== WM_RBUTTONUP
)
3111 nmhdr
.hwndFrom
= GetHwnd();
3112 nmhdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
3113 nmhdr
.code
= NM_RCLICK
;
3114 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY
,
3115 nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
3119 m_dragStarted
= false;
3124 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) )
3128 // the tree control greys out the selected item when it loses focus
3129 // and paints it as selected again when it regains it, but it won't
3130 // do it for the other items itself - help it
3131 wxArrayTreeItemIds selections
;
3132 size_t count
= GetSelections(selections
);
3133 TVGetItemRectParam param
;
3135 for ( size_t n
= 0; n
< count
; n
++ )
3137 // TreeView_GetItemRect() will return false if item is not
3138 // visible, which may happen perfectly well
3139 if ( wxTreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
3142 ::InvalidateRect(GetHwnd(), ¶m
.rect
, FALSE
);
3147 if ( nMsg
== WM_KILLFOCUS
)
3152 else if ( (nMsg
== WM_KEYDOWN
|| nMsg
== WM_SYSKEYDOWN
) && isMultiple
)
3154 // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
3155 // notification but for the keys which can be used to change selection
3156 // we need to do it from here so as to not apply the default behaviour
3157 // if the events are handled by the user code
3170 if ( !HandleKeyDown(wParam
, lParam
) &&
3171 !MSWHandleTreeKeyDownEvent(wParam
, lParam
) )
3173 // use the key to update the selection if it was left
3175 MSWHandleSelectionKey(wParam
);
3178 // pretend that we did process it in any case as we already
3179 // generated an event for it
3182 //default: for all the other keys leave processed as false so that
3183 // the tree control generates a TVN_KEYDOWN for us
3187 else if ( nMsg
== WM_COMMAND
)
3189 // if we receive a EN_KILLFOCUS command from the in-place edit control
3190 // used for label editing, make sure to end editing
3193 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
3195 if ( cmd
== EN_KILLFOCUS
)
3197 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
3207 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
3213 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
3215 if ( nMsg
== WM_CHAR
)
3217 // don't let the control process Space and Return keys because it
3218 // doesn't do anything useful with them anyhow but always beeps
3219 // annoyingly when it receives them and there is no way to turn it off
3220 // simply if you just process TREEITEM_ACTIVATED event to which Space
3221 // and Enter presses are mapped in your code
3222 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
3226 else if ( nMsg
== WM_KEYDOWN
)
3228 if ( wParam
== VK_ESCAPE
)
3232 m_dragImage
->EndDrag();
3233 wxDELETE(m_dragImage
);
3235 // if we don't do it, the tree seems to think that 2 items
3236 // are selected simultaneously which is quite weird
3237 TreeView_SelectDropTarget(GetHwnd(), 0);
3241 #endif // wxUSE_DRAGIMAGE
3243 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
3246 // process WM_NOTIFY Windows message
3247 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
3249 wxTreeEvent
event(wxEVT_NULL
, this);
3250 wxEventType eventType
= wxEVT_NULL
;
3251 NMHDR
*hdr
= (NMHDR
*)lParam
;
3253 switch ( hdr
->code
)
3256 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3259 case TVN_BEGINRDRAG
:
3261 if ( eventType
== wxEVT_NULL
)
3262 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
3263 //else: left drag, already set above
3265 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3267 event
.m_item
= tv
->itemNew
.hItem
;
3268 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
3270 // don't allow dragging by default: the user code must
3271 // explicitly say that it wants to allow it to avoid breaking
3277 case TVN_BEGINLABELEDIT
:
3279 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
3280 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3282 // although the user event handler may still veto it, it is
3283 // important to set it now so that calls to SetItemText() from
3284 // the event handler would change the text controls contents
3286 event
.m_item
= info
->item
.hItem
;
3287 event
.m_label
= info
->item
.pszText
;
3288 event
.m_editCancelled
= false;
3292 case TVN_DELETEITEM
:
3294 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
3295 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3297 event
.m_item
= tv
->itemOld
.hItem
;
3301 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
3302 if ( it
!= m_attrs
.end() )
3311 case TVN_ENDLABELEDIT
:
3313 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
3314 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3316 event
.m_item
= info
->item
.hItem
;
3317 event
.m_label
= info
->item
.pszText
;
3318 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
3323 // These *must* not be removed or TVN_GETINFOTIP will
3324 // not be processed each time the mouse is moved
3325 // and the tooltip will only ever update once.
3334 #ifdef TVN_GETINFOTIP
3335 case TVN_GETINFOTIP
:
3337 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
3338 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
3340 // Which item are we trying to get a tooltip for?
3341 event
.m_item
= info
->hItem
;
3345 #endif // TVN_GETINFOTIP
3346 #endif // !__WXWINCE__
3348 case TVN_GETDISPINFO
:
3349 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
3352 case TVN_SETDISPINFO
:
3354 if ( eventType
== wxEVT_NULL
)
3355 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
3356 //else: get, already set above
3358 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3360 event
.m_item
= info
->item
.hItem
;
3364 case TVN_ITEMEXPANDING
:
3365 case TVN_ITEMEXPANDED
:
3367 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3370 switch ( tv
->action
)
3373 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
3381 what
= IDX_COLLAPSE
;
3385 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
3388 eventType
= gs_expandEvents
[what
][how
];
3390 event
.m_item
= tv
->itemNew
.hItem
;
3396 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
3398 // fabricate the lParam and wParam parameters sufficiently
3399 // similar to the ones from a "real" WM_KEYDOWN so that
3400 // CreateKeyEvent() works correctly
3401 return MSWHandleTreeKeyDownEvent(
3402 info
->wVKey
, (wxIsAltDown() ? KF_ALTDOWN
: 0) << 16);
3406 // Vista's tree control has introduced some problems with our
3407 // multi-selection tree. When TreeView_SelectItem() is called,
3408 // the wrong items are deselected.
3410 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3411 // that can be used to regulate this incorrect behaviour. The
3412 // following messages will allow only the unlocked item's selection
3415 case TVN_ITEMCHANGINGA
:
3416 case TVN_ITEMCHANGINGW
:
3418 // we only need to handles these in multi-select trees
3419 if ( HasFlag(wxTR_MULTIPLE
) )
3421 // get info about the item about to be changed
3422 NMTVITEMCHANGE
* info
= (NMTVITEMCHANGE
*)lParam
;
3423 if (TreeItemUnlocker::IsLocked(info
->hItem
))
3425 // item's state is locked, don't allow the change
3426 // returning 1 will disallow the change
3432 // allow the state change
3436 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
3437 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3438 // we have to handle both messages:
3439 case TVN_SELCHANGEDA
:
3440 case TVN_SELCHANGEDW
:
3441 if ( !m_changingSelection
)
3443 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
3447 case TVN_SELCHANGINGA
:
3448 case TVN_SELCHANGINGW
:
3449 if ( !m_changingSelection
)
3451 if ( eventType
== wxEVT_NULL
)
3452 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
3453 //else: already set above
3455 if (hdr
->code
== TVN_SELCHANGINGW
||
3456 hdr
->code
== TVN_SELCHANGEDW
)
3458 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
3459 event
.m_item
= tv
->itemNew
.hItem
;
3460 event
.m_itemOld
= tv
->itemOld
.hItem
;
3464 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
3465 event
.m_item
= tv
->itemNew
.hItem
;
3466 event
.m_itemOld
= tv
->itemOld
.hItem
;
3470 // we receive this message from WM_LBUTTONDOWN handler inside
3471 // comctl32.dll and so before the click is passed to
3472 // DefWindowProc() which sets the focus to the window which was
3473 // clicked and this can lead to unexpected event sequences: for
3474 // example, we may get a "selection change" event from the tree
3475 // before getting a "kill focus" event for the text control which
3476 // had the focus previously, thus breaking user code doing input
3479 // to avoid such surprises, we force the generation of focus events
3480 // now, before we generate the selection change ones
3481 if ( !m_changingSelection
&& !m_isBeingDeleted
)
3485 // instead of explicitly checking for _WIN32_IE, check if the
3486 // required symbols are available in the headers
3487 #if defined(CDDS_PREPAINT)
3490 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
3491 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
3492 switch ( nmcd
.dwDrawStage
)
3495 // if we've got any items with non standard attributes,
3496 // notify us before painting each item
3497 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
3500 // windows in TreeCtrl use one-based index for item state images,
3501 // 0 indexed image is not being used, we're using zero-based index,
3502 // so we have to add temp image (of zero index) to state image list
3503 // before we draw any item, then after items are drawn we have to
3504 // delete it (in POSTPAINT notify)
3505 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3507 typedef BOOL (wxSTDCALL
*ImageList_Copy_t
)
3508 (HIMAGELIST
, int, HIMAGELIST
, int, UINT
);
3509 static ImageList_Copy_t s_pfnImageList_Copy
= NULL
;
3510 static bool loaded
= false;
3514 wxLoadedDLL
dllComCtl32(wxT("comctl32.dll"));
3515 if ( dllComCtl32
.IsLoaded() )
3517 wxDL_INIT_FUNC(s_pfn
, ImageList_Copy
, dllComCtl32
);
3522 if ( !s_pfnImageList_Copy
)
3524 // this code is broken with ImageList_Copy()
3525 // but I don't care enough about Win95 support
3526 // to write it now -- if anybody does, please
3528 wxFAIL_MSG("TODO: implement this for Win95");
3533 hImageList
= GetHimagelistOf(m_imageListState
);
3535 // add temporary image
3537 m_imageListState
->GetSize(0, width
, height
);
3539 HBITMAP hbmpTemp
= ::CreateBitmap(width
, height
, 1, 1, NULL
);
3540 int index
= ::ImageList_Add(hImageList
, hbmpTemp
, hbmpTemp
);
3541 ::DeleteObject(hbmpTemp
);
3545 // move images to right
3546 for ( int i
= index
; i
> 0; i
-- )
3548 (*s_pfnImageList_Copy
)(hImageList
, i
,
3553 // we must remove the image in POSTPAINT notify
3554 *result
|= CDRF_NOTIFYPOSTPAINT
;
3559 case CDDS_POSTPAINT
:
3560 // we are deleting temp image of 0 index, which was
3561 // added before items were drawn (in PREPAINT notify)
3562 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3563 m_imageListState
->Remove(0);
3566 case CDDS_ITEMPREPAINT
:
3568 wxMapTreeAttr::iterator
3569 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
3571 if ( it
== m_attrs
.end() )
3573 // nothing to do for this item
3574 *result
= CDRF_DODEFAULT
;
3578 wxTreeItemAttr
* const attr
= it
->second
;
3580 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
3581 TVIF_STATE
, TVIS_DROPHILITED
);
3583 const UINT tvItemState
= tvItem
.state
;
3585 // selection colours should override ours,
3586 // otherwise it is too confusing to the user
3587 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
3588 !(tvItemState
& TVIS_DROPHILITED
) )
3591 if ( attr
->HasBackgroundColour() )
3593 colBack
= attr
->GetBackgroundColour();
3594 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
3598 // but we still want to keep the special foreground
3599 // colour when we don't have focus (we can't keep
3600 // it when we do, it would usually be unreadable on
3601 // the almost inverted bg colour...)
3602 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
3603 FindFocus() != this ) &&
3604 !(tvItemState
& TVIS_DROPHILITED
) )
3607 if ( attr
->HasTextColour() )
3609 colText
= attr
->GetTextColour();
3610 lptvcd
->clrText
= wxColourToRGB(colText
);
3614 if ( attr
->HasFont() )
3616 HFONT hFont
= GetHfontOf(attr
->GetFont());
3618 ::SelectObject(nmcd
.hdc
, hFont
);
3620 *result
= CDRF_NEWFONT
;
3622 else // no specific font
3624 *result
= CDRF_DODEFAULT
;
3630 *result
= CDRF_DODEFAULT
;
3634 // we always process it
3636 #endif // have owner drawn support in headers
3640 DWORD pos
= GetMessagePos();
3642 point
.x
= LOWORD(pos
);
3643 point
.y
= HIWORD(pos
);
3644 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
3646 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), htFlags
);
3648 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3650 event
.m_item
= item
;
3651 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
3660 TV_HITTESTINFO tvhti
;
3661 wxGetCursorPosMSW(&tvhti
.pt
);
3662 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
3663 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
3665 if ( MSWIsOnItem(tvhti
.flags
) )
3667 event
.m_item
= tvhti
.hItem
;
3668 eventType
= (int)hdr
->code
== NM_DBLCLK
3669 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
3670 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
3672 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
3673 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
3682 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
3685 event
.SetEventType(eventType
);
3687 bool processed
= HandleTreeEvent(event
);
3690 switch ( hdr
->code
)
3693 // we translate NM_DBLCLK into ACTIVATED event and if the user
3694 // handled the activation of the item we shouldn't proceed with
3695 // also using the same double click for toggling the item expanded
3696 // state -- but OTOH do let the user to expand/collapse the item by
3697 // double clicking on it if the activation is not handled specially
3698 *result
= processed
;
3702 // prevent tree control from sending WM_CONTEXTMENU to our parent
3703 // (which it does if NM_RCLICK is not handled) because we want to
3704 // send it to the control itself
3708 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
3709 (WPARAM
)GetHwnd(), ::GetMessagePos());
3713 case TVN_BEGINRDRAG
:
3715 if ( event
.IsAllowed() )
3717 // normally this is impossible because the m_dragImage is
3718 // deleted once the drag operation is over
3719 wxASSERT_MSG( !m_dragImage
, wxT("starting to drag once again?") );
3721 m_dragImage
= new wxDragImage(*this, event
.m_item
);
3722 m_dragImage
->BeginDrag(wxPoint(0,0), this);
3723 m_dragImage
->Show();
3725 m_dragStarted
= true;
3727 #endif // wxUSE_DRAGIMAGE
3730 case TVN_DELETEITEM
:
3732 // NB: we might process this message using wxWidgets event
3733 // tables, but due to overhead of wxWin event system we
3734 // prefer to do it here ourself (otherwise deleting a tree
3735 // with many items is just too slow)
3736 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3738 wxTreeItemParam
*param
=
3739 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
3742 processed
= true; // Make sure we don't get called twice
3746 case TVN_BEGINLABELEDIT
:
3747 // return true to cancel label editing
3748 *result
= !event
.IsAllowed();
3750 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3751 if ( event
.IsAllowed() )
3753 HWND hText
= TreeView_GetEditControl(GetHwnd());
3756 // MBN: if m_textCtrl already has an HWND, it is a stale
3757 // pointer from a previous edit (because the user
3758 // didn't modify the label before dismissing the control,
3759 // and TVN_ENDLABELEDIT was not sent), so delete it
3760 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
3763 m_textCtrl
= new wxTextCtrl();
3764 m_textCtrl
->SetParent(this);
3765 m_textCtrl
->SetHWND((WXHWND
)hText
);
3766 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3768 // set wxTE_PROCESS_ENTER style for the text control to
3769 // force it to process the Enter presses itself, otherwise
3770 // they could be stolen from it by the dialog
3772 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3773 | wxTE_PROCESS_ENTER
);
3776 else // we had set m_idEdited before
3782 case TVN_ENDLABELEDIT
:
3783 // return true to set the label to the new string: note that we
3784 // also must pretend that we did process the message or it is going
3785 // to be passed to DefWindowProc() which will happily return false
3786 // cancelling the label change
3787 *result
= event
.IsAllowed();
3790 // ensure that we don't have the text ctrl which is going to be
3796 #ifdef TVN_GETINFOTIP
3797 case TVN_GETINFOTIP
:
3799 // If the user permitted a tooltip change, change it
3800 if (event
.IsAllowed())
3802 SetToolTip(event
.m_label
);
3809 case TVN_SELCHANGING
:
3810 case TVN_ITEMEXPANDING
:
3811 // return true to prevent the action from happening
3812 *result
= !event
.IsAllowed();
3815 case TVN_ITEMEXPANDED
:
3817 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3818 const wxTreeItemId
id(tv
->itemNew
.hItem
);
3820 if ( tv
->action
== TVE_COLLAPSE
)
3822 if ( wxApp::GetComCtl32Version() >= 600 )
3824 // for some reason the item selection rectangle depends
3825 // on whether it is expanded or collapsed (at least
3826 // with comctl32.dll v6): it is wider (by 3 pixels) in
3827 // the expanded state, so when the item collapses and
3828 // then is deselected the rightmost 3 pixels of the
3829 // previously drawn selection are left on the screen
3831 // it's not clear if it's a bug in comctl32.dll or in
3832 // our code (because it does not happen in Explorer but
3833 // OTOH we don't do anything which could result in this
3834 // AFAICS) but we do need to work around it to avoid
3841 // the item is also not refreshed properly after expansion when
3842 // it has an image depending on the expanded/collapsed state:
3843 // again, it's not clear if the bug is in comctl32.dll or our
3845 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3854 case TVN_GETDISPINFO
:
3855 // NB: so far the user can't set the image himself anyhow, so do it
3856 // anyway - but this may change later
3857 //if ( /* !processed && */ )
3859 wxTreeItemId item
= event
.m_item
;
3860 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3862 const wxTreeItemParam
* const param
= GetItemParam(item
);
3866 if ( info
->item
.mask
& TVIF_IMAGE
)
3871 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3872 : wxTreeItemIcon_Normal
3875 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3877 info
->item
.iSelectedImage
=
3880 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3881 : wxTreeItemIcon_Selected
3888 // for the other messages the return value is ignored and there is
3889 // nothing special to do
3894 // ----------------------------------------------------------------------------
3896 // ----------------------------------------------------------------------------
3898 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3899 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3901 int wxTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
3903 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
3905 // receive the desired information
3906 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3909 // state images are one-based
3910 return STATEIMAGEMASKTOINDEX(tvItem
.state
) - 1;
3913 void wxTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
3915 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
3917 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3919 // state images are one-based
3920 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3921 tvItem
.state
= INDEXTOSTATEIMAGEMASK(state
+ 1);
3926 // ----------------------------------------------------------------------------
3928 // ----------------------------------------------------------------------------
3930 // Using WM_SETREDRAW with the native control is a bad idea as it's broken in
3931 // some Windows versions (see http://support.microsoft.com/kb/130611) and
3932 // doesn't seem to do anything in other ones (e.g. under Windows 7 the tree
3933 // control keeps updating its scrollbars while the items are added to it,
3934 // resulting in horrible flicker when adding even a couple of dozen items).
3935 // So we hide it instead of freezing -- this still flickers, but actually not
3936 // as badly as it would if we didn't do it.
3938 void wxTreeCtrl::DoFreeze()
3940 // Notice that we don't call wxWindow::Hide() here as we want the window to
3941 // remain shown from wxWidgets point of view and also because
3942 // wxWindowMSW::Show() calls Do{Freeze,Thaw}() itself, so we'd get into
3943 // infinite recursion this way.
3945 ::ShowWindow(GetHwnd(), SW_HIDE
);
3948 void wxTreeCtrl::DoThaw()
3951 ::ShowWindow(GetHwnd(), SW_SHOW
);
3954 #endif // wxUSE_TREECTRL