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"
34 #if defined(__WIN95__)
37 #include "wx/dynarray.h"
38 #include "wx/imaglist.h"
39 #include "wx/msw/treectrl.h"
41 #include "wx/msw/private.h"
43 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
63 // Bug in headers, sometimes
65 #define TVIS_FOCUSED 0x0001
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // a convenient wrapper around TV_ITEM struct which adds a ctor
73 struct wxTreeViewItem
: public TV_ITEM
75 wxTreeViewItem(const wxTreeItemId
& item
,
76 UINT mask_
, UINT stateMask_
= 0)
79 stateMask
= stateMask_
;
80 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 #if !USE_SHARED_LIBRARY
89 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
92 // hide the ugly cast (of course, the macro is _quite_ ugly too...)
93 #define wxhWnd ((HWND)m_hWnd)
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 // handy table for sending events
100 static const wxEventType g_events
[2][2] =
102 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
103 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
106 // ============================================================================
108 // ============================================================================
110 // ----------------------------------------------------------------------------
111 // construction and destruction
112 // ----------------------------------------------------------------------------
114 void wxTreeCtrl::Init()
116 m_imageListNormal
= NULL
;
117 m_imageListState
= NULL
;
121 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
122 const wxPoint
& pos
, const wxSize
& size
,
123 long style
, const wxValidator
& validator
,
124 const wxString
& name
)
128 wxSystemSettings settings
;
131 SetValidator(validator
);
133 m_windowStyle
= style
;
137 m_windowId
= (id
== -1) ? NewControlId() : id
;
139 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
;
142 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
144 // Even with extended styles, need to combine with WS_BORDER
145 // for them to look right.
146 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
151 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
152 wstyle
|= TVS_HASBUTTONS
;
154 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
155 wstyle
|= TVS_EDITLABELS
;
157 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
158 wstyle
|= TVS_LINESATROOT
;
160 // Create the tree control.
161 m_hWnd
= (WXHWND
)::CreateWindowEx
167 pos
.x
, pos
.y
, size
.x
, size
.y
,
168 (HWND
)parent
->GetHWND(),
174 wxCHECK_MSG( m_hWnd
, FALSE
, "Failed to create tree ctrl" );
177 parent
->AddChild(this);
184 wxTreeCtrl::~wxTreeCtrl()
188 // delete user data to prevent memory leaks
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 // simple wrappers which add error checking in debug mode
198 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
200 if ( !TreeView_GetItem(wxhWnd
, tvItem
) )
202 wxLogLastError("TreeView_GetItem");
210 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
212 if ( TreeView_SetItem(wxhWnd
, tvItem
) == -1 )
214 wxLogLastError("TreeView_SetItem");
218 size_t wxTreeCtrl::GetCount() const
220 return (size_t)TreeView_GetCount(wxhWnd
);
223 unsigned int wxTreeCtrl::GetIndent() const
225 return TreeView_GetIndent(wxhWnd
);
228 void wxTreeCtrl::SetIndent(unsigned int indent
)
230 TreeView_SetIndent(wxhWnd
, indent
);
233 wxImageList
*wxTreeCtrl::GetImageList() const
235 return m_imageListNormal
;
238 wxImageList
*wxTreeCtrl::GetStateImageList() const
240 return m_imageListNormal
;
243 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
246 TreeView_SetImageList(wxhWnd
,
247 imageList
? imageList
->GetHIMAGELIST() : 0,
251 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
253 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
256 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
258 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
261 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
267 wxArrayLong children
;
268 wxTreeItemId child
= GetFirstChild(item
, cookie
);
269 while ( child
.IsOk() )
274 result
+= GetChildrenCount(child
, TRUE
);
277 // add the child to the result in any case
280 child
= GetNextChild(item
, cookie
);
286 // ----------------------------------------------------------------------------
288 // ----------------------------------------------------------------------------
290 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
292 char buf
[512]; // the size is arbitrary...
294 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
295 tvItem
.pszText
= buf
;
296 tvItem
.cchTextMax
= WXSIZEOF(buf
);
297 if ( !DoGetItem(&tvItem
) )
299 // don't return some garbage which was on stack, but an empty string
303 return wxString(buf
);
306 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
308 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
309 tvItem
.pszText
= (char *)text
.c_str(); // conversion is ok
313 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
315 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
318 return tvItem
.iImage
;
321 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
323 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
324 tvItem
.iImage
= image
;
328 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
330 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
333 return tvItem
.iSelectedImage
;
336 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
338 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
339 tvItem
.iSelectedImage
= image
;
343 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
345 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
346 if ( !DoGetItem(&tvItem
) )
351 return (wxTreeItemData
*)tvItem
.lParam
;
354 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
356 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
357 tvItem
.lParam
= (LPARAM
)data
;
361 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
363 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
364 tvItem
.cChildren
= (int)has
;
368 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
370 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
371 tvItem
.state
= bold
? TVIS_BOLD
: 0;
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
381 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
383 return SendMessage(wxhWnd
, TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
387 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
389 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
392 return tvItem
.cChildren
!= 0;
395 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
397 // probably not a good idea to put it here
398 //wxASSERT( ItemHasChildren(item) );
400 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
403 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
406 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
408 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
411 return (tvItem
.state
& TVIS_SELECTED
) != 0;
414 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
416 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
419 return (tvItem
.state
& TVIS_BOLD
) != 0;
422 // ----------------------------------------------------------------------------
424 // ----------------------------------------------------------------------------
426 wxTreeItemId
wxTreeCtrl::GetRootItem() const
428 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(wxhWnd
));
431 wxTreeItemId
wxTreeCtrl::GetSelection() const
433 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(wxhWnd
));
436 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
438 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
441 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
444 // remember the last child returned in 'cookie'
445 _cookie
= (long)TreeView_GetChild(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
)item
);
447 return wxTreeItemId((WXHTREEITEM
)_cookie
);
450 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
453 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(wxhWnd
,
454 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
460 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
462 // can this be done more efficiently?
465 wxTreeItemId childLast
,
466 child
= GetFirstChild(item
, cookie
);
467 while ( child
.IsOk() )
470 child
= GetNextChild(item
, cookie
);
476 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
478 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
481 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
483 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
486 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
488 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(wxhWnd
));
491 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
493 wxASSERT_MSG( IsVisible(item
), "The item you call GetNextVisible() "
494 "for must be visible itself!");
496 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
499 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
501 wxASSERT_MSG( IsVisible(item
), "The item you call GetPrevVisible() "
502 "for must be visible itself!");
504 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
507 // ----------------------------------------------------------------------------
509 // ----------------------------------------------------------------------------
511 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
512 wxTreeItemId hInsertAfter
,
513 const wxString
& text
,
514 int image
, int selectedImage
,
515 wxTreeItemData
*data
)
517 TV_INSERTSTRUCT tvIns
;
518 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
519 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
521 if ( !text
.IsEmpty() )
524 tvIns
.item
.pszText
= (char *)text
.c_str(); // cast is ok
530 tvIns
.item
.iImage
= image
;
532 if ( selectedImage
== -1 )
534 // take the same image for selected icon if not specified
535 selectedImage
= image
;
539 if ( selectedImage
!= -1 )
541 mask
|= TVIF_SELECTEDIMAGE
;
542 tvIns
.item
.iSelectedImage
= selectedImage
;
548 tvIns
.item
.lParam
= (LPARAM
)data
;
551 tvIns
.item
.mask
= mask
;
553 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(wxhWnd
, &tvIns
);
556 wxLogLastError("TreeView_InsertItem");
561 // associate the application tree item with Win32 tree item handle
562 data
->SetId((WXHTREEITEM
)id
);
565 return wxTreeItemId((WXHTREEITEM
)id
);
568 // for compatibility only
569 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
570 const wxString
& text
,
571 int image
, int selImage
,
574 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
575 image
, selImage
, NULL
);
578 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
579 int image
, int selectedImage
,
580 wxTreeItemData
*data
)
582 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
583 text
, image
, selectedImage
, data
);
586 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
587 const wxString
& text
,
588 int image
, int selectedImage
,
589 wxTreeItemData
*data
)
591 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
592 text
, image
, selectedImage
, data
);
595 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
596 const wxTreeItemId
& idPrevious
,
597 const wxString
& text
,
598 int image
, int selectedImage
,
599 wxTreeItemData
*data
)
601 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
604 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
605 const wxString
& text
,
606 int image
, int selectedImage
,
607 wxTreeItemData
*data
)
609 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
610 text
, image
, selectedImage
, data
);
613 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
615 if ( !TreeView_DeleteItem(wxhWnd
, (HTREEITEM
)(WXHTREEITEM
)item
) )
617 wxLogLastError("TreeView_DeleteItem");
621 // delete all children (but don't delete the item itself)
622 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
626 wxArrayLong children
;
627 wxTreeItemId child
= GetFirstChild(item
, cookie
);
628 while ( child
.IsOk() )
630 children
.Add((long)(WXHTREEITEM
)child
);
632 child
= GetNextChild(item
, cookie
);
635 size_t nCount
= children
.Count();
636 for ( size_t n
= 0; n
< nCount
; n
++ )
638 if ( !TreeView_DeleteItem(wxhWnd
, (HTREEITEM
)children
[n
]) )
640 wxLogLastError("TreeView_DeleteItem");
645 void wxTreeCtrl::DeleteAllItems()
647 if ( !TreeView_DeleteAllItems(wxhWnd
) )
649 wxLogLastError("TreeView_DeleteAllItems");
653 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
655 wxASSERT_MSG( flag
== TVE_COLLAPSE
|| flag
== TVE_COLLAPSERESET
||
656 flag
== TVE_EXPAND
|| flag
== TVE_TOGGLE
,
657 "Unknown flag in wxTreeCtrl::DoExpand" );
659 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
661 if ( TreeView_Expand(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
663 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
666 bool isExpanded
= IsExpanded(item
);
668 event
.SetEventObject(this);
670 // @@@ return values of {EXPAND|COLLAPS}ING event handler is discarded
671 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
672 GetEventHandler()->ProcessEvent(event
);
674 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
675 GetEventHandler()->ProcessEvent(event
);
679 // I wonder if it really ever happens...
680 wxLogDebug("TreeView_Expand: change didn't took place.");
684 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
686 DoExpand(item
, TVE_EXPAND
);
689 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
691 DoExpand(item
, TVE_COLLAPSE
);
694 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
696 DoExpand(item
, TVE_COLLAPSERESET
);
699 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
701 DoExpand(item
, TVE_TOGGLE
);
704 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
706 DoExpand(item
, action
);
709 void wxTreeCtrl::Unselect()
711 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
714 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
716 if ( !TreeView_SelectItem(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
) )
718 wxLogLastError("TreeView_SelectItem");
722 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
725 TreeView_EnsureVisible(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
);
728 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
730 if ( !TreeView_SelectSetFirstVisible(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
) )
732 wxLogLastError("TreeView_SelectSetFirstVisible");
736 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
741 void wxTreeCtrl::DeleteTextCtrl()
745 m_textCtrl
->UnsubclassWin();
746 m_textCtrl
->SetHWND(0);
752 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
753 wxClassInfo
* textControlClass
)
755 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
757 HWND hWnd
= (HWND
) TreeView_EditLabel(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
);
759 wxCHECK_MSG( hWnd
, NULL
, "Can't edit tree ctrl label" );
763 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
764 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
765 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
770 // End label editing, optionally cancelling the edit
771 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
773 TreeView_EndEditLabelNow(wxhWnd
, discardChanges
);
778 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
780 TV_HITTESTINFO hitTestInfo
;
781 hitTestInfo
.pt
.x
= (int)point
.x
;
782 hitTestInfo
.pt
.y
= (int)point
.y
;
784 TreeView_HitTest(wxhWnd
, &hitTestInfo
);
789 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
790 flags |= wxTREE_HITTEST_##flag
792 TRANSLATE_FLAG(ABOVE
);
793 TRANSLATE_FLAG(BELOW
);
794 TRANSLATE_FLAG(NOWHERE
);
795 TRANSLATE_FLAG(ONITEMBUTTON
);
796 TRANSLATE_FLAG(ONITEMICON
);
797 TRANSLATE_FLAG(ONITEMINDENT
);
798 TRANSLATE_FLAG(ONITEMLABEL
);
799 TRANSLATE_FLAG(ONITEMRIGHT
);
800 TRANSLATE_FLAG(ONITEMSTATEICON
);
801 TRANSLATE_FLAG(TOLEFT
);
802 TRANSLATE_FLAG(TORIGHT
);
804 #undef TRANSLATE_FLAG
806 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
809 // ----------------------------------------------------------------------------
811 // ----------------------------------------------------------------------------
812 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
813 wxTreeItemData
*pItem2
,
816 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
819 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
820 const wxTreeItemId
& item2
)
822 return strcmp(GetItemText(item1
), GetItemText(item2
));
825 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
827 // rely on the fact that TreeView_SortChildren does the same thing as our
828 // default behaviour, i.e. sorts items alphabetically and so call it
829 // directly if we're not in derived class (much more efficient!)
830 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
832 TreeView_SortChildren(wxhWnd
, (HTREEITEM
)(WXHTREEITEM
)item
, 0);
837 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
838 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
839 tvSort
.lParam
= (LPARAM
)this;
840 TreeView_SortChildrenCB(wxhWnd
, &tvSort
, 0 /* reserved */);
844 // ----------------------------------------------------------------------------
846 // ----------------------------------------------------------------------------
848 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
850 if ( cmd
== EN_UPDATE
)
852 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
853 event
.SetEventObject( this );
854 ProcessCommand(event
);
856 else if ( cmd
== EN_KILLFOCUS
)
858 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
859 event
.SetEventObject( this );
860 ProcessCommand(event
);
872 // process WM_NOTIFY Windows message
873 bool wxTreeCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
*result
)
875 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
876 wxEventType eventType
= wxEVT_NULL
;
877 NMHDR
*hdr
= (NMHDR
*)lParam
;
882 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
887 if ( eventType
== wxEVT_NULL
)
888 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
889 //else: left drag, already set above
891 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
893 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
894 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
898 case TVN_BEGINLABELEDIT
:
900 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
901 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
903 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
909 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
910 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
912 event
.m_item
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
916 case TVN_ENDLABELEDIT
:
918 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
919 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
921 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
925 case TVN_GETDISPINFO
:
926 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
929 case TVN_SETDISPINFO
:
931 if ( eventType
== wxEVT_NULL
)
932 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
933 //else: get, already set above
935 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
937 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
941 case TVN_ITEMEXPANDING
:
942 event
.m_code
= FALSE
;
945 case TVN_ITEMEXPANDED
:
947 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
950 switch ( tv
->action
)
961 wxLogDebug("unexpected code %d in TVN_ITEMEXPAND "
962 "message", tv
->action
);
965 bool ing
= (hdr
->code
== TVN_ITEMEXPANDING
);
966 eventType
= g_events
[expand
][ing
];
968 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
974 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
975 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
977 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
979 // a separate event for this case
980 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
982 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
984 event2
.SetEventObject(this);
986 GetEventHandler()->ProcessEvent(event2
);
992 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
995 case TVN_SELCHANGING
:
997 if ( eventType
== wxEVT_NULL
)
998 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
999 //else: already set above
1001 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1003 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1004 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1009 return wxControl::MSWNotify(wParam
, lParam
, result
);
1012 event
.SetEventObject(this);
1013 event
.SetEventType(eventType
);
1015 bool processed
= GetEventHandler()->ProcessEvent(event
);
1018 if ( hdr
->code
== TVN_DELETEITEM
)
1020 // NB: we might process this message using wxWindows event tables, but
1021 // due to overhead of wxWin event system we prefer to do it here
1022 // (otherwise deleting a tree with many items is just too slow)
1023 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1024 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1025 delete data
; // may be NULL, ok
1028 *result
= !event
.IsAllowed();
1033 // ----------------------------------------------------------------------------
1035 // ----------------------------------------------------------------------------
1037 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1039 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1040 : wxNotifyEvent(commandType
, id
)