1 /////////////////////////////////////////////////////////////////////////////
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 #pragma implementation "treectrl.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
30 #include "wx/msw/private.h"
32 // Mingw32 is a bit mental even though this is done in winundef
41 #if defined(__WIN95__)
44 #include "wx/dynarray.h"
45 #include "wx/imaglist.h"
46 #include "wx/treectrl.h"
47 #include "wx/settings.h"
49 #include "wx/msw/dragimag.h"
51 #ifdef __GNUWIN32_OLD__
52 #include "wx/msw/gnuwin32/extra.h"
55 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
59 // Bug in headers, sometimes
61 #define TVIS_FOCUSED 0x0001
65 #define TV_FIRST 0x1100
68 #ifndef TVS_CHECKBOXES
69 #define TVS_CHECKBOXES 0x0100
72 // old headers might miss these messages (comctl32.dll 4.71+ only)
73 #ifndef TVM_SETBKCOLOR
74 #define TVM_SETBKCOLOR (TV_FIRST + 29)
75 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
78 // a macro to hide the ugliness of nested casts
79 #define HITEM(item) (HTREEITEM)(WXHTREEITEM)(item)
81 // the native control doesn't support multiple selections under MSW and we
82 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
83 // checkboxes be the selection status (checked == selected) or by really
84 // emulating everything, i.e. intercepting mouse and key events &c. The first
85 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
87 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // wrapper for TreeView_HitTest
94 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
100 // TreeView_HitTest() doesn't do the right cast in mingw32 headers
101 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
104 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
106 // wrappers for TreeView_GetItem/TreeView_SetItem
107 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
110 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
111 tvi
.stateMask
= TVIS_SELECTED
;
114 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
116 wxLogLastError("TreeView_GetItem");
119 return (tvi
.state
& TVIS_SELECTED
) != 0;
122 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= TRUE
)
125 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
126 tvi
.stateMask
= TVIS_SELECTED
;
127 tvi
.state
= select
? TVIS_SELECTED
: 0;
130 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
132 wxLogLastError("TreeView_SetItem");
136 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
138 SelectItem(hwndTV
, htItem
, FALSE
);
141 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
143 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
146 // helper function which selects all items in a range and, optionally,
147 // unselects all others
148 static void SelectRange(HWND hwndTV
,
151 bool unselectOthers
= TRUE
)
153 // find the first (or last) item and select it
155 HTREEITEM htItem
= TreeView_GetRoot(hwndTV
);
156 while ( htItem
&& cont
)
158 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
160 if ( !IsItemSelected(hwndTV
, htItem
) )
162 SelectItem(hwndTV
, htItem
);
169 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
171 UnselectItem(hwndTV
, htItem
);
175 htItem
= TreeView_GetNextVisible(hwndTV
, htItem
);
178 // select the items in range
179 cont
= htFirst
!= htLast
;
180 while ( htItem
&& cont
)
182 if ( !IsItemSelected(hwndTV
, htItem
) )
184 SelectItem(hwndTV
, htItem
);
187 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
189 htItem
= TreeView_GetNextVisible(hwndTV
, htItem
);
193 if ( unselectOthers
)
197 if ( IsItemSelected(hwndTV
, htItem
) )
199 UnselectItem(hwndTV
, htItem
);
202 htItem
= TreeView_GetNextVisible(hwndTV
, htItem
);
206 // seems to be necessary - otherwise the just selected items don't always
207 // appear as selected
208 UpdateWindow(hwndTV
);
211 // helper function which tricks the standard control into changing the focused
212 // item without changing anything else (if someone knows why Microsoft doesn't
213 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
214 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
217 HTREEITEM htFocus
= TreeView_GetSelection(hwndTV
);
222 if ( htItem
!= htFocus
)
224 // remember the selection state of the item
225 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
227 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
229 // prevent the tree from unselecting the old focus which it
230 // would do by default (TreeView_SelectItem unselects the
232 TreeView_SelectItem(hwndTV
, 0);
233 SelectItem(hwndTV
, htFocus
);
236 TreeView_SelectItem(hwndTV
, htItem
);
240 // need to clear the selection which TreeView_SelectItem() gave
242 UnselectItem(hwndTV
, htItem
);
244 //else: was selected, still selected - ok
246 //else: nothing to do, focus already there
252 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
254 // just clear the focus
255 TreeView_SelectItem(hwndTV
, 0);
257 if ( wasFocusSelected
)
259 // restore the selection state
260 SelectItem(hwndTV
, htFocus
);
263 //else: nothing to do, no focus already
267 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 // a convenient wrapper around TV_ITEM struct which adds a ctor
275 #pragma warning( disable : 4097 ) // inheriting from typedef
278 struct wxTreeViewItem
: public TV_ITEM
280 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
281 UINT mask_
, // fields which are valid
282 UINT stateMask_
= 0) // for TVIF_STATE only
284 // hItem member is always valid
285 mask
= mask_
| TVIF_HANDLE
;
286 stateMask
= stateMask_
;
292 #pragma warning( default : 4097 )
295 // a class which encapsulates the tree traversal logic: it vists all (unless
296 // OnVisit() returns FALSE) items under the given one
297 class wxTreeTraversal
300 wxTreeTraversal(const wxTreeCtrl
*tree
)
305 // do traverse the tree: visit all items (recursively by default) under the
306 // given one; return TRUE if all items were traversed or FALSE if the
307 // traversal was aborted because OnVisit returned FALSE
308 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
310 // override this function to do whatever is needed for each item, return
311 // FALSE to stop traversing
312 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
315 const wxTreeCtrl
*GetTree() const { return m_tree
; }
318 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
320 const wxTreeCtrl
*m_tree
;
323 // internal class for getting the selected items
324 class TraverseSelections
: public wxTreeTraversal
327 TraverseSelections(const wxTreeCtrl
*tree
,
328 wxArrayTreeItemIds
& selections
)
329 : wxTreeTraversal(tree
), m_selections(selections
)
331 m_selections
.Empty();
333 DoTraverse(tree
->GetRootItem());
336 virtual bool OnVisit(const wxTreeItemId
& item
)
338 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
339 if ( GetTree()->IsItemChecked(item
) )
341 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
344 m_selections
.Add(item
);
350 size_t GetCount() const { return m_selections
.GetCount(); }
353 wxArrayTreeItemIds
& m_selections
;
356 // internal class for counting tree items
357 class TraverseCounter
: public wxTreeTraversal
360 TraverseCounter(const wxTreeCtrl
*tree
,
361 const wxTreeItemId
& root
,
363 : wxTreeTraversal(tree
)
367 DoTraverse(root
, recursively
);
370 virtual bool OnVisit(const wxTreeItemId
& item
)
377 size_t GetCount() const { return m_count
; }
383 // ----------------------------------------------------------------------------
384 // This class is needed for support of different images: the Win32 common
385 // control natively supports only 2 images (the normal one and another for the
386 // selected state). We wish to provide support for 2 more of them for folder
387 // items (i.e. those which have children): for expanded state and for expanded
388 // selected state. For this we use this structure to store the additional items
391 // There is only one problem with this: when we retrieve the item's data, we
392 // don't know whether we get a pointer to wxTreeItemData or
393 // wxTreeItemIndirectData. So we have to maintain a list of all items which
394 // have indirect data inside the listctrl itself.
395 // ----------------------------------------------------------------------------
397 class wxTreeItemIndirectData
400 // ctor associates this data with the item and the real item data becomes
401 // available through our GetData() method
402 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
404 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
410 m_data
= tree
->GetItemData(item
);
412 // and set ourselves as the new one
413 tree
->SetIndirectItemData(item
, this);
416 // dtor deletes the associated data as well
417 ~wxTreeItemIndirectData() { delete m_data
; }
420 // get the real data associated with the item
421 wxTreeItemData
*GetData() const { return m_data
; }
423 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
425 // do we have such image?
426 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
428 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
430 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
433 // all the images associated with the item
434 int m_images
[wxTreeItemIcon_Max
];
436 wxTreeItemData
*m_data
;
439 // ----------------------------------------------------------------------------
441 // ----------------------------------------------------------------------------
443 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
445 // ----------------------------------------------------------------------------
447 // ----------------------------------------------------------------------------
449 // handy table for sending events
450 static const wxEventType g_events
[2][2] =
452 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
453 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
456 // ============================================================================
458 // ============================================================================
460 // ----------------------------------------------------------------------------
462 // ----------------------------------------------------------------------------
464 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
466 if ( !OnVisit(root
) )
469 return Traverse(root
, recursively
);
472 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
475 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
476 while ( child
.IsOk() )
478 // depth first traversal
479 if ( recursively
&& !Traverse(child
, TRUE
) )
482 if ( !OnVisit(child
) )
485 child
= m_tree
->GetNextChild(root
, cookie
);
491 // ----------------------------------------------------------------------------
492 // construction and destruction
493 // ----------------------------------------------------------------------------
495 void wxTreeCtrl::Init()
497 m_imageListNormal
= NULL
;
498 m_imageListState
= NULL
;
500 m_hasAnyAttr
= FALSE
;
506 bool wxTreeCtrl::Create(wxWindow
*parent
,
511 const wxValidator
& validator
,
512 const wxString
& name
)
516 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
519 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
520 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
522 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
523 wstyle
|= TVS_HASBUTTONS
;
525 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
526 wstyle
|= TVS_EDITLABELS
;
528 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
529 wstyle
|= TVS_LINESATROOT
;
531 // using TVS_CHECKBOXES for emulation of a multiselection tree control
532 // doesn't work without the new enough headers
533 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
534 !defined( __GNUWIN32_OLD__ ) && \
535 !defined( __BORLANDC__ ) && \
536 !defined( __WATCOMC__ ) && \
537 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
539 // we emulate the multiple selection tree controls by using checkboxes: set
540 // up the image list we need for this if we do have multiple selections
541 if ( m_windowStyle
& wxTR_MULTIPLE
)
542 wstyle
|= TVS_CHECKBOXES
;
543 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
545 // Create the tree control.
546 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
549 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
550 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
552 // VZ: this is some experimental code which may be used to get the
553 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
554 // AFAIK, the standard DLL does about the same thing anyhow.
556 if ( m_windowStyle
& wxTR_MULTIPLE
)
560 // create the DC compatible with the current screen
561 HDC hdcMem
= CreateCompatibleDC(NULL
);
563 // create a mono bitmap of the standard size
564 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
565 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
566 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
567 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
568 1, // # of color planes
569 1, // # bits needed for one pixel
570 0); // array containing colour data
571 SelectObject(hdcMem
, hbmpCheck
);
573 // then draw a check mark into it
574 RECT rect
= { 0, 0, x
, y
};
575 if ( !::DrawFrameControl(hdcMem
, &rect
,
577 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
579 wxLogLastError(wxT("DrawFrameControl(check)"));
582 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
583 imagelistCheckboxes
.Add(bmp
);
585 if ( !::DrawFrameControl(hdcMem
, &rect
,
589 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
592 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
593 imagelistCheckboxes
.Add(bmp
);
599 SetStateImageList(&imagelistCheckboxes
);
603 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
608 wxTreeCtrl::~wxTreeCtrl()
610 // delete any attributes
613 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
615 delete (wxTreeItemAttr
*)node
->Data();
618 // prevent TVN_DELETEITEM handler from deleting the attributes again!
619 m_hasAnyAttr
= FALSE
;
624 // delete user data to prevent memory leaks
628 // ----------------------------------------------------------------------------
630 // ----------------------------------------------------------------------------
632 // simple wrappers which add error checking in debug mode
634 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
636 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
638 wxLogLastError("TreeView_GetItem");
646 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
648 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
650 wxLogLastError("TreeView_SetItem");
654 size_t wxTreeCtrl::GetCount() const
656 return (size_t)TreeView_GetCount(GetHwnd());
659 unsigned int wxTreeCtrl::GetIndent() const
661 return TreeView_GetIndent(GetHwnd());
664 void wxTreeCtrl::SetIndent(unsigned int indent
)
666 TreeView_SetIndent(GetHwnd(), indent
);
669 wxImageList
*wxTreeCtrl::GetImageList() const
671 return m_imageListNormal
;
674 wxImageList
*wxTreeCtrl::GetStateImageList() const
676 return m_imageListNormal
;
679 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
682 TreeView_SetImageList(GetHwnd(),
683 imageList
? imageList
->GetHIMAGELIST() : 0,
687 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
689 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
692 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
694 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
697 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
698 bool recursively
) const
700 TraverseCounter
counter(this, item
, recursively
);
702 return counter
.GetCount() - 1;
705 // ----------------------------------------------------------------------------
707 // ----------------------------------------------------------------------------
709 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
711 if ( !wxWindowBase::SetBackgroundColour(colour
) )
714 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
719 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
721 if ( !wxWindowBase::SetForegroundColour(colour
) )
724 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
729 // ----------------------------------------------------------------------------
731 // ----------------------------------------------------------------------------
733 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
735 wxChar buf
[512]; // the size is arbitrary...
737 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
738 tvItem
.pszText
= buf
;
739 tvItem
.cchTextMax
= WXSIZEOF(buf
);
740 if ( !DoGetItem(&tvItem
) )
742 // don't return some garbage which was on stack, but an empty string
746 return wxString(buf
);
749 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
751 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
752 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
756 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
757 wxTreeItemIcon which
) const
759 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
760 if ( !DoGetItem(&tvItem
) )
765 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
768 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
770 wxTreeItemIcon which
) const
772 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
773 if ( !DoGetItem(&tvItem
) )
778 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
780 data
->SetImage(image
, which
);
782 // make sure that we have selected images as well
783 if ( which
== wxTreeItemIcon_Normal
&&
784 !data
->HasImage(wxTreeItemIcon_Selected
) )
786 data
->SetImage(image
, wxTreeItemIcon_Selected
);
789 if ( which
== wxTreeItemIcon_Expanded
&&
790 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
792 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
796 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
800 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
801 tvItem
.iSelectedImage
= imageSel
;
802 tvItem
.iImage
= image
;
806 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
807 wxTreeItemIcon which
) const
809 if ( HasIndirectData(item
) )
811 return DoGetItemImageFromData(item
, which
);
818 wxFAIL_MSG( wxT("unknown tree item image type") );
820 case wxTreeItemIcon_Normal
:
824 case wxTreeItemIcon_Selected
:
825 mask
= TVIF_SELECTEDIMAGE
;
828 case wxTreeItemIcon_Expanded
:
829 case wxTreeItemIcon_SelectedExpanded
:
833 wxTreeViewItem
tvItem(item
, mask
);
836 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
839 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
840 wxTreeItemIcon which
)
842 int imageNormal
, imageSel
;
846 wxFAIL_MSG( wxT("unknown tree item image type") );
848 case wxTreeItemIcon_Normal
:
850 imageSel
= GetItemSelectedImage(item
);
853 case wxTreeItemIcon_Selected
:
854 imageNormal
= GetItemImage(item
);
858 case wxTreeItemIcon_Expanded
:
859 case wxTreeItemIcon_SelectedExpanded
:
860 if ( !HasIndirectData(item
) )
862 // we need to get the old images first, because after we create
863 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
865 imageNormal
= GetItemImage(item
);
866 imageSel
= GetItemSelectedImage(item
);
868 // if it doesn't have it yet, add it
869 wxTreeItemIndirectData
*data
= new
870 wxTreeItemIndirectData(this, item
);
872 // copy the data to the new location
873 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
874 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
877 DoSetItemImageFromData(item
, image
, which
);
879 // reset the normal/selected images because we won't use them any
880 // more - now they're stored inside the indirect data
882 imageSel
= I_IMAGECALLBACK
;
886 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
887 // change both normal and selected image - otherwise the change simply
888 // doesn't take place!
889 DoSetItemImages(item
, imageNormal
, imageSel
);
892 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
894 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
895 if ( !DoGetItem(&tvItem
) )
900 if ( HasIndirectData(item
) )
902 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
906 return (wxTreeItemData
*)tvItem
.lParam
;
910 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
912 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
914 if ( HasIndirectData(item
) )
916 if ( DoGetItem(&tvItem
) )
918 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
922 wxFAIL_MSG( wxT("failed to change tree items data") );
927 tvItem
.lParam
= (LPARAM
)data
;
932 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
933 wxTreeItemIndirectData
*data
)
935 // this should never happen because it's unnecessary and will probably lead
936 // to crash too because the code elsewhere supposes that the pointer the
937 // wxTreeItemIndirectData has is a real wxItemData and not
938 // wxTreeItemIndirectData as well
939 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
941 SetItemData(item
, (wxTreeItemData
*)data
);
943 m_itemsWithIndirectData
.Add(item
);
946 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
948 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
951 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
953 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
954 tvItem
.cChildren
= (int)has
;
958 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
960 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
961 tvItem
.state
= bold
? TVIS_BOLD
: 0;
965 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
967 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
968 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
972 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
977 long id
= (long)(WXHTREEITEM
)item
;
978 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
981 attr
= new wxTreeItemAttr
;
982 m_attrs
.Put(id
, (wxObject
*)attr
);
985 attr
->SetTextColour(col
);
988 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
993 long id
= (long)(WXHTREEITEM
)item
;
994 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
997 attr
= new wxTreeItemAttr
;
998 m_attrs
.Put(id
, (wxObject
*)attr
);
1001 attr
->SetBackgroundColour(col
);
1004 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1006 m_hasAnyAttr
= TRUE
;
1008 long id
= (long)(WXHTREEITEM
)item
;
1009 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1012 attr
= new wxTreeItemAttr
;
1013 m_attrs
.Put(id
, (wxObject
*)attr
);
1016 attr
->SetFont(font
);
1019 // ----------------------------------------------------------------------------
1021 // ----------------------------------------------------------------------------
1023 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1025 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1028 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1029 // the HTREEITEM with TVM_GETITEMRECT
1030 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
1032 // FALSE means get item rect for the whole item, not only text
1033 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
1037 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1039 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1042 return tvItem
.cChildren
!= 0;
1045 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1047 // probably not a good idea to put it here
1048 //wxASSERT( ItemHasChildren(item) );
1050 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1053 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1056 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1058 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1061 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1064 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1066 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1069 return (tvItem
.state
& TVIS_BOLD
) != 0;
1072 // ----------------------------------------------------------------------------
1074 // ----------------------------------------------------------------------------
1076 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1078 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
1081 wxTreeItemId
wxTreeCtrl::GetSelection() const
1083 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
1084 wxT("this only works with single selection controls") );
1086 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
1089 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1091 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), HITEM(item
)));
1094 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1095 long& _cookie
) const
1097 // remember the last child returned in 'cookie'
1098 _cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1100 return wxTreeItemId((WXHTREEITEM
)_cookie
);
1103 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1104 long& _cookie
) const
1106 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
1113 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1115 // can this be done more efficiently?
1118 wxTreeItemId childLast
,
1119 child
= GetFirstChild(item
, cookie
);
1120 while ( child
.IsOk() )
1123 child
= GetNextChild(item
, cookie
);
1129 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1131 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1134 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1136 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1139 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1141 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
1144 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1146 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() "
1147 "for must be visible itself!"));
1149 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1152 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1154 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() "
1155 "for must be visible itself!"));
1157 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1160 // ----------------------------------------------------------------------------
1161 // multiple selections emulation
1162 // ----------------------------------------------------------------------------
1164 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1166 // receive the desired information.
1167 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1170 // state image indices are 1 based
1171 return ((tvItem
.state
>> 12) - 1) == 1;
1174 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1176 // receive the desired information.
1177 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1179 // state images are one-based
1180 tvItem
.state
= (check
? 2 : 1) << 12;
1185 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1187 TraverseSelections
selector(this, selections
);
1189 return selector
.GetCount();
1192 // ----------------------------------------------------------------------------
1194 // ----------------------------------------------------------------------------
1196 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1197 wxTreeItemId hInsertAfter
,
1198 const wxString
& text
,
1199 int image
, int selectedImage
,
1200 wxTreeItemData
*data
)
1202 TV_INSERTSTRUCT tvIns
;
1203 tvIns
.hParent
= HITEM(parent
);
1204 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1206 // this is how we insert the item as the first child: supply a NULL
1208 if ( !tvIns
.hInsertAfter
)
1210 tvIns
.hInsertAfter
= TVI_FIRST
;
1214 if ( !text
.IsEmpty() )
1217 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1223 tvIns
.item
.iImage
= image
;
1225 if ( selectedImage
== -1 )
1227 // take the same image for selected icon if not specified
1228 selectedImage
= image
;
1232 if ( selectedImage
!= -1 )
1234 mask
|= TVIF_SELECTEDIMAGE
;
1235 tvIns
.item
.iSelectedImage
= selectedImage
;
1241 tvIns
.item
.lParam
= (LPARAM
)data
;
1244 tvIns
.item
.mask
= mask
;
1246 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1249 wxLogLastError("TreeView_InsertItem");
1254 // associate the application tree item with Win32 tree item handle
1255 data
->SetId((WXHTREEITEM
)id
);
1258 return wxTreeItemId((WXHTREEITEM
)id
);
1261 // for compatibility only
1262 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1263 const wxString
& text
,
1264 int image
, int selImage
,
1267 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1268 image
, selImage
, NULL
);
1271 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1272 int image
, int selectedImage
,
1273 wxTreeItemData
*data
)
1275 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
1276 text
, image
, selectedImage
, data
);
1279 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1280 const wxString
& text
,
1281 int image
, int selectedImage
,
1282 wxTreeItemData
*data
)
1284 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1285 text
, image
, selectedImage
, data
);
1288 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1289 const wxTreeItemId
& idPrevious
,
1290 const wxString
& text
,
1291 int image
, int selectedImage
,
1292 wxTreeItemData
*data
)
1294 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1297 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1299 const wxString
& text
,
1300 int image
, int selectedImage
,
1301 wxTreeItemData
*data
)
1303 // find the item from index
1305 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1306 while ( index
!= 0 && idCur
.IsOk() )
1311 idCur
= GetNextChild(parent
, cookie
);
1314 // assert, not check: if the index is invalid, we will append the item
1316 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1318 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1321 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1322 const wxString
& text
,
1323 int image
, int selectedImage
,
1324 wxTreeItemData
*data
)
1326 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1327 text
, image
, selectedImage
, data
);
1330 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1332 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1334 wxLogLastError("TreeView_DeleteItem");
1338 // delete all children (but don't delete the item itself)
1339 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1343 wxArrayLong children
;
1344 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1345 while ( child
.IsOk() )
1347 children
.Add((long)(WXHTREEITEM
)child
);
1349 child
= GetNextChild(item
, cookie
);
1352 size_t nCount
= children
.Count();
1353 for ( size_t n
= 0; n
< nCount
; n
++ )
1355 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1357 wxLogLastError("TreeView_DeleteItem");
1362 void wxTreeCtrl::DeleteAllItems()
1364 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1366 wxLogLastError("TreeView_DeleteAllItems");
1370 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1372 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1373 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1374 flag
== TVE_EXPAND
||
1376 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1378 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1379 // emulate them. This behaviour has changed slightly with comctl32.dll
1380 // v 4.70 - now it does send them but only the first time. To maintain
1381 // compatible behaviour and also in order to not have surprises with the
1382 // future versions, don't rely on this and still do everything ourselves.
1383 // To avoid that the messages be sent twice when the item is expanded for
1384 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1386 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1390 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1392 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1393 event
.m_item
= item
;
1395 bool isExpanded
= IsExpanded(item
);
1397 event
.SetEventObject(this);
1399 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1400 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1401 GetEventHandler()->ProcessEvent(event
);
1403 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1404 GetEventHandler()->ProcessEvent(event
);
1406 //else: change didn't took place, so do nothing at all
1409 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1411 DoExpand(item
, TVE_EXPAND
);
1414 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1416 DoExpand(item
, TVE_COLLAPSE
);
1419 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1421 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1424 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1426 DoExpand(item
, TVE_TOGGLE
);
1429 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1431 DoExpand(item
, action
);
1434 void wxTreeCtrl::Unselect()
1436 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1437 wxT("doesn't make sense, may be you want UnselectAll()?") );
1439 // just remove the selection
1440 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
1443 void wxTreeCtrl::UnselectAll()
1445 if ( m_windowStyle
& wxTR_MULTIPLE
)
1447 wxArrayTreeItemIds selections
;
1448 size_t count
= GetSelections(selections
);
1449 for ( size_t n
= 0; n
< count
; n
++ )
1451 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1452 SetItemCheck(selections
[n
], FALSE
);
1453 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1454 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1455 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1460 // just remove the selection
1465 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1467 if ( m_windowStyle
& wxTR_MULTIPLE
)
1469 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1470 // selecting the item means checking it
1472 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1473 ::SelectItem(GetHwnd(), HITEM(item
));
1474 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1478 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1479 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1480 // send them ourselves
1482 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1483 event
.m_item
= item
;
1484 event
.SetEventObject(this);
1486 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1487 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1489 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1491 wxLogLastError("TreeView_SelectItem");
1495 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1496 (void)GetEventHandler()->ProcessEvent(event
);
1499 //else: program vetoed the change
1503 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1506 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1509 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1511 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1513 wxLogLastError("TreeView_SelectSetFirstVisible");
1517 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1519 // normally, we could try to do something like this to return something
1520 // even when the editing was started by the user and not by calling
1521 // EditLabel() - but as nobody has asked for this so far and there might be
1522 // problems in the code below, I leave it disabled for now (VZ)
1526 HWND hwndText
= TreeView_GetEditControl(GetHwnd());
1529 m_textCtrl
= new wxTextCtrl(this, -1);
1531 m_textCtrl
->SetHWND((WXHWND
)hwndText
);
1533 //else: not editing label right now
1540 void wxTreeCtrl::DeleteTextCtrl()
1544 m_textCtrl
->UnsubclassWin();
1545 m_textCtrl
->SetHWND(0);
1551 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1552 wxClassInfo
* textControlClass
)
1554 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1558 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1560 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1567 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1568 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1569 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1574 // End label editing, optionally cancelling the edit
1575 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1577 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1582 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1584 TV_HITTESTINFO hitTestInfo
;
1585 hitTestInfo
.pt
.x
= (int)point
.x
;
1586 hitTestInfo
.pt
.y
= (int)point
.y
;
1588 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1593 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1594 flags |= wxTREE_HITTEST_##flag
1596 TRANSLATE_FLAG(ABOVE
);
1597 TRANSLATE_FLAG(BELOW
);
1598 TRANSLATE_FLAG(NOWHERE
);
1599 TRANSLATE_FLAG(ONITEMBUTTON
);
1600 TRANSLATE_FLAG(ONITEMICON
);
1601 TRANSLATE_FLAG(ONITEMINDENT
);
1602 TRANSLATE_FLAG(ONITEMLABEL
);
1603 TRANSLATE_FLAG(ONITEMRIGHT
);
1604 TRANSLATE_FLAG(ONITEMSTATEICON
);
1605 TRANSLATE_FLAG(TOLEFT
);
1606 TRANSLATE_FLAG(TORIGHT
);
1608 #undef TRANSLATE_FLAG
1610 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1613 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1615 bool textOnly
) const
1618 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1621 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1627 // couldn't retrieve rect: for example, item isn't visible
1632 // ----------------------------------------------------------------------------
1634 // ----------------------------------------------------------------------------
1636 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1637 wxTreeItemData
*pItem2
,
1640 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1641 wxT("sorting tree without data doesn't make sense") );
1643 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1646 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1647 const wxTreeItemId
& item2
)
1649 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1652 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1654 // rely on the fact that TreeView_SortChildren does the same thing as our
1655 // default behaviour, i.e. sorts items alphabetically and so call it
1656 // directly if we're not in derived class (much more efficient!)
1657 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1659 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1664 tvSort
.hParent
= HITEM(item
);
1665 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1666 tvSort
.lParam
= (LPARAM
)this;
1667 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1671 // ----------------------------------------------------------------------------
1673 // ----------------------------------------------------------------------------
1675 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1677 if ( cmd
== EN_UPDATE
)
1679 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1680 event
.SetEventObject( this );
1681 ProcessCommand(event
);
1683 else if ( cmd
== EN_KILLFOCUS
)
1685 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1686 event
.SetEventObject( this );
1687 ProcessCommand(event
);
1695 // command processed
1699 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1700 // only do it during dragging, minimize wxWin overhead (this is important for
1701 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1702 // instead of passing by wxWin events
1703 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1705 bool processed
= FALSE
;
1708 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
1710 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1712 // we only process mouse messages here and these parameters have the same
1713 // meaning for all of them
1714 int x
= GET_X_LPARAM(lParam
),
1715 y
= GET_Y_LPARAM(lParam
);
1716 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
1720 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1721 case WM_LBUTTONDOWN
:
1722 if ( htItem
&& isMultiple
)
1724 if ( wParam
& MK_CONTROL
)
1728 // toggle selected state
1729 ToggleItemSelection(GetHwnd(), htItem
);
1731 ::SetFocus(GetHwnd(), htItem
);
1733 // reset on any click without Shift
1738 else if ( wParam
& MK_SHIFT
)
1740 // this selects all items between the starting one and
1743 if ( !m_htSelStart
)
1745 // take the focused item
1746 m_htSelStart
= (WXHTREEITEM
)
1747 TreeView_GetSelection(GetHwnd());
1750 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
1751 !(wParam
& MK_CONTROL
));
1753 ::SetFocus(GetHwnd(), htItem
);
1757 else // normal click
1759 // clear the selection and then let the default handler
1763 // prevent the click from starting in-place editing
1764 // when there was no selection in the control
1765 TreeView_SelectItem(GetHwnd(), 0);
1767 // reset on any click without Shift
1772 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1777 m_dragImage
->Move(wxPoint(x
, y
));
1780 // highlight the item as target (hiding drag image is
1781 // necessary - otherwise the display will be corrupted)
1782 m_dragImage
->Hide();
1783 TreeView_SelectDropTarget(GetHwnd(), htItem
);
1784 m_dragImage
->Show();
1793 m_dragImage
->EndDrag();
1797 // generate the drag end event
1798 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
1800 event
.m_item
= (WXHTREEITEM
)htItem
;
1801 event
.m_pointDrag
= wxPoint(x
, y
);
1802 event
.SetEventObject(this);
1804 (void)GetEventHandler()->ProcessEvent(event
);
1806 // if we don't do it, the tree seems to think that 2 items
1807 // are selected simultaneously which is quite weird
1808 TreeView_SelectDropTarget(GetHwnd(), 0);
1813 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1814 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
1816 // the tree control greys out the selected item when it loses focus and
1817 // paints it as selected again when it regains it, but it won't do it
1818 // for the other items itself - help it
1819 wxArrayTreeItemIds selections
;
1820 size_t count
= GetSelections(selections
);
1822 for ( size_t n
= 0; n
< count
; n
++ )
1824 // TreeView_GetItemRect() will return FALSE if item is not visible,
1825 // which may happen perfectly well
1826 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
1829 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1833 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
1835 bool bCtrl
= wxIsCtrlDown(),
1836 bShift
= wxIsShiftDown();
1838 // we handle.arrows and space, but not page up/down and home/end: the
1839 // latter should be easy, but not the former
1841 HTREEITEM htSel
= TreeView_GetSelection(GetHwnd());
1842 if ( !m_htSelStart
)
1844 m_htSelStart
= (WXHTREEITEM
)htSel
;
1847 if ( wParam
== VK_SPACE
)
1851 ToggleItemSelection(GetHwnd(), htSel
);
1857 ::SelectItem(GetHwnd(), htSel
);
1862 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
1864 if ( !bCtrl
&& !bShift
)
1866 // no modifiers, just clear selection and then let the default
1867 // processing to take place
1872 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1875 wParam
== VK_UP
? TreeView_GetPrevVisible(GetHwnd(), htSel
)
1876 : TreeView_GetNextVisible(GetHwnd(), htSel
);
1880 // at the top/bottom
1886 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
1890 // without changing selection
1891 ::SetFocus(GetHwnd(), htNext
);
1898 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1901 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1906 // process WM_NOTIFY Windows message
1907 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1909 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1910 wxEventType eventType
= wxEVT_NULL
;
1911 NMHDR
*hdr
= (NMHDR
*)lParam
;
1913 switch ( hdr
->code
)
1917 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1920 TV_HITTESTINFO tvhti
;
1921 ::GetCursorPos(&(tvhti
.pt
));
1922 ::ScreenToClient(GetHwnd(),&(tvhti
.pt
));
1923 if ( TreeView_HitTest(GetHwnd(),&tvhti
) )
1925 if( tvhti
.flags
& TVHT_ONITEM
)
1927 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
1928 eventType
= wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
1935 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1938 case TVN_BEGINRDRAG
:
1940 if ( eventType
== wxEVT_NULL
)
1941 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1942 //else: left drag, already set above
1944 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1946 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1947 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1949 // don't allow dragging by default: the user code must
1950 // explicitly say that it wants to allow it to avoid breaking
1956 case TVN_BEGINLABELEDIT
:
1958 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1959 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1961 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1962 event
.m_label
= info
->item
.pszText
;
1966 case TVN_DELETEITEM
:
1968 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1969 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1971 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
1975 delete (wxTreeItemAttr
*)m_attrs
.
1976 Delete((long)tv
->itemOld
.hItem
);
1981 case TVN_ENDLABELEDIT
:
1983 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1984 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1986 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1987 event
.m_label
= info
->item
.pszText
;
1988 if (info
->item
.pszText
== NULL
)
1993 case TVN_GETDISPINFO
:
1994 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1997 case TVN_SETDISPINFO
:
1999 if ( eventType
== wxEVT_NULL
)
2000 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2001 //else: get, already set above
2003 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2005 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2009 case TVN_ITEMEXPANDING
:
2010 event
.m_code
= FALSE
;
2013 case TVN_ITEMEXPANDED
:
2015 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2017 bool expand
= FALSE
;
2018 switch ( tv
->action
)
2029 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
2030 "message"), tv
->action
);
2033 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
2034 eventType
= g_events
[expand
][ing
];
2036 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2042 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2043 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2045 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
2047 // a separate event for Space/Return
2048 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2049 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2051 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2053 event2
.SetEventObject(this);
2054 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2056 event2
.m_item
= GetSelection();
2058 //else: don't know how to get it
2060 (void)GetEventHandler()->ProcessEvent(event2
);
2065 case TVN_SELCHANGED
:
2066 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2069 case TVN_SELCHANGING
:
2071 if ( eventType
== wxEVT_NULL
)
2072 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2073 //else: already set above
2075 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2077 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2078 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2082 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
2085 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2086 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2087 switch( nmcd
.dwDrawStage
)
2090 // if we've got any items with non standard attributes,
2091 // notify us before painting each item
2092 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2096 case CDDS_ITEMPREPAINT
:
2098 wxTreeItemAttr
*attr
=
2099 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
2103 // nothing to do for this item
2104 return CDRF_DODEFAULT
;
2108 wxColour colText
, colBack
;
2109 if ( attr
->HasFont() )
2111 wxFont font
= attr
->GetFont();
2112 hFont
= (HFONT
)font
.GetResourceHandle();
2119 if ( attr
->HasTextColour() )
2121 colText
= attr
->GetTextColour();
2125 colText
= GetForegroundColour();
2128 // selection colours should override ours
2129 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2131 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
2132 lptvcd
->clrTextBk
= clrBk
;
2134 // try to make the text visible
2135 lptvcd
->clrText
= wxColourToRGB(colText
);
2136 lptvcd
->clrText
|= ~clrBk
;
2137 lptvcd
->clrText
&= 0x00ffffff;
2141 if ( attr
->HasBackgroundColour() )
2143 colBack
= attr
->GetBackgroundColour();
2147 colBack
= GetBackgroundColour();
2150 lptvcd
->clrText
= wxColourToRGB(colText
);
2151 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2154 // note that if we wanted to set colours for
2155 // individual columns (subitems), we would have
2156 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2159 ::SelectObject(nmcd
.hdc
, hFont
);
2161 *result
= CDRF_NEWFONT
;
2165 *result
= CDRF_DODEFAULT
;
2172 *result
= CDRF_DODEFAULT
;
2177 #endif // _WIN32_IE >= 0x300
2180 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2183 event
.SetEventObject(this);
2184 event
.SetEventType(eventType
);
2186 bool processed
= GetEventHandler()->ProcessEvent(event
);
2189 switch ( hdr
->code
)
2192 case TVN_BEGINRDRAG
:
2193 if ( event
.IsAllowed() )
2195 // normally this is impossible because the m_dragImage is
2196 // deleted once the drag operation is over
2197 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2199 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2200 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2201 m_dragImage
->Show();
2205 case TVN_DELETEITEM
:
2207 // NB: we might process this message using wxWindows event
2208 // tables, but due to overhead of wxWin event system we
2209 // prefer to do it here ourself (otherwise deleting a tree
2210 // with many items is just too slow)
2211 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2213 wxTreeItemId item
= event
.m_item
;
2214 if ( HasIndirectData(item
) )
2216 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2218 delete data
; // can't be NULL here
2220 m_itemsWithIndirectData
.Remove(item
);
2224 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2225 delete data
; // may be NULL, ok
2228 processed
= TRUE
; // Make sure we don't get called twice
2232 case TVN_BEGINLABELEDIT
:
2233 // return TRUE to cancel label editing
2234 *result
= !event
.IsAllowed();
2237 case TVN_ENDLABELEDIT
:
2238 // return TRUE to set the label to the new string
2239 *result
= event
.IsAllowed();
2241 // ensure that we don't have the text ctrl which is going to be
2246 case TVN_SELCHANGING
:
2247 case TVN_ITEMEXPANDING
:
2248 // return TRUE to prevent the action from happening
2249 *result
= !event
.IsAllowed();
2252 case TVN_GETDISPINFO
:
2253 // NB: so far the user can't set the image himself anyhow, so do it
2254 // anyway - but this may change later
2255 if ( /* !processed && */ 1 )
2257 wxTreeItemId item
= event
.m_item
;
2258 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2259 if ( info
->item
.mask
& TVIF_IMAGE
)
2262 DoGetItemImageFromData
2265 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2266 : wxTreeItemIcon_Normal
2269 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2271 info
->item
.iSelectedImage
=
2272 DoGetItemImageFromData
2275 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2276 : wxTreeItemIcon_Selected
2283 // for the other messages the return value is ignored and there is
2284 // nothing special to do
2290 // ----------------------------------------------------------------------------
2292 // ----------------------------------------------------------------------------
2294 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
2296 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
2297 : wxNotifyEvent(commandType
, id
)