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"
44 #include "wx/msw/gnuwin32/extra.h"
47 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
67 // Bug in headers, sometimes
69 #define TVIS_FOCUSED 0x0001
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 // a convenient wrapper around TV_ITEM struct which adds a ctor
77 struct wxTreeViewItem
: public TV_ITEM
79 wxTreeViewItem(const wxTreeItemId
& item
,
80 UINT mask_
, UINT stateMask_
= 0)
83 stateMask
= stateMask_
;
84 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 #if !USE_SHARED_LIBRARY
93 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
96 // hide the ugly cast (of course, the macro is _quite_ ugly too...)
97 #define wxhWnd ((HWND)m_hWnd)
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 // handy table for sending events
104 static const wxEventType g_events
[2][2] =
106 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
107 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
110 // ============================================================================
112 // ============================================================================
114 // ----------------------------------------------------------------------------
115 // construction and destruction
116 // ----------------------------------------------------------------------------
118 void wxTreeCtrl::Init()
120 m_imageListNormal
= NULL
;
121 m_imageListState
= NULL
;
125 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
126 const wxPoint
& pos
, const wxSize
& size
,
127 long style
, const wxValidator
& validator
,
128 const wxString
& name
)
132 wxSystemSettings settings
;
135 SetValidator(validator
);
137 m_windowStyle
= style
;
141 m_windowId
= (id
== -1) ? NewControlId() : id
;
143 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_SHOWSELALWAYS
;
147 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
149 // Even with extended styles, need to combine with WS_BORDER
150 // for them to look right.
151 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
156 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
157 wstyle
|= TVS_HASBUTTONS
;
159 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
160 wstyle
|= TVS_EDITLABELS
;
162 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
163 wstyle
|= TVS_LINESATROOT
;
165 // Create the tree control.
166 m_hWnd
= (WXHWND
)::CreateWindowEx
172 pos
.x
, pos
.y
, size
.x
, size
.y
,
173 (HWND
)parent
->GetHWND(),
179 wxCHECK_MSG( m_hWnd
, FALSE
, "Failed to create tree ctrl" );
182 parent
->AddChild(this);
189 wxTreeCtrl::~wxTreeCtrl()
193 // delete user data to prevent memory leaks
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 // simple wrappers which add error checking in debug mode
203 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
205 if ( !TreeView_GetItem(wxhWnd
, tvItem
) )
207 wxLogLastError("TreeView_GetItem");
215 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
217 if ( TreeView_SetItem(wxhWnd
, tvItem
) == -1 )
219 wxLogLastError("TreeView_SetItem");
223 size_t wxTreeCtrl::GetCount() const
225 return (size_t)TreeView_GetCount(wxhWnd
);
228 unsigned int wxTreeCtrl::GetIndent() const
230 return TreeView_GetIndent(wxhWnd
);
233 void wxTreeCtrl::SetIndent(unsigned int indent
)
235 TreeView_SetIndent(wxhWnd
, indent
);
238 wxImageList
*wxTreeCtrl::GetImageList() const
240 return m_imageListNormal
;
243 wxImageList
*wxTreeCtrl::GetStateImageList() const
245 return m_imageListNormal
;
248 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
251 TreeView_SetImageList(wxhWnd
,
252 imageList
? imageList
->GetHIMAGELIST() : 0,
256 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
258 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
261 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
263 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
266 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
272 wxArrayLong children
;
273 wxTreeItemId child
= GetFirstChild(item
, cookie
);
274 while ( child
.IsOk() )
279 result
+= GetChildrenCount(child
, TRUE
);
282 // add the child to the result in any case
285 child
= GetNextChild(item
, cookie
);
291 // ----------------------------------------------------------------------------
293 // ----------------------------------------------------------------------------
295 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
297 char buf
[512]; // the size is arbitrary...
299 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
300 tvItem
.pszText
= buf
;
301 tvItem
.cchTextMax
= WXSIZEOF(buf
);
302 if ( !DoGetItem(&tvItem
) )
304 // don't return some garbage which was on stack, but an empty string
308 return wxString(buf
);
311 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
313 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
314 tvItem
.pszText
= (char *)text
.c_str(); // conversion is ok
318 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
320 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
323 return tvItem
.iImage
;
326 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
328 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
329 tvItem
.iImage
= image
;
333 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
335 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
338 return tvItem
.iSelectedImage
;
341 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
343 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
344 tvItem
.iSelectedImage
= image
;
348 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
350 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
351 if ( !DoGetItem(&tvItem
) )
356 return (wxTreeItemData
*)tvItem
.lParam
;
359 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
361 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
362 tvItem
.lParam
= (LPARAM
)data
;
366 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
368 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
369 tvItem
.cChildren
= (int)has
;
373 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
375 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
376 tvItem
.state
= bold
? TVIS_BOLD
: 0;
380 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
382 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
383 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
387 // ----------------------------------------------------------------------------
389 // ----------------------------------------------------------------------------
391 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
393 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
395 return SendMessage(wxhWnd
, TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
399 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
401 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
404 return tvItem
.cChildren
!= 0;
407 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
409 // probably not a good idea to put it here
410 //wxASSERT( ItemHasChildren(item) );
412 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
415 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
418 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
420 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
423 return (tvItem
.state
& TVIS_SELECTED
) != 0;
426 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
428 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
431 return (tvItem
.state
& TVIS_BOLD
) != 0;
434 // ----------------------------------------------------------------------------
436 // ----------------------------------------------------------------------------
438 wxTreeItemId
wxTreeCtrl::GetRootItem() const
440 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(wxhWnd
));
443 wxTreeItemId
wxTreeCtrl::GetSelection() const
445 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(wxhWnd
));
448 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
450 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
453 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
456 // remember the last child returned in 'cookie'
457 _cookie
= (long)TreeView_GetChild(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
)item
);
459 return wxTreeItemId((WXHTREEITEM
)_cookie
);
462 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
465 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(wxhWnd
,
466 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
472 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
474 // can this be done more efficiently?
477 wxTreeItemId childLast
,
478 child
= GetFirstChild(item
, cookie
);
479 while ( child
.IsOk() )
482 child
= GetNextChild(item
, cookie
);
488 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
490 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
493 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
495 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
498 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
500 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(wxhWnd
));
503 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
505 wxASSERT_MSG( IsVisible(item
), "The item you call GetNextVisible() "
506 "for must be visible itself!");
508 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
511 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
513 wxASSERT_MSG( IsVisible(item
), "The item you call GetPrevVisible() "
514 "for must be visible itself!");
516 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
));
519 // ----------------------------------------------------------------------------
521 // ----------------------------------------------------------------------------
523 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
524 wxTreeItemId hInsertAfter
,
525 const wxString
& text
,
526 int image
, int selectedImage
,
527 wxTreeItemData
*data
)
529 TV_INSERTSTRUCT tvIns
;
530 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
531 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
533 // This is how we insert the item as the first child: supply a NULL hInsertAfter
534 if (tvIns
.hInsertAfter
== (HTREEITEM
) 0)
536 tvIns
.hInsertAfter
= TVI_FIRST
;
540 if ( !text
.IsEmpty() )
543 tvIns
.item
.pszText
= (char *)text
.c_str(); // cast is ok
549 tvIns
.item
.iImage
= image
;
551 if ( selectedImage
== -1 )
553 // take the same image for selected icon if not specified
554 selectedImage
= image
;
558 if ( selectedImage
!= -1 )
560 mask
|= TVIF_SELECTEDIMAGE
;
561 tvIns
.item
.iSelectedImage
= selectedImage
;
567 tvIns
.item
.lParam
= (LPARAM
)data
;
570 tvIns
.item
.mask
= mask
;
572 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(wxhWnd
, &tvIns
);
575 wxLogLastError("TreeView_InsertItem");
580 // associate the application tree item with Win32 tree item handle
581 data
->SetId((WXHTREEITEM
)id
);
584 return wxTreeItemId((WXHTREEITEM
)id
);
587 // for compatibility only
588 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
589 const wxString
& text
,
590 int image
, int selImage
,
593 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
594 image
, selImage
, NULL
);
597 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
598 int image
, int selectedImage
,
599 wxTreeItemData
*data
)
601 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
602 text
, image
, selectedImage
, data
);
605 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
606 const wxString
& text
,
607 int image
, int selectedImage
,
608 wxTreeItemData
*data
)
610 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
611 text
, image
, selectedImage
, data
);
614 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
615 const wxTreeItemId
& idPrevious
,
616 const wxString
& text
,
617 int image
, int selectedImage
,
618 wxTreeItemData
*data
)
620 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
623 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
624 const wxString
& text
,
625 int image
, int selectedImage
,
626 wxTreeItemData
*data
)
628 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
629 text
, image
, selectedImage
, data
);
632 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
634 if ( !TreeView_DeleteItem(wxhWnd
, (HTREEITEM
)(WXHTREEITEM
)item
) )
636 wxLogLastError("TreeView_DeleteItem");
640 // delete all children (but don't delete the item itself)
641 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
645 wxArrayLong children
;
646 wxTreeItemId child
= GetFirstChild(item
, cookie
);
647 while ( child
.IsOk() )
649 children
.Add((long)(WXHTREEITEM
)child
);
651 child
= GetNextChild(item
, cookie
);
654 size_t nCount
= children
.Count();
655 for ( size_t n
= 0; n
< nCount
; n
++ )
657 if ( !TreeView_DeleteItem(wxhWnd
, (HTREEITEM
)children
[n
]) )
659 wxLogLastError("TreeView_DeleteItem");
664 void wxTreeCtrl::DeleteAllItems()
666 if ( !TreeView_DeleteAllItems(wxhWnd
) )
668 wxLogLastError("TreeView_DeleteAllItems");
672 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
674 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
675 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
676 flag
== TVE_EXPAND
||
678 "Unknown flag in wxTreeCtrl::DoExpand" );
680 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
682 if ( TreeView_Expand(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
684 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
687 bool isExpanded
= IsExpanded(item
);
689 event
.SetEventObject(this);
691 // @@@ return values of {EXPAND|COLLAPS}ING event handler is discarded
692 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
693 GetEventHandler()->ProcessEvent(event
);
695 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
696 GetEventHandler()->ProcessEvent(event
);
700 // I wonder if it really ever happens...
701 wxLogDebug("TreeView_Expand: change didn't took place.");
705 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
707 DoExpand(item
, TVE_EXPAND
);
710 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
712 DoExpand(item
, TVE_COLLAPSE
);
715 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
717 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
720 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
722 DoExpand(item
, TVE_TOGGLE
);
725 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
727 DoExpand(item
, action
);
730 void wxTreeCtrl::Unselect()
732 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
735 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
737 if ( !TreeView_SelectItem(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
) )
739 wxLogLastError("TreeView_SelectItem");
743 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
746 TreeView_EnsureVisible(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
);
749 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
751 if ( !TreeView_SelectSetFirstVisible(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
) )
753 wxLogLastError("TreeView_SelectSetFirstVisible");
757 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
762 void wxTreeCtrl::DeleteTextCtrl()
766 m_textCtrl
->UnsubclassWin();
767 m_textCtrl
->SetHWND(0);
773 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
774 wxClassInfo
* textControlClass
)
776 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
778 HWND hWnd
= (HWND
) TreeView_EditLabel(wxhWnd
, (HTREEITEM
) (WXHTREEITEM
) item
);
780 wxCHECK_MSG( hWnd
, NULL
, "Can't edit tree ctrl label" );
784 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
785 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
786 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
791 // End label editing, optionally cancelling the edit
792 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
794 TreeView_EndEditLabelNow(wxhWnd
, discardChanges
);
799 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
801 TV_HITTESTINFO hitTestInfo
;
802 hitTestInfo
.pt
.x
= (int)point
.x
;
803 hitTestInfo
.pt
.y
= (int)point
.y
;
805 TreeView_HitTest(wxhWnd
, &hitTestInfo
);
810 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
811 flags |= wxTREE_HITTEST_##flag
813 TRANSLATE_FLAG(ABOVE
);
814 TRANSLATE_FLAG(BELOW
);
815 TRANSLATE_FLAG(NOWHERE
);
816 TRANSLATE_FLAG(ONITEMBUTTON
);
817 TRANSLATE_FLAG(ONITEMICON
);
818 TRANSLATE_FLAG(ONITEMINDENT
);
819 TRANSLATE_FLAG(ONITEMLABEL
);
820 TRANSLATE_FLAG(ONITEMRIGHT
);
821 TRANSLATE_FLAG(ONITEMSTATEICON
);
822 TRANSLATE_FLAG(TOLEFT
);
823 TRANSLATE_FLAG(TORIGHT
);
825 #undef TRANSLATE_FLAG
827 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
830 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
835 if ( TreeView_GetItemRect(wxhWnd
, (HTREEITEM
)(WXHTREEITEM
)item
,
838 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
844 // couldn't retrieve rect: for example, item isn't visible
849 // ----------------------------------------------------------------------------
851 // ----------------------------------------------------------------------------
853 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
854 wxTreeItemData
*pItem2
,
857 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
860 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
861 const wxTreeItemId
& item2
)
863 return strcmp(GetItemText(item1
), GetItemText(item2
));
866 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
868 // rely on the fact that TreeView_SortChildren does the same thing as our
869 // default behaviour, i.e. sorts items alphabetically and so call it
870 // directly if we're not in derived class (much more efficient!)
871 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
873 TreeView_SortChildren(wxhWnd
, (HTREEITEM
)(WXHTREEITEM
)item
, 0);
878 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
879 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
880 tvSort
.lParam
= (LPARAM
)this;
881 TreeView_SortChildrenCB(wxhWnd
, &tvSort
, 0 /* reserved */);
885 // ----------------------------------------------------------------------------
887 // ----------------------------------------------------------------------------
889 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
891 if ( cmd
== EN_UPDATE
)
893 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
894 event
.SetEventObject( this );
895 ProcessCommand(event
);
897 else if ( cmd
== EN_KILLFOCUS
)
899 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
900 event
.SetEventObject( this );
901 ProcessCommand(event
);
913 // process WM_NOTIFY Windows message
914 bool wxTreeCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
*result
)
916 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
917 wxEventType eventType
= wxEVT_NULL
;
918 NMHDR
*hdr
= (NMHDR
*)lParam
;
923 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
928 if ( eventType
== wxEVT_NULL
)
929 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
930 //else: left drag, already set above
932 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
934 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
935 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
939 case TVN_BEGINLABELEDIT
:
941 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
942 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
944 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
950 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
951 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
953 event
.m_item
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
957 case TVN_ENDLABELEDIT
:
959 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
960 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
962 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
966 case TVN_GETDISPINFO
:
967 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
970 case TVN_SETDISPINFO
:
972 if ( eventType
== wxEVT_NULL
)
973 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
974 //else: get, already set above
976 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
978 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
982 case TVN_ITEMEXPANDING
:
983 event
.m_code
= FALSE
;
986 case TVN_ITEMEXPANDED
:
988 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
991 switch ( tv
->action
)
1002 wxLogDebug("unexpected code %d in TVN_ITEMEXPAND "
1003 "message", tv
->action
);
1006 bool ing
= (hdr
->code
== TVN_ITEMEXPANDING
);
1007 eventType
= g_events
[expand
][ing
];
1009 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1015 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1016 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1018 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1020 // a separate event for this case
1021 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1023 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1025 event2
.SetEventObject(this);
1027 GetEventHandler()->ProcessEvent(event2
);
1032 case TVN_SELCHANGED
:
1033 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1036 case TVN_SELCHANGING
:
1038 if ( eventType
== wxEVT_NULL
)
1039 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1040 //else: already set above
1042 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1044 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1045 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1050 return wxControl::MSWNotify(wParam
, lParam
, result
);
1053 event
.SetEventObject(this);
1054 event
.SetEventType(eventType
);
1056 bool processed
= GetEventHandler()->ProcessEvent(event
);
1059 if ( hdr
->code
== TVN_DELETEITEM
)
1061 // NB: we might process this message using wxWindows event tables, but
1062 // due to overhead of wxWin event system we prefer to do it here
1063 // (otherwise deleting a tree with many items is just too slow)
1064 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1065 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1066 delete data
; // may be NULL, ok
1067 processed
= TRUE
; // Make sure we don't get called twice
1070 *result
= !event
.IsAllowed();
1075 // ----------------------------------------------------------------------------
1077 // ----------------------------------------------------------------------------
1079 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1081 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1082 : wxNotifyEvent(commandType
, id
)