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 struct wxTreeViewItem
: public TV_ITEM
71 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
72 UINT mask_
, // fields which are valid
73 UINT stateMask_
= 0) // for TVIF_STATE only
75 // hItem member is always valid
76 mask
= mask_
| TVIF_HANDLE
;
77 stateMask
= stateMask_
;
78 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
82 // a class which encapsulates the tree traversal logic: it vists all (unless
83 // OnVisit() returns FALSE) items under the given one
87 wxTreeTraversal(const wxTreeCtrl
*tree
)
92 // do traverse the tree: visit all items (recursively by default) under the
93 // given one; return TRUE if all items were traversed or FALSE if the
94 // traversal was aborted because OnVisit returned FALSE
95 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
97 // override this function to do whatever is needed for each item, return
98 // FALSE to stop traversing
99 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
102 const wxTreeCtrl
*GetTree() const { return m_tree
; }
105 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
107 const wxTreeCtrl
*m_tree
;
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 #if !USE_SHARED_LIBRARY
115 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 // handy table for sending events
123 static const wxEventType g_events
[2][2] =
125 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
126 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
129 // ============================================================================
131 // ============================================================================
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
139 if ( !OnVisit(root
) )
142 return Traverse(root
, recursively
);
145 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
148 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
149 while ( child
.IsOk() )
151 // depth first traversal
152 if ( recursively
&& !Traverse(child
, TRUE
) )
155 if ( !OnVisit(child
) )
158 child
= m_tree
->GetNextChild(root
, cookie
);
164 // ----------------------------------------------------------------------------
165 // construction and destruction
166 // ----------------------------------------------------------------------------
168 void wxTreeCtrl::Init()
170 m_imageListNormal
= NULL
;
171 m_imageListState
= NULL
;
175 bool wxTreeCtrl::Create(wxWindow
*parent
,
180 const wxValidator
& validator
,
181 const wxString
& name
)
185 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
188 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
189 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
191 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
192 wstyle
|= TVS_HASBUTTONS
;
194 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
195 wstyle
|= TVS_EDITLABELS
;
197 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
198 wstyle
|= TVS_LINESATROOT
;
200 #if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !defined(wxUSE_NORLANDER_HEADERS)
201 // we emulate the multiple selection tree controls by using checkboxes: set
202 // up the image list we need for this if we do have multiple selections
203 #if !defined(__VISUALC__) || (__VISUALC__ != 1010)
204 if ( m_windowStyle
& wxTR_MULTIPLE
)
205 wstyle
|= TVS_CHECKBOXES
;
209 // Create the tree control.
210 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
213 // the treectrl with any other background looks ugly because the items
214 // background is white anyhow
215 SetBackgroundColour(*wxWHITE
);
217 // VZ: this is some experimental code which may be used to get the
218 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
219 // AFAIK, the standard DLL does about the same thing anyhow.
221 if ( m_windowStyle
& wxTR_MULTIPLE
)
225 // create the DC compatible with the current screen
226 HDC hdcMem
= CreateCompatibleDC(NULL
);
228 // create a mono bitmap of the standard size
229 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
230 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
231 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
232 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
233 1, // # of color planes
234 1, // # bits needed for one pixel
235 0); // array containing colour data
236 SelectObject(hdcMem
, hbmpCheck
);
238 // then draw a check mark into it
239 RECT rect
= { 0, 0, x
, y
};
240 if ( !::DrawFrameControl(hdcMem
, &rect
,
242 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
244 wxLogLastError(_T("DrawFrameControl(check)"));
247 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
248 imagelistCheckboxes
.Add(bmp
);
250 if ( !::DrawFrameControl(hdcMem
, &rect
,
254 wxLogLastError(_T("DrawFrameControl(uncheck)"));
257 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
258 imagelistCheckboxes
.Add(bmp
);
264 SetStateImageList(&imagelistCheckboxes
);
268 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
273 wxTreeCtrl::~wxTreeCtrl()
277 // delete user data to prevent memory leaks
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 // simple wrappers which add error checking in debug mode
287 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
289 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
291 wxLogLastError("TreeView_GetItem");
299 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
301 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
303 wxLogLastError("TreeView_SetItem");
307 size_t wxTreeCtrl::GetCount() const
309 return (size_t)TreeView_GetCount(GetHwnd());
312 unsigned int wxTreeCtrl::GetIndent() const
314 return TreeView_GetIndent(GetHwnd());
317 void wxTreeCtrl::SetIndent(unsigned int indent
)
319 TreeView_SetIndent(GetHwnd(), indent
);
322 wxImageList
*wxTreeCtrl::GetImageList() const
324 return m_imageListNormal
;
327 wxImageList
*wxTreeCtrl::GetStateImageList() const
329 return m_imageListNormal
;
332 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
335 TreeView_SetImageList(GetHwnd(),
336 imageList
? imageList
->GetHIMAGELIST() : 0,
340 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
342 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
345 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
347 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
350 // internal class for counting tree items
352 class TraverseCounter
: public wxTreeTraversal
355 TraverseCounter(const wxTreeCtrl
*tree
,
356 const wxTreeItemId
& root
,
358 : wxTreeTraversal(tree
)
362 DoTraverse(root
, recursively
);
365 virtual bool OnVisit(const wxTreeItemId
& item
)
372 size_t GetCount() const { return m_count
; }
379 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
380 bool recursively
) const
382 TraverseCounter
counter(this, item
, recursively
);
384 return counter
.GetCount();
387 // ----------------------------------------------------------------------------
389 // ----------------------------------------------------------------------------
391 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
393 wxChar buf
[512]; // the size is arbitrary...
395 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
396 tvItem
.pszText
= buf
;
397 tvItem
.cchTextMax
= WXSIZEOF(buf
);
398 if ( !DoGetItem(&tvItem
) )
400 // don't return some garbage which was on stack, but an empty string
404 return wxString(buf
);
407 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
409 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
410 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
414 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
418 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
419 tvItem
.iSelectedImage
= imageSel
;
420 tvItem
.iImage
= image
;
424 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
426 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
429 return tvItem
.iImage
;
432 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
434 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
435 // change both normal and selected image - otherwise the change simply
436 // doesn't take place!
437 DoSetItemImages(item
, image
, GetItemSelectedImage(item
));
440 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
442 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
445 return tvItem
.iSelectedImage
;
448 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
450 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
451 // change both normal and selected image - otherwise the change simply
452 // doesn't take place!
453 DoSetItemImages(item
, GetItemImage(item
), image
);
456 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
458 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
459 if ( !DoGetItem(&tvItem
) )
464 return (wxTreeItemData
*)tvItem
.lParam
;
467 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
469 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
470 tvItem
.lParam
= (LPARAM
)data
;
474 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
476 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
477 tvItem
.cChildren
= (int)has
;
481 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
483 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
484 tvItem
.state
= bold
? TVIS_BOLD
: 0;
488 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
490 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
491 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
495 // ----------------------------------------------------------------------------
497 // ----------------------------------------------------------------------------
499 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
501 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
503 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
507 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
509 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
512 return tvItem
.cChildren
!= 0;
515 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
517 // probably not a good idea to put it here
518 //wxASSERT( ItemHasChildren(item) );
520 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
523 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
526 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
528 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
531 return (tvItem
.state
& TVIS_SELECTED
) != 0;
534 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
536 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
539 return (tvItem
.state
& TVIS_BOLD
) != 0;
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
546 wxTreeItemId
wxTreeCtrl::GetRootItem() const
548 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
551 wxTreeItemId
wxTreeCtrl::GetSelection() const
553 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
554 _T("this only works with single selection controls") );
556 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
559 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
561 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
564 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
567 // remember the last child returned in 'cookie'
568 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
570 return wxTreeItemId((WXHTREEITEM
)_cookie
);
573 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
576 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
577 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
583 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
585 // can this be done more efficiently?
588 wxTreeItemId childLast
,
589 child
= GetFirstChild(item
, cookie
);
590 while ( child
.IsOk() )
593 child
= GetNextChild(item
, cookie
);
599 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
601 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
604 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
606 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
609 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
611 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
614 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
616 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetNextVisible() "
617 "for must be visible itself!"));
619 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
622 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
624 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetPrevVisible() "
625 "for must be visible itself!"));
627 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
630 // ----------------------------------------------------------------------------
631 // multiple selections emulation
632 // ----------------------------------------------------------------------------
634 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
636 // receive the desired information.
637 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
640 // state image indices are 1 based
641 return ((tvItem
.state
>> 12) - 1) == 1;
644 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
646 // receive the desired information.
647 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
649 // state images are one-based
650 tvItem
.state
= (check
? 2 : 1) << 12;
655 // internal class for getting the selected
657 class TraverseSelections
: public wxTreeTraversal
660 TraverseSelections(const wxTreeCtrl
*tree
,
661 wxArrayTreeItemIds
& selections
)
662 : wxTreeTraversal(tree
), m_selections(selections
)
664 m_selections
.Empty();
666 DoTraverse(tree
->GetRootItem());
669 virtual bool OnVisit(const wxTreeItemId
& item
)
671 if ( GetTree()->IsItemChecked(item
) )
673 m_selections
.Add(item
);
680 wxArrayTreeItemIds
& m_selections
;
683 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
685 TraverseSelections
selector(this, selections
);
687 return selections
.GetCount();
690 // ----------------------------------------------------------------------------
692 // ----------------------------------------------------------------------------
694 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
695 wxTreeItemId hInsertAfter
,
696 const wxString
& text
,
697 int image
, int selectedImage
,
698 wxTreeItemData
*data
)
700 TV_INSERTSTRUCT tvIns
;
701 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
702 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
704 // This is how we insert the item as the first child: supply a NULL hInsertAfter
705 if (tvIns
.hInsertAfter
== (HTREEITEM
) 0)
707 tvIns
.hInsertAfter
= TVI_FIRST
;
711 if ( !text
.IsEmpty() )
714 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
720 tvIns
.item
.iImage
= image
;
722 if ( selectedImage
== -1 )
724 // take the same image for selected icon if not specified
725 selectedImage
= image
;
729 if ( selectedImage
!= -1 )
731 mask
|= TVIF_SELECTEDIMAGE
;
732 tvIns
.item
.iSelectedImage
= selectedImage
;
738 tvIns
.item
.lParam
= (LPARAM
)data
;
741 tvIns
.item
.mask
= mask
;
743 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
746 wxLogLastError("TreeView_InsertItem");
751 // associate the application tree item with Win32 tree item handle
752 data
->SetId((WXHTREEITEM
)id
);
755 return wxTreeItemId((WXHTREEITEM
)id
);
758 // for compatibility only
759 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
760 const wxString
& text
,
761 int image
, int selImage
,
764 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
765 image
, selImage
, NULL
);
768 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
769 int image
, int selectedImage
,
770 wxTreeItemData
*data
)
772 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
773 text
, image
, selectedImage
, data
);
776 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
777 const wxString
& text
,
778 int image
, int selectedImage
,
779 wxTreeItemData
*data
)
781 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
782 text
, image
, selectedImage
, data
);
785 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
786 const wxTreeItemId
& idPrevious
,
787 const wxString
& text
,
788 int image
, int selectedImage
,
789 wxTreeItemData
*data
)
791 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
794 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
795 const wxString
& text
,
796 int image
, int selectedImage
,
797 wxTreeItemData
*data
)
799 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
800 text
, image
, selectedImage
, data
);
803 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
805 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
807 wxLogLastError("TreeView_DeleteItem");
811 // delete all children (but don't delete the item itself)
812 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
816 wxArrayLong children
;
817 wxTreeItemId child
= GetFirstChild(item
, cookie
);
818 while ( child
.IsOk() )
820 children
.Add((long)(WXHTREEITEM
)child
);
822 child
= GetNextChild(item
, cookie
);
825 size_t nCount
= children
.Count();
826 for ( size_t n
= 0; n
< nCount
; n
++ )
828 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
830 wxLogLastError("TreeView_DeleteItem");
835 void wxTreeCtrl::DeleteAllItems()
837 if ( !TreeView_DeleteAllItems(GetHwnd()) )
839 wxLogLastError("TreeView_DeleteAllItems");
843 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
845 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
846 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
847 flag
== TVE_EXPAND
||
849 _T("Unknown flag in wxTreeCtrl::DoExpand") );
851 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
852 // emulate them. This behaviour has changed slightly with comctl32.dll
853 // v 4.70 - now it does send them but only the first time. To maintain
854 // compatible behaviour and also in order to not have surprises with the
855 // future versions, don't rely on this and still do everything ourselves.
856 // To avoid that the messages be sent twice when the item is expanded for
857 // the first time we must clear TVIS_EXPANDEDONCE style manually.
859 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
863 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
865 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
868 bool isExpanded
= IsExpanded(item
);
870 event
.SetEventObject(this);
872 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
873 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
874 GetEventHandler()->ProcessEvent(event
);
876 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
877 GetEventHandler()->ProcessEvent(event
);
879 //else: change didn't took place, so do nothing at all
882 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
884 DoExpand(item
, TVE_EXPAND
);
887 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
889 DoExpand(item
, TVE_COLLAPSE
);
892 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
894 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
897 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
899 DoExpand(item
, TVE_TOGGLE
);
902 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
904 DoExpand(item
, action
);
907 void wxTreeCtrl::Unselect()
909 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), _T("doesn't make sense") );
911 // just remove the selection
912 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
915 void wxTreeCtrl::UnselectAll()
917 if ( m_windowStyle
& wxTR_MULTIPLE
)
919 wxArrayTreeItemIds selections
;
920 size_t count
= GetSelections(selections
);
921 for ( size_t n
= 0; n
< count
; n
++ )
923 SetItemCheck(selections
[n
], FALSE
);
928 // just remove the selection
933 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
935 if ( m_windowStyle
& wxTR_MULTIPLE
)
937 // selecting the item means checking it
942 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
943 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
944 // send them ourselves
946 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
948 event
.SetEventObject(this);
950 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
951 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
953 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
955 wxLogLastError("TreeView_SelectItem");
959 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
960 (void)GetEventHandler()->ProcessEvent(event
);
963 //else: program vetoed the change
967 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
970 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
973 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
975 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
977 wxLogLastError("TreeView_SelectSetFirstVisible");
981 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
986 void wxTreeCtrl::DeleteTextCtrl()
990 m_textCtrl
->UnsubclassWin();
991 m_textCtrl
->SetHWND(0);
997 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
998 wxClassInfo
* textControlClass
)
1000 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1002 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1004 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1013 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1014 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1015 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1020 // End label editing, optionally cancelling the edit
1021 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1023 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1028 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1030 TV_HITTESTINFO hitTestInfo
;
1031 hitTestInfo
.pt
.x
= (int)point
.x
;
1032 hitTestInfo
.pt
.y
= (int)point
.y
;
1034 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1039 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1040 flags |= wxTREE_HITTEST_##flag
1042 TRANSLATE_FLAG(ABOVE
);
1043 TRANSLATE_FLAG(BELOW
);
1044 TRANSLATE_FLAG(NOWHERE
);
1045 TRANSLATE_FLAG(ONITEMBUTTON
);
1046 TRANSLATE_FLAG(ONITEMICON
);
1047 TRANSLATE_FLAG(ONITEMINDENT
);
1048 TRANSLATE_FLAG(ONITEMLABEL
);
1049 TRANSLATE_FLAG(ONITEMRIGHT
);
1050 TRANSLATE_FLAG(ONITEMSTATEICON
);
1051 TRANSLATE_FLAG(TOLEFT
);
1052 TRANSLATE_FLAG(TORIGHT
);
1054 #undef TRANSLATE_FLAG
1056 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1059 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1061 bool textOnly
) const
1064 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1067 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1073 // couldn't retrieve rect: for example, item isn't visible
1078 // ----------------------------------------------------------------------------
1080 // ----------------------------------------------------------------------------
1082 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1083 wxTreeItemData
*pItem2
,
1086 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1087 _T("sorting tree without data doesn't make sense") );
1089 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1092 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1093 const wxTreeItemId
& item2
)
1095 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1098 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1100 // rely on the fact that TreeView_SortChildren does the same thing as our
1101 // default behaviour, i.e. sorts items alphabetically and so call it
1102 // directly if we're not in derived class (much more efficient!)
1103 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1105 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1110 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1111 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1112 tvSort
.lParam
= (LPARAM
)this;
1113 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1117 // ----------------------------------------------------------------------------
1119 // ----------------------------------------------------------------------------
1121 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1123 if ( cmd
== EN_UPDATE
)
1125 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1126 event
.SetEventObject( this );
1127 ProcessCommand(event
);
1129 else if ( cmd
== EN_KILLFOCUS
)
1131 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1132 event
.SetEventObject( this );
1133 ProcessCommand(event
);
1141 // command processed
1145 // process WM_NOTIFY Windows message
1146 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1148 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1149 wxEventType eventType
= wxEVT_NULL
;
1150 NMHDR
*hdr
= (NMHDR
*)lParam
;
1152 switch ( hdr
->code
)
1155 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1158 case TVN_BEGINRDRAG
:
1160 if ( eventType
== wxEVT_NULL
)
1161 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1162 //else: left drag, already set above
1164 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1166 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1167 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1171 case TVN_BEGINLABELEDIT
:
1173 eventType
= wxEVT_COMMAND_TREE_BEGIN_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_DELETEITEM
:
1183 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1184 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1186 event
.m_item
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1190 case TVN_ENDLABELEDIT
:
1192 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1193 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1195 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1196 event
.m_label
= info
->item
.pszText
;
1200 case TVN_GETDISPINFO
:
1201 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1204 case TVN_SETDISPINFO
:
1206 if ( eventType
== wxEVT_NULL
)
1207 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1208 //else: get, already set above
1210 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1212 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1216 case TVN_ITEMEXPANDING
:
1217 event
.m_code
= FALSE
;
1220 case TVN_ITEMEXPANDED
:
1222 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1224 bool expand
= FALSE
;
1225 switch ( tv
->action
)
1236 wxLogDebug(_T("unexpected code %d in TVN_ITEMEXPAND "
1237 "message"), tv
->action
);
1240 bool ing
= (hdr
->code
== TVN_ITEMEXPANDING
);
1241 eventType
= g_events
[expand
][ing
];
1243 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1249 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1250 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1252 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1254 // a separate event for this case
1255 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1257 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1259 event2
.SetEventObject(this);
1261 GetEventHandler()->ProcessEvent(event2
);
1266 case TVN_SELCHANGED
:
1267 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1270 case TVN_SELCHANGING
:
1272 if ( eventType
== wxEVT_NULL
)
1273 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1274 //else: already set above
1276 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1278 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1279 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1284 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1287 event
.SetEventObject(this);
1288 event
.SetEventType(eventType
);
1290 bool processed
= GetEventHandler()->ProcessEvent(event
);
1293 switch ( hdr
->code
)
1295 case TVN_DELETEITEM
:
1297 // NB: we might process this message using wxWindows event
1298 // tables, but due to overhead of wxWin event system we
1299 // prefer to do it here ourself (otherwise deleting a tree
1300 // with many items is just too slow)
1301 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1302 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1303 delete data
; // may be NULL, ok
1305 processed
= TRUE
; // Make sure we don't get called twice
1309 case TVN_BEGINLABELEDIT
:
1310 // return TRUE to cancel label editing
1311 *result
= !event
.IsAllowed();
1314 case TVN_ENDLABELEDIT
:
1315 // return TRUE to set the label to the new string
1316 *result
= event
.IsAllowed();
1318 // ensure that we don't have the text ctrl which is going to be
1323 case TVN_SELCHANGING
:
1324 case TVN_ITEMEXPANDING
:
1325 // return TRUE to prevent the action from happening
1326 *result
= !event
.IsAllowed();
1330 // for the other messages the return value is ignored and there is
1331 // nothing special to do
1337 // ----------------------------------------------------------------------------
1339 // ----------------------------------------------------------------------------
1341 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1343 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1344 : wxNotifyEvent(commandType
, id
)