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 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 // wrappers for TreeView_GetItem/TreeView_SetItem
114 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
117 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
118 tvi
.stateMask
= TVIS_SELECTED
;
121 TreeItemUnlocker
unlocker(hItem
);
123 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
125 wxLogLastError(wxT("TreeView_GetItem"));
128 return (tvi
.state
& TVIS_SELECTED
) != 0;
131 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
134 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
135 tvi
.stateMask
= TVIS_SELECTED
;
136 tvi
.state
= select
? TVIS_SELECTED
: 0;
139 TreeItemUnlocker
unlocker(hItem
);
141 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
143 wxLogLastError(wxT("TreeView_SetItem"));
150 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
152 SelectItem(hwndTV
, htItem
, false);
155 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
157 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
160 // helper function which selects all items in a range and, optionally,
161 // deselects all the other ones
163 // returns true if the selection changed at all or false if nothing changed
165 // flags for SelectRange()
168 SR_SIMULATE
= 1, // don't do anything, just return true or false
169 SR_UNSELECT_OTHERS
= 2 // deselect the items not in range
172 static bool SelectRange(HWND hwndTV
,
177 // find the first (or last) item and select it
178 bool changed
= false;
180 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
182 while ( htItem
&& cont
)
184 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
186 if ( !IsItemSelected(hwndTV
, htItem
) )
188 if ( !(flags
& SR_SIMULATE
) )
190 SelectItem(hwndTV
, htItem
);
198 else // not first or last
200 if ( flags
& SR_UNSELECT_OTHERS
)
202 if ( IsItemSelected(hwndTV
, htItem
) )
204 if ( !(flags
& SR_SIMULATE
) )
205 UnselectItem(hwndTV
, htItem
);
212 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
215 // select the items in range
216 cont
= htFirst
!= htLast
;
217 while ( htItem
&& cont
)
219 if ( !IsItemSelected(hwndTV
, htItem
) )
221 if ( !(flags
& SR_SIMULATE
) )
223 SelectItem(hwndTV
, htItem
);
229 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
231 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
234 // optionally deselect the rest
235 if ( flags
& SR_UNSELECT_OTHERS
)
239 if ( IsItemSelected(hwndTV
, htItem
) )
241 if ( !(flags
& SR_SIMULATE
) )
243 UnselectItem(hwndTV
, htItem
);
249 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
253 // seems to be necessary - otherwise the just selected items don't always
254 // appear as selected
255 if ( !(flags
& SR_SIMULATE
) )
257 UpdateWindow(hwndTV
);
263 // helper function which tricks the standard control into changing the focused
264 // item without changing anything else (if someone knows why Microsoft doesn't
265 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
267 // returns true if the focus was changed, false if the given item was already
269 static bool SetFocus(HWND hwndTV
, HTREEITEM htItem
)
272 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
274 if ( htItem
== htFocus
)
279 // remember the selection state of the item
280 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
282 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
284 // prevent the tree from unselecting the old focus which it
285 // would do by default (TreeView_SelectItem unselects the
287 TreeView_SelectItem(hwndTV
, 0);
288 SelectItem(hwndTV
, htFocus
);
291 TreeView_SelectItem(hwndTV
, htItem
);
295 // need to clear the selection which TreeView_SelectItem() gave
297 UnselectItem(hwndTV
, htItem
);
299 //else: was selected, still selected - ok
303 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
305 // just clear the focus
306 TreeView_SelectItem(hwndTV
, 0);
308 if ( wasFocusSelected
)
310 // restore the selection state
311 SelectItem(hwndTV
, htFocus
);
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 // a convenient wrapper around TV_ITEM struct which adds a ctor
324 #pragma warning( disable : 4097 ) // inheriting from typedef
327 struct wxTreeViewItem
: public TV_ITEM
329 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
330 UINT mask_
, // fields which are valid
331 UINT stateMask_
= 0) // for TVIF_STATE only
335 // hItem member is always valid
336 mask
= mask_
| TVIF_HANDLE
;
337 stateMask
= stateMask_
;
342 // ----------------------------------------------------------------------------
343 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
345 // We need this for a couple of reasons:
347 // 1) This class is needed for support of different images: the Win32 common
348 // control natively supports only 2 images (the normal one and another for the
349 // selected state). We wish to provide support for 2 more of them for folder
350 // items (i.e. those which have children): for expanded state and for expanded
351 // selected state. For this we use this structure to store the additional items
354 // 2) This class is also needed to hold the HITEM so that we can sort
355 // it correctly in the MSW sort callback.
357 // In addition it makes other workarounds such as this easier and helps
358 // simplify the code.
359 // ----------------------------------------------------------------------------
361 class wxTreeItemParam
368 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
374 // dtor deletes the associated data as well
375 virtual ~wxTreeItemParam() { delete m_data
; }
378 // get the real data associated with the item
379 wxTreeItemData
*GetData() const { return m_data
; }
381 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
383 // do we have such image?
384 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
385 // get image, falling back to the other images if this one is not
387 int GetImage(wxTreeItemIcon which
) const
389 int image
= m_images
[which
];
394 case wxTreeItemIcon_SelectedExpanded
:
395 image
= GetImage(wxTreeItemIcon_Expanded
);
400 case wxTreeItemIcon_Selected
:
401 case wxTreeItemIcon_Expanded
:
402 image
= GetImage(wxTreeItemIcon_Normal
);
405 case wxTreeItemIcon_Normal
:
410 wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") );
416 // change the given image
417 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
420 const wxTreeItemId
& GetItem() const { return m_item
; }
422 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
425 // all the images associated with the item
426 int m_images
[wxTreeItemIcon_Max
];
428 // item for sort callbacks
431 // the real client data
432 wxTreeItemData
*m_data
;
434 wxDECLARE_NO_COPY_CLASS(wxTreeItemParam
);
437 // wxVirutalNode is used in place of a single root when 'hidden' root is
439 class wxVirtualNode
: public wxTreeViewItem
442 wxVirtualNode(wxTreeItemParam
*param
)
443 : wxTreeViewItem(TVI_ROOT
, 0)
453 wxTreeItemParam
*GetParam() const { return m_param
; }
454 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
457 wxTreeItemParam
*m_param
;
459 wxDECLARE_NO_COPY_CLASS(wxVirtualNode
);
463 #pragma warning( default : 4097 )
466 // a macro to get the virtual root, returns NULL if none
467 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
469 // returns true if the item is the virtual root
470 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
472 // a class which encapsulates the tree traversal logic: it vists all (unless
473 // OnVisit() returns false) items under the given one
474 class wxTreeTraversal
477 wxTreeTraversal(const wxTreeCtrl
*tree
)
482 // give it a virtual dtor: not really needed as the class is never used
483 // polymorphically and not even allocated on heap at all, but this is safer
484 // (in case it ever is) and silences the compiler warnings for now
485 virtual ~wxTreeTraversal() { }
487 // do traverse the tree: visit all items (recursively by default) under the
488 // given one; return true if all items were traversed or false if the
489 // traversal was aborted because OnVisit returned false
490 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
492 // override this function to do whatever is needed for each item, return
493 // false to stop traversing
494 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
497 const wxTreeCtrl
*GetTree() const { return m_tree
; }
500 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
502 const wxTreeCtrl
*m_tree
;
504 wxDECLARE_NO_COPY_CLASS(wxTreeTraversal
);
507 // internal class for getting the selected items
508 class TraverseSelections
: public wxTreeTraversal
511 TraverseSelections(const wxTreeCtrl
*tree
,
512 wxArrayTreeItemIds
& selections
)
513 : wxTreeTraversal(tree
), m_selections(selections
)
515 m_selections
.Empty();
517 if (tree
->GetCount() > 0)
518 DoTraverse(tree
->GetRootItem());
521 virtual bool OnVisit(const wxTreeItemId
& item
)
523 const wxTreeCtrl
* const tree
= GetTree();
525 // can't visit a virtual node.
526 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
531 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
533 m_selections
.Add(item
);
539 size_t GetCount() const { return m_selections
.GetCount(); }
542 wxArrayTreeItemIds
& m_selections
;
544 wxDECLARE_NO_COPY_CLASS(TraverseSelections
);
547 // internal class for counting tree items
548 class TraverseCounter
: public wxTreeTraversal
551 TraverseCounter(const wxTreeCtrl
*tree
,
552 const wxTreeItemId
& root
,
554 : wxTreeTraversal(tree
)
558 DoTraverse(root
, recursively
);
561 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
568 size_t GetCount() const { return m_count
; }
573 wxDECLARE_NO_COPY_CLASS(TraverseCounter
);
576 // ----------------------------------------------------------------------------
578 // ----------------------------------------------------------------------------
580 #if wxUSE_EXTENDED_RTTI
581 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
583 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
584 // new style border flags, we put them first to
585 // use them for streaming out
586 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
587 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
588 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
589 wxFLAGS_MEMBER(wxBORDER_RAISED
)
590 wxFLAGS_MEMBER(wxBORDER_STATIC
)
591 wxFLAGS_MEMBER(wxBORDER_NONE
)
593 // old style border flags
594 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
595 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
596 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
597 wxFLAGS_MEMBER(wxRAISED_BORDER
)
598 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
599 wxFLAGS_MEMBER(wxBORDER
)
601 // standard window styles
602 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
603 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
604 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
605 wxFLAGS_MEMBER(wxWANTS_CHARS
)
606 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
607 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
608 wxFLAGS_MEMBER(wxVSCROLL
)
609 wxFLAGS_MEMBER(wxHSCROLL
)
611 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
612 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
613 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
614 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
615 wxFLAGS_MEMBER(wxTR_NO_LINES
)
616 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
617 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
618 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
619 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
620 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
621 wxFLAGS_MEMBER(wxTR_SINGLE
)
622 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
623 #if WXWIN_COMPATIBILITY_2_8
624 wxFLAGS_MEMBER(wxTR_EXTENDED
)
626 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
628 wxEND_FLAGS( wxTreeCtrlStyle
)
630 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
632 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
633 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
634 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
635 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
636 wxEND_PROPERTIES_TABLE()
638 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
639 wxEND_HANDLERS_TABLE()
641 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
643 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
646 // ----------------------------------------------------------------------------
648 // ----------------------------------------------------------------------------
650 // indices in gs_expandEvents table below
665 // handy table for sending events - it has to be initialized during run-time
666 // now so can't be const any more
667 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
670 but logically it's a const table with the following entries:
673 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
674 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
678 // ============================================================================
680 // ============================================================================
682 // ----------------------------------------------------------------------------
684 // ----------------------------------------------------------------------------
686 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
688 if ( !OnVisit(root
) )
691 return Traverse(root
, recursively
);
694 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
696 wxTreeItemIdValue cookie
;
697 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
698 while ( child
.IsOk() )
700 // depth first traversal
701 if ( recursively
&& !Traverse(child
, true) )
704 if ( !OnVisit(child
) )
707 child
= m_tree
->GetNextChild(root
, cookie
);
713 // ----------------------------------------------------------------------------
714 // construction and destruction
715 // ----------------------------------------------------------------------------
717 void wxTreeCtrl::Init()
720 m_hasAnyAttr
= false;
724 m_pVirtualRoot
= NULL
;
725 m_dragStarted
= false;
727 m_triggerStateImageClick
= false;
729 // initialize the global array of events now as it can't be done statically
730 // with the wxEVT_XXX values being allocated during run-time only
731 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
732 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
733 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
734 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
737 bool wxTreeCtrl::Create(wxWindow
*parent
,
742 const wxValidator
& validator
,
743 const wxString
& name
)
747 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
748 style
|= wxBORDER_SUNKEN
;
750 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
754 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
755 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
757 if ( !(m_windowStyle
& wxTR_NO_LINES
) )
758 wstyle
|= TVS_HASLINES
;
759 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
760 wstyle
|= TVS_HASBUTTONS
;
762 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
763 wstyle
|= TVS_EDITLABELS
;
765 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
766 wstyle
|= TVS_LINESATROOT
;
768 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
770 if ( wxApp::GetComCtl32Version() >= 471 )
771 wstyle
|= TVS_FULLROWSELECT
;
774 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
775 // Need so that TVN_GETINFOTIP messages will be sent
776 wstyle
|= TVS_INFOTIP
;
779 // Create the tree control.
780 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
783 #if wxUSE_COMCTL32_SAFELY
784 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
785 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
787 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
788 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
790 // This works around a bug in the Windows tree control whereby for some versions
791 // of comctrl32, setting any colour actually draws the background in black.
792 // This will initialise the background to the system colour.
793 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
794 // Assume the user has an updated comctl32.dll.
795 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
796 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
797 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
800 wxSetCCUnicodeFormat(GetHwnd());
805 wxTreeCtrl::~wxTreeCtrl()
807 // delete any attributes
810 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
812 // prevent TVN_DELETEITEM handler from deleting the attributes again!
813 m_hasAnyAttr
= false;
818 // delete user data to prevent memory leaks
819 // also deletes hidden root node storage.
823 // ----------------------------------------------------------------------------
825 // ----------------------------------------------------------------------------
827 /* static */ wxVisualAttributes
828 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
830 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
832 // common controls have their own default font
833 attrs
.font
= wxGetCCDefaultFont();
839 // simple wrappers which add error checking in debug mode
841 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
843 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
844 _T("can't retrieve virtual root item") );
846 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
848 wxLogLastError(wxT("TreeView_GetItem"));
856 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
858 TreeItemUnlocker
unlocker(tvItem
->hItem
);
860 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
862 wxLogLastError(wxT("TreeView_SetItem"));
866 unsigned int wxTreeCtrl::GetCount() const
868 return (unsigned int)TreeView_GetCount(GetHwnd());
871 unsigned int wxTreeCtrl::GetIndent() const
873 return TreeView_GetIndent(GetHwnd());
876 void wxTreeCtrl::SetIndent(unsigned int indent
)
878 TreeView_SetIndent(GetHwnd(), indent
);
881 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
884 (void) TreeView_SetImageList(GetHwnd(),
885 imageList
? imageList
->GetHIMAGELIST() : 0,
889 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
891 if (m_ownsImageListNormal
)
892 delete m_imageListNormal
;
894 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
895 m_ownsImageListNormal
= false;
898 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
900 if (m_ownsImageListState
) delete m_imageListState
;
901 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
902 m_ownsImageListState
= false;
905 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
906 bool recursively
) const
908 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
910 TraverseCounter
counter(this, item
, recursively
);
911 return counter
.GetCount() - 1;
914 // ----------------------------------------------------------------------------
916 // ----------------------------------------------------------------------------
918 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
920 #if !wxUSE_COMCTL32_SAFELY
921 if ( !wxWindowBase::SetBackgroundColour(colour
) )
924 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
930 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
932 #if !wxUSE_COMCTL32_SAFELY
933 if ( !wxWindowBase::SetForegroundColour(colour
) )
936 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
942 // ----------------------------------------------------------------------------
944 // ----------------------------------------------------------------------------
946 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
948 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
951 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
953 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
955 wxChar buf
[512]; // the size is arbitrary...
957 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
958 tvItem
.pszText
= buf
;
959 tvItem
.cchTextMax
= WXSIZEOF(buf
);
960 if ( !DoGetItem(&tvItem
) )
962 // don't return some garbage which was on stack, but an empty string
966 return wxString(buf
);
969 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
971 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
973 if ( IS_VIRTUAL_ROOT(item
) )
976 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
977 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
980 // when setting the text of the item being edited, the text control should
981 // be updated to reflect the new text as well, otherwise calling
982 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
984 // don't use GetEditControl() here because m_textCtrl is not set yet
985 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
988 if ( item
== m_idEdited
)
990 ::SetWindowText(hwndEdit
, text
.wx_str());
995 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
996 wxTreeItemIcon which
) const
998 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1000 if ( IsHiddenRoot(item
) )
1002 // no images for hidden root item
1006 wxTreeItemParam
*param
= GetItemParam(item
);
1008 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
1011 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1012 wxTreeItemIcon which
)
1014 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1015 wxCHECK_RET( which
>= 0 &&
1016 which
< wxTreeItemIcon_Max
,
1017 wxT("invalid image index"));
1020 if ( IsHiddenRoot(item
) )
1022 // no images for hidden root item
1026 wxTreeItemParam
*data
= GetItemParam(item
);
1030 data
->SetImage(image
, which
);
1035 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
1037 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1039 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1041 // hidden root may still have data.
1042 if ( IS_VIRTUAL_ROOT(item
) )
1044 return GET_VIRTUAL_ROOT()->GetParam();
1048 if ( !DoGetItem(&tvItem
) )
1053 return (wxTreeItemParam
*)tvItem
.lParam
;
1056 bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent
& event
) const
1058 if ( event
.m_item
.IsOk() )
1060 event
.SetClientObject(GetItemData(event
.m_item
));
1063 return HandleWindowEvent(event
);
1066 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1068 wxTreeItemParam
*data
= GetItemParam(item
);
1070 return data
? data
->GetData() : NULL
;
1073 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1075 // first, associate this piece of data with this item
1081 wxTreeItemParam
*param
= GetItemParam(item
);
1083 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1085 param
->SetData(data
);
1088 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1090 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1092 if ( IS_VIRTUAL_ROOT(item
) )
1095 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1096 tvItem
.cChildren
= (int)has
;
1100 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1102 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1104 if ( IS_VIRTUAL_ROOT(item
) )
1107 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1108 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1112 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1114 if ( IS_VIRTUAL_ROOT(item
) )
1117 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1118 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1122 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1124 if ( IS_VIRTUAL_ROOT(item
) )
1128 if ( GetBoundingRect(item
, rect
) )
1134 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1136 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1138 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1139 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1142 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1144 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1146 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1147 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1150 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1152 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1154 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1155 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1158 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1159 const wxColour
& col
)
1161 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1163 wxTreeItemAttr
*attr
;
1164 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1165 if ( it
== m_attrs
.end() )
1167 m_hasAnyAttr
= true;
1169 m_attrs
[item
.m_pItem
] =
1170 attr
= new wxTreeItemAttr
;
1177 attr
->SetTextColour(col
);
1182 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1183 const wxColour
& col
)
1185 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1187 wxTreeItemAttr
*attr
;
1188 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1189 if ( it
== m_attrs
.end() )
1191 m_hasAnyAttr
= true;
1193 m_attrs
[item
.m_pItem
] =
1194 attr
= new wxTreeItemAttr
;
1196 else // already in the hash
1201 attr
->SetBackgroundColour(col
);
1206 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1208 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1210 wxTreeItemAttr
*attr
;
1211 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1212 if ( it
== m_attrs
.end() )
1214 m_hasAnyAttr
= true;
1216 m_attrs
[item
.m_pItem
] =
1217 attr
= new wxTreeItemAttr
;
1219 else // already in the hash
1224 attr
->SetFont(font
);
1226 // Reset the item's text to ensure that the bounding rect will be adjusted
1227 // for the new font.
1228 SetItemText(item
, GetItemText(item
));
1233 // ----------------------------------------------------------------------------
1235 // ----------------------------------------------------------------------------
1237 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1239 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1241 if ( item
== wxTreeItemId(TVI_ROOT
) )
1243 // virtual (hidden) root is never visible
1247 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1250 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1251 // the HTREEITEM with TVM_GETITEMRECT
1252 *(HTREEITEM
*)&rect
= HITEM(item
);
1254 // true means to get rect for just the text, not the whole line
1255 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1257 // if TVM_GETITEMRECT returned false, then the item is definitely not
1258 // visible (because its parent is not expanded)
1262 // however if it returned true, the item might still be outside the
1263 // currently visible part of the tree, test for it (notice that partly
1264 // visible means visible here)
1265 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1268 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1270 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1272 if ( IS_VIRTUAL_ROOT(item
) )
1274 wxTreeItemIdValue cookie
;
1275 return GetFirstChild(item
, cookie
).IsOk();
1278 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1281 return tvItem
.cChildren
!= 0;
1284 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1286 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1288 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1291 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1294 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1296 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1298 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1301 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1304 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1306 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1308 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1311 return (tvItem
.state
& TVIS_BOLD
) != 0;
1314 // ----------------------------------------------------------------------------
1316 // ----------------------------------------------------------------------------
1318 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1320 // Root may be real (visible) or virtual (hidden).
1321 if ( GET_VIRTUAL_ROOT() )
1324 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1327 wxTreeItemId
wxTreeCtrl::GetSelection() const
1329 wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE
), wxTreeItemId(),
1330 wxT("this only works with single selection controls") );
1332 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1335 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1337 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1341 if ( IS_VIRTUAL_ROOT(item
) )
1343 // no parent for the virtual root
1348 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1349 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1351 // the top level items should have the virtual root as their parent
1356 return wxTreeItemId(hItem
);
1359 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1360 wxTreeItemIdValue
& cookie
) const
1362 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1364 // remember the last child returned in 'cookie'
1365 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1367 return wxTreeItemId(cookie
);
1370 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1371 wxTreeItemIdValue
& cookie
) const
1373 wxTreeItemId
fromCookie(cookie
);
1375 HTREEITEM hitem
= HITEM(fromCookie
);
1377 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1379 wxTreeItemId
item(hitem
);
1381 cookie
= item
.m_pItem
;
1386 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1388 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1390 // can this be done more efficiently?
1391 wxTreeItemIdValue cookie
;
1393 wxTreeItemId childLast
,
1394 child
= GetFirstChild(item
, cookie
);
1395 while ( child
.IsOk() )
1398 child
= GetNextChild(item
, cookie
);
1404 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1406 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1407 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1410 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1412 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1413 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1416 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1418 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1421 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1423 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1424 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1426 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1427 if ( next
.IsOk() && !IsVisible(next
) )
1429 // Win32 considers that any non-collapsed item is visible while we want
1430 // to return only really visible items
1437 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1439 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1440 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1442 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1443 if ( prev
.IsOk() && !IsVisible(prev
) )
1445 // just as above, Win32 function will happily return the previous item
1446 // in the tree for the first visible item too
1453 // ----------------------------------------------------------------------------
1454 // multiple selections emulation
1455 // ----------------------------------------------------------------------------
1457 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1459 TraverseSelections
selector(this, selections
);
1461 return selector
.GetCount();
1464 // ----------------------------------------------------------------------------
1466 // ----------------------------------------------------------------------------
1468 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1469 const wxTreeItemId
& hInsertAfter
,
1470 const wxString
& text
,
1471 int image
, int selectedImage
,
1472 wxTreeItemData
*data
)
1474 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1476 _T("can't have more than one root in the tree") );
1478 TV_INSERTSTRUCT tvIns
;
1479 tvIns
.hParent
= HITEM(parent
);
1480 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1482 // this is how we insert the item as the first child: supply a NULL
1484 if ( !tvIns
.hInsertAfter
)
1486 tvIns
.hInsertAfter
= TVI_FIRST
;
1490 if ( !text
.empty() )
1493 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1497 tvIns
.item
.pszText
= NULL
;
1498 tvIns
.item
.cchTextMax
= 0;
1501 // create the param which will store the other item parameters
1502 wxTreeItemParam
*param
= new wxTreeItemParam
;
1504 // we return the images on demand as they depend on whether the item is
1505 // expanded or collapsed too in our case
1506 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1507 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1508 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1510 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1511 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1514 tvIns
.item
.lParam
= (LPARAM
)param
;
1515 tvIns
.item
.mask
= mask
;
1517 // don't use the hack below for the children of hidden root: this results
1518 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1519 const bool firstChild
= !IsHiddenRoot(parent
) &&
1520 !TreeView_GetChild(GetHwnd(), HITEM(parent
));
1522 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1525 wxLogLastError(wxT("TreeView_InsertItem"));
1528 // apparently some Windows versions (2000 and XP are reported to do this)
1529 // sometimes don't refresh the tree after adding the first child and so we
1530 // need this to make the "[+]" appear
1534 TreeView_GetItemRect(GetHwnd(), HITEM(parent
), &rect
, FALSE
);
1535 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1538 // associate the application tree item with Win32 tree item handle
1541 // setup wxTreeItemData
1544 param
->SetData(data
);
1548 return wxTreeItemId(id
);
1551 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1552 int image
, int selectedImage
,
1553 wxTreeItemData
*data
)
1555 if ( HasFlag(wxTR_HIDE_ROOT
) )
1557 wxASSERT_MSG( !m_pVirtualRoot
, _T("tree can have only a single root") );
1559 // create a virtual root item, the parent for all the others
1560 wxTreeItemParam
*param
= new wxTreeItemParam
;
1561 param
->SetData(data
);
1563 m_pVirtualRoot
= new wxVirtualNode(param
);
1568 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1569 text
, image
, selectedImage
, data
);
1572 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1574 const wxString
& text
,
1575 int image
, int selectedImage
,
1576 wxTreeItemData
*data
)
1578 wxTreeItemId idPrev
;
1579 if ( index
== (size_t)-1 )
1581 // special value: append to the end
1584 else // find the item from index
1586 wxTreeItemIdValue cookie
;
1587 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1588 while ( index
!= 0 && idCur
.IsOk() )
1593 idCur
= GetNextChild(parent
, cookie
);
1596 // assert, not check: if the index is invalid, we will append the item
1598 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1601 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1604 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1606 // unlock tree selections on vista, without this the
1607 // tree ctrl will eventually crash after item deletion
1608 TreeItemUnlocker unlock_all
;
1610 if ( HasFlag(wxTR_MULTIPLE
) )
1612 bool selected
= IsSelected(item
);
1617 next
= TreeView_GetNextVisible(GetHwnd(), HITEM(item
));
1621 next
= TreeView_GetPrevVisible(GetHwnd(), HITEM(item
));
1625 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1627 wxLogLastError(wxT("TreeView_DeleteItem"));
1638 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
1640 if ( IsTreeEventAllowed(changingEvent
) )
1642 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
1643 (void)HandleTreeEvent(changedEvent
);
1647 ::SelectItem(GetHwnd(), HITEM(next
), false);
1648 TreeView_SelectItem(GetHwnd(), 0);
1654 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1656 wxLogLastError(wxT("TreeView_DeleteItem"));
1661 // delete all children (but don't delete the item itself)
1662 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1664 // unlock tree selections on vista for the duration of this call
1665 TreeItemUnlocker unlock_all
;
1667 wxTreeItemIdValue cookie
;
1669 wxArrayTreeItemIds children
;
1670 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1671 while ( child
.IsOk() )
1673 children
.Add(child
);
1675 child
= GetNextChild(item
, cookie
);
1678 size_t nCount
= children
.Count();
1679 for ( size_t n
= 0; n
< nCount
; n
++ )
1681 Delete(children
[n
]);
1685 void wxTreeCtrl::DeleteAllItems()
1687 // unlock tree selections on vista for the duration of this call
1688 TreeItemUnlocker unlock_all
;
1690 // delete the "virtual" root item.
1691 if ( GET_VIRTUAL_ROOT() )
1693 delete GET_VIRTUAL_ROOT();
1694 m_pVirtualRoot
= NULL
;
1697 // and all the real items
1699 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1701 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1705 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1707 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1708 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1709 flag
== TVE_EXPAND
||
1711 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1713 // A hidden root can be neither expanded nor collapsed.
1714 wxCHECK_RET( !IsHiddenRoot(item
),
1715 wxT("Can't expand/collapse hidden root node!") );
1717 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1718 // emulate them. This behaviour has changed slightly with comctl32.dll
1719 // v 4.70 - now it does send them but only the first time. To maintain
1720 // compatible behaviour and also in order to not have surprises with the
1721 // future versions, don't rely on this and still do everything ourselves.
1722 // To avoid that the messages be sent twice when the item is expanded for
1723 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1725 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1729 if ( IsExpanded(item
) )
1731 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
,
1732 this, wxTreeItemId(item
));
1734 if ( !IsTreeEventAllowed(event
) )
1738 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) )
1740 if ( IsExpanded(item
) )
1743 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, this, item
);
1744 (void)HandleTreeEvent(event
);
1746 //else: change didn't took place, so do nothing at all
1749 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1751 DoExpand(item
, TVE_EXPAND
);
1754 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1756 DoExpand(item
, TVE_COLLAPSE
);
1759 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1761 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1764 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1766 DoExpand(item
, TVE_TOGGLE
);
1769 void wxTreeCtrl::Unselect()
1771 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE
),
1772 wxT("doesn't make sense, may be you want UnselectAll()?") );
1774 // the current focus
1775 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1782 if ( HasFlag(wxTR_MULTIPLE
) )
1784 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
1785 this, wxTreeItemId());
1786 changingEvent
.m_itemOld
= htFocus
;
1788 if ( IsTreeEventAllowed(changingEvent
) )
1790 TreeView_SelectItem(GetHwnd(), 0);
1792 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1793 this, wxTreeItemId());
1794 changedEvent
.m_itemOld
= htFocus
;
1795 (void)HandleTreeEvent(changedEvent
);
1800 TreeView_SelectItem(GetHwnd(), 0);
1804 void wxTreeCtrl::DoUnselectAll()
1806 wxArrayTreeItemIds selections
;
1807 size_t count
= GetSelections(selections
);
1809 for ( size_t n
= 0; n
< count
; n
++ )
1811 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1814 m_htSelStart
.Unset();
1817 void wxTreeCtrl::UnselectAll()
1819 if ( HasFlag(wxTR_MULTIPLE
) )
1821 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1822 if ( !htFocus
) return;
1824 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this);
1825 changingEvent
.m_itemOld
= htFocus
;
1827 if ( IsTreeEventAllowed(changingEvent
) )
1830 TreeView_SelectItem(GetHwnd(), 0);
1832 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this);
1833 changedEvent
.m_itemOld
= htFocus
;
1834 (void)HandleTreeEvent(changedEvent
);
1843 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1845 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't select hidden root item") );
1847 if ( IsSelected(item
) == select
)
1852 if ( HasFlag(wxTR_MULTIPLE
) )
1854 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1856 if ( IsTreeEventAllowed(changingEvent
) )
1858 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1859 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1863 ::SetFocus(GetHwnd(), HITEM(item
));
1866 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1868 (void)HandleTreeEvent(changedEvent
);
1873 wxASSERT_MSG( select
,
1874 _T("SelectItem(false) works only for multiselect") );
1876 // in spite of the docs (MSDN Jan 99 edition), we don't seem to receive
1877 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1878 // send them ourselves
1880 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1882 if ( IsTreeEventAllowed(changingEvent
) )
1884 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1886 wxLogLastError(wxT("TreeView_SelectItem"));
1890 ::SetFocus(GetHwnd(), HITEM(item
));
1892 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
1894 (void)HandleTreeEvent(changedEvent
);
1897 //else: program vetoed the change
1901 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1903 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't show hidden root item") );
1906 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1909 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1911 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1913 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1917 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1922 void wxTreeCtrl::DeleteTextCtrl()
1926 // the HWND corresponding to this control is deleted by the tree
1927 // control itself and we don't know when exactly this happens, so check
1928 // if the window still exists before calling UnsubclassWin()
1929 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1931 m_textCtrl
->SetHWND(0);
1934 m_textCtrl
->UnsubclassWin();
1935 m_textCtrl
->SetHWND(0);
1943 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1944 wxClassInfo
*textControlClass
)
1946 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1951 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1952 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1954 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1963 // textctrl is subclassed in MSWOnNotify
1967 // End label editing, optionally cancelling the edit
1968 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1970 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1975 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1977 TV_HITTESTINFO hitTestInfo
;
1978 hitTestInfo
.pt
.x
= (int)point
.x
;
1979 hitTestInfo
.pt
.y
= (int)point
.y
;
1981 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1986 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1987 flags |= wxTREE_HITTEST_##flag
1989 TRANSLATE_FLAG(ABOVE
);
1990 TRANSLATE_FLAG(BELOW
);
1991 TRANSLATE_FLAG(NOWHERE
);
1992 TRANSLATE_FLAG(ONITEMBUTTON
);
1993 TRANSLATE_FLAG(ONITEMICON
);
1994 TRANSLATE_FLAG(ONITEMINDENT
);
1995 TRANSLATE_FLAG(ONITEMLABEL
);
1996 TRANSLATE_FLAG(ONITEMRIGHT
);
1997 TRANSLATE_FLAG(ONITEMSTATEICON
);
1998 TRANSLATE_FLAG(TOLEFT
);
1999 TRANSLATE_FLAG(TORIGHT
);
2001 #undef TRANSLATE_FLAG
2003 return wxTreeItemId(hitTestInfo
.hItem
);
2006 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2008 bool textOnly
) const
2012 // Virtual root items have no bounding rectangle
2013 if ( IS_VIRTUAL_ROOT(item
) )
2018 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2021 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2027 // couldn't retrieve rect: for example, item isn't visible
2032 // ----------------------------------------------------------------------------
2034 // ----------------------------------------------------------------------------
2036 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2037 // functions such as IsDataIndirect()
2038 class wxTreeSortHelper
2041 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2044 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
2046 return ((wxTreeItemParam
*)lParam
)->GetItem();
2050 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2054 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2055 wxT("sorting tree without data doesn't make sense") );
2057 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2059 return tree
->OnCompareItems(GetIdFromData(pItem1
),
2060 GetIdFromData(pItem2
));
2063 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2065 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2067 // rely on the fact that TreeView_SortChildren does the same thing as our
2068 // default behaviour, i.e. sorts items alphabetically and so call it
2069 // directly if we're not in derived class (much more efficient!)
2070 // RN: Note that if you find you're code doesn't sort as expected this
2071 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
2072 // combo for your derived wxTreeCtrl if will sort without
2074 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2076 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2081 tvSort
.hParent
= HITEM(item
);
2082 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2083 tvSort
.lParam
= (LPARAM
)this;
2084 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2088 // ----------------------------------------------------------------------------
2090 // ----------------------------------------------------------------------------
2092 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
2094 if ( msg
->message
== WM_KEYDOWN
)
2096 // Only eat VK_RETURN if not being used by the application in
2097 // conjunction with modifiers
2098 if ( (msg
->wParam
== VK_RETURN
) && !wxIsAnyModifierDown() )
2100 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2105 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
2108 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
2110 const int id
= (signed short)id_
;
2112 if ( cmd
== EN_UPDATE
)
2114 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2115 event
.SetEventObject( this );
2116 ProcessCommand(event
);
2118 else if ( cmd
== EN_KILLFOCUS
)
2120 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2121 event
.SetEventObject( this );
2122 ProcessCommand(event
);
2130 // command processed
2134 bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey
)
2136 const bool bCtrl
= wxIsCtrlDown();
2137 const bool bShift
= wxIsShiftDown();
2138 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2147 if ( vkey
!= VK_RETURN
&& bCtrl
)
2149 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2151 changingEvent
.m_itemOld
= htSel
;
2153 if ( IsTreeEventAllowed(changingEvent
) )
2155 ::ToggleItemSelection(GetHwnd(), htSel
);
2157 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2159 changedEvent
.m_itemOld
= htSel
;
2160 (void)HandleTreeEvent(changedEvent
);
2165 wxArrayTreeItemIds selections
;
2166 size_t count
= GetSelections(selections
);
2168 if ( count
!= 1 || HITEM(selections
[0]) != htSel
)
2170 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2172 changingEvent
.m_itemOld
= htSel
;
2174 if ( IsTreeEventAllowed(changingEvent
) )
2177 ::SelectItem(GetHwnd(), htSel
);
2179 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2181 changedEvent
.m_itemOld
= htSel
;
2182 (void)HandleTreeEvent(changedEvent
);
2190 if ( !bCtrl
&& !bShift
)
2192 wxArrayTreeItemIds selections
;
2193 size_t count
= GetSelections(selections
);
2196 if ( htSel
&& count
> 0 )
2198 next
= vkey
== VK_UP
2199 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2200 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2204 next
= GetRootItem();
2206 if ( IsHiddenRoot(next
) )
2207 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2209 if ( vkey
== VK_DOWN
)
2211 wxTreeItemId next2
= TreeView_GetNextVisible(
2212 GetHwnd(), HITEM(next
));
2223 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2225 changingEvent
.m_itemOld
= htSel
;
2227 if ( IsTreeEventAllowed(changingEvent
) )
2230 ::SelectItem(GetHwnd(), HITEM(next
));
2231 ::SetFocus(GetHwnd(), HITEM(next
));
2233 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2235 changedEvent
.m_itemOld
= htSel
;
2236 (void)HandleTreeEvent(changedEvent
);
2241 wxTreeItemId next
= vkey
== VK_UP
2242 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2243 : TreeView_GetNextVisible(GetHwnd(), htSel
);
2250 if ( !m_htSelStart
)
2252 m_htSelStart
= htSel
;
2255 if ( bShift
&& SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2256 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2258 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2259 changingEvent
.m_itemOld
= htSel
;
2261 if ( IsTreeEventAllowed(changingEvent
) )
2263 SelectRange(GetHwnd(), HITEM(m_htSelStart
), HITEM(next
),
2264 SR_UNSELECT_OTHERS
);
2266 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2267 changedEvent
.m_itemOld
= htSel
;
2268 (void)HandleTreeEvent(changedEvent
);
2272 ::SetFocus(GetHwnd(), HITEM(next
));
2277 if ( HasChildren(htSel
) && IsExpanded(htSel
) )
2283 wxTreeItemId next
= GetItemParent(htSel
);
2285 if ( next
.IsOk() && !IsHiddenRoot(next
) )
2287 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2289 changingEvent
.m_itemOld
= htSel
;
2291 if ( IsTreeEventAllowed(changingEvent
) )
2294 ::SelectItem(GetHwnd(), HITEM(next
));
2295 ::SetFocus(GetHwnd(), HITEM(next
));
2297 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2299 changedEvent
.m_itemOld
= htSel
;
2300 (void)HandleTreeEvent(changedEvent
);
2307 if ( !IsVisible(htSel
) )
2309 EnsureVisible(htSel
);
2312 if ( !HasChildren(htSel
) )
2315 if ( !IsExpanded(htSel
) )
2321 wxTreeItemId next
= TreeView_GetChild(GetHwnd(), htSel
);
2323 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, next
);
2324 changingEvent
.m_itemOld
= htSel
;
2326 if ( IsTreeEventAllowed(changingEvent
) )
2329 ::SelectItem(GetHwnd(), HITEM(next
));
2330 ::SetFocus(GetHwnd(), HITEM(next
));
2332 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
, this, next
);
2333 changedEvent
.m_itemOld
= htSel
;
2334 (void)HandleTreeEvent(changedEvent
);
2342 wxTreeItemId next
= GetRootItem();
2344 if ( IsHiddenRoot(next
) )
2346 next
= TreeView_GetChild(GetHwnd(), HITEM(next
));
2352 if ( vkey
== VK_END
)
2356 wxTreeItemId nextTemp
= TreeView_GetNextVisible(
2357 GetHwnd(), HITEM(next
));
2359 if ( !nextTemp
.IsOk() )
2366 if ( htSel
== HITEM(next
) )
2371 if ( !m_htSelStart
)
2373 m_htSelStart
= htSel
;
2376 if ( SelectRange(GetHwnd(),
2377 HITEM(m_htSelStart
), HITEM(next
),
2378 SR_UNSELECT_OTHERS
| SR_SIMULATE
) )
2380 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2382 changingEvent
.m_itemOld
= htSel
;
2384 if ( IsTreeEventAllowed(changingEvent
) )
2386 SelectRange(GetHwnd(),
2387 HITEM(m_htSelStart
), HITEM(next
),
2388 SR_UNSELECT_OTHERS
);
2389 ::SetFocus(GetHwnd(), HITEM(next
));
2391 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2393 changedEvent
.m_itemOld
= htSel
;
2394 (void)HandleTreeEvent(changedEvent
);
2400 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2402 changingEvent
.m_itemOld
= htSel
;
2404 if ( IsTreeEventAllowed(changingEvent
) )
2407 ::SelectItem(GetHwnd(), HITEM(next
));
2408 ::SetFocus(GetHwnd(), HITEM(next
));
2410 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2412 changedEvent
.m_itemOld
= htSel
;
2413 (void)HandleTreeEvent(changedEvent
);
2423 wxTreeItemId firstVisible
= GetFirstVisibleItem();
2424 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2425 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2426 TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible
)) :
2427 TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible
));
2429 if ( !nextAdjacent
)
2434 wxTreeItemId nextStart
= firstVisible
;
2436 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2438 wxTreeItemId nextTemp
= (vkey
== VK_PRIOR
) ?
2439 TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart
)) :
2440 TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart
));
2442 if ( nextTemp
.IsOk() )
2444 nextStart
= nextTemp
;
2452 EnsureVisible(nextStart
);
2454 if ( vkey
== VK_NEXT
)
2456 wxTreeItemId nextEnd
= nextStart
;
2458 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2460 wxTreeItemId nextTemp
=
2461 TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd
));
2463 if ( nextTemp
.IsOk() )
2473 EnsureVisible(nextEnd
);
2478 size_t visibleCount
= TreeView_GetVisibleCount(GetHwnd());
2479 wxTreeItemId nextAdjacent
= (vkey
== VK_PRIOR
) ?
2480 TreeView_GetPrevVisible(GetHwnd(), htSel
) :
2481 TreeView_GetNextVisible(GetHwnd(), htSel
);
2483 if ( !nextAdjacent
)
2488 wxTreeItemId
next(htSel
);
2490 for ( size_t n
= 1; n
< visibleCount
; n
++ )
2492 wxTreeItemId nextTemp
= vkey
== VK_PRIOR
?
2493 TreeView_GetPrevVisible(GetHwnd(), HITEM(next
)) :
2494 TreeView_GetNextVisible(GetHwnd(), HITEM(next
));
2496 if ( !nextTemp
.IsOk() )
2502 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2504 changingEvent
.m_itemOld
= htSel
;
2506 if ( IsTreeEventAllowed(changingEvent
) )
2509 m_htSelStart
.Unset();
2510 ::SelectItem(GetHwnd(), HITEM(next
));
2511 ::SetFocus(GetHwnd(), HITEM(next
));
2513 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2515 changedEvent
.m_itemOld
= htSel
;
2516 (void)HandleTreeEvent(changedEvent
);
2528 bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam
, WXLPARAM lParam
)
2530 wxTreeEvent
keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2532 int keyCode
= wxCharCodeMSWToWX(wParam
);
2536 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2541 keyEvent
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
, keyCode
,
2544 bool processed
= HandleTreeEvent(keyEvent
);
2546 // generate a separate event for Space/Return
2547 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
2548 ((wParam
== VK_SPACE
) || (wParam
== VK_RETURN
)) )
2550 const HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2553 wxTreeEvent
activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2555 (void)HandleTreeEvent(activatedEvent
);
2562 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2563 // only do it during dragging, minimize wxWin overhead (this is important for
2564 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2565 // instead of passing by wxWin events
2567 wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2569 bool processed
= false;
2571 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2573 if ( nMsg
== WM_CONTEXTMENU
)
2575 int x
= GET_X_LPARAM(lParam
),
2576 y
= GET_Y_LPARAM(lParam
);
2578 // the item for which the menu should be shown
2581 // the position where the menu should be shown in client coordinates
2582 // (so that it can be passed directly to PopupMenu())
2585 if ( x
== -1 || y
== -1 )
2587 // this means that the event was generated from keyboard (e.g. with
2588 // Shift-F10 or special Windows menu key)
2590 // use the Explorer standard of putting the menu at the left edge
2591 // of the text, in the vertical middle of the text
2592 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2595 // Use the bounding rectangle of only the text part
2597 GetBoundingRect(item
, rect
, true);
2598 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
2601 else // event from mouse, use mouse position
2603 pt
= ScreenToClient(wxPoint(x
, y
));
2605 TV_HITTESTINFO tvhti
;
2609 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2610 item
= wxTreeItemId(tvhti
.hItem
);
2614 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2616 event
.m_pointDrag
= pt
;
2618 if ( HandleTreeEvent(event
) )
2620 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2622 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2624 // we only process mouse messages here and these parameters have the
2625 // same meaning for all of them
2626 int x
= GET_X_LPARAM(lParam
),
2627 y
= GET_Y_LPARAM(lParam
);
2629 TV_HITTESTINFO tvht
;
2633 HTREEITEM htOldItem
= TreeView_GetSelection(GetHwnd());
2634 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2638 case WM_LBUTTONDOWN
:
2643 m_htClickedItem
.Unset();
2645 if ( !(tvht
.flags
& TVHT_ONITEM
) )
2647 if ( !HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2649 if ( tvht
.flags
& TVHT_ONITEMBUTTON
)
2651 if ( !IsExpanded(htItem
) )
2666 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2667 m_ptClick
= wxPoint(x
, y
);
2669 if ( wParam
& MK_CONTROL
)
2671 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2673 m_htClickedItem
.Unset();
2677 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2679 changingEvent
.m_itemOld
= htOldItem
;
2681 if ( IsTreeEventAllowed(changingEvent
) )
2683 // toggle selected state
2684 ::ToggleItemSelection(GetHwnd(), htItem
);
2686 ::SetFocus(GetHwnd(), htItem
);
2688 // reset on any click without Shift
2689 m_htSelStart
.Unset();
2691 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2693 changedEvent
.m_itemOld
= htOldItem
;
2694 (void)HandleTreeEvent(changedEvent
);
2697 else if ( wParam
& MK_SHIFT
)
2699 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2701 m_htClickedItem
.Unset();
2706 bool willChange
= true;
2708 if ( !(wParam
& MK_CONTROL
) )
2710 srFlags
|= SR_UNSELECT_OTHERS
;
2713 if ( !m_htSelStart
)
2715 // take the focused item
2716 m_htSelStart
= htOldItem
;
2720 willChange
= SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2721 htItem
, srFlags
| SR_SIMULATE
);
2726 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2728 changingEvent
.m_itemOld
= htOldItem
;
2730 if ( IsTreeEventAllowed(changingEvent
) )
2732 // this selects all items between the starting one
2736 SelectRange(GetHwnd(), HITEM(m_htSelStart
),
2741 ::SelectItem(GetHwnd(), htItem
);
2744 ::SetFocus(GetHwnd(), htItem
);
2746 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2748 changedEvent
.m_itemOld
= htOldItem
;
2749 (void)HandleTreeEvent(changedEvent
);
2753 else // normal click
2755 // avoid doing anything if we click on the only
2756 // currently selected item
2758 wxArrayTreeItemIds selections
;
2759 size_t count
= GetSelections(selections
);
2763 HITEM(selections
[0]) != htItem
)
2765 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) )
2767 m_htClickedItem
.Unset();
2771 // clear the previously selected items, if the user
2772 // clicked outside of the present selection, otherwise,
2773 // perform the deselection on mouse-up, this allows
2774 // multiple drag and drop to work.
2776 if ( !IsItemSelected(GetHwnd(), htItem
))
2778 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2780 changingEvent
.m_itemOld
= htOldItem
;
2782 if ( IsTreeEventAllowed(changingEvent
) )
2785 ::SelectItem(GetHwnd(), htItem
);
2786 ::SetFocus(GetHwnd(), htItem
);
2788 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2790 changedEvent
.m_itemOld
= htOldItem
;
2791 (void)HandleTreeEvent(changedEvent
);
2796 ::SetFocus(GetHwnd(), htItem
);
2799 else // click on a single selected item
2801 // don't interfere with the default processing in
2802 // WM_MOUSEMOVE handler below as the default window
2803 // proc will start the drag itself if we let have
2805 m_htClickedItem
.Unset();
2807 // prevent in-place editing from starting if focus lost
2808 // since previous click
2811 TreeView_SelectItem(GetHwnd(), 0);
2812 ::SelectItem(GetHwnd(), htItem
);
2818 // reset on any click without Shift
2819 m_htSelStart
.Unset();
2822 m_focusLost
= false;
2824 // we consumed the event so we need to trigger state image
2829 wxTreeItemId item
= HitTest(wxPoint(x
, y
), htFlags
);
2831 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2833 m_triggerStateImageClick
= true;
2838 case WM_RBUTTONDOWN
:
2845 if ( HandleMouseEvent(nMsg
, x
, y
, wParam
) || !htItem
)
2850 // default handler removes the highlight from the currently
2851 // focused item when right mouse button is pressed on another
2852 // one but keeps the remaining items highlighted, which is
2853 // confusing, so override this default behaviour
2854 if ( !IsItemSelected(GetHwnd(), htItem
) )
2856 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2858 changingEvent
.m_itemOld
= htOldItem
;
2860 if ( IsTreeEventAllowed(changingEvent
) )
2863 ::SelectItem(GetHwnd(), htItem
);
2864 ::SetFocus(GetHwnd(), htItem
);
2866 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2868 changedEvent
.m_itemOld
= htOldItem
;
2869 (void)HandleTreeEvent(changedEvent
);
2877 if ( m_htClickedItem
)
2879 int cx
= abs(m_ptClick
.x
- x
);
2880 int cy
= abs(m_ptClick
.y
- y
);
2882 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2883 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2888 tv
.hdr
.hwndFrom
= GetHwnd();
2889 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2890 tv
.hdr
.code
= TVN_BEGINDRAG
;
2892 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2896 wxZeroMemory(tviAux
);
2898 tviAux
.hItem
= HITEM(m_htClickedItem
);
2899 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2900 tviAux
.stateMask
= 0xffffffff;
2901 TreeView_GetItem(GetHwnd(), &tviAux
);
2903 tv
.itemNew
.state
= tviAux
.state
;
2904 tv
.itemNew
.lParam
= tviAux
.lParam
;
2909 // do it before SendMessage() call below to avoid
2910 // reentrancies here if there is another WM_MOUSEMOVE
2911 // in the queue already
2912 m_htClickedItem
.Unset();
2914 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
2915 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2917 // don't pass it to the default window proc, it would
2918 // start dragging again
2922 #endif // __WXWINCE__
2927 m_dragImage
->Move(wxPoint(x
, y
));
2930 // highlight the item as target (hiding drag image is
2931 // necessary - otherwise the display will be corrupted)
2932 m_dragImage
->Hide();
2933 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2934 m_dragImage
->Show();
2937 #endif // wxUSE_DRAGIMAGE
2943 // deselect other items if multiple items selected
2946 wxArrayTreeItemIds selections
;
2947 size_t count
= GetSelections(selections
);
2950 !(wParam
& MK_CONTROL
) &&
2951 !(wParam
& MK_SHIFT
) )
2953 wxTreeEvent
changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING
,
2955 changingEvent
.m_itemOld
= htOldItem
;
2957 if ( IsTreeEventAllowed(changingEvent
) )
2960 ::SelectItem(GetHwnd(), htItem
);
2961 ::SetFocus(GetHwnd(), htItem
);
2963 wxTreeEvent
changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED
,
2965 changedEvent
.m_itemOld
= htOldItem
;
2966 (void)HandleTreeEvent(changedEvent
);
2971 m_htClickedItem
.Unset();
2973 if ( m_triggerStateImageClick
)
2975 if ( tvht
.flags
& TVHT_ONITEMSTATEICON
)
2977 wxTreeEvent
event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
,
2979 (void)HandleTreeEvent(event
);
2981 m_triggerStateImageClick
= false;
2986 if ( !m_dragStarted
&&
2987 (tvht
.flags
& TVHT_ONITEMSTATEICON
||
2988 tvht
.flags
& TVHT_ONITEMICON
||
2989 tvht
.flags
& TVHT_ONITEM
) )
3001 m_dragImage
->EndDrag();
3005 // generate the drag end event
3006 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
,
3008 event
.m_pointDrag
= wxPoint(x
, y
);
3009 (void)HandleTreeEvent(event
);
3011 // if we don't do it, the tree seems to think that 2 items
3012 // are selected simultaneously which is quite weird
3013 TreeView_SelectDropTarget(GetHwnd(), 0);
3015 #endif // wxUSE_DRAGIMAGE
3017 if ( isMultiple
&& nMsg
== WM_RBUTTONUP
)
3021 nmhdr
.hwndFrom
= GetHwnd();
3022 nmhdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
3023 nmhdr
.code
= NM_RCLICK
;
3024 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY
,
3025 nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
3029 m_dragStarted
= false;
3034 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) )
3038 // the tree control greys out the selected item when it loses focus
3039 // and paints it as selected again when it regains it, but it won't
3040 // do it for the other items itself - help it
3041 wxArrayTreeItemIds selections
;
3042 size_t count
= GetSelections(selections
);
3045 for ( size_t n
= 0; n
< count
; n
++ )
3047 // TreeView_GetItemRect() will return false if item is not
3048 // visible, which may happen perfectly well
3049 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
3052 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
3057 if ( nMsg
== WM_KILLFOCUS
)
3062 else if ( (nMsg
== WM_KEYDOWN
|| nMsg
== WM_SYSKEYDOWN
) && isMultiple
)
3064 // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
3065 // notification but for the keys which can be used to change selection
3066 // we need to do it from here so as to not apply the default behaviour
3067 // if the events are handled by the user code
3080 if ( !MSWHandleTreeKeyDownEvent(wParam
, lParam
) )
3082 // use the key to update the selection if it was left
3084 MSWHandleSelectionKey(wParam
);
3086 // pretend that we did process it in any case as we already
3087 // generated an event for it
3091 //default: for all the other keys leave processed as false so that
3092 // the tree control generates a TVN_KEYDOWN for us
3096 else if ( nMsg
== WM_COMMAND
)
3098 // if we receive a EN_KILLFOCUS command from the in-place edit control
3099 // used for label editing, make sure to end editing
3102 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
3104 if ( cmd
== EN_KILLFOCUS
)
3106 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
3116 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
3122 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
3124 if ( nMsg
== WM_CHAR
)
3126 // don't let the control process Space and Return keys because it
3127 // doesn't do anything useful with them anyhow but always beeps
3128 // annoyingly when it receives them and there is no way to turn it off
3129 // simply if you just process TREEITEM_ACTIVATED event to which Space
3130 // and Enter presses are mapped in your code
3131 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
3135 else if ( nMsg
== WM_KEYDOWN
)
3137 if ( wParam
== VK_ESCAPE
)
3141 m_dragImage
->EndDrag();
3145 // if we don't do it, the tree seems to think that 2 items
3146 // are selected simultaneously which is quite weird
3147 TreeView_SelectDropTarget(GetHwnd(), 0);
3151 #endif // wxUSE_DRAGIMAGE
3153 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
3156 // process WM_NOTIFY Windows message
3157 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
3159 wxTreeEvent
event(wxEVT_NULL
, this);
3160 wxEventType eventType
= wxEVT_NULL
;
3161 NMHDR
*hdr
= (NMHDR
*)lParam
;
3163 switch ( hdr
->code
)
3166 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3169 case TVN_BEGINRDRAG
:
3171 if ( eventType
== wxEVT_NULL
)
3172 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
3173 //else: left drag, already set above
3175 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3177 event
.m_item
= tv
->itemNew
.hItem
;
3178 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
3180 // don't allow dragging by default: the user code must
3181 // explicitly say that it wants to allow it to avoid breaking
3187 case TVN_BEGINLABELEDIT
:
3189 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
3190 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3192 // although the user event handler may still veto it, it is
3193 // important to set it now so that calls to SetItemText() from
3194 // the event handler would change the text controls contents
3196 event
.m_item
= info
->item
.hItem
;
3197 event
.m_label
= info
->item
.pszText
;
3198 event
.m_editCancelled
= false;
3202 case TVN_DELETEITEM
:
3204 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
3205 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3207 event
.m_item
= tv
->itemOld
.hItem
;
3211 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
3212 if ( it
!= m_attrs
.end() )
3221 case TVN_ENDLABELEDIT
:
3223 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
3224 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3226 event
.m_item
= info
->item
.hItem
;
3227 event
.m_label
= info
->item
.pszText
;
3228 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
3233 // These *must* not be removed or TVN_GETINFOTIP will
3234 // not be processed each time the mouse is moved
3235 // and the tooltip will only ever update once.
3244 #ifdef TVN_GETINFOTIP
3245 case TVN_GETINFOTIP
:
3247 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
3248 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
3250 // Which item are we trying to get a tooltip for?
3251 event
.m_item
= info
->hItem
;
3255 #endif // TVN_GETINFOTIP
3256 #endif // !__WXWINCE__
3258 case TVN_GETDISPINFO
:
3259 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
3262 case TVN_SETDISPINFO
:
3264 if ( eventType
== wxEVT_NULL
)
3265 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
3266 //else: get, already set above
3268 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3270 event
.m_item
= info
->item
.hItem
;
3274 case TVN_ITEMEXPANDING
:
3275 case TVN_ITEMEXPANDED
:
3277 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3280 switch ( tv
->action
)
3283 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
3291 what
= IDX_COLLAPSE
;
3295 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
3298 eventType
= gs_expandEvents
[what
][how
];
3300 event
.m_item
= tv
->itemNew
.hItem
;
3306 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
3308 // fabricate the lParam and wParam parameters sufficiently
3309 // similar to the ones from a "real" WM_KEYDOWN so that
3310 // CreateKeyEvent() works correctly
3311 return MSWHandleTreeKeyDownEvent(
3312 info
->wVKey
, (wxIsAltDown() ? KF_ALTDOWN
: 0) << 16);
3317 // Vista's tree control has introduced some problems with our
3318 // multi-selection tree. When TreeView_SelectItem() is called,
3319 // the wrong items are deselected.
3321 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3322 // that can be used to regulate this incorrect behavior. The
3323 // following messages will allow only the unlocked item's selection
3326 case TVN_ITEMCHANGINGA
:
3327 case TVN_ITEMCHANGINGW
:
3329 // we only need to handles these in multi-select trees
3330 if ( HasFlag(wxTR_MULTIPLE
) )
3332 // get info about the item about to be changed
3333 NMTVITEMCHANGE
* info
= (NMTVITEMCHANGE
*)lParam
;
3334 if (TreeItemUnlocker::IsLocked(info
->hItem
))
3336 // item's state is locked, don't allow the change
3337 // returning 1 will disallow the change
3343 // allow the state change
3347 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
3348 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3349 // we have to handle both messages:
3350 case TVN_SELCHANGEDA
:
3351 case TVN_SELCHANGEDW
:
3352 if ( !HasFlag(wxTR_MULTIPLE
) )
3354 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
3358 case TVN_SELCHANGINGA
:
3359 case TVN_SELCHANGINGW
:
3360 if ( !HasFlag(wxTR_MULTIPLE
) )
3362 if ( eventType
== wxEVT_NULL
)
3363 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
3364 //else: already set above
3366 if (hdr
->code
== TVN_SELCHANGINGW
||
3367 hdr
->code
== TVN_SELCHANGEDW
)
3369 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
3370 event
.m_item
= tv
->itemNew
.hItem
;
3371 event
.m_itemOld
= tv
->itemOld
.hItem
;
3375 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
3376 event
.m_item
= tv
->itemNew
.hItem
;
3377 event
.m_itemOld
= tv
->itemOld
.hItem
;
3381 // we receive this message from WM_LBUTTONDOWN handler inside
3382 // comctl32.dll and so before the click is passed to
3383 // DefWindowProc() which sets the focus to the window which was
3384 // clicked and this can lead to unexpected event sequences: for
3385 // example, we may get a "selection change" event from the tree
3386 // before getting a "kill focus" event for the text control which
3387 // had the focus previously, thus breaking user code doing input
3390 // to avoid such surprises, we force the generation of focus events
3391 // now, before we generate the selection change ones
3395 // instead of explicitly checking for _WIN32_IE, check if the
3396 // required symbols are available in the headers
3397 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
3400 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
3401 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
3402 switch ( nmcd
.dwDrawStage
)
3405 // if we've got any items with non standard attributes,
3406 // notify us before painting each item
3407 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
3410 // windows in TreeCtrl use one-based index for item state images,
3411 // 0 indexed image is not being used, we're using zero-based index,
3412 // so we have to add temp image (of zero index) to state image list
3413 // before we draw any item, then after items are drawn we have to
3414 // delete it (in POSTPAINT notify)
3415 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3417 typedef BOOL (wxSTDCALL
*ImageList_Copy_t
)
3418 (HIMAGELIST
, int, HIMAGELIST
, int, UINT
);
3419 static ImageList_Copy_t s_pfnImageList_Copy
= NULL
;
3420 static bool loaded
= false;
3424 wxLoadedDLL
dllComCtl32(_T("comctl32.dll"));
3425 if ( dllComCtl32
.IsLoaded() )
3426 wxDL_INIT_FUNC(s_pfn
, ImageList_Copy
, dllComCtl32
);
3429 if ( !s_pfnImageList_Copy
)
3431 // this code is broken with ImageList_Copy()
3432 // but I don't care enough about Win95 support
3433 // to write it now -- if anybody does, please
3435 wxFAIL_MSG("TODO: implement this for Win95");
3440 hImageList
= GetHimagelistOf(m_imageListState
);
3442 // add temporary image
3444 m_imageListState
->GetSize(0, width
, height
);
3446 HBITMAP hbmpTemp
= ::CreateBitmap(width
, height
, 1, 1, NULL
);
3447 int index
= ::ImageList_Add(hImageList
, hbmpTemp
, hbmpTemp
);
3448 ::DeleteObject(hbmpTemp
);
3452 // move images to right
3453 for ( int i
= index
; i
> 0; i
-- )
3455 (*s_pfnImageList_Copy
)(hImageList
, i
,
3460 // we must remove the image in POSTPAINT notify
3461 *result
|= CDRF_NOTIFYPOSTPAINT
;
3466 case CDDS_POSTPAINT
:
3467 // we are deleting temp image of 0 index, which was
3468 // added before items were drawn (in PREPAINT notify)
3469 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
3470 m_imageListState
->Remove(0);
3473 case CDDS_ITEMPREPAINT
:
3475 wxMapTreeAttr::iterator
3476 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
3478 if ( it
== m_attrs
.end() )
3480 // nothing to do for this item
3481 *result
= CDRF_DODEFAULT
;
3485 wxTreeItemAttr
* const attr
= it
->second
;
3487 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
3488 TVIF_STATE
, TVIS_DROPHILITED
);
3490 const UINT tvItemState
= tvItem
.state
;
3492 // selection colours should override ours,
3493 // otherwise it is too confusing to the user
3494 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
3495 !(tvItemState
& TVIS_DROPHILITED
) )
3498 if ( attr
->HasBackgroundColour() )
3500 colBack
= attr
->GetBackgroundColour();
3501 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
3505 // but we still want to keep the special foreground
3506 // colour when we don't have focus (we can't keep
3507 // it when we do, it would usually be unreadable on
3508 // the almost inverted bg colour...)
3509 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
3510 FindFocus() != this ) &&
3511 !(tvItemState
& TVIS_DROPHILITED
) )
3514 if ( attr
->HasTextColour() )
3516 colText
= attr
->GetTextColour();
3517 lptvcd
->clrText
= wxColourToRGB(colText
);
3521 if ( attr
->HasFont() )
3523 HFONT hFont
= GetHfontOf(attr
->GetFont());
3525 ::SelectObject(nmcd
.hdc
, hFont
);
3527 *result
= CDRF_NEWFONT
;
3529 else // no specific font
3531 *result
= CDRF_DODEFAULT
;
3537 *result
= CDRF_DODEFAULT
;
3541 // we always process it
3543 #endif // have owner drawn support in headers
3547 DWORD pos
= GetMessagePos();
3549 point
.x
= LOWORD(pos
);
3550 point
.y
= HIWORD(pos
);
3551 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
3553 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), htFlags
);
3555 if ( htFlags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3557 event
.m_item
= item
;
3558 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
3567 TV_HITTESTINFO tvhti
;
3568 ::GetCursorPos(&tvhti
.pt
);
3569 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
3570 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
3572 if ( tvhti
.flags
& TVHT_ONITEM
)
3574 event
.m_item
= tvhti
.hItem
;
3575 eventType
= (int)hdr
->code
== NM_DBLCLK
3576 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
3577 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
3579 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
3580 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
3589 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
3592 event
.SetEventType(eventType
);
3594 bool processed
= HandleTreeEvent(event
);
3597 switch ( hdr
->code
)
3600 // we translate NM_DBLCLK into ACTIVATED event and if the user
3601 // handled the activation of the item we shouldn't proceed with
3602 // also using the same double click for toggling the item expanded
3603 // state -- but OTOH do let the user to expand/collapse the item by
3604 // double clicking on it if the activation is not handled specially
3605 *result
= processed
;
3609 // prevent tree control from sending WM_CONTEXTMENU to our parent
3610 // (which it does if NM_RCLICK is not handled) because we want to
3611 // send it to the control itself
3615 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
3616 (WPARAM
)GetHwnd(), ::GetMessagePos());
3620 case TVN_BEGINRDRAG
:
3622 if ( event
.IsAllowed() )
3624 // normally this is impossible because the m_dragImage is
3625 // deleted once the drag operation is over
3626 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
3628 m_dragImage
= new wxDragImage(*this, event
.m_item
);
3629 m_dragImage
->BeginDrag(wxPoint(0,0), this);
3630 m_dragImage
->Show();
3632 m_dragStarted
= true;
3634 #endif // wxUSE_DRAGIMAGE
3637 case TVN_DELETEITEM
:
3639 // NB: we might process this message using wxWidgets event
3640 // tables, but due to overhead of wxWin event system we
3641 // prefer to do it here ourself (otherwise deleting a tree
3642 // with many items is just too slow)
3643 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3645 wxTreeItemParam
*param
=
3646 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
3649 processed
= true; // Make sure we don't get called twice
3653 case TVN_BEGINLABELEDIT
:
3654 // return true to cancel label editing
3655 *result
= !event
.IsAllowed();
3657 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3658 if ( event
.IsAllowed() )
3660 HWND hText
= TreeView_GetEditControl(GetHwnd());
3663 // MBN: if m_textCtrl already has an HWND, it is a stale
3664 // pointer from a previous edit (because the user
3665 // didn't modify the label before dismissing the control,
3666 // and TVN_ENDLABELEDIT was not sent), so delete it
3667 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
3670 m_textCtrl
= new wxTextCtrl();
3671 m_textCtrl
->SetParent(this);
3672 m_textCtrl
->SetHWND((WXHWND
)hText
);
3673 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3675 // set wxTE_PROCESS_ENTER style for the text control to
3676 // force it to process the Enter presses itself, otherwise
3677 // they could be stolen from it by the dialog
3679 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3680 | wxTE_PROCESS_ENTER
);
3683 else // we had set m_idEdited before
3689 case TVN_ENDLABELEDIT
:
3690 // return true to set the label to the new string: note that we
3691 // also must pretend that we did process the message or it is going
3692 // to be passed to DefWindowProc() which will happily return false
3693 // cancelling the label change
3694 *result
= event
.IsAllowed();
3697 // ensure that we don't have the text ctrl which is going to be
3703 #ifdef TVN_GETINFOTIP
3704 case TVN_GETINFOTIP
:
3706 // If the user permitted a tooltip change, change it
3707 if (event
.IsAllowed())
3709 SetToolTip(event
.m_label
);
3716 case TVN_SELCHANGING
:
3717 case TVN_ITEMEXPANDING
:
3718 // return true to prevent the action from happening
3719 *result
= !event
.IsAllowed();
3722 case TVN_ITEMEXPANDED
:
3724 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
3725 const wxTreeItemId
id(tv
->itemNew
.hItem
);
3727 if ( tv
->action
== TVE_COLLAPSE
)
3729 if ( wxApp::GetComCtl32Version() >= 600 )
3731 // for some reason the item selection rectangle depends
3732 // on whether it is expanded or collapsed (at least
3733 // with comctl32.dll v6): it is wider (by 3 pixels) in
3734 // the expanded state, so when the item collapses and
3735 // then is deselected the rightmost 3 pixels of the
3736 // previously drawn selection are left on the screen
3738 // it's not clear if it's a bug in comctl32.dll or in
3739 // our code (because it does not happen in Explorer but
3740 // OTOH we don't do anything which could result in this
3741 // AFAICS) but we do need to work around it to avoid
3748 // the item is also not refreshed properly after expansion when
3749 // it has an image depending on the expanded/collapsed state:
3750 // again, it's not clear if the bug is in comctl32.dll or our
3752 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3761 case TVN_GETDISPINFO
:
3762 // NB: so far the user can't set the image himself anyhow, so do it
3763 // anyway - but this may change later
3764 //if ( /* !processed && */ )
3766 wxTreeItemId item
= event
.m_item
;
3767 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3769 const wxTreeItemParam
* const param
= GetItemParam(item
);
3773 if ( info
->item
.mask
& TVIF_IMAGE
)
3778 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3779 : wxTreeItemIcon_Normal
3782 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3784 info
->item
.iSelectedImage
=
3787 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3788 : wxTreeItemIcon_Selected
3795 // for the other messages the return value is ignored and there is
3796 // nothing special to do
3801 // ----------------------------------------------------------------------------
3803 // ----------------------------------------------------------------------------
3805 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3806 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3808 int wxTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
3810 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
3812 // receive the desired information
3813 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3816 // state images are one-based
3817 return STATEIMAGEMASKTOINDEX(tvItem
.state
) - 1;
3820 void wxTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
3822 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
3824 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3826 // state images are one-based
3827 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3828 tvItem
.state
= INDEXTOSTATEIMAGEMASK(state
+ 1);
3833 #endif // wxUSE_TREECTRL