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"
34 #include "wx/settings.h"
37 // Mingw32 is a bit mental even though this is done in winundef
45 #if defined(__WIN95__)
48 #include "wx/dynarray.h"
49 #include "wx/imaglist.h"
50 #include "wx/msw/treectrl.h"
53 #include "wx/msw/gnuwin32/extra.h"
56 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
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 struct wxTreeViewItem
: public TV_ITEM
72 wxTreeViewItem(const wxTreeItemId
& item
,
73 UINT mask_
, UINT stateMask_
= 0)
76 stateMask
= stateMask_
;
77 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 #if !USE_SHARED_LIBRARY
86 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
89 // hide the ugly cast (of course, the macro is _quite_ ugly too...)
90 #define wxhWnd ((HWND)m_hWnd)
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 // handy table for sending events
97 static const wxEventType g_events
[2][2] =
99 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
100 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
103 // ============================================================================
105 // ============================================================================
107 // ----------------------------------------------------------------------------
108 // construction and destruction
109 // ----------------------------------------------------------------------------
111 void wxTreeCtrl::Init()
113 m_imageListNormal
= NULL
;
114 m_imageListState
= NULL
;
118 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
119 const wxPoint
& pos
, const wxSize
& size
,
120 long style
, const wxValidator
& validator
,
121 const wxString
& name
)
125 wxSystemSettings settings
;
128 SetValidator(validator
);
130 m_windowStyle
= style
;
134 m_windowId
= (id
== -1) ? NewControlId() : id
;
136 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_SHOWSELALWAYS
;
140 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
142 // Even with extended styles, need to combine with WS_BORDER
143 // for them to look right.
144 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
149 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
150 wstyle
|= TVS_HASBUTTONS
;
152 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
153 wstyle
|= TVS_EDITLABELS
;
155 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
156 wstyle
|= TVS_LINESATROOT
;
158 // Create the tree control.
159 m_hWnd
= (WXHWND
)::CreateWindowEx
165 pos
.x
, pos
.y
, size
.x
, size
.y
,
166 (HWND
)parent
->GetHWND(),
172 wxCHECK_MSG( m_hWnd
, FALSE
, "Failed to create tree ctrl" );
175 parent
->AddChild(this);
182 wxTreeCtrl::~wxTreeCtrl()
186 // delete user data to prevent memory leaks
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 // simple wrappers which add error checking in debug mode
196 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
198 if ( !TreeView_GetItem(wxhWnd
, tvItem
) )
200 wxLogLastError("TreeView_GetItem");
208 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
210 if ( TreeView_SetItem(wxhWnd
, tvItem
) == -1 )
212 wxLogLastError("TreeView_SetItem");
216 size_t wxTreeCtrl::GetCount() const
218 return (size_t)TreeView_GetCount(wxhWnd
);
221 unsigned int wxTreeCtrl::GetIndent() const
223 return TreeView_GetIndent(wxhWnd
);
226 void wxTreeCtrl::SetIndent(unsigned int indent
)
228 TreeView_SetIndent(wxhWnd
, indent
);
231 wxImageList
*wxTreeCtrl::GetImageList() const
233 return m_imageListNormal
;
236 wxImageList
*wxTreeCtrl::GetStateImageList() const
238 return m_imageListNormal
;
241 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
244 TreeView_SetImageList(wxhWnd
,
245 imageList
? imageList
->GetHIMAGELIST() : 0,
249 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
251 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
254 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
256 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
259 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
265 wxArrayLong children
;
266 wxTreeItemId child
= GetFirstChild(item
, cookie
);
267 while ( child
.IsOk() )
272 result
+= GetChildrenCount(child
, TRUE
);
275 // add the child to the result in any case
278 child
= GetNextChild(item
, cookie
);
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
290 char buf
[512]; // the size is arbitrary...
292 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
293 tvItem
.pszText
= buf
;
294 tvItem
.cchTextMax
= WXSIZEOF(buf
);
295 if ( !DoGetItem(&tvItem
) )
297 // don't return some garbage which was on stack, but an empty string
301 return wxString(buf
);
304 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
306 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
307 tvItem
.pszText
= (char *)text
.c_str(); // conversion is ok
311 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
313 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
316 return tvItem
.iImage
;
319 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
321 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
322 tvItem
.iImage
= image
;
326 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
328 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
331 return tvItem
.iSelectedImage
;
334 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
336 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
337 tvItem
.iSelectedImage
= image
;
341 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
343 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
344 if ( !DoGetItem(&tvItem
) )
349 return (wxTreeItemData
*)tvItem
.lParam
;
352 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
354 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
355 tvItem
.lParam
= (LPARAM
)data
;
359 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
361 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
362 tvItem
.cChildren
= (int)has
;
366 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
368 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
369 tvItem
.state
= bold
? TVIS_BOLD
: 0;
373 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
375 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
376 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
386 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
388 return SendMessage(wxhWnd
, TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
392 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
394 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
397 return tvItem
.cChildren
!= 0;
400 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
402 // probably not a good idea to put it here
403 //wxASSERT( ItemHasChildren(item) );
405 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
408 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
411 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
413 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
416 return (tvItem
.state
& TVIS_SELECTED
) != 0;
419 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
421 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
424 return (tvItem
.state
& TVIS_BOLD
) != 0;
427 // ----------------------------------------------------------------------------
429 // ----------------------------------------------------------------------------
431 wxTreeItemId
wxTreeCtrl::GetRootItem() const
433 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(wxhWnd
));
436 wxTreeItemId
wxTreeCtrl::GetSelection() const
438 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(wxhWnd
));
441 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
443 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
446 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
449 // remember the last child returned in 'cookie'
450 _cookie
= (long)TreeView_GetChild(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
)item
);
452 return wxTreeItemId((WXHTREEITEM
)_cookie
);
455 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
458 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(wxhWnd
,
459 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
465 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
467 // can this be done more efficiently?
470 wxTreeItemId childLast
,
471 child
= GetFirstChild(item
, cookie
);
472 while ( child
.IsOk() )
475 child
= GetNextChild(item
, cookie
);
481 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
483 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
486 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
488 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
491 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
493 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(wxhWnd
));
496 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
498 wxASSERT_MSG( IsVisible(item
), "The item you call GetNextVisible() "
499 "for must be visible itself!");
501 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
504 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
506 wxASSERT_MSG( IsVisible(item
), "The item you call GetPrevVisible() "
507 "for must be visible itself!");
509 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
512 // ----------------------------------------------------------------------------
514 // ----------------------------------------------------------------------------
516 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
517 wxTreeItemId hInsertAfter
,
518 const wxString
& text
,
519 int image
, int selectedImage
,
520 wxTreeItemData
*data
)
522 TV_INSERTSTRUCT tvIns
;
523 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
524 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
526 // This is how we insert the item as the first child: supply a NULL hInsertAfter
527 if (tvIns
.hInsertAfter
== (HTREEITEM
) 0)
529 tvIns
.hInsertAfter
= TVI_FIRST
;
533 if ( !text
.IsEmpty() )
536 tvIns
.item
.pszText
= (char *)text
.c_str(); // cast is ok
542 tvIns
.item
.iImage
= image
;
544 if ( selectedImage
== -1 )
546 // take the same image for selected icon if not specified
547 selectedImage
= image
;
551 if ( selectedImage
!= -1 )
553 mask
|= TVIF_SELECTEDIMAGE
;
554 tvIns
.item
.iSelectedImage
= selectedImage
;
560 tvIns
.item
.lParam
= (LPARAM
)data
;
563 tvIns
.item
.mask
= mask
;
565 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(wxhWnd
, &tvIns
);
568 wxLogLastError("TreeView_InsertItem");
573 // associate the application tree item with Win32 tree item handle
574 data
->SetId((WXHTREEITEM
)id
);
577 return wxTreeItemId((WXHTREEITEM
)id
);
580 // for compatibility only
581 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
582 const wxString
& text
,
583 int image
, int selImage
,
586 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
587 image
, selImage
, NULL
);
590 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
591 int image
, int selectedImage
,
592 wxTreeItemData
*data
)
594 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
595 text
, image
, selectedImage
, data
);
598 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
599 const wxString
& text
,
600 int image
, int selectedImage
,
601 wxTreeItemData
*data
)
603 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
604 text
, image
, selectedImage
, data
);
607 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
608 const wxTreeItemId
& idPrevious
,
609 const wxString
& text
,
610 int image
, int selectedImage
,
611 wxTreeItemData
*data
)
613 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
616 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
617 const wxString
& text
,
618 int image
, int selectedImage
,
619 wxTreeItemData
*data
)
621 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
622 text
, image
, selectedImage
, data
);
625 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
627 if ( !TreeView_DeleteItem(wxhWnd
, (HTREEITEM
)(WXHTREEITEM
)item
) )
629 wxLogLastError("TreeView_DeleteItem");
633 // delete all children (but don't delete the item itself)
634 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
638 wxArrayLong children
;
639 wxTreeItemId child
= GetFirstChild(item
, cookie
);
640 while ( child
.IsOk() )
642 children
.Add((long)(WXHTREEITEM
)child
);
644 child
= GetNextChild(item
, cookie
);
647 size_t nCount
= children
.Count();
648 for ( size_t n
= 0; n
< nCount
; n
++ )
650 if ( !TreeView_DeleteItem(wxhWnd
, (HTREEITEM
)children
[n
]) )
652 wxLogLastError("TreeView_DeleteItem");
657 void wxTreeCtrl::DeleteAllItems()
659 if ( !TreeView_DeleteAllItems(wxhWnd
) )
661 wxLogLastError("TreeView_DeleteAllItems");
665 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
667 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
668 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
669 flag
== TVE_EXPAND
||
671 "Unknown flag in wxTreeCtrl::DoExpand" );
673 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
675 if ( TreeView_Expand(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
677 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
680 bool isExpanded
= IsExpanded(item
);
682 event
.SetEventObject(this);
684 // @@@ return values of {EXPAND|COLLAPS}ING event handler is discarded
685 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
686 GetEventHandler()->ProcessEvent(event
);
688 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
689 GetEventHandler()->ProcessEvent(event
);
693 // I wonder if it really ever happens...
694 wxLogDebug("TreeView_Expand: change didn't took place.");
698 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
700 DoExpand(item
, TVE_EXPAND
);
703 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
705 DoExpand(item
, TVE_COLLAPSE
);
708 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
710 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
713 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
715 DoExpand(item
, TVE_TOGGLE
);
718 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
720 DoExpand(item
, action
);
723 void wxTreeCtrl::Unselect()
725 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
728 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
730 if ( !TreeView_SelectItem(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
) )
732 wxLogLastError("TreeView_SelectItem");
736 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
739 TreeView_EnsureVisible(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
);
742 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
744 if ( !TreeView_SelectSetFirstVisible(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
) )
746 wxLogLastError("TreeView_SelectSetFirstVisible");
750 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
755 void wxTreeCtrl::DeleteTextCtrl()
759 m_textCtrl
->UnsubclassWin();
760 m_textCtrl
->SetHWND(0);
766 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
767 wxClassInfo
* textControlClass
)
769 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
771 HWND hWnd
= (HWND
) TreeView_EditLabel(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
);
773 wxCHECK_MSG( hWnd
, NULL
, "Can't edit tree ctrl label" );
777 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
778 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
779 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
784 // End label editing, optionally cancelling the edit
785 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
787 TreeView_EndEditLabelNow(wxhWnd
, discardChanges
);
792 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
794 TV_HITTESTINFO hitTestInfo
;
795 hitTestInfo
.pt
.x
= (int)point
.x
;
796 hitTestInfo
.pt
.y
= (int)point
.y
;
798 TreeView_HitTest(wxhWnd
, &hitTestInfo
);
803 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
804 flags |= wxTREE_HITTEST_##flag
806 TRANSLATE_FLAG(ABOVE
);
807 TRANSLATE_FLAG(BELOW
);
808 TRANSLATE_FLAG(NOWHERE
);
809 TRANSLATE_FLAG(ONITEMBUTTON
);
810 TRANSLATE_FLAG(ONITEMICON
);
811 TRANSLATE_FLAG(ONITEMINDENT
);
812 TRANSLATE_FLAG(ONITEMLABEL
);
813 TRANSLATE_FLAG(ONITEMRIGHT
);
814 TRANSLATE_FLAG(ONITEMSTATEICON
);
815 TRANSLATE_FLAG(TOLEFT
);
816 TRANSLATE_FLAG(TORIGHT
);
818 #undef TRANSLATE_FLAG
820 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
823 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
828 if ( TreeView_GetItemRect(wxhWnd
, (HTREEITEM
)(WXHTREEITEM
)item
,
831 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
837 // couldn't retrieve rect: for example, item isn't visible
842 // ----------------------------------------------------------------------------
844 // ----------------------------------------------------------------------------
846 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
847 wxTreeItemData
*pItem2
,
850 wxCHECK_MSG( pItem1
&& pItem2
, 0,
851 _T("sorting tree without data doesn't make sense") );
853 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
856 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
857 const wxTreeItemId
& item2
)
859 return strcmp(GetItemText(item1
), GetItemText(item2
));
862 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
864 // rely on the fact that TreeView_SortChildren does the same thing as our
865 // default behaviour, i.e. sorts items alphabetically and so call it
866 // directly if we're not in derived class (much more efficient!)
867 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
869 TreeView_SortChildren(wxhWnd
, (HTREEITEM
)(WXHTREEITEM
)item
, 0);
874 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
875 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
876 tvSort
.lParam
= (LPARAM
)this;
877 TreeView_SortChildrenCB(wxhWnd
, &tvSort
, 0 /* reserved */);
881 // ----------------------------------------------------------------------------
883 // ----------------------------------------------------------------------------
885 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
887 if ( cmd
== EN_UPDATE
)
889 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
890 event
.SetEventObject( this );
891 ProcessCommand(event
);
893 else if ( cmd
== EN_KILLFOCUS
)
895 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
896 event
.SetEventObject( this );
897 ProcessCommand(event
);
909 // process WM_NOTIFY Windows message
910 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
912 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
913 wxEventType eventType
= wxEVT_NULL
;
914 NMHDR
*hdr
= (NMHDR
*)lParam
;
919 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
924 if ( eventType
== wxEVT_NULL
)
925 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
926 //else: left drag, already set above
928 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
930 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
931 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
935 case TVN_BEGINLABELEDIT
:
937 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
938 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
940 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
946 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
947 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
949 event
.m_item
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
953 case TVN_ENDLABELEDIT
:
955 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
956 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
958 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
962 case TVN_GETDISPINFO
:
963 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
966 case TVN_SETDISPINFO
:
968 if ( eventType
== wxEVT_NULL
)
969 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
970 //else: get, already set above
972 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
974 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
978 case TVN_ITEMEXPANDING
:
979 event
.m_code
= FALSE
;
982 case TVN_ITEMEXPANDED
:
984 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
987 switch ( tv
->action
)
998 wxLogDebug("unexpected code %d in TVN_ITEMEXPAND "
999 "message", tv
->action
);
1002 bool ing
= (hdr
->code
== TVN_ITEMEXPANDING
);
1003 eventType
= g_events
[expand
][ing
];
1005 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1011 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1012 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1014 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1016 // a separate event for this case
1017 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1019 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1021 event2
.SetEventObject(this);
1023 GetEventHandler()->ProcessEvent(event2
);
1028 case TVN_SELCHANGED
:
1029 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1032 case TVN_SELCHANGING
:
1034 if ( eventType
== wxEVT_NULL
)
1035 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1036 //else: already set above
1038 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1040 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1041 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1046 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1049 event
.SetEventObject(this);
1050 event
.SetEventType(eventType
);
1052 bool processed
= GetEventHandler()->ProcessEvent(event
);
1055 if ( hdr
->code
== TVN_DELETEITEM
)
1057 // NB: we might process this message using wxWindows event tables, but
1058 // due to overhead of wxWin event system we prefer to do it here
1059 // (otherwise deleting a tree with many items is just too slow)
1060 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1061 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1062 delete data
; // may be NULL, ok
1063 processed
= TRUE
; // Make sure we don't get called twice
1066 *result
= !event
.IsAllowed();
1071 // ----------------------------------------------------------------------------
1073 // ----------------------------------------------------------------------------
1075 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1077 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1078 : wxNotifyEvent(commandType
, id
)