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 #if wxUSE_EXTENDED_RTTI
605 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
607 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
608 // new style border flags, we put them first to
609 // use them for streaming out
610 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
611 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
612 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
613 wxFLAGS_MEMBER(wxBORDER_RAISED
)
614 wxFLAGS_MEMBER(wxBORDER_STATIC
)
615 wxFLAGS_MEMBER(wxBORDER_NONE
)
617 // old style border flags
618 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
619 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
620 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
621 wxFLAGS_MEMBER(wxRAISED_BORDER
)
622 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
623 wxFLAGS_MEMBER(wxBORDER
)
625 // standard window styles
626 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
627 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
628 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
629 wxFLAGS_MEMBER(wxWANTS_CHARS
)
630 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
631 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
632 wxFLAGS_MEMBER(wxVSCROLL
)
633 wxFLAGS_MEMBER(wxHSCROLL
)
635 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
636 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
637 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
638 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
639 wxFLAGS_MEMBER(wxTR_NO_LINES
)
640 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
641 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
642 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
643 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
644 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
645 wxFLAGS_MEMBER(wxTR_SINGLE
)
646 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
647 #if WXWIN_COMPATIBILITY_2_8
648 wxFLAGS_MEMBER(wxTR_EXTENDED
)
650 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
652 wxEND_FLAGS( wxTreeCtrlStyle
)
654 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
656 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
657 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
658 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
659 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
660 wxEND_PROPERTIES_TABLE()
662 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
663 wxEND_HANDLERS_TABLE()
665 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
667 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
670 // ----------------------------------------------------------------------------
672 // ----------------------------------------------------------------------------
674 // indices in gs_expandEvents table below
689 // handy table for sending events - it has to be initialized during run-time
690 // now so can't be const any more
691 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
694 but logically it's a const table with the following entries:
697 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
698 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
702 // ============================================================================
704 // ============================================================================
706 // ----------------------------------------------------------------------------
708 // ----------------------------------------------------------------------------
710 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
712 if ( !OnVisit(root
) )
715 return Traverse(root
, recursively
);
718 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
720 wxTreeItemIdValue cookie
;
721 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
722 while ( child
.IsOk() )
724 // depth first traversal
725 if ( recursively
&& !Traverse(child
, true) )
728 if ( !OnVisit(child
) )
731 child
= m_tree
->GetNextChild(root
, cookie
);
737 // ----------------------------------------------------------------------------
738 // construction and destruction
739 // ----------------------------------------------------------------------------
741 void wxTreeCtrl::Init()
744 m_hasAnyAttr
= false;
748 m_pVirtualRoot
= NULL
;
749 m_dragStarted
= false;
751 m_changingSelection
= false;
752 m_triggerStateImageClick
= false;
753 m_mouseUpDeselect
= false;
755 // initialize the global array of events now as it can't be done statically
756 // with the wxEVT_XXX values being allocated during run-time only
757 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
758 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
759 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
760 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
763 bool wxTreeCtrl::Create(wxWindow
*parent
,
768 const wxValidator
& validator
,
769 const wxString
& name
)
773 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
774 style
|= wxBORDER_SUNKEN
;
776 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
780 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
781 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
783 if ( !(m_windowStyle
& wxTR_NO_LINES
) )
784 wstyle
|= TVS_HASLINES
;
785 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
786 wstyle
|= TVS_HASBUTTONS
;
788 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
789 wstyle
|= TVS_EDITLABELS
;
791 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
792 wstyle
|= TVS_LINESATROOT
;
794 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
796 if ( wxApp::GetComCtl32Version() >= 471 )
797 wstyle
|= TVS_FULLROWSELECT
;
800 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
801 // Need so that TVN_GETINFOTIP messages will be sent
802 wstyle
|= TVS_INFOTIP
;
805 // Create the tree control.
806 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
809 #if wxUSE_COMCTL32_SAFELY
810 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
811 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
813 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
814 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
816 // This works around a bug in the Windows tree control whereby for some versions
817 // of comctrl32, setting any colour actually draws the background in black.
818 // This will initialise the background to the system colour.
819 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
820 // Assume the user has an updated comctl32.dll.
821 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
822 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
823 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
826 wxSetCCUnicodeFormat(GetHwnd());
831 wxTreeCtrl::~wxTreeCtrl()
833 // delete any attributes
836 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
838 // prevent TVN_DELETEITEM handler from deleting the attributes again!
839 m_hasAnyAttr
= false;
844 // delete user data to prevent memory leaks
845 // also deletes hidden root node storage.
849 // ----------------------------------------------------------------------------
851 // ----------------------------------------------------------------------------
853 /* static */ wxVisualAttributes
854 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
856 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
858 // common controls have their own default font
859 attrs
.font
= wxGetCCDefaultFont();
865 // simple wrappers which add error checking in debug mode
867 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
869 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
870 wxT("can't retrieve virtual root item") );
872 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
874 wxLogLastError(wxT("TreeView_GetItem"));
882 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
884 TreeItemUnlocker
unlocker(tvItem
->hItem
);
886 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
888 wxLogLastError(wxT("TreeView_SetItem"));
892 unsigned int wxTreeCtrl::GetCount() const
894 return (unsigned int)TreeView_GetCount(GetHwnd());
897 unsigned int wxTreeCtrl::GetIndent() const
899 return TreeView_GetIndent(GetHwnd());
902 void wxTreeCtrl::SetIndent(unsigned int indent
)
904 TreeView_SetIndent(GetHwnd(), indent
);
907 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
910 (void) TreeView_SetImageList(GetHwnd(),
911 imageList
? imageList
->GetHIMAGELIST() : 0,
915 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
917 if (m_ownsImageListNormal
)
918 delete m_imageListNormal
;
920 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
921 m_ownsImageListNormal
= false;
924 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
926 if (m_ownsImageListState
) delete m_imageListState
;
927 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
928 m_ownsImageListState
= false;
931 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
932 bool recursively
) const
934 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
936 TraverseCounter
counter(this, item
, recursively
);
937 return counter
.GetCount() - 1;
940 // ----------------------------------------------------------------------------
942 // ----------------------------------------------------------------------------
944 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
946 #if !wxUSE_COMCTL32_SAFELY
947 if ( !wxWindowBase::SetBackgroundColour(colour
) )
950 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
956 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
958 #if !wxUSE_COMCTL32_SAFELY
959 if ( !wxWindowBase::SetForegroundColour(colour
) )
962 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
968 // ----------------------------------------------------------------------------
970 // ----------------------------------------------------------------------------
972 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
974 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
977 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
979 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
981 wxChar buf
[512]; // the size is arbitrary...
983 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
984 tvItem
.pszText
= buf
;
985 tvItem
.cchTextMax
= WXSIZEOF(buf
);
986 if ( !DoGetItem(&tvItem
) )
988 // don't return some garbage which was on stack, but an empty string
992 return wxString(buf
);
995 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
997 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
999 if ( IS_VIRTUAL_ROOT(item
) )
1002 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
1003 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
1006 // when setting the text of the item being edited, the text control should
1007 // be updated to reflect the new text as well, otherwise calling
1008 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
1010 // don't use GetEditControl() here because m_textCtrl is not set yet
1011 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
1014 if ( item
== m_idEdited
)
1016 ::SetWindowText(hwndEdit
, text
.wx_str());
1021 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
1022 wxTreeItemIcon which
) const
1024 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1026 if ( IsHiddenRoot(item
) )
1028 // no images for hidden root item
1032 wxTreeItemParam
*param
= GetItemParam(item
);
1034 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
1037 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1038 wxTreeItemIcon which
)
1040 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1041 wxCHECK_RET( which
>= 0 &&
1042 which
< wxTreeItemIcon_Max
,
1043 wxT("invalid image index"));
1046 if ( IsHiddenRoot(item
) )
1048 // no images for hidden root item
1052 wxTreeItemParam
*data
= GetItemParam(item
);
1056 data
->SetImage(image
, which
);
1061 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
1063 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1065 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1067 // hidden root may still have data.
1068 if ( IS_VIRTUAL_ROOT(item
) )
1070 return GET_VIRTUAL_ROOT()->GetParam();
1074 if ( !DoGetItem(&tvItem
) )
1079 return (wxTreeItemParam
*)tvItem
.lParam
;
1082 bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent
& event
) const
1084 if ( event
.m_item
.IsOk() )
1086 event
.SetClientObject(GetItemData(event
.m_item
));
1089 return HandleWindowEvent(event
);
1092 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1094 wxTreeItemParam
*data
= GetItemParam(item
);
1096 return data
? data
->GetData() : NULL
;
1099 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1101 // first, associate this piece of data with this item
1107 wxTreeItemParam
*param
= GetItemParam(item
);
1109 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1111 param
->SetData(data
);
1114 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1116 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1118 if ( IS_VIRTUAL_ROOT(item
) )
1121 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1122 tvItem
.cChildren
= (int)has
;
1126 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1128 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1130 if ( IS_VIRTUAL_ROOT(item
) )
1133 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1134 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1138 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1140 if ( IS_VIRTUAL_ROOT(item
) )
1143 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1144 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1148 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1150 if ( IS_VIRTUAL_ROOT(item
) )
1154 if ( GetBoundingRect(item
, rect
) )
1160 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1162 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1164 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1165 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1168 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1170 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1172 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1173 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1176 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1178 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1180 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1181 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1184 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1185 const wxColour
& col
)
1187 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1189 wxTreeItemAttr
*attr
;
1190 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1191 if ( it
== m_attrs
.end() )
1193 m_hasAnyAttr
= true;
1195 m_attrs
[item
.m_pItem
] =
1196 attr
= new wxTreeItemAttr
;
1203 attr
->SetTextColour(col
);
1208 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1209 const wxColour
& col
)
1211 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1213 wxTreeItemAttr
*attr
;
1214 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1215 if ( it
== m_attrs
.end() )
1217 m_hasAnyAttr
= true;
1219 m_attrs
[item
.m_pItem
] =
1220 attr
= new wxTreeItemAttr
;
1222 else // already in the hash
1227 attr
->SetBackgroundColour(col
);
1232 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1234 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1236 wxTreeItemAttr
*attr
;
1237 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1238 if ( it
== m_attrs
.end() )
1240 m_hasAnyAttr
= true;
1242 m_attrs
[item
.m_pItem
] =
1243 attr
= new wxTreeItemAttr
;
1245 else // already in the hash
1250 attr
->SetFont(font
);
1252 // Reset the item's text to ensure that the bounding rect will be adjusted
1253 // for the new font.
1254 SetItemText(item
, GetItemText(item
));
1259 // ----------------------------------------------------------------------------
1261 // ----------------------------------------------------------------------------
1263 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1265 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1267 if ( item
== wxTreeItemId(TVI_ROOT
) )
1269 // virtual (hidden) root is never visible
1273 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1276 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1277 // the HTREEITEM with TVM_GETITEMRECT
1278 *(HTREEITEM
*)&rect
= HITEM(item
);
1280 // true means to get rect for just the text, not the whole line
1281 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1283 // if TVM_GETITEMRECT returned false, then the item is definitely not
1284 // visible (because its parent is not expanded)
1288 // however if it returned true, the item might still be outside the
1289 // currently visible part of the tree, test for it (notice that partly
1290 // visible means visible here)
1291 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1294 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1296 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1298 if ( IS_VIRTUAL_ROOT(item
) )
1300 wxTreeItemIdValue cookie
;
1301 return GetFirstChild(item
, cookie
).IsOk();
1304 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1307 return tvItem
.cChildren
!= 0;
1310 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1312 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1314 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1317 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1320 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1322 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1324 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1327 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1330 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1332 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1334 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1337 return (tvItem
.state
& TVIS_BOLD
) != 0;
1340 // ----------------------------------------------------------------------------
1342 // ----------------------------------------------------------------------------
1344 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1346 // Root may be real (visible) or virtual (hidden).
1347 if ( GET_VIRTUAL_ROOT() )
1350 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1353 wxTreeItemId
wxTreeCtrl::GetSelection() const
1355 wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE
), wxTreeItemId(),
1356 wxT("this only works with single selection controls") );
1358 return GetFocusedItem();
1361 wxTreeItemId
wxTreeCtrl::GetFocusedItem() const
1363 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1366 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1368 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1372 if ( IS_VIRTUAL_ROOT(item
) )
1374 // no parent for the virtual root
1379 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1380 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1382 // the top level items should have the virtual root as their parent
1387 return wxTreeItemId(hItem
);
1390 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1391 wxTreeItemIdValue
& cookie
) const
1393 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1395 // remember the last child returned in 'cookie'
1396 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1398 return wxTreeItemId(cookie
);
1401 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1402 wxTreeItemIdValue
& cookie
) const
1404 wxTreeItemId
fromCookie(cookie
);
1406 HTREEITEM hitem
= HITEM(fromCookie
);
1408 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1410 wxTreeItemId
item(hitem
);
1412 cookie
= item
.m_pItem
;
1417 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1419 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1421 // can this be done more efficiently?
1422 wxTreeItemIdValue cookie
;
1424 wxTreeItemId childLast
,
1425 child
= GetFirstChild(item
, cookie
);
1426 while ( child
.IsOk() )
1429 child
= GetNextChild(item
, cookie
);
1435 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1437 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1438 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1441 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1443 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1444 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1447 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1449 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1452 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1454 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1455 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1457 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1458 if ( next
.IsOk() && !IsVisible(next
) )
1460 // Win32 considers that any non-collapsed item is visible while we want
1461 // to return only really visible items
1468 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1470 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1471 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1473 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1474 if ( prev
.IsOk() && !IsVisible(prev
) )
1476 // just as above, Win32 function will happily return the previous item
1477 // in the tree for the first visible item too
1484 // ----------------------------------------------------------------------------
1485 // multiple selections emulation
1486 // ----------------------------------------------------------------------------
1488 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1490 TraverseSelections
selector(this, selections
);
1492 return selector
.GetCount();
1495 // ----------------------------------------------------------------------------
1497 // ----------------------------------------------------------------------------
1499 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1500 const wxTreeItemId
& hInsertAfter
,
1501 const wxString
& text
,
1502 int image
, int selectedImage
,
1503 wxTreeItemData
*data
)
1505 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1507 wxT("can't have more than one root in the tree") );
1509 TV_INSERTSTRUCT tvIns
;
1510 tvIns
.hParent
= HITEM(parent
);
1511 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1513 // this is how we insert the item as the first child: supply a NULL
1515 if ( !tvIns
.hInsertAfter
)
1517 tvIns
.hInsertAfter
= TVI_FIRST
;
1521 if ( !text
.empty() )
1524 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1528 tvIns
.item
.pszText
= NULL
;
1529 tvIns
.item
.cchTextMax
= 0;
1532 // create the param which will store the other item parameters
1533 wxTreeItemParam
*param
= new wxTreeItemParam
;
1535 // we return the images on demand as they depend on whether the item is
1536 // expanded or collapsed too in our case
1537 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1538 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1539 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1541 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1542 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1545 tvIns
.item
.lParam
= (LPARAM
)param
;
1546 tvIns
.item
.mask
= mask
;
1548 // don't use the hack below for the children of hidden root: this results
1549 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1550 const bool firstChild
= !IsHiddenRoot(parent
) &&
1551 !TreeView_GetChild(GetHwnd(), HITEM(parent
));
1553 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1556 wxLogLastError(wxT("TreeView_InsertItem"));
1559 // apparently some Windows versions (2000 and XP are reported to do this)
1560 // sometimes don't refresh the tree after adding the first child and so we
1561 // need this to make the "[+]" appear
1565 TreeView_GetItemRect(GetHwnd(), HITEM(parent
), &rect
, FALSE
);
1566 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1569 // associate the application tree item with Win32 tree item handle
1572 // setup wxTreeItemData
1575 param
->SetData(data
);
1579 return wxTreeItemId(id
);
1582 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1583 int image
, int selectedImage
,
1584 wxTreeItemData
*data
)
1586 if ( HasFlag(wxTR_HIDE_ROOT
) )
1588 wxASSERT_MSG( !m_pVirtualRoot
, wxT("tree can have only a single root") );
1590 // create a virtual root item, the parent for all the others
1591 wxTreeItemParam
*param
= new wxTreeItemParam
;
1592 param
->SetData(data
);
1594 m_pVirtualRoot
= new wxVirtualNode(param
);
1599 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1600 text
, image
, selectedImage
, data
);
1603 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1605 const wxString
& text
,
1606 int image
, int selectedImage
,
1607 wxTreeItemData
*data
)
1609 wxTreeItemId idPrev
;
1610 if ( index
== (size_t)-1 )
1612 // special value: append to the end
1615 else // find the item from index
1617 wxTreeItemIdValue cookie
;
1618 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1619 while ( index
!= 0 && idCur
.IsOk() )
1624 idCur
= GetNextChild(parent
, cookie
);
1627 // assert, not check: if the index is invalid, we will append the item
1629 wxASSERT_MSG( index
== 0, wxT("bad index in wxTreeCtrl::InsertItem") );
1632 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1635 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1637 // unlock tree selections on vista, without this the
1638 // tree ctrl will eventually crash after item deletion
1639 TreeItemUnlocker unlock_all
;
1641 if ( HasFlag(wxTR_MULTIPLE
) )
1643 bool selected
= IsSelected(item
);
1648 next
= TreeView_GetNextVisible(GetHwnd(), HITEM(item
));
1652 next
= TreeView_GetPrevVisible(GetHwnd(), HITEM(item
));
1657 TempSetter
set(m_changingSelection
);
1658 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1660 wxLogLastError(wxT("TreeView_DeleteItem"));
1670 if ( item
== m_htSelStart
)
1671 m_htSelStart
.Unset();
1673 if ( item
== m_htClickedItem
)
1674 m_htClickedItem
.Unset();
1678 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
1680 if ( IsTreeEventAllowed(changingEvent
) )
1682 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
1683 (void)HandleTreeEvent(changedEvent
);
1687 DoUnselectItem(next
);
1694 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1696 wxLogLastError(wxT("TreeView_DeleteItem"));
1701 // delete all children (but don't delete the item itself)
1702 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1704 // unlock tree selections on vista for the duration of this call
1705 TreeItemUnlocker unlock_all
;
1707 wxTreeItemIdValue cookie
;
1709 wxArrayTreeItemIds children
;
1710 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1711 while ( child
.IsOk() )
1713 children
.Add(child
);
1715 child
= GetNextChild(item
, cookie
);
1718 size_t nCount
= children
.Count();
1719 for ( size_t n
= 0; n
< nCount
; n
++ )
1721 Delete(children
[n
]);
1725 void wxTreeCtrl::DeleteAllItems()
1727 // unlock tree selections on vista for the duration of this call
1728 TreeItemUnlocker unlock_all
;
1730 // invalidate all the items we store as they're going to become invalid
1732 m_htClickedItem
= wxTreeItemId();
1734 // delete the "virtual" root item.
1735 if ( GET_VIRTUAL_ROOT() )
1737 delete GET_VIRTUAL_ROOT();
1738 m_pVirtualRoot
= NULL
;
1741 // and all the real items
1743 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1745 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1749 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1751 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1752 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1753 flag
== TVE_EXPAND
||
1755 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1757 // A hidden root can be neither expanded nor collapsed.
1758 wxCHECK_RET( !IsHiddenRoot(item
),
1759 wxT("Can't expand/collapse hidden root node!") );
1761 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1762 // emulate them. This behaviour has changed slightly with comctl32.dll
1763 // v 4.70 - now it does send them but only the first time. To maintain
1764 // compatible behaviour and also in order to not have surprises with the
1765 // future versions, don't rely on this and still do everything ourselves.
1766 // To avoid that the messages be sent twice when the item is expanded for
1767 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1769 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1773 if ( IsExpanded(item
) )
1775 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
,
1776 this, wxTreeItemId(item
));
1778 if ( !IsTreeEventAllowed(event
) )
1782 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) )
1784 if ( IsExpanded(item
) )
1787 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, this, item
);
1788 (void)HandleTreeEvent(event
);
1790 //else: change didn't took place, so do nothing at all
1793 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1795 DoExpand(item
, TVE_EXPAND
);
1798 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1800 DoExpand(item
, TVE_COLLAPSE
);
1803 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1805 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1808 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1810 DoExpand(item
, TVE_TOGGLE
);
1813 void wxTreeCtrl::Unselect()
1815 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE
),
1816 wxT("doesn't make sense, may be you want UnselectAll()?") );
1818 // the current focus
1819 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1826 if ( HasFlag(wxTR_MULTIPLE
) )
1828 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
1829 this, wxTreeItemId());
1830 changingEvent
.m_itemOld
= htFocus
;
1832 if ( IsTreeEventAllowed(changingEvent
) )
1836 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1837 this, wxTreeItemId());
1838 changedEvent
.m_itemOld
= htFocus
;
1839 (void)HandleTreeEvent(changedEvent
);
1848 void wxTreeCtrl::DoUnselectAll()
1850 wxArrayTreeItemIds selections
;
1851 size_t count
= GetSelections(selections
);
1853 for ( size_t n
= 0; n
< count
; n
++ )
1855 DoUnselectItem(selections
[n
]);
1858 m_htSelStart
.Unset();
1861 void wxTreeCtrl::UnselectAll()
1863 if ( HasFlag(wxTR_MULTIPLE
) )
1865 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1866 if ( !htFocus
) return;
1868 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1869 changingEvent
.m_itemOld
= htFocus
;
1871 if ( IsTreeEventAllowed(changingEvent
) )
1875 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1876 changedEvent
.m_itemOld
= htFocus
;
1877 (void)HandleTreeEvent(changedEvent
);
1886 void wxTreeCtrl::DoSelectChildren(const wxTreeItemId
& parent
)
1890 wxTreeItemIdValue cookie
;
1891 wxTreeItemId child
= GetFirstChild(parent
, cookie
);
1892 while ( child
.IsOk() )
1894 DoSelectItem(child
, true);
1895 child
= GetNextChild(child
, cookie
);
1899 void wxTreeCtrl::SelectChildren(const wxTreeItemId
& parent
)
1901 wxCHECK_RET( HasFlag(wxTR_MULTIPLE
),
1902 "this only works with multiple selection controls" );
1904 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1906 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1907 changingEvent
.m_itemOld
= htFocus
;
1909 if ( IsTreeEventAllowed(changingEvent
) )
1911 DoSelectChildren(parent
);
1913 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1914 changedEvent
.m_itemOld
= htFocus
;
1915 (void)HandleTreeEvent(changedEvent
);
1919 void wxTreeCtrl::DoSelectItem(const wxTreeItemId
& item
, bool select
)
1921 TempSetter
set(m_changingSelection
);
1923 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1926 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1928 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't select hidden root item") );
1930 if ( select
== IsSelected(item
) )
1932 // nothing to do, the item is already in the requested state
1936 if ( HasFlag(wxTR_MULTIPLE
) )
1938 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1940 if ( IsTreeEventAllowed(changingEvent
) )
1942 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1943 DoSelectItem(item
, select
);
1947 SetFocusedItem(item
);
1950 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1952 (void)HandleTreeEvent(changedEvent
);
1955 else // single selection
1957 wxTreeItemId itemOld
, itemNew
;
1960 itemOld
= GetSelection();
1963 else // deselecting the currently selected item
1966 // leave itemNew invalid
1969 // Recent versions of comctl32.dll send TVN_SELCHANG{ED,ING} events
1970 // when we call TreeView_SelectItem() but apparently some old ones did
1971 // not so send the events ourselves and ignore those generated by
1972 // TreeView_SelectItem() if m_changingSelection is set.
1974 changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, itemNew
);
1975 changingEvent
.SetOldItem(itemOld
);
1977 if ( IsTreeEventAllowed(changingEvent
) )
1979 TempSetter
set(m_changingSelection
);
1981 if ( !TreeView_SelectItem(GetHwnd(), HITEM(itemNew
)) )
1983 wxLogLastError(wxT("TreeView_SelectItem"));
1987 ::SetFocus(GetHwnd(), HITEM(item
));
1989 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1991 changedEvent
.SetOldItem(itemOld
);
1992 (void)HandleTreeEvent(changedEvent
);
1995 //else: program vetoed the change
1999 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
2001 wxCHECK_RET( !IsHiddenRoot(item
), wxT("can't show hidden root item") );
2004 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
2007 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
2009 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
2011 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
2015 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
2020 void wxTreeCtrl::DeleteTextCtrl()
2024 // the HWND corresponding to this control is deleted by the tree
2025 // control itself and we don't know when exactly this happens, so check
2026 // if the window still exists before calling UnsubclassWin()
2027 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
2029 m_textCtrl
->SetHWND(0);
2032 m_textCtrl
->UnsubclassWin();
2033 m_textCtrl
->SetHWND(0);
2034 wxDELETE(m_textCtrl
);
2040 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2041 wxClassInfo
*textControlClass
)
2043 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
2048 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
2049 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
2051 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2055 wxDELETE(m_textCtrl
);
2059 // textctrl is subclassed in MSWOnNotify
2063 // End label editing, optionally cancelling the edit
2064 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
2066 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
2071 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2073 TV_HITTESTINFO hitTestInfo
;
2074 hitTestInfo
.pt
.x
= (int)point
.x
;
2075 hitTestInfo
.pt
.y
= (int)point
.y
;
2077 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2082 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2083 flags |= wxTREE_HITTEST_##flag
2085 TRANSLATE_FLAG(ABOVE
);
2086 TRANSLATE_FLAG(BELOW
);
2087 TRANSLATE_FLAG(NOWHERE
);
2088 TRANSLATE_FLAG(ONITEMBUTTON
);
2089 TRANSLATE_FLAG(ONITEMICON
);
2090 TRANSLATE_FLAG(ONITEMINDENT
);
2091 TRANSLATE_FLAG(ONITEMLABEL
);
2092 TRANSLATE_FLAG(ONITEMRIGHT
);
2093 TRANSLATE_FLAG(ONITEMSTATEICON
);
2094 TRANSLATE_FLAG(TOLEFT
);
2095 TRANSLATE_FLAG(TORIGHT
);
2097 #undef TRANSLATE_FLAG
2099 return wxTreeItemId(hitTestInfo
.hItem
);
2102 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2104 bool textOnly
) const
2108 // Virtual root items have no bounding rectangle
2109 if ( IS_VIRTUAL_ROOT(item
) )
2114 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2117 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2123 // couldn't retrieve rect: for example, item isn't visible
2128 void wxTreeCtrl::ClearFocusedItem()
2130 TempSetter
set(m_changingSelection
);
2132 if ( !TreeView_SelectItem(GetHwnd(), 0) )
2134 wxLogLastError(wxT("TreeView_SelectItem"));
2138 void wxTreeCtrl::SetFocusedItem(const wxTreeItemId
& item
)
2140 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2142 TempSetter
set(m_changingSelection
);
2144 ::SetFocus(GetHwnd(), HITEM(item
));
2147 void wxTreeCtrl::DoUnselectItem(const wxTreeItemId
& item
)
2149 TempSetter
set(m_changingSelection
);
2151 ::UnselectItem(GetHwnd(), HITEM(item
));
2154 void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId
& item
)
2156 TempSetter
set(m_changingSelection
);
2158 ::ToggleItemSelection(GetHwnd(), HITEM(item
));
2161 // ----------------------------------------------------------------------------
2163 // ----------------------------------------------------------------------------
2165 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2166 // functions such as IsDataIndirect()
2167 class wxTreeSortHelper
2170 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2173 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
2175 return ((wxTreeItemParam
*)lParam
)->GetItem();
2179 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2183 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2184 wxT("sorting tree without data doesn't make sense") );
2186 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2188 return tree
->OnCompareItems(GetIdFromData(pItem1
),
2189 GetIdFromData(pItem2
));
2192 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2194 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2196 // rely on the fact that TreeView_SortChildren does the same thing as our
2197 // default behaviour, i.e. sorts items alphabetically and so call it
2198 // directly if we're not in derived class (much more efficient!)
2199 // RN: Note that if you find you're code doesn't sort as expected this
2200 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
2201 // combo for your derived wxTreeCtrl if will sort without
2203 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2205 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2210 tvSort
.hParent
= HITEM(item
);
2211 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2212 tvSort
.lParam
= (LPARAM
)this;
2213 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2217 // ----------------------------------------------------------------------------
2219 // ----------------------------------------------------------------------------
2221 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
2223 if ( msg
->message
== WM_KEYDOWN
)
2225 // Only eat VK_RETURN if not being used by the application in
2226 // conjunction with modifiers
2227 if ( (msg
->wParam
== VK_RETURN
) && !wxIsAnyModifierDown() )
2229 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2234 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
2237 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
2239 const int id
= (signed short)id_
;
2241 if ( cmd
== EN_UPDATE
)
2243 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2244 event
.SetEventObject( this );
2245 ProcessCommand(event
);
2247 else if ( cmd
== EN_KILLFOCUS
)
2249 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2250 event
.SetEventObject( this );
2251 ProcessCommand(event
);
2259 // command processed
2263 bool wxTreeCtrl::MSWIsOnItem(unsigned flags
) const
2265 unsigned mask
= TVHT_ONITEM
;
2266 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2267 mask
|= TVHT_ONITEMINDENT
| TVHT_ONITEMRIGHT
;
2269 return (flags
& mask
) != 0;
2272 bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey
)
2274 const bool bCtrl
= wxIsCtrlDown();
2275 const bool bShift
= wxIsShiftDown();
2276 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2285 if ( vkey
!= VK_RETURN
&& bCtrl
)
2287 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2289 changingEvent
.m_itemOld
= htSel
;
2291 if ( IsTreeEventAllowed(changingEvent
) )
2293 DoToggleItemSelection(wxTreeItemId(htSel
));
2295 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2297 changedEvent
.m_itemOld
= htSel
;
2298 (void)HandleTreeEvent(changedEvent
);
2303 wxArrayTreeItemIds selections
;
2304 size_t count
= GetSelections(selections
);
2306 if ( count
!= 1 || HITEM(selections
[0]) != htSel
)
2308 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2310 changingEvent
.m_itemOld
= htSel
;
2312 if ( IsTreeEventAllowed(changingEvent
) )
2315 DoSelectItem(wxTreeItemId(htSel
));
2317 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2319 changedEvent
.m_itemOld
= htSel
;
2320 (void)HandleTreeEvent(changedEvent
);
2328 if ( !bCtrl
&& !bShift
)
2330 wxArrayTreeItemIds selections
;
2335 next
= vkey
== VK_UP
2336 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2337 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2341 next
= GetRootItem();
2343 if ( IsHiddenRoot(next
) )
2344 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2352 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2354 changingEvent
.m_itemOld
= htSel
;
2356 if ( IsTreeEventAllowed(changingEvent
) )
2360 SetFocusedItem(next
);
2362 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2364 changedEvent
.m_itemOld
= htSel
;
2365 (void)HandleTreeEvent(changedEvent
);
2370 wxTreeItemId next
= vkey
== VK_UP
2371 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2372 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2379 if ( !m_htSelStart
)
2381 m_htSelStart
= htSel
;
2384 if ( bShift
&& SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2385 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2387 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2388 changingEvent
.m_itemOld
= htSel
;
2390 if ( IsTreeEventAllowed(changingEvent
) )
2392 SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2393 SR_UNSELECT_OTHERS
);
2395 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2396 changedEvent
.m_itemOld
= htSel
;
2397 (void)HandleTreeEvent(changedEvent
);
2401 SetFocusedItem(next
);
2406 if ( HasChildren(htSel
) && IsExpanded(htSel
) )
2412 wxTreeItemId next
= GetItemParent(htSel
);
2414 if ( next
.IsOk() && !IsHiddenRoot(next
) )
2416 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2418 changingEvent
.m_itemOld
= htSel
;
2420 if ( IsTreeEventAllowed(changingEvent
) )
2424 SetFocusedItem(next
);
2426 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2428 changedEvent
.m_itemOld
= htSel
;
2429 (void)HandleTreeEvent(changedEvent
);
2436 if ( !IsVisible(htSel
) )
2438 EnsureVisible(htSel
);
2441 if ( !HasChildren(htSel
) )
2444 if ( !IsExpanded(htSel
) )
2450 wxTreeItemId next
= TreeView_GetChild(GetHwnd(), htSel
);
2452 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2453 changingEvent
.m_itemOld
= htSel
;
2455 if ( IsTreeEventAllowed(changingEvent
) )
2459 SetFocusedItem(next
);
2461 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2462 changedEvent
.m_itemOld
= htSel
;
2463 (void)HandleTreeEvent(changedEvent
);
2471 wxTreeItemId next
= GetRootItem();
2473 if ( IsHiddenRoot(next
) )
2475 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2481 if ( vkey
== VK_END
)
2485 wxTreeItemId nextTemp
= TreeView_GetNextVisible(
2486 GetHwnd(), HITEM(next
));
2488 if ( !nextTemp
.IsOk() )
2495 if ( htSel
== HITEM(next
) )
2500 if ( !m_htSelStart
)
2502 m_htSelStart
= htSel
;
2505 if ( SelectRange(GetHwnd(),
2506 HITEM(m_htSelStart
), HITEM(next
),
2507 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2509 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2511 changingEvent
.m_itemOld
= htSel
;
2513 if ( IsTreeEventAllowed(changingEvent
) )
2515 SelectRange(GetHwnd(),
2516 HITEM(m_htSelStart
), HITEM(next
),
2517 SR_UNSELECT_OTHERS
);
2518 SetFocusedItem(next
);
2520 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2522 changedEvent
.m_itemOld
= htSel
;
2523 (void)HandleTreeEvent(changedEvent
);
2529 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2531 changingEvent
.m_itemOld
= htSel
;
2533 if ( IsTreeEventAllowed(changingEvent
) )
2537 SetFocusedItem(next
);
2539 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2541 changedEvent
.m_itemOld
= htSel
;
2542 (void)HandleTreeEvent(changedEvent
);
2552 wxTreeItemId firstVisible
= GetFirstVisibleItem();
2553 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2554 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2555 TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible
)) :
2556 TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible
));
2558 if ( !nextAdjacent
)
2563 wxTreeItemId nextStart
= firstVisible
;
2565 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2567 wxTreeItemId nextTemp
= (vkey
== VK_PRIOR
) ?
2568 TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart
)) :
2569 TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart
));
2571 if ( nextTemp
.IsOk() )
2573 nextStart
= nextTemp
;
2581 EnsureVisible(nextStart
);
2583 if ( vkey
== VK_NEXT
)
2585 wxTreeItemId nextEnd
= nextStart
;
2587 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2589 wxTreeItemId nextTemp
=
2590 TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd
));
2592 if ( nextTemp
.IsOk() )
2602 EnsureVisible(nextEnd
);
2607 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2608 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2609 TreeView_GetPrevVisible(GetHwnd(), htSel
) :
2610 TreeView_GetNextVisible(GetHwnd(), htSel
);
2612 if ( !nextAdjacent
)
2617 wxTreeItemId
next(htSel
);
2619 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2621 wxTreeItemId nextTemp
= vkey
== VK_PRIOR
?
2622 TreeView_GetPrevVisible(GetHwnd(), HITEM(next
)) :
2623 TreeView_GetNextVisible(GetHwnd(), HITEM(next
));
2625 if ( !nextTemp
.IsOk() )
2631 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2633 changingEvent
.m_itemOld
= htSel
;
2635 if ( IsTreeEventAllowed(changingEvent
) )
2638 m_htSelStart
.Unset();
2640 SetFocusedItem(next
);
2642 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2644 changedEvent
.m_itemOld
= htSel
;
2645 (void)HandleTreeEvent(changedEvent
);
2657 bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam
, WXLPARAM lParam
)
2659 wxTreeEvent
keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2660 keyEvent
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
, wParam
, lParam
);
2662 bool processed
= HandleTreeEvent(keyEvent
);
2664 // generate a separate event for Space/Return
2665 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
2666 ((wParam
== VK_SPACE
) || (wParam
== VK_RETURN
)) )
2668 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2671 wxTreeEvent
activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2673 (void)HandleTreeEvent(activatedEvent
);
2680 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2681 // only do it during dragging, minimize wxWin overhead (this is important for
2682 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2683 // instead of passing by wxWin events
2685 wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2687 bool processed
= false;
2689 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2691 if ( nMsg
== WM_CONTEXTMENU
)
2693 int x
= GET_X_LPARAM(lParam
),
2694 y
= GET_Y_LPARAM(lParam
);
2696 // the item for which the menu should be shown
2699 // the position where the menu should be shown in client coordinates
2700 // (so that it can be passed directly to PopupMenu())
2703 if ( x
== -1 || y
== -1 )
2705 // this means that the event was generated from keyboard (e.g. with
2706 // Shift-F10 or special Windows menu key)
2708 // use the Explorer standard of putting the menu at the left edge
2709 // of the text, in the vertical middle of the text
2710 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2713 // Use the bounding rectangle of only the text part
2715 GetBoundingRect(item
, rect
, true);
2716 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
2719 else // event from mouse, use mouse position
2721 pt
= ScreenToClient(wxPoint(x
, y
));
2723 TV_HITTESTINFO tvhti
;
2727 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2728 item
= wxTreeItemId(tvhti
.hItem
);
2734 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2736 event
.m_pointDrag
= pt
;
2738 if ( HandleTreeEvent(event
) )
2740 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2743 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2745 // we only process mouse messages here and these parameters have the
2746 // same meaning for all of them
2747 int x
= GET_X_LPARAM(lParam
),
2748 y
= GET_Y_LPARAM(lParam
);
2750 TV_HITTESTINFO tvht
;
2754 HTREEITEM htOldItem
= TreeView_GetSelection(GetHwnd());
2755 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2759 case WM_LBUTTONDOWN
:
2763 m_htClickedItem
.Unset();
2765 if ( !MSWIsOnItem(tvht
.flags
) )
2767 if ( tvht
.flags
& TVHT_ONITEMBUTTON
)
2769 // either it's going to be handled by user code or
2770 // we're going to use it ourselves to toggle the
2771 // branch, in either case don't pass it to the base
2772 // class which would generate another mouse click event
2773 // for it even though it's already handled here
2777 if ( !HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2779 if ( !IsExpanded(htItem
) )
2790 m_focusLost
= false;
2796 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2797 m_ptClick
= wxPoint(x
, y
);
2799 if ( wParam
& MK_CONTROL
)
2801 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2803 m_htClickedItem
.Unset();
2807 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2809 changingEvent
.m_itemOld
= htOldItem
;
2811 if ( IsTreeEventAllowed(changingEvent
) )
2813 // toggle selected state
2814 DoToggleItemSelection(wxTreeItemId(htItem
));
2816 SetFocusedItem(wxTreeItemId(htItem
));
2818 // reset on any click without Shift
2819 m_htSelStart
.Unset();
2821 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2823 changedEvent
.m_itemOld
= htOldItem
;
2824 (void)HandleTreeEvent(changedEvent
);
2827 else if ( wParam
& MK_SHIFT
)
2829 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2831 m_htClickedItem
.Unset();
2836 bool willChange
= true;
2838 if ( !(wParam
& MK_CONTROL
) )
2840 srFlags
|= SR_UNSELECT_OTHERS
;
2843 if ( !m_htSelStart
)
2845 // take the focused item
2846 m_htSelStart
= htOldItem
;
2850 willChange
= SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2851 htItem
, srFlags
| SR_SIMULATE
);
2856 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2858 changingEvent
.m_itemOld
= htOldItem
;
2860 if ( IsTreeEventAllowed(changingEvent
) )
2862 // this selects all items between the starting one
2866 SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2871 DoSelectItem(wxTreeItemId(htItem
));
2874 SetFocusedItem(wxTreeItemId(htItem
));
2876 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2878 changedEvent
.m_itemOld
= htOldItem
;
2879 (void)HandleTreeEvent(changedEvent
);
2883 else // normal click
2885 // avoid doing anything if we click on the only
2886 // currently selected item
2888 wxArrayTreeItemIds selections
;
2889 size_t count
= GetSelections(selections
);
2893 HITEM(selections
[0]) != htItem
)
2895 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2897 m_htClickedItem
.Unset();
2901 // clear the previously selected items, if the user
2902 // clicked outside of the present selection, otherwise,
2903 // perform the deselection on mouse-up, this allows
2904 // multiple drag and drop to work.
2905 if ( !IsItemSelected(GetHwnd(), htItem
))
2907 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2909 changingEvent
.m_itemOld
= htOldItem
;
2911 if ( IsTreeEventAllowed(changingEvent
) )
2914 DoSelectItem(wxTreeItemId(htItem
));
2915 SetFocusedItem(wxTreeItemId(htItem
));
2917 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2919 changedEvent
.m_itemOld
= htOldItem
;
2920 (void)HandleTreeEvent(changedEvent
);
2925 SetFocusedItem(wxTreeItemId(htItem
));
2926 m_mouseUpDeselect
= true;
2929 else // click on a single selected item
2931 // don't interfere with the default processing in
2932 // WM_MOUSEMOVE handler below as the default window
2933 // proc will start the drag itself if we let have
2935 m_htClickedItem
.Unset();
2937 // prevent in-place editing from starting if focus lost
2938 // since previous click
2942 DoSelectItem(wxTreeItemId(htItem
));
2943 SetFocusedItem(wxTreeItemId(htItem
));
2951 // reset on any click without Shift
2952 m_htSelStart
.Unset();
2955 m_focusLost
= false;
2957 // we consumed the event so we need to trigger state image
2962 wxTreeItemId item
= HitTest(wxPoint(x
, y
), htFlags
);
2964 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2966 m_triggerStateImageClick
= true;
2971 case WM_RBUTTONDOWN
:
2978 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) || !htItem
)
2983 // default handler removes the highlight from the currently
2984 // focused item when right mouse button is pressed on another
2985 // one but keeps the remaining items highlighted, which is
2986 // confusing, so override this default behaviour
2987 if ( !IsItemSelected(GetHwnd(), htItem
) )
2989 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2991 changingEvent
.m_itemOld
= htOldItem
;
2993 if ( IsTreeEventAllowed(changingEvent
) )
2996 DoSelectItem(wxTreeItemId(htItem
));
2997 SetFocusedItem(wxTreeItemId(htItem
));
2999 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
3001 changedEvent
.m_itemOld
= htOldItem
;
3002 (void)HandleTreeEvent(changedEvent
);
3010 if ( m_htClickedItem
)
3012 int cx
= abs(m_ptClick
.x
- x
);
3013 int cy
= abs(m_ptClick
.y
- y
);
3015 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
3016 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
3021 tv
.hdr
.hwndFrom
= GetHwnd();
3022 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
3023 tv
.hdr
.code
= TVN_BEGINDRAG
;
3025 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
3029 wxZeroMemory(tviAux
);
3031 tviAux
.hItem
= HITEM(m_htClickedItem
);
3032 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
3033 tviAux
.stateMask
= 0xffffffff;
3034 TreeView_GetItem(GetHwnd(), &tviAux
);
3036 tv
.itemNew
.state
= tviAux
.state
;
3037 tv
.itemNew
.lParam
= tviAux
.lParam
;
3042 // do it before SendMessage() call below to avoid
3043 // reentrancies here if there is another WM_MOUSEMOVE
3044 // in the queue already
3045 m_htClickedItem
.Unset();
3047 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
3048 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
3050 // don't pass it to the default window proc, it would
3051 // start dragging again
3055 #endif // __WXWINCE__
3060 m_dragImage
->Move(wxPoint(x
, y
));
3063 // highlight the item as target (hiding drag image is
3064 // necessary - otherwise the display will be corrupted)
3065 m_dragImage
->Hide();
3066 TreeView_SelectDropTarget(GetHwnd(), htItem
);
3067 m_dragImage
->Show();
3070 #endif // wxUSE_DRAGIMAGE
3076 // deselect other items if needed
3079 if ( m_mouseUpDeselect
)
3081 m_mouseUpDeselect
= false;
3083 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
3085 changingEvent
.m_itemOld
= htOldItem
;
3087 if ( IsTreeEventAllowed(changingEvent
) )
3090 DoSelectItem(wxTreeItemId(htItem
));
3091 SetFocusedItem(wxTreeItemId(htItem
));
3093 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
3095 changedEvent
.m_itemOld
= htOldItem
;
3096 (void)HandleTreeEvent(changedEvent
);
3101 m_htClickedItem
.Unset();
3103 if ( m_triggerStateImageClick
)
3105 if ( tvht
.flags
& TVHT_ONITEMSTATEICON
)
3107 wxTreeEvent
event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
,
3109 (void)HandleTreeEvent(event
);
3111 m_triggerStateImageClick
= false;
3116 if ( !m_dragStarted
&& MSWIsOnItem(tvht
.flags
) )
3128 m_dragImage
->EndDrag();
3129 wxDELETE(m_dragImage
);
3131 // generate the drag end event
3132 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
,
3134 event
.m_pointDrag
= wxPoint(x
, y
);
3135 (void)HandleTreeEvent(event
);
3137 // if we don't do it, the tree seems to think that 2 items
3138 // are selected simultaneously which is quite weird
3139 TreeView_SelectDropTarget(GetHwnd(), 0);
3141 #endif // wxUSE_DRAGIMAGE
3143 if ( isMultiple
&& nMsg
== WM_RBUTTONUP
)
3147 nmhdr
.hwndFrom
= GetHwnd();
3148 nmhdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
3149 nmhdr
.code
= NM_RCLICK
;
3150 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY
,
3151 nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
3155 m_dragStarted
= false;
3160 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) )
3164 // the tree control greys out the selected item when it loses focus
3165 // and paints it as selected again when it regains it, but it won't
3166 // do it for the other items itself - help it
3167 wxArrayTreeItemIds selections
;
3168 size_t count
= GetSelections(selections
);
3171 for ( size_t n
= 0; n
< count
; n
++ )
3173 // TreeView_GetItemRect() will return false if item is not
3174 // visible, which may happen perfectly well
3175 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
3178 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
3183 if ( nMsg
== WM_KILLFOCUS
)
3188 else if ( (nMsg
== WM_KEYDOWN
|| nMsg
== WM_SYSKEYDOWN
) && isMultiple
)
3190 // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
3191 // notification but for the keys which can be used to change selection
3192 // we need to do it from here so as to not apply the default behaviour
3193 // if the events are handled by the user code
3206 if ( !HandleKeyDown(wParam
, lParam
) &&
3207 !MSWHandleTreeKeyDownEvent(wParam
, lParam
) )
3209 // use the key to update the selection if it was left
3211 MSWHandleSelectionKey(wParam
);
3214 // pretend that we did process it in any case as we already
3215 // generated an event for it
3218 //default: for all the other keys leave processed as false so that
3219 // the tree control generates a TVN_KEYDOWN for us
3223 else if ( nMsg
== WM_COMMAND
)
3225 // if we receive a EN_KILLFOCUS command from the in-place edit control
3226 // used for label editing, make sure to end editing
3229 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
3231 if ( cmd
== EN_KILLFOCUS
)
3233 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
3243 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
3249 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
3251 if ( nMsg
== WM_CHAR
)
3253 // don't let the control process Space and Return keys because it
3254 // doesn't do anything useful with them anyhow but always beeps
3255 // annoyingly when it receives them and there is no way to turn it off
3256 // simply if you just process TREEITEM_ACTIVATED event to which Space
3257 // and Enter presses are mapped in your code
3258 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
3262 else if ( nMsg
== WM_KEYDOWN
)
3264 if ( wParam
== VK_ESCAPE
)
3268 m_dragImage
->EndDrag();
3269 wxDELETE(m_dragImage
);
3271 // if we don't do it, the tree seems to think that 2 items
3272 // are selected simultaneously which is quite weird
3273 TreeView_SelectDropTarget(GetHwnd(), 0);
3277 #endif // wxUSE_DRAGIMAGE
3279 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
3282 // process WM_NOTIFY Windows message
3283 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
3285 wxTreeEvent
event(wxEVT_NULL
, this);
3286 wxEventType eventType
= wxEVT_NULL
;
3287 NMHDR
*hdr
= (NMHDR
*)lParam
;
3289 switch ( hdr
->code
)
3292 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3295 case TVN_BEGINRDRAG
:
3297 if ( eventType
== wxEVT_NULL
)
3298 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
3299 //else: left drag, already set above
3301 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3303 event
.m_item
= tv
->itemNew
.hItem
;
3304 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
3306 // don't allow dragging by default: the user code must
3307 // explicitly say that it wants to allow it to avoid breaking
3313 case TVN_BEGINLABELEDIT
:
3315 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
3316 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3318 // although the user event handler may still veto it, it is
3319 // important to set it now so that calls to SetItemText() from
3320 // the event handler would change the text controls contents
3322 event
.m_item
= info
->item
.hItem
;
3323 event
.m_label
= info
->item
.pszText
;
3324 event
.m_editCancelled
= false;
3328 case TVN_DELETEITEM
:
3330 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
3331 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3333 event
.m_item
= tv
->itemOld
.hItem
;
3337 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
3338 if ( it
!= m_attrs
.end() )
3347 case TVN_ENDLABELEDIT
:
3349 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
3350 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3352 event
.m_item
= info
->item
.hItem
;
3353 event
.m_label
= info
->item
.pszText
;
3354 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
3359 // These *must* not be removed or TVN_GETINFOTIP will
3360 // not be processed each time the mouse is moved
3361 // and the tooltip will only ever update once.
3370 #ifdef TVN_GETINFOTIP
3371 case TVN_GETINFOTIP
:
3373 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
3374 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
3376 // Which item are we trying to get a tooltip for?
3377 event
.m_item
= info
->hItem
;
3381 #endif // TVN_GETINFOTIP
3382 #endif // !__WXWINCE__
3384 case TVN_GETDISPINFO
:
3385 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
3388 case TVN_SETDISPINFO
:
3390 if ( eventType
== wxEVT_NULL
)
3391 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
3392 //else: get, already set above
3394 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3396 event
.m_item
= info
->item
.hItem
;
3400 case TVN_ITEMEXPANDING
:
3401 case TVN_ITEMEXPANDED
:
3403 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3406 switch ( tv
->action
)
3409 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
3417 what
= IDX_COLLAPSE
;
3421 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
3424 eventType
= gs_expandEvents
[what
][how
];
3426 event
.m_item
= tv
->itemNew
.hItem
;
3432 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
3434 // fabricate the lParam and wParam parameters sufficiently
3435 // similar to the ones from a "real" WM_KEYDOWN so that
3436 // CreateKeyEvent() works correctly
3437 return MSWHandleTreeKeyDownEvent(
3438 info
->wVKey
, (wxIsAltDown() ? KF_ALTDOWN
: 0) << 16);
3442 // Vista's tree control has introduced some problems with our
3443 // multi-selection tree. When TreeView_SelectItem() is called,
3444 // the wrong items are deselected.
3446 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3447 // that can be used to regulate this incorrect behavior. The
3448 // following messages will allow only the unlocked item's selection
3451 case TVN_ITEMCHANGINGA
:
3452 case TVN_ITEMCHANGINGW
:
3454 // we only need to handles these in multi-select trees
3455 if ( HasFlag(wxTR_MULTIPLE
) )
3457 // get info about the item about to be changed
3458 NMTVITEMCHANGE
* info
= (NMTVITEMCHANGE
*)lParam
;
3459 if (TreeItemUnlocker::IsLocked(info
->hItem
))
3461 // item's state is locked, don't allow the change
3462 // returning 1 will disallow the change
3468 // allow the state change
3472 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
3473 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3474 // we have to handle both messages:
3475 case TVN_SELCHANGEDA
:
3476 case TVN_SELCHANGEDW
:
3477 if ( !m_changingSelection
)
3479 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
3483 case TVN_SELCHANGINGA
:
3484 case TVN_SELCHANGINGW
:
3485 if ( !m_changingSelection
)
3487 if ( eventType
== wxEVT_NULL
)
3488 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
3489 //else: already set above
3491 if (hdr
->code
== TVN_SELCHANGINGW
||
3492 hdr
->code
== TVN_SELCHANGEDW
)
3494 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
3495 event
.m_item
= tv
->itemNew
.hItem
;
3496 event
.m_itemOld
= tv
->itemOld
.hItem
;
3500 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
3501 event
.m_item
= tv
->itemNew
.hItem
;
3502 event
.m_itemOld
= tv
->itemOld
.hItem
;
3506 // we receive this message from WM_LBUTTONDOWN handler inside
3507 // comctl32.dll and so before the click is passed to
3508 // DefWindowProc() which sets the focus to the window which was
3509 // clicked and this can lead to unexpected event sequences: for
3510 // example, we may get a "selection change" event from the tree
3511 // before getting a "kill focus" event for the text control which
3512 // had the focus previously, thus breaking user code doing input
3515 // to avoid such surprises, we force the generation of focus events
3516 // now, before we generate the selection change ones
3517 if ( !m_changingSelection
)
3521 // instead of explicitly checking for _WIN32_IE, check if the
3522 // required symbols are available in the headers
3523 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
3526 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
3527 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
3528 switch ( nmcd
.dwDrawStage
)
3531 // if we've got any items with non standard attributes,
3532 // notify us before painting each item
3533 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
3536 // windows in TreeCtrl use one-based index for item state images,
3537 // 0 indexed image is not being used, we're using zero-based index,
3538 // so we have to add temp image (of zero index) to state image list
3539 // before we draw any item, then after items are drawn we have to
3540 // delete it (in POSTPAINT notify)
3541 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3543 typedef BOOL (wxSTDCALL
*ImageList_Copy_t
)
3544 (HIMAGELIST
, int, HIMAGELIST
, int, UINT
);
3545 static ImageList_Copy_t s_pfnImageList_Copy
= NULL
;
3546 static bool loaded
= false;
3550 wxLoadedDLL
dllComCtl32(wxT("comctl32.dll"));
3551 if ( dllComCtl32
.IsLoaded() )
3552 wxDL_INIT_FUNC(s_pfn
, ImageList_Copy
, dllComCtl32
);
3555 if ( !s_pfnImageList_Copy
)
3557 // this code is broken with ImageList_Copy()
3558 // but I don't care enough about Win95 support
3559 // to write it now -- if anybody does, please
3561 wxFAIL_MSG("TODO: implement this for Win95");
3566 hImageList
= GetHimagelistOf(m_imageListState
);
3568 // add temporary image
3570 m_imageListState
->GetSize(0, width
, height
);
3572 HBITMAP hbmpTemp
= ::CreateBitmap(width
, height
, 1, 1, NULL
);
3573 int index
= ::ImageList_Add(hImageList
, hbmpTemp
, hbmpTemp
);
3574 ::DeleteObject(hbmpTemp
);
3578 // move images to right
3579 for ( int i
= index
; i
> 0; i
-- )
3581 (*s_pfnImageList_Copy
)(hImageList
, i
,
3586 // we must remove the image in POSTPAINT notify
3587 *result
|= CDRF_NOTIFYPOSTPAINT
;
3592 case CDDS_POSTPAINT
:
3593 // we are deleting temp image of 0 index, which was
3594 // added before items were drawn (in PREPAINT notify)
3595 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3596 m_imageListState
->Remove(0);
3599 case CDDS_ITEMPREPAINT
:
3601 wxMapTreeAttr::iterator
3602 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
3604 if ( it
== m_attrs
.end() )
3606 // nothing to do for this item
3607 *result
= CDRF_DODEFAULT
;
3611 wxTreeItemAttr
* const attr
= it
->second
;
3613 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
3614 TVIF_STATE
, TVIS_DROPHILITED
);
3616 const UINT tvItemState
= tvItem
.state
;
3618 // selection colours should override ours,
3619 // otherwise it is too confusing to the user
3620 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
3621 !(tvItemState
& TVIS_DROPHILITED
) )
3624 if ( attr
->HasBackgroundColour() )
3626 colBack
= attr
->GetBackgroundColour();
3627 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
3631 // but we still want to keep the special foreground
3632 // colour when we don't have focus (we can't keep
3633 // it when we do, it would usually be unreadable on
3634 // the almost inverted bg colour...)
3635 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
3636 FindFocus() != this ) &&
3637 !(tvItemState
& TVIS_DROPHILITED
) )
3640 if ( attr
->HasTextColour() )
3642 colText
= attr
->GetTextColour();
3643 lptvcd
->clrText
= wxColourToRGB(colText
);
3647 if ( attr
->HasFont() )
3649 HFONT hFont
= GetHfontOf(attr
->GetFont());
3651 ::SelectObject(nmcd
.hdc
, hFont
);
3653 *result
= CDRF_NEWFONT
;
3655 else // no specific font
3657 *result
= CDRF_DODEFAULT
;
3663 *result
= CDRF_DODEFAULT
;
3667 // we always process it
3669 #endif // have owner drawn support in headers
3673 DWORD pos
= GetMessagePos();
3675 point
.x
= LOWORD(pos
);
3676 point
.y
= HIWORD(pos
);
3677 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
3679 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), htFlags
);
3681 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3683 event
.m_item
= item
;
3684 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
3693 TV_HITTESTINFO tvhti
;
3694 ::GetCursorPos(&tvhti
.pt
);
3695 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
3696 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
3698 if ( MSWIsOnItem(tvhti
.flags
) )
3700 event
.m_item
= tvhti
.hItem
;
3701 eventType
= (int)hdr
->code
== NM_DBLCLK
3702 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
3703 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
3705 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
3706 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
3715 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
3718 event
.SetEventType(eventType
);
3720 bool processed
= HandleTreeEvent(event
);
3723 switch ( hdr
->code
)
3726 // we translate NM_DBLCLK into ACTIVATED event and if the user
3727 // handled the activation of the item we shouldn't proceed with
3728 // also using the same double click for toggling the item expanded
3729 // state -- but OTOH do let the user to expand/collapse the item by
3730 // double clicking on it if the activation is not handled specially
3731 *result
= processed
;
3735 // prevent tree control from sending WM_CONTEXTMENU to our parent
3736 // (which it does if NM_RCLICK is not handled) because we want to
3737 // send it to the control itself
3741 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
3742 (WPARAM
)GetHwnd(), ::GetMessagePos());
3746 case TVN_BEGINRDRAG
:
3748 if ( event
.IsAllowed() )
3750 // normally this is impossible because the m_dragImage is
3751 // deleted once the drag operation is over
3752 wxASSERT_MSG( !m_dragImage
, wxT("starting to drag once again?") );
3754 m_dragImage
= new wxDragImage(*this, event
.m_item
);
3755 m_dragImage
->BeginDrag(wxPoint(0,0), this);
3756 m_dragImage
->Show();
3758 m_dragStarted
= true;
3760 #endif // wxUSE_DRAGIMAGE
3763 case TVN_DELETEITEM
:
3765 // NB: we might process this message using wxWidgets event
3766 // tables, but due to overhead of wxWin event system we
3767 // prefer to do it here ourself (otherwise deleting a tree
3768 // with many items is just too slow)
3769 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3771 wxTreeItemParam
*param
=
3772 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
3775 processed
= true; // Make sure we don't get called twice
3779 case TVN_BEGINLABELEDIT
:
3780 // return true to cancel label editing
3781 *result
= !event
.IsAllowed();
3783 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3784 if ( event
.IsAllowed() )
3786 HWND hText
= TreeView_GetEditControl(GetHwnd());
3789 // MBN: if m_textCtrl already has an HWND, it is a stale
3790 // pointer from a previous edit (because the user
3791 // didn't modify the label before dismissing the control,
3792 // and TVN_ENDLABELEDIT was not sent), so delete it
3793 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
3796 m_textCtrl
= new wxTextCtrl();
3797 m_textCtrl
->SetParent(this);
3798 m_textCtrl
->SetHWND((WXHWND
)hText
);
3799 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3801 // set wxTE_PROCESS_ENTER style for the text control to
3802 // force it to process the Enter presses itself, otherwise
3803 // they could be stolen from it by the dialog
3805 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3806 | wxTE_PROCESS_ENTER
);
3809 else // we had set m_idEdited before
3815 case TVN_ENDLABELEDIT
:
3816 // return true to set the label to the new string: note that we
3817 // also must pretend that we did process the message or it is going
3818 // to be passed to DefWindowProc() which will happily return false
3819 // cancelling the label change
3820 *result
= event
.IsAllowed();
3823 // ensure that we don't have the text ctrl which is going to be
3829 #ifdef TVN_GETINFOTIP
3830 case TVN_GETINFOTIP
:
3832 // If the user permitted a tooltip change, change it
3833 if (event
.IsAllowed())
3835 SetToolTip(event
.m_label
);
3842 case TVN_SELCHANGING
:
3843 case TVN_ITEMEXPANDING
:
3844 // return true to prevent the action from happening
3845 *result
= !event
.IsAllowed();
3848 case TVN_ITEMEXPANDED
:
3850 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3851 const wxTreeItemId
id(tv
->itemNew
.hItem
);
3853 if ( tv
->action
== TVE_COLLAPSE
)
3855 if ( wxApp::GetComCtl32Version() >= 600 )
3857 // for some reason the item selection rectangle depends
3858 // on whether it is expanded or collapsed (at least
3859 // with comctl32.dll v6): it is wider (by 3 pixels) in
3860 // the expanded state, so when the item collapses and
3861 // then is deselected the rightmost 3 pixels of the
3862 // previously drawn selection are left on the screen
3864 // it's not clear if it's a bug in comctl32.dll or in
3865 // our code (because it does not happen in Explorer but
3866 // OTOH we don't do anything which could result in this
3867 // AFAICS) but we do need to work around it to avoid
3874 // the item is also not refreshed properly after expansion when
3875 // it has an image depending on the expanded/collapsed state:
3876 // again, it's not clear if the bug is in comctl32.dll or our
3878 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3887 case TVN_GETDISPINFO
:
3888 // NB: so far the user can't set the image himself anyhow, so do it
3889 // anyway - but this may change later
3890 //if ( /* !processed && */ )
3892 wxTreeItemId item
= event
.m_item
;
3893 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3895 const wxTreeItemParam
* const param
= GetItemParam(item
);
3899 if ( info
->item
.mask
& TVIF_IMAGE
)
3904 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3905 : wxTreeItemIcon_Normal
3908 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3910 info
->item
.iSelectedImage
=
3913 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3914 : wxTreeItemIcon_Selected
3921 // for the other messages the return value is ignored and there is
3922 // nothing special to do
3927 // ----------------------------------------------------------------------------
3929 // ----------------------------------------------------------------------------
3931 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3932 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3934 int wxTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
3936 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
3938 // receive the desired information
3939 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3942 // state images are one-based
3943 return STATEIMAGEMASKTOINDEX(tvItem
.state
) - 1;
3946 void wxTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
3948 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
3950 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3952 // state images are one-based
3953 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3954 tvItem
.state
= INDEXTOSTATEIMAGEMASK(state
+ 1);
3959 #endif // wxUSE_TREECTRL