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"
50 #ifndef wxUSE_NORLANDER_HEADERS
51 #include "wx/msw/gnuwin32/extra.h"
55 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
59 // Bug in headers, sometimes
61 #define TVIS_FOCUSED 0x0001
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // a convenient wrapper around TV_ITEM struct which adds a ctor
69 #pragma warning( disable : 4097 )
70 struct wxTreeViewItem
: public TV_ITEM
72 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
73 UINT mask_
, // fields which are valid
74 UINT stateMask_
= 0) // for TVIF_STATE only
76 // hItem member is always valid
77 mask
= mask_
| TVIF_HANDLE
;
78 stateMask
= stateMask_
;
79 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
82 #pragma warning( default : 4097 )
84 // a class which encapsulates the tree traversal logic: it vists all (unless
85 // OnVisit() returns FALSE) items under the given one
89 wxTreeTraversal(const wxTreeCtrl
*tree
)
94 // do traverse the tree: visit all items (recursively by default) under the
95 // given one; return TRUE if all items were traversed or FALSE if the
96 // traversal was aborted because OnVisit returned FALSE
97 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
99 // override this function to do whatever is needed for each item, return
100 // FALSE to stop traversing
101 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
104 const wxTreeCtrl
*GetTree() const { return m_tree
; }
107 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
109 const wxTreeCtrl
*m_tree
;
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 #if !USE_SHARED_LIBRARY
117 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 // handy table for sending events
125 static const wxEventType g_events
[2][2] =
127 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
128 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
131 // ============================================================================
133 // ============================================================================
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
141 if ( !OnVisit(root
) )
144 return Traverse(root
, recursively
);
147 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
150 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
151 while ( child
.IsOk() )
153 // depth first traversal
154 if ( recursively
&& !Traverse(child
, TRUE
) )
157 if ( !OnVisit(child
) )
160 child
= m_tree
->GetNextChild(root
, cookie
);
166 // ----------------------------------------------------------------------------
167 // construction and destruction
168 // ----------------------------------------------------------------------------
170 void wxTreeCtrl::Init()
172 m_imageListNormal
= NULL
;
173 m_imageListState
= NULL
;
177 bool wxTreeCtrl::Create(wxWindow
*parent
,
182 const wxValidator
& validator
,
183 const wxString
& name
)
187 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
190 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
191 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
193 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
194 wstyle
|= TVS_HASBUTTONS
;
196 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
197 wstyle
|= TVS_EDITLABELS
;
199 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
200 wstyle
|= TVS_LINESATROOT
;
202 #if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !defined(wxUSE_NORLANDER_HEADERS)
203 // we emulate the multiple selection tree controls by using checkboxes: set
204 // up the image list we need for this if we do have multiple selections
205 #if !defined(__VISUALC__) || (__VISUALC__ != 1010)
206 if ( m_windowStyle
& wxTR_MULTIPLE
)
207 wstyle
|= TVS_CHECKBOXES
;
211 // Create the tree control.
212 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
215 // the treectrl with any other background looks ugly because the items
216 // background is white anyhow
217 SetBackgroundColour(*wxWHITE
);
219 // VZ: this is some experimental code which may be used to get the
220 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
221 // AFAIK, the standard DLL does about the same thing anyhow.
223 if ( m_windowStyle
& wxTR_MULTIPLE
)
227 // create the DC compatible with the current screen
228 HDC hdcMem
= CreateCompatibleDC(NULL
);
230 // create a mono bitmap of the standard size
231 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
232 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
233 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
234 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
235 1, // # of color planes
236 1, // # bits needed for one pixel
237 0); // array containing colour data
238 SelectObject(hdcMem
, hbmpCheck
);
240 // then draw a check mark into it
241 RECT rect
= { 0, 0, x
, y
};
242 if ( !::DrawFrameControl(hdcMem
, &rect
,
244 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
246 wxLogLastError(_T("DrawFrameControl(check)"));
249 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
250 imagelistCheckboxes
.Add(bmp
);
252 if ( !::DrawFrameControl(hdcMem
, &rect
,
256 wxLogLastError(_T("DrawFrameControl(uncheck)"));
259 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
260 imagelistCheckboxes
.Add(bmp
);
266 SetStateImageList(&imagelistCheckboxes
);
270 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
275 wxTreeCtrl::~wxTreeCtrl()
279 // delete user data to prevent memory leaks
283 // ----------------------------------------------------------------------------
285 // ----------------------------------------------------------------------------
287 // simple wrappers which add error checking in debug mode
289 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
291 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
293 wxLogLastError("TreeView_GetItem");
301 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
303 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
305 wxLogLastError("TreeView_SetItem");
309 size_t wxTreeCtrl::GetCount() const
311 return (size_t)TreeView_GetCount(GetHwnd());
314 unsigned int wxTreeCtrl::GetIndent() const
316 return TreeView_GetIndent(GetHwnd());
319 void wxTreeCtrl::SetIndent(unsigned int indent
)
321 TreeView_SetIndent(GetHwnd(), indent
);
324 wxImageList
*wxTreeCtrl::GetImageList() const
326 return m_imageListNormal
;
329 wxImageList
*wxTreeCtrl::GetStateImageList() const
331 return m_imageListNormal
;
334 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
337 TreeView_SetImageList(GetHwnd(),
338 imageList
? imageList
->GetHIMAGELIST() : 0,
342 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
344 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
347 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
349 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
352 // internal class for counting tree items
354 class TraverseCounter
: public wxTreeTraversal
357 TraverseCounter(const wxTreeCtrl
*tree
,
358 const wxTreeItemId
& root
,
360 : wxTreeTraversal(tree
)
364 DoTraverse(root
, recursively
);
367 virtual bool OnVisit(const wxTreeItemId
& item
)
374 size_t GetCount() const { return m_count
; }
381 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
382 bool recursively
) const
384 TraverseCounter
counter(this, item
, recursively
);
386 return counter
.GetCount();
389 // ----------------------------------------------------------------------------
391 // ----------------------------------------------------------------------------
393 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
395 wxChar buf
[512]; // the size is arbitrary...
397 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
398 tvItem
.pszText
= buf
;
399 tvItem
.cchTextMax
= WXSIZEOF(buf
);
400 if ( !DoGetItem(&tvItem
) )
402 // don't return some garbage which was on stack, but an empty string
406 return wxString(buf
);
409 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
411 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
412 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
416 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
420 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
421 tvItem
.iSelectedImage
= imageSel
;
422 tvItem
.iImage
= image
;
426 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
428 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
431 return tvItem
.iImage
;
434 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
436 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
437 // change both normal and selected image - otherwise the change simply
438 // doesn't take place!
439 DoSetItemImages(item
, image
, GetItemSelectedImage(item
));
442 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
444 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
447 return tvItem
.iSelectedImage
;
450 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
452 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
453 // change both normal and selected image - otherwise the change simply
454 // doesn't take place!
455 DoSetItemImages(item
, GetItemImage(item
), image
);
458 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
460 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
461 if ( !DoGetItem(&tvItem
) )
466 return (wxTreeItemData
*)tvItem
.lParam
;
469 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
471 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
472 tvItem
.lParam
= (LPARAM
)data
;
476 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
478 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
479 tvItem
.cChildren
= (int)has
;
483 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
485 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
486 tvItem
.state
= bold
? TVIS_BOLD
: 0;
490 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
492 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
493 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
497 // ----------------------------------------------------------------------------
499 // ----------------------------------------------------------------------------
501 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
503 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
505 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
509 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
511 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
514 return tvItem
.cChildren
!= 0;
517 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
519 // probably not a good idea to put it here
520 //wxASSERT( ItemHasChildren(item) );
522 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
525 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
528 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
530 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
533 return (tvItem
.state
& TVIS_SELECTED
) != 0;
536 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
538 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
541 return (tvItem
.state
& TVIS_BOLD
) != 0;
544 // ----------------------------------------------------------------------------
546 // ----------------------------------------------------------------------------
548 wxTreeItemId
wxTreeCtrl::GetRootItem() const
550 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
553 wxTreeItemId
wxTreeCtrl::GetSelection() const
555 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
556 _T("this only works with single selection controls") );
558 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
561 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
563 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
566 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
569 // remember the last child returned in 'cookie'
570 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
572 return wxTreeItemId((WXHTREEITEM
)_cookie
);
575 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
578 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
579 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
585 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
587 // can this be done more efficiently?
590 wxTreeItemId childLast
,
591 child
= GetFirstChild(item
, cookie
);
592 while ( child
.IsOk() )
595 child
= GetNextChild(item
, cookie
);
601 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
603 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
606 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
608 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
611 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
613 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
616 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
618 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetNextVisible() "
619 "for must be visible itself!"));
621 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
624 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
626 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetPrevVisible() "
627 "for must be visible itself!"));
629 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
632 // ----------------------------------------------------------------------------
633 // multiple selections emulation
634 // ----------------------------------------------------------------------------
636 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
638 // receive the desired information.
639 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
642 // state image indices are 1 based
643 return ((tvItem
.state
>> 12) - 1) == 1;
646 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
648 // receive the desired information.
649 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
651 // state images are one-based
652 tvItem
.state
= (check
? 2 : 1) << 12;
657 // internal class for getting the selected
659 class TraverseSelections
: public wxTreeTraversal
662 TraverseSelections(const wxTreeCtrl
*tree
,
663 wxArrayTreeItemIds
& selections
)
664 : wxTreeTraversal(tree
), m_selections(selections
)
666 m_selections
.Empty();
668 DoTraverse(tree
->GetRootItem());
671 virtual bool OnVisit(const wxTreeItemId
& item
)
673 if ( GetTree()->IsItemChecked(item
) )
675 m_selections
.Add(item
);
682 wxArrayTreeItemIds
& m_selections
;
685 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
687 TraverseSelections
selector(this, selections
);
689 return selections
.GetCount();
692 // ----------------------------------------------------------------------------
694 // ----------------------------------------------------------------------------
696 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
697 wxTreeItemId hInsertAfter
,
698 const wxString
& text
,
699 int image
, int selectedImage
,
700 wxTreeItemData
*data
)
702 TV_INSERTSTRUCT tvIns
;
703 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
704 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
706 // This is how we insert the item as the first child: supply a NULL hInsertAfter
707 if (tvIns
.hInsertAfter
== (HTREEITEM
) 0)
709 tvIns
.hInsertAfter
= TVI_FIRST
;
713 if ( !text
.IsEmpty() )
716 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
722 tvIns
.item
.iImage
= image
;
724 if ( selectedImage
== -1 )
726 // take the same image for selected icon if not specified
727 selectedImage
= image
;
731 if ( selectedImage
!= -1 )
733 mask
|= TVIF_SELECTEDIMAGE
;
734 tvIns
.item
.iSelectedImage
= selectedImage
;
740 tvIns
.item
.lParam
= (LPARAM
)data
;
743 tvIns
.item
.mask
= mask
;
745 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
748 wxLogLastError("TreeView_InsertItem");
753 // associate the application tree item with Win32 tree item handle
754 data
->SetId((WXHTREEITEM
)id
);
757 return wxTreeItemId((WXHTREEITEM
)id
);
760 // for compatibility only
761 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
762 const wxString
& text
,
763 int image
, int selImage
,
766 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
767 image
, selImage
, NULL
);
770 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
771 int image
, int selectedImage
,
772 wxTreeItemData
*data
)
774 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
775 text
, image
, selectedImage
, data
);
778 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
779 const wxString
& text
,
780 int image
, int selectedImage
,
781 wxTreeItemData
*data
)
783 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
784 text
, image
, selectedImage
, data
);
787 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
788 const wxTreeItemId
& idPrevious
,
789 const wxString
& text
,
790 int image
, int selectedImage
,
791 wxTreeItemData
*data
)
793 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
796 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
797 const wxString
& text
,
798 int image
, int selectedImage
,
799 wxTreeItemData
*data
)
801 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
802 text
, image
, selectedImage
, data
);
805 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
807 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
809 wxLogLastError("TreeView_DeleteItem");
813 // delete all children (but don't delete the item itself)
814 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
818 wxArrayLong children
;
819 wxTreeItemId child
= GetFirstChild(item
, cookie
);
820 while ( child
.IsOk() )
822 children
.Add((long)(WXHTREEITEM
)child
);
824 child
= GetNextChild(item
, cookie
);
827 size_t nCount
= children
.Count();
828 for ( size_t n
= 0; n
< nCount
; n
++ )
830 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
832 wxLogLastError("TreeView_DeleteItem");
837 void wxTreeCtrl::DeleteAllItems()
839 if ( !TreeView_DeleteAllItems(GetHwnd()) )
841 wxLogLastError("TreeView_DeleteAllItems");
845 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
847 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
848 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
849 flag
== TVE_EXPAND
||
851 _T("Unknown flag in wxTreeCtrl::DoExpand") );
853 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
854 // emulate them. This behaviour has changed slightly with comctl32.dll
855 // v 4.70 - now it does send them but only the first time. To maintain
856 // compatible behaviour and also in order to not have surprises with the
857 // future versions, don't rely on this and still do everything ourselves.
858 // To avoid that the messages be sent twice when the item is expanded for
859 // the first time we must clear TVIS_EXPANDEDONCE style manually.
861 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
865 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
867 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
870 bool isExpanded
= IsExpanded(item
);
872 event
.SetEventObject(this);
874 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
875 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
876 GetEventHandler()->ProcessEvent(event
);
878 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
879 GetEventHandler()->ProcessEvent(event
);
881 //else: change didn't took place, so do nothing at all
884 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
886 DoExpand(item
, TVE_EXPAND
);
889 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
891 DoExpand(item
, TVE_COLLAPSE
);
894 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
896 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
899 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
901 DoExpand(item
, TVE_TOGGLE
);
904 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
906 DoExpand(item
, action
);
909 void wxTreeCtrl::Unselect()
911 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), _T("doesn't make sense") );
913 // just remove the selection
914 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
917 void wxTreeCtrl::UnselectAll()
919 if ( m_windowStyle
& wxTR_MULTIPLE
)
921 wxArrayTreeItemIds selections
;
922 size_t count
= GetSelections(selections
);
923 for ( size_t n
= 0; n
< count
; n
++ )
925 SetItemCheck(selections
[n
], FALSE
);
930 // just remove the selection
935 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
937 if ( m_windowStyle
& wxTR_MULTIPLE
)
939 // selecting the item means checking it
944 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
945 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
946 // send them ourselves
948 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
950 event
.SetEventObject(this);
952 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
953 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
955 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
957 wxLogLastError("TreeView_SelectItem");
961 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
962 (void)GetEventHandler()->ProcessEvent(event
);
965 //else: program vetoed the change
969 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
972 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
975 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
977 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
979 wxLogLastError("TreeView_SelectSetFirstVisible");
983 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
988 void wxTreeCtrl::DeleteTextCtrl()
992 m_textCtrl
->UnsubclassWin();
993 m_textCtrl
->SetHWND(0);
999 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1000 wxClassInfo
* textControlClass
)
1002 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1004 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1006 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1015 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1016 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1017 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1022 // End label editing, optionally cancelling the edit
1023 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1025 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1030 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1032 TV_HITTESTINFO hitTestInfo
;
1033 hitTestInfo
.pt
.x
= (int)point
.x
;
1034 hitTestInfo
.pt
.y
= (int)point
.y
;
1036 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1041 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1042 flags |= wxTREE_HITTEST_##flag
1044 TRANSLATE_FLAG(ABOVE
);
1045 TRANSLATE_FLAG(BELOW
);
1046 TRANSLATE_FLAG(NOWHERE
);
1047 TRANSLATE_FLAG(ONITEMBUTTON
);
1048 TRANSLATE_FLAG(ONITEMICON
);
1049 TRANSLATE_FLAG(ONITEMINDENT
);
1050 TRANSLATE_FLAG(ONITEMLABEL
);
1051 TRANSLATE_FLAG(ONITEMRIGHT
);
1052 TRANSLATE_FLAG(ONITEMSTATEICON
);
1053 TRANSLATE_FLAG(TOLEFT
);
1054 TRANSLATE_FLAG(TORIGHT
);
1056 #undef TRANSLATE_FLAG
1058 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1061 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1063 bool textOnly
) const
1066 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1069 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1075 // couldn't retrieve rect: for example, item isn't visible
1080 // ----------------------------------------------------------------------------
1082 // ----------------------------------------------------------------------------
1084 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1085 wxTreeItemData
*pItem2
,
1088 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1089 _T("sorting tree without data doesn't make sense") );
1091 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1094 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1095 const wxTreeItemId
& item2
)
1097 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1100 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1102 // rely on the fact that TreeView_SortChildren does the same thing as our
1103 // default behaviour, i.e. sorts items alphabetically and so call it
1104 // directly if we're not in derived class (much more efficient!)
1105 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1107 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1112 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1113 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1114 tvSort
.lParam
= (LPARAM
)this;
1115 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1119 // ----------------------------------------------------------------------------
1121 // ----------------------------------------------------------------------------
1123 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1125 if ( cmd
== EN_UPDATE
)
1127 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1128 event
.SetEventObject( this );
1129 ProcessCommand(event
);
1131 else if ( cmd
== EN_KILLFOCUS
)
1133 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1134 event
.SetEventObject( this );
1135 ProcessCommand(event
);
1143 // command processed
1147 // process WM_NOTIFY Windows message
1148 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1150 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1151 wxEventType eventType
= wxEVT_NULL
;
1152 NMHDR
*hdr
= (NMHDR
*)lParam
;
1154 switch ( hdr
->code
)
1157 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1160 case TVN_BEGINRDRAG
:
1162 if ( eventType
== wxEVT_NULL
)
1163 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1164 //else: left drag, already set above
1166 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1168 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1169 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1173 case TVN_BEGINLABELEDIT
:
1175 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1176 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1178 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1179 event
.m_label
= info
->item
.pszText
;
1183 case TVN_DELETEITEM
:
1185 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1186 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1188 event
.m_item
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1192 case TVN_ENDLABELEDIT
:
1194 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1195 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1197 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1198 event
.m_label
= info
->item
.pszText
;
1202 case TVN_GETDISPINFO
:
1203 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1206 case TVN_SETDISPINFO
:
1208 if ( eventType
== wxEVT_NULL
)
1209 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1210 //else: get, already set above
1212 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1214 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1218 case TVN_ITEMEXPANDING
:
1219 event
.m_code
= FALSE
;
1222 case TVN_ITEMEXPANDED
:
1224 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1226 bool expand
= FALSE
;
1227 switch ( tv
->action
)
1238 wxLogDebug(_T("unexpected code %d in TVN_ITEMEXPAND "
1239 "message"), tv
->action
);
1242 bool ing
= (hdr
->code
== TVN_ITEMEXPANDING
);
1243 eventType
= g_events
[expand
][ing
];
1245 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1251 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1252 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1254 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1256 // a separate event for this case
1257 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1259 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1261 event2
.SetEventObject(this);
1263 GetEventHandler()->ProcessEvent(event2
);
1268 case TVN_SELCHANGED
:
1269 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1272 case TVN_SELCHANGING
:
1274 if ( eventType
== wxEVT_NULL
)
1275 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1276 //else: already set above
1278 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1280 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1281 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1286 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1289 event
.SetEventObject(this);
1290 event
.SetEventType(eventType
);
1292 bool processed
= GetEventHandler()->ProcessEvent(event
);
1295 switch ( hdr
->code
)
1297 case TVN_DELETEITEM
:
1299 // NB: we might process this message using wxWindows event
1300 // tables, but due to overhead of wxWin event system we
1301 // prefer to do it here ourself (otherwise deleting a tree
1302 // with many items is just too slow)
1303 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1304 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1305 delete data
; // may be NULL, ok
1307 processed
= TRUE
; // Make sure we don't get called twice
1311 case TVN_BEGINLABELEDIT
:
1312 // return TRUE to cancel label editing
1313 *result
= !event
.IsAllowed();
1316 case TVN_ENDLABELEDIT
:
1317 // return TRUE to set the label to the new string
1318 *result
= event
.IsAllowed();
1320 // ensure that we don't have the text ctrl which is going to be
1325 case TVN_SELCHANGING
:
1326 case TVN_ITEMEXPANDING
:
1327 // return TRUE to prevent the action from happening
1328 *result
= !event
.IsAllowed();
1332 // for the other messages the return value is ignored and there is
1333 // nothing special to do
1339 // ----------------------------------------------------------------------------
1341 // ----------------------------------------------------------------------------
1343 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1345 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1346 : wxNotifyEvent(commandType
, id
)