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 #include "wx/msw/gnuwin32/extra.h"
53 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
57 // Bug in headers, sometimes
59 #define TVIS_FOCUSED 0x0001
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // a convenient wrapper around TV_ITEM struct which adds a ctor
67 struct wxTreeViewItem
: public TV_ITEM
69 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
70 UINT mask_
, // fields which are valid
71 UINT stateMask_
= 0) // for TVIF_STATE only
73 // hItem member is always valid
74 mask
= mask_
| TVIF_HANDLE
;
75 stateMask
= stateMask_
;
76 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
80 // a class which encapsulates the tree traversal logic: it vists all (unless
81 // OnVisit() returns FALSE) items under the given one
85 wxTreeTraversal(const wxTreeCtrl
*tree
)
90 // do traverse the tree: visit all items (recursively by default) under the
91 // given one; return TRUE if all items were traversed or FALSE if the
92 // traversal was aborted because OnVisit returned FALSE
93 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
95 // override this function to do whatever is needed for each item, return
96 // FALSE to stop traversing
97 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
100 const wxTreeCtrl
*GetTree() const { return m_tree
; }
103 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
105 const wxTreeCtrl
*m_tree
;
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 #if !USE_SHARED_LIBRARY
113 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 // handy table for sending events
121 static const wxEventType g_events
[2][2] =
123 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
124 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
127 // ============================================================================
129 // ============================================================================
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
137 if ( !OnVisit(root
) )
140 return Traverse(root
, recursively
);
143 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
146 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
147 while ( child
.IsOk() )
149 // depth first traversal
150 if ( recursively
&& !Traverse(child
, TRUE
) )
153 if ( !OnVisit(child
) )
156 child
= m_tree
->GetNextChild(root
, cookie
);
162 // ----------------------------------------------------------------------------
163 // construction and destruction
164 // ----------------------------------------------------------------------------
166 void wxTreeCtrl::Init()
168 m_imageListNormal
= NULL
;
169 m_imageListState
= NULL
;
173 bool wxTreeCtrl::Create(wxWindow
*parent
,
178 const wxValidator
& validator
,
179 const wxString
& name
)
183 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
186 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
187 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
189 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
190 wstyle
|= TVS_HASBUTTONS
;
192 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
193 wstyle
|= TVS_EDITLABELS
;
195 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
196 wstyle
|= TVS_LINESATROOT
;
198 // we emulate the multiple selection tree controls by using checkboxes: set
199 // up the image list we need for this if we do have multiple selections
200 if ( m_windowStyle
& wxTR_MULTIPLE
)
201 wstyle
|= TVS_CHECKBOXES
;
203 // Create the tree control.
204 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
207 // VZ: this is some experimental code which may be used to get the
208 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
209 // AFAIK, the standard DLL does about the same thing anyhow.
211 if ( m_windowStyle
& wxTR_MULTIPLE
)
215 // create the DC compatible with the current screen
216 HDC hdcMem
= CreateCompatibleDC(NULL
);
218 // create a mono bitmap of the standard size
219 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
220 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
221 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
222 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
223 1, // # of color planes
224 1, // # bits needed for one pixel
225 0); // array containing colour data
226 SelectObject(hdcMem
, hbmpCheck
);
228 // then draw a check mark into it
229 RECT rect
= { 0, 0, x
, y
};
230 if ( !::DrawFrameControl(hdcMem
, &rect
,
232 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
234 wxLogLastError(_T("DrawFrameControl(check)"));
237 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
238 imagelistCheckboxes
.Add(bmp
);
240 if ( !::DrawFrameControl(hdcMem
, &rect
,
244 wxLogLastError(_T("DrawFrameControl(uncheck)"));
247 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
248 imagelistCheckboxes
.Add(bmp
);
254 SetStateImageList(&imagelistCheckboxes
);
258 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
263 wxTreeCtrl::~wxTreeCtrl()
267 // delete user data to prevent memory leaks
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 // simple wrappers which add error checking in debug mode
277 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
279 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
281 wxLogLastError("TreeView_GetItem");
289 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
291 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
293 wxLogLastError("TreeView_SetItem");
297 size_t wxTreeCtrl::GetCount() const
299 return (size_t)TreeView_GetCount(GetHwnd());
302 unsigned int wxTreeCtrl::GetIndent() const
304 return TreeView_GetIndent(GetHwnd());
307 void wxTreeCtrl::SetIndent(unsigned int indent
)
309 TreeView_SetIndent(GetHwnd(), indent
);
312 wxImageList
*wxTreeCtrl::GetImageList() const
314 return m_imageListNormal
;
317 wxImageList
*wxTreeCtrl::GetStateImageList() const
319 return m_imageListNormal
;
322 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
325 TreeView_SetImageList(GetHwnd(),
326 imageList
? imageList
->GetHIMAGELIST() : 0,
330 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
332 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
335 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
337 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
340 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
341 bool recursively
) const
343 class TraverseCounter
: public wxTreeTraversal
346 TraverseCounter(const wxTreeCtrl
*tree
,
347 const wxTreeItemId
& root
,
349 : wxTreeTraversal(tree
)
353 DoTraverse(root
, recursively
);
356 virtual bool OnVisit(const wxTreeItemId
& item
)
363 size_t GetCount() const { return m_count
; }
367 } counter(this, item
, recursively
);
369 return counter
.GetCount();
372 // ----------------------------------------------------------------------------
374 // ----------------------------------------------------------------------------
376 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
378 wxChar buf
[512]; // the size is arbitrary...
380 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
381 tvItem
.pszText
= buf
;
382 tvItem
.cchTextMax
= WXSIZEOF(buf
);
383 if ( !DoGetItem(&tvItem
) )
385 // don't return some garbage which was on stack, but an empty string
389 return wxString(buf
);
392 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
394 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
395 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
399 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
403 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
404 tvItem
.iSelectedImage
= imageSel
;
405 tvItem
.iImage
= image
;
409 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
411 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
414 return tvItem
.iImage
;
417 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
419 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
420 // change both normal and selected image - otherwise the change simply
421 // doesn't take place!
422 DoSetItemImages(item
, image
, GetItemSelectedImage(item
));
425 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
427 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
430 return tvItem
.iSelectedImage
;
433 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
435 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
436 // change both normal and selected image - otherwise the change simply
437 // doesn't take place!
438 DoSetItemImages(item
, GetItemImage(item
), image
);
441 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
443 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
444 if ( !DoGetItem(&tvItem
) )
449 return (wxTreeItemData
*)tvItem
.lParam
;
452 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
454 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
455 tvItem
.lParam
= (LPARAM
)data
;
459 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
461 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
462 tvItem
.cChildren
= (int)has
;
466 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
468 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
469 tvItem
.state
= bold
? TVIS_BOLD
: 0;
473 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
475 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
476 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
480 // ----------------------------------------------------------------------------
482 // ----------------------------------------------------------------------------
484 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
486 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
488 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
492 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
494 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
497 return tvItem
.cChildren
!= 0;
500 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
502 // probably not a good idea to put it here
503 //wxASSERT( ItemHasChildren(item) );
505 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
508 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
511 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
513 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
516 return (tvItem
.state
& TVIS_SELECTED
) != 0;
519 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
521 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
524 return (tvItem
.state
& TVIS_BOLD
) != 0;
527 // ----------------------------------------------------------------------------
529 // ----------------------------------------------------------------------------
531 wxTreeItemId
wxTreeCtrl::GetRootItem() const
533 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
536 wxTreeItemId
wxTreeCtrl::GetSelection() const
538 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
539 _T("this only works with single selection controls") );
541 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
544 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
546 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
549 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
552 // remember the last child returned in 'cookie'
553 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
555 return wxTreeItemId((WXHTREEITEM
)_cookie
);
558 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
561 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
562 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
568 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
570 // can this be done more efficiently?
573 wxTreeItemId childLast
,
574 child
= GetFirstChild(item
, cookie
);
575 while ( child
.IsOk() )
578 child
= GetNextChild(item
, cookie
);
584 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
586 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
589 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
591 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
594 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
596 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
599 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
601 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetNextVisible() "
602 "for must be visible itself!"));
604 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
607 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
609 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetPrevVisible() "
610 "for must be visible itself!"));
612 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
615 // ----------------------------------------------------------------------------
616 // multiple selections emulation
617 // ----------------------------------------------------------------------------
619 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
621 // receive the desired information.
622 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
625 // state image indices are 1 based
626 return ((tvItem
.state
>> 12) - 1) == 1;
629 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
631 // receive the desired information.
632 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
634 // state images are one-based
635 tvItem
.state
= (check
? 2 : 1) << 12;
640 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
642 class TraverseSelections
: public wxTreeTraversal
645 TraverseSelections(const wxTreeCtrl
*tree
,
646 wxArrayTreeItemIds
& selections
)
647 : wxTreeTraversal(tree
), m_selections(selections
)
649 m_selections
.Empty();
651 DoTraverse(tree
->GetRootItem());
654 virtual bool OnVisit(const wxTreeItemId
& item
)
656 if ( GetTree()->IsItemChecked(item
) )
658 m_selections
.Add(item
);
665 wxArrayTreeItemIds
& m_selections
;
666 } selector(this, selections
);
668 return selections
.GetCount();
671 // ----------------------------------------------------------------------------
673 // ----------------------------------------------------------------------------
675 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
676 wxTreeItemId hInsertAfter
,
677 const wxString
& text
,
678 int image
, int selectedImage
,
679 wxTreeItemData
*data
)
681 TV_INSERTSTRUCT tvIns
;
682 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
683 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
685 // This is how we insert the item as the first child: supply a NULL hInsertAfter
686 if (tvIns
.hInsertAfter
== (HTREEITEM
) 0)
688 tvIns
.hInsertAfter
= TVI_FIRST
;
692 if ( !text
.IsEmpty() )
695 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
701 tvIns
.item
.iImage
= image
;
703 if ( selectedImage
== -1 )
705 // take the same image for selected icon if not specified
706 selectedImage
= image
;
710 if ( selectedImage
!= -1 )
712 mask
|= TVIF_SELECTEDIMAGE
;
713 tvIns
.item
.iSelectedImage
= selectedImage
;
719 tvIns
.item
.lParam
= (LPARAM
)data
;
722 tvIns
.item
.mask
= mask
;
724 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
727 wxLogLastError("TreeView_InsertItem");
732 // associate the application tree item with Win32 tree item handle
733 data
->SetId((WXHTREEITEM
)id
);
736 return wxTreeItemId((WXHTREEITEM
)id
);
739 // for compatibility only
740 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
741 const wxString
& text
,
742 int image
, int selImage
,
745 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
746 image
, selImage
, NULL
);
749 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
750 int image
, int selectedImage
,
751 wxTreeItemData
*data
)
753 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
754 text
, image
, selectedImage
, data
);
757 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
758 const wxString
& text
,
759 int image
, int selectedImage
,
760 wxTreeItemData
*data
)
762 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
763 text
, image
, selectedImage
, data
);
766 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
767 const wxTreeItemId
& idPrevious
,
768 const wxString
& text
,
769 int image
, int selectedImage
,
770 wxTreeItemData
*data
)
772 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
775 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
776 const wxString
& text
,
777 int image
, int selectedImage
,
778 wxTreeItemData
*data
)
780 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
781 text
, image
, selectedImage
, data
);
784 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
786 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
788 wxLogLastError("TreeView_DeleteItem");
792 // delete all children (but don't delete the item itself)
793 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
797 wxArrayLong children
;
798 wxTreeItemId child
= GetFirstChild(item
, cookie
);
799 while ( child
.IsOk() )
801 children
.Add((long)(WXHTREEITEM
)child
);
803 child
= GetNextChild(item
, cookie
);
806 size_t nCount
= children
.Count();
807 for ( size_t n
= 0; n
< nCount
; n
++ )
809 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
811 wxLogLastError("TreeView_DeleteItem");
816 void wxTreeCtrl::DeleteAllItems()
818 if ( !TreeView_DeleteAllItems(GetHwnd()) )
820 wxLogLastError("TreeView_DeleteAllItems");
824 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
826 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
827 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
828 flag
== TVE_EXPAND
||
830 _T("Unknown flag in wxTreeCtrl::DoExpand") );
832 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
833 // emulate them. This behaviour has changed slightly with comctl32.dll
834 // v 4.70 - now it does send them but only the first time. To maintain
835 // compatible behaviour and also in order to not have surprises with the
836 // future versions, don't rely on this and still do everything ourselves.
837 // To avoid that the messages be sent twice when the item is expanded for
838 // the first time we must clear TVIS_EXPANDEDONCE style manually.
840 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
844 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
846 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
849 bool isExpanded
= IsExpanded(item
);
851 event
.SetEventObject(this);
853 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
854 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
855 GetEventHandler()->ProcessEvent(event
);
857 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
858 GetEventHandler()->ProcessEvent(event
);
860 //else: change didn't took place, so do nothing at all
863 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
865 DoExpand(item
, TVE_EXPAND
);
868 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
870 DoExpand(item
, TVE_COLLAPSE
);
873 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
875 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
878 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
880 DoExpand(item
, TVE_TOGGLE
);
883 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
885 DoExpand(item
, action
);
888 void wxTreeCtrl::Unselect()
890 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), _T("doesn't make sense") );
892 // just remove the selection
893 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
896 void wxTreeCtrl::UnselectAll()
898 if ( m_windowStyle
& wxTR_MULTIPLE
)
900 wxArrayTreeItemIds selections
;
901 size_t count
= GetSelections(selections
);
902 for ( size_t n
= 0; n
< count
; n
++ )
904 SetItemCheck(selections
[n
], FALSE
);
909 // just remove the selection
914 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
916 if ( m_windowStyle
& wxTR_MULTIPLE
)
918 // selecting the item means checking it
923 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
924 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
925 // send them ourselves
927 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
929 event
.SetEventObject(this);
931 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
932 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
934 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
936 wxLogLastError("TreeView_SelectItem");
940 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
941 (void)GetEventHandler()->ProcessEvent(event
);
944 //else: program vetoed the change
948 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
951 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
954 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
956 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
958 wxLogLastError("TreeView_SelectSetFirstVisible");
962 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
967 void wxTreeCtrl::DeleteTextCtrl()
971 m_textCtrl
->UnsubclassWin();
972 m_textCtrl
->SetHWND(0);
978 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
979 wxClassInfo
* textControlClass
)
981 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
983 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
985 // this is not an error - the TVN_BEGINLABELEDIT handler might have
994 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
995 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
996 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1001 // End label editing, optionally cancelling the edit
1002 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1004 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1009 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1011 TV_HITTESTINFO hitTestInfo
;
1012 hitTestInfo
.pt
.x
= (int)point
.x
;
1013 hitTestInfo
.pt
.y
= (int)point
.y
;
1015 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1020 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1021 flags |= wxTREE_HITTEST_##flag
1023 TRANSLATE_FLAG(ABOVE
);
1024 TRANSLATE_FLAG(BELOW
);
1025 TRANSLATE_FLAG(NOWHERE
);
1026 TRANSLATE_FLAG(ONITEMBUTTON
);
1027 TRANSLATE_FLAG(ONITEMICON
);
1028 TRANSLATE_FLAG(ONITEMINDENT
);
1029 TRANSLATE_FLAG(ONITEMLABEL
);
1030 TRANSLATE_FLAG(ONITEMRIGHT
);
1031 TRANSLATE_FLAG(ONITEMSTATEICON
);
1032 TRANSLATE_FLAG(TOLEFT
);
1033 TRANSLATE_FLAG(TORIGHT
);
1035 #undef TRANSLATE_FLAG
1037 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1040 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1042 bool textOnly
) const
1045 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1048 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1054 // couldn't retrieve rect: for example, item isn't visible
1059 // ----------------------------------------------------------------------------
1061 // ----------------------------------------------------------------------------
1063 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1064 wxTreeItemData
*pItem2
,
1067 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1068 _T("sorting tree without data doesn't make sense") );
1070 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1073 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1074 const wxTreeItemId
& item2
)
1076 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1079 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1081 // rely on the fact that TreeView_SortChildren does the same thing as our
1082 // default behaviour, i.e. sorts items alphabetically and so call it
1083 // directly if we're not in derived class (much more efficient!)
1084 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1086 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1091 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1092 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1093 tvSort
.lParam
= (LPARAM
)this;
1094 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1098 // ----------------------------------------------------------------------------
1100 // ----------------------------------------------------------------------------
1102 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1104 if ( cmd
== EN_UPDATE
)
1106 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1107 event
.SetEventObject( this );
1108 ProcessCommand(event
);
1110 else if ( cmd
== EN_KILLFOCUS
)
1112 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1113 event
.SetEventObject( this );
1114 ProcessCommand(event
);
1122 // command processed
1126 // process WM_NOTIFY Windows message
1127 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1129 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1130 wxEventType eventType
= wxEVT_NULL
;
1131 NMHDR
*hdr
= (NMHDR
*)lParam
;
1133 switch ( hdr
->code
)
1136 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1139 case TVN_BEGINRDRAG
:
1141 if ( eventType
== wxEVT_NULL
)
1142 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1143 //else: left drag, already set above
1145 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1147 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1148 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1152 case TVN_BEGINLABELEDIT
:
1154 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1155 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1157 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1158 event
.m_label
= info
->item
.pszText
;
1162 case TVN_DELETEITEM
:
1164 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1165 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1167 event
.m_item
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1171 case TVN_ENDLABELEDIT
:
1173 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1174 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1176 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1177 event
.m_label
= info
->item
.pszText
;
1181 case TVN_GETDISPINFO
:
1182 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1185 case TVN_SETDISPINFO
:
1187 if ( eventType
== wxEVT_NULL
)
1188 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1189 //else: get, already set above
1191 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1193 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1197 case TVN_ITEMEXPANDING
:
1198 event
.m_code
= FALSE
;
1201 case TVN_ITEMEXPANDED
:
1203 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1205 bool expand
= FALSE
;
1206 switch ( tv
->action
)
1217 wxLogDebug(_T("unexpected code %d in TVN_ITEMEXPAND "
1218 "message"), tv
->action
);
1221 bool ing
= (hdr
->code
== TVN_ITEMEXPANDING
);
1222 eventType
= g_events
[expand
][ing
];
1224 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1230 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1231 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1233 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1235 // a separate event for this case
1236 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1238 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1240 event2
.SetEventObject(this);
1242 GetEventHandler()->ProcessEvent(event2
);
1247 case TVN_SELCHANGED
:
1248 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1251 case TVN_SELCHANGING
:
1253 if ( eventType
== wxEVT_NULL
)
1254 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1255 //else: already set above
1257 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1259 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1260 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1265 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1268 event
.SetEventObject(this);
1269 event
.SetEventType(eventType
);
1271 bool processed
= GetEventHandler()->ProcessEvent(event
);
1274 switch ( hdr
->code
)
1276 case TVN_DELETEITEM
:
1278 // NB: we might process this message using wxWindows event
1279 // tables, but due to overhead of wxWin event system we
1280 // prefer to do it here ourself (otherwise deleting a tree
1281 // with many items is just too slow)
1282 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1283 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1284 delete data
; // may be NULL, ok
1286 processed
= TRUE
; // Make sure we don't get called twice
1290 case TVN_BEGINLABELEDIT
:
1291 // return TRUE to cancel label editing
1292 *result
= !event
.IsAllowed();
1295 case TVN_ENDLABELEDIT
:
1296 // return TRUE to set the label to the new string
1297 *result
= event
.IsAllowed();
1299 // ensure that we don't have the text ctrl which is going to be
1304 case TVN_SELCHANGING
:
1305 case TVN_ITEMEXPANDING
:
1306 // return TRUE to prevent the action from happening
1307 *result
= !event
.IsAllowed();
1311 // for the other messages the return value is ignored and there is
1312 // nothing special to do
1318 // ----------------------------------------------------------------------------
1320 // ----------------------------------------------------------------------------
1322 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1324 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1325 : wxNotifyEvent(commandType
, id
)