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( _T("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;
754 // initialize the global array of events now as it can't be done statically
755 // with the wxEVT_XXX values being allocated during run-time only
756 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
757 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
758 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
759 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
762 bool wxTreeCtrl::Create(wxWindow
*parent
,
767 const wxValidator
& validator
,
768 const wxString
& name
)
772 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
773 style
|= wxBORDER_SUNKEN
;
775 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
779 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
780 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
782 if ( !(m_windowStyle
& wxTR_NO_LINES
) )
783 wstyle
|= TVS_HASLINES
;
784 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
785 wstyle
|= TVS_HASBUTTONS
;
787 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
788 wstyle
|= TVS_EDITLABELS
;
790 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
791 wstyle
|= TVS_LINESATROOT
;
793 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
795 if ( wxApp::GetComCtl32Version() >= 471 )
796 wstyle
|= TVS_FULLROWSELECT
;
799 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
800 // Need so that TVN_GETINFOTIP messages will be sent
801 wstyle
|= TVS_INFOTIP
;
804 // Create the tree control.
805 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
808 #if wxUSE_COMCTL32_SAFELY
809 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
810 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
812 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
813 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
815 // This works around a bug in the Windows tree control whereby for some versions
816 // of comctrl32, setting any colour actually draws the background in black.
817 // This will initialise the background to the system colour.
818 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
819 // Assume the user has an updated comctl32.dll.
820 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
821 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
822 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
825 wxSetCCUnicodeFormat(GetHwnd());
830 wxTreeCtrl::~wxTreeCtrl()
832 // delete any attributes
835 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
837 // prevent TVN_DELETEITEM handler from deleting the attributes again!
838 m_hasAnyAttr
= false;
843 // delete user data to prevent memory leaks
844 // also deletes hidden root node storage.
848 // ----------------------------------------------------------------------------
850 // ----------------------------------------------------------------------------
852 /* static */ wxVisualAttributes
853 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
855 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
857 // common controls have their own default font
858 attrs
.font
= wxGetCCDefaultFont();
864 // simple wrappers which add error checking in debug mode
866 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
868 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
869 _T("can't retrieve virtual root item") );
871 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
873 wxLogLastError(wxT("TreeView_GetItem"));
881 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
883 TreeItemUnlocker
unlocker(tvItem
->hItem
);
885 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
887 wxLogLastError(wxT("TreeView_SetItem"));
891 unsigned int wxTreeCtrl::GetCount() const
893 return (unsigned int)TreeView_GetCount(GetHwnd());
896 unsigned int wxTreeCtrl::GetIndent() const
898 return TreeView_GetIndent(GetHwnd());
901 void wxTreeCtrl::SetIndent(unsigned int indent
)
903 TreeView_SetIndent(GetHwnd(), indent
);
906 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
909 (void) TreeView_SetImageList(GetHwnd(),
910 imageList
? imageList
->GetHIMAGELIST() : 0,
914 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
916 if (m_ownsImageListNormal
)
917 delete m_imageListNormal
;
919 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
920 m_ownsImageListNormal
= false;
923 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
925 if (m_ownsImageListState
) delete m_imageListState
;
926 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
927 m_ownsImageListState
= false;
930 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
931 bool recursively
) const
933 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
935 TraverseCounter
counter(this, item
, recursively
);
936 return counter
.GetCount() - 1;
939 // ----------------------------------------------------------------------------
941 // ----------------------------------------------------------------------------
943 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
945 #if !wxUSE_COMCTL32_SAFELY
946 if ( !wxWindowBase::SetBackgroundColour(colour
) )
949 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
955 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
957 #if !wxUSE_COMCTL32_SAFELY
958 if ( !wxWindowBase::SetForegroundColour(colour
) )
961 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
967 // ----------------------------------------------------------------------------
969 // ----------------------------------------------------------------------------
971 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
973 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
976 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
978 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
980 wxChar buf
[512]; // the size is arbitrary...
982 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
983 tvItem
.pszText
= buf
;
984 tvItem
.cchTextMax
= WXSIZEOF(buf
);
985 if ( !DoGetItem(&tvItem
) )
987 // don't return some garbage which was on stack, but an empty string
991 return wxString(buf
);
994 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
996 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
998 if ( IS_VIRTUAL_ROOT(item
) )
1001 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
1002 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
1005 // when setting the text of the item being edited, the text control should
1006 // be updated to reflect the new text as well, otherwise calling
1007 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
1009 // don't use GetEditControl() here because m_textCtrl is not set yet
1010 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
1013 if ( item
== m_idEdited
)
1015 ::SetWindowText(hwndEdit
, text
.wx_str());
1020 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
1021 wxTreeItemIcon which
) const
1023 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1025 if ( IsHiddenRoot(item
) )
1027 // no images for hidden root item
1031 wxTreeItemParam
*param
= GetItemParam(item
);
1033 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
1036 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1037 wxTreeItemIcon which
)
1039 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1040 wxCHECK_RET( which
>= 0 &&
1041 which
< wxTreeItemIcon_Max
,
1042 wxT("invalid image index"));
1045 if ( IsHiddenRoot(item
) )
1047 // no images for hidden root item
1051 wxTreeItemParam
*data
= GetItemParam(item
);
1055 data
->SetImage(image
, which
);
1060 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
1062 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1064 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1066 // hidden root may still have data.
1067 if ( IS_VIRTUAL_ROOT(item
) )
1069 return GET_VIRTUAL_ROOT()->GetParam();
1073 if ( !DoGetItem(&tvItem
) )
1078 return (wxTreeItemParam
*)tvItem
.lParam
;
1081 bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent
& event
) const
1083 if ( event
.m_item
.IsOk() )
1085 event
.SetClientObject(GetItemData(event
.m_item
));
1088 return HandleWindowEvent(event
);
1091 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1093 wxTreeItemParam
*data
= GetItemParam(item
);
1095 return data
? data
->GetData() : NULL
;
1098 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1100 // first, associate this piece of data with this item
1106 wxTreeItemParam
*param
= GetItemParam(item
);
1108 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1110 param
->SetData(data
);
1113 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1115 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1117 if ( IS_VIRTUAL_ROOT(item
) )
1120 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1121 tvItem
.cChildren
= (int)has
;
1125 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1127 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1129 if ( IS_VIRTUAL_ROOT(item
) )
1132 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1133 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1137 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1139 if ( IS_VIRTUAL_ROOT(item
) )
1142 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1143 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1147 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1149 if ( IS_VIRTUAL_ROOT(item
) )
1153 if ( GetBoundingRect(item
, rect
) )
1159 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1161 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1163 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1164 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1167 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1169 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1171 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1172 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1175 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1177 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1179 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1180 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1183 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1184 const wxColour
& col
)
1186 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1188 wxTreeItemAttr
*attr
;
1189 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1190 if ( it
== m_attrs
.end() )
1192 m_hasAnyAttr
= true;
1194 m_attrs
[item
.m_pItem
] =
1195 attr
= new wxTreeItemAttr
;
1202 attr
->SetTextColour(col
);
1207 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1208 const wxColour
& col
)
1210 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1212 wxTreeItemAttr
*attr
;
1213 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1214 if ( it
== m_attrs
.end() )
1216 m_hasAnyAttr
= true;
1218 m_attrs
[item
.m_pItem
] =
1219 attr
= new wxTreeItemAttr
;
1221 else // already in the hash
1226 attr
->SetBackgroundColour(col
);
1231 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1233 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1235 wxTreeItemAttr
*attr
;
1236 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1237 if ( it
== m_attrs
.end() )
1239 m_hasAnyAttr
= true;
1241 m_attrs
[item
.m_pItem
] =
1242 attr
= new wxTreeItemAttr
;
1244 else // already in the hash
1249 attr
->SetFont(font
);
1251 // Reset the item's text to ensure that the bounding rect will be adjusted
1252 // for the new font.
1253 SetItemText(item
, GetItemText(item
));
1258 // ----------------------------------------------------------------------------
1260 // ----------------------------------------------------------------------------
1262 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1264 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1266 if ( item
== wxTreeItemId(TVI_ROOT
) )
1268 // virtual (hidden) root is never visible
1272 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1275 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1276 // the HTREEITEM with TVM_GETITEMRECT
1277 *(HTREEITEM
*)&rect
= HITEM(item
);
1279 // true means to get rect for just the text, not the whole line
1280 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1282 // if TVM_GETITEMRECT returned false, then the item is definitely not
1283 // visible (because its parent is not expanded)
1287 // however if it returned true, the item might still be outside the
1288 // currently visible part of the tree, test for it (notice that partly
1289 // visible means visible here)
1290 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1293 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1295 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1297 if ( IS_VIRTUAL_ROOT(item
) )
1299 wxTreeItemIdValue cookie
;
1300 return GetFirstChild(item
, cookie
).IsOk();
1303 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1306 return tvItem
.cChildren
!= 0;
1309 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1311 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1313 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1316 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1319 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1321 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1323 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1326 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1329 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1331 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1333 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1336 return (tvItem
.state
& TVIS_BOLD
) != 0;
1339 // ----------------------------------------------------------------------------
1341 // ----------------------------------------------------------------------------
1343 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1345 // Root may be real (visible) or virtual (hidden).
1346 if ( GET_VIRTUAL_ROOT() )
1349 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1352 wxTreeItemId
wxTreeCtrl::GetSelection() const
1354 wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE
), wxTreeItemId(),
1355 wxT("this only works with single selection controls") );
1357 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1360 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1362 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1366 if ( IS_VIRTUAL_ROOT(item
) )
1368 // no parent for the virtual root
1373 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1374 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1376 // the top level items should have the virtual root as their parent
1381 return wxTreeItemId(hItem
);
1384 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1385 wxTreeItemIdValue
& cookie
) const
1387 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1389 // remember the last child returned in 'cookie'
1390 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1392 return wxTreeItemId(cookie
);
1395 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1396 wxTreeItemIdValue
& cookie
) const
1398 wxTreeItemId
fromCookie(cookie
);
1400 HTREEITEM hitem
= HITEM(fromCookie
);
1402 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1404 wxTreeItemId
item(hitem
);
1406 cookie
= item
.m_pItem
;
1411 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1413 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1415 // can this be done more efficiently?
1416 wxTreeItemIdValue cookie
;
1418 wxTreeItemId childLast
,
1419 child
= GetFirstChild(item
, cookie
);
1420 while ( child
.IsOk() )
1423 child
= GetNextChild(item
, cookie
);
1429 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1431 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1432 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1435 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1437 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1438 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1441 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1443 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1446 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1448 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1449 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1451 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1452 if ( next
.IsOk() && !IsVisible(next
) )
1454 // Win32 considers that any non-collapsed item is visible while we want
1455 // to return only really visible items
1462 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1464 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1465 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1467 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1468 if ( prev
.IsOk() && !IsVisible(prev
) )
1470 // just as above, Win32 function will happily return the previous item
1471 // in the tree for the first visible item too
1478 // ----------------------------------------------------------------------------
1479 // multiple selections emulation
1480 // ----------------------------------------------------------------------------
1482 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1484 TraverseSelections
selector(this, selections
);
1486 return selector
.GetCount();
1489 // ----------------------------------------------------------------------------
1491 // ----------------------------------------------------------------------------
1493 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1494 const wxTreeItemId
& hInsertAfter
,
1495 const wxString
& text
,
1496 int image
, int selectedImage
,
1497 wxTreeItemData
*data
)
1499 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1501 _T("can't have more than one root in the tree") );
1503 TV_INSERTSTRUCT tvIns
;
1504 tvIns
.hParent
= HITEM(parent
);
1505 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1507 // this is how we insert the item as the first child: supply a NULL
1509 if ( !tvIns
.hInsertAfter
)
1511 tvIns
.hInsertAfter
= TVI_FIRST
;
1515 if ( !text
.empty() )
1518 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1522 tvIns
.item
.pszText
= NULL
;
1523 tvIns
.item
.cchTextMax
= 0;
1526 // create the param which will store the other item parameters
1527 wxTreeItemParam
*param
= new wxTreeItemParam
;
1529 // we return the images on demand as they depend on whether the item is
1530 // expanded or collapsed too in our case
1531 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1532 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1533 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1535 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1536 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1539 tvIns
.item
.lParam
= (LPARAM
)param
;
1540 tvIns
.item
.mask
= mask
;
1542 // don't use the hack below for the children of hidden root: this results
1543 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1544 const bool firstChild
= !IsHiddenRoot(parent
) &&
1545 !TreeView_GetChild(GetHwnd(), HITEM(parent
));
1547 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1550 wxLogLastError(wxT("TreeView_InsertItem"));
1553 // apparently some Windows versions (2000 and XP are reported to do this)
1554 // sometimes don't refresh the tree after adding the first child and so we
1555 // need this to make the "[+]" appear
1559 TreeView_GetItemRect(GetHwnd(), HITEM(parent
), &rect
, FALSE
);
1560 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1563 // associate the application tree item with Win32 tree item handle
1566 // setup wxTreeItemData
1569 param
->SetData(data
);
1573 return wxTreeItemId(id
);
1576 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1577 int image
, int selectedImage
,
1578 wxTreeItemData
*data
)
1580 if ( HasFlag(wxTR_HIDE_ROOT
) )
1582 wxASSERT_MSG( !m_pVirtualRoot
, _T("tree can have only a single root") );
1584 // create a virtual root item, the parent for all the others
1585 wxTreeItemParam
*param
= new wxTreeItemParam
;
1586 param
->SetData(data
);
1588 m_pVirtualRoot
= new wxVirtualNode(param
);
1593 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1594 text
, image
, selectedImage
, data
);
1597 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1599 const wxString
& text
,
1600 int image
, int selectedImage
,
1601 wxTreeItemData
*data
)
1603 wxTreeItemId idPrev
;
1604 if ( index
== (size_t)-1 )
1606 // special value: append to the end
1609 else // find the item from index
1611 wxTreeItemIdValue cookie
;
1612 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1613 while ( index
!= 0 && idCur
.IsOk() )
1618 idCur
= GetNextChild(parent
, cookie
);
1621 // assert, not check: if the index is invalid, we will append the item
1623 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1626 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1629 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1631 // unlock tree selections on vista, without this the
1632 // tree ctrl will eventually crash after item deletion
1633 TreeItemUnlocker unlock_all
;
1635 if ( HasFlag(wxTR_MULTIPLE
) )
1637 bool selected
= IsSelected(item
);
1642 next
= TreeView_GetNextVisible(GetHwnd(), HITEM(item
));
1646 next
= TreeView_GetPrevVisible(GetHwnd(), HITEM(item
));
1651 TempSetter
set(m_changingSelection
);
1652 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1654 wxLogLastError(wxT("TreeView_DeleteItem"));
1666 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
1668 if ( IsTreeEventAllowed(changingEvent
) )
1670 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
1671 (void)HandleTreeEvent(changedEvent
);
1675 DoUnselectItem(next
);
1682 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1684 wxLogLastError(wxT("TreeView_DeleteItem"));
1689 // delete all children (but don't delete the item itself)
1690 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1692 // unlock tree selections on vista for the duration of this call
1693 TreeItemUnlocker unlock_all
;
1695 wxTreeItemIdValue cookie
;
1697 wxArrayTreeItemIds children
;
1698 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1699 while ( child
.IsOk() )
1701 children
.Add(child
);
1703 child
= GetNextChild(item
, cookie
);
1706 size_t nCount
= children
.Count();
1707 for ( size_t n
= 0; n
< nCount
; n
++ )
1709 Delete(children
[n
]);
1713 void wxTreeCtrl::DeleteAllItems()
1715 // unlock tree selections on vista for the duration of this call
1716 TreeItemUnlocker unlock_all
;
1718 // delete the "virtual" root item.
1719 if ( GET_VIRTUAL_ROOT() )
1721 delete GET_VIRTUAL_ROOT();
1722 m_pVirtualRoot
= NULL
;
1725 // and all the real items
1727 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1729 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1733 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1735 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1736 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1737 flag
== TVE_EXPAND
||
1739 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1741 // A hidden root can be neither expanded nor collapsed.
1742 wxCHECK_RET( !IsHiddenRoot(item
),
1743 wxT("Can't expand/collapse hidden root node!") );
1745 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1746 // emulate them. This behaviour has changed slightly with comctl32.dll
1747 // v 4.70 - now it does send them but only the first time. To maintain
1748 // compatible behaviour and also in order to not have surprises with the
1749 // future versions, don't rely on this and still do everything ourselves.
1750 // To avoid that the messages be sent twice when the item is expanded for
1751 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1753 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1757 if ( IsExpanded(item
) )
1759 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
,
1760 this, wxTreeItemId(item
));
1762 if ( !IsTreeEventAllowed(event
) )
1766 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) )
1768 if ( IsExpanded(item
) )
1771 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, this, item
);
1772 (void)HandleTreeEvent(event
);
1774 //else: change didn't took place, so do nothing at all
1777 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1779 DoExpand(item
, TVE_EXPAND
);
1782 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1784 DoExpand(item
, TVE_COLLAPSE
);
1787 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1789 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1792 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1794 DoExpand(item
, TVE_TOGGLE
);
1797 void wxTreeCtrl::Unselect()
1799 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE
),
1800 wxT("doesn't make sense, may be you want UnselectAll()?") );
1802 // the current focus
1803 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1810 if ( HasFlag(wxTR_MULTIPLE
) )
1812 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
1813 this, wxTreeItemId());
1814 changingEvent
.m_itemOld
= htFocus
;
1816 if ( IsTreeEventAllowed(changingEvent
) )
1820 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1821 this, wxTreeItemId());
1822 changedEvent
.m_itemOld
= htFocus
;
1823 (void)HandleTreeEvent(changedEvent
);
1832 void wxTreeCtrl::DoUnselectAll()
1834 wxArrayTreeItemIds selections
;
1835 size_t count
= GetSelections(selections
);
1837 for ( size_t n
= 0; n
< count
; n
++ )
1839 DoUnselectItem(selections
[n
]);
1842 m_htSelStart
.Unset();
1845 void wxTreeCtrl::UnselectAll()
1847 if ( HasFlag(wxTR_MULTIPLE
) )
1849 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1850 if ( !htFocus
) return;
1852 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1853 changingEvent
.m_itemOld
= htFocus
;
1855 if ( IsTreeEventAllowed(changingEvent
) )
1859 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1860 changedEvent
.m_itemOld
= htFocus
;
1861 (void)HandleTreeEvent(changedEvent
);
1870 void wxTreeCtrl::DoSelectItem(const wxTreeItemId
& item
, bool select
)
1872 TempSetter
set(m_changingSelection
);
1874 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1877 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1879 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't select hidden root item") );
1881 if ( select
== IsSelected(item
) )
1883 // nothing to do, the item is already in the requested state
1887 if ( HasFlag(wxTR_MULTIPLE
) )
1889 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1891 if ( IsTreeEventAllowed(changingEvent
) )
1893 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1894 DoSelectItem(item
, select
);
1898 SetFocusedItem(item
);
1901 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1903 (void)HandleTreeEvent(changedEvent
);
1906 else // single selection
1908 wxTreeItemId itemOld
, itemNew
;
1911 itemOld
= GetSelection();
1914 else // deselecting the currently selected item
1917 // leave itemNew invalid
1920 // in spite of the docs (MSDN Jan 99 edition), we don't seem to receive
1921 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1922 // send them ourselves
1925 changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, itemNew
);
1926 changingEvent
.SetOldItem(itemOld
);
1928 if ( IsTreeEventAllowed(changingEvent
) )
1930 if ( !TreeView_SelectItem(GetHwnd(), HITEM(itemNew
)) )
1932 wxLogLastError(wxT("TreeView_SelectItem"));
1936 SetFocusedItem(item
);
1938 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1940 changedEvent
.SetOldItem(itemOld
);
1941 (void)HandleTreeEvent(changedEvent
);
1944 //else: program vetoed the change
1948 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1950 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't show hidden root item") );
1953 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1956 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1958 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1960 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1964 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1969 void wxTreeCtrl::DeleteTextCtrl()
1973 // the HWND corresponding to this control is deleted by the tree
1974 // control itself and we don't know when exactly this happens, so check
1975 // if the window still exists before calling UnsubclassWin()
1976 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1978 m_textCtrl
->SetHWND(0);
1981 m_textCtrl
->UnsubclassWin();
1982 m_textCtrl
->SetHWND(0);
1990 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1991 wxClassInfo
*textControlClass
)
1993 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1998 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1999 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
2001 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2010 // textctrl is subclassed in MSWOnNotify
2014 // End label editing, optionally cancelling the edit
2015 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
2017 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
2022 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2024 TV_HITTESTINFO hitTestInfo
;
2025 hitTestInfo
.pt
.x
= (int)point
.x
;
2026 hitTestInfo
.pt
.y
= (int)point
.y
;
2028 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2033 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2034 flags |= wxTREE_HITTEST_##flag
2036 TRANSLATE_FLAG(ABOVE
);
2037 TRANSLATE_FLAG(BELOW
);
2038 TRANSLATE_FLAG(NOWHERE
);
2039 TRANSLATE_FLAG(ONITEMBUTTON
);
2040 TRANSLATE_FLAG(ONITEMICON
);
2041 TRANSLATE_FLAG(ONITEMINDENT
);
2042 TRANSLATE_FLAG(ONITEMLABEL
);
2043 TRANSLATE_FLAG(ONITEMRIGHT
);
2044 TRANSLATE_FLAG(ONITEMSTATEICON
);
2045 TRANSLATE_FLAG(TOLEFT
);
2046 TRANSLATE_FLAG(TORIGHT
);
2048 #undef TRANSLATE_FLAG
2050 return wxTreeItemId(hitTestInfo
.hItem
);
2053 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2055 bool textOnly
) const
2059 // Virtual root items have no bounding rectangle
2060 if ( IS_VIRTUAL_ROOT(item
) )
2065 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2068 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2074 // couldn't retrieve rect: for example, item isn't visible
2079 void wxTreeCtrl::ClearFocusedItem()
2081 TempSetter
set(m_changingSelection
);
2083 if ( !TreeView_SelectItem(GetHwnd(), 0) )
2085 wxLogLastError(wxT("TreeView_SelectItem"));
2089 void wxTreeCtrl::SetFocusedItem(const wxTreeItemId
& item
)
2091 TempSetter
set(m_changingSelection
);
2093 ::SetFocus(GetHwnd(), HITEM(item
));
2096 void wxTreeCtrl::DoUnselectItem(const wxTreeItemId
& item
)
2098 TempSetter
set(m_changingSelection
);
2100 ::UnselectItem(GetHwnd(), HITEM(item
));
2103 void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId
& item
)
2105 TempSetter
set(m_changingSelection
);
2107 ::ToggleItemSelection(GetHwnd(), HITEM(item
));
2110 // ----------------------------------------------------------------------------
2112 // ----------------------------------------------------------------------------
2114 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2115 // functions such as IsDataIndirect()
2116 class wxTreeSortHelper
2119 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2122 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
2124 return ((wxTreeItemParam
*)lParam
)->GetItem();
2128 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2132 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2133 wxT("sorting tree without data doesn't make sense") );
2135 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2137 return tree
->OnCompareItems(GetIdFromData(pItem1
),
2138 GetIdFromData(pItem2
));
2141 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2143 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2145 // rely on the fact that TreeView_SortChildren does the same thing as our
2146 // default behaviour, i.e. sorts items alphabetically and so call it
2147 // directly if we're not in derived class (much more efficient!)
2148 // RN: Note that if you find you're code doesn't sort as expected this
2149 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
2150 // combo for your derived wxTreeCtrl if will sort without
2152 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2154 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2159 tvSort
.hParent
= HITEM(item
);
2160 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2161 tvSort
.lParam
= (LPARAM
)this;
2162 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2166 // ----------------------------------------------------------------------------
2168 // ----------------------------------------------------------------------------
2170 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
2172 if ( msg
->message
== WM_KEYDOWN
)
2174 // Only eat VK_RETURN if not being used by the application in
2175 // conjunction with modifiers
2176 if ( (msg
->wParam
== VK_RETURN
) && !wxIsAnyModifierDown() )
2178 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2183 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
2186 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
2188 const int id
= (signed short)id_
;
2190 if ( cmd
== EN_UPDATE
)
2192 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2193 event
.SetEventObject( this );
2194 ProcessCommand(event
);
2196 else if ( cmd
== EN_KILLFOCUS
)
2198 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2199 event
.SetEventObject( this );
2200 ProcessCommand(event
);
2208 // command processed
2212 bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey
)
2214 const bool bCtrl
= wxIsCtrlDown();
2215 const bool bShift
= wxIsShiftDown();
2216 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2225 if ( vkey
!= VK_RETURN
&& bCtrl
)
2227 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2229 changingEvent
.m_itemOld
= htSel
;
2231 if ( IsTreeEventAllowed(changingEvent
) )
2233 DoToggleItemSelection(wxTreeItemId(htSel
));
2235 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2237 changedEvent
.m_itemOld
= htSel
;
2238 (void)HandleTreeEvent(changedEvent
);
2243 wxArrayTreeItemIds selections
;
2244 size_t count
= GetSelections(selections
);
2246 if ( count
!= 1 || HITEM(selections
[0]) != htSel
)
2248 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2250 changingEvent
.m_itemOld
= htSel
;
2252 if ( IsTreeEventAllowed(changingEvent
) )
2255 DoSelectItem(wxTreeItemId(htSel
));
2257 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2259 changedEvent
.m_itemOld
= htSel
;
2260 (void)HandleTreeEvent(changedEvent
);
2268 if ( !bCtrl
&& !bShift
)
2270 wxArrayTreeItemIds selections
;
2275 next
= vkey
== VK_UP
2276 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2277 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2281 next
= GetRootItem();
2283 if ( IsHiddenRoot(next
) )
2284 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2292 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2294 changingEvent
.m_itemOld
= htSel
;
2296 if ( IsTreeEventAllowed(changingEvent
) )
2300 SetFocusedItem(next
);
2302 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2304 changedEvent
.m_itemOld
= htSel
;
2305 (void)HandleTreeEvent(changedEvent
);
2310 wxTreeItemId next
= vkey
== VK_UP
2311 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2312 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2319 if ( !m_htSelStart
)
2321 m_htSelStart
= htSel
;
2324 if ( bShift
&& SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2325 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2327 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2328 changingEvent
.m_itemOld
= htSel
;
2330 if ( IsTreeEventAllowed(changingEvent
) )
2332 SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2333 SR_UNSELECT_OTHERS
);
2335 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2336 changedEvent
.m_itemOld
= htSel
;
2337 (void)HandleTreeEvent(changedEvent
);
2341 SetFocusedItem(next
);
2346 if ( HasChildren(htSel
) && IsExpanded(htSel
) )
2352 wxTreeItemId next
= GetItemParent(htSel
);
2354 if ( next
.IsOk() && !IsHiddenRoot(next
) )
2356 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2358 changingEvent
.m_itemOld
= htSel
;
2360 if ( IsTreeEventAllowed(changingEvent
) )
2364 SetFocusedItem(next
);
2366 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2368 changedEvent
.m_itemOld
= htSel
;
2369 (void)HandleTreeEvent(changedEvent
);
2376 if ( !IsVisible(htSel
) )
2378 EnsureVisible(htSel
);
2381 if ( !HasChildren(htSel
) )
2384 if ( !IsExpanded(htSel
) )
2390 wxTreeItemId next
= TreeView_GetChild(GetHwnd(), htSel
);
2392 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2393 changingEvent
.m_itemOld
= htSel
;
2395 if ( IsTreeEventAllowed(changingEvent
) )
2399 SetFocusedItem(next
);
2401 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2402 changedEvent
.m_itemOld
= htSel
;
2403 (void)HandleTreeEvent(changedEvent
);
2411 wxTreeItemId next
= GetRootItem();
2413 if ( IsHiddenRoot(next
) )
2415 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2421 if ( vkey
== VK_END
)
2425 wxTreeItemId nextTemp
= TreeView_GetNextVisible(
2426 GetHwnd(), HITEM(next
));
2428 if ( !nextTemp
.IsOk() )
2435 if ( htSel
== HITEM(next
) )
2440 if ( !m_htSelStart
)
2442 m_htSelStart
= htSel
;
2445 if ( SelectRange(GetHwnd(),
2446 HITEM(m_htSelStart
), HITEM(next
),
2447 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2449 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2451 changingEvent
.m_itemOld
= htSel
;
2453 if ( IsTreeEventAllowed(changingEvent
) )
2455 SelectRange(GetHwnd(),
2456 HITEM(m_htSelStart
), HITEM(next
),
2457 SR_UNSELECT_OTHERS
);
2458 SetFocusedItem(next
);
2460 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2462 changedEvent
.m_itemOld
= htSel
;
2463 (void)HandleTreeEvent(changedEvent
);
2469 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2471 changingEvent
.m_itemOld
= htSel
;
2473 if ( IsTreeEventAllowed(changingEvent
) )
2477 SetFocusedItem(next
);
2479 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2481 changedEvent
.m_itemOld
= htSel
;
2482 (void)HandleTreeEvent(changedEvent
);
2492 wxTreeItemId firstVisible
= GetFirstVisibleItem();
2493 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2494 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2495 TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible
)) :
2496 TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible
));
2498 if ( !nextAdjacent
)
2503 wxTreeItemId nextStart
= firstVisible
;
2505 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2507 wxTreeItemId nextTemp
= (vkey
== VK_PRIOR
) ?
2508 TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart
)) :
2509 TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart
));
2511 if ( nextTemp
.IsOk() )
2513 nextStart
= nextTemp
;
2521 EnsureVisible(nextStart
);
2523 if ( vkey
== VK_NEXT
)
2525 wxTreeItemId nextEnd
= nextStart
;
2527 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2529 wxTreeItemId nextTemp
=
2530 TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd
));
2532 if ( nextTemp
.IsOk() )
2542 EnsureVisible(nextEnd
);
2547 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2548 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2549 TreeView_GetPrevVisible(GetHwnd(), htSel
) :
2550 TreeView_GetNextVisible(GetHwnd(), htSel
);
2552 if ( !nextAdjacent
)
2557 wxTreeItemId
next(htSel
);
2559 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2561 wxTreeItemId nextTemp
= vkey
== VK_PRIOR
?
2562 TreeView_GetPrevVisible(GetHwnd(), HITEM(next
)) :
2563 TreeView_GetNextVisible(GetHwnd(), HITEM(next
));
2565 if ( !nextTemp
.IsOk() )
2571 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2573 changingEvent
.m_itemOld
= htSel
;
2575 if ( IsTreeEventAllowed(changingEvent
) )
2578 m_htSelStart
.Unset();
2580 SetFocusedItem(next
);
2582 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2584 changedEvent
.m_itemOld
= htSel
;
2585 (void)HandleTreeEvent(changedEvent
);
2597 bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam
, WXLPARAM lParam
)
2599 wxTreeEvent
keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2601 int keyCode
= wxCharCodeMSWToWX(wParam
);
2605 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2610 keyEvent
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
, keyCode
,
2613 bool processed
= HandleTreeEvent(keyEvent
);
2615 // generate a separate event for Space/Return
2616 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
2617 ((wParam
== VK_SPACE
) || (wParam
== VK_RETURN
)) )
2619 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2622 wxTreeEvent
activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2624 (void)HandleTreeEvent(activatedEvent
);
2631 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2632 // only do it during dragging, minimize wxWin overhead (this is important for
2633 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2634 // instead of passing by wxWin events
2636 wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2638 bool processed
= false;
2640 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2642 if ( nMsg
== WM_CONTEXTMENU
)
2644 int x
= GET_X_LPARAM(lParam
),
2645 y
= GET_Y_LPARAM(lParam
);
2647 // the item for which the menu should be shown
2650 // the position where the menu should be shown in client coordinates
2651 // (so that it can be passed directly to PopupMenu())
2654 if ( x
== -1 || y
== -1 )
2656 // this means that the event was generated from keyboard (e.g. with
2657 // Shift-F10 or special Windows menu key)
2659 // use the Explorer standard of putting the menu at the left edge
2660 // of the text, in the vertical middle of the text
2661 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2664 // Use the bounding rectangle of only the text part
2666 GetBoundingRect(item
, rect
, true);
2667 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
2670 else // event from mouse, use mouse position
2672 pt
= ScreenToClient(wxPoint(x
, y
));
2674 TV_HITTESTINFO tvhti
;
2678 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2679 item
= wxTreeItemId(tvhti
.hItem
);
2683 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2685 event
.m_pointDrag
= pt
;
2687 if ( HandleTreeEvent(event
) )
2689 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2691 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2693 // we only process mouse messages here and these parameters have the
2694 // same meaning for all of them
2695 int x
= GET_X_LPARAM(lParam
),
2696 y
= GET_Y_LPARAM(lParam
);
2698 TV_HITTESTINFO tvht
;
2702 HTREEITEM htOldItem
= TreeView_GetSelection(GetHwnd());
2703 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2707 case WM_LBUTTONDOWN
:
2711 m_htClickedItem
.Unset();
2713 if ( !(tvht
.flags
& TVHT_ONITEM
) )
2715 if ( !HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2717 if ( tvht
.flags
& TVHT_ONITEMBUTTON
)
2719 if ( !IsExpanded(htItem
) )
2732 m_focusLost
= false;
2738 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2739 m_ptClick
= wxPoint(x
, y
);
2741 if ( wParam
& MK_CONTROL
)
2743 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2745 m_htClickedItem
.Unset();
2749 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2751 changingEvent
.m_itemOld
= htOldItem
;
2753 if ( IsTreeEventAllowed(changingEvent
) )
2755 // toggle selected state
2756 DoToggleItemSelection(wxTreeItemId(htItem
));
2758 SetFocusedItem(wxTreeItemId(htItem
));
2760 // reset on any click without Shift
2761 m_htSelStart
.Unset();
2763 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2765 changedEvent
.m_itemOld
= htOldItem
;
2766 (void)HandleTreeEvent(changedEvent
);
2769 else if ( wParam
& MK_SHIFT
)
2771 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2773 m_htClickedItem
.Unset();
2778 bool willChange
= true;
2780 if ( !(wParam
& MK_CONTROL
) )
2782 srFlags
|= SR_UNSELECT_OTHERS
;
2785 if ( !m_htSelStart
)
2787 // take the focused item
2788 m_htSelStart
= htOldItem
;
2792 willChange
= SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2793 htItem
, srFlags
| SR_SIMULATE
);
2798 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2800 changingEvent
.m_itemOld
= htOldItem
;
2802 if ( IsTreeEventAllowed(changingEvent
) )
2804 // this selects all items between the starting one
2808 SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2813 DoSelectItem(wxTreeItemId(htItem
));
2816 SetFocusedItem(wxTreeItemId(htItem
));
2818 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2820 changedEvent
.m_itemOld
= htOldItem
;
2821 (void)HandleTreeEvent(changedEvent
);
2825 else // normal click
2827 // avoid doing anything if we click on the only
2828 // currently selected item
2830 wxArrayTreeItemIds selections
;
2831 size_t count
= GetSelections(selections
);
2835 HITEM(selections
[0]) != htItem
)
2837 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2839 m_htClickedItem
.Unset();
2843 // clear the previously selected items, if the user
2844 // clicked outside of the present selection, otherwise,
2845 // perform the deselection on mouse-up, this allows
2846 // multiple drag and drop to work.
2847 if ( !IsItemSelected(GetHwnd(), htItem
))
2849 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2851 changingEvent
.m_itemOld
= htOldItem
;
2853 if ( IsTreeEventAllowed(changingEvent
) )
2856 DoSelectItem(wxTreeItemId(htItem
));
2857 SetFocusedItem(wxTreeItemId(htItem
));
2859 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2861 changedEvent
.m_itemOld
= htOldItem
;
2862 (void)HandleTreeEvent(changedEvent
);
2867 SetFocusedItem(wxTreeItemId(htItem
));
2870 else // click on a single selected item
2872 // don't interfere with the default processing in
2873 // WM_MOUSEMOVE handler below as the default window
2874 // proc will start the drag itself if we let have
2876 m_htClickedItem
.Unset();
2878 // prevent in-place editing from starting if focus lost
2879 // since previous click
2883 DoSelectItem(wxTreeItemId(htItem
));
2884 SetFocusedItem(wxTreeItemId(htItem
));
2892 // reset on any click without Shift
2893 m_htSelStart
.Unset();
2896 m_focusLost
= false;
2898 // we consumed the event so we need to trigger state image
2903 wxTreeItemId item
= HitTest(wxPoint(x
, y
), htFlags
);
2905 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2907 m_triggerStateImageClick
= true;
2912 case WM_RBUTTONDOWN
:
2919 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) || !htItem
)
2924 // default handler removes the highlight from the currently
2925 // focused item when right mouse button is pressed on another
2926 // one but keeps the remaining items highlighted, which is
2927 // confusing, so override this default behaviour
2928 if ( !IsItemSelected(GetHwnd(), htItem
) )
2930 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2932 changingEvent
.m_itemOld
= htOldItem
;
2934 if ( IsTreeEventAllowed(changingEvent
) )
2937 DoSelectItem(wxTreeItemId(htItem
));
2938 SetFocusedItem(wxTreeItemId(htItem
));
2940 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2942 changedEvent
.m_itemOld
= htOldItem
;
2943 (void)HandleTreeEvent(changedEvent
);
2951 if ( m_htClickedItem
)
2953 int cx
= abs(m_ptClick
.x
- x
);
2954 int cy
= abs(m_ptClick
.y
- y
);
2956 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2957 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2962 tv
.hdr
.hwndFrom
= GetHwnd();
2963 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2964 tv
.hdr
.code
= TVN_BEGINDRAG
;
2966 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2970 wxZeroMemory(tviAux
);
2972 tviAux
.hItem
= HITEM(m_htClickedItem
);
2973 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2974 tviAux
.stateMask
= 0xffffffff;
2975 TreeView_GetItem(GetHwnd(), &tviAux
);
2977 tv
.itemNew
.state
= tviAux
.state
;
2978 tv
.itemNew
.lParam
= tviAux
.lParam
;
2983 // do it before SendMessage() call below to avoid
2984 // reentrancies here if there is another WM_MOUSEMOVE
2985 // in the queue already
2986 m_htClickedItem
.Unset();
2988 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
2989 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2991 // don't pass it to the default window proc, it would
2992 // start dragging again
2996 #endif // __WXWINCE__
3001 m_dragImage
->Move(wxPoint(x
, y
));
3004 // highlight the item as target (hiding drag image is
3005 // necessary - otherwise the display will be corrupted)
3006 m_dragImage
->Hide();
3007 TreeView_SelectDropTarget(GetHwnd(), htItem
);
3008 m_dragImage
->Show();
3011 #endif // wxUSE_DRAGIMAGE
3017 // deselect other items if multiple items selected
3020 wxArrayTreeItemIds selections
;
3021 size_t count
= GetSelections(selections
);
3024 !(wParam
& MK_CONTROL
) &&
3025 !(wParam
& MK_SHIFT
) )
3027 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
3029 changingEvent
.m_itemOld
= htOldItem
;
3031 if ( IsTreeEventAllowed(changingEvent
) )
3034 DoSelectItem(wxTreeItemId(htItem
));
3035 SetFocusedItem(wxTreeItemId(htItem
));
3037 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
3039 changedEvent
.m_itemOld
= htOldItem
;
3040 (void)HandleTreeEvent(changedEvent
);
3045 m_htClickedItem
.Unset();
3047 if ( m_triggerStateImageClick
)
3049 if ( tvht
.flags
& TVHT_ONITEMSTATEICON
)
3051 wxTreeEvent
event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
,
3053 (void)HandleTreeEvent(event
);
3055 m_triggerStateImageClick
= false;
3060 if ( !m_dragStarted
&&
3061 (tvht
.flags
& TVHT_ONITEMSTATEICON
||
3062 tvht
.flags
& TVHT_ONITEMICON
||
3063 tvht
.flags
& TVHT_ONITEM
) )
3075 m_dragImage
->EndDrag();
3079 // generate the drag end event
3080 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
,
3082 event
.m_pointDrag
= wxPoint(x
, y
);
3083 (void)HandleTreeEvent(event
);
3085 // if we don't do it, the tree seems to think that 2 items
3086 // are selected simultaneously which is quite weird
3087 TreeView_SelectDropTarget(GetHwnd(), 0);
3089 #endif // wxUSE_DRAGIMAGE
3091 if ( isMultiple
&& nMsg
== WM_RBUTTONUP
)
3095 nmhdr
.hwndFrom
= GetHwnd();
3096 nmhdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
3097 nmhdr
.code
= NM_RCLICK
;
3098 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY
,
3099 nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
3103 m_dragStarted
= false;
3108 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) )
3112 // the tree control greys out the selected item when it loses focus
3113 // and paints it as selected again when it regains it, but it won't
3114 // do it for the other items itself - help it
3115 wxArrayTreeItemIds selections
;
3116 size_t count
= GetSelections(selections
);
3119 for ( size_t n
= 0; n
< count
; n
++ )
3121 // TreeView_GetItemRect() will return false if item is not
3122 // visible, which may happen perfectly well
3123 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
3126 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
3131 if ( nMsg
== WM_KILLFOCUS
)
3136 else if ( (nMsg
== WM_KEYDOWN
|| nMsg
== WM_SYSKEYDOWN
) && isMultiple
)
3138 // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
3139 // notification but for the keys which can be used to change selection
3140 // we need to do it from here so as to not apply the default behaviour
3141 // if the events are handled by the user code
3154 if ( !MSWHandleTreeKeyDownEvent(wParam
, lParam
) )
3156 // use the key to update the selection if it was left
3158 MSWHandleSelectionKey(wParam
);
3161 // pretend that we did process it in any case as we already
3162 // generated an event for it
3165 //default: for all the other keys leave processed as false so that
3166 // the tree control generates a TVN_KEYDOWN for us
3170 else if ( nMsg
== WM_COMMAND
)
3172 // if we receive a EN_KILLFOCUS command from the in-place edit control
3173 // used for label editing, make sure to end editing
3176 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
3178 if ( cmd
== EN_KILLFOCUS
)
3180 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
3190 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
3196 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
3198 if ( nMsg
== WM_CHAR
)
3200 // don't let the control process Space and Return keys because it
3201 // doesn't do anything useful with them anyhow but always beeps
3202 // annoyingly when it receives them and there is no way to turn it off
3203 // simply if you just process TREEITEM_ACTIVATED event to which Space
3204 // and Enter presses are mapped in your code
3205 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
3209 else if ( nMsg
== WM_KEYDOWN
)
3211 if ( wParam
== VK_ESCAPE
)
3215 m_dragImage
->EndDrag();
3219 // if we don't do it, the tree seems to think that 2 items
3220 // are selected simultaneously which is quite weird
3221 TreeView_SelectDropTarget(GetHwnd(), 0);
3225 #endif // wxUSE_DRAGIMAGE
3227 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
3230 // process WM_NOTIFY Windows message
3231 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
3233 wxTreeEvent
event(wxEVT_NULL
, this);
3234 wxEventType eventType
= wxEVT_NULL
;
3235 NMHDR
*hdr
= (NMHDR
*)lParam
;
3237 switch ( hdr
->code
)
3240 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3243 case TVN_BEGINRDRAG
:
3245 if ( eventType
== wxEVT_NULL
)
3246 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
3247 //else: left drag, already set above
3249 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3251 event
.m_item
= tv
->itemNew
.hItem
;
3252 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
3254 // don't allow dragging by default: the user code must
3255 // explicitly say that it wants to allow it to avoid breaking
3261 case TVN_BEGINLABELEDIT
:
3263 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
3264 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3266 // although the user event handler may still veto it, it is
3267 // important to set it now so that calls to SetItemText() from
3268 // the event handler would change the text controls contents
3270 event
.m_item
= info
->item
.hItem
;
3271 event
.m_label
= info
->item
.pszText
;
3272 event
.m_editCancelled
= false;
3276 case TVN_DELETEITEM
:
3278 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
3279 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3281 event
.m_item
= tv
->itemOld
.hItem
;
3285 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
3286 if ( it
!= m_attrs
.end() )
3295 case TVN_ENDLABELEDIT
:
3297 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
3298 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3300 event
.m_item
= info
->item
.hItem
;
3301 event
.m_label
= info
->item
.pszText
;
3302 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
3307 // These *must* not be removed or TVN_GETINFOTIP will
3308 // not be processed each time the mouse is moved
3309 // and the tooltip will only ever update once.
3318 #ifdef TVN_GETINFOTIP
3319 case TVN_GETINFOTIP
:
3321 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
3322 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
3324 // Which item are we trying to get a tooltip for?
3325 event
.m_item
= info
->hItem
;
3329 #endif // TVN_GETINFOTIP
3330 #endif // !__WXWINCE__
3332 case TVN_GETDISPINFO
:
3333 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
3336 case TVN_SETDISPINFO
:
3338 if ( eventType
== wxEVT_NULL
)
3339 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
3340 //else: get, already set above
3342 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3344 event
.m_item
= info
->item
.hItem
;
3348 case TVN_ITEMEXPANDING
:
3349 case TVN_ITEMEXPANDED
:
3351 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3354 switch ( tv
->action
)
3357 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
3365 what
= IDX_COLLAPSE
;
3369 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
3372 eventType
= gs_expandEvents
[what
][how
];
3374 event
.m_item
= tv
->itemNew
.hItem
;
3380 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
3382 // fabricate the lParam and wParam parameters sufficiently
3383 // similar to the ones from a "real" WM_KEYDOWN so that
3384 // CreateKeyEvent() works correctly
3385 return MSWHandleTreeKeyDownEvent(
3386 info
->wVKey
, (wxIsAltDown() ? KF_ALTDOWN
: 0) << 16);
3390 // Vista's tree control has introduced some problems with our
3391 // multi-selection tree. When TreeView_SelectItem() is called,
3392 // the wrong items are deselected.
3394 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3395 // that can be used to regulate this incorrect behavior. The
3396 // following messages will allow only the unlocked item's selection
3399 case TVN_ITEMCHANGINGA
:
3400 case TVN_ITEMCHANGINGW
:
3402 // we only need to handles these in multi-select trees
3403 if ( HasFlag(wxTR_MULTIPLE
) )
3405 // get info about the item about to be changed
3406 NMTVITEMCHANGE
* info
= (NMTVITEMCHANGE
*)lParam
;
3407 if (TreeItemUnlocker::IsLocked(info
->hItem
))
3409 // item's state is locked, don't allow the change
3410 // returning 1 will disallow the change
3416 // allow the state change
3420 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
3421 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3422 // we have to handle both messages:
3423 case TVN_SELCHANGEDA
:
3424 case TVN_SELCHANGEDW
:
3425 if ( !HasFlag(wxTR_MULTIPLE
) || !m_changingSelection
)
3427 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
3431 case TVN_SELCHANGINGA
:
3432 case TVN_SELCHANGINGW
:
3433 if ( !HasFlag(wxTR_MULTIPLE
) || !m_changingSelection
)
3435 if ( eventType
== wxEVT_NULL
)
3436 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
3437 //else: already set above
3439 if (hdr
->code
== TVN_SELCHANGINGW
||
3440 hdr
->code
== TVN_SELCHANGEDW
)
3442 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
3443 event
.m_item
= tv
->itemNew
.hItem
;
3444 event
.m_itemOld
= tv
->itemOld
.hItem
;
3448 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
3449 event
.m_item
= tv
->itemNew
.hItem
;
3450 event
.m_itemOld
= tv
->itemOld
.hItem
;
3454 // we receive this message from WM_LBUTTONDOWN handler inside
3455 // comctl32.dll and so before the click is passed to
3456 // DefWindowProc() which sets the focus to the window which was
3457 // clicked and this can lead to unexpected event sequences: for
3458 // example, we may get a "selection change" event from the tree
3459 // before getting a "kill focus" event for the text control which
3460 // had the focus previously, thus breaking user code doing input
3463 // to avoid such surprises, we force the generation of focus events
3464 // now, before we generate the selection change ones
3468 // instead of explicitly checking for _WIN32_IE, check if the
3469 // required symbols are available in the headers
3470 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
3473 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
3474 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
3475 switch ( nmcd
.dwDrawStage
)
3478 // if we've got any items with non standard attributes,
3479 // notify us before painting each item
3480 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
3483 // windows in TreeCtrl use one-based index for item state images,
3484 // 0 indexed image is not being used, we're using zero-based index,
3485 // so we have to add temp image (of zero index) to state image list
3486 // before we draw any item, then after items are drawn we have to
3487 // delete it (in POSTPAINT notify)
3488 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3490 typedef BOOL (wxSTDCALL
*ImageList_Copy_t
)
3491 (HIMAGELIST
, int, HIMAGELIST
, int, UINT
);
3492 static ImageList_Copy_t s_pfnImageList_Copy
= NULL
;
3493 static bool loaded
= false;
3497 wxLoadedDLL
dllComCtl32(_T("comctl32.dll"));
3498 if ( dllComCtl32
.IsLoaded() )
3499 wxDL_INIT_FUNC(s_pfn
, ImageList_Copy
, dllComCtl32
);
3502 if ( !s_pfnImageList_Copy
)
3504 // this code is broken with ImageList_Copy()
3505 // but I don't care enough about Win95 support
3506 // to write it now -- if anybody does, please
3508 wxFAIL_MSG("TODO: implement this for Win95");
3513 hImageList
= GetHimagelistOf(m_imageListState
);
3515 // add temporary image
3517 m_imageListState
->GetSize(0, width
, height
);
3519 HBITMAP hbmpTemp
= ::CreateBitmap(width
, height
, 1, 1, NULL
);
3520 int index
= ::ImageList_Add(hImageList
, hbmpTemp
, hbmpTemp
);
3521 ::DeleteObject(hbmpTemp
);
3525 // move images to right
3526 for ( int i
= index
; i
> 0; i
-- )
3528 (*s_pfnImageList_Copy
)(hImageList
, i
,
3533 // we must remove the image in POSTPAINT notify
3534 *result
|= CDRF_NOTIFYPOSTPAINT
;
3539 case CDDS_POSTPAINT
:
3540 // we are deleting temp image of 0 index, which was
3541 // added before items were drawn (in PREPAINT notify)
3542 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3543 m_imageListState
->Remove(0);
3546 case CDDS_ITEMPREPAINT
:
3548 wxMapTreeAttr::iterator
3549 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
3551 if ( it
== m_attrs
.end() )
3553 // nothing to do for this item
3554 *result
= CDRF_DODEFAULT
;
3558 wxTreeItemAttr
* const attr
= it
->second
;
3560 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
3561 TVIF_STATE
, TVIS_DROPHILITED
);
3563 const UINT tvItemState
= tvItem
.state
;
3565 // selection colours should override ours,
3566 // otherwise it is too confusing to the user
3567 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
3568 !(tvItemState
& TVIS_DROPHILITED
) )
3571 if ( attr
->HasBackgroundColour() )
3573 colBack
= attr
->GetBackgroundColour();
3574 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
3578 // but we still want to keep the special foreground
3579 // colour when we don't have focus (we can't keep
3580 // it when we do, it would usually be unreadable on
3581 // the almost inverted bg colour...)
3582 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
3583 FindFocus() != this ) &&
3584 !(tvItemState
& TVIS_DROPHILITED
) )
3587 if ( attr
->HasTextColour() )
3589 colText
= attr
->GetTextColour();
3590 lptvcd
->clrText
= wxColourToRGB(colText
);
3594 if ( attr
->HasFont() )
3596 HFONT hFont
= GetHfontOf(attr
->GetFont());
3598 ::SelectObject(nmcd
.hdc
, hFont
);
3600 *result
= CDRF_NEWFONT
;
3602 else // no specific font
3604 *result
= CDRF_DODEFAULT
;
3610 *result
= CDRF_DODEFAULT
;
3614 // we always process it
3616 #endif // have owner drawn support in headers
3620 DWORD pos
= GetMessagePos();
3622 point
.x
= LOWORD(pos
);
3623 point
.y
= HIWORD(pos
);
3624 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
3626 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), htFlags
);
3628 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3630 event
.m_item
= item
;
3631 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
3640 TV_HITTESTINFO tvhti
;
3641 ::GetCursorPos(&tvhti
.pt
);
3642 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
3643 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
3645 if ( tvhti
.flags
& TVHT_ONITEM
)
3647 event
.m_item
= tvhti
.hItem
;
3648 eventType
= (int)hdr
->code
== NM_DBLCLK
3649 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
3650 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
3652 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
3653 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
3662 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
3665 event
.SetEventType(eventType
);
3667 bool processed
= HandleTreeEvent(event
);
3670 switch ( hdr
->code
)
3673 // we translate NM_DBLCLK into ACTIVATED event and if the user
3674 // handled the activation of the item we shouldn't proceed with
3675 // also using the same double click for toggling the item expanded
3676 // state -- but OTOH do let the user to expand/collapse the item by
3677 // double clicking on it if the activation is not handled specially
3678 *result
= processed
;
3682 // prevent tree control from sending WM_CONTEXTMENU to our parent
3683 // (which it does if NM_RCLICK is not handled) because we want to
3684 // send it to the control itself
3688 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
3689 (WPARAM
)GetHwnd(), ::GetMessagePos());
3693 case TVN_BEGINRDRAG
:
3695 if ( event
.IsAllowed() )
3697 // normally this is impossible because the m_dragImage is
3698 // deleted once the drag operation is over
3699 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
3701 m_dragImage
= new wxDragImage(*this, event
.m_item
);
3702 m_dragImage
->BeginDrag(wxPoint(0,0), this);
3703 m_dragImage
->Show();
3705 m_dragStarted
= true;
3707 #endif // wxUSE_DRAGIMAGE
3710 case TVN_DELETEITEM
:
3712 // NB: we might process this message using wxWidgets event
3713 // tables, but due to overhead of wxWin event system we
3714 // prefer to do it here ourself (otherwise deleting a tree
3715 // with many items is just too slow)
3716 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3718 wxTreeItemParam
*param
=
3719 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
3722 processed
= true; // Make sure we don't get called twice
3726 case TVN_BEGINLABELEDIT
:
3727 // return true to cancel label editing
3728 *result
= !event
.IsAllowed();
3730 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3731 if ( event
.IsAllowed() )
3733 HWND hText
= TreeView_GetEditControl(GetHwnd());
3736 // MBN: if m_textCtrl already has an HWND, it is a stale
3737 // pointer from a previous edit (because the user
3738 // didn't modify the label before dismissing the control,
3739 // and TVN_ENDLABELEDIT was not sent), so delete it
3740 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
3743 m_textCtrl
= new wxTextCtrl();
3744 m_textCtrl
->SetParent(this);
3745 m_textCtrl
->SetHWND((WXHWND
)hText
);
3746 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3748 // set wxTE_PROCESS_ENTER style for the text control to
3749 // force it to process the Enter presses itself, otherwise
3750 // they could be stolen from it by the dialog
3752 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3753 | wxTE_PROCESS_ENTER
);
3756 else // we had set m_idEdited before
3762 case TVN_ENDLABELEDIT
:
3763 // return true to set the label to the new string: note that we
3764 // also must pretend that we did process the message or it is going
3765 // to be passed to DefWindowProc() which will happily return false
3766 // cancelling the label change
3767 *result
= event
.IsAllowed();
3770 // ensure that we don't have the text ctrl which is going to be
3776 #ifdef TVN_GETINFOTIP
3777 case TVN_GETINFOTIP
:
3779 // If the user permitted a tooltip change, change it
3780 if (event
.IsAllowed())
3782 SetToolTip(event
.m_label
);
3789 case TVN_SELCHANGING
:
3790 case TVN_ITEMEXPANDING
:
3791 // return true to prevent the action from happening
3792 *result
= !event
.IsAllowed();
3795 case TVN_ITEMEXPANDED
:
3797 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3798 const wxTreeItemId
id(tv
->itemNew
.hItem
);
3800 if ( tv
->action
== TVE_COLLAPSE
)
3802 if ( wxApp::GetComCtl32Version() >= 600 )
3804 // for some reason the item selection rectangle depends
3805 // on whether it is expanded or collapsed (at least
3806 // with comctl32.dll v6): it is wider (by 3 pixels) in
3807 // the expanded state, so when the item collapses and
3808 // then is deselected the rightmost 3 pixels of the
3809 // previously drawn selection are left on the screen
3811 // it's not clear if it's a bug in comctl32.dll or in
3812 // our code (because it does not happen in Explorer but
3813 // OTOH we don't do anything which could result in this
3814 // AFAICS) but we do need to work around it to avoid
3821 // the item is also not refreshed properly after expansion when
3822 // it has an image depending on the expanded/collapsed state:
3823 // again, it's not clear if the bug is in comctl32.dll or our
3825 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3834 case TVN_GETDISPINFO
:
3835 // NB: so far the user can't set the image himself anyhow, so do it
3836 // anyway - but this may change later
3837 //if ( /* !processed && */ )
3839 wxTreeItemId item
= event
.m_item
;
3840 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3842 const wxTreeItemParam
* const param
= GetItemParam(item
);
3846 if ( info
->item
.mask
& TVIF_IMAGE
)
3851 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3852 : wxTreeItemIcon_Normal
3855 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3857 info
->item
.iSelectedImage
=
3860 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3861 : wxTreeItemIcon_Selected
3868 // for the other messages the return value is ignored and there is
3869 // nothing special to do
3874 // ----------------------------------------------------------------------------
3876 // ----------------------------------------------------------------------------
3878 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3879 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3881 int wxTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
3883 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
3885 // receive the desired information
3886 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3889 // state images are one-based
3890 return STATEIMAGEMASKTOINDEX(tvItem
.state
) - 1;
3893 void wxTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
3895 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
3897 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3899 // state images are one-based
3900 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3901 tvItem
.state
= INDEXTOSTATEIMAGEMASK(state
+ 1);
3906 #endif // wxUSE_TREECTRL