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
;
199 // we emulate the multiple selection tree controls by using checkboxes: set
200 // up the image list we need for this if we do have multiple selections
201 if ( m_windowStyle
& wxTR_MULTIPLE
)
202 wstyle
|= TVS_CHECKBOXES
;
205 // Create the tree control.
206 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
209 // VZ: this is some experimental code which may be used to get the
210 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
211 // AFAIK, the standard DLL does about the same thing anyhow.
213 if ( m_windowStyle
& wxTR_MULTIPLE
)
217 // create the DC compatible with the current screen
218 HDC hdcMem
= CreateCompatibleDC(NULL
);
220 // create a mono bitmap of the standard size
221 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
222 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
223 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
224 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
225 1, // # of color planes
226 1, // # bits needed for one pixel
227 0); // array containing colour data
228 SelectObject(hdcMem
, hbmpCheck
);
230 // then draw a check mark into it
231 RECT rect
= { 0, 0, x
, y
};
232 if ( !::DrawFrameControl(hdcMem
, &rect
,
234 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
236 wxLogLastError(_T("DrawFrameControl(check)"));
239 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
240 imagelistCheckboxes
.Add(bmp
);
242 if ( !::DrawFrameControl(hdcMem
, &rect
,
246 wxLogLastError(_T("DrawFrameControl(uncheck)"));
249 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
250 imagelistCheckboxes
.Add(bmp
);
256 SetStateImageList(&imagelistCheckboxes
);
260 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
265 wxTreeCtrl::~wxTreeCtrl()
269 // delete user data to prevent memory leaks
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 // simple wrappers which add error checking in debug mode
279 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
281 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
283 wxLogLastError("TreeView_GetItem");
291 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
293 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
295 wxLogLastError("TreeView_SetItem");
299 size_t wxTreeCtrl::GetCount() const
301 return (size_t)TreeView_GetCount(GetHwnd());
304 unsigned int wxTreeCtrl::GetIndent() const
306 return TreeView_GetIndent(GetHwnd());
309 void wxTreeCtrl::SetIndent(unsigned int indent
)
311 TreeView_SetIndent(GetHwnd(), indent
);
314 wxImageList
*wxTreeCtrl::GetImageList() const
316 return m_imageListNormal
;
319 wxImageList
*wxTreeCtrl::GetStateImageList() const
321 return m_imageListNormal
;
324 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
327 TreeView_SetImageList(GetHwnd(),
328 imageList
? imageList
->GetHIMAGELIST() : 0,
332 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
334 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
337 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
339 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
342 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
343 bool recursively
) const
345 class TraverseCounter
: public wxTreeTraversal
348 TraverseCounter(const wxTreeCtrl
*tree
,
349 const wxTreeItemId
& root
,
351 : wxTreeTraversal(tree
)
355 DoTraverse(root
, recursively
);
358 virtual bool OnVisit(const wxTreeItemId
& item
)
365 size_t GetCount() const { return m_count
; }
369 } counter(this, item
, recursively
);
371 return counter
.GetCount();
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
380 wxChar buf
[512]; // the size is arbitrary...
382 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
383 tvItem
.pszText
= buf
;
384 tvItem
.cchTextMax
= WXSIZEOF(buf
);
385 if ( !DoGetItem(&tvItem
) )
387 // don't return some garbage which was on stack, but an empty string
391 return wxString(buf
);
394 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
396 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
397 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
401 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
405 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
406 tvItem
.iSelectedImage
= imageSel
;
407 tvItem
.iImage
= image
;
411 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
413 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
416 return tvItem
.iImage
;
419 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
421 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
422 // change both normal and selected image - otherwise the change simply
423 // doesn't take place!
424 DoSetItemImages(item
, image
, GetItemSelectedImage(item
));
427 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
429 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
432 return tvItem
.iSelectedImage
;
435 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
437 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
438 // change both normal and selected image - otherwise the change simply
439 // doesn't take place!
440 DoSetItemImages(item
, GetItemImage(item
), image
);
443 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
445 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
446 if ( !DoGetItem(&tvItem
) )
451 return (wxTreeItemData
*)tvItem
.lParam
;
454 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
456 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
457 tvItem
.lParam
= (LPARAM
)data
;
461 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
463 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
464 tvItem
.cChildren
= (int)has
;
468 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
470 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
471 tvItem
.state
= bold
? TVIS_BOLD
: 0;
475 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
477 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
478 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
482 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
488 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
490 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
494 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
496 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
499 return tvItem
.cChildren
!= 0;
502 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
504 // probably not a good idea to put it here
505 //wxASSERT( ItemHasChildren(item) );
507 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
510 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
513 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
515 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
518 return (tvItem
.state
& TVIS_SELECTED
) != 0;
521 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
523 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
526 return (tvItem
.state
& TVIS_BOLD
) != 0;
529 // ----------------------------------------------------------------------------
531 // ----------------------------------------------------------------------------
533 wxTreeItemId
wxTreeCtrl::GetRootItem() const
535 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
538 wxTreeItemId
wxTreeCtrl::GetSelection() const
540 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
541 _T("this only works with single selection controls") );
543 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
546 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
548 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
551 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
554 // remember the last child returned in 'cookie'
555 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
557 return wxTreeItemId((WXHTREEITEM
)_cookie
);
560 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
563 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
564 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
570 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
572 // can this be done more efficiently?
575 wxTreeItemId childLast
,
576 child
= GetFirstChild(item
, cookie
);
577 while ( child
.IsOk() )
580 child
= GetNextChild(item
, cookie
);
586 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
588 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
591 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
593 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
596 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
598 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
601 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
603 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetNextVisible() "
604 "for must be visible itself!"));
606 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
609 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
611 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetPrevVisible() "
612 "for must be visible itself!"));
614 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
617 // ----------------------------------------------------------------------------
618 // multiple selections emulation
619 // ----------------------------------------------------------------------------
621 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
623 // receive the desired information.
624 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
627 // state image indices are 1 based
628 return ((tvItem
.state
>> 12) - 1) == 1;
631 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
633 // receive the desired information.
634 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
636 // state images are one-based
637 tvItem
.state
= (check
? 2 : 1) << 12;
642 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
644 class TraverseSelections
: public wxTreeTraversal
647 TraverseSelections(const wxTreeCtrl
*tree
,
648 wxArrayTreeItemIds
& selections
)
649 : wxTreeTraversal(tree
), m_selections(selections
)
651 m_selections
.Empty();
653 DoTraverse(tree
->GetRootItem());
656 virtual bool OnVisit(const wxTreeItemId
& item
)
658 if ( GetTree()->IsItemChecked(item
) )
660 m_selections
.Add(item
);
667 wxArrayTreeItemIds
& m_selections
;
668 } selector(this, selections
);
670 return selections
.GetCount();
673 // ----------------------------------------------------------------------------
675 // ----------------------------------------------------------------------------
677 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
678 wxTreeItemId hInsertAfter
,
679 const wxString
& text
,
680 int image
, int selectedImage
,
681 wxTreeItemData
*data
)
683 TV_INSERTSTRUCT tvIns
;
684 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
685 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
687 // This is how we insert the item as the first child: supply a NULL hInsertAfter
688 if (tvIns
.hInsertAfter
== (HTREEITEM
) 0)
690 tvIns
.hInsertAfter
= TVI_FIRST
;
694 if ( !text
.IsEmpty() )
697 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
703 tvIns
.item
.iImage
= image
;
705 if ( selectedImage
== -1 )
707 // take the same image for selected icon if not specified
708 selectedImage
= image
;
712 if ( selectedImage
!= -1 )
714 mask
|= TVIF_SELECTEDIMAGE
;
715 tvIns
.item
.iSelectedImage
= selectedImage
;
721 tvIns
.item
.lParam
= (LPARAM
)data
;
724 tvIns
.item
.mask
= mask
;
726 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
729 wxLogLastError("TreeView_InsertItem");
734 // associate the application tree item with Win32 tree item handle
735 data
->SetId((WXHTREEITEM
)id
);
738 return wxTreeItemId((WXHTREEITEM
)id
);
741 // for compatibility only
742 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
743 const wxString
& text
,
744 int image
, int selImage
,
747 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
748 image
, selImage
, NULL
);
751 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
752 int image
, int selectedImage
,
753 wxTreeItemData
*data
)
755 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
756 text
, image
, selectedImage
, data
);
759 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
760 const wxString
& text
,
761 int image
, int selectedImage
,
762 wxTreeItemData
*data
)
764 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
765 text
, image
, selectedImage
, data
);
768 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
769 const wxTreeItemId
& idPrevious
,
770 const wxString
& text
,
771 int image
, int selectedImage
,
772 wxTreeItemData
*data
)
774 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
777 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
778 const wxString
& text
,
779 int image
, int selectedImage
,
780 wxTreeItemData
*data
)
782 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
783 text
, image
, selectedImage
, data
);
786 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
788 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
790 wxLogLastError("TreeView_DeleteItem");
794 // delete all children (but don't delete the item itself)
795 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
799 wxArrayLong children
;
800 wxTreeItemId child
= GetFirstChild(item
, cookie
);
801 while ( child
.IsOk() )
803 children
.Add((long)(WXHTREEITEM
)child
);
805 child
= GetNextChild(item
, cookie
);
808 size_t nCount
= children
.Count();
809 for ( size_t n
= 0; n
< nCount
; n
++ )
811 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
813 wxLogLastError("TreeView_DeleteItem");
818 void wxTreeCtrl::DeleteAllItems()
820 if ( !TreeView_DeleteAllItems(GetHwnd()) )
822 wxLogLastError("TreeView_DeleteAllItems");
826 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
828 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
829 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
830 flag
== TVE_EXPAND
||
832 _T("Unknown flag in wxTreeCtrl::DoExpand") );
834 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
835 // emulate them. This behaviour has changed slightly with comctl32.dll
836 // v 4.70 - now it does send them but only the first time. To maintain
837 // compatible behaviour and also in order to not have surprises with the
838 // future versions, don't rely on this and still do everything ourselves.
839 // To avoid that the messages be sent twice when the item is expanded for
840 // the first time we must clear TVIS_EXPANDEDONCE style manually.
842 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
846 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
848 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
851 bool isExpanded
= IsExpanded(item
);
853 event
.SetEventObject(this);
855 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
856 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
857 GetEventHandler()->ProcessEvent(event
);
859 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
860 GetEventHandler()->ProcessEvent(event
);
862 //else: change didn't took place, so do nothing at all
865 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
867 DoExpand(item
, TVE_EXPAND
);
870 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
872 DoExpand(item
, TVE_COLLAPSE
);
875 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
877 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
880 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
882 DoExpand(item
, TVE_TOGGLE
);
885 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
887 DoExpand(item
, action
);
890 void wxTreeCtrl::Unselect()
892 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), _T("doesn't make sense") );
894 // just remove the selection
895 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
898 void wxTreeCtrl::UnselectAll()
900 if ( m_windowStyle
& wxTR_MULTIPLE
)
902 wxArrayTreeItemIds selections
;
903 size_t count
= GetSelections(selections
);
904 for ( size_t n
= 0; n
< count
; n
++ )
906 SetItemCheck(selections
[n
], FALSE
);
911 // just remove the selection
916 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
918 if ( m_windowStyle
& wxTR_MULTIPLE
)
920 // selecting the item means checking it
925 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
926 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
927 // send them ourselves
929 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
931 event
.SetEventObject(this);
933 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
934 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
936 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
938 wxLogLastError("TreeView_SelectItem");
942 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
943 (void)GetEventHandler()->ProcessEvent(event
);
946 //else: program vetoed the change
950 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
953 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
956 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
958 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
960 wxLogLastError("TreeView_SelectSetFirstVisible");
964 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
969 void wxTreeCtrl::DeleteTextCtrl()
973 m_textCtrl
->UnsubclassWin();
974 m_textCtrl
->SetHWND(0);
980 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
981 wxClassInfo
* textControlClass
)
983 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
985 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
987 // this is not an error - the TVN_BEGINLABELEDIT handler might have
996 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
997 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
998 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1003 // End label editing, optionally cancelling the edit
1004 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1006 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1011 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1013 TV_HITTESTINFO hitTestInfo
;
1014 hitTestInfo
.pt
.x
= (int)point
.x
;
1015 hitTestInfo
.pt
.y
= (int)point
.y
;
1017 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1022 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1023 flags |= wxTREE_HITTEST_##flag
1025 TRANSLATE_FLAG(ABOVE
);
1026 TRANSLATE_FLAG(BELOW
);
1027 TRANSLATE_FLAG(NOWHERE
);
1028 TRANSLATE_FLAG(ONITEMBUTTON
);
1029 TRANSLATE_FLAG(ONITEMICON
);
1030 TRANSLATE_FLAG(ONITEMINDENT
);
1031 TRANSLATE_FLAG(ONITEMLABEL
);
1032 TRANSLATE_FLAG(ONITEMRIGHT
);
1033 TRANSLATE_FLAG(ONITEMSTATEICON
);
1034 TRANSLATE_FLAG(TOLEFT
);
1035 TRANSLATE_FLAG(TORIGHT
);
1037 #undef TRANSLATE_FLAG
1039 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1042 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1044 bool textOnly
) const
1047 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1050 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1056 // couldn't retrieve rect: for example, item isn't visible
1061 // ----------------------------------------------------------------------------
1063 // ----------------------------------------------------------------------------
1065 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1066 wxTreeItemData
*pItem2
,
1069 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1070 _T("sorting tree without data doesn't make sense") );
1072 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1075 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1076 const wxTreeItemId
& item2
)
1078 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1081 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1083 // rely on the fact that TreeView_SortChildren does the same thing as our
1084 // default behaviour, i.e. sorts items alphabetically and so call it
1085 // directly if we're not in derived class (much more efficient!)
1086 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1088 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1093 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1094 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1095 tvSort
.lParam
= (LPARAM
)this;
1096 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1100 // ----------------------------------------------------------------------------
1102 // ----------------------------------------------------------------------------
1104 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1106 if ( cmd
== EN_UPDATE
)
1108 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1109 event
.SetEventObject( this );
1110 ProcessCommand(event
);
1112 else if ( cmd
== EN_KILLFOCUS
)
1114 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1115 event
.SetEventObject( this );
1116 ProcessCommand(event
);
1124 // command processed
1128 // process WM_NOTIFY Windows message
1129 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1131 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1132 wxEventType eventType
= wxEVT_NULL
;
1133 NMHDR
*hdr
= (NMHDR
*)lParam
;
1135 switch ( hdr
->code
)
1138 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1141 case TVN_BEGINRDRAG
:
1143 if ( eventType
== wxEVT_NULL
)
1144 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1145 //else: left drag, already set above
1147 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1149 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1150 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1154 case TVN_BEGINLABELEDIT
:
1156 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1157 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1159 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1160 event
.m_label
= info
->item
.pszText
;
1164 case TVN_DELETEITEM
:
1166 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1167 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1169 event
.m_item
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1173 case TVN_ENDLABELEDIT
:
1175 eventType
= wxEVT_COMMAND_TREE_END_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_GETDISPINFO
:
1184 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1187 case TVN_SETDISPINFO
:
1189 if ( eventType
== wxEVT_NULL
)
1190 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1191 //else: get, already set above
1193 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1195 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1199 case TVN_ITEMEXPANDING
:
1200 event
.m_code
= FALSE
;
1203 case TVN_ITEMEXPANDED
:
1205 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1207 bool expand
= FALSE
;
1208 switch ( tv
->action
)
1219 wxLogDebug(_T("unexpected code %d in TVN_ITEMEXPAND "
1220 "message"), tv
->action
);
1223 bool ing
= (hdr
->code
== TVN_ITEMEXPANDING
);
1224 eventType
= g_events
[expand
][ing
];
1226 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1232 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1233 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1235 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1237 // a separate event for this case
1238 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1240 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1242 event2
.SetEventObject(this);
1244 GetEventHandler()->ProcessEvent(event2
);
1249 case TVN_SELCHANGED
:
1250 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1253 case TVN_SELCHANGING
:
1255 if ( eventType
== wxEVT_NULL
)
1256 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1257 //else: already set above
1259 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1261 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1262 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1267 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1270 event
.SetEventObject(this);
1271 event
.SetEventType(eventType
);
1273 bool processed
= GetEventHandler()->ProcessEvent(event
);
1276 switch ( hdr
->code
)
1278 case TVN_DELETEITEM
:
1280 // NB: we might process this message using wxWindows event
1281 // tables, but due to overhead of wxWin event system we
1282 // prefer to do it here ourself (otherwise deleting a tree
1283 // with many items is just too slow)
1284 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1285 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1286 delete data
; // may be NULL, ok
1288 processed
= TRUE
; // Make sure we don't get called twice
1292 case TVN_BEGINLABELEDIT
:
1293 // return TRUE to cancel label editing
1294 *result
= !event
.IsAllowed();
1297 case TVN_ENDLABELEDIT
:
1298 // return TRUE to set the label to the new string
1299 *result
= event
.IsAllowed();
1301 // ensure that we don't have the text ctrl which is going to be
1306 case TVN_SELCHANGING
:
1307 case TVN_ITEMEXPANDING
:
1308 // return TRUE to prevent the action from happening
1309 *result
= !event
.IsAllowed();
1313 // for the other messages the return value is ignored and there is
1314 // nothing special to do
1320 // ----------------------------------------------------------------------------
1322 // ----------------------------------------------------------------------------
1324 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1326 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1327 : wxNotifyEvent(commandType
, id
)