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/window.h"
31 #include "wx/msw/private.h"
33 // Mingw32 is a bit mental even though this is done in winundef
42 #if defined(__WIN95__)
45 #include "wx/dynarray.h"
46 #include "wx/imaglist.h"
47 #include "wx/treectrl.h"
48 #include "wx/settings.h"
51 #ifndef wxUSE_NORLANDER_HEADERS
52 #include "wx/msw/gnuwin32/extra.h"
56 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
60 // Bug in headers, sometimes
62 #define TVIS_FOCUSED 0x0001
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // a convenient wrapper around TV_ITEM struct which adds a ctor
71 #pragma warning( disable : 4097 )
74 struct wxTreeViewItem
: public TV_ITEM
76 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
77 UINT mask_
, // fields which are valid
78 UINT stateMask_
= 0) // for TVIF_STATE only
80 // hItem member is always valid
81 mask
= mask_
| TVIF_HANDLE
;
82 stateMask
= stateMask_
;
83 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
88 #pragma warning( default : 4097 )
91 // a class which encapsulates the tree traversal logic: it vists all (unless
92 // OnVisit() returns FALSE) items under the given one
96 wxTreeTraversal(const wxTreeCtrl
*tree
)
101 // do traverse the tree: visit all items (recursively by default) under the
102 // given one; return TRUE if all items were traversed or FALSE if the
103 // traversal was aborted because OnVisit returned FALSE
104 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
106 // override this function to do whatever is needed for each item, return
107 // FALSE to stop traversing
108 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
111 const wxTreeCtrl
*GetTree() const { return m_tree
; }
114 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
116 const wxTreeCtrl
*m_tree
;
119 // internal class for getting the selected items
120 class TraverseSelections
: public wxTreeTraversal
123 TraverseSelections(const wxTreeCtrl
*tree
,
124 wxArrayTreeItemIds
& selections
)
125 : wxTreeTraversal(tree
), m_selections(selections
)
127 m_selections
.Empty();
129 DoTraverse(tree
->GetRootItem());
132 virtual bool OnVisit(const wxTreeItemId
& item
)
134 if ( GetTree()->IsItemChecked(item
) )
136 m_selections
.Add(item
);
143 wxArrayTreeItemIds
& m_selections
;
146 // internal class for counting tree items
147 class TraverseCounter
: public wxTreeTraversal
150 TraverseCounter(const wxTreeCtrl
*tree
,
151 const wxTreeItemId
& root
,
153 : wxTreeTraversal(tree
)
157 DoTraverse(root
, recursively
);
160 virtual bool OnVisit(const wxTreeItemId
& item
)
167 size_t GetCount() const { return m_count
; }
173 // ----------------------------------------------------------------------------
174 // This class is needed for support of different images: the Win32 common
175 // control natively supports only 2 images (the normal one and another for the
176 // selected state). We wish to provide support for 2 more of them for folder
177 // items (i.e. those which have children): for expanded state and for expanded
178 // selected state. For this we use this structure to store the additional items
181 // There is only one problem with this: when we retrieve the item's data, we
182 // don't know whether we get a pointer to wxTreeItemData or
183 // wxTreeItemIndirectData. So we have to maintain a list of all items which
184 // have indirect data inside the listctrl itself.
185 // ----------------------------------------------------------------------------
186 class wxTreeItemIndirectData
189 // ctor associates this data with the item and the real item data becomes
190 // available through our GetData() method
191 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
193 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
199 m_data
= tree
->GetItemData(item
);
201 // and set ourselves as the new one
202 tree
->SetIndirectItemData(item
, this);
205 // dtor deletes the associated data as well
206 ~wxTreeItemIndirectData() { delete m_data
; }
209 // get the real data associated with the item
210 wxTreeItemData
*GetData() const { return m_data
; }
212 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
214 // do we have such image?
215 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
217 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
219 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
222 // all the images associated with the item
223 int m_images
[wxTreeItemIcon_Max
];
225 wxTreeItemData
*m_data
;
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 #if !USE_SHARED_LIBRARY
233 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
240 // handy table for sending events
241 static const wxEventType g_events
[2][2] =
243 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
244 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
247 // ============================================================================
249 // ============================================================================
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
257 if ( !OnVisit(root
) )
260 return Traverse(root
, recursively
);
263 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
266 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
267 while ( child
.IsOk() )
269 // depth first traversal
270 if ( recursively
&& !Traverse(child
, TRUE
) )
273 if ( !OnVisit(child
) )
276 child
= m_tree
->GetNextChild(root
, cookie
);
282 // ----------------------------------------------------------------------------
283 // construction and destruction
284 // ----------------------------------------------------------------------------
286 void wxTreeCtrl::Init()
288 m_imageListNormal
= NULL
;
289 m_imageListState
= NULL
;
293 bool wxTreeCtrl::Create(wxWindow
*parent
,
298 const wxValidator
& validator
,
299 const wxString
& name
)
303 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
306 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
307 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
309 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
310 wstyle
|= TVS_HASBUTTONS
;
312 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
313 wstyle
|= TVS_EDITLABELS
;
315 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
316 wstyle
|= TVS_LINESATROOT
;
318 #if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !defined( __WATCOMC__ ) && !defined(wxUSE_NORLANDER_HEADERS)
319 // we emulate the multiple selection tree controls by using checkboxes: set
320 // up the image list we need for this if we do have multiple selections
321 #if !defined(__VISUALC__) || (__VISUALC__ > 1010)
322 if ( m_windowStyle
& wxTR_MULTIPLE
)
323 wstyle
|= TVS_CHECKBOXES
;
327 // Create the tree control.
328 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
331 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
332 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
334 // VZ: this is some experimental code which may be used to get the
335 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
336 // AFAIK, the standard DLL does about the same thing anyhow.
338 if ( m_windowStyle
& wxTR_MULTIPLE
)
342 // create the DC compatible with the current screen
343 HDC hdcMem
= CreateCompatibleDC(NULL
);
345 // create a mono bitmap of the standard size
346 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
347 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
348 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
349 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
350 1, // # of color planes
351 1, // # bits needed for one pixel
352 0); // array containing colour data
353 SelectObject(hdcMem
, hbmpCheck
);
355 // then draw a check mark into it
356 RECT rect
= { 0, 0, x
, y
};
357 if ( !::DrawFrameControl(hdcMem
, &rect
,
359 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
361 wxLogLastError(wxT("DrawFrameControl(check)"));
364 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
365 imagelistCheckboxes
.Add(bmp
);
367 if ( !::DrawFrameControl(hdcMem
, &rect
,
371 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
374 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
375 imagelistCheckboxes
.Add(bmp
);
381 SetStateImageList(&imagelistCheckboxes
);
385 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
390 wxTreeCtrl::~wxTreeCtrl()
394 // delete user data to prevent memory leaks
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
402 // simple wrappers which add error checking in debug mode
404 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
406 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
408 wxLogLastError("TreeView_GetItem");
416 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
418 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
420 wxLogLastError("TreeView_SetItem");
424 size_t wxTreeCtrl::GetCount() const
426 return (size_t)TreeView_GetCount(GetHwnd());
429 unsigned int wxTreeCtrl::GetIndent() const
431 return TreeView_GetIndent(GetHwnd());
434 void wxTreeCtrl::SetIndent(unsigned int indent
)
436 TreeView_SetIndent(GetHwnd(), indent
);
439 wxImageList
*wxTreeCtrl::GetImageList() const
441 return m_imageListNormal
;
444 wxImageList
*wxTreeCtrl::GetStateImageList() const
446 return m_imageListNormal
;
449 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
452 TreeView_SetImageList(GetHwnd(),
453 imageList
? imageList
->GetHIMAGELIST() : 0,
457 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
459 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
462 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
464 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
467 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
468 bool recursively
) const
470 TraverseCounter
counter(this, item
, recursively
);
472 return counter
.GetCount() - 1;
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
481 wxChar buf
[512]; // the size is arbitrary...
483 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
484 tvItem
.pszText
= buf
;
485 tvItem
.cchTextMax
= WXSIZEOF(buf
);
486 if ( !DoGetItem(&tvItem
) )
488 // don't return some garbage which was on stack, but an empty string
492 return wxString(buf
);
495 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
497 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
498 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
502 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
503 wxTreeItemIcon which
) const
505 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
506 if ( !DoGetItem(&tvItem
) )
511 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
514 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
516 wxTreeItemIcon which
) const
518 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
519 if ( !DoGetItem(&tvItem
) )
524 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
526 data
->SetImage(image
, which
);
528 // make sure that we have selected images as well
529 if ( which
== wxTreeItemIcon_Normal
&&
530 !data
->HasImage(wxTreeItemIcon_Selected
) )
532 data
->SetImage(image
, wxTreeItemIcon_Selected
);
535 if ( which
== wxTreeItemIcon_Expanded
&&
536 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
538 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
542 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
546 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
547 tvItem
.iSelectedImage
= imageSel
;
548 tvItem
.iImage
= image
;
552 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
553 wxTreeItemIcon which
) const
555 if ( HasIndirectData(item
) )
557 return DoGetItemImageFromData(item
, which
);
564 wxFAIL_MSG( wxT("unknown tree item image type") );
566 case wxTreeItemIcon_Normal
:
570 case wxTreeItemIcon_Selected
:
571 mask
= TVIF_SELECTEDIMAGE
;
574 case wxTreeItemIcon_Expanded
:
575 case wxTreeItemIcon_SelectedExpanded
:
579 wxTreeViewItem
tvItem(item
, mask
);
582 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
585 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
586 wxTreeItemIcon which
)
588 int imageNormal
, imageSel
;
592 wxFAIL_MSG( wxT("unknown tree item image type") );
594 case wxTreeItemIcon_Normal
:
596 imageSel
= GetItemSelectedImage(item
);
599 case wxTreeItemIcon_Selected
:
600 imageNormal
= GetItemImage(item
);
604 case wxTreeItemIcon_Expanded
:
605 case wxTreeItemIcon_SelectedExpanded
:
606 if ( !HasIndirectData(item
) )
608 // we need to get the old images first, because after we create
609 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
611 imageNormal
= GetItemImage(item
);
612 imageSel
= GetItemSelectedImage(item
);
614 // if it doesn't have it yet, add it
615 wxTreeItemIndirectData
*data
= new
616 wxTreeItemIndirectData(this, item
);
618 // copy the data to the new location
619 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
620 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
623 DoSetItemImageFromData(item
, image
, which
);
625 // reset the normal/selected images because we won't use them any
626 // more - now they're stored inside the indirect data
628 imageSel
= I_IMAGECALLBACK
;
632 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
633 // change both normal and selected image - otherwise the change simply
634 // doesn't take place!
635 DoSetItemImages(item
, imageNormal
, imageSel
);
638 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
640 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
641 if ( !DoGetItem(&tvItem
) )
646 if ( HasIndirectData(item
) )
648 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
652 return (wxTreeItemData
*)tvItem
.lParam
;
656 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
658 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
660 if ( HasIndirectData(item
) )
662 if ( DoGetItem(&tvItem
) )
664 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
668 wxFAIL_MSG( wxT("failed to change tree items data") );
673 tvItem
.lParam
= (LPARAM
)data
;
678 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
679 wxTreeItemIndirectData
*data
)
681 // this should never happen because it's unnecessary and will probably lead
682 // to crash too because the code elsewhere supposes that the pointer the
683 // wxTreeItemIndirectData has is a real wxItemData and not
684 // wxTreeItemIndirectData as well
685 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
687 SetItemData(item
, (wxTreeItemData
*)data
);
689 m_itemsWithIndirectData
.Add(item
);
692 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
694 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
697 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
699 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
700 tvItem
.cChildren
= (int)has
;
704 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
706 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
707 tvItem
.state
= bold
? TVIS_BOLD
: 0;
711 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
713 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
714 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
718 // ----------------------------------------------------------------------------
720 // ----------------------------------------------------------------------------
722 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
724 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
727 // this ugliness comes directly from MSDN - it *is* the correct way to pass
728 // the HTREEITEM with TVM_GETITEMRECT
729 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
731 // FALSE means get item rect for the whole item, not only text
732 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
736 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
738 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
741 return tvItem
.cChildren
!= 0;
744 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
746 // probably not a good idea to put it here
747 //wxASSERT( ItemHasChildren(item) );
749 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
752 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
755 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
757 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
760 return (tvItem
.state
& TVIS_SELECTED
) != 0;
763 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
765 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
768 return (tvItem
.state
& TVIS_BOLD
) != 0;
771 // ----------------------------------------------------------------------------
773 // ----------------------------------------------------------------------------
775 wxTreeItemId
wxTreeCtrl::GetRootItem() const
777 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
780 wxTreeItemId
wxTreeCtrl::GetSelection() const
782 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
783 wxT("this only works with single selection controls") );
785 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
788 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
790 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
793 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
796 // remember the last child returned in 'cookie'
797 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
799 return wxTreeItemId((WXHTREEITEM
)_cookie
);
802 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
805 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
806 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
812 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
814 // can this be done more efficiently?
817 wxTreeItemId childLast
,
818 child
= GetFirstChild(item
, cookie
);
819 while ( child
.IsOk() )
822 child
= GetNextChild(item
, cookie
);
828 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
830 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
833 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
835 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
838 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
840 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
843 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
845 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() "
846 "for must be visible itself!"));
848 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
851 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
853 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() "
854 "for must be visible itself!"));
856 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
859 // ----------------------------------------------------------------------------
860 // multiple selections emulation
861 // ----------------------------------------------------------------------------
863 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
865 // receive the desired information.
866 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
869 // state image indices are 1 based
870 return ((tvItem
.state
>> 12) - 1) == 1;
873 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
875 // receive the desired information.
876 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
878 // state images are one-based
879 tvItem
.state
= (check
? 2 : 1) << 12;
884 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
886 TraverseSelections
selector(this, selections
);
888 return selections
.GetCount();
891 // ----------------------------------------------------------------------------
893 // ----------------------------------------------------------------------------
895 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
896 wxTreeItemId hInsertAfter
,
897 const wxString
& text
,
898 int image
, int selectedImage
,
899 wxTreeItemData
*data
)
901 TV_INSERTSTRUCT tvIns
;
902 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
903 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
905 // this is how we insert the item as the first child: supply a NULL
907 if ( !tvIns
.hInsertAfter
)
909 tvIns
.hInsertAfter
= TVI_FIRST
;
913 if ( !text
.IsEmpty() )
916 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
922 tvIns
.item
.iImage
= image
;
924 if ( selectedImage
== -1 )
926 // take the same image for selected icon if not specified
927 selectedImage
= image
;
931 if ( selectedImage
!= -1 )
933 mask
|= TVIF_SELECTEDIMAGE
;
934 tvIns
.item
.iSelectedImage
= selectedImage
;
940 tvIns
.item
.lParam
= (LPARAM
)data
;
943 tvIns
.item
.mask
= mask
;
945 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
948 wxLogLastError("TreeView_InsertItem");
953 // associate the application tree item with Win32 tree item handle
954 data
->SetId((WXHTREEITEM
)id
);
957 return wxTreeItemId((WXHTREEITEM
)id
);
960 // for compatibility only
961 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
962 const wxString
& text
,
963 int image
, int selImage
,
966 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
967 image
, selImage
, NULL
);
970 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
971 int image
, int selectedImage
,
972 wxTreeItemData
*data
)
974 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
975 text
, image
, selectedImage
, data
);
978 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
979 const wxString
& text
,
980 int image
, int selectedImage
,
981 wxTreeItemData
*data
)
983 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
984 text
, image
, selectedImage
, data
);
987 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
988 const wxTreeItemId
& idPrevious
,
989 const wxString
& text
,
990 int image
, int selectedImage
,
991 wxTreeItemData
*data
)
993 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
996 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
997 const wxString
& text
,
998 int image
, int selectedImage
,
999 wxTreeItemData
*data
)
1001 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1002 text
, image
, selectedImage
, data
);
1005 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1007 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
1009 wxLogLastError("TreeView_DeleteItem");
1013 // delete all children (but don't delete the item itself)
1014 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1018 wxArrayLong children
;
1019 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1020 while ( child
.IsOk() )
1022 children
.Add((long)(WXHTREEITEM
)child
);
1024 child
= GetNextChild(item
, cookie
);
1027 size_t nCount
= children
.Count();
1028 for ( size_t n
= 0; n
< nCount
; n
++ )
1030 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1032 wxLogLastError("TreeView_DeleteItem");
1037 void wxTreeCtrl::DeleteAllItems()
1039 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1041 wxLogLastError("TreeView_DeleteAllItems");
1045 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1047 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1048 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1049 flag
== TVE_EXPAND
||
1051 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1053 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1054 // emulate them. This behaviour has changed slightly with comctl32.dll
1055 // v 4.70 - now it does send them but only the first time. To maintain
1056 // compatible behaviour and also in order to not have surprises with the
1057 // future versions, don't rely on this and still do everything ourselves.
1058 // To avoid that the messages be sent twice when the item is expanded for
1059 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1061 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1065 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
1067 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1068 event
.m_item
= item
;
1070 bool isExpanded
= IsExpanded(item
);
1072 event
.SetEventObject(this);
1074 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1075 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1076 GetEventHandler()->ProcessEvent(event
);
1078 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1079 GetEventHandler()->ProcessEvent(event
);
1081 //else: change didn't took place, so do nothing at all
1084 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1086 DoExpand(item
, TVE_EXPAND
);
1089 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1091 DoExpand(item
, TVE_COLLAPSE
);
1094 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1096 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1099 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1101 DoExpand(item
, TVE_TOGGLE
);
1104 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1106 DoExpand(item
, action
);
1109 void wxTreeCtrl::Unselect()
1111 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxT("doesn't make sense") );
1113 // just remove the selection
1114 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
1117 void wxTreeCtrl::UnselectAll()
1119 if ( m_windowStyle
& wxTR_MULTIPLE
)
1121 wxArrayTreeItemIds selections
;
1122 size_t count
= GetSelections(selections
);
1123 for ( size_t n
= 0; n
< count
; n
++ )
1125 SetItemCheck(selections
[n
], FALSE
);
1130 // just remove the selection
1135 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1137 if ( m_windowStyle
& wxTR_MULTIPLE
)
1139 // selecting the item means checking it
1144 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1145 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1146 // send them ourselves
1148 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1149 event
.m_item
= item
;
1150 event
.SetEventObject(this);
1152 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1153 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1155 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1157 wxLogLastError("TreeView_SelectItem");
1161 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1162 (void)GetEventHandler()->ProcessEvent(event
);
1165 //else: program vetoed the change
1169 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1172 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1175 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1177 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1179 wxLogLastError("TreeView_SelectSetFirstVisible");
1183 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1188 void wxTreeCtrl::DeleteTextCtrl()
1192 m_textCtrl
->UnsubclassWin();
1193 m_textCtrl
->SetHWND(0);
1199 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1200 wxClassInfo
* textControlClass
)
1202 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1204 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1206 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1215 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1216 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1217 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1222 // End label editing, optionally cancelling the edit
1223 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1225 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1230 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1232 TV_HITTESTINFO hitTestInfo
;
1233 hitTestInfo
.pt
.x
= (int)point
.x
;
1234 hitTestInfo
.pt
.y
= (int)point
.y
;
1236 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1241 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1242 flags |= wxTREE_HITTEST_##flag
1244 TRANSLATE_FLAG(ABOVE
);
1245 TRANSLATE_FLAG(BELOW
);
1246 TRANSLATE_FLAG(NOWHERE
);
1247 TRANSLATE_FLAG(ONITEMBUTTON
);
1248 TRANSLATE_FLAG(ONITEMICON
);
1249 TRANSLATE_FLAG(ONITEMINDENT
);
1250 TRANSLATE_FLAG(ONITEMLABEL
);
1251 TRANSLATE_FLAG(ONITEMRIGHT
);
1252 TRANSLATE_FLAG(ONITEMSTATEICON
);
1253 TRANSLATE_FLAG(TOLEFT
);
1254 TRANSLATE_FLAG(TORIGHT
);
1256 #undef TRANSLATE_FLAG
1258 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1261 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1263 bool textOnly
) const
1266 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1269 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1275 // couldn't retrieve rect: for example, item isn't visible
1280 // ----------------------------------------------------------------------------
1282 // ----------------------------------------------------------------------------
1284 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1285 wxTreeItemData
*pItem2
,
1288 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1289 wxT("sorting tree without data doesn't make sense") );
1291 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1294 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1295 const wxTreeItemId
& item2
)
1297 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1300 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1302 // rely on the fact that TreeView_SortChildren does the same thing as our
1303 // default behaviour, i.e. sorts items alphabetically and so call it
1304 // directly if we're not in derived class (much more efficient!)
1305 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1307 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1312 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1313 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1314 tvSort
.lParam
= (LPARAM
)this;
1315 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1319 // ----------------------------------------------------------------------------
1321 // ----------------------------------------------------------------------------
1323 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1325 if ( cmd
== EN_UPDATE
)
1327 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1328 event
.SetEventObject( this );
1329 ProcessCommand(event
);
1331 else if ( cmd
== EN_KILLFOCUS
)
1333 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1334 event
.SetEventObject( this );
1335 ProcessCommand(event
);
1343 // command processed
1347 // process WM_NOTIFY Windows message
1348 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1350 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1351 wxEventType eventType
= wxEVT_NULL
;
1352 NMHDR
*hdr
= (NMHDR
*)lParam
;
1354 switch ( hdr
->code
)
1358 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1361 TV_HITTESTINFO tvhti
;
1362 ::GetCursorPos(&(tvhti
.pt
));
1363 ::ScreenToClient(GetHwnd(),&(tvhti
.pt
));
1364 if ( TreeView_HitTest(GetHwnd(),&tvhti
) )
1366 if( tvhti
.flags
& TVHT_ONITEM
)
1368 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
1369 eventType
=wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
1376 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1379 case TVN_BEGINRDRAG
:
1381 if ( eventType
== wxEVT_NULL
)
1382 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1383 //else: left drag, already set above
1385 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1387 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1388 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1392 case TVN_BEGINLABELEDIT
:
1394 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1395 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1397 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1398 event
.m_label
= info
->item
.pszText
;
1402 case TVN_DELETEITEM
:
1404 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1405 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1407 event
.m_item
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1411 case TVN_ENDLABELEDIT
:
1413 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1414 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1416 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1417 event
.m_label
= info
->item
.pszText
;
1418 if (info
->item
.pszText
== NULL
)
1423 case TVN_GETDISPINFO
:
1424 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1427 case TVN_SETDISPINFO
:
1429 if ( eventType
== wxEVT_NULL
)
1430 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1431 //else: get, already set above
1433 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1435 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1439 case TVN_ITEMEXPANDING
:
1440 event
.m_code
= FALSE
;
1443 case TVN_ITEMEXPANDED
:
1445 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1447 bool expand
= FALSE
;
1448 switch ( tv
->action
)
1459 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
1460 "message"), tv
->action
);
1463 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
1464 eventType
= g_events
[expand
][ing
];
1466 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1472 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1473 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1475 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1477 // a separate event for this case
1478 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1480 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1482 event2
.SetEventObject(this);
1484 GetEventHandler()->ProcessEvent(event2
);
1489 case TVN_SELCHANGED
:
1490 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1493 case TVN_SELCHANGING
:
1495 if ( eventType
== wxEVT_NULL
)
1496 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1497 //else: already set above
1499 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1501 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1502 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1507 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1510 event
.SetEventObject(this);
1511 event
.SetEventType(eventType
);
1513 bool processed
= GetEventHandler()->ProcessEvent(event
);
1516 switch ( hdr
->code
)
1518 case TVN_DELETEITEM
:
1520 // NB: we might process this message using wxWindows event
1521 // tables, but due to overhead of wxWin event system we
1522 // prefer to do it here ourself (otherwise deleting a tree
1523 // with many items is just too slow)
1524 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1526 wxTreeItemId item
= event
.m_item
;
1527 if ( HasIndirectData(item
) )
1529 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
1531 delete data
; // can't be NULL here
1533 m_itemsWithIndirectData
.Remove(item
);
1537 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1538 delete data
; // may be NULL, ok
1541 processed
= TRUE
; // Make sure we don't get called twice
1545 case TVN_BEGINLABELEDIT
:
1546 // return TRUE to cancel label editing
1547 *result
= !event
.IsAllowed();
1550 case TVN_ENDLABELEDIT
:
1551 // return TRUE to set the label to the new string
1552 *result
= event
.IsAllowed();
1554 // ensure that we don't have the text ctrl which is going to be
1559 case TVN_SELCHANGING
:
1560 case TVN_ITEMEXPANDING
:
1561 // return TRUE to prevent the action from happening
1562 *result
= !event
.IsAllowed();
1565 case TVN_GETDISPINFO
:
1566 // NB: so far the user can't set the image himself anyhow, so do it
1567 // anyway - but this may change later
1568 if ( /* !processed && */ 1 )
1570 wxTreeItemId item
= event
.m_item
;
1571 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1572 if ( info
->item
.mask
& TVIF_IMAGE
)
1575 DoGetItemImageFromData
1578 IsExpanded(item
) ? wxTreeItemIcon_Expanded
1579 : wxTreeItemIcon_Normal
1582 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
1584 info
->item
.iSelectedImage
=
1585 DoGetItemImageFromData
1588 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
1589 : wxTreeItemIcon_Selected
1596 // for the other messages the return value is ignored and there is
1597 // nothing special to do
1603 // ----------------------------------------------------------------------------
1605 // ----------------------------------------------------------------------------
1607 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1609 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1610 : wxNotifyEvent(commandType
, id
)