1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "treectrl.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
30 #include "wx/window.h"
31 #include "wx/msw/private.h"
33 // Mingw32 is a bit mental even though this is done in winundef
42 #if defined(__WIN95__)
45 #include "wx/dynarray.h"
46 #include "wx/imaglist.h"
47 #include "wx/treectrl.h"
48 #include "wx/settings.h"
51 #ifndef wxUSE_NORLANDER_HEADERS
52 #include "wx/msw/gnuwin32/extra.h"
56 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
60 // Bug in headers, sometimes
62 #define TVIS_FOCUSED 0x0001
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // a convenient wrapper around TV_ITEM struct which adds a ctor
70 #pragma warning( disable : 4097 )
71 struct wxTreeViewItem
: public TV_ITEM
73 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
74 UINT mask_
, // fields which are valid
75 UINT stateMask_
= 0) // for TVIF_STATE only
77 // hItem member is always valid
78 mask
= mask_
| TVIF_HANDLE
;
79 stateMask
= stateMask_
;
80 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
83 #pragma warning( default : 4097 )
85 // a class which encapsulates the tree traversal logic: it vists all (unless
86 // OnVisit() returns FALSE) items under the given one
90 wxTreeTraversal(const wxTreeCtrl
*tree
)
95 // do traverse the tree: visit all items (recursively by default) under the
96 // given one; return TRUE if all items were traversed or FALSE if the
97 // traversal was aborted because OnVisit returned FALSE
98 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
100 // override this function to do whatever is needed for each item, return
101 // FALSE to stop traversing
102 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
105 const wxTreeCtrl
*GetTree() const { return m_tree
; }
108 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
110 const wxTreeCtrl
*m_tree
;
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 #if !USE_SHARED_LIBRARY
118 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 // handy table for sending events
126 static const wxEventType g_events
[2][2] =
128 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
129 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
132 // ============================================================================
134 // ============================================================================
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
142 if ( !OnVisit(root
) )
145 return Traverse(root
, recursively
);
148 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
151 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
152 while ( child
.IsOk() )
154 // depth first traversal
155 if ( recursively
&& !Traverse(child
, TRUE
) )
158 if ( !OnVisit(child
) )
161 child
= m_tree
->GetNextChild(root
, cookie
);
167 // ----------------------------------------------------------------------------
168 // construction and destruction
169 // ----------------------------------------------------------------------------
171 void wxTreeCtrl::Init()
173 m_imageListNormal
= NULL
;
174 m_imageListState
= NULL
;
178 bool wxTreeCtrl::Create(wxWindow
*parent
,
183 const wxValidator
& validator
,
184 const wxString
& name
)
188 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
191 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
192 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
194 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
195 wstyle
|= TVS_HASBUTTONS
;
197 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
198 wstyle
|= TVS_EDITLABELS
;
200 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
201 wstyle
|= TVS_LINESATROOT
;
203 #if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !defined(wxUSE_NORLANDER_HEADERS)
204 // we emulate the multiple selection tree controls by using checkboxes: set
205 // up the image list we need for this if we do have multiple selections
206 #if !defined(__VISUALC__) || (__VISUALC__ != 1010)
207 if ( m_windowStyle
& wxTR_MULTIPLE
)
208 wstyle
|= TVS_CHECKBOXES
;
212 // Create the tree control.
213 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
216 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
217 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
220 // VZ: this is some experimental code which may be used to get the
221 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
222 // AFAIK, the standard DLL does about the same thing anyhow.
224 if ( m_windowStyle
& wxTR_MULTIPLE
)
228 // create the DC compatible with the current screen
229 HDC hdcMem
= CreateCompatibleDC(NULL
);
231 // create a mono bitmap of the standard size
232 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
233 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
234 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
235 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
236 1, // # of color planes
237 1, // # bits needed for one pixel
238 0); // array containing colour data
239 SelectObject(hdcMem
, hbmpCheck
);
241 // then draw a check mark into it
242 RECT rect
= { 0, 0, x
, y
};
243 if ( !::DrawFrameControl(hdcMem
, &rect
,
245 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
247 wxLogLastError(_T("DrawFrameControl(check)"));
250 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
251 imagelistCheckboxes
.Add(bmp
);
253 if ( !::DrawFrameControl(hdcMem
, &rect
,
257 wxLogLastError(_T("DrawFrameControl(uncheck)"));
260 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
261 imagelistCheckboxes
.Add(bmp
);
267 SetStateImageList(&imagelistCheckboxes
);
271 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
276 wxTreeCtrl::~wxTreeCtrl()
280 // delete user data to prevent memory leaks
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 // simple wrappers which add error checking in debug mode
290 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
292 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
294 wxLogLastError("TreeView_GetItem");
302 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
304 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
306 wxLogLastError("TreeView_SetItem");
310 size_t wxTreeCtrl::GetCount() const
312 return (size_t)TreeView_GetCount(GetHwnd());
315 unsigned int wxTreeCtrl::GetIndent() const
317 return TreeView_GetIndent(GetHwnd());
320 void wxTreeCtrl::SetIndent(unsigned int indent
)
322 TreeView_SetIndent(GetHwnd(), indent
);
325 wxImageList
*wxTreeCtrl::GetImageList() const
327 return m_imageListNormal
;
330 wxImageList
*wxTreeCtrl::GetStateImageList() const
332 return m_imageListNormal
;
335 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
338 TreeView_SetImageList(GetHwnd(),
339 imageList
? imageList
->GetHIMAGELIST() : 0,
343 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
345 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
348 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
350 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
353 // internal class for counting tree items
355 class TraverseCounter
: public wxTreeTraversal
358 TraverseCounter(const wxTreeCtrl
*tree
,
359 const wxTreeItemId
& root
,
361 : wxTreeTraversal(tree
)
365 DoTraverse(root
, recursively
);
368 virtual bool OnVisit(const wxTreeItemId
& item
)
375 size_t GetCount() const { return m_count
; }
382 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
383 bool recursively
) const
385 TraverseCounter
counter(this, item
, recursively
);
387 return counter
.GetCount();
390 // ----------------------------------------------------------------------------
392 // ----------------------------------------------------------------------------
394 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
396 wxChar buf
[512]; // the size is arbitrary...
398 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
399 tvItem
.pszText
= buf
;
400 tvItem
.cchTextMax
= WXSIZEOF(buf
);
401 if ( !DoGetItem(&tvItem
) )
403 // don't return some garbage which was on stack, but an empty string
407 return wxString(buf
);
410 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
412 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
413 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
417 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
421 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
422 tvItem
.iSelectedImage
= imageSel
;
423 tvItem
.iImage
= image
;
427 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
429 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
432 return tvItem
.iImage
;
435 void wxTreeCtrl::SetItemImage(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
, image
, GetItemSelectedImage(item
));
443 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
445 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
448 return tvItem
.iSelectedImage
;
451 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
453 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
454 // change both normal and selected image - otherwise the change simply
455 // doesn't take place!
456 DoSetItemImages(item
, GetItemImage(item
), image
);
459 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
461 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
462 if ( !DoGetItem(&tvItem
) )
467 return (wxTreeItemData
*)tvItem
.lParam
;
470 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
472 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
473 tvItem
.lParam
= (LPARAM
)data
;
477 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
479 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
480 tvItem
.cChildren
= (int)has
;
484 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
486 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
487 tvItem
.state
= bold
? TVIS_BOLD
: 0;
491 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
493 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
494 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
498 // ----------------------------------------------------------------------------
500 // ----------------------------------------------------------------------------
502 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
504 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
506 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
510 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
512 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
515 return tvItem
.cChildren
!= 0;
518 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
520 // probably not a good idea to put it here
521 //wxASSERT( ItemHasChildren(item) );
523 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
526 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
529 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
531 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
534 return (tvItem
.state
& TVIS_SELECTED
) != 0;
537 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
539 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
542 return (tvItem
.state
& TVIS_BOLD
) != 0;
545 // ----------------------------------------------------------------------------
547 // ----------------------------------------------------------------------------
549 wxTreeItemId
wxTreeCtrl::GetRootItem() const
551 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
554 wxTreeItemId
wxTreeCtrl::GetSelection() const
556 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
557 _T("this only works with single selection controls") );
559 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
562 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
564 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
567 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
570 // remember the last child returned in 'cookie'
571 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
573 return wxTreeItemId((WXHTREEITEM
)_cookie
);
576 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
579 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
580 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
586 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
588 // can this be done more efficiently?
591 wxTreeItemId childLast
,
592 child
= GetFirstChild(item
, cookie
);
593 while ( child
.IsOk() )
596 child
= GetNextChild(item
, cookie
);
602 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
604 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
607 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
609 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
612 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
614 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
617 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
619 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetNextVisible() "
620 "for must be visible itself!"));
622 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
625 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
627 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetPrevVisible() "
628 "for must be visible itself!"));
630 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
633 // ----------------------------------------------------------------------------
634 // multiple selections emulation
635 // ----------------------------------------------------------------------------
637 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
639 // receive the desired information.
640 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
643 // state image indices are 1 based
644 return ((tvItem
.state
>> 12) - 1) == 1;
647 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
649 // receive the desired information.
650 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
652 // state images are one-based
653 tvItem
.state
= (check
? 2 : 1) << 12;
658 // internal class for getting the selected
660 class TraverseSelections
: public wxTreeTraversal
663 TraverseSelections(const wxTreeCtrl
*tree
,
664 wxArrayTreeItemIds
& selections
)
665 : wxTreeTraversal(tree
), m_selections(selections
)
667 m_selections
.Empty();
669 DoTraverse(tree
->GetRootItem());
672 virtual bool OnVisit(const wxTreeItemId
& item
)
674 if ( GetTree()->IsItemChecked(item
) )
676 m_selections
.Add(item
);
683 wxArrayTreeItemIds
& m_selections
;
686 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
688 TraverseSelections
selector(this, selections
);
690 return selections
.GetCount();
693 // ----------------------------------------------------------------------------
695 // ----------------------------------------------------------------------------
697 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
698 wxTreeItemId hInsertAfter
,
699 const wxString
& text
,
700 int image
, int selectedImage
,
701 wxTreeItemData
*data
)
703 TV_INSERTSTRUCT tvIns
;
704 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
705 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
707 // This is how we insert the item as the first child: supply a NULL hInsertAfter
708 if (tvIns
.hInsertAfter
== (HTREEITEM
) 0)
710 tvIns
.hInsertAfter
= TVI_FIRST
;
714 if ( !text
.IsEmpty() )
717 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
723 tvIns
.item
.iImage
= image
;
725 if ( selectedImage
== -1 )
727 // take the same image for selected icon if not specified
728 selectedImage
= image
;
732 if ( selectedImage
!= -1 )
734 mask
|= TVIF_SELECTEDIMAGE
;
735 tvIns
.item
.iSelectedImage
= selectedImage
;
741 tvIns
.item
.lParam
= (LPARAM
)data
;
744 tvIns
.item
.mask
= mask
;
746 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
749 wxLogLastError("TreeView_InsertItem");
754 // associate the application tree item with Win32 tree item handle
755 data
->SetId((WXHTREEITEM
)id
);
758 return wxTreeItemId((WXHTREEITEM
)id
);
761 // for compatibility only
762 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
763 const wxString
& text
,
764 int image
, int selImage
,
767 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
768 image
, selImage
, NULL
);
771 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
772 int image
, int selectedImage
,
773 wxTreeItemData
*data
)
775 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
776 text
, image
, selectedImage
, data
);
779 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
780 const wxString
& text
,
781 int image
, int selectedImage
,
782 wxTreeItemData
*data
)
784 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
785 text
, image
, selectedImage
, data
);
788 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
789 const wxTreeItemId
& idPrevious
,
790 const wxString
& text
,
791 int image
, int selectedImage
,
792 wxTreeItemData
*data
)
794 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
797 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
798 const wxString
& text
,
799 int image
, int selectedImage
,
800 wxTreeItemData
*data
)
802 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
803 text
, image
, selectedImage
, data
);
806 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
808 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
810 wxLogLastError("TreeView_DeleteItem");
814 // delete all children (but don't delete the item itself)
815 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
819 wxArrayLong children
;
820 wxTreeItemId child
= GetFirstChild(item
, cookie
);
821 while ( child
.IsOk() )
823 children
.Add((long)(WXHTREEITEM
)child
);
825 child
= GetNextChild(item
, cookie
);
828 size_t nCount
= children
.Count();
829 for ( size_t n
= 0; n
< nCount
; n
++ )
831 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
833 wxLogLastError("TreeView_DeleteItem");
838 void wxTreeCtrl::DeleteAllItems()
840 if ( !TreeView_DeleteAllItems(GetHwnd()) )
842 wxLogLastError("TreeView_DeleteAllItems");
846 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
848 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
849 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
850 flag
== TVE_EXPAND
||
852 _T("Unknown flag in wxTreeCtrl::DoExpand") );
854 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
855 // emulate them. This behaviour has changed slightly with comctl32.dll
856 // v 4.70 - now it does send them but only the first time. To maintain
857 // compatible behaviour and also in order to not have surprises with the
858 // future versions, don't rely on this and still do everything ourselves.
859 // To avoid that the messages be sent twice when the item is expanded for
860 // the first time we must clear TVIS_EXPANDEDONCE style manually.
862 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
866 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
868 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
871 bool isExpanded
= IsExpanded(item
);
873 event
.SetEventObject(this);
875 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
876 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
877 GetEventHandler()->ProcessEvent(event
);
879 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
880 GetEventHandler()->ProcessEvent(event
);
882 //else: change didn't took place, so do nothing at all
885 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
887 DoExpand(item
, TVE_EXPAND
);
890 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
892 DoExpand(item
, TVE_COLLAPSE
);
895 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
897 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
900 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
902 DoExpand(item
, TVE_TOGGLE
);
905 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
907 DoExpand(item
, action
);
910 void wxTreeCtrl::Unselect()
912 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), _T("doesn't make sense") );
914 // just remove the selection
915 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
918 void wxTreeCtrl::UnselectAll()
920 if ( m_windowStyle
& wxTR_MULTIPLE
)
922 wxArrayTreeItemIds selections
;
923 size_t count
= GetSelections(selections
);
924 for ( size_t n
= 0; n
< count
; n
++ )
926 SetItemCheck(selections
[n
], FALSE
);
931 // just remove the selection
936 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
938 if ( m_windowStyle
& wxTR_MULTIPLE
)
940 // selecting the item means checking it
945 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
946 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
947 // send them ourselves
949 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
951 event
.SetEventObject(this);
953 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
954 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
956 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
958 wxLogLastError("TreeView_SelectItem");
962 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
963 (void)GetEventHandler()->ProcessEvent(event
);
966 //else: program vetoed the change
970 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
973 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
976 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
978 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
980 wxLogLastError("TreeView_SelectSetFirstVisible");
984 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
989 void wxTreeCtrl::DeleteTextCtrl()
993 m_textCtrl
->UnsubclassWin();
994 m_textCtrl
->SetHWND(0);
1000 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1001 wxClassInfo
* textControlClass
)
1003 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1005 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1007 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1016 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1017 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1018 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1023 // End label editing, optionally cancelling the edit
1024 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1026 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1031 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1033 TV_HITTESTINFO hitTestInfo
;
1034 hitTestInfo
.pt
.x
= (int)point
.x
;
1035 hitTestInfo
.pt
.y
= (int)point
.y
;
1037 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1042 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1043 flags |= wxTREE_HITTEST_##flag
1045 TRANSLATE_FLAG(ABOVE
);
1046 TRANSLATE_FLAG(BELOW
);
1047 TRANSLATE_FLAG(NOWHERE
);
1048 TRANSLATE_FLAG(ONITEMBUTTON
);
1049 TRANSLATE_FLAG(ONITEMICON
);
1050 TRANSLATE_FLAG(ONITEMINDENT
);
1051 TRANSLATE_FLAG(ONITEMLABEL
);
1052 TRANSLATE_FLAG(ONITEMRIGHT
);
1053 TRANSLATE_FLAG(ONITEMSTATEICON
);
1054 TRANSLATE_FLAG(TOLEFT
);
1055 TRANSLATE_FLAG(TORIGHT
);
1057 #undef TRANSLATE_FLAG
1059 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1062 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1064 bool textOnly
) const
1067 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1070 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1076 // couldn't retrieve rect: for example, item isn't visible
1081 // ----------------------------------------------------------------------------
1083 // ----------------------------------------------------------------------------
1085 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1086 wxTreeItemData
*pItem2
,
1089 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1090 _T("sorting tree without data doesn't make sense") );
1092 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1095 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1096 const wxTreeItemId
& item2
)
1098 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1101 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1103 // rely on the fact that TreeView_SortChildren does the same thing as our
1104 // default behaviour, i.e. sorts items alphabetically and so call it
1105 // directly if we're not in derived class (much more efficient!)
1106 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1108 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1113 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1114 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1115 tvSort
.lParam
= (LPARAM
)this;
1116 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1120 // ----------------------------------------------------------------------------
1122 // ----------------------------------------------------------------------------
1124 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1126 if ( cmd
== EN_UPDATE
)
1128 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1129 event
.SetEventObject( this );
1130 ProcessCommand(event
);
1132 else if ( cmd
== EN_KILLFOCUS
)
1134 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1135 event
.SetEventObject( this );
1136 ProcessCommand(event
);
1144 // command processed
1148 // process WM_NOTIFY Windows message
1149 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1151 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1152 wxEventType eventType
= wxEVT_NULL
;
1153 NMHDR
*hdr
= (NMHDR
*)lParam
;
1155 switch ( hdr
->code
)
1158 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1161 case TVN_BEGINRDRAG
:
1163 if ( eventType
== wxEVT_NULL
)
1164 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1165 //else: left drag, already set above
1167 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1169 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1170 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1174 case TVN_BEGINLABELEDIT
:
1176 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1177 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1179 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1180 event
.m_label
= info
->item
.pszText
;
1184 case TVN_DELETEITEM
:
1186 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1187 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1189 event
.m_item
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1193 case TVN_ENDLABELEDIT
:
1195 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1196 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1198 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1199 event
.m_label
= info
->item
.pszText
;
1203 case TVN_GETDISPINFO
:
1204 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1207 case TVN_SETDISPINFO
:
1209 if ( eventType
== wxEVT_NULL
)
1210 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1211 //else: get, already set above
1213 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1215 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1219 case TVN_ITEMEXPANDING
:
1220 event
.m_code
= FALSE
;
1223 case TVN_ITEMEXPANDED
:
1225 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1227 bool expand
= FALSE
;
1228 switch ( tv
->action
)
1239 wxLogDebug(_T("unexpected code %d in TVN_ITEMEXPAND "
1240 "message"), tv
->action
);
1243 bool ing
= (hdr
->code
== TVN_ITEMEXPANDING
);
1244 eventType
= g_events
[expand
][ing
];
1246 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1252 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1253 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1255 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1257 // a separate event for this case
1258 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1260 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1262 event2
.SetEventObject(this);
1264 GetEventHandler()->ProcessEvent(event2
);
1269 case TVN_SELCHANGED
:
1270 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1273 case TVN_SELCHANGING
:
1275 if ( eventType
== wxEVT_NULL
)
1276 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1277 //else: already set above
1279 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1281 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1282 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1287 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1290 event
.SetEventObject(this);
1291 event
.SetEventType(eventType
);
1293 bool processed
= GetEventHandler()->ProcessEvent(event
);
1296 switch ( hdr
->code
)
1298 case TVN_DELETEITEM
:
1300 // NB: we might process this message using wxWindows event
1301 // tables, but due to overhead of wxWin event system we
1302 // prefer to do it here ourself (otherwise deleting a tree
1303 // with many items is just too slow)
1304 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1305 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1306 delete data
; // may be NULL, ok
1308 processed
= TRUE
; // Make sure we don't get called twice
1312 case TVN_BEGINLABELEDIT
:
1313 // return TRUE to cancel label editing
1314 *result
= !event
.IsAllowed();
1317 case TVN_ENDLABELEDIT
:
1318 // return TRUE to set the label to the new string
1319 *result
= event
.IsAllowed();
1321 // ensure that we don't have the text ctrl which is going to be
1326 case TVN_SELCHANGING
:
1327 case TVN_ITEMEXPANDING
:
1328 // return TRUE to prevent the action from happening
1329 *result
= !event
.IsAllowed();
1333 // for the other messages the return value is ignored and there is
1334 // nothing special to do
1340 // ----------------------------------------------------------------------------
1342 // ----------------------------------------------------------------------------
1344 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1346 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1347 : wxNotifyEvent(commandType
, id
)