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/msw/private.h"
42 // Set this to 1 to be _absolutely_ sure that repainting will work for all
43 // comctl32.dll versions
44 #define wxUSE_COMCTL32_SAFELY 0
46 #include "wx/imaglist.h"
47 #include "wx/msw/dragimag.h"
49 // macros to hide the cast ugliness
50 // --------------------------------
52 // get HTREEITEM from wxTreeItemId
53 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
56 // older SDKs are missing these
57 #ifndef TVN_ITEMCHANGINGA
59 #define TVN_ITEMCHANGINGA (TVN_FIRST-16)
60 #define TVN_ITEMCHANGINGW (TVN_FIRST-17)
62 typedef struct tagNMTVITEMCHANGE
75 // this helper class is used on vista systems for preventing unwanted
76 // item state changes in the vista tree control. It is only effective in
77 // multi-select mode on vista systems.
79 // The vista tree control includes some new code that originally broke the
80 // multi-selection tree, causing seemingly spurious item selection state changes
81 // during Shift or Ctrl-click item selection. (To witness the original broken
82 // behavior, simply make IsLocked() below always return false). This problem was
83 // solved by using the following class to 'unlock' an item's selection state.
85 class TreeItemUnlocker
88 // unlock a single item
89 TreeItemUnlocker(HTREEITEM item
) { ms_unlockedItem
= item
; }
91 // unlock all items, don't use unless absolutely necessary
92 TreeItemUnlocker() { ms_unlockedItem
= (HTREEITEM
)-1; }
94 // lock everything back
95 ~TreeItemUnlocker() { ms_unlockedItem
= NULL
; }
98 // check if the item state is currently locked
99 static bool IsLocked(HTREEITEM item
)
100 { return ms_unlockedItem
!= (HTREEITEM
)-1 && item
!= ms_unlockedItem
; }
103 static HTREEITEM ms_unlockedItem
;
106 HTREEITEM
TreeItemUnlocker::ms_unlockedItem
= NULL
;
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 // wrappers for TreeView_GetItem/TreeView_SetItem
113 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 // helper function which selects all items in a range and, optionally,
156 // unselects all others
157 static void SelectRange(HWND hwndTV
,
160 bool unselectOthers
= true)
162 // find the first (or last) item and select it
164 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
165 while ( htItem
&& cont
)
167 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
169 if ( !IsItemSelected(hwndTV
, htItem
) )
171 SelectItem(hwndTV
, htItem
);
178 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
180 UnselectItem(hwndTV
, htItem
);
184 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
187 // select the items in range
188 cont
= htFirst
!= htLast
;
189 while ( htItem
&& cont
)
191 if ( !IsItemSelected(hwndTV
, htItem
) )
193 SelectItem(hwndTV
, htItem
);
196 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
198 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
202 if ( unselectOthers
)
206 if ( IsItemSelected(hwndTV
, htItem
) )
208 UnselectItem(hwndTV
, htItem
);
211 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
215 // seems to be necessary - otherwise the just selected items don't always
216 // appear as selected
217 UpdateWindow(hwndTV
);
220 // helper function which tricks the standard control into changing the focused
221 // item without changing anything else (if someone knows why Microsoft doesn't
222 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
224 // returns true if the focus was changed, false if the given item was already
226 static bool SetFocus(HWND hwndTV
, HTREEITEM htItem
)
229 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
231 if ( htItem
== htFocus
)
236 // remember the selection state of the item
237 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
239 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
241 // prevent the tree from unselecting the old focus which it
242 // would do by default (TreeView_SelectItem unselects the
244 TreeView_SelectItem(hwndTV
, 0);
245 SelectItem(hwndTV
, htFocus
);
248 TreeView_SelectItem(hwndTV
, htItem
);
252 // need to clear the selection which TreeView_SelectItem() gave
254 UnselectItem(hwndTV
, htItem
);
256 //else: was selected, still selected - ok
260 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
262 // just clear the focus
263 TreeView_SelectItem(hwndTV
, 0);
265 if ( wasFocusSelected
)
267 // restore the selection state
268 SelectItem(hwndTV
, htFocus
);
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 // a convenient wrapper around TV_ITEM struct which adds a ctor
281 #pragma warning( disable : 4097 ) // inheriting from typedef
284 struct wxTreeViewItem
: public TV_ITEM
286 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
287 UINT mask_
, // fields which are valid
288 UINT stateMask_
= 0) // for TVIF_STATE only
292 // hItem member is always valid
293 mask
= mask_
| TVIF_HANDLE
;
294 stateMask
= stateMask_
;
299 // ----------------------------------------------------------------------------
300 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
302 // We need this for a couple of reasons:
304 // 1) This class is needed for support of different images: the Win32 common
305 // control natively supports only 2 images (the normal one and another for the
306 // selected state). We wish to provide support for 2 more of them for folder
307 // items (i.e. those which have children): for expanded state and for expanded
308 // selected state. For this we use this structure to store the additional items
311 // 2) This class is also needed to hold the HITEM so that we can sort
312 // it correctly in the MSW sort callback.
314 // In addition it makes other workarounds such as this easier and helps
315 // simplify the code.
316 // ----------------------------------------------------------------------------
318 class wxTreeItemParam
325 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
331 // dtor deletes the associated data as well
332 virtual ~wxTreeItemParam() { delete m_data
; }
335 // get the real data associated with the item
336 wxTreeItemData
*GetData() const { return m_data
; }
338 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
340 // do we have such image?
341 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
342 // get image, falling back to the other images if this one is not
344 int GetImage(wxTreeItemIcon which
) const
346 int image
= m_images
[which
];
351 case wxTreeItemIcon_SelectedExpanded
:
352 image
= GetImage(wxTreeItemIcon_Expanded
);
357 case wxTreeItemIcon_Selected
:
358 case wxTreeItemIcon_Expanded
:
359 image
= GetImage(wxTreeItemIcon_Normal
);
362 case wxTreeItemIcon_Normal
:
367 wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") );
373 // change the given image
374 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
377 const wxTreeItemId
& GetItem() const { return m_item
; }
379 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
382 // all the images associated with the item
383 int m_images
[wxTreeItemIcon_Max
];
385 // item for sort callbacks
388 // the real client data
389 wxTreeItemData
*m_data
;
391 DECLARE_NO_COPY_CLASS(wxTreeItemParam
)
394 // wxVirutalNode is used in place of a single root when 'hidden' root is
396 class wxVirtualNode
: public wxTreeViewItem
399 wxVirtualNode(wxTreeItemParam
*param
)
400 : wxTreeViewItem(TVI_ROOT
, 0)
410 wxTreeItemParam
*GetParam() const { return m_param
; }
411 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
414 wxTreeItemParam
*m_param
;
416 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
420 #pragma warning( default : 4097 )
423 // a macro to get the virtual root, returns NULL if none
424 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
426 // returns true if the item is the virtual root
427 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
429 // a class which encapsulates the tree traversal logic: it vists all (unless
430 // OnVisit() returns false) items under the given one
431 class wxTreeTraversal
434 wxTreeTraversal(const wxTreeCtrl
*tree
)
439 // give it a virtual dtor: not really needed as the class is never used
440 // polymorphically and not even allocated on heap at all, but this is safer
441 // (in case it ever is) and silences the compiler warnings for now
442 virtual ~wxTreeTraversal() { }
444 // do traverse the tree: visit all items (recursively by default) under the
445 // given one; return true if all items were traversed or false if the
446 // traversal was aborted because OnVisit returned false
447 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
449 // override this function to do whatever is needed for each item, return
450 // false to stop traversing
451 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
454 const wxTreeCtrl
*GetTree() const { return m_tree
; }
457 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
459 const wxTreeCtrl
*m_tree
;
461 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
464 // internal class for getting the selected items
465 class TraverseSelections
: public wxTreeTraversal
468 TraverseSelections(const wxTreeCtrl
*tree
,
469 wxArrayTreeItemIds
& selections
)
470 : wxTreeTraversal(tree
), m_selections(selections
)
472 m_selections
.Empty();
474 if (tree
->GetCount() > 0)
475 DoTraverse(tree
->GetRootItem());
478 virtual bool OnVisit(const wxTreeItemId
& item
)
480 const wxTreeCtrl
* const tree
= GetTree();
482 // can't visit a virtual node.
483 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
488 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
490 m_selections
.Add(item
);
496 size_t GetCount() const { return m_selections
.GetCount(); }
499 wxArrayTreeItemIds
& m_selections
;
501 DECLARE_NO_COPY_CLASS(TraverseSelections
)
504 // internal class for counting tree items
505 class TraverseCounter
: public wxTreeTraversal
508 TraverseCounter(const wxTreeCtrl
*tree
,
509 const wxTreeItemId
& root
,
511 : wxTreeTraversal(tree
)
515 DoTraverse(root
, recursively
);
518 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
525 size_t GetCount() const { return m_count
; }
530 DECLARE_NO_COPY_CLASS(TraverseCounter
)
533 // ----------------------------------------------------------------------------
535 // ----------------------------------------------------------------------------
537 #if wxUSE_EXTENDED_RTTI
538 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
540 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
541 // new style border flags, we put them first to
542 // use them for streaming out
543 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
544 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
545 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
546 wxFLAGS_MEMBER(wxBORDER_RAISED
)
547 wxFLAGS_MEMBER(wxBORDER_STATIC
)
548 wxFLAGS_MEMBER(wxBORDER_NONE
)
550 // old style border flags
551 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
552 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
553 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
554 wxFLAGS_MEMBER(wxRAISED_BORDER
)
555 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
556 wxFLAGS_MEMBER(wxBORDER
)
558 // standard window styles
559 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
560 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
561 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
562 wxFLAGS_MEMBER(wxWANTS_CHARS
)
563 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
564 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
565 wxFLAGS_MEMBER(wxVSCROLL
)
566 wxFLAGS_MEMBER(wxHSCROLL
)
568 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
569 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
570 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
571 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
572 wxFLAGS_MEMBER(wxTR_NO_LINES
)
573 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
574 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
575 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
576 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
577 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
578 wxFLAGS_MEMBER(wxTR_SINGLE
)
579 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
580 #if WXWIN_COMPATIBILITY_2_8
581 wxFLAGS_MEMBER(wxTR_EXTENDED
)
583 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
585 wxEND_FLAGS( wxTreeCtrlStyle
)
587 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
589 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
590 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
591 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
592 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
593 wxEND_PROPERTIES_TABLE()
595 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
596 wxEND_HANDLERS_TABLE()
598 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
600 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
603 // ----------------------------------------------------------------------------
605 // ----------------------------------------------------------------------------
607 // indices in gs_expandEvents table below
622 // handy table for sending events - it has to be initialized during run-time
623 // now so can't be const any more
624 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
627 but logically it's a const table with the following entries:
630 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
631 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
635 // ============================================================================
637 // ============================================================================
639 // ----------------------------------------------------------------------------
641 // ----------------------------------------------------------------------------
643 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
645 if ( !OnVisit(root
) )
648 return Traverse(root
, recursively
);
651 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
653 wxTreeItemIdValue cookie
;
654 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
655 while ( child
.IsOk() )
657 // depth first traversal
658 if ( recursively
&& !Traverse(child
, true) )
661 if ( !OnVisit(child
) )
664 child
= m_tree
->GetNextChild(root
, cookie
);
670 // ----------------------------------------------------------------------------
671 // construction and destruction
672 // ----------------------------------------------------------------------------
674 void wxTreeCtrl::Init()
677 m_hasAnyAttr
= false;
681 m_pVirtualRoot
= NULL
;
683 // initialize the global array of events now as it can't be done statically
684 // with the wxEVT_XXX values being allocated during run-time only
685 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
686 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
687 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
688 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
691 bool wxTreeCtrl::Create(wxWindow
*parent
,
696 const wxValidator
& validator
,
697 const wxString
& name
)
701 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
702 style
|= wxBORDER_SUNKEN
;
704 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
708 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
709 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
711 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
712 wstyle
|= TVS_HASLINES
;
713 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
714 wstyle
|= TVS_HASBUTTONS
;
716 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
717 wstyle
|= TVS_EDITLABELS
;
719 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
720 wstyle
|= TVS_LINESATROOT
;
722 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
724 if ( wxApp::GetComCtl32Version() >= 471 )
725 wstyle
|= TVS_FULLROWSELECT
;
728 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
729 // Need so that TVN_GETINFOTIP messages will be sent
730 wstyle
|= TVS_INFOTIP
;
733 // Create the tree control.
734 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
737 #if wxUSE_COMCTL32_SAFELY
738 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
739 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
741 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
742 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
744 // This works around a bug in the Windows tree control whereby for some versions
745 // of comctrl32, setting any colour actually draws the background in black.
746 // This will initialise the background to the system colour.
747 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
748 // Assume the user has an updated comctl32.dll.
749 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
750 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
751 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
754 wxSetCCUnicodeFormat(GetHwnd());
759 wxTreeCtrl::~wxTreeCtrl()
761 // delete any attributes
764 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
766 // prevent TVN_DELETEITEM handler from deleting the attributes again!
767 m_hasAnyAttr
= false;
772 // delete user data to prevent memory leaks
773 // also deletes hidden root node storage.
777 // ----------------------------------------------------------------------------
779 // ----------------------------------------------------------------------------
781 /* static */ wxVisualAttributes
782 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
784 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
786 // common controls have their own default font
787 attrs
.font
= wxGetCCDefaultFont();
793 // simple wrappers which add error checking in debug mode
795 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
797 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
798 _T("can't retrieve virtual root item") );
800 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
802 wxLogLastError(wxT("TreeView_GetItem"));
810 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
812 TreeItemUnlocker
unlocker(tvItem
->hItem
);
814 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
816 wxLogLastError(wxT("TreeView_SetItem"));
820 unsigned int wxTreeCtrl::GetCount() const
822 return (unsigned int)TreeView_GetCount(GetHwnd());
825 unsigned int wxTreeCtrl::GetIndent() const
827 return TreeView_GetIndent(GetHwnd());
830 void wxTreeCtrl::SetIndent(unsigned int indent
)
832 TreeView_SetIndent(GetHwnd(), indent
);
835 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
838 (void) TreeView_SetImageList(GetHwnd(),
839 imageList
? imageList
->GetHIMAGELIST() : 0,
843 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
845 if (m_ownsImageListNormal
)
846 delete m_imageListNormal
;
848 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
849 m_ownsImageListNormal
= false;
852 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
854 if (m_ownsImageListState
) delete m_imageListState
;
855 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
856 m_ownsImageListState
= false;
859 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
860 bool recursively
) const
862 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
864 TraverseCounter
counter(this, item
, recursively
);
865 return counter
.GetCount() - 1;
868 // ----------------------------------------------------------------------------
870 // ----------------------------------------------------------------------------
872 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
874 #if !wxUSE_COMCTL32_SAFELY
875 if ( !wxWindowBase::SetBackgroundColour(colour
) )
878 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
884 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
886 #if !wxUSE_COMCTL32_SAFELY
887 if ( !wxWindowBase::SetForegroundColour(colour
) )
890 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
896 // ----------------------------------------------------------------------------
898 // ----------------------------------------------------------------------------
900 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
902 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
905 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
907 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
909 wxChar buf
[512]; // the size is arbitrary...
911 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
912 tvItem
.pszText
= buf
;
913 tvItem
.cchTextMax
= WXSIZEOF(buf
);
914 if ( !DoGetItem(&tvItem
) )
916 // don't return some garbage which was on stack, but an empty string
920 return wxString(buf
);
923 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
925 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
927 if ( IS_VIRTUAL_ROOT(item
) )
930 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
931 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
934 // when setting the text of the item being edited, the text control should
935 // be updated to reflect the new text as well, otherwise calling
936 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
938 // don't use GetEditControl() here because m_textCtrl is not set yet
939 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
942 if ( item
== m_idEdited
)
944 ::SetWindowText(hwndEdit
, text
.wx_str());
949 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
950 wxTreeItemIcon which
) const
952 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
954 if ( IsHiddenRoot(item
) )
956 // no images for hidden root item
960 wxTreeItemParam
*param
= GetItemParam(item
);
962 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
965 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
966 wxTreeItemIcon which
)
968 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
969 wxCHECK_RET( which
>= 0 &&
970 which
< wxTreeItemIcon_Max
,
971 wxT("invalid image index"));
974 if ( IsHiddenRoot(item
) )
976 // no images for hidden root item
980 wxTreeItemParam
*data
= GetItemParam(item
);
984 data
->SetImage(image
, which
);
989 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
991 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
993 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
995 // hidden root may still have data.
996 if ( IS_VIRTUAL_ROOT(item
) )
998 return GET_VIRTUAL_ROOT()->GetParam();
1002 if ( !DoGetItem(&tvItem
) )
1007 return (wxTreeItemParam
*)tvItem
.lParam
;
1010 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1012 wxTreeItemParam
*data
= GetItemParam(item
);
1014 return data
? data
->GetData() : NULL
;
1017 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1019 // first, associate this piece of data with this item
1025 wxTreeItemParam
*param
= GetItemParam(item
);
1027 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1029 param
->SetData(data
);
1032 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1034 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1036 if ( IS_VIRTUAL_ROOT(item
) )
1039 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1040 tvItem
.cChildren
= (int)has
;
1044 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1046 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1048 if ( IS_VIRTUAL_ROOT(item
) )
1051 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1052 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1056 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1058 if ( IS_VIRTUAL_ROOT(item
) )
1061 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1062 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1066 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1068 if ( IS_VIRTUAL_ROOT(item
) )
1072 if ( GetBoundingRect(item
, rect
) )
1078 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1080 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1082 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1083 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1086 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1088 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1090 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1091 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1094 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1096 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1098 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1099 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1102 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1103 const wxColour
& col
)
1105 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1107 wxTreeItemAttr
*attr
;
1108 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1109 if ( it
== m_attrs
.end() )
1111 m_hasAnyAttr
= true;
1113 m_attrs
[item
.m_pItem
] =
1114 attr
= new wxTreeItemAttr
;
1121 attr
->SetTextColour(col
);
1126 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1127 const wxColour
& col
)
1129 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1131 wxTreeItemAttr
*attr
;
1132 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1133 if ( it
== m_attrs
.end() )
1135 m_hasAnyAttr
= true;
1137 m_attrs
[item
.m_pItem
] =
1138 attr
= new wxTreeItemAttr
;
1140 else // already in the hash
1145 attr
->SetBackgroundColour(col
);
1150 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1152 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1154 wxTreeItemAttr
*attr
;
1155 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1156 if ( it
== m_attrs
.end() )
1158 m_hasAnyAttr
= true;
1160 m_attrs
[item
.m_pItem
] =
1161 attr
= new wxTreeItemAttr
;
1163 else // already in the hash
1168 attr
->SetFont(font
);
1170 // Reset the item's text to ensure that the bounding rect will be adjusted
1171 // for the new font.
1172 SetItemText(item
, GetItemText(item
));
1177 // ----------------------------------------------------------------------------
1179 // ----------------------------------------------------------------------------
1181 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1183 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1185 if ( item
== wxTreeItemId(TVI_ROOT
) )
1187 // virtual (hidden) root is never visible
1191 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1194 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1195 // the HTREEITEM with TVM_GETITEMRECT
1196 *(HTREEITEM
*)&rect
= HITEM(item
);
1198 // true means to get rect for just the text, not the whole line
1199 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1201 // if TVM_GETITEMRECT returned false, then the item is definitely not
1202 // visible (because its parent is not expanded)
1206 // however if it returned true, the item might still be outside the
1207 // currently visible part of the tree, test for it (notice that partly
1208 // visible means visible here)
1209 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1212 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1214 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1216 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1219 return tvItem
.cChildren
!= 0;
1222 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1224 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1226 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1229 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1232 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1234 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1236 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1239 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1242 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1244 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1246 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1249 return (tvItem
.state
& TVIS_BOLD
) != 0;
1252 // ----------------------------------------------------------------------------
1254 // ----------------------------------------------------------------------------
1256 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1258 // Root may be real (visible) or virtual (hidden).
1259 if ( GET_VIRTUAL_ROOT() )
1262 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1265 wxTreeItemId
wxTreeCtrl::GetSelection() const
1267 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1268 wxT("this only works with single selection controls") );
1270 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1273 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1275 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1279 if ( IS_VIRTUAL_ROOT(item
) )
1281 // no parent for the virtual root
1286 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1287 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1289 // the top level items should have the virtual root as their parent
1294 return wxTreeItemId(hItem
);
1297 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1298 wxTreeItemIdValue
& cookie
) const
1300 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1302 // remember the last child returned in 'cookie'
1303 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1305 return wxTreeItemId(cookie
);
1308 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1309 wxTreeItemIdValue
& cookie
) const
1311 wxTreeItemId
fromCookie(cookie
);
1313 HTREEITEM hitem
= HITEM(fromCookie
);
1315 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1317 wxTreeItemId
item(hitem
);
1319 cookie
= item
.m_pItem
;
1324 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1326 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1328 // can this be done more efficiently?
1329 wxTreeItemIdValue cookie
;
1331 wxTreeItemId childLast
,
1332 child
= GetFirstChild(item
, cookie
);
1333 while ( child
.IsOk() )
1336 child
= GetNextChild(item
, cookie
);
1342 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1344 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1345 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1348 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1350 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1351 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1354 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1356 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1359 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1361 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1362 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1364 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1365 if ( next
.IsOk() && !IsVisible(next
) )
1367 // Win32 considers that any non-collapsed item is visible while we want
1368 // to return only really visible items
1375 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1377 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1378 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1380 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1381 if ( prev
.IsOk() && !IsVisible(prev
) )
1383 // just as above, Win32 function will happily return the previous item
1384 // in the tree for the first visible item too
1391 // ----------------------------------------------------------------------------
1392 // multiple selections emulation
1393 // ----------------------------------------------------------------------------
1395 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1397 TraverseSelections
selector(this, selections
);
1399 return selector
.GetCount();
1402 // ----------------------------------------------------------------------------
1404 // ----------------------------------------------------------------------------
1406 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1407 const wxTreeItemId
& hInsertAfter
,
1408 const wxString
& text
,
1409 int image
, int selectedImage
,
1410 wxTreeItemData
*data
)
1412 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1414 _T("can't have more than one root in the tree") );
1416 TV_INSERTSTRUCT tvIns
;
1417 tvIns
.hParent
= HITEM(parent
);
1418 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1420 // this is how we insert the item as the first child: supply a NULL
1422 if ( !tvIns
.hInsertAfter
)
1424 tvIns
.hInsertAfter
= TVI_FIRST
;
1428 if ( !text
.empty() )
1431 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1435 tvIns
.item
.pszText
= NULL
;
1436 tvIns
.item
.cchTextMax
= 0;
1439 // create the param which will store the other item parameters
1440 wxTreeItemParam
*param
= new wxTreeItemParam
;
1442 // we return the images on demand as they depend on whether the item is
1443 // expanded or collapsed too in our case
1444 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1445 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1446 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1448 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1449 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1452 tvIns
.item
.lParam
= (LPARAM
)param
;
1453 tvIns
.item
.mask
= mask
;
1455 // don't use the hack below for the children of hidden root: this results
1456 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1457 const bool firstChild
= !IsHiddenRoot(parent
) &&
1458 !TreeView_GetChild(GetHwnd(), HITEM(parent
));
1460 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1463 wxLogLastError(wxT("TreeView_InsertItem"));
1466 // apparently some Windows versions (2000 and XP are reported to do this)
1467 // sometimes don't refresh the tree after adding the first child and so we
1468 // need this to make the "[+]" appear
1472 TreeView_GetItemRect(GetHwnd(), HITEM(parent
), &rect
, FALSE
);
1473 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1476 // associate the application tree item with Win32 tree item handle
1479 // setup wxTreeItemData
1482 param
->SetData(data
);
1486 return wxTreeItemId(id
);
1489 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1490 int image
, int selectedImage
,
1491 wxTreeItemData
*data
)
1493 if ( HasFlag(wxTR_HIDE_ROOT
) )
1495 wxASSERT_MSG( !m_pVirtualRoot
, _T("tree can have only a single root") );
1497 // create a virtual root item, the parent for all the others
1498 wxTreeItemParam
*param
= new wxTreeItemParam
;
1499 param
->SetData(data
);
1501 m_pVirtualRoot
= new wxVirtualNode(param
);
1506 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1507 text
, image
, selectedImage
, data
);
1510 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1512 const wxString
& text
,
1513 int image
, int selectedImage
,
1514 wxTreeItemData
*data
)
1516 wxTreeItemId idPrev
;
1517 if ( index
== (size_t)-1 )
1519 // special value: append to the end
1522 else // find the item from index
1524 wxTreeItemIdValue cookie
;
1525 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1526 while ( index
!= 0 && idCur
.IsOk() )
1531 idCur
= GetNextChild(parent
, cookie
);
1534 // assert, not check: if the index is invalid, we will append the item
1536 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1539 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1542 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1544 // unlock tree selections on vista, without this the
1545 // tree ctrl will eventually crash after item deletion
1546 TreeItemUnlocker unlock_all
;
1548 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1550 wxLogLastError(wxT("TreeView_DeleteItem"));
1554 // delete all children (but don't delete the item itself)
1555 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1557 // unlock tree selections on vista for the duration of this call
1558 TreeItemUnlocker unlock_all
;
1560 wxTreeItemIdValue cookie
;
1562 wxArrayTreeItemIds children
;
1563 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1564 while ( child
.IsOk() )
1566 children
.Add(child
);
1568 child
= GetNextChild(item
, cookie
);
1571 size_t nCount
= children
.Count();
1572 for ( size_t n
= 0; n
< nCount
; n
++ )
1574 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1576 wxLogLastError(wxT("TreeView_DeleteItem"));
1581 void wxTreeCtrl::DeleteAllItems()
1583 // unlock tree selections on vista for the duration of this call
1584 TreeItemUnlocker unlock_all
;
1586 // delete the "virtual" root item.
1587 if ( GET_VIRTUAL_ROOT() )
1589 delete GET_VIRTUAL_ROOT();
1590 m_pVirtualRoot
= NULL
;
1593 // and all the real items
1595 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1597 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1601 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1603 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1604 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1605 flag
== TVE_EXPAND
||
1607 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1609 // A hidden root can be neither expanded nor collapsed.
1610 wxCHECK_RET( !IsHiddenRoot(item
),
1611 wxT("Can't expand/collapse hidden root node!") );
1613 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1614 // emulate them. This behaviour has changed slightly with comctl32.dll
1615 // v 4.70 - now it does send them but only the first time. To maintain
1616 // compatible behaviour and also in order to not have surprises with the
1617 // future versions, don't rely on this and still do everything ourselves.
1618 // To avoid that the messages be sent twice when the item is expanded for
1619 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1621 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1625 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1627 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1629 wxTreeEvent
event(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1633 (void)HandleWindowEvent(event
);
1635 //else: change didn't took place, so do nothing at all
1638 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1640 DoExpand(item
, TVE_EXPAND
);
1643 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1645 DoExpand(item
, TVE_COLLAPSE
);
1648 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1650 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1653 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1655 DoExpand(item
, TVE_TOGGLE
);
1658 void wxTreeCtrl::Unselect()
1660 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1661 wxT("doesn't make sense, may be you want UnselectAll()?") );
1663 // just remove the selection
1664 SelectItem(wxTreeItemId());
1667 void wxTreeCtrl::UnselectAll()
1669 if ( m_windowStyle
& wxTR_MULTIPLE
)
1671 wxArrayTreeItemIds selections
;
1672 size_t count
= GetSelections(selections
);
1673 for ( size_t n
= 0; n
< count
; n
++ )
1675 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1678 m_htSelStart
.Unset();
1682 // just remove the selection
1687 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1689 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't select hidden root item") );
1691 wxASSERT_MSG( select
|| HasFlag(wxTR_MULTIPLE
),
1692 _T("SelectItem(false) works only for multiselect") );
1694 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1695 if ( !HandleWindowEvent(event
) || event
.IsAllowed() )
1697 if ( HasFlag(wxTR_MULTIPLE
) )
1699 if ( !::SelectItem(GetHwnd(), HITEM(item
), select
) )
1701 wxLogLastError(wxT("TreeView_SelectItem"));
1705 else // single selection
1707 // use TreeView_SelectItem() to deselect the previous selection
1708 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1710 wxLogLastError(wxT("TreeView_SelectItem"));
1715 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1716 (void)HandleWindowEvent(event
);
1718 //else: program vetoed the change
1721 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1723 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't show hidden root item") );
1726 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1729 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1731 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1733 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1737 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1742 void wxTreeCtrl::DeleteTextCtrl()
1746 // the HWND corresponding to this control is deleted by the tree
1747 // control itself and we don't know when exactly this happens, so check
1748 // if the window still exists before calling UnsubclassWin()
1749 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1751 m_textCtrl
->SetHWND(0);
1754 m_textCtrl
->UnsubclassWin();
1755 m_textCtrl
->SetHWND(0);
1763 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1764 wxClassInfo
*textControlClass
)
1766 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1771 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1772 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1774 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1783 // textctrl is subclassed in MSWOnNotify
1787 // End label editing, optionally cancelling the edit
1788 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1790 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1795 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1797 TV_HITTESTINFO hitTestInfo
;
1798 hitTestInfo
.pt
.x
= (int)point
.x
;
1799 hitTestInfo
.pt
.y
= (int)point
.y
;
1801 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1806 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1807 flags |= wxTREE_HITTEST_##flag
1809 TRANSLATE_FLAG(ABOVE
);
1810 TRANSLATE_FLAG(BELOW
);
1811 TRANSLATE_FLAG(NOWHERE
);
1812 TRANSLATE_FLAG(ONITEMBUTTON
);
1813 TRANSLATE_FLAG(ONITEMICON
);
1814 TRANSLATE_FLAG(ONITEMINDENT
);
1815 TRANSLATE_FLAG(ONITEMLABEL
);
1816 TRANSLATE_FLAG(ONITEMRIGHT
);
1817 TRANSLATE_FLAG(ONITEMSTATEICON
);
1818 TRANSLATE_FLAG(TOLEFT
);
1819 TRANSLATE_FLAG(TORIGHT
);
1821 #undef TRANSLATE_FLAG
1823 return wxTreeItemId(hitTestInfo
.hItem
);
1826 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1828 bool textOnly
) const
1832 // Virtual root items have no bounding rectangle
1833 if ( IS_VIRTUAL_ROOT(item
) )
1838 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1841 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1847 // couldn't retrieve rect: for example, item isn't visible
1852 // ----------------------------------------------------------------------------
1854 // ----------------------------------------------------------------------------
1856 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1857 // functions such as IsDataIndirect()
1858 class wxTreeSortHelper
1861 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1864 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
1866 return ((wxTreeItemParam
*)lParam
)->GetItem();
1870 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1874 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1875 wxT("sorting tree without data doesn't make sense") );
1877 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1879 return tree
->OnCompareItems(GetIdFromData(pItem1
),
1880 GetIdFromData(pItem2
));
1883 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1885 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1887 // rely on the fact that TreeView_SortChildren does the same thing as our
1888 // default behaviour, i.e. sorts items alphabetically and so call it
1889 // directly if we're not in derived class (much more efficient!)
1890 // RN: Note that if you find you're code doesn't sort as expected this
1891 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1892 // combo for your derived wxTreeCtrl if will sort without
1894 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1896 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1901 tvSort
.hParent
= HITEM(item
);
1902 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1903 tvSort
.lParam
= (LPARAM
)this;
1904 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1908 // ----------------------------------------------------------------------------
1910 // ----------------------------------------------------------------------------
1912 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1914 if ( msg
->message
== WM_KEYDOWN
)
1916 // Only eat VK_RETURN if not being used by the application in
1917 // conjunction with modifiers
1918 if ( (msg
->wParam
== VK_RETURN
) && !wxIsAnyModifierDown() )
1920 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
1925 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
1928 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
1930 const int id
= (signed short)id_
;
1932 if ( cmd
== EN_UPDATE
)
1934 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1935 event
.SetEventObject( this );
1936 ProcessCommand(event
);
1938 else if ( cmd
== EN_KILLFOCUS
)
1940 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1941 event
.SetEventObject( this );
1942 ProcessCommand(event
);
1950 // command processed
1954 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1955 // only do it during dragging, minimize wxWin overhead (this is important for
1956 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1957 // instead of passing by wxWin events
1958 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1960 bool processed
= false;
1962 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
1964 // This message is sent after a right-click, or when the "menu" key is pressed
1965 if ( nMsg
== WM_CONTEXTMENU
)
1967 int x
= GET_X_LPARAM(lParam
),
1968 y
= GET_Y_LPARAM(lParam
);
1970 // the item for which the menu should be shown
1973 // the position where the menu should be shown in client coordinates
1974 // (so that it can be passed directly to PopupMenu())
1977 if ( x
== -1 || y
== -1 )
1979 // this means that the event was generated from keyboard (e.g. with
1980 // Shift-F10 or special Windows menu key)
1982 // use the Explorer standard of putting the menu at the left edge
1983 // of the text, in the vertical middle of the text
1984 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1987 // Use the bounding rectangle of only the text part
1989 GetBoundingRect(item
, rect
, true);
1990 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
1993 else // event from mouse, use mouse position
1995 pt
= ScreenToClient(wxPoint(x
, y
));
1997 TV_HITTESTINFO tvhti
;
2000 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2001 item
= wxTreeItemId(tvhti
.hItem
);
2005 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2007 event
.m_pointDrag
= pt
;
2009 if ( HandleWindowEvent(event
) )
2011 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2013 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2015 // we only process mouse messages here and these parameters have the
2016 // same meaning for all of them
2017 int x
= GET_X_LPARAM(lParam
),
2018 y
= GET_Y_LPARAM(lParam
);
2020 TV_HITTESTINFO tvht
;
2024 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2028 case WM_LBUTTONDOWN
:
2029 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2031 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2032 m_ptClick
= wxPoint(x
, y
);
2034 if ( wParam
& MK_CONTROL
)
2038 // toggle selected state
2039 ToggleItemSelection(htItem
);
2041 ::SetFocus(GetHwnd(), htItem
);
2043 // reset on any click without Shift
2044 m_htSelStart
.Unset();
2048 else if ( wParam
& MK_SHIFT
)
2050 // this selects all items between the starting one and
2053 if ( !m_htSelStart
)
2055 // take the focused item
2056 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2060 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2061 !(wParam
& MK_CONTROL
));
2063 ::SelectItem(GetHwnd(), htItem
);
2065 ::SetFocus(GetHwnd(), htItem
);
2069 else // normal click
2071 // avoid doing anything if we click on the only
2072 // currently selected item
2076 wxArrayTreeItemIds selections
;
2077 size_t count
= GetSelections(selections
);
2080 HITEM(selections
[0]) != htItem
)
2082 // clear the previously selected items, if the
2083 // user clicked outside of the present selection.
2084 // otherwise, perform the deselection on mouse-up.
2085 // this allows multiple drag and drop to work.
2087 if (!IsItemSelected(GetHwnd(), htItem
))
2091 // prevent the click from starting in-place editing
2092 // which should only happen if we click on the
2093 // already selected item (and nothing else is
2096 TreeView_SelectItem(GetHwnd(), 0);
2097 ::SelectItem(GetHwnd(), htItem
);
2099 ::SetFocus(GetHwnd(), htItem
);
2102 else // click on a single selected item
2104 // don't interfere with the default processing in
2105 // WM_MOUSEMOVE handler below as the default window
2106 // proc will start the drag itself if we let have
2108 m_htClickedItem
.Unset();
2111 // reset on any click without Shift
2112 m_htSelStart
.Unset();
2117 case WM_RBUTTONDOWN
:
2118 // default handler removes the highlight from the currently
2119 // focused item when right mouse button is pressed on another
2120 // one but keeps the remaining items highlighted, which is
2121 // confusing, so override this default behaviour for tree with
2122 // multiple selections
2125 if ( !IsItemSelected(GetHwnd(), htItem
) )
2129 ::SetFocus(GetHwnd(), htItem
);
2132 // fire EVT_RIGHT_DOWN
2133 HandleMouseEvent(nMsg
, x
, y
, wParam
);
2137 nmhdr
.hwndFrom
= GetHwnd();
2138 nmhdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2139 nmhdr
.code
= NM_RCLICK
;
2140 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY
,
2141 nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
2143 // prevent tree control default processing, as we've
2144 // already done everything
2151 if ( m_htClickedItem
)
2153 int cx
= abs(m_ptClick
.x
- x
);
2154 int cy
= abs(m_ptClick
.y
- y
);
2156 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2157 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2162 tv
.hdr
.hwndFrom
= GetHwnd();
2163 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2164 tv
.hdr
.code
= TVN_BEGINDRAG
;
2166 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2170 wxZeroMemory(tviAux
);
2172 tviAux
.hItem
= HITEM(m_htClickedItem
);
2173 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2174 tviAux
.stateMask
= 0xffffffff;
2175 TreeView_GetItem(GetHwnd(), &tviAux
);
2177 tv
.itemNew
.state
= tviAux
.state
;
2178 tv
.itemNew
.lParam
= tviAux
.lParam
;
2183 // do it before SendMessage() call below to avoid
2184 // reentrancies here if there is another WM_MOUSEMOVE
2185 // in the queue already
2186 m_htClickedItem
.Unset();
2188 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
2189 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2191 // don't pass it to the default window proc, it would
2192 // start dragging again
2196 #endif // __WXWINCE__
2201 m_dragImage
->Move(wxPoint(x
, y
));
2204 // highlight the item as target (hiding drag image is
2205 // necessary - otherwise the display will be corrupted)
2206 m_dragImage
->Hide();
2207 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2208 m_dragImage
->Show();
2211 #endif // wxUSE_DRAGIMAGE
2216 // facilitates multiple drag-and-drop
2217 if (htItem
&& isMultiple
)
2219 wxArrayTreeItemIds selections
;
2220 size_t count
= GetSelections(selections
);
2223 !(wParam
& MK_CONTROL
) &&
2224 !(wParam
& MK_SHIFT
))
2227 TreeView_SelectItem(GetHwnd(), htItem
);
2228 ::SelectItem(GetHwnd(), htItem
);
2229 ::SetFocus(GetHwnd(), htItem
);
2231 m_htClickedItem
.Unset();
2240 m_dragImage
->EndDrag();
2244 // generate the drag end event
2245 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, this, htItem
);
2246 event
.m_pointDrag
= wxPoint(x
, y
);
2248 (void)HandleWindowEvent(event
);
2250 // if we don't do it, the tree seems to think that 2 items
2251 // are selected simultaneously which is quite weird
2252 TreeView_SelectDropTarget(GetHwnd(), 0);
2254 #endif // wxUSE_DRAGIMAGE
2258 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2260 // the tree control greys out the selected item when it loses focus and
2261 // paints it as selected again when it regains it, but it won't do it
2262 // for the other items itself - help it
2263 wxArrayTreeItemIds selections
;
2264 size_t count
= GetSelections(selections
);
2266 for ( size_t n
= 0; n
< count
; n
++ )
2268 // TreeView_GetItemRect() will return false if item is not visible,
2269 // which may happen perfectly well
2270 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2273 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2277 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2279 bool bCtrl
= wxIsCtrlDown(),
2280 bShift
= wxIsShiftDown();
2282 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2288 ToggleItemSelection(htSel
);
2294 ::SelectItem(GetHwnd(), htSel
);
2302 if ( !bCtrl
&& !bShift
)
2304 // no modifiers, just clear selection and then let the default
2305 // processing to take place
2310 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2312 HTREEITEM htNext
= (HTREEITEM
)
2313 TreeView_GetNextItem
2317 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2323 // at the top/bottom
2329 if ( !m_htSelStart
)
2330 m_htSelStart
= htSel
;
2332 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2336 // without changing selection
2337 ::SetFocus(GetHwnd(), htNext
);
2348 // TODO: handle Shift/Ctrl with these keys
2349 if ( !bCtrl
&& !bShift
)
2353 m_htSelStart
.Unset();
2357 else if ( nMsg
== WM_COMMAND
)
2359 // if we receive a EN_KILLFOCUS command from the in-place edit control
2360 // used for label editing, make sure to end editing
2363 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2365 if ( cmd
== EN_KILLFOCUS
)
2367 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2377 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2383 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2385 if ( nMsg
== WM_CHAR
)
2387 // don't let the control process Space and Return keys because it
2388 // doesn't do anything useful with them anyhow but always beeps
2389 // annoyingly when it receives them and there is no way to turn it off
2390 // simply if you just process TREEITEM_ACTIVATED event to which Space
2391 // and Enter presses are mapped in your code
2392 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2396 else if ( nMsg
== WM_KEYDOWN
)
2398 if ( wParam
== VK_ESCAPE
)
2402 m_dragImage
->EndDrag();
2406 // if we don't do it, the tree seems to think that 2 items
2407 // are selected simultaneously which is quite weird
2408 TreeView_SelectDropTarget(GetHwnd(), 0);
2412 #endif // wxUSE_DRAGIMAGE
2414 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2417 // process WM_NOTIFY Windows message
2418 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2420 wxTreeEvent
event(wxEVT_NULL
, this);
2421 wxEventType eventType
= wxEVT_NULL
;
2422 NMHDR
*hdr
= (NMHDR
*)lParam
;
2424 switch ( hdr
->code
)
2427 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2430 case TVN_BEGINRDRAG
:
2432 if ( eventType
== wxEVT_NULL
)
2433 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2434 //else: left drag, already set above
2436 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2438 event
.m_item
= tv
->itemNew
.hItem
;
2439 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2441 // don't allow dragging by default: the user code must
2442 // explicitly say that it wants to allow it to avoid breaking
2448 case TVN_BEGINLABELEDIT
:
2450 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2451 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2453 // although the user event handler may still veto it, it is
2454 // important to set it now so that calls to SetItemText() from
2455 // the event handler would change the text controls contents
2457 event
.m_item
= info
->item
.hItem
;
2458 event
.m_label
= info
->item
.pszText
;
2459 event
.m_editCancelled
= false;
2463 case TVN_DELETEITEM
:
2465 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2466 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2468 event
.m_item
= tv
->itemOld
.hItem
;
2472 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2473 if ( it
!= m_attrs
.end() )
2482 case TVN_ENDLABELEDIT
:
2484 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2485 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2487 event
.m_item
= info
->item
.hItem
;
2488 event
.m_label
= info
->item
.pszText
;
2489 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2494 // These *must* not be removed or TVN_GETINFOTIP will
2495 // not be processed each time the mouse is moved
2496 // and the tooltip will only ever update once.
2505 #ifdef TVN_GETINFOTIP
2506 case TVN_GETINFOTIP
:
2508 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2509 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2511 // Which item are we trying to get a tooltip for?
2512 event
.m_item
= info
->hItem
;
2516 #endif // TVN_GETINFOTIP
2517 #endif // !__WXWINCE__
2519 case TVN_GETDISPINFO
:
2520 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2523 case TVN_SETDISPINFO
:
2525 if ( eventType
== wxEVT_NULL
)
2526 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2527 //else: get, already set above
2529 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2531 event
.m_item
= info
->item
.hItem
;
2535 case TVN_ITEMEXPANDING
:
2536 case TVN_ITEMEXPANDED
:
2538 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2541 switch ( tv
->action
)
2544 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2552 what
= IDX_COLLAPSE
;
2556 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2559 eventType
= gs_expandEvents
[what
][how
];
2561 event
.m_item
= tv
->itemNew
.hItem
;
2567 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2568 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2570 // fabricate the lParam and wParam parameters sufficiently
2571 // similar to the ones from a "real" WM_KEYDOWN so that
2572 // CreateKeyEvent() works correctly
2573 WXLPARAM lParam
= (wxIsAltDown() ? KF_ALTDOWN
: 0) << 16;
2575 WXWPARAM wParam
= info
->wVKey
;
2577 int keyCode
= wxCharCodeMSWToWX(wParam
);
2580 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2585 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2590 // a separate event for Space/Return
2591 if ( !wxIsAnyModifierDown() &&
2592 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2595 if ( !HasFlag(wxTR_MULTIPLE
) )
2596 item
= GetSelection();
2598 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2600 (void)HandleWindowEvent(event2
);
2606 // Vista's tree control has introduced some problems with our
2607 // multi-selection tree. When TreeView_SelectItem() is called,
2608 // the wrong items are deselected.
2610 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
2611 // that can be used to regulate this incorrect behavior. The
2612 // following messages will allow only the unlocked item's selection
2615 case TVN_ITEMCHANGINGA
:
2616 case TVN_ITEMCHANGINGW
:
2618 // we only need to handles these in multi-select trees
2619 if ( HasFlag(wxTR_MULTIPLE
) )
2621 // get info about the item about to be changed
2622 NMTVITEMCHANGE
* info
= (NMTVITEMCHANGE
*)lParam
;
2623 if (TreeItemUnlocker::IsLocked(info
->hItem
))
2625 // item's state is locked, don't allow the change
2626 // returning 1 will disallow the change
2632 // allow the state change
2636 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2637 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2638 // we have to handle both messages:
2639 case TVN_SELCHANGEDA
:
2640 case TVN_SELCHANGEDW
:
2641 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2644 case TVN_SELCHANGINGA
:
2645 case TVN_SELCHANGINGW
:
2647 if ( eventType
== wxEVT_NULL
)
2648 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2649 //else: already set above
2651 if (hdr
->code
== TVN_SELCHANGINGW
||
2652 hdr
->code
== TVN_SELCHANGEDW
)
2654 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
2655 event
.m_item
= tv
->itemNew
.hItem
;
2656 event
.m_itemOld
= tv
->itemOld
.hItem
;
2660 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
2661 event
.m_item
= tv
->itemNew
.hItem
;
2662 event
.m_itemOld
= tv
->itemOld
.hItem
;
2666 // we receive this message from WM_LBUTTONDOWN handler inside
2667 // comctl32.dll and so before the click is passed to
2668 // DefWindowProc() which sets the focus to the window which was
2669 // clicked and this can lead to unexpected event sequences: for
2670 // example, we may get a "selection change" event from the tree
2671 // before getting a "kill focus" event for the text control which
2672 // had the focus previously, thus breaking user code doing input
2675 // to avoid such surprises, we force the generation of focus events
2676 // now, before we generate the selection change ones
2680 // instead of explicitly checking for _WIN32_IE, check if the
2681 // required symbols are available in the headers
2682 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2685 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2686 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2687 switch ( nmcd
.dwDrawStage
)
2690 // if we've got any items with non standard attributes,
2691 // notify us before painting each item
2692 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2695 // windows in TreeCtrl use one-based index for item state images,
2696 // 0 indexed image is not being used, we're using zero-based index,
2697 // so we have to add temp image (of zero index) to state image list
2698 // before we draw any item, then after items are drawn we have to
2699 // delete it (in POSTPAINT notify)
2700 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
2702 #define hImageList (HIMAGELIST)m_imageListState->GetHIMAGELIST()
2704 // add temporary image
2706 m_imageListState
->GetSize(0, width
, height
);
2708 HBITMAP hbmpTemp
= ::CreateBitmap(width
, height
, 1, 1, NULL
);
2709 int index
= ::ImageList_Add(hImageList
, hbmpTemp
, hbmpTemp
);
2710 ::DeleteObject(hbmpTemp
);
2714 // move images to right
2715 for ( int i
= index
; i
> 0; i
-- )
2716 ::ImageList_Copy(hImageList
, i
, hImageList
, i
-1, 0);
2718 // we must remove the image in POSTPAINT notify
2719 *result
|= CDRF_NOTIFYPOSTPAINT
;
2726 case CDDS_POSTPAINT
:
2727 // we are deleting temp image of 0 index, which was
2728 // added before items were drawn (in PREPAINT notify)
2729 if (m_imageListState
&& m_imageListState
->GetImageCount() > 0)
2730 m_imageListState
->Remove(0);
2733 case CDDS_ITEMPREPAINT
:
2735 wxMapTreeAttr::iterator
2736 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2738 if ( it
== m_attrs
.end() )
2740 // nothing to do for this item
2741 *result
= CDRF_DODEFAULT
;
2745 wxTreeItemAttr
* const attr
= it
->second
;
2747 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
2748 TVIF_STATE
, TVIS_DROPHILITED
);
2750 const UINT tvItemState
= tvItem
.state
;
2752 // selection colours should override ours,
2753 // otherwise it is too confusing to the user
2754 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
2755 !(tvItemState
& TVIS_DROPHILITED
) )
2758 if ( attr
->HasBackgroundColour() )
2760 colBack
= attr
->GetBackgroundColour();
2761 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2765 // but we still want to keep the special foreground
2766 // colour when we don't have focus (we can't keep
2767 // it when we do, it would usually be unreadable on
2768 // the almost inverted bg colour...)
2769 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2770 FindFocus() != this ) &&
2771 !(tvItemState
& TVIS_DROPHILITED
) )
2774 if ( attr
->HasTextColour() )
2776 colText
= attr
->GetTextColour();
2777 lptvcd
->clrText
= wxColourToRGB(colText
);
2781 if ( attr
->HasFont() )
2783 HFONT hFont
= GetHfontOf(attr
->GetFont());
2785 ::SelectObject(nmcd
.hdc
, hFont
);
2787 *result
= CDRF_NEWFONT
;
2789 else // no specific font
2791 *result
= CDRF_DODEFAULT
;
2797 *result
= CDRF_DODEFAULT
;
2801 // we always process it
2803 #endif // have owner drawn support in headers
2807 DWORD pos
= GetMessagePos();
2809 point
.x
= LOWORD(pos
);
2810 point
.y
= HIWORD(pos
);
2811 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2813 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2814 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2816 event
.m_item
= item
;
2817 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2825 TV_HITTESTINFO tvhti
;
2826 ::GetCursorPos(&tvhti
.pt
);
2827 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2828 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2830 if ( tvhti
.flags
& TVHT_ONITEM
)
2832 event
.m_item
= tvhti
.hItem
;
2833 eventType
= (int)hdr
->code
== NM_DBLCLK
2834 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2835 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2837 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2838 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2847 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2850 event
.SetEventType(eventType
);
2852 if ( event
.m_item
.IsOk() )
2853 event
.SetClientObject(GetItemData(event
.m_item
));
2855 bool processed
= HandleWindowEvent(event
);
2858 switch ( hdr
->code
)
2861 // we translate NM_DBLCLK into ACTIVATED event and if the user
2862 // handled the activation of the item we shouldn't proceed with
2863 // also using the same double click for toggling the item expanded
2864 // state -- but OTOH do let the user to expand/collapse the item by
2865 // double clicking on it if the activation is not handled specially
2866 *result
= processed
;
2870 // prevent tree control from sending WM_CONTEXTMENU to our parent
2871 // (which it does if NM_RCLICK is not handled) because we want to
2872 // send it to the control itself
2876 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
2877 (WPARAM
)GetHwnd(), ::GetMessagePos());
2881 case TVN_BEGINRDRAG
:
2883 if ( event
.IsAllowed() )
2885 // normally this is impossible because the m_dragImage is
2886 // deleted once the drag operation is over
2887 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2889 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2890 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2891 m_dragImage
->Show();
2893 #endif // wxUSE_DRAGIMAGE
2896 case TVN_DELETEITEM
:
2898 // NB: we might process this message using wxWidgets event
2899 // tables, but due to overhead of wxWin event system we
2900 // prefer to do it here ourself (otherwise deleting a tree
2901 // with many items is just too slow)
2902 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2904 wxTreeItemParam
*param
=
2905 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
2908 processed
= true; // Make sure we don't get called twice
2912 case TVN_BEGINLABELEDIT
:
2913 // return true to cancel label editing
2914 *result
= !event
.IsAllowed();
2916 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2917 if ( event
.IsAllowed() )
2919 HWND hText
= TreeView_GetEditControl(GetHwnd());
2922 // MBN: if m_textCtrl already has an HWND, it is a stale
2923 // pointer from a previous edit (because the user
2924 // didn't modify the label before dismissing the control,
2925 // and TVN_ENDLABELEDIT was not sent), so delete it
2926 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
2929 m_textCtrl
= new wxTextCtrl();
2930 m_textCtrl
->SetParent(this);
2931 m_textCtrl
->SetHWND((WXHWND
)hText
);
2932 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2934 // set wxTE_PROCESS_ENTER style for the text control to
2935 // force it to process the Enter presses itself, otherwise
2936 // they could be stolen from it by the dialog
2938 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2939 | wxTE_PROCESS_ENTER
);
2942 else // we had set m_idEdited before
2948 case TVN_ENDLABELEDIT
:
2949 // return true to set the label to the new string: note that we
2950 // also must pretend that we did process the message or it is going
2951 // to be passed to DefWindowProc() which will happily return false
2952 // cancelling the label change
2953 *result
= event
.IsAllowed();
2956 // ensure that we don't have the text ctrl which is going to be
2962 #ifdef TVN_GETINFOTIP
2963 case TVN_GETINFOTIP
:
2965 // If the user permitted a tooltip change, change it
2966 if (event
.IsAllowed())
2968 SetToolTip(event
.m_label
);
2975 case TVN_SELCHANGING
:
2976 case TVN_ITEMEXPANDING
:
2977 // return true to prevent the action from happening
2978 *result
= !event
.IsAllowed();
2981 case TVN_ITEMEXPANDED
:
2983 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2984 const wxTreeItemId
id(tv
->itemNew
.hItem
);
2986 if ( tv
->action
== TVE_COLLAPSE
)
2988 if ( wxApp::GetComCtl32Version() >= 600 )
2990 // for some reason the item selection rectangle depends
2991 // on whether it is expanded or collapsed (at least
2992 // with comctl32.dll v6): it is wider (by 3 pixels) in
2993 // the expanded state, so when the item collapses and
2994 // then is deselected the rightmost 3 pixels of the
2995 // previously drawn selection are left on the screen
2997 // it's not clear if it's a bug in comctl32.dll or in
2998 // our code (because it does not happen in Explorer but
2999 // OTOH we don't do anything which could result in this
3000 // AFAICS) but we do need to work around it to avoid
3007 // the item is also not refreshed properly after expansion when
3008 // it has an image depending on the expanded/collapsed state:
3009 // again, it's not clear if the bug is in comctl32.dll or our
3011 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3020 case TVN_GETDISPINFO
:
3021 // NB: so far the user can't set the image himself anyhow, so do it
3022 // anyway - but this may change later
3023 //if ( /* !processed && */ )
3025 wxTreeItemId item
= event
.m_item
;
3026 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3028 const wxTreeItemParam
* const param
= GetItemParam(item
);
3032 if ( info
->item
.mask
& TVIF_IMAGE
)
3037 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3038 : wxTreeItemIcon_Normal
3041 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3043 info
->item
.iSelectedImage
=
3046 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3047 : wxTreeItemIcon_Selected
3054 // for the other messages the return value is ignored and there is
3055 // nothing special to do
3060 // ----------------------------------------------------------------------------
3062 // ----------------------------------------------------------------------------
3064 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3065 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3067 int wxTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
3069 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
3071 // receive the desired information
3072 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3075 // state images are one-based
3076 return STATEIMAGEMASKTOINDEX(tvItem
.state
) - 1;
3079 void wxTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
3081 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
3083 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
3085 // state images are one-based
3086 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3087 tvItem
.state
= INDEXTOSTATEIMAGEMASK(state
+ 1);
3092 #endif // wxUSE_TREECTRL